2 * pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
3 * copyright (C) 1999,2015,2017 by Willem van Schaik <willem at schaik.com>
5 * version 1.0 - 1999.10.15 - First version.
6 * version 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
7 * version 1.2 - 2017.04.22 - Add buffer-size check
8 * 1.3 - 2017.08.24 - Fix potential overflow in buffer-size check
9 * (Glenn Randers-Pehrson)
10 * 1.4 - 2017.08.28 - Add PNGMINUS_UNUSED (Christian Hesse)
12 * Permission to use, copy, modify, and distribute this software and
13 * its documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and
15 * that both that copyright notice and this permission notice appear in
16 * supporting documentation. This software is provided "as is" without
17 * express or implied warranty.
29 #define BOOL unsigned char
35 #define FALSE (BOOL) 0
42 /* to make pnm2png verbose so we can find problems (needs to be before png.h) */
49 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
51 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
54 #ifndef PNGMINUS_UNUSED
55 /* Unused formal parameter warnings are silenced using the following macro
56 * which is expected to have no bad effects on performance (optimizing
57 * compilers will probably remove it entirely).
59 # define PNGMINUS_UNUSED(param) (void)param
63 /* function prototypes */
65 int main (int argc, char *argv[]);
67 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
69 void get_token(FILE *pnm_file, char *token);
70 png_uint_32 get_data (FILE *pnm_file, int depth);
71 png_uint_32 get_value (FILE *pnm_file, int depth);
77 int main(int argc, char *argv[])
82 BOOL interlace = FALSE;
86 for (argi = 1; argi < argc; argi++)
88 if (argv[argi][0] == '-')
90 switch (argv[argi][1])
98 if ((fp_al = fopen (argv[argi], "rb")) == NULL)
100 fprintf (stderr, "PNM2PNG\n");
101 fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
112 fprintf (stderr, "PNM2PNG\n");
113 fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
119 else if (fp_rd == stdin)
121 if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
123 fprintf (stderr, "PNM2PNG\n");
124 fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
128 else if (fp_wr == stdout)
130 if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
132 fprintf (stderr, "PNM2PNG\n");
133 fprintf (stderr, "Error: can not create PNG-file %s\n", argv[argi]);
139 fprintf (stderr, "PNM2PNG\n");
140 fprintf (stderr, "Error: too many parameters\n");
147 /* set stdin/stdout to binary, we're reading the PNM always! in binary format */
150 setmode (STDIN, O_BINARY);
154 setmode (STDOUT, O_BINARY);
158 /* call the conversion program itself */
159 if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
161 fprintf (stderr, "PNM2PNG\n");
162 fprintf (stderr, "Error: unsuccessful converting to PNG-image\n");
166 /* close input file */
168 /* close output file */
170 /* close alpha file */
183 fprintf (stderr, "PNM2PNG\n");
184 fprintf (stderr, " by Willem van Schaik, 1999\n");
186 fprintf (stderr, " for Turbo-C and Borland-C compilers\n");
188 fprintf (stderr, " for Linux (and Unix) compilers\n");
190 fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
191 fprintf (stderr, " or: ... | pnm2png [options]\n");
192 fprintf (stderr, "Options:\n");
193 fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n");
195 " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
196 fprintf (stderr, " -h | -? print this help-information\n");
203 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
206 png_struct *png_ptr = NULL;
207 png_info *info_ptr = NULL;
208 png_byte *png_pixels = NULL;
209 png_byte **row_pointers = NULL;
210 png_byte *pix_ptr = NULL;
211 volatile png_uint_32 row_bytes;
214 char width_token[16];
215 char height_token[16];
216 char maxval_token[16];
217 volatile int color_type=1;
218 unsigned long ul_width=0, ul_alpha_width=0;
219 unsigned long ul_height=0, ul_alpha_height=0;
220 unsigned long ul_maxval=0;
221 volatile png_uint_32 width=0, height=0;
222 volatile png_uint_32 alpha_width=0, alpha_height=0;
224 volatile int bit_depth = 0;
229 BOOL raw, alpha_raw = FALSE;
230 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
231 BOOL packed_bitmap = FALSE;
236 /* read header of PNM file */
238 get_token(pnm_file, type_token);
239 if (type_token[0] != 'P')
243 else if ((type_token[1] == '1') || (type_token[1] == '4'))
245 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
246 raw = (type_token[1] == '4');
247 color_type = PNG_COLOR_TYPE_GRAY;
248 get_token(pnm_file, width_token);
249 sscanf (width_token, "%lu", &ul_width);
250 width = (png_uint_32) ul_width;
251 get_token(pnm_file, height_token);
252 sscanf (height_token, "%lu", &ul_height);
253 height = (png_uint_32) ul_height;
255 packed_bitmap = TRUE;
257 fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and \n");
258 fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
261 else if ((type_token[1] == '2') || (type_token[1] == '5'))
263 raw = (type_token[1] == '5');
264 color_type = PNG_COLOR_TYPE_GRAY;
265 get_token(pnm_file, width_token);
266 sscanf (width_token, "%lu", &ul_width);
267 width = (png_uint_32) ul_width;
268 get_token(pnm_file, height_token);
269 sscanf (height_token, "%lu", &ul_height);
270 height = (png_uint_32) ul_height;
271 get_token(pnm_file, maxval_token);
272 sscanf (maxval_token, "%lu", &ul_maxval);
273 maxval = (png_uint_32) ul_maxval;
277 else if (maxval <= 3)
279 else if (maxval <= 15)
281 else if (maxval <= 255)
283 else /* if (maxval <= 65535) */
286 else if ((type_token[1] == '3') || (type_token[1] == '6'))
288 raw = (type_token[1] == '6');
289 color_type = PNG_COLOR_TYPE_RGB;
290 get_token(pnm_file, width_token);
291 sscanf (width_token, "%lu", &ul_width);
292 width = (png_uint_32) ul_width;
293 get_token(pnm_file, height_token);
294 sscanf (height_token, "%lu", &ul_height);
295 height = (png_uint_32) ul_height;
296 get_token(pnm_file, maxval_token);
297 sscanf (maxval_token, "%lu", &ul_maxval);
298 maxval = (png_uint_32) ul_maxval;
301 else if (maxval <= 3)
303 else if (maxval <= 15)
305 else if (maxval <= 255)
307 else /* if (maxval <= 65535) */
315 /* read header of PGM file with alpha channel */
319 if (color_type == PNG_COLOR_TYPE_GRAY)
320 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
321 if (color_type == PNG_COLOR_TYPE_RGB)
322 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
324 get_token(alpha_file, type_token);
325 if (type_token[0] != 'P')
329 else if ((type_token[1] == '2') || (type_token[1] == '5'))
331 alpha_raw = (type_token[1] == '5');
332 get_token(alpha_file, width_token);
333 sscanf (width_token, "%lu", &ul_alpha_width);
334 alpha_width=(png_uint_32) ul_alpha_width;
335 if (alpha_width != width)
337 get_token(alpha_file, height_token);
338 sscanf (height_token, "%lu", &ul_alpha_height);
339 alpha_height = (png_uint_32) ul_alpha_height;
340 if (alpha_height != height)
342 get_token(alpha_file, maxval_token);
343 sscanf (maxval_token, "%lu", &ul_maxval);
344 maxval = (png_uint_32) ul_maxval;
347 else if (maxval <= 3)
349 else if (maxval <= 15)
351 else if (maxval <= 255)
353 else /* if (maxval <= 65535) */
355 if (alpha_depth != bit_depth)
364 /* calculate the number of channels and store alpha-presence */
365 if (color_type == PNG_COLOR_TYPE_GRAY)
367 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
369 else if (color_type == PNG_COLOR_TYPE_RGB)
371 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
375 channels = 0; /* cannot happen */
378 alpha_present = (channels - 1) % 2;
380 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
382 /* row data is as many bytes as can fit width x channels x bit_depth */
383 row_bytes = (width * channels * bit_depth + 7) / 8;
386 /* row_bytes is the width x number of channels x (bit-depth / 8) */
387 row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
389 if ((row_bytes == 0 || (size_t)height > ((size_t)(-1))/(size_t)row_bytes))
394 if ((png_pixels = (png_byte *)
395 malloc ((size_t)row_bytes * (size_t)height * sizeof (png_byte))) == NULL)
398 /* read data from PNM file */
399 pix_ptr = png_pixels;
401 for (row = 0; row < (int) height; row++)
403 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
406 for (i = 0; i < (int) row_bytes; i++)
407 /* png supports this format natively so no conversion is needed */
408 *pix_ptr++ = get_data (pnm_file, 8);
412 for (col = 0; col < (int) width; col++)
414 for (i = 0; i < (channels - alpha_present); i++)
417 *pix_ptr++ = get_data (pnm_file, bit_depth);
420 *pix_ptr++ = get_value (pnm_file, bit_depth);
423 tmp16 = get_value (pnm_file, bit_depth);
424 *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
426 *pix_ptr = (png_byte) (tmp16 & 0xFF);
431 if (alpha) /* read alpha-channel from pgm file */
434 *pix_ptr++ = get_data (alpha_file, alpha_depth);
436 if (alpha_depth <= 8)
437 *pix_ptr++ = get_value (alpha_file, bit_depth);
440 tmp16 = get_value (alpha_file, bit_depth);
441 *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
442 *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
445 } /* if packed_bitmap */
449 /* prepare the standard PNG structures */
450 png_ptr = png_create_write_struct (png_get_libpng_ver(NULL), NULL, NULL,
458 info_ptr = png_create_info_struct (png_ptr);
461 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
467 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
468 if (packed_bitmap == TRUE)
470 png_set_packing (png_ptr);
471 png_set_invert_mono (png_ptr);
475 /* setjmp() must be called in every function that calls a PNG-reading libpng function */
476 if (setjmp (png_jmpbuf(png_ptr)))
478 png_destroy_write_struct (&png_ptr, &info_ptr);
484 /* initialize the png structure */
485 png_init_io (png_ptr, png_file);
487 /* we're going to write more or less the same PNG as the input file */
488 png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
489 (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
490 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
492 /* write the file header information */
493 png_write_info (png_ptr, info_ptr);
495 /* if needed we will allocate memory for an new array of row-pointers */
496 if (row_pointers == (unsigned char**) NULL)
498 if ((row_pointers = (png_byte **)
499 malloc (height * sizeof (png_bytep))) == NULL)
501 png_destroy_write_struct (&png_ptr, &info_ptr);
508 /* set the individual row_pointers to point at the correct offsets */
509 for (i = 0; i < (int) height; i++)
510 row_pointers[i] = png_pixels + i * row_bytes;
512 /* write out the entire image data in one call */
513 png_write_image (png_ptr, row_pointers);
515 /* write the additional chunks to the PNG file (not really needed) */
516 png_write_end (png_ptr, info_ptr);
518 /* clean up after the write, and free any memory allocated */
519 png_destroy_write_struct (&png_ptr, &info_ptr);
521 if (row_pointers != (unsigned char**) NULL)
523 if (png_pixels != (unsigned char*) NULL)
526 PNGMINUS_UNUSED(raw); /* Quiet a Coverity defect */
529 } /* end of pnm2png */
532 * get_token() - gets the first string after whitespace
535 void get_token(FILE *pnm_file, char *token)
540 /* remove white-space and comment lines */
543 ret = fgetc(pnm_file);
546 /* the rest of this line is a comment */
549 ret = fgetc(pnm_file);
551 while ((ret != '\n') && (ret != '\r') && (ret != EOF));
553 if (ret == EOF) break;
554 token[i] = (unsigned char) ret;
556 while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' '));
561 ret = fgetc(pnm_file);
562 if (ret == EOF) break;
564 token[i] = (unsigned char) ret;
566 while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' '));
574 * get_data() - takes first byte and converts into next pixel value,
575 * taking as much bits as defined by bit-depth and
576 * using the bit-depth to fill up a byte (0Ah -> AAh)
579 png_uint_32 get_data (FILE *pnm_file, int depth)
581 static int bits_left = 0;
582 static int old_value = 0;
585 png_uint_32 ret_value;
588 for (i = 0; i < depth; i++)
589 mask = (mask >> 1) | 0x80;
593 old_value = fgetc (pnm_file);
597 ret_value = old_value & mask;
598 for (i = 1; i < (8 / depth); i++)
599 ret_value = ret_value || (ret_value >> depth);
601 old_value = (old_value << depth) & 0xFF;
608 * get_value() - takes first (numeric) string and converts into number,
609 * using the bit-depth to fill up a byte (0Ah -> AAh)
612 png_uint_32 get_value (FILE *pnm_file, int depth)
614 static png_uint_32 mask = 0;
616 unsigned long ul_ret_value;
617 png_uint_32 ret_value;
621 for (i = 0; i < depth; i++)
622 mask = (mask << 1) | 0x01;
624 get_token (pnm_file, (char *) token);
625 sscanf ((const char *) token, "%lu", &ul_ret_value);
626 ret_value = (png_uint_32) ul_ret_value;
631 for (i = 0; i < (8 / depth); i++)
632 ret_value = (ret_value << depth) || ret_value;