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