OLD | NEW |
1 | 1 |
2 /* pngwtran.c - transforms the data in a row for PNG writers | 2 /* pngwtran.c - transforms the data in a row for PNG writers |
3 * | 3 * |
4 * Last changed in libpng 1.2.43 [February 25, 2010] | 4 * Last changed in libpng 1.6.0 [February 14, 2013] |
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson | 5 * Copyright (c) 1998-2013 Glenn Randers-Pehrson |
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
8 * | 8 * |
9 * This code is released under the libpng license. | 9 * This code is released under the libpng license. |
10 * For conditions of distribution and use, see the disclaimer | 10 * For conditions of distribution and use, see the disclaimer |
11 * and license in png.h | 11 * and license in png.h |
12 */ | 12 */ |
13 | 13 |
14 #define PNG_INTERNAL | 14 #include "pngpriv.h" |
15 #define PNG_NO_PEDANTIC_WARNINGS | 15 |
16 #include "png.h" | |
17 #ifdef PNG_WRITE_SUPPORTED | 16 #ifdef PNG_WRITE_SUPPORTED |
18 | 17 |
| 18 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED |
19 /* Transform the data according to the user's wishes. The order of | 19 /* Transform the data according to the user's wishes. The order of |
20 * transformations is significant. | 20 * transformations is significant. |
21 */ | 21 */ |
22 void /* PRIVATE */ | 22 void /* PRIVATE */ |
23 png_do_write_transformations(png_structp png_ptr) | 23 png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info) |
24 { | 24 { |
25 png_debug(1, "in png_do_write_transformations"); | 25 png_debug(1, "in png_do_write_transformations"); |
26 | 26 |
27 if (png_ptr == NULL) | 27 if (png_ptr == NULL) |
28 return; | 28 return; |
29 | 29 |
30 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED | 30 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
31 if (png_ptr->transformations & PNG_USER_TRANSFORM) | 31 if (png_ptr->transformations & PNG_USER_TRANSFORM) |
32 if (png_ptr->write_user_transform_fn != NULL) | 32 if (png_ptr->write_user_transform_fn != NULL) |
33 (*(png_ptr->write_user_transform_fn)) /* User write transform | 33 (*(png_ptr->write_user_transform_fn)) /* User write transform |
34 function */ | 34 function */ |
35 (png_ptr, /* png_ptr */ | 35 (png_ptr, /* png_ptr */ |
36 &(png_ptr->row_info), /* row_info: */ | 36 row_info, /* row_info: */ |
37 /* png_uint_32 width; width of row */ | 37 /* png_uint_32 width; width of row */ |
38 /* png_uint_32 rowbytes; number of bytes in row */ | 38 /* png_size_t rowbytes; number of bytes in row */ |
39 /* png_byte color_type; color type of pixels */ | 39 /* png_byte color_type; color type of pixels */ |
40 /* png_byte bit_depth; bit depth of samples */ | 40 /* png_byte bit_depth; bit depth of samples */ |
41 /* png_byte channels; number of channels (1-4) */ | 41 /* png_byte channels; number of channels (1-4) */ |
42 /* png_byte pixel_depth; bits per pixel (depth*channels) */ | 42 /* png_byte pixel_depth; bits per pixel (depth*channels) */ |
43 png_ptr->row_buf + 1); /* start of pixel data for row */ | 43 png_ptr->row_buf + 1); /* start of pixel data for row */ |
44 #endif | 44 #endif |
| 45 |
45 #ifdef PNG_WRITE_FILLER_SUPPORTED | 46 #ifdef PNG_WRITE_FILLER_SUPPORTED |
46 if (png_ptr->transformations & PNG_FILLER) | 47 if (png_ptr->transformations & PNG_FILLER) |
47 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, | 48 png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
48 png_ptr->flags); | 49 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER)); |
49 #endif | 50 #endif |
| 51 |
50 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED | 52 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED |
51 if (png_ptr->transformations & PNG_PACKSWAP) | 53 if (png_ptr->transformations & PNG_PACKSWAP) |
52 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); | 54 png_do_packswap(row_info, png_ptr->row_buf + 1); |
53 #endif | 55 #endif |
| 56 |
54 #ifdef PNG_WRITE_PACK_SUPPORTED | 57 #ifdef PNG_WRITE_PACK_SUPPORTED |
55 if (png_ptr->transformations & PNG_PACK) | 58 if (png_ptr->transformations & PNG_PACK) |
56 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1, | 59 png_do_pack(row_info, png_ptr->row_buf + 1, |
57 (png_uint_32)png_ptr->bit_depth); | 60 (png_uint_32)png_ptr->bit_depth); |
58 #endif | 61 #endif |
| 62 |
59 #ifdef PNG_WRITE_SWAP_SUPPORTED | 63 #ifdef PNG_WRITE_SWAP_SUPPORTED |
60 if (png_ptr->transformations & PNG_SWAP_BYTES) | 64 if (png_ptr->transformations & PNG_SWAP_BYTES) |
61 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); | 65 png_do_swap(row_info, png_ptr->row_buf + 1); |
62 #endif | 66 #endif |
| 67 |
63 #ifdef PNG_WRITE_SHIFT_SUPPORTED | 68 #ifdef PNG_WRITE_SHIFT_SUPPORTED |
64 if (png_ptr->transformations & PNG_SHIFT) | 69 if (png_ptr->transformations & PNG_SHIFT) |
65 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1, | 70 png_do_shift(row_info, png_ptr->row_buf + 1, |
66 &(png_ptr->shift)); | 71 &(png_ptr->shift)); |
67 #endif | 72 #endif |
| 73 |
68 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED | 74 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
69 if (png_ptr->transformations & PNG_SWAP_ALPHA) | 75 if (png_ptr->transformations & PNG_SWAP_ALPHA) |
70 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); | 76 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1); |
71 #endif | 77 #endif |
| 78 |
72 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED | 79 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
73 if (png_ptr->transformations & PNG_INVERT_ALPHA) | 80 if (png_ptr->transformations & PNG_INVERT_ALPHA) |
74 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); | 81 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1); |
75 #endif | 82 #endif |
| 83 |
76 #ifdef PNG_WRITE_BGR_SUPPORTED | 84 #ifdef PNG_WRITE_BGR_SUPPORTED |
77 if (png_ptr->transformations & PNG_BGR) | 85 if (png_ptr->transformations & PNG_BGR) |
78 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); | 86 png_do_bgr(row_info, png_ptr->row_buf + 1); |
79 #endif | 87 #endif |
| 88 |
80 #ifdef PNG_WRITE_INVERT_SUPPORTED | 89 #ifdef PNG_WRITE_INVERT_SUPPORTED |
81 if (png_ptr->transformations & PNG_INVERT_MONO) | 90 if (png_ptr->transformations & PNG_INVERT_MONO) |
82 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); | 91 png_do_invert(row_info, png_ptr->row_buf + 1); |
83 #endif | 92 #endif |
84 } | 93 } |
85 | 94 |
86 #ifdef PNG_WRITE_PACK_SUPPORTED | 95 #ifdef PNG_WRITE_PACK_SUPPORTED |
87 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The | 96 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The |
88 * row_info bit depth should be 8 (one pixel per byte). The channels | 97 * row_info bit depth should be 8 (one pixel per byte). The channels |
89 * should be 1 (this only happens on grayscale and paletted images). | 98 * should be 1 (this only happens on grayscale and paletted images). |
90 */ | 99 */ |
91 void /* PRIVATE */ | 100 void /* PRIVATE */ |
92 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) | 101 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) |
93 { | 102 { |
94 png_debug(1, "in png_do_pack"); | 103 png_debug(1, "in png_do_pack"); |
95 | 104 |
96 if (row_info->bit_depth == 8 && | 105 if (row_info->bit_depth == 8 && |
97 #ifdef PNG_USELESS_TESTS_SUPPORTED | |
98 row != NULL && row_info != NULL && | |
99 #endif | |
100 row_info->channels == 1) | 106 row_info->channels == 1) |
101 { | 107 { |
102 switch ((int)bit_depth) | 108 switch ((int)bit_depth) |
103 { | 109 { |
104 case 1: | 110 case 1: |
105 { | 111 { |
106 png_bytep sp, dp; | 112 png_bytep sp, dp; |
107 int mask, v; | 113 int mask, v; |
108 png_uint_32 i; | 114 png_uint_32 i; |
109 png_uint_32 row_width = row_info->width; | 115 png_uint_32 row_width = row_info->width; |
110 | 116 |
111 sp = row; | 117 sp = row; |
112 dp = row; | 118 dp = row; |
113 mask = 0x80; | 119 mask = 0x80; |
114 v = 0; | 120 v = 0; |
115 | 121 |
116 for (i = 0; i < row_width; i++) | 122 for (i = 0; i < row_width; i++) |
117 { | 123 { |
118 if (*sp != 0) | 124 if (*sp != 0) |
119 v |= mask; | 125 v |= mask; |
| 126 |
120 sp++; | 127 sp++; |
| 128 |
121 if (mask > 1) | 129 if (mask > 1) |
122 mask >>= 1; | 130 mask >>= 1; |
| 131 |
123 else | 132 else |
124 { | 133 { |
125 mask = 0x80; | 134 mask = 0x80; |
126 *dp = (png_byte)v; | 135 *dp = (png_byte)v; |
127 dp++; | 136 dp++; |
128 v = 0; | 137 v = 0; |
129 } | 138 } |
130 } | 139 } |
| 140 |
131 if (mask != 0x80) | 141 if (mask != 0x80) |
132 *dp = (png_byte)v; | 142 *dp = (png_byte)v; |
| 143 |
133 break; | 144 break; |
134 } | 145 } |
| 146 |
135 case 2: | 147 case 2: |
136 { | 148 { |
137 png_bytep sp, dp; | 149 png_bytep sp, dp; |
138 int shift, v; | 150 int shift, v; |
139 png_uint_32 i; | 151 png_uint_32 i; |
140 png_uint_32 row_width = row_info->width; | 152 png_uint_32 row_width = row_info->width; |
141 | 153 |
142 sp = row; | 154 sp = row; |
143 dp = row; | 155 dp = row; |
144 shift = 6; | 156 shift = 6; |
145 v = 0; | 157 v = 0; |
| 158 |
146 for (i = 0; i < row_width; i++) | 159 for (i = 0; i < row_width; i++) |
147 { | 160 { |
148 png_byte value; | 161 png_byte value; |
149 | 162 |
150 value = (png_byte)(*sp & 0x03); | 163 value = (png_byte)(*sp & 0x03); |
151 v |= (value << shift); | 164 v |= (value << shift); |
| 165 |
152 if (shift == 0) | 166 if (shift == 0) |
153 { | 167 { |
154 shift = 6; | 168 shift = 6; |
155 *dp = (png_byte)v; | 169 *dp = (png_byte)v; |
156 dp++; | 170 dp++; |
157 v = 0; | 171 v = 0; |
158 } | 172 } |
| 173 |
159 else | 174 else |
160 shift -= 2; | 175 shift -= 2; |
| 176 |
161 sp++; | 177 sp++; |
162 } | 178 } |
| 179 |
163 if (shift != 6) | 180 if (shift != 6) |
164 *dp = (png_byte)v; | 181 *dp = (png_byte)v; |
| 182 |
165 break; | 183 break; |
166 } | 184 } |
| 185 |
167 case 4: | 186 case 4: |
168 { | 187 { |
169 png_bytep sp, dp; | 188 png_bytep sp, dp; |
170 int shift, v; | 189 int shift, v; |
171 png_uint_32 i; | 190 png_uint_32 i; |
172 png_uint_32 row_width = row_info->width; | 191 png_uint_32 row_width = row_info->width; |
173 | 192 |
174 sp = row; | 193 sp = row; |
175 dp = row; | 194 dp = row; |
176 shift = 4; | 195 shift = 4; |
177 v = 0; | 196 v = 0; |
| 197 |
178 for (i = 0; i < row_width; i++) | 198 for (i = 0; i < row_width; i++) |
179 { | 199 { |
180 png_byte value; | 200 png_byte value; |
181 | 201 |
182 value = (png_byte)(*sp & 0x0f); | 202 value = (png_byte)(*sp & 0x0f); |
183 v |= (value << shift); | 203 v |= (value << shift); |
184 | 204 |
185 if (shift == 0) | 205 if (shift == 0) |
186 { | 206 { |
187 shift = 4; | 207 shift = 4; |
188 *dp = (png_byte)v; | 208 *dp = (png_byte)v; |
189 dp++; | 209 dp++; |
190 v = 0; | 210 v = 0; |
191 } | 211 } |
| 212 |
192 else | 213 else |
193 shift -= 4; | 214 shift -= 4; |
194 | 215 |
195 sp++; | 216 sp++; |
196 } | 217 } |
| 218 |
197 if (shift != 4) | 219 if (shift != 4) |
198 *dp = (png_byte)v; | 220 *dp = (png_byte)v; |
| 221 |
199 break; | 222 break; |
200 } | 223 } |
| 224 |
| 225 default: |
| 226 break; |
201 } | 227 } |
| 228 |
202 row_info->bit_depth = (png_byte)bit_depth; | 229 row_info->bit_depth = (png_byte)bit_depth; |
203 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); | 230 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); |
204 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, | 231 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
205 row_info->width); | 232 row_info->width); |
206 } | 233 } |
207 } | 234 } |
208 #endif | 235 #endif |
209 | 236 |
210 #ifdef PNG_WRITE_SHIFT_SUPPORTED | 237 #ifdef PNG_WRITE_SHIFT_SUPPORTED |
211 /* Shift pixel values to take advantage of whole range. Pass the | 238 /* Shift pixel values to take advantage of whole range. Pass the |
212 * true number of bits in bit_depth. The row should be packed | 239 * true number of bits in bit_depth. The row should be packed |
213 * according to row_info->bit_depth. Thus, if you had a row of | 240 * according to row_info->bit_depth. Thus, if you had a row of |
214 * bit depth 4, but the pixels only had values from 0 to 7, you | 241 * bit depth 4, but the pixels only had values from 0 to 7, you |
215 * would pass 3 as bit_depth, and this routine would translate the | 242 * would pass 3 as bit_depth, and this routine would translate the |
216 * data to 0 to 15. | 243 * data to 0 to 15. |
217 */ | 244 */ |
218 void /* PRIVATE */ | 245 void /* PRIVATE */ |
219 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth) | 246 png_do_shift(png_row_infop row_info, png_bytep row, |
| 247 png_const_color_8p bit_depth) |
220 { | 248 { |
221 png_debug(1, "in png_do_shift"); | 249 png_debug(1, "in png_do_shift"); |
222 | 250 |
223 #ifdef PNG_USELESS_TESTS_SUPPORTED | 251 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
224 if (row != NULL && row_info != NULL && | |
225 #else | |
226 if ( | |
227 #endif | |
228 row_info->color_type != PNG_COLOR_TYPE_PALETTE) | |
229 { | 252 { |
230 int shift_start[4], shift_dec[4]; | 253 int shift_start[4], shift_dec[4]; |
231 int channels = 0; | 254 int channels = 0; |
232 | 255 |
233 if (row_info->color_type & PNG_COLOR_MASK_COLOR) | 256 if (row_info->color_type & PNG_COLOR_MASK_COLOR) |
234 { | 257 { |
235 shift_start[channels] = row_info->bit_depth - bit_depth->red; | 258 shift_start[channels] = row_info->bit_depth - bit_depth->red; |
236 shift_dec[channels] = bit_depth->red; | 259 shift_dec[channels] = bit_depth->red; |
237 channels++; | 260 channels++; |
| 261 |
238 shift_start[channels] = row_info->bit_depth - bit_depth->green; | 262 shift_start[channels] = row_info->bit_depth - bit_depth->green; |
239 shift_dec[channels] = bit_depth->green; | 263 shift_dec[channels] = bit_depth->green; |
240 channels++; | 264 channels++; |
| 265 |
241 shift_start[channels] = row_info->bit_depth - bit_depth->blue; | 266 shift_start[channels] = row_info->bit_depth - bit_depth->blue; |
242 shift_dec[channels] = bit_depth->blue; | 267 shift_dec[channels] = bit_depth->blue; |
243 channels++; | 268 channels++; |
244 } | 269 } |
| 270 |
245 else | 271 else |
246 { | 272 { |
247 shift_start[channels] = row_info->bit_depth - bit_depth->gray; | 273 shift_start[channels] = row_info->bit_depth - bit_depth->gray; |
248 shift_dec[channels] = bit_depth->gray; | 274 shift_dec[channels] = bit_depth->gray; |
249 channels++; | 275 channels++; |
250 } | 276 } |
| 277 |
251 if (row_info->color_type & PNG_COLOR_MASK_ALPHA) | 278 if (row_info->color_type & PNG_COLOR_MASK_ALPHA) |
252 { | 279 { |
253 shift_start[channels] = row_info->bit_depth - bit_depth->alpha; | 280 shift_start[channels] = row_info->bit_depth - bit_depth->alpha; |
254 shift_dec[channels] = bit_depth->alpha; | 281 shift_dec[channels] = bit_depth->alpha; |
255 channels++; | 282 channels++; |
256 } | 283 } |
257 | 284 |
258 /* With low row depths, could only be grayscale, so one channel */ | 285 /* With low row depths, could only be grayscale, so one channel */ |
259 if (row_info->bit_depth < 8) | 286 if (row_info->bit_depth < 8) |
260 { | 287 { |
261 png_bytep bp = row; | 288 png_bytep bp = row; |
262 png_uint_32 i; | 289 png_size_t i; |
263 png_byte mask; | 290 unsigned int mask; |
264 png_uint_32 row_bytes = row_info->rowbytes; | 291 png_size_t row_bytes = row_info->rowbytes; |
265 | 292 |
266 if (bit_depth->gray == 1 && row_info->bit_depth == 2) | 293 if (bit_depth->gray == 1 && row_info->bit_depth == 2) |
267 mask = 0x55; | 294 mask = 0x55; |
| 295 |
268 else if (row_info->bit_depth == 4 && bit_depth->gray == 3) | 296 else if (row_info->bit_depth == 4 && bit_depth->gray == 3) |
269 mask = 0x11; | 297 mask = 0x11; |
| 298 |
270 else | 299 else |
271 mask = 0xff; | 300 mask = 0xff; |
272 | 301 |
273 for (i = 0; i < row_bytes; i++, bp++) | 302 for (i = 0; i < row_bytes; i++, bp++) |
274 { | 303 { |
275 png_uint_16 v; | |
276 int j; | 304 int j; |
| 305 unsigned int v, out; |
277 | 306 |
278 v = *bp; | 307 v = *bp; |
279 *bp = 0; | 308 out = 0; |
| 309 |
280 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) | 310 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) |
281 { | 311 { |
282 if (j > 0) | 312 if (j > 0) |
283 *bp |= (png_byte)((v << j) & 0xff); | 313 out |= v << j; |
| 314 |
284 else | 315 else |
285 *bp |= (png_byte)((v >> (-j)) & mask); | 316 out |= (v >> (-j)) & mask; |
286 } | 317 } |
| 318 |
| 319 *bp = (png_byte)(out & 0xff); |
287 } | 320 } |
288 } | 321 } |
| 322 |
289 else if (row_info->bit_depth == 8) | 323 else if (row_info->bit_depth == 8) |
290 { | 324 { |
291 png_bytep bp = row; | 325 png_bytep bp = row; |
292 png_uint_32 i; | 326 png_uint_32 i; |
293 png_uint_32 istop = channels * row_info->width; | 327 png_uint_32 istop = channels * row_info->width; |
294 | 328 |
295 for (i = 0; i < istop; i++, bp++) | 329 for (i = 0; i < istop; i++, bp++) |
296 { | 330 { |
297 | 331 |
298 png_uint_16 v; | 332 const unsigned int c = i%channels; |
299 int j; | 333 int j; |
300 int c = (int)(i%channels); | 334 unsigned int v, out; |
301 | 335 |
302 v = *bp; | 336 v = *bp; |
303 *bp = 0; | 337 out = 0; |
| 338 |
304 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) | 339 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) |
305 { | 340 { |
306 if (j > 0) | 341 if (j > 0) |
307 *bp |= (png_byte)((v << j) & 0xff); | 342 out |= v << j; |
| 343 |
308 else | 344 else |
309 *bp |= (png_byte)((v >> (-j)) & 0xff); | 345 out |= v >> (-j); |
310 } | 346 } |
| 347 |
| 348 *bp = (png_byte)(out & 0xff); |
311 } | 349 } |
312 } | 350 } |
| 351 |
313 else | 352 else |
314 { | 353 { |
315 png_bytep bp; | 354 png_bytep bp; |
316 png_uint_32 i; | 355 png_uint_32 i; |
317 png_uint_32 istop = channels * row_info->width; | 356 png_uint_32 istop = channels * row_info->width; |
318 | 357 |
319 for (bp = row, i = 0; i < istop; i++) | 358 for (bp = row, i = 0; i < istop; i++) |
320 { | 359 { |
321 int c = (int)(i%channels); | 360 const unsigned int c = i%channels; |
322 png_uint_16 value, v; | |
323 int j; | 361 int j; |
| 362 unsigned int value, v; |
324 | 363 |
325 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1)); | 364 v = png_get_uint_16(bp); |
326 value = 0; | 365 value = 0; |
| 366 |
327 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) | 367 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) |
328 { | 368 { |
329 if (j > 0) | 369 if (j > 0) |
330 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff); | 370 value |= v << j; |
| 371 |
331 else | 372 else |
332 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff); | 373 value |= v >> (-j); |
333 } | 374 } |
334 *bp++ = (png_byte)(value >> 8); | 375 *bp++ = (png_byte)((value >> 8) & 0xff); |
335 *bp++ = (png_byte)(value & 0xff); | 376 *bp++ = (png_byte)(value & 0xff); |
336 } | 377 } |
337 } | 378 } |
338 } | 379 } |
339 } | 380 } |
340 #endif | 381 #endif |
341 | 382 |
342 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED | 383 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
343 void /* PRIVATE */ | 384 void /* PRIVATE */ |
344 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) | 385 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) |
345 { | 386 { |
346 png_debug(1, "in png_do_write_swap_alpha"); | 387 png_debug(1, "in png_do_write_swap_alpha"); |
347 | 388 |
348 #ifdef PNG_USELESS_TESTS_SUPPORTED | |
349 if (row != NULL && row_info != NULL) | |
350 #endif | |
351 { | 389 { |
352 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | 390 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
353 { | 391 { |
354 /* This converts from ARGB to RGBA */ | |
355 if (row_info->bit_depth == 8) | 392 if (row_info->bit_depth == 8) |
356 { | 393 { |
| 394 /* This converts from ARGB to RGBA */ |
357 png_bytep sp, dp; | 395 png_bytep sp, dp; |
358 png_uint_32 i; | 396 png_uint_32 i; |
359 png_uint_32 row_width = row_info->width; | 397 png_uint_32 row_width = row_info->width; |
| 398 |
360 for (i = 0, sp = dp = row; i < row_width; i++) | 399 for (i = 0, sp = dp = row; i < row_width; i++) |
361 { | 400 { |
362 png_byte save = *(sp++); | 401 png_byte save = *(sp++); |
363 *(dp++) = *(sp++); | 402 *(dp++) = *(sp++); |
364 *(dp++) = *(sp++); | 403 *(dp++) = *(sp++); |
365 *(dp++) = *(sp++); | 404 *(dp++) = *(sp++); |
366 *(dp++) = save; | 405 *(dp++) = save; |
367 } | 406 } |
368 } | 407 } |
369 /* This converts from AARRGGBB to RRGGBBAA */ | 408 |
| 409 #ifdef PNG_WRITE_16BIT_SUPPORTED |
370 else | 410 else |
371 { | 411 { |
| 412 /* This converts from AARRGGBB to RRGGBBAA */ |
372 png_bytep sp, dp; | 413 png_bytep sp, dp; |
373 png_uint_32 i; | 414 png_uint_32 i; |
374 png_uint_32 row_width = row_info->width; | 415 png_uint_32 row_width = row_info->width; |
375 | 416 |
376 for (i = 0, sp = dp = row; i < row_width; i++) | 417 for (i = 0, sp = dp = row; i < row_width; i++) |
377 { | 418 { |
378 png_byte save[2]; | 419 png_byte save[2]; |
379 save[0] = *(sp++); | 420 save[0] = *(sp++); |
380 save[1] = *(sp++); | 421 save[1] = *(sp++); |
381 *(dp++) = *(sp++); | 422 *(dp++) = *(sp++); |
382 *(dp++) = *(sp++); | 423 *(dp++) = *(sp++); |
383 *(dp++) = *(sp++); | 424 *(dp++) = *(sp++); |
384 *(dp++) = *(sp++); | 425 *(dp++) = *(sp++); |
385 *(dp++) = *(sp++); | 426 *(dp++) = *(sp++); |
386 *(dp++) = *(sp++); | 427 *(dp++) = *(sp++); |
387 *(dp++) = save[0]; | 428 *(dp++) = save[0]; |
388 *(dp++) = save[1]; | 429 *(dp++) = save[1]; |
389 } | 430 } |
390 } | 431 } |
| 432 #endif /* PNG_WRITE_16BIT_SUPPORTED */ |
391 } | 433 } |
| 434 |
392 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | 435 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
393 { | 436 { |
394 /* This converts from AG to GA */ | |
395 if (row_info->bit_depth == 8) | 437 if (row_info->bit_depth == 8) |
396 { | 438 { |
| 439 /* This converts from AG to GA */ |
397 png_bytep sp, dp; | 440 png_bytep sp, dp; |
398 png_uint_32 i; | 441 png_uint_32 i; |
399 png_uint_32 row_width = row_info->width; | 442 png_uint_32 row_width = row_info->width; |
400 | 443 |
401 for (i = 0, sp = dp = row; i < row_width; i++) | 444 for (i = 0, sp = dp = row; i < row_width; i++) |
402 { | 445 { |
403 png_byte save = *(sp++); | 446 png_byte save = *(sp++); |
404 *(dp++) = *(sp++); | 447 *(dp++) = *(sp++); |
405 *(dp++) = save; | 448 *(dp++) = save; |
406 } | 449 } |
407 } | 450 } |
408 /* This converts from AAGG to GGAA */ | 451 |
| 452 #ifdef PNG_WRITE_16BIT_SUPPORTED |
409 else | 453 else |
410 { | 454 { |
| 455 /* This converts from AAGG to GGAA */ |
411 png_bytep sp, dp; | 456 png_bytep sp, dp; |
412 png_uint_32 i; | 457 png_uint_32 i; |
413 png_uint_32 row_width = row_info->width; | 458 png_uint_32 row_width = row_info->width; |
414 | 459 |
415 for (i = 0, sp = dp = row; i < row_width; i++) | 460 for (i = 0, sp = dp = row; i < row_width; i++) |
416 { | 461 { |
417 png_byte save[2]; | 462 png_byte save[2]; |
418 save[0] = *(sp++); | 463 save[0] = *(sp++); |
419 save[1] = *(sp++); | 464 save[1] = *(sp++); |
420 *(dp++) = *(sp++); | 465 *(dp++) = *(sp++); |
421 *(dp++) = *(sp++); | 466 *(dp++) = *(sp++); |
422 *(dp++) = save[0]; | 467 *(dp++) = save[0]; |
423 *(dp++) = save[1]; | 468 *(dp++) = save[1]; |
424 } | 469 } |
425 } | 470 } |
| 471 #endif /* PNG_WRITE_16BIT_SUPPORTED */ |
426 } | 472 } |
427 } | 473 } |
428 } | 474 } |
429 #endif | 475 #endif |
430 | 476 |
431 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED | 477 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
432 void /* PRIVATE */ | 478 void /* PRIVATE */ |
433 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) | 479 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) |
434 { | 480 { |
435 png_debug(1, "in png_do_write_invert_alpha"); | 481 png_debug(1, "in png_do_write_invert_alpha"); |
436 | 482 |
437 #ifdef PNG_USELESS_TESTS_SUPPORTED | |
438 if (row != NULL && row_info != NULL) | |
439 #endif | |
440 { | 483 { |
441 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | 484 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
442 { | 485 { |
443 /* This inverts the alpha channel in RGBA */ | |
444 if (row_info->bit_depth == 8) | 486 if (row_info->bit_depth == 8) |
445 { | 487 { |
| 488 /* This inverts the alpha channel in RGBA */ |
446 png_bytep sp, dp; | 489 png_bytep sp, dp; |
447 png_uint_32 i; | 490 png_uint_32 i; |
448 png_uint_32 row_width = row_info->width; | 491 png_uint_32 row_width = row_info->width; |
| 492 |
449 for (i = 0, sp = dp = row; i < row_width; i++) | 493 for (i = 0, sp = dp = row; i < row_width; i++) |
450 { | 494 { |
451 /* Does nothing | 495 /* Does nothing |
452 *(dp++) = *(sp++); | 496 *(dp++) = *(sp++); |
453 *(dp++) = *(sp++); | 497 *(dp++) = *(sp++); |
454 *(dp++) = *(sp++); | 498 *(dp++) = *(sp++); |
455 */ | 499 */ |
456 sp+=3; dp = sp; | 500 sp+=3; dp = sp; |
457 *(dp++) = (png_byte)(255 - *(sp++)); | 501 *(dp++) = (png_byte)(255 - *(sp++)); |
458 } | 502 } |
459 } | 503 } |
460 /* This inverts the alpha channel in RRGGBBAA */ | 504 |
| 505 #ifdef PNG_WRITE_16BIT_SUPPORTED |
461 else | 506 else |
462 { | 507 { |
| 508 /* This inverts the alpha channel in RRGGBBAA */ |
463 png_bytep sp, dp; | 509 png_bytep sp, dp; |
464 png_uint_32 i; | 510 png_uint_32 i; |
465 png_uint_32 row_width = row_info->width; | 511 png_uint_32 row_width = row_info->width; |
466 | 512 |
467 for (i = 0, sp = dp = row; i < row_width; i++) | 513 for (i = 0, sp = dp = row; i < row_width; i++) |
468 { | 514 { |
469 /* Does nothing | 515 /* Does nothing |
470 *(dp++) = *(sp++); | 516 *(dp++) = *(sp++); |
471 *(dp++) = *(sp++); | 517 *(dp++) = *(sp++); |
472 *(dp++) = *(sp++); | 518 *(dp++) = *(sp++); |
473 *(dp++) = *(sp++); | 519 *(dp++) = *(sp++); |
474 *(dp++) = *(sp++); | 520 *(dp++) = *(sp++); |
475 *(dp++) = *(sp++); | 521 *(dp++) = *(sp++); |
476 */ | 522 */ |
477 sp+=6; dp = sp; | 523 sp+=6; dp = sp; |
478 *(dp++) = (png_byte)(255 - *(sp++)); | 524 *(dp++) = (png_byte)(255 - *(sp++)); |
479 *(dp++) = (png_byte)(255 - *(sp++)); | 525 *(dp++) = (png_byte)(255 - *(sp++)); |
480 } | 526 } |
481 } | 527 } |
| 528 #endif /* PNG_WRITE_16BIT_SUPPORTED */ |
482 } | 529 } |
| 530 |
483 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | 531 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
484 { | 532 { |
485 /* This inverts the alpha channel in GA */ | |
486 if (row_info->bit_depth == 8) | 533 if (row_info->bit_depth == 8) |
487 { | 534 { |
| 535 /* This inverts the alpha channel in GA */ |
488 png_bytep sp, dp; | 536 png_bytep sp, dp; |
489 png_uint_32 i; | 537 png_uint_32 i; |
490 png_uint_32 row_width = row_info->width; | 538 png_uint_32 row_width = row_info->width; |
491 | 539 |
492 for (i = 0, sp = dp = row; i < row_width; i++) | 540 for (i = 0, sp = dp = row; i < row_width; i++) |
493 { | 541 { |
494 *(dp++) = *(sp++); | 542 *(dp++) = *(sp++); |
495 *(dp++) = (png_byte)(255 - *(sp++)); | 543 *(dp++) = (png_byte)(255 - *(sp++)); |
496 } | 544 } |
497 } | 545 } |
498 /* This inverts the alpha channel in GGAA */ | 546 |
| 547 #ifdef PNG_WRITE_16BIT_SUPPORTED |
499 else | 548 else |
500 { | 549 { |
| 550 /* This inverts the alpha channel in GGAA */ |
501 png_bytep sp, dp; | 551 png_bytep sp, dp; |
502 png_uint_32 i; | 552 png_uint_32 i; |
503 png_uint_32 row_width = row_info->width; | 553 png_uint_32 row_width = row_info->width; |
504 | 554 |
505 for (i = 0, sp = dp = row; i < row_width; i++) | 555 for (i = 0, sp = dp = row; i < row_width; i++) |
506 { | 556 { |
507 /* Does nothing | 557 /* Does nothing |
508 *(dp++) = *(sp++); | 558 *(dp++) = *(sp++); |
509 *(dp++) = *(sp++); | 559 *(dp++) = *(sp++); |
510 */ | 560 */ |
511 sp+=2; dp = sp; | 561 sp+=2; dp = sp; |
512 *(dp++) = (png_byte)(255 - *(sp++)); | 562 *(dp++) = (png_byte)(255 - *(sp++)); |
513 *(dp++) = (png_byte)(255 - *(sp++)); | 563 *(dp++) = (png_byte)(255 - *(sp++)); |
514 } | 564 } |
515 } | 565 } |
| 566 #endif /* PNG_WRITE_16BIT_SUPPORTED */ |
516 } | 567 } |
517 } | 568 } |
518 } | 569 } |
519 #endif | 570 #endif |
| 571 #endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */ |
520 | 572 |
521 #ifdef PNG_MNG_FEATURES_SUPPORTED | 573 #ifdef PNG_MNG_FEATURES_SUPPORTED |
522 /* Undoes intrapixel differencing */ | 574 /* Undoes intrapixel differencing */ |
523 void /* PRIVATE */ | 575 void /* PRIVATE */ |
524 png_do_write_intrapixel(png_row_infop row_info, png_bytep row) | 576 png_do_write_intrapixel(png_row_infop row_info, png_bytep row) |
525 { | 577 { |
526 png_debug(1, "in png_do_write_intrapixel"); | 578 png_debug(1, "in png_do_write_intrapixel"); |
527 | 579 |
528 if ( | 580 if ((row_info->color_type & PNG_COLOR_MASK_COLOR)) |
529 #ifdef PNG_USELESS_TESTS_SUPPORTED | |
530 row != NULL && row_info != NULL && | |
531 #endif | |
532 (row_info->color_type & PNG_COLOR_MASK_COLOR)) | |
533 { | 581 { |
534 int bytes_per_pixel; | 582 int bytes_per_pixel; |
535 png_uint_32 row_width = row_info->width; | 583 png_uint_32 row_width = row_info->width; |
536 if (row_info->bit_depth == 8) | 584 if (row_info->bit_depth == 8) |
537 { | 585 { |
538 png_bytep rp; | 586 png_bytep rp; |
539 png_uint_32 i; | 587 png_uint_32 i; |
540 | 588 |
541 if (row_info->color_type == PNG_COLOR_TYPE_RGB) | 589 if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
542 bytes_per_pixel = 3; | 590 bytes_per_pixel = 3; |
| 591 |
543 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | 592 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
544 bytes_per_pixel = 4; | 593 bytes_per_pixel = 4; |
| 594 |
545 else | 595 else |
546 return; | 596 return; |
547 | 597 |
548 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) | 598 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
549 { | 599 { |
550 *(rp) = (png_byte)((*rp - *(rp+1))&0xff); | 600 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff); |
551 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff); | 601 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff); |
552 } | 602 } |
553 } | 603 } |
| 604 |
| 605 #ifdef PNG_WRITE_16BIT_SUPPORTED |
554 else if (row_info->bit_depth == 16) | 606 else if (row_info->bit_depth == 16) |
555 { | 607 { |
556 png_bytep rp; | 608 png_bytep rp; |
557 png_uint_32 i; | 609 png_uint_32 i; |
558 | 610 |
559 if (row_info->color_type == PNG_COLOR_TYPE_RGB) | 611 if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
560 bytes_per_pixel = 6; | 612 bytes_per_pixel = 6; |
| 613 |
561 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | 614 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
562 bytes_per_pixel = 8; | 615 bytes_per_pixel = 8; |
| 616 |
563 else | 617 else |
564 return; | 618 return; |
565 | 619 |
566 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) | 620 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
567 { | 621 { |
568 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1); | 622 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); |
569 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3); | 623 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); |
570 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5); | 624 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); |
571 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); | 625 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); |
572 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); | 626 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); |
573 *(rp ) = (png_byte)((red >> 8) & 0xff); | 627 *(rp ) = (png_byte)((red >> 8) & 0xff); |
574 *(rp+1) = (png_byte)(red & 0xff); | 628 *(rp + 1) = (png_byte)(red & 0xff); |
575 *(rp+4) = (png_byte)((blue >> 8) & 0xff); | 629 *(rp + 4) = (png_byte)((blue >> 8) & 0xff); |
576 *(rp+5) = (png_byte)(blue & 0xff); | 630 *(rp + 5) = (png_byte)(blue & 0xff); |
577 } | 631 } |
578 } | 632 } |
| 633 #endif /* PNG_WRITE_16BIT_SUPPORTED */ |
579 } | 634 } |
580 } | 635 } |
581 #endif /* PNG_MNG_FEATURES_SUPPORTED */ | 636 #endif /* PNG_MNG_FEATURES_SUPPORTED */ |
582 #endif /* PNG_WRITE_SUPPORTED */ | 637 #endif /* PNG_WRITE_SUPPORTED */ |
OLD | NEW |