2 * pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
3 * copyright (C) 1999-2019 by Willem van Schaik <willem at schaik dot com>
5 * This software is released under the MIT license. For conditions of
6 * distribution and use, see the LICENSE file part of this package.
14 #define BOOL unsigned char
20 #define FALSE (BOOL) 0
23 /* make pnm2png verbose so we can find problems (needs to be before png.h) */
30 /* function prototypes */
32 int main (int argc, char *argv[]);
34 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
35 BOOL interlace, BOOL alpha);
36 void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size);
37 png_uint_32 get_data (FILE *pnm_file, int depth);
38 png_uint_32 get_value (FILE *pnm_file, int depth);
44 int main (int argc, char *argv[])
49 BOOL interlace = FALSE;
53 for (argi = 1; argi < argc; argi++)
55 if (argv[argi][0] == '-')
57 switch (argv[argi][1])
65 if ((fp_al = fopen (argv[argi], "rb")) == NULL)
67 fprintf (stderr, "PNM2PNG\n");
68 fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
79 fprintf (stderr, "PNM2PNG\n");
80 fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
86 else if (fp_rd == stdin)
88 if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
90 fprintf (stderr, "PNM2PNG\n");
91 fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
95 else if (fp_wr == stdout)
97 if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
99 fprintf (stderr, "PNM2PNG\n");
100 fprintf (stderr, "Error: cannot create PNG-file %s\n", argv[argi]);
106 fprintf (stderr, "PNM2PNG\n");
107 fprintf (stderr, "Error: too many parameters\n");
113 #if defined(O_BINARY) && (O_BINARY != 0)
114 /* set stdin/stdout to binary,
115 * we're reading the PNM always! in binary format
118 setmode (fileno (stdin), O_BINARY);
120 setmode (fileno (stdout), O_BINARY);
123 /* call the conversion program itself */
124 if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
126 fprintf (stderr, "PNM2PNG\n");
127 fprintf (stderr, "Error: unsuccessful converting to PNG-image\n");
131 /* close input file */
133 /* close output file */
135 /* close alpha file */
148 fprintf (stderr, "PNM2PNG\n");
149 fprintf (stderr, " by Willem van Schaik, 1999\n");
150 fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
151 fprintf (stderr, " or: ... | pnm2png [options]\n");
152 fprintf (stderr, "Options:\n");
153 fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n");
155 " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
156 fprintf (stderr, " -h | -? print this help-information\n");
163 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
164 BOOL interlace, BOOL alpha)
166 png_struct *png_ptr = NULL;
167 png_info *info_ptr = NULL;
168 png_byte *png_pixels = NULL;
169 png_byte **row_pointers = NULL;
170 png_byte *pix_ptr = NULL;
171 volatile png_uint_32 row_bytes;
174 char width_token[16];
175 char height_token[16];
176 char maxval_token[16];
177 volatile int color_type = 1;
178 unsigned long ul_width = 0, ul_alpha_width = 0;
179 unsigned long ul_height = 0, ul_alpha_height = 0;
180 unsigned long ul_maxval = 0;
181 volatile png_uint_32 width = 0, height = 0;
182 volatile png_uint_32 alpha_width = 0, alpha_height = 0;
184 volatile int bit_depth = 0;
187 int alpha_present = 0;
189 BOOL raw, alpha_raw = FALSE;
190 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
191 BOOL packed_bitmap = FALSE;
196 /* read header of PNM file */
198 get_token (pnm_file, type_token, sizeof (type_token));
199 if (type_token[0] != 'P')
203 else if ((type_token[1] == '1') || (type_token[1] == '4'))
205 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
206 raw = (type_token[1] == '4');
207 color_type = PNG_COLOR_TYPE_GRAY;
208 get_token (pnm_file, width_token, sizeof (width_token));
209 sscanf (width_token, "%lu", &ul_width);
210 width = (png_uint_32) ul_width;
211 get_token (pnm_file, height_token, sizeof (height_token));
212 sscanf (height_token, "%lu", &ul_height);
213 height = (png_uint_32) ul_height;
215 packed_bitmap = TRUE;
217 fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and\n");
218 fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
222 else if ((type_token[1] == '2') || (type_token[1] == '5'))
224 raw = (type_token[1] == '5');
225 color_type = PNG_COLOR_TYPE_GRAY;
226 get_token (pnm_file, width_token, sizeof (width_token));
227 sscanf (width_token, "%lu", &ul_width);
228 width = (png_uint_32) ul_width;
229 get_token (pnm_file, height_token, sizeof (height_token));
230 sscanf (height_token, "%lu", &ul_height);
231 height = (png_uint_32) ul_height;
232 get_token (pnm_file, maxval_token, sizeof (maxval_token));
233 sscanf (maxval_token, "%lu", &ul_maxval);
234 maxval = (png_uint_32) ul_maxval;
238 else if (maxval <= 3)
240 else if (maxval <= 15)
242 else if (maxval <= 255)
244 else if (maxval <= 65535U)
246 else /* maxval > 65535U */
249 else if ((type_token[1] == '3') || (type_token[1] == '6'))
251 raw = (type_token[1] == '6');
252 color_type = PNG_COLOR_TYPE_RGB;
253 get_token (pnm_file, width_token, sizeof (width_token));
254 sscanf (width_token, "%lu", &ul_width);
255 width = (png_uint_32) ul_width;
256 get_token (pnm_file, height_token, sizeof (height_token));
257 sscanf (height_token, "%lu", &ul_height);
258 height = (png_uint_32) ul_height;
259 get_token (pnm_file, maxval_token, sizeof (maxval_token));
260 sscanf (maxval_token, "%lu", &ul_maxval);
261 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 <= 65535U)
272 else /* maxval > 65535U */
280 /* read header of PGM file with alpha channel */
284 if (color_type == PNG_COLOR_TYPE_GRAY)
285 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
286 if (color_type == PNG_COLOR_TYPE_RGB)
287 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
289 get_token (alpha_file, type_token, sizeof (type_token));
290 if (type_token[0] != 'P')
294 else if ((type_token[1] == '2') || (type_token[1] == '5'))
296 alpha_raw = (type_token[1] == '5');
297 get_token (alpha_file, width_token, sizeof (width_token));
298 sscanf (width_token, "%lu", &ul_alpha_width);
299 alpha_width = (png_uint_32) ul_alpha_width;
300 if (alpha_width != width)
302 get_token (alpha_file, height_token, sizeof (height_token));
303 sscanf (height_token, "%lu", &ul_alpha_height);
304 alpha_height = (png_uint_32) ul_alpha_height;
305 if (alpha_height != height)
307 get_token (alpha_file, maxval_token, sizeof (maxval_token));
308 sscanf (maxval_token, "%lu", &ul_maxval);
309 maxval = (png_uint_32) ul_maxval;
312 else if (maxval <= 3)
314 else if (maxval <= 15)
316 else if (maxval <= 255)
318 else if (maxval <= 65535U)
320 else /* maxval > 65535U */
322 if (alpha_depth != bit_depth)
331 /* calculate the number of channels and store alpha-presence */
332 if (color_type == PNG_COLOR_TYPE_GRAY)
334 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
336 else if (color_type == PNG_COLOR_TYPE_RGB)
338 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
342 channels = 0; /* cannot happen */
345 alpha_present = (channels - 1) % 2;
347 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
350 /* row data is as many bytes as can fit width x channels x bit_depth */
351 row_bytes = (width * channels * bit_depth + 7) / 8;
356 /* row_bytes is the width x number of channels x (bit-depth / 8) */
357 row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
360 if ((row_bytes == 0) ||
361 ((size_t) height > (size_t) (-1) / (size_t) row_bytes))
366 if ((png_pixels = (png_byte *)
367 malloc ((size_t) row_bytes * (size_t) height)) == NULL)
373 /* read data from PNM file */
374 pix_ptr = png_pixels;
376 for (row = 0; row < (int) height; row++)
378 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
381 for (i = 0; i < (int) row_bytes; i++)
383 /* png supports this format natively so no conversion is needed */
384 *pix_ptr++ = get_data (pnm_file, 8);
390 for (col = 0; col < (int) width; col++)
392 for (i = 0; i < (channels - alpha_present); i++)
396 *pix_ptr++ = get_data (pnm_file, bit_depth);
402 *pix_ptr++ = get_value (pnm_file, bit_depth);
406 tmp16 = get_value (pnm_file, bit_depth);
407 *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
409 *pix_ptr = (png_byte) (tmp16 & 0xFF);
415 if (alpha) /* read alpha-channel from pgm file */
419 *pix_ptr++ = get_data (alpha_file, alpha_depth);
423 if (alpha_depth <= 8)
425 *pix_ptr++ = get_value (alpha_file, bit_depth);
429 tmp16 = get_value (alpha_file, bit_depth);
430 *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
431 *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
435 } /* end if packed_bitmap */
439 /* prepare the standard PNG structures */
440 png_ptr = png_create_write_struct (png_get_libpng_ver(NULL),
447 info_ptr = png_create_info_struct (png_ptr);
450 png_destroy_write_struct (&png_ptr, NULL);
455 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
456 if (packed_bitmap == TRUE)
458 png_set_packing (png_ptr);
459 png_set_invert_mono (png_ptr);
463 if (setjmp (png_jmpbuf (png_ptr)))
465 png_destroy_write_struct (&png_ptr, &info_ptr);
470 /* initialize the png structure */
471 png_init_io (png_ptr, png_file);
473 /* we're going to write more or less the same PNG as the input file */
474 png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
475 (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
476 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
478 /* write the file header information */
479 png_write_info (png_ptr, info_ptr);
481 /* if needed we will allocate memory for an new array of row-pointers */
482 if (row_pointers == NULL)
484 if ((row_pointers = (png_byte **)
485 malloc (height * sizeof (png_byte *))) == NULL)
487 png_destroy_write_struct (&png_ptr, &info_ptr);
493 /* set the individual row_pointers to point at the correct offsets */
494 for (i = 0; i < (int) height; i++)
495 row_pointers[i] = png_pixels + i * row_bytes;
497 /* write out the entire image data in one call */
498 png_write_image (png_ptr, row_pointers);
500 /* write the additional chunks to the PNG file (not really needed) */
501 png_write_end (png_ptr, info_ptr);
503 /* clean up after the write, and free any memory allocated */
504 png_destroy_write_struct (&png_ptr, &info_ptr);
506 if (row_pointers != NULL)
508 if (png_pixels != NULL)
512 } /* end of pnm2png */
515 * get_token - gets the first string after whitespace
518 void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size)
523 /* remove white-space and comment lines */
526 ret = fgetc (pnm_file);
529 /* the rest of this line is a comment */
532 ret = fgetc (pnm_file);
534 while ((ret != '\n') && (ret != '\r') && (ret != EOF));
536 if (ret == EOF) break;
537 token_buf[i] = (char) ret;
539 while ((ret == '\n') || (ret == '\r') || (ret == ' '));
544 ret = fgetc (pnm_file);
545 if (ret == EOF) break;
546 if (++i == token_buf_size - 1) break;
547 token_buf[i] = (char) ret;
549 while ((ret != '\n') && (ret != '\r') && (ret != ' '));
557 * get_data - takes first byte and converts into next pixel value,
558 * taking as much bits as defined by bit-depth and
559 * using the bit-depth to fill up a byte (0Ah -> AAh)
562 png_uint_32 get_data (FILE *pnm_file, int depth)
564 static int bits_left = 0;
565 static int old_value = 0;
568 png_uint_32 ret_value;
571 for (i = 0; i < depth; i++)
572 mask = (mask >> 1) | 0x80;
576 old_value = fgetc (pnm_file);
580 ret_value = old_value & mask;
581 for (i = 1; i < (8 / depth); i++)
582 ret_value = ret_value || (ret_value >> depth);
584 old_value = (old_value << depth) & 0xFF;
591 * get_value - takes first (numeric) string and converts into number,
592 * using the bit-depth to fill up a byte (0Ah -> AAh)
595 png_uint_32 get_value (FILE *pnm_file, int depth)
597 static png_uint_32 mask = 0;
599 unsigned long ul_ret_value;
600 png_uint_32 ret_value;
604 for (i = 0; i < depth; i++)
605 mask = (mask << 1) | 0x01;
607 get_token (pnm_file, token, sizeof (token));
608 sscanf (token, "%lu", &ul_ret_value);
609 ret_value = (png_uint_32) ul_ret_value;
614 for (i = 0; i < (8 / depth); i++)
615 ret_value = (ret_value << depth) || ret_value;