pngget.c 26.1 KB
Newer Older
A
Andreas Dilger 已提交
1 2

/* pngget.c - retrieval of values from info struct
3
 *
4
 * Last changed in libpng 1.4.1 [July 31, 2010]
5
 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6 7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (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
 */
A
Andreas Dilger 已提交
14

15
#include "pngpriv.h"
16

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

19
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
20 21
png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
{
22
   if (png_ptr != NULL && info_ptr != NULL)
A
Andreas Dilger 已提交
23
      return(info_ptr->valid & flag);
24

A
Andreas Dilger 已提交
25 26 27 28
   else
      return(0);
}

29
png_size_t PNGAPI
A
Andreas Dilger 已提交
30 31
png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
{
32
   if (png_ptr != NULL && info_ptr != NULL)
A
Andreas Dilger 已提交
33
      return(info_ptr->rowbytes);
34

A
Andreas Dilger 已提交
35 36 37 38
   else
      return(0);
}

39
#ifdef PNG_INFO_IMAGE_SUPPORTED
40
png_bytepp PNGAPI
41 42 43 44
png_get_rows(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
      return(info_ptr->row_pointers);
45

46 47 48 49 50
   else
      return(0);
}
#endif

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

59 60 61
   return (0);
}

62
png_uint_32 PNGAPI
63 64
png_get_image_height(png_structp png_ptr, png_infop info_ptr)
{
65
   if (png_ptr != NULL && info_ptr != NULL)
66
      return info_ptr->height;
67

68 69 70
   return (0);
}

71
png_byte PNGAPI
72 73
png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)
{
74
   if (png_ptr != NULL && info_ptr != NULL)
75
      return info_ptr->bit_depth;
76

77 78 79
   return (0);
}

80
png_byte PNGAPI
81 82
png_get_color_type(png_structp png_ptr, png_infop info_ptr)
{
83
   if (png_ptr != NULL && info_ptr != NULL)
84
      return info_ptr->color_type;
85

86 87 88
   return (0);
}

89
png_byte PNGAPI
90 91
png_get_filter_type(png_structp png_ptr, png_infop info_ptr)
{
92
   if (png_ptr != NULL && info_ptr != NULL)
93
      return info_ptr->filter_type;
94

95 96 97
   return (0);
}

98
png_byte PNGAPI
99 100
png_get_interlace_type(png_structp png_ptr, png_infop info_ptr)
{
101
   if (png_ptr != NULL && info_ptr != NULL)
102
      return info_ptr->interlace_type;
103

104 105 106
   return (0);
}

107
png_byte PNGAPI
108 109
png_get_compression_type(png_structp png_ptr, png_infop info_ptr)
{
110
   if (png_ptr != NULL && info_ptr != NULL)
111
      return info_ptr->compression_type;
112

113 114 115
   return (0);
}

116
png_uint_32 PNGAPI
117 118
png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
{
119
   if (png_ptr != NULL && info_ptr != NULL)
120
#ifdef PNG_pHYs_SUPPORTED
121
   if (info_ptr->valid & PNG_INFO_pHYs)
122
   {
123
      png_debug1(1, "in %s retrieval function", "png_get_x_pixels_per_meter");
124

125
      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
126
         return (0);
127

128
      else
129
         return (info_ptr->x_pixels_per_unit);
130
   }
131 132
#else
   return (0);
133 134 135 136
#endif
   return (0);
}

137
png_uint_32 PNGAPI
138 139
png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
{
140
   if (png_ptr != NULL && info_ptr != NULL)
141
#ifdef PNG_pHYs_SUPPORTED
142
   if (info_ptr->valid & PNG_INFO_pHYs)
143
   {
144
      png_debug1(1, "in %s retrieval function", "png_get_y_pixels_per_meter");
145

146
      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
147
         return (0);
148

149
      else
150
         return (info_ptr->y_pixels_per_unit);
151
   }
152 153
#else
   return (0);
154 155 156 157
#endif
   return (0);
}

158
png_uint_32 PNGAPI
159 160
png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
{
161
   if (png_ptr != NULL && info_ptr != NULL)
162
#ifdef PNG_pHYs_SUPPORTED
163
   if (info_ptr->valid & PNG_INFO_pHYs)
164
   {
165
      png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter");
166

167
      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER ||
168 169
          info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit)
         return (0);
170

171
      else
172
         return (info_ptr->x_pixels_per_unit);
173
   }
