2
* png2pnm.c --- conversion from PNG-file to PGM/PPM-file
3
* copyright (C) 1999 by Willem van Schaik <willem@schaik.com>
5
* version 1.0 - 1999.10.15 - First version.
7
* Permission to use, copy, modify, and distribute this software and
8
* its documentation for any purpose and without fee is hereby granted,
9
* provided that the above copyright notice appear in all copies and
10
* that both that copyright notice and this permission notice appear in
11
* supporting documentation. This software is provided "as is" without
12
* express or implied warranty.
23
#define BOOL unsigned char
29
#define FALSE (BOOL) 0
38
/* to make png2pnm verbose so we can find problems (needs to be before png.h) */
45
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
47
# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
50
/* function prototypes */
52
int main (int argc, char *argv[]);
54
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw, BOOL alpha);
60
int main(int argc, char *argv[])
69
for (argi = 1; argi < argc; argi++)
71
if (argv[argi][0] == '-')
73
switch (argv[argi][1])
84
if ((fp_al = fopen (argv[argi], "wb")) == NULL)
86
fprintf (stderr, "PNM2PNG\n");
87
fprintf (stderr, "Error: can not create alpha-channel file %s\n", argv[argi]);
97
fprintf (stderr, "PNG2PNM\n");
98
fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
104
else if (fp_rd == stdin)
106
if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
108
fprintf (stderr, "PNG2PNM\n");
109
fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
113
else if (fp_wr == stdout)
115
if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
117
fprintf (stderr, "PNG2PNM\n");
118
fprintf (stderr, "Error: can not create file %s\n", argv[argi]);
124
fprintf (stderr, "PNG2PNM\n");
125
fprintf (stderr, "Error: too many parameters\n");
132
/* set stdin/stdout if required to binary */
135
setmode (STDIN, O_BINARY);
137
if ((raw) && (fp_wr == stdout))
139
setmode (STDOUT, O_BINARY);
143
/* call the conversion program itself */
144
if (png2pnm (fp_rd, fp_wr, fp_al, raw, alpha) == FALSE)
146
fprintf (stderr, "PNG2PNM\n");
147
fprintf (stderr, "Error: unsuccessful convertion of PNG-image\n");
151
/* close input file */
153
/* close output file */
155
/* close alpha file */
168
fprintf (stderr, "PNG2PNM\n");
169
fprintf (stderr, " by Willem van Schaik, 1999\n");
171
fprintf (stderr, " for Turbo-C and Borland-C compilers\n");
173
fprintf (stderr, " for Linux (and Unix) compilers\n");
175
fprintf (stderr, "Usage: png2pnm [options] <file>.png [<file>.pnm]\n");
176
fprintf (stderr, " or: ... | png2pnm [options]\n");
177
fprintf (stderr, "Options:\n");
178
fprintf (stderr, " -r[aw] write pnm-file in binary format (P4/P5/P6) (default)\n");
179
fprintf (stderr, " -n[oraw] write pnm-file in ascii format (P1/P2/P3)\n");
180
fprintf (stderr, " -a[lpha] <file>.pgm write PNG alpha channel as pgm-file\n");
181
fprintf (stderr, " -h | -? print this help-information\n");
188
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw, BOOL alpha)
190
png_struct *png_ptr = NULL;
191
png_info *info_ptr = NULL;
193
png_byte *png_pixels = NULL;
194
png_byte **row_pointers = NULL;
195
png_byte *pix_ptr = NULL;
196
png_uint_32 row_bytes;
208
/* read and check signature in PNG file */
209
ret = fread (buf, 1, 8, png_file);
213
ret = png_check_sig (buf, 8);
217
/* create png and info structures */
219
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
222
return FALSE; /* out of memory */
224
info_ptr = png_create_info_struct (png_ptr);
227
png_destroy_read_struct (&png_ptr, NULL, NULL);
228
return FALSE; /* out of memory */
231
if (setjmp (png_jmpbuf(png_ptr)))
233
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
237
/* set up the input control for C streams */
238
png_init_io (png_ptr, png_file);
239
png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
241
/* read the file information */
242
png_read_info (png_ptr, info_ptr);
244
/* get size and bit-depth of the PNG-image */
245
png_get_IHDR (png_ptr, info_ptr,
246
&width, &height, &bit_depth, &color_type,
249
/* set-up the transformations */
251
/* transform paletted images into full-color rgb */
252
if (color_type == PNG_COLOR_TYPE_PALETTE)
253
png_set_expand (png_ptr);
254
/* expand images to bit-depth 8 (only applicable for grayscale images) */
255
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
256
png_set_expand (png_ptr);
257
/* transform transparency maps into full alpha-channel */
258
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
259
png_set_expand (png_ptr);
262
/* downgrade 16-bit images to 8 bit */
264
png_set_strip_16 (png_ptr);
265
/* transform grayscale images into full-color */
266
if (color_type == PNG_COLOR_TYPE_GRAY ||
267
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
268
png_set_gray_to_rgb (png_ptr);
269
/* only if file has a file gamma, we do a correction */
270
if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
271
png_set_gamma (png_ptr, (double) 2.2, file_gamma);
274
/* all transformations have been registered; now update info_ptr data,
275
* get rowbytes and channels, and allocate image memory */
277
png_read_update_info (png_ptr, info_ptr);
279
/* get the new color-type and bit-depth (after expansion/stripping) */
280
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
283
/* check for 16-bit files */
288
pnm_file->flags &= ~((unsigned) _F_BIN);
292
/* calculate new number of channels and store alpha-presence */
293
if (color_type == PNG_COLOR_TYPE_GRAY)
295
else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
297
else if (color_type == PNG_COLOR_TYPE_RGB)
299
else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
302
channels = 0; /* should never happen */
303
alpha_present = (channels - 1) % 2;
305
/* check if alpha is expected to be present in file */
306
if (alpha && !alpha_present)
308
fprintf (stderr, "PNG2PNM\n");
309
fprintf (stderr, "Error: PNG-file doesn't contain alpha channel\n");
313
/* row_bytes is the width x number of channels x (bit-depth / 8) */
314
row_bytes = png_get_rowbytes (png_ptr, info_ptr);
316
if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL) {
317
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
321
if ((row_pointers = (png_byte **) malloc (height * sizeof (png_bytep))) == NULL)
323
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
329
/* set the individual row_pointers to point at the correct offsets */
330
for (i = 0; i < (height); i++)
331
row_pointers[i] = png_pixels + i * row_bytes;
333
/* now we can go ahead and just read the whole image */
334
png_read_image (png_ptr, row_pointers);
336
/* read rest of file, and get additional chunks in info_ptr - REQUIRED */
337
png_read_end (png_ptr, info_ptr);
339
/* clean up after the read, and free any memory allocated - REQUIRED */
340
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
342
/* write header of PNM file */
344
if ((color_type == PNG_COLOR_TYPE_GRAY) ||
345
(color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
347
fprintf (pnm_file, "%s\n", (raw) ? "P5" : "P2");
348
fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
349
fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
351
else if ((color_type == PNG_COLOR_TYPE_RGB) ||
352
(color_type == PNG_COLOR_TYPE_RGB_ALPHA))
354
fprintf (pnm_file, "%s\n", (raw) ? "P6" : "P3");
355
fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
356
fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
359
/* write header of PGM file with alpha channel */
362
((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
363
(color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
365
fprintf (alpha_file, "%s\n", (raw) ? "P5" : "P2");
366
fprintf (alpha_file, "%d %d\n", (int) width, (int) height);
367
fprintf (alpha_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
370
/* write data to PNM file */
371
pix_ptr = png_pixels;
373
for (row = 0; row < height; row++)
375
for (col = 0; col < width; col++)
377
for (i = 0; i < (channels - alpha_present); i++)
380
fputc ((int) *pix_ptr++ , pnm_file);
383
fprintf (pnm_file, "%ld ", ((long) *pix_ptr++ << 8) + (long) *pix_ptr++);
385
fprintf (pnm_file, "%ld ", (long) *pix_ptr++);
391
pix_ptr++; /* alpha */
395
else /* output alpha-channel as pgm file */
398
fputc ((int) *pix_ptr++ , alpha_file);
401
fprintf (alpha_file, "%ld ", ((long) *pix_ptr++ << 8) + (long) *pix_ptr++);
403
fprintf (alpha_file, "%ld ", (long) *pix_ptr++);
405
} /* if alpha_present */
409
fprintf (pnm_file, "\n");
414
fprintf (pnm_file, "\n");
417
if (row_pointers != (unsigned char**) NULL)
419
if (png_pixels != (unsigned char*) NULL)
424
} /* end of source */