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