pngget.c 30.7 KB
Newer Older
wester committed
1 2 3

/* pngget.c - retrieval of values from info struct
 *
wester committed
4 5
 * Last changed in libpng 1.5.19 [August 21, 2014]
 * Copyright (c) 1998-2002,2004,2006-2014 Glenn Randers-Pehrson
wester committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
 *
 * This code is released under the libpng license.
 * For conditions of distribution and use, see the disclaimer
 * and license in png.h
 *
 */

#include "pngpriv.h"

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)

png_uint_32 PNGAPI
wester committed
20
png_get_valid(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
21 22 23 24 25 26 27 28 29
    png_uint_32 flag)
{
   if (png_ptr != NULL && info_ptr != NULL)
      return(info_ptr->valid & flag);

   return(0);
}

png_size_t PNGAPI
wester committed
30
png_get_rowbytes(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
31 32 33 34 35 36 37 38 39
{
   if (png_ptr != NULL && info_ptr != NULL)
      return(info_ptr->rowbytes);

   return(0);
}

#ifdef PNG_INFO_IMAGE_SUPPORTED
png_bytepp PNGAPI
wester committed
40
png_get_rows(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
41 42 43 44 45 46 47 48 49 50 51
{
   if (png_ptr != NULL && info_ptr != NULL)
      return(info_ptr->row_pointers);

   return(0);
}
#endif

#ifdef PNG_EASY_ACCESS_SUPPORTED
/* Easy access to info, added in libpng-0.99 */
png_uint_32 PNGAPI
wester committed
52
png_get_image_width(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
53 54 55 56 57 58 59 60
{
   if (png_ptr != NULL && info_ptr != NULL)
      return info_ptr->width;

   return (0);
}

png_uint_32 PNGAPI
wester committed
61
png_get_image_height(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
62 63 64 65 66 67 68 69
{
   if (png_ptr != NULL && info_ptr != NULL)
      return info_ptr->height;

   return (0);
}

png_byte PNGAPI
wester committed
70
png_get_bit_depth(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
71 72 73 74 75 76 77 78
{
   if (png_ptr != NULL && info_ptr != NULL)
      return info_ptr->bit_depth;

   return (0);
}

png_byte PNGAPI
wester committed
79
png_get_color_type(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
80 81 82 83 84 85 86 87
{
   if (png_ptr != NULL && info_ptr != NULL)
      return info_ptr->color_type;

   return (0);
}

png_byte PNGAPI
wester committed
88
png_get_filter_type(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
89 90 91 92 93 94 95 96
{
   if (png_ptr != NULL && info_ptr != NULL)
      return info_ptr->filter_type;

   return (0);
}

png_byte PNGAPI
wester committed
97
png_get_interlace_type(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
98 99 100 101 102 103 104 105
{
   if (png_ptr != NULL && info_ptr != NULL)
      return info_ptr->interlace_type;

   return (0);
}

png_byte PNGAPI
wester committed
106
png_get_compression_type(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
107 108 109 110 111 112 113 114
{
   if (png_ptr != NULL && info_ptr != NULL)
      return info_ptr->compression_type;

   return (0);
}

png_uint_32 PNGAPI
wester committed
115
png_get_x_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
116 117
{
#ifdef PNG_pHYs_SUPPORTED
wester committed
118
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
wester committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      {
         png_debug1(1, "in %s retrieval function",
             "png_get_x_pixels_per_meter");

         if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER)
            return (info_ptr->x_pixels_per_unit);
      }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return (0);
}

png_uint_32 PNGAPI
wester committed
135
png_get_y_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
136 137
{
#ifdef PNG_pHYs_SUPPORTED
wester committed
138
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
wester committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
   {
      png_debug1(1, "in %s retrieval function",
          "png_get_y_pixels_per_meter");

      if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER)
         return (info_ptr->y_pixels_per_unit);
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return (0);
}

png_uint_32 PNGAPI
wester committed
155
png_get_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
156 157
{
#ifdef PNG_pHYs_SUPPORTED
wester committed
158
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
wester committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
   {
      png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter");

      if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER &&
          info_ptr->x_pixels_per_unit == info_ptr->y_pixels_per_unit)
         return (info_ptr->x_pixels_per_unit);
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return (0);
}

#ifdef PNG_FLOATING_POINT_SUPPORTED
float PNGAPI
wester committed
176
png_get_pixel_aspect_ratio(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
177 178
{
#ifdef PNG_READ_pHYs_SUPPORTED
wester committed
179
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
wester committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
   {
      png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio");

      if (info_ptr->x_pixels_per_unit != 0)
         return ((float)((float)info_ptr->y_pixels_per_unit
             /(float)info_ptr->x_pixels_per_unit));
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return ((float)0.0);
}
#endif

#ifdef PNG_FIXED_POINT_SUPPORTED
png_fixed_point PNGAPI
wester committed
198 199
png_get_pixel_aspect_ratio_fixed(png_const_structp png_ptr,
    png_const_infop info_ptr)
wester committed
200 201
{
#ifdef PNG_READ_pHYs_SUPPORTED
wester committed
202 203 204 205
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)
       && info_ptr->x_pixels_per_unit > 0 && info_ptr->y_pixels_per_unit > 0
       && info_ptr->x_pixels_per_unit <= PNG_UINT_31_MAX
       && info_ptr->y_pixels_per_unit <= PNG_UINT_31_MAX)
wester committed
206 207 208 209 210 211 212 213 214
   {
      png_fixed_point res;

      png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio_fixed");

      /* The following casts work because a PNG 4 byte integer only has a valid
       * range of 0..2^31-1; otherwise the cast might overflow.
       */
      if (png_muldiv(&res, (png_int_32)info_ptr->y_pixels_per_unit, PNG_FP_1,
wester committed
215
          (png_int_32)info_ptr->x_pixels_per_unit))
wester committed
216 217 218 219 220 221 222 223 224 225 226 227
         return res;
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return 0;
}
#endif

png_int_32 PNGAPI
wester committed
228
png_get_x_offset_microns(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
229 230
{
#ifdef PNG_oFFs_SUPPORTED
wester committed
231
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
wester committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
   {
      png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns");

      if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER)
         return (info_ptr->x_offset);
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return (0);
}

png_int_32 PNGAPI
wester committed
247
png_get_y_offset_microns(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
248 249
{
#ifdef PNG_oFFs_SUPPORTED
wester committed
250
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
wester committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
   {
      png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns");

      if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER)
         return (info_ptr->y_offset);
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return (0);
}

png_int_32 PNGAPI
wester committed
266
png_get_x_offset_pixels(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
267 268
{
#ifdef PNG_oFFs_SUPPORTED
wester committed
269
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
wester committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
   {
      png_debug1(1, "in %s retrieval function", "png_get_x_offset_pixels");

      if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL)
         return (info_ptr->x_offset);
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return (0);
}

png_int_32 PNGAPI
wester committed
285
png_get_y_offset_pixels(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
286 287
{
#ifdef PNG_oFFs_SUPPORTED
wester committed
288
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
wester committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
   {
      png_debug1(1, "in %s retrieval function", "png_get_y_offset_pixels");

      if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL)
         return (info_ptr->y_offset);
   }
#else
   PNG_UNUSED(png_ptr)
   PNG_UNUSED(info_ptr)
#endif

   return (0);
}

#ifdef PNG_INCH_CONVERSIONS_SUPPORTED
static png_uint_32
ppi_from_ppm(png_uint_32 ppm)
{
#if 0
   /* The conversion is *(2.54/100), in binary (32 digits):
    * .00000110100000001001110101001001
    */
   png_uint_32 t1001, t1101;
   ppm >>= 1;                  /* .1 */
   t1001 = ppm + (ppm >> 3);   /* .1001 */
   t1101 = t1001 + (ppm >> 1); /* .1101 */
   ppm >>= 20;                 /* .000000000000000000001 */
   t1101 += t1101 >> 15;       /* .1101000000000001101 */
   t1001 >>= 11;               /* .000000000001001 */
   t1001 += t1001 >> 12;       /* .000000000001001000000001001 */
   ppm += t1001;               /* .000000000001001000001001001 */
   ppm += t1101;               /* .110100000001001110101001001 */
   return (ppm + 16) >> 5;/* .00000110100000001001110101001001 */
#else
   /* The argument is a PNG unsigned integer, so it is not permitted
    * to be bigger than 2^31.
    */
   png_fixed_point result;
   if (ppm <= PNG_UINT_31_MAX && png_muldiv(&result, (png_int_32)ppm, 127,
wester committed
328
       5000))
wester committed
329 330 331 332 333 334 335 336
      return result;

   /* Overflow. */
   return 0;
#endif
}

png_uint_32 PNGAPI
wester committed
337
png_get_pixels_per_inch(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
338 339 340 341 342
{
   return ppi_from_ppm(png_get_pixels_per_meter(png_ptr, info_ptr));
}

png_uint_32 PNGAPI
wester committed
343
png_get_x_pixels_per_inch(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
344 345 346 347 348
{
   return ppi_from_ppm(png_get_x_pixels_per_meter(png_ptr, info_ptr));
}

png_uint_32 PNGAPI
wester committed
349
png_get_y_pixels_per_inch(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
350 351 352 353 354 355
{
   return ppi_from_ppm(png_get_y_pixels_per_meter(png_ptr, info_ptr));
}

#ifdef PNG_FIXED_POINT_SUPPORTED
static png_fixed_point
wester committed
356
png_fixed_inches_from_microns(png_structp png_ptr, png_int_32 microns)
wester committed
357 358 359 360 361 362 363 364 365 366
{
   /* Convert from metres * 1,000,000 to inches * 100,000, meters to
    * inches is simply *(100/2.54), so we want *(10/2.54) == 500/127.
    * Notice that this can overflow - a warning is output and 0 is
    * returned.
    */
   return png_muldiv_warn(png_ptr, microns, 500, 127);
}

png_fixed_point PNGAPI
wester committed
367 368
png_get_x_offset_inches_fixed(png_structp png_ptr,
    png_const_infop info_ptr)
wester committed
369 370 371 372 373 374 375 376
{
   return png_fixed_inches_from_microns(png_ptr,
       png_get_x_offset_microns(png_ptr, info_ptr));
}
#endif

#ifdef PNG_FIXED_POINT_SUPPORTED
png_fixed_point PNGAPI
wester committed
377 378
png_get_y_offset_inches_fixed(png_structp png_ptr,
    png_const_infop info_ptr)
wester committed
379 380 381 382 383 384 385 386
{
   return png_fixed_inches_from_microns(png_ptr,
       png_get_y_offset_microns(png_ptr, info_ptr));
}
#endif

#ifdef PNG_FLOATING_POINT_SUPPORTED
float PNGAPI
wester committed
387
png_get_x_offset_inches(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
388 389 390 391 392 393 394 395 396 397
{
   /* To avoid the overflow do the conversion directly in floating
    * point.
    */
   return (float)(png_get_x_offset_microns(png_ptr, info_ptr) * .00003937);
}
#endif

#ifdef PNG_FLOATING_POINT_SUPPORTED
float PNGAPI
wester committed
398
png_get_y_offset_inches(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
399 400 401 402 403 404 405 406 407 408
{
   /* To avoid the overflow do the conversion directly in floating
    * point.
    */
   return (float)(png_get_y_offset_microns(png_ptr, info_ptr) * .00003937);
}
#endif

#ifdef PNG_pHYs_SUPPORTED
png_uint_32 PNGAPI
wester committed
409
png_get_pHYs_dpi(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
410 411 412 413
    png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
{
   png_uint_32 retval = 0;

wester committed
414
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
wester committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
   {
      png_debug1(1, "in %s retrieval function", "pHYs");

      if (res_x != NULL)
      {
         *res_x = info_ptr->x_pixels_per_unit;
         retval |= PNG_INFO_pHYs;
      }

      if (res_y != NULL)
      {
         *res_y = info_ptr->y_pixels_per_unit;
         retval |= PNG_INFO_pHYs;
      }

      if (unit_type != NULL)
      {
         *unit_type = (int)info_ptr->phys_unit_type;
         retval |= PNG_INFO_pHYs;

         if (*unit_type == 1)
         {
            if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);
            if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);
         }
      }
   }

   return (retval);
}
wester committed
445 446
#endif /* PNG_pHYs_SUPPORTED */
#endif  /* PNG_INCH_CONVERSIONS_SUPPORTED */
wester committed
447 448 449

/* png_get_channels really belongs in here, too, but it's been around longer */

wester committed
450
#endif  /* PNG_EASY_ACCESS_SUPPORTED */
wester committed
451 452

png_byte PNGAPI
wester committed
453
png_get_channels(png_const_structp png_ptr, png_const_infop info_ptr)
wester committed
454 455 456 457 458 459 460 461
{
   if (png_ptr != NULL && info_ptr != NULL)
      return(info_ptr->channels);

   return (0);
}

png_const_bytep PNGAPI
wester committed
462
png_get_signature(png_const_structp png_ptr, png_infop info_ptr)
wester committed
463 464 465 466 467 468 469 470 471
{
   if (png_ptr != NULL && info_ptr != NULL)
      return(info_ptr->signature);

   return (NULL);
}

#ifdef PNG_bKGD_SUPPORTED
png_uint_32 PNGAPI
wester committed
472
png_get_bKGD(png_const_structp png_ptr, png_infop info_ptr,
a  
Kai Westerkamp committed
473
   png_color_16p *background)
wester committed
474
{
wester committed
475 476
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
       && background != NULL)
wester committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
   {
      png_debug1(1, "in %s retrieval function", "bKGD");

      *background = &(info_ptr->background);
      return (PNG_INFO_bKGD);
   }

   return (0);
}
#endif

#ifdef PNG_cHRM_SUPPORTED
/* The XYZ APIs were added in 1.5.5 to take advantage of the code added at the
 * same time to correct the rgb grayscale coefficient defaults obtained from the
 * cHRM chunk in 1.5.4
 */
wester committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
png_uint_32 PNGFAPI
png_get_cHRM_XYZ_fixed(png_structp png_ptr, png_const_infop info_ptr,
    png_fixed_point *int_red_X, png_fixed_point *int_red_Y,
    png_fixed_point *int_red_Z, png_fixed_point *int_green_X,
    png_fixed_point *int_green_Y, png_fixed_point *int_green_Z,
    png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y,
    png_fixed_point *int_blue_Z)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
   {
      png_xy xy;
      png_XYZ XYZ;

      png_debug1(1, "in %s retrieval function", "cHRM_XYZ");

      xy.whitex = info_ptr->x_white;
      xy.whitey = info_ptr->y_white;
      xy.redx = info_ptr->x_red;
      xy.redy = info_ptr->y_red;
      xy.greenx = info_ptr->x_green;
      xy.greeny = info_ptr->y_green;
      xy.bluex = info_ptr->x_blue;
      xy.bluey = info_ptr->y_blue;

      /* The *_checked function handles error reporting, so just return 0 if
       * there is a failure here.
       */
      if (png_XYZ_from_xy_checked(png_ptr, &XYZ, xy))
      {
         if (int_red_X != NULL)
            *int_red_X = XYZ.redX;
         if (int_red_Y != NULL)
            *int_red_Y = XYZ.redY;
         if (int_red_Z != NULL)
            *int_red_Z = XYZ.redZ;
         if (int_green_X != NULL)
            *int_green_X = XYZ.greenX;
         if (int_green_Y != NULL)
            *int_green_Y = XYZ.greenY;
         if (int_green_Z != NULL)
            *int_green_Z = XYZ.greenZ;
         if (int_blue_X != NULL)
            *int_blue_X = XYZ.blueX;
         if (int_blue_Y != NULL)
            *int_blue_Y = XYZ.blueY;
         if (int_blue_Z != NULL)
            *int_blue_Z = XYZ.blueZ;

         return (PNG_INFO_cHRM);
      }
   }

   return (0);
}

wester committed
548 549
#  ifdef PNG_FLOATING_POINT_SUPPORTED
png_uint_32 PNGAPI
wester committed
550
png_get_cHRM(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
551 552 553
    double *white_x, double *white_y, double *red_x, double *red_y,
    double *green_x, double *green_y, double *blue_x, double *blue_y)
{
wester committed
554
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
wester committed
555 556 557 558
   {
      png_debug1(1, "in %s retrieval function", "cHRM");

      if (white_x != NULL)
wester committed
559
         *white_x = png_float(png_ptr, info_ptr->x_white, "cHRM white X");
wester committed
560
      if (white_y != NULL)
wester committed
561
         *white_y = png_float(png_ptr, info_ptr->y_white, "cHRM white Y");
wester committed
562
      if (red_x != NULL)
wester committed
563
         *red_x = png_float(png_ptr, info_ptr->x_red, "cHRM red X");
wester committed
564
      if (red_y != NULL)
wester committed
565
         *red_y = png_float(png_ptr, info_ptr->y_red, "cHRM red Y");
wester committed
566
      if (green_x != NULL)
wester committed
567
         *green_x = png_float(png_ptr, info_ptr->x_green, "cHRM green X");
wester committed
568
      if (green_y != NULL)
wester committed
569
         *green_y = png_float(png_ptr, info_ptr->y_green, "cHRM green Y");
wester committed
570
      if (blue_x != NULL)
wester committed
571
         *blue_x = png_float(png_ptr, info_ptr->x_blue, "cHRM blue X");
wester committed
572
      if (blue_y != NULL)
wester committed
573
         *blue_y = png_float(png_ptr, info_ptr->y_blue, "cHRM blue Y");
wester committed
574 575 576 577 578 579 580
      return (PNG_INFO_cHRM);
   }

   return (0);
}

png_uint_32 PNGAPI
wester committed
581
png_get_cHRM_XYZ(png_structp png_ptr, png_const_infop info_ptr,
a  
Kai Westerkamp committed
582 583 584
   double *red_X, double *red_Y, double *red_Z, double *green_X,
   double *green_Y, double *green_Z, double *blue_X, double *blue_Y,
   double *blue_Z)
wester committed
585
{
wester committed
586
   png_XYZ XYZ;
wester committed
587

wester committed
588 589 590 591
   if (png_get_cHRM_XYZ_fixed(png_ptr, info_ptr,
      &XYZ.redX, &XYZ.redY, &XYZ.redZ, &XYZ.greenX, &XYZ.greenY, &XYZ.greenZ,
      &XYZ.blueX, &XYZ.blueY, &XYZ.blueZ) & PNG_INFO_cHRM)
   {
wester committed
592
      if (red_X != NULL)
wester committed
593
         *red_X = png_float(png_ptr, XYZ.redX, "cHRM red X");
wester committed
594
      if (red_Y != NULL)
wester committed
595
         *red_Y = png_float(png_ptr, XYZ.redY, "cHRM red Y");
wester committed
596
      if (red_Z != NULL)
wester committed
597
         *red_Z = png_float(png_ptr, XYZ.redZ, "cHRM red Z");
wester committed
598
      if (green_X != NULL)
wester committed
599
         *green_X = png_float(png_ptr, XYZ.greenX, "cHRM green X");
wester committed
600
      if (green_Y != NULL)
wester committed
601
         *green_Y = png_float(png_ptr, XYZ.greenY, "cHRM green Y");
wester committed
602
      if (green_Z != NULL)
wester committed
603
         *green_Z = png_float(png_ptr, XYZ.greenZ, "cHRM green Z");
wester committed
604
      if (blue_X != NULL)
wester committed
605
         *blue_X = png_float(png_ptr, XYZ.blueX, "cHRM blue X");
wester committed
606
      if (blue_Y != NULL)
wester committed
607
         *blue_Y = png_float(png_ptr, XYZ.blueY, "cHRM blue Y");
wester committed
608
      if (blue_Z != NULL)
wester committed
609
         *blue_Z = png_float(png_ptr, XYZ.blueZ, "cHRM blue Z");
wester committed
610 611 612 613 614 615 616 617 618
      return (PNG_INFO_cHRM);
   }

   return (0);
}
#  endif

#  ifdef PNG_FIXED_POINT_SUPPORTED
png_uint_32 PNGAPI
wester committed
619
png_get_cHRM_fixed(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
620 621 622 623 624 625
    png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,
    png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,
    png_fixed_point *blue_x, png_fixed_point *blue_y)
{
   png_debug1(1, "in %s retrieval function", "cHRM");

wester committed
626
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
wester committed
627 628
   {
      if (white_x != NULL)
wester committed
629
         *white_x = info_ptr->x_white;
wester committed
630
      if (white_y != NULL)
wester committed
631
         *white_y = info_ptr->y_white;
wester committed
632
      if (red_x != NULL)
wester committed
633
         *red_x = info_ptr->x_red;
wester committed
634
      if (red_y != NULL)
wester committed
635
         *red_y = info_ptr->y_red;
wester committed
636
      if (green_x != NULL)
wester committed
637
         *green_x = info_ptr->x_green;
wester committed
638
      if (green_y != NULL)
wester committed
639
         *green_y = info_ptr->y_green;
wester committed
640
      if (blue_x != NULL)
wester committed
641
         *blue_x = info_ptr->x_blue;
wester committed
642
      if (blue_y != NULL)
wester committed
643
         *blue_y = info_ptr->y_blue;
wester committed
644 645 646 647 648 649 650 651 652
      return (PNG_INFO_cHRM);
   }

   return (0);
}
#  endif
#endif

#ifdef PNG_gAMA_SUPPORTED
wester committed
653 654
png_uint_32 PNGFAPI
png_get_gAMA_fixed(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
655 656 657 658
    png_fixed_point *file_gamma)
{
   png_debug1(1, "in %s retrieval function", "gAMA");

wester committed
659 660
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
       && file_gamma != NULL)
wester committed
661
   {
wester committed
662
      *file_gamma = info_ptr->gamma;
wester committed
663 664 665 666 667 668 669
      return (PNG_INFO_gAMA);
   }

   return (0);
}
#  ifdef PNG_FLOATING_POINT_SUPPORTED
png_uint_32 PNGAPI
wester committed
670
png_get_gAMA(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
671 672
    double *file_gamma)
{
wester committed
673 674
   png_fixed_point igamma;
   png_uint_32 ok = png_get_gAMA_fixed(png_ptr, info_ptr, &igamma);
wester committed
675

wester committed
676 677
   if (ok != 0)
      *file_gamma = png_float(png_ptr, igamma, "png_get_gAMA");
wester committed
678

wester committed
679
   return ok;
wester committed
680
}
wester committed
681

wester committed
682 683 684 685 686
#  endif
#endif

#ifdef PNG_sRGB_SUPPORTED
png_uint_32 PNGAPI
wester committed
687
png_get_sRGB(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
688 689 690 691
    int *file_srgb_intent)
{
   png_debug1(1, "in %s retrieval function", "sRGB");

wester committed
692 693
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
       && file_srgb_intent != NULL)
wester committed
694
   {
wester committed
695
      *file_srgb_intent = (int)info_ptr->srgb_intent;
wester committed
696 697 698 699 700 701 702 703 704
      return (PNG_INFO_sRGB);
   }

   return (0);
}
#endif

#ifdef PNG_iCCP_SUPPORTED
png_uint_32 PNGAPI
wester committed
705
png_get_iCCP(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
706 707 708 709 710
    png_charpp name, int *compression_type,
    png_bytepp profile, png_uint_32 *proflen)
{
   png_debug1(1, "in %s retrieval function", "iCCP");

wester committed
711 712 713
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)
       && name != NULL && compression_type != NULL && profile != NULL &&
		 proflen != NULL)
wester committed
714 715 716
   {
      *name = info_ptr->iccp_name;
      *profile = info_ptr->iccp_profile;
wester committed
717 718
      /* Compression_type is a dummy so the API won't have to change
       * if we introduce multiple compression types later.
wester committed
719
       */
wester committed
720 721
      *proflen = info_ptr->iccp_proflen;
      *compression_type = info_ptr->iccp_compression;
wester committed
722 723 724 725 726 727 728 729
      return (PNG_INFO_iCCP);
   }

   return (0);
}
#endif

#ifdef PNG_sPLT_SUPPORTED
wester committed
730 731
png_uint_32 PNGAPI
png_get_sPLT(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
732 733 734 735 736
    png_sPLT_tpp spalettes)
{
   if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)
   {
      *spalettes = info_ptr->splt_palettes;
wester committed
737
      return ((png_uint_32)info_ptr->splt_palettes_num);
wester committed
738 739 740 741 742 743 744 745
   }

   return (0);
}
#endif

#ifdef PNG_hIST_SUPPORTED
png_uint_32 PNGAPI
wester committed
746
png_get_hIST(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
747 748 749 750
    png_uint_16p *hist)
{
   png_debug1(1, "in %s retrieval function", "hIST");

wester committed
751 752
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
       && hist != NULL)
wester committed
753 754 755 756 757 758 759 760 761 762
   {
      *hist = info_ptr->hist;
      return (PNG_INFO_hIST);
   }

   return (0);
}
#endif

png_uint_32 PNGAPI
wester committed
763
png_get_IHDR(png_structp png_ptr, png_infop info_ptr,
wester committed
764 765 766
    png_uint_32 *width, png_uint_32 *height, int *bit_depth,
    int *color_type, int *interlace_type, int *compression_type,
    int *filter_type)
wester committed
767

wester committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
{
   png_debug1(1, "in %s retrieval function", "IHDR");

   if (png_ptr == NULL || info_ptr == NULL)
      return (0);

   if (width != NULL)
       *width = info_ptr->width;

   if (height != NULL)
       *height = info_ptr->height;

   if (bit_depth != NULL)
       *bit_depth = info_ptr->bit_depth;

   if (color_type != NULL)
       *color_type = info_ptr->color_type;

   if (compression_type != NULL)
      *compression_type = info_ptr->compression_type;

   if (filter_type != NULL)
      *filter_type = info_ptr->filter_type;

   if (interlace_type != NULL)
      *interlace_type = info_ptr->interlace_type;

   /* This is redundant if we can be sure that the info_ptr values were all
    * assigned in png_set_IHDR().  We do the check anyhow in case an
    * application has ignored our advice not to mess with the members
    * of info_ptr directly.
    */
wester committed
800
   png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
wester committed
801 802 803 804 805 806 807 808
       info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
       info_ptr->compression_type, info_ptr->filter_type);

   return (1);
}

#ifdef PNG_oFFs_SUPPORTED
png_uint_32 PNGAPI
wester committed
809
png_get_oFFs(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
810 811 812 813
    png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)
{
   png_debug1(1, "in %s retrieval function", "oFFs");

wester committed
814 815
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
       && offset_x != NULL && offset_y != NULL && unit_type != NULL)
wester committed
816 817 818 819 820 821 822 823 824 825 826 827 828
   {
      *offset_x = info_ptr->x_offset;
      *offset_y = info_ptr->y_offset;
      *unit_type = (int)info_ptr->offset_unit_type;
      return (PNG_INFO_oFFs);
   }

   return (0);
}
#endif

#ifdef PNG_pCAL_SUPPORTED
png_uint_32 PNGAPI
wester committed
829
png_get_pCAL(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
830 831 832 833 834
    png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
    png_charp *units, png_charpp *params)
{
   png_debug1(1, "in %s retrieval function", "pCAL");

wester committed
835 836
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)
       && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
wester committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
       nparams != NULL && units != NULL && params != NULL)
   {
      *purpose = info_ptr->pcal_purpose;
      *X0 = info_ptr->pcal_X0;
      *X1 = info_ptr->pcal_X1;
      *type = (int)info_ptr->pcal_type;
      *nparams = (int)info_ptr->pcal_nparams;
      *units = info_ptr->pcal_units;
      *params = info_ptr->pcal_params;
      return (PNG_INFO_pCAL);
   }

   return (0);
}
#endif

#ifdef PNG_sCAL_SUPPORTED
#  ifdef PNG_FIXED_POINT_SUPPORTED
wester committed
855
#    ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
wester committed
856
png_uint_32 PNGAPI
wester committed
857
png_get_sCAL_fixed(png_structp png_ptr, png_const_infop info_ptr,
wester committed
858 859 860
    int *unit, png_fixed_point *width, png_fixed_point *height)
{
   if (png_ptr != NULL && info_ptr != NULL &&
wester committed
861
       (info_ptr->valid & PNG_INFO_sCAL))
wester committed
862 863
   {
      *unit = info_ptr->scal_unit;
wester committed
864
      /*TODO: make this work without FP support */
wester committed
865 866
      *width = png_fixed(png_ptr, atof(info_ptr->scal_s_width), "sCAL width");
      *height = png_fixed(png_ptr, atof(info_ptr->scal_s_height),
a  
Kai Westerkamp committed
867
         "sCAL height");
wester committed
868 869 870 871 872 873 874 875 876
      return (PNG_INFO_sCAL);
   }

   return(0);
}
#    endif /* FLOATING_ARITHMETIC */
#  endif /* FIXED_POINT */
#  ifdef PNG_FLOATING_POINT_SUPPORTED
png_uint_32 PNGAPI
wester committed
877
png_get_sCAL(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
878 879 880
    int *unit, double *width, double *height)
{
   if (png_ptr != NULL && info_ptr != NULL &&
wester committed
881
       (info_ptr->valid & PNG_INFO_sCAL))
wester committed
882 883 884 885 886 887 888 889 890 891 892
   {
      *unit = info_ptr->scal_unit;
      *width = atof(info_ptr->scal_s_width);
      *height = atof(info_ptr->scal_s_height);
      return (PNG_INFO_sCAL);
   }

   return(0);
}
#  endif /* FLOATING POINT */
png_uint_32 PNGAPI
wester committed
893
png_get_sCAL_s(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
894 895 896
    int *unit, png_charpp width, png_charpp height)
{
   if (png_ptr != NULL && info_ptr != NULL &&
wester committed
897
       (info_ptr->valid & PNG_INFO_sCAL))
wester committed
898 899 900 901 902 903 904 905 906 907 908 909 910
   {
      *unit = info_ptr->scal_unit;
      *width = info_ptr->scal_s_width;
      *height = info_ptr->scal_s_height;
      return (PNG_INFO_sCAL);
   }

   return(0);
}
#endif /* sCAL */

#ifdef PNG_pHYs_SUPPORTED
png_uint_32 PNGAPI
wester committed
911
png_get_pHYs(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
912 913 914 915 916 917 918
    png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
{
   png_uint_32 retval = 0;

   png_debug1(1, "in %s retrieval function", "pHYs");

   if (png_ptr != NULL && info_ptr != NULL &&
wester committed
919
       (info_ptr->valid & PNG_INFO_pHYs))
wester committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
   {
      if (res_x != NULL)
      {
         *res_x = info_ptr->x_pixels_per_unit;
         retval |= PNG_INFO_pHYs;
      }

      if (res_y != NULL)
      {
         *res_y = info_ptr->y_pixels_per_unit;
         retval |= PNG_INFO_pHYs;
      }

      if (unit_type != NULL)
      {
         *unit_type = (int)info_ptr->phys_unit_type;
         retval |= PNG_INFO_pHYs;
      }
   }

   return (retval);
}
#endif /* pHYs */

png_uint_32 PNGAPI
wester committed
945
png_get_PLTE(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
946 947 948 949
    png_colorp *palette, int *num_palette)
{
   png_debug1(1, "in %s retrieval function", "PLTE");

wester committed
950 951
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)
       && palette != NULL)
wester committed
952 953 954 955 956 957 958 959 960 961 962 963
   {
      *palette = info_ptr->palette;
      *num_palette = info_ptr->num_palette;
      png_debug1(3, "num_palette = %d", *num_palette);
      return (PNG_INFO_PLTE);
   }

   return (0);
}

#ifdef PNG_sBIT_SUPPORTED
png_uint_32 PNGAPI
wester committed
964
png_get_sBIT(png_const_structp png_ptr, png_infop info_ptr,
wester committed
965 966 967 968
    png_color_8p *sig_bit)
{
   png_debug1(1, "in %s retrieval function", "sBIT");

wester committed
969 970
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)
       && sig_bit != NULL)
wester committed
971 972 973 974 975 976 977 978 979 980
   {
      *sig_bit = &(info_ptr->sig_bit);
      return (PNG_INFO_sBIT);
   }

   return (0);
}
#endif

#ifdef PNG_TEXT_SUPPORTED
wester committed
981 982
png_uint_32 PNGAPI
png_get_text(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
983 984 985 986 987 988 989 990 991 992 993 994 995
    png_textp *text_ptr, int *num_text)
{
   if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
   {
      png_debug1(1, "in 0x%lx retrieval function",
         (unsigned long)png_ptr->chunk_name);

      if (text_ptr != NULL)
         *text_ptr = info_ptr->text;

      if (num_text != NULL)
         *num_text = info_ptr->num_text;

wester committed
996
      return ((png_uint_32)info_ptr->num_text);
wester committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
   }

   if (num_text != NULL)
      *num_text = 0;

   return(0);
}
#endif

#ifdef PNG_tIME_SUPPORTED
png_uint_32 PNGAPI
wester committed
1008
png_get_tIME(png_const_structp png_ptr, png_infop info_ptr, png_timep *mod_time)
wester committed
1009 1010 1011
{
   png_debug1(1, "in %s retrieval function", "tIME");

wester committed
1012 1013
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)
       && mod_time != NULL)
