~noskcaj/ubuntu/trusty/libpng/libpng

« back to all changes in this revision

Viewing changes to pngrtran.c

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2009-12-04 11:23:50 UTC
  • mfrom: (1.1.10 upstream) (15.2.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091204112350-ln22xyakgn9bh39k
Tags: 1.2.41-1
* New upstream release
* Debian source format is 3.0 (quilt)
* Update debian/watch
* Add 02-export-png_set_strip_error_numbers.patch
  Define PNG_ERROR_NUMBERS_SUPPORTED
  Upstream doesn't define PNG_ERROR_NUMBERS_SUPPORTED since 1.2.41. As
  a consecuence, the symbol png_set_strip_error_numbe@@PNG12_0 wasn't
  exported.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* pngrtran.c - transforms the data in a row for PNG readers
3
3
 *
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.)
17
17
 */
18
18
 
19
19
#define PNG_INTERNAL
 
20
#define PNG_NO_PEDANTIC_WARNINGS
20
21
#include "png.h"
21
 
#if defined(PNG_READ_SUPPORTED)
 
22
#ifdef PNG_READ_SUPPORTED
22
23
 
23
24
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
24
25
void PNGAPI
25
26
png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
26
27
{
27
28
   png_debug(1, "in png_set_crc_action");
 
29
 
 
30
   if (png_ptr == NULL)
 
31
      return;
 
32
 
28
33
   /* Tell libpng how we react to CRC errors in critical chunks */
29
 
   if (png_ptr == NULL)
30
 
      return;
31
34
   switch (crit_action)
32
35
   {
33
36
      case PNG_CRC_NO_CHANGE:                        /* Leave setting as is */
55
58
         break;
56
59
   }
57
60
 
 
61
   /* Tell libpng how we react to CRC errors in ancillary chunks */
58
62
   switch (ancil_action)
59
63
   {
60
64
      case PNG_CRC_NO_CHANGE:                       /* Leave setting as is */
94
98
   int need_expand, double background_gamma)
95
99
{
96
100
   png_debug(1, "in png_set_background");
 
101
 
97
102
   if (png_ptr == NULL)
98
103
      return;
99
104
   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
111
116
}
112
117
#endif
113
118
 
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 */
116
121
void PNGAPI
117
122
png_set_strip_16(png_structp png_ptr)
118
123
{
119
124
   png_debug(1, "in png_set_strip_16");
 
125
 
120
126
   if (png_ptr == NULL)
121
127
      return;
122
128
   png_ptr->transformations |= PNG_16_TO_8;
123
129
}
124
130
#endif
125
131
 
126
 
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 
132
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
127
133
void PNGAPI
128
134
png_set_strip_alpha(png_structp png_ptr)
129
135
{
130
136
   png_debug(1, "in png_set_strip_alpha");
 
137
 
131
138
   if (png_ptr == NULL)
132
139
      return;
133
140
   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
134
141
}
135
142
#endif
136
143
 
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
159
166
   int full_dither)
160
167
{
161
168
   png_debug(1, "in png_set_dither");
 
169
 
162
170
   if (png_ptr == NULL)
163
171
      return;
164
172
   png_ptr->transformations |= PNG_DITHER;
328
336
            png_ptr->palette_to_index[i] = (png_byte)i;
329
337
         }
330
338
 
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));
334
341
 
335
342
         num_new_palette = num_palette;
336
343
 
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,
 
486
 
 
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));
483
489
 
484
490
      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
485
491
         png_sizeof(png_byte)));
486
 
 
487
492
      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
488
493
 
489
494
      for (i = 0; i < num_palette; i++)
544
549
png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
545
550
{
546
551
   png_debug(1, "in png_set_gamma");
 
552
 
547
553
   if (png_ptr == NULL)
548
554
      return;
 
555
 
549
556
   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
550
557
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
551
558
       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
555
562
}
556
563
#endif
557
564
 
558
 
#if defined(PNG_READ_EXPAND_SUPPORTED)
 
