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