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;
209 /* read and check signature in PNG file */
210 ret = fread (buf, 1, 8, png_file);
214 ret = png_check_sig (buf, 8);
218 /* create png and info structures */
220 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
223 return FALSE; /* out of memory */
225 info_ptr = png_create_info_struct (png_ptr);
228 png_destroy_read_struct (&png_ptr, NULL, NULL);
229 return FALSE; /* out of memory */
232 if (setjmp (png_jmpbuf(png_ptr)))
234 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
238 /* set up the input control for C streams */
239 png_init_io (png_ptr, png_file);
240 png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
242 /* read the file information */
243 png_read_info (png_ptr, info_ptr);
245 /* get size and bit-depth of the PNG-image */
246 png_get_IHDR (png_ptr, info_ptr,
247 &width, &height, &bit_depth, &color_type,
250 /* set-up the transformations */
252 /* transform paletted images into full-color rgb */
253 if (color_type == PNG_COLOR_TYPE_PALETTE)
254 png_set_expand (png_ptr);
255 /* expand images to bit-depth 8 (only applicable for grayscale images) */
256 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
257 png_set_expand (png_ptr);
258 /* transform transparency maps into full alpha-channel */
259 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
260 png_set_expand (png_ptr);
263 /* downgrade 16-bit images to 8 bit */
265 png_set_strip_16 (png_ptr);
266 /* transform grayscale images into full-color */
267 if (color_type == PNG_COLOR_TYPE_GRAY ||
268 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
269 png_set_gray_to_rgb (png_ptr);
270 /* only if file has a file gamma, we do a correction */
271 if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
272 png_set_gamma (png_ptr, (double) 2.2, file_gamma);
275 /* all transformations have been registered; now update info_ptr data,
276 * get rowbytes and channels, and allocate image memory */
278 png_read_update_info (png_ptr, info_ptr);
280 /* get the new color-type and bit-depth (after expansion/stripping) */
281 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
284 /* check for 16-bit files */
289 pnm_file->flags &= ~((unsigned) _F_BIN);
293 /* calculate new number of channels and store alpha-presence */
294 if (color_type == PNG_COLOR_TYPE_GRAY)
296 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
298 else if (color_type == PNG_COLOR_TYPE_RGB)
300 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
303 channels = 0; /* should never happen */
304 alpha_present = (channels - 1) % 2;
306 /* check if alpha is expected to be present in file */
307 if (alpha && !alpha_present)
309 fprintf (stderr, "PNG2PNM\n");
310 fprintf (stderr, "Error: PNG-file doesn't contain alpha channel\n");
314 /* row_bytes is the width x number of channels x (bit-depth / 8) */
315 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
317 if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL) {
318 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
322 if ((row_pointers = (png_byte **) malloc (height * sizeof (png_bytep))) == NULL)
324 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
330 /* set the individual row_pointers to point at the correct offsets */
331 for (i = 0; i < (height); i++)
332 row_pointers[i] = png_pixels + i * row_bytes;
334 /* now we can go ahead and just read the whole image */
335 png_read_image (png_ptr, row_pointers);
337 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
338 png_read_end (png_ptr, info_ptr);
340 /* clean up after the read, and free any memory allocated - REQUIRED */
341 png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
343 /* write header of PNM file */
345 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
346 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
348 fprintf (pnm_file, "%s\n", (raw) ? "P5" : "P2");
349 fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
350 fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
352 else if ((color_type == PNG_COLOR_TYPE_RGB) ||
353 (color_type == PNG_COLOR_TYPE_RGB_ALPHA))
355 fprintf (pnm_file, "%s\n", (raw) ? "P6" : "P3");
356 fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
357 fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
360 /* write header of PGM file with alpha channel */
363 ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
364 (color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
366 fprintf (alpha_file, "%s\n", (raw) ? "P5" : "P2");
367 fprintf (alpha_file, "%d %d\n", (int) width, (int) height);
368 fprintf (alpha_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
371 /* write data to PNM file */
372 pix_ptr = png_pixels;
374 for (row = 0; row < height; row++)
376 for (col = 0; col < width; col++)
378 for (i = 0; i < (channels - alpha_present); i++)
381 fputc ((int) *pix_ptr++ , pnm_file);
383 if (bit_depth == 16){
384 dep_16 = (long) *pix_ptr++;
385 fprintf (pnm_file, "%ld ", (dep_16 << 8) + ((long) *pix_ptr++));
388 fprintf (pnm_file, "%ld ", (long) *pix_ptr++);
394 pix_ptr++; /* alpha */
398 else /* output alpha-channel as pgm file */
401 fputc ((int) *pix_ptr++ , alpha_file);
403 if (bit_depth == 16){
404 dep_16 = (long) *pix_ptr++;
405 fprintf (alpha_file, "%ld ", (dep_16 << 8) + (long) *pix_ptr++);
408 fprintf (alpha_file, "%ld ", (long) *pix_ptr++);
410 } /* if alpha_present */
414 fprintf (pnm_file, "\n");
419 fprintf (pnm_file, "\n");
422 if (row_pointers != (unsigned char**) NULL)
424 if (png_pixels != (unsigned char*) NULL)
429 } /* end of source */