aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py2/libImaging/Crop.c
blob: 4407c1b1d2d6a249663613a36d001d0d2692575f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * The Python Imaging Library
 * $Id$
 *
 * cut region from image
 *
 * history:
 * 95-11-27 fl	Created
 * 98-07-10 fl	Fixed "null result" error
 * 99-02-05 fl	Rewritten to use Paste primitive
 *
 * 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;

    if (!imIn)
	return (Imaging) ImagingError_ModeError();

    xsize = sx1 - sx0;
    if (xsize < 0)
        xsize = 0;
    ysize = sy1 - sy0;
    if (ysize < 0)
        ysize = 0;

    imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
    if (!imOut)
	return NULL;

    ImagingCopyPalette(imOut, imIn);

    if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize)
	(void) ImagingFill(imOut, &zero);

    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;
}