blob: c2f371dfcef3e88d24199d35e5a6ac8d988e325e [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-Pehrson67864af2004-08-28 23:30:07 -05004 * libpng version 1.2.7beta2 - August 29, 2004
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05006 * Copyright (c) 1998-2004 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,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500237 (png_uint_32)(comp->max_output_ptr *
238 png_sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500239 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500240 * png_sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600241 png_free(png_ptr, old_ptr);
242 }
243 else
244 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500245 (png_uint_32)(comp->max_output_ptr *
246 png_sizeof (png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247 }
248
249 /* save the data */
250 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
251 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500252 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
253 png_ptr->zbuf_size);
254 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600255
256 /* and reset the buffer */
257 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
258 png_ptr->zstream.next_out = png_ptr->zbuf;
259 }
260 /* continue until we don't have any more to compress */
261 } while (png_ptr->zstream.avail_in);
262
263 /* finish the compression */
264 do
265 {
266 /* tell zlib we are finished */
267 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500268
269 if (ret == Z_OK)
270 {
271 /* check to see if we need more room */
272 if (!(png_ptr->zstream.avail_out))
273 {
274 /* check to make sure our output array has room */
275 if (comp->num_output_ptr >= comp->max_output_ptr)
276 {
277 int old_max;
278
279 old_max = comp->max_output_ptr;
280 comp->max_output_ptr = comp->num_output_ptr + 4;
281 if (comp->output_ptr != NULL)
282 {
283 png_charpp old_ptr;
284
285 old_ptr = comp->output_ptr;
286 /* This could be optimized to realloc() */
287 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500288 (png_uint_32)(comp->max_output_ptr *
289 png_sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500290 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500291 old_max * png_sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500292 png_free(png_ptr, old_ptr);
293 }
294 else
295 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500296 (png_uint_32)(comp->max_output_ptr *
297 png_sizeof (png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500298 }
299
300 /* save off the data */
301 comp->output_ptr[comp->num_output_ptr] =
302 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500303 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
304 png_ptr->zbuf_size);
305 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500306
307 /* and reset the buffer pointers */
308 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
309 png_ptr->zstream.next_out = png_ptr->zbuf;
310 }
311 }
312 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600313 {
314 /* we got an error */
315 if (png_ptr->zstream.msg != NULL)
316 png_error(png_ptr, png_ptr->zstream.msg);
317 else
318 png_error(png_ptr, "zlib error");
319 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600320 } while (ret != Z_STREAM_END);
321
322 /* text length is number of buffers plus last buffer */
323 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
324 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
325 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
326
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500327 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600328}
329
330/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500331static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600332png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
333{
334 int i;
335
336 /* handle the no-compression case */
337 if (comp->input)
338 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500339 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
340 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600341 return;
342 }
343
344 /* write saved output buffers, if any */
345 for (i = 0; i < comp->num_output_ptr; i++)
346 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600347 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
348 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600349 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500350 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 }
352 if (comp->max_output_ptr != 0)
353 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500354 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600355 /* write anything left in zbuf */
356 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
357 png_write_chunk_data(png_ptr, png_ptr->zbuf,
358 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
359
360 /* reset zlib for another zTXt/iTXt or the image data */
361 deflateReset(&png_ptr->zstream);
362
363}
364#endif
365
Guy Schalnat0d580581995-07-20 02:43:20 -0500366/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600367 * information. Note that the rest of this code depends upon this
368 * information being correct.
369 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500370void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600371png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 int bit_depth, int color_type, int compression_type, int filter_type,
373 int interlace_type)
374{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600375#ifdef PNG_USE_LOCAL_ARRAYS
376 PNG_IHDR;
377#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500378 png_byte buf[13]; /* buffer to store the IHDR info */
379
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500381 /* Check that we have valid input data from the application info */
382 switch (color_type)
383 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500385 switch (bit_depth)
386 {
387 case 1:
388 case 2:
389 case 4:
390 case 8:
391 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500393 }
394 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500396 if (bit_depth != 8 && bit_depth != 16)
397 png_error(png_ptr, "Invalid bit depth for RGB image");
398 png_ptr->channels = 3;
399 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500401 switch (bit_depth)
402 {
403 case 1:
404 case 2:
405 case 4:
406 case 8: png_ptr->channels = 1; break;
407 default: png_error(png_ptr, "Invalid bit depth for paletted image");
408 }
409 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500411 if (bit_depth != 8 && bit_depth != 16)
412 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
413 png_ptr->channels = 2;
414 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500416 if (bit_depth != 8 && bit_depth != 16)
417 png_error(png_ptr, "Invalid bit depth for RGBA image");
418 png_ptr->channels = 4;
419 break;
420 default:
421 png_error(png_ptr, "Invalid image color type specified");
422 }
423
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500425 {
426 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500428 }
429
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600430 /* Write filter_method 64 (intrapixel differencing) only if
431 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
432 * 2. Libpng did not write a PNG signature (this filter_method is only
433 * used in PNG datastreams that are embedded in MNG datastreams) and
434 * 3. The application called png_permit_mng_features with a mask that
435 * included PNG_FLAG_MNG_FILTER_64 and
436 * 4. The filter_method is 64 and
437 * 5. The color_type is RGB or RGBA
438 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600439 if (
440#if defined(PNG_MNG_FEATURES_SUPPORTED)
441 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600442 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500443 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600444 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600445 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
446#endif
447 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500448 {
449 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500450 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500451 }
452
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600453#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500454 if (interlace_type != PNG_INTERLACE_NONE &&
455 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500456 {
457 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500459 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600460#else
461 interlace_type=PNG_INTERLACE_NONE;
462#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500463
464 /* save off the relevent information */
465 png_ptr->bit_depth = (png_byte)bit_depth;
466 png_ptr->color_type = (png_byte)color_type;
467 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500468#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600469 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500470#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500471 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500472 png_ptr->width = width;
473 png_ptr->height = height;
474
475 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500476 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500477 /* set the usr info, so any transformations can modify it */
478 png_ptr->usr_width = png_ptr->width;
479 png_ptr->usr_bit_depth = png_ptr->bit_depth;
480 png_ptr->usr_channels = png_ptr->channels;
481
Guy Schalnat0d580581995-07-20 02:43:20 -0500482 /* pack the header information into the buffer */
483 png_save_uint_32(buf, width);
484 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600485 buf[8] = (png_byte)bit_depth;
486 buf[9] = (png_byte)color_type;
487 buf[10] = (png_byte)compression_type;
488 buf[11] = (png_byte)filter_type;
489 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500490
491 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600492 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500493
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600495 png_ptr->zstream.zalloc = png_zalloc;
496 png_ptr->zstream.zfree = png_zfree;
497 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
501 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500503 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500505 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500507 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500509 png_ptr->zlib_strategy = Z_FILTERED;
510 else
511 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
512 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500514 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500516 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500517 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrsona4981d42004-08-25 22:00:45 -0500518 {
519 if (png_ptr->rowbytes <= 16384 && png_ptr->height <= 16384)
520 {
521 png_uint_32 imagebytes = (png_ptr->rowbytes+1) * png_ptr->height;
522 png_uint_32 windowsize = 14; /* try for a smaller window */
523 while ((1U << windowsize) >= imagebytes && windowsize > 7)
524 --windowsize;
525 png_ptr->zlib_window_bits = windowsize + 1;
526 }
527 else
528 png_ptr->zlib_window_bits = 15;
529 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500530 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500531 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600532 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533 png_ptr->zlib_method, png_ptr->zlib_window_bits,
534 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600535 png_ptr->zstream.next_out = png_ptr->zbuf;
536 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500537
Guy Schalnate5a37791996-06-05 15:50:50 -0500538 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500539}
540
541/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500542 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600543 * structure.
544 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500545void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500547{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600548#ifdef PNG_USE_LOCAL_ARRAYS
549 PNG_PLTE;
550#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500551 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600552 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 png_byte buf[3];
554
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500556 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500557#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600558 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500559#endif
560 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500561 {
562 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500563 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500564 png_error(png_ptr, "Invalid number of colors in palette");
565 }
566 else
567 {
568 png_warning(png_ptr, "Invalid number of colors in palette");
569 return;
570 }
571 }
572
573 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
574 {
575 png_warning(png_ptr,
576 "Ignoring request to write a PLTE chunk in grayscale PNG");
577 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500578 }
579
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 png_ptr->num_palette = (png_uint_16)num_pal;
581 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500582
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600583 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500584#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500586 {
587 buf[0] = pal_ptr->red;
588 buf[1] = pal_ptr->green;
589 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500590 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500591 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500592#else
593 /* This is a little slower but some buggy compilers need to do this instead */
594 pal_ptr=palette;
595 for (i = 0; i < num_pal; i++)
596 {
597 buf[0] = pal_ptr[i].red;
598 buf[1] = pal_ptr[i].green;
599 buf[2] = pal_ptr[i].blue;
600 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
601 }
602#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500603 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500604 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500605}
606
607/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500608void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500609png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500610{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600611#ifdef PNG_USE_LOCAL_ARRAYS
612 PNG_IDAT;
613#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500614 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500615
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600616 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500617 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500618}
619
620/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500621void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600622png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500623{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600624#ifdef PNG_USE_LOCAL_ARRAYS
625 PNG_IEND;
626#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500627 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500628 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500629 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600630 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500631}
632
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500633#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500634/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600635#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500636void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500637png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500638{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600639#ifdef PNG_USE_LOCAL_ARRAYS
640 PNG_gAMA;
641#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500642 png_uint_32 igamma;
643 png_byte buf[4];
644
Andreas Dilger47a0c421997-05-16 02:46:07 -0500645 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600646 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600647 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500648 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600649 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500650}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500651#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600652#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500653void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600654png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600655{
656#ifdef PNG_USE_LOCAL_ARRAYS
657 PNG_gAMA;
658#endif
659 png_byte buf[4];
660
661 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600662 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500663 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600664 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
665}
666#endif
667#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500668
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600669#if defined(PNG_WRITE_sRGB_SUPPORTED)
670/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500671void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600672png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600673{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600674#ifdef PNG_USE_LOCAL_ARRAYS
675 PNG_sRGB;
676#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600677 png_byte buf[1];
678
679 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600680 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600681 png_warning(png_ptr,
682 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600683 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600684 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600685}
686#endif
687
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600688#if defined(PNG_WRITE_iCCP_SUPPORTED)
689/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500690void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600691png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
692 png_charp profile, int profile_len)
693{
694#ifdef PNG_USE_LOCAL_ARRAYS
695 PNG_iCCP;
696#endif
697 png_size_t name_len;
698 png_charp new_name;
699 compression_state comp;
700
701 png_debug(1, "in png_write_iCCP\n");
702 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
703 &new_name)) == 0)
704 {
705 png_warning(png_ptr, "Empty keyword in iCCP chunk");
706 return;
707 }
708
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600709 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600710 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600711
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500712 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600713 profile_len = 0;
714
715 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500716 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600717 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600718
719 /* make sure we include the NULL after the name and the compression type */
720 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
721 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600722 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600723 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
724
725 if (profile_len)
726 png_write_compressed_data_out(png_ptr, &comp);
727
728 png_write_chunk_end(png_ptr);
729 png_free(png_ptr, new_name);
730}
731#endif
732
733#if defined(PNG_WRITE_sPLT_SUPPORTED)
734/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500735void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600736png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600737{
738#ifdef PNG_USE_LOCAL_ARRAYS
739 PNG_sPLT;
740#endif
741 png_size_t name_len;
742 png_charp new_name;
743 png_byte entrybuf[10];
744 int entry_size = (spalette->depth == 8 ? 6 : 10);
745 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600746 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500747#ifdef PNG_NO_POINTER_INDEXING
748 int i;
749#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600750
751 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600752 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
753 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600754 {
755 png_warning(png_ptr, "Empty keyword in sPLT chunk");
756 return;
757 }
758
759 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500760 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600761 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600762 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600763 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600764
765 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500766#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600767 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
768 {
769 if (spalette->depth == 8)
770 {
771 entrybuf[0] = (png_byte)ep->red;
772 entrybuf[1] = (png_byte)ep->green;
773 entrybuf[2] = (png_byte)ep->blue;
774 entrybuf[3] = (png_byte)ep->alpha;
775 png_save_uint_16(entrybuf + 4, ep->frequency);
776 }
777 else
778 {
779 png_save_uint_16(entrybuf + 0, ep->red);
780 png_save_uint_16(entrybuf + 2, ep->green);
781 png_save_uint_16(entrybuf + 4, ep->blue);
782 png_save_uint_16(entrybuf + 6, ep->alpha);
783 png_save_uint_16(entrybuf + 8, ep->frequency);
784 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500785 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600786 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500787#else
788 ep=spalette->entries;
789 for (i=0; i>spalette->nentries; i++)
790 {
791 if (spalette->depth == 8)
792 {
793 entrybuf[0] = (png_byte)ep[i].red;
794 entrybuf[1] = (png_byte)ep[i].green;
795 entrybuf[2] = (png_byte)ep[i].blue;
796 entrybuf[3] = (png_byte)ep[i].alpha;
797 png_save_uint_16(entrybuf + 4, ep[i].frequency);
798 }
799 else
800 {
801 png_save_uint_16(entrybuf + 0, ep[i].red);
802 png_save_uint_16(entrybuf + 2, ep[i].green);
803 png_save_uint_16(entrybuf + 4, ep[i].blue);
804 png_save_uint_16(entrybuf + 6, ep[i].alpha);
805 png_save_uint_16(entrybuf + 8, ep[i].frequency);
806 }
807 png_write_chunk_data(png_ptr, entrybuf, entry_size);
808 }
809#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600810
811 png_write_chunk_end(png_ptr);
812 png_free(png_ptr, new_name);
813}
814#endif
815
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500816#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500817/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500818void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600819png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500820{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600821#ifdef PNG_USE_LOCAL_ARRAYS
822 PNG_sBIT;
823#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500824 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500825 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500826
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600828 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500829 if (color_type & PNG_COLOR_MASK_COLOR)
830 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600831 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500832
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500833 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
834 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600835 if (sbit->red == 0 || sbit->red > maxbits ||
836 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500837 sbit->blue == 0 || sbit->blue > maxbits)
838 {
839 png_warning(png_ptr, "Invalid sBIT depth specified");
840 return;
841 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500842 buf[0] = sbit->red;
843 buf[1] = sbit->green;
844 buf[2] = sbit->blue;
845 size = 3;
846 }
847 else
848 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500849 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
850 {
851 png_warning(png_ptr, "Invalid sBIT depth specified");
852 return;
853 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500854 buf[0] = sbit->gray;
855 size = 1;
856 }
857
858 if (color_type & PNG_COLOR_MASK_ALPHA)
859 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500860 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
861 {
862 png_warning(png_ptr, "Invalid sBIT depth specified");
863 return;
864 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500865 buf[size++] = sbit->alpha;
866 }
867
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600868 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500869}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500870#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500871
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500872#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500873/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600874#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500875void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600877 double red_x, double red_y, double green_x, double green_y,
878 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500879{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600880#ifdef PNG_USE_LOCAL_ARRAYS
881 PNG_cHRM;
882#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500883 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600884 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500885
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600887 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500888 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
889 white_x + white_y > 1.0)
890 {
891 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500892#if !defined(PNG_NO_CONSOLE_IO)
893 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600894#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500895 return;
896 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600897 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500898 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600899 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500900 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500901
902 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
903 red_x + red_y > 1.0)
904 {
905 png_warning(png_ptr, "Invalid cHRM red point specified");
906 return;
907 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600908 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500909 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600910 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500911 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500912
913 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
914 green_x + green_y > 1.0)
915 {
916 png_warning(png_ptr, "Invalid cHRM green point specified");
917 return;
918 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600919 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500920 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600921 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500922 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500923
924 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
925 blue_x + blue_y > 1.0)
926 {
927 png_warning(png_ptr, "Invalid cHRM blue point specified");
928 return;
929 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600930 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500931 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600932 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500933 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500934
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600935 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500936}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500937#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600938#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500939void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600940png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
941 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
942 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
943 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600944{
945#ifdef PNG_USE_LOCAL_ARRAYS
946 PNG_cHRM;
947#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600948 png_byte buf[32];
949
950 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600951 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600952 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
953 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600954 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500955#if !defined(PNG_NO_CONSOLE_IO)
956 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600957#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600958 return;
959 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500960 png_save_uint_32(buf, (png_uint_32)white_x);
961 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600962
963 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
964 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600965 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600966 return;
967 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500968 png_save_uint_32(buf + 8, (png_uint_32)red_x);
969 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600970
971 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
972 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600973 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600974 return;
975 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500976 png_save_uint_32(buf + 16, (png_uint_32)green_x);
977 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600978
979 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
980 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600981 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600982 return;
983 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500984 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
985 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600986
987 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
988}
989#endif
990#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500991
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500992#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500993/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500994void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600995png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500996 int num_trans, int color_type)
997{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600998#ifdef PNG_USE_LOCAL_ARRAYS
999 PNG_tRNS;
1000#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001001 png_byte buf[6];
1002
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001004 if (color_type == PNG_COLOR_TYPE_PALETTE)
1005 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001006 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001007 {
1008 png_warning(png_ptr,"Invalid number of transparent colors specified");
1009 return;
1010 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001011 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001012 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001013 }
1014 else if (color_type == PNG_COLOR_TYPE_GRAY)
1015 {
1016 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001017 if(tran->gray >= (1 << png_ptr->bit_depth))
1018 {
1019 png_warning(png_ptr,
1020 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1021 return;
1022 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001023 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001024 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001025 }
1026 else if (color_type == PNG_COLOR_TYPE_RGB)
1027 {
1028 /* three 16 bit values */
1029 png_save_uint_16(buf, tran->red);
1030 png_save_uint_16(buf + 2, tran->green);
1031 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001032 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1033 {
1034 png_warning(png_ptr,
1035 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1036 return;
1037 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001038 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001039 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001040 else
1041 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001042 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001043 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001044}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001045#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001046
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001047#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001048/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001049void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001050png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001051{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001052#ifdef PNG_USE_LOCAL_ARRAYS
1053 PNG_bKGD;
1054#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001055 png_byte buf[6];
1056
Andreas Dilger47a0c421997-05-16 02:46:07 -05001057 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001058 if (color_type == PNG_COLOR_TYPE_PALETTE)
1059 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001060 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001061#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001062 (png_ptr->num_palette ||
1063 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001064#endif
1065 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001066 {
1067 png_warning(png_ptr, "Invalid background palette index");
1068 return;
1069 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001070 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001071 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001072 }
1073 else if (color_type & PNG_COLOR_MASK_COLOR)
1074 {
1075 png_save_uint_16(buf, back->red);
1076 png_save_uint_16(buf + 2, back->green);
1077 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001078 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1079 {
1080 png_warning(png_ptr,
1081 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1082 return;
1083 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001084 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001085 }
1086 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001087 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001088 if(back->gray >= (1 << png_ptr->bit_depth))
1089 {
1090 png_warning(png_ptr,
1091 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1092 return;
1093 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001094 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001095 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001096 }
1097}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001098#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001099
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001100#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001101/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001102void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001103png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001104{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001105#ifdef PNG_USE_LOCAL_ARRAYS
1106 PNG_hIST;
1107#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001108 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001109 png_byte buf[3];
1110
Andreas Dilger47a0c421997-05-16 02:46:07 -05001111 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001112 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001113 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1115 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001116 png_warning(png_ptr, "Invalid number of histogram entries specified");
1117 return;
1118 }
1119
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001120 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001121 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001122 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001123 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001125 }
1126 png_write_chunk_end(png_ptr);
1127}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001128#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001129
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001130#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1131 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001132/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1133 * and if invalid, correct the keyword rather than discarding the entire
1134 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1135 * length, forbids leading or trailing whitespace, multiple internal spaces,
1136 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 *
1138 * The new_key is allocated to hold the corrected keyword and must be freed
1139 * by the calling routine. This avoids problems with trying to write to
1140 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001141 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001142png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001143png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001144{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001146 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001147 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001148 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001149
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 png_debug(1, "in png_check_keyword\n");
1151 *new_key = NULL;
1152
1153 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001154 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001155 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001156 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001157 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001158
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1160
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001161 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1162 if (*new_key == NULL)
1163 {
1164 png_warning(png_ptr, "Out of memory while procesing keyword");
1165 return ((png_size_t)0);
1166 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001167
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001168 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001170 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001171 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001173#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001175
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001176 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001177 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001178#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001179 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001180#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001181 *dp = ' ';
1182 }
1183 else
1184 {
1185 *dp = *kp;
1186 }
1187 }
1188 *dp = '\0';
1189
1190 /* Remove any trailing white space. */
1191 kp = *new_key + key_len - 1;
1192 if (*kp == ' ')
1193 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001194 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195
1196 while (*kp == ' ')
1197 {
1198 *(kp--) = '\0';
1199 key_len--;
1200 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001201 }
1202
1203 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 kp = *new_key;
1205 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001206 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001207 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001208
1209 while (*kp == ' ')
1210 {
1211 kp++;
1212 key_len--;
1213 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001214 }
1215
Andreas Dilger47a0c421997-05-16 02:46:07 -05001216 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001217
Andreas Dilger47a0c421997-05-16 02:46:07 -05001218 /* Remove multiple internal spaces. */
1219 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001220 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001222 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001223 *(dp++) = *kp;
1224 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001225 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001227 {
1228 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001229 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001230 }
1231 else
1232 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001233 *(dp++) = *kp;
1234 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001235 }
1236 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001238 if(kwarn)
1239 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001240
1241 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001243 png_free(png_ptr, *new_key);
1244 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001245 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 }
1247
1248 if (key_len > 79)
1249 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001250 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 new_key[79] = '\0';
1252 key_len = 79;
1253 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001255 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001256}
1257#endif
1258
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001259#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001260/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001261void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001262png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001264{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001265#ifdef PNG_USE_LOCAL_ARRAYS
1266 PNG_tEXt;
1267#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001268 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001269 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001270
Andreas Dilger47a0c421997-05-16 02:46:07 -05001271 png_debug(1, "in png_write_tEXt\n");
1272 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1273 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001274 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001275 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001277
Andreas Dilger47a0c421997-05-16 02:46:07 -05001278 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001279 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001280 else
1281 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001282
1283 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001284 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 -05001285 /*
1286 * We leave it to the application to meet PNG-1.0 requirements on the
1287 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1288 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001289 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001290 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001291 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001292 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001293 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001294
Guy Schalnat0d580581995-07-20 02:43:20 -05001295 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001296 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001297}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001298#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001299
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001300#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001301/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001302void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001303png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001304 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001305{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001306#ifdef PNG_USE_LOCAL_ARRAYS
1307 PNG_zTXt;
1308#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001310 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001311 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001312 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001313
Andreas Dilger47a0c421997-05-16 02:46:07 -05001314 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001315
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001317 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001318 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001319 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001320 }
1321
Andreas Dilger47a0c421997-05-16 02:46:07 -05001322 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1323 {
1324 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1325 png_free(png_ptr, new_key);
1326 return;
1327 }
1328
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001329 text_len = png_strlen(text);
1330
Andreas Dilger47a0c421997-05-16 02:46:07 -05001331 png_free(png_ptr, new_key);
1332
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001333 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001334 text_len = png_text_compress(png_ptr, text, text_len, compression,
1335 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001336
1337 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001338 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1339 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001340 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001342 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001343 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001345 /* write the compressed data */
1346 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001347
Guy Schalnat0d580581995-07-20 02:43:20 -05001348 /* close the chunk */
1349 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001350}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001351#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001352
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001353#if defined(PNG_WRITE_iTXt_SUPPORTED)
1354/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001355void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001356png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001357 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001358{
1359#ifdef PNG_USE_LOCAL_ARRAYS
1360 PNG_iTXt;
1361#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001362 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001363 png_charp new_lang, new_key;
1364 png_byte cbuf[2];
1365 compression_state comp;
1366
1367 png_debug(1, "in png_write_iTXt\n");
1368
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001369 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1370 {
1371 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1372 return;
1373 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001374 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001375 {
1376 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001377 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001378 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001379 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001380
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001381 if (lang_key == NULL)
1382 lang_key_len = 0;
1383 else
1384 lang_key_len = png_strlen(lang_key);
1385
1386 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001387 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001388 else
1389 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001390
1391 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001392 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1393 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001394
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001395
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001396 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001397 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001398
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001399 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1400 (png_uint_32)(
1401 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1402 + key_len
1403 + lang_len
1404 + lang_key_len
1405 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001406
1407 /*
1408 * We leave it to the application to meet PNG-1.0 requirements on the
1409 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1410 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1411 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1412 */
1413 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001414
1415 /* set the compression flag */
1416 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1417 compression == PNG_TEXT_COMPRESSION_NONE)
1418 cbuf[0] = 0;
1419 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1420 cbuf[0] = 1;
1421 /* set the compression method */
1422 cbuf[1] = 0;
1423 png_write_chunk_data(png_ptr, cbuf, 2);
1424
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001425 cbuf[0] = 0;
1426 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1427 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001428 png_write_compressed_data_out(png_ptr, &comp);
1429
1430 png_write_chunk_end(png_ptr);
1431 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001432 if (new_lang)
1433 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001434}
1435#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001436
1437#if defined(PNG_WRITE_oFFs_SUPPORTED)
1438/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001439void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001440png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001441 int unit_type)
1442{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001443#ifdef PNG_USE_LOCAL_ARRAYS
1444 PNG_oFFs;
1445#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001446 png_byte buf[9];
1447
1448 png_debug(1, "in png_write_oFFs\n");
1449 if (unit_type >= PNG_OFFSET_LAST)
1450 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1451
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001452 png_save_int_32(buf, x_offset);
1453 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001454 buf[8] = (png_byte)unit_type;
1455
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001456 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001457}
1458#endif
1459
1460#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001461/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001462void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001463png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1464 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1465{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001466#ifdef PNG_USE_LOCAL_ARRAYS
1467 PNG_pCAL;
1468#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001469 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001470 png_uint_32p params_len;
1471 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001472 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001473 int i;
1474
1475 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1476 if (type >= PNG_EQUATION_LAST)
1477 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1478
1479 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001480 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001481 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001482 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001483 total_len = purpose_len + units_len + 10;
1484
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001485 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001486 *png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001487
1488 /* Find the length of each parameter, making sure we don't count the
1489 null terminator for the last parameter. */
1490 for (i = 0; i < nparams; i++)
1491 {
1492 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001493 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001494 total_len += (png_size_t)params_len[i];
1495 }
1496
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001497 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001498 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001499 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001500 png_save_int_32(buf, X0);
1501 png_save_int_32(buf + 4, X1);
1502 buf[8] = (png_byte)type;
1503 buf[9] = (png_byte)nparams;
1504 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1505 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1506
1507 png_free(png_ptr, new_purpose);
1508
1509 for (i = 0; i < nparams; i++)
1510 {
1511 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1512 (png_size_t)params_len[i]);
1513 }
1514
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001515 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001516 png_write_chunk_end(png_ptr);
1517}
1518#endif
1519
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001520#if defined(PNG_WRITE_sCAL_SUPPORTED)
1521/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001522#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001523void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001524png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001525{
1526#ifdef PNG_USE_LOCAL_ARRAYS
1527 PNG_sCAL;
1528#endif
1529 png_size_t total_len;
1530 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001531 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001532
1533 png_debug(1, "in png_write_sCAL\n");
1534
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001535#if defined(_WIN32_WCE)
1536/* sprintf() function is not supported on WindowsCE */
1537 {
1538 wchar_t wc_buf[32];
1539 swprintf(wc_buf, TEXT("%12.12e"), width);
1540 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1541 swprintf(wc_buf, TEXT("%12.12e"), height);
1542 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1543 }
1544#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001545 sprintf(wbuf, "%12.12e", width);
1546 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001547#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001548 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001549
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001550 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001551 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001552 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001553 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1554 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001555
1556 png_write_chunk_end(png_ptr);
1557}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001558#else
1559#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001560void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001561png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001562 png_charp height)
1563{
1564#ifdef PNG_USE_LOCAL_ARRAYS
1565 PNG_sCAL;
1566#endif
1567 png_size_t total_len;
1568 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001569 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001570
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001571 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001572
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001573 png_strcpy(wbuf,(const char *)width);
1574 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001575 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001576
1577 png_debug1(3, "sCAL total length = %d\n", total_len);
1578 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001579 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001580 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1581 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001582
1583 png_write_chunk_end(png_ptr);
1584}
1585#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001586#endif
1587#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001588
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001589#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001590/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001591void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001592png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001593 png_uint_32 y_pixels_per_unit,
1594 int unit_type)
1595{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001596#ifdef PNG_USE_LOCAL_ARRAYS
1597 PNG_pHYs;
1598#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001599 png_byte buf[9];
1600
Andreas Dilger47a0c421997-05-16 02:46:07 -05001601 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001602 if (unit_type >= PNG_RESOLUTION_LAST)
1603 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1604
Guy Schalnat0d580581995-07-20 02:43:20 -05001605 png_save_uint_32(buf, x_pixels_per_unit);
1606 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001607 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001608
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001609 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001610}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001611#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001612
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001613#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001614/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1615 * or png_convert_from_time_t(), or fill in the structure yourself.
1616 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001617void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001618png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001619{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001620#ifdef PNG_USE_LOCAL_ARRAYS
1621 PNG_tIME;
1622#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001623 png_byte buf[7];
1624
Andreas Dilger47a0c421997-05-16 02:46:07 -05001625 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001626 if (mod_time->month > 12 || mod_time->month < 1 ||
1627 mod_time->day > 31 || mod_time->day < 1 ||
1628 mod_time->hour > 23 || mod_time->second > 60)
1629 {
1630 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1631 return;
1632 }
1633
Guy Schalnat0d580581995-07-20 02:43:20 -05001634 png_save_uint_16(buf, mod_time->year);
1635 buf[2] = mod_time->month;
1636 buf[3] = mod_time->day;
1637 buf[4] = mod_time->hour;
1638 buf[5] = mod_time->minute;
1639 buf[6] = mod_time->second;
1640
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001641 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001642}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001643#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001644
1645/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001646void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001647png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001648{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001649#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001650 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001652 /* start of interlace block */
1653 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001654
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001655 /* offset to next interlace block */
1656 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001657
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001658 /* start of interlace block in the y direction */
1659 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001660
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001661 /* offset to next interlace block in the y direction */
1662 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001663#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001664
Andreas Dilger47a0c421997-05-16 02:46:07 -05001665 png_size_t buf_size;
1666
1667 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001668 buf_size = (png_size_t)(PNG_ROWBYTES(
1669 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001670
Guy Schalnat0d580581995-07-20 02:43:20 -05001671 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001672 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001673 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001674
1675 /* set up filtering buffer, if using this filter */
1676 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001677 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001678 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001679 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001680 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001681 }
1682
Andreas Dilger47a0c421997-05-16 02:46:07 -05001683 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001684 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1685 {
1686 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001687 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001688 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001689
1690 if (png_ptr->do_filter & PNG_FILTER_UP)
1691 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001692 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001693 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001694 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001695 }
1696
1697 if (png_ptr->do_filter & PNG_FILTER_AVG)
1698 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001699 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1700 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001701 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001702 }
1703
1704 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1705 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001706 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001707 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001708 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001709 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001710 }
1711
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001712#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001713 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001714 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001715 {
1716 if (!(png_ptr->transformations & PNG_INTERLACE))
1717 {
1718 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1719 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001720 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1721 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001722 }
1723 else
1724 {
1725 png_ptr->num_rows = png_ptr->height;
1726 png_ptr->usr_width = png_ptr->width;
1727 }
1728 }
1729 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001730#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001731 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001732 png_ptr->num_rows = png_ptr->height;
1733 png_ptr->usr_width = png_ptr->width;
1734 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001735 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1736 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001737}
1738
Andreas Dilger47a0c421997-05-16 02:46:07 -05001739/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001740void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001741png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001742{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001743#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001744 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001745
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001746 /* start of interlace block */
1747 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001748
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001749 /* offset to next interlace block */
1750 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001751
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001752 /* start of interlace block in the y direction */
1753 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001754
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001755 /* offset to next interlace block in the y direction */
1756 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001757#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001758
Guy Schalnat0d580581995-07-20 02:43:20 -05001759 int ret;
1760
Andreas Dilger47a0c421997-05-16 02:46:07 -05001761 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001762 /* next row */
1763 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001764
Guy Schalnat0d580581995-07-20 02:43:20 -05001765 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001766 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001767 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001768
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001769#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001770 /* if interlaced, go to next pass */
1771 if (png_ptr->interlaced)
1772 {
1773 png_ptr->row_number = 0;
1774 if (png_ptr->transformations & PNG_INTERLACE)
1775 {
1776 png_ptr->pass++;
1777 }
1778 else
1779 {
1780 /* loop until we find a non-zero width or height pass */
1781 do
1782 {
1783 png_ptr->pass++;
1784 if (png_ptr->pass >= 7)
1785 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001786 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001787 png_pass_inc[png_ptr->pass] - 1 -
1788 png_pass_start[png_ptr->pass]) /
1789 png_pass_inc[png_ptr->pass];
1790 png_ptr->num_rows = (png_ptr->height +
1791 png_pass_yinc[png_ptr->pass] - 1 -
1792 png_pass_ystart[png_ptr->pass]) /
1793 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001794 if (png_ptr->transformations & PNG_INTERLACE)
1795 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001796 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1797
1798 }
1799
Guy Schalnate5a37791996-06-05 15:50:50 -05001800 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001801 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001802 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001803 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001804 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001805 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1806 png_ptr->usr_bit_depth,png_ptr->width))+1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001807 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001808 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001809 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001810#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001811
1812 /* if we get here, we've just written the last row, so we need
1813 to flush the compressor */
1814 do
1815 {
1816 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001817 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001818 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001819 if (ret == Z_OK)
1820 {
1821 /* check to see if we need more room */
1822 if (!(png_ptr->zstream.avail_out))
1823 {
1824 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1825 png_ptr->zstream.next_out = png_ptr->zbuf;
1826 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1827 }
1828 }
1829 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001830 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001831 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001832 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001833 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001834 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001835 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001836 } while (ret != Z_STREAM_END);
1837
1838 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001839 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001840 {
1841 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001842 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001843 }
1844
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001845 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001846}
1847
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001848#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001849/* Pick out the correct pixels for the interlace pass.
1850 * The basic idea here is to go through the row with a source
1851 * pointer and a destination pointer (sp and dp), and copy the
1852 * correct pixels for the pass. As the row gets compacted,
1853 * sp will always be >= dp, so we should never overwrite anything.
1854 * See the default: case for the easiest code to understand.
1855 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001856void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001857png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001858{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001859#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001860 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001861
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001862 /* start of interlace block */
1863 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001864
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001865 /* offset to next interlace block */
1866 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001867#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001868
Andreas Dilger47a0c421997-05-16 02:46:07 -05001869 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001870 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001871#if defined(PNG_USELESS_TESTS_SUPPORTED)
1872 if (row != NULL && row_info != NULL && pass < 6)
1873#else
1874 if (pass < 6)
1875#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001876 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001877 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001878 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001879 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001880 case 1:
1881 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001882 png_bytep sp;
1883 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001884 int shift;
1885 int d;
1886 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001887 png_uint_32 i;
1888 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001889
1890 dp = row;
1891 d = 0;
1892 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001893 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 i += png_pass_inc[pass])
1895 {
1896 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001897 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001898 d |= (value << shift);
1899
1900 if (shift == 0)
1901 {
1902 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001903 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001904 d = 0;
1905 }
1906 else
1907 shift--;
1908
1909 }
1910 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001911 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001912 break;
1913 }
1914 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001915 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001916 png_bytep sp;
1917 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001918 int shift;
1919 int d;
1920 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001921 png_uint_32 i;
1922 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001923
1924 dp = row;
1925 shift = 6;
1926 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001927 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 i += png_pass_inc[pass])
1929 {
1930 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001931 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 d |= (value << shift);
1933
1934 if (shift == 0)
1935 {
1936 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001937 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001938 d = 0;
1939 }
1940 else
1941 shift -= 2;
1942 }
1943 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001944 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001945 break;
1946 }
1947 case 4:
1948 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001949 png_bytep sp;
1950 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001951 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001952 int d;
1953 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001954 png_uint_32 i;
1955 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001956
1957 dp = row;
1958 shift = 4;
1959 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001960 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001961 i += png_pass_inc[pass])
1962 {
1963 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001964 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001965 d |= (value << shift);
1966
1967 if (shift == 0)
1968 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001969 shift = 4;
1970 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001971 d = 0;
1972 }
1973 else
1974 shift -= 4;
1975 }
1976 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001977 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001978 break;
1979 }
1980 default:
1981 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001982 png_bytep sp;
1983 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001984 png_uint_32 i;
1985 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001986 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001987
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001988 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001989 dp = row;
1990 /* find out how many bytes each pixel takes up */
1991 pixel_bytes = (row_info->pixel_depth >> 3);
1992 /* loop through the row, only looking at the pixels that
1993 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001994 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001995 i += png_pass_inc[pass])
1996 {
1997 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001998 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001999 /* move the pixel */
2000 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002001 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002002 /* next pixel */
2003 dp += pixel_bytes;
2004 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002005 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002006 }
2007 }
2008 /* set new row width */
2009 row_info->width = (row_info->width +
2010 png_pass_inc[pass] - 1 -
2011 png_pass_start[pass]) /
2012 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002013 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2014 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002015 }
2016}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002017#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002018
Andreas Dilger47a0c421997-05-16 02:46:07 -05002019/* This filters the row, chooses which filter to use, if it has not already
2020 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002021 * chosen filter.
2022 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002023#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002024#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002025#define PNG_LOMASK ((png_uint_32)0xffffL)
2026#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002027void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002028png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002029{
Guy Schalnate5a37791996-06-05 15:50:50 -05002030 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002031 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002032 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002033 png_uint_32 row_bytes = row_info->rowbytes;
2034#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2035 int num_p_filters = (int)png_ptr->num_prev_filters;
2036#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002037
Andreas Dilger47a0c421997-05-16 02:46:07 -05002038 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002039 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002040 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002041
2042 prev_row = png_ptr->prev_row;
2043 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002044 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002045
Andreas Dilger47a0c421997-05-16 02:46:07 -05002046 /* The prediction method we use is to find which method provides the
2047 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002048 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002049 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002050 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002051 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002052 * predictive" method (not implemented yet), which does test compressions
2053 * of lines using different filter methods, and then chooses the
2054 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002055 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002056 *
2057 * GRR 980525: consider also
2058 * (1) minimum sum of absolute differences from running average (i.e.,
2059 * keep running sum of non-absolute differences & count of bytes)
2060 * [track dispersion, too? restart average if dispersion too large?]
2061 * (1b) minimum sum of absolute differences from sliding average, probably
2062 * with window size <= deflate window (usually 32K)
2063 * (2) minimum sum of squared differences from zero or running average
2064 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002065 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002066
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002067
Guy Schalnate5a37791996-06-05 15:50:50 -05002068 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002069 * that has been chosen, as it doesn't actually do anything to the data.
2070 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002071 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002072 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002073 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002074 png_bytep rp;
2075 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002076 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002077 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002078
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002079 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002080 {
2081 v = *rp;
2082 sum += (v < 128) ? v : 256 - v;
2083 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002084
2085#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2086 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2087 {
2088 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002089 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002090 sumlo = sum & PNG_LOMASK;
2091 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2092
2093 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002094 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002096 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002097 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002098 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002099 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002100 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002101 PNG_WEIGHT_SHIFT;
2102 }
2103 }
2104
2105 /* Factor in the cost of this filter (this is here for completeness,
2106 * but it makes no sense to have a "cost" for the NONE filter, as
2107 * it has the minimum possible computational cost - none).
2108 */
2109 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2110 PNG_COST_SHIFT;
2111 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2112 PNG_COST_SHIFT;
2113
2114 if (sumhi > PNG_HIMASK)
2115 sum = PNG_MAXSUM;
2116 else
2117 sum = (sumhi << PNG_HISHIFT) + sumlo;
2118 }
2119#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002120 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002121 }
2122
Guy Schalnate5a37791996-06-05 15:50:50 -05002123 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002124 if (filter_to_do == PNG_FILTER_SUB)
2125 /* it's the only filter so no testing is needed */
2126 {
2127 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002128 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002129 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2130 i++, rp++, dp++)
2131 {
2132 *dp = *rp;
2133 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002134 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002135 i++, rp++, lp++, dp++)
2136 {
2137 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2138 }
2139 best_row = png_ptr->sub_row;
2140 }
2141
2142 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002143 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002144 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002146 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002147 int v;
2148
2149#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002150 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002151 * would reduce the sum of this filter, so that we can do the
2152 * early exit comparison without scaling the sum each time.
2153 */
2154 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2155 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002156 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002157 png_uint_32 lmhi, lmlo;
2158 lmlo = lmins & PNG_LOMASK;
2159 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2160
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002161 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002162 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002163 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002164 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002165 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002167 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168 PNG_WEIGHT_SHIFT;
2169 }
2170 }
2171
2172 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2173 PNG_COST_SHIFT;
2174 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2175 PNG_COST_SHIFT;
2176
2177 if (lmhi > PNG_HIMASK)
2178 lmins = PNG_MAXSUM;
2179 else
2180 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2181 }
2182#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002183
Guy Schalnate5a37791996-06-05 15:50:50 -05002184 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2185 i++, rp++, dp++)
2186 {
2187 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002188
Guy Schalnate5a37791996-06-05 15:50:50 -05002189 sum += (v < 128) ? v : 256 - v;
2190 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002191 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002192 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002193 {
2194 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002195
Guy Schalnate5a37791996-06-05 15:50:50 -05002196 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002197
2198 if (sum > lmins) /* We are already worse, don't continue. */
2199 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002200 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002201
2202#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2203 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2204 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002205 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002206 png_uint_32 sumhi, sumlo;
2207 sumlo = sum & PNG_LOMASK;
2208 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2209
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002210 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002211 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002212 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002213 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002214 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002216 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002217 PNG_WEIGHT_SHIFT;
2218 }
2219 }
2220
2221 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2222 PNG_COST_SHIFT;
2223 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2224 PNG_COST_SHIFT;
2225
2226 if (sumhi > PNG_HIMASK)
2227 sum = PNG_MAXSUM;
2228 else
2229 sum = (sumhi << PNG_HISHIFT) + sumlo;
2230 }
2231#endif
2232
Guy Schalnate5a37791996-06-05 15:50:50 -05002233 if (sum < mins)
2234 {
2235 mins = sum;
2236 best_row = png_ptr->sub_row;
2237 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002238 }
2239
Guy Schalnate5a37791996-06-05 15:50:50 -05002240 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002241 if (filter_to_do == PNG_FILTER_UP)
2242 {
2243 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002244 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002245
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002246 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002247 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002248 i++, rp++, pp++, dp++)
2249 {
2250 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2251 }
2252 best_row = png_ptr->up_row;
2253 }
2254
2255 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002256 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002257 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002258 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002260 int v;
2261
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002262
Andreas Dilger47a0c421997-05-16 02:46:07 -05002263#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2264 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2265 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002266 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002267 png_uint_32 lmhi, lmlo;
2268 lmlo = lmins & PNG_LOMASK;
2269 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2270
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002271 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002272 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002273 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002274 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002275 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002276 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002277 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002278 PNG_WEIGHT_SHIFT;
2279 }
2280 }
2281
2282 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2283 PNG_COST_SHIFT;
2284 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2285 PNG_COST_SHIFT;
2286
2287 if (lmhi > PNG_HIMASK)
2288 lmins = PNG_MAXSUM;
2289 else
2290 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2291 }
2292#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002293
2294 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002295 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002296 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002297 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002298
2299 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300
2301 if (sum > lmins) /* We are already worse, don't continue. */
2302 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002303 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304
2305#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2306 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2307 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002308 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002309 png_uint_32 sumhi, sumlo;
2310 sumlo = sum & PNG_LOMASK;
2311 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2312
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002313 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002315 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002316 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002317 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002318 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002319 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002320 PNG_WEIGHT_SHIFT;
2321 }
2322 }
2323
2324 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2325 PNG_COST_SHIFT;
2326 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2327 PNG_COST_SHIFT;
2328
2329 if (sumhi > PNG_HIMASK)
2330 sum = PNG_MAXSUM;
2331 else
2332 sum = (sumhi << PNG_HISHIFT) + sumlo;
2333 }
2334#endif
2335
Guy Schalnate5a37791996-06-05 15:50:50 -05002336 if (sum < mins)
2337 {
2338 mins = sum;
2339 best_row = png_ptr->up_row;
2340 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002341 }
2342
Guy Schalnate5a37791996-06-05 15:50:50 -05002343 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002344 if (filter_to_do == PNG_FILTER_AVG)
2345 {
2346 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002348 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002349 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002350 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002351 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002352 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002354 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002355 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2356 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002357 }
2358 best_row = png_ptr->avg_row;
2359 }
2360
2361 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002362 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002363 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002364 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002365 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002366 int v;
2367
2368#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2369 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2370 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002371 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002372 png_uint_32 lmhi, lmlo;
2373 lmlo = lmins & PNG_LOMASK;
2374 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2375
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002376 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002377 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002378 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002379 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002380 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002381 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002382 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002383 PNG_WEIGHT_SHIFT;
2384 }
2385 }
2386
2387 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2388 PNG_COST_SHIFT;
2389 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2390 PNG_COST_SHIFT;
2391
2392 if (lmhi > PNG_HIMASK)
2393 lmins = PNG_MAXSUM;
2394 else
2395 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2396 }
2397#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002398
2399 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002400 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002401 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002402 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002403
2404 sum += (v < 128) ? v : 256 - v;
2405 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002406 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002407 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002408 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002409 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002410
2411 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002412
2413 if (sum > lmins) /* We are already worse, don't continue. */
2414 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002415 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002416
2417#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2418 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2419 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002420 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002421 png_uint_32 sumhi, sumlo;
2422 sumlo = sum & PNG_LOMASK;
2423 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2424
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002425 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002426 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002427 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002428 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002429 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002430 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002431 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002432 PNG_WEIGHT_SHIFT;
2433 }
2434 }
2435
2436 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2437 PNG_COST_SHIFT;
2438 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2439 PNG_COST_SHIFT;
2440
2441 if (sumhi > PNG_HIMASK)
2442 sum = PNG_MAXSUM;
2443 else
2444 sum = (sumhi << PNG_HISHIFT) + sumlo;
2445 }
2446#endif
2447
Guy Schalnate5a37791996-06-05 15:50:50 -05002448 if (sum < mins)
2449 {
2450 mins = sum;
2451 best_row = png_ptr->avg_row;
2452 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002453 }
2454
Andreas Dilger47a0c421997-05-16 02:46:07 -05002455 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002456 if (filter_to_do == PNG_FILTER_PAETH)
2457 {
2458 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002460 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002461 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002462 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002463 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002464 }
2465
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002466 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002467 {
2468 int a, b, c, pa, pb, pc, p;
2469
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002470 b = *pp++;
2471 c = *cp++;
2472 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002473
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002474 p = b - c;
2475 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002476
2477#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002478 pa = abs(p);
2479 pb = abs(pc);
2480 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002481#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002482 pa = p < 0 ? -p : p;
2483 pb = pc < 0 ? -pc : pc;
2484 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002485#endif
2486
2487 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2488
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002489 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002490 }
2491 best_row = png_ptr->paeth_row;
2492 }
2493
2494 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002495 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002496 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002497 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002498 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002499 int v;
2500
2501#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2502 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2503 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002504 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002505 png_uint_32 lmhi, lmlo;
2506 lmlo = lmins & PNG_LOMASK;
2507 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2508
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002509 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002511 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002512 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002513 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002514 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002515 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002516 PNG_WEIGHT_SHIFT;
2517 }
2518 }
2519
2520 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2521 PNG_COST_SHIFT;
2522 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2523 PNG_COST_SHIFT;
2524
2525 if (lmhi > PNG_HIMASK)
2526 lmins = PNG_MAXSUM;
2527 else
2528 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2529 }
2530#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002531
2532 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002533 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002534 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002535 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002536
2537 sum += (v < 128) ? v : 256 - v;
2538 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002539
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002540 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002541 {
2542 int a, b, c, pa, pb, pc, p;
2543
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002544 b = *pp++;
2545 c = *cp++;
2546 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002547
2548#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002549 p = b - c;
2550 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002551#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002552 pa = abs(p);
2553 pb = abs(pc);
2554 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002555#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002556 pa = p < 0 ? -p : p;
2557 pb = pc < 0 ? -pc : pc;
2558 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002559#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002560 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2561#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002562 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002563 pa = abs(p - a);
2564 pb = abs(p - b);
2565 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002566 if (pa <= pb && pa <= pc)
2567 p = a;
2568 else if (pb <= pc)
2569 p = b;
2570 else
2571 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002572#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002573
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002574 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002575
2576 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002577
2578 if (sum > lmins) /* We are already worse, don't continue. */
2579 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002580 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002581
2582#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2583 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2584 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002585 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002586 png_uint_32 sumhi, sumlo;
2587 sumlo = sum & PNG_LOMASK;
2588 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2589
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002590 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002591 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002593 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002594 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002595 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002596 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002597 PNG_WEIGHT_SHIFT;
2598 }
2599 }
2600
2601 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2602 PNG_COST_SHIFT;
2603 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2604 PNG_COST_SHIFT;
2605
2606 if (sumhi > PNG_HIMASK)
2607 sum = PNG_MAXSUM;
2608 else
2609 sum = (sumhi << PNG_HISHIFT) + sumlo;
2610 }
2611#endif
2612
Guy Schalnate5a37791996-06-05 15:50:50 -05002613 if (sum < mins)
2614 {
2615 best_row = png_ptr->paeth_row;
2616 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002617 }
2618
Andreas Dilger47a0c421997-05-16 02:46:07 -05002619 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002620
Guy Schalnate5a37791996-06-05 15:50:50 -05002621 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002622
2623#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2624 /* Save the type of filter we picked this time for future calculations */
2625 if (png_ptr->num_prev_filters > 0)
2626 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002627 int j;
2628 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002629 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002630 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002631 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002632 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002633 }
2634#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002635}
2636
2637
Andreas Dilger47a0c421997-05-16 02:46:07 -05002638/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002639void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002640png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2641{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002642 png_debug(1, "in png_write_filtered_row\n");
2643 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002644 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002645
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002646 png_ptr->zstream.next_in = filtered_row;
2647 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002648 /* repeat until we have compressed all the data */
2649 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002650 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002651 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002652
Guy Schalnate5a37791996-06-05 15:50:50 -05002653 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002654 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002655 /* check for compression errors */
2656 if (ret != Z_OK)
2657 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002658 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002659 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002660 else
2661 png_error(png_ptr, "zlib error");
2662 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002663
Guy Schalnate5a37791996-06-05 15:50:50 -05002664 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002665 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002666 {
2667 /* write the IDAT and reset the zlib output buffer */
2668 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002669 png_ptr->zstream.next_out = png_ptr->zbuf;
2670 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002671 }
2672 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002673 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002674
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002675 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002676 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002677 {
2678 png_bytep tptr;
2679
2680 tptr = png_ptr->prev_row;
2681 png_ptr->prev_row = png_ptr->row_buf;
2682 png_ptr->row_buf = tptr;
2683 }
2684
Guy Schalnate5a37791996-06-05 15:50:50 -05002685 /* finish row - updates counters and flushes zlib if last row */
2686 png_write_finish_row(png_ptr);
2687
2688#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2689 png_ptr->flush_rows++;
2690
2691 if (png_ptr->flush_dist > 0 &&
2692 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002693 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002694 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002695 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002696#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002697}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002698#endif /* PNG_WRITE_SUPPORTED */