File: exact_alpha.c

package info (click to toggle)
libgd2 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 10,360 kB
  • sloc: ansic: 48,194; sh: 5,603; cpp: 1,298; makefile: 323; perl: 225; tcl: 45
file content (67 lines) | stat: -rw-r--r-- 1,915 bytes parent folder | download | duplicates (3)
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
/* Test for exact colors of gdImageCopyResampled */

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


#define EXP_RED    66
#define EXP_GREEN  66
#define EXP_BLUE  133
#define EXP_ALPHA  32


int main()
{
	gdImagePtr im, copy;
	int solid, transparent, color;
	int red, green, blue, alpha;
	int i, j;

	/* create the source image */
	im = gdImageCreateTrueColor(10, 10);
	gdImageAlphaBlending(im, 0);
	solid = gdImageColorAllocate(im, 0, 100, 150);
	transparent = gdImageColorAllocateAlpha(im, 200, 0, 100, 64);

	/* draw a checker pattern */
	for (i = 0; i < gdImageSX(im); i++) {
		for (j = 0; j < gdImageSY(im); j++) {
			gdImageSetPixel(im, i, j, (i%2 != j%2 ? solid : transparent));
		}
	}

	/* create the destination image */
	copy = gdImageCreateTrueColor(5, 5);
	gdImageAlphaBlending(copy, 0);
	gdImageSaveAlpha(copy, 1);
	gdImageCopyResampled(copy, im, 0,0, 0,0, 5,5, 10, 10);

	/* assert all pixels have the same color */
	color = gdImageTrueColorPixel(copy, 3, 3);
	for (i = 0; i < gdImageSX(copy); i++) {
		for (j = 0; j < gdImageSY(copy); j++) {
			gdTestAssert(gdImageTrueColorPixel(copy, i, j) == color);
		}
	}

	/* assign actual component values */
	red = gdTrueColorGetRed(color);
	green = gdTrueColorGetGreen(color);
	blue = gdTrueColorGetBlue(color);
	alpha = gdTrueColorGetAlpha(color);

	/* test for expected component values */
	gdTestAssertMsg(red >= EXP_RED - 1 && red <= EXP_RED + 1,
					"red: expected roughly %d, got %d\n", EXP_RED, red);
	gdTestAssertMsg(green >= EXP_GREEN - 1 && green <= EXP_GREEN + 1,
					"green: expected roughly %d, got %d\n", EXP_GREEN, green);
	gdTestAssertMsg(blue >= EXP_BLUE - 1 && blue <= EXP_BLUE + 1,
					"blue: expected roughly %d, got %d\n", EXP_BLUE, blue);
	gdTestAssertMsg(alpha >= EXP_ALPHA - 1 && alpha <= EXP_ALPHA + 1,
					"alpha: expected roughly %d, got %d\n", EXP_ALPHA, alpha);

	gdImageDestroy(copy);
	gdImageDestroy(im);

	return gdNumFailures();
}