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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include <stdio.h>
#include "gd.h"
#include "gdtest.h"
/* Test gdImageScale() with bicubic interpolation on a simple
* all-white image. */
gdImagePtr mkwhite(int x, int y)
{
gdImagePtr im;
im = gdImageCreateTrueColor(x, y);
gdImageFilledRectangle(im, 0, 0, x-1, y-1,
gdImageColorExactAlpha(im, 255, 255, 255, 0));
gdTestAssert(im != NULL);
gdImageSetInterpolationMethod(im, GD_BICUBIC); // FP interp'n
return im;
}/* mkwhite*/
/* Fill with almost-black. */
void mkblack(gdImagePtr ptr)
{
gdImageFilledRectangle(ptr, 0, 0, ptr->sx - 1, ptr->sy - 1,
gdImageColorExactAlpha(ptr, 2, 2, 2, 0));
}/* mkblack*/
#define CLOSE_ENOUGH 15
void scaletest(int x, int y, int nx, int ny)
{
gdImagePtr im, imref, tmp, same;
imref = mkwhite(x, y);
im = mkwhite(x, y);
tmp = gdImageScale(im, nx, ny);
same = gdImageScale(tmp, x, y);
/* Test the result to insure that it's close enough to the
* original. */
gdTestAssert(gdMaxPixelDiff(im, same) < CLOSE_ENOUGH);
/* Modify the original and test for a change again. (I.e. test
* for accidentally shared memory.) */
mkblack(tmp);
gdTestAssert(gdMaxPixelDiff(imref, same) < CLOSE_ENOUGH);
gdImageDestroy(im);
gdImageDestroy(tmp);
gdImageDestroy(same);
}/* scaletest*/
void do_test(int x, int y, int nx, int ny)
{
gdImagePtr im, imref, tmp;
gdImagePtr same, same2;
im = mkwhite(x, y);
imref = mkwhite(x, y);
same = gdImageScale(im, x, y);
/* Trivial resize should be a straight copy. */
gdTestAssert(im != same);
gdTestAssert(gdMaxPixelDiff(im, same) == 0);
gdTestAssert(gdMaxPixelDiff(imref, same) == 0);
/* Ensure that modifying im doesn't modify same (i.e. see if we
* can catch them accidentally sharing the same pixel buffer.) */
mkblack(im);
gdTestAssert(gdMaxPixelDiff(imref, same) == 0);
gdImageDestroy(same);
gdImageDestroy(im);
/* Scale horizontally, vertically and both. */
scaletest(x, y, nx, y);
scaletest(x, y, x, ny);
scaletest(x, y, nx, ny);
}
int main(int argc, char **argv)
{
do_test(300, 300, 600, 600);
do_test(3200, 2133, 640, 427);
return gdNumFailures();
}
|