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.
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
37 /* to make pnm2png verbose so we can find problems (needs to be before png.h) */
44 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
46 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
49 /* function prototypes */
51 int main (int argc, char *argv[]);
53 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha);
54 void get_token(FILE *pnm_file, char *token);
55 png_uint_32 get_data (FILE *pnm_file, int depth);
56 png_uint_32 get_value (FILE *pnm_file, int depth);
62 int main(int argc, char *argv[])
67 BOOL interlace = FALSE;
71 for (argi = 1; argi < argc; argi++)
73 if (argv[argi][0] == '-')
75 switch (argv[argi][1])
83 if ((fp_al = fopen (argv[argi], "rb")) == NULL)
85 fprintf (stderr, "PNM2PNG\n");
86 fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
97 fprintf (stderr, "PNM2PNG\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, "PNM2PNG\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, "PNM2PNG\n");
118 fprintf (stderr, "Error: can not create PNG-file %s\n", argv[argi]);
124 fprintf (stderr, "PNM2PNG\n");
125 fprintf (stderr, "Error: too many parameters\n");
132 /* set stdin/stdout to binary, we're reading the PNM always! in binary format */
135 setmode (STDIN, O_BINARY);
139 setmode (STDOUT, O_BINARY);
143 /* call the conversion program itself */
144 if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
146 fprintf (stderr, "PNM2PNG\n");
147 fprintf (stderr, "Error: unsuccessful converting to PNG-image\n");
151 /* close input file */
153 /* close output file */
155 /* close alpha file */
168 fprintf (stderr, "PNM2PNG\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: pnm2png [options] <file>.<pnm> [<file>.png]\n");
176 fprintf (stderr, " or: ... | pnm2png [options]\n");
177 fprintf (stderr, "Options:\n");
178 fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n");
179 fprintf (stderr, " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
180 fprintf (stderr, " -h | -? print this help-information\n");
187 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha)
189 png_struct *png_ptr = NULL;
190 png_info *info_ptr = NULL;
191 png_byte *png_pixels = NULL;
192 png_byte **row_pointers = NULL;
193 png_byte *pix_ptr = NULL;
194 png_uint_32 row_bytes;
197 char width_token[16];
198 char height_token[16];
199 char maxval_token[16];
201 unsigned long ul_width=0, ul_alpha_width=0;
202 unsigned long ul_height=0, ul_alpha_height=0;
203 unsigned long ul_maxval=0;
204 png_uint_32 width, alpha_width;
205 png_uint_32 height, alpha_height;
212 BOOL raw, alpha_raw = FALSE;
213 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
214 BOOL packed_bitmap = FALSE;
219 /* read header of PNM file */
221 get_token(pnm_file, type_token);
222 if (type_token[0] != 'P')
226 else if ((type_token[1] == '1') || (type_token[1] == '4'))
228 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
229 raw = (type_token[1] == '4');
230 color_type = PNG_COLOR_TYPE_GRAY;
231 get_token(pnm_file, width_token);
232 sscanf (width_token, "%lu", &ul_width);
233 width = (png_uint_32) ul_width;
234 get_token(pnm_file, height_token);
235 sscanf (height_token, "%lu", &ul_height);
236 height = (png_uint_32) ul_height;
238 packed_bitmap = TRUE;
240 fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and \n");
241 fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
244 else if ((type_token[1] == '2') || (type_token[1] == '5'))
246 raw = (type_token[1] == '5');
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;
254 get_token(pnm_file, maxval_token);
255 sscanf (maxval_token, "%lu", &ul_maxval);
256 maxval = (png_uint_32) ul_maxval;
260 else if (maxval <= 3)
262 else if (maxval <= 15)
264 else if (maxval <= 255)
266 else /* if (maxval <= 65535) */
269 else if ((type_token[1] == '3') || (type_token[1] == '6'))
271 raw = (type_token[1] == '6');
272 color_type = PNG_COLOR_TYPE_RGB;
273 get_token(pnm_file, width_token);
274 sscanf (width_token, "%lu", &ul_width);
275 width = (png_uint_32) ul_width;
276 get_token(pnm_file, height_token);
277 sscanf (height_token, "%lu", &ul_height);
278 height = (png_uint_32) ul_height;
279 get_token(pnm_file, maxval_token);
280 sscanf (maxval_token, "%lu", &ul_maxval);
281 maxval = (png_uint_32) ul_maxval;
284 else if (maxval <= 3)
286 else if (maxval <= 15)
288 else if (maxval <= 255)
290 else /* if (maxval <= 65535) */
298 /* read header of PGM file with alpha channel */
302 if (color_type == PNG_COLOR_TYPE_GRAY)
303 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
304 if (color_type == PNG_COLOR_TYPE_RGB)
305 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
307 get_token(alpha_file, type_token);
308 if (type_token[0] != 'P')
312 else if ((type_token[1] == '2') || (type_token[1] == '5'))
314 alpha_raw = (type_token[1] == '5');
315 get_token(alpha_file, width_token);
316 sscanf (width_token, "%lu", &ul_alpha_width);
317 alpha_width=(png_uint_32) ul_alpha_width;
318 if (alpha_width != width)
320 get_token(alpha_file, height_token);
321 sscanf (height_token, "%lu", &ul_alpha_height);
322 alpha_height = (png_uint_32) ul_alpha_height;
323 if (alpha_height != height)
325 get_token(alpha_file, maxval_token);
326 sscanf (maxval_token, "%lu", &ul_maxval);
327 maxval = (png_uint_32) ul_maxval;
330 else if (maxval <= 3)
332 else if (maxval <= 15)
334 else if (maxval <= 255)
336 else /* if (maxval <= 65535) */
338 if (alpha_depth != bit_depth)
347 /* calculate the number of channels and store alpha-presence */
348 if (color_type == PNG_COLOR_TYPE_GRAY)
350 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
352 else if (color_type == PNG_COLOR_TYPE_RGB)
354 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
357 channels = 0; /* should not happen */
359 alpha_present = (channels - 1) % 2;
361 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
363 /* row data is as many bytes as can fit width x channels x bit_depth */
364 row_bytes = (width * channels * bit_depth + 7) / 8;
367 /* row_bytes is the width x number of channels x (bit-depth / 8) */
368 row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
370 if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL)
373 /* read data from PNM file */
374 pix_ptr = png_pixels;
376 for (row = 0; row < height; row++)
378 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
380 for (i = 0; i < row_bytes; i++)
381 /* png supports this format natively so no conversion is needed */
382 *pix_ptr++ = get_data (pnm_file, 8);
386 for (col = 0; col < width; col++)
388 for (i = 0; i < (channels - alpha_present); i++)
391 *pix_ptr++ = get_data (pnm_file, bit_depth);
394 *pix_ptr++ = get_value (pnm_file, bit_depth);
397 tmp16 = get_value (pnm_file, bit_depth);
398 *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
400 *pix_ptr = (png_byte) (tmp16 & 0xFF);
405 if (alpha) /* read alpha-channel from pgm file */
408 *pix_ptr++ = get_data (alpha_file, alpha_depth);
410 if (alpha_depth <= 8)
411 *pix_ptr++ = get_value (alpha_file, bit_depth);
414 tmp16 = get_value (alpha_file, bit_depth);
415 *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
416 *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
419 } /* if packed_bitmap */
423 /* prepare the standard PNG structures */
424 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
429 info_ptr = png_create_info_struct (png_ptr);
432 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
436 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
437 if (packed_bitmap == TRUE)
439 png_set_packing (png_ptr);
440 png_set_invert_mono (png_ptr);
444 /* setjmp() must be called in every function that calls a PNG-reading libpng function */
445 if (setjmp (png_jmpbuf(png_ptr)))
447 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
451 /* initialize the png structure */
452 png_init_io (png_ptr, png_file);
454 /* we're going to write more or less the same PNG as the input file */
455 png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
456 (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
457 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
459 /* write the file header information */
460 png_write_info (png_ptr, info_ptr);
462 /* if needed we will allocate memory for an new array of row-pointers */
463 if (row_pointers == (unsigned char**) NULL)
465 if ((row_pointers = (png_byte **) malloc (height * sizeof (png_bytep))) == NULL)
467 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
472 /* set the individual row_pointers to point at the correct offsets */
473 for (i = 0; i < (height); i++)
474 row_pointers[i] = png_pixels + i * row_bytes;
476 /* write out the entire image data in one call */
477 png_write_image (png_ptr, row_pointers);
479 /* write the additional chuncks to the PNG file (not really needed) */
480 png_write_end (png_ptr, info_ptr);
482 /* clean up after the write, and free any memory allocated */
483 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
485 if (row_pointers != (unsigned char**) NULL)
487 if (png_pixels != (unsigned char*) NULL)
491 } /* end of pnm2png */
494 * get_token() - gets the first string after whitespace
497 void get_token(FILE *pnm_file, char *token)
502 /* remove white-space and comment lines */
505 ret = fgetc(pnm_file);
507 /* the rest of this line is a comment */
510 ret = fgetc(pnm_file);
512 while ((ret != '\n') && (ret != '\r') && (ret != EOF));
514 if (ret == EOF) break;
515 token[i] = (unsigned char) ret;
517 while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' '));
522 ret = fgetc(pnm_file);
523 if (ret == EOF) break;
525 token[i] = (unsigned char) ret;
527 while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' '));
535 * get_data() - takes first byte and converts into next pixel value,
536 * taking as much bits as defined by bit-depth and
537 * using the bit-depth to fill up a byte (0Ah -> AAh)
540 png_uint_32 get_data (FILE *pnm_file, int depth)
542 static int bits_left = 0;
543 static int old_value = 0;
546 png_uint_32 ret_value;
549 for (i = 0; i < depth; i++)
550 mask = (mask >> 1) | 0x80;
554 old_value = fgetc (pnm_file);
558 ret_value = old_value & mask;
559 for (i = 1; i < (8 / depth); i++)
560 ret_value = ret_value || (ret_value >> depth);
562 old_value = (old_value << depth) & 0xFF;
569 * get_value() - takes first (numeric) string and converts into number,
570 * using the bit-depth to fill up a byte (0Ah -> AAh)
573 png_uint_32 get_value (FILE *pnm_file, int depth)
575 static png_uint_32 mask = 0;
577 unsigned long ul_ret_value;
578 png_uint_32 ret_value;
582 for (i = 0; i < depth; i++)
583 mask = (mask << 1) | 0x01;
585 get_token (pnm_file, (char *) token);
586 sscanf ((const char *) token, "%lu", &ul_ret_value);
587 ret_value = (png_uint_32) ul_ret_value;
592 for (i = 0; i < (8 / depth); i++)
593 ret_value = (ret_value << depth) || ret_value;