testac.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdio.h>
  2. #include "gd.h"
  3. /* If palette is true, we convert from truecolor to palette at the end,
  4. to test gdImageTrueColorToPalette and see file size/
  5. quality tradeoffs. */
  6. void testDrawing (
  7. gdImagePtr im_in,
  8. double scale,
  9. int blending,
  10. int palette,
  11. char *filename);
  12. int
  13. main (int argc, char *argv[])
  14. {
  15. /* Input and output files */
  16. FILE *in;
  17. FILE *out;
  18. /* Input image */
  19. gdImagePtr im_in = 0;
  20. /* Colors */
  21. int lightBlue;
  22. if (argc != 2)
  23. {
  24. fprintf (stderr, "Usage: testac filename.png\n");
  25. exit (1);
  26. }
  27. /* Load original PNG, which should contain alpha channel
  28. information. We will use it in two ways: preserving it
  29. literally, for use with compatible browsers, and
  30. compositing it ourselves against a background of our
  31. choosing (alpha blending). We'll change its size
  32. and try creating palette versions of it. */
  33. in = fopen (argv[1], "rb");
  34. if (!in)
  35. {
  36. fprintf (stderr, "Can't load %s.\n", argv[1]);
  37. exit (1);
  38. }
  39. else
  40. {
  41. im_in = gdImageCreateFromPng (in);
  42. fclose (in);
  43. }
  44. testDrawing (im_in, 1.0, 0, 0, "noblending-fullsize-truecolor.png");
  45. testDrawing (im_in, 1.0, 1, 0, "blending-fullsize-truecolor.png");
  46. testDrawing (im_in, 0.5, 0, 0, "noblending-halfsize-truecolor.png");
  47. testDrawing (im_in, 0.5, 1, 0, "blending-halfsize-truecolor.png");
  48. testDrawing (im_in, 2.0, 0, 0, "noblending-doublesize-truecolor.png");
  49. testDrawing (im_in, 2.0, 1, 0, "blending-doublesize-truecolor.png");
  50. testDrawing (im_in, 1.0, 0, 1, "noblending-fullsize-palette.png");
  51. testDrawing (im_in, 1.0, 1, 1, "blending-fullsize-palette.png");
  52. testDrawing (im_in, 0.5, 0, 1, "noblending-halfsize-palette.png");
  53. testDrawing (im_in, 0.5, 1, 1, "blending-halfsize-palette.png");
  54. testDrawing (im_in, 2.0, 0, 1, "noblending-doublesize-palette.png");
  55. testDrawing (im_in, 2.0, 1, 1, "blending-doublesize-palette.png");
  56. gdImageDestroy (im_in);
  57. return 0;
  58. }
  59. /* If palette is true, we convert from truecolor to palette at the end,
  60. to test gdImageTrueColorToPalette and see file size/
  61. quality tradeoffs. */
  62. void
  63. testDrawing (
  64. gdImagePtr im_in,
  65. double scale,
  66. int blending,
  67. int palette,
  68. char *filename)
  69. {
  70. gdImagePtr im_out;
  71. FILE *out;
  72. /* Create output image. */
  73. im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale),
  74. (int) (gdImageSY (im_in) * scale));
  75. /*
  76. Request alpha blending. This causes future
  77. drawing operations to perform alpha channel blending
  78. with the background, resulting in an opaque image.
  79. Without this call, pixels in the foreground color are
  80. copied literally, *including* the alpha channel value,
  81. resulting in an output image which is potentially
  82. not opaque. This flag can be set and cleared as often
  83. as desired. */
  84. gdImageAlphaBlending (im_out, blending);
  85. /* Flood with light blue. */
  86. gdImageFill (im_out, (int) (gdImageSX (im_in) * scale / 2),
  87. (int) (gdImageSY (im_in) * scale / 2),
  88. gdTrueColor (192, 192, 255));
  89. /* Copy the source image. Alpha blending should result in
  90. compositing against red. With blending turned off, the
  91. browser or viewer will composite against its preferred
  92. background, or, if it does not support an alpha channel,
  93. we will see the original colors for the pixels that
  94. ought to be transparent or semitransparent. */
  95. gdImageCopyResampled (im_out, im_in,
  96. 0, 0,
  97. 0, 0,
  98. (int) (gdImageSX (im_in) * scale), (int) (gdImageSY (im_in) * scale),
  99. gdImageSX (im_in), gdImageSY (im_in));
  100. /* Write PNG */
  101. out = fopen (filename, "wb");
  102. /* If this image is the result of alpha channel blending,
  103. it will not contain an interesting alpha channel itself.
  104. Save a little file size by not saving the alpha channel.
  105. Otherwise the file would typically be slightly larger. */
  106. gdImageSaveAlpha (im_out, !blending);
  107. /* If requested, convert from truecolor to palette. */
  108. if (palette)
  109. {
  110. /* Dithering, 256 colors. */
  111. gdImageTrueColorToPalette (im_out, 1, 256);
  112. }
  113. gdImagePng (im_out, out);
  114. fclose (out);
  115. gdImageDestroy (im_out);
  116. }