gdtest.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include <stdio.h>
  2. #ifdef _WIN32
  3. #include <process.h>
  4. int
  5. unlink (const char *filename)
  6. {
  7. return _unlink (filename);
  8. }
  9. #else
  10. #include <unistd.h> /* for getpid(), unlink() */
  11. #endif
  12. #include "gd.h"
  13. void CompareImages (char *msg, gdImagePtr im1, gdImagePtr im2);
  14. static int freadWrapper (void *context, char *buf, int len);
  15. static int fwriteWrapper (void *context, const char *buffer, int len);
  16. int
  17. main (int argc, char **argv)
  18. {
  19. gdImagePtr im, ref, im2, im3;
  20. FILE *in, *out;
  21. void *iptr;
  22. int sz;
  23. gdIOCtxPtr ctx;
  24. char of[256];
  25. int colRed, colBlu;
  26. gdSource imgsrc;
  27. gdSink imgsnk;
  28. int foreground;
  29. int i;
  30. if (argc != 2)
  31. {
  32. fprintf (stderr, "Usage: gdtest filename.png\n");
  33. exit (1);
  34. }
  35. in = fopen (argv[1], "rb");
  36. if (!in)
  37. {
  38. fprintf (stderr, "Input file does not exist!\n");
  39. exit (1);
  40. }
  41. im = gdImageCreateFromPng (in);
  42. rewind (in);
  43. ref = gdImageCreateFromPng (in);
  44. fclose (in);
  45. printf ("Reference File has %d Palette entries\n", ref->colorsTotal);
  46. CompareImages ("Initial Versions", ref, im);
  47. /* */
  48. /* Send to PNG File then Ptr */
  49. /* */
  50. snprintf (of, sizeof(of), "%s.png", argv[1]);
  51. out = fopen (of, "wb");
  52. gdImagePng (im, out);
  53. fclose (out);
  54. in = fopen (of, "rb");
  55. if (!in)
  56. {
  57. fprintf (stderr, "PNG Output file does not exist!\n");
  58. exit (1);
  59. }
  60. im2 = gdImageCreateFromPng (in);
  61. fclose (in);
  62. CompareImages ("GD->PNG File->GD", ref, im2);
  63. unlink (of);
  64. gdImageDestroy (im2);
  65. iptr = gdImagePngPtr (im, &sz);
  66. ctx = gdNewDynamicCtx (sz, iptr);
  67. im2 = gdImageCreateFromPngCtx (ctx);
  68. CompareImages ("GD->PNG ptr->GD", ref, im2);
  69. gdImageDestroy (im2);
  70. ctx->gd_free (ctx);
  71. /* */
  72. /* Send to GD2 File then Ptr */
  73. /* */
  74. snprintf (of, sizeof(of), "%s.gd2", argv[1]);
  75. out = fopen (of, "wb");
  76. gdImageGd2 (im, out, 128, 2);
  77. fclose (out);
  78. in = fopen (of, "rb");
  79. if (!in)
  80. {
  81. fprintf (stderr, "GD2 Output file does not exist!\n");
  82. exit (1);
  83. }
  84. im2 = gdImageCreateFromGd2 (in);
  85. fclose (in);
  86. CompareImages ("GD->GD2 File->GD", ref, im2);
  87. unlink (of);
  88. gdImageDestroy (im2);
  89. iptr = gdImageGd2Ptr (im, 128, 2, &sz);
  90. /*printf("Got ptr %d (size %d)\n",iptr, sz); */
  91. ctx = gdNewDynamicCtx (sz, iptr);
  92. /*printf("Got ctx %d\n",ctx); */
  93. im2 = gdImageCreateFromGd2Ctx (ctx);
  94. /*printf("Got img2 %d\n",im2); */
  95. CompareImages ("GD->GD2 ptr->GD", ref, im2);
  96. gdImageDestroy (im2);
  97. ctx->gd_free (ctx);
  98. /* */
  99. /* Send to GD File then Ptr */
  100. /* */
  101. snprintf (of, sizeof(of), "%s.gd", argv[1]);
  102. out = fopen (of, "wb");
  103. gdImageGd (im, out);
  104. fclose (out);
  105. in = fopen (of, "rb");
  106. if (!in)
  107. {
  108. fprintf (stderr, "GD Output file does not exist!\n");
  109. exit (1);
  110. }
  111. im2 = gdImageCreateFromGd (in);
  112. fclose (in);
  113. CompareImages ("GD->GD File->GD", ref, im2);
  114. unlink (of);
  115. gdImageDestroy (im2);
  116. iptr = gdImageGdPtr (im, &sz);
  117. /*printf("Got ptr %d (size %d)\n",iptr, sz); */
  118. ctx = gdNewDynamicCtx (sz, iptr);
  119. /*printf("Got ctx %d\n",ctx); */
  120. im2 = gdImageCreateFromGdCtx (ctx);
  121. /*printf("Got img2 %d\n",im2); */
  122. CompareImages ("GD->GD ptr->GD", ref, im2);
  123. gdImageDestroy (im2);
  124. ctx->gd_free (ctx);
  125. /*
  126. ** Test gdImageCreateFromPngSource'
  127. * */
  128. in = fopen (argv[1], "rb");
  129. imgsrc.source = freadWrapper;
  130. imgsrc.context = in;
  131. im2 = gdImageCreateFromPngSource (&imgsrc);
  132. fclose (in);
  133. if (im2 == NULL)
  134. {
  135. printf ("GD Source: ERROR Null returned by gdImageCreateFromPngSource\n");
  136. }
  137. else
  138. {
  139. CompareImages ("GD Source", ref, im2);
  140. gdImageDestroy (im2);
  141. };
  142. /*
  143. ** Test gdImagePngToSink'
  144. * */
  145. snprintf (of, sizeof(of), "%s.snk", argv[1]);
  146. out = fopen (of, "wb");
  147. imgsnk.sink = fwriteWrapper;
  148. imgsnk.context = out;
  149. gdImagePngToSink (im, &imgsnk);
  150. fclose (out);
  151. in = fopen (of, "rb");
  152. if (!in)
  153. {
  154. fprintf (stderr, "GD Sink: ERROR - GD Sink Output file does not exist!\n");
  155. }
  156. else
  157. {
  158. im2 = gdImageCreateFromPng (in);
  159. fclose (in);
  160. CompareImages ("GD Sink", ref, im2);
  161. gdImageDestroy (im2);
  162. };
  163. unlink (of);
  164. /* */
  165. /* Test Extraction */
  166. /* */
  167. in = fopen ("test/gdtest_200_300_150_100.png", "rb");
  168. if (!in)
  169. {
  170. fprintf (stderr, "gdtest_200_300_150_100.png does not exist!\n");
  171. exit (1);
  172. }
  173. im2 = gdImageCreateFromPng (in);
  174. fclose (in);
  175. in = fopen ("test/gdtest.gd2", "rb");
  176. if (!in)
  177. {
  178. fprintf (stderr, "gdtest.gd2 does not exist!\n");
  179. exit (1);
  180. }
  181. im3 = gdImageCreateFromGd2Part (in, 200, 300, 150, 100);
  182. fclose (in);
  183. CompareImages ("GD2Part (gdtest_200_300_150_100.png, gdtest.gd2(part))", im2, im3);
  184. gdImageDestroy (im2);
  185. gdImageDestroy (im3);
  186. /* */
  187. /* Copy Blend */
  188. /* */
  189. in = fopen ("test/gdtest.png", "rb");
  190. if (!in)
  191. {
  192. fprintf (stderr, "gdtest.png does not exist!\n");
  193. exit (1);
  194. }
  195. im2 = gdImageCreateFromPng (in);
  196. fclose (in);
  197. im3 = gdImageCreate (100, 60);
  198. colRed = gdImageColorAllocate (im3, 255, 0, 0);
  199. colBlu = gdImageColorAllocate (im3, 0, 0, 255);
  200. gdImageFilledRectangle (im3, 0, 0, 49, 30, colRed);
  201. gdImageFilledRectangle (im3, 50, 30, 99, 59, colBlu);
  202. gdImageCopyMerge (im2, im3, 150, 200, 10, 10, 90, 50, 50);
  203. gdImageCopyMerge (im2, im3, 180, 70, 10, 10, 90, 50, 50);
  204. gdImageCopyMergeGray (im2, im3, 250, 160, 10, 10, 90, 50, 50);
  205. gdImageCopyMergeGray (im2, im3, 80, 70, 10, 10, 90, 50, 50);
  206. gdImageDestroy (im3);
  207. in = fopen ("test/gdtest_merge.png", "rb");
  208. if (!in)
  209. {
  210. fprintf (stderr, "gdtest_merge.png does not exist!\n");
  211. exit (1);
  212. }
  213. im3 = gdImageCreateFromPng (in);
  214. fclose (in);
  215. printf ("[Merged Image has %d colours]\n", im2->colorsTotal);
  216. CompareImages ("Merged (gdtest.png, gdtest_merge.png)", im2, im3);
  217. gdImageDestroy (im2);
  218. gdImageDestroy (im3);
  219. #ifdef HAVE_JPEG
  220. out = fopen ("test/gdtest.jpg", "wb");
  221. if (!out)
  222. {
  223. fprintf (stderr, "Can't create file test/gdtest.jpg.\n");
  224. exit (1);
  225. }
  226. gdImageJpeg (im, out, -1);
  227. fclose (out);
  228. in = fopen ("test/gdtest.jpg", "rb");
  229. if (!in)
  230. {
  231. fprintf (stderr, "Can't open file test/gdtest.jpg.\n");
  232. exit (1);
  233. }
  234. im2 = gdImageCreateFromJpeg (in);
  235. fclose (in);
  236. if (!im2)
  237. {
  238. fprintf (stderr, "gdImageCreateFromJpeg failed.\n");
  239. exit (1);
  240. }
  241. gdImageDestroy (im2);
  242. printf ("Created test/gdtest.jpg successfully. Compare this image\n"
  243. "to the input image manually. Some difference must be\n"
  244. "expected as JPEG is a lossy file format.\n");
  245. #endif /* HAVE_JPEG */
  246. /* Assume the color closest to black is the foreground
  247. color for the B&W wbmp image. */
  248. fprintf (stderr, "NOTE: the WBMP output image will NOT match the original unless the original\n"
  249. "is also black and white. This is OK!\n");
  250. foreground = gdImageColorClosest (im, 0, 0, 0);
  251. fprintf (stderr, "Foreground index is %d\n", foreground);
  252. if (foreground == -1)
  253. {
  254. fprintf (stderr, "Source image has no colors, skipping wbmp test.\n");
  255. }
  256. else
  257. {
  258. out = fopen ("test/gdtest.wbmp", "wb");
  259. if (!out)
  260. {
  261. fprintf (stderr, "Can't create file test/gdtest.wbmp.\n");
  262. exit (1);
  263. }
  264. gdImageWBMP (im, foreground, out);
  265. fclose (out);
  266. in = fopen ("test/gdtest.wbmp", "rb");
  267. if (!in)
  268. {
  269. fprintf (stderr, "Can't open file test/gdtest.wbmp.\n");
  270. exit (1);
  271. }
  272. im2 = gdImageCreateFromWBMP (in);
  273. fprintf (stderr, "WBMP has %d colors\n", gdImageColorsTotal (im2));
  274. fprintf (stderr, "WBMP colors are:\n");
  275. for (i = 0; (i < gdImageColorsTotal (im2)); i++)
  276. {
  277. fprintf (stderr, "%02X%02X%02X\n",
  278. gdImageRed (im2, i),
  279. gdImageGreen (im2, i),
  280. gdImageBlue (im2, i));
  281. }
  282. fclose (in);
  283. if (!im2)
  284. {
  285. fprintf (stderr, "gdImageCreateFromWBMP failed.\n");
  286. exit (1);
  287. }
  288. CompareImages ("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
  289. out = fopen ("test/gdtest_wbmp_to_png.png", "wb");
  290. if (!out)
  291. {
  292. fprintf (stderr, "Can't create file test/gdtest_wbmp_to_png.png.\n");
  293. exit (1);
  294. }
  295. gdImagePng (im2, out);
  296. fclose (out);
  297. gdImageDestroy (im2);
  298. }
  299. gdImageDestroy (im);
  300. gdImageDestroy (ref);
  301. return 0;
  302. }
  303. void
  304. CompareImages (char *msg, gdImagePtr im1, gdImagePtr im2)
  305. {
  306. int cmpRes;
  307. cmpRes = gdImageCompare (im1, im2);
  308. if (cmpRes & GD_CMP_IMAGE)
  309. {
  310. printf ("%%%s: ERROR images differ: BAD\n", msg);
  311. }
  312. else if (cmpRes != 0)
  313. {
  314. printf ("%%%s: WARNING images differ: WARNING - Probably OK\n", msg);
  315. }
  316. else
  317. {
  318. printf ("%%%s: OK\n", msg);
  319. return;
  320. }
  321. if (cmpRes & (GD_CMP_SIZE_X + GD_CMP_SIZE_Y))
  322. {
  323. printf ("-%s: INFO image sizes differ\n", msg);
  324. }
  325. if (cmpRes & GD_CMP_NUM_COLORS)
  326. {
  327. printf ("-%s: INFO number of palette entries differ %d Vs. %d\n", msg,
  328. im1->colorsTotal, im2->colorsTotal);
  329. }
  330. if (cmpRes & GD_CMP_COLOR)
  331. {
  332. printf ("-%s: INFO actual colours of pixels differ\n", msg);
  333. }
  334. }
  335. static int
  336. freadWrapper (void *context, char *buf, int len)
  337. {
  338. int got = fread (buf, 1, len, (FILE *) context);
  339. return got;
  340. }
  341. static int
  342. fwriteWrapper (void *context, const char *buffer, int len)
  343. {
  344. return fwrite (buffer, 1, len, (FILE *) context);
  345. }