Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1897 serge 1
 
2
 *
3
 * Last changed in libpng 1.5.1 [February 3, 2011]
4
 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
5
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7
 *
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
10
 * and license in png.h
11
 *
12
 * This file contains functions optionally called by an application
13
 * in order to tell libpng how to handle data when reading a PNG.
14
 * Transformations that are used in both reading and writing are
15
 * in pngtrans.c.
16
 */
17
18
 
19
20
 
21
22
 
23
void PNGAPI
24
png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
25
{
26
   png_debug(1, "in png_set_crc_action");
27
28
 
29
      return;
30
31
 
32
   switch (crit_action)
33
   {
34
      case PNG_CRC_NO_CHANGE:                        /* Leave setting as is */
35
         break;
36
37
 
38
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
39
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
40
         break;
41
42
 
43
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
44
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
45
                           PNG_FLAG_CRC_CRITICAL_IGNORE;
46
         break;
47
48
 
49
         png_warning(png_ptr,
50
            "Can't discard critical data on CRC error");
51
      case PNG_CRC_ERROR_QUIT:                                /* Error/quit */
52
53
 
54
      default:
55
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
56
         break;
57
   }
58
59
 
60
   switch (ancil_action)
61
   {
62
      case PNG_CRC_NO_CHANGE:                       /* Leave setting as is */
63
         break;
64
65
 
66
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
67
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
68
         break;
69
70
 
71
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
72
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
73
                           PNG_FLAG_CRC_ANCILLARY_NOWARN;
74
         break;
75
76
 
77
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
78
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
79
         break;
80
81
 
82
83
 
84
      default:
85
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
86
         break;
87
   }
88
}
89
90
 
91
/* Handle alpha and tRNS via a background color */
92
void PNGFAPI
93
png_set_background_fixed(png_structp png_ptr,
94
    png_const_color_16p background_color, int background_gamma_code,
95
    int need_expand, png_fixed_point background_gamma)
96
{
97
   png_debug(1, "in png_set_background_fixed");
98
99
 
100
      return;
101
102
 
103
   {
104
      png_warning(png_ptr, "Application must supply a known background gamma");
105
      return;
106
   }
107
108
 
109
   png_memcpy(&(png_ptr->background), background_color,
110
      png_sizeof(png_color_16));
111
   png_ptr->background_gamma = background_gamma;
112
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
113
   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
114
}
115
116
 
117
void PNGAPI
118
png_set_background(png_structp png_ptr,
119
    png_const_color_16p background_color, int background_gamma_code,
120
    int need_expand, double background_gamma)
121
{
122
   png_set_background_fixed(png_ptr, background_color, background_gamma_code,
123
      need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
124
}
125
#  endif  /* FLOATING_POINT */
126
#endif /* READ_BACKGROUND */
127
128
 
129
/* Strip 16 bit depth files to 8 bit depth */
130
void PNGAPI
131
png_set_strip_16(png_structp png_ptr)
132
{
133
   png_debug(1, "in png_set_strip_16");
134
135
 
136
      return;
137
138
 
139
}
140
#endif
141
142
 
143
void PNGAPI
144
png_set_strip_alpha(png_structp png_ptr)
145
{
146
   png_debug(1, "in png_set_strip_alpha");
147
148
 
149
      return;
150
151
 
152
}
153
#endif
154
155
 
156
/* Dither file to 8 bit.  Supply a palette, the current number
157
 * of elements in the palette, the maximum number of elements
158
 * allowed, and a histogram if possible.  If the current number
159
 * of colors is greater then the maximum number, the palette will be
160
 * modified to fit in the maximum number.  "full_quantize" indicates
161
 * whether we need a quantizing cube set up for RGB images, or if we
162
 * simply are reducing the number of colors in a paletted image.
163
 */
164
165
 
166
{
167
   struct png_dsort_struct FAR * next;
168
   png_byte left;
169
   png_byte right;
170
} png_dsort;
171
typedef png_dsort FAR *       png_dsortp;
172
typedef png_dsort FAR * FAR * png_dsortpp;
173
174
 
175
png_set_quantize(png_structp png_ptr, png_colorp palette,
176
    int num_palette, int maximum_colors, png_const_uint_16p histogram,
177
    int full_quantize)
178
{
179
   png_debug(1, "in png_set_quantize");
180
181
 
182
      return;
183
184
 
185
186
 
187
   {
188
      int i;
189
190
 
191
          (png_uint_32)(num_palette * png_sizeof(png_byte)));
192
      for (i = 0; i < num_palette; i++)
193
         png_ptr->quantize_index[i] = (png_byte)i;
194
   }
195
196
 
197
   {
198
      if (histogram != NULL)
199
      {
200
         /* This is easy enough, just throw out the least used colors.
201
          * Perhaps not the best solution, but good enough.
202
          */
203
204
 
205
206
 
207
         png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
208
             (png_uint_32)(num_palette * png_sizeof(png_byte)));
209
210
 
211
         for (i = 0; i < num_palette; i++)
212
            png_ptr->quantize_sort[i] = (png_byte)i;
213
214
 
215
          * bubble sort, and running it until we have sorted
216
          * out enough colors.  Note that we don't care about
217
          * sorting all the colors, just finding which are
218
          * least used.
219
          */
220
221
 
222
         {
223
            int done; /* To stop early if the list is pre-sorted */
224
            int j;
225
226
 
227
            for (j = 0; j < i; j++)
228
            {
229
               if (histogram[png_ptr->quantize_sort[j]]
230
                   < histogram[png_ptr->quantize_sort[j + 1]])
231
               {
232
                  png_byte t;
233
234
 
235
                  png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
236
                  png_ptr->quantize_sort[j + 1] = t;
237
                  done = 0;
238
               }
239
            }
240
241
 
242
               break;
243
         }
244
245
 
246
         if (full_quantize)
247
         {
248
            int j = num_palette;
249
250
 
251
             * move the others.
252
             */
253
            for (i = 0; i < maximum_colors; i++)
254
            {
255
               if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
256
               {
257
                  do
258
                     j--;
259
                  while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
260
261
 
262
               }
263
            }
264
         }
265
         else
266
         {
267
            int j = num_palette;
268
269
 
270
             * develop a translation table.
271
             */
272
            for (i = 0; i < maximum_colors; i++)
273
            {
274
               /* Only move the colors we need to */
275
               if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
276
               {
277
                  png_color tmp_color;
278
279
 
280
                     j--;
281
                  while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
282
283
 
284
                  palette[j] = palette[i];
285
                  palette[i] = tmp_color;
286
                  /* Indicate where the color went */
287
                  png_ptr->quantize_index[j] = (png_byte)i;
288
                  png_ptr->quantize_index[i] = (png_byte)j;
289
               }
290
            }
291
292
 
293
            for (i = 0; i < num_palette; i++)
294
            {
295
               if ((int)png_ptr->quantize_index[i] >= maximum_colors)
296
               {
297
                  int min_d, k, min_k, d_index;
298
299
 
300
                  d_index = png_ptr->quantize_index[i];
301
                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
302
                  for (k = 1, min_k = 0; k < maximum_colors; k++)
303
                  {
304
                     int d;
305
306
 
307
308
 
309
                     {
310
                        min_d = d;
311
                        min_k = k;
312
                     }
313
                  }
314
                  /* Point to closest color */
315
                  png_ptr->quantize_index[i] = (png_byte)min_k;
316
               }
317
            }
318
         }
319
         png_free(png_ptr, png_ptr->quantize_sort);
320
         png_ptr->quantize_sort = NULL;
321
      }
322
      else
323
      {
324
         /* This is much harder to do simply (and quickly).  Perhaps
325
          * we need to go through a median cut routine, but those
326
          * don't always behave themselves with only a few colors
327
          * as input.  So we will just find the closest two colors,
328
          * and throw out one of them (chosen somewhat randomly).
329
          * [We don't understand this at all, so if someone wants to
330
          *  work on improving it, be our guest - AED, GRP]
331
          */
332
         int i;
333
         int max_d;
334
         int num_new_palette;
335
         png_dsortp t;
336
         png_dsortpp hash;
337
338
 
339
340
 
341
         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
342
             (png_uint_32)(num_palette * png_sizeof(png_byte)));
343
         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
344
             (png_uint_32)(num_palette * png_sizeof(png_byte)));
345
346
 
347
         for (i = 0; i < num_palette; i++)
348
         {
349
            png_ptr->index_to_palette[i] = (png_byte)i;
350
            png_ptr->palette_to_index[i] = (png_byte)i;
351
         }
352
353
 
354
             png_sizeof(png_dsortp)));
355
356
 
357
358
 
359
          * pair we will be eliminating will be.  Larger
360
          * numbers mean more areas will be allocated, Smaller
361
          * numbers run the risk of not saving enough data, and
362
          * having to do this all over again.
363
          *
364
          * I have not done extensive checking on this number.
365
          */
366
         max_d = 96;
367
368
 
369
         {
370
            for (i = 0; i < num_new_palette - 1; i++)
371
            {
372
               int j;
373
374
 
375
               {
376
                  int d;
377
378
 
379
380
 
381
                  {
382
383
 
384
                         (png_uint_32)(png_sizeof(png_dsort)));
385
386
 
387
                         break;
388
389
 
390
                     t->left = (png_byte)i;
391
                     t->right = (png_byte)j;
392
                     hash[d] = t;
393
                  }
394
               }
395
               if (t == NULL)
396
                  break;
397
            }
398
399
 
400
            for (i = 0; i <= max_d; i++)
401
            {
402
               if (hash[i] != NULL)
403
               {
404
                  png_dsortp p;
405
406
 
407
                  {
408
                     if ((int)png_ptr->index_to_palette[p->left]
409
                         < num_new_palette &&
410
                         (int)png_ptr->index_to_palette[p->right]
411
                         < num_new_palette)
412
                     {
413
                        int j, next_j;
414
415
 
416
                        {
417
                           j = p->left;
418
                           next_j = p->right;
419
                        }
420
                        else
421
                        {
422
                           j = p->right;
423
                           next_j = p->left;
424
                        }
425
426
 
427
                        palette[png_ptr->index_to_palette[j]]
428
                            = palette[num_new_palette];
429
                        if (!full_quantize)
430
                        {
431
                           int k;
432
433
 
434
                           {
435
                              if (png_ptr->quantize_index[k] ==
436
                                  png_ptr->index_to_palette[j])
437
                                 png_ptr->quantize_index[k] =
438
                                     png_ptr->index_to_palette[next_j];
439
440
 
441
                                  num_new_palette)
442
                                 png_ptr->quantize_index[k] =
443
                                     png_ptr->index_to_palette[j];
444
                           }
445
                        }
446
447
 
448
                            [num_new_palette]] = png_ptr->index_to_palette[j];
449
450
 
451
                            = png_ptr->palette_to_index[num_new_palette];
452
453
 
454
                            (png_byte)num_new_palette;
455
456
 
457
                            (png_byte)j;
458
                     }
459
                     if (num_new_palette <= maximum_colors)
460
                        break;
461
                  }
462
                  if (num_new_palette <= maximum_colors)
463
                     break;
464
               }
465
            }
466
467
 
468
            {
469
               if (hash[i] != NULL)
470
               {
471
                  png_dsortp p = hash[i];
472
                  while (p)
473
                  {
474
                     t = p->next;
475
                     png_free(png_ptr, p);
476
                     p = t;
477
                  }
478
               }
479
               hash[i] = 0;
480
            }
481
            max_d += 96;
482
         }
483
         png_free(png_ptr, hash);
484
         png_free(png_ptr, png_ptr->palette_to_index);
485
         png_free(png_ptr, png_ptr->index_to_palette);
486
         png_ptr->palette_to_index = NULL;
487
         png_ptr->index_to_palette = NULL;
488
      }
489
      num_palette = maximum_colors;
490
   }
491
   if (png_ptr->palette == NULL)
492
   {
493
      png_ptr->palette = palette;
494
   }
495
   png_ptr->num_palette = (png_uint_16)num_palette;
496
497
 
498
   {
499
      int i;
500
      png_bytep distance;
501
      int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
502
          PNG_QUANTIZE_BLUE_BITS;
503
      int num_red = (1 << PNG_QUANTIZE_RED_BITS);
504
      int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
505
      int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
506
      png_size_t num_entries = ((png_size_t)1 << total_bits);
507
508
 
509
          (png_uint_32)(num_entries * png_sizeof(png_byte)));
510
511
 
512
          png_sizeof(png_byte)));
513
514
 
515
516
 
517
      {
518
         int ir, ig, ib;
519
         int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
520
         int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
521
         int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
522
523
 
524
         {
525
            /* int dr = abs(ir - r); */
526
            int dr = ((ir > r) ? ir - r : r - ir);
527
            int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
528
                PNG_QUANTIZE_GREEN_BITS));
529
530
 
531
            {
532
               /* int dg = abs(ig - g); */
533
               int dg = ((ig > g) ? ig - g : g - ig);
534
               int dt = dr + dg;
535
               int dm = ((dr > dg) ? dr : dg);
536
               int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
537
538
 
539
               {
540
                  int d_index = index_g | ib;
541
                  /* int db = abs(ib - b); */
542
                  int db = ((ib > b) ? ib - b : b - ib);
543
                  int dmax = ((dm > db) ? dm : db);
544
                  int d = dmax + dt + db;
545
546
 
547
                  {
548
                     distance[d_index] = (png_byte)d;
549
                     png_ptr->palette_lookup[d_index] = (png_byte)i;
550
                  }
551
               }
552
            }
553
         }
554
      }
555
556
 
557
   }
558
}
559
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
560
561
 
562
/* Transform the image from the file_gamma to the screen_gamma.  We
563
 * only do transformations on images where the file_gamma and screen_gamma
564
 * are not close reciprocals, otherwise it slows things down slightly, and
565
 * also needlessly introduces small errors.
566
 *
567
 * We will turn off gamma transformation later if no semitransparent entries
568
 * are present in the tRNS array for palette images.  We can't do it here
569
 * because we don't necessarily have the tRNS chunk yet.
570
 */
571
static int /* PRIVATE */
572
png_gamma_threshold(png_fixed_point scrn_gamma, png_fixed_point file_gamma)
573
{
574
   /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
575
    * correction as a difference of the overall transform from 1.0
576
    *
577
    * We want to compare the threshold with s*f - 1, if we get
578
    * overflow here it is because of wacky gamma values so we
579
    * turn on processing anyway.
580
    */
581
   png_fixed_point gtest;
582
   return !png_muldiv(>est, scrn_gamma, file_gamma, PNG_FP_1) ||
583
       png_gamma_significant(gtest);
584
}
585
586
 