174 175
#else
   return (0);
176 177 178 179
#endif
   return (0);
}

180
#ifdef PNG_FLOATING_POINT_SUPPORTED
181
float PNGAPI
182
png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr)
183
{
184
   if (png_ptr != NULL && info_ptr != NULL)
185
#ifdef PNG_READ_pHYs_SUPPORTED
186

187
   if (info_ptr->valid & PNG_INFO_pHYs)
188
   {
189
      png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio");
190

191
      if (info_ptr->x_pixels_per_unit == 0)
192
         return ((float)0.0);
193

194
      else
195
         return ((float)((float)info_ptr->y_pixels_per_unit
196
             /(float)info_ptr->x_pixels_per_unit));
197
   }
198
#else
199
      return (0.0);
200
#endif
201
   return ((float)0.0);
202
}
203
#endif
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
#ifdef PNG_FIXED_POINT_SUPPORTED
png_fixed_point PNGAPI
png_get_pixel_aspect_ratio_fixed(png_structp png_ptr, png_infop info_ptr)
{
#ifdef PNG_READ_pHYs_SUPPORTED
   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)
   {
      png_fixed_point res;

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

      if (png_muldiv(&res, info_ptr->y_pixels_per_unit, PNG_FP_1,
          info_ptr->x_pixels_per_unit))
	 return res;
   }
#endif
   return 0;
}
#endif

226
png_int_32 PNGAPI
227 228
png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr)
{
229
   if (png_ptr != NULL && info_ptr != NULL)
230
#ifdef PNG_oFFs_SUPPORTED
231

232
   if (info_ptr->valid & PNG_INFO_oFFs)
233
   {
234
      png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns");
235

236
      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
237
         return (0);
238

239
      else
240
         return (info_ptr->x_offset);
241
   }
242
#else
243
      return (0);
244 245 246 247
#endif
   return (0);
}

248
png_int_32 PNGAPI
249 250
png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr)
{
251
   if (png_ptr != NULL && info_ptr != NULL)
252

253
#ifdef PNG_oFFs_SUPPORTED
254
   if (info_ptr->valid & PNG_INFO_oFFs)
255
   {
256
      png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns");
257

258
      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
259
         return (0);
260

261
      else
262
         return (info_ptr->y_offset);
263
   }
264 265
#else
   return (0);
266 267 268 269
#endif
   return (0);
}

270
png_int_32 PNGAPI
271 272
png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr)
{
273
   if (png_ptr != NULL && info_ptr != NULL)
274

275
#ifdef PNG_oFFs_SUPPORTED
276
   if (info_ptr->valid & PNG_INFO_oFFs)
277
   {
G
[devel]  
Glenn Randers-Pehrson 已提交
278
      png_debug1(1, "in %s retrieval function", "png_get_x_offset_pixels");
279

280
      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
281
         return (0);
282

283
      else
284
         return (info_ptr->x_offset);
285
   }
286 287
#else
   return (0);
288 289 290 291
#endif
   return (0);
}

292
png_int_32 PNGAPI
293 294
png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr)
{
295
   if (png_ptr != NULL && info_ptr != NULL)
296

297
#ifdef PNG_oFFs_SUPPORTED
298
   if (info_ptr->valid & PNG_INFO_oFFs)
299
   {
G
[devel]  
Glenn Randers-Pehrson 已提交
300
      png_debug1(1, "in %s retrieval function", "png_get_y_offset_pixels");
301

302
      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
303
         return (0);
304

305
      else
306
         return (info_ptr->y_offset);
307
   }
308 309
#else
   return (0);
310 311 312 313
#endif
   return (0);
}

