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