565
#ifdef PNG_READ_EXPAND_SUPPORTED
559
566
/* Expand paletted images to RGB, expand grayscale images of
560
567
 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
561
568
 * to alpha channels.
564
571
png_set_expand(png_structp png_ptr)
565
572
{
566
573
   png_debug(1, "in png_set_expand");
 
574
 
567
575
   if (png_ptr == NULL)
568
576
      return;
 
577
 
569
578
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
570
579
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
571
580
}
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.
585
594
 *
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().
588
598
 */
589
599
 
590
600
/* Expand paletted images to RGB. */
592
602
png_set_palette_to_rgb(png_structp png_ptr)
593
603
{
594
604
   png_debug(1, "in png_set_palette_to_rgb");
 
605
 
595
606
   if (png_ptr == NULL)
596
607
      return;
 
608
 
597
609
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
598
610
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
599
611
}
600
612
 
601
 
#if !defined(PNG_1_0_X)
 
613
#ifndef PNG_1_0_X
602
614
/* Expand grayscale images of less than 8-bit depth to 8 bits. */
603
615
void PNGAPI
604
616
png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
605
617
{
606
618
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
 
619
 
607
620
   if (png_ptr == NULL)
608
621
      return;
 
622
 
609
623
   png_ptr->transformations |= PNG_EXPAND;
610
624
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
611
625
}
618
632
png_set_gray_1_2_4_to_8(png_structp png_ptr)
619
633
{
620
634
   png_debug(1, "in png_set_gray_1_2_4_to_8");
 
635
 
621
636
   if (png_ptr == NULL)
622
637
      return;
 
638
 
623
639
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
624
640
}
625
641
#endif
630
646
png_set_tRNS_to_alpha(png_structp png_ptr)
631
647
{
632
648
   png_debug(1, "in png_set_tRNS_to_alpha");
 
649
 
633
650
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
634
651
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
635
652
}
636
653
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
637
654
 
638
 
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 
655
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
639
656
void PNGAPI
640
657
png_set_gray_to_rgb(png_structp png_ptr)
641
658
{
642
659
   png_debug(1, "in png_set_gray_to_rgb");
 
660
 
643
661
   png_ptr->transformations |= PNG_GRAY_TO_RGB;
644
662
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
645
663
}
646
664
#endif
647
665
 
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.
652
670
 */
668
686
   png_fixed_point red, png_fixed_point green)
669
687
{
670
688
   png_debug(1, "in png_set_rgb_to_gray");
 
689
 
671
690
   if (png_ptr == NULL)
672
691
      return;
 
692
 
673
693
   switch(error_action)
674
694
   {
675
695
      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
681
701
      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
682
702
   }
683
703
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
684
 
#if defined(PNG_READ_EXPAND_SUPPORTED)
 
704
#ifdef PNG_READ_EXPAND_SUPPORTED
685
705
      png_ptr->transformations |= PNG_EXPAND;
686
706
#else
687
707
   {
724
744
   read_user_transform_fn)
725
745
{
726
746
   png_debug(1, "in png_set_read_user_transform_fn");
 
747
 
727
748
   if (png_ptr == NULL)
728
749
      return;
729
 
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 
750
 
 
751
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
730
752
   png_ptr->transformations |= PNG_USER_TRANSFORM;
731
753
   png_ptr->read_user_transform_fn = read_user_transform_fn;
732
754
#endif
745
767
png_init_read_transformations(png_structp png_ptr)
746
768
{
747
769
   png_debug(1, "in png_init_read_transformations");
748
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
749
 
   if (png_ptr != NULL)
 
770
 
 
771
#ifdef PNG_USELESS_TESTS_SUPPORTED
 
772
  if (png_ptr != NULL)
750
773
#endif
751
774
  {
752
775
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
756
779
 
757
780
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
758
781
 
759
 
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 
782
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
760
783
   /* Detect gray background and attempt to enable optimization
761
784
    * for gray --> RGB case
762
785
    *
843
866
         png_ptr->background.blue  =
844
867
            png_ptr->palette[png_ptr->background.index].blue;
845
868
 
846
 
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 
869
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
847
870
        if (png_ptr->transformations & PNG_INVERT_ALPHA)
848
871
        {
849
 
#if defined(PNG_READ_EXPAND_SUPPORTED)
 
872
#ifdef PNG_READ_EXPAND_SUPPORTED
850
873
           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
851
874
#endif
852
875
           {
889
912
        png_ptr->gamma != 0.0)
890
913
   {
891
914
      png_build_gamma_table(png_ptr);
892
 
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 
915
#ifdef PNG_READ_BACKGROUND_SUPPORTED
893
916
      if (png_ptr->transformations & PNG_BACKGROUND)
894
917
      {
895
918
         if (color_type == PNG_COLOR_TYPE_PALETTE)
990
1013
                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
991
1014
               }
992
1015
            }
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.
997
 
             */
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.
 
1020
             */
 
1021
            png_ptr->transformations &= ~PNG_BACKGROUND;
 
1022
            png_ptr->transformations &= ~PNG_GAMMA;
 
1023
            png_ptr->transformations |= PNG_STRIP_ALPHA;
1001
1024
         }
1002
1025
         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1003
1026
         else
1075
1098
            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1076
1099
         }
1077
1100
 
1078
 
         /* Done the gamma correction. */
1079
 
         png_ptr->transformations &= ~PNG_GAMMA;
 
1101
         /* Done the gamma correction. */
 
1102
         png_ptr->transformations &= ~PNG_GAMMA;
1080
1103
      }
1081
1104
   }
1082
 
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 
1105
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1083
1106
   else
1084
1107
#endif
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))
1121
1144
   }
1122
1145
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
1123
1146
 
1124
 
#if defined(PNG_READ_SHIFT_SUPPORTED)
 
1147
#ifdef PNG_READ_SHIFT_SUPPORTED
1125
1148
   if ((png_ptr->transformations & PNG_SHIFT) &&
1126
1149
      (color_type == PNG_COLOR_TYPE_PALETTE))
1127
1150
   {
1161
1184
png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1162
1185
{
1163
1186
   png_debug(1, "in png_read_transform_info");
1164
 
#if defined(PNG_READ_EXPAND_SUPPORTED)
 
1187
 
 
1188
#ifdef PNG_READ_EXPAND_SUPPORTED
1165
1189
   if (png_ptr->transformations & PNG_EXPAND)
1166
1190
   {
1167
1191
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1188
1212
   }
1189
1213
#endif
1190
1214
 
1191
 
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 
1215
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1192
1216
   if (png_ptr->transformations & PNG_BACKGROUND)
1193
1217
   {
1194
1218
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1197
1221
   }
1198
1222
#endif
1199
1223
 
1200
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
1224
#ifdef PNG_READ_GAMMA_SUPPORTED
1201
1225
   if (png_ptr->transformations & PNG_GAMMA)
1202
1226
   {
1203
1227
#ifdef PNG_FLOATING_POINT_SUPPORTED
1209
1233
   }
1210
1234
#endif
1211
1235
 
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;
1215
1239
#endif
1216
1240
 
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;
1220
1244
#endif
1221
1245
 
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;
1225
1249
#endif
1226
1250
 
1227
 
#if defined(PNG_READ_DITHER_SUPPORTED)
 
1251
#ifdef PNG_READ_DITHER_SUPPORTED
1228
1252
   if (png_ptr->transformations & PNG_DITHER)
1229
1253
   {
1230
1254
      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1236
1260
   }
1237
1261
#endif
1238
1262
 
1239
 
#if defined(PNG_READ_PACK_SUPPORTED)
 
1263
#ifdef PNG_READ_PACK_SUPPORTED
1240
1264
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1241
1265
      info_ptr->bit_depth = 8;
1242
1266
#endif
1248
1272
   else
1249
1273
      info_ptr->channels = 1;
1250
1274
 
1251
 
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 
1275
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1252
1276
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1253
1277
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1254
1278
#endif
1256
1280
   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1257
1281
      info_ptr->channels++;
1258
1282
 
1259
 
#if defined(PNG_READ_FILLER_SUPPORTED)
 
1283
#ifdef PNG_READ_FILLER_SUPPORTED
1260
1284
   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
1261
1285
   if ((png_ptr->transformations & PNG_FILLER) &&
1262
1286
       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1264
1288
   {
1265
1289
      info_ptr->channels++;
1266
1290
      /* If adding a true alpha channel not just filler */
1267
 
#if !defined(PNG_1_0_X)
 
1291
#ifndef PNG_1_0_X
1268
1292
      if (png_ptr->transformations & PNG_ADD_ALPHA)
1269
1293
        info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1270
1294
#endif
1287
1311
 
1288
1312
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1289
1313
 
1290
 
#if !defined(PNG_READ_EXPAND_SUPPORTED)
 
1314
#ifndef PNG_READ_EXPAND_SUPPORTED
1291
1315
   if (png_ptr)
1292
1316
      return;
1293
1317
#endif
1301
1325
png_do_read_transformations(png_structp png_ptr)
1302
1326
{
1303
1327
   png_debug(1, "in png_do_read_transformations");
 
1328
 
1304
1329
   if (png_ptr->row_buf == NULL)
1305
1330
   {
1306
 
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
 
1331
#if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
1307
1332
      char msg[50];
1308
1333
 
1309
1334
      png_snprintf2(msg, 50,
1327
1352
#endif
1328
1353
#endif
1329
1354
 
1330
 
#if defined(PNG_READ_EXPAND_SUPPORTED)
 
1355
#ifdef PNG_READ_EXPAND_SUPPORTED
1331
1356
   if (png_ptr->transformations & PNG_EXPAND)
1332
1357
   {
1333
1358
      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
1348
1373
   }
1349
1374
#endif
1350
1375
 
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));
1355
1380
#endif
1356
1381
 
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)
1359
1384
   {
1360
1385
      int rgb_error =
1403
1428
 *  transform appropriately, then it would save a lot of work/time.
1404
1429
 */
1405
1430
 
1406
 
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 
1431
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1407
1432
   /* If gray -> RGB, do so now only if background is non-gray; else do later
1408
1433
    * for performance reasons
1409
1434
    */
1412
1437
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1413
1438
#endif
1414
1439
 
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);
 
1443
#endif
 
1444
 
 
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,
1428
1458
);
1429
1459
#endif
1430
1460
 
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);
1442
1472
#endif
1443
1473
 
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);
1447
 
#endif
1448
 
 
1449
 
#if defined(PNG_READ_DITHER_SUPPORTED)
 
1474
#ifdef PNG_READ_DITHER_SUPPORTED
1450
1475
   if (png_ptr->transformations & PNG_DITHER)
1451
1476
   {
1452
1477
      png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
1456
1481
   }
1457
1482
#endif
1458
1483
 
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);
1462
1487
#endif
1463
1488
 
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));
1468
1493
#endif
1469
1494
 
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);
1473
1498
#endif
1474
1499
 
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);
1478
1503
#endif
1479
1504
 
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);
1483
1508
#endif
1484
1509
 
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);
1490
1515
#endif
1491
1516
 
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);
1496
1521
#endif
1497
1522
 
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);
1501
1526
#endif
1502
1527
 
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);
1506
1531
#endif
1507
1532
 
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);
1511
1536
#endif
1512
1537
 
1513
 
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 
1538
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1514
1539
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
1515
1540
    {
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)
1539
1564
 
1540
1565
}
1541
1566
 
1542
 
#if defined(PNG_READ_PACK_SUPPORTED)
 
1567
#ifdef PNG_READ_PACK_SUPPORTED
1543
1568
/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1544
1569
 * without changing the actual values.  Thus, if you had a row with
1545
1570
 * a bit depth of 1, you would end up with bytes that only contained
1550
1575
png_do_unpack(png_row_infop row_info, png_bytep row)
1551
1576
{
1552
1577
   png_debug(1, "in png_do_unpack");
1553
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
1578
 
 
1579
#ifdef PNG_USELESS_TESTS_SUPPORTED
1554
1580
   if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
1555
1581
#else
1556
1582
   if (row_info->bit_depth < 8)
1632
1658
}
1633
1659
#endif
1634
1660
 
1635
 
#if defined(PNG_READ_SHIFT_SUPPORTED)
 
1661
#ifdef PNG_READ_SHIFT_SUPPORTED
1636
1662
/* Reverse the effects of png_do_shift.  This routine merely shifts the
1637
1663
 * pixels back to their significant bits values.  Thus, if you have
1638
1664
 * a row of bit depth 8, but only 5 are significant, this will shift
1642
1668
png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
1643
1669
{
1644
1670
   png_debug(1, "in png_do_unshift");
 
1671
 
1645
1672
   if (
1646
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
1673
#ifdef PNG_USELESS_TESTS_SUPPORTED
1647
1674
       row != NULL && row_info != NULL && sig_bits != NULL &&
1648
1675
#endif
1649
1676
       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1745
1772
}
1746
1773
#endif
1747
1774
 
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)
1752
1779
{
1753
1780
   png_debug(1, "in png_do_chop");
1754
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
1781
 
 
1782
#ifdef PNG_USELESS_TESTS_SUPPORTED
1755
1783
   if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
1756
1784
#else
1757
1785
   if (row_info->bit_depth == 16)
1764
1792
 
1765
1793
      for (i = 0; i<istop; i++, sp += 2, dp++)
1766
1794
      {
1767
 
#if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
 
1795
#ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1768
1796
      /* This does a more accurate scaling of the 16-bit color
1769
1797
       * value, rather than a simple low-byte truncation.
1770
1798
       *
1803
1831
}
1804
1832
#endif
1805
1833
 
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)
1809
1837
{
1810
1838
   png_debug(1, "in png_do_read_swap_alpha");
1811
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
1839
 
 
1840
#ifdef PNG_USELESS_TESTS_SUPPORTED
1812
1841
   if (row != NULL && row_info != NULL)
1813
1842
#endif
1814
1843
   {
1895
1924
}
1896
1925
#endif
1897
1926
 
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)
1901
1930
{
1902
1931
   png_debug(1, "in png_do_read_invert_alpha");
1903
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
1932
 
 
1933
#ifdef PNG_USELESS_TESTS_SUPPORTED
1904
1934
   if (row != NULL && row_info != NULL)
1905
1935
#endif
1906
1936
   {
1993
2023
}
1994
2024
#endif
1995
2025
 
1996
 
#if defined(PNG_READ_FILLER_SUPPORTED)
 
2026
#ifdef PNG_READ_FILLER_SUPPORTED
1997
2027
/* Add filler channel if we have RGB color */
1998
2028
void /* PRIVATE */
1999
2029
png_do_read_filler(png_row_infop row_info, png_bytep row,
2006
2036
   png_byte lo_filler = (png_byte)(filler & 0xff);
2007
2037
 
2008
2038
   png_debug(1, "in png_do_read_filler");
 
2039
 
2009
2040
   if (
2010
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
2041
#ifdef PNG_USELESS_TESTS_SUPPORTED
2011
2042
       row != NULL  && row_info != NULL &&
2012
2043
#endif
2013
2044
       row_info->color_type == PNG_COLOR_TYPE_GRAY)
2169
2200
}
2170
2201
#endif
2171
2202
 
2172
 
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 
2203
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2173
2204
/* Expand grayscale files to RGB, with or without alpha */
2174
2205
void /* PRIVATE */
2175
2206
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2178
2209
   png_uint_32 row_width = row_info->width;
2179
2210
 
2180
2211
   png_debug(1, "in png_do_gray_to_rgb");
 
2212
 
2181
2213
   if (row_info->bit_depth >= 8 &&
2182
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
2214
#ifdef PNG_USELESS_TESTS_SUPPORTED
2183
2215
       row != NULL && row_info != NULL &&
2184
2216
#endif
2185
2217
      !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2252
2284
}
2253
2285
#endif
2254
2286
 
2255
 
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 
2287
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2256
2288
/* Reduce RGB files to grayscale, with or without alpha
2257
2289
 * using the equation given in Poynton's ColorFAQ at
2258
2290
 * <http://www.inforamp.net/~poynton/>  (THIS LINK IS DEAD June 2008)
2284
2316
   int rgb_error = 0;
2285
2317
 
2286
2318
   png_debug(1, "in png_do_rgb_to_gray");
 
2319
 
2287
2320
   if (
2288
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
2321
#ifdef PNG_USELESS_TESTS_SUPPORTED
2289
2322
       row != NULL && row_info != NULL &&
2290
2323
#endif
2291
2324
      (row_info->color_type & PNG_COLOR_MASK_COLOR))
2522
2555
   int v;
2523
2556
 
2524
2557
   png_debug(1, "in png_do_build_grayscale_palette");
 
2558
 
2525
2559
   if (palette == NULL)
2526
2560
      return;
2527
2561
 
2568
2602
   int num_palette)
2569
2603
{
2570
2604
   png_debug(1, "in png_correct_palette");
 
2605
 
2571
2606
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
2572
2607
    defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
2573
2608
   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
2678
2713
   }
2679
2714
   else
2680
2715
#endif
2681
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
2716
#ifdef PNG_READ_GAMMA_SUPPORTED
2682
2717
   if (png_ptr->transformations & PNG_GAMMA)
2683
2718
   {
2684
2719
      int i;
2690
2725
         palette[i].blue = png_ptr->gamma_table[palette[i].blue];
2691
2726
      }
2692
2727
   }
2693
 
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 
2728
#ifdef PNG_READ_BACKGROUND_SUPPORTED
2694
2729
   else
2695
2730
#endif
2696
2731
#endif
2697
 
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 
2732
#ifdef PNG_READ_BACKGROUND_SUPPORTED
2698
2733
   if (png_ptr->transformations & PNG_BACKGROUND)
2699
2734
   {
2700
2735
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2743
2778
}
2744
2779
#endif
2745
2780
 
2746
 
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 
2781
#ifdef PNG_READ_BACKGROUND_SUPPORTED
2747
2782
/* Replace any alpha or transparency with the supplied background color.
2748
2783
 * "background" is already in the screen gamma, while "background_1" is
2749
2784
 * at a gamma of 1.0.  Paletted files have already been taken care of.
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,
2765
2800
   int shift;
2766
2801
 
2767
2802
   png_debug(1, "in png_do_background");
 
2803
 
2768
2804
   if (background != NULL &&
2769
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
2805
#ifdef PNG_USELESS_TESTS_SUPPORTED
2770
2806
       row != NULL && row_info != NULL &&
2771
2807
#endif
2772
2808
      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
2803
2839
 
2804
2840
               case 2:
2805
2841
               {
2806
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
2842
#ifdef PNG_READ_GAMMA_SUPPORTED
2807
2843
                  if (gamma_table != NULL)
2808
2844
                  {
2809
2845
                     sp = row;
2860
2896
 
2861
2897
               case 4:
2862
2898
               {
2863
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
2899
#ifdef PNG_READ_GAMMA_SUPPORTED
2864
2900
                  if (gamma_table != NULL)
2865
2901
                  {
2866
2902
                     sp = row;
2917
2953
 
2918
2954
               case 8:
2919
2955
               {
2920
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
2956
#ifdef PNG_READ_GAMMA_SUPPORTED
2921
2957
                  if (gamma_table != NULL)
2922
2958
                  {
2923
2959
                     sp = row;
2950
2986
 
2951
2987
               case 16:
2952
2988
               {
2953
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
2989
#ifdef PNG_READ_GAMMA_SUPPORTED
2954
2990
                  if (gamma_16 != NULL)
2955
2991
                  {
2956
2992
                     sp = row;
2999
3035
         {
3000
3036
            if (row_info->bit_depth == 8)
3001
3037
            {
3002
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3038
#ifdef PNG_READ_GAMMA_SUPPORTED
3003
3039
               if (gamma_table != NULL)
3004
3040
               {
3005
3041
                  sp = row;
3040
3076
            }
3041
3077
            else /* if (row_info->bit_depth == 16) */
3042
3078
            {
3043
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3079
#ifdef PNG_READ_GAMMA_SUPPORTED
3044
3080
               if (gamma_16 != NULL)
3045
3081
               {
3046
3082
                  sp = row;
3104
3140
         {
3105
3141
            if (row_info->bit_depth == 8)
3106
3142
            {
3107
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3143
#ifdef PNG_READ_GAMMA_SUPPORTED
3108
3144
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3109
3145
                   gamma_table != NULL)
3110
3146
               {
3146
3182
                     {
3147
3183
                        *dp = *sp;
3148
3184
                     }
3149
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3185
#ifdef PNG_READ_GAMMA_SUPPORTED
3150
3186
                     else if (a == 0)
3151
3187
                     {
3152
3188
                        *dp = (png_byte)background->gray;
3163
3199
            }
3164
3200
            else /* if (png_ptr->bit_depth == 16) */
3165
3201
            {
3166
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3202
#ifdef PNG_READ_GAMMA_SUPPORTED
3167
3203
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3168
3204
                   gamma_16_to_1 != NULL)
3169
3205
               {
3181
3217
                        *dp = (png_byte)((v >> 8) & 0xff);
3182
3218
                        *(dp + 1) = (png_byte)(v & 0xff);
3183
3219
                     }
3184
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3220
#ifdef PNG_READ_GAMMA_SUPPORTED
3185
3221
                     else if (a == 0)
3186
3222
#else
3187
3223
                     else
3191
3227
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
3192
3228
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
3193
3229
                     }
3194
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3230
#ifdef PNG_READ_GAMMA_SUPPORTED
3195
3231
                     else
3196
3232
                     {
3197
3233
                        png_uint_16 g, v, w;
3217
3253
                     {
3218
3254
                        png_memcpy(dp, sp, 2);
3219
3255
                     }
3220
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3256
#ifdef PNG_READ_GAMMA_SUPPORTED
3221
3257
                     else if (a == 0)
3222
3258
#else
3223
3259
                     else
3226
3262
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
3227
3263
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
3228
3264
                     }
3229
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3265
#ifdef PNG_READ_GAMMA_SUPPORTED
3230
3266
                     else
3231
3267
                     {
3232
3268
                        png_uint_16 g, v;
3247
3283
         {
3248
3284
            if (row_info->bit_depth == 8)
3249
3285
            {
3250
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3286
#ifdef PNG_READ_GAMMA_SUPPORTED
3251
3287
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3252
3288
                   gamma_table != NULL)
3253
3289
               {
3320
3356
            }
3321
3357
            else /* if (row_info->bit_depth == 16) */
3322
3358
            {
3323
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3359
#ifdef PNG_READ_GAMMA_SUPPORTED
3324
3360
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3325
3361
                   gamma_16_to_1 != NULL)
3326
3362
               {
3437
3473
}
3438
3474
#endif
3439
3475
 
3440
 
#if defined(PNG_READ_GAMMA_SUPPORTED)
 
3476
#ifdef PNG_READ_GAMMA_SUPPORTED
3441
3477
/* Gamma correct the image, avoiding the alpha channel.  Make sure
3442
3478
 * you do this after you deal with the transparency issue on grayscale
3443
3479
 * or RGB images. If your bit depth is 8, use gamma_table, if it
3454
3490
   png_uint_32 row_width=row_info->width;
3455
3491
 
3456
3492
   png_debug(1, "in png_do_gamma");
 
3493
 
3457
3494
   if (
3458
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
3495
#ifdef PNG_USELESS_TESTS_SUPPORTED
3459
3496
       row != NULL && row_info != NULL &&
3460
3497
#endif
3461
3498
       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3628
3665
}
3629
3666
#endif
3630
3667
 
3631
 
#if defined(PNG_READ_EXPAND_SUPPORTED)
 
3668
#ifdef PNG_READ_EXPAND_SUPPORTED
3632
3669
/* Expands a palette row to an RGB or RGBA row depending
3633
3670
 * upon whether you supply trans and num_trans.
3634
3671
 */
3642
3679
   png_uint_32 row_width=row_info->width;
3643
3680
 
3644
3681
   png_debug(1, "in png_do_expand_palette");
 
3682
 
3645
3683
   if (
3646
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
3684
#ifdef PNG_USELESS_TESTS_SUPPORTED
3647
3685
       row != NULL && row_info != NULL &&
3648
3686
#endif
3649
3687
       row_info->color_type == PNG_COLOR_TYPE_PALETTE)
3788
3826
   png_uint_32 row_width=row_info->width;
3789
3827
 
3790
3828
   png_debug(1, "in png_do_expand");
3791
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
3829
 
 
3830
#ifdef PNG_USELESS_TESTS_SUPPORTED
3792
3831
   if (row != NULL && row_info != NULL)
3793
3832
#endif
3794
3833
   {
3989
4028
}
3990
4029
#endif
3991
4030
 
3992
 
#if defined(PNG_READ_DITHER_SUPPORTED)
 
4031
#ifdef PNG_READ_DITHER_SUPPORTED
3993
4032
void /* PRIVATE */
3994
4033
png_do_dither(png_row_infop row_info, png_bytep row,
3995
4034
    png_bytep palette_lookup, png_bytep dither_lookup)
3999
4038
   png_uint_32 row_width=row_info->width;
4000
4039
 
4001
4040
   png_debug(1, "in png_do_dither");
4002
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
4041
 
 
4042
#ifdef PNG_USELESS_TESTS_SUPPORTED
4003
4043
   if (row != NULL && row_info != NULL)
4004
4044
#endif
4005
4045
   {
4081
4121
#endif
4082
4122
 
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};
4087
4127
 
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.
 
4132
 *
 
4133
 * See the PNG extensions document for an integer algorithm for creating
 
4134
 * the gamma tables.  Maybe we will implement that here someday.
 
4135
 *
 
4136
 * We should only reach this point if
 
4137
 *
 
4138
 *      the file_gamma is known (i.e., the gAMA or sRGB chunk is present,
 
4139
 *      or the application has provided a file_gamma)
 
4140
 *
 
4141
 *   AND
 
4142
 *      {
 
4143
 *         the screen_gamma is known
 
4144
 *      OR
 
4145
 *
 
4146
 *         RGB_to_gray transformation is being performed
 
4147
 *      }
 
4148
 *
 
4149
 *   AND
 
4150
 *      {
 
4151
 *         the screen_gamma is different from the reciprocal of the
 
4152
 *         file_gamma by more than the specified threshold
 
4153
 *
 
4154
 *      OR
 
4155
 *
 
4156
 *         a background color has been specified and the file_gamma
 
4157
 *         and screen_gamma are not 1.0, within the specified threshold.
 
4158
 *      }
4092
4159
 */
 
4160
 
4093
4161
void /* PRIVATE */
4094
4162
png_build_gamma_table(png_structp png_ptr)
4095
4163
{
4199
4267
     else
4200
4268
        g = 1.0;
4201
4269
 
4202
 
     png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
 
4270
     png_ptr->gamma_16_table = (png_uint_16pp)png_calloc(png_ptr,
4203
4271
        (png_uint_32)(num * png_sizeof(png_uint_16p)));
4204
 
     png_memset(png_ptr->gamma_16_table, 0, num * png_sizeof(png_uint_16p));
4205
4272
 
4206
4273
     if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
4207
4274
     {
4261
4328
 
4262
4329
        g = 1.0 / (png_ptr->gamma);
4263
4330
 
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));
4267
4333
 
4268
4334
        for (i = 0; i < num; i++)
4269
4335
        {
4286
4352
        else
4287
4353
           g = png_ptr->gamma;   /* Probably doing rgb_to_gray */
4288
4354
 
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));
4293
4357
 
4294
4358
        for (i = 0; i < num; i++)
4295
4359
        {
4314
4378
/* To do: install integer version of png_build_gamma_table here */
4315
4379
#endif
4316
4380
 
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)
4321
4385
{
4322
4386
   png_debug(1, "in png_do_read_intrapixel");
 
4387
 
4323
4388
   if (
4324
 
#if defined(PNG_USELESS_TESTS_SUPPORTED)
 
4389
#ifdef PNG_USELESS_TESTS_SUPPORTED
4325
4390
       row != NULL && row_info != NULL &&
4326
4391
#endif
4327
4392
       (row_info->color_type & PNG_COLOR_MASK_COLOR))