2 /* pngget.c - retrieval of values from info struct
4 * libpng 1.0.3 - January 14, 1999
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, 1999 Glenn Randers-Pehrson
15 png_get_valid(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 flag
)
17 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
18 return(info_ptr
->valid
& flag
);
24 png_get_rowbytes(png_structp png_ptr
, png_infop info_ptr
)
26 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
27 return(info_ptr
->rowbytes
);
32 #ifdef PNG_EASY_ACCESS_SUPPORTED
33 /* easy access to info, added in libpng-0.99 */
35 png_get_image_width(png_structp png_ptr
, png_infop info_ptr
)
37 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
39 return info_ptr
->width
;
45 png_get_image_height(png_structp png_ptr
, png_infop info_ptr
)
47 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
49 return info_ptr
->height
;
55 png_get_bit_depth(png_structp png_ptr
, png_infop info_ptr
)
57 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
59 return info_ptr
->bit_depth
;
65 png_get_color_type(png_structp png_ptr
, png_infop info_ptr
)
67 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
69 return info_ptr
->color_type
;
75 png_get_filter_type(png_structp png_ptr
, png_infop info_ptr
)
77 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
79 return info_ptr
->filter_type
;
85 png_get_interlace_type(png_structp png_ptr
, png_infop info_ptr
)
87 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
89 return info_ptr
->interlace_type
;
95 png_get_compression_type(png_structp png_ptr
, png_infop info_ptr
)
97 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
99 return info_ptr
->compression_type
;
105 png_get_x_pixels_per_meter(png_structp png_ptr
, png_infop info_ptr
)
107 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
108 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pHYs
))
110 png_debug1(1, "in %s retrieval function\n", "png_get_x_pixels_per_meter");
111 if(info_ptr
->phys_unit_type
!= PNG_RESOLUTION_METER
)
113 else return (info_ptr
->x_pixels_per_unit
);
121 png_get_y_pixels_per_meter(png_structp png_ptr
, png_infop info_ptr
)
123 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
124 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pHYs
))
126 png_debug1(1, "in %s retrieval function\n", "png_get_y_pixels_per_meter");
127 if(info_ptr
->phys_unit_type
!= PNG_RESOLUTION_METER
)
129 else return (info_ptr
->y_pixels_per_unit
);
137 png_get_pixels_per_meter(png_structp png_ptr
, png_infop info_ptr
)
139 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
140 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pHYs
))
142 png_debug1(1, "in %s retrieval function\n", "png_get_pixels_per_meter");
143 if(info_ptr
->phys_unit_type
!= PNG_RESOLUTION_METER
||
144 info_ptr
->x_pixels_per_unit
!= info_ptr
->y_pixels_per_unit
)
146 else return (info_ptr
->x_pixels_per_unit
);
154 png_get_pixel_aspect_ratio(png_structp png_ptr
, png_infop info_ptr
)
156 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
157 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_pHYs
))
159 png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio");
160 if (info_ptr
->x_pixels_per_unit
== 0)
163 return ((float)info_ptr
->y_pixels_per_unit
164 /(float)info_ptr
->x_pixels_per_unit
);
172 png_get_x_offset_microns(png_structp png_ptr
, png_infop info_ptr
)
174 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
175 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
))
177 png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
178 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_MICROMETER
)
180 else return (info_ptr
->x_offset
);
188 png_get_y_offset_microns(png_structp png_ptr
, png_infop info_ptr
)
190 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
191 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
))
193 png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
194 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_MICROMETER
)
196 else return (info_ptr
->y_offset
);
204 png_get_x_offset_pixels(png_structp png_ptr
, png_infop info_ptr
)
206 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
207 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
))
209 png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
210 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_PIXEL
)
212 else return (info_ptr
->x_offset
);
220 png_get_y_offset_pixels(png_structp png_ptr
, png_infop info_ptr
)
222 #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
223 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
))
225 png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
226 if(info_ptr
->offset_unit_type
!= PNG_OFFSET_PIXEL
)
228 else return (info_ptr
->y_offset
);
235 #ifdef PNG_INCH_CONVERSIONS
237 png_get_pixels_per_inch(png_structp png_ptr
, png_infop info_ptr
)
239 return ((png_uint_32
)((float)png_get_pixels_per_meter(png_ptr
, info_ptr
)
244 png_get_x_pixels_per_inch(png_structp png_ptr
, png_infop info_ptr
)
246 return ((png_uint_32
)((float)png_get_x_pixels_per_meter(png_ptr
, info_ptr
)
251 png_get_y_pixels_per_inch(png_structp png_ptr
, png_infop info_ptr
)
253 return ((png_uint_32
)((float)png_get_y_pixels_per_meter(png_ptr
, info_ptr
)
258 png_get_x_offset_inches(png_structp png_ptr
, png_infop info_ptr
)
260 return ((float)png_get_x_offset_microns(png_ptr
, info_ptr
)
261 *.03937/1000000. +.5)
265 png_get_y_offset_inches(png_structp png_ptr
, png_infop info_ptr
)
267 return ((float)png_get_y_offset_microns(png_ptr
, info_ptr
)
268 *.03937/1000000. +.5)
271 #if defined(PNG_READ_pHYs_SUPPORTED)
273 png_get_pHYs_dpi(png_structp png_ptr
, png_infop info_ptr
,
274 png_uint_32
*res_x
, png_uint_32
*res_y
, int *unit_type
)
276 png_uint_32 retval
= 0;
278 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_pHYs
)
280 png_debug1(1, "in %s retrieval function\n", "pHYs");
283 *res_x
= info_ptr
->x_pixels_per_unit
;
284 retval
|= PNG_INFO_pHYs
;
288 *res_y
= info_ptr
->y_pixels_per_unit
;
289 retval
|= PNG_INFO_pHYs
;
291 if (unit_type
!= NULL
)
293 *unit_type
= (int)info_ptr
->phys_unit_type
;
294 retval
|= PNG_INFO_pHYs
;
297 if (res_x
!= NULL
) *res_x
= (png_uint_32
)(*res_x
* 39.37 + .50);
298 if (res_y
!= NULL
) *res_y
= (png_uint_32
)(*res_y
* 39.37 + .50);
304 #endif /* PNG_READ_pHYs_SUPPORTED */
305 #endif /* PNG_INCH_CONVERSIONS */
307 /* png_get_channels really belongs in here, too, but it's been around longer */
309 #endif /* PNG_EASY_ACCESS_SUPPORTED */
312 png_get_channels(png_structp png_ptr
, png_infop info_ptr
)
314 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
315 return(info_ptr
->channels
);
321 png_get_signature(png_structp png_ptr
, png_infop info_ptr
)
323 if (png_ptr
!= NULL
&& info_ptr
!= NULL
)
324 return(info_ptr
->signature
);
329 #if defined(PNG_READ_bKGD_SUPPORTED)
331 png_get_bKGD(png_structp png_ptr
, png_infop info_ptr
,
332 png_color_16p
*background
)
334 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_bKGD
)
335 && background
!= NULL
)
337 png_debug1(1, "in %s retrieval function\n", "bKGD");
338 *background
= &(info_ptr
->background
);
339 return (PNG_INFO_bKGD
);
345 #if defined(PNG_READ_cHRM_SUPPORTED)
347 png_get_cHRM(png_structp png_ptr
, png_infop info_ptr
,
348 double *white_x
, double *white_y
, double *red_x
, double *red_y
,
349 double *green_x
, double *green_y
, double *blue_x
, double *blue_y
)
351 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_cHRM
))
353 png_debug1(1, "in %s retrieval function\n", "cHRM");
355 *white_x
= (double)info_ptr
->x_white
;
357 *white_y
= (double)info_ptr
->y_white
;
359 *red_x
= (double)info_ptr
->x_red
;
361 *red_y
= (double)info_ptr
->y_red
;
363 *green_x
= (double)info_ptr
->x_green
;
365 *green_y
= (double)info_ptr
->y_green
;
367 *blue_x
= (double)info_ptr
->x_blue
;
369 *blue_y
= (double)info_ptr
->y_blue
;
370 return (PNG_INFO_cHRM
);
376 #if defined(PNG_READ_gAMA_SUPPORTED)
378 png_get_gAMA(png_structp png_ptr
, png_infop info_ptr
, double *file_gamma
)
380 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_gAMA
)
381 && file_gamma
!= NULL
)
383 png_debug1(1, "in %s retrieval function\n", "gAMA");
384 *file_gamma
= (double)info_ptr
->gamma
;
385 return (PNG_INFO_gAMA
);
391 #if defined(PNG_READ_sRGB_SUPPORTED)
393 png_get_sRGB(png_structp png_ptr
, png_infop info_ptr
, int *file_srgb_intent
)
395 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_sRGB
)
396 && file_srgb_intent
!= NULL
)
398 png_debug1(1, "in %s retrieval function\n", "sRGB");
399 *file_srgb_intent
= (int)info_ptr
->srgb_intent
;
400 return (PNG_INFO_sRGB
);
406 #if defined(PNG_READ_hIST_SUPPORTED)
408 png_get_hIST(png_structp png_ptr
, png_infop info_ptr
, png_uint_16p
*hist
)
410 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_hIST
)
413 png_debug1(1, "in %s retrieval function\n", "hIST");
414 *hist
= info_ptr
->hist
;
415 return (PNG_INFO_hIST
);
422 png_get_IHDR(png_structp png_ptr
, png_infop info_ptr
,
423 png_uint_32
*width
, png_uint_32
*height
, int *bit_depth
,
424 int *color_type
, int *interlace_type
, int *compression_type
,
428 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& width
!= NULL
&& height
!= NULL
&&
429 bit_depth
!= NULL
&& color_type
!= NULL
)
431 int pixel_depth
, channels
;
432 png_uint_32 rowbytes_per_pixel
;
434 png_debug1(1, "in %s retrieval function\n", "IHDR");
435 *width
= info_ptr
->width
;
436 *height
= info_ptr
->height
;
437 *bit_depth
= info_ptr
->bit_depth
;
438 *color_type
= info_ptr
->color_type
;
439 if (compression_type
!= NULL
)
440 *compression_type
= info_ptr
->compression_type
;
441 if (filter_type
!= NULL
)
442 *filter_type
= info_ptr
->filter_type
;
443 if (interlace_type
!= NULL
)
444 *interlace_type
= info_ptr
->interlace_type
;
446 /* check for potential overflow of rowbytes */
447 if (*color_type
== PNG_COLOR_TYPE_PALETTE
)
449 else if (*color_type
& PNG_COLOR_MASK_COLOR
)
453 if (*color_type
& PNG_COLOR_MASK_ALPHA
)
455 pixel_depth
= *bit_depth
* channels
;
456 rowbytes_per_pixel
= (pixel_depth
+ 7) >> 3;
457 if ((*width
> (png_uint_32
)2147483647L/rowbytes_per_pixel
))
460 "Width too large for libpng to process image data.");
467 #if defined(PNG_READ_oFFs_SUPPORTED)
469 png_get_oFFs(png_structp png_ptr
, png_infop info_ptr
,
470 png_uint_32
*offset_x
, png_uint_32
*offset_y
, int *unit_type
)
472 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& (info_ptr
->valid
& PNG_INFO_oFFs
)
473 && offset_x
!= NULL
&& offset_y
!= NULL
&& unit_type
!= NULL
)
475 png_debug1(1, "in %s retrieval function\n", "oFFs");
476 *offset_x
= info_ptr
->x_offset
;
477 *offset_y
= info_ptr
->y_offset
;
478 *unit_type
= (int)info_ptr
->offset_unit_type
;
479 return (PNG_INFO_oFFs
);
485 #if defined(PNG_READ_pCAL_SUPPORTED)
487 png_get_pCAL(png_structp png_ptr
, png_infop info_ptr
,
488 png_charp
*purpose
, png_int_32
*X0
, png_int_32
*X1
, int *type
, int *nparams
,
489 png_charp
*units
, png_charpp
*params
)
491 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_pCAL
&&
492 purpose
!= NULL
&& X0
!= NULL
&& X1
!= NULL
&& type
!= NULL
&&
493 nparams
!= NULL
&& units
!= NULL
&& params
!= NULL
)
495 png_debug1(1, "in %s retrieval function\n", "pCAL");
496 *purpose
= info_ptr
->pcal_purpose
;
497 *X0
= info_ptr
->pcal_X0
;
498 *X1
= info_ptr
->pcal_X1
;
499 *type
= (int)info_ptr
->pcal_type
;
500 *nparams
= (int)info_ptr
->pcal_nparams
;
501 *units
= info_ptr
->pcal_units
;
502 *params
= info_ptr
->pcal_params
;
503 return (PNG_INFO_pCAL
);
509 #if defined(PNG_READ_pHYs_SUPPORTED)
511 png_get_pHYs(png_structp png_ptr
, png_infop info_ptr
,
512 png_uint_32
*res_x
, png_uint_32
*res_y
, int *unit_type
)
514 png_uint_32 retval
= 0;
516 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_pHYs
)
518 png_debug1(1, "in %s retrieval function\n", "pHYs");
521 *res_x
= info_ptr
->x_pixels_per_unit
;
522 retval
|= PNG_INFO_pHYs
;
526 *res_y
= info_ptr
->y_pixels_per_unit
;
527 retval
|= PNG_INFO_pHYs
;
529 if (unit_type
!= NULL
)
531 *unit_type
= (int)info_ptr
->phys_unit_type
;
532 retval
|= PNG_INFO_pHYs
;
540 png_get_PLTE(png_structp png_ptr
, png_infop info_ptr
, png_colorp
*palette
,
543 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_PLTE
&&
546 png_debug1(1, "in %s retrieval function\n", "PLTE");
547 *palette
= info_ptr
->palette
;
548 *num_palette
= info_ptr
->num_palette
;
549 png_debug1(3, "num_palette = %d\n", *num_palette
);
550 return (PNG_INFO_PLTE
);
555 #if defined(PNG_READ_sBIT_SUPPORTED)
557 png_get_sBIT(png_structp png_ptr
, png_infop info_ptr
, png_color_8p
*sig_bit
)
559 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_sBIT
&&
562 png_debug1(1, "in %s retrieval function\n", "sBIT");
563 *sig_bit
= &(info_ptr
->sig_bit
);
564 return (PNG_INFO_sBIT
);
570 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
572 png_get_text(png_structp png_ptr
, png_infop info_ptr
, png_textp
*text_ptr
,
575 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->num_text
> 0)
577 png_debug1(1, "in %s retrieval function\n",
578 (png_ptr
->chunk_name
[0] == '\0' ? "text"
579 : (png_const_charp
)png_ptr
->chunk_name
));
580 if (text_ptr
!= NULL
)
581 *text_ptr
= info_ptr
->text
;
582 if (num_text
!= NULL
)
583 *num_text
= info_ptr
->num_text
;
584 return ((png_uint_32
)info_ptr
->num_text
);
590 #if defined(PNG_READ_tIME_SUPPORTED)
592 png_get_tIME(png_structp png_ptr
, png_infop info_ptr
, png_timep
*mod_time
)
594 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_tIME
&&
597 png_debug1(1, "in %s retrieval function\n", "tIME");
598 *mod_time
= &(info_ptr
->mod_time
);
599 return (PNG_INFO_tIME
);
605 #if defined(PNG_READ_tRNS_SUPPORTED)
607 png_get_tRNS(png_structp png_ptr
, png_infop info_ptr
,
608 png_bytep
*trans
, int *num_trans
, png_color_16p
*trans_values
)
610 png_uint_32 retval
= 0;
611 if (png_ptr
!= NULL
&& info_ptr
!= NULL
&& info_ptr
->valid
& PNG_INFO_tRNS
)
613 png_debug1(1, "in %s retrieval function\n", "tRNS");
614 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
618 *trans
= info_ptr
->trans
;
619 retval
|= PNG_INFO_tRNS
;
621 if (trans_values
!= NULL
)
622 *trans_values
= &(info_ptr
->trans_values
);
624 else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
626 if (trans_values
!= NULL
)
628 *trans_values
= &(info_ptr
->trans_values
);
629 retval
|= PNG_INFO_tRNS
;
634 if(num_trans
!= NULL
)
636 *num_trans
= info_ptr
->num_trans
;
637 retval
|= PNG_INFO_tRNS
;
644 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
646 png_get_rgb_to_gray_status (png_structp png_ptr
)
648 return png_ptr
->rgb_to_gray_status
;