Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1 | |
| 2 | /* pngwtran.c - transforms the data in a row for png writers |
| 3 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 4 | libpng 1.0 beta 2 - version 0.88 |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 5 | For conditions of distribution and use, see copyright notice in png.h |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 6 | Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
| 7 | January 25, 1996 |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #define PNG_INTERNAL |
| 11 | #include "png.h" |
| 12 | |
| 13 | /* transform the data according to the users wishes. The order of |
| 14 | transformations is significant. */ |
| 15 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 16 | png_do_write_transformations(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 17 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 18 | #if defined(PNG_WRITE_FILLER_SUPPORTED) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 19 | if (png_ptr->transformations & PNG_FILLER) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 20 | png_do_write_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 21 | png_ptr->filler_loc); |
| 22 | #endif |
| 23 | #if defined(PNG_WRITE_PACK_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 24 | if (png_ptr->transformations & PNG_PACK) |
| 25 | png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 26 | png_ptr->bit_depth); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 27 | #endif |
| 28 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 29 | if (png_ptr->transformations & PNG_SHIFT) |
| 30 | png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 31 | &(png_ptr->shift)); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 32 | #endif |
| 33 | #if defined(PNG_WRITE_SWAP_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 34 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
| 35 | png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 36 | #endif |
| 37 | #if defined(PNG_WRITE_BGR_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 38 | if (png_ptr->transformations & PNG_BGR) |
| 39 | png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 40 | #endif |
| 41 | #if defined(PNG_WRITE_INVERT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 42 | if (png_ptr->transformations & PNG_INVERT_MONO) |
| 43 | png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 44 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 47 | #if defined(PNG_WRITE_PACK_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 48 | /* pack pixels into bytes. Pass the true bit depth in bit_depth. The |
| 49 | row_info bit depth should be 8 (one pixel per byte). The channels |
| 50 | should be 1 (this only happens on grayscale and paletted images) */ |
| 51 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 52 | png_do_pack(png_row_infop row_info, png_bytep row, png_byte bit_depth) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 53 | { |
| 54 | if (row_info && row && row_info->bit_depth == 8 && |
| 55 | row_info->channels == 1) |
| 56 | { |
| 57 | switch (bit_depth) |
| 58 | { |
| 59 | case 1: |
| 60 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 61 | png_bytep sp; |
| 62 | png_bytep dp; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 63 | int mask; |
| 64 | png_int_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 65 | int v; |
| 66 | |
| 67 | sp = row; |
| 68 | dp = row; |
| 69 | mask = 0x80; |
| 70 | v = 0; |
| 71 | for (i = 0; i < row_info->width; i++) |
| 72 | { |
| 73 | if (*sp) |
| 74 | v |= mask; |
| 75 | sp++; |
| 76 | if (mask > 1) |
| 77 | mask >>= 1; |
| 78 | else |
| 79 | { |
| 80 | mask = 0x80; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 81 | *dp = (png_byte)v; |
| 82 | dp++; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 83 | v = 0; |
| 84 | } |
| 85 | } |
| 86 | if (mask != 0x80) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 87 | *dp = (png_byte)v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | case 2: |
| 91 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 92 | png_bytep sp; |
| 93 | png_bytep dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 94 | int shift; |
| 95 | png_int_32 i; |
| 96 | int v; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 97 | png_byte value; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 98 | |
| 99 | sp = row; |
| 100 | dp = row; |
| 101 | shift = 6; |
| 102 | v = 0; |
| 103 | for (i = 0; i < row_info->width; i++) |
| 104 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 105 | value = (png_byte)(*sp & 0x3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 106 | v |= (value << shift); |
| 107 | if (shift == 0) |
| 108 | { |
| 109 | shift = 6; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 110 | *dp = (png_byte)v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 111 | dp++; |
| 112 | v = 0; |
| 113 | } |
| 114 | else |
| 115 | shift -= 2; |
| 116 | sp++; |
| 117 | } |
| 118 | if (shift != 6) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 119 | *dp = (png_byte)v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | case 4: |
| 123 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 124 | png_bytep sp; |
| 125 | png_bytep dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 126 | int shift; |
| 127 | png_int_32 i; |
| 128 | int v; |
| 129 | png_byte value; |
| 130 | |
| 131 | sp = row; |
| 132 | dp = row; |
| 133 | shift = 4; |
| 134 | v = 0; |
| 135 | for (i = 0; i < row_info->width; i++) |
| 136 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 137 | value = (png_byte)(*sp & 0xf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 138 | v |= (value << shift); |
| 139 | |
| 140 | if (shift == 0) |
| 141 | { |
| 142 | shift = 4; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 143 | *dp = (png_byte)v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 144 | dp++; |
| 145 | v = 0; |
| 146 | } |
| 147 | else |
| 148 | shift -= 4; |
| 149 | |
| 150 | sp++; |
| 151 | } |
| 152 | if (shift != 4) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 153 | *dp = (png_byte)v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | } |
| 157 | row_info->bit_depth = bit_depth; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 158 | row_info->pixel_depth = (png_uint_16)(bit_depth * row_info->channels); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 159 | row_info->rowbytes = |
| 160 | ((row_info->width * row_info->pixel_depth + 7) >> 3); |
| 161 | } |
| 162 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 163 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 164 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 165 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 166 | /* shift pixel values to take advantage of whole range. Pass the |
| 167 | true number of bits in bit_depth. The row should be packed |
| 168 | according to row_info->bit_depth. Thus, if you had a row of |
| 169 | bit depth 4, but the pixels only had values from 0 to 7, you |
| 170 | would pass 3 as bit_depth, and this routine would translate the |
| 171 | data to 0 to 15. */ |
| 172 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 173 | png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 174 | { |
| 175 | if (row && row_info && |
| 176 | row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
| 177 | { |
| 178 | int shift_start[4], shift_dec[4]; |
| 179 | int channels; |
| 180 | |
| 181 | channels = 0; |
| 182 | if (row_info->color_type & PNG_COLOR_MASK_COLOR) |
| 183 | { |
| 184 | shift_start[channels] = row_info->bit_depth - bit_depth->red; |
| 185 | shift_dec[channels] = bit_depth->red; |
| 186 | channels++; |
| 187 | shift_start[channels] = row_info->bit_depth - bit_depth->green; |
| 188 | shift_dec[channels] = bit_depth->green; |
| 189 | channels++; |
| 190 | shift_start[channels] = row_info->bit_depth - bit_depth->blue; |
| 191 | shift_dec[channels] = bit_depth->blue; |
| 192 | channels++; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | shift_start[channels] = row_info->bit_depth - bit_depth->gray; |
| 197 | shift_dec[channels] = bit_depth->gray; |
| 198 | channels++; |
| 199 | } |
| 200 | if (row_info->color_type & PNG_COLOR_MASK_ALPHA) |
| 201 | { |
| 202 | shift_start[channels] = row_info->bit_depth - bit_depth->alpha; |
| 203 | shift_dec[channels] = bit_depth->alpha; |
| 204 | channels++; |
| 205 | } |
| 206 | |
| 207 | /* with low row dephts, could only be grayscale, so one channel */ |
| 208 | if (row_info->bit_depth < 8) |
| 209 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 210 | png_bytep bp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 211 | png_uint_32 i; |
| 212 | int j; |
| 213 | png_byte mask; |
| 214 | |
| 215 | if (bit_depth->gray == 1 && row_info->bit_depth == 2) |
| 216 | mask = 0x55; |
| 217 | else if (row_info->bit_depth == 4 && bit_depth->gray == 3) |
| 218 | mask = 0x11; |
| 219 | else |
| 220 | mask = 0xff; |
| 221 | |
| 222 | for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++) |
| 223 | { |
| 224 | int v; |
| 225 | |
| 226 | v = *bp; |
| 227 | *bp = 0; |
| 228 | for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) |
| 229 | { |
| 230 | if (j > 0) |
| 231 | *bp |= (png_byte)((v << j) & 0xff); |
| 232 | else |
| 233 | *bp |= (png_byte)((v >> (-j)) & mask); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | else if (row_info->bit_depth == 8) |
| 238 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 239 | png_bytep bp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 240 | png_uint_32 i; |
| 241 | int j; |
| 242 | |
| 243 | for (bp = row, i = 0; i < row_info->width; i++) |
| 244 | { |
| 245 | int c; |
| 246 | |
| 247 | for (c = 0; c < channels; c++, bp++) |
| 248 | { |
| 249 | int v; |
| 250 | |
| 251 | v = *bp; |
| 252 | *bp = 0; |
| 253 | for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) |
| 254 | { |
| 255 | if (j > 0) |
| 256 | *bp |= (png_byte)((v << j) & 0xff); |
| 257 | else |
| 258 | *bp |= (png_byte)((v >> (-j)) & 0xff); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | else |
| 264 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 265 | png_bytep bp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 266 | png_uint_32 i; |
| 267 | int j; |
| 268 | |
| 269 | for (bp = row, i = 0; |
| 270 | i < row_info->width * row_info->channels; |
| 271 | i++) |
| 272 | { |
| 273 | int c; |
| 274 | |
| 275 | for (c = 0; c < channels; c++, bp += 2) |
| 276 | { |
| 277 | png_uint_16 value, v; |
| 278 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 279 | v = (png_uint_16)(((png_uint_16)(*bp) << 8) + |
| 280 | (png_uint_16)(*(bp + 1))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 281 | value = 0; |
| 282 | for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) |
| 283 | { |
| 284 | if (j > 0) |
| 285 | value |= (png_uint_16)((v << j) & (png_uint_16)0xffff); |
| 286 | else |
| 287 | value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff); |
| 288 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 289 | *bp = (png_byte)(value >> 8); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 290 | *(bp + 1) = (png_byte)(value & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 296 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 297 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 298 | #ifdef PNG_WRITE_FILLER_SUPPORTED |
| 299 | /* remove filler byte */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 300 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 301 | png_do_write_filler(png_row_infop row_info, png_bytep row, |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 302 | png_byte filler_loc) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 303 | { |
| 304 | if (row && row_info && row_info->color_type == PNG_COLOR_TYPE_RGB && |
| 305 | row_info->bit_depth == 8) |
| 306 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 307 | if (filler_loc == PNG_FILLER_AFTER) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 308 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 309 | png_bytep sp, dp; |
| 310 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 311 | png_uint_32 i; |
| 312 | |
| 313 | for (i = 1, sp = row + 4, dp = row + 3; |
| 314 | i < row_info->width; |
| 315 | i++) |
| 316 | { |
| 317 | *dp++ = *sp++; |
| 318 | *dp++ = *sp++; |
| 319 | *dp++ = *sp++; |
| 320 | sp++; |
| 321 | } |
| 322 | row_info->channels = 3; |
| 323 | row_info->pixel_depth = 24; |
| 324 | row_info->rowbytes = row_info->width * 3; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 325 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 326 | else |
| 327 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 328 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 329 | png_uint_32 i; |
| 330 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 331 | for (i = 0, sp = row, dp = row; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 332 | i < row_info->width; |
| 333 | i++) |
| 334 | { |
| 335 | sp++; |
| 336 | *dp++ = *sp++; |
| 337 | *dp++ = *sp++; |
| 338 | *dp++ = *sp++; |
| 339 | } |
| 340 | row_info->channels = 3; |
| 341 | row_info->pixel_depth = 24; |
| 342 | row_info->rowbytes = row_info->width * 3; |
| 343 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 344 | } |
| 345 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 346 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 347 | |