1
/* Bring in the gd library functions */
4
/* Bring in standard I/O and string manipulation functions */
6
#include <stdlib.h> /* for atoi() */
17
#include <unistd.h> /* for getpid(), unlink() */
20
main (int argc, char **argv)
25
int useStdinStdout = 0;
27
/* Declare our image pointer */
30
/* We'll clear 'no' once we know the user has made a
31
reasonable request. */
33
/* We'll set 'write' once we know the user's request
34
requires that the image be written back to disk. */
36
/* C programs always get at least one argument; we want at
37
least one more (the image), more in practice. */
38
if (argc < 2 || !strcmp (argv[1], "--help"))
44
/* The last argument should be the image. Open the file. */
45
if (strcmp ("-", argv[argc - 1]) == 0)
46
{ /* - is synonymous with STDIN */
52
in = fopen (argv[argc - 1], "rb");
57
"Error: can't open file %s.\n", argv[argc - 1]);
60
/* Now load the image. */
61
im = gdImageCreateFromPng (in);
63
/* If the load failed, it must not be a PNG file. */
67
"Error: %s is not a valid PNG file.\n", argv[argc - 1]);
70
/* Consider each argument in turn. */
71
for (i = 1; (i < (argc - 1)); i++)
73
/* -i turns on and off interlacing. */
74
if (!strcmp (argv[i], "--help"))
76
/* Every program should use this for help! :) */
80
else if (!strcmp (argv[i], "-i"))
85
"Error: -i specified without y or n.\n");
89
if (!strcmp (argv[i + 1], "y"))
92
gdImageInterlace (im, 1);
94
else if (!strcmp (argv[i + 1], "n"))
96
/* Clear interlace. */
97
gdImageInterlace (im, 0);
102
"Error: -i specified without y or n.\n");
110
else if (!strcmp (argv[i], "-t"))
112
/* Set transparent index (or none). */
117
"Error: -t specified without a color table index.\n");
121
if (!strcmp (argv[i + 1], "none"))
123
/* -1 means not transparent. */
124
gdImageColorTransparent (im, -1);
128
/* OK, get an integer and set the index. */
129
index = atoi (argv[i + 1]);
130
gdImageColorTransparent (im, index);
136
else if (!strcmp (argv[i], "-l"))
138
/* List the colors in the color table. */
142
/* Tabs used below. */
143
printf ("Index Red Green Blue Alpha\n");
144
for (j = 0; (j < gdImageColorsTotal (im)); j++)
146
/* Use access macros to learn colors. */
147
printf ("%d %d %d %d %d\n",
150
gdImageGreen (im, j),
152
gdImageAlpha (im, j));
157
printf ("Truecolor image, no palette entries to list.\n");
161
else if (!strcmp (argv[i], "-d"))
163
/* Output dimensions, etc. */
165
printf ("Width: %d Height: %d Colors: %d\n",
166
gdImageSX (im), gdImageSY (im),
167
gdImageColorsTotal (im));
168
t = gdImageGetTransparent (im);
171
printf ("First 100% transparent index: %d\n", t);
175
/* -1 means the image is not transparent. */
176
printf ("First 100% transparent index: none\n");
178
if (gdImageGetInterlaced (im))
180
printf ("Interlaced: yes\n");
184
printf ("Interlaced: no\n");
190
fprintf (stderr, "Unknown argument: %s\n", argv[i]);
197
/* If the command failed, output an explanation. */
199
"Usage: webpng [-i y|n ] [-l] [-t index|none ] [-d] pngname.png\n"
201
" -i [y|n] Turns on/off interlace\n"
202
" -l Prints the table of color indexes\n"
203
" -t [index] Set the transparent color to the specified index (0-255 or \"none\")\n"
204
" -d Reports the dimensions and other characteristics of the image.\n"
206
"If you specify '-' as the input file, stdin/stdout will be used input/output.\n"
217
/* Open a temporary file. */
219
/* "temp.tmp" is not good temporary filename. */
220
sprintf (outFn, "webpng.tmp%d", getpid ());
221
out = fopen (outFn, "wb");
226
"Unable to write to %s -- exiting\n", outFn);
231
/* Write the new PNG. */
232
gdImagePng (im, out);
237
/* Erase the old PNG. */
238
unlink (argv[argc - 1]);
239
/* Rename the new to the old. */
240
if (rename (outFn, argv[argc - 1]) != 0)
247
/* Delete the image from memory. */
252
/* All's well that ends well. */