587
png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
588
   png_fixed_point file_gamma)
589
{
590
   png_debug(1, "in png_set_gamma_fixed");
591
592
 
593
      return;
594
595
 
596
       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
597
       png_gamma_threshold(scrn_gamma, file_gamma))
598
      png_ptr->transformations |= PNG_GAMMA;
599
   png_ptr->gamma = file_gamma;
600
   png_ptr->screen_gamma = scrn_gamma;
601
}
602
603
 
604
void PNGAPI
605
png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
606
{
607
   png_set_gamma_fixed(png_ptr,
608
      png_fixed(png_ptr, scrn_gamma, "png_set_gamma screen gamma"),
609
      png_fixed(png_ptr, file_gamma, "png_set_gamma file gamma"));
610
}
611
#  endif /* FLOATING_POINT_SUPPORTED */
612
#endif /* READ_GAMMA */
613
614
 
615
/* Expand paletted images to RGB, expand grayscale images of
616
 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
617
 * to alpha channels.
618
 */
619
void PNGAPI
620
png_set_expand(png_structp png_ptr)
621
{
622
   png_debug(1, "in png_set_expand");
623
624
 
625
      return;
626
627
 
628
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
629
}
630
631
 
632
 *  to png_set_expand().  However, it is entirely reasonable that someone
633
 *  might wish to expand an indexed image to RGB but *not* expand a single,
634
 *  fully transparent palette entry to a full alpha channel--perhaps instead
635
 *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
636
 *  the transparent color with a particular RGB value, or drop tRNS entirely.
637
 *  IOW, a future version of the library may make the transformations flag
638
 *  a bit more fine-grained, with separate bits for each of these three
639
 *  functions.
640
 *
641
 *  More to the point, these functions make it obvious what libpng will be
642
 *  doing, whereas "expand" can (and does) mean any number of things.
643
 *
644
 *  GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
645
 *  to expand only the sample depth but not to expand the tRNS to alpha
646
 *  and its name was changed to png_set_expand_gray_1_2_4_to_8().
647
 */
648
649
 
650
void PNGAPI
651
png_set_palette_to_rgb(png_structp png_ptr)
652
{
653
   png_debug(1, "in png_set_palette_to_rgb");
654
655
 
656
      return;
657
658
 
659
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
660
}
661
662
 
663
void PNGAPI
664
png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
665
{
666
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
667
668
 
669
      return;
670
671
 
672
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
673
}
674
675
 
676
 
677
 
678
void PNGAPI
679
png_set_tRNS_to_alpha(png_structp png_ptr)
680
{
681
   png_debug(1, "in png_set_tRNS_to_alpha");
682
683
 
684
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
685
}
686
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
687
688
 
689
void PNGAPI
690
png_set_gray_to_rgb(png_structp png_ptr)
691
{
692
   png_debug(1, "in png_set_gray_to_rgb");
693
694
 
695
   {
696
      /* Because rgb must be 8 bits or more: */
697
      png_set_expand_gray_1_2_4_to_8(png_ptr);
698
      png_ptr->transformations |= PNG_GRAY_TO_RGB;
699
      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
700
   }
701
}
702
#endif
703
704
 
705
void PNGFAPI
706
png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
707
    png_fixed_point red, png_fixed_point green)
708
{
709
   png_debug(1, "in png_set_rgb_to_gray");
710
711
 
712
      return;
713
714
 
715
   {
716
      case 1:
717
         png_ptr->transformations |= PNG_RGB_TO_GRAY;
718
         break;
719
720
 
721
         png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
722
         break;
723
724
 
725
         png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
726
         break;
727
728
 
729
         png_error(png_ptr, "invalid error action to rgb_to_gray");
730
         break;
731
   }
732
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
733
#ifdef PNG_READ_EXPAND_SUPPORTED
734
      png_ptr->transformations |= PNG_EXPAND;
735
#else
736
   {
737
      png_warning(png_ptr,
738
        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
739
740
 
741
   }
742
#endif
743
   {
744
      png_uint_16 red_int, green_int;
745
      if (red < 0 || green < 0)
746
      {
747
         red_int   =  6968; /* .212671 * 32768 + .5 */
748
         green_int = 23434; /* .715160 * 32768 + .5 */
749
      }
750
751
 
752
      {
753
         red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
754
         green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
755
      }
756
757
 
758
      {
759
         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
760
         red_int   =  6968;
761
         green_int = 23434;
762
      }
763
764
 
765
      png_ptr->rgb_to_gray_green_coeff = green_int;
766
      png_ptr->rgb_to_gray_blue_coeff  =
767
          (png_uint_16)(32768 - red_int - green_int);
768
   }
769
}
770
771
 
772
/* Convert a RGB image to a grayscale of the same width.  This allows us,
773
 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
774
 */
775
776
 
777
png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
778
   double green)
779
{
780
   if (png_ptr == NULL)
781
      return;
782
783
 
784
      png_fixed(png_ptr, red, "rgb to gray red coefficient"),
785
      png_fixed(png_ptr, green, "rgb to gray green coefficient"));
786
}
787
#endif /* FLOATING POINT */
788
789
 
790
791
 
792
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
793
void PNGAPI
794
png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
795
    read_user_transform_fn)
796
{
797
   png_debug(1, "in png_set_read_user_transform_fn");
798
799
 
800
      return;
801
802
 
803
   png_ptr->transformations |= PNG_USER_TRANSFORM;
804
   png_ptr->read_user_transform_fn = read_user_transform_fn;
805
#endif
806
}
807
#endif
808
809
 
810
 * the palette.
811
 */
812
void /* PRIVATE */
813
png_init_read_transformations(png_structp png_ptr)
814
{
815
   png_debug(1, "in png_init_read_transformations");
816
817
 
818
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
819
    defined(PNG_READ_SHIFT_SUPPORTED) || \
820
    defined(PNG_READ_GAMMA_SUPPORTED)
821
   int color_type = png_ptr->color_type;
822
#endif
823
824
 
825
826
 
827
   /* Detect gray background and attempt to enable optimization
828
    * for gray --> RGB case
829
    *
830
    * Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
831
    * RGB_ALPHA (in which case need_expand is superfluous anyway), the
832
    * background color might actually be gray yet not be flagged as such.
833
    * This is not a problem for the current code, which uses
834
    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
835
    * png_do_gray_to_rgb() transformation.
836
    */
837
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
838
       !(color_type & PNG_COLOR_MASK_COLOR))
839
   {
840
      png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
841
   }
842
843
 
844
       !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
845
       (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
846
       png_ptr->background.red == png_ptr->background.green &&
847
       png_ptr->background.red == png_ptr->background.blue)
848
   {
849
      png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
850
      png_ptr->background.gray = png_ptr->background.red;
851
   }
852
#endif
853
854
 
855
       (png_ptr->transformations & PNG_EXPAND))
856
   {
857
      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
858
      {
859
         /* Expand background and tRNS chunks */
860
         switch (png_ptr->bit_depth)
861
         {
862
            case 1:
863
               png_ptr->background.gray *= (png_uint_16)0xff;
864
               png_ptr->background.red = png_ptr->background.green
865
                   =  png_ptr->background.blue = png_ptr->background.gray;
866
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
867
               {
868
                 png_ptr->trans_color.gray *= (png_uint_16)0xff;
869
                 png_ptr->trans_color.red = png_ptr->trans_color.green
870
                     = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
871
               }
872
               break;
873
874
 
875
               png_ptr->background.gray *= (png_uint_16)0x55;
876
               png_ptr->background.red = png_ptr->background.green
877
                   = png_ptr->background.blue = png_ptr->background.gray;
878
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
879
               {
880
                  png_ptr->trans_color.gray *= (png_uint_16)0x55;
881
                  png_ptr->trans_color.red = png_ptr->trans_color.green
882
                      = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
883
               }
884
               break;
885
886
 
887
               png_ptr->background.gray *= (png_uint_16)0x11;
888
               png_ptr->background.red = png_ptr->background.green
889
                   = png_ptr->background.blue = png_ptr->background.gray;
890
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
891
               {
892
                  png_ptr->trans_color.gray *= (png_uint_16)0x11;
893
                  png_ptr->trans_color.red = png_ptr->trans_color.green
894
                      = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
895
               }
896
               break;
897
898
 
899
900
 
901
902
 
903
               png_ptr->background.red = png_ptr->background.green
904
                   = png_ptr->background.blue = png_ptr->background.gray;
905
               break;
906
         }
907
      }
908
      else if (color_type == PNG_COLOR_TYPE_PALETTE)
909
      {
910
         png_ptr->background.red   =
911
             png_ptr->palette[png_ptr->background.index].red;
912
         png_ptr->background.green =
913
             png_ptr->palette[png_ptr->background.index].green;
914
         png_ptr->background.blue  =
915
             png_ptr->palette[png_ptr->background.index].blue;
916
917
 
918
        if (png_ptr->transformations & PNG_INVERT_ALPHA)
919
        {
920
#ifdef PNG_READ_EXPAND_SUPPORTED
921
           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
922
#endif
923
           {
924
              /* Invert the alpha channel (in tRNS) unless the pixels are
925
               * going to be expanded, in which case leave it for later
926
               */
927
              int i, istop;
928
              istop=(int)png_ptr->num_trans;
929
              for (i=0; i
930
                 png_ptr->trans_alpha[i] = (png_byte)(255 -
931
                    png_ptr->trans_alpha[i]);
932
           }
933
        }
934
#endif
935
936
 
937
   }
938
#endif
939
940
 
941
   png_ptr->background_1 = png_ptr->background;
942
#endif
943
#ifdef PNG_READ_GAMMA_SUPPORTED
944
945
 
946
       && png_gamma_threshold(png_ptr->screen_gamma, png_ptr->gamma))
947
   {
948
      int i, k;
949
      k=0;
950
      for (i=0; inum_trans; i++)
951
      {
952
        if (png_ptr->trans_alpha[i] != 0 && png_ptr->trans_alpha[i] != 0xff)
953
           k=1; /* Partial transparency is present */
954
      }
955
      if (k == 0)
956
         png_ptr->transformations &= ~PNG_GAMMA;
957
   }
958
959
 
960
       png_ptr->gamma != 0)
961
   {
962
      png_build_gamma_table(png_ptr, png_ptr->bit_depth);
963
964
 
965
      if (png_ptr->transformations & PNG_BACKGROUND)
966
      {
967
         if (color_type == PNG_COLOR_TYPE_PALETTE)
968
         {
969
            /* Could skip if no transparency */
970
            png_color back, back_1;
971
            png_colorp palette = png_ptr->palette;
972
            int num_palette = png_ptr->num_palette;
973
            int i;
974
            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
975
            {
976
977
 
978
               back.green = png_ptr->gamma_table[png_ptr->background.green];
979
               back.blue = png_ptr->gamma_table[png_ptr->background.blue];
980
981
 
982
               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
983
               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
984
            }
985
            else
986
            {
987
               png_fixed_point g, gs;
988
989
 
990
               {
991
                  case PNG_BACKGROUND_GAMMA_SCREEN:
992
                     g = (png_ptr->screen_gamma);
993
                     gs = PNG_FP_1;
994
                     break;
995
996
 
997
                     g = png_reciprocal(png_ptr->gamma);
998
                     gs = png_reciprocal2(png_ptr->gamma,
999
                        png_ptr->screen_gamma);
1000
                     break;
1001
1002
 
1003
                     g = png_reciprocal(png_ptr->background_gamma);
1004
                     gs = png_reciprocal2(png_ptr->background_gamma,
1005
                        png_ptr->screen_gamma);
1006
                     break;
1007
                  default:
1008
                     g = PNG_FP_1;    /* back_1 */
1009
                     gs = PNG_FP_1;   /* back */
1010
                     break;
1011
               }
1012
1013
 
1014
               {
1015
                  back.red   = (png_byte)png_ptr->background.red;
1016
                  back.green = (png_byte)png_ptr->background.green;
1017
                  back.blue  = (png_byte)png_ptr->background.blue;
1018
               }
1019
1020
 
1021
               {
1022
                  back.red = png_gamma_8bit_correct(png_ptr->background.red,
1023
                      gs);
1024
                  back.green = png_gamma_8bit_correct(png_ptr->background.green,
1025
                      gs);
1026
                  back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1027
                      gs);
1028
               }
1029
               back_1.red = png_gamma_8bit_correct(png_ptr->background.red, g);
1030
               back_1.green = png_gamma_8bit_correct(png_ptr->background.green,
1031
                   g);
1032
               back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1033
                   g);
1034
            }
1035
            for (i = 0; i < num_palette; i++)
1036
            {
1037
               if (i < (int)png_ptr->num_trans &&
1038
                   png_ptr->trans_alpha[i] != 0xff)
1039
               {
1040
                  if (png_ptr->trans_alpha[i] == 0)
1041
                  {
1042
                     palette[i] = back;
1043
                  }
1044
                  else /* if (png_ptr->trans_alpha[i] != 0xff) */
1045
                  {
1046
                     png_byte v, w;
1047
1048
 
1049
                     png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
1050
                     palette[i].red = png_ptr->gamma_from_1[w];
1051
1052
 
1053
                     png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
1054
                     palette[i].green = png_ptr->gamma_from_1[w];
1055
1056
 
1057
                     png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
1058
                     palette[i].blue = png_ptr->gamma_from_1[w];
1059
                  }
1060
               }
1061
               else
1062
               {
1063
                  palette[i].red = png_ptr->gamma_table[palette[i].red];
1064
                  palette[i].green = png_ptr->gamma_table[palette[i].green];
1065
                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1066
               }
1067
            }
1068
            /* Prevent the transformations being done again, and make sure
1069
             * that the now spurious alpha channel is stripped - the code
1070
             * has just reduced background composition and gamma correction
1071
             * to a simple alpha channel strip.
1072
             */
1073
            png_ptr->transformations &= ~PNG_BACKGROUND;
1074
            png_ptr->transformations &= ~PNG_GAMMA;
1075
            png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
1076
         }
1077
1078
 
1079
         else
1080
         /* color_type != PNG_COLOR_TYPE_PALETTE */
