w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pngtopnm.c
Go to the documentation of this file.
1 /*
2 ** pngtopnm.c -
3 ** read a Portable Network Graphics file and produce a portable anymap
4 **
5 ** Copyright (C) 1995,1998 by Alexander Lehmann <alex@hal.rhein-main.de>
6 ** and Willem van Schaik <willem@schaik.com>
7 **
8 ** Permission to use, copy, modify, and distribute this software and its
9 ** documentation for any purpose and without fee is hereby granted, provided
10 ** that the above copyright notice appear in all copies and that both that
11 ** copyright notice and this permission notice appear in supporting
12 ** documentation. This software is provided "as is" without express or
13 ** implied warranty.
14 **
15 ** modeled after giftopnm by David Koblas and
16 ** with lots of bits pasted from libpng.txt by Guy Eric Schalnat
17 */
18 
19 #define VERSION "2.37.3 (13 July 1999) +netpbm"
20 
21 /*
22  BJH 20000408: rename PPM_MAXMAXVAL to PPM_OVERALLMAXVAL
23  BJH 20000303: fix include statement so dependencies work out right.
24 */
25 /* GRR 19990713: fixed redundant freeing of png_ptr and info_ptr in setjmp()
26  * blocks and added "pm_close(ifp)" in each. */
27 
28 /* GRR 19990317: declared "clobberable" automatic variables in convertpng()
29  * static to fix Solaris/gcc stack-corruption bug. Also installed custom
30  * error-handler to avoid jmp_buf size-related problems (i.e., jmp_buf
31  * compiled with one size in libpng and another size here). */
32 
33 #include <math.h>
34 #include <png.h> /* includes zlib.h and setjmp.h */
35 #include "pnm.h"
36 
37 typedef struct _jmpbuf_wrapper {
38  jmp_buf jmpbuf;
40 
41 #ifndef TRUE
42 # define TRUE 1
43 #endif
44 #ifndef FALSE
45 # define FALSE 0
46 #endif
47 #ifndef NONE
48 # define NONE 0
49 #endif
50 
51 /* function prototypes */
52 #ifdef __STDC__
53 static png_uint_16 _get_png_val (png_byte **pp, int bit_depth);
55  png_uint_16 a);
56 static int iscolor (png_color c);
57 static void save_text (png_info *info_ptr, FILE *tfp);
58 static void show_time (png_info *info_ptr);
60 static void convertpng (FILE *ifp, FILE *tfp);
61 int main (int argc, char *argv[]);
62 #endif
63 
66 
68 static png_uint_16 bgr, bgg, bgb; /* background colors */
69 static int verbose = FALSE;
70 static enum alpha_handling alpha = none;
71 static int background = -1;
72 static char *backstring;
73 static float displaygamma = -1.0; /* display gamma */
74 static float totalgamma = -1.0;
75 static int text = FALSE;
76 static char *text_file;
77 static int mtime = FALSE;
79 
80 #define get_png_val(p) _get_png_val (&(p), info_ptr->bit_depth)
81 
82 #ifdef __STDC__
83 static png_uint_16 _get_png_val (png_byte **pp, int bit_depth)
84 #else
85 static png_uint_16 _get_png_val (pp, bit_depth)
86 png_byte **pp;
87 int bit_depth;
88 #endif
89 {
90  png_uint_16 c = 0;
91 
92  if (bit_depth == 16) {
93  c = (*((*pp)++)) << 8;
94  }
95  c |= (*((*pp)++));
96 
97  if (maxval > maxmaxval)
98  c /= ((maxval + 1) / (maxmaxval + 1));
99 
100  return c;
101 }
102 
103 #ifdef __STDC__
105 #else
106 static void store_pixel (pix, r, g, b, a)
107 xel *pix;
108 png_uint_16 r, g, b, a;
109 #endif
110 {
111  if (alpha == alpha_only) {
112  PNM_ASSIGN1 (*pix, a);
113  } else {
114  if ((alpha == mix) && (a != maxval)) {
115  r = r * (double)a / maxval + ((1.0 - (double)a / maxval) * bgr);
116  g = g * (double)a / maxval + ((1.0 - (double)a / maxval) * bgg);
117  b = b * (double)a / maxval + ((1.0 - (double)a / maxval) * bgb);
118  }
119  PPM_ASSIGN (*pix, r, g, b);
120  }
121 }
122 
123 #ifdef __STDC__
124 static png_uint_16 gamma_correct (png_uint_16 v, float g)
125 #else
127 png_uint_16 v;
128 float g;
129 #endif
130 {
131  if (g != -1.0)
132  return (png_uint_16) (pow ((double) v / maxval,
133  (1.0 / g)) * maxval + 0.5);
134  else
135  return v;
136 }
137 
138 #ifdef __STDC__
139 static int iscolor (png_color c)
140 #else
141 static int iscolor (c)
142 png_color c;
143 #endif
144 {
145  return c.red != c.green || c.green != c.blue;
146 }
147 
148 #ifdef __STDC__
149 static void save_text (png_info *info_ptr, FILE *tfp)
150 #else
151 static void save_text (info_ptr, tfp)
152 png_info *info_ptr;
153 FILE *tfp;
154 #endif
155 {
156  int i, j, k;
157 
158  for (i = 0 ; i < info_ptr->num_text ; i++) {
159  j = 0;
160  while (info_ptr->text[i].key[j] != '\0' && info_ptr->text[i].key[j] != ' ')
161  j++;
162  if (info_ptr->text[i].key[j] != ' ') {
163  fprintf (tfp, "%s", info_ptr->text[i].key);
164  for (j = strlen (info_ptr->text[i].key) ; j < 15 ; j++)
165  putc (' ', tfp);
166  } else {
167  fprintf (tfp, "\"%s\"", info_ptr->text[i].key);
168  for (j = strlen (info_ptr->text[i].key) ; j < 13 ; j++)
169  putc (' ', tfp);
170  }
171  putc (' ', tfp); /* at least one space between key and text */
172 
173  for (j = 0 ; j < info_ptr->text[i].text_length ; j++) {
174  putc (info_ptr->text[i].text[j], tfp);
175  if (info_ptr->text[i].text[j] == '\n')
176  for (k = 0 ; k < 16 ; k++)
177  putc ((int)' ', tfp);
178  }
179  putc ((int)'\n', tfp);
180  }
181 }
182 
183 #ifdef __STDC__
184 static void show_time (png_info *info_ptr)
185 #else
186 static void show_time (info_ptr)
187 png_info *info_ptr;
188 #endif
189 {
190  static char *month[] =
191  {"", "January", "February", "March", "April", "May", "June",
192  "July", "August", "September", "October", "November", "December"};
193 
194  if (info_ptr->valid & PNG_INFO_tIME) {
195  pm_message ("modification time: %02d %s %d %02d:%02d:%02d",
196  info_ptr->mod_time.day, month[info_ptr->mod_time.month],
197  info_ptr->mod_time.year, info_ptr->mod_time.hour,
198  info_ptr->mod_time.minute, info_ptr->mod_time.second);
199  }
200 }
201 
202 #ifdef __STDC__
204 #else
208 #endif
209 {
210  jmpbuf_wrapper *jmpbuf_ptr;
211 
212  /* this function, aside from the extra step of retrieving the "error
213  * pointer" (below) and the fact that it exists within the application
214  * rather than within libpng, is essentially identical to libpng's
215  * default error handler. The second point is critical: since both
216  * setjmp() and longjmp() are called from the same code, they are
217  * guaranteed to have compatible notions of how big a jmp_buf is,
218  * regardless of whether _BSD_SOURCE or anything else has (or has not)
219  * been defined. */
220 
221  fprintf(stderr, "pnmtopng: fatal libpng error: %s\n", msg);
222  fflush(stderr);
223 
224  jmpbuf_ptr = png_get_error_ptr(png_ptr);
225  if (jmpbuf_ptr == NULL) { /* we are completely hosed now */
226  fprintf(stderr,
227  "pnmtopng: EXTREMELY fatal error: jmpbuf unrecoverable; terminating.\n");
228  fflush(stderr);
229  exit(99);
230  }
231 
232  longjmp(jmpbuf_ptr->jmpbuf, 1);
233 }
234 
235 #define SIG_CHECK_SIZE 4
236 
237 #ifdef __STDC__
238 static void convertpng (FILE *ifp, FILE *tfp)
239 #else
240 static void convertpng (ifp, tfp)
241 FILE *ifp;
242 FILE *tfp;
243 #endif
244 {
245  unsigned char sig_buf [SIG_CHECK_SIZE];
247  png_info *info_ptr;
248  pixel *row;
250  png_byte *png_pixel;
251  pixel *pnm_pixel;
252  int x, y;
253  int linesize;
254  png_uint_16 c, c2, c3, a;
255  int pnm_type;
256  int i;
257  int trans_mix;
259  char gamma_string[80];
260 
261  /* these variables are declared static because gcc wasn't kidding
262  * about "variable XXX might be clobbered by `longjmp' or `vfork'"
263  * (stack corruption observed on Solaris 2.6 with gcc 2.8.1, even
264  * in the absence of any other error condition) */
265  static char *type_string;
266  static char *alpha_string;
267 
268  type_string = alpha_string = "";
269 
270  if (fread (sig_buf, 1, SIG_CHECK_SIZE, ifp) != SIG_CHECK_SIZE) {
271  pm_close (ifp);
272  pm_error ("input file empty or too short");
273  }
274  if (png_sig_cmp (sig_buf, (png_size_t) 0, (png_size_t) SIG_CHECK_SIZE) != 0) {
275  pm_close (ifp);
276  pm_error ("input file not a PNG file");
277  }
278 
281  if (png_ptr == NULL) {
282  pm_close (ifp);
283  pm_error ("cannot allocate LIBPNG structure");
284  }
285 
286  info_ptr = png_create_info_struct (png_ptr);
287  if (info_ptr == NULL) {
289  pm_close (ifp);
290  pm_error ("cannot allocate LIBPNG structures");
291  }
292 
293  if (setjmp (pngtopnm_jmpbuf_struct.jmpbuf)) {
295  pm_close (ifp);
296  pm_error ("setjmp returns error condition");
297  }
298 
301  png_read_info (png_ptr, info_ptr);
302 
303  if (verbose) {
304  switch (info_ptr->color_type) {
305  case PNG_COLOR_TYPE_GRAY:
306  type_string = "gray";
307  alpha_string = "";
308  break;
309 
311  type_string = "gray";
312  alpha_string = "+alpha";
313  break;
314 
316  type_string = "palette";
317  alpha_string = "";
318  break;
319 
320  case PNG_COLOR_TYPE_RGB:
321  type_string = "truecolor";
322  alpha_string = "";
323  break;
324 
326  type_string = "truecolor";
327  alpha_string = "+alpha";
328  break;
329  }
330  if (info_ptr->valid & PNG_INFO_tRNS) {
331  alpha_string = "+transparency";
332  }
333 
334  if (info_ptr->valid & PNG_INFO_gAMA) {
335  sprintf (gamma_string, ", image gamma = %4.2f", info_ptr->gamma);
336  } else {
337  strcpy (gamma_string, "");
338  }
339 
340  if (verbose)
341  pm_message ("reading a %d x %d image, %d bit%s %s%s%s%s",
342  info_ptr->width, info_ptr->height,
343  info_ptr->bit_depth, info_ptr->bit_depth > 1 ? "s" : "",
344  type_string, alpha_string, gamma_string,
345  info_ptr->interlace_type ? ", Adam7 interlaced" : "");
346  }
347 
348  png_image = (png_byte **)malloc (info_ptr->height * sizeof (png_byte*));
349  if (png_image == NULL) {
351  pm_close (ifp);
352  pm_error ("couldn't allocate space for image");
353  }
354 
355  if (info_ptr->bit_depth == 16)
356  linesize = 2 * info_ptr->width;
357  else
358  linesize = info_ptr->width;
359 
360  if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
361  linesize *= 2;
362  else
363  if (info_ptr->color_type == PNG_COLOR_TYPE_RGB)
364  linesize *= 3;
365  else
366  if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
367  linesize *= 4;
368 
369  for (y = 0 ; y < info_ptr->height ; y++) {
370  png_image[y] = malloc (linesize);
371  if (png_image[y] == NULL) {
372  for (x = 0 ; x < y ; x++)
373  free (png_image[x]);
374  free (png_image);
376  pm_close (ifp);
377  pm_error ("couldn't allocate space for image");
378  }
379  }
380 
381  if (info_ptr->bit_depth < 8)
383 
384  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
385  maxval = 255;
386  } else {
387  maxval = (1l << info_ptr->bit_depth) - 1;
388  }
389 
390  /* gamma-correction */
391  if (displaygamma != -1.0) {
392  if (info_ptr->valid & PNG_INFO_gAMA) {
393  if (displaygamma != info_ptr->gamma) {
394  png_set_gamma (png_ptr, displaygamma, info_ptr->gamma);
395  totalgamma = (double) info_ptr->gamma * (double) displaygamma;
396  /* in case of gamma-corrections, sBIT's as in the PNG-file are not valid anymore */
397  info_ptr->valid &= ~~PNG_INFO_sBIT;
398  if (verbose)
399  pm_message ("image gamma is %4.2f, converted for display gamma of %4.2f",
400  info_ptr->gamma, displaygamma);
401  }
402  } else {
403  if (displaygamma != info_ptr->gamma) {
405  totalgamma = (double) displaygamma;
406  info_ptr->valid &= ~~PNG_INFO_sBIT;
407  if (verbose)
408  pm_message ("image gamma assumed 1.0, converted for display gamma of %4.2f",
409  displaygamma);
410  }
411  }
412  }
413 
414  /* sBIT handling is very tricky. If we are extracting only the image, we
415  can use the sBIT info for grayscale and color images, if the three
416  values agree. If we extract the transparency/alpha mask, sBIT is
417  irrelevant for trans and valid for alpha. If we mix both, the
418  multiplication may result in values that require the normal bit depth,
419  so we will use the sBIT info only for transparency, if we know that only
420  solid and fully transparent is used */
421 
422  if (info_ptr->valid & PNG_INFO_sBIT) {
423  switch (alpha) {
424  case mix:
425  if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
426  info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
427  break;
428  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
429  (info_ptr->valid & PNG_INFO_tRNS)) {
430  trans_mix = TRUE;
431  for (i = 0 ; i < info_ptr->num_trans ; i++)
432  if (info_ptr->trans[i] != 0 && info_ptr->trans[i] != 255) {
433  trans_mix = FALSE;
434  break;
435  }
436  if (!trans_mix)
437  break;
438  }
439 
440  /* else fall though to normal case */
441 
442  case none:
443  if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
444  info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
445  info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
446  (info_ptr->sig_bit.red != info_ptr->sig_bit.green ||
447  info_ptr->sig_bit.red != info_ptr->sig_bit.blue) &&
448  alpha == none) {
449  pm_message ("different bit depths for color channels not supported");
450  pm_message ("writing file with %d bit resolution", info_ptr->bit_depth);
451  } else {
452  if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) &&
453  (info_ptr->sig_bit.red < 255)) {
454  for (i = 0 ; i < info_ptr->num_palette ; i++) {
455  info_ptr->palette[i].red >>= (8 - info_ptr->sig_bit.red);
456  info_ptr->palette[i].green >>= (8 - info_ptr->sig_bit.green);
457  info_ptr->palette[i].blue >>= (8 - info_ptr->sig_bit.blue);
458  }
459  maxval = (1l << info_ptr->sig_bit.red) - 1;
460  if (verbose)
461  pm_message ("image has fewer significant bits, writing file with %d bits per channel",
462  info_ptr->sig_bit.red);
463  } else
464  if ((info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
465  info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
466  (info_ptr->sig_bit.red < info_ptr->bit_depth)) {
467  png_set_shift (png_ptr, &(info_ptr->sig_bit));
468  maxval = (1l << info_ptr->sig_bit.red) - 1;
469  if (verbose)
470  pm_message ("image has fewer significant bits, writing file with %d bits per channel",
471  info_ptr->sig_bit.red);
472  } else
473  if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
474  info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) &&
475  (info_ptr->sig_bit.gray < info_ptr->bit_depth)) {
476  png_set_shift (png_ptr, &(info_ptr->sig_bit));
477  maxval = (1l << info_ptr->sig_bit.gray) - 1;
478  if (verbose)
479  pm_message ("image has fewer significant bits, writing file with %d bits",
480  info_ptr->sig_bit.gray);
481  }
482  }
483  break;
484 
485  case alpha_only:
486  if ((info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
487  info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) &&
488  (info_ptr->sig_bit.gray < info_ptr->bit_depth)) {
489  png_set_shift (png_ptr, &(info_ptr->sig_bit));
490  if (verbose)
491  pm_message ("image has fewer significant bits, writing file with %d bits",
492  info_ptr->sig_bit.alpha);
493  maxval = (1l << info_ptr->sig_bit.alpha) - 1;
494  }
495  break;
496 
497  }
498  }
499 
500  /* didn't manage to get libpng to work (bugs?) concerning background */
501  /* processing, therefore we do our own using bgr, bgg and bgb */
502  if (info_ptr->valid & PNG_INFO_bKGD)
503  switch (info_ptr->color_type) {
504  case PNG_COLOR_TYPE_GRAY:
506  bgr = bgg = bgb = gamma_correct (info_ptr->background.gray, totalgamma);
507  break;
509  bgr = gamma_correct (info_ptr->palette[info_ptr->background.index].red, totalgamma);
510  bgg = gamma_correct (info_ptr->palette[info_ptr->background.index].green, totalgamma);
511  bgb = gamma_correct (info_ptr->palette[info_ptr->background.index].blue, totalgamma);
512  break;
513  case PNG_COLOR_TYPE_RGB:
515  bgr = gamma_correct (info_ptr->background.red, totalgamma);
516  bgg = gamma_correct (info_ptr->background.green, totalgamma);
517  bgb = gamma_correct (info_ptr->background.blue, totalgamma);
518  break;
519  }
520  else
521  /* when no background given, we use white [from version 2.37] */
522  bgr = bgg = bgb = maxval;
523 
524  /* but if background was specified from the command-line, we always use that */
525  /* I chose to do no gamma-correction in this case; which is a bit arbitrary */
526  if (background > -1)
527  {
529  switch (info_ptr->color_type) {
530  case PNG_COLOR_TYPE_GRAY:
532  bgr = bgg = bgb = PNM_GET1 (backcolor);
533  break;
535  case PNG_COLOR_TYPE_RGB:
537  bgr = PPM_GETR (backcolor);
538  bgg = PPM_GETG (backcolor);
539  bgb = PPM_GETB (backcolor);
540  break;
541  }
542  }
543 
545  png_read_end (png_ptr, info_ptr);
546 
547  if (mtime)
548  show_time (info_ptr);
549  if (text)
550  save_text (info_ptr, tfp);
551 
552  if (info_ptr->valid & PNG_INFO_pHYs) {
553  float r;
554  r = (float)info_ptr->x_pixels_per_unit / info_ptr->y_pixels_per_unit;
555  if (r != 1.0) {
556  pm_message ("warning - non-square pixels; to fix do a 'pnmscale -%cscale %g'",
557  r < 1.0 ? 'x' : 'y',
558  r < 1.0 ? 1.0 / r : r );
559  }
560  }
561 
562  if ((row = pnm_allocrow (info_ptr->width)) == NULL) {
563  for (y = 0 ; y < info_ptr->height ; y++)
564  free (png_image[y]);
565  free (png_image);
567  pm_close (ifp);
568  pm_error ("couldn't allocate space for image");
569  }
570 
571  if (alpha == alpha_only) {
572  if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
573  info_ptr->color_type == PNG_COLOR_TYPE_RGB) {
574  pnm_type = PBM_TYPE;
575  } else
576  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
577  pnm_type = PBM_TYPE;
578  if (info_ptr->valid & PNG_INFO_tRNS) {
579  for (i = 0 ; i < info_ptr->num_trans ; i++) {
580  if (info_ptr->trans[i] != 0 && info_ptr->trans[i] != maxval) {
581  pnm_type = PGM_TYPE;
582  break;
583  }
584  }
585  }
586  } else {
587  if (maxval == 1)
588  pnm_type = PBM_TYPE;
589  else
590  pnm_type = PGM_TYPE;
591  }
592  } else {
593  if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
594  info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
595  if (info_ptr->bit_depth == 1) {
596  pnm_type = PBM_TYPE;
597  } else {
598  pnm_type = PGM_TYPE;
599  }
600  } else
601  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
602  pnm_type = PGM_TYPE;
603  for (i = 0 ; i < info_ptr->num_palette ; i++) {
604  if (iscolor (info_ptr->palette[i])) {
605  pnm_type = PPM_TYPE;
606  break;
607  }
608  }
609  } else {
610  pnm_type = PPM_TYPE;
611  }
612  }
613 
614  if ((pnm_type == PGM_TYPE) && (maxval > PGM_OVERALLMAXVAL))
616  else if ((pnm_type == PPM_TYPE) && (maxval > PPM_OVERALLMAXVAL))
618  else maxmaxval = maxval;
619 
620  if (verbose)
621  pm_message ("writing a %s file (maxval=%u)",
622  pnm_type == PBM_TYPE ? "PBM" :
623  pnm_type == PGM_TYPE ? "PGM" :
624  pnm_type == PPM_TYPE ? "PPM" :
625  "UNKNOWN!",
626  maxmaxval);
627 
628  pnm_writepnminit (stdout, info_ptr->width, info_ptr->height, maxval,
629  pnm_type, FALSE);
630 
631  for (y = 0 ; y < info_ptr->height ; y++) {
632  png_pixel = png_image[y];
633  pnm_pixel = row;
634  for (x = 0 ; x < info_ptr->width ; x++) {
635  c = get_png_val (png_pixel);
636  switch (info_ptr->color_type) {
637  case PNG_COLOR_TYPE_GRAY:
638  store_pixel (pnm_pixel, c, c, c,
639  ((info_ptr->valid & PNG_INFO_tRNS) &&
640  (c == gamma_correct (info_ptr->trans_values.gray, totalgamma))) ?
641  0 : maxval);
642  break;
643 
645  a = get_png_val (png_pixel);
646  store_pixel (pnm_pixel, c, c, c, a);
647  break;
648 
650  store_pixel (pnm_pixel, info_ptr->palette[c].red,
651  info_ptr->palette[c].green, info_ptr->palette[c].blue,
652  (info_ptr->valid & PNG_INFO_tRNS) &&
653  c<info_ptr->num_trans ?
654  info_ptr->trans[c] : maxval);
655  break;
656 
657  case PNG_COLOR_TYPE_RGB:
658  c2 = get_png_val (png_pixel);
659  c3 = get_png_val (png_pixel);
660  store_pixel (pnm_pixel, c, c2, c3,
661  ((info_ptr->valid & PNG_INFO_tRNS) &&
662  (c == gamma_correct (info_ptr->trans_values.red, totalgamma)) &&
663  (c2 == gamma_correct (info_ptr->trans_values.green, totalgamma)) &&
664  (c3 == gamma_correct (info_ptr->trans_values.blue, totalgamma))) ?
665  0 : maxval);
666  break;
667 
669  c2 = get_png_val (png_pixel);
670  c3 = get_png_val (png_pixel);
671  a = get_png_val (png_pixel);
672  store_pixel (pnm_pixel, c, c2, c3, a);
673  break;
674 
675  default:
676  pnm_freerow (row);
677  for (i = 0 ; i < info_ptr->height ; i++)
678  free (png_image[i]);
679  free (png_image);
681  pm_close (ifp);
682  pm_error ("unknown PNG color type");
683  }
684  pnm_pixel++;
685  }
686  pnm_writepnmrow (stdout, row, info_ptr->width, maxval, pnm_type, FALSE);
687  }
688 
689  fflush(stdout);
690  pnm_freerow (row);
691  for (y = 0 ; y < info_ptr->height ; y++)
692  free (png_image[y]);
693  free (png_image);
695 }
696 
697 #ifdef __STDC__
698 int main (int argc, char *argv[])
699 #else
700 int main (argc, argv)
701 int argc;
702 char *argv[];
703 #endif
704 {
705  FILE *ifp, *tfp;
706  int argn;
707 
708  char *usage = "[-verbose] [-alpha | -mix] [-background color] ...\n\
709  ... [-gamma value] [-text file] [-time] [pngfile]";
710 
711  pnm_init (&argc, argv);
712  argn = 1;
713 
714  while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
715  if (pm_keymatch (argv[argn], "-verbose", 2)) {
716  verbose = TRUE;
717  } else
718  if (pm_keymatch (argv[argn], "-alpha", 2)) {
719  alpha = alpha_only;
720  } else
721  if (pm_keymatch (argv[argn], "-mix", 2)) {
722  alpha = mix;
723  } else
724  if (pm_keymatch (argv[argn], "-background", 2)) {
725  background = 1;
726  if (++argn < argc)
727  backstring = argv[argn];
728  else
729  pm_usage (usage);
730  } else
731  if (pm_keymatch (argv[argn], "-gamma", 2)) {
732  if (++argn < argc)
733  sscanf (argv[argn], "%f", &displaygamma);
734  else
735  pm_usage (usage);
736  } else
737  if (pm_keymatch (argv[argn], "-text", 3)) {
738  text = TRUE;
739  if (++argn < argc)
740  text_file = argv[argn];
741  else
742  pm_usage (usage);
743  } else
744  if (pm_keymatch (argv[argn], "-time", 3)) {
745  mtime = TRUE;
746  } else {
747  fprintf(stderr,"pngtopnm version %s, compiled with libpng version %s\n",
749  pm_usage (usage);
750  }
751  argn++;
752  }
753 
754  if (argn != argc) {
755  ifp = pm_openr (argv[argn]);
756  ++argn;
757  } else {
758  ifp = stdin;
759  }
760  if (argn != argc)
761  pm_usage (usage);
762 
763  if (text)
764  tfp = pm_openw (text_file);
765  else
766  tfp = NULL;
767 
768  convertpng (ifp, tfp);
769 
770  if (text)
771  pm_close (tfp);
772 
773  pm_close (ifp);
774  pm_close (stdout);
775  exit (0);
776 }
double __cdecl pow(double _X, double _Y)
#define month
Definition: aptex-macros.h:807
#define b
Definition: jpegint.h:372
@ FALSE
Definition: dd.h:101
@ TRUE
Definition: dd.h:102
#define free(a)
Definition: decNumber.cpp:310
int v
Definition: dviconv.c:10
#define fread
Definition: xxstdio.h:25
#define fflush
Definition: xxstdio.h:24
int sscanf()
char * strcpy()
mpz_t pp
Definition: gen-psqr.c:108
static char usage[]
Definition: giftopnm.c:59
#define c(n)
Definition: gpos-common.c:150
#define a(n)
Definition: gpos-common.c:148
pix
Definition: in_pcx.cpp:383
#define putc
Definition: jbib.h:20
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
sizeof(AF_ModuleRec)
void exit()
kerning y
Definition: ttdriver.c:212
int png_sig_cmp(png_const_bytep sig, size_t start, size_t num_to_check)
Definition: png.c:74
void png_set_sig_bytes(png_structrp png_ptr, int num_bytes)
Definition: png.c:47
void png_init_io(png_structrp png_ptr, png_FILE_p fp)
Definition: png.c:703
png_infop png_create_info_struct(png_const_structrp png_ptr)
Definition: png.c:354
png_structp() png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)
Definition: pngread.c:27
#define PNG_INFO_gAMA
Definition: png.h:732
#define PNG_COLOR_TYPE_RGB
Definition: png.h:670
#define PNG_INFO_pHYs
Definition: png.h:739
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:671
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:672
void() png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)
Definition: pngread.c:1011
void() png_set_gamma(png_structrp png_ptr, double screen_gamma, double override_file_gamma)
Definition: pngrtran.c:858
void() png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Definition: pngread.c:766
#define PNG_INFO_tIME
Definition: png.h:741
void() png_set_shift(png_structrp png_ptr, png_const_color_8p true_bits)
Definition: pngtrans.c:84
void() png_set_packing(png_structrp png_ptr)
Definition: pngtrans.c:50
void() png_read_image(png_structrp png_ptr, png_bytepp image)
Definition: pngread.c:700
#define PNG_INFO_sBIT
Definition: png.h:733
#define PNG_INFO_tRNS
Definition: png.h:736
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:668
#define PNG_INFO_bKGD
Definition: png.h:737
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:669
#define PNG_LIBPNG_VER_STRING
Definition: png.h:281
void() png_read_info(png_structrp png_ptr, png_inforp info_ptr)
Definition: pngread.c:92
png_voidp() png_get_error_ptr(png_const_structrp png_ptr)
Definition: pngerror.c:856
unsigned short png_uint_16
Definition: pngconf.h:497
unsigned char png_byte
Definition: pngconf.h:481
size_t png_size_t
Definition: pngconf.h:523
const char * png_const_charp
Definition: pngconf.h:590
#define fprintf
Definition: mendex.h:64
#define malloc
Definition: alloca.c:91
#define png_ptr(N)
Definition: image.h:99
FILE * text_file
Definition: tie.c:122
#define sprintf
Definition: snprintf.c:44
float x
Definition: cordic.py:15
char msg[512]
Definition: nsfix.c:80
int k
Definition: otp-parser.c:70
FILE * pm_openw(char *name)
Definition: libpbm1.c:624
void pm_usage(char *usage)
Definition: libpbm1.c:343
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
#define PBM_TYPE
Definition: pbm.h:21
static bit backcolor
Definition: pbmmask.c:20
#define PGM_TYPE
Definition: pgm.h:45
#define PGM_OVERALLMAXVAL
Definition: pgm.h:34
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
#define pm_error
Definition: png22pnm.c:118
#define pm_keymatch(stra, strb, _x)
Definition: png22pnm.c:121
#define pnm_init(_a, _b)
Definition: png22pnm.c:114
#define pm_close(file)
Definition: png22pnm.c:120
#define pm_message
Definition: png22pnm.c:116
static void store_pixel(xel *pix, png_uint_16 r, png_uint_16 g, png_uint_16 b, png_uint_16 a)
Definition: pngtopnm.c:106
static int verbose
Definition: pngtopnm.c:69
static float displaygamma
Definition: pngtopnm.c:73
static png_uint_16 bgg
Definition: pngtopnm.c:68
static png_uint_16 bgr
Definition: pngtopnm.c:68
#define VERSION
Definition: pngtopnm.c:19
static void show_time(png_info *info_ptr)
Definition: pngtopnm.c:186
static png_uint_16 maxmaxval
Definition: pngtopnm.c:67
static int mtime
Definition: pngtopnm.c:77
static png_uint_16 bgb
Definition: pngtopnm.c:68
static png_uint_16 maxval
Definition: pngtopnm.c:67
#define SIG_CHECK_SIZE
Definition: pngtopnm.c:235
static jmpbuf_wrapper pngtopnm_jmpbuf_struct
Definition: pngtopnm.c:78
static void pngtopnm_error_handler(png_structp png_ptr, png_const_charp msg)
Definition: pngtopnm.c:205
static int iscolor(png_color c)
Definition: pngtopnm.c:141
static float totalgamma
Definition: pngtopnm.c:74
static png_uint_16 gamma_correct(png_uint_16 v, float g)
Definition: pngtopnm.c:126
alpha_handling
Definition: pngtopnm.c:65
@ mix
Definition: pngtopnm.c:65
@ none
Definition: pngtopnm.c:65
@ alpha_only
Definition: pngtopnm.c:65
static void convertpng(FILE *ifp, FILE *tfp)
Definition: pngtopnm.c:240
static png_uint_16 _get_png_val(png_byte **pp, int bit_depth)
Definition: pngtopnm.c:85
static void save_text(png_info *info_ptr, FILE *tfp)
Definition: pngtopnm.c:151
#define get_png_val(p)
Definition: pngtopnm.c:80
static char * text_file
Definition: pngtopnm.c:76
static char * backstring
Definition: pngtopnm.c:72
static enum alpha_handling alpha
Definition: pngtopnm.c:70
static int background
Definition: pngtopnm.c:71
struct _jmpbuf_wrapper jmpbuf_wrapper
int main(int argc, argv)
Definition: pngtopnm.c:700
void pnm_writepnminit(FILE *file, int cols, int rows, xelval maxval, int format, int forceplain)
Definition: libpnm2.c:29
void pnm_writepnmrow(FILE *file, xel *xelrow, int cols, xelval maxval, int format, int forceplain)
Definition: libpnm2.c:62
#define PNM_ASSIGN1(x, v)
Definition: pnm.h:13
#define PNM_GET1(x)
Definition: pnm.h:12
#define pnm_freerow(xelrow)
Definition: pnm.h:24
#define pnm_allocrow(cols)
Definition: pnm.h:22
pixel ppm_parsecolor(char *colorname, pixval maxval)
Definition: libppm4.c:139
#define PPM_GETR(p)
Definition: ppm.h:36
#define PPM_ASSIGN(p, red, grn, blu)
Definition: ppm.h:46
#define PPM_OVERALLMAXVAL
Definition: ppm.h:30
#define PPM_GETG(p)
Definition: ppm.h:37
#define PPM_GETB(p)
Definition: ppm.h:38
#define PPM_TYPE
Definition: ppm.h:59
int g
Definition: ppmqvga.c:68
int r
Definition: ppmqvga.c:68
static int row
Definition: ps2pk.c:587
jmp_buf jmpbuf
Definition: pngtopnm.c:38
Definition: texview.c:48
Definition: ppm.h:33
int width
Definition: pngimage.h:56
int height
Definition: pngimage.h:57
#define c2
Definition: t1io.c:53
#define FILE
Definition: t1stdio.h:34
int j
Definition: t4ht.c:1589
*job_name strlen((char *) job_name) - 4)
FILE * ifp
Definition: t1asm.c:88
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269
#define argn