wester committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
   {
      *mod_time = &(info_ptr->mod_time);
      return (PNG_INFO_tIME);
   }

   return (0);
}
#endif

#ifdef PNG_tRNS_SUPPORTED
png_uint_32 PNGAPI
wester committed
1025
png_get_tRNS(png_const_structp png_ptr, png_infop info_ptr,
wester committed
1026 1027 1028
    png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color)
{
   png_uint_32 retval = 0;
wester committed
1029
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
wester committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
   {
      png_debug1(1, "in %s retrieval function", "tRNS");

      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
      {
         if (trans_alpha != NULL)
         {
            *trans_alpha = info_ptr->trans_alpha;
            retval |= PNG_INFO_tRNS;
         }

         if (trans_color != NULL)
            *trans_color = &(info_ptr->trans_color);
      }

      else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
      {
         if (trans_color != NULL)
         {
            *trans_color = &(info_ptr->trans_color);
            retval |= PNG_INFO_tRNS;
         }

         if (trans_alpha != NULL)
            *trans_alpha = NULL;
      }

      if (num_trans != NULL)
      {
         *num_trans = info_ptr->num_trans;
         retval |= PNG_INFO_tRNS;
      }
   }

   return (retval);
}
#endif

wester committed
1068
#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
wester committed
1069
int PNGAPI
wester committed
1070
png_get_unknown_chunks(png_const_structp png_ptr, png_const_infop info_ptr,
wester committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    png_unknown_chunkpp unknowns)
{
   if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)
   {
      *unknowns = info_ptr->unknown_chunks;
      return info_ptr->unknown_chunks_num;
   }

   return (0);
}
#endif