1081
         {
1082
            png_fixed_point g = PNG_FP_1;
1083
            png_fixed_point gs = PNG_FP_1;
1084
1085
 
1086
            {
1087
               case PNG_BACKGROUND_GAMMA_SCREEN:
1088
                  g = png_ptr->screen_gamma;
1089
                  /* gs = PNG_FP_1; */
1090
                  break;
1091
1092
 
1093
                  g = png_reciprocal(png_ptr->gamma);
1094
                  gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
1095
                  break;
1096
1097
 
1098
                  g = png_reciprocal(png_ptr->background_gamma);
1099
                  gs = png_reciprocal2(png_ptr->background_gamma,
1100
                      png_ptr->screen_gamma);
1101
                  break;
1102
1103
 
1104
                  png_error(png_ptr, "invalid background gamma type");
1105
            }
1106
1107
 
1108
                png_ptr->background.gray, g);
1109
1110
 
1111
                png_ptr->background.gray, gs);
1112
1113
 
1114
                (png_ptr->background.red != png_ptr->background.blue) ||
1115
                (png_ptr->background.red != png_ptr->background.gray))
1116
            {
1117
               /* RGB or RGBA with color background */
1118
               png_ptr->background_1.red = png_gamma_correct(png_ptr,
1119
                   png_ptr->background.red, g);
1120
1121
 
1122
                   png_ptr->background.green, g);
1123
1124
 
1125
                   png_ptr->background.blue, g);
1126
1127
 
1128
                   png_ptr->background.red, gs);
1129
1130
 
1131
                   png_ptr->background.green, gs);
1132
1133
 
1134
                   png_ptr->background.blue, gs);
1135
            }
1136
1137
 
1138
            {
1139
               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1140
               png_ptr->background_1.red = png_ptr->background_1.green
1141
                   = png_ptr->background_1.blue = png_ptr->background_1.gray;
1142
1143
 
1144
                   = png_ptr->background.blue = png_ptr->background.gray;
1145
            }
1146
         }
1147
      }
1148
      else
1149
      /* Transformation does not include PNG_BACKGROUND */
1150
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
1151
      if (color_type == PNG_COLOR_TYPE_PALETTE)
1152
      {
1153
         png_colorp palette = png_ptr->palette;
1154
         int num_palette = png_ptr->num_palette;
1155
         int i;
1156
1157
 
1158
         {
1159
            palette[i].red = png_ptr->gamma_table[palette[i].red];
1160
            palette[i].green = png_ptr->gamma_table[palette[i].green];
1161
            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1162
         }
1163
1164
 
1165
         png_ptr->transformations &= ~PNG_GAMMA;
1166
      }
1167
   }
1168
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1169
   else
1170
#endif
1171
#endif /* PNG_READ_GAMMA_SUPPORTED */
1172
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1173
   /* No GAMMA transformation */
1174
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
1175
       (color_type == PNG_COLOR_TYPE_PALETTE))
1176
   {
1177
      int i;
1178
      int istop = (int)png_ptr->num_trans;
1179
      png_color back;
1180
      png_colorp palette = png_ptr->palette;
1181
1182
 
1183
      back.green = (png_byte)png_ptr->background.green;
1184
      back.blue  = (png_byte)png_ptr->background.blue;
1185
1186
 
1187
      {
1188
         if (png_ptr->trans_alpha[i] == 0)
1189
         {
1190
            palette[i] = back;
1191
         }
1192
1193
 
1194
         {
1195
            /* The png_composite() macro is defined in png.h */
1196
            png_composite(palette[i].red, palette[i].red,
1197
                png_ptr->trans_alpha[i], back.red);
1198
1199
 
1200
                png_ptr->trans_alpha[i], back.green);
1201
1202
 
1203
                png_ptr->trans_alpha[i], back.blue);
1204
         }
1205
      }
1206
1207
 
1208
      png_ptr->transformations &= ~PNG_BACKGROUND;
1209
      png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
1210
   }
1211
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
1212
1213
 
1214
   if ((png_ptr->transformations & PNG_SHIFT) &&
1215
       (color_type == PNG_COLOR_TYPE_PALETTE))
1216
   {
1217
      png_uint_16 i;
1218
      png_uint_16 istop = png_ptr->num_palette;
1219
      int sr = 8 - png_ptr->sig_bit.red;
1220
      int sg = 8 - png_ptr->sig_bit.green;
1221
      int sb = 8 - png_ptr->sig_bit.blue;
1222
1223
 
1224
         sr = 0;
1225
1226
 
1227
         sg = 0;
1228
1229
 
1230
         sb = 0;
1231
1232
 
1233
      {
1234
         png_ptr->palette[i].red >>= sr;
1235
         png_ptr->palette[i].green >>= sg;
1236
         png_ptr->palette[i].blue >>= sb;
1237
      }
1238
   }
1239
#endif  /* PNG_READ_SHIFT_SUPPORTED */
1240
 }
1241
#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1242
 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1243
   if (png_ptr)
1244
      return;
1245
#endif
1246
}
1247
1248
 
1249
 * info should be updated so a PNG file could be written with it,
1250
 * assuming the transformations result in valid PNG data.
1251
 */
1252
void /* PRIVATE */
1253
png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1254
{
1255
   png_debug(1, "in png_read_transform_info");
1256
1257
 
1258
   if (png_ptr->transformations & PNG_EXPAND)
1259
   {
1260
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1261
      {
1262
         if (png_ptr->num_trans &&
1263
             (png_ptr->transformations & PNG_EXPAND_tRNS))
1264
            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1265
1266
 
1267
            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1268
1269
 
1270
         info_ptr->num_trans = 0;
1271
      }
1272
      else
1273
      {
1274
         if (png_ptr->num_trans)
1275
         {
1276
            if (png_ptr->transformations & PNG_EXPAND_tRNS)
1277
               info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1278
         }
1279
         if (info_ptr->bit_depth < 8)
1280
            info_ptr->bit_depth = 8;
1281
1282
 
1283
      }
1284
   }
1285
#endif
1286
1287
 
1288
   if (png_ptr->transformations & PNG_BACKGROUND)
1289
   {
1290
      info_ptr->color_type = (png_byte)(info_ptr->color_type &
1291
          ~PNG_COLOR_MASK_ALPHA);
1292
      info_ptr->num_trans = 0;
1293
      info_ptr->background = png_ptr->background;
1294
   }
1295
#endif
1296
1297
 
1298
   if (png_ptr->transformations & PNG_GAMMA)
1299
   {
1300
      info_ptr->gamma = png_ptr->gamma;
1301
   }
1302
#endif
1303
1304
 
1305
#ifdef PNG_READ_16BIT_SUPPORTED
1306
   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1307
      info_ptr->bit_depth = 8;
1308
#else
1309
   /* Force chopping 16-bit input down to 8 */
1310
   if (info_ptr->bit_depth == 16)
1311
   {
1312
      png_ptr->transformations |=PNG_16_TO_8;
1313
      info_ptr->bit_depth = 8;
1314
   }
1315
#endif
1316
#endif
1317
1318
 
1319
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1320
      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1321
#endif
1322
1323
 
1324
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1325
      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1326
#endif
1327
1328
 
1329
   if (png_ptr->transformations & PNG_QUANTIZE)
1330
   {
1331
      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1332
          (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1333
          png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1334
      {
1335
         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1336
      }
1337
   }
1338
#endif
1339
1340
 
1341
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1342
      info_ptr->bit_depth = 8;
1343
#endif
1344
1345
 
1346
      info_ptr->channels = 1;
1347
1348
 
1349
      info_ptr->channels = 3;
1350
1351
 
1352
      info_ptr->channels = 1;
1353
1354
 
1355
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1356
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1357
#endif
1358
1359
 
1360
      info_ptr->channels++;
1361
1362
 
1363
   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
1364
   if ((png_ptr->transformations & PNG_FILLER) &&
1365
       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1366
       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1367
   {
1368
      info_ptr->channels++;
1369
      /* If adding a true alpha channel not just filler */
1370
      if (png_ptr->transformations & PNG_ADD_ALPHA)
1371
         info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1372
   }
1373
#endif
1374
1375
 
1376
defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1377
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
1378
   {
1379
      if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1380
         info_ptr->bit_depth = png_ptr->user_transform_depth;
1381
1382
 
1383
         info_ptr->channels = png_ptr->user_transform_channels;
1384
   }
1385
#endif
1386
1387
 
1388
       info_ptr->bit_depth);
1389
1390
 
1391
1392
 
1393
   if (png_ptr)
1394
      return;
1395
#endif
1396
}
1397
1398
 
1399
 * and is very touchy.  If you add a transformation, take care to
1400
 * decide how it fits in with the other transformations here.
1401
 */
1402
void /* PRIVATE */
1403
png_do_read_transformations(png_structp png_ptr)
1404
{
1405
   png_debug(1, "in png_do_read_transformations");
1406
1407
 
1408
   {
1409
#ifdef PNG_CONSOLE_IO_SUPPORTED
1410
      char msg[50];
1411
1412
 
1413
          "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
1414
          png_ptr->pass);
1415
      png_error(png_ptr, msg);
1416
#else
1417
      png_error(png_ptr, "NULL row buffer");
1418
#endif
1419
   }
1420
#ifdef PNG_WARN_UNINITIALIZED_ROW
1421
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
1422
      /* Application has failed to call either png_read_start_image()
1423
       * or png_read_update_info() after setting transforms that expand
1424
       * pixels.  This check added to libpng-1.2.19
1425
       */
1426
#if (PNG_WARN_UNINITIALIZED_ROW==1)
1427
      png_error(png_ptr, "Uninitialized row");
1428
#else
1429
      png_warning(png_ptr, "Uninitialized row");
1430
#endif
1431
#endif
1432
1433
 
1434
   if (png_ptr->transformations & PNG_EXPAND)
1435
   {
1436
      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
1437
      {
1438
         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
1439
             png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
1440
      }
1441
      else
1442
      {
1443
         if (png_ptr->num_trans &&
1444
             (png_ptr->transformations & PNG_EXPAND_tRNS))
1445
            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1446
                &(png_ptr->trans_color));
1447
         else
1448
1449
 
1450
                NULL);
1451
      }
1452
   }
1453
#endif
1454
1455
 
1456
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1457
      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1458
          PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
1459
#endif
1460
1461
 
1462
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1463
   {
1464
      int rgb_error =
1465
          png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
1466
              png_ptr->row_buf + 1);
1467
1468
 
1469
      {
1470
         png_ptr->rgb_to_gray_status=1;
1471
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1472
             PNG_RGB_TO_GRAY_WARN)
1473
            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1474
1475
 
1476
             PNG_RGB_TO_GRAY_ERR)
1477
            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1478
      }
1479
   }
1480
#endif
1481
1482
 
1483
 *
1484
 *   In most cases, the "simple transparency" should be done prior to doing
1485
 *   gray-to-RGB, or you will have to test 3x as many bytes to check if a
1486
 *   pixel is transparent.  You would also need to make sure that the
1487
 *   transparency information is upgraded to RGB.
1488
 *
1489
 *   To summarize, the current flow is:
1490
 *   - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1491
 *                                   with background "in place" if transparent,
1492
 *                                   convert to RGB if necessary
1493
 *   - Gray + alpha -> composite with gray background and remove alpha bytes,
1494
 *                                   convert to RGB if necessary
1495
 *
1496
 *   To support RGB backgrounds for gray images we need:
1497
 *   - Gray + simple transparency -> convert to RGB + simple transparency,
1498
 *                                   compare 3 or 6 bytes and composite with
1499
 *                                   background "in place" if transparent
1500
 *                                   (3x compare/pixel compared to doing
1501
 *                                   composite with gray bkgrnd)
1502
 *   - Gray + alpha -> convert to RGB + alpha, composite with background and
1503
 *                                   remove alpha bytes (3x float
1504
 *                                   operations/pixel compared with composite
1505
 *                                   on gray background)
1506
 *
1507
 *  Greg's change will do this.  The reason it wasn't done before is for
1508
 *  performance, as this increases the per-pixel operations.  If we would check
1509
 *  in advance if the background was gray or RGB, and position the gray-to-RGB
1510
 *  transform appropriately, then it would save a lot of work/time.
1511
 */
1512
1513
 
1514
   /* If gray -> RGB, do so now only if background is non-gray; else do later
1515
    * for performance reasons
1516
    */
1517
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1518
       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1519
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1520
#endif
1521
1522
 
1523
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
1524
       ((png_ptr->num_trans != 0) ||
1525
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1526
      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1527
          &(png_ptr->trans_color), &(png_ptr->background)
1528
#ifdef PNG_READ_GAMMA_SUPPORTED
1529
          , &(png_ptr->background_1),
1530
          png_ptr->gamma_table, png_ptr->gamma_from_1,
1531
          png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1532
          png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1533
          png_ptr->gamma_shift
1534
#endif
1535
          );
1536
#endif
1537
1538
 
1539
   if ((png_ptr->transformations & PNG_GAMMA) &&
1540
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1541
       !((png_ptr->transformations & PNG_BACKGROUND) &&
1542
       ((png_ptr->num_trans != 0) ||
1543
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1544
#endif
1545
       (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
1546
      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
1547
          png_ptr->gamma_table, png_ptr->gamma_16_table,
1548
          png_ptr->gamma_shift);
1549
#endif
1550
1551
 
1552
   if (png_ptr->transformations & PNG_16_TO_8)
1553
      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1554
#endif
1555
1556
 
1557
   if (png_ptr->transformations & PNG_QUANTIZE)
1558
   {
1559
      png_do_quantize(&(png_ptr->row_info), png_ptr->row_buf + 1,
1560
          png_ptr->palette_lookup, png_ptr->quantize_index);
1561
1562
 
1563
         png_error(png_ptr, "png_do_quantize returned rowbytes=0");
1564
   }
1565
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
1566
1567
 
1568
   if (png_ptr->transformations & PNG_INVERT_MONO)
1569
      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
1570
#endif
1571
1572
 
1573
   if (png_ptr->transformations & PNG_SHIFT)
1574
      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
1575
          &(png_ptr->shift));
1576
#endif
1577
1578
 
1579
   if (png_ptr->transformations & PNG_PACK)
1580
      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
1581
#endif
1582
1583
 
1584
   if (png_ptr->transformations & PNG_BGR)
1585
      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1586
#endif
1587
1588
 
1589
   if (png_ptr->transformations & PNG_PACKSWAP)
1590
      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1591
#endif
1592
1593
 
1594
   /* If gray -> RGB, do so now only if we did not do so above */
1595
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1596
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1597
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1598
#endif
1599
1600
 
1601
   if (png_ptr->transformations & PNG_FILLER)
1602
      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1603
          (png_uint_32)png_ptr->filler, png_ptr->flags);
1604
#endif
1605
1606
 
1607
   if (png_ptr->transformations & PNG_INVERT_ALPHA)
1608
      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1609
#endif
1610
1611
 
1612
   if (png_ptr->transformations & PNG_SWAP_ALPHA)
1613
      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1614
#endif
1615
1616
 
1617
#ifdef PNG_READ_SWAP_SUPPORTED
1618
   if (png_ptr->transformations & PNG_SWAP_BYTES)
1619
      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1620
#endif
1621
#endif
1622
1623
 
1624
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
1625
    {
1626
      if (png_ptr->read_user_transform_fn != NULL)
1627
         (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1628
             (png_ptr,                    /* png_ptr */
1629
             &(png_ptr->row_info),     /* row_info: */
1630
                /*  png_uint_32 width;       width of row */
1631
                /*  png_size_t rowbytes;     number of bytes in row */
1632
                /*  png_byte color_type;     color type of pixels */
1633
                /*  png_byte bit_depth;      bit depth of samples */
1634
                /*  png_byte channels;       number of channels (1-4) */
1635
                /*  png_byte pixel_depth;    bits per pixel (depth*channels) */
1636
             png_ptr->row_buf + 1);    /* start of pixel data for row */
1637
#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1638
      if (png_ptr->user_transform_depth)
1639
         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1640
1641
 
1642
         png_ptr->row_info.channels = png_ptr->user_transform_channels;
1643
#endif
1644
      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
1645
          png_ptr->row_info.channels);
1646
1647
 
1648
          png_ptr->row_info.width);
1649
   }
