Debian Codegraph @

PNG/libpng/pngrtran.c

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