1 /*
2 * png2pnm.c --- conversion from PNG-file to PGM/PPM-file
3 * copyright (C) 1999-2019 by Willem van Schaik <willem at schaik dot com>
4 *
5 * This software is released under the MIT license. For conditions of
6 * distribution and use, see the LICENSE file part of this package.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12
13 #ifndef BOOL
14 #define BOOL unsigned char
15 #endif
16 #ifndef TRUE
17 #define TRUE (BOOL) 1
18 #endif
19 #ifndef FALSE
20 #define FALSE (BOOL) 0
21 #endif
22
23 /* make png2pnm verbose so we can find problems (needs to be before png.h) */
24 #ifndef PNG_DEBUG
25 #define PNG_DEBUG 0
26 #endif
27
28 #include "png.h"
29
30 /* function prototypes */
31
32 int main (int argc, char *argv[]);
33 void usage ();
34 BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
35 BOOL raw, BOOL alpha);
36
37 /*
38 * main
39 */
40
main(int argc,char * argv[])41 int main (int argc, char *argv[])
42 {
43 FILE *fp_rd = stdin;
44 FILE *fp_wr = stdout;
45 FILE *fp_al = NULL;
46 BOOL raw = TRUE;
47 BOOL alpha = FALSE;
48 int argi;
49
50 for (argi = 1; argi < argc; argi++)
51 {
52 if (argv[argi][0] == '-')
53 {
54 switch (argv[argi][1])
55 {
56 case 'n':
57 raw = FALSE;
58 break;
59 case 'r':
60 raw = TRUE;
61 break;
62 case 'a':
63 alpha = TRUE;
64 argi++;
65 if ((fp_al = fopen (argv[argi], "wb")) == NULL)
66 {
67 fprintf (stderr, "PNM2PNG\n");
68 fprintf (stderr, "Error: cannot create alpha-channel file %s\n",
69 argv[argi]);
70 exit (1);
71 }
72 break;
73 case 'h':
74 case '?':
75 usage ();
76 exit (0);
77 break;
78 default:
79 fprintf (stderr, "PNG2PNM\n");
80 fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
81 usage ();
82 exit (1);
83 break;
84 } /* end switch */
85 }
86 else if (fp_rd == stdin)
87 {
88 if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
89 {
90 fprintf (stderr, "PNG2PNM\n");
91 fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
92 exit (1);
93 }
94 }
95 else if (fp_wr == stdout)
96 {
97 if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
98 {
99 fprintf (stderr, "PNG2PNM\n");
100 fprintf (stderr, "Error: cannot create file %s\n", argv[argi]);
101 exit (1);
102 }
103 }
104 else
105 {
106 fprintf (stderr, "PNG2PNM\n");
107 fprintf (stderr, "Error: too many parameters\n");
108 usage ();
109 exit (1);
110 }
111 } /* end for */
112
113 #if defined(O_BINARY) && (O_BINARY != 0)
114 /* set stdin/stdout if required to binary */
115 if (fp_rd == stdin)
116 setmode (fileno (stdin), O_BINARY);
117 if ((raw) && (fp_wr == stdout))
118 setmode (fileno (stdout), O_BINARY);
119 #endif
120
121 /* call the conversion program itself */
122 if (png2pnm (fp_rd, fp_wr, fp_al, raw, alpha) == FALSE)
123 {
124 fprintf (stderr, "PNG2PNM\n");
125 fprintf (stderr, "Error: unsuccessful conversion of PNG-image\n");
126 exit (1);
127 }
128
129 /* close input file */
130 fclose (fp_rd);
131 /* close output file */
132 fclose (fp_wr);
133 /* close alpha file */
134 if (alpha)
135 fclose (fp_al);
136
137 return 0;
138 }
139
140 /*
141 * usage
142 */
143
usage()144 void usage ()
145 {
146 fprintf (stderr, "PNG2PNM\n");
147 fprintf (stderr, " by Willem van Schaik, 1999\n");
148 fprintf (stderr, "Usage: png2pnm [options] <file>.png [<file>.pnm]\n");
149 fprintf (stderr, " or: ... | png2pnm [options]\n");
150 fprintf (stderr, "Options:\n");
151 fprintf (stderr,
152 " -r[aw] write pnm-file in binary format (P4/P5/P6) (default)\n");
153 fprintf (stderr, " -n[oraw] write pnm-file in ascii format (P1/P2/P3)\n");
154 fprintf (stderr,
155 " -a[lpha] <file>.pgm write PNG alpha channel as pgm-file\n");
156 fprintf (stderr, " -h | -? print this help-information\n");
157 }
158
159 /*
160 * png2pnm
161 */
162
png2pnm(FILE * png_file,FILE * pnm_file,FILE * alpha_file,BOOL raw,BOOL alpha)163 BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
164 BOOL raw, BOOL alpha)
165 {
166 png_struct *png_ptr = NULL;
167 png_info *info_ptr = NULL;
168 png_byte buf[8];
169 png_byte *png_pixels = NULL;
170 png_byte **row_pointers = NULL;
171 png_byte *pix_ptr = NULL;
172 png_uint_32 row_bytes;
173
174 png_uint_32 width;
175 png_uint_32 height;
176 int bit_depth;
177 int channels;
178 int color_type;
179 int alpha_present;
180 int row, col;
181 int ret;
182 int i;
183 long dep_16;
184
185 /* read and check signature in PNG file */
186 ret = fread (buf, 1, 8, png_file);
187 if (ret != 8)
188 return FALSE;
189
190 ret = png_sig_cmp (buf, 0, 8);
191 if (ret != 0)
192 return FALSE;
193
194 /* create png and info structures */
195
196 png_ptr = png_create_read_struct (png_get_libpng_ver(NULL),
197 NULL, NULL, NULL);
198 if (!png_ptr)
199 return FALSE; /* out of memory */
200
201 info_ptr = png_create_info_struct (png_ptr);
202 if (!info_ptr)
203 {
204 png_destroy_read_struct (&png_ptr, NULL, NULL);
205 return FALSE; /* out of memory */
206 }
207
208 if (setjmp (png_jmpbuf (png_ptr)))
209 {
210 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
211 if(row_pointers != NULL)
212 {
213 free(row_pointers);
214 row_pointers = NULL;
215 }
216 if(png_pixels != NULL)
217 {
218 free(png_pixels);
219 png_pixels = NULL;
220 }
221 return FALSE;
222 }
223
224 /* set up the input control for C streams */
225 png_init_io (png_ptr, png_file);
226 png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
227
228 /* read the file information */
229 png_read_info (png_ptr, info_ptr);
230
231 /* get size and bit-depth of the PNG-image */
232 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
233 NULL, NULL, NULL);
234
235 /* set-up the transformations */
236
237 /* transform paletted images into full-color rgb */
238 if (color_type == PNG_COLOR_TYPE_PALETTE)
239 png_set_expand (png_ptr);
240 /* expand images to bit-depth 8 (only applicable for grayscale images) */
241 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
242 png_set_expand (png_ptr);
243 /* transform transparency maps into full alpha-channel */
244 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
245 png_set_expand (png_ptr);
246
247 #ifdef NJET
248 /* downgrade 16-bit images to 8-bit */
249 if (bit_depth == 16)
250 png_set_strip_16 (png_ptr);
251 /* transform grayscale images into full-color */
252 if (color_type == PNG_COLOR_TYPE_GRAY ||
253 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
254 png_set_gray_to_rgb (png_ptr);
255 /* only if file has a file gamma, we do a correction */
256 if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
257 png_set_gamma (png_ptr, (double) 2.2, file_gamma);
258 #endif
259
260 /* all transformations have been registered; now update info_ptr data,
261 * get rowbytes and channels, and allocate image memory */
262
263 png_read_update_info (png_ptr, info_ptr);
264
265 /* get the new color-type and bit-depth (after expansion/stripping) */
266 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
267 NULL, NULL, NULL);
268
269 /* check for 16-bit files */
270 if (bit_depth == 16)
271 {
272 raw = FALSE;
273 #if defined(O_BINARY) && (O_BINARY != 0)
274 setmode (fileno (pnm_file), O_BINARY);
275 #endif
276 }
277
278 /* calculate new number of channels and store alpha-presence */
279 if (color_type == PNG_COLOR_TYPE_GRAY)
280 channels = 1;
281 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
282 channels = 2;
283 else if (color_type == PNG_COLOR_TYPE_RGB)
284 channels = 3;
285 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
286 channels = 4;
287 else
288 channels = 0; /* should never happen */
289 alpha_present = (channels - 1) % 2;
290
291 /* check if alpha is expected to be present in file */
292 if (alpha && !alpha_present)
293 {
294 fprintf (stderr, "PNG2PNM\n");
295 fprintf (stderr, "Error: PNG-file doesn't contain alpha channel\n");
296 exit (1);
297 }
298
299 /* row_bytes is the width x number of channels x (bit-depth / 8) */
300 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
301
302 if ((row_bytes == 0) ||
303 ((size_t) height > (size_t) (-1) / (size_t) row_bytes))
304 {
305 /* too big */
306 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
307 return FALSE;
308 }
309 if ((png_pixels = (png_byte *)
310 malloc ((size_t) row_bytes * (size_t) height)) == NULL)
311 {
312 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
313 return FALSE;
314 }
315
316 if ((row_pointers = (png_byte **)
317 malloc ((size_t) height * sizeof (png_byte *))) == NULL)
318 {
319 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
320 free (png_pixels);
321 return FALSE;
322 }
323
324 /* set the individual row_pointers to point at the correct offsets */
325 for (i = 0; i < ((int) height); i++)
326 row_pointers[i] = png_pixels + i * row_bytes;
327
328 /* now we can go ahead and just read the whole image */
329 png_read_image (png_ptr, row_pointers);
330
331 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
332 png_read_end (png_ptr, info_ptr);
333
334 /* clean up after the read, and free any memory allocated - REQUIRED */
335 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
336
337 /* write header of PNM file */
338
339 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
340 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
341 {
342 fprintf (pnm_file, "%s\n", (raw) ? "P5" : "P2");
343 fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
344 fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
345 }
346 else if ((color_type == PNG_COLOR_TYPE_RGB) ||
347 (color_type == PNG_COLOR_TYPE_RGB_ALPHA))
348 {
349 fprintf (pnm_file, "%s\n", (raw) ? "P6" : "P3");
350 fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
351 fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
352 }
353
354 /* write header of PGM file with alpha channel */
355
356 if ((alpha) &&
357 ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
358 (color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
359 {
360 fprintf (alpha_file, "%s\n", (raw) ? "P5" : "P2");
361 fprintf (alpha_file, "%d %d\n", (int) width, (int) height);
362 fprintf (alpha_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
363 }
364
365 /* write data to PNM file */
366 pix_ptr = png_pixels;
367
368 for (row = 0; row < (int) height; row++)
369 {
370 for (col = 0; col < (int) width; col++)
371 {
372 for (i = 0; i < (channels - alpha_present); i++)
373 {
374 if (raw)
375 {
376 fputc ((int) *pix_ptr++, pnm_file);
377 }
378 else
379 {
380 if (bit_depth == 16)
381 {
382 dep_16 = (long) *pix_ptr++;
383 fprintf (pnm_file, "%ld ", (dep_16 << 8) + ((long) *pix_ptr++));
384 }
385 else
386 {
387 fprintf (pnm_file, "%ld ", (long) *pix_ptr++);
388 }
389 }
390 }
391 if (alpha_present)
392 {
393 if (!alpha)
394 {
395 pix_ptr++; /* alpha */
396 if (bit_depth == 16)
397 pix_ptr++;
398 }
399 else /* output alpha-channel as pgm file */
400 {
401 if (raw)
402 {
403 fputc ((int) *pix_ptr++, alpha_file);
404 }
405 else
406 {
407 if (bit_depth == 16)
408 {
409 dep_16 = (long) *pix_ptr++;
410 fprintf (alpha_file, "%ld ", (dep_16 << 8) + (long) *pix_ptr++);
411 }
412 else
413 {
414 fprintf (alpha_file, "%ld ", (long) *pix_ptr++);
415 }
416 }
417 }
418 } /* end if alpha_present */
419
420 if (!raw)
421 if (col % 4 == 3)
422 fprintf (pnm_file, "\n");
423 } /* end for col */
424
425 if (!raw)
426 if (col % 4 != 0)
427 fprintf (pnm_file, "\n");
428 } /* end for row */
429
430 if (row_pointers != NULL)
431 free (row_pointers);
432 if (png_pixels != NULL)
433 free (png_pixels);
434
435 return TRUE;
436
437 } /* end of source */
438