1650
#endif
1651
1652
 
1653
1654
 
1655
/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1656
 * without changing the actual values.  Thus, if you had a row with
1657
 * a bit depth of 1, you would end up with bytes that only contained
1658
 * the numbers 0 or 1.  If you would rather they contain 0 and 255, use
1659
 * png_do_shift() after this.
1660
 */
1661
void /* PRIVATE */
1662
png_do_unpack(png_row_infop row_info, png_bytep row)
1663
{
1664
   png_debug(1, "in png_do_unpack");
1665
1666
 
1667
   {
1668
      png_uint_32 i;
1669
      png_uint_32 row_width=row_info->width;
1670
1671
 
1672
      {
1673
         case 1:
1674
         {
1675
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
1676
            png_bytep dp = row + (png_size_t)row_width - 1;
1677
            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1678
            for (i = 0; i < row_width; i++)
1679
            {
1680
               *dp = (png_byte)((*sp >> shift) & 0x01);
1681
1682
 
1683
               {
1684
                  shift = 0;
1685
                  sp--;
1686
               }
1687
1688
 
1689
                  shift++;
1690
1691
 
1692
            }
1693
            break;
1694
         }
1695
1696
 
1697
         {
1698
1699
 
1700
            png_bytep dp = row + (png_size_t)row_width - 1;
1701
            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1702
            for (i = 0; i < row_width; i++)
1703
            {
1704
               *dp = (png_byte)((*sp >> shift) & 0x03);
1705
1706
 
1707
               {
1708
                  shift = 0;
1709
                  sp--;
1710
               }
1711
1712
 
1713
                  shift += 2;
1714
1715
 
1716
            }
1717
            break;
1718
         }
1719
1720
 
1721
         {
1722
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
1723
            png_bytep dp = row + (png_size_t)row_width - 1;
1724
            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1725
            for (i = 0; i < row_width; i++)
1726
            {
1727
               *dp = (png_byte)((*sp >> shift) & 0x0f);
1728
1729
 
1730
               {
1731
                  shift = 0;
1732
                  sp--;
1733
               }
1734
1735
 
1736
                  shift = 4;
1737
1738
 
1739
            }
1740
            break;
1741
         }
1742
1743
 
1744
            break;
1745
      }
1746
      row_info->bit_depth = 8;
1747
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1748
      row_info->rowbytes = row_width * row_info->channels;
1749
   }
1750
}
1751
#endif
1752
1753
 
1754
/* Reverse the effects of png_do_shift.  This routine merely shifts the
1755
 * pixels back to their significant bits values.  Thus, if you have
1756
 * a row of bit depth 8, but only 5 are significant, this will shift
1757
 * the values back to 0 through 31.
1758
 */
1759
void /* PRIVATE */
1760
png_do_unshift(png_row_infop row_info, png_bytep row,
1761
    png_const_color_8p sig_bits)
1762
{
1763
   png_debug(1, "in png_do_unshift");
1764
1765
 
1766
       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1767
   {
1768
      int shift[4];
1769
      int channels = 0;
1770
      int c;
1771
      png_uint_16 value = 0;
1772
      png_uint_32 row_width = row_info->width;
1773
1774
 
1775
      {
1776
         shift[channels++] = row_info->bit_depth - sig_bits->red;
1777
         shift[channels++] = row_info->bit_depth - sig_bits->green;
1778
         shift[channels++] = row_info->bit_depth - sig_bits->blue;
1779
      }
1780
1781
 
1782
      {
1783
         shift[channels++] = row_info->bit_depth - sig_bits->gray;
1784
      }
1785
1786
 
1787
      {
1788
         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
1789
      }
1790
1791
 
1792
      {
1793
         if (shift[c] <= 0)
1794
            shift[c] = 0;
1795
1796
 
1797
            value = 1;
1798
      }
1799
1800
 
1801
         return;
1802
1803
 
1804
      {
1805
         default:
1806
            break;
1807
1808
 
1809
         {
1810
            png_bytep bp;
1811
            png_size_t i;
1812
            png_size_t istop = row_info->rowbytes;
1813
1814
 
1815
            {
1816
               *bp >>= 1;
1817
               *bp++ &= 0x55;
1818
            }
1819
            break;
1820
         }
1821
1822
 
1823
         {
1824
            png_bytep bp = row;
1825
            png_size_t i;
1826
            png_size_t istop = row_info->rowbytes;
1827
            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
1828
                (png_byte)((int)0xf >> shift[0]));
1829
1830
 
1831
            {
1832
               *bp >>= shift[0];
1833
               *bp++ &= mask;
1834
            }
1835
            break;
1836
         }
1837
1838
 
1839
         {
1840
            png_bytep bp = row;
1841
            png_uint_32 i;
1842
            png_uint_32 istop = row_width * channels;
1843
1844
 
1845
            {
1846
               *bp++ >>= shift[i%channels];
1847
            }
1848
            break;
1849
         }
1850
1851
 
1852
         case 16:
1853
         {
1854
            png_bytep bp = row;
1855
            png_uint_32 i;
1856
            png_uint_32 istop = channels * row_width;
1857
1858
 
1859
            {
1860
               value = (png_uint_16)((*bp << 8) + *(bp + 1));
1861
               value >>= shift[i%channels];
1862
               *bp++ = (png_byte)(value >> 8);
1863
               *bp++ = (png_byte)(value & 0xff);
1864
            }
1865
            break;
1866
         }
1867
#endif
1868
      }
1869
   }
1870
}
1871
#endif
1872
1873
 
1874
/* Chop rows of bit depth 16 down to 8 */
1875
void /* PRIVATE */
1876
png_do_chop(png_row_infop row_info, png_bytep row)
1877
{
1878
   png_debug(1, "in png_do_chop");
1879
1880
 
1881
   {
1882
      png_bytep sp = row;
1883
      png_bytep dp = row;
1884
      png_uint_32 i;
1885
      png_uint_32 istop = row_info->width * row_info->channels;
1886
1887
 
1888
      {
1889
#ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1890
      /* This does a more accurate scaling of the 16-bit color
1891
       * value, rather than a simple low-byte truncation.
1892
       *
1893
       * What the ideal calculation should be:
1894
       *   *dp = (((((png_uint_32)(*sp) << 8) |
1895
       *          (png_uint_32)(*(sp + 1))) * 255 + 127)
1896
       *          / (png_uint_32)65535L;
1897
       *
1898
       * GRR: no, I think this is what it really should be:
1899
       *   *dp = (((((png_uint_32)(*sp) << 8) |
1900
       *           (png_uint_32)(*(sp + 1))) + 128L)
1901
       *           / (png_uint_32)257L;
1902
       *
1903
       * GRR: here's the exact calculation with shifts:
1904
       *   temp = (((png_uint_32)(*sp) << 8) |
1905
       *           (png_uint_32)(*(sp + 1))) + 128L;
1906
       *   *dp = (temp - (temp >> 8)) >> 8;
1907
       *
1908
       * Approximate calculation with shift/add instead of multiply/divide:
1909
       *   *dp = ((((png_uint_32)(*sp) << 8) |
1910
       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1911
       *
1912
       * What we actually do to avoid extra shifting and conversion:
1913
       */
1914
1915
 
1916
#else
1917
       /* Simply discard the low order byte */
1918
         *dp = *sp;
1919
#endif
1920
      }
1921
      row_info->bit_depth = 8;
1922
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1923
      row_info->rowbytes = row_info->width * row_info->channels;
1924
   }
1925
}
1926
#endif
1927
1928
 
1929
void /* PRIVATE */
1930
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
1931
{
1932
   png_debug(1, "in png_do_read_swap_alpha");
1933
1934
 
1935
      png_uint_32 row_width = row_info->width;
1936
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1937
      {
1938
         /* This converts from RGBA to ARGB */
1939
         if (row_info->bit_depth == 8)
1940
         {
1941
            png_bytep sp = row + row_info->rowbytes;
1942
            png_bytep dp = sp;
1943
            png_byte save;
1944
            png_uint_32 i;
1945
1946
 
1947
            {
1948
               save = *(--sp);
1949
               *(--dp) = *(--sp);
1950
               *(--dp) = *(--sp);
1951
               *(--dp) = *(--sp);
1952
               *(--dp) = save;
1953
            }
1954
         }
1955
1956
 
1957
         /* This converts from RRGGBBAA to AARRGGBB */
1958
         else
1959
         {
1960
            png_bytep sp = row + row_info->rowbytes;
1961
            png_bytep dp = sp;
1962
            png_byte save[2];
1963
            png_uint_32 i;
1964
1965
 
1966
            {
1967
               save[0] = *(--sp);
1968
               save[1] = *(--sp);
1969
               *(--dp) = *(--sp);
1970
               *(--dp) = *(--sp);
1971
               *(--dp) = *(--sp);
1972
               *(--dp) = *(--sp);
1973
               *(--dp) = *(--sp);
1974
               *(--dp) = *(--sp);
1975
               *(--dp) = save[0];
1976
               *(--dp) = save[1];
1977
            }
1978
         }
1979
#endif
1980
      }
1981
1982
 
1983
      {
1984
         /* This converts from GA to AG */
1985
         if (row_info->bit_depth == 8)
1986
         {
1987
            png_bytep sp = row + row_info->rowbytes;
1988
            png_bytep dp = sp;
1989
            png_byte save;
1990
            png_uint_32 i;
1991
1992
 
1993
            {
1994
               save = *(--sp);
1995
               *(--dp) = *(--sp);
1996
               *(--dp) = save;
1997
            }
1998
         }
1999
2000
 
2001
         /* This converts from GGAA to AAGG */
2002
         else
2003
         {
2004
            png_bytep sp = row + row_info->rowbytes;
2005
            png_bytep dp = sp;
2006
            png_byte save[2];
2007
            png_uint_32 i;
2008
2009
 
2010
            {
2011
               save[0] = *(--sp);
2012
               save[1] = *(--sp);
2013
               *(--dp) = *(--sp);
2014
               *(--dp) = *(--sp);
2015
               *(--dp) = save[0];
2016
               *(--dp) = save[1];
2017
            }
2018
         }
2019
#endif
2020
      }
2021
   }
2022
}
2023
#endif
2024
2025
 
2026
void /* PRIVATE */
2027
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2028
{
2029
   png_uint_32 row_width;
2030
   png_debug(1, "in png_do_read_invert_alpha");
2031
2032
 
2033
   if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2034
   {
2035
      if (row_info->bit_depth == 8)
2036
      {
2037
         /* This inverts the alpha channel in RGBA */
2038
         png_bytep sp = row + row_info->rowbytes;
2039
         png_bytep dp = sp;
2040
         png_uint_32 i;
2041
2042
 
2043
         {
2044
            *(--dp) = (png_byte)(255 - *(--sp));
2045
2046
 
2047
            *(--dp) = *(--sp);
2048
            *(--dp) = *(--sp);
2049
            *(--dp) = *(--sp);
2050
            We can replace it with:
2051
*/
2052
            sp-=3;
2053
            dp=sp;
2054
         }
2055
      }
2056
2057
 
2058
      /* This inverts the alpha channel in RRGGBBAA */
2059
      else
2060
      {
2061
         png_bytep sp = row + row_info->rowbytes;
2062
         png_bytep dp = sp;
2063
         png_uint_32 i;
2064
2065
 
2066
         {
2067
            *(--dp) = (png_byte)(255 - *(--sp));
2068
            *(--dp) = (png_byte)(255 - *(--sp));
2069
2070
 
2071
            *(--dp) = *(--sp);
2072
            *(--dp) = *(--sp);
2073
            *(--dp) = *(--sp);
2074
            *(--dp) = *(--sp);
2075
            *(--dp) = *(--sp);
2076
            *(--dp) = *(--sp);
2077
            We can replace it with:
2078
*/
2079
            sp-=6;
2080
            dp=sp;
2081
         }
2082
      }
2083
#endif
2084
   }
2085
   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2086
   {
2087
      if (row_info->bit_depth == 8)
2088
      {
2089
         /* This inverts the alpha channel in GA */
2090
         png_bytep sp = row + row_info->rowbytes;
2091
         png_bytep dp = sp;
2092
         png_uint_32 i;
2093
2094
 
2095
         {
2096
            *(--dp) = (png_byte)(255 - *(--sp));
2097
            *(--dp) = *(--sp);
2098
         }
2099
      }
2100
2101
 
2102
      else
2103
      {
2104
         /* This inverts the alpha channel in GGAA */
2105
         png_bytep sp  = row + row_info->rowbytes;
2106
         png_bytep dp = sp;
2107
         png_uint_32 i;
2108
2109
 
2110
         {
2111
            *(--dp) = (png_byte)(255 - *(--sp));
2112
            *(--dp) = (png_byte)(255 - *(--sp));
2113
/*
2114
            *(--dp) = *(--sp);
2115
            *(--dp) = *(--sp);
2116
*/
2117
            sp-=2;
2118
            dp=sp;
2119
         }
2120
      }
2121
#endif
2122
   }
2123
}
2124
#endif
2125
2126
 
