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