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.
24 #define BOOL unsigned char
30 #define FALSE (BOOL) 0
39 /* to make png2pnm verbose so we can find problems (needs to be before png.h) */
46 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
48 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
51 /* function prototypes */
53 int main (int argc
, char *argv
[]);
55 BOOL
png2pnm (FILE *png_file
, FILE *pnm_file
, FILE *alpha_file
, BOOL raw
,
62 int main(int argc
, char *argv
[])
71 for (argi
= 1; argi
< argc
; argi
++)
73 if (argv
[argi
][0] == '-')
75 switch (argv
[argi
][1])
86 if ((fp_al
= fopen (argv
[argi
], "wb")) == NULL
)
88 fprintf (stderr
, "PNM2PNG\n");
89 fprintf (stderr
, "Error: can not create alpha-channel file %s\n",
100 fprintf (stderr
, "PNG2PNM\n");
101 fprintf (stderr
, "Error: unknown option %s\n", argv
[argi
]);
107 else if (fp_rd
== stdin
)
109 if ((fp_rd
= fopen (argv
[argi
], "rb")) == NULL
)
111 fprintf (stderr
, "PNG2PNM\n");
112 fprintf (stderr
, "Error: file %s does not exist\n", argv
[argi
]);
116 else if (fp_wr
== stdout
)
118 if ((fp_wr
= fopen (argv
[argi
], "wb")) == NULL
)
120 fprintf (stderr
, "PNG2PNM\n");
121 fprintf (stderr
, "Error: can not create file %s\n", argv
[argi
]);
127 fprintf (stderr
, "PNG2PNM\n");
128 fprintf (stderr
, "Error: too many parameters\n");
135 /* set stdin/stdout if required to binary */
138 setmode (STDIN
, O_BINARY
);
140 if ((raw
) && (fp_wr
== stdout
))
142 setmode (STDOUT
, O_BINARY
);
146 /* call the conversion program itself */
147 if (png2pnm (fp_rd
, fp_wr
, fp_al
, raw
, alpha
) == FALSE
)
149 fprintf (stderr
, "PNG2PNM\n");
150 fprintf (stderr
, "Error: unsuccessful conversion of PNG-image\n");
154 /* close input file */
156 /* close output file */
158 /* close alpha file */
171 fprintf (stderr
, "PNG2PNM\n");
172 fprintf (stderr
, " by Willem van Schaik, 1999\n");
174 fprintf (stderr
, " for Turbo-C and Borland-C compilers\n");
176 fprintf (stderr
, " for Linux (and Unix) compilers\n");
178 fprintf (stderr
, "Usage: png2pnm [options] <file>.png [<file>.pnm]\n");
179 fprintf (stderr
, " or: ... | png2pnm [options]\n");
180 fprintf (stderr
, "Options:\n");
182 " -r[aw] write pnm-file in binary format (P4/P5/P6) (default)\n");
183 fprintf (stderr
, " -n[oraw] write pnm-file in ascii format (P1/P2/P3)\n");
185 " -a[lpha] <file>.pgm write PNG alpha channel as pgm-file\n");
186 fprintf (stderr
, " -h | -? print this help-information\n");
193 BOOL
png2pnm (FILE *png_file
, FILE *pnm_file
, FILE *alpha_file
,
194 volatile BOOL raw
, BOOL alpha
)
196 png_struct
*png_ptr
= NULL
;
197 png_info
*info_ptr
= NULL
;
199 png_byte
*png_pixels
= NULL
;
200 png_byte
**row_pointers
= NULL
;
201 png_byte
*pix_ptr
= NULL
;
202 png_uint_32 row_bytes
;
215 /* read and check signature in PNG file */
216 ret
= fread (buf
, 1, 8, png_file
);
220 ret
= png_sig_cmp (buf
, 0, 8);
224 /* create png and info structures */
226 png_ptr
= png_create_read_struct (png_get_libpng_ver(NULL
),
229 return FALSE
; /* out of memory */
231 info_ptr
= png_create_info_struct (png_ptr
);
234 png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
235 return FALSE
; /* out of memory */
238 if (setjmp (png_jmpbuf(png_ptr
)))
240 png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
244 /* set up the input control for C streams */
245 png_init_io (png_ptr
, png_file
);
246 png_set_sig_bytes (png_ptr
, 8); /* we already read the 8 signature bytes */
248 /* read the file information */
249 png_read_info (png_ptr
, info_ptr
);
251 /* get size and bit-depth of the PNG-image */
252 png_get_IHDR (png_ptr
, info_ptr
,
253 &width
, &height
, &bit_depth
, &color_type
,
256 /* set-up the transformations */
258 /* transform paletted images into full-color rgb */
259 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
260 png_set_expand (png_ptr
);
261 /* expand images to bit-depth 8 (only applicable for grayscale images) */
262 if (color_type
== PNG_COLOR_TYPE_GRAY
&& bit_depth
< 8)
263 png_set_expand (png_ptr
);
264 /* transform transparency maps into full alpha-channel */
265 if (png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
266 png_set_expand (png_ptr
);
269 /* downgrade 16-bit images to 8-bit */
271 png_set_strip_16 (png_ptr
);
272 /* transform grayscale images into full-color */
273 if (color_type
== PNG_COLOR_TYPE_GRAY
||
274 color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
275 png_set_gray_to_rgb (png_ptr
);
276 /* only if file has a file gamma, we do a correction */
277 if (png_get_gAMA (png_ptr
, info_ptr
, &file_gamma
))
278 png_set_gamma (png_ptr
, (double) 2.2, file_gamma
);
281 /* all transformations have been registered; now update info_ptr data,
282 * get rowbytes and channels, and allocate image memory */
284 png_read_update_info (png_ptr
, info_ptr
);
286 /* get the new color-type and bit-depth (after expansion/stripping) */
287 png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
290 /* check for 16-bit files */
295 pnm_file
->flags
&= ~((unsigned) _F_BIN
);
299 /* calculate new number of channels and store alpha-presence */
300 if (color_type
== PNG_COLOR_TYPE_GRAY
)
302 else if (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
304 else if (color_type
== PNG_COLOR_TYPE_RGB
)
306 else if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
309 channels
= 0; /* should never happen */
310 alpha_present
= (channels
- 1) % 2;
312 /* check if alpha is expected to be present in file */
313 if (alpha
&& !alpha_present
)
315 fprintf (stderr
, "PNG2PNM\n");
316 fprintf (stderr
, "Error: PNG-file doesn't contain alpha channel\n");
320 /* row_bytes is the width x number of channels x (bit-depth / 8) */
321 row_bytes
= png_get_rowbytes (png_ptr
, info_ptr
);
323 if ((png_pixels
= (png_byte
*)
324 malloc (row_bytes
* height
* sizeof (png_byte
))) == NULL
) {
325 png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
329 if ((row_pointers
= (png_byte
**)
330 malloc (height
* sizeof (png_bytep
))) == NULL
)
332 png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
338 /* set the individual row_pointers to point at the correct offsets */
339 for (i
= 0; i
< ((int) height
); i
++)
340 row_pointers
[i
] = png_pixels
+ i
* row_bytes
;
342 /* now we can go ahead and just read the whole image */
343 png_read_image (png_ptr
, row_pointers
);
345 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
346 png_read_end (png_ptr
, info_ptr
);
348 /* clean up after the read, and free any memory allocated - REQUIRED */
349 png_destroy_read_struct (&png_ptr
, &info_ptr
, (png_infopp
) NULL
);
351 /* write header of PNM file */
353 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
354 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
356 fprintf (pnm_file
, "%s\n", (raw
) ? "P5" : "P2");
357 fprintf (pnm_file
, "%d %d\n", (int) width
, (int) height
);
358 fprintf (pnm_file
, "%ld\n", ((1L << (int) bit_depth
) - 1L));
360 else if ((color_type
== PNG_COLOR_TYPE_RGB
) ||
361 (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
))
363 fprintf (pnm_file
, "%s\n", (raw
) ? "P6" : "P3");
364 fprintf (pnm_file
, "%d %d\n", (int) width
, (int) height
);
365 fprintf (pnm_file
, "%ld\n", ((1L << (int) bit_depth
) - 1L));
368 /* write header of PGM file with alpha channel */
371 ((color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
) ||
372 (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)))
374 fprintf (alpha_file
, "%s\n", (raw
) ? "P5" : "P2");
375 fprintf (alpha_file
, "%d %d\n", (int) width
, (int) height
);
376 fprintf (alpha_file
, "%ld\n", ((1L << (int) bit_depth
) - 1L));
379 /* write data to PNM file */
380 pix_ptr
= png_pixels
;
382 for (row
= 0; row
< (int) height
; row
++)
384 for (col
= 0; col
< (int) width
; col
++)
386 for (i
= 0; i
< (channels
- alpha_present
); i
++)
389 fputc ((int) *pix_ptr
++ , pnm_file
);
391 if (bit_depth
== 16){
392 dep_16
= (long) *pix_ptr
++;
393 fprintf (pnm_file
, "%ld ", (dep_16
<< 8) + ((long) *pix_ptr
++));
396 fprintf (pnm_file
, "%ld ", (long) *pix_ptr
++);
402 pix_ptr
++; /* alpha */
406 else /* output alpha-channel as pgm file */
409 fputc ((int) *pix_ptr
++ , alpha_file
);
411 if (bit_depth
== 16){
412 dep_16
= (long) *pix_ptr
++;
413 fprintf (alpha_file
, "%ld ", (dep_16
<< 8) + (long) *pix_ptr
++);
416 fprintf (alpha_file
, "%ld ", (long) *pix_ptr
++);
418 } /* if alpha_present */
422 fprintf (pnm_file
, "\n");
427 fprintf (pnm_file
, "\n");
430 if (row_pointers
!= (unsigned char**) NULL
)
432 if (png_pixels
!= (unsigned char*) NULL
)
437 } /* end of source */