2127
/* Add filler channel if we have RGB color */
2128
void /* PRIVATE */
2129
png_do_read_filler(png_row_infop row_info, png_bytep row,
2130
    png_uint_32 filler, png_uint_32 flags)
2131
{
2132
   png_uint_32 i;
2133
   png_uint_32 row_width = row_info->width;
2134
2135
 
2136
   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
2137
#endif
2138
   png_byte lo_filler = (png_byte)(filler & 0xff);
2139
2140
 
2141
2142
 
2143
       row_info->color_type == PNG_COLOR_TYPE_GRAY)
2144
   {
2145
      if (row_info->bit_depth == 8)
2146
      {
2147
         if (flags & PNG_FLAG_FILLER_AFTER)
2148
         {
2149
            /* This changes the data from G to GX */
2150
            png_bytep sp = row + (png_size_t)row_width;
2151
            png_bytep dp =  sp + (png_size_t)row_width;
2152
            for (i = 1; i < row_width; i++)
2153
            {
2154
               *(--dp) = lo_filler;
2155
               *(--dp) = *(--sp);
2156
            }
2157
            *(--dp) = lo_filler;
2158
            row_info->channels = 2;
2159
            row_info->pixel_depth = 16;
2160
            row_info->rowbytes = row_width * 2;
2161
         }
2162
2163
 
2164
         {
2165
            /* This changes the data from G to XG */
2166
            png_bytep sp = row + (png_size_t)row_width;
2167
            png_bytep dp = sp  + (png_size_t)row_width;
2168
            for (i = 0; i < row_width; i++)
2169
            {
2170
               *(--dp) = *(--sp);
2171
               *(--dp) = lo_filler;
2172
            }
2173
            row_info->channels = 2;
2174
            row_info->pixel_depth = 16;
2175
            row_info->rowbytes = row_width * 2;
2176
         }
2177
      }
2178
2179
 
2180
      else if (row_info->bit_depth == 16)
2181
      {
2182
         if (flags & PNG_FLAG_FILLER_AFTER)
2183
         {
2184
            /* This changes the data from GG to GGXX */
2185
            png_bytep sp = row + (png_size_t)row_width * 2;
2186
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2187
            for (i = 1; i < row_width; i++)
2188
            {
2189
               *(--dp) = hi_filler;
2190
               *(--dp) = lo_filler;
2191
               *(--dp) = *(--sp);
2192
               *(--dp) = *(--sp);
2193
            }
2194
            *(--dp) = hi_filler;
2195
            *(--dp) = lo_filler;
2196
            row_info->channels = 2;
2197
            row_info->pixel_depth = 32;
2198
            row_info->rowbytes = row_width * 4;
2199
         }
2200
2201
 
2202
         {
2203
            /* This changes the data from GG to XXGG */
2204
            png_bytep sp = row + (png_size_t)row_width * 2;
2205
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2206
            for (i = 0; i < row_width; i++)
2207
            {
2208
               *(--dp) = *(--sp);
2209
               *(--dp) = *(--sp);
2210
               *(--dp) = hi_filler;
2211
               *(--dp) = lo_filler;
2212
            }
2213
            row_info->channels = 2;
2214
            row_info->pixel_depth = 32;
2215
            row_info->rowbytes = row_width * 4;
2216
         }
2217
      }
2218
#endif
2219
   } /* COLOR_TYPE == GRAY */
2220
   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2221
   {
2222
      if (row_info->bit_depth == 8)
2223
      {
2224
         if (flags & PNG_FLAG_FILLER_AFTER)
2225
         {
2226
            /* This changes the data from RGB to RGBX */
2227
            png_bytep sp = row + (png_size_t)row_width * 3;
2228
            png_bytep dp = sp  + (png_size_t)row_width;
2229
            for (i = 1; i < row_width; i++)
2230
            {
2231
               *(--dp) = lo_filler;
2232
               *(--dp) = *(--sp);
2233
               *(--dp) = *(--sp);
2234
               *(--dp) = *(--sp);
2235
            }
2236
            *(--dp) = lo_filler;
2237
            row_info->channels = 4;
2238
            row_info->pixel_depth = 32;
2239
            row_info->rowbytes = row_width * 4;
2240
         }
2241
2242
 
2243
         {
2244
            /* This changes the data from RGB to XRGB */
2245
            png_bytep sp = row + (png_size_t)row_width * 3;
2246
            png_bytep dp = sp + (png_size_t)row_width;
2247
            for (i = 0; i < row_width; i++)
2248
            {
2249
               *(--dp) = *(--sp);
2250
               *(--dp) = *(--sp);
2251
               *(--dp) = *(--sp);
2252
               *(--dp) = lo_filler;
2253
            }
2254
            row_info->channels = 4;
2255
            row_info->pixel_depth = 32;
2256
            row_info->rowbytes = row_width * 4;
2257
         }
2258
      }
2259
2260
 
2261
      else if (row_info->bit_depth == 16)
2262
      {
2263
         if (flags & PNG_FLAG_FILLER_AFTER)
2264
         {
2265
            /* This changes the data from RRGGBB to RRGGBBXX */
2266
            png_bytep sp = row + (png_size_t)row_width * 6;
2267
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2268
            for (i = 1; i < row_width; i++)
2269
            {
2270
               *(--dp) = hi_filler;
2271
               *(--dp) = lo_filler;
2272
               *(--dp) = *(--sp);
2273
               *(--dp) = *(--sp);
2274
               *(--dp) = *(--sp);
2275
               *(--dp) = *(--sp);
2276
               *(--dp) = *(--sp);
2277
               *(--dp) = *(--sp);
2278
            }
2279
            *(--dp) = hi_filler;
2280
            *(--dp) = lo_filler;
2281
            row_info->channels = 4;
2282
            row_info->pixel_depth = 64;
2283
            row_info->rowbytes = row_width * 8;
2284
         }
2285
2286
 
2287
         {
2288
            /* This changes the data from RRGGBB to XXRRGGBB */
2289
            png_bytep sp = row + (png_size_t)row_width * 6;
2290
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2291
            for (i = 0; i < row_width; i++)
2292
            {
2293
               *(--dp) = *(--sp);
2294
               *(--dp) = *(--sp);
2295
               *(--dp) = *(--sp);
2296
               *(--dp) = *(--sp);
2297
               *(--dp) = *(--sp);
2298
               *(--dp) = *(--sp);
2299
               *(--dp) = hi_filler;
2300
               *(--dp) = lo_filler;
2301
            }
2302
2303
 
2304
            row_info->pixel_depth = 64;
2305
            row_info->rowbytes = row_width * 8;
2306
         }
2307
      }
2308
#endif
2309
   } /* COLOR_TYPE == RGB */
2310
}
2311
#endif
2312
2313
 
2314
/* Expand grayscale files to RGB, with or without alpha */
2315
void /* PRIVATE */
2316
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2317
{
2318
   png_uint_32 i;
2319
   png_uint_32 row_width = row_info->width;
2320
2321
 
2322
2323
 
2324
       !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2325
   {
2326
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2327
      {
2328
         if (row_info->bit_depth == 8)
2329
         {
2330
            /* This changes G to RGB */
2331
            png_bytep sp = row + (png_size_t)row_width - 1;
2332
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2333
            for (i = 0; i < row_width; i++)
2334
            {
2335
               *(dp--) = *sp;
2336
               *(dp--) = *sp;
2337
               *(dp--) = *(sp--);
2338
            }
2339
         }
2340
2341
 
2342
         {
2343
            /* This changes GG to RRGGBB */
2344
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2345
            png_bytep dp = sp  + (png_size_t)row_width * 4;
2346
            for (i = 0; i < row_width; i++)
2347
            {
2348
               *(dp--) = *sp;
2349
               *(dp--) = *(sp - 1);
2350
               *(dp--) = *sp;
2351
               *(dp--) = *(sp - 1);
2352
               *(dp--) = *(sp--);
2353
               *(dp--) = *(sp--);
2354
            }
2355
         }
2356
      }
2357
2358
 
2359
      {
2360
         if (row_info->bit_depth == 8)
2361
         {
2362
            /* This changes GA to RGBA */
2363
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2364
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2365
            for (i = 0; i < row_width; i++)
2366
            {
2367
               *(dp--) = *(sp--);
2368
               *(dp--) = *sp;
2369
               *(dp--) = *sp;
2370
               *(dp--) = *(sp--);
2371
            }
2372
         }
2373
2374
 
2375
         {
2376
            /* This changes GGAA to RRGGBBAA */
2377
            png_bytep sp = row + (png_size_t)row_width * 4 - 1;
2378
            png_bytep dp = sp  + (png_size_t)row_width * 4;
2379
            for (i = 0; i < row_width; i++)
2380
            {
2381
               *(dp--) = *(sp--);
2382
               *(dp--) = *(sp--);
2383
               *(dp--) = *sp;
2384
               *(dp--) = *(sp - 1);
2385
               *(dp--) = *sp;
2386
               *(dp--) = *(sp - 1);
2387
               *(dp--) = *(sp--);
2388
               *(dp--) = *(sp--);
2389
            }
2390
         }
2391
      }
2392
      row_info->channels += (png_byte)2;
2393
      row_info->color_type |= PNG_COLOR_MASK_COLOR;
2394
      row_info->pixel_depth = (png_byte)(row_info->channels *
2395
          row_info->bit_depth);
2396
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2397
   }
2398
}
2399
#endif
2400
2401
 
2402
/* Reduce RGB files to grayscale, with or without alpha
2403
 * using the equation given in Poynton's ColorFAQ at
2404
 *   (THIS LINK IS DEAD June 2008)
2405
 * New link:
2406
 * 
2407
 * Charles Poynton poynton at poynton.com
2408
 *
2409
 *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2410
 *
2411
 *  We approximate this with
2412
 *
2413
 *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
2414
 *
2415
 *  which can be expressed with integers as
2416
 *
2417
 *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
2418
 *
2419
 *  The calculation is to be done in a linear colorspace.
2420
 *
2421
 *  Other integer coefficents can be used via png_set_rgb_to_gray().
2422
 */
2423
int /* PRIVATE */
2424
png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
2425
2426
 
2427
   png_uint_32 i;
2428
2429
 
2430
   int rgb_error = 0;
2431
2432
 
2433
2434
 
2435
       (row_info->color_type & PNG_COLOR_MASK_COLOR))
2436
   {
2437
      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
2438
      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
2439
      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
2440
2441
 
2442
      {
2443
         if (row_info->bit_depth == 8)
2444
         {
2445
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2446
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2447
            {
2448
               png_bytep sp = row;
2449
               png_bytep dp = row;
2450
2451
 
2452
               {
2453
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
2454
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
2455
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
2456
2457
 
2458
                  {
2459
                     rgb_error |= 1;
2460
                     *(dp++) = png_ptr->gamma_from_1[
2461
                         (rc*red + gc*green + bc*blue)>>15];
2462
                  }
2463
2464
 
2465
                     *(dp++) = *(sp - 1);
2466
               }
2467
            }
2468
            else
2469
#endif
2470
            {
2471
               png_bytep sp = row;
2472
               png_bytep dp = row;
2473
               for (i = 0; i < row_width; i++)
2474
               {
2475
                  png_byte red   = *(sp++);
2476
                  png_byte green = *(sp++);
2477
                  png_byte blue  = *(sp++);
2478
2479
 
2480
                  {
2481
                     rgb_error |= 1;
2482
                     *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2483
                  }
2484
2485
 
2486
                     *(dp++) = *(sp - 1);
2487
               }
2488
            }
2489
         }
2490
2491
 
2492
         {
2493
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2494
            if (png_ptr->gamma_16_to_1 != NULL &&
2495
                png_ptr->gamma_16_from_1 != NULL)
2496
            {
2497
               png_bytep sp = row;
2498
               png_bytep dp = row;
2499
               for (i = 0; i < row_width; i++)
2500
               {
2501
                  png_uint_16 red, green, blue, w;
2502
2503
 
2504
                  green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2505
                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2506
2507
 
2508
                     w = red;
2509
2510
 
2511
                  {
2512
                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff)
2513
                         >> png_ptr->gamma_shift][red>>8];
2514
                     png_uint_16 green_1 =
2515
                         png_ptr->gamma_16_to_1[(green&0xff) >>
2516
                         png_ptr->gamma_shift][green>>8];
2517
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff)
2518
                         >> png_ptr->gamma_shift][blue>>8];
2519
                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
2520
                         + bc*blue_1)>>15);
2521
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2522
                         png_ptr->gamma_shift][gray16 >> 8];
2523
                     rgb_error |= 1;
2524
                  }
2525
2526
 
2527
                  *(dp++) = (png_byte)(w & 0xff);
2528
               }
2529
            }
2530
            else
2531
#endif
2532
            {
2533
               png_bytep sp = row;
2534
               png_bytep dp = row;
2535
               for (i = 0; i < row_width; i++)
2536
               {
2537
                  png_uint_16 red, green, blue, gray16;
2538
2539
 
2540
                  green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2541
                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2542
2543
 
2544
                     rgb_error |= 1;
2545
2546
 
2547
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
2548
                  *(dp++) = (png_byte)(gray16 & 0xff);
2549
               }
2550
            }
2551
         }
2552
      }
2553
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2554
      {
2555
         if (row_info->bit_depth == 8)
2556
         {
2557
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2558
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2559
            {
2560
               png_bytep sp = row;
2561
               png_bytep dp = row;
2562
               for (i = 0; i < row_width; i++)
2563
               {
2564
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
2565
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
2566
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
2567
2568
 
2569
                     rgb_error |= 1;
2570
2571
 
2572
                      [(rc*red + gc*green + bc*blue)>>15];
2573
2574
 
2575
               }
2576
            }
2577
            else
2578
#endif
2579
            {
2580
               png_bytep sp = row;
2581
               png_bytep dp = row;
2582
               for (i = 0; i < row_width; i++)
2583
               {
2584
                  png_byte red   = *(sp++);
2585
                  png_byte green = *(sp++);
2586
                  png_byte blue  = *(sp++);
2587
                  if (red != green || red != blue)
2588
                     rgb_error |= 1;
2589
2590
 
2591
                  *(dp++) = *(sp++);  /* alpha */
2592
               }
2593
            }
2594
         }
2595
         else /* RGBA bit_depth == 16 */
2596
         {
2597
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2598
            if (png_ptr->gamma_16_to_1 != NULL &&
2599
                png_ptr->gamma_16_from_1 != NULL)
2600
            {
2601
               png_bytep sp = row;
2602
               png_bytep dp = row;
2603
               for (i = 0; i < row_width; i++)
2604
               {
2605
                  png_uint_16 red, green, blue, w;
2606
2607
 
2608
                  green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2609
                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2610
2611
 
2612
                     w = red;
2613
2614
 
2615
                  {
2616
                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
2617
                         png_ptr->gamma_shift][red>>8];
2618
2619
 
2620
                         png_ptr->gamma_16_to_1[(green&0xff) >>
2621
                         png_ptr->gamma_shift][green>>8];
2622
2623
 
2624
                         png_ptr->gamma_shift][blue>>8];
2625
2626
 
2627
                         + gc * green_1 + bc * blue_1)>>15);
2628
2629
 
2630
                         png_ptr->gamma_shift][gray16 >> 8];
2631
2632
 
2633
                  }
2634
2635
 
2636
                  *(dp++) = (png_byte)(w & 0xff);
2637
                  *(dp++) = *(sp++);  /* alpha */
2638
                  *(dp++) = *(sp++);
2639
               }
2640
            }
2641
            else
2642
#endif
2643
            {
2644
               png_bytep sp = row;
2645
               png_bytep dp = row;
2646
               for (i = 0; i < row_width; i++)
2647
               {
2648
                  png_uint_16 red, green, blue, gray16;
2649
                  red   = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2650
                  green = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2651
                  blue  = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2652
2653
 
2654
                     rgb_error |= 1;
2655
2656
 
2657
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
2658
                  *(dp++) = (png_byte)(gray16 & 0xff);
2659
                  *(dp++) = *(sp++);  /* alpha */
2660
                  *(dp++) = *(sp++);
2661
               }
2662
            }
2663
         }
2664
      }
2665
      row_info->channels -= 2;
2666
      row_info->color_type = (png_byte)(row_info->color_type &
2667
          ~PNG_COLOR_MASK_COLOR);
2668
      row_info->pixel_depth = (png_byte)(row_info->channels *
2669
          row_info->bit_depth);
2670
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2671
   }
2672
   return rgb_error;
2673
}
2674
#endif
2675
2676
 
2677
 * large of png_color.  This lets grayscale images be treated as
2678
 * paletted.  Most useful for gamma correction and simplification
2679
 * of code.
2680
 */
2681
void PNGAPI
2682
png_build_grayscale_palette(int bit_depth, png_colorp palette)
2683
{
2684
   int num_palette;
2685
   int color_inc;
2686
   int i;
2687
   int v;
2688
2689
 
2690
2691
 
2692
      return;
2693
2694
 
2695
   {
2696
      case 1:
2697
         num_palette = 2;
2698
         color_inc = 0xff;
2699
         break;
2700
2701
 
2702
         num_palette = 4;
2703
         color_inc = 0x55;
2704
         break;
2705
2706
 
2707
         num_palette = 16;
2708
         color_inc = 0x11;
2709
         break;
2710
2711
 
2712
         num_palette = 256;
2713
         color_inc = 1;
2714
         break;
2715
2716
 
2717
         num_palette = 0;
2718
         color_inc = 0;
2719
         break;
2720
   }
2721
2722
 
2723
   {
2724
      palette[i].red = (png_byte)v;
2725
      palette[i].green = (png_byte)v;
2726
      palette[i].blue = (png_byte)v;
2727
   }
2728
}
2729
2730
 
2731
 
2732
/* Replace any alpha or transparency with the supplied background color.
2733
 * "background" is already in the screen gamma, while "background_1" is
2734
 * at a gamma of 1.0.  Paletted files have already been taken care of.
2735
 */
2736
void /* PRIVATE */
2737
png_do_background(png_row_infop row_info, png_bytep row,
2738
    png_const_color_16p trans_color, png_const_color_16p background
2739
#ifdef PNG_READ_GAMMA_SUPPORTED
2740
    , png_const_color_16p background_1, png_const_bytep gamma_table,
2741
    png_const_bytep gamma_from_1, png_const_bytep gamma_to_1,
2742
    png_const_uint_16pp gamma_16, png_const_uint_16pp gamma_16_from_1,
2743
    png_const_uint_16pp gamma_16_to_1, int gamma_shift
2744
#endif
2745
    )
2746
{
2747
   png_bytep sp, dp;
2748
   png_uint_32 i;
2749
   png_uint_32 row_width = row_info->width;
2750
   int shift;
2751
2752
 
2753
2754
 
2755
      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
2756
      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_color)))
2757
   {
2758
      switch (row_info->color_type)
2759
      {
2760
         case PNG_COLOR_TYPE_GRAY:
2761
         {
2762
            switch (row_info->bit_depth)
2763
            {
2764
               case 1:
2765
               {
2766
                  sp = row;
2767
                  shift = 7;
2768
                  for (i = 0; i < row_width; i++)
2769
                  {
2770
                     if ((png_uint_16)((*sp >> shift) & 0x01)
2771
                        == trans_color->gray)
2772
                     {
2773
                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2774
                        *sp |= (png_byte)(background->gray << shift);
2775
                     }
2776
2777
 
2778
                     {
2779
                        shift = 7;
2780
                        sp++;
2781
                     }
2782
2783
 
2784
                        shift--;
2785
                  }
2786
                  break;
2787
               }
2788
2789
 
2790
               {
2791
#ifdef PNG_READ_GAMMA_SUPPORTED
2792
                  if (gamma_table != NULL)
2793
                  {
2794
                     sp = row;
2795
                     shift = 6;
2796
                     for (i = 0; i < row_width; i++)
2797
                     {
2798
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2799
                            == trans_color->gray)
2800
                        {
2801
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2802
                           *sp |= (png_byte)(background->gray << shift);
2803
                        }
2804
2805
 
2806
                        {
2807
                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
2808
                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2809
                               (p << 4) | (p << 6)] >> 6) & 0x03);
2810
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2811
                           *sp |= (png_byte)(g << shift);
2812
                        }
2813
2814
 
2815
                        {
2816
                           shift = 6;
2817
                           sp++;
2818
                        }
2819
2820
 
2821
                           shift -= 2;
2822
                     }
2823
                  }
2824
2825
 
2826
#endif
2827
                  {
2828
                     sp = row;
2829
                     shift = 6;
2830
                     for (i = 0; i < row_width; i++)
2831
                     {
2832
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2833
                            == trans_color->gray)
2834
                        {
2835
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2836
                           *sp |= (png_byte)(background->gray << shift);
2837
                        }
2838
2839
 
2840
                        {
2841
                           shift = 6;
2842
                           sp++;
2843
                        }
2844
2845
 
2846
                           shift -= 2;
2847
                     }
2848
                  }
2849
                  break;
2850
               }
2851
2852
 
2853
               {
2854
#ifdef PNG_READ_GAMMA_SUPPORTED
2855
                  if (gamma_table != NULL)
2856
                  {
2857
                     sp = row;
2858
                     shift = 4;
2859
                     for (i = 0; i < row_width; i++)
2860
                     {
2861
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2862
                            == trans_color->gray)
2863
                        {
2864
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2865
                           *sp |= (png_byte)(background->gray << shift);
2866
                        }
2867
2868
 
2869
                        {
2870
                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2871
                           png_byte g = (png_byte)((gamma_table[p |
2872
                               (p << 4)] >> 4) & 0x0f);
2873
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2874
                           *sp |= (png_byte)(g << shift);
2875
                        }
2876
2877
 
2878
                        {
2879
                           shift = 4;
2880
                           sp++;
2881
                        }
2882
2883
 
2884
                           shift -= 4;
2885
                     }
2886
                  }
2887
2888
 
2889
#endif
2890
                  {
2891
                     sp = row;
2892
                     shift = 4;
2893
                     for (i = 0; i < row_width; i++)
2894
                     {
2895
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2896
                            == trans_color->gray)
2897
                        {
2898
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2899
                           *sp |= (png_byte)(background->gray << shift);
2900
                        }
2901
2902
 
2903
                        {
2904
                           shift = 4;
2905
                           sp++;
2906
                        }
2907
2908
 
2909
                           shift -= 4;
2910
                     }
2911
                  }
2912
                  break;
2913
               }
2914
2915
 
2916
               {
2917
#ifdef PNG_READ_GAMMA_SUPPORTED
2918
                  if (gamma_table != NULL)
2919
                  {
2920
                     sp = row;
2921
                     for (i = 0; i < row_width; i++, sp++)
2922
                     {
2923
                        if (*sp == trans_color->gray)
2924
                           *sp = (png_byte)background->gray;
2925
2926
 
2927
                           *sp = gamma_table[*sp];
2928
                     }
2929
                  }
2930
                  else
2931
#endif
2932
                  {
2933
                     sp = row;
2934
                     for (i = 0; i < row_width; i++, sp++)
2935
                     {
2936
                        if (*sp == trans_color->gray)
2937
                           *sp = (png_byte)background->gray;
2938
                     }
2939
                  }
2940
                  break;
2941
               }
2942
2943
 
2944
               {
2945
#ifdef PNG_READ_GAMMA_SUPPORTED
2946
                  if (gamma_16 != NULL)
2947
                  {
2948
                     sp = row;
2949
                     for (i = 0; i < row_width; i++, sp += 2)
2950
                     {
2951
                        png_uint_16 v;
2952
2953
 
2954
2955
 
2956
                        {
2957
                           /* Background is already in screen gamma */
2958
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
2959
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
2960
                        }
2961
2962
 
2963
                        {
2964
                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
2965
                           *sp = (png_byte)((v >> 8) & 0xff);
2966
                           *(sp + 1) = (png_byte)(v & 0xff);
2967
                        }
2968
                     }
2969
                  }
2970
                  else
2971
#endif
2972
                  {
2973
                     sp = row;
2974
                     for (i = 0; i < row_width; i++, sp += 2)
2975
                     {
2976
                        png_uint_16 v;
2977
2978
 
2979
2980
 
2981
                        {
2982
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
2983
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
2984
                        }
2985
                     }
2986
                  }
2987
                  break;
2988
               }
2989
2990
 
2991
                  break;
2992
            }
2993
            break;
2994
         }
2995
2996
 
2997
         {
2998
            if (row_info->bit_depth == 8)
2999
            {
3000
#ifdef PNG_READ_GAMMA_SUPPORTED
3001
               if (gamma_table != NULL)
3002
               {
3003
                  sp = row;
3004
                  for (i = 0; i < row_width; i++, sp += 3)
3005
                  {
3006
                     if (*sp == trans_color->red &&
3007
                         *(sp + 1) == trans_color->green &&
3008
                         *(sp + 2) == trans_color->blue)
3009
                     {
3010
                        *sp = (png_byte)background->red;
3011
                        *(sp + 1) = (png_byte)background->green;
3012
                        *(sp + 2) = (png_byte)background->blue;
3013
                     }
3014
3015
 
3016
                     {
3017
                        *sp = gamma_table[*sp];
3018
                        *(sp + 1) = gamma_table[*(sp + 1)];
3019
                        *(sp + 2) = gamma_table[*(sp + 2)];
3020
                     }
3021
                  }
3022
               }
3023
               else
3024
#endif
3025
               {
3026
                  sp = row;
3027
                  for (i = 0; i < row_width; i++, sp += 3)
3028
                  {
3029
                     if (*sp == trans_color->red &&
3030
                         *(sp + 1) == trans_color->green &&
3031
                         *(sp + 2) == trans_color->blue)
3032
                     {
3033
                        *sp = (png_byte)background->red;
3034
                        *(sp + 1) = (png_byte)background->green;
3035
                        *(sp + 2) = (png_byte)background->blue;
3036
                     }
3037
                  }
3038
               }
3039
            }
3040
            else /* if (row_info->bit_depth == 16) */
3041
            {
3042
#ifdef PNG_READ_GAMMA_SUPPORTED
3043
               if (gamma_16 != NULL)
3044
               {
3045
                  sp = row;
3046
                  for (i = 0; i < row_width; i++, sp += 6)
3047
                  {
3048
                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3049
3050
 
3051
                         + *(sp + 3));
3052
3053
 
3054
                         + *(sp + 5));
3055
3056
 
3057
                         b == trans_color->blue)
3058
                     {
3059
                        /* Background is already in screen gamma */
3060
                        *sp = (png_byte)((background->red >> 8) & 0xff);
3061
                        *(sp + 1) = (png_byte)(background->red & 0xff);
3062
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3063
                        *(sp + 3) = (png_byte)(background->green & 0xff);
3064
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3065
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
3066
                     }
3067
3068
 
3069
                     {
3070
                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3071
                        *sp = (png_byte)((v >> 8) & 0xff);
3072
                        *(sp + 1) = (png_byte)(v & 0xff);
3073
3074
 
3075
                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3076
                        *(sp + 3) = (png_byte)(v & 0xff);
3077
3078
 
3079
                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3080
                        *(sp + 5) = (png_byte)(v & 0xff);
3081
                     }
3082
                  }
3083
               }
3084
3085
 
3086
#endif
3087
               {
3088
                  sp = row;
3089
                  for (i = 0; i < row_width; i++, sp += 6)
3090
                  {
3091
                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3092
3093
 
3094
                         + *(sp + 3));
3095
3096
 
3097
                         + *(sp + 5));
3098
3099
 
3100
                         b == trans_color->blue)
3101
                     {
3102
                        *sp = (png_byte)((background->red >> 8) & 0xff);
3103
                        *(sp + 1) = (png_byte)(background->red & 0xff);
3104
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3105
                        *(sp + 3) = (png_byte)(background->green & 0xff);
3106
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3107
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
3108
                     }
3109
                  }
3110
               }
3111
            }
3112
            break;
3113
         }
3114
3115
 
3116
         {
3117
            if (row_info->bit_depth == 8)
3118
            {
3119
#ifdef PNG_READ_GAMMA_SUPPORTED
3120
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3121
                   gamma_table != NULL)
3122
               {
3123
                  sp = row;
3124
                  dp = row;
3125
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
3126
                  {
3127
                     png_uint_16 a = *(sp + 1);
3128
3129
 
3130
                        *dp = gamma_table[*sp];
3131
3132
 
3133
                     {
3134
                        /* Background is already in screen gamma */
3135
                        *dp = (png_byte)background->gray;
3136
                     }
3137
3138
 
3139
                     {
3140
                        png_byte v, w;
3141
3142
 
3143
                        png_composite(w, v, a, background_1->gray);
3144
                        *dp = gamma_from_1[w];
3145
                     }
3146
                  }
3147
               }
3148
               else
3149
#endif
3150
               {
3151
                  sp = row;
3152
                  dp = row;
3153
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
3154
                  {
3155
                     png_byte a = *(sp + 1);
3156
3157
 
3158
                        *dp = *sp;
3159
3160
 
3161
                     else if (a == 0)
3162
                        *dp = (png_byte)background->gray;
3163
3164
 
3165
                        png_composite(*dp, *sp, a, background_1->gray);
3166
3167
 
3168
                     *dp = (png_byte)background->gray;
3169
#endif
3170
                  }
3171
               }
3172
            }
3173
            else /* if (png_ptr->bit_depth == 16) */
3174
            {
3175
#ifdef PNG_READ_GAMMA_SUPPORTED
3176
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3177
                   gamma_16_to_1 != NULL)
3178
               {
3179
                  sp = row;
3180
                  dp = row;
3181
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3182
                  {
3183
                     png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3184
                         + *(sp + 3));
3185
3186
 
3187
                     {
3188
                        png_uint_16 v;
3189
3190
 
3191
                        *dp = (png_byte)((v >> 8) & 0xff);
3192
                        *(dp + 1) = (png_byte)(v & 0xff);
3193
                     }
3194
3195
 
3196
                     else if (a == 0)
3197
#else
3198
                     else
3199
#endif
3200
                     {
3201
                        /* Background is already in screen gamma */
3202
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
3203
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
3204
                     }
3205
3206
 
3207
                     else
3208
                     {
3209
                        png_uint_16 g, v, w;
3210
3211
 
3212
                        png_composite_16(v, g, a, background_1->gray);
3213
                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3214
                        *dp = (png_byte)((w >> 8) & 0xff);
3215
                        *(dp + 1) = (png_byte)(w & 0xff);
3216
                     }
3217
#endif
3218
                  }
3219
               }
3220
               else
3221
#endif
3222
               {
3223
                  sp = row;
3224
                  dp = row;
3225
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3226
                  {
3227
                     png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3228
                         + *(sp + 3));
3229
3230
 
3231
                        png_memcpy(dp, sp, 2);
3232
3233
 
3234
                     else if (a == 0)
3235
#else
3236
                     else
3237
#endif
3238
                     {
3239
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
3240
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
3241
                     }
3242
3243
 
3244
                     else
3245
                     {
3246
                        png_uint_16 g, v;
3247
3248
 
3249
                        png_composite_16(v, g, a, background_1->gray);
3250
                        *dp = (png_byte)((v >> 8) & 0xff);
3251
                        *(dp + 1) = (png_byte)(v & 0xff);
3252
                     }
3253
#endif
3254
                  }
3255
               }
3256
            }
3257
            break;
3258
         }
3259
3260
 
3261
         {
3262
            if (row_info->bit_depth == 8)
3263
            {
3264
#ifdef PNG_READ_GAMMA_SUPPORTED
3265
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3266
                   gamma_table != NULL)
3267
               {
3268
                  sp = row;
3269
                  dp = row;
3270
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3271
                  {
3272
                     png_byte a = *(sp + 3);
3273
3274
 
3275
                     {
3276
                        *dp = gamma_table[*sp];
3277
                        *(dp + 1) = gamma_table[*(sp + 1)];
3278
                        *(dp + 2) = gamma_table[*(sp + 2)];
3279
                     }
3280
3281
 
3282
                     {
3283
                        /* Background is already in screen gamma */
3284
                        *dp = (png_byte)background->red;
3285
                        *(dp + 1) = (png_byte)background->green;
3286
                        *(dp + 2) = (png_byte)background->blue;
3287
                     }
3288
3289
 
3290
                     {
3291
                        png_byte v, w;
3292
3293
 
3294
                        png_composite(w, v, a, background_1->red);
3295
                        *dp = gamma_from_1[w];
3296
3297
 
3298
                        png_composite(w, v, a, background_1->green);
3299
                        *(dp + 1) = gamma_from_1[w];
3300
3301
 
3302
                        png_composite(w, v, a, background_1->blue);
3303
                        *(dp + 2) = gamma_from_1[w];
3304
                     }
3305
                  }
3306
               }
3307
               else
3308
#endif
3309
               {
3310
                  sp = row;
3311
                  dp = row;
3312
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3313
                  {
3314
                     png_byte a = *(sp + 3);
3315
3316
 
3317
                     {
3318
                        *dp = *sp;
3319
                        *(dp + 1) = *(sp + 1);
3320
                        *(dp + 2) = *(sp + 2);
3321
                     }
3322
3323
 
3324
                     {
3325
                        *dp = (png_byte)background->red;
3326
                        *(dp + 1) = (png_byte)background->green;
3327
                        *(dp + 2) = (png_byte)background->blue;
3328
                     }
3329
3330
 
3331
                     {
3332
                        png_composite(*dp, *sp, a, background->red);
3333
3334
 
3335
                            background->green);
3336
3337
 
3338
                            background->blue);
3339
                     }
3340
                  }
3341
               }
3342
            }
3343
            else /* if (row_info->bit_depth == 16) */
3344
            {
3345
#ifdef PNG_READ_GAMMA_SUPPORTED
3346
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3347
                   gamma_16_to_1 != NULL)
3348
               {
3349
                  sp = row;
3350
                  dp = row;
3351
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3352
                  {
3353
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3354
                         << 8) + (png_uint_16)(*(sp + 7)));
3355
3356
 
3357
                     {
3358
                        png_uint_16 v;
3359
3360
 
3361
                        *dp = (png_byte)((v >> 8) & 0xff);
3362
                        *(dp + 1) = (png_byte)(v & 0xff);
3363
3364
 
3365
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3366
                        *(dp + 3) = (png_byte)(v & 0xff);
3367
3368
 
3369
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3370
                        *(dp + 5) = (png_byte)(v & 0xff);
3371
                     }
3372
3373
 
3374
                     {
3375
                        /* Background is already in screen gamma */
3376
                        *dp = (png_byte)((background->red >> 8) & 0xff);
3377
                        *(dp + 1) = (png_byte)(background->red & 0xff);
3378
                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3379
                        *(dp + 3) = (png_byte)(background->green & 0xff);
3380
                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3381
                        *(dp + 5) = (png_byte)(background->blue & 0xff);
3382
                     }
3383
3384
 
3385
                     {
3386
                        png_uint_16 v, w, x;
3387
3388
 
3389
                        png_composite_16(w, v, a, background_1->red);
3390
3391
 
3392
                        *dp = (png_byte)((x >> 8) & 0xff);
3393
                        *(dp + 1) = (png_byte)(x & 0xff);
3394
3395
 
3396
                        png_composite_16(w, v, a, background_1->green);
3397
3398
 
3399
                        *(dp + 2) = (png_byte)((x >> 8) & 0xff);
3400
                        *(dp + 3) = (png_byte)(x & 0xff);
3401
3402
 
3403
                        png_composite_16(w, v, a, background_1->blue);
3404
3405
 
3406
                        *(dp + 4) = (png_byte)((x >> 8) & 0xff);
3407
                        *(dp + 5) = (png_byte)(x & 0xff);
3408
                     }
3409
                  }
3410
               }
3411
3412
 
3413
#endif
3414
               {
3415
                  sp = row;
3416
                  dp = row;
3417
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3418
                  {
3419
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3420
                         << 8) + (png_uint_16)(*(sp + 7)));
3421
3422
 
3423
                     {
3424
                        png_memcpy(dp, sp, 6);
3425
                     }
3426
3427
 
3428
                     {
3429
                        *dp = (png_byte)((background->red >> 8) & 0xff);
3430
                        *(dp + 1) = (png_byte)(background->red & 0xff);
3431
                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3432
                        *(dp + 3) = (png_byte)(background->green & 0xff);
3433
                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3434
                        *(dp + 5) = (png_byte)(background->blue & 0xff);
3435
                     }
3436
3437
 
3438
                     {
3439
                        png_uint_16 v;
3440
3441
 
3442
                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3443
                            + *(sp + 3));
3444
                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3445
                            + *(sp + 5));
3446
3447
 
3448
                        *dp = (png_byte)((v >> 8) & 0xff);
3449
                        *(dp + 1) = (png_byte)(v & 0xff);
3450
3451
 
3452
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3453
                        *(dp + 3) = (png_byte)(v & 0xff);
3454
3455
 
3456
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3457
                        *(dp + 5) = (png_byte)(v & 0xff);
3458
                     }
3459
                  }
3460
               }
3461
            }
3462
            break;
3463
         }
3464
3465
 
3466
            break;
3467
      }
3468
3469
 
3470
      {
3471
         row_info->color_type = (png_byte)(row_info->color_type &
3472
             ~PNG_COLOR_MASK_ALPHA);
3473
         row_info->channels--;
3474
         row_info->pixel_depth = (png_byte)(row_info->channels *
3475
             row_info->bit_depth);
3476
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3477
      }
3478
   }
3479
}
3480
#endif
3481
3482
 
3483
/* Gamma correct the image, avoiding the alpha channel.  Make sure
3484
 * you do this after you deal with the transparency issue on grayscale
3485
 * or RGB images. If your bit depth is 8, use gamma_table, if it
3486
 * is 16, use gamma_16_table and gamma_shift.  Build these with
3487
 * build_gamma_table().
3488
 */
3489
void /* PRIVATE */
3490
png_do_gamma(png_row_infop row_info, png_bytep row,
3491
    png_const_bytep gamma_table, png_const_uint_16pp gamma_16_table,
3492
    int gamma_shift)
