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