summaryrefslogtreecommitdiff
path: root/Build/source/libs/gd/libgd-src/tests/gdimagecrop/bug00485_threshold.c
blob: 31ac38f1577e1838995b183260fc63b26811e3bf (plain)
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
/**
 * Test that gdImageCropThreshold() works pixel precise
 *
 * We test that a single black pixel anywhere on an 8x8 pixel image with a white
 * background is threshold-cropped to a 1x1 pixel image with a black pixel.
 *
 * See <https://github.com/libgd/libgd/issues/485>
 */


#include "gd.h"
#include "gdtest.h"


gdImagePtr createWhiteImageWithBlackPixelAt(int x, int y)
{
    gdImagePtr im = gdImageCreateTrueColor(8, 8);
    gdImageFilledRectangle(im, 0, 0, 7, 7, 0xffffff);
    gdImageSetPixel(im, x, y, 0x000000);
    return im;
}


int main()
{
    int x, y, height, width, color;
    gdImagePtr orig, cropped;

    for (y = 0; y < 8; y++) {
        for (x = 0; x < 8; x++) {
            orig = createWhiteImageWithBlackPixelAt(x, y);
            cropped = gdImageCropThreshold(orig, 0xffffff, 1.0);
            gdTestAssertMsg(cropped != NULL, "Pixel at %d, %d: unexpected NULL crop\n", x, y);
            if (cropped) {
                width = gdImageSX(cropped);
                gdTestAssertMsg(width == 1, "Pixel at %d, %d: unexpected width (%d)\n", x, y, width);
                height = gdImageSY(cropped);
                gdTestAssertMsg(height == 1, "Pixel at %d, %d: unexpected height (%d)\n", x, y, height);
                color = gdImageGetPixel(cropped, 0, 0);
                gdTestAssertMsg(color == 0x000000, "Pixel at %d, %d: unexpected color (%d)\n", x, y, color);
                gdImageDestroy(cropped);
            }
            gdImageDestroy(orig);
        }
    }

    return gdNumFailures();
}