Pillow
63 строки · 1.3 Кб
1/*
2* The Python Imaging Library
3* $Id$
4*
5* cut region from image
6*
7* history:
8* 95-11-27 fl Created
9* 98-07-10 fl Fixed "null result" error
10* 99-02-05 fl Rewritten to use Paste primitive
11*
12* Copyright (c) Secret Labs AB 1997-99.
13* Copyright (c) Fredrik Lundh 1995.
14*
15* See the README file for information on usage and redistribution.
16*/
17
18#include "Imaging.h"19
20Imaging
21ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1) {22Imaging imOut;23int xsize, ysize;24int dx0, dy0, dx1, dy1;25INT32 zero = 0;26
27if (!imIn) {28return (Imaging)ImagingError_ModeError();29}30
31xsize = sx1 - sx0;32if (xsize < 0) {33xsize = 0;34}35ysize = sy1 - sy0;36if (ysize < 0) {37ysize = 0;38}39
40imOut = ImagingNewDirty(imIn->mode, xsize, ysize);41if (!imOut) {42return NULL;43}44
45ImagingCopyPalette(imOut, imIn);46
47if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize) {48(void)ImagingFill(imOut, &zero);49}50
51dx0 = -sx0;52dy0 = -sy0;53dx1 = imIn->xsize - sx0;54dy1 = imIn->ysize - sy0;55
56/* paste the source image on top of the output image!!! */57if (ImagingPaste(imOut, imIn, NULL, dx0, dy0, dx1, dy1) < 0) {58ImagingDelete(imOut);59return NULL;60}61
62return imOut;63}
64