blob: 3e1b345fdcdb6fb3127a0078d506af518862b217 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwutil.c - utilities to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -05004 * libpng 1.2.1beta2 - October 25, 2001
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -06006 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050013#ifdef PNG_WRITE_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
18 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050019void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -050028#if defined(PNG_WRITE_pCAL_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -050029/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050033void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050034png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
41#endif
42
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050047void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Andreas Dilger47a0c421997-05-16 02:46:07 -050054/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 * representing the chunk name. The array must be at least 4 bytes in
56 * length, and does not need to be null terminated. To be safe, pass the
57 * pre-defined chunk names here, and if you need a new one, define it
58 * where the others are defined. The length is the length of the data.
59 * All the data must be present. If that is not possible, use the
60 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
61 * functions instead.
62 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050063void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050066{
Andreas Dilger47a0c421997-05-16 02:46:07 -050067 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 png_write_chunk_data(png_ptr, data, length);
69 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050070}
71
Andreas Dilger47a0c421997-05-16 02:46:07 -050072/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073 * The total_length is the sum of the lengths of all the data you will be
74 * passing in png_write_chunk_data().
75 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050076void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
78 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050079{
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050081 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050082
Guy Schalnat0d580581995-07-20 02:43:20 -050083 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050084 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
86
Guy Schalnat0d580581995-07-20 02:43:20 -050087 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050089 /* reset the crc and run it over the chunk name */
90 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050092}
93
Andreas Dilger47a0c421997-05-16 02:46:07 -050094/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095 * Note that multiple calls to this function are allowed, and that the
96 * sum of the lengths from these calls *must* add up to the total_length
97 * given to png_write_chunk_start().
98 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050099void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 /* write the data, and run the CRC over it */
103 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 {
105 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600106 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 }
108}
109
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600112png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 png_byte buf[4];
115
Guy Schalnat0d580581995-07-20 02:43:20 -0500116 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118
119 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500120}
121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600122/* Simple function to write the signature. If we have already written
123 * the magic bytes of the signature, or more likely, the PNG stream is
124 * being embedded into another stream and doesn't need its own signature,
125 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600126 * bytes have already been written.
127 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500128void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600129png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600132 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600133 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 (png_size_t)8 - png_ptr->sig_bytes);
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600135 if(png_ptr->sig_bytes < 3)
136 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137}
138
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600139#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600140/*
141 * This pair of functions encapsulates the operation of (a) compressing a
142 * text string, and (b) issuing it later as a series of chunk data writes.
143 * The compression_state structure is shared context for these functions
144 * set up by the caller in order to make the whole mess thread-safe.
145 */
146
147typedef struct
148{
149 char *input; /* the uncompressed input data */
150 int input_len; /* its length */
151 int num_output_ptr; /* number of output pointers used */
152 int max_output_ptr; /* size of output_ptr */
153 png_charpp output_ptr; /* array of pointers to output */
154} compression_state;
155
156/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500157static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600158png_text_compress(png_structp png_ptr,
159 png_charp text, png_size_t text_len, int compression,
160 compression_state *comp)
161{
162 int ret;
163
164 comp->num_output_ptr = comp->max_output_ptr = 0;
165 comp->output_ptr = NULL;
166 comp->input = NULL;
167
168 /* we may just want to pass the text right through */
169 if (compression == PNG_TEXT_COMPRESSION_NONE)
170 {
171 comp->input = text;
172 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500173 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600174 }
175
176 if (compression >= PNG_TEXT_COMPRESSION_LAST)
177 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500178#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600179 char msg[50];
180 sprintf(msg, "Unknown compression type %d", compression);
181 png_warning(png_ptr, msg);
182#else
183 png_warning(png_ptr, "Unknown compression type");
184#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600185 }
186
187 /* We can't write the chunk until we find out how much data we have,
188 * which means we need to run the compressor first and save the
189 * output. This shouldn't be a problem, as the vast majority of
190 * comments should be reasonable, but we will set up an array of
191 * malloc'd pointers to be sure.
192 *
193 * If we knew the application was well behaved, we could simplify this
194 * greatly by assuming we can always malloc an output buffer large
195 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
196 * and malloc this directly. The only time this would be a bad idea is
197 * if we can't malloc more than 64K and we have 64K of random input
198 * data, or if the input string is incredibly large (although this
199 * wouldn't cause a failure, just a slowdown due to swapping).
200 */
201
202 /* set up the compression buffers */
203 png_ptr->zstream.avail_in = (uInt)text_len;
204 png_ptr->zstream.next_in = (Bytef *)text;
205 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
206 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
207
208 /* this is the same compression loop as in png_write_row() */
209 do
210 {
211 /* compress the data */
212 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
213 if (ret != Z_OK)
214 {
215 /* error */
216 if (png_ptr->zstream.msg != NULL)
217 png_error(png_ptr, png_ptr->zstream.msg);
218 else
219 png_error(png_ptr, "zlib error");
220 }
221 /* check to see if we need more room */
222 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
223 {
224 /* make sure the output array has room */
225 if (comp->num_output_ptr >= comp->max_output_ptr)
226 {
227 int old_max;
228
229 old_max = comp->max_output_ptr;
230 comp->max_output_ptr = comp->num_output_ptr + 4;
231 if (comp->output_ptr != NULL)
232 {
233 png_charpp old_ptr;
234
235 old_ptr = comp->output_ptr;
236 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
237 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500238 png_memcpy(comp->output_ptr, old_ptr, old_max
239 * sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600240 png_free(png_ptr, old_ptr);
241 }
242 else
243 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
244 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
245 }
246
247 /* save the data */
248 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
249 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500250 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
251 png_ptr->zbuf_size);
252 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600253
254 /* and reset the buffer */
255 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
256 png_ptr->zstream.next_out = png_ptr->zbuf;
257 }
258 /* continue until we don't have any more to compress */
259 } while (png_ptr->zstream.avail_in);
260
261 /* finish the compression */
262 do
263 {
264 /* tell zlib we are finished */
265 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500266
267 if (ret == Z_OK)
268 {
269 /* check to see if we need more room */
270 if (!(png_ptr->zstream.avail_out))
271 {
272 /* check to make sure our output array has room */
273 if (comp->num_output_ptr >= comp->max_output_ptr)
274 {
275 int old_max;
276
277 old_max = comp->max_output_ptr;
278 comp->max_output_ptr = comp->num_output_ptr + 4;
279 if (comp->output_ptr != NULL)
280 {
281 png_charpp old_ptr;
282
283 old_ptr = comp->output_ptr;
284 /* This could be optimized to realloc() */
285 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
286 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500287 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500288 old_max * sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500289 png_free(png_ptr, old_ptr);
290 }
291 else
292 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
293 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
294 }
295
296 /* save off the data */
297 comp->output_ptr[comp->num_output_ptr] =
298 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500299 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
300 png_ptr->zbuf_size);
301 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500302
303 /* and reset the buffer pointers */
304 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
305 png_ptr->zstream.next_out = png_ptr->zbuf;
306 }
307 }
308 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600309 {
310 /* we got an error */
311 if (png_ptr->zstream.msg != NULL)
312 png_error(png_ptr, png_ptr->zstream.msg);
313 else
314 png_error(png_ptr, "zlib error");
315 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600316 } while (ret != Z_STREAM_END);
317
318 /* text length is number of buffers plus last buffer */
319 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
320 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
321 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
322
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500323 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600324}
325
326/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500327static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600328png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
329{
330 int i;
331
332 /* handle the no-compression case */
333 if (comp->input)
334 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500335 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
336 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600337 return;
338 }
339
340 /* write saved output buffers, if any */
341 for (i = 0; i < comp->num_output_ptr; i++)
342 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600343 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
344 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600345 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500346 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600347 }
348 if (comp->max_output_ptr != 0)
349 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500350 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 /* write anything left in zbuf */
352 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
353 png_write_chunk_data(png_ptr, png_ptr->zbuf,
354 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
355
356 /* reset zlib for another zTXt/iTXt or the image data */
357 deflateReset(&png_ptr->zstream);
358
359}
360#endif
361
Guy Schalnat0d580581995-07-20 02:43:20 -0500362/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600363 * information. Note that the rest of this code depends upon this
364 * information being correct.
365 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500366void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600367png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 int bit_depth, int color_type, int compression_type, int filter_type,
369 int interlace_type)
370{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600371#ifdef PNG_USE_LOCAL_ARRAYS
372 PNG_IHDR;
373#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 png_byte buf[13]; /* buffer to store the IHDR info */
375
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500377 /* Check that we have valid input data from the application info */
378 switch (color_type)
379 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500381 switch (bit_depth)
382 {
383 case 1:
384 case 2:
385 case 4:
386 case 8:
387 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500389 }
390 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500392 if (bit_depth != 8 && bit_depth != 16)
393 png_error(png_ptr, "Invalid bit depth for RGB image");
394 png_ptr->channels = 3;
395 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500397 switch (bit_depth)
398 {
399 case 1:
400 case 2:
401 case 4:
402 case 8: png_ptr->channels = 1; break;
403 default: png_error(png_ptr, "Invalid bit depth for paletted image");
404 }
405 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500407 if (bit_depth != 8 && bit_depth != 16)
408 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
409 png_ptr->channels = 2;
410 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500411 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500412 if (bit_depth != 8 && bit_depth != 16)
413 png_error(png_ptr, "Invalid bit depth for RGBA image");
414 png_ptr->channels = 4;
415 break;
416 default:
417 png_error(png_ptr, "Invalid image color type specified");
418 }
419
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500421 {
422 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500424 }
425
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600426 /* Write filter_method 64 (intrapixel differencing) only if
427 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
428 * 2. Libpng did not write a PNG signature (this filter_method is only
429 * used in PNG datastreams that are embedded in MNG datastreams) and
430 * 3. The application called png_permit_mng_features with a mask that
431 * included PNG_FLAG_MNG_FILTER_64 and
432 * 4. The filter_method is 64 and
433 * 5. The color_type is RGB or RGBA
434 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600435 if (
436#if defined(PNG_MNG_FEATURES_SUPPORTED)
437 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600438 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500439 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600440 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600441 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
442#endif
443 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500444 {
445 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500447 }
448
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600449#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500450 if (interlace_type != PNG_INTERLACE_NONE &&
451 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500452 {
453 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500454 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500455 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600456#else
457 interlace_type=PNG_INTERLACE_NONE;
458#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500459
460 /* save off the relevent information */
461 png_ptr->bit_depth = (png_byte)bit_depth;
462 png_ptr->color_type = (png_byte)color_type;
463 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500464#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600465 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500466#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500467 png_ptr->width = width;
468 png_ptr->height = height;
469
470 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500472 /* set the usr info, so any transformations can modify it */
473 png_ptr->usr_width = png_ptr->width;
474 png_ptr->usr_bit_depth = png_ptr->bit_depth;
475 png_ptr->usr_channels = png_ptr->channels;
476
Guy Schalnat0d580581995-07-20 02:43:20 -0500477 /* pack the header information into the buffer */
478 png_save_uint_32(buf, width);
479 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600480 buf[8] = (png_byte)bit_depth;
481 buf[9] = (png_byte)color_type;
482 buf[10] = (png_byte)compression_type;
483 buf[11] = (png_byte)filter_type;
484 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500485
486 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600487 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500488
Andreas Dilger47a0c421997-05-16 02:46:07 -0500489 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600490 png_ptr->zstream.zalloc = png_zalloc;
491 png_ptr->zstream.zfree = png_zfree;
492 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500493 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500494 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500495 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
496 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500497 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500498 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500499 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500500 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500501 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500502 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500503 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500504 png_ptr->zlib_strategy = Z_FILTERED;
505 else
506 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
507 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500509 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500511 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500512 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500513 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500514 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500515 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600516 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500517 png_ptr->zlib_method, png_ptr->zlib_window_bits,
518 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600519 png_ptr->zstream.next_out = png_ptr->zbuf;
520 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500521
Guy Schalnate5a37791996-06-05 15:50:50 -0500522 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500523}
524
525/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500526 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600527 * structure.
528 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500529void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500531{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600532#ifdef PNG_USE_LOCAL_ARRAYS
533 PNG_PLTE;
534#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600536 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500537 png_byte buf[3];
538
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500540 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500541#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600542 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500543#endif
544 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500545 {
546 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500547 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500548 png_error(png_ptr, "Invalid number of colors in palette");
549 }
550 else
551 {
552 png_warning(png_ptr, "Invalid number of colors in palette");
553 return;
554 }
555 }
556
557 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
558 {
559 png_warning(png_ptr,
560 "Ignoring request to write a PLTE chunk in grayscale PNG");
561 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500562 }
563
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564 png_ptr->num_palette = (png_uint_16)num_pal;
565 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500566
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600567 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500568#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500570 {
571 buf[0] = pal_ptr->red;
572 buf[1] = pal_ptr->green;
573 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500576#else
577 /* This is a little slower but some buggy compilers need to do this instead */
578 pal_ptr=palette;
579 for (i = 0; i < num_pal; i++)
580 {
581 buf[0] = pal_ptr[i].red;
582 buf[1] = pal_ptr[i].green;
583 buf[2] = pal_ptr[i].blue;
584 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
585 }
586#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500587 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500588 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500589}
590
591/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500592void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500593png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500594{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600595#ifdef PNG_USE_LOCAL_ARRAYS
596 PNG_IDAT;
597#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500598 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600599 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500600 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500601}
602
603/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600605png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500606{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600607#ifdef PNG_USE_LOCAL_ARRAYS
608 PNG_IEND;
609#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500611 png_write_chunk(png_ptr, (png_bytep)png_IEND, (png_bytep)NULL,
612 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500614}
615
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500616#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500617/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600618#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500619void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500620png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500621{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600622#ifdef PNG_USE_LOCAL_ARRAYS
623 PNG_gAMA;
624#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500625 png_uint_32 igamma;
626 png_byte buf[4];
627
Andreas Dilger47a0c421997-05-16 02:46:07 -0500628 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600629 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600630 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500631 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600632 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500633}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500634#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600635#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500636void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600637png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600638{
639#ifdef PNG_USE_LOCAL_ARRAYS
640 PNG_gAMA;
641#endif
642 png_byte buf[4];
643
644 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600645 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500646 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600647 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
648}
649#endif
650#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500651
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600652#if defined(PNG_WRITE_sRGB_SUPPORTED)
653/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500654void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600655png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600656{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600657#ifdef PNG_USE_LOCAL_ARRAYS
658 PNG_sRGB;
659#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600660 png_byte buf[1];
661
662 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600663 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600664 png_warning(png_ptr,
665 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600666 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600667 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600668}
669#endif
670
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600671#if defined(PNG_WRITE_iCCP_SUPPORTED)
672/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500673void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600674png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
675 png_charp profile, int profile_len)
676{
677#ifdef PNG_USE_LOCAL_ARRAYS
678 PNG_iCCP;
679#endif
680 png_size_t name_len;
681 png_charp new_name;
682 compression_state comp;
683
684 png_debug(1, "in png_write_iCCP\n");
685 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
686 &new_name)) == 0)
687 {
688 png_warning(png_ptr, "Empty keyword in iCCP chunk");
689 return;
690 }
691
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600692 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600693 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600694
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500695 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600696 profile_len = 0;
697
698 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500699 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600700 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600701
702 /* make sure we include the NULL after the name and the compression type */
703 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
704 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600705 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600706 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
707
708 if (profile_len)
709 png_write_compressed_data_out(png_ptr, &comp);
710
711 png_write_chunk_end(png_ptr);
712 png_free(png_ptr, new_name);
713}
714#endif
715
716#if defined(PNG_WRITE_sPLT_SUPPORTED)
717/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500718void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600719png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600720{
721#ifdef PNG_USE_LOCAL_ARRAYS
722 PNG_sPLT;
723#endif
724 png_size_t name_len;
725 png_charp new_name;
726 png_byte entrybuf[10];
727 int entry_size = (spalette->depth == 8 ? 6 : 10);
728 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600729 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500730#ifdef PNG_NO_POINTER_INDEXING
731 int i;
732#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600733
734 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600735 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
736 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600737 {
738 png_warning(png_ptr, "Empty keyword in sPLT chunk");
739 return;
740 }
741
742 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500743 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600744 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600745 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600746 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600747
748 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500749#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600750 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
751 {
752 if (spalette->depth == 8)
753 {
754 entrybuf[0] = (png_byte)ep->red;
755 entrybuf[1] = (png_byte)ep->green;
756 entrybuf[2] = (png_byte)ep->blue;
757 entrybuf[3] = (png_byte)ep->alpha;
758 png_save_uint_16(entrybuf + 4, ep->frequency);
759 }
760 else
761 {
762 png_save_uint_16(entrybuf + 0, ep->red);
763 png_save_uint_16(entrybuf + 2, ep->green);
764 png_save_uint_16(entrybuf + 4, ep->blue);
765 png_save_uint_16(entrybuf + 6, ep->alpha);
766 png_save_uint_16(entrybuf + 8, ep->frequency);
767 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500768 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600769 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500770#else
771 ep=spalette->entries;
772 for (i=0; i>spalette->nentries; i++)
773 {
774 if (spalette->depth == 8)
775 {
776 entrybuf[0] = (png_byte)ep[i].red;
777 entrybuf[1] = (png_byte)ep[i].green;
778 entrybuf[2] = (png_byte)ep[i].blue;
779 entrybuf[3] = (png_byte)ep[i].alpha;
780 png_save_uint_16(entrybuf + 4, ep[i].frequency);
781 }
782 else
783 {
784 png_save_uint_16(entrybuf + 0, ep[i].red);
785 png_save_uint_16(entrybuf + 2, ep[i].green);
786 png_save_uint_16(entrybuf + 4, ep[i].blue);
787 png_save_uint_16(entrybuf + 6, ep[i].alpha);
788 png_save_uint_16(entrybuf + 8, ep[i].frequency);
789 }
790 png_write_chunk_data(png_ptr, entrybuf, entry_size);
791 }
792#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600793
794 png_write_chunk_end(png_ptr);
795 png_free(png_ptr, new_name);
796}
797#endif
798
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500799#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500800/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500801void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600802png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500803{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600804#ifdef PNG_USE_LOCAL_ARRAYS
805 PNG_sBIT;
806#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500807 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500808 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500809
Andreas Dilger47a0c421997-05-16 02:46:07 -0500810 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600811 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500812 if (color_type & PNG_COLOR_MASK_COLOR)
813 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600814 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500815
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500816 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
817 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600818 if (sbit->red == 0 || sbit->red > maxbits ||
819 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500820 sbit->blue == 0 || sbit->blue > maxbits)
821 {
822 png_warning(png_ptr, "Invalid sBIT depth specified");
823 return;
824 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500825 buf[0] = sbit->red;
826 buf[1] = sbit->green;
827 buf[2] = sbit->blue;
828 size = 3;
829 }
830 else
831 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500832 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
833 {
834 png_warning(png_ptr, "Invalid sBIT depth specified");
835 return;
836 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 buf[0] = sbit->gray;
838 size = 1;
839 }
840
841 if (color_type & PNG_COLOR_MASK_ALPHA)
842 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500843 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
844 {
845 png_warning(png_ptr, "Invalid sBIT depth specified");
846 return;
847 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500848 buf[size++] = sbit->alpha;
849 }
850
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600851 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500852}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500853#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500854
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500855#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500856/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600857#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500858void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500859png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600860 double red_x, double red_y, double green_x, double green_y,
861 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500862{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600863#ifdef PNG_USE_LOCAL_ARRAYS
864 PNG_cHRM;
865#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500866 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600867 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500868
Andreas Dilger47a0c421997-05-16 02:46:07 -0500869 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600870 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500871 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
872 white_x + white_y > 1.0)
873 {
874 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500875#if !defined(PNG_NO_CONSOLE_IO)
876 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600877#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500878 return;
879 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600880 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500881 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600882 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500883 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500884
885 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
886 red_x + red_y > 1.0)
887 {
888 png_warning(png_ptr, "Invalid cHRM red point specified");
889 return;
890 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600891 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500892 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600893 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500894 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500895
896 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
897 green_x + green_y > 1.0)
898 {
899 png_warning(png_ptr, "Invalid cHRM green point specified");
900 return;
901 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600902 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500903 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600904 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500905 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500906
907 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
908 blue_x + blue_y > 1.0)
909 {
910 png_warning(png_ptr, "Invalid cHRM blue point specified");
911 return;
912 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600913 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500914 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600915 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500916 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500917
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600918 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500919}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500920#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600921#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500922void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600923png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
924 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
925 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
926 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600927{
928#ifdef PNG_USE_LOCAL_ARRAYS
929 PNG_cHRM;
930#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600931 png_byte buf[32];
932
933 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600934 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600935 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
936 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600937 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500938#if !defined(PNG_NO_CONSOLE_IO)
939 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600940#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600941 return;
942 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500943 png_save_uint_32(buf, (png_uint_32)white_x);
944 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600945
946 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
947 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600948 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600949 return;
950 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500951 png_save_uint_32(buf + 8, (png_uint_32)red_x);
952 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600953
954 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
955 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600956 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600957 return;
958 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500959 png_save_uint_32(buf + 16, (png_uint_32)green_x);
960 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600961
962 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
963 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600964 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600965 return;
966 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500967 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
968 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600969
970 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
971}
972#endif
973#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500974
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500975#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500976/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500977void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600978png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500979 int num_trans, int color_type)
980{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600981#ifdef PNG_USE_LOCAL_ARRAYS
982 PNG_tRNS;
983#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500984 png_byte buf[6];
985
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500987 if (color_type == PNG_COLOR_TYPE_PALETTE)
988 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600989 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500990 {
991 png_warning(png_ptr,"Invalid number of transparent colors specified");
992 return;
993 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500994 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600995 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500996 }
997 else if (color_type == PNG_COLOR_TYPE_GRAY)
998 {
999 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001000 if(tran->gray >= (1 << png_ptr->bit_depth))
1001 {
1002 png_warning(png_ptr,
1003 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1004 return;
1005 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001006 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001007 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001008 }
1009 else if (color_type == PNG_COLOR_TYPE_RGB)
1010 {
1011 /* three 16 bit values */
1012 png_save_uint_16(buf, tran->red);
1013 png_save_uint_16(buf + 2, tran->green);
1014 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001015 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1016 {
1017 png_warning(png_ptr,
1018 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1019 return;
1020 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001021 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001022 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001023 else
1024 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001025 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001026 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001027}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001028#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001029
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001030#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001031/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001032void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001033png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001034{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001035#ifdef PNG_USE_LOCAL_ARRAYS
1036 PNG_bKGD;
1037#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001038 png_byte buf[6];
1039
Andreas Dilger47a0c421997-05-16 02:46:07 -05001040 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001041 if (color_type == PNG_COLOR_TYPE_PALETTE)
1042 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001043 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001044#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001045 (png_ptr->num_palette ||
1046 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001047#endif
1048 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001049 {
1050 png_warning(png_ptr, "Invalid background palette index");
1051 return;
1052 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001053 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001054 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001055 }
1056 else if (color_type & PNG_COLOR_MASK_COLOR)
1057 {
1058 png_save_uint_16(buf, back->red);
1059 png_save_uint_16(buf + 2, back->green);
1060 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001061 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1062 {
1063 png_warning(png_ptr,
1064 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1065 return;
1066 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001067 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001068 }
1069 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001070 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001071 if(back->gray >= (1 << png_ptr->bit_depth))
1072 {
1073 png_warning(png_ptr,
1074 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1075 return;
1076 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001077 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001078 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001079 }
1080}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001081#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001082
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001083#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001084/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001085void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001086png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001087{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001088#ifdef PNG_USE_LOCAL_ARRAYS
1089 PNG_hIST;
1090#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001091 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001092 png_byte buf[3];
1093
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001095 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001096 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1098 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001099 png_warning(png_ptr, "Invalid number of histogram entries specified");
1100 return;
1101 }
1102
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001103 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001104 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001105 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001106 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001107 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001108 }
1109 png_write_chunk_end(png_ptr);
1110}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001111#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001112
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001113#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1114 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001115/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1116 * and if invalid, correct the keyword rather than discarding the entire
1117 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1118 * length, forbids leading or trailing whitespace, multiple internal spaces,
1119 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001120 *
1121 * The new_key is allocated to hold the corrected keyword and must be freed
1122 * by the calling routine. This avoids problems with trying to write to
1123 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001124 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001125png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001126png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001127{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001128 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001129 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001130 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001131 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001132
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133 png_debug(1, "in png_check_keyword\n");
1134 *new_key = NULL;
1135
1136 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001137 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001138 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001139 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001140 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001141
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1143
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001144 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001145
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001146 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001148 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001149 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001151#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001153
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001154 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001155 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001156#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001157 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001158#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 *dp = ' ';
1160 }
1161 else
1162 {
1163 *dp = *kp;
1164 }
1165 }
1166 *dp = '\0';
1167
1168 /* Remove any trailing white space. */
1169 kp = *new_key + key_len - 1;
1170 if (*kp == ' ')
1171 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001172 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173
1174 while (*kp == ' ')
1175 {
1176 *(kp--) = '\0';
1177 key_len--;
1178 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001179 }
1180
1181 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001182 kp = *new_key;
1183 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001184 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001185 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001186
1187 while (*kp == ' ')
1188 {
1189 kp++;
1190 key_len--;
1191 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001192 }
1193
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001195
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 /* Remove multiple internal spaces. */
1197 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001200 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001201 *(dp++) = *kp;
1202 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001203 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001205 {
1206 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001207 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001208 }
1209 else
1210 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 *(dp++) = *kp;
1212 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001213 }
1214 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001215 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001216 if(kwarn)
1217 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001218
1219 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001220 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001221 png_free(png_ptr, *new_key);
1222 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001223 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 }
1225
1226 if (key_len > 79)
1227 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001228 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 new_key[79] = '\0';
1230 key_len = 79;
1231 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001232
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001233 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001234}
1235#endif
1236
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001237#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001238/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001239void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001240png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001242{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001243#ifdef PNG_USE_LOCAL_ARRAYS
1244 PNG_tEXt;
1245#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001247 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001248
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 png_debug(1, "in png_write_tEXt\n");
1250 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1251 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001252 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001253 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001254 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001255
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001258 else
1259 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001260
1261 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001262 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001263 /*
1264 * We leave it to the application to meet PNG-1.0 requirements on the
1265 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1266 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001267 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001268 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001269 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001270 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001271 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001272
Guy Schalnat0d580581995-07-20 02:43:20 -05001273 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001274 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001275}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001276#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001277
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001278#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001279/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001280void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001281png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001282 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001283{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001284#ifdef PNG_USE_LOCAL_ARRAYS
1285 PNG_zTXt;
1286#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001287 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001288 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001289 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001290 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001291
Andreas Dilger47a0c421997-05-16 02:46:07 -05001292 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001293
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001295 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001296 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001298 }
1299
Andreas Dilger47a0c421997-05-16 02:46:07 -05001300 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1301 {
1302 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1303 png_free(png_ptr, new_key);
1304 return;
1305 }
1306
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001307 text_len = png_strlen(text);
1308
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 png_free(png_ptr, new_key);
1310
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001311 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001312 text_len = png_text_compress(png_ptr, text, text_len, compression,
1313 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001314
1315 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001316 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1317 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001318 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001319 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001320 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001321 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001322 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001323 /* write the compressed data */
1324 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001325
Guy Schalnat0d580581995-07-20 02:43:20 -05001326 /* close the chunk */
1327 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001328}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001329#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001330
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001331#if defined(PNG_WRITE_iTXt_SUPPORTED)
1332/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001333void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001334png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001335 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001336{
1337#ifdef PNG_USE_LOCAL_ARRAYS
1338 PNG_iTXt;
1339#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001340 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001341 png_charp new_lang, new_key;
1342 png_byte cbuf[2];
1343 compression_state comp;
1344
1345 png_debug(1, "in png_write_iTXt\n");
1346
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001347 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1348 {
1349 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1350 return;
1351 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001352 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1353 &new_lang))==0)
1354 {
1355 png_warning(png_ptr, "Empty language field in iTXt chunk");
1356 return;
1357 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001358 lang_key_len = png_strlen(lang_key);
1359 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001360
1361 if (text == NULL || *text == '\0')
1362 text_len = 0;
1363
1364 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001365 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1366 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001367
1368 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001369 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001370
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001371 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1372 (png_uint_32)(
1373 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1374 + key_len
1375 + lang_len
1376 + lang_key_len
1377 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001378
1379 /*
1380 * We leave it to the application to meet PNG-1.0 requirements on the
1381 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1382 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1383 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1384 */
1385 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001386
1387 /* set the compression flag */
1388 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1389 compression == PNG_TEXT_COMPRESSION_NONE)
1390 cbuf[0] = 0;
1391 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1392 cbuf[0] = 1;
1393 /* set the compression method */
1394 cbuf[1] = 0;
1395 png_write_chunk_data(png_ptr, cbuf, 2);
1396
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001397 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001398 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001399 png_write_chunk_data(png_ptr, '\0', 1);
1400
1401 png_write_compressed_data_out(png_ptr, &comp);
1402
1403 png_write_chunk_end(png_ptr);
1404 png_free(png_ptr, new_key);
1405 png_free(png_ptr, new_lang);
1406}
1407#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001408
1409#if defined(PNG_WRITE_oFFs_SUPPORTED)
1410/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001411void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001412png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 int unit_type)
1414{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001415#ifdef PNG_USE_LOCAL_ARRAYS
1416 PNG_oFFs;
1417#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 png_byte buf[9];
1419
1420 png_debug(1, "in png_write_oFFs\n");
1421 if (unit_type >= PNG_OFFSET_LAST)
1422 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1423
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001424 png_save_int_32(buf, x_offset);
1425 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001426 buf[8] = (png_byte)unit_type;
1427
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001428 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001429}
1430#endif
1431
1432#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001433/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001434void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001435png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1436 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1437{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001438#ifdef PNG_USE_LOCAL_ARRAYS
1439 PNG_pCAL;
1440#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001441 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001442 png_uint_32p params_len;
1443 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001444 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001445 int i;
1446
1447 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1448 if (type >= PNG_EQUATION_LAST)
1449 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1450
1451 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001452 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001453 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001454 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001455 total_len = purpose_len + units_len + 10;
1456
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001457 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1458 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001459
1460 /* Find the length of each parameter, making sure we don't count the
1461 null terminator for the last parameter. */
1462 for (i = 0; i < nparams; i++)
1463 {
1464 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001465 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001466 total_len += (png_size_t)params_len[i];
1467 }
1468
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001469 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001470 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001471 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001472 png_save_int_32(buf, X0);
1473 png_save_int_32(buf + 4, X1);
1474 buf[8] = (png_byte)type;
1475 buf[9] = (png_byte)nparams;
1476 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1477 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1478
1479 png_free(png_ptr, new_purpose);
1480
1481 for (i = 0; i < nparams; i++)
1482 {
1483 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1484 (png_size_t)params_len[i]);
1485 }
1486
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001487 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001488 png_write_chunk_end(png_ptr);
1489}
1490#endif
1491
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001492#if defined(PNG_WRITE_sCAL_SUPPORTED)
1493/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001494#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001495void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001496png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001497{
1498#ifdef PNG_USE_LOCAL_ARRAYS
1499 PNG_sCAL;
1500#endif
1501 png_size_t total_len;
1502 char wbuf[32], hbuf[32];
1503
1504 png_debug(1, "in png_write_sCAL\n");
1505
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001506#if defined(_WIN32_WCE)
1507/* sprintf() function is not supported on WindowsCE */
1508 {
1509 wchar_t wc_buf[32];
1510 swprintf(wc_buf, TEXT("%12.12e"), width);
1511 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1512 swprintf(wc_buf, TEXT("%12.12e"), height);
1513 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1514 }
1515#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001516 sprintf(wbuf, "%12.12e", width);
1517 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001518#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001519 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001520
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001521 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001522 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001523 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001524 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1525 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001526
1527 png_write_chunk_end(png_ptr);
1528}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001529#else
1530#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001531void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001532png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001533 png_charp height)
1534{
1535#ifdef PNG_USE_LOCAL_ARRAYS
1536 PNG_sCAL;
1537#endif
1538 png_size_t total_len;
1539 char wbuf[32], hbuf[32];
1540
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001541 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001542
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001543 png_strcpy(wbuf,(const char *)width);
1544 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001545 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001546
1547 png_debug1(3, "sCAL total length = %d\n", total_len);
1548 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001549 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001550 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1551 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001552
1553 png_write_chunk_end(png_ptr);
1554}
1555#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001556#endif
1557#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001558
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001559#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001560/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001561void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001562png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001563 png_uint_32 y_pixels_per_unit,
1564 int unit_type)
1565{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001566#ifdef PNG_USE_LOCAL_ARRAYS
1567 PNG_pHYs;
1568#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001569 png_byte buf[9];
1570
Andreas Dilger47a0c421997-05-16 02:46:07 -05001571 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001572 if (unit_type >= PNG_RESOLUTION_LAST)
1573 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1574
Guy Schalnat0d580581995-07-20 02:43:20 -05001575 png_save_uint_32(buf, x_pixels_per_unit);
1576 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001577 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001578
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001579 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001580}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001581#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001582
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001583#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001584/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1585 * or png_convert_from_time_t(), or fill in the structure yourself.
1586 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001587void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001588png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001589{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001590#ifdef PNG_USE_LOCAL_ARRAYS
1591 PNG_tIME;
1592#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001593 png_byte buf[7];
1594
Andreas Dilger47a0c421997-05-16 02:46:07 -05001595 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001596 if (mod_time->month > 12 || mod_time->month < 1 ||
1597 mod_time->day > 31 || mod_time->day < 1 ||
1598 mod_time->hour > 23 || mod_time->second > 60)
1599 {
1600 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1601 return;
1602 }
1603
Guy Schalnat0d580581995-07-20 02:43:20 -05001604 png_save_uint_16(buf, mod_time->year);
1605 buf[2] = mod_time->month;
1606 buf[3] = mod_time->day;
1607 buf[4] = mod_time->hour;
1608 buf[5] = mod_time->minute;
1609 buf[6] = mod_time->second;
1610
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001611 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001612}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001613#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001614
1615/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001616void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001617png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001618{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001619#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001620 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001621
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001622 /* start of interlace block */
1623 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001624
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001625 /* offset to next interlace block */
1626 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001628 /* start of interlace block in the y direction */
1629 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001630
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001631 /* offset to next interlace block in the y direction */
1632 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001633#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001634
Andreas Dilger47a0c421997-05-16 02:46:07 -05001635 png_size_t buf_size;
1636
1637 png_debug(1, "in png_write_start_row\n");
1638 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1639 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1640
Guy Schalnat0d580581995-07-20 02:43:20 -05001641 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001642 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001643 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001644
1645 /* set up filtering buffer, if using this filter */
1646 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001647 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001648 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001649 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001650 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001651 }
1652
Andreas Dilger47a0c421997-05-16 02:46:07 -05001653 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001654 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1655 {
1656 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001657 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001658 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001659
1660 if (png_ptr->do_filter & PNG_FILTER_UP)
1661 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001662 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001663 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001664 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001665 }
1666
1667 if (png_ptr->do_filter & PNG_FILTER_AVG)
1668 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001669 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1670 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001671 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001672 }
1673
1674 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1675 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001676 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001677 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001678 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001679 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001680 }
1681
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001682#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001683 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001684 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001685 {
1686 if (!(png_ptr->transformations & PNG_INTERLACE))
1687 {
1688 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1689 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001690 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1691 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001692 }
1693 else
1694 {
1695 png_ptr->num_rows = png_ptr->height;
1696 png_ptr->usr_width = png_ptr->width;
1697 }
1698 }
1699 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001700#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001701 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001702 png_ptr->num_rows = png_ptr->height;
1703 png_ptr->usr_width = png_ptr->width;
1704 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001705 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1706 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001707}
1708
Andreas Dilger47a0c421997-05-16 02:46:07 -05001709/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001710void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001711png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001712{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001713#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001714 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001715
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001716 /* start of interlace block */
1717 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001718
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001719 /* offset to next interlace block */
1720 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001721
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001722 /* start of interlace block in the y direction */
1723 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001724
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001725 /* offset to next interlace block in the y direction */
1726 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001727#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001728
Guy Schalnat0d580581995-07-20 02:43:20 -05001729 int ret;
1730
Andreas Dilger47a0c421997-05-16 02:46:07 -05001731 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001732 /* next row */
1733 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001734
Guy Schalnat0d580581995-07-20 02:43:20 -05001735 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001736 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001737 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001738
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001739#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001740 /* if interlaced, go to next pass */
1741 if (png_ptr->interlaced)
1742 {
1743 png_ptr->row_number = 0;
1744 if (png_ptr->transformations & PNG_INTERLACE)
1745 {
1746 png_ptr->pass++;
1747 }
1748 else
1749 {
1750 /* loop until we find a non-zero width or height pass */
1751 do
1752 {
1753 png_ptr->pass++;
1754 if (png_ptr->pass >= 7)
1755 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001756 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001757 png_pass_inc[png_ptr->pass] - 1 -
1758 png_pass_start[png_ptr->pass]) /
1759 png_pass_inc[png_ptr->pass];
1760 png_ptr->num_rows = (png_ptr->height +
1761 png_pass_yinc[png_ptr->pass] - 1 -
1762 png_pass_ystart[png_ptr->pass]) /
1763 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001764 if (png_ptr->transformations & PNG_INTERLACE)
1765 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001766 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1767
1768 }
1769
Guy Schalnate5a37791996-06-05 15:50:50 -05001770 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001771 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001772 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001773 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001774 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001775 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001776 (png_uint_32)png_ptr->usr_bit_depth *
1777 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001778 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001779 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001780 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001781#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001782
1783 /* if we get here, we've just written the last row, so we need
1784 to flush the compressor */
1785 do
1786 {
1787 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001788 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001789 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001790 if (ret == Z_OK)
1791 {
1792 /* check to see if we need more room */
1793 if (!(png_ptr->zstream.avail_out))
1794 {
1795 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1796 png_ptr->zstream.next_out = png_ptr->zbuf;
1797 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1798 }
1799 }
1800 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001801 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001802 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001803 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001804 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001805 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001806 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001807 } while (ret != Z_STREAM_END);
1808
1809 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001810 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001811 {
1812 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001813 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001814 }
1815
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001816 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001817}
1818
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001819#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001820/* Pick out the correct pixels for the interlace pass.
1821 * The basic idea here is to go through the row with a source
1822 * pointer and a destination pointer (sp and dp), and copy the
1823 * correct pixels for the pass. As the row gets compacted,
1824 * sp will always be >= dp, so we should never overwrite anything.
1825 * See the default: case for the easiest code to understand.
1826 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001827void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001828png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001829{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001830#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001831 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001832
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001833 /* start of interlace block */
1834 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001835
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001836 /* offset to next interlace block */
1837 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001838#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001839
Andreas Dilger47a0c421997-05-16 02:46:07 -05001840 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001841 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001842#if defined(PNG_USELESS_TESTS_SUPPORTED)
1843 if (row != NULL && row_info != NULL && pass < 6)
1844#else
1845 if (pass < 6)
1846#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001847 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001848 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001849 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001850 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001851 case 1:
1852 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001853 png_bytep sp;
1854 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001855 int shift;
1856 int d;
1857 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001858 png_uint_32 i;
1859 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001860
1861 dp = row;
1862 d = 0;
1863 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001864 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001865 i += png_pass_inc[pass])
1866 {
1867 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001868 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001869 d |= (value << shift);
1870
1871 if (shift == 0)
1872 {
1873 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001874 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001875 d = 0;
1876 }
1877 else
1878 shift--;
1879
1880 }
1881 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001882 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001883 break;
1884 }
1885 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001886 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001887 png_bytep sp;
1888 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001889 int shift;
1890 int d;
1891 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001892 png_uint_32 i;
1893 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001894
1895 dp = row;
1896 shift = 6;
1897 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001898 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001899 i += png_pass_inc[pass])
1900 {
1901 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001902 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001903 d |= (value << shift);
1904
1905 if (shift == 0)
1906 {
1907 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001908 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001909 d = 0;
1910 }
1911 else
1912 shift -= 2;
1913 }
1914 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001915 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001916 break;
1917 }
1918 case 4:
1919 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001920 png_bytep sp;
1921 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001922 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001923 int d;
1924 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001925 png_uint_32 i;
1926 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001927
1928 dp = row;
1929 shift = 4;
1930 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001931 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 i += png_pass_inc[pass])
1933 {
1934 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001935 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 d |= (value << shift);
1937
1938 if (shift == 0)
1939 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001940 shift = 4;
1941 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001942 d = 0;
1943 }
1944 else
1945 shift -= 4;
1946 }
1947 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001948 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001949 break;
1950 }
1951 default:
1952 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001953 png_bytep sp;
1954 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001955 png_uint_32 i;
1956 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001957 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001958
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001959 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001960 dp = row;
1961 /* find out how many bytes each pixel takes up */
1962 pixel_bytes = (row_info->pixel_depth >> 3);
1963 /* loop through the row, only looking at the pixels that
1964 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001965 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001966 i += png_pass_inc[pass])
1967 {
1968 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001969 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 /* move the pixel */
1971 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001972 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001973 /* next pixel */
1974 dp += pixel_bytes;
1975 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001976 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001977 }
1978 }
1979 /* set new row width */
1980 row_info->width = (row_info->width +
1981 png_pass_inc[pass] - 1 -
1982 png_pass_start[pass]) /
1983 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001984 row_info->rowbytes = ((row_info->width *
1985 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001986 }
1987}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001988#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001989
Andreas Dilger47a0c421997-05-16 02:46:07 -05001990/* This filters the row, chooses which filter to use, if it has not already
1991 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001992 * chosen filter.
1993 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001994#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001995#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001996#define PNG_LOMASK ((png_uint_32)0xffffL)
1997#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001998void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001999png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002000{
Guy Schalnate5a37791996-06-05 15:50:50 -05002001 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002002 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002003 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002004 png_uint_32 row_bytes = row_info->rowbytes;
2005#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2006 int num_p_filters = (int)png_ptr->num_prev_filters;
2007#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002008
Andreas Dilger47a0c421997-05-16 02:46:07 -05002009 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002010 /* find out how many bytes offset each pixel is */
2011 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05002012
2013 prev_row = png_ptr->prev_row;
2014 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002015 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002016
Andreas Dilger47a0c421997-05-16 02:46:07 -05002017 /* The prediction method we use is to find which method provides the
2018 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002019 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002020 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002021 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002022 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002023 * predictive" method (not implemented yet), which does test compressions
2024 * of lines using different filter methods, and then chooses the
2025 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002026 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002027 *
2028 * GRR 980525: consider also
2029 * (1) minimum sum of absolute differences from running average (i.e.,
2030 * keep running sum of non-absolute differences & count of bytes)
2031 * [track dispersion, too? restart average if dispersion too large?]
2032 * (1b) minimum sum of absolute differences from sliding average, probably
2033 * with window size <= deflate window (usually 32K)
2034 * (2) minimum sum of squared differences from zero or running average
2035 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002036 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002037
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002038
Guy Schalnate5a37791996-06-05 15:50:50 -05002039 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002040 * that has been chosen, as it doesn't actually do anything to the data.
2041 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002042 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002043 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002044 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002045 png_bytep rp;
2046 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002047 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002048 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002049
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002050 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002051 {
2052 v = *rp;
2053 sum += (v < 128) ? v : 256 - v;
2054 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002055
2056#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2057 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2058 {
2059 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002060 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002061 sumlo = sum & PNG_LOMASK;
2062 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2063
2064 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002065 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002066 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002067 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002068 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002069 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002070 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002071 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002072 PNG_WEIGHT_SHIFT;
2073 }
2074 }
2075
2076 /* Factor in the cost of this filter (this is here for completeness,
2077 * but it makes no sense to have a "cost" for the NONE filter, as
2078 * it has the minimum possible computational cost - none).
2079 */
2080 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2081 PNG_COST_SHIFT;
2082 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2083 PNG_COST_SHIFT;
2084
2085 if (sumhi > PNG_HIMASK)
2086 sum = PNG_MAXSUM;
2087 else
2088 sum = (sumhi << PNG_HISHIFT) + sumlo;
2089 }
2090#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002091 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002092 }
2093
Guy Schalnate5a37791996-06-05 15:50:50 -05002094 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002095 if (filter_to_do == PNG_FILTER_SUB)
2096 /* it's the only filter so no testing is needed */
2097 {
2098 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002099 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002100 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2101 i++, rp++, dp++)
2102 {
2103 *dp = *rp;
2104 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002105 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002106 i++, rp++, lp++, dp++)
2107 {
2108 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2109 }
2110 best_row = png_ptr->sub_row;
2111 }
2112
2113 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002114 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002115 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002117 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002118 int v;
2119
2120#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002121 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002122 * would reduce the sum of this filter, so that we can do the
2123 * early exit comparison without scaling the sum each time.
2124 */
2125 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2126 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002127 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002128 png_uint_32 lmhi, lmlo;
2129 lmlo = lmins & PNG_LOMASK;
2130 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2131
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002132 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002133 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002134 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002136 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002138 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139 PNG_WEIGHT_SHIFT;
2140 }
2141 }
2142
2143 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2144 PNG_COST_SHIFT;
2145 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2146 PNG_COST_SHIFT;
2147
2148 if (lmhi > PNG_HIMASK)
2149 lmins = PNG_MAXSUM;
2150 else
2151 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2152 }
2153#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002154
Guy Schalnate5a37791996-06-05 15:50:50 -05002155 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2156 i++, rp++, dp++)
2157 {
2158 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002159
Guy Schalnate5a37791996-06-05 15:50:50 -05002160 sum += (v < 128) ? v : 256 - v;
2161 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002162 for (lp = row_buf + 1; i < row_info->rowbytes;
2163 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002164 {
2165 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002166
Guy Schalnate5a37791996-06-05 15:50:50 -05002167 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168
2169 if (sum > lmins) /* We are already worse, don't continue. */
2170 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002171 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002172
2173#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2174 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2175 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002176 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002177 png_uint_32 sumhi, sumlo;
2178 sumlo = sum & PNG_LOMASK;
2179 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2180
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002181 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002183 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002185 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002187 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002188 PNG_WEIGHT_SHIFT;
2189 }
2190 }
2191
2192 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2193 PNG_COST_SHIFT;
2194 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2195 PNG_COST_SHIFT;
2196
2197 if (sumhi > PNG_HIMASK)
2198 sum = PNG_MAXSUM;
2199 else
2200 sum = (sumhi << PNG_HISHIFT) + sumlo;
2201 }
2202#endif
2203
Guy Schalnate5a37791996-06-05 15:50:50 -05002204 if (sum < mins)
2205 {
2206 mins = sum;
2207 best_row = png_ptr->sub_row;
2208 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002209 }
2210
Guy Schalnate5a37791996-06-05 15:50:50 -05002211 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002212 if (filter_to_do == PNG_FILTER_UP)
2213 {
2214 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002215 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002216
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002217 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002218 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002219 i++, rp++, pp++, dp++)
2220 {
2221 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2222 }
2223 best_row = png_ptr->up_row;
2224 }
2225
2226 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002227 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002228 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002229 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002230 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002231 int v;
2232
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002233
Andreas Dilger47a0c421997-05-16 02:46:07 -05002234#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2235 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2236 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002237 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002238 png_uint_32 lmhi, lmlo;
2239 lmlo = lmins & PNG_LOMASK;
2240 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2241
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002242 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002243 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002244 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002246 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002247 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002248 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002249 PNG_WEIGHT_SHIFT;
2250 }
2251 }
2252
2253 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2254 PNG_COST_SHIFT;
2255 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2256 PNG_COST_SHIFT;
2257
2258 if (lmhi > PNG_HIMASK)
2259 lmins = PNG_MAXSUM;
2260 else
2261 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2262 }
2263#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002264
2265 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002266 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002267 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002268 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002269
2270 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002271
2272 if (sum > lmins) /* We are already worse, don't continue. */
2273 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002274 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002275
2276#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2277 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2278 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002279 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002280 png_uint_32 sumhi, sumlo;
2281 sumlo = sum & PNG_LOMASK;
2282 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2283
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002284 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002285 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002286 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002287 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002288 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002289 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002290 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002291 PNG_WEIGHT_SHIFT;
2292 }
2293 }
2294
2295 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2296 PNG_COST_SHIFT;
2297 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2298 PNG_COST_SHIFT;
2299
2300 if (sumhi > PNG_HIMASK)
2301 sum = PNG_MAXSUM;
2302 else
2303 sum = (sumhi << PNG_HISHIFT) + sumlo;
2304 }
2305#endif
2306
Guy Schalnate5a37791996-06-05 15:50:50 -05002307 if (sum < mins)
2308 {
2309 mins = sum;
2310 best_row = png_ptr->up_row;
2311 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002312 }
2313
Guy Schalnate5a37791996-06-05 15:50:50 -05002314 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002315 if (filter_to_do == PNG_FILTER_AVG)
2316 {
2317 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002318 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002319 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002320 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002321 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002322 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002323 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002324 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002325 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002326 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2327 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002328 }
2329 best_row = png_ptr->avg_row;
2330 }
2331
2332 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002333 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002334 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002335 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002336 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002337 int v;
2338
2339#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2340 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2341 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002342 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002343 png_uint_32 lmhi, lmlo;
2344 lmlo = lmins & PNG_LOMASK;
2345 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2346
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002349 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002350 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002351 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354 PNG_WEIGHT_SHIFT;
2355 }
2356 }
2357
2358 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2359 PNG_COST_SHIFT;
2360 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2361 PNG_COST_SHIFT;
2362
2363 if (lmhi > PNG_HIMASK)
2364 lmins = PNG_MAXSUM;
2365 else
2366 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2367 }
2368#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002369
2370 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002372 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002373 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002374
2375 sum += (v < 128) ? v : 256 - v;
2376 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002377 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002378 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002379 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002380 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002381
2382 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002383
2384 if (sum > lmins) /* We are already worse, don't continue. */
2385 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002386 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002387
2388#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2389 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2390 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002391 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002392 png_uint_32 sumhi, sumlo;
2393 sumlo = sum & PNG_LOMASK;
2394 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2395
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002396 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002397 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002398 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002399 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002400 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002401 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002402 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 PNG_WEIGHT_SHIFT;
2404 }
2405 }
2406
2407 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2408 PNG_COST_SHIFT;
2409 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2410 PNG_COST_SHIFT;
2411
2412 if (sumhi > PNG_HIMASK)
2413 sum = PNG_MAXSUM;
2414 else
2415 sum = (sumhi << PNG_HISHIFT) + sumlo;
2416 }
2417#endif
2418
Guy Schalnate5a37791996-06-05 15:50:50 -05002419 if (sum < mins)
2420 {
2421 mins = sum;
2422 best_row = png_ptr->avg_row;
2423 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002424 }
2425
Andreas Dilger47a0c421997-05-16 02:46:07 -05002426 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002427 if (filter_to_do == PNG_FILTER_PAETH)
2428 {
2429 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002430 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002431 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002432 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002433 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002434 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002435 }
2436
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002437 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002438 {
2439 int a, b, c, pa, pb, pc, p;
2440
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002441 b = *pp++;
2442 c = *cp++;
2443 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002444
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002445 p = b - c;
2446 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002447
2448#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 pa = abs(p);
2450 pb = abs(pc);
2451 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002452#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002453 pa = p < 0 ? -p : p;
2454 pb = pc < 0 ? -pc : pc;
2455 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002456#endif
2457
2458 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2459
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002460 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002461 }
2462 best_row = png_ptr->paeth_row;
2463 }
2464
2465 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002466 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002467 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002469 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002470 int v;
2471
2472#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2473 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2474 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002475 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002476 png_uint_32 lmhi, lmlo;
2477 lmlo = lmins & PNG_LOMASK;
2478 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2479
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002480 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002481 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002482 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002483 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002484 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002485 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002486 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002487 PNG_WEIGHT_SHIFT;
2488 }
2489 }
2490
2491 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2492 PNG_COST_SHIFT;
2493 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2494 PNG_COST_SHIFT;
2495
2496 if (lmhi > PNG_HIMASK)
2497 lmins = PNG_MAXSUM;
2498 else
2499 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2500 }
2501#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002502
2503 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002504 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002505 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002507
2508 sum += (v < 128) ? v : 256 - v;
2509 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002510
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002511 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002512 {
2513 int a, b, c, pa, pb, pc, p;
2514
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002515 b = *pp++;
2516 c = *cp++;
2517 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002518
2519#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002520 p = b - c;
2521 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002522#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002523 pa = abs(p);
2524 pb = abs(pc);
2525 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002526#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002527 pa = p < 0 ? -p : p;
2528 pb = pc < 0 ? -pc : pc;
2529 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002530#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002531 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2532#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002533 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002534 pa = abs(p - a);
2535 pb = abs(p - b);
2536 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002537 if (pa <= pb && pa <= pc)
2538 p = a;
2539 else if (pb <= pc)
2540 p = b;
2541 else
2542 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002543#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002544
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002545 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002546
2547 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002548
2549 if (sum > lmins) /* We are already worse, don't continue. */
2550 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002551 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002552
2553#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2554 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2555 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002556 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002557 png_uint_32 sumhi, sumlo;
2558 sumlo = sum & PNG_LOMASK;
2559 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2560
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002561 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002562 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002564 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002565 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002566 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002567 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002568 PNG_WEIGHT_SHIFT;
2569 }
2570 }
2571
2572 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2573 PNG_COST_SHIFT;
2574 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2575 PNG_COST_SHIFT;
2576
2577 if (sumhi > PNG_HIMASK)
2578 sum = PNG_MAXSUM;
2579 else
2580 sum = (sumhi << PNG_HISHIFT) + sumlo;
2581 }
2582#endif
2583
Guy Schalnate5a37791996-06-05 15:50:50 -05002584 if (sum < mins)
2585 {
2586 best_row = png_ptr->paeth_row;
2587 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002588 }
2589
Andreas Dilger47a0c421997-05-16 02:46:07 -05002590 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002591
Guy Schalnate5a37791996-06-05 15:50:50 -05002592 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002593
2594#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2595 /* Save the type of filter we picked this time for future calculations */
2596 if (png_ptr->num_prev_filters > 0)
2597 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002598 int j;
2599 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002600 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002601 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002602 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002603 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002604 }
2605#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002606}
2607
2608
Andreas Dilger47a0c421997-05-16 02:46:07 -05002609/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002610void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002611png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2612{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002613 png_debug(1, "in png_write_filtered_row\n");
2614 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002615 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002616
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002617 png_ptr->zstream.next_in = filtered_row;
2618 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002619 /* repeat until we have compressed all the data */
2620 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002621 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002622 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002623
Guy Schalnate5a37791996-06-05 15:50:50 -05002624 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002625 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002626 /* check for compression errors */
2627 if (ret != Z_OK)
2628 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002629 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002630 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002631 else
2632 png_error(png_ptr, "zlib error");
2633 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002634
Guy Schalnate5a37791996-06-05 15:50:50 -05002635 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002636 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002637 {
2638 /* write the IDAT and reset the zlib output buffer */
2639 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002640 png_ptr->zstream.next_out = png_ptr->zbuf;
2641 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002642 }
2643 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002644 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002645
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002646 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002647 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002648 {
2649 png_bytep tptr;
2650
2651 tptr = png_ptr->prev_row;
2652 png_ptr->prev_row = png_ptr->row_buf;
2653 png_ptr->row_buf = tptr;
2654 }
2655
Guy Schalnate5a37791996-06-05 15:50:50 -05002656 /* finish row - updates counters and flushes zlib if last row */
2657 png_write_finish_row(png_ptr);
2658
2659#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2660 png_ptr->flush_rows++;
2661
2662 if (png_ptr->flush_dist > 0 &&
2663 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002664 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002665 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002666 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002667#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002668}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002669#endif /* PNG_WRITE_SUPPORTED */