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.
23 #define BOOL unsigned char
29 #define FALSE (BOOL) 0
36 /* to make pnm2png verbose so we can find problems (needs to be before png.h) */
43 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
45 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
48 /* function prototypes */
50 int main (int argc, char *argv[]);
52 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha);
53 void get_token(FILE *pnm_file, char *token);
54 png_uint_32 get_data (FILE *pnm_file, int depth);
55 png_uint_32 get_value (FILE *pnm_file, int depth);
61 int main(int argc, char *argv[])
66 BOOL interlace = FALSE;
70 for (argi = 1; argi < argc; argi++)
72 if (argv[argi][0] == '-')
74 switch (argv[argi][1])
82 if ((fp_al = fopen (argv[argi], "rb")) == NULL)
84 fprintf (stderr, "PNM2PNG\n");
85 fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
96 fprintf (stderr, "PNM2PNG\n");
97 fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
103 else if (fp_rd == stdin)
105 if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
107 fprintf (stderr, "PNM2PNG\n");
108 fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
112 else if (fp_wr == stdout)
114 if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
116 fprintf (stderr, "PNM2PNG\n");
117 fprintf (stderr, "Error: can not create PNG-file %s\n", argv[argi]);
123 fprintf (stderr, "PNM2PNG\n");
124 fprintf (stderr, "Error: too many parameters\n");
131 /* set stdin/stdout to binary, we're reading the PNM always! in binary format */
134 setmode (STDIN, O_BINARY);
138 setmode (STDOUT, O_BINARY);
142 /* call the conversion program itself */
143 if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
145 fprintf (stderr, "PNM2PNG\n");
146 fprintf (stderr, "Error: unsuccessful converting to PNG-image\n");
150 /* close input file */
152 /* close output file */
154 /* close alpha file */
167 fprintf (stderr, "PNM2PNG\n");
168 fprintf (stderr, " by Willem van Schaik, 1999\n");
170 fprintf (stderr, " for Turbo-C and Borland-C compilers\n");
172 fprintf (stderr, " for Linux (and Unix) compilers\n");
174 fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
175 fprintf (stderr, " or: ... | pnm2png [options]\n");
176 fprintf (stderr, "Options:\n");
177 fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n");
178 fprintf (stderr, " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
179 fprintf (stderr, " -h | -? print this help-information\n");
186 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha)
188 png_struct *png_ptr = NULL;
189 png_info *info_ptr = NULL;
190 png_byte *png_pixels = NULL;
191 png_byte **row_pointers = NULL;
192 png_byte *pix_ptr = NULL;
193 png_uint_32 row_bytes;
196 char width_token[16];
197 char height_token[16];
198 char maxval_token[16];
200 png_uint_32 width, alpha_width;
201 png_uint_32 height, alpha_height;
208 BOOL raw, alpha_raw = FALSE;
212 /* read header of PNM file */
214 get_token(pnm_file, type_token);
215 if (type_token[0] != 'P')
219 else if ((type_token[1] == '1') || (type_token[1] == '4'))
221 raw = (type_token[1] == '4');
222 color_type = PNG_COLOR_TYPE_GRAY;
225 else if ((type_token[1] == '2') || (type_token[1] == '5'))
227 raw = (type_token[1] == '5');
228 color_type = PNG_COLOR_TYPE_GRAY;
229 get_token(pnm_file, width_token);
230 sscanf (width_token, "%lu", &width);
231 get_token(pnm_file, height_token);
232 sscanf (height_token, "%lu", &height);
233 get_token(pnm_file, maxval_token);
234 sscanf (maxval_token, "%lu", &maxval);
237 else if (maxval <= 3)
239 else if (maxval <= 15)
241 else if (maxval <= 255)
243 else /* if (maxval <= 65535) */
246 else if ((type_token[1] == '3') || (type_token[1] == '6'))
248 raw = (type_token[1] == '6');
249 color_type = PNG_COLOR_TYPE_RGB;
250 get_token(pnm_file, width_token);
251 sscanf (width_token, "%lu", &width);
252 get_token(pnm_file, height_token);
253 sscanf (height_token, "%lu", &height);
254 get_token(pnm_file, maxval_token);
255 sscanf (maxval_token, "%lu", &maxval);
258 else if (maxval <= 3)
260 else if (maxval <= 15)
262 else if (maxval <= 255)
264 else /* if (maxval <= 65535) */
272 /* read header of PGM file with alpha channel */
276 if (color_type == PNG_COLOR_TYPE_GRAY)
277 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
278 if (color_type == PNG_COLOR_TYPE_RGB)
279 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
281 get_token(alpha_file, type_token);
282 if (type_token[0] != 'P')
286 else if ((type_token[1] == '2') || (type_token[1] == '5'))
288 alpha_raw = (type_token[1] == '5');
289 get_token(alpha_file, width_token);
290 sscanf (width_token, "%lu", &alpha_width);
291 if (alpha_width != width)
293 get_token(alpha_file, height_token);
294 sscanf (height_token, "%lu", &alpha_height);
295 if (alpha_height != height)
297 get_token(alpha_file, maxval_token);
298 sscanf (maxval_token, "%lu", &maxval);
301 else if (maxval <= 3)
303 else if (maxval <= 15)
305 else if (maxval <= 255)
307 else /* if (maxval <= 65535) */
309 if (alpha_depth != bit_depth)
318 /* calculate the number of channels and store alpha-presence */
319 if (color_type == PNG_COLOR_TYPE_GRAY)
321 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
323 else if (color_type == PNG_COLOR_TYPE_RGB)
325 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
328 channels = 0; /* should not happen */
330 alpha_present = (channels - 1) % 2;
332 /* row_bytes is the width x number of channels x (bit-depth / 8) */
333 row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
335 if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL)
338 /* read data from PNM file */
339 pix_ptr = png_pixels;
341 for (row = 0; row < height; row++)
343 for (col = 0; col < width; col++)
345 for (i = 0; i < (channels - alpha_present); i++)
348 *pix_ptr++ = get_data (pnm_file, bit_depth);
351 *pix_ptr++ = get_value (pnm_file, bit_depth);
354 tmp16 = get_value (pnm_file, bit_depth);
355 *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
357 *pix_ptr = (png_byte) (tmp16 & 0xFF);
362 if (alpha) /* read alpha-channel from pgm file */
365 *pix_ptr++ = get_data (alpha_file, alpha_depth);
367 if (alpha_depth <= 8)
368 *pix_ptr++ = get_value (alpha_file, bit_depth);
371 tmp16 = get_value (alpha_file, bit_depth);
372 *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
373 *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
380 /* prepare the standard PNG structures */
381 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
386 info_ptr = png_create_info_struct (png_ptr);
389 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
393 /* setjmp() must be called in every function that calls a PNG-reading libpng function */
394 if (setjmp (png_jmpbuf(png_ptr)))
396 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
400 /* initialize the png structure */
401 png_init_io (png_ptr, png_file);
403 /* we're going to write more or less the same PNG as the input file */
404 png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
405 (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
406 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
408 /* write the file header information */
409 png_write_info (png_ptr, info_ptr);
411 /* if needed we will allocate memory for an new array of row-pointers */
412 if (row_pointers == (unsigned char**) NULL)
414 if ((row_pointers = (png_byte **) malloc (height * sizeof (png_bytep))) == NULL)
416 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
421 /* set the individual row_pointers to point at the correct offsets */
422 for (i = 0; i < (height); i++)
423 row_pointers[i] = png_pixels + i * row_bytes;
425 /* write out the entire image data in one call */
426 png_write_image (png_ptr, row_pointers);
428 /* write the additional chuncks to the PNG file (not really needed) */
429 png_write_end (png_ptr, info_ptr);
431 /* clean up after the write, and free any memory allocated */
432 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
434 if (row_pointers != (unsigned char**) NULL)
436 if (png_pixels != (unsigned char*) NULL)
440 } /* end of pnm2png */
443 * get_token() - gets the first string after whitespace
446 void get_token(FILE *pnm_file, char *token)
450 /* remove white-space */
453 token[i] = (unsigned char) fgetc (pnm_file);
455 while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' '));
461 token[i] = (unsigned char) fgetc (pnm_file);
463 while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' '));
471 * get_data() - takes first byte and converts into next pixel value,
472 * taking as much bits as defined by bit-depth and
473 * using the bit-depth to fill up a byte (0Ah -> AAh)
476 png_uint_32 get_data (FILE *pnm_file, int depth)
478 static int bits_left = 0;
479 static int old_value = 0;
482 png_uint_32 ret_value;
485 for (i = 0; i < depth; i++)
486 mask = (mask >> 1) | 0x80;
490 old_value = fgetc (pnm_file);
494 ret_value = old_value & mask;
495 for (i = 1; i < (8 / depth); i++)
496 ret_value = ret_value || (ret_value >> depth);
498 old_value = (old_value << depth) & 0xFF;
505 * get_value() - takes first (numeric) string and converts into number,
506 * using the bit-depth to fill up a byte (0Ah -> AAh)
509 png_uint_32 get_value (FILE *pnm_file, int depth)
511 static png_uint_32 mask = 0;
513 png_uint_32 ret_value;
517 for (i = 0; i < depth; i++)
518 mask = (mask << 1) | 0x01;
520 get_token (pnm_file, (char *) token);
521 sscanf ((const char *) token, "%lu", &ret_value);
526 for (i = 0; i < (8 / depth); i++)
527 ret_value = (ret_value << depth) || ret_value;