blob: a5003d0a59cf279af2e7e7b125e612bf845d8b90 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwtran.c - transforms the data in a row for PNG writers
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsoncb1aee22011-04-16 19:27:34 -05004 * Last changed in libpng 1.5.3 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (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-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_WRITE_SUPPORTED
17
John Bowler4a12f4a2011-04-17 18:34:22 -050018#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050019/* Transform the data according to the user's wishes. The order of
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060020 * transformations is significant.
21 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050022void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060023png_do_write_transformations(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050024{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050025 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060026
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060027 if (png_ptr == NULL)
28 return;
29
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050030#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060031 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050032 if (png_ptr->write_user_transform_fn != NULL)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -050033 (*(png_ptr->write_user_transform_fn)) /* User write transform
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060034 function */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -050035 (png_ptr, /* png_ptr */
36 &(png_ptr->row_info), /* row_info: */
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-Pehrson08a33431998-03-07 06:06:55 -060044#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050045
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050046#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -060047 if (png_ptr->transformations & PNG_FILLER)
John Bowler9b872f42011-02-12 09:00:16 -060048 png_do_strip_channel(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrsoncb1aee22011-04-16 19:27:34 -050049 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050050#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050051
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050052#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060053 if (png_ptr->transformations & PNG_PACKSWAP)
54 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
55#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050056
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050057#ifdef PNG_WRITE_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050058 if (png_ptr->transformations & PNG_PACK)
59 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060060 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050061#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050062
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050063#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060064 if (png_ptr->transformations & PNG_SWAP_BYTES)
65 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
66#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050067
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050068#ifdef PNG_WRITE_SHIFT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050069 if (png_ptr->transformations & PNG_SHIFT)
70 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060071 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050072#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050073
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050074#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060075 if (png_ptr->transformations & PNG_SWAP_ALPHA)
76 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
77#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050078
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050079#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060080 if (png_ptr->transformations & PNG_INVERT_ALPHA)
81 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
82#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050083
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050084#ifdef PNG_WRITE_BGR_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050085 if (png_ptr->transformations & PNG_BGR)
86 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050087#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050088
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050089#ifdef PNG_WRITE_INVERT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050090 if (png_ptr->transformations & PNG_INVERT_MONO)
91 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050092#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050093}
94
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050095#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060096/* 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-Pehrson75294572000-05-06 14:09:57 -0500100void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500102{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500103 png_debug(1, "in png_do_pack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500104
Andreas Dilger47a0c421997-05-16 02:46:07 -0500105 if (row_info->bit_depth == 8 &&
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 row_info->channels == 1)
107 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500108 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500109 {
110 case 1:
111 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 png_bytep sp, dp;
113 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500114 png_uint_32 i;
115 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500116
117 sp = row;
118 dp = row;
119 mask = 0x80;
120 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600121
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500122 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500123 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500124 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500125 v |= mask;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500126
Guy Schalnat0d580581995-07-20 02:43:20 -0500127 sp++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500128
Guy Schalnat0d580581995-07-20 02:43:20 -0500129 if (mask > 1)
130 mask >>= 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500131
Guy Schalnat0d580581995-07-20 02:43:20 -0500132 else
133 {
134 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600135 *dp = (png_byte)v;
136 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137 v = 0;
138 }
139 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500140
Guy Schalnat0d580581995-07-20 02:43:20 -0500141 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600142 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500143
Guy Schalnat0d580581995-07-20 02:43:20 -0500144 break;
145 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500146
Guy Schalnat0d580581995-07-20 02:43:20 -0500147 case 2:
148 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500149 png_bytep sp, dp;
150 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500151 png_uint_32 i;
152 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500153
154 sp = row;
155 dp = row;
156 shift = 6;
157 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500158
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500159 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500160 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500161 png_byte value;
162
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600163 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500164 v |= (value << shift);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500165
Guy Schalnat0d580581995-07-20 02:43:20 -0500166 if (shift == 0)
167 {
168 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600169 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500170 dp++;
171 v = 0;
172 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500173
Guy Schalnat0d580581995-07-20 02:43:20 -0500174 else
175 shift -= 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500176
Guy Schalnat0d580581995-07-20 02:43:20 -0500177 sp++;
178 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500179
Guy Schalnat0d580581995-07-20 02:43:20 -0500180 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600181 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500182
Guy Schalnat0d580581995-07-20 02:43:20 -0500183 break;
184 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500185
Guy Schalnat0d580581995-07-20 02:43:20 -0500186 case 4:
187 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188 png_bytep sp, dp;
189 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500190 png_uint_32 i;
191 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500192
193 sp = row;
194 dp = row;
195 shift = 4;
196 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500197
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500198 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500199 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 png_byte value;
201
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600202 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500203 v |= (value << shift);
204
205 if (shift == 0)
206 {
207 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600208 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500209 dp++;
210 v = 0;
211 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500212
Guy Schalnat0d580581995-07-20 02:43:20 -0500213 else
214 shift -= 4;
215
216 sp++;
217 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500218
Guy Schalnat0d580581995-07-20 02:43:20 -0500219 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600220 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500221
Guy Schalnat0d580581995-07-20 02:43:20 -0500222 break;
223 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600224
225 default:
226 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500227 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500228
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 row_info->bit_depth = (png_byte)bit_depth;
230 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500231 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600232 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500233 }
234}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500235#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500236
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500237#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600238/* 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-Pehrson75294572000-05-06 14:09:57 -0500245void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500246png_do_shift(png_row_infop row_info, png_bytep row,
247 png_const_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500248{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500249 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500250
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500251 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500252 {
253 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500254 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500255
Guy Schalnat0d580581995-07-20 02:43:20 -0500256 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-Pehrsonf24daf22010-05-06 09:44:04 -0500261
Guy Schalnat0d580581995-07-20 02:43:20 -0500262 shift_start[channels] = row_info->bit_depth - bit_depth->green;
263 shift_dec[channels] = bit_depth->green;
264 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500265
Guy Schalnat0d580581995-07-20 02:43:20 -0500266 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
267 shift_dec[channels] = bit_depth->blue;
268 channels++;
269 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500270
Guy Schalnat0d580581995-07-20 02:43:20 -0500271 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-Pehrsonf24daf22010-05-06 09:44:04 -0500277
Guy Schalnat0d580581995-07-20 02:43:20 -0500278 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-Pehrson4bb4d012009-05-20 12:45:29 -0500285 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500286 if (row_info->bit_depth < 8)
287 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500288 png_bytep bp = row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500289 png_size_t i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500290 png_byte mask;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500291 png_size_t row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500292
293 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
294 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500295
Guy Schalnat0d580581995-07-20 02:43:20 -0500296 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
297 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500298
Guy Schalnat0d580581995-07-20 02:43:20 -0500299 else
300 mask = 0xff;
301
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500302 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500303 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 png_uint_16 v;
305 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500306
307 v = *bp;
308 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500309
Guy Schalnat0d580581995-07-20 02:43:20 -0500310 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
311 {
312 if (j > 0)
313 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500314
Guy Schalnat0d580581995-07-20 02:43:20 -0500315 else
316 *bp |= (png_byte)((v >> (-j)) & mask);
317 }
318 }
319 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500320
Guy Schalnat0d580581995-07-20 02:43:20 -0500321 else if (row_info->bit_depth == 8)
322 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500323 png_bytep bp = row;
324 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500325 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500326
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500327 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500328 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500329
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500330 png_uint_16 v;
331 int j;
332 int c = (int)(i%channels);
333
334 v = *bp;
335 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500336
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500337 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500338 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500339 if (j > 0)
340 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500341
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500342 else
343 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500344 }
345 }
346 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500347
Guy Schalnat0d580581995-07-20 02:43:20 -0500348 else
349 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600350 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500351 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500352 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500353
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500354 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500355 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500356 int c = (int)(i%channels);
357 png_uint_16 value, v;
358 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500359
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500360 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500361 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500362
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500363 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500364 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500365 if (j > 0)
366 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500367
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500368 else
369 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500370 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500371 *bp++ = (png_byte)(value >> 8);
372 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500373 }
374 }
375 }
376}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500377#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500378
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500379#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500380void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500382{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500383 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500384
Guy Schalnat0d580581995-07-20 02:43:20 -0500385 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500387 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500389 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600390 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500392 png_uint_32 i;
393 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500394
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500395 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500397 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500398 *(dp++) = *(sp++);
399 *(dp++) = *(sp++);
400 *(dp++) = *(sp++);
401 *(dp++) = save;
402 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500403 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500404
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500405#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406 else
407 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600408 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500410 png_uint_32 i;
411 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500412
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500413 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500415 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500416 save[0] = *(sp++);
417 save[1] = *(sp++);
418 *(dp++) = *(sp++);
419 *(dp++) = *(sp++);
420 *(dp++) = *(sp++);
421 *(dp++) = *(sp++);
422 *(dp++) = *(sp++);
423 *(dp++) = *(sp++);
424 *(dp++) = save[0];
425 *(dp++) = save[1];
426 }
427 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500428#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500429 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Andreas Dilger47a0c421997-05-16 02:46:07 -0500431 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500432 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500434 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600435 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500436 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500437 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500438 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500440 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500442 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 *(dp++) = *(sp++);
444 *(dp++) = save;
445 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500446 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500447
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500448#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500449 else
450 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600451 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500454 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500455
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500456 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500457 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500458 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500459 save[0] = *(sp++);
460 save[1] = *(sp++);
461 *(dp++) = *(sp++);
462 *(dp++) = *(sp++);
463 *(dp++) = save[0];
464 *(dp++) = save[1];
465 }
466 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500467#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500468 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500469 }
470}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500471#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500472
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500473#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500474void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600475png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
476{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500477 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500478
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600479 {
480 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
481 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600482 if (row_info->bit_depth == 8)
483 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600484 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600485 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500486 png_uint_32 i;
487 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500488
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500489 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600490 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500491 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600492 *(dp++) = *(sp++);
493 *(dp++) = *(sp++);
494 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600495 */
496 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500497 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600498 }
499 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500500
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500501#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600502 else
503 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600504 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600505 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500506 png_uint_32 i;
507 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600508
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500509 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600510 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500511 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600512 *(dp++) = *(sp++);
513 *(dp++) = *(sp++);
514 *(dp++) = *(sp++);
515 *(dp++) = *(sp++);
516 *(dp++) = *(sp++);
517 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600518 */
519 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500520 *(dp++) = (png_byte)(255 - *(sp++));
521 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600522 }
523 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500524#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600525 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500526
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600527 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
528 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600529 if (row_info->bit_depth == 8)
530 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600531 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600532 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500533 png_uint_32 i;
534 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600535
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500536 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600537 {
538 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500539 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600540 }
541 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500542
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500543#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600544 else
545 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600546 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600547 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500548 png_uint_32 i;
549 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600550
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500551 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600552 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500553 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600554 *(dp++) = *(sp++);
555 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600556 */
557 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500558 *(dp++) = (png_byte)(255 - *(sp++));
559 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600560 }
561 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500562#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600563 }
564 }
565}
566#endif
John Bowler4a12f4a2011-04-17 18:34:22 -0500567#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600568
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500569#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500570/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600571void /* PRIVATE */
572png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
573{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500574 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500575
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500576 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600577 {
578 int bytes_per_pixel;
579 png_uint_32 row_width = row_info->width;
580 if (row_info->bit_depth == 8)
581 {
582 png_bytep rp;
583 png_uint_32 i;
584
585 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
586 bytes_per_pixel = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500587
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600588 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
589 bytes_per_pixel = 4;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500590
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600591 else
592 return;
593
594 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
595 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600596 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
597 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600598 }
599 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500600
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500601#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600602 else if (row_info->bit_depth == 16)
603 {
604 png_bytep rp;
605 png_uint_32 i;
606
607 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
608 bytes_per_pixel = 6;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500609
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600610 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
611 bytes_per_pixel = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500612
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600613 else
614 return;
615
616 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
617 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600618 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
619 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
620 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500621 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
622 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600623 *(rp ) = (png_byte)((red >> 8) & 0xff);
624 *(rp + 1) = (png_byte)(red & 0xff);
625 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
626 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600627 }
628 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500629#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600630 }
631}
632#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500633#endif /* PNG_WRITE_SUPPORTED */