2
2
/* pngrtran.c - transforms the data in a row for PNG readers
4
* Last changed in libpng 1.2.38 [July 16, 2009]
4
* Last changed in libpng 1.2.41 [December 3, 2009]
5
5
* Copyright (c) 1998-2009 Glenn Randers-Pehrson
6
6
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7
7
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
19
19
#define PNG_INTERNAL
20
#define PNG_NO_PEDANTIC_WARNINGS
21
#if defined(PNG_READ_SUPPORTED)
22
#ifdef PNG_READ_SUPPORTED
23
24
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
25
26
png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
27
28
png_debug(1, "in png_set_crc_action");
28
33
/* Tell libpng how we react to CRC errors in critical chunks */
31
34
switch (crit_action)
33
36
case PNG_CRC_NO_CHANGE: /* Leave setting as is */
114
#if defined(PNG_READ_16_TO_8_SUPPORTED)
119
#ifdef PNG_READ_16_TO_8_SUPPORTED
115
120
/* Strip 16 bit depth files to 8 bit depth */
117
122
png_set_strip_16(png_structp png_ptr)
119
124
png_debug(1, "in png_set_strip_16");
120
126
if (png_ptr == NULL)
122
128
png_ptr->transformations |= PNG_16_TO_8;
126
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
132
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
128
134
png_set_strip_alpha(png_structp png_ptr)
130
136
png_debug(1, "in png_set_strip_alpha");
131
138
if (png_ptr == NULL)
133
140
png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
137
#if defined(PNG_READ_DITHER_SUPPORTED)
144
#ifdef PNG_READ_DITHER_SUPPORTED
138
145
/* Dither file to 8 bit. Supply a palette, the current number
139
146
* of elements in the palette, the maximum number of elements
140
147
* allowed, and a histogram if possible. If the current number
328
336
png_ptr->palette_to_index[i] = (png_byte)i;
331
hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
339
hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
332
340
png_sizeof(png_dsortp)));
333
png_memset(hash, 0, 769 * png_sizeof(png_dsortp));
335
342
num_new_palette = num_palette;
476
483
int num_green = (1 << PNG_DITHER_GREEN_BITS);
477
484
int num_blue = (1 << PNG_DITHER_BLUE_BITS);
478
485
png_size_t num_entries = ((png_size_t)1 << total_bits);
479
png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
487
png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr,
480
488
(png_uint_32)(num_entries * png_sizeof(png_byte)));
481
png_memset(png_ptr->palette_lookup, 0, num_entries *
482
png_sizeof(png_byte));
484
490
distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
485
491
png_sizeof(png_byte)));
487
492
png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
489
494
for (i = 0; i < num_palette; i++)
583
592
* More to the point, these functions make it obvious what libpng will be
584
593
* doing, whereas "expand" can (and does) mean any number of things.
586
* GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified
587
* to expand only the sample depth but not to expand the tRNS to alpha.
595
* GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
596
* to expand only the sample depth but not to expand the tRNS to alpha
597
* and its name was changed to png_set_expand_gray_1_2_4_to_8().
590
600
/* Expand paletted images to RGB. */
592
602
png_set_palette_to_rgb(png_structp png_ptr)
594
604
png_debug(1, "in png_set_palette_to_rgb");
595
606
if (png_ptr == NULL)
597
609
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
598
610
png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
601
#if !defined(PNG_1_0_X)
602
614
/* Expand grayscale images of less than 8-bit depth to 8 bits. */
604
616
png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
606
618
png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
607
620
if (png_ptr == NULL)
609
623
png_ptr->transformations |= PNG_EXPAND;
610
624
png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
630
646
png_set_tRNS_to_alpha(png_structp png_ptr)
632
648
png_debug(1, "in png_set_tRNS_to_alpha");
633
650
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
634
651
png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
636
653
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
638
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
655
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
640
657
png_set_gray_to_rgb(png_structp png_ptr)
642
659
png_debug(1, "in png_set_gray_to_rgb");
643
661
png_ptr->transformations |= PNG_GRAY_TO_RGB;
644
662
png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
648
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
649
#if defined(PNG_FLOATING_POINT_SUPPORTED)
666
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
667
#ifdef PNG_FLOATING_POINT_SUPPORTED
650
668
/* Convert a RGB image to a grayscale of the same width. This allows us,
651
669
* for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
843
866
png_ptr->background.blue =
844
867
png_ptr->palette[png_ptr->background.index].blue;
846
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
869
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
847
870
if (png_ptr->transformations & PNG_INVERT_ALPHA)
849
#if defined(PNG_READ_EXPAND_SUPPORTED)
872
#ifdef PNG_READ_EXPAND_SUPPORTED
850
873
if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
990
1013
palette[i].blue = png_ptr->gamma_table[palette[i].blue];
993
/* Prevent the transformations being done again, and make sure
994
* that the now spurious alpha channel is stripped - the code
995
* has just reduced background composition and gamma correction
996
* to a simple alpha channel strip.
998
png_ptr->transformations &= ~PNG_BACKGROUND;
999
png_ptr->transformations &= ~PNG_GAMMA;
1000
png_ptr->transformations |= PNG_STRIP_ALPHA;
1016
/* Prevent the transformations being done again, and make sure
1017
* that the now spurious alpha channel is stripped - the code
1018
* has just reduced background composition and gamma correction
1019
* to a simple alpha channel strip.
1021
png_ptr->transformations &= ~PNG_BACKGROUND;
1022
png_ptr->transformations &= ~PNG_GAMMA;
1023
png_ptr->transformations |= PNG_STRIP_ALPHA;
1002
1025
/* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1075
1098
palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1078
/* Done the gamma correction. */
1079
png_ptr->transformations &= ~PNG_GAMMA;
1101
/* Done the gamma correction. */
1102
png_ptr->transformations &= ~PNG_GAMMA;
1082
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1105
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1085
1108
#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
1086
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1109
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1087
1110
/* No GAMMA transformation */
1088
1111
if ((png_ptr->transformations & PNG_BACKGROUND) &&
1089
1112
(color_type == PNG_COLOR_TYPE_PALETTE))
1212
#if defined(PNG_READ_16_TO_8_SUPPORTED)
1236
#ifdef PNG_READ_16_TO_8_SUPPORTED
1213
1237
if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1214
1238
info_ptr->bit_depth = 8;
1217
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1241
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1218
1242
if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1219
1243
info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1222
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
1246
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1223
1247
if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1224
1248
info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1227
#if defined(PNG_READ_DITHER_SUPPORTED)
1251
#ifdef PNG_READ_DITHER_SUPPORTED
1228
1252
if (png_ptr->transformations & PNG_DITHER)
1230
1254
if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1351
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1376
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1352
1377
if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1353
1378
png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1354
1379
PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
1357
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
1382
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1358
1383
if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1360
1385
int rgb_error =
1412
1437
png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1415
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1440
#ifdef PNG_READ_16_TO_8_SUPPORTED
1441
if (png_ptr->transformations & PNG_16_TO_8)
1442
png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1445
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1416
1446
if ((png_ptr->transformations & PNG_BACKGROUND) &&
1417
1447
((png_ptr->num_trans != 0 ) ||
1418
1448
(png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1419
1449
png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1420
1450
&(png_ptr->trans_values), &(png_ptr->background)
1421
#if defined(PNG_READ_GAMMA_SUPPORTED)
1451
#ifdef PNG_READ_GAMMA_SUPPORTED
1422
1452
, &(png_ptr->background_1),
1423
1453
png_ptr->gamma_table, png_ptr->gamma_from_1,
1424
1454
png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1431
#if defined(PNG_READ_GAMMA_SUPPORTED)
1461
#ifdef PNG_READ_GAMMA_SUPPORTED
1432
1462
if ((png_ptr->transformations & PNG_GAMMA) &&
1433
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1463
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1434
1464
!((png_ptr->transformations & PNG_BACKGROUND) &&
1435
1465
((png_ptr->num_trans != 0) ||
1436
1466
(png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1441
1471
png_ptr->gamma_shift);
1444
#if defined(PNG_READ_16_TO_8_SUPPORTED)
1445
if (png_ptr->transformations & PNG_16_TO_8)
1446
png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1449
#if defined(PNG_READ_DITHER_SUPPORTED)
1474
#ifdef PNG_READ_DITHER_SUPPORTED
1450
1475
if (png_ptr->transformations & PNG_DITHER)
1452
1477
png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
1459
#if defined(PNG_READ_INVERT_SUPPORTED)
1484
#ifdef PNG_READ_INVERT_SUPPORTED
1460
1485
if (png_ptr->transformations & PNG_INVERT_MONO)
1461
1486
png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
1464
#if defined(PNG_READ_SHIFT_SUPPORTED)
1489
#ifdef PNG_READ_SHIFT_SUPPORTED
1465
1490
if (png_ptr->transformations & PNG_SHIFT)
1466
1491
png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
1467
1492
&(png_ptr->shift));
1470
#if defined(PNG_READ_PACK_SUPPORTED)
1495
#ifdef PNG_READ_PACK_SUPPORTED
1471
1496
if (png_ptr->transformations & PNG_PACK)
1472
1497
png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
1475
#if defined(PNG_READ_BGR_SUPPORTED)
1500
#ifdef PNG_READ_BGR_SUPPORTED
1476
1501
if (png_ptr->transformations & PNG_BGR)
1477
1502
png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1480
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
1505
#ifdef PNG_READ_PACKSWAP_SUPPORTED
1481
1506
if (png_ptr->transformations & PNG_PACKSWAP)
1482
1507
png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1485
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1510
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1486
1511
/* If gray -> RGB, do so now only if we did not do so above */
1487
1512
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1488
1513
(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1489
1514
png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1492
#if defined(PNG_READ_FILLER_SUPPORTED)
1517
#ifdef PNG_READ_FILLER_SUPPORTED
1493
1518
if (png_ptr->transformations & PNG_FILLER)
1494
1519
png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1495
1520
(png_uint_32)png_ptr->filler, png_ptr->flags);
1498
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1523
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1499
1524
if (png_ptr->transformations & PNG_INVERT_ALPHA)
1500
1525
png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1503
#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1528
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1504
1529
if (png_ptr->transformations & PNG_SWAP_ALPHA)
1505
1530
png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1508
#if defined(PNG_READ_SWAP_SUPPORTED)
1533
#ifdef PNG_READ_SWAP_SUPPORTED
1509
1534
if (png_ptr->transformations & PNG_SWAP_BYTES)
1510
1535
png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1513
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1538
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1514
1539
if (png_ptr->transformations & PNG_USER_TRANSFORM)
1516
1541
if (png_ptr->read_user_transform_fn != NULL)
1524
1549
/* png_byte channels; number of channels (1-4) */
1525
1550
/* png_byte pixel_depth; bits per pixel (depth*channels) */
1526
1551
png_ptr->row_buf + 1); /* start of pixel data for row */
1527
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
1552
#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1528
1553
if (png_ptr->user_transform_depth)
1529
1554
png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1530
1555
if (png_ptr->user_transform_channels)
1642
1668
png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
1644
1670
png_debug(1, "in png_do_unshift");
1646
#if defined(PNG_USELESS_TESTS_SUPPORTED)
1673
#ifdef PNG_USELESS_TESTS_SUPPORTED
1647
1674
row != NULL && row_info != NULL && sig_bits != NULL &&
1649
1676
row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1748
#if defined(PNG_READ_16_TO_8_SUPPORTED)
1775
#ifdef PNG_READ_16_TO_8_SUPPORTED
1749
1776
/* Chop rows of bit depth 16 down to 8 */
1750
1777
void /* PRIVATE */
1751
1778
png_do_chop(png_row_infop row_info, png_bytep row)
1753
1780
png_debug(1, "in png_do_chop");
1754
#if defined(PNG_USELESS_TESTS_SUPPORTED)
1782
#ifdef PNG_USELESS_TESTS_SUPPORTED
1755
1783
if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
1757
1785
if (row_info->bit_depth == 16)
1806
#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1834
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1807
1835
void /* PRIVATE */
1808
1836
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
1810
1838
png_debug(1, "in png_do_read_swap_alpha");
1811
#if defined(PNG_USELESS_TESTS_SUPPORTED)
1840
#ifdef PNG_USELESS_TESTS_SUPPORTED
1812
1841
if (row != NULL && row_info != NULL)
1898
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1927
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1899
1928
void /* PRIVATE */
1900
1929
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
1902
1931
png_debug(1, "in png_do_read_invert_alpha");
1903
#if defined(PNG_USELESS_TESTS_SUPPORTED)
1933
#ifdef PNG_USELESS_TESTS_SUPPORTED
1904
1934
if (row != NULL && row_info != NULL)
2751
2786
void /* PRIVATE */
2752
2787
png_do_background(png_row_infop row_info, png_bytep row,
2753
2788
png_color_16p trans_values, png_color_16p background
2754
#if defined(PNG_READ_GAMMA_SUPPORTED)
2789
#ifdef PNG_READ_GAMMA_SUPPORTED
2755
2790
, png_color_16p background_1,
2756
2791
png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
2757
2792
png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
4083
4123
#ifdef PNG_FLOATING_POINT_SUPPORTED
4084
#if defined(PNG_READ_GAMMA_SUPPORTED)
4124
#ifdef PNG_READ_GAMMA_SUPPORTED
4085
4125
static PNG_CONST int png_gamma_shift[] =
4086
4126
{0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
4089
4129
* tables, we don't make a full table if we are reducing to 8-bit in
4090
4130
* the future. Note also how the gamma_16 tables are segmented so that
4091
4131
* we don't need to allocate > 64K chunks for a full 16-bit table.
4133
* See the PNG extensions document for an integer algorithm for creating
4134
* the gamma tables. Maybe we will implement that here someday.
4136
* We should only reach this point if
4138
* the file_gamma is known (i.e., the gAMA or sRGB chunk is present,
4139
* or the application has provided a file_gamma)
4143
* the screen_gamma is known
4146
* RGB_to_gray transformation is being performed
4151
* the screen_gamma is different from the reciprocal of the
4152
* file_gamma by more than the specified threshold
4156
* a background color has been specified and the file_gamma
4157
* and screen_gamma are not 1.0, within the specified threshold.
4093
4161
void /* PRIVATE */
4094
4162
png_build_gamma_table(png_structp png_ptr)
4262
4329
g = 1.0 / (png_ptr->gamma);
4264
png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
4331
png_ptr->gamma_16_to_1 = (png_uint_16pp)png_calloc(png_ptr,
4265
4332
(png_uint_32)(num * png_sizeof(png_uint_16p )));
4266
png_memset(png_ptr->gamma_16_to_1, 0, num * png_sizeof(png_uint_16p));
4268
4334
for (i = 0; i < num; i++)
4287
4353
g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4289
png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
4355
png_ptr->gamma_16_from_1 = (png_uint_16pp)png_calloc(png_ptr,
4290
4356
(png_uint_32)(num * png_sizeof(png_uint_16p)));
4291
png_memset(png_ptr->gamma_16_from_1, 0,
4292
num * png_sizeof(png_uint_16p));
4294
4358
for (i = 0; i < num; i++)
4314
4378
/* To do: install integer version of png_build_gamma_table here */
4317
#if defined(PNG_MNG_FEATURES_SUPPORTED)
4381
#ifdef PNG_MNG_FEATURES_SUPPORTED
4318
4382
/* Undoes intrapixel differencing */
4319
4383
void /* PRIVATE */
4320
4384
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4322
4386
png_debug(1, "in png_do_read_intrapixel");
4324
#if defined(PNG_USELESS_TESTS_SUPPORTED)
4389
#ifdef PNG_USELESS_TESTS_SUPPORTED
4325
4390
row != NULL && row_info != NULL &&
4327
4392
(row_info->color_type & PNG_COLOR_MASK_COLOR))