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