G
[devel]  
Glenn Randers-Pehrson 已提交
314 315 316 317 318
#ifdef PNG_INCH_CONVERSIONS_SUPPORTED
static png_uint_32
ppi_from_ppm(png_uint_32 ppm)
{
#if 0
319
   /* The conversion is *(2.54/100), in binary (32 digits):
G
[devel]  
Glenn Randers-Pehrson 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    * .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, ppm, 127, 5000))
      return result;

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

346
png_uint_32 PNGAPI
347 348
png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
{
G
[devel]  
Glenn Randers-Pehrson 已提交
349
   return ppi_from_ppm(png_get_pixels_per_meter(png_ptr, info_ptr));
350 351
}

352
png_uint_32 PNGAPI
353 354
png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
{
G
[devel]  
Glenn Randers-Pehrson 已提交
355
   return ppi_from_ppm(png_get_x_pixels_per_meter(png_ptr, info_ptr));
356 357
}

358
png_uint_32 PNGAPI
359 360
png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
{
G
[devel]  
Glenn Randers-Pehrson 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
   return ppi_from_ppm(png_get_y_pixels_per_meter(png_ptr, info_ptr));
}

#ifdef PNG_FIXED_POINT_SUPPORTED
static png_fixed_point
png_fixed_inches_from_microns(png_structp png_ptr, png_int_32 microns)
{
   /* Convert from metres * 1,000,000 to inches * 100,000, meters to
    * inches is simply *(100/2.54), so we want *(10/2.54) == 1000/254.
    * 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
png_get_x_offset_inches_fixed(png_structp png_ptr, png_infop info_ptr)
{
   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
png_get_y_offset_inches_fixed(png_structp png_ptr, png_infop info_ptr)
{
   return png_fixed_inches_from_microns(png_ptr,
      png_get_y_offset_microns(png_ptr, info_ptr));
390
}
G
[devel]  
Glenn Randers-Pehrson 已提交
391
#endif
392

G
[devel]  
Glenn Randers-Pehrson 已提交
393
#ifdef PNG_FLOATING_POINT_SUPPORTED
394
float PNGAPI
395 396
png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr)
{
G
[devel]  
Glenn Randers-Pehrson 已提交
397 398 399 400
   /* To avoid the overflow do the conversion directly in floating
    * point.
    */
   return png_get_x_offset_microns(png_ptr, info_ptr) * .00003937f;
401
}
G
[devel]  
Glenn Randers-Pehrson 已提交
402
#endif
403

G
[devel]  
Glenn Randers-Pehrson 已提交
404
#ifdef PNG_FLOATING_POINT_SUPPORTED
405
float PNGAPI
406 407
png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr)
{
G
[devel]  
Glenn Randers-Pehrson 已提交
408 409 410 411
   /* To avoid the overflow do the conversion directly in floating
    * point.
    */
   return png_get_y_offset_microns(png_ptr, info_ptr) * .00003937f;
412
}
G
[devel]  
Glenn Randers-Pehrson 已提交
413
#endif
414

415
#ifdef PNG_pHYs_SUPPORTED
416
png_uint_32 PNGAPI
417
png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr,
418
    png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
419 420 421
{
   png_uint_32 retval = 0;

422
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
423
   {
424
      png_debug1(1, "in %s retrieval function", "pHYs");
425

426 427 428 429 430
      if (res_x != NULL)
      {
         *res_x = info_ptr->x_pixels_per_unit;
         retval |= PNG_INFO_pHYs;
      }
431

432 433 434 435 436
      if (res_y != NULL)
      {
         *res_y = info_ptr->y_pixels_per_unit;
         retval |= PNG_INFO_pHYs;
      }
437

438 439 440 441
      if (unit_type != NULL)
      {
         *unit_type = (int)info_ptr->phys_unit_type;
         retval |= PNG_INFO_pHYs;
442
         if (*unit_type == 1)
443
         {
444 445
            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);
446 447 448 449 450
         }
      }
   }
   return (retval);
}
451
#endif /* PNG_pHYs_SUPPORTED */
G
[devel]  
Glenn Randers-Pehrson 已提交
452
#endif  /* PNG_INCH_CONVERSIONS_SUPPORTED */
453 454

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

456 457
#endif  /* PNG_EASY_ACCESS_SUPPORTED */

458
png_byte PNGAPI
A
Andreas Dilger 已提交
459 460
png_get_channels(png_structp png_ptr, png_infop info_ptr)
{
461
   if (png_ptr != NULL && info_ptr != NULL)
A
Andreas Dilger 已提交
462
      return(info_ptr->channels);
463

A
Andreas Dilger 已提交
464
   else
465
      return (0);
A
Andreas Dilger 已提交
466 467
}

