blob: bbd3881eaa57f46fd69a54208cb8420d935bc093 [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-Pehrson5b5dcf82004-07-17 22:45:44 -05004 * libpng version 1.2.6beta3 - July 18, 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,
237 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500238 png_memcpy(comp->output_ptr, old_ptr, old_max
239 * sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600240 png_free(png_ptr, old_ptr);
241 }
242 else
243 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
244 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
245 }
246
247 /* save the data */
248 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
249 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500250 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
251 png_ptr->zbuf_size);
252 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600253
254 /* and reset the buffer */
255 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
256 png_ptr->zstream.next_out = png_ptr->zbuf;
257 }
258 /* continue until we don't have any more to compress */
259 } while (png_ptr->zstream.avail_in);
260
261 /* finish the compression */
262 do
263 {
264 /* tell zlib we are finished */
265 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500266
267 if (ret == Z_OK)
268 {
269 /* check to see if we need more room */
270 if (!(png_ptr->zstream.avail_out))
271 {
272 /* check to make sure our output array has room */
273 if (comp->num_output_ptr >= comp->max_output_ptr)
274 {
275 int old_max;
276
277 old_max = comp->max_output_ptr;
278 comp->max_output_ptr = comp->num_output_ptr + 4;
279 if (comp->output_ptr != NULL)
280 {
281 png_charpp old_ptr;
282
283 old_ptr = comp->output_ptr;
284 /* This could be optimized to realloc() */
285 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
286 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500287 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500288 old_max * sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500289 png_free(png_ptr, old_ptr);
290 }
291 else
292 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
293 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
294 }
295
296 /* save off the data */
297 comp->output_ptr[comp->num_output_ptr] =
298 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500299 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
300 png_ptr->zbuf_size);
301 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500302
303 /* and reset the buffer pointers */
304 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
305 png_ptr->zstream.next_out = png_ptr->zbuf;
306 }
307 }
308 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600309 {
310 /* we got an error */
311 if (png_ptr->zstream.msg != NULL)
312 png_error(png_ptr, png_ptr->zstream.msg);
313 else
314 png_error(png_ptr, "zlib error");
315 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600316 } while (ret != Z_STREAM_END);
317
318 /* text length is number of buffers plus last buffer */
319 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
320 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
321 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
322
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500323 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600324}
325
326/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500327static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600328png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
329{
330 int i;
331
332 /* handle the no-compression case */
333 if (comp->input)
334 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500335 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
336 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600337 return;
338 }
339
340 /* write saved output buffers, if any */
341 for (i = 0; i < comp->num_output_ptr; i++)
342 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600343 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
344 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600345 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500346 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600347 }
348 if (comp->max_output_ptr != 0)
349 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500350 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 /* write anything left in zbuf */
352 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
353 png_write_chunk_data(png_ptr, png_ptr->zbuf,
354 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
355
356 /* reset zlib for another zTXt/iTXt or the image data */
357 deflateReset(&png_ptr->zstream);
358
359}
360#endif
361
Guy Schalnat0d580581995-07-20 02:43:20 -0500362/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600363 * information. Note that the rest of this code depends upon this
364 * information being correct.
365 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500366void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600367png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 int bit_depth, int color_type, int compression_type, int filter_type,
369 int interlace_type)
370{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600371#ifdef PNG_USE_LOCAL_ARRAYS
372 PNG_IHDR;
373#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 png_byte buf[13]; /* buffer to store the IHDR info */
375
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500377 /* Check that we have valid input data from the application info */
378 switch (color_type)
379 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500381 switch (bit_depth)
382 {
383 case 1:
384 case 2:
385 case 4:
386 case 8:
387 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500389 }
390 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500392 if (bit_depth != 8 && bit_depth != 16)
393 png_error(png_ptr, "Invalid bit depth for RGB image");
394 png_ptr->channels = 3;
395 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500397 switch (bit_depth)
398 {
399 case 1:
400 case 2:
401 case 4:
402 case 8: png_ptr->channels = 1; break;
403 default: png_error(png_ptr, "Invalid bit depth for paletted image");
404 }
405 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500407 if (bit_depth != 8 && bit_depth != 16)
408 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
409 png_ptr->channels = 2;
410 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500411 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500412 if (bit_depth != 8 && bit_depth != 16)
413 png_error(png_ptr, "Invalid bit depth for RGBA image");
414 png_ptr->channels = 4;
415 break;
416 default:
417 png_error(png_ptr, "Invalid image color type specified");
418 }
419
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500421 {
422 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500424 }
425
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600426 /* Write filter_method 64 (intrapixel differencing) only if
427 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
428 * 2. Libpng did not write a PNG signature (this filter_method is only
429 * used in PNG datastreams that are embedded in MNG datastreams) and
430 * 3. The application called png_permit_mng_features with a mask that
431 * included PNG_FLAG_MNG_FILTER_64 and
432 * 4. The filter_method is 64 and
433 * 5. The color_type is RGB or RGBA
434 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600435 if (
436#if defined(PNG_MNG_FEATURES_SUPPORTED)
437 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600438 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500439 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600440 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600441 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
442#endif
443 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500444 {
445 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500447 }
448
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600449#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500450 if (interlace_type != PNG_INTERLACE_NONE &&
451 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500452 {
453 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500454 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500455 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600456#else
457 interlace_type=PNG_INTERLACE_NONE;
458#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500459
460 /* save off the relevent information */
461 png_ptr->bit_depth = (png_byte)bit_depth;
462 png_ptr->color_type = (png_byte)color_type;
463 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500464#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600465 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500466#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500467 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500468 png_ptr->width = width;
469 png_ptr->height = height;
470
471 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500472 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500473 /* set the usr info, so any transformations can modify it */
474 png_ptr->usr_width = png_ptr->width;
475 png_ptr->usr_bit_depth = png_ptr->bit_depth;
476 png_ptr->usr_channels = png_ptr->channels;
477
Guy Schalnat0d580581995-07-20 02:43:20 -0500478 /* pack the header information into the buffer */
479 png_save_uint_32(buf, width);
480 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600481 buf[8] = (png_byte)bit_depth;
482 buf[9] = (png_byte)color_type;
483 buf[10] = (png_byte)compression_type;
484 buf[11] = (png_byte)filter_type;
485 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500486
487 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600488 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500489
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600491 png_ptr->zstream.zalloc = png_zalloc;
492 png_ptr->zstream.zfree = png_zfree;
493 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500495 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
497 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500500 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500501 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500503 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500505 png_ptr->zlib_strategy = Z_FILTERED;
506 else
507 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
508 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500509 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500510 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500511 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500512 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500514 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500516 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600517 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 png_ptr->zlib_method, png_ptr->zlib_window_bits,
519 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600520 png_ptr->zstream.next_out = png_ptr->zbuf;
521 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500522
Guy Schalnate5a37791996-06-05 15:50:50 -0500523 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500524}
525
526/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500527 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600528 * structure.
529 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500530void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500531png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500532{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600533#ifdef PNG_USE_LOCAL_ARRAYS
534 PNG_PLTE;
535#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500536 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600537 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500538 png_byte buf[3];
539
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500541 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500542#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600543 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500544#endif
545 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500546 {
547 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500548 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500549 png_error(png_ptr, "Invalid number of colors in palette");
550 }
551 else
552 {
553 png_warning(png_ptr, "Invalid number of colors in palette");
554 return;
555 }
556 }
557
558 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
559 {
560 png_warning(png_ptr,
561 "Ignoring request to write a PLTE chunk in grayscale PNG");
562 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500563 }
564
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565 png_ptr->num_palette = (png_uint_16)num_pal;
566 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500567
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600568 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500569#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500570 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500571 {
572 buf[0] = pal_ptr->red;
573 buf[1] = pal_ptr->green;
574 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500575 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500577#else
578 /* This is a little slower but some buggy compilers need to do this instead */
579 pal_ptr=palette;
580 for (i = 0; i < num_pal; i++)
581 {
582 buf[0] = pal_ptr[i].red;
583 buf[1] = pal_ptr[i].green;
584 buf[2] = pal_ptr[i].blue;
585 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
586 }
587#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500588 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500589 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500590}
591
592/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500593void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500595{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600596#ifdef PNG_USE_LOCAL_ARRAYS
597 PNG_IDAT;
598#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500599 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500600
601 /* Optimize the CMF field in the zlib stream. */
602 /* This hack of the zlib stream is compliant to the stream specification. */
603 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
604 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
605 {
606 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
607 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
608 {
609 /* Avoid memory underflows and multiplication overflows. */
610 /* The conditions below are practically always satisfied;
611 however, they still must be checked. */
612 if (length >= 2 &&
613 png_ptr->height < 16384 && png_ptr->width < 16384)
614 {
615 png_uint_32 uncompressed_idat_size = png_ptr->height *
616 ((png_ptr->width *
617 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
618 unsigned int z_cinfo = z_cmf >> 4;
619 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
620 while (uncompressed_idat_size <= half_z_window_size &&
621 half_z_window_size >= 256)
622 {
623 z_cinfo--;
624 half_z_window_size >>= 1;
625 }
626 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
627 if (data[0] != (png_byte)z_cmf)
628 {
629 data[0] = (png_byte)z_cmf;
630 data[1] &= 0xe0;
631 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
632 }
633 }
634 }
635 else
636 png_error(png_ptr,
637 "Invalid zlib compression method or flags in IDAT");
638 }
639
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600640 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500641 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500642}
643
644/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500645void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600646png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500647{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600648#ifdef PNG_USE_LOCAL_ARRAYS
649 PNG_IEND;
650#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500651 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500652 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500653 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600654 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500655}
656
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500657#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500658/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600659#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500660void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500661png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500662{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600663#ifdef PNG_USE_LOCAL_ARRAYS
664 PNG_gAMA;
665#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500666 png_uint_32 igamma;
667 png_byte buf[4];
668
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600670 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600671 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500672 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600673 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500674}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500675#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600676#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500677void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600678png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600679{
680#ifdef PNG_USE_LOCAL_ARRAYS
681 PNG_gAMA;
682#endif
683 png_byte buf[4];
684
685 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600686 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500687 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600688 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
689}
690#endif
691#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500692
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600693#if defined(PNG_WRITE_sRGB_SUPPORTED)
694/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500695void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600696png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600697{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600698#ifdef PNG_USE_LOCAL_ARRAYS
699 PNG_sRGB;
700#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600701 png_byte buf[1];
702
703 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600704 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600705 png_warning(png_ptr,
706 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600707 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600708 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600709}
710#endif
711
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600712#if defined(PNG_WRITE_iCCP_SUPPORTED)
713/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500714void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600715png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
716 png_charp profile, int profile_len)
717{
718#ifdef PNG_USE_LOCAL_ARRAYS
719 PNG_iCCP;
720#endif
721 png_size_t name_len;
722 png_charp new_name;
723 compression_state comp;
724
725 png_debug(1, "in png_write_iCCP\n");
726 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
727 &new_name)) == 0)
728 {
729 png_warning(png_ptr, "Empty keyword in iCCP chunk");
730 return;
731 }
732
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600733 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600734 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600735
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500736 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600737 profile_len = 0;
738
739 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500740 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600741 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600742
743 /* make sure we include the NULL after the name and the compression type */
744 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
745 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600746 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600747 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
748
749 if (profile_len)
750 png_write_compressed_data_out(png_ptr, &comp);
751
752 png_write_chunk_end(png_ptr);
753 png_free(png_ptr, new_name);
754}
755#endif
756
757#if defined(PNG_WRITE_sPLT_SUPPORTED)
758/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500759void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600760png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600761{
762#ifdef PNG_USE_LOCAL_ARRAYS
763 PNG_sPLT;
764#endif
765 png_size_t name_len;
766 png_charp new_name;
767 png_byte entrybuf[10];
768 int entry_size = (spalette->depth == 8 ? 6 : 10);
769 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600770 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500771#ifdef PNG_NO_POINTER_INDEXING
772 int i;
773#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600774
775 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600776 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
777 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600778 {
779 png_warning(png_ptr, "Empty keyword in sPLT chunk");
780 return;
781 }
782
783 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500784 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600785 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600786 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600787 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600788
789 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500790#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600791 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
792 {
793 if (spalette->depth == 8)
794 {
795 entrybuf[0] = (png_byte)ep->red;
796 entrybuf[1] = (png_byte)ep->green;
797 entrybuf[2] = (png_byte)ep->blue;
798 entrybuf[3] = (png_byte)ep->alpha;
799 png_save_uint_16(entrybuf + 4, ep->frequency);
800 }
801 else
802 {
803 png_save_uint_16(entrybuf + 0, ep->red);
804 png_save_uint_16(entrybuf + 2, ep->green);
805 png_save_uint_16(entrybuf + 4, ep->blue);
806 png_save_uint_16(entrybuf + 6, ep->alpha);
807 png_save_uint_16(entrybuf + 8, ep->frequency);
808 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500809 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600810 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500811#else
812 ep=spalette->entries;
813 for (i=0; i>spalette->nentries; i++)
814 {
815 if (spalette->depth == 8)
816 {
817 entrybuf[0] = (png_byte)ep[i].red;
818 entrybuf[1] = (png_byte)ep[i].green;
819 entrybuf[2] = (png_byte)ep[i].blue;
820 entrybuf[3] = (png_byte)ep[i].alpha;
821 png_save_uint_16(entrybuf + 4, ep[i].frequency);
822 }
823 else
824 {
825 png_save_uint_16(entrybuf + 0, ep[i].red);
826 png_save_uint_16(entrybuf + 2, ep[i].green);
827 png_save_uint_16(entrybuf + 4, ep[i].blue);
828 png_save_uint_16(entrybuf + 6, ep[i].alpha);
829 png_save_uint_16(entrybuf + 8, ep[i].frequency);
830 }
831 png_write_chunk_data(png_ptr, entrybuf, entry_size);
832 }
833#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600834
835 png_write_chunk_end(png_ptr);
836 png_free(png_ptr, new_name);
837}
838#endif
839
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500840#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500841/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500842void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600843png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500844{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600845#ifdef PNG_USE_LOCAL_ARRAYS
846 PNG_sBIT;
847#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500848 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500849 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500850
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600852 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500853 if (color_type & PNG_COLOR_MASK_COLOR)
854 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600855 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500856
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500857 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
858 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600859 if (sbit->red == 0 || sbit->red > maxbits ||
860 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500861 sbit->blue == 0 || sbit->blue > maxbits)
862 {
863 png_warning(png_ptr, "Invalid sBIT depth specified");
864 return;
865 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500866 buf[0] = sbit->red;
867 buf[1] = sbit->green;
868 buf[2] = sbit->blue;
869 size = 3;
870 }
871 else
872 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500873 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
874 {
875 png_warning(png_ptr, "Invalid sBIT depth specified");
876 return;
877 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500878 buf[0] = sbit->gray;
879 size = 1;
880 }
881
882 if (color_type & PNG_COLOR_MASK_ALPHA)
883 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500884 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
885 {
886 png_warning(png_ptr, "Invalid sBIT depth specified");
887 return;
888 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500889 buf[size++] = sbit->alpha;
890 }
891
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600892 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500893}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500894#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500895
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500896#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500897/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600898#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500899void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600901 double red_x, double red_y, double green_x, double green_y,
902 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500903{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600904#ifdef PNG_USE_LOCAL_ARRAYS
905 PNG_cHRM;
906#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500907 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600908 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500909
Andreas Dilger47a0c421997-05-16 02:46:07 -0500910 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600911 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500912 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
913 white_x + white_y > 1.0)
914 {
915 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500916#if !defined(PNG_NO_CONSOLE_IO)
917 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600918#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500919 return;
920 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600921 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500922 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600923 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500924 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500925
926 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
927 red_x + red_y > 1.0)
928 {
929 png_warning(png_ptr, "Invalid cHRM red point specified");
930 return;
931 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600932 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500933 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600934 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500935 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500936
937 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
938 green_x + green_y > 1.0)
939 {
940 png_warning(png_ptr, "Invalid cHRM green point specified");
941 return;
942 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600943 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500944 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600945 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500946 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500947
948 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
949 blue_x + blue_y > 1.0)
950 {
951 png_warning(png_ptr, "Invalid cHRM blue point specified");
952 return;
953 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600954 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500955 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600956 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500957 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500958
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600959 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500960}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500961#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600962#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500963void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600964png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
965 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
966 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
967 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600968{
969#ifdef PNG_USE_LOCAL_ARRAYS
970 PNG_cHRM;
971#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600972 png_byte buf[32];
973
974 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600975 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600976 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
977 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600978 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500979#if !defined(PNG_NO_CONSOLE_IO)
980 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600981#endif
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, (png_uint_32)white_x);
985 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600986
987 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
988 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600989 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600990 return;
991 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500992 png_save_uint_32(buf + 8, (png_uint_32)red_x);
993 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600994
995 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
996 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600997 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600998 return;
999 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001000 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1001 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001002
1003 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
1004 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001005 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006 return;
1007 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001008 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1009 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001010
1011 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1012}
1013#endif
1014#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001015
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001016#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001017/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001018void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001019png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001020 int num_trans, int color_type)
1021{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001022#ifdef PNG_USE_LOCAL_ARRAYS
1023 PNG_tRNS;
1024#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001025 png_byte buf[6];
1026
Andreas Dilger47a0c421997-05-16 02:46:07 -05001027 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001028 if (color_type == PNG_COLOR_TYPE_PALETTE)
1029 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001030 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001031 {
1032 png_warning(png_ptr,"Invalid number of transparent colors specified");
1033 return;
1034 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001035 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001036 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001037 }
1038 else if (color_type == PNG_COLOR_TYPE_GRAY)
1039 {
1040 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001041 if(tran->gray >= (1 << png_ptr->bit_depth))
1042 {
1043 png_warning(png_ptr,
1044 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1045 return;
1046 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001047 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001048 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001049 }
1050 else if (color_type == PNG_COLOR_TYPE_RGB)
1051 {
1052 /* three 16 bit values */
1053 png_save_uint_16(buf, tran->red);
1054 png_save_uint_16(buf + 2, tran->green);
1055 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001056 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1057 {
1058 png_warning(png_ptr,
1059 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1060 return;
1061 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001062 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001063 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001064 else
1065 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001066 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001067 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001068}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001069#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001070
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001071#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001072/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001073void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001074png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001075{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001076#ifdef PNG_USE_LOCAL_ARRAYS
1077 PNG_bKGD;
1078#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001079 png_byte buf[6];
1080
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001082 if (color_type == PNG_COLOR_TYPE_PALETTE)
1083 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001084 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001085#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001086 (png_ptr->num_palette ||
1087 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001088#endif
1089 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001090 {
1091 png_warning(png_ptr, "Invalid background palette index");
1092 return;
1093 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001094 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001095 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001096 }
1097 else if (color_type & PNG_COLOR_MASK_COLOR)
1098 {
1099 png_save_uint_16(buf, back->red);
1100 png_save_uint_16(buf + 2, back->green);
1101 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001102 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1103 {
1104 png_warning(png_ptr,
1105 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1106 return;
1107 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001108 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001109 }
1110 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001111 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001112 if(back->gray >= (1 << png_ptr->bit_depth))
1113 {
1114 png_warning(png_ptr,
1115 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1116 return;
1117 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001118 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001119 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001120 }
1121}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001122#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001123
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001124#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001125/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001126void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001127png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001128{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001129#ifdef PNG_USE_LOCAL_ARRAYS
1130 PNG_hIST;
1131#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001132 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001133 png_byte buf[3];
1134
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001136 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001137 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001138 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1139 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001140 png_warning(png_ptr, "Invalid number of histogram entries specified");
1141 return;
1142 }
1143
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001144 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001146 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001147 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001148 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001149 }
1150 png_write_chunk_end(png_ptr);
1151}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001152#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001153
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001154#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1155 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001156/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1157 * and if invalid, correct the keyword rather than discarding the entire
1158 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1159 * length, forbids leading or trailing whitespace, multiple internal spaces,
1160 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 *
1162 * The new_key is allocated to hold the corrected keyword and must be freed
1163 * by the calling routine. This avoids problems with trying to write to
1164 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001165 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001166png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001167png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001168{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001170 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001171 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001172 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001173
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174 png_debug(1, "in png_check_keyword\n");
1175 *new_key = NULL;
1176
1177 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001178 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001179 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001180 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001181 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001182
Andreas Dilger47a0c421997-05-16 02:46:07 -05001183 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1184
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001185 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001186
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001187 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001188 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001190 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001191 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001192#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001193 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001194
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001195 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001196 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001197#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001198 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001199#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001200 *dp = ' ';
1201 }
1202 else
1203 {
1204 *dp = *kp;
1205 }
1206 }
1207 *dp = '\0';
1208
1209 /* Remove any trailing white space. */
1210 kp = *new_key + key_len - 1;
1211 if (*kp == ' ')
1212 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001213 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214
1215 while (*kp == ' ')
1216 {
1217 *(kp--) = '\0';
1218 key_len--;
1219 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001220 }
1221
1222 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001223 kp = *new_key;
1224 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001225 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001226 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001227
1228 while (*kp == ' ')
1229 {
1230 kp++;
1231 key_len--;
1232 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233 }
1234
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 /* Remove multiple internal spaces. */
1238 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001239 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001240 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001241 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242 *(dp++) = *kp;
1243 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001244 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001245 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246 {
1247 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001248 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001249 }
1250 else
1251 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001252 *(dp++) = *kp;
1253 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254 }
1255 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001257 if(kwarn)
1258 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001259
1260 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001262 png_free(png_ptr, *new_key);
1263 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001264 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001265 }
1266
1267 if (key_len > 79)
1268 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001269 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 new_key[79] = '\0';
1271 key_len = 79;
1272 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001273
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001274 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001275}
1276#endif
1277
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001278#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001279/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001280void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001281png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001282 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001283{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001284#ifdef PNG_USE_LOCAL_ARRAYS
1285 PNG_tEXt;
1286#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001287 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001288 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001289
Andreas Dilger47a0c421997-05-16 02:46:07 -05001290 png_debug(1, "in png_write_tEXt\n");
1291 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1292 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001293 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001294 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001295 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001296
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001298 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001299 else
1300 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001301
1302 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001303 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 -05001304 /*
1305 * We leave it to the application to meet PNG-1.0 requirements on the
1306 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1307 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001308 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001309 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001310 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001311 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001312 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001313
Guy Schalnat0d580581995-07-20 02:43:20 -05001314 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001316}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001317#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001318
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001319#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001320/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001321void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001322png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001323 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001324{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001325#ifdef PNG_USE_LOCAL_ARRAYS
1326 PNG_zTXt;
1327#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001328 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001329 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001330 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001331 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001332
Andreas Dilger47a0c421997-05-16 02:46:07 -05001333 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001334
Andreas Dilger47a0c421997-05-16 02:46:07 -05001335 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001336 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001337 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001339 }
1340
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1342 {
1343 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1344 png_free(png_ptr, new_key);
1345 return;
1346 }
1347
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001348 text_len = png_strlen(text);
1349
Andreas Dilger47a0c421997-05-16 02:46:07 -05001350 png_free(png_ptr, new_key);
1351
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001352 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001353 text_len = png_text_compress(png_ptr, text, text_len, compression,
1354 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001355
1356 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001357 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1358 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001359 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001360 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001361 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001362 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001363 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001364 /* write the compressed data */
1365 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001366
Guy Schalnat0d580581995-07-20 02:43:20 -05001367 /* close the chunk */
1368 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001369}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001370#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001371
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001372#if defined(PNG_WRITE_iTXt_SUPPORTED)
1373/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001374void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001375png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001376 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001377{
1378#ifdef PNG_USE_LOCAL_ARRAYS
1379 PNG_iTXt;
1380#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001381 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001382 png_charp new_lang, new_key;
1383 png_byte cbuf[2];
1384 compression_state comp;
1385
1386 png_debug(1, "in png_write_iTXt\n");
1387
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001388 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1389 {
1390 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1391 return;
1392 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001393 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001394 {
1395 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001396 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001397 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001398 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001399
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001400 if (lang_key == NULL)
1401 lang_key_len = 0;
1402 else
1403 lang_key_len = png_strlen(lang_key);
1404
1405 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001406 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001407 else
1408 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001409
1410 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001411 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1412 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001413
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001414
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001415 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001416 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001417
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001418 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1419 (png_uint_32)(
1420 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1421 + key_len
1422 + lang_len
1423 + lang_key_len
1424 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001425
1426 /*
1427 * We leave it to the application to meet PNG-1.0 requirements on the
1428 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1429 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1430 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1431 */
1432 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001433
1434 /* set the compression flag */
1435 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1436 compression == PNG_TEXT_COMPRESSION_NONE)
1437 cbuf[0] = 0;
1438 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1439 cbuf[0] = 1;
1440 /* set the compression method */
1441 cbuf[1] = 0;
1442 png_write_chunk_data(png_ptr, cbuf, 2);
1443
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001444 cbuf[0] = 0;
1445 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1446 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 -06001447 png_write_compressed_data_out(png_ptr, &comp);
1448
1449 png_write_chunk_end(png_ptr);
1450 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001451 if (new_lang)
1452 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001453}
1454#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001455
1456#if defined(PNG_WRITE_oFFs_SUPPORTED)
1457/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001458void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001459png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001460 int unit_type)
1461{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001462#ifdef PNG_USE_LOCAL_ARRAYS
1463 PNG_oFFs;
1464#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001465 png_byte buf[9];
1466
1467 png_debug(1, "in png_write_oFFs\n");
1468 if (unit_type >= PNG_OFFSET_LAST)
1469 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1470
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001471 png_save_int_32(buf, x_offset);
1472 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001473 buf[8] = (png_byte)unit_type;
1474
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001475 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001476}
1477#endif
1478
1479#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001480/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001481void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001482png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1483 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1484{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001485#ifdef PNG_USE_LOCAL_ARRAYS
1486 PNG_pCAL;
1487#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001488 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001489 png_uint_32p params_len;
1490 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001491 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001492 int i;
1493
1494 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1495 if (type >= PNG_EQUATION_LAST)
1496 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1497
1498 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001499 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001500 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001501 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001502 total_len = purpose_len + units_len + 10;
1503
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001504 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1505 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001506
1507 /* Find the length of each parameter, making sure we don't count the
1508 null terminator for the last parameter. */
1509 for (i = 0; i < nparams; i++)
1510 {
1511 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001512 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001513 total_len += (png_size_t)params_len[i];
1514 }
1515
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001516 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001517 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001518 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001519 png_save_int_32(buf, X0);
1520 png_save_int_32(buf + 4, X1);
1521 buf[8] = (png_byte)type;
1522 buf[9] = (png_byte)nparams;
1523 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1524 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1525
1526 png_free(png_ptr, new_purpose);
1527
1528 for (i = 0; i < nparams; i++)
1529 {
1530 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1531 (png_size_t)params_len[i]);
1532 }
1533
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001534 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001535 png_write_chunk_end(png_ptr);
1536}
1537#endif
1538
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001539#if defined(PNG_WRITE_sCAL_SUPPORTED)
1540/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001541#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001542void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001543png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001544{
1545#ifdef PNG_USE_LOCAL_ARRAYS
1546 PNG_sCAL;
1547#endif
1548 png_size_t total_len;
1549 char wbuf[32], hbuf[32];
1550
1551 png_debug(1, "in png_write_sCAL\n");
1552
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001553#if defined(_WIN32_WCE)
1554/* sprintf() function is not supported on WindowsCE */
1555 {
1556 wchar_t wc_buf[32];
1557 swprintf(wc_buf, TEXT("%12.12e"), width);
1558 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1559 swprintf(wc_buf, TEXT("%12.12e"), height);
1560 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1561 }
1562#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001563 sprintf(wbuf, "%12.12e", width);
1564 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001565#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001566 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001567
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001568 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001569 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001570 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001571 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1572 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001573
1574 png_write_chunk_end(png_ptr);
1575}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001576#else
1577#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001578void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001579png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001580 png_charp height)
1581{
1582#ifdef PNG_USE_LOCAL_ARRAYS
1583 PNG_sCAL;
1584#endif
1585 png_size_t total_len;
1586 char wbuf[32], hbuf[32];
1587
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001588 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001589
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001590 png_strcpy(wbuf,(const char *)width);
1591 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001592 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001593
1594 png_debug1(3, "sCAL total length = %d\n", total_len);
1595 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001596 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001597 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1598 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001599
1600 png_write_chunk_end(png_ptr);
1601}
1602#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001603#endif
1604#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001605
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001606#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001607/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001608void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001609png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001610 png_uint_32 y_pixels_per_unit,
1611 int unit_type)
1612{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001613#ifdef PNG_USE_LOCAL_ARRAYS
1614 PNG_pHYs;
1615#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001616 png_byte buf[9];
1617
Andreas Dilger47a0c421997-05-16 02:46:07 -05001618 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001619 if (unit_type >= PNG_RESOLUTION_LAST)
1620 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1621
Guy Schalnat0d580581995-07-20 02:43:20 -05001622 png_save_uint_32(buf, x_pixels_per_unit);
1623 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001624 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001625
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001626 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001627}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001628#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001629
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001630#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001631/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1632 * or png_convert_from_time_t(), or fill in the structure yourself.
1633 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001634void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001635png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001636{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001637#ifdef PNG_USE_LOCAL_ARRAYS
1638 PNG_tIME;
1639#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001640 png_byte buf[7];
1641
Andreas Dilger47a0c421997-05-16 02:46:07 -05001642 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001643 if (mod_time->month > 12 || mod_time->month < 1 ||
1644 mod_time->day > 31 || mod_time->day < 1 ||
1645 mod_time->hour > 23 || mod_time->second > 60)
1646 {
1647 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1648 return;
1649 }
1650
Guy Schalnat0d580581995-07-20 02:43:20 -05001651 png_save_uint_16(buf, mod_time->year);
1652 buf[2] = mod_time->month;
1653 buf[3] = mod_time->day;
1654 buf[4] = mod_time->hour;
1655 buf[5] = mod_time->minute;
1656 buf[6] = mod_time->second;
1657
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001658 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001659}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001660#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001661
1662/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001663void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001664png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001665{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001666#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001667 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001668
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001669 /* start of interlace block */
1670 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001671
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001672 /* offset to next interlace block */
1673 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001674
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001675 /* start of interlace block in the y direction */
1676 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001677
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001678 /* offset to next interlace block in the y direction */
1679 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001680#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001681
Andreas Dilger47a0c421997-05-16 02:46:07 -05001682 png_size_t buf_size;
1683
1684 png_debug(1, "in png_write_start_row\n");
1685 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1686 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1687
Guy Schalnat0d580581995-07-20 02:43:20 -05001688 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001689 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001690 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001691
1692 /* set up filtering buffer, if using this filter */
1693 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001694 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001695 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001696 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001697 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001698 }
1699
Andreas Dilger47a0c421997-05-16 02:46:07 -05001700 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001701 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1702 {
1703 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001704 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001705 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001706
1707 if (png_ptr->do_filter & PNG_FILTER_UP)
1708 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001709 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001710 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001711 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001712 }
1713
1714 if (png_ptr->do_filter & PNG_FILTER_AVG)
1715 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001716 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1717 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001718 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001719 }
1720
1721 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1722 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001723 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001724 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001725 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001726 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001727 }
1728
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001729#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001730 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001731 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001732 {
1733 if (!(png_ptr->transformations & PNG_INTERLACE))
1734 {
1735 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1736 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001737 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1738 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001739 }
1740 else
1741 {
1742 png_ptr->num_rows = png_ptr->height;
1743 png_ptr->usr_width = png_ptr->width;
1744 }
1745 }
1746 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001747#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001748 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001749 png_ptr->num_rows = png_ptr->height;
1750 png_ptr->usr_width = png_ptr->width;
1751 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001752 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1753 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001754}
1755
Andreas Dilger47a0c421997-05-16 02:46:07 -05001756/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001757void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001758png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001759{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001760#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001761 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001762
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001763 /* start of interlace block */
1764 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001765
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001766 /* offset to next interlace block */
1767 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001768
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001769 /* start of interlace block in the y direction */
1770 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001771
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001772 /* offset to next interlace block in the y direction */
1773 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001774#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001775
Guy Schalnat0d580581995-07-20 02:43:20 -05001776 int ret;
1777
Andreas Dilger47a0c421997-05-16 02:46:07 -05001778 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001779 /* next row */
1780 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001781
Guy Schalnat0d580581995-07-20 02:43:20 -05001782 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001783 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001784 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001785
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001786#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001787 /* if interlaced, go to next pass */
1788 if (png_ptr->interlaced)
1789 {
1790 png_ptr->row_number = 0;
1791 if (png_ptr->transformations & PNG_INTERLACE)
1792 {
1793 png_ptr->pass++;
1794 }
1795 else
1796 {
1797 /* loop until we find a non-zero width or height pass */
1798 do
1799 {
1800 png_ptr->pass++;
1801 if (png_ptr->pass >= 7)
1802 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001803 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001804 png_pass_inc[png_ptr->pass] - 1 -
1805 png_pass_start[png_ptr->pass]) /
1806 png_pass_inc[png_ptr->pass];
1807 png_ptr->num_rows = (png_ptr->height +
1808 png_pass_yinc[png_ptr->pass] - 1 -
1809 png_pass_ystart[png_ptr->pass]) /
1810 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001811 if (png_ptr->transformations & PNG_INTERLACE)
1812 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001813 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1814
1815 }
1816
Guy Schalnate5a37791996-06-05 15:50:50 -05001817 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001818 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001819 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001820 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001821 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001822 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001823 (png_uint_32)png_ptr->usr_bit_depth *
1824 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001825 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001826 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001827 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001828#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001829
1830 /* if we get here, we've just written the last row, so we need
1831 to flush the compressor */
1832 do
1833 {
1834 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001835 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001836 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001837 if (ret == Z_OK)
1838 {
1839 /* check to see if we need more room */
1840 if (!(png_ptr->zstream.avail_out))
1841 {
1842 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1843 png_ptr->zstream.next_out = png_ptr->zbuf;
1844 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1845 }
1846 }
1847 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001849 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001850 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001851 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001852 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001853 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001854 } while (ret != Z_STREAM_END);
1855
1856 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001857 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001858 {
1859 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001860 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001861 }
1862
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001863 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001864}
1865
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001866#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001867/* Pick out the correct pixels for the interlace pass.
1868 * The basic idea here is to go through the row with a source
1869 * pointer and a destination pointer (sp and dp), and copy the
1870 * correct pixels for the pass. As the row gets compacted,
1871 * sp will always be >= dp, so we should never overwrite anything.
1872 * See the default: case for the easiest code to understand.
1873 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001874void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001875png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001876{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001877#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001878 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001879
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001880 /* start of interlace block */
1881 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001882
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001883 /* offset to next interlace block */
1884 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001885#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001886
Andreas Dilger47a0c421997-05-16 02:46:07 -05001887 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001888 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001889#if defined(PNG_USELESS_TESTS_SUPPORTED)
1890 if (row != NULL && row_info != NULL && pass < 6)
1891#else
1892 if (pass < 6)
1893#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001895 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001896 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001897 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001898 case 1:
1899 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001900 png_bytep sp;
1901 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001902 int shift;
1903 int d;
1904 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001905 png_uint_32 i;
1906 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001907
1908 dp = row;
1909 d = 0;
1910 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001911 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001912 i += png_pass_inc[pass])
1913 {
1914 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001915 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001916 d |= (value << shift);
1917
1918 if (shift == 0)
1919 {
1920 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001921 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001922 d = 0;
1923 }
1924 else
1925 shift--;
1926
1927 }
1928 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001929 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001930 break;
1931 }
1932 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001933 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001934 png_bytep sp;
1935 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 int shift;
1937 int d;
1938 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001939 png_uint_32 i;
1940 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001941
1942 dp = row;
1943 shift = 6;
1944 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001945 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001946 i += png_pass_inc[pass])
1947 {
1948 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001949 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001950 d |= (value << shift);
1951
1952 if (shift == 0)
1953 {
1954 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001955 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001956 d = 0;
1957 }
1958 else
1959 shift -= 2;
1960 }
1961 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001962 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001963 break;
1964 }
1965 case 4:
1966 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001967 png_bytep sp;
1968 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001969 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 int d;
1971 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001972 png_uint_32 i;
1973 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001974
1975 dp = row;
1976 shift = 4;
1977 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001978 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001979 i += png_pass_inc[pass])
1980 {
1981 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001982 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001983 d |= (value << shift);
1984
1985 if (shift == 0)
1986 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001987 shift = 4;
1988 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001989 d = 0;
1990 }
1991 else
1992 shift -= 4;
1993 }
1994 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001995 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001996 break;
1997 }
1998 default:
1999 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002000 png_bytep sp;
2001 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002002 png_uint_32 i;
2003 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002004 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002005
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002006 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002007 dp = row;
2008 /* find out how many bytes each pixel takes up */
2009 pixel_bytes = (row_info->pixel_depth >> 3);
2010 /* loop through the row, only looking at the pixels that
2011 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002013 i += png_pass_inc[pass])
2014 {
2015 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002016 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002017 /* move the pixel */
2018 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002019 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002020 /* next pixel */
2021 dp += pixel_bytes;
2022 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002023 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002024 }
2025 }
2026 /* set new row width */
2027 row_info->width = (row_info->width +
2028 png_pass_inc[pass] - 1 -
2029 png_pass_start[pass]) /
2030 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002031 row_info->rowbytes = ((row_info->width *
2032 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05002033 }
2034}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002035#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002036
Andreas Dilger47a0c421997-05-16 02:46:07 -05002037/* This filters the row, chooses which filter to use, if it has not already
2038 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002039 * chosen filter.
2040 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002041#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002042#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002043#define PNG_LOMASK ((png_uint_32)0xffffL)
2044#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002045void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002046png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002047{
Guy Schalnate5a37791996-06-05 15:50:50 -05002048 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002049 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002050 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002051 png_uint_32 row_bytes = row_info->rowbytes;
2052#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2053 int num_p_filters = (int)png_ptr->num_prev_filters;
2054#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002055
Andreas Dilger47a0c421997-05-16 02:46:07 -05002056 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002057 /* find out how many bytes offset each pixel is */
2058 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05002059
2060 prev_row = png_ptr->prev_row;
2061 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002062 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002063
Andreas Dilger47a0c421997-05-16 02:46:07 -05002064 /* The prediction method we use is to find which method provides the
2065 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002066 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002067 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002068 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002069 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002070 * predictive" method (not implemented yet), which does test compressions
2071 * of lines using different filter methods, and then chooses the
2072 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002073 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002074 *
2075 * GRR 980525: consider also
2076 * (1) minimum sum of absolute differences from running average (i.e.,
2077 * keep running sum of non-absolute differences & count of bytes)
2078 * [track dispersion, too? restart average if dispersion too large?]
2079 * (1b) minimum sum of absolute differences from sliding average, probably
2080 * with window size <= deflate window (usually 32K)
2081 * (2) minimum sum of squared differences from zero or running average
2082 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002083 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002084
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002085
Guy Schalnate5a37791996-06-05 15:50:50 -05002086 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002087 * that has been chosen, as it doesn't actually do anything to the data.
2088 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002089 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002090 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002091 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002092 png_bytep rp;
2093 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002094 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002096
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002097 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002098 {
2099 v = *rp;
2100 sum += (v < 128) ? v : 256 - v;
2101 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002102
2103#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2104 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2105 {
2106 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002107 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002108 sumlo = sum & PNG_LOMASK;
2109 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2110
2111 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002112 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002114 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002115 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002116 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002117 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002118 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002119 PNG_WEIGHT_SHIFT;
2120 }
2121 }
2122
2123 /* Factor in the cost of this filter (this is here for completeness,
2124 * but it makes no sense to have a "cost" for the NONE filter, as
2125 * it has the minimum possible computational cost - none).
2126 */
2127 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2128 PNG_COST_SHIFT;
2129 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2130 PNG_COST_SHIFT;
2131
2132 if (sumhi > PNG_HIMASK)
2133 sum = PNG_MAXSUM;
2134 else
2135 sum = (sumhi << PNG_HISHIFT) + sumlo;
2136 }
2137#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002138 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002139 }
2140
Guy Schalnate5a37791996-06-05 15:50:50 -05002141 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002142 if (filter_to_do == PNG_FILTER_SUB)
2143 /* it's the only filter so no testing is needed */
2144 {
2145 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002146 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002147 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2148 i++, rp++, dp++)
2149 {
2150 *dp = *rp;
2151 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002152 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002153 i++, rp++, lp++, dp++)
2154 {
2155 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2156 }
2157 best_row = png_ptr->sub_row;
2158 }
2159
2160 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002161 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002162 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002163 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002164 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002165 int v;
2166
2167#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002168 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002169 * would reduce the sum of this filter, so that we can do the
2170 * early exit comparison without scaling the sum each time.
2171 */
2172 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2173 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002174 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175 png_uint_32 lmhi, lmlo;
2176 lmlo = lmins & PNG_LOMASK;
2177 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2178
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002179 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002180 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002181 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002183 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002185 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 PNG_WEIGHT_SHIFT;
2187 }
2188 }
2189
2190 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2191 PNG_COST_SHIFT;
2192 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2193 PNG_COST_SHIFT;
2194
2195 if (lmhi > PNG_HIMASK)
2196 lmins = PNG_MAXSUM;
2197 else
2198 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2199 }
2200#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002201
Guy Schalnate5a37791996-06-05 15:50:50 -05002202 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2203 i++, rp++, dp++)
2204 {
2205 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002206
Guy Schalnate5a37791996-06-05 15:50:50 -05002207 sum += (v < 128) ? v : 256 - v;
2208 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002209 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002210 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002211 {
2212 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002213
Guy Schalnate5a37791996-06-05 15:50:50 -05002214 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215
2216 if (sum > lmins) /* We are already worse, don't continue. */
2217 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002218 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002219
2220#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2221 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2222 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002223 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002224 png_uint_32 sumhi, sumlo;
2225 sumlo = sum & PNG_LOMASK;
2226 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2227
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002228 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002229 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002230 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002231 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002232 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002233 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002234 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002235 PNG_WEIGHT_SHIFT;
2236 }
2237 }
2238
2239 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2240 PNG_COST_SHIFT;
2241 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2242 PNG_COST_SHIFT;
2243
2244 if (sumhi > PNG_HIMASK)
2245 sum = PNG_MAXSUM;
2246 else
2247 sum = (sumhi << PNG_HISHIFT) + sumlo;
2248 }
2249#endif
2250
Guy Schalnate5a37791996-06-05 15:50:50 -05002251 if (sum < mins)
2252 {
2253 mins = sum;
2254 best_row = png_ptr->sub_row;
2255 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002256 }
2257
Guy Schalnate5a37791996-06-05 15:50:50 -05002258 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002259 if (filter_to_do == PNG_FILTER_UP)
2260 {
2261 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002262 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002263
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002264 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002265 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002266 i++, rp++, pp++, dp++)
2267 {
2268 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2269 }
2270 best_row = png_ptr->up_row;
2271 }
2272
2273 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002274 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002275 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002276 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002277 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002278 int v;
2279
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002280
Andreas Dilger47a0c421997-05-16 02:46:07 -05002281#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2282 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2283 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002284 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002285 png_uint_32 lmhi, lmlo;
2286 lmlo = lmins & PNG_LOMASK;
2287 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2288
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002289 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002290 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002291 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002292 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002293 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002294 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002295 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002296 PNG_WEIGHT_SHIFT;
2297 }
2298 }
2299
2300 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2301 PNG_COST_SHIFT;
2302 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2303 PNG_COST_SHIFT;
2304
2305 if (lmhi > PNG_HIMASK)
2306 lmins = PNG_MAXSUM;
2307 else
2308 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2309 }
2310#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002311
2312 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002313 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002314 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002315 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002316
2317 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002318
2319 if (sum > lmins) /* We are already worse, don't continue. */
2320 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002321 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002322
2323#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2324 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2325 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002326 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002327 png_uint_32 sumhi, sumlo;
2328 sumlo = sum & PNG_LOMASK;
2329 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2330
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002331 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002332 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002333 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002334 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002335 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002336 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002337 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002338 PNG_WEIGHT_SHIFT;
2339 }
2340 }
2341
2342 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2343 PNG_COST_SHIFT;
2344 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2345 PNG_COST_SHIFT;
2346
2347 if (sumhi > PNG_HIMASK)
2348 sum = PNG_MAXSUM;
2349 else
2350 sum = (sumhi << PNG_HISHIFT) + sumlo;
2351 }
2352#endif
2353
Guy Schalnate5a37791996-06-05 15:50:50 -05002354 if (sum < mins)
2355 {
2356 mins = sum;
2357 best_row = png_ptr->up_row;
2358 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002359 }
2360
Guy Schalnate5a37791996-06-05 15:50:50 -05002361 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002362 if (filter_to_do == PNG_FILTER_AVG)
2363 {
2364 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002365 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002366 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002367 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002368 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002369 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002370 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002372 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002373 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2374 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002375 }
2376 best_row = png_ptr->avg_row;
2377 }
2378
2379 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002380 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002381 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002382 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002383 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002384 int v;
2385
2386#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2387 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2388 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002389 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002390 png_uint_32 lmhi, lmlo;
2391 lmlo = lmins & PNG_LOMASK;
2392 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2393
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002394 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002395 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002396 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002397 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002398 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002399 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002400 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002401 PNG_WEIGHT_SHIFT;
2402 }
2403 }
2404
2405 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2406 PNG_COST_SHIFT;
2407 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2408 PNG_COST_SHIFT;
2409
2410 if (lmhi > PNG_HIMASK)
2411 lmins = PNG_MAXSUM;
2412 else
2413 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2414 }
2415#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002416
2417 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002418 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002419 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002420 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002421
2422 sum += (v < 128) ? v : 256 - v;
2423 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002424 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002425 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002426 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002427 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002428
2429 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002430
2431 if (sum > lmins) /* We are already worse, don't continue. */
2432 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002433 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002434
2435#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2436 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2437 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002438 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002439 png_uint_32 sumhi, sumlo;
2440 sumlo = sum & PNG_LOMASK;
2441 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2442
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002443 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002444 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002445 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002446 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002448 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450 PNG_WEIGHT_SHIFT;
2451 }
2452 }
2453
2454 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2455 PNG_COST_SHIFT;
2456 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2457 PNG_COST_SHIFT;
2458
2459 if (sumhi > PNG_HIMASK)
2460 sum = PNG_MAXSUM;
2461 else
2462 sum = (sumhi << PNG_HISHIFT) + sumlo;
2463 }
2464#endif
2465
Guy Schalnate5a37791996-06-05 15:50:50 -05002466 if (sum < mins)
2467 {
2468 mins = sum;
2469 best_row = png_ptr->avg_row;
2470 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002471 }
2472
Andreas Dilger47a0c421997-05-16 02:46:07 -05002473 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002474 if (filter_to_do == PNG_FILTER_PAETH)
2475 {
2476 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002477 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002478 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002479 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002480 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002481 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002482 }
2483
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002484 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002485 {
2486 int a, b, c, pa, pb, pc, p;
2487
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002488 b = *pp++;
2489 c = *cp++;
2490 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002491
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002492 p = b - c;
2493 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002494
2495#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002496 pa = abs(p);
2497 pb = abs(pc);
2498 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002499#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 pa = p < 0 ? -p : p;
2501 pb = pc < 0 ? -pc : pc;
2502 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002503#endif
2504
2505 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2506
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002507 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002508 }
2509 best_row = png_ptr->paeth_row;
2510 }
2511
2512 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002513 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002514 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002515 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 int v;
2518
2519#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2520 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2521 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002522 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002523 png_uint_32 lmhi, lmlo;
2524 lmlo = lmins & PNG_LOMASK;
2525 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2526
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002527 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002528 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002529 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002530 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002531 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002532 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002533 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002534 PNG_WEIGHT_SHIFT;
2535 }
2536 }
2537
2538 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2539 PNG_COST_SHIFT;
2540 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2541 PNG_COST_SHIFT;
2542
2543 if (lmhi > PNG_HIMASK)
2544 lmins = PNG_MAXSUM;
2545 else
2546 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2547 }
2548#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002549
2550 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002551 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002552 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002553 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002554
2555 sum += (v < 128) ? v : 256 - v;
2556 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002557
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002558 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002559 {
2560 int a, b, c, pa, pb, pc, p;
2561
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002562 b = *pp++;
2563 c = *cp++;
2564 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002565
2566#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002567 p = b - c;
2568 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002569#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002570 pa = abs(p);
2571 pb = abs(pc);
2572 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002573#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002574 pa = p < 0 ? -p : p;
2575 pb = pc < 0 ? -pc : pc;
2576 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002577#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002578 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2579#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002581 pa = abs(p - a);
2582 pb = abs(p - b);
2583 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002584 if (pa <= pb && pa <= pc)
2585 p = a;
2586 else if (pb <= pc)
2587 p = b;
2588 else
2589 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002590#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002591
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002593
2594 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002595
2596 if (sum > lmins) /* We are already worse, don't continue. */
2597 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002598 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002599
2600#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2601 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2602 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002603 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002604 png_uint_32 sumhi, sumlo;
2605 sumlo = sum & PNG_LOMASK;
2606 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2607
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002608 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002609 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002610 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002611 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002612 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002613 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002614 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002615 PNG_WEIGHT_SHIFT;
2616 }
2617 }
2618
2619 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2620 PNG_COST_SHIFT;
2621 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2622 PNG_COST_SHIFT;
2623
2624 if (sumhi > PNG_HIMASK)
2625 sum = PNG_MAXSUM;
2626 else
2627 sum = (sumhi << PNG_HISHIFT) + sumlo;
2628 }
2629#endif
2630
Guy Schalnate5a37791996-06-05 15:50:50 -05002631 if (sum < mins)
2632 {
2633 best_row = png_ptr->paeth_row;
2634 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002635 }
2636
Andreas Dilger47a0c421997-05-16 02:46:07 -05002637 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002638
Guy Schalnate5a37791996-06-05 15:50:50 -05002639 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002640
2641#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2642 /* Save the type of filter we picked this time for future calculations */
2643 if (png_ptr->num_prev_filters > 0)
2644 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002645 int j;
2646 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002647 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002648 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002649 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002650 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002651 }
2652#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002653}
2654
2655
Andreas Dilger47a0c421997-05-16 02:46:07 -05002656/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002657void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002658png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2659{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002660 png_debug(1, "in png_write_filtered_row\n");
2661 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002662 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002663
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002664 png_ptr->zstream.next_in = filtered_row;
2665 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002666 /* repeat until we have compressed all the data */
2667 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002668 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002669 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002670
Guy Schalnate5a37791996-06-05 15:50:50 -05002671 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002672 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002673 /* check for compression errors */
2674 if (ret != Z_OK)
2675 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002676 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002677 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002678 else
2679 png_error(png_ptr, "zlib error");
2680 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002681
Guy Schalnate5a37791996-06-05 15:50:50 -05002682 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002683 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002684 {
2685 /* write the IDAT and reset the zlib output buffer */
2686 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002687 png_ptr->zstream.next_out = png_ptr->zbuf;
2688 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002689 }
2690 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002691 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002692
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002693 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002694 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002695 {
2696 png_bytep tptr;
2697
2698 tptr = png_ptr->prev_row;
2699 png_ptr->prev_row = png_ptr->row_buf;
2700 png_ptr->row_buf = tptr;
2701 }
2702
Guy Schalnate5a37791996-06-05 15:50:50 -05002703 /* finish row - updates counters and flushes zlib if last row */
2704 png_write_finish_row(png_ptr);
2705
2706#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2707 png_ptr->flush_rows++;
2708
2709 if (png_ptr->flush_dist > 0 &&
2710 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002711 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002712 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002713 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002714#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002715}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002716#endif /* PNG_WRITE_SUPPORTED */