webpng.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Bring in the gd library functions */
  2. #include "gd.h"
  3. /* Bring in standard I/O and string manipulation functions */
  4. #include <stdio.h>
  5. #include <stdlib.h> /* for atoi() */
  6. #include <string.h>
  7. #ifdef _WIN32
  8. #include <process.h>
  9. int
  10. getpid ()
  11. {
  12. return _getpid ();
  13. }
  14. #else
  15. #include <unistd.h> /* for getpid(), unlink() */
  16. #endif
  17. int
  18. main (int argc, char **argv)
  19. {
  20. FILE *in;
  21. FILE *out;
  22. char outFn[20];
  23. int useStdinStdout = 0;
  24. /* Declare our image pointer */
  25. gdImagePtr im = 0;
  26. int i;
  27. /* We'll clear 'no' once we know the user has made a
  28. reasonable request. */
  29. int no = 1;
  30. /* We'll set 'write' once we know the user's request
  31. requires that the image be written back to disk. */
  32. int write = 0;
  33. /* C programs always get at least one argument; we want at
  34. least one more (the image), more in practice. */
  35. if (argc < 2 || !strcmp (argv[1], "--help"))
  36. {
  37. no = 1;
  38. goto usage;
  39. }
  40. /* The last argument should be the image. Open the file. */
  41. if (strcmp ("-", argv[argc - 1]) == 0)
  42. { /* - is synonymous with STDIN */
  43. useStdinStdout = 1;
  44. in = stdin;
  45. }
  46. else
  47. {
  48. in = fopen (argv[argc - 1], "rb");
  49. }
  50. if (!in)
  51. {
  52. fprintf (stderr,
  53. "Error: can't open file %s.\n", argv[argc - 1]);
  54. exit (1);
  55. }
  56. /* Now load the image. */
  57. im = gdImageCreateFromPng (in);
  58. fclose (in);
  59. /* If the load failed, it must not be a PNG file. */
  60. if (!im)
  61. {
  62. fprintf (stderr,
  63. "Error: %s is not a valid PNG file.\n", argv[argc - 1]);
  64. exit (1);
  65. }
  66. /* Consider each argument in turn. */
  67. for (i = 1; (i < (argc - 1)); i++)
  68. {
  69. /* -i turns on and off interlacing. */
  70. if (!strcmp (argv[i], "--help"))
  71. {
  72. /* Every program should use this for help! :) */
  73. no = 1;
  74. goto usage;
  75. }
  76. else if (!strcmp (argv[i], "-i"))
  77. {
  78. if (i == (argc - 2))
  79. {
  80. fprintf (stderr,
  81. "Error: -i specified without y or n.\n");
  82. no = 1;
  83. goto usage;
  84. }
  85. if (!strcmp (argv[i + 1], "y"))
  86. {
  87. /* Set interlace. */
  88. gdImageInterlace (im, 1);
  89. }
  90. else if (!strcmp (argv[i + 1], "n"))
  91. {
  92. /* Clear interlace. */
  93. gdImageInterlace (im, 0);
  94. }
  95. else
  96. {
  97. fprintf (stderr,
  98. "Error: -i specified without y or n.\n");
  99. no = 1;
  100. goto usage;
  101. }
  102. i++;
  103. no = 0;
  104. write = 1;
  105. }
  106. else if (!strcmp (argv[i], "-t"))
  107. {
  108. /* Set transparent index (or none). */
  109. int index;
  110. if (i == (argc - 2))
  111. {
  112. fprintf (stderr,
  113. "Error: -t specified without a color table index.\n");
  114. no = 1;
  115. goto usage;
  116. }
  117. if (!strcmp (argv[i + 1], "none"))
  118. {
  119. /* -1 means not transparent. */
  120. gdImageColorTransparent (im, -1);
  121. }
  122. else
  123. {
  124. /* OK, get an integer and set the index. */
  125. index = atoi (argv[i + 1]);
  126. gdImageColorTransparent (im, index);
  127. }
  128. i++;
  129. write = 1;
  130. no = 0;
  131. }
  132. else if (!strcmp (argv[i], "-l"))
  133. {
  134. /* List the colors in the color table. */
  135. int j;
  136. if (!im->trueColor)
  137. {
  138. /* Tabs used below. */
  139. printf ("Index Red Green Blue Alpha\n");
  140. for (j = 0; (j < gdImageColorsTotal (im)); j++)
  141. {
  142. /* Use access macros to learn colors. */
  143. printf ("%d %d %d %d %d\n",
  144. j,
  145. gdImageRed (im, j),
  146. gdImageGreen (im, j),
  147. gdImageBlue (im, j),
  148. gdImageAlpha (im, j));
  149. }
  150. }
  151. else
  152. {
  153. printf ("Truecolor image, no palette entries to list.\n");
  154. }
  155. no = 0;
  156. }
  157. else if (!strcmp (argv[i], "-d"))
  158. {
  159. /* Output dimensions, etc. */
  160. int t;
  161. printf ("Width: %d Height: %d Colors: %d\n",
  162. gdImageSX (im), gdImageSY (im),
  163. gdImageColorsTotal (im));
  164. t = gdImageGetTransparent (im);
  165. if (t != (-1))
  166. {
  167. printf ("First 100%% transparent index: %d\n", t);
  168. }
  169. else
  170. {
  171. /* -1 means the image is not transparent. */
  172. printf ("First 100%% transparent index: none\n");
  173. }
  174. if (gdImageGetInterlaced (im))
  175. {
  176. printf ("Interlaced: yes\n");
  177. }
  178. else
  179. {
  180. printf ("Interlaced: no\n");
  181. }
  182. no = 0;
  183. }
  184. else if (!strcmp(argv[i], "-a"))
  185. {
  186. int maxx, maxy, x, y, alpha, pix, nalpha = 0;
  187. maxx = gdImageSX(im);
  188. maxy = gdImageSY(im);
  189. printf("alpha channel information:\n");
  190. if (im->trueColor) {
  191. for (y = 0; y < maxy; y++) {
  192. for (x = 0; x < maxx; x++) {
  193. pix = gdImageGetPixel(im, x, y);
  194. alpha = gdTrueColorGetAlpha(pix);
  195. if (alpha > gdAlphaOpaque) {
  196. /* Use access macros to learn colors. */
  197. printf ("%d %d %d %d\n",
  198. gdTrueColorGetRed(pix),
  199. gdTrueColorGetGreen(pix),
  200. gdTrueColorGetBlue(pix),
  201. alpha);
  202. nalpha++;
  203. }
  204. }
  205. }
  206. }
  207. else
  208. printf("NOT a true color image\n");
  209. no = 0;
  210. printf("%d alpha channels\n", nalpha);
  211. }
  212. else
  213. {
  214. fprintf (stderr, "Unknown argument: %s\n", argv[i]);
  215. break;
  216. }
  217. }
  218. usage:
  219. if (no)
  220. {
  221. /* If the command failed, output an explanation. */
  222. fprintf (stderr,
  223. "Usage: webpng [-i y|n ] [-l] [-t index|none ] [-d] pngname.png\n"
  224. " -i [y|n] Turns on/off interlace\n"
  225. " -l Prints the table of color indexes\n"
  226. " -t [index] Set the transparent color to the specified index (0-255 or \"none\")\n"
  227. " -d Reports the dimensions and other characteristics of the image.\n"
  228. " -a Prints all alpha channels that are not 100%% opaque.\n"
  229. "\n"
  230. "If you specify '-' as the input file, stdin/stdout will be used input/output.\n"
  231. );
  232. }
  233. if (write)
  234. {
  235. if (useStdinStdout)
  236. {
  237. out = stdout;
  238. }
  239. else
  240. {
  241. /* Open a temporary file. */
  242. /* "temp.tmp" is not good temporary filename. */
  243. snprintf (outFn, sizeof(outFn), "webpng.tmp%d", getpid ());
  244. out = fopen (outFn, "wb");
  245. if (!out)
  246. {
  247. fprintf (stderr,
  248. "Unable to write to %s -- exiting\n", outFn);
  249. exit (1);
  250. }
  251. }
  252. /* Write the new PNG. */
  253. gdImagePng (im, out);
  254. if (!useStdinStdout)
  255. {
  256. fclose (out);
  257. /* Erase the old PNG. */
  258. unlink (argv[argc - 1]);
  259. /* Rename the new to the old. */
  260. if (rename (outFn, argv[argc - 1]) != 0)
  261. {
  262. perror ("rename");
  263. exit (1);
  264. }
  265. }
  266. }
  267. /* Delete the image from memory. */
  268. if (im)
  269. {
  270. gdImageDestroy (im);
  271. }
  272. /* All's well that ends well. */
  273. return 0;
  274. }