#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
png_byte PNGAPI
wester committed
1085
png_get_rgb_to_gray_status (png_const_structp png_ptr)
wester committed
1086 1087 1088 1089 1090 1091 1092
{
   return (png_byte)(png_ptr ? png_ptr->rgb_to_gray_status : 0);
}
#endif

#ifdef PNG_USER_CHUNKS_SUPPORTED
png_voidp PNGAPI
wester committed
1093
png_get_user_chunk_ptr(png_const_structp png_ptr)
wester committed
1094 1095 1096 1097 1098 1099
{
   return (png_ptr ? png_ptr->user_chunk_ptr : NULL);
}
#endif

png_size_t PNGAPI
wester committed
1100
png_get_compression_buffer_size(png_const_structp png_ptr)
wester committed
1101
{
wester committed
1102
   return (png_ptr ? png_ptr->zbuf_size : 0);
wester committed
1103 1104 1105 1106 1107 1108
}

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
/* These functions were added to libpng 1.2.6 and were enabled
 * by default in libpng-1.4.0 */
png_uint_32 PNGAPI
wester committed
1109
png_get_user_width_max (png_const_structp png_ptr)
wester committed
1110 1111 1112 1113 1114
{
   return (png_ptr ? png_ptr->user_width_max : 0);
}

png_uint_32 PNGAPI
wester committed
1115
png_get_user_height_max (png_const_structp png_ptr)
wester committed
1116 1117 1118 1119 1120 1121
{
   return (png_ptr ? png_ptr->user_height_max : 0);
}

/* This function was added to libpng 1.4.0 */
png_uint_32 PNGAPI
wester committed
1122
png_get_chunk_cache_max (png_const_structp png_ptr)
wester committed
1123 1124 1125 1126 1127 1128
{
   return (png_ptr ? png_ptr->user_chunk_cache_max : 0);
}

/* This function was added to libpng 1.4.1 */
png_alloc_size_t PNGAPI
wester committed
1129
png_get_chunk_malloc_max (png_const_structp png_ptr)
wester committed
1130 1131 1132
{
   return (png_ptr ? png_ptr->user_chunk_malloc_max : 0);
}
wester committed
1133
#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
wester committed
1134 1135 1136 1137

/* These functions were added to libpng 1.4.0 */
#ifdef PNG_IO_STATE_SUPPORTED
png_uint_32 PNGAPI
wester committed
1138
png_get_io_state (png_structp png_ptr)
wester committed
1139 1140 1141 1142 1143
{
   return png_ptr->io_state;
}

png_uint_32 PNGAPI
wester committed
1144
png_get_io_chunk_type (png_const_structp png_ptr)
wester committed
1145 1146 1147
{
   return png_ptr->chunk_name;
}
wester committed
1148 1149 1150 1151 1152 1153 1154 1155

png_const_bytep PNGAPI
png_get_io_chunk_name (png_structp png_ptr)
{
   PNG_CSTRING_FROM_CHUNK(png_ptr->io_chunk_string, png_ptr->chunk_name);
   return png_ptr->io_chunk_string;
}
#endif /* ?PNG_IO_STATE_SUPPORTED */
wester committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
#  ifdef PNG_GET_PALETTE_MAX_SUPPORTED
int PNGAPI
png_get_palette_max(png_const_structp png_ptr, png_const_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
      return png_ptr->num_palette_max;

   return (-1);
}
#  endif
#endif

wester committed
1170
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */