annotate lib/libpng/pngwtran.c @ 2407:378e5732ba38

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