468
png_bytep PNGAPI
A
Andreas Dilger 已提交
469 470
png_get_signature(png_structp png_ptr, png_infop info_ptr)
{
471
   if (png_ptr != NULL && info_ptr != NULL)
A
Andreas Dilger 已提交
472
      return(info_ptr->signature);
473

A
Andreas Dilger 已提交
474
   else
475
      return (NULL);
A
Andreas Dilger 已提交
476 477
}

478
#ifdef PNG_bKGD_SUPPORTED
479
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
480 481 482
png_get_bKGD(png_structp png_ptr, png_infop info_ptr,
   png_color_16p *background)
{
483
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
484
       && background != NULL)
A
Andreas Dilger 已提交
485
   {
486
      png_debug1(1, "in %s retrieval function", "bKGD");
487

A
Andreas Dilger 已提交
488 489 490 491 492 493 494
      *background = &(info_ptr->background);
      return (PNG_INFO_bKGD);
   }
   return (0);
}
#endif

495
#ifdef PNG_cHRM_SUPPORTED
496
#ifdef PNG_FLOATING_POINT_SUPPORTED
497
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
498
png_get_cHRM(png_structp png_ptr, png_infop info_ptr,
499 500
    double *white_x, double *white_y, double *red_x, double *red_y,
    double *green_x, double *green_y, double *blue_x, double *blue_y)
