2
2
/* pngread.c - read a PNG file
4
* Last changed in libpng 1.2.35 [February 14, 2009]
5
* For conditions of distribution and use, see copyright notice in png.h
6
* Copyright (c) 1998-2009 Glenn Randers-Pehrson
4
* Last changed in libpng 1.2.44 [June 26, 2010]
5
* Copyright (c) 1998-2010 Glenn Randers-Pehrson
7
6
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8
7
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
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
10
13
* This file contains routines that an application calls directly to
11
14
* read a PNG file or stream.
14
17
#define PNG_INTERNAL
18
#define PNG_NO_PEDANTIC_WARNINGS
16
#if defined(PNG_READ_SUPPORTED)
20
#ifdef PNG_READ_SUPPORTED
18
23
/* Create a PNG structure for reading, and allocate any memory needed. */
99
if (user_png_ver[i] != png_libpng_ver[i])
100
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
101
} while (png_libpng_ver[i++]);
104
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
107
if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
109
/* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
110
* we must recompile any applications that use any older library version.
111
* For versions after libpng 1.0, we will be compatible, so we need
112
* only check the first digit.
114
if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
115
(user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
116
(user_png_ver[0] == '0' && user_png_ver[2] < '9'))
118
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
122
png_snprintf(msg, 80,
123
"Application was compiled with png.h from libpng-%.20s",
125
png_warning(png_ptr, msg);
127
png_snprintf(msg, 80,
115
if (user_png_ver[i] != png_libpng_ver[i])
116
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
117
} while (png_libpng_ver[i++]);
120
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
123
if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
125
/* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
126
* we must recompile any applications that use any older library version.
127
* For versions after libpng 1.0, we will be compatible, so we need
128
* only check the first digit.
130
if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
131
(user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
132
(user_png_ver[0] == '0' && user_png_ver[2] < '9'))
134
#if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
138
png_snprintf(msg, 80,
139
"Application was compiled with png.h from libpng-%.20s",
141
png_warning(png_ptr, msg);
143
png_snprintf(msg, 80,
128
144
"Application is running with png.c from libpng-%.20s",
130
png_warning(png_ptr, msg);
146
png_warning(png_ptr, msg);
132
148
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
136
"Incompatible libpng version in application and library");
152
"Incompatible libpng version in application and library");
140
/* initialize zbuf - compression buffer */
156
/* Initialize zbuf - compression buffer */
141
157
png_ptr->zbuf_size = PNG_ZBUF_SIZE;
142
158
png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
143
159
(png_uint_32)png_ptr->zbuf_size);
145
161
png_ptr->zstream.zfree = png_zfree;
146
162
png_ptr->zstream.opaque = (voidpf)png_ptr;
148
switch (inflateInit(&png_ptr->zstream))
150
case Z_OK: /* Do nothing */ break;
152
case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
153
case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
154
default: png_error(png_ptr, "Unknown zlib error");
164
switch (inflateInit(&png_ptr->zstream))
166
case Z_OK: /* Do nothing */ break;
168
case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error");
170
case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error");
172
default: png_error(png_ptr, "Unknown zlib error");
157
176
png_ptr->zstream.next_out = png_ptr->zbuf;
158
177
png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
160
179
png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
162
181
#ifdef PNG_SETJMP_SUPPORTED
163
/* Applications that neglect to set up their own setjmp() and then encounter
164
a png_error() will longjmp here. Since the jmpbuf is then meaningless we
165
abort instead of returning. */
182
/* Applications that neglect to set up their own setjmp() and then
183
encounter a png_error() will longjmp here. Since the jmpbuf is
184
then meaningless we abort instead of returning. */
166
185
#ifdef USE_FAR_KEYWORD
167
186
if (setjmp(jmpbuf))
169
188
png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
171
190
if (setjmp(png_ptr->jmpbuf))
193
#endif /* PNG_SETJMP_SUPPORTED */
175
195
return (png_ptr);
178
198
#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
179
199
/* Initialize PNG structure for reading, and allocate any memory needed.
180
This interface is deprecated in favour of the png_create_read_struct(),
181
and it will disappear as of libpng-1.3.0. */
200
* This interface is deprecated in favour of the png_create_read_struct(),
201
* and it will disappear as of libpng-1.3.0.
182
203
#undef png_read_init
184
205
png_read_init(png_structp png_ptr)
247
271
png_structp png_ptr=*ptr_ptr;
249
if (png_ptr == NULL) return;
253
if (user_png_ver[i] != png_libpng_ver[i])
278
if (user_png_ver[i] != png_libpng_ver[i])
255
280
#ifdef PNG_LEGACY_SUPPORTED
256
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
281
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
258
png_ptr->warning_fn = NULL;
260
"Application uses deprecated png_read_init() and should be recompiled.");
283
png_ptr->warning_fn = NULL;
285
"Application uses deprecated png_read_init() and should be"
264
290
} while (png_libpng_ver[i++]);
266
292
png_debug(1, "in png_read_init_3");
268
294
#ifdef PNG_SETJMP_SUPPORTED
269
/* save jump buffer and error functions */
295
/* Save jump buffer and error functions */
270
296
png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
277
303
png_ptr = *ptr_ptr;
280
/* reset all variables to 0 */
306
/* Reset all variables to 0 */
281
307
png_memset(png_ptr, 0, png_sizeof(png_struct));
283
309
#ifdef PNG_SETJMP_SUPPORTED
284
/* restore jump buffer */
310
/* Restore jump buffer */
285
311
png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
288
/* added at libpng-1.2.6 */
314
/* Added at libpng-1.2.6 */
289
315
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
290
png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
291
png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
316
png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
317
png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
294
/* initialize zbuf - compression buffer */
320
/* Initialize zbuf - compression buffer */
295
321
png_ptr->zbuf_size = PNG_ZBUF_SIZE;
322
png_ptr->zstream.zalloc = png_zalloc;
296
323
png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
297
324
(png_uint_32)png_ptr->zbuf_size);
298
325
png_ptr->zstream.zalloc = png_zalloc;
356
386
PNG_CONST PNG_IDAT;
357
387
PNG_CONST PNG_IEND;
358
388
PNG_CONST PNG_PLTE;
359
#if defined(PNG_READ_bKGD_SUPPORTED)
389
#ifdef PNG_READ_bKGD_SUPPORTED
360
390
PNG_CONST PNG_bKGD;
362
#if defined(PNG_READ_cHRM_SUPPORTED)
392
#ifdef PNG_READ_cHRM_SUPPORTED
363
393
PNG_CONST PNG_cHRM;
365
#if defined(PNG_READ_gAMA_SUPPORTED)
395
#ifdef PNG_READ_gAMA_SUPPORTED
366
396
PNG_CONST PNG_gAMA;
368
#if defined(PNG_READ_hIST_SUPPORTED)
398
#ifdef PNG_READ_hIST_SUPPORTED
369
399
PNG_CONST PNG_hIST;
371
#if defined(PNG_READ_iCCP_SUPPORTED)
401
#ifdef PNG_READ_iCCP_SUPPORTED
372
402
PNG_CONST PNG_iCCP;
374
#if defined(PNG_READ_iTXt_SUPPORTED)
404
#ifdef PNG_READ_iTXt_SUPPORTED
375
405
PNG_CONST PNG_iTXt;
377
#if defined(PNG_READ_oFFs_SUPPORTED)
407
#ifdef PNG_READ_oFFs_SUPPORTED
378
408
PNG_CONST PNG_oFFs;
380
#if defined(PNG_READ_pCAL_SUPPORTED)
410
#ifdef PNG_READ_pCAL_SUPPORTED
381
411
PNG_CONST PNG_pCAL;
383
#if defined(PNG_READ_pHYs_SUPPORTED)
413
#ifdef PNG_READ_pHYs_SUPPORTED
384
414
PNG_CONST PNG_pHYs;
386
#if defined(PNG_READ_sBIT_SUPPORTED)
416
#ifdef PNG_READ_sBIT_SUPPORTED
387
417
PNG_CONST PNG_sBIT;
389
#if defined(PNG_READ_sCAL_SUPPORTED)
419
#ifdef PNG_READ_sCAL_SUPPORTED
390
420
PNG_CONST PNG_sCAL;
392
#if defined(PNG_READ_sPLT_SUPPORTED)
422
#ifdef PNG_READ_sPLT_SUPPORTED
393
423
PNG_CONST PNG_sPLT;
395
#if defined(PNG_READ_sRGB_SUPPORTED)
425
#ifdef PNG_READ_sRGB_SUPPORTED
396
426
PNG_CONST PNG_sRGB;
398
#if defined(PNG_READ_tEXt_SUPPORTED)
428
#ifdef PNG_READ_tEXt_SUPPORTED
399
429
PNG_CONST PNG_tEXt;
401
#if defined(PNG_READ_tIME_SUPPORTED)
431
#ifdef PNG_READ_tIME_SUPPORTED
402
432
PNG_CONST PNG_tIME;
404
#if defined(PNG_READ_tRNS_SUPPORTED)
434
#ifdef PNG_READ_tRNS_SUPPORTED
405
435
PNG_CONST PNG_tRNS;
407
#if defined(PNG_READ_zTXt_SUPPORTED)
437
#ifdef PNG_READ_zTXt_SUPPORTED
408
438
PNG_CONST PNG_zTXt;
410
440
#endif /* PNG_USE_LOCAL_ARRAYS */
455
485
png_ptr->mode |= PNG_HAVE_IDAT;
458
#if defined(PNG_READ_bKGD_SUPPORTED)
488
#ifdef PNG_READ_bKGD_SUPPORTED
459
489
else if (!png_memcmp(chunk_name, png_bKGD, 4))
460
490
png_handle_bKGD(png_ptr, info_ptr, length);
462
#if defined(PNG_READ_cHRM_SUPPORTED)
492
#ifdef PNG_READ_cHRM_SUPPORTED
463
493
else if (!png_memcmp(chunk_name, png_cHRM, 4))
464
494
png_handle_cHRM(png_ptr, info_ptr, length);
466
#if defined(PNG_READ_gAMA_SUPPORTED)
496
#ifdef PNG_READ_gAMA_SUPPORTED
467
497
else if (!png_memcmp(chunk_name, png_gAMA, 4))
468
498
png_handle_gAMA(png_ptr, info_ptr, length);
470
#if defined(PNG_READ_hIST_SUPPORTED)
500
#ifdef PNG_READ_hIST_SUPPORTED
471
501
else if (!png_memcmp(chunk_name, png_hIST, 4))
472
502
png_handle_hIST(png_ptr, info_ptr, length);
474
#if defined(PNG_READ_oFFs_SUPPORTED)
504
#ifdef PNG_READ_oFFs_SUPPORTED
475
505
else if (!png_memcmp(chunk_name, png_oFFs, 4))
476
506
png_handle_oFFs(png_ptr, info_ptr, length);
478
#if defined(PNG_READ_pCAL_SUPPORTED)
508
#ifdef PNG_READ_pCAL_SUPPORTED
479
509
else if (!png_memcmp(chunk_name, png_pCAL, 4))
480
510
png_handle_pCAL(png_ptr, info_ptr, length);
482
#if defined(PNG_READ_sCAL_SUPPORTED)
512
#ifdef PNG_READ_sCAL_SUPPORTED
483
513
else if (!png_memcmp(chunk_name, png_sCAL, 4))
484
514
png_handle_sCAL(png_ptr, info_ptr, length);
486
#if defined(PNG_READ_pHYs_SUPPORTED)
516
#ifdef PNG_READ_pHYs_SUPPORTED
487
517
else if (!png_memcmp(chunk_name, png_pHYs, 4))
488
518
png_handle_pHYs(png_ptr, info_ptr, length);
490
#if defined(PNG_READ_sBIT_SUPPORTED)
520
#ifdef PNG_READ_sBIT_SUPPORTED
491
521
else if (!png_memcmp(chunk_name, png_sBIT, 4))
492
522
png_handle_sBIT(png_ptr, info_ptr, length);
494
#if defined(PNG_READ_sRGB_SUPPORTED)
524
#ifdef PNG_READ_sRGB_SUPPORTED
495
525
else if (!png_memcmp(chunk_name, png_sRGB, 4))
496
526
png_handle_sRGB(png_ptr, info_ptr, length);
498
#if defined(PNG_READ_iCCP_SUPPORTED)
528
#ifdef PNG_READ_iCCP_SUPPORTED
499
529
else if (!png_memcmp(chunk_name, png_iCCP, 4))
500
530
png_handle_iCCP(png_ptr, info_ptr, length);
502
#if defined(PNG_READ_sPLT_SUPPORTED)
532
#ifdef PNG_READ_sPLT_SUPPORTED
503
533
else if (!png_memcmp(chunk_name, png_sPLT, 4))
504
534
png_handle_sPLT(png_ptr, info_ptr, length);
506
#if defined(PNG_READ_tEXt_SUPPORTED)
536
#ifdef PNG_READ_tEXt_SUPPORTED
507
537
else if (!png_memcmp(chunk_name, png_tEXt, 4))
508
538
png_handle_tEXt(png_ptr, info_ptr, length);
510
#if defined(PNG_READ_tIME_SUPPORTED)
540
#ifdef PNG_READ_tIME_SUPPORTED
511
541
else if (!png_memcmp(chunk_name, png_tIME, 4))
512
542
png_handle_tIME(png_ptr, info_ptr, length);
514
#if defined(PNG_READ_tRNS_SUPPORTED)
544
#ifdef PNG_READ_tRNS_SUPPORTED
515
545
else if (!png_memcmp(chunk_name, png_tRNS, 4))
516
546
png_handle_tRNS(png_ptr, info_ptr, length);
518
#if defined(PNG_READ_zTXt_SUPPORTED)
548
#ifdef PNG_READ_zTXt_SUPPORTED
519
549
else if (!png_memcmp(chunk_name, png_zTXt, 4))
520
550
png_handle_zTXt(png_ptr, info_ptr, length);
522
#if defined(PNG_READ_iTXt_SUPPORTED)
552
#ifdef PNG_READ_iTXt_SUPPORTED
523
553
else if (!png_memcmp(chunk_name, png_iTXt, 4))
524
554
png_handle_iTXt(png_ptr, info_ptr, length);
527
557
png_handle_unknown(png_ptr, info_ptr, length);
530
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
560
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
532
/* optional call to update the users info_ptr structure */
562
/* Optional call to update the users info_ptr structure */
534
564
png_read_update_info(png_structp png_ptr, png_infop info_ptr)
536
566
png_debug(1, "in png_read_update_info");
537
if (png_ptr == NULL) return;
538
570
if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
539
571
png_read_start_row(png_ptr);
541
573
png_warning(png_ptr,
542
574
"Ignoring extra png_read_update_info() call; row buffer not reallocated");
543
576
png_read_transform_info(png_ptr, info_ptr);
546
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
579
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
547
580
/* Initialize palette, background, etc, after transformations
548
581
* are set, but before any reading takes place. This allows
549
582
* the user to obtain a gamma-corrected palette, for example.
553
586
png_start_read_image(png_structp png_ptr)
555
588
png_debug(1, "in png_start_read_image");
556
if (png_ptr == NULL) return;
557
592
if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
558
593
png_read_start_row(png_ptr);
560
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
595
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
562
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
597
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
564
599
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
566
#ifdef PNG_USE_LOCAL_ARRAYS
567
601
PNG_CONST PNG_IDAT;
568
602
PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
570
604
PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
573
if (png_ptr == NULL) return;
574
610
png_debug2(1, "in png_read_row (row %lu, pass %d)",
575
611
png_ptr->row_number, png_ptr->pass);
576
613
if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
577
614
png_read_start_row(png_ptr);
578
615
if (png_ptr->row_number == 0 && png_ptr->pass == 0)
580
/* check for transforms that have been set but were defined out */
617
/* Check for transforms that have been set but were defined out */
581
618
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
582
619
if (png_ptr->transformations & PNG_INVERT_MONO)
583
620
png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
755
795
if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
756
796
png_do_read_transformations(png_ptr);
758
#if defined(PNG_READ_INTERLACING_SUPPORTED)
759
/* blow up interlaced rows to full size */
798
#ifdef PNG_READ_INTERLACING_SUPPORTED
799
/* Blow up interlaced rows to full size */
760
800
if (png_ptr->interlaced &&
761
801
(png_ptr->transformations & PNG_INTERLACE))
763
803
if (png_ptr->pass < 6)
764
/* old interface (pre-1.0.9):
765
png_do_read_interlace(&(png_ptr->row_info),
766
png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
804
/* Old interface (pre-1.0.9):
805
* png_do_read_interlace(&(png_ptr->row_info),
806
* png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
768
808
png_do_read_interlace(png_ptr);
770
810
if (dsp_row != NULL)
918
964
PNG_CONST PNG_IDAT;
919
965
PNG_CONST PNG_IEND;
920
966
PNG_CONST PNG_PLTE;
921
#if defined(PNG_READ_bKGD_SUPPORTED)
967
#ifdef PNG_READ_bKGD_SUPPORTED
922
968
PNG_CONST PNG_bKGD;
924
#if defined(PNG_READ_cHRM_SUPPORTED)
970
#ifdef PNG_READ_cHRM_SUPPORTED
925
971
PNG_CONST PNG_cHRM;
927
#if defined(PNG_READ_gAMA_SUPPORTED)
973
#ifdef PNG_READ_gAMA_SUPPORTED
928
974
PNG_CONST PNG_gAMA;
930
#if defined(PNG_READ_hIST_SUPPORTED)
976
#ifdef PNG_READ_hIST_SUPPORTED
931
977
PNG_CONST PNG_hIST;
933
#if defined(PNG_READ_iCCP_SUPPORTED)
979
#ifdef PNG_READ_iCCP_SUPPORTED
934
980
PNG_CONST PNG_iCCP;
936
#if defined(PNG_READ_iTXt_SUPPORTED)
982
#ifdef PNG_READ_iTXt_SUPPORTED
937
983
PNG_CONST PNG_iTXt;
939
#if defined(PNG_READ_oFFs_SUPPORTED)
985
#ifdef PNG_READ_oFFs_SUPPORTED
940
986
PNG_CONST PNG_oFFs;
942
#if defined(PNG_READ_pCAL_SUPPORTED)
988
#ifdef PNG_READ_pCAL_SUPPORTED
943
989
PNG_CONST PNG_pCAL;
945
#if defined(PNG_READ_pHYs_SUPPORTED)
991
#ifdef PNG_READ_pHYs_SUPPORTED
946
992
PNG_CONST PNG_pHYs;
948
#if defined(PNG_READ_sBIT_SUPPORTED)
994
#ifdef PNG_READ_sBIT_SUPPORTED
949
995
PNG_CONST PNG_sBIT;
951
#if defined(PNG_READ_sCAL_SUPPORTED)
997
#ifdef PNG_READ_sCAL_SUPPORTED
952
998
PNG_CONST PNG_sCAL;
954
#if defined(PNG_READ_sPLT_SUPPORTED)
1000
#ifdef PNG_READ_sPLT_SUPPORTED
955
1001
PNG_CONST PNG_sPLT;
957
#if defined(PNG_READ_sRGB_SUPPORTED)
1003
#ifdef PNG_READ_sRGB_SUPPORTED
958
1004
PNG_CONST PNG_sRGB;
960
#if defined(PNG_READ_tEXt_SUPPORTED)
1006
#ifdef PNG_READ_tEXt_SUPPORTED
961
1007
PNG_CONST PNG_tEXt;
963
#if defined(PNG_READ_tIME_SUPPORTED)
1009
#ifdef PNG_READ_tIME_SUPPORTED
964
1010
PNG_CONST PNG_tIME;
966
#if defined(PNG_READ_tRNS_SUPPORTED)
1012
#ifdef PNG_READ_tRNS_SUPPORTED
967
1013
PNG_CONST PNG_tRNS;
969
#if defined(PNG_READ_zTXt_SUPPORTED)
1015
#ifdef PNG_READ_zTXt_SUPPORTED
970
1016
PNG_CONST PNG_zTXt;
972
1018
#endif /* PNG_USE_LOCAL_ARRAYS */
1002
1048
else if (!png_memcmp(chunk_name, png_PLTE, 4))
1003
1049
png_handle_PLTE(png_ptr, info_ptr, length);
1004
#if defined(PNG_READ_bKGD_SUPPORTED)
1050
#ifdef PNG_READ_bKGD_SUPPORTED
1005
1051
else if (!png_memcmp(chunk_name, png_bKGD, 4))
1006
1052
png_handle_bKGD(png_ptr, info_ptr, length);
1008
#if defined(PNG_READ_cHRM_SUPPORTED)
1054
#ifdef PNG_READ_cHRM_SUPPORTED
1009
1055
else if (!png_memcmp(chunk_name, png_cHRM, 4))
1010
1056
png_handle_cHRM(png_ptr, info_ptr, length);
1012
#if defined(PNG_READ_gAMA_SUPPORTED)
1058
#ifdef PNG_READ_gAMA_SUPPORTED
1013
1059
else if (!png_memcmp(chunk_name, png_gAMA, 4))
1014
1060
png_handle_gAMA(png_ptr, info_ptr, length);
1016
#if defined(PNG_READ_hIST_SUPPORTED)
1062
#ifdef PNG_READ_hIST_SUPPORTED
1017
1063
else if (!png_memcmp(chunk_name, png_hIST, 4))
1018
1064
png_handle_hIST(png_ptr, info_ptr, length);
1020
#if defined(PNG_READ_oFFs_SUPPORTED)
1066
#ifdef PNG_READ_oFFs_SUPPORTED
1021
1067
else if (!png_memcmp(chunk_name, png_oFFs, 4))
1022
1068
png_handle_oFFs(png_ptr, info_ptr, length);
1024
#if defined(PNG_READ_pCAL_SUPPORTED)
1070
#ifdef PNG_READ_pCAL_SUPPORTED
1025
1071
else if (!png_memcmp(chunk_name, png_pCAL, 4))
1026
1072
png_handle_pCAL(png_ptr, info_ptr, length);
1028
#if defined(PNG_READ_sCAL_SUPPORTED)
1074
#ifdef PNG_READ_sCAL_SUPPORTED
1029
1075
else if (!png_memcmp(chunk_name, png_sCAL, 4))
1030
1076
png_handle_sCAL(png_ptr, info_ptr, length);
1032
#if defined(PNG_READ_pHYs_SUPPORTED)
1078
#ifdef PNG_READ_pHYs_SUPPORTED
1033
1079
else if (!png_memcmp(chunk_name, png_pHYs, 4))
1034
1080
png_handle_pHYs(png_ptr, info_ptr, length);
1036
#if defined(PNG_READ_sBIT_SUPPORTED)
1082
#ifdef PNG_READ_sBIT_SUPPORTED
1037
1083
else if (!png_memcmp(chunk_name, png_sBIT, 4))
1038
1084
png_handle_sBIT(png_ptr, info_ptr, length);
1040
#if defined(PNG_READ_sRGB_SUPPORTED)
1086
#ifdef PNG_READ_sRGB_SUPPORTED
1041
1087
else if (!png_memcmp(chunk_name, png_sRGB, 4))
1042
1088
png_handle_sRGB(png_ptr, info_ptr, length);
1044
#if defined(PNG_READ_iCCP_SUPPORTED)
1090
#ifdef PNG_READ_iCCP_SUPPORTED
1045
1091
else if (!png_memcmp(chunk_name, png_iCCP, 4))
1046
1092
png_handle_iCCP(png_ptr, info_ptr, length);
1048
#if defined(PNG_READ_sPLT_SUPPORTED)
1094
#ifdef PNG_READ_sPLT_SUPPORTED
1049
1095
else if (!png_memcmp(chunk_name, png_sPLT, 4))
1050
1096
png_handle_sPLT(png_ptr, info_ptr, length);
1052
#if defined(PNG_READ_tEXt_SUPPORTED)
1098
#ifdef PNG_READ_tEXt_SUPPORTED
1053
1099
else if (!png_memcmp(chunk_name, png_tEXt, 4))
1054
1100
png_handle_tEXt(png_ptr, info_ptr, length);
1056
#if defined(PNG_READ_tIME_SUPPORTED)
1102
#ifdef PNG_READ_tIME_SUPPORTED
1057
1103
else if (!png_memcmp(chunk_name, png_tIME, 4))
1058
1104
png_handle_tIME(png_ptr, info_ptr, length);
1060
#if defined(PNG_READ_tRNS_SUPPORTED)
1106
#ifdef PNG_READ_tRNS_SUPPORTED
1061
1107
else if (!png_memcmp(chunk_name, png_tRNS, 4))
1062
1108
png_handle_tRNS(png_ptr, info_ptr, length);
1064
#if defined(PNG_READ_zTXt_SUPPORTED)
1110
#ifdef PNG_READ_zTXt_SUPPORTED
1065
1111
else if (!png_memcmp(chunk_name, png_zTXt, 4))
1066
1112
png_handle_zTXt(png_ptr, info_ptr, length);
1068
#if defined(PNG_READ_iTXt_SUPPORTED)
1114
#ifdef PNG_READ_iTXt_SUPPORTED
1069
1115
else if (!png_memcmp(chunk_name, png_iTXt, 4))
1070
1116
png_handle_iTXt(png_ptr, info_ptr, length);
1172
1221
png_free(png_ptr, png_ptr->big_row_buf);
1173
1222
png_free(png_ptr, png_ptr->prev_row);
1174
1223
png_free(png_ptr, png_ptr->chunkdata);
1175
#if defined(PNG_READ_DITHER_SUPPORTED)
1224
#ifdef PNG_READ_DITHER_SUPPORTED
1176
1225
png_free(png_ptr, png_ptr->palette_lookup);
1177
1226
png_free(png_ptr, png_ptr->dither_index);
1179
#if defined(PNG_READ_GAMMA_SUPPORTED)
1228
#ifdef PNG_READ_GAMMA_SUPPORTED
1180
1229
png_free(png_ptr, png_ptr->gamma_table);
1182
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1231
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1183
1232
png_free(png_ptr, png_ptr->gamma_from_1);
1184
1233
png_free(png_ptr, png_ptr->gamma_to_1);
1347
1398
* byte into separate bytes (useful for paletted and grayscale images).
1349
1400
if (transforms & PNG_TRANSFORM_PACKING)
1350
png_set_packing(png_ptr);
1401
png_set_packing(png_ptr);
1353
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
1404
#ifdef PNG_READ_PACKSWAP_SUPPORTED
1354
1405
/* Change the order of packed pixels to least significant bit first
1355
1406
* (not useful if you are using png_set_packing).
1357
1408
if (transforms & PNG_TRANSFORM_PACKSWAP)
1358
png_set_packswap(png_ptr);
1409
png_set_packswap(png_ptr);
1361
#if defined(PNG_READ_EXPAND_SUPPORTED)
1412
#ifdef PNG_READ_EXPAND_SUPPORTED
1362
1413
/* Expand paletted colors into true RGB triplets
1363
1414
* Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1364
1415
* Expand paletted or RGB images with transparency to full alpha
1365
1416
* channels so the data will be available as RGBA quartets.
1367
1418
if (transforms & PNG_TRANSFORM_EXPAND)
1368
if ((png_ptr->bit_depth < 8) ||
1369
(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1370
(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1419
if ((png_ptr->bit_depth < 8) ||
1420
(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1421
(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1371
1422
png_set_expand(png_ptr);
1374
1425
/* We don't handle background color or gamma transformation or dithering.
1377
#if defined(PNG_READ_INVERT_SUPPORTED)
1378
/* invert monochrome files to have 0 as white and 1 as black
1428
#ifdef PNG_READ_INVERT_SUPPORTED
1429
/* Invert monochrome files to have 0 as white and 1 as black
1380
1431
if (transforms & PNG_TRANSFORM_INVERT_MONO)
1381
png_set_invert_mono(png_ptr);
1432
png_set_invert_mono(png_ptr);
1384
#if defined(PNG_READ_SHIFT_SUPPORTED)
1435
#ifdef PNG_READ_SHIFT_SUPPORTED
1385
1436
/* If you want to shift the pixel values from the range [0,255] or
1386
1437
* [0,65535] to the original [0,7] or [0,31], or whatever range the
1387
1438
* colors were originally in:
1399
#if defined(PNG_READ_BGR_SUPPORTED)
1400
/* flip the RGB pixels to BGR (or RGBA to BGRA)
1450
#ifdef PNG_READ_BGR_SUPPORTED
1451
/* Flip the RGB pixels to BGR (or RGBA to BGRA)
1402
1453
if (transforms & PNG_TRANSFORM_BGR)
1403
png_set_bgr(png_ptr);
1454
png_set_bgr(png_ptr);
1406
#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1407
/* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1457
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1458
/* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1409
1460
if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1410
1461
png_set_swap_alpha(png_ptr);
1413
#if defined(PNG_READ_SWAP_SUPPORTED)
1414
/* swap bytes of 16 bit files to least significant byte first
1464
#ifdef PNG_READ_SWAP_SUPPORTED
1465
/* Swap bytes of 16 bit files to least significant byte first
1416
1467
if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1417
png_set_swap(png_ptr);
1468
png_set_swap(png_ptr);
1471
/* Added at libpng-1.2.41 */
1472
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1473
/* Invert the alpha channel from opacity to transparency
1475
if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1476
png_set_invert_alpha(png_ptr);
1479
/* Added at libpng-1.2.41 */
1480
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1481
/* Expand grayscale image to RGB
1483
if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1484
png_set_gray_to_rgb(png_ptr);
1420
1487
/* We don't handle adding filler bytes */