3493
{
3494
   png_bytep sp;
3495
   png_uint_32 i;
3496
   png_uint_32 row_width=row_info->width;
3497
3498
 
3499
3500
 
3501
       (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3502
   {
3503
      switch (row_info->color_type)
3504
      {
3505
         case PNG_COLOR_TYPE_RGB:
3506
         {
3507
            if (row_info->bit_depth == 8)
3508
            {
3509
               sp = row;
3510
               for (i = 0; i < row_width; i++)
3511
               {
3512
                  *sp = gamma_table[*sp];
3513
                  sp++;
3514
                  *sp = gamma_table[*sp];
3515
                  sp++;
3516
                  *sp = gamma_table[*sp];
3517
                  sp++;
3518
               }
3519
            }
3520
3521
 
3522
            {
3523
               sp = row;
3524
               for (i = 0; i < row_width; i++)
3525
               {
3526
                  png_uint_16 v;
3527
3528
 
3529
                  *sp = (png_byte)((v >> 8) & 0xff);
3530
                  *(sp + 1) = (png_byte)(v & 0xff);
3531
                  sp += 2;
3532
3533
 
3534
                  *sp = (png_byte)((v >> 8) & 0xff);
3535
                  *(sp + 1) = (png_byte)(v & 0xff);
3536
                  sp += 2;
3537
3538
 
3539
                  *sp = (png_byte)((v >> 8) & 0xff);
3540
                  *(sp + 1) = (png_byte)(v & 0xff);
3541
                  sp += 2;
3542
               }
3543
            }
3544
            break;
3545
         }
3546
3547
 
3548
         {
3549
            if (row_info->bit_depth == 8)
3550
            {
3551
               sp = row;
3552
               for (i = 0; i < row_width; i++)
3553
               {
3554
                  *sp = gamma_table[*sp];
3555
                  sp++;
3556
3557
 
3558
                  sp++;
3559
3560
 
3561
                  sp++;
3562
3563
 
3564
               }
3565
            }
3566
3567
 
3568
            {
3569
               sp = row;
3570
               for (i = 0; i < row_width; i++)
3571
               {
3572
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3573
                  *sp = (png_byte)((v >> 8) & 0xff);
3574
                  *(sp + 1) = (png_byte)(v & 0xff);
3575
                  sp += 2;
3576
3577
 
3578
                  *sp = (png_byte)((v >> 8) & 0xff);
3579
                  *(sp + 1) = (png_byte)(v & 0xff);
3580
                  sp += 2;
3581
3582
 
3583
                  *sp = (png_byte)((v >> 8) & 0xff);
3584
                  *(sp + 1) = (png_byte)(v & 0xff);
3585
                  sp += 4;
3586
               }
3587
            }
3588
            break;
3589
         }
3590
3591
 
3592
         {
3593
            if (row_info->bit_depth == 8)
3594
            {
3595
               sp = row;
3596
               for (i = 0; i < row_width; i++)
3597
               {
3598
                  *sp = gamma_table[*sp];
3599
                  sp += 2;
3600
               }
3601
            }
3602
3603
 
3604
            {
3605
               sp = row;
3606
               for (i = 0; i < row_width; i++)
3607
               {
3608
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3609
                  *sp = (png_byte)((v >> 8) & 0xff);
3610
                  *(sp + 1) = (png_byte)(v & 0xff);
3611
                  sp += 4;
3612
               }
3613
            }
3614
            break;
3615
         }
3616
3617
 
3618
         {
3619
            if (row_info->bit_depth == 2)
3620
            {
3621
               sp = row;
3622
               for (i = 0; i < row_width; i += 4)
3623
               {
3624
                  int a = *sp & 0xc0;
3625
                  int b = *sp & 0x30;
3626
                  int c = *sp & 0x0c;
3627
                  int d = *sp & 0x03;
3628
3629
 
3630
                      ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
3631
                      ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
3632
                      ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3633
                      ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3634
                  sp++;
3635
               }
3636
            }
3637
3638
 
3639
            {
3640
               sp = row;
3641
               for (i = 0; i < row_width; i += 2)
3642
               {
3643
                  int msb = *sp & 0xf0;
3644
                  int lsb = *sp & 0x0f;
3645
3646
 
3647
                      | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
3648
                  sp++;
3649
               }
3650
            }
3651
3652
 
3653
            {
3654
               sp = row;
3655
               for (i = 0; i < row_width; i++)
3656
               {
3657
                  *sp = gamma_table[*sp];
3658
                  sp++;
3659
               }
3660
            }
3661
3662
 
3663
            {
3664
               sp = row;
3665
               for (i = 0; i < row_width; i++)
3666
               {
3667
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3668
                  *sp = (png_byte)((v >> 8) & 0xff);
3669
                  *(sp + 1) = (png_byte)(v & 0xff);
3670
                  sp += 2;
3671
               }
3672
            }
3673
            break;
3674
         }
3675
3676
 
3677
            break;
3678
      }
3679
   }
3680
}
3681
#endif
3682
3683
 
3684
/* Expands a palette row to an RGB or RGBA row depending
3685
 * upon whether you supply trans and num_trans.
3686
 */
3687
void /* PRIVATE */
3688
png_do_expand_palette(png_row_infop row_info, png_bytep row,
3689
   png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
3690
{
3691
   int shift, value;
3692
   png_bytep sp, dp;
3693
   png_uint_32 i;
3694
   png_uint_32 row_width=row_info->width;
3695
3696
 
3697
3698
 
3699
   {
3700
      if (row_info->bit_depth < 8)
3701
      {
3702
         switch (row_info->bit_depth)
3703
         {
3704
            case 1:
3705
            {
3706
               sp = row + (png_size_t)((row_width - 1) >> 3);
3707
               dp = row + (png_size_t)row_width - 1;
3708
               shift = 7 - (int)((row_width + 7) & 0x07);
3709
               for (i = 0; i < row_width; i++)
3710
               {
3711
                  if ((*sp >> shift) & 0x01)
3712
                     *dp = 1;
3713
3714
 
3715
                     *dp = 0;
3716
3717
 
3718
                  {
3719
                     shift = 0;
3720
                     sp--;
3721
                  }
3722
3723
 
3724
                     shift++;
3725
3726
 
3727
               }
3728
               break;
3729
            }
3730
3731
 
3732
            {
3733
               sp = row + (png_size_t)((row_width - 1) >> 2);
3734
               dp = row + (png_size_t)row_width - 1;
3735
               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3736
               for (i = 0; i < row_width; i++)
3737
               {
3738
                  value = (*sp >> shift) & 0x03;
3739
                  *dp = (png_byte)value;
3740
                  if (shift == 6)
3741
                  {
3742
                     shift = 0;
3743
                     sp--;
3744
                  }
3745
3746
 
3747
                     shift += 2;
3748
3749
 
3750
               }
3751
               break;
3752
            }
3753
3754
 
3755
            {
3756
               sp = row + (png_size_t)((row_width - 1) >> 1);
3757
               dp = row + (png_size_t)row_width - 1;
3758
               shift = (int)((row_width & 0x01) << 2);
3759
               for (i = 0; i < row_width; i++)
3760
               {
3761
                  value = (*sp >> shift) & 0x0f;
3762
                  *dp = (png_byte)value;
3763
                  if (shift == 4)
3764
                  {
3765
                     shift = 0;
3766
                     sp--;
3767
                  }
3768
3769
 
3770
                     shift += 4;
3771
3772
 
3773
               }
3774
               break;
3775
            }
3776
3777
 
3778
               break;
3779
         }
3780
         row_info->bit_depth = 8;
3781
         row_info->pixel_depth = 8;
3782
         row_info->rowbytes = row_width;
3783
      }
3784
3785
 
3786
      {
3787
         {
3788
            if (trans_alpha != NULL)
3789
            {
3790
               sp = row + (png_size_t)row_width - 1;
3791
               dp = row + (png_size_t)(row_width << 2) - 1;
3792
3793
 
3794
               {
3795
                  if ((int)(*sp) >= num_trans)
3796
                     *dp-- = 0xff;
3797
3798
 
3799
                     *dp-- = trans_alpha[*sp];
3800
3801
 
3802
                  *dp-- = palette[*sp].green;
3803
                  *dp-- = palette[*sp].red;
3804
                  sp--;
3805
               }
3806
               row_info->bit_depth = 8;
3807
               row_info->pixel_depth = 32;
3808
               row_info->rowbytes = row_width * 4;
3809
               row_info->color_type = 6;
3810
               row_info->channels = 4;
3811
            }
3812
3813
 
3814
            {
3815
               sp = row + (png_size_t)row_width - 1;
3816
               dp = row + (png_size_t)(row_width * 3) - 1;
3817
3818
 
3819
               {
3820
                  *dp-- = palette[*sp].blue;
3821
                  *dp-- = palette[*sp].green;
3822
                  *dp-- = palette[*sp].red;
3823
                  sp--;
3824
               }
3825
3826
 
3827
               row_info->pixel_depth = 24;
3828
               row_info->rowbytes = row_width * 3;
3829
               row_info->color_type = 2;
3830
               row_info->channels = 3;
3831
            }
3832
         }
3833
      }
3834
   }
3835
}
3836
3837
 
3838
 * expanded transparency value is supplied, an alpha channel is built.
3839
 */
3840
void /* PRIVATE */
3841
png_do_expand(png_row_infop row_info, png_bytep row,
3842
    png_const_color_16p trans_value)
3843
{
3844
   int shift, value;
3845
   png_bytep sp, dp;
3846
   png_uint_32 i;
3847
   png_uint_32 row_width=row_info->width;
3848
3849
 
3850
3851
 
3852
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3853
      {
3854
         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
3855
3856
 
3857
         {
3858
            switch (row_info->bit_depth)
3859
            {
3860
               case 1:
3861
               {
3862
                  gray = (png_uint_16)((gray & 0x01) * 0xff);
3863
                  sp = row + (png_size_t)((row_width - 1) >> 3);
3864
                  dp = row + (png_size_t)row_width - 1;
3865
                  shift = 7 - (int)((row_width + 7) & 0x07);
3866
                  for (i = 0; i < row_width; i++)
3867
                  {
3868
                     if ((*sp >> shift) & 0x01)
3869
                        *dp = 0xff;
3870
3871
 
3872
                        *dp = 0;
3873
3874
 
3875
                     {
3876
                        shift = 0;
3877
                        sp--;
3878
                     }
3879
3880
 
3881
                        shift++;
3882
3883
 
3884
                  }
3885
                  break;
3886
               }
3887
3888
 
3889
               {
3890
                  gray = (png_uint_16)((gray & 0x03) * 0x55);
3891
                  sp = row + (png_size_t)((row_width - 1) >> 2);
3892
                  dp = row + (png_size_t)row_width - 1;
3893
                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3894
                  for (i = 0; i < row_width; i++)
3895
                  {
3896
                     value = (*sp >> shift) & 0x03;
3897
                     *dp = (png_byte)(value | (value << 2) | (value << 4) |
3898
                        (value << 6));
3899
                     if (shift == 6)
3900
                     {
3901
                        shift = 0;
3902
                        sp--;
3903
                     }
3904
3905
 
3906
                        shift += 2;
3907
3908
 
3909
                  }
3910
                  break;
3911
               }
3912
3913
 
3914
               {
3915
                  gray = (png_uint_16)((gray & 0x0f) * 0x11);
3916
                  sp = row + (png_size_t)((row_width - 1) >> 1);
3917
                  dp = row + (png_size_t)row_width - 1;
3918
                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3919
                  for (i = 0; i < row_width; i++)
3920
                  {
3921
                     value = (*sp >> shift) & 0x0f;
3922
                     *dp = (png_byte)(value | (value << 4));
3923
                     if (shift == 4)
3924
                     {
3925
                        shift = 0;
3926
                        sp--;
3927
                     }
3928
3929
 
3930
                        shift = 4;
3931
3932
 
3933
                  }
3934
                  break;
3935
               }
3936
3937
 
3938
                  break;
3939
            }
3940
3941
 
3942
            row_info->pixel_depth = 8;
3943
            row_info->rowbytes = row_width;
3944
         }
3945
3946
 
3947
         {
3948
            if (row_info->bit_depth == 8)
3949
            {
3950
               gray = gray & 0xff;
3951
               sp = row + (png_size_t)row_width - 1;
3952
               dp = row + (png_size_t)(row_width << 1) - 1;
3953
3954
 
3955
               {
3956
                  if (*sp == gray)
3957
                     *dp-- = 0;
3958
3959
 
3960
                     *dp-- = 0xff;
3961
3962
 
3963
               }
3964
            }
3965
3966
 
3967
            {
3968
               png_byte gray_high = (png_byte)((gray >> 8) & 0xff);
3969
               png_byte gray_low = (png_byte)(gray & 0xff);
3970
               sp = row + row_info->rowbytes - 1;
3971
               dp = row + (row_info->rowbytes << 1) - 1;
3972
               for (i = 0; i < row_width; i++)
3973
               {
3974
                  if (*(sp - 1) == gray_high && *(sp) == gray_low)
3975
                  {
3976
                     *dp-- = 0;
3977
                     *dp-- = 0;
3978
                  }
3979
3980
 
3981
                  {
3982
                     *dp-- = 0xff;
3983
                     *dp-- = 0xff;
3984
                  }
3985
3986
 
3987
                  *dp-- = *sp--;
3988
               }
3989
            }
3990
3991
 
3992
            row_info->channels = 2;
3993
            row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
3994
            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
3995
               row_width);
3996
         }
3997
      }
3998
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
3999
      {
4000
         if (row_info->bit_depth == 8)
4001
         {
4002
            png_byte red = (png_byte)(trans_value->red & 0xff);
4003
            png_byte green = (png_byte)(trans_value->green & 0xff);
4004
            png_byte blue = (png_byte)(trans_value->blue & 0xff);
4005
            sp = row + (png_size_t)row_info->rowbytes - 1;
4006
            dp = row + (png_size_t)(row_width << 2) - 1;
4007
            for (i = 0; i < row_width; i++)
4008
            {
4009
               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4010
                  *dp-- = 0;
4011
4012
 
4013
                  *dp-- = 0xff;
4014
4015
 
4016
               *dp-- = *sp--;
4017
               *dp-- = *sp--;
4018
            }
4019
         }
4020
         else if (row_info->bit_depth == 16)
4021
         {
4022
            png_byte red_high = (png_byte)((trans_value->red >> 8) & 0xff);
4023
            png_byte green_high = (png_byte)((trans_value->green >> 8) & 0xff);
4024
            png_byte blue_high = (png_byte)((trans_value->blue >> 8) & 0xff);
4025
            png_byte red_low = (png_byte)(trans_value->red & 0xff);
4026
            png_byte green_low = (png_byte)(trans_value->green & 0xff);
4027
            png_byte blue_low = (png_byte)(trans_value->blue & 0xff);
4028
            sp = row + row_info->rowbytes - 1;
4029
            dp = row + (png_size_t)(row_width << 3) - 1;
4030
            for (i = 0; i < row_width; i++)
4031
            {
4032
               if (*(sp - 5) == red_high &&
4033
                   *(sp - 4) == red_low &&
4034
                   *(sp - 3) == green_high &&
4035
                   *(sp - 2) == green_low &&
4036
                   *(sp - 1) == blue_high &&
4037
                   *(sp    ) == blue_low)
4038
               {
4039
                  *dp-- = 0;
4040
                  *dp-- = 0;
4041
               }
4042
4043
 
4044
               {
4045
                  *dp-- = 0xff;
4046
                  *dp-- = 0xff;
4047
               }
4048
4049
 
4050
               *dp-- = *sp--;
4051
               *dp-- = *sp--;
4052
               *dp-- = *sp--;
4053
               *dp-- = *sp--;
4054
               *dp-- = *sp--;
4055
            }
4056
         }
4057
         row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4058
         row_info->channels = 4;
4059
         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4060
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4061
      }
4062
   }
4063
}
4064
#endif
4065
4066
 
4067
void /* PRIVATE */
4068
png_do_quantize(png_row_infop row_info, png_bytep row,
4069
    png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4070
{
4071
   png_bytep sp, dp;
4072
   png_uint_32 i;
4073
   png_uint_32 row_width=row_info->width;
4074
4075
 
4076
4077
 
4078
   {
4079
      if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
4080
      {
4081
         int r, g, b, p;
4082
         sp = row;
4083
         dp = row;
4084
         for (i = 0; i < row_width; i++)
4085
         {
4086
            r = *sp++;
4087
            g = *sp++;
4088
            b = *sp++;
4089
4090
 
4091
             * it down to a reasonable formula.  For example, with
4092
             * 5 bits per color, we get:
4093
             * p = (((r >> 3) & 0x1f) << 10) |
4094
             *    (((g >> 3) & 0x1f) << 5) |
4095
             *    ((b >> 3) & 0x1f);
4096
             */
4097
            p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4098
                ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4099
                (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4100
                (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4101
                ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4102
                (PNG_QUANTIZE_BLUE_BITS)) |
4103
                ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4104
                ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4105
4106
 
4107
         }
4108
4109
 
4110
         row_info->channels = 1;
4111
         row_info->pixel_depth = row_info->bit_depth;
4112
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4113
      }
4114
4115
 
4116
         palette_lookup != NULL)
4117
      {
4118
         int r, g, b, p;
4119
         sp = row;
4120
         dp = row;
4121
         for (i = 0; i < row_width; i++)
4122
         {
4123
            r = *sp++;
4124
            g = *sp++;
4125
            b = *sp++;
4126
            sp++;
4127
4128
 
4129
                ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4130
                (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4131
                (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4132
                ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4133
                (PNG_QUANTIZE_BLUE_BITS)) |
4134
                ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4135
                ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4136
4137
 
4138
         }
4139
4140
 
4141
         row_info->channels = 1;
4142
         row_info->pixel_depth = row_info->bit_depth;
4143
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4144
      }
4145
4146
 
4147
         quantize_lookup)
4148
      {
4149
         sp = row;
4150
4151
 
4152
         {
4153
            *sp = quantize_lookup[*sp];
4154
         }
4155
      }
4156
   }
4157
}
4158
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
4159
4160
 
4161
/* Undoes intrapixel differencing  */
4162
void /* PRIVATE */
4163
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4164
{
4165
   png_debug(1, "in png_do_read_intrapixel");
4166
4167
 
4168
       (row_info->color_type & PNG_COLOR_MASK_COLOR))
4169
   {
4170
      int bytes_per_pixel;
4171
      png_uint_32 row_width = row_info->width;
4172
4173
 
4174
      {
4175
         png_bytep rp;
4176
         png_uint_32 i;
4177
4178
 
4179
            bytes_per_pixel = 3;
4180
4181
 
4182
            bytes_per_pixel = 4;
4183
4184
 
4185
            return;
4186
4187
 
4188
         {
4189
            *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
4190
            *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
4191
         }
4192
      }
4193
      else if (row_info->bit_depth == 16)
4194
      {
4195
         png_bytep rp;
4196
         png_uint_32 i;
4197
4198
 
4199
            bytes_per_pixel = 6;
4200
4201
 
4202
            bytes_per_pixel = 8;
4203
4204
 
4205
            return;
4206
4207
 
4208
         {
4209
            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
4210
            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
4211
            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
4212
            png_uint_32 red  = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
4213
            png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4214
            *(rp    ) = (png_byte)((red >> 8) & 0xff);
4215
            *(rp + 1) = (png_byte)(red & 0xff);
4216
            *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
4217
            *(rp + 5) = (png_byte)(blue & 0xff);
4218
         }
4219
      }
4220
   }
4221
}
4222
#endif /* PNG_MNG_FEATURES_SUPPORTED */
4223
#endif /* PNG_READ_SUPPORTED */
4224
>