A
Andreas Dilger 已提交
501
{
502
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
A
Andreas Dilger 已提交
503
   {
504
      png_debug1(1, "in %s retrieval function", "cHRM");
505

A
Andreas Dilger 已提交
506
      if (white_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
507
         *white_x = png_float(png_ptr, info_ptr->x_white, "cHRM white X");
A
Andreas Dilger 已提交
508
      if (white_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
509
         *white_y = png_float(png_ptr, info_ptr->y_white, "cHRM white Y");
A
Andreas Dilger 已提交
510
      if (red_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
511
         *red_x = png_float(png_ptr, info_ptr->x_red, "cHRM red X");
A
Andreas Dilger 已提交
512
      if (red_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
513
         *red_y = png_float(png_ptr, info_ptr->y_red, "cHRM red Y");
A
Andreas Dilger 已提交
514
      if (green_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
515
         *green_x = png_float(png_ptr, info_ptr->x_green, "cHRM green X");
A
Andreas Dilger 已提交
516
      if (green_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
517
         *green_y = png_float(png_ptr, info_ptr->y_green, "cHRM green Y");
A
Andreas Dilger 已提交
518
      if (blue_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
519
         *blue_x = png_float(png_ptr, info_ptr->x_blue, "cHRM blue X");
A
Andreas Dilger 已提交
520
      if (blue_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
521
         *blue_y = png_float(png_ptr, info_ptr->y_blue, "cHRM blue Y");
A
Andreas Dilger 已提交
522 523 524 525 526
      return (PNG_INFO_cHRM);
   }
   return (0);
}
#endif
G
[devel]  
Glenn Randers-Pehrson 已提交
527

528
#ifdef PNG_FIXED_POINT_SUPPORTED
529
png_uint_32 PNGAPI
530
png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
531 532 533
    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)
534
{
535 536
   png_debug1(1, "in %s retrieval function", "cHRM");

537 538 539
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
   {
      if (white_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
540
         *white_x = info_ptr->x_white;
541
      if (white_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
542
         *white_y = info_ptr->y_white;
543
      if (red_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
544
         *red_x = info_ptr->x_red;
545
      if (red_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
546
         *red_y = info_ptr->y_red;
547
      if (green_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
548
         *green_x = info_ptr->x_green;
549
      if (green_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
550
         *green_y = info_ptr->y_green;
551
      if (blue_x != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
552
         *blue_x = info_ptr->x_blue;
553
      if (blue_y != NULL)
G
[devel]  
Glenn Randers-Pehrson 已提交
554
         *blue_y = info_ptr->y_blue;
555 556 557 558 559 560
      return (PNG_INFO_cHRM);
   }
   return (0);
}
#endif
#endif
A
Andreas Dilger 已提交
561

562
#ifdef PNG_gAMA_SUPPORTED
G
[devel]  
Glenn Randers-Pehrson 已提交
563 564 565
png_uint_32 PNGFAPI
png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr,
    png_fixed_point *file_gamma)
A
Andreas Dilger 已提交
566
{
567 568
   png_debug1(1, "in %s retrieval function", "gAMA");

569
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
570
       && file_gamma != NULL)
A
Andreas Dilger 已提交
571
   {
G
[devel]  
Glenn Randers-Pehrson 已提交
572
      *file_gamma = info_ptr->gamma;
A
Andreas Dilger 已提交
573 574
      return (PNG_INFO_gAMA);
   }
G
[devel]  
Glenn Randers-Pehrson 已提交
575

A
Andreas Dilger 已提交
576 577
   return (0);
}
G
[devel]  
Glenn Randers-Pehrson 已提交
578
#ifdef PNG_FLOATING_POINT_SUPPORTED
579
png_uint_32 PNGAPI
G
[devel]  
Glenn Randers-Pehrson 已提交
580
png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
581
{
G
[devel]  
Glenn Randers-Pehrson 已提交
582 583 584 585 586
   png_fixed_point igamma;
   png_uint_32 ok = png_get_gAMA_fixed(png_ptr, info_ptr, &igamma);
   if (ok)
      *file_gamma = png_float(png_ptr, igamma, "png_get_gAMA");
   return ok;
587 588
}
#endif
589
#endif
A
Andreas Dilger 已提交
590

591
#ifdef PNG_sRGB_SUPPORTED
592
png_uint_32 PNGAPI
593
png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent)
594
{
595 596
   png_debug1(1, "in %s retrieval function", "sRGB");

597
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
598
       && file_srgb_intent != NULL)
599
   {
600
      *file_srgb_intent = (int)info_ptr->srgb_intent;
601 602
      return (PNG_INFO_sRGB);
   }
603

604 605 606 607
   return (0);
}
#endif

608
#ifdef PNG_iCCP_SUPPORTED
609
png_uint_32 PNGAPI
610
png_get_iCCP(png_structp png_ptr, png_infop info_ptr,
611
    png_charpp name, int *compression_type,
G
[devel]  
Glenn Randers-Pehrson 已提交
612
    png_bytepp profile, png_uint_32 *proflen)
613
{
614 615
   png_debug1(1, "in %s retrieval function", "iCCP");

616
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)
617
       && name != NULL && profile != NULL && proflen != NULL)
618 619 620
   {
      *name = info_ptr->iccp_name;
      *profile = info_ptr->iccp_profile;
621 622 623
      /* Compression_type is a dummy so the API won't have to change
       * if we introduce multiple compression types later.
       */
624 625 626 627
      *proflen = (int)info_ptr->iccp_proflen;
      *compression_type = (int)info_ptr->iccp_compression;
      return (PNG_INFO_iCCP);
   }
628

629 630 631 632
   return (0);
}
#endif

633
#ifdef PNG_sPLT_SUPPORTED
634
png_uint_32 PNGAPI
635
png_get_sPLT(png_structp png_ptr, png_infop info_ptr,
636
    png_sPLT_tpp spalettes)
637 638
{
   if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)
639
   {
640 641
      *spalettes = info_ptr->splt_palettes;
      return ((png_uint_32)info_ptr->splt_palettes_num);
642
   }
643

644
   return (0);
645 646 647
}
#endif

648
#ifdef PNG_hIST_SUPPORTED
649
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
650 651
png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)
{
652 653
   png_debug1(1, "in %s retrieval function", "hIST");

654
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
655
       && hist != NULL)
A
Andreas Dilger 已提交
656 657 658 659
   {
      *hist = info_ptr->hist;
      return (PNG_INFO_hIST);
   }
660

A
Andreas Dilger 已提交
661 662 663 664
   return (0);
}
#endif

665
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
666
png_get_IHDR(png_structp png_ptr, png_infop info_ptr,
667 668 669
    png_uint_32 *width, png_uint_32 *height, int *bit_depth,
    int *color_type, int *interlace_type, int *compression_type,
    int *filter_type)
670

A
Andreas Dilger 已提交
671
{
672
   png_debug1(1, "in %s retrieval function", "IHDR");
673

674 675 676
   if (png_ptr == NULL || info_ptr == NULL || width == NULL ||
       height == NULL || bit_depth == NULL || color_type == NULL)
      return (0);
677

678 679 680 681
   *width = info_ptr->width;
   *height = info_ptr->height;
   *bit_depth = info_ptr->bit_depth;
   *color_type = info_ptr->color_type;
682

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

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

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

692 693 694 695 696 697 698 699
   /* 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.
    */
   png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
       info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
       info_ptr->compression_type, info_ptr->filter_type);
700

701
   return (1);
A
Andreas Dilger 已提交
702 703
}

704
#ifdef PNG_oFFs_SUPPORTED
705
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
706
png_get_oFFs(png_structp png_ptr, png_infop info_ptr,
707
    png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)
A
Andreas Dilger 已提交
708
{
709 710
   png_debug1(1, "in %s retrieval function", "oFFs");

711
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
712
       && offset_x != NULL && offset_y != NULL && unit_type != NULL)
A
Andreas Dilger 已提交
713 714 715 716 717 718
   {
      *offset_x = info_ptr->x_offset;
      *offset_y = info_ptr->y_offset;
      *unit_type = (int)info_ptr->offset_unit_type;
      return (PNG_INFO_oFFs);
   }
719

A
Andreas Dilger 已提交
720 721 722 723
   return (0);
}
#endif

724
#ifdef PNG_pCAL_SUPPORTED
725
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
726
png_get_pCAL(png_structp png_ptr, png_infop info_ptr,
727 728
    png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
    png_charp *units, png_charpp *params)
A
Andreas Dilger 已提交
729
{
730 731
   png_debug1(1, "in %s retrieval function", "pCAL");

732
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)
733 734
       && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
       nparams != NULL && units != NULL && params != NULL)
A
Andreas Dilger 已提交
735 736 737 738 739 740 741 742 743 744
   {
      *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);
   }
745

A
Andreas Dilger 已提交
746 747 748 749
   return (0);
}
#endif

750
#ifdef PNG_sCAL_SUPPORTED
G
[devel]  
Glenn Randers-Pehrson 已提交
751
#ifdef PNG_FIXED_POINT_SUPPORTED
752
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
G
[devel]  
Glenn Randers-Pehrson 已提交
753 754 755 756 757 758 759 760
png_uint_32 PNGAPI
png_get_sCAL_fixed(png_structp png_ptr, png_infop info_ptr,
    int *unit, png_fixed_point *width, png_fixed_point *height)
{
   if (png_ptr != NULL && info_ptr != NULL &&
       (info_ptr->valid & PNG_INFO_sCAL))
   {
      *unit = info_ptr->scal_unit;
761
      /*TODO: make this work without FP support */
G
[devel]  
Glenn Randers-Pehrson 已提交
762 763 764 765 766 767 768 769
      *width = png_fixed(png_ptr, atof(info_ptr->scal_s_width), "sCAL width");
      *height = png_fixed(png_ptr, atof(info_ptr->scal_s_height),
         "sCAL height");
      return (PNG_INFO_sCAL);
   }

   return(0);
}
770 771
#endif /*FLOATING_ARITHMETIC*/
#endif /*FIXED_POINT*/
772
#ifdef PNG_FLOATING_POINT_SUPPORTED
773
png_uint_32 PNGAPI
774
png_get_sCAL(png_structp png_ptr, png_infop info_ptr,
775 776 777 778 779 780
    int *unit, double *width, double *height)
{
   if (png_ptr != NULL && info_ptr != NULL &&
       (info_ptr->valid & PNG_INFO_sCAL))
   {
      *unit = info_ptr->scal_unit;
G
[devel]  
Glenn Randers-Pehrson 已提交
781 782
      *width = atof(info_ptr->scal_s_width);
      *height = atof(info_ptr->scal_s_height);
783 784
      return (PNG_INFO_sCAL);
   }
785

786
   return(0);
787
}
788
#endif
789
png_uint_32 PNGAPI
790
png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr,
791
             int *unit, png_charpp width, png_charpp height)
792
{
793 794 795 796 797 798 799 800
   if (png_ptr != NULL && info_ptr != NULL &&
       (info_ptr->valid & PNG_INFO_sCAL))
   {
      *unit = info_ptr->scal_unit;
      *width = info_ptr->scal_s_width;
      *height = info_ptr->scal_s_height;
      return (PNG_INFO_sCAL);
   }
801

802
   return(0);
803 804 805
}
#endif

806
#ifdef PNG_pHYs_SUPPORTED
807
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
808
png_get_pHYs(png_structp png_ptr, png_infop info_ptr,
809
    png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
A
Andreas Dilger 已提交
810
{
811 812
   png_uint_32 retval = 0;

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

815
   if (png_ptr != NULL && info_ptr != NULL &&
816
       (info_ptr->valid & PNG_INFO_pHYs))
A
Andreas Dilger 已提交
817
   {
818
      if (res_x != NULL)
819 820
      {
         *res_x = info_ptr->x_pixels_per_unit;
821 822
         retval |= PNG_INFO_pHYs;
      }
823

824 825
      if (res_y != NULL)
      {
826 827 828
         *res_y = info_ptr->y_pixels_per_unit;
         retval |= PNG_INFO_pHYs;
      }
829

830 831 832 833 834
      if (unit_type != NULL)
      {
         *unit_type = (int)info_ptr->phys_unit_type;
         retval |= PNG_INFO_pHYs;
      }
A
Andreas Dilger 已提交
835
   }
836

837
   return (retval);
A
Andreas Dilger 已提交
838 839 840
}
#endif

841
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
842
png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette,
843
    int *num_palette)
A
Andreas Dilger 已提交
844
{
845 846
   png_debug1(1, "in %s retrieval function", "PLTE");

847 848
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)
       && palette != NULL)
A
Andreas Dilger 已提交
849 850 851
   {
      *palette = info_ptr->palette;
      *num_palette = info_ptr->num_palette;
852
      png_debug1(3, "num_palette = %d", *num_palette);
A
Andreas Dilger 已提交
853 854
      return (PNG_INFO_PLTE);
   }
855

A
Andreas Dilger 已提交
856 857 858
   return (0);
}

859
#ifdef PNG_sBIT_SUPPORTED
860
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
861 862
png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)
{
863 864
   png_debug1(1, "in %s retrieval function", "sBIT");

865
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)
866
       && sig_bit != NULL)
A
Andreas Dilger 已提交
867 868 869 870
   {
      *sig_bit = &(info_ptr->sig_bit);
      return (PNG_INFO_sBIT);
   }
871

A
Andreas Dilger 已提交
872 873 874 875
   return (0);
}
#endif

876
#ifdef PNG_TEXT_SUPPORTED
877
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
878
png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr,
879
    int *num_text)
A
Andreas Dilger 已提交
880
{
881
   if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
A
Andreas Dilger 已提交
882
   {
883
      png_debug1(1, "in %s retrieval function",
884 885
          (png_ptr->chunk_name[0] == '\0' ? "text" :
          (png_const_charp)png_ptr->chunk_name));
886

A
Andreas Dilger 已提交
887 888
      if (text_ptr != NULL)
         *text_ptr = info_ptr->text;
889

A
Andreas Dilger 已提交
890 891
      if (num_text != NULL)
         *num_text = info_ptr->num_text;
892

893
      return ((png_uint_32)info_ptr->num_text);
A
Andreas Dilger 已提交
894
   }
895

896
   if (num_text != NULL)
897
      *num_text = 0;
898

A
Andreas Dilger 已提交
899 900 901 902
   return(0);
}
#endif

903
#ifdef PNG_tIME_SUPPORTED
904
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
905 906
png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)
{
907 908
   png_debug1(1, "in %s retrieval function", "tIME");

909 910
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)
       && mod_time != NULL)
A
Andreas Dilger 已提交
911 912 913 914
   {
      *mod_time = &(info_ptr->mod_time);
      return (PNG_INFO_tIME);
   }
915

A
Andreas Dilger 已提交
916 917 918 919
   return (0);
}
#endif

920
#ifdef PNG_tRNS_SUPPORTED
921
png_uint_32 PNGAPI
A
Andreas Dilger 已提交
922
png_get_tRNS(png_structp png_ptr, png_infop info_ptr,
923
    png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color)
A
Andreas Dilger 已提交
924
{
925
   png_uint_32 retval = 0;
926
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
A
Andreas Dilger 已提交
927
   {
928
      png_debug1(1, "in %s retrieval function", "tRNS");
929

930
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
A
Andreas Dilger 已提交
931
      {
932 933 934 935 936 937 938 939
         if (trans_alpha != NULL)
         {
            *trans_alpha = info_ptr->trans_alpha;
            retval |= PNG_INFO_tRNS;
         }

         if (trans_color != NULL)
            *trans_color = &(info_ptr->trans_color);
A
Andreas Dilger 已提交
940
      }
941

942
      else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
A
Andreas Dilger 已提交
943
      {
944 945 946 947 948 949 950 951
         if (trans_color != NULL)
         {
            *trans_color = &(info_ptr->trans_color);
            retval |= PNG_INFO_tRNS;
         }

         if (trans_alpha != NULL)
            *trans_alpha = NULL;
A
Andreas Dilger 已提交
952
      }
953

954
      if (num_trans != NULL)
A
Andreas Dilger 已提交
955
      {
956 957
         *num_trans = info_ptr->num_trans;
         retval |= PNG_INFO_tRNS;
A
Andreas Dilger 已提交
958 959
      }
   }
960

961
   return (retval);
A
Andreas Dilger 已提交
962 963 964
}
#endif

965
#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
966
png_uint_32 PNGAPI
967
png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr,
968
    png_unknown_chunkpp unknowns)
969 970
{
   if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)
971
   {
972 973
      *unknowns = info_ptr->unknown_chunks;
      return ((png_uint_32)info_ptr->unknown_chunks_num);
974
   }
975

976
   return (0);
977 978 979
}
#endif

980
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
981
png_byte PNGAPI
982 983
png_get_rgb_to_gray_status (png_structp png_ptr)
{
984
   return (png_byte)(png_ptr ? png_ptr->rgb_to_gray_status : 0);
985 986
}
#endif
987

988
#ifdef PNG_USER_CHUNKS_SUPPORTED
989
png_voidp PNGAPI
990 991
png_get_user_chunk_ptr(png_structp png_ptr)
{
992
   return (png_ptr ? png_ptr->user_chunk_ptr : NULL);
993 994 995
}
#endif

996
png_size_t PNGAPI
997 998
png_get_compression_buffer_size(png_structp png_ptr)
{
999
   return (png_ptr ? png_ptr->zbuf_size : 0L);
1000 1001
}

1002 1003

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
1004 1005
/* These functions were added to libpng 1.2.6 and were enabled
 * by default in libpng-1.4.0 */
1006 1007 1008
png_uint_32 PNGAPI
png_get_user_width_max (png_structp png_ptr)
{
1009
   return (png_ptr ? png_ptr->user_width_max : 0);
1010 1011 1012 1013
}
png_uint_32 PNGAPI
png_get_user_height_max (png_structp png_ptr)
{
1014
   return (png_ptr ? png_ptr->user_height_max : 0);
1015
}
1016
/* This function was added to libpng 1.4.0 */
1017 1018 1019
png_uint_32 PNGAPI
png_get_chunk_cache_max (png_structp png_ptr)
{
1020
   return (png_ptr ? png_ptr->user_chunk_cache_max : 0);
1021 1022
}
/* This function was added to libpng 1.4.1 */
1023
png_alloc_size_t PNGAPI
1024 1025
png_get_chunk_malloc_max (png_structp png_ptr)
{
1026
   return (png_ptr ? png_ptr->user_chunk_malloc_max : 0);
1027
}
1028
#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1029

1030
/* These functions were added to libpng 1.4.0 */
1031 1032 1033 1034
#ifdef PNG_IO_STATE_SUPPORTED
png_uint_32 PNGAPI
png_get_io_state (png_structp png_ptr)
{
1035
   return png_ptr->io_state;
1036 1037 1038 1039 1040 1041 1042 1043 1044
}

png_bytep PNGAPI
png_get_io_chunk_name (png_structp png_ptr)
{
   return png_ptr->chunk_name;
}
#endif /* ?PNG_IO_STATE_SUPPORTED */

1045
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */