2 /* pngget.c - retrieval of values from info struct
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
13 #include "../png/png.h"
16 png_get_valid(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 flag
)
18 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
19 return(info_ptr
->valid
& flag
);
25 png_get_rowbytes(png_structp png_ptr
, png_infop info_ptr
)
27 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
28 return(info_ptr
->rowbytes
);
33 #ifdef PNG_EASY_ACCESS_SUPPORTED
34 /* easy access to info, added in libpng-0.99 */
36 png_get_image_width(png_structp png_ptr
, png_infop info_ptr
)
38 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
40 return info_ptr
->width
;
46 png_get_image_height(png_structp png_ptr
, png_infop info_ptr
)
48 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
50 return info_ptr
->height
;
56 png_get_bit_depth(png_structp png_ptr
, png_infop info_ptr
)
58 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
60 return info_ptr
->bit_depth
;
66 png_get_color_type(png_structp png_ptr
, png_infop info_ptr
)
68 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
70 return info_ptr
->color_type
;
76 png_get_filter_type(png_structp png_ptr
, png_infop info_ptr
)
78 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
80 return info_ptr
->filter_type
;
86 png_get_interlace_type(png_structp png_ptr
, png_infop info_ptr
)
88 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
90 return info_ptr
->interlace_type
;
96 png_get_compression_type(png_structp png_ptr
, png_infop info_ptr
)
98 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
100 return info_ptr
->compression_type
;
106 png_get_x_pixels_per_meter(png_structp png_ptr
, png_infop info_ptr
)
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
))
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
)
114 else return (info_ptr
->x_pixels_per_unit
);
122 png_get_y_pixels_per_meter(png_structp png_ptr
, png_infop info_ptr
)
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
))
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
)
130 else return (info_ptr
->y_pixels_per_unit
);
138 png_get_pixels_per_meter(png_structp png_ptr
, png_infop info_ptr
)
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
))
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
)
147 else return (info_ptr
->x_pixels_per_unit
);
155 png_get_pixel_aspect_ratio(png_structp png_ptr
, png_infop info_ptr
)
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
))
160 png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio");
161 if (info_ptr
->x_pixels_per_unit
== 0)
164 return ((float)info_ptr
->y_pixels_per_unit
165 /(float)info_ptr
->x_pixels_per_unit
);
173 png_get_x_offset_microns(png_structp png_ptr
, png_infop info_ptr
)
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
))
178 png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
179 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_MICROMETER
)
181 else return (info_ptr
->x_offset
);
189 png_get_y_offset_microns(png_structp png_ptr
, png_infop info_ptr
)
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
))
194 png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
195 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_MICROMETER
)
197 else return (info_ptr
->y_offset
);
205 png_get_x_offset_pixels(png_structp png_ptr
, png_infop info_ptr
)
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
))
210 png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
211 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_PIXEL
)
213 else return (info_ptr
->x_offset
);
221 png_get_y_offset_pixels(png_structp png_ptr
, png_infop info_ptr
)
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
))
226 png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
227 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_PIXEL
)
229 else return (info_ptr
->y_offset
);
236 #ifdef PNG_INCH_CONVERSIONS
238 png_get_pixels_per_inch(png_structp png_ptr
, png_infop info_ptr
)
240 return ((png_uint_32
)((float)png_get_pixels_per_meter(png_ptr
, info_ptr
)
245 png_get_x_pixels_per_inch(png_structp png_ptr
, png_infop info_ptr
)
247 return ((png_uint_32
)((float)png_get_x_pixels_per_meter(png_ptr
, info_ptr
)
252 png_get_y_pixels_per_inch(png_structp png_ptr
, png_infop info_ptr
)
254 return ((png_uint_32
)((float)png_get_y_pixels_per_meter(png_ptr
, info_ptr
)
259 png_get_x_offset_inches(png_structp png_ptr
, png_infop info_ptr
)
261 return ((float)png_get_x_offset_microns(png_ptr
, info_ptr
)
262 *.03937/1000000. +.5)
266 png_get_y_offset_inches(png_structp png_ptr
, png_infop info_ptr
)
268 return ((float)png_get_y_offset_microns(png_ptr
, info_ptr
)
269 *.03937/1000000. +.5)
272 #if defined(PNG_READ_pHYs_SUPPORTED)
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
)
277 png_uint_32 retval
= 0;
279 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_pHYs
)
281 png_debug1(1, "in %s retrieval function\n", "pHYs");
284 *res_x
= info_ptr
->x_pixels_per_unit
;
285 retval
|= PNG_INFO_pHYs
;
289 *res_y
= info_ptr
->y_pixels_per_unit
;
290 retval
|= PNG_INFO_pHYs
;
292 if (unit_type
!= NULL
)
294 *unit_type
= (int)info_ptr
->phys_unit_type
;
295 retval
|= PNG_INFO_pHYs
;
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);
307 #endif /* PNG_INCH_CONVERSIONS */
309 /* png_get_channels really belongs in here, too, but it's been around longer */
310 #endif /* PNG_EASY_ACCESS_SUPPORTED */
313 png_get_channels(png_structp png_ptr
, png_infop info_ptr
)
315 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
316 return(info_ptr
->channels
);
322 png_get_signature(png_structp png_ptr
, png_infop info_ptr
)
324 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
325 return(info_ptr
->signature
);
330 #if defined(PNG_READ_bKGD_SUPPORTED)
332 png_get_bKGD(png_structp png_ptr
, png_infop info_ptr
,
333 png_color_16p
*background
)
335 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_bKGD
)
336 && background
!= NULL
)
338 png_debug1(1, "in %s retrieval function\n", "bKGD");
339 *background
= &(info_ptr
->background
);
340 return (PNG_INFO_bKGD
);
346 #if defined(PNG_READ_cHRM_SUPPORTED)
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
)
352 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_cHRM
))
354 png_debug1(1, "in %s retrieval function\n", "cHRM");
356 *white_x
= (double)info_ptr
->x_white
;
358 *white_y
= (double)info_ptr
->y_white
;
360 *red_x
= (double)info_ptr
->x_red
;
362 *red_y
= (double)info_ptr
->y_red
;
364 *green_x
= (double)info_ptr
->x_green
;
366 *green_y
= (double)info_ptr
->y_green
;
368 *blue_x
= (double)info_ptr
->x_blue
;
370 *blue_y
= (double)info_ptr
->y_blue
;
371 return (PNG_INFO_cHRM
);
377 #if defined(PNG_READ_gAMA_SUPPORTED)
379 png_get_gAMA(png_structp png_ptr
, png_infop info_ptr
, double *file_gamma
)
381 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_gAMA
)
382 && file_gamma
!= NULL
)
384 png_debug1(1, "in %s retrieval function\n", "gAMA");
385 *file_gamma
= (double)info_ptr
->gamma
;
386 return (PNG_INFO_gAMA
);
392 #if defined(PNG_READ_sRGB_SUPPORTED)
394 png_get_sRGB(png_structp png_ptr
, png_infop info_ptr
, int *file_srgb_intent
)
396 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sRGB
)
397 && file_srgb_intent
!= NULL
)
399 png_debug1(1, "in %s retrieval function\n", "sRGB");
400 *file_srgb_intent
= (int)info_ptr
->srgb_intent
;
401 return (PNG_INFO_sRGB
);
407 #if defined(PNG_READ_hIST_SUPPORTED)
409 png_get_hIST(png_structp png_ptr
, png_infop info_ptr
, png_uint_16p
*hist
)
411 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_hIST
)
414 png_debug1(1, "in %s retrieval function\n", "hIST");
415 *hist
= info_ptr
->hist
;
416 return (PNG_INFO_hIST
);
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
,
429 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& width
!= NULL
&& height
!= NULL
&&
430 bit_depth
!= NULL
&& color_type
!= NULL
)
432 int pixel_depth
, channels
;
433 png_uint_32 rowbytes_per_pixel
;
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
;
447 /* check for potential overflow of rowbytes */
448 if (*color_type
== PNG_COLOR_TYPE_PALETTE
)
450 else if (*color_type
& PNG_COLOR_MASK_COLOR
)
454 if (*color_type
& PNG_COLOR_MASK_ALPHA
)
456 pixel_depth
= *bit_depth
* channels
;
457 rowbytes_per_pixel
= (pixel_depth
+ 7) >> 3;
458 if ((*width
> (png_uint_32
)2147483647L/rowbytes_per_pixel
))
461 "Width too large for libpng to process image data.");
468 #if defined(PNG_READ_oFFs_SUPPORTED)
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
)
473 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
)
474 && offset_x
!= NULL
&& offset_y
!= NULL
&& unit_type
!= NULL
)
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
);
486 #if defined(PNG_READ_pCAL_SUPPORTED)
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
)
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
)
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
);
510 #if defined(PNG_READ_pHYs_SUPPORTED)
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
)
515 png_uint_32 retval
= 0;
517 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_pHYs
)
519 png_debug1(1, "in %s retrieval function\n", "pHYs");
522 *res_x
= info_ptr
->x_pixels_per_unit
;
523 retval
|= PNG_INFO_pHYs
;
527 *res_y
= info_ptr
->y_pixels_per_unit
;
528 retval
|= PNG_INFO_pHYs
;
530 if (unit_type
!= NULL
)
532 *unit_type
= (int)info_ptr
->phys_unit_type
;
533 retval
|= PNG_INFO_pHYs
;
541 png_get_PLTE(png_structp png_ptr
, png_infop info_ptr
, png_colorp
*palette
,
544 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_PLTE
&&
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
);
556 #if defined(PNG_READ_sBIT_SUPPORTED)
558 png_get_sBIT(png_structp png_ptr
, png_infop info_ptr
, png_color_8p
*sig_bit
)
560 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_sBIT
&&
563 png_debug1(1, "in %s retrieval function\n", "sBIT");
564 *sig_bit
= &(info_ptr
->sig_bit
);
565 return (PNG_INFO_sBIT
);
571 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
573 png_get_text(png_structp png_ptr
, png_infop info_ptr
, png_textp
*text_ptr
,
576 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->num_text
> 0)
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
);
591 #if defined(PNG_READ_tIME_SUPPORTED)
593 png_get_tIME(png_structp png_ptr
, png_infop info_ptr
, png_timep
*mod_time
)
595 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_tIME
&&
598 png_debug1(1, "in %s retrieval function\n", "tIME");
599 *mod_time
= &(info_ptr
->mod_time
);
600 return (PNG_INFO_tIME
);
606 #if defined(PNG_READ_tRNS_SUPPORTED)
608 png_get_tRNS(png_structp png_ptr
, png_infop info_ptr
,
609 png_bytep
*trans
, int *num_trans
, png_color_16p
*trans_values
)
611 png_uint_32 retval
= 0;
612 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_tRNS
)
614 png_debug1(1, "in %s retrieval function\n", "tRNS");
615 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
619 *trans
= info_ptr
->trans
;
620 retval
|= PNG_INFO_tRNS
;
622 if (trans_values
!= NULL
)
623 *trans_values
= &(info_ptr
->trans_values
);
625 else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
627 if (trans_values
!= NULL
)
629 *trans_values
= &(info_ptr
->trans_values
);
630 retval
|= PNG_INFO_tRNS
;
635 if(num_trans
!= NULL
)
637 *num_trans
= info_ptr
->num_trans
;
638 retval
|= PNG_INFO_tRNS
;