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