Pillow/src/libImaging/Crop.c

65 lines
1.3 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* cut region from image
*
* history:
2020-05-01 15:08:57 +03:00
* 95-11-27 fl Created
* 98-07-10 fl Fixed "null result" error
* 99-02-05 fl Rewritten to use Paste primitive
2010-07-31 06:52:47 +04:00
*
* Copyright (c) Secret Labs AB 1997-99.
* Copyright (c) Fredrik Lundh 1995.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
Imaging
ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1)
{
Imaging imOut;
int xsize, ysize;
int dx0, dy0, dx1, dy1;
INT32 zero = 0;
2020-05-10 12:56:36 +03:00
if (!imIn) {
2020-05-01 15:08:57 +03:00
return (Imaging) ImagingError_ModeError();
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
xsize = sx1 - sx0;
if (xsize < 0)
xsize = 0;
ysize = sy1 - sy0;
if (ysize < 0)
ysize = 0;
2017-08-06 15:01:17 +03:00
imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
2020-05-10 12:56:36 +03:00
if (!imOut) {
2020-05-01 15:08:57 +03:00
return NULL;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
ImagingCopyPalette(imOut, imIn);
2010-07-31 06:52:47 +04:00
2020-05-10 12:56:36 +03:00
if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize) {
2020-05-01 15:08:57 +03:00
(void) ImagingFill(imOut, &zero);
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
dx0 = -sx0;
dy0 = -sy0;
dx1 = imIn->xsize - sx0;
dy1 = imIn->ysize - sy0;
/* paste the source image on top of the output image!!! */
if (ImagingPaste(imOut, imIn, NULL, dx0, dy0, dx1, dy1) < 0) {
ImagingDelete(imOut);
return NULL;
}
return imOut;
}