Initial revision
[wxWidgets.git] / src / png / pngget.c
1
2 /* pngget.c - retrieval of values from info struct
3 *
4 * libpng 1.0.1
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, Glenn Randers-Pehrson
9 * March 15, 1998
10 */
11
12 #define PNG_INTERNAL
13 #include "png.h"
14
15 png_uint_32
16 png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
17 {
18 if (png_ptr != NULL && info_ptr != NULL)
19 return(info_ptr->valid & flag);
20 else
21 return(0);
22 }
23
24 png_uint_32
25 png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
26 {
27 if (png_ptr != NULL && info_ptr != NULL)
28 return(info_ptr->rowbytes);
29 else
30 return(0);
31 }
32
33 #ifdef PNG_EASY_ACCESS_SUPPORTED
34 /* easy access to info, added in libpng-0.99 */
35 png_uint_32
36 png_get_image_width(png_structp png_ptr, png_infop info_ptr)
37 {
38 if (png_ptr != NULL && info_ptr != NULL)
39 {
40 return info_ptr->width;
41 }
42 return (0);
43 }
44
45 png_uint_32
46 png_get_image_height(png_structp png_ptr, png_infop info_ptr)
47 {
48 if (png_ptr != NULL && info_ptr != NULL)
49 {
50 return info_ptr->height;
51 }
52 return (0);
53 }
54
55 png_byte
56 png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)
57 {
58 if (png_ptr != NULL && info_ptr != NULL)
59 {
60 return info_ptr->bit_depth;
61 }
62 return (0);
63 }
64
65 png_byte
66 png_get_color_type(png_structp png_ptr, png_infop info_ptr)
67 {
68 if (png_ptr != NULL && info_ptr != NULL)
69 {
70 return info_ptr->color_type;
71 }
72 return (0);
73 }
74
75 png_byte
76 png_get_filter_type(png_structp png_ptr, png_infop info_ptr)
77 {
78 if (png_ptr != NULL && info_ptr != NULL)
79 {
80 return info_ptr->filter_type;
81 }
82 return (0);
83 }
84
85 png_byte
86 png_get_interlace_type(png_structp png_ptr, png_infop info_ptr)
87 {
88 if (png_ptr != NULL && info_ptr != NULL)
89 {
90 return info_ptr->interlace_type;
91 }
92 return (0);
93 }
94
95 png_byte
96 png_get_compression_type(png_structp png_ptr, png_infop info_ptr)
97 {
98 if (png_ptr != NULL && info_ptr != NULL)
99 {
100 return info_ptr->compression_type;
101 }
102 return (0);
103 }
104
105 png_uint_32
106 png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
107 {
108 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
109 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
110 {
111 png_debug1(1, "in %s retrieval function\n", "png_get_x_pixels_per_meter");
112 if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
113 return (0);
114 else return (info_ptr->x_pixels_per_unit);
115 }
116 else
117 #endif
118 return (0);
119 }
120
121 png_uint_32
122 png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
123 {
124 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
125 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
126 {
127 png_debug1(1, "in %s retrieval function\n", "png_get_y_pixels_per_meter");
128 if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
129 return (0);
130 else return (info_ptr->y_pixels_per_unit);
131 }
132 else
133 #endif
134 return (0);
135 }
136
137 png_uint_32
138 png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
139 {
140 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
141 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
142 {
143 png_debug1(1, "in %s retrieval function\n", "png_get_pixels_per_meter");
144 if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER ||
145 info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit)
146 return (0);
147 else return (info_ptr->x_pixels_per_unit);
148 }
149 else
150 #endif
151 return (0);
152 }
153
154 float
155 png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr)
156 {
157 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
158 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
159 {
160 png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio");
161 if (info_ptr->x_pixels_per_unit == 0)
162 return ((float)0.0);
163 else
164 return ((float)info_ptr->y_pixels_per_unit
165 /(float)info_ptr->x_pixels_per_unit);
166 }
167 else
168 #endif
169 return ((float)0.0);
170 }
171
172 png_uint_32
173 png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr)
174 {
175 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
176 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
177 {
178 png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
179 if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
180 return (0);
181 else return (info_ptr->x_offset);
182 }
183 else
184 #endif
185 return (0);
186 }
187
188 png_uint_32
189 png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr)
190 {
191 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
192 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
193 {
194 png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
195 if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
196 return (0);
197 else return (info_ptr->y_offset);
198 }
199 else
200 #endif
201 return (0);
202 }
203
204 png_uint_32
205 png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr)
206 {
207 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
208 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
209 {
210 png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
211 if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
212 return (0);
213 else return (info_ptr->x_offset);
214 }
215 else
216 #endif
217 return (0);
218 }
219
220 png_uint_32
221 png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr)
222 {
223 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
224 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
225 {
226 png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
227 if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
228 return (0);
229 else return (info_ptr->y_offset);
230 }
231 else
232 #endif
233 return (0);
234 }
235
236 #ifdef PNG_INCH_CONVERSIONS
237 png_uint_32
238 png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
239 {
240 return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr)
241 *.03937 +.5)
242 }
243
244 png_uint_32
245 png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
246 {
247 return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr)
248 *.03937 +.5)
249 }
250
251 png_uint_32
252 png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
253 {
254 return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr)
255 *.03937 +.5)
256 }
257
258 float
259 png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr)
260 {
261 return ((float)png_get_x_offset_microns(png_ptr, info_ptr)
262 *.03937/1000000. +.5)
263 }
264
265 float
266 png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr)
267 {
268 return ((float)png_get_y_offset_microns(png_ptr, info_ptr)
269 *.03937/1000000. +.5)
270 }
271
272 #if defined(PNG_READ_pHYs_SUPPORTED)
273 png_uint_32
274 png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr,
275 png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
276 {
277 png_uint_32 retval = 0;
278
279 if (png_ptr != NULL && info_ptr != NULL && info_ptr->valid & PNG_INFO_pHYs)
280 {
281 png_debug1(1, "in %s retrieval function\n", "pHYs");
282 if (res_x != NULL)
283 {
284 *res_x = info_ptr->x_pixels_per_unit;
285 retval |= PNG_INFO_pHYs;
286 }
287 if (res_y != NULL)
288 {
289 *res_y = info_ptr->y_pixels_per_unit;
290 retval |= PNG_INFO_pHYs;
291 }
292 if (unit_type != NULL)
293 {
294 *unit_type = (int)info_ptr->phys_unit_type;
295 retval |= PNG_INFO_pHYs;
296 if(unit_type == 1)
297 {
298 if (res_x != NULL) *res_x = (png_uint_32)(*res_x * 39.37 + .50);
299 if (res_y != NULL) *res_y = (png_uint_32)(*res_y * 39.37 + .50);
300 }
301 }
302 }
303 return (retval);
304 }
305 #endif
306
307 #endif /* PNG_INCH_CONVERSIONS */
308
309 /* png_get_channels really belongs in here, too, but it's been around longer */
310 #endif /* PNG_EASY_ACCESS_SUPPORTED */
311
312 png_byte
313 png_get_channels(png_structp png_ptr, png_infop info_ptr)
314 {
315 if (png_ptr != NULL && info_ptr != NULL)
316 return(info_ptr->channels);
317 else
318 return (0);
319 }
320
321 png_bytep
322 png_get_signature(png_structp png_ptr, png_infop info_ptr)
323 {
324 if (png_ptr != NULL && info_ptr != NULL)
325 return(info_ptr->signature);
326 else
327 return (NULL);
328 }
329
330 #if defined(PNG_READ_bKGD_SUPPORTED)
331 png_uint_32
332 png_get_bKGD(png_structp png_ptr, png_infop info_ptr,
333 png_color_16p *background)
334 {
335 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
336 && background != NULL)
337 {
338 png_debug1(1, "in %s retrieval function\n", "bKGD");
339 *background = &(info_ptr->background);
340 return (PNG_INFO_bKGD);
341 }
342 return (0);
343 }
344 #endif
345
346 #if defined(PNG_READ_cHRM_SUPPORTED)
347 png_uint_32
348 png_get_cHRM(png_structp png_ptr, png_infop info_ptr,
349 double *white_x, double *white_y, double *red_x, double *red_y,
350 double *green_x, double *green_y, double *blue_x, double *blue_y)
351 {
352 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
353 {
354 png_debug1(1, "in %s retrieval function\n", "cHRM");
355 if (white_x != NULL)
356 *white_x = (double)info_ptr->x_white;
357 if (white_y != NULL)
358 *white_y = (double)info_ptr->y_white;
359 if (red_x != NULL)
360 *red_x = (double)info_ptr->x_red;
361 if (red_y != NULL)
362 *red_y = (double)info_ptr->y_red;
363 if (green_x != NULL)
364 *green_x = (double)info_ptr->x_green;
365 if (green_y != NULL)
366 *green_y = (double)info_ptr->y_green;
367 if (blue_x != NULL)
368 *blue_x = (double)info_ptr->x_blue;
369 if (blue_y != NULL)
370 *blue_y = (double)info_ptr->y_blue;
371 return (PNG_INFO_cHRM);
372 }
373 return (0);
374 }
375 #endif
376
377 #if defined(PNG_READ_gAMA_SUPPORTED)
378 png_uint_32
379 png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
380 {
381 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
382 && file_gamma != NULL)
383 {
384 png_debug1(1, "in %s retrieval function\n", "gAMA");
385 *file_gamma = (double)info_ptr->gamma;
386 return (PNG_INFO_gAMA);
387 }
388 return (0);
389 }
390 #endif
391
392 #if defined(PNG_READ_sRGB_SUPPORTED)
393 png_uint_32
394 png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent)
395 {
396 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
397 && file_srgb_intent != NULL)
398 {
399 png_debug1(1, "in %s retrieval function\n", "sRGB");
400 *file_srgb_intent = (int)info_ptr->srgb_intent;
401 return (PNG_INFO_sRGB);
402 }
403 return (0);
404 }
405 #endif
406
407 #if defined(PNG_READ_hIST_SUPPORTED)
408 png_uint_32
409 png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)
410 {
411 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
412 && hist != NULL)
413 {
414 png_debug1(1, "in %s retrieval function\n", "hIST");
415 *hist = info_ptr->hist;
416 return (PNG_INFO_hIST);
417 }
418 return (0);
419 }
420 #endif
421
422 png_uint_32
423 png_get_IHDR(png_structp png_ptr, png_infop info_ptr,
424 png_uint_32 *width, png_uint_32 *height, int *bit_depth,
425 int *color_type, int *interlace_type, int *compression_type,
426 int *filter_type)
427
428 {
429 if (png_ptr != NULL && info_ptr != NULL && width != NULL && height != NULL &&
430 bit_depth != NULL && color_type != NULL)
431 {
432 int pixel_depth, channels;
433 png_uint_32 rowbytes_per_pixel;
434
435 png_debug1(1, "in %s retrieval function\n", "IHDR");
436 *width = info_ptr->width;
437 *height = info_ptr->height;
438 *bit_depth = info_ptr->bit_depth;
439 *color_type = info_ptr->color_type;
440 if (compression_type != NULL)
441 *compression_type = info_ptr->compression_type;
442 if (filter_type != NULL)
443 *filter_type = info_ptr->filter_type;
444 if (interlace_type != NULL)
445 *interlace_type = info_ptr->interlace_type;
446
447 /* check for potential overflow of rowbytes */
448 if (*color_type == PNG_COLOR_TYPE_PALETTE)
449 channels = 1;
450 else if (*color_type & PNG_COLOR_MASK_COLOR)
451 channels = 3;
452 else
453 channels = 1;
454 if (*color_type & PNG_COLOR_MASK_ALPHA)
455 channels++;
456 pixel_depth = *bit_depth * channels;
457 rowbytes_per_pixel = (pixel_depth + 7) >> 3;
458 if ((*width > (png_uint_32)2147483647L/rowbytes_per_pixel))
459 {
460 png_warning(png_ptr,
461 "Width too large for libpng to process image data.");
462 }
463 return (1);
464 }
465 return (0);
466 }
467
468 #if defined(PNG_READ_oFFs_SUPPORTED)
469 png_uint_32
470 png_get_oFFs(png_structp png_ptr, png_infop info_ptr,
471 png_uint_32 *offset_x, png_uint_32 *offset_y, int *unit_type)
472 {
473 if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
474 && offset_x != NULL && offset_y != NULL && unit_type != NULL)
475 {
476 png_debug1(1, "in %s retrieval function\n", "oFFs");
477 *offset_x = info_ptr->x_offset;
478 *offset_y = info_ptr->y_offset;
479 *unit_type = (int)info_ptr->offset_unit_type;
480 return (PNG_INFO_oFFs);
481 }
482 return (0);
483 }
484 #endif
485
486 #if defined(PNG_READ_pCAL_SUPPORTED)
487 png_uint_32
488 png_get_pCAL(png_structp png_ptr, png_infop info_ptr,
489 png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
490 png_charp *units, png_charpp *params)
491 {
492 if (png_ptr != NULL && info_ptr != NULL && info_ptr->valid & PNG_INFO_pCAL &&
493 purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
494 nparams != NULL && units != NULL && params != NULL)
495 {
496 png_debug1(1, "in %s retrieval function\n", "pCAL");
497 *purpose = info_ptr->pcal_purpose;
498 *X0 = info_ptr->pcal_X0;
499 *X1 = info_ptr->pcal_X1;
500 *type = (int)info_ptr->pcal_type;
501 *nparams = (int)info_ptr->pcal_nparams;
502 *units = info_ptr->pcal_units;
503 *params = info_ptr->pcal_params;
504 return (PNG_INFO_pCAL);
505 }
506 return (0);
507 }
508 #endif
509
510 #if defined(PNG_READ_pHYs_SUPPORTED)
511 png_uint_32
512 png_get_pHYs(png_structp png_ptr, png_infop info_ptr,
513 png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
514 {
515 png_uint_32 retval = 0;
516
517 if (png_ptr != NULL && info_ptr != NULL && info_ptr->valid & PNG_INFO_pHYs)
518 {
519 png_debug1(1, "in %s retrieval function\n", "pHYs");
520 if (res_x != NULL)
521 {
522 *res_x = info_ptr->x_pixels_per_unit;
523 retval |= PNG_INFO_pHYs;
524 }
525 if (res_y != NULL)
526 {
527 *res_y = info_ptr->y_pixels_per_unit;
528 retval |= PNG_INFO_pHYs;
529 }
530 if (unit_type != NULL)
531 {
532 *unit_type = (int)info_ptr->phys_unit_type;
533 retval |= PNG_INFO_pHYs;
534 }
535 }
536 return (retval);
537 }
538 #endif
539
540 png_uint_32
541 png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette,
542 int *num_palette)
543 {
544 if (png_ptr != NULL && info_ptr != NULL && info_ptr->valid & PNG_INFO_PLTE &&
545 palette != NULL)
546 {
547 png_debug1(1, "in %s retrieval function\n", "PLTE");
548 *palette = info_ptr->palette;
549 *num_palette = info_ptr->num_palette;
550 png_debug1(3, "num_palette = %d\n", *num_palette);
551 return (PNG_INFO_PLTE);
552 }
553 return (0);
554 }
555
556 #if defined(PNG_READ_sBIT_SUPPORTED)
557 png_uint_32
558 png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)
559 {
560 if (png_ptr != NULL && info_ptr != NULL && info_ptr->valid & PNG_INFO_sBIT &&
561 sig_bit != NULL)
562 {
563 png_debug1(1, "in %s retrieval function\n", "sBIT");
564 *sig_bit = &(info_ptr->sig_bit);
565 return (PNG_INFO_sBIT);
566 }
567 return (0);
568 }
569 #endif
570
571 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
572 png_uint_32
573 png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr,
574 int *num_text)
575 {
576 if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
577 {
578 png_debug1(1, "in %s retrieval function\n",
579 (png_ptr->chunk_name[0] == '\0' ? "text"
580 : (png_const_charp)png_ptr->chunk_name));
581 if (text_ptr != NULL)
582 *text_ptr = info_ptr->text;
583 if (num_text != NULL)
584 *num_text = info_ptr->num_text;
585 return ((png_uint_32)info_ptr->num_text);
586 }
587 return(0);
588 }
589 #endif
590
591 #if defined(PNG_READ_tIME_SUPPORTED)
592 png_uint_32
593 png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)
594 {
595 if (png_ptr != NULL && info_ptr != NULL && info_ptr->valid & PNG_INFO_tIME &&
596 mod_time != NULL)
597 {
598 png_debug1(1, "in %s retrieval function\n", "tIME");
599 *mod_time = &(info_ptr->mod_time);
600 return (PNG_INFO_tIME);
601 }
602 return (0);
603 }
604 #endif
605
606 #if defined(PNG_READ_tRNS_SUPPORTED)
607 png_uint_32
608 png_get_tRNS(png_structp png_ptr, png_infop info_ptr,
609 png_bytep *trans, int *num_trans, png_color_16p *trans_values)
610 {
611 png_uint_32 retval = 0;
612 if (png_ptr != NULL && info_ptr != NULL && info_ptr->valid & PNG_INFO_tRNS)
613 {
614 png_debug1(1, "in %s retrieval function\n", "tRNS");
615 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
616 {
617 if (trans != NULL)
618 {
619 *trans = info_ptr->trans;
620 retval |= PNG_INFO_tRNS;
621 }
622 if (trans_values != NULL)
623 *trans_values = &(info_ptr->trans_values);
624 }
625 else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
626 {
627 if (trans_values != NULL)
628 {
629 *trans_values = &(info_ptr->trans_values);
630 retval |= PNG_INFO_tRNS;
631 }
632 if(trans != NULL)
633 *trans = NULL;
634 }
635 if(num_trans != NULL)
636 {
637 *num_trans = info_ptr->num_trans;
638 retval |= PNG_INFO_tRNS;
639 }
640 }
641 return (retval);
642 }
643 #endif
644