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