172f50a90015c829eda4087bc33624c0c800ead3
[platform/upstream/libpng.git] / pngget.c
1
2 /* pngget.c - retrieval of values from info struct
3  *
4  * Last changed in libpng 1.6.11 [June 5, 2014]
5  * Copyright (c) 1998-2014 Glenn Randers-Pehrson
6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8  *
9  * This code is released under the libpng license.
10  * For conditions of distribution and use, see the disclaimer
11  * and license in png.h
12  *
13  */
14
15 #include "pngpriv.h"
16
17 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
18
19 png_uint_32 PNGAPI
20 png_get_valid(png_const_structrp png_ptr, png_const_inforp info_ptr,
21     png_uint_32 flag)
22 {
23    if (png_ptr != NULL && info_ptr != NULL)
24       return(info_ptr->valid & flag);
25
26    return(0);
27 }
28
29 png_size_t PNGAPI
30 png_get_rowbytes(png_const_structrp png_ptr, png_const_inforp info_ptr)
31 {
32    if (png_ptr != NULL && info_ptr != NULL)
33       return(info_ptr->rowbytes);
34
35    return(0);
36 }
37
38 #ifdef PNG_INFO_IMAGE_SUPPORTED
39 png_bytepp PNGAPI
40 png_get_rows(png_const_structrp png_ptr, png_const_inforp info_ptr)
41 {
42    if (png_ptr != NULL && info_ptr != NULL)
43       return(info_ptr->row_pointers);
44
45    return(0);
46 }
47 #endif
48
49 #ifdef PNG_EASY_ACCESS_SUPPORTED
50 /* Easy access to info, added in libpng-0.99 */
51 png_uint_32 PNGAPI
52 png_get_image_width(png_const_structrp png_ptr, png_const_inforp info_ptr)
53 {
54    if (png_ptr != NULL && info_ptr != NULL)
55       return info_ptr->width;
56
57    return (0);
58 }
59
60 png_uint_32 PNGAPI
61 png_get_image_height(png_const_structrp png_ptr, png_const_inforp info_ptr)
62 {
63    if (png_ptr != NULL && info_ptr != NULL)
64       return info_ptr->height;
65
66    return (0);
67 }
68
69 png_byte PNGAPI
70 png_get_bit_depth(png_const_structrp png_ptr, png_const_inforp info_ptr)
71 {
72    if (png_ptr != NULL && info_ptr != NULL)
73       return info_ptr->bit_depth;
74
75    return (0);
76 }
77
78 png_byte PNGAPI
79 png_get_color_type(png_const_structrp png_ptr, png_const_inforp info_ptr)
80 {
81    if (png_ptr != NULL && info_ptr != NULL)
82       return info_ptr->color_type;
83
84    return (0);
85 }
86
87 png_byte PNGAPI
88 png_get_filter_type(png_const_structrp png_ptr, png_const_inforp info_ptr)
89 {
90    if (png_ptr != NULL && info_ptr != NULL)
91       return info_ptr->filter_type;
92
93    return (0);
94 }
95
96 png_byte PNGAPI
97 png_get_interlace_type(png_const_structrp png_ptr, png_const_inforp info_ptr)
98 {
99    if (png_ptr != NULL && info_ptr != NULL)
100       return info_ptr->interlace_type;
101
102    return (0);
103 }
104
105 png_byte PNGAPI
106 png_get_compression_type(png_const_structrp png_ptr, png_const_inforp info_ptr)
107 {
108    if (png_ptr != NULL && info_ptr != NULL)
109       return info_ptr->compression_type;
110
111    return (0);
112 }
113
114 png_uint_32 PNGAPI
115 png_get_x_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp
116    info_ptr)
117 {
118 #ifdef PNG_pHYs_SUPPORTED
119    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
120       {
121          png_debug1(1, "in %s retrieval function",
122              "png_get_x_pixels_per_meter");
123
124          if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER)
125             return (info_ptr->x_pixels_per_unit);
126       }
127 #else
128    PNG_UNUSED(png_ptr)
129    PNG_UNUSED(info_ptr)
130 #endif
131
132    return (0);
133 }
134
135 png_uint_32 PNGAPI
136 png_get_y_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp
137     info_ptr)
138 {
139 #ifdef PNG_pHYs_SUPPORTED
140    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
141    {
142       png_debug1(1, "in %s retrieval function",
143           "png_get_y_pixels_per_meter");
144
145       if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER)
146          return (info_ptr->y_pixels_per_unit);
147    }
148 #else
149    PNG_UNUSED(png_ptr)
150    PNG_UNUSED(info_ptr)
151 #endif
152
153    return (0);
154 }
155
156 png_uint_32 PNGAPI
157 png_get_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp info_ptr)
158 {
159 #ifdef PNG_pHYs_SUPPORTED
160    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
161    {
162       png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter");
163
164       if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER &&
165           info_ptr->x_pixels_per_unit == info_ptr->y_pixels_per_unit)
166          return (info_ptr->x_pixels_per_unit);
167    }
168 #else
169    PNG_UNUSED(png_ptr)
170    PNG_UNUSED(info_ptr)
171 #endif
172
173    return (0);
174 }
175
176 #ifdef PNG_FLOATING_POINT_SUPPORTED
177 float PNGAPI
178 png_get_pixel_aspect_ratio(png_const_structrp png_ptr, png_const_inforp
179    info_ptr)
180 {
181 #ifdef PNG_READ_pHYs_SUPPORTED
182    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
183    {
184       png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio");
185
186       if (info_ptr->x_pixels_per_unit != 0)
187          return ((float)((float)info_ptr->y_pixels_per_unit
188              /(float)info_ptr->x_pixels_per_unit));
189    }
190 #else
191    PNG_UNUSED(png_ptr)
192    PNG_UNUSED(info_ptr)
193 #endif
194
195    return ((float)0.0);
196 }
197 #endif
198
199 #ifdef PNG_FIXED_POINT_SUPPORTED
200 png_fixed_point PNGAPI
201 png_get_pixel_aspect_ratio_fixed(png_const_structrp png_ptr,
202     png_const_inforp info_ptr)
203 {
204 #ifdef PNG_READ_pHYs_SUPPORTED
205    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)
206        && info_ptr->x_pixels_per_unit > 0 && info_ptr->y_pixels_per_unit > 0
207        && info_ptr->x_pixels_per_unit <= PNG_UINT_31_MAX
208        && info_ptr->y_pixels_per_unit <= PNG_UINT_31_MAX)
209    {
210       png_fixed_point res;
211
212       png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio_fixed");
213
214       /* The following casts work because a PNG 4 byte integer only has a valid
215        * range of 0..2^31-1; otherwise the cast might overflow.
216        */
217       if (png_muldiv(&res, (png_int_32)info_ptr->y_pixels_per_unit, PNG_FP_1,
218           (png_int_32)info_ptr->x_pixels_per_unit))
219          return res;
220    }
221 #else
222    PNG_UNUSED(png_ptr)
223    PNG_UNUSED(info_ptr)
224 #endif
225
226    return 0;
227 }
228 #endif
229
230 png_int_32 PNGAPI
231 png_get_x_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr)
232 {
233 #ifdef PNG_oFFs_SUPPORTED
234    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
235    {
236       png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns");
237
238       if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER)
239          return (info_ptr->x_offset);
240    }
241 #else
242    PNG_UNUSED(png_ptr)
243    PNG_UNUSED(info_ptr)
244 #endif
245
246    return (0);
247 }
248
249 png_int_32 PNGAPI
250 png_get_y_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr)
251 {
252 #ifdef PNG_oFFs_SUPPORTED
253    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
254    {
255       png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns");
256
257       if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER)
258          return (info_ptr->y_offset);
259    }
260 #else
261    PNG_UNUSED(png_ptr)
262    PNG_UNUSED(info_ptr)
263 #endif
264
265    return (0);
266 }
267
268 png_int_32 PNGAPI
269 png_get_x_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr)
270 {
271 #ifdef PNG_oFFs_SUPPORTED
272    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
273    {
274       png_debug1(1, "in %s retrieval function", "png_get_x_offset_pixels");
275
276       if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL)
277          return (info_ptr->x_offset);
278    }
279 #else
280    PNG_UNUSED(png_ptr)
281    PNG_UNUSED(info_ptr)
282 #endif
283
284    return (0);
285 }
286
287 png_int_32 PNGAPI
288 png_get_y_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr)
289 {
290 #ifdef PNG_oFFs_SUPPORTED
291    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
292    {
293       png_debug1(1, "in %s retrieval function", "png_get_y_offset_pixels");
294
295       if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL)
296          return (info_ptr->y_offset);
297    }
298 #else
299    PNG_UNUSED(png_ptr)
300    PNG_UNUSED(info_ptr)
301 #endif
302
303    return (0);
304 }
305
306 #ifdef PNG_INCH_CONVERSIONS_SUPPORTED
307 static png_uint_32
308 ppi_from_ppm(png_uint_32 ppm)
309 {
310 #if 0
311    /* The conversion is *(2.54/100), in binary (32 digits):
312     * .00000110100000001001110101001001
313     */
314    png_uint_32 t1001, t1101;
315    ppm >>= 1;                  /* .1 */
316    t1001 = ppm + (ppm >> 3);   /* .1001 */
317    t1101 = t1001 + (ppm >> 1); /* .1101 */
318    ppm >>= 20;                 /* .000000000000000000001 */
319    t1101 += t1101 >> 15;       /* .1101000000000001101 */
320    t1001 >>= 11;               /* .000000000001001 */
321    t1001 += t1001 >> 12;       /* .000000000001001000000001001 */
322    ppm += t1001;               /* .000000000001001000001001001 */
323    ppm += t1101;               /* .110100000001001110101001001 */
324    return (ppm + 16) >> 5;/* .00000110100000001001110101001001 */
325 #else
326    /* The argument is a PNG unsigned integer, so it is not permitted
327     * to be bigger than 2^31.
328     */
329    png_fixed_point result;
330    if (ppm <= PNG_UINT_31_MAX && png_muldiv(&result, (png_int_32)ppm, 127,
331        5000))
332       return result;
333
334    /* Overflow. */
335    return 0;
336 #endif
337 }
338
339 png_uint_32 PNGAPI
340 png_get_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr)
341 {
342    return ppi_from_ppm(png_get_pixels_per_meter(png_ptr, info_ptr));
343 }
344
345 png_uint_32 PNGAPI
346 png_get_x_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr)
347 {
348    return ppi_from_ppm(png_get_x_pixels_per_meter(png_ptr, info_ptr));
349 }
350
351 png_uint_32 PNGAPI
352 png_get_y_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr)
353 {
354    return ppi_from_ppm(png_get_y_pixels_per_meter(png_ptr, info_ptr));
355 }
356
357 #ifdef PNG_FIXED_POINT_SUPPORTED
358 static png_fixed_point
359 png_fixed_inches_from_microns(png_const_structrp png_ptr, png_int_32 microns)
360 {
361    /* Convert from metres * 1,000,000 to inches * 100,000, meters to
362     * inches is simply *(100/2.54), so we want *(10/2.54) == 500/127.
363     * Notice that this can overflow - a warning is output and 0 is
364     * returned.
365     */
366    return png_muldiv_warn(png_ptr, microns, 500, 127);
367 }
368
369 png_fixed_point PNGAPI
370 png_get_x_offset_inches_fixed(png_const_structrp png_ptr,
371     png_const_inforp info_ptr)
372 {
373    return png_fixed_inches_from_microns(png_ptr,
374        png_get_x_offset_microns(png_ptr, info_ptr));
375 }
376 #endif
377
378 #ifdef PNG_FIXED_POINT_SUPPORTED
379 png_fixed_point PNGAPI
380 png_get_y_offset_inches_fixed(png_const_structrp png_ptr,
381     png_const_inforp info_ptr)
382 {
383    return png_fixed_inches_from_microns(png_ptr,
384        png_get_y_offset_microns(png_ptr, info_ptr));
385 }
386 #endif
387
388 #ifdef PNG_FLOATING_POINT_SUPPORTED
389 float PNGAPI
390 png_get_x_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr)
391 {
392    /* To avoid the overflow do the conversion directly in floating
393     * point.
394     */
395    return (float)(png_get_x_offset_microns(png_ptr, info_ptr) * .00003937);
396 }
397 #endif
398
399 #ifdef PNG_FLOATING_POINT_SUPPORTED
400 float PNGAPI
401 png_get_y_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr)
402 {
403    /* To avoid the overflow do the conversion directly in floating
404     * point.
405     */
406    return (float)(png_get_y_offset_microns(png_ptr, info_ptr) * .00003937);
407 }
408 #endif
409
410 #ifdef PNG_pHYs_SUPPORTED
411 png_uint_32 PNGAPI
412 png_get_pHYs_dpi(png_const_structrp png_ptr, png_const_inforp info_ptr,
413     png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
414 {
415    png_uint_32 retval = 0;
416
417    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
418    {
419       png_debug1(1, "in %s retrieval function", "pHYs");
420
421       if (res_x != NULL)
422       {
423          *res_x = info_ptr->x_pixels_per_unit;
424          retval |= PNG_INFO_pHYs;
425       }
426
427       if (res_y != NULL)
428       {
429          *res_y = info_ptr->y_pixels_per_unit;
430          retval |= PNG_INFO_pHYs;
431       }
432
433       if (unit_type != NULL)
434       {
435          *unit_type = (int)info_ptr->phys_unit_type;
436          retval |= PNG_INFO_pHYs;
437
438          if (*unit_type == 1)
439          {
440             if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);
441             if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);
442          }
443       }
444    }
445
446    return (retval);
447 }
448 #endif /* PNG_pHYs_SUPPORTED */
449 #endif  /* PNG_INCH_CONVERSIONS_SUPPORTED */
450
451 /* png_get_channels really belongs in here, too, but it's been around longer */
452
453 #endif  /* PNG_EASY_ACCESS_SUPPORTED */
454
455
456 png_byte PNGAPI
457 png_get_channels(png_const_structrp png_ptr, png_const_inforp info_ptr)
458 {
459    if (png_ptr != NULL && info_ptr != NULL)
460       return(info_ptr->channels);
461
462    return (0);
463 }
464
465 #ifdef PNG_READ_SUPPORTED
466 png_const_bytep PNGAPI
467 png_get_signature(png_const_structrp png_ptr, png_const_inforp info_ptr)
468 {
469    if (png_ptr != NULL && info_ptr != NULL)
470       return(info_ptr->signature);
471
472    return (NULL);
473 }
474 #endif
475
476 #ifdef PNG_bKGD_SUPPORTED
477 png_uint_32 PNGAPI
478 png_get_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
479    png_color_16p *background)
480 {
481    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
482        && background != NULL)
483    {
484       png_debug1(1, "in %s retrieval function", "bKGD");
485
486       *background = &(info_ptr->background);
487       return (PNG_INFO_bKGD);
488    }
489
490    return (0);
491 }
492 #endif
493
494 #ifdef PNG_cHRM_SUPPORTED
495 /* The XYZ APIs were added in 1.5.5 to take advantage of the code added at the
496  * same time to correct the rgb grayscale coefficient defaults obtained from the
497  * cHRM chunk in 1.5.4
498  */
499 #  ifdef PNG_FLOATING_POINT_SUPPORTED
500 png_uint_32 PNGAPI
501 png_get_cHRM(png_const_structrp png_ptr, png_const_inforp info_ptr,
502     double *white_x, double *white_y, double *red_x, double *red_y,
503     double *green_x, double *green_y, double *blue_x, double *blue_y)
504 {
505    /* Quiet API change: this code used to only return the end points if a cHRM
506     * chunk was present, but the end points can also come from iCCP or sRGB
507     * chunks, so in 1.6.0 the png_get_ APIs return the end points regardless and
508     * the png_set_ APIs merely check that set end points are mutually
509     * consistent.
510     */
511    if (png_ptr != NULL && info_ptr != NULL &&
512       (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS))
513    {
514       png_debug1(1, "in %s retrieval function", "cHRM");
515
516       if (white_x != NULL)
517          *white_x = png_float(png_ptr,
518             info_ptr->colorspace.end_points_xy.whitex, "cHRM white X");
519       if (white_y != NULL)
520          *white_y = png_float(png_ptr,
521             info_ptr->colorspace.end_points_xy.whitey, "cHRM white Y");
522       if (red_x != NULL)
523          *red_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redx,
524             "cHRM red X");
525       if (red_y != NULL)
526          *red_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redy,
527             "cHRM red Y");
528       if (green_x != NULL)
529          *green_x = png_float(png_ptr,
530             info_ptr->colorspace.end_points_xy.greenx, "cHRM green X");
531       if (green_y != NULL)
532          *green_y = png_float(png_ptr,
533             info_ptr->colorspace.end_points_xy.greeny, "cHRM green Y");
534       if (blue_x != NULL)
535          *blue_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluex,
536             "cHRM blue X");
537       if (blue_y != NULL)
538          *blue_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluey,
539             "cHRM blue Y");
540       return (PNG_INFO_cHRM);
541    }
542
543    return (0);
544 }
545
546 png_uint_32 PNGAPI
547 png_get_cHRM_XYZ(png_const_structrp png_ptr, png_const_inforp info_ptr,
548    double *red_X, double *red_Y, double *red_Z, double *green_X,
549    double *green_Y, double *green_Z, double *blue_X, double *blue_Y,
550    double *blue_Z)
551 {
552    if (png_ptr != NULL && info_ptr != NULL &&
553       (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS))
554    {
555       png_debug1(1, "in %s retrieval function", "cHRM_XYZ(float)");
556
557       if (red_X != NULL)
558          *red_X = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_X,
559             "cHRM red X");
560       if (red_Y != NULL)
561          *red_Y = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Y,
562             "cHRM red Y");
563       if (red_Z != NULL)
564          *red_Z = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Z,
565             "cHRM red Z");
566       if (green_X != NULL)
567          *green_X = png_float(png_ptr,
568             info_ptr->colorspace.end_points_XYZ.green_X, "cHRM green X");
569       if (green_Y != NULL)
570          *green_Y = png_float(png_ptr,
571             info_ptr->colorspace.end_points_XYZ.green_Y, "cHRM green Y");
572       if (green_Z != NULL)
573          *green_Z = png_float(png_ptr,
574             info_ptr->colorspace.end_points_XYZ.green_Z, "cHRM green Z");
575       if (blue_X != NULL)
576          *blue_X = png_float(png_ptr,
577             info_ptr->colorspace.end_points_XYZ.blue_X, "cHRM blue X");
578       if (blue_Y != NULL)
579          *blue_Y = png_float(png_ptr,
580             info_ptr->colorspace.end_points_XYZ.blue_Y, "cHRM blue Y");
581       if (blue_Z != NULL)
582          *blue_Z = png_float(png_ptr,
583             info_ptr->colorspace.end_points_XYZ.blue_Z, "cHRM blue Z");
584       return (PNG_INFO_cHRM);
585    }
586
587    return (0);
588 }
589 #  endif
590
591 #  ifdef PNG_FIXED_POINT_SUPPORTED
592 png_uint_32 PNGAPI
593 png_get_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,
594     png_fixed_point *int_red_X, png_fixed_point *int_red_Y,
595     png_fixed_point *int_red_Z, png_fixed_point *int_green_X,
596     png_fixed_point *int_green_Y, png_fixed_point *int_green_Z,
597     png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y,
598     png_fixed_point *int_blue_Z)
599 {
600    if (png_ptr != NULL && info_ptr != NULL &&
601       (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS))
602    {
603       png_debug1(1, "in %s retrieval function", "cHRM_XYZ");
604
605       if (int_red_X != NULL)
606          *int_red_X = info_ptr->colorspace.end_points_XYZ.red_X;
607       if (int_red_Y != NULL)
608          *int_red_Y = info_ptr->colorspace.end_points_XYZ.red_Y;
609       if (int_red_Z != NULL)
610          *int_red_Z = info_ptr->colorspace.end_points_XYZ.red_Z;
611       if (int_green_X != NULL)
612          *int_green_X = info_ptr->colorspace.end_points_XYZ.green_X;
613       if (int_green_Y != NULL)
614          *int_green_Y = info_ptr->colorspace.end_points_XYZ.green_Y;
615       if (int_green_Z != NULL)
616          *int_green_Z = info_ptr->colorspace.end_points_XYZ.green_Z;
617       if (int_blue_X != NULL)
618          *int_blue_X = info_ptr->colorspace.end_points_XYZ.blue_X;
619       if (int_blue_Y != NULL)
620          *int_blue_Y = info_ptr->colorspace.end_points_XYZ.blue_Y;
621       if (int_blue_Z != NULL)
622          *int_blue_Z = info_ptr->colorspace.end_points_XYZ.blue_Z;
623       return (PNG_INFO_cHRM);
624    }
625
626    return (0);
627 }
628
629 png_uint_32 PNGAPI
630 png_get_cHRM_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,
631     png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,
632     png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,
633     png_fixed_point *blue_x, png_fixed_point *blue_y)
634 {
635    png_debug1(1, "in %s retrieval function", "cHRM");
636
637    if (png_ptr != NULL && info_ptr != NULL &&
638       (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS))
639    {
640       if (white_x != NULL)
641          *white_x = info_ptr->colorspace.end_points_xy.whitex;
642       if (white_y != NULL)
643          *white_y = info_ptr->colorspace.end_points_xy.whitey;
644       if (red_x != NULL)
645          *red_x = info_ptr->colorspace.end_points_xy.redx;
646       if (red_y != NULL)
647          *red_y = info_ptr->colorspace.end_points_xy.redy;
648       if (green_x != NULL)
649          *green_x = info_ptr->colorspace.end_points_xy.greenx;
650       if (green_y != NULL)
651          *green_y = info_ptr->colorspace.end_points_xy.greeny;
652       if (blue_x != NULL)
653          *blue_x = info_ptr->colorspace.end_points_xy.bluex;
654       if (blue_y != NULL)
655          *blue_y = info_ptr->colorspace.end_points_xy.bluey;
656       return (PNG_INFO_cHRM);
657    }
658
659    return (0);
660 }
661 #  endif
662 #endif
663
664 #ifdef PNG_gAMA_SUPPORTED
665 #  ifdef PNG_FIXED_POINT_SUPPORTED
666 png_uint_32 PNGAPI
667 png_get_gAMA_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,
668     png_fixed_point *file_gamma)
669 {
670    png_debug1(1, "in %s retrieval function", "gAMA");
671
672    if (png_ptr != NULL && info_ptr != NULL &&
673       (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) &&
674       file_gamma != NULL)
675    {
676       *file_gamma = info_ptr->colorspace.gamma;
677       return (PNG_INFO_gAMA);
678    }
679
680    return (0);
681 }
682 #  endif
683
684 #  ifdef PNG_FLOATING_POINT_SUPPORTED
685 png_uint_32 PNGAPI
686 png_get_gAMA(png_const_structrp png_ptr, png_const_inforp info_ptr,
687     double *file_gamma)
688 {
689    png_debug1(1, "in %s retrieval function", "gAMA(float)");
690
691    if (png_ptr != NULL && info_ptr != NULL &&
692       (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) &&
693       file_gamma != NULL)
694    {
695       *file_gamma = png_float(png_ptr, info_ptr->colorspace.gamma,
696          "png_get_gAMA");
697       return (PNG_INFO_gAMA);
698    }
699
700    return (0);
701 }
702 #  endif
703 #endif
704
705 #ifdef PNG_sRGB_SUPPORTED
706 png_uint_32 PNGAPI
707 png_get_sRGB(png_const_structrp png_ptr, png_const_inforp info_ptr,
708     int *file_srgb_intent)
709 {
710    png_debug1(1, "in %s retrieval function", "sRGB");
711
712    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
713        && file_srgb_intent != NULL)
714    {
715       *file_srgb_intent = info_ptr->colorspace.rendering_intent;
716       return (PNG_INFO_sRGB);
717    }
718
719    return (0);
720 }
721 #endif
722
723 #ifdef PNG_iCCP_SUPPORTED
724 png_uint_32 PNGAPI
725 png_get_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
726     png_charpp name, int *compression_type,
727     png_bytepp profile, png_uint_32 *proflen)
728 {
729    png_debug1(1, "in %s retrieval function", "iCCP");
730
731    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)
732        && name != NULL && compression_type != NULL && profile != NULL &&
733                  proflen != NULL)
734    {
735       *name = info_ptr->iccp_name;
736       *profile = info_ptr->iccp_profile;
737       *proflen = png_get_uint_32(info_ptr->iccp_profile);
738       /* This is somewhat irrelevant since the profile data returned has
739        * actually been uncompressed.
740        */
741       *compression_type = PNG_COMPRESSION_TYPE_BASE;
742       return (PNG_INFO_iCCP);
743    }
744
745    return (0);
746 }
747 #endif
748
749 #ifdef PNG_sPLT_SUPPORTED
750 int PNGAPI
751 png_get_sPLT(png_const_structrp png_ptr, png_inforp info_ptr,
752     png_sPLT_tpp spalettes)
753 {
754    if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)
755    {
756       *spalettes = info_ptr->splt_palettes;
757       return info_ptr->splt_palettes_num;
758    }
759
760    return (0);
761 }
762 #endif
763
764 #ifdef PNG_hIST_SUPPORTED
765 png_uint_32 PNGAPI
766 png_get_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
767     png_uint_16p *hist)
768 {
769    png_debug1(1, "in %s retrieval function", "hIST");
770
771    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
772        && hist != NULL)
773    {
774       *hist = info_ptr->hist;
775       return (PNG_INFO_hIST);
776    }
777
778    return (0);
779 }
780 #endif
781
782 png_uint_32 PNGAPI
783 png_get_IHDR(png_const_structrp png_ptr, png_const_inforp info_ptr,
784     png_uint_32 *width, png_uint_32 *height, int *bit_depth,
785     int *color_type, int *interlace_type, int *compression_type,
786     int *filter_type)
787 {
788    png_debug1(1, "in %s retrieval function", "IHDR");
789
790    if (png_ptr == NULL || info_ptr == NULL || width == NULL ||
791        height == NULL || bit_depth == NULL || color_type == NULL)
792       return (0);
793
794    *width = info_ptr->width;
795    *height = info_ptr->height;
796    *bit_depth = info_ptr->bit_depth;
797    *color_type = info_ptr->color_type;
798
799    if (compression_type != NULL)
800       *compression_type = info_ptr->compression_type;
801
802    if (filter_type != NULL)
803       *filter_type = info_ptr->filter_type;
804
805    if (interlace_type != NULL)
806       *interlace_type = info_ptr->interlace_type;
807
808    /* This is redundant if we can be sure that the info_ptr values were all
809     * assigned in png_set_IHDR().  We do the check anyhow in case an
810     * application has ignored our advice not to mess with the members
811     * of info_ptr directly.
812     */
813    png_check_IHDR(png_ptr, info_ptr->width, info_ptr->height,
814        info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
815        info_ptr->compression_type, info_ptr->filter_type);
816
817    return (1);
818 }
819
820 #ifdef PNG_oFFs_SUPPORTED
821 png_uint_32 PNGAPI
822 png_get_oFFs(png_const_structrp png_ptr, png_const_inforp info_ptr,
823     png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)
824 {
825    png_debug1(1, "in %s retrieval function", "oFFs");
826
827    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
828        && offset_x != NULL && offset_y != NULL && unit_type != NULL)
829    {
830       *offset_x = info_ptr->x_offset;
831       *offset_y = info_ptr->y_offset;
832       *unit_type = (int)info_ptr->offset_unit_type;
833       return (PNG_INFO_oFFs);
834    }
835
836    return (0);
837 }
838 #endif
839
840 #ifdef PNG_pCAL_SUPPORTED
841 png_uint_32 PNGAPI
842 png_get_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
843     png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
844     png_charp *units, png_charpp *params)
845 {
846    png_debug1(1, "in %s retrieval function", "pCAL");
847
848    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)
849        && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
850        nparams != NULL && units != NULL && params != NULL)
851    {
852       *purpose = info_ptr->pcal_purpose;
853       *X0 = info_ptr->pcal_X0;
854       *X1 = info_ptr->pcal_X1;
855       *type = (int)info_ptr->pcal_type;
856       *nparams = (int)info_ptr->pcal_nparams;
857       *units = info_ptr->pcal_units;
858       *params = info_ptr->pcal_params;
859       return (PNG_INFO_pCAL);
860    }
861
862    return (0);
863 }
864 #endif
865
866 #ifdef PNG_sCAL_SUPPORTED
867 #  ifdef PNG_FIXED_POINT_SUPPORTED
868 #    if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || \
869          defined(PNG_FLOATING_POINT_SUPPORTED)
870 png_uint_32 PNGAPI
871 png_get_sCAL_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,
872     int *unit, png_fixed_point *width, png_fixed_point *height)
873 {
874    if (png_ptr != NULL && info_ptr != NULL &&
875        (info_ptr->valid & PNG_INFO_sCAL))
876    {
877       *unit = info_ptr->scal_unit;
878       /*TODO: make this work without FP support; the API is currently eliminated
879        * if neither floating point APIs nor internal floating point arithmetic
880        * are enabled.
881        */
882       *width = png_fixed(png_ptr, atof(info_ptr->scal_s_width), "sCAL width");
883       *height = png_fixed(png_ptr, atof(info_ptr->scal_s_height),
884          "sCAL height");
885       return (PNG_INFO_sCAL);
886    }
887
888    return(0);
889 }
890 #    endif /* FLOATING_ARITHMETIC */
891 #  endif /* FIXED_POINT */
892 #  ifdef PNG_FLOATING_POINT_SUPPORTED
893 png_uint_32 PNGAPI
894 png_get_sCAL(png_const_structrp png_ptr, png_const_inforp info_ptr,
895     int *unit, double *width, double *height)
896 {
897    if (png_ptr != NULL && info_ptr != NULL &&
898        (info_ptr->valid & PNG_INFO_sCAL))
899    {
900       *unit = info_ptr->scal_unit;
901       *width = atof(info_ptr->scal_s_width);
902       *height = atof(info_ptr->scal_s_height);
903       return (PNG_INFO_sCAL);
904    }
905
906    return(0);
907 }
908 #  endif /* FLOATING POINT */
909 png_uint_32 PNGAPI
910 png_get_sCAL_s(png_const_structrp png_ptr, png_const_inforp info_ptr,
911     int *unit, png_charpp width, png_charpp height)
912 {
913    if (png_ptr != NULL && info_ptr != NULL &&
914        (info_ptr->valid & PNG_INFO_sCAL))
915    {
916       *unit = info_ptr->scal_unit;
917       *width = info_ptr->scal_s_width;
918       *height = info_ptr->scal_s_height;
919       return (PNG_INFO_sCAL);
920    }
921
922    return(0);
923 }
924 #endif /* sCAL */
925
926 #ifdef PNG_pHYs_SUPPORTED
927 png_uint_32 PNGAPI
928 png_get_pHYs(png_const_structrp png_ptr, png_const_inforp info_ptr,
929     png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
930 {
931    png_uint_32 retval = 0;
932
933    png_debug1(1, "in %s retrieval function", "pHYs");
934
935    if (png_ptr != NULL && info_ptr != NULL &&
936        (info_ptr->valid & PNG_INFO_pHYs))
937    {
938       if (res_x != NULL)
939       {
940          *res_x = info_ptr->x_pixels_per_unit;
941          retval |= PNG_INFO_pHYs;
942       }
943
944       if (res_y != NULL)
945       {
946          *res_y = info_ptr->y_pixels_per_unit;
947          retval |= PNG_INFO_pHYs;
948       }
949
950       if (unit_type != NULL)
951       {
952          *unit_type = (int)info_ptr->phys_unit_type;
953          retval |= PNG_INFO_pHYs;
954       }
955    }
956
957    return (retval);
958 }
959 #endif /* pHYs */
960
961 png_uint_32 PNGAPI
962 png_get_PLTE(png_const_structrp png_ptr, png_inforp info_ptr,
963     png_colorp *palette, int *num_palette)
964 {
965    png_debug1(1, "in %s retrieval function", "PLTE");
966
967    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)
968        && palette != NULL)
969    {
970       *palette = info_ptr->palette;
971       *num_palette = info_ptr->num_palette;
972       png_debug1(3, "num_palette = %d", *num_palette);
973       return (PNG_INFO_PLTE);
974    }
975
976    return (0);
977 }
978
979 #ifdef PNG_sBIT_SUPPORTED
980 png_uint_32 PNGAPI
981 png_get_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
982     png_color_8p *sig_bit)
983 {
984    png_debug1(1, "in %s retrieval function", "sBIT");
985
986    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)
987        && sig_bit != NULL)
988    {
989       *sig_bit = &(info_ptr->sig_bit);
990       return (PNG_INFO_sBIT);
991    }
992
993    return (0);
994 }
995 #endif
996
997 #ifdef PNG_TEXT_SUPPORTED
998 int PNGAPI
999 png_get_text(png_const_structrp png_ptr, png_inforp info_ptr,
1000     png_textp *text_ptr, int *num_text)
1001 {
1002    if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
1003    {
1004       png_debug1(1, "in 0x%lx retrieval function",
1005          (unsigned long)png_ptr->chunk_name);
1006
1007       if (text_ptr != NULL)
1008          *text_ptr = info_ptr->text;
1009
1010       if (num_text != NULL)
1011          *num_text = info_ptr->num_text;
1012
1013       return info_ptr->num_text;
1014    }
1015
1016    if (num_text != NULL)
1017       *num_text = 0;
1018
1019    return(0);
1020 }
1021 #endif
1022
1023 #ifdef PNG_tIME_SUPPORTED
1024 png_uint_32 PNGAPI
1025 png_get_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
1026     png_timep *mod_time)
1027 {
1028    png_debug1(1, "in %s retrieval function", "tIME");
1029
1030    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)
1031        && mod_time != NULL)
1032    {
1033       *mod_time = &(info_ptr->mod_time);
1034       return (PNG_INFO_tIME);
1035    }
1036
1037    return (0);
1038 }
1039 #endif
1040
1041 #ifdef PNG_tRNS_SUPPORTED
1042 png_uint_32 PNGAPI
1043 png_get_tRNS(png_const_structrp png_ptr, png_inforp info_ptr,
1044     png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color)
1045 {
1046    png_uint_32 retval = 0;
1047    if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
1048    {
1049       png_debug1(1, "in %s retrieval function", "tRNS");
1050
1051       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1052       {
1053          if (trans_alpha != NULL)
1054          {
1055             *trans_alpha = info_ptr->trans_alpha;
1056             retval |= PNG_INFO_tRNS;
1057          }
1058
1059          if (trans_color != NULL)
1060             *trans_color = &(info_ptr->trans_color);
1061       }
1062
1063       else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
1064       {
1065          if (trans_color != NULL)
1066          {
1067             *trans_color = &(info_ptr->trans_color);
1068             retval |= PNG_INFO_tRNS;
1069          }
1070
1071          if (trans_alpha != NULL)
1072             *trans_alpha = NULL;
1073       }
1074
1075       if (num_trans != NULL)
1076       {
1077          *num_trans = info_ptr->num_trans;
1078          retval |= PNG_INFO_tRNS;
1079       }
1080    }
1081
1082    return (retval);
1083 }
1084 #endif
1085
1086 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1087 int PNGAPI
1088 png_get_unknown_chunks(png_const_structrp png_ptr, png_inforp info_ptr,
1089     png_unknown_chunkpp unknowns)
1090 {
1091    if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)
1092    {
1093       *unknowns = info_ptr->unknown_chunks;
1094       return info_ptr->unknown_chunks_num;
1095    }
1096
1097    return (0);
1098 }
1099 #endif
1100
1101 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1102 png_byte PNGAPI
1103 png_get_rgb_to_gray_status (png_const_structrp png_ptr)
1104 {
1105    return (png_byte)(png_ptr ? png_ptr->rgb_to_gray_status : 0);
1106 }
1107 #endif
1108
1109 #ifdef PNG_USER_CHUNKS_SUPPORTED
1110 png_voidp PNGAPI
1111 png_get_user_chunk_ptr(png_const_structrp png_ptr)
1112 {
1113    return (png_ptr ? png_ptr->user_chunk_ptr : NULL);
1114 }
1115 #endif
1116
1117 png_size_t PNGAPI
1118 png_get_compression_buffer_size(png_const_structrp png_ptr)
1119 {
1120    if (png_ptr == NULL)
1121       return 0;
1122
1123 #  ifdef PNG_WRITE_SUPPORTED
1124       if (png_ptr->mode & PNG_IS_READ_STRUCT)
1125 #  endif
1126    {
1127 #     ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1128          return png_ptr->IDAT_read_size;
1129 #     else
1130          return PNG_IDAT_READ_SIZE;
1131 #     endif
1132    }
1133
1134 #  ifdef PNG_WRITE_SUPPORTED
1135       else
1136          return png_ptr->zbuffer_size;
1137 #  endif
1138 }
1139
1140 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
1141 /* These functions were added to libpng 1.2.6 and were enabled
1142  * by default in libpng-1.4.0 */
1143 png_uint_32 PNGAPI
1144 png_get_user_width_max (png_const_structrp png_ptr)
1145 {
1146    return (png_ptr ? png_ptr->user_width_max : 0);
1147 }
1148
1149 png_uint_32 PNGAPI
1150 png_get_user_height_max (png_const_structrp png_ptr)
1151 {
1152    return (png_ptr ? png_ptr->user_height_max : 0);
1153 }
1154
1155 /* This function was added to libpng 1.4.0 */
1156 png_uint_32 PNGAPI
1157 png_get_chunk_cache_max (png_const_structrp png_ptr)
1158 {
1159    return (png_ptr ? png_ptr->user_chunk_cache_max : 0);
1160 }
1161
1162 /* This function was added to libpng 1.4.1 */
1163 png_alloc_size_t PNGAPI
1164 png_get_chunk_malloc_max (png_const_structrp png_ptr)
1165 {
1166    return (png_ptr ? png_ptr->user_chunk_malloc_max : 0);
1167 }
1168 #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1169
1170 /* These functions were added to libpng 1.4.0 */
1171 #ifdef PNG_IO_STATE_SUPPORTED
1172 png_uint_32 PNGAPI
1173 png_get_io_state (png_const_structrp png_ptr)
1174 {
1175    return png_ptr->io_state;
1176 }
1177
1178 png_uint_32 PNGAPI
1179 png_get_io_chunk_type (png_const_structrp png_ptr)
1180 {
1181    return png_ptr->chunk_name;
1182 }
1183 #endif /* ?PNG_IO_STATE_SUPPORTED */
1184
1185 #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
1186 #  ifdef PNG_GET_PALETTE_MAX_SUPPORTED
1187 int PNGAPI
1188 png_get_palette_max(png_const_structp png_ptr, png_const_infop info_ptr)
1189 {
1190    if (png_ptr != NULL && info_ptr != NULL)
1191       return png_ptr->num_palette_max;
1192
1193    return (-1);
1194 }
1195 #  endif
1196 #endif
1197
1198 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */