2 * pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
3 * copyright (C) 1999 by Willem van Schaik <willem@schaik.com>
5 * version 1.0 - 1999.10.15 - First version.
6 * version 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
8 * Permission to use, copy, modify, and distribute this software and
9 * its documentation for any purpose and without fee is hereby granted,
10 * provided that the above copyright notice appear in all copies and
11 * that both that copyright notice and this permission notice appear in
12 * supporting documentation. This software is provided "as is" without
13 * express or implied warranty.
25 #define BOOL unsigned char
31 #define FALSE (BOOL) 0
38 /* to make pnm2png 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
pnm2png (FILE *pnm_file
, FILE *png_file
, FILE *alpha_file
, BOOL interlace
,
56 void get_token(FILE *pnm_file
, char *token
);
57 png_uint_32
get_data (FILE *pnm_file
, int depth
);
58 png_uint_32
get_value (FILE *pnm_file
, int depth
);
64 int main(int argc
, char *argv
[])
69 BOOL interlace
= FALSE
;
73 for (argi
= 1; argi
< argc
; argi
++)
75 if (argv
[argi
][0] == '-')
77 switch (argv
[argi
][1])
85 if ((fp_al
= fopen (argv
[argi
], "rb")) == NULL
)
87 fprintf (stderr
, "PNM2PNG\n");
88 fprintf (stderr
, "Error: alpha-channel file %s does not exist\n",
99 fprintf (stderr
, "PNM2PNG\n");
100 fprintf (stderr
, "Error: unknown option %s\n", argv
[argi
]);
106 else if (fp_rd
== stdin
)
108 if ((fp_rd
= fopen (argv
[argi
], "rb")) == NULL
)
110 fprintf (stderr
, "PNM2PNG\n");
111 fprintf (stderr
, "Error: file %s does not exist\n", argv
[argi
]);
115 else if (fp_wr
== stdout
)
117 if ((fp_wr
= fopen (argv
[argi
], "wb")) == NULL
)
119 fprintf (stderr
, "PNM2PNG\n");
120 fprintf (stderr
, "Error: can not create PNG-file %s\n", argv
[argi
]);
126 fprintf (stderr
, "PNM2PNG\n");
127 fprintf (stderr
, "Error: too many parameters\n");
134 /* set stdin/stdout to binary, we're reading the PNM always! in binary format */
137 setmode (STDIN
, O_BINARY
);
141 setmode (STDOUT
, O_BINARY
);
145 /* call the conversion program itself */
146 if (pnm2png (fp_rd
, fp_wr
, fp_al
, interlace
, alpha
) == FALSE
)
148 fprintf (stderr
, "PNM2PNG\n");
149 fprintf (stderr
, "Error: unsuccessful converting to PNG-image\n");
153 /* close input file */
155 /* close output file */
157 /* close alpha file */
170 fprintf (stderr
, "PNM2PNG\n");
171 fprintf (stderr
, " by Willem van Schaik, 1999\n");
173 fprintf (stderr
, " for Turbo-C and Borland-C compilers\n");
175 fprintf (stderr
, " for Linux (and Unix) compilers\n");
177 fprintf (stderr
, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
178 fprintf (stderr
, " or: ... | pnm2png [options]\n");
179 fprintf (stderr
, "Options:\n");
180 fprintf (stderr
, " -i[nterlace] write png-file with interlacing on\n");
182 " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
183 fprintf (stderr
, " -h | -? print this help-information\n");
190 BOOL
pnm2png (FILE *pnm_file
, FILE *png_file
, FILE *alpha_file
, BOOL interlace
,
193 png_struct
*png_ptr
= NULL
;
194 png_info
*info_ptr
= NULL
;
195 png_byte
*png_pixels
= NULL
;
196 png_byte
**row_pointers
= NULL
;
197 png_byte
*pix_ptr
= NULL
;
198 volatile png_uint_32 row_bytes
;
201 char width_token
[16];
202 char height_token
[16];
203 char maxval_token
[16];
204 volatile int color_type
=1;
205 unsigned long ul_width
=0, ul_alpha_width
=0;
206 unsigned long ul_height
=0, ul_alpha_height
=0;
207 unsigned long ul_maxval
=0;
208 volatile png_uint_32 width
=0, height
=0;
209 volatile png_uint_32 alpha_width
=0, alpha_height
=0;
211 volatile int bit_depth
= 0;
216 BOOL raw
, alpha_raw
= FALSE
;
217 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
218 BOOL packed_bitmap
= FALSE
;
223 /* read header of PNM file */
225 get_token(pnm_file
, type_token
);
226 if (type_token
[0] != 'P')
230 else if ((type_token
[1] == '1') || (type_token
[1] == '4'))
232 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
233 raw
= (type_token
[1] == '4');
234 color_type
= PNG_COLOR_TYPE_GRAY
;
235 get_token(pnm_file
, width_token
);
236 sscanf (width_token
, "%lu", &ul_width
);
237 width
= (png_uint_32
) ul_width
;
238 get_token(pnm_file
, height_token
);
239 sscanf (height_token
, "%lu", &ul_height
);
240 height
= (png_uint_32
) ul_height
;
242 packed_bitmap
= TRUE
;
244 fprintf (stderr
, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and \n");
245 fprintf (stderr
, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
248 else if ((type_token
[1] == '2') || (type_token
[1] == '5'))
250 raw
= (type_token
[1] == '5');
251 color_type
= PNG_COLOR_TYPE_GRAY
;
252 get_token(pnm_file
, width_token
);
253 sscanf (width_token
, "%lu", &ul_width
);
254 width
= (png_uint_32
) ul_width
;
255 get_token(pnm_file
, height_token
);
256 sscanf (height_token
, "%lu", &ul_height
);
257 height
= (png_uint_32
) ul_height
;
258 get_token(pnm_file
, maxval_token
);
259 sscanf (maxval_token
, "%lu", &ul_maxval
);
260 maxval
= (png_uint_32
) ul_maxval
;
264 else if (maxval
<= 3)
266 else if (maxval
<= 15)
268 else if (maxval
<= 255)
270 else /* if (maxval <= 65535) */
273 else if ((type_token
[1] == '3') || (type_token
[1] == '6'))
275 raw
= (type_token
[1] == '6');
276 color_type
= PNG_COLOR_TYPE_RGB
;
277 get_token(pnm_file
, width_token
);
278 sscanf (width_token
, "%lu", &ul_width
);
279 width
= (png_uint_32
) ul_width
;
280 get_token(pnm_file
, height_token
);
281 sscanf (height_token
, "%lu", &ul_height
);
282 height
= (png_uint_32
) ul_height
;
283 get_token(pnm_file
, maxval_token
);
284 sscanf (maxval_token
, "%lu", &ul_maxval
);
285 maxval
= (png_uint_32
) ul_maxval
;
288 else if (maxval
<= 3)
290 else if (maxval
<= 15)
292 else if (maxval
<= 255)
294 else /* if (maxval <= 65535) */
302 /* read header of PGM file with alpha channel */
306 if (color_type
== PNG_COLOR_TYPE_GRAY
)
307 color_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
308 if (color_type
== PNG_COLOR_TYPE_RGB
)
309 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
311 get_token(alpha_file
, type_token
);
312 if (type_token
[0] != 'P')
316 else if ((type_token
[1] == '2') || (type_token
[1] == '5'))
318 alpha_raw
= (type_token
[1] == '5');
319 get_token(alpha_file
, width_token
);
320 sscanf (width_token
, "%lu", &ul_alpha_width
);
321 alpha_width
=(png_uint_32
) ul_alpha_width
;
322 if (alpha_width
!= width
)
324 get_token(alpha_file
, height_token
);
325 sscanf (height_token
, "%lu", &ul_alpha_height
);
326 alpha_height
= (png_uint_32
) ul_alpha_height
;
327 if (alpha_height
!= height
)
329 get_token(alpha_file
, maxval_token
);
330 sscanf (maxval_token
, "%lu", &ul_maxval
);
331 maxval
= (png_uint_32
) ul_maxval
;
334 else if (maxval
<= 3)
336 else if (maxval
<= 15)
338 else if (maxval
<= 255)
340 else /* if (maxval <= 65535) */
342 if (alpha_depth
!= bit_depth
)
351 /* calculate the number of channels and store alpha-presence */
352 if (color_type
== PNG_COLOR_TYPE_GRAY
)
354 else if (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
356 else if (color_type
== PNG_COLOR_TYPE_RGB
)
358 else if (color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
362 channels
= 0; /* cannot happen */
365 alpha_present
= (channels
- 1) % 2;
367 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
369 /* row data is as many bytes as can fit width x channels x bit_depth */
370 row_bytes
= (width
* channels
* bit_depth
+ 7) / 8;
373 /* row_bytes is the width x number of channels x (bit-depth / 8) */
374 row_bytes
= width
* channels
* ((bit_depth
<= 8) ? 1 : 2);
376 if ((png_pixels
= (png_byte
*)
377 malloc (row_bytes
* height
* sizeof (png_byte
))) == NULL
)
380 /* read data from PNM file */
381 pix_ptr
= png_pixels
;
383 for (row
= 0; row
< (int) height
; row
++)
385 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
387 for (i
= 0; i
< (int) row_bytes
; i
++)
388 /* png supports this format natively so no conversion is needed */
389 *pix_ptr
++ = get_data (pnm_file
, 8);
393 for (col
= 0; col
< (int) width
; col
++)
395 for (i
= 0; i
< (channels
- alpha_present
); i
++)
398 *pix_ptr
++ = get_data (pnm_file
, bit_depth
);
401 *pix_ptr
++ = get_value (pnm_file
, bit_depth
);
404 tmp16
= get_value (pnm_file
, bit_depth
);
405 *pix_ptr
= (png_byte
) ((tmp16
>> 8) & 0xFF);
407 *pix_ptr
= (png_byte
) (tmp16
& 0xFF);
412 if (alpha
) /* read alpha-channel from pgm file */
415 *pix_ptr
++ = get_data (alpha_file
, alpha_depth
);
417 if (alpha_depth
<= 8)
418 *pix_ptr
++ = get_value (alpha_file
, bit_depth
);
421 tmp16
= get_value (alpha_file
, bit_depth
);
422 *pix_ptr
++ = (png_byte
) ((tmp16
>> 8) & 0xFF);
423 *pix_ptr
++ = (png_byte
) (tmp16
& 0xFF);
426 } /* if packed_bitmap */
430 /* prepare the standard PNG structures */
431 png_ptr
= png_create_write_struct (png_get_libpng_ver(NULL
), NULL
, NULL
,
439 info_ptr
= png_create_info_struct (png_ptr
);
442 png_destroy_write_struct (&png_ptr
, (png_infopp
) NULL
);
448 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
449 if (packed_bitmap
== TRUE
)
451 png_set_packing (png_ptr
);
452 png_set_invert_mono (png_ptr
);
456 /* setjmp() must be called in every function that calls a PNG-reading libpng function */
457 if (setjmp (png_jmpbuf(png_ptr
)))
459 png_destroy_write_struct (&png_ptr
, &info_ptr
);
465 /* initialize the png structure */
466 png_init_io (png_ptr
, png_file
);
468 /* we're going to write more or less the same PNG as the input file */
469 png_set_IHDR (png_ptr
, info_ptr
, width
, height
, bit_depth
, color_type
,
470 (!interlace
) ? PNG_INTERLACE_NONE
: PNG_INTERLACE_ADAM7
,
471 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
473 /* write the file header information */
474 png_write_info (png_ptr
, info_ptr
);
476 /* if needed we will allocate memory for an new array of row-pointers */
477 if (row_pointers
== (unsigned char**) NULL
)
479 if ((row_pointers
= (png_byte
**)
480 malloc (height
* sizeof (png_bytep
))) == NULL
)
482 png_destroy_write_struct (&png_ptr
, &info_ptr
);
489 /* set the individual row_pointers to point at the correct offsets */
490 for (i
= 0; i
< (int) height
; i
++)
491 row_pointers
[i
] = png_pixels
+ i
* row_bytes
;
493 /* write out the entire image data in one call */
494 png_write_image (png_ptr
, row_pointers
);
496 /* write the additional chunks to the PNG file (not really needed) */
497 png_write_end (png_ptr
, info_ptr
);
499 /* clean up after the write, and free any memory allocated */
500 png_destroy_write_struct (&png_ptr
, &info_ptr
);
502 if (row_pointers
!= (unsigned char**) NULL
)
504 if (png_pixels
!= (unsigned char*) NULL
)
508 } /* end of pnm2png */
511 * get_token() - gets the first string after whitespace
514 void get_token(FILE *pnm_file
, char *token
)
519 /* remove white-space and comment lines */
522 ret
= fgetc(pnm_file
);
524 /* the rest of this line is a comment */
527 ret
= fgetc(pnm_file
);
529 while ((ret
!= '\n') && (ret
!= '\r') && (ret
!= EOF
));
531 if (ret
== EOF
) break;
532 token
[i
] = (unsigned char) ret
;
534 while ((token
[i
] == '\n') || (token
[i
] == '\r') || (token
[i
] == ' '));
539 ret
= fgetc(pnm_file
);
540 if (ret
== EOF
) break;
542 token
[i
] = (unsigned char) ret
;
544 while ((token
[i
] != '\n') && (token
[i
] != '\r') && (token
[i
] != ' '));
552 * get_data() - takes first byte and converts into next pixel value,
553 * taking as much bits as defined by bit-depth and
554 * using the bit-depth to fill up a byte (0Ah -> AAh)
557 png_uint_32
get_data (FILE *pnm_file
, int depth
)
559 static int bits_left
= 0;
560 static int old_value
= 0;
563 png_uint_32 ret_value
;
566 for (i
= 0; i
< depth
; i
++)
567 mask
= (mask
>> 1) | 0x80;
571 old_value
= fgetc (pnm_file
);
575 ret_value
= old_value
& mask
;
576 for (i
= 1; i
< (8 / depth
); i
++)
577 ret_value
= ret_value
|| (ret_value
>> depth
);
579 old_value
= (old_value
<< depth
) & 0xFF;
586 * get_value() - takes first (numeric) string and converts into number,
587 * using the bit-depth to fill up a byte (0Ah -> AAh)
590 png_uint_32
get_value (FILE *pnm_file
, int depth
)
592 static png_uint_32 mask
= 0;
594 unsigned long ul_ret_value
;
595 png_uint_32 ret_value
;
599 for (i
= 0; i
< depth
; i
++)
600 mask
= (mask
<< 1) | 0x01;
602 get_token (pnm_file
, (char *) token
);
603 sscanf ((const char *) token
, "%lu", &ul_ret_value
);
604 ret_value
= (png_uint_32
) ul_ret_value
;
609 for (i
= 0; i
< (8 / depth
); i
++)
610 ret_value
= (ret_value
<< depth
) || ret_value
;