Lines 1-894
Link Here
|
1 |
$OpenBSD: patch-converter_other_pngtopnm_c,v 1.2 2011/12/13 21:39:17 naddy Exp $ |
|
|
2 |
|
3 |
Fix build with png-1.5. |
4 |
|
5 |
--- converter/other/pngtopnm.c.orig Fri Nov 25 01:15:46 2011 |
6 |
+++ converter/other/pngtopnm.c Tue Dec 13 22:25:03 2011 |
7 |
@@ -44,12 +44,6 @@ |
8 |
#include "nstring.h" |
9 |
#include "shhopt.h" |
10 |
|
11 |
-#if PNG_LIBPNG_VER >= 10400 |
12 |
-#error Your PNG library (<png.h>) is incompatible with this Netpbm source code. |
13 |
-#error You need either an older PNG library (older than 1.4) |
14 |
-#error newer Netpbm source code (at least 10.48) |
15 |
-#endif |
16 |
- |
17 |
typedef struct _jmpbuf_wrapper { |
18 |
jmp_buf jmpbuf; |
19 |
} jmpbuf_wrapper; |
20 |
@@ -187,7 +181,7 @@ parseCommandLine(int argc, |
21 |
|
22 |
|
23 |
|
24 |
-#define get_png_val(p) _get_png_val (&(p), info_ptr->bit_depth) |
25 |
+#define get_png_val(p) _get_png_val (&(p), png_get_bit_depth(png_ptr, info_ptr)) |
26 |
|
27 |
static png_uint_16 |
28 |
_get_png_val (png_byte ** const pp, |
29 |
@@ -266,33 +260,39 @@ png_color c; |
30 |
} |
31 |
|
32 |
#ifdef __STDC__ |
33 |
-static void save_text (png_info *info_ptr, FILE *tfp) |
34 |
+static void save_text (png_structp png_ptr, png_info *info_ptr, FILE *tfp) |
35 |
#else |
36 |
-static void save_text (info_ptr, tfp) |
37 |
+static void save_text (png_ptr, info_ptr, tfp) |
38 |
+png_structp png_ptr; |
39 |
png_info *info_ptr; |
40 |
FILE *tfp; |
41 |
#endif |
42 |
{ |
43 |
int i, j, k; |
44 |
+ png_textp text_ptr; |
45 |
+ int num_text; |
46 |
|
47 |
- for (i = 0 ; i < info_ptr->num_text ; i++) { |
48 |
+ if (png_get_text(png_ptr, info_ptr, &text_ptr, &num_text) == 0) |
49 |
+ return; |
50 |
+ |
51 |
+ for (i = 0 ; i < num_text ; i++) { |
52 |
j = 0; |
53 |
- while (info_ptr->text[i].key[j] != '\0' && info_ptr->text[i].key[j] != ' ') |
54 |
+ while (text_ptr[i].key[j] != '\0' && text_ptr[i].key[j] != ' ') |
55 |
j++; |
56 |
- if (info_ptr->text[i].key[j] != ' ') { |
57 |
- fprintf (tfp, "%s", info_ptr->text[i].key); |
58 |
- for (j = strlen (info_ptr->text[i].key) ; j < 15 ; j++) |
59 |
+ if (text_ptr[i].key[j] != ' ') { |
60 |
+ fprintf (tfp, "%s", text_ptr[i].key); |
61 |
+ for (j = strlen (text_ptr[i].key) ; j < 15 ; j++) |
62 |
putc (' ', tfp); |
63 |
} else { |
64 |
- fprintf (tfp, "\"%s\"", info_ptr->text[i].key); |
65 |
- for (j = strlen (info_ptr->text[i].key) ; j < 13 ; j++) |
66 |
+ fprintf (tfp, "\"%s\"", text_ptr[i].key); |
67 |
+ for (j = strlen (text_ptr[i].key) ; j < 13 ; j++) |
68 |
putc (' ', tfp); |
69 |
} |
70 |
putc (' ', tfp); /* at least one space between key and text */ |
71 |
|
72 |
- for (j = 0 ; j < info_ptr->text[i].text_length ; j++) { |
73 |
- putc (info_ptr->text[i].text[j], tfp); |
74 |
- if (info_ptr->text[i].text[j] == '\n') |
75 |
+ for (j = 0 ; j < text_ptr[i].text_length ; j++) { |
76 |
+ putc (text_ptr[i].text[j], tfp); |
77 |
+ if (text_ptr[i].text[j] == '\n') |
78 |
for (k = 0 ; k < 16 ; k++) |
79 |
putc ((int)' ', tfp); |
80 |
} |
81 |
@@ -301,9 +301,10 @@ FILE *tfp; |
82 |
} |
83 |
|
84 |
#ifdef __STDC__ |
85 |
-static void show_time (png_info *info_ptr) |
86 |
+static void show_time (png_structp png_ptr, png_info *info_ptr) |
87 |
#else |
88 |
-static void show_time (info_ptr) |
89 |
+static void show_time (png_ptr, info_ptr) |
90 |
+png_structp png_ptr; |
91 |
png_info *info_ptr; |
92 |
#endif |
93 |
{ |
94 |
@@ -311,19 +312,20 @@ png_info *info_ptr; |
95 |
"", "January", "February", "March", "April", "May", "June", |
96 |
"July", "August", "September", "October", "November", "December" |
97 |
}; |
98 |
+ png_timep mod_time; |
99 |
|
100 |
- if (info_ptr->valid & PNG_INFO_tIME) { |
101 |
- if (info_ptr->mod_time.month < 1 || |
102 |
- info_ptr->mod_time.month >= ARRAY_SIZE(month)) { |
103 |
+ if (png_get_tIME(png_ptr, info_ptr, &mod_time) & PNG_INFO_tIME) { |
104 |
+ if (mod_time->month < 1 || |
105 |
+ mod_time->month >= ARRAY_SIZE(month)) { |
106 |
pm_message("tIME chunk in PNG input is invalid; " |
107 |
"modification time of image is unknown. " |
108 |
"The month value, which should be in the range " |
109 |
- "1-12, is %u", info_ptr->mod_time.month); |
110 |
+ "1-12, is %u", mod_time->month); |
111 |
} else |
112 |
pm_message ("modification time: %02d %s %d %02d:%02d:%02d", |
113 |
- info_ptr->mod_time.day, month[info_ptr->mod_time.month], |
114 |
- info_ptr->mod_time.year, info_ptr->mod_time.hour, |
115 |
- info_ptr->mod_time.minute, info_ptr->mod_time.second); |
116 |
+ mod_time->day, month[mod_time->month], |
117 |
+ mod_time->year, mod_time->hour, |
118 |
+ mod_time->minute, mod_time->second); |
119 |
} |
120 |
} |
121 |
|
122 |
@@ -360,12 +362,28 @@ png_const_charp msg; |
123 |
|
124 |
|
125 |
static void |
126 |
-dump_png_info(png_info *info_ptr) { |
127 |
+dump_png_info(png_structp png_ptr, png_info *info_ptr) { |
128 |
|
129 |
const char *type_string; |
130 |
const char *filter_string; |
131 |
+ png_color_16p background; |
132 |
+ int num_trans; |
133 |
+ double gamma; |
134 |
+ png_color_8p sig_bit; |
135 |
+ png_colorp palette; |
136 |
+ int num_palette; |
137 |
+ png_uint_16p hist; |
138 |
+ int res_x, res_y, unit_type; |
139 |
+ png_int_32 offset_x, offset_y; |
140 |
+ png_timep mod_time; |
141 |
+ png_charp purpose; |
142 |
+ png_int_32 X0, X1; |
143 |
+ int type, nparams; |
144 |
+ png_charp units; |
145 |
+ png_charpp params; |
146 |
+ int file_srgb_intent; |
147 |
|
148 |
- switch (info_ptr->color_type) { |
149 |
+ switch (png_get_color_type(png_ptr, info_ptr)) { |
150 |
case PNG_COLOR_TYPE_GRAY: |
151 |
type_string = "gray"; |
152 |
break; |
153 |
@@ -387,90 +405,101 @@ dump_png_info(png_info *info_ptr) { |
154 |
break; |
155 |
} |
156 |
|
157 |
- switch (info_ptr->filter_type) { |
158 |
+ switch (png_get_filter_type(png_ptr, info_ptr)) { |
159 |
case PNG_FILTER_TYPE_BASE: |
160 |
asprintfN(&filter_string, "base filter"); |
161 |
break; |
162 |
default: |
163 |
asprintfN(&filter_string, "unknown filter type %d", |
164 |
- info_ptr->filter_type); |
165 |
+ png_get_filter_type(png_ptr, info_ptr)); |
166 |
} |
167 |
|
168 |
- pm_message("reading a %ldw x %ldh image, %d bit%s", |
169 |
- info_ptr->width, info_ptr->height, |
170 |
- info_ptr->bit_depth, info_ptr->bit_depth > 1 ? "s" : ""); |
171 |
+ pm_message("reading a %uw x %uh image, %d bit%s", |
172 |
+ png_get_image_width(png_ptr, info_ptr), |
173 |
+ png_get_image_height(png_ptr, info_ptr), |
174 |
+ png_get_bit_depth(png_ptr, info_ptr), |
175 |
+ png_get_bit_depth(png_ptr, info_ptr) > 1 ? "s" : ""); |
176 |
pm_message("%s, %s, %s", |
177 |
type_string, |
178 |
- info_ptr->interlace_type ? |
179 |
+ png_get_interlace_type(png_ptr, info_ptr) ? |
180 |
"Adam7 interlaced" : "not interlaced", |
181 |
filter_string); |
182 |
- pm_message("background {index, gray, red, green, blue} = " |
183 |
- "{%d, %d, %d, %d, %d}", |
184 |
- info_ptr->background.index, |
185 |
- info_ptr->background.gray, |
186 |
- info_ptr->background.red, |
187 |
- info_ptr->background.green, |
188 |
- info_ptr->background.blue); |
189 |
|
190 |
+ if (png_get_bKGD(png_ptr, info_ptr, &background) & PNG_INFO_bKGD) { |
191 |
+ pm_message("background {index, gray, red, green, blue} = " |
192 |
+ "{%d, %d, %d, %d, %d}", |
193 |
+ background->index, |
194 |
+ background->gray, |
195 |
+ background->red, |
196 |
+ background->green, |
197 |
+ background->blue); |
198 |
+ } |
199 |
+ |
200 |
strfree(filter_string); |
201 |
|
202 |
- if (info_ptr->valid & PNG_INFO_tRNS) |
203 |
+ if (png_get_tRNS(png_ptr, info_ptr, |
204 |
+ NULL, &num_trans, NULL) & PNG_INFO_tRNS) |
205 |
pm_message("tRNS chunk (transparency): %u entries", |
206 |
- info_ptr->num_trans); |
207 |
+ num_trans); |
208 |
else |
209 |
pm_message("tRNS chunk (transparency): not present"); |
210 |
|
211 |
- if (info_ptr->valid & PNG_INFO_gAMA) |
212 |
- pm_message("gAMA chunk (image gamma): gamma = %4.2f", info_ptr->gamma); |
213 |
+ if (png_get_gAMA(png_ptr, info_ptr, &gamma) & PNG_INFO_gAMA) |
214 |
+ pm_message("gAMA chunk (image gamma): gamma = %4.2f", gamma); |
215 |
else |
216 |
pm_message("gAMA chunk (image gamma): not present"); |
217 |
|
218 |
- if (info_ptr->valid & PNG_INFO_sBIT) |
219 |
+ if (png_get_sBIT(png_ptr, info_ptr, &sig_bit) & PNG_INFO_sBIT) |
220 |
pm_message("sBIT chunk: present"); |
221 |
else |
222 |
pm_message("sBIT chunk: not present"); |
223 |
|
224 |
- if (info_ptr->valid & PNG_INFO_cHRM) |
225 |
+ if (png_get_cHRM(png_ptr, info_ptr, |
226 |
+ NULL, NULL, NULL, NULL, |
227 |
+ NULL, NULL, NULL, NULL) & PNG_INFO_cHRM) |
228 |
pm_message("cHRM chunk: present"); |
229 |
else |
230 |
pm_message("cHRM chunk: not present"); |
231 |
|
232 |
- if (info_ptr->valid & PNG_INFO_PLTE) |
233 |
- pm_message("PLTE chunk: %d entries", info_ptr->num_palette); |
234 |
+ if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette) & PNG_INFO_PLTE) |
235 |
+ pm_message("PLTE chunk: %d entries", num_palette); |
236 |
else |
237 |
pm_message("PLTE chunk: not present"); |
238 |
|
239 |
- if (info_ptr->valid & PNG_INFO_bKGD) |
240 |
+ if (png_get_bKGD(png_ptr, info_ptr, &background) & PNG_INFO_bKGD) |
241 |
pm_message("bKGD chunk: present"); |
242 |
else |
243 |
pm_message("bKGD chunk: not present"); |
244 |
|
245 |
- if (info_ptr->valid & PNG_INFO_hIST) |
246 |
+ if (png_get_hIST(png_ptr, info_ptr, &hist) & PNG_INFO_hIST) |
247 |
pm_message("hIST chunk: present"); |
248 |
else |
249 |
pm_message("hIST chunk: not present"); |
250 |
|
251 |
- if (info_ptr->valid & PNG_INFO_pHYs) |
252 |
+ if (png_get_pHYs(png_ptr, info_ptr, |
253 |
+ &res_x, &res_y, &unit_type) & PNG_INFO_pHYs) |
254 |
pm_message("pHYs chunk: present"); |
255 |
else |
256 |
pm_message("pHYs chunk: not present"); |
257 |
|
258 |
- if (info_ptr->valid & PNG_INFO_oFFs) |
259 |
+ if (png_get_oFFs(png_ptr, info_ptr, |
260 |
+ &offset_x, &offset_y, &unit_type) & PNG_INFO_oFFs) |
261 |
pm_message("oFFs chunk: present"); |
262 |
else |
263 |
pm_message("oFFs chunk: not present"); |
264 |
|
265 |
- if (info_ptr->valid & PNG_INFO_tIME) |
266 |
+ if (png_get_tIME(png_ptr, info_ptr, &mod_time) & PNG_INFO_tIME) |
267 |
pm_message("tIME chunk: present"); |
268 |
else |
269 |
pm_message("tIME chunk: not present"); |
270 |
|
271 |
- if (info_ptr->valid & PNG_INFO_pCAL) |
272 |
+ if (png_get_pCAL(png_ptr, info_ptr, &purpose, &X0, &X1, |
273 |
+ &type, &nparams, &units, ¶ms) & PNG_INFO_pCAL) |
274 |
pm_message("pCAL chunk: present"); |
275 |
else |
276 |
pm_message("pCAL chunk: not present"); |
277 |
|
278 |
- if (info_ptr->valid & PNG_INFO_sRGB) |
279 |
+ if (png_get_sRGB(png_ptr, info_ptr, &file_srgb_intent) & PNG_INFO_sRGB) |
280 |
pm_message("sRGB chunk: present"); |
281 |
else |
282 |
pm_message("sRGB chunk: not present"); |
283 |
@@ -479,19 +508,19 @@ dump_png_info(png_info *info_ptr) { |
284 |
|
285 |
|
286 |
static bool |
287 |
-isTransparentColor(pngcolor const color, |
288 |
- png_info * const info_ptr, |
289 |
- double const totalgamma) { |
290 |
+isTransparentColor(pngcolor const color, |
291 |
+ png_structp const png_ptr, |
292 |
+ png_info * const info_ptr, |
293 |
+ double const totalgamma) { |
294 |
/*---------------------------------------------------------------------------- |
295 |
Return TRUE iff pixels of color 'color' are supposed to be transparent |
296 |
everywhere they occur. Assume it's an RGB image. |
297 |
-----------------------------------------------------------------------------*/ |
298 |
bool retval; |
299 |
+ png_color_16p transColorP; |
300 |
|
301 |
- if (info_ptr->valid & PNG_INFO_tRNS) { |
302 |
- const png_color_16 * const transColorP = &info_ptr->trans_values; |
303 |
- |
304 |
- |
305 |
+ if (png_get_tRNS(png_ptr, info_ptr, |
306 |
+ NULL, NULL, &transColorP) & PNG_INFO_tRNS) { |
307 |
/* There seems to be a problem here: you can't compare real |
308 |
numbers for equality. Also, I'm not sure the gamma |
309 |
corrected/uncorrected color spaces are right here. |
310 |
@@ -537,9 +566,11 @@ setupGammaCorrection(png_struct * const png_ptr, |
311 |
if (displaygamma == -1.0) |
312 |
*totalgammaP = -1.0; |
313 |
else { |
314 |
+ double fileGamma; |
315 |
float imageGamma; |
316 |
- if (info_ptr->valid & PNG_INFO_gAMA) |
317 |
- imageGamma = info_ptr->gamma; |
318 |
+ |
319 |
+ if (png_get_gAMA(png_ptr, info_ptr, &fileGamma) & PNG_INFO_gAMA) |
320 |
+ imageGamma = fileGamma; |
321 |
else { |
322 |
if (verbose) |
323 |
pm_message("PNG doesn't specify image gamma. Assuming 1.0"); |
324 |
@@ -555,10 +586,14 @@ setupGammaCorrection(png_struct * const png_ptr, |
325 |
} else { |
326 |
png_set_gamma(png_ptr, displaygamma, imageGamma); |
327 |
*totalgammaP = imageGamma * displaygamma; |
328 |
+#ifdef NOT_SUPPORTED_ANYMORE |
329 |
+ /* The API doesn't clearing PNG_INFO_sBIT. */ |
330 |
+ |
331 |
/* in case of gamma-corrections, sBIT's as in the |
332 |
PNG-file are not valid anymore |
333 |
*/ |
334 |
info_ptr->valid &= ~PNG_INFO_sBIT; |
335 |
+#endif |
336 |
if (verbose) |
337 |
pm_message("image gamma is %4.2f, " |
338 |
"converted for display gamma of %4.2f", |
339 |
@@ -570,20 +605,24 @@ setupGammaCorrection(png_struct * const png_ptr, |
340 |
|
341 |
|
342 |
static bool |
343 |
-paletteHasPartialTransparency(png_info * const info_ptr) { |
344 |
+paletteHasPartialTransparency(png_structp png_ptr, png_info * const info_ptr) { |
345 |
|
346 |
bool retval; |
347 |
|
348 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { |
349 |
- if (info_ptr->valid & PNG_INFO_tRNS) { |
350 |
+ if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) { |
351 |
+ png_bytep trans_alpha; |
352 |
+ int num_trans; |
353 |
+ |
354 |
+ if (png_get_tRNS(png_ptr, info_ptr, |
355 |
+ &trans_alpha, &num_trans, NULL) & PNG_INFO_tRNS) { |
356 |
bool foundGray; |
357 |
unsigned int i; |
358 |
|
359 |
for (i = 0, foundGray = FALSE; |
360 |
- i < info_ptr->num_trans && !foundGray; |
361 |
+ i < num_trans && !foundGray; |
362 |
++i) { |
363 |
- if (info_ptr->trans[i] != 0 && |
364 |
- info_ptr->trans[i] != maxval) { |
365 |
+ if (trans_alpha[i] != 0 && |
366 |
+ trans_alpha[i] != maxval) { |
367 |
foundGray = TRUE; |
368 |
} |
369 |
} |
370 |
@@ -611,14 +650,16 @@ setupSignificantBits(png_struct * const png_ptr |
371 |
|
372 |
Return the result as *maxvalP. |
373 |
-----------------------------------------------------------------------------*/ |
374 |
+ png_color_8p sig_bit; |
375 |
+ |
376 |
/* Initial assumption of maxval */ |
377 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { |
378 |
+ if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) { |
379 |
if (alpha == ALPHA_ONLY) { |
380 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY || |
381 |
- info_ptr->color_type == PNG_COLOR_TYPE_RGB) |
382 |
+ if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY || |
383 |
+ png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB) |
384 |
/* The alpha mask will be all opaque, so maxval 1 is plenty */ |
385 |
*maxvalP = 1; |
386 |
- else if (paletteHasPartialTransparency(info_ptr)) |
387 |
+ else if (paletteHasPartialTransparency(png_ptr, info_ptr)) |
388 |
/* Use same maxval as PNG transparency palette for simplicity*/ |
389 |
*maxvalP = 255; |
390 |
else |
391 |
@@ -628,7 +669,7 @@ setupSignificantBits(png_struct * const png_ptr |
392 |
/* Use same maxval as PNG palette for simplicity */ |
393 |
*maxvalP = 255; |
394 |
} else { |
395 |
- *maxvalP = (1l << info_ptr->bit_depth) - 1; |
396 |
+ *maxvalP = (1l << png_get_bit_depth(png_ptr, info_ptr)) - 1; |
397 |
} |
398 |
|
399 |
/* sBIT handling is very tricky. If we are extracting only the |
400 |
@@ -641,20 +682,26 @@ setupSignificantBits(png_struct * const png_ptr |
401 |
is used |
402 |
*/ |
403 |
|
404 |
- if (info_ptr->valid & PNG_INFO_sBIT) { |
405 |
+ if (png_get_sBIT(png_ptr, info_ptr, &sig_bit) & PNG_INFO_sBIT) { |
406 |
+ png_byte color_type; |
407 |
+ png_bytep trans_alpha; |
408 |
+ int num_trans; |
409 |
+ |
410 |
+ color_type = png_get_color_type(png_ptr, info_ptr); |
411 |
switch (alpha) { |
412 |
case ALPHA_MIX: |
413 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
414 |
- info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
415 |
+ if (color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
416 |
+ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
417 |
break; |
418 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
419 |
- (info_ptr->valid & PNG_INFO_tRNS)) { |
420 |
+ if (color_type == PNG_COLOR_TYPE_PALETTE && |
421 |
+ png_get_tRNS(png_ptr, info_ptr, |
422 |
+ &trans_alpha, &num_trans, NULL) & PNG_INFO_tRNS) { |
423 |
|
424 |
bool trans_mix; |
425 |
unsigned int i; |
426 |
trans_mix = TRUE; |
427 |
- for (i = 0; i < info_ptr->num_trans; ++i) |
428 |
- if (info_ptr->trans[i] != 0 && info_ptr->trans[i] != 255) { |
429 |
+ for (i = 0; i < num_trans; ++i) |
430 |
+ if (trans_alpha[i] != 0 && trans_alpha[i] != 255) { |
431 |
trans_mix = FALSE; |
432 |
break; |
433 |
} |
434 |
@@ -665,70 +712,76 @@ setupSignificantBits(png_struct * const png_ptr |
435 |
/* else fall though to normal case */ |
436 |
|
437 |
case ALPHA_NONE: |
438 |
- if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
439 |
- info_ptr->color_type == PNG_COLOR_TYPE_RGB || |
440 |
- info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) && |
441 |
- (info_ptr->sig_bit.red != info_ptr->sig_bit.green || |
442 |
- info_ptr->sig_bit.red != info_ptr->sig_bit.blue) && |
443 |
+ if ((color_type == PNG_COLOR_TYPE_PALETTE || |
444 |
+ color_type == PNG_COLOR_TYPE_RGB || |
445 |
+ color_type == PNG_COLOR_TYPE_RGB_ALPHA) && |
446 |
+ (sig_bit->red != sig_bit->green || |
447 |
+ sig_bit->red != sig_bit->blue) && |
448 |
alpha == ALPHA_NONE) { |
449 |
pm_message("This program cannot handle " |
450 |
"different bit depths for color channels"); |
451 |
pm_message("writing file with %d bit resolution", |
452 |
- info_ptr->bit_depth); |
453 |
+ png_get_bit_depth(png_ptr, info_ptr)); |
454 |
*errorlevelP = PNMTOPNG_WARNING_LEVEL; |
455 |
} else { |
456 |
- if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) && |
457 |
- (info_ptr->sig_bit.red < 255)) { |
458 |
+ png_colorp palette; |
459 |
+ int num_palette; |
460 |
+ |
461 |
+ if ((color_type == PNG_COLOR_TYPE_PALETTE) && |
462 |
+ (sig_bit->red < 255) && |
463 |
+ png_get_PLTE(png_ptr, info_ptr, |
464 |
+ &palette, &num_palette) & PNG_INFO_PLTE) { |
465 |
unsigned int i; |
466 |
- for (i = 0; i < info_ptr->num_palette; ++i) { |
467 |
- info_ptr->palette[i].red >>= |
468 |
- (8 - info_ptr->sig_bit.red); |
469 |
- info_ptr->palette[i].green >>= |
470 |
- (8 - info_ptr->sig_bit.green); |
471 |
- info_ptr->palette[i].blue >>= |
472 |
- (8 - info_ptr->sig_bit.blue); |
473 |
+ |
474 |
+ for (i = 0; i < num_palette; ++i) { |
475 |
+ palette[i].red >>= |
476 |
+ (8 - sig_bit->red); |
477 |
+ palette[i].green >>= |
478 |
+ (8 - sig_bit->green); |
479 |
+ palette[i].blue >>= |
480 |
+ (8 - sig_bit->blue); |
481 |
} |
482 |
- *maxvalP = (1l << info_ptr->sig_bit.red) - 1; |
483 |
+ *maxvalP = (1l << sig_bit->red) - 1; |
484 |
if (verbose) |
485 |
pm_message ("image has fewer significant bits, " |
486 |
"writing file with %d bits per channel", |
487 |
- info_ptr->sig_bit.red); |
488 |
+ sig_bit->red); |
489 |
} else |
490 |
- if ((info_ptr->color_type == PNG_COLOR_TYPE_RGB || |
491 |
- info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) && |
492 |
- (info_ptr->sig_bit.red < info_ptr->bit_depth)) { |
493 |
- png_set_shift (png_ptr, &(info_ptr->sig_bit)); |
494 |
- *maxvalP = (1l << info_ptr->sig_bit.red) - 1; |
495 |
+ if ((color_type == PNG_COLOR_TYPE_RGB || |
496 |
+ color_type == PNG_COLOR_TYPE_RGB_ALPHA) && |
497 |
+ (sig_bit->red < png_get_bit_depth(png_ptr, info_ptr))) { |
498 |
+ png_set_shift (png_ptr, sig_bit); |
499 |
+ *maxvalP = (1l << sig_bit->red) - 1; |
500 |
if (verbose) |
501 |
pm_message("image has fewer significant bits, " |
502 |
"writing file with %d " |
503 |
"bits per channel", |
504 |
- info_ptr->sig_bit.red); |
505 |
+ sig_bit->red); |
506 |
} else |
507 |
- if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY || |
508 |
- info_ptr->color_type == |
509 |
+ if ((color_type == PNG_COLOR_TYPE_GRAY || |
510 |
+ color_type == |
511 |
PNG_COLOR_TYPE_GRAY_ALPHA) && |
512 |
- (info_ptr->sig_bit.gray < info_ptr->bit_depth)) { |
513 |
- png_set_shift (png_ptr, &(info_ptr->sig_bit)); |
514 |
- *maxvalP = (1l << info_ptr->sig_bit.gray) - 1; |
515 |
+ (sig_bit->gray < png_get_bit_depth(png_ptr, info_ptr))) { |
516 |
+ png_set_shift (png_ptr, sig_bit); |
517 |
+ *maxvalP = (1l << sig_bit->gray) - 1; |
518 |
if (verbose) |
519 |
pm_message("image has fewer significant bits, " |
520 |
"writing file with %d bits", |
521 |
- info_ptr->sig_bit.gray); |
522 |
+ sig_bit->gray); |
523 |
} |
524 |
} |
525 |
break; |
526 |
|
527 |
case ALPHA_ONLY: |
528 |
- if ((info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
529 |
- info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) && |
530 |
- (info_ptr->sig_bit.gray < info_ptr->bit_depth)) { |
531 |
- png_set_shift (png_ptr, &(info_ptr->sig_bit)); |
532 |
+ if ((color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
533 |
+ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) && |
534 |
+ (sig_bit->gray < png_get_bit_depth(png_ptr, info_ptr))) { |
535 |
+ png_set_shift (png_ptr, sig_bit); |
536 |
if (verbose) |
537 |
pm_message ("image has fewer significant bits, " |
538 |
"writing file with %d bits", |
539 |
- info_ptr->sig_bit.alpha); |
540 |
- *maxvalP = (1l << info_ptr->sig_bit.alpha) - 1; |
541 |
+ sig_bit->alpha); |
542 |
+ *maxvalP = (1l << sig_bit->alpha) - 1; |
543 |
} |
544 |
break; |
545 |
|
546 |
@@ -739,22 +792,28 @@ setupSignificantBits(png_struct * const png_ptr |
547 |
|
548 |
|
549 |
static bool |
550 |
-imageHasColor(png_info * const info_ptr) { |
551 |
+imageHasColor(png_structp const png_ptr, png_info * const info_ptr) { |
552 |
|
553 |
bool retval; |
554 |
+ png_byte color_type; |
555 |
+ png_colorp palette; |
556 |
+ int num_palette; |
557 |
|
558 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY || |
559 |
- info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
560 |
+ color_type = png_get_color_type(png_ptr, info_ptr); |
561 |
+ if (color_type == PNG_COLOR_TYPE_GRAY || |
562 |
+ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
563 |
|
564 |
retval = FALSE; |
565 |
- else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { |
566 |
+ else if (color_type == PNG_COLOR_TYPE_PALETTE && |
567 |
+ png_get_PLTE(png_ptr, info_ptr, |
568 |
+ &palette, &num_palette) & PNG_INFO_PLTE) { |
569 |
bool foundColor; |
570 |
unsigned int i; |
571 |
|
572 |
for (i = 0, foundColor = FALSE; |
573 |
- i < info_ptr->num_palette && !foundColor; |
574 |
+ i < num_palette && !foundColor; |
575 |
++i) { |
576 |
- if (iscolor(info_ptr->palette[i])) |
577 |
+ if (iscolor(palette[i])) |
578 |
foundColor = TRUE; |
579 |
} |
580 |
retval = foundColor; |
581 |
@@ -767,14 +826,15 @@ imageHasColor(png_info * const info_ptr) { |
582 |
|
583 |
|
584 |
static void |
585 |
-determineOutputType(png_info * const info_ptr, |
586 |
+determineOutputType(png_structp const png_ptr, |
587 |
+ png_info * const info_ptr, |
588 |
enum alpha_handling const alphaHandling, |
589 |
pngcolor const bgColor, |
590 |
xelval const maxval, |
591 |
int * const pnmTypeP) { |
592 |
|
593 |
if (alphaHandling != ALPHA_ONLY && |
594 |
- (imageHasColor(info_ptr) || !isGrayscale(bgColor))) |
595 |
+ (imageHasColor(png_ptr, info_ptr) || !isGrayscale(bgColor))) |
596 |
*pnmTypeP = PPM_TYPE; |
597 |
else { |
598 |
if (maxval > 1) |
599 |
@@ -787,7 +847,8 @@ determineOutputType(png_info * const info_ptr |
600 |
|
601 |
|
602 |
static void |
603 |
-getBackgroundColor(png_info * const info_ptr, |
604 |
+getBackgroundColor(png_structp const png_ptr, |
605 |
+ png_info * const info_ptr, |
606 |
const char * const requestedColor, |
607 |
float const totalgamma, |
608 |
xelval const maxval, |
609 |
@@ -798,6 +859,8 @@ getBackgroundColor(png_info * const info_ptr, |
610 |
Otherwise, if the PNG specifies a background color, that's the one. |
611 |
And otherwise, it's white. |
612 |
-----------------------------------------------------------------------------*/ |
613 |
+ png_color_16p background; |
614 |
+ |
615 |
if (requestedColor) { |
616 |
/* Background was specified from the command-line; we always |
617 |
use that. I chose to do no gamma-correction in this case; |
618 |
@@ -809,27 +872,32 @@ getBackgroundColor(png_info * const info_ptr, |
619 |
bgColorP->g = PPM_GETG(backcolor); |
620 |
bgColorP->b = PPM_GETB(backcolor); |
621 |
|
622 |
- } else if (info_ptr->valid & PNG_INFO_bKGD) { |
623 |
+ } else if (png_get_bKGD(png_ptr, info_ptr, &background) & PNG_INFO_bKGD) { |
624 |
/* didn't manage to get libpng to work (bugs?) concerning background |
625 |
processing, therefore we do our own. |
626 |
*/ |
627 |
- switch (info_ptr->color_type) { |
628 |
+ switch (png_get_color_type(png_ptr, info_ptr)) { |
629 |
case PNG_COLOR_TYPE_GRAY: |
630 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
631 |
bgColorP->r = bgColorP->g = bgColorP->b = |
632 |
- gamma_correct(info_ptr->background.gray, totalgamma); |
633 |
+ gamma_correct(background->gray, totalgamma); |
634 |
break; |
635 |
case PNG_COLOR_TYPE_PALETTE: { |
636 |
- png_color const rawBgcolor = |
637 |
- info_ptr->palette[info_ptr->background.index]; |
638 |
- bgColorP->r = gamma_correct(rawBgcolor.red, totalgamma); |
639 |
- bgColorP->g = gamma_correct(rawBgcolor.green, totalgamma); |
640 |
- bgColorP->b = gamma_correct(rawBgcolor.blue, totalgamma); |
641 |
+ png_colorp palette; |
642 |
+ int num_palette; |
643 |
+ |
644 |
+ if (png_get_PLTE(png_ptr, info_ptr, |
645 |
+ &palette, &num_palette) & PNG_INFO_PLTE) { |
646 |
+ png_color const rawBgcolor = palette[background->index]; |
647 |
+ bgColorP->r = gamma_correct(rawBgcolor.red, totalgamma); |
648 |
+ bgColorP->g = gamma_correct(rawBgcolor.green, totalgamma); |
649 |
+ bgColorP->b = gamma_correct(rawBgcolor.blue, totalgamma); |
650 |
+ } |
651 |
} |
652 |
break; |
653 |
case PNG_COLOR_TYPE_RGB: |
654 |
case PNG_COLOR_TYPE_RGB_ALPHA: { |
655 |
- png_color_16 const rawBgcolor = info_ptr->background; |
656 |
+ png_color_16 const rawBgcolor = *background; |
657 |
|
658 |
bgColorP->r = gamma_correct(rawBgcolor.red, totalgamma); |
659 |
bgColorP->g = gamma_correct(rawBgcolor.green, totalgamma); |
660 |
@@ -848,6 +916,7 @@ static void |
661 |
writePnm(FILE * const ofP, |
662 |
xelval const maxval, |
663 |
int const pnm_type, |
664 |
+ png_structp const png_ptr, |
665 |
png_info * const info_ptr, |
666 |
png_byte ** const png_image, |
667 |
pngcolor const bgColor, |
668 |
@@ -865,6 +934,7 @@ writePnm(FILE * const ofP, |
669 |
-----------------------------------------------------------------------------*/ |
670 |
xel * xelrow; |
671 |
unsigned int row; |
672 |
+ png_uint_32 width, height; |
673 |
|
674 |
if (verbose) |
675 |
pm_message ("writing a %s file (maxval=%u)", |
676 |
@@ -874,27 +944,35 @@ writePnm(FILE * const ofP, |
677 |
"UNKNOWN!", |
678 |
maxval); |
679 |
|
680 |
- xelrow = pnm_allocrow(info_ptr->width); |
681 |
+ xelrow = pnm_allocrow(png_get_image_width(png_ptr, info_ptr)); |
682 |
|
683 |
- pnm_writepnminit(stdout, info_ptr->width, info_ptr->height, maxval, |
684 |
- pnm_type, FALSE); |
685 |
+ width = png_get_image_width(png_ptr, info_ptr); |
686 |
+ height = png_get_image_height(png_ptr, info_ptr); |
687 |
|
688 |
- for (row = 0; row < info_ptr->height; ++row) { |
689 |
+ pnm_writepnminit(stdout, width, height, maxval, pnm_type, FALSE); |
690 |
+ |
691 |
+ for (row = 0; row < height; ++row) { |
692 |
png_byte * png_pixelP; |
693 |
int col; |
694 |
|
695 |
png_pixelP = &png_image[row][0]; /* initial value */ |
696 |
- for (col = 0; col < info_ptr->width; ++col) { |
697 |
- switch (info_ptr->color_type) { |
698 |
+ for (col = 0; col < width; ++col) { |
699 |
+ switch (png_get_color_type(png_ptr, info_ptr)) { |
700 |
case PNG_COLOR_TYPE_GRAY: { |
701 |
pngcolor fgColor; |
702 |
+ png_color_16p trans_color; |
703 |
+ |
704 |
fgColor.r = fgColor.g = fgColor.b = get_png_val(png_pixelP); |
705 |
- setXel(&xelrow[col], fgColor, bgColor, alpha_handling, |
706 |
- ((info_ptr->valid & PNG_INFO_tRNS) && |
707 |
- (fgColor.r == |
708 |
- gamma_correct(info_ptr->trans_values.gray, |
709 |
- totalgamma))) ? |
710 |
- 0 : maxval); |
711 |
+ |
712 |
+ if (png_get_tRNS(png_ptr, info_ptr, |
713 |
+ NULL, NULL, &trans_color) & PNG_INFO_tRNS && |
714 |
+ (fgColor.r == gamma_correct(trans_color->gray, |
715 |
+ totalgamma))) { |
716 |
+ setXel(&xelrow[col], fgColor, bgColor, alpha_handling, 0); |
717 |
+ } else { |
718 |
+ setXel(&xelrow[col], fgColor, bgColor, alpha_handling, |
719 |
+ maxval); |
720 |
+ } |
721 |
} |
722 |
break; |
723 |
|
724 |
@@ -909,19 +987,31 @@ writePnm(FILE * const ofP, |
725 |
break; |
726 |
|
727 |
case PNG_COLOR_TYPE_PALETTE: { |
728 |
- png_uint_16 const index = get_png_val(png_pixelP); |
729 |
- png_color const paletteColor = info_ptr->palette[index]; |
730 |
+ png_uint_16 const index = get_png_val(png_pixelP); |
731 |
+ png_colorp palette; |
732 |
+ int num_palette; |
733 |
|
734 |
- pngcolor fgColor; |
735 |
+ if (png_get_PLTE(png_ptr, info_ptr, |
736 |
+ &palette, &num_palette) & PNG_INFO_PLTE) { |
737 |
+ png_color const paletteColor = palette[index]; |
738 |
+ pngcolor fgColor; |
739 |
+ png_bytep trans_alpha; |
740 |
+ int num_trans; |
741 |
|
742 |
- fgColor.r = paletteColor.red; |
743 |
- fgColor.g = paletteColor.green; |
744 |
- fgColor.b = paletteColor.blue; |
745 |
+ fgColor.r = paletteColor.red; |
746 |
+ fgColor.g = paletteColor.green; |
747 |
+ fgColor.b = paletteColor.blue; |
748 |
|
749 |
- setXel(&xelrow[col], fgColor, bgColor, alpha_handling, |
750 |
- (info_ptr->valid & PNG_INFO_tRNS) && |
751 |
- index < info_ptr->num_trans ? |
752 |
- info_ptr->trans[index] : maxval); |
753 |
+ if (png_get_tRNS(png_ptr, info_ptr, |
754 |
+ &trans_alpha, |
755 |
+ &num_trans, NULL) & PNG_INFO_tRNS) { |
756 |
+ setXel(&xelrow[col], fgColor, bgColor, alpha_handling, |
757 |
+ index < num_trans ? trans_alpha[index] : maxval); |
758 |
+ } else { |
759 |
+ setXel(&xelrow[col], fgColor, bgColor, alpha_handling, |
760 |
+ maxval); |
761 |
+ } |
762 |
+ } |
763 |
} |
764 |
break; |
765 |
|
766 |
@@ -932,8 +1022,8 @@ writePnm(FILE * const ofP, |
767 |
fgColor.g = get_png_val(png_pixelP); |
768 |
fgColor.b = get_png_val(png_pixelP); |
769 |
setXel(&xelrow[col], fgColor, bgColor, alpha_handling, |
770 |
- isTransparentColor(fgColor, info_ptr, totalgamma) ? |
771 |
- 0 : maxval); |
772 |
+ isTransparentColor(fgColor, png_ptr, info_ptr, |
773 |
+ totalgamma) ? 0 : maxval); |
774 |
} |
775 |
break; |
776 |
|
777 |
@@ -950,10 +1040,10 @@ writePnm(FILE * const ofP, |
778 |
break; |
779 |
|
780 |
default: |
781 |
- pm_error ("unknown PNG color type: %d", info_ptr->color_type); |
782 |
+ pm_error ("unknown PNG color type: %d", png_get_color_type(png_ptr, info_ptr)); |
783 |
} |
784 |
} |
785 |
- pnm_writepnmrow(ofP, xelrow, info_ptr->width, maxval, pnm_type, FALSE); |
786 |
+ pnm_writepnmrow(ofP, xelrow, width, maxval, pnm_type, FALSE); |
787 |
} |
788 |
pnm_freerow (xelrow); |
789 |
} |
790 |
@@ -974,6 +1064,7 @@ convertpng(FILE * const ifp, |
791 |
int pnm_type; |
792 |
pngcolor bgColor; |
793 |
float totalgamma; |
794 |
+ int res_x, res_y, unit_type; |
795 |
|
796 |
*errorlevelP = 0; |
797 |
|
798 |
@@ -996,28 +1087,28 @@ convertpng(FILE * const ifp, |
799 |
png_set_sig_bytes (png_ptr, SIG_CHECK_SIZE); |
800 |
png_read_info (png_ptr, info_ptr); |
801 |
|
802 |
- MALLOCARRAY(png_image, info_ptr->height); |
803 |
+ MALLOCARRAY(png_image, png_get_image_height(png_ptr, info_ptr)); |
804 |
if (png_image == NULL) { |
805 |
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp)NULL); |
806 |
pm_closer (ifp); |
807 |
pm_error ("couldn't allocate space for image"); |
808 |
} |
809 |
|
810 |
- if (info_ptr->bit_depth == 16) |
811 |
- linesize = 2 * info_ptr->width; |
812 |
+ if (png_get_bit_depth(png_ptr, info_ptr) == 16) |
813 |
+ linesize = 2 * png_get_image_width(png_ptr, info_ptr); |
814 |
else |
815 |
- linesize = info_ptr->width; |
816 |
+ linesize = png_get_image_width(png_ptr, info_ptr); |
817 |
|
818 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
819 |
+ if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA) |
820 |
linesize *= 2; |
821 |
else |
822 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_RGB) |
823 |
+ if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB) |
824 |
linesize *= 3; |
825 |
else |
826 |
- if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
827 |
+ if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB_ALPHA) |
828 |
linesize *= 4; |
829 |
|
830 |
- for (y = 0 ; y < info_ptr->height ; y++) { |
831 |
+ for (y = 0 ; y < png_get_image_height(png_ptr, info_ptr) ; y++) { |
832 |
png_image[y] = malloc (linesize); |
833 |
if (png_image[y] == NULL) { |
834 |
for (x = 0 ; x < y ; x++) |
835 |
@@ -1029,7 +1120,7 @@ convertpng(FILE * const ifp, |
836 |
} |
837 |
} |
838 |
|
839 |
- if (info_ptr->bit_depth < 8) |
840 |
+ if (png_get_bit_depth(png_ptr, info_ptr) < 8) |
841 |
png_set_packing (png_ptr); |
842 |
|
843 |
setupGammaCorrection(png_ptr, info_ptr, cmdline.gamma, &totalgamma); |
844 |
@@ -1037,8 +1128,8 @@ convertpng(FILE * const ifp, |
845 |
setupSignificantBits(png_ptr, info_ptr, cmdline.alpha, |
846 |
&maxval, errorlevelP); |
847 |
|
848 |
- getBackgroundColor(info_ptr, cmdline.background, totalgamma, maxval, |
849 |
- &bgColor); |
850 |
+ getBackgroundColor(png_ptr, info_ptr, cmdline.background, totalgamma, |
851 |
+ maxval, &bgColor); |
852 |
|
853 |
png_read_image (png_ptr, png_image); |
854 |
png_read_end (png_ptr, info_ptr); |
855 |
@@ -1048,16 +1139,17 @@ convertpng(FILE * const ifp, |
856 |
completes. That's because it comes from chunks that are at the |
857 |
end of the stream. |
858 |
*/ |
859 |
- dump_png_info(info_ptr); |
860 |
+ dump_png_info(png_ptr, info_ptr); |
861 |
|
862 |
if (mtime) |
863 |
- show_time (info_ptr); |
864 |
+ show_time (png_ptr, info_ptr); |
865 |
if (tfp) |
866 |
- save_text (info_ptr, tfp); |
867 |
+ save_text (png_ptr, info_ptr, tfp); |
868 |
|
869 |
- if (info_ptr->valid & PNG_INFO_pHYs) { |
870 |
+ if (png_get_pHYs(png_ptr, info_ptr, |
871 |
+ &res_x, &res_y, &unit_type) & PNG_INFO_pHYs) { |
872 |
float r; |
873 |
- r = (float)info_ptr->x_pixels_per_unit / info_ptr->y_pixels_per_unit; |
874 |
+ r = (float)res_x / res_y; |
875 |
if (r != 1.0) { |
876 |
pm_message ("warning - non-square pixels; " |
877 |
"to fix do a 'pamscale -%cscale %g'", |
878 |
@@ -1067,13 +1159,13 @@ convertpng(FILE * const ifp, |
879 |
} |
880 |
} |
881 |
|
882 |
- determineOutputType(info_ptr, cmdline.alpha, bgColor, maxval, &pnm_type); |
883 |
+ determineOutputType(png_ptr, info_ptr, cmdline.alpha, bgColor, maxval, &pnm_type); |
884 |
|
885 |
- writePnm(stdout, maxval, pnm_type, info_ptr, png_image, bgColor, |
886 |
+ writePnm(stdout, maxval, pnm_type, png_ptr, info_ptr, png_image, bgColor, |
887 |
cmdline.alpha, totalgamma); |
888 |
|
889 |
fflush(stdout); |
890 |
- for (y = 0 ; y < info_ptr->height ; y++) |
891 |
+ for (y = 0 ; y < png_get_image_height(png_ptr, info_ptr) ; y++) |
892 |
free (png_image[y]); |
893 |
free (png_image); |
894 |
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp)NULL); |