blob: 8f9b7f5e981a0a87bbc45194bbdc823211763229 [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-Pehrsond60b8fa2006-04-20 21:31:14 -05004 * Last changed in libpng 1.4.0 April 20, 2006
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06006 * Copyright (c) 1998-2006 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#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050012#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -050013#include "pngintrn.h"
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-Pehrson9c3ab682006-02-20 22:09:05 -060019void PNGAPI
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-Pehrson86dc9812006-05-10 07:27:44 -050028#if defined(PNG_SAVE_INT_32_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-Pehrson9c3ab682006-02-20 22:09:05 -060033void PNGAPI
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}
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050041#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050042
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-Pehrson9c3ab682006-02-20 22:09:05 -060047void PNGAPI
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
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600164 comp->num_output_ptr = 0;
165 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600166 comp->output_ptr = NULL;
167 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600168 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600169
170 /* we may just want to pass the text right through */
171 if (compression == PNG_TEXT_COMPRESSION_NONE)
172 {
173 comp->input = text;
174 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500175 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600176 }
177
178 if (compression >= PNG_TEXT_COMPRESSION_LAST)
179 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500180#ifndef PNG_NO_STDIO
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600181 char msg[50];
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500182 png_sprintf(msg, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600183 png_warning(png_ptr, msg);
184#else
185 png_warning(png_ptr, "Unknown compression type");
186#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600187 }
188
189 /* We can't write the chunk until we find out how much data we have,
190 * which means we need to run the compressor first and save the
191 * output. This shouldn't be a problem, as the vast majority of
192 * comments should be reasonable, but we will set up an array of
193 * malloc'd pointers to be sure.
194 *
195 * If we knew the application was well behaved, we could simplify this
196 * greatly by assuming we can always malloc an output buffer large
197 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
198 * and malloc this directly. The only time this would be a bad idea is
199 * if we can't malloc more than 64K and we have 64K of random input
200 * data, or if the input string is incredibly large (although this
201 * wouldn't cause a failure, just a slowdown due to swapping).
202 */
203
204 /* set up the compression buffers */
205 png_ptr->zstream.avail_in = (uInt)text_len;
206 png_ptr->zstream.next_in = (Bytef *)text;
207 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
208 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
209
210 /* this is the same compression loop as in png_write_row() */
211 do
212 {
213 /* compress the data */
214 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
215 if (ret != Z_OK)
216 {
217 /* error */
218 if (png_ptr->zstream.msg != NULL)
219 png_error(png_ptr, png_ptr->zstream.msg);
220 else
221 png_error(png_ptr, "zlib error");
222 }
223 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600224 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225 {
226 /* make sure the output array has room */
227 if (comp->num_output_ptr >= comp->max_output_ptr)
228 {
229 int old_max;
230
231 old_max = comp->max_output_ptr;
232 comp->max_output_ptr = comp->num_output_ptr + 4;
233 if (comp->output_ptr != NULL)
234 {
235 png_charpp old_ptr;
236
237 old_ptr = comp->output_ptr;
238 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500239 (png_uint_32)(comp->max_output_ptr *
240 png_sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500241 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500242 * png_sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600243 png_free(png_ptr, old_ptr);
244 }
245 else
246 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500247 (png_uint_32)(comp->max_output_ptr *
248 png_sizeof (png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600249 }
250
251 /* save the data */
252 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
253 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500254 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
255 png_ptr->zbuf_size);
256 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600257
258 /* and reset the buffer */
259 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
260 png_ptr->zstream.next_out = png_ptr->zbuf;
261 }
262 /* continue until we don't have any more to compress */
263 } while (png_ptr->zstream.avail_in);
264
265 /* finish the compression */
266 do
267 {
268 /* tell zlib we are finished */
269 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500270
271 if (ret == Z_OK)
272 {
273 /* check to see if we need more room */
274 if (!(png_ptr->zstream.avail_out))
275 {
276 /* check to make sure our output array has room */
277 if (comp->num_output_ptr >= comp->max_output_ptr)
278 {
279 int old_max;
280
281 old_max = comp->max_output_ptr;
282 comp->max_output_ptr = comp->num_output_ptr + 4;
283 if (comp->output_ptr != NULL)
284 {
285 png_charpp old_ptr;
286
287 old_ptr = comp->output_ptr;
288 /* This could be optimized to realloc() */
289 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500290 (png_uint_32)(comp->max_output_ptr *
291 png_sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500292 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500293 old_max * png_sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500294 png_free(png_ptr, old_ptr);
295 }
296 else
297 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500298 (png_uint_32)(comp->max_output_ptr *
299 png_sizeof (png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500300 }
301
302 /* save off the data */
303 comp->output_ptr[comp->num_output_ptr] =
304 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500305 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
306 png_ptr->zbuf_size);
307 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500308
309 /* and reset the buffer pointers */
310 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
311 png_ptr->zstream.next_out = png_ptr->zbuf;
312 }
313 }
314 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600315 {
316 /* we got an error */
317 if (png_ptr->zstream.msg != NULL)
318 png_error(png_ptr, png_ptr->zstream.msg);
319 else
320 png_error(png_ptr, "zlib error");
321 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600322 } while (ret != Z_STREAM_END);
323
324 /* text length is number of buffers plus last buffer */
325 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
326 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
327 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
328
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500329 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600330}
331
332/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500333static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600334png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
335{
336 int i;
337
338 /* handle the no-compression case */
339 if (comp->input)
340 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500341 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
342 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600343 return;
344 }
345
346 /* write saved output buffers, if any */
347 for (i = 0; i < comp->num_output_ptr; i++)
348 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600349 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
350 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500352 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600353 }
354 if (comp->max_output_ptr != 0)
355 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500356 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600357 /* write anything left in zbuf */
358 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
359 png_write_chunk_data(png_ptr, png_ptr->zbuf,
360 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
361
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600362 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600364 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365}
366#endif
367
Guy Schalnat0d580581995-07-20 02:43:20 -0500368/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600369 * information. Note that the rest of this code depends upon this
370 * information being correct.
371 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500372void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600373png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 int bit_depth, int color_type, int compression_type, int filter_type,
375 int interlace_type)
376{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600377#ifdef PNG_USE_LOCAL_ARRAYS
378 PNG_IHDR;
379#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500380 png_byte buf[13]; /* buffer to store the IHDR info */
381
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500383 /* Check that we have valid input data from the application info */
384 switch (color_type)
385 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500387 switch (bit_depth)
388 {
389 case 1:
390 case 2:
391 case 4:
392 case 8:
393 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500395 }
396 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500398 if (bit_depth != 8 && bit_depth != 16)
399 png_error(png_ptr, "Invalid bit depth for RGB image");
400 png_ptr->channels = 3;
401 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500403 switch (bit_depth)
404 {
405 case 1:
406 case 2:
407 case 4:
408 case 8: png_ptr->channels = 1; break;
409 default: png_error(png_ptr, "Invalid bit depth for paletted image");
410 }
411 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500412 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500413 if (bit_depth != 8 && bit_depth != 16)
414 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
415 png_ptr->channels = 2;
416 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 if (bit_depth != 8 && bit_depth != 16)
419 png_error(png_ptr, "Invalid bit depth for RGBA image");
420 png_ptr->channels = 4;
421 break;
422 default:
423 png_error(png_ptr, "Invalid image color type specified");
424 }
425
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500427 {
428 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500430 }
431
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600432 /* Write filter_method 64 (intrapixel differencing) only if
433 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
434 * 2. Libpng did not write a PNG signature (this filter_method is only
435 * used in PNG datastreams that are embedded in MNG datastreams) and
436 * 3. The application called png_permit_mng_features with a mask that
437 * included PNG_FLAG_MNG_FILTER_64 and
438 * 4. The filter_method is 64 and
439 * 5. The color_type is RGB or RGBA
440 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600441 if (
442#if defined(PNG_MNG_FEATURES_SUPPORTED)
443 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600444 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500445 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600446 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600447 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
448#endif
449 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500450 {
451 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500453 }
454
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600455#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 if (interlace_type != PNG_INTERLACE_NONE &&
457 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500458 {
459 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500460 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500461 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600462#else
463 interlace_type=PNG_INTERLACE_NONE;
464#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500465
466 /* save off the relevent information */
467 png_ptr->bit_depth = (png_byte)bit_depth;
468 png_ptr->color_type = (png_byte)color_type;
469 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500470#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600471 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500472#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500473 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500474 png_ptr->width = width;
475 png_ptr->height = height;
476
477 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500478 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 /* set the usr info, so any transformations can modify it */
480 png_ptr->usr_width = png_ptr->width;
481 png_ptr->usr_bit_depth = png_ptr->bit_depth;
482 png_ptr->usr_channels = png_ptr->channels;
483
Guy Schalnat0d580581995-07-20 02:43:20 -0500484 /* pack the header information into the buffer */
485 png_save_uint_32(buf, width);
486 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600487 buf[8] = (png_byte)bit_depth;
488 buf[9] = (png_byte)color_type;
489 buf[10] = (png_byte)compression_type;
490 buf[11] = (png_byte)filter_type;
491 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500492
493 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600494 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500495
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600497 png_ptr->zstream.zalloc = png_zalloc;
498 png_ptr->zstream.zfree = png_zfree;
499 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500500 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500501 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500502 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
503 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500505 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500507 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500509 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500511 png_ptr->zlib_strategy = Z_FILTERED;
512 else
513 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
514 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500516 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500517 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500518 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500519 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500520 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500521 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500522 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600523 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500524 png_ptr->zlib_method, png_ptr->zlib_window_bits,
525 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600526 png_ptr->zstream.next_out = png_ptr->zbuf;
527 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600528 /* libpng is not interested in zstream.data_type */
529 /* set it to a predefined value, to avoid its evaluation inside zlib */
530 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500531
Guy Schalnate5a37791996-06-05 15:50:50 -0500532 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500533}
534
535/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500536 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600537 * structure.
538 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500539void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500541{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600542#ifdef PNG_USE_LOCAL_ARRAYS
543 PNG_PLTE;
544#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600546 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500547 png_byte buf[3];
548
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500550 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500551#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600552 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500553#endif
554 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500555 {
556 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500557 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500558 png_error(png_ptr, "Invalid number of colors in palette");
559 }
560 else
561 {
562 png_warning(png_ptr, "Invalid number of colors in palette");
563 return;
564 }
565 }
566
567 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
568 {
569 png_warning(png_ptr,
570 "Ignoring request to write a PLTE chunk in grayscale PNG");
571 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500572 }
573
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 png_ptr->num_palette = (png_uint_16)num_pal;
575 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500576
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600577 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500578#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500580 {
581 buf[0] = pal_ptr->red;
582 buf[1] = pal_ptr->green;
583 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500584 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500585 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500586#else
587 /* This is a little slower but some buggy compilers need to do this instead */
588 pal_ptr=palette;
589 for (i = 0; i < num_pal; i++)
590 {
591 buf[0] = pal_ptr[i].red;
592 buf[1] = pal_ptr[i].green;
593 buf[2] = pal_ptr[i].blue;
594 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
595 }
596#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500597 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500598 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500599}
600
601/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500602void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500604{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600605#ifdef PNG_USE_LOCAL_ARRAYS
606 PNG_IDAT;
607#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500609
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500610 /* Optimize the CMF field in the zlib stream. */
611 /* This hack of the zlib stream is compliant to the stream specification. */
612 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
613 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
614 {
615 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
616 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
617 {
618 /* Avoid memory underflows and multiplication overflows. */
619 /* The conditions below are practically always satisfied;
620 however, they still must be checked. */
621 if (length >= 2 &&
622 png_ptr->height < 16384 && png_ptr->width < 16384)
623 {
624 png_uint_32 uncompressed_idat_size = png_ptr->height *
625 ((png_ptr->width *
626 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
627 unsigned int z_cinfo = z_cmf >> 4;
628 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
629 while (uncompressed_idat_size <= half_z_window_size &&
630 half_z_window_size >= 256)
631 {
632 z_cinfo--;
633 half_z_window_size >>= 1;
634 }
635 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
636 if (data[0] != (png_byte)z_cmf)
637 {
638 data[0] = (png_byte)z_cmf;
639 data[1] &= 0xe0;
640 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
641 }
642 }
643 }
644 else
645 png_error(png_ptr,
646 "Invalid zlib compression method or flags in IDAT");
647 }
648
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600649 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500650 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500651}
652
653/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500654void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600655png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500656{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600657#ifdef PNG_USE_LOCAL_ARRAYS
658 PNG_IEND;
659#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500661 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500662 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600663 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500664}
665
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500666#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500667/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600668#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500669void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500671{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600672#ifdef PNG_USE_LOCAL_ARRAYS
673 PNG_gAMA;
674#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500675 png_uint_32 igamma;
676 png_byte buf[4];
677
Andreas Dilger47a0c421997-05-16 02:46:07 -0500678 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600679 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600680 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500681 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600682 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500683}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500684#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600685#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500686void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600687png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600688{
689#ifdef PNG_USE_LOCAL_ARRAYS
690 PNG_gAMA;
691#endif
692 png_byte buf[4];
693
694 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600695 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500696 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600697 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
698}
699#endif
700#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500701
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600702#if defined(PNG_WRITE_sRGB_SUPPORTED)
703/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500704void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600705png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600706{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600707#ifdef PNG_USE_LOCAL_ARRAYS
708 PNG_sRGB;
709#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600710 png_byte buf[1];
711
712 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600713 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600714 png_warning(png_ptr,
715 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600716 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600717 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600718}
719#endif
720
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600721#if defined(PNG_WRITE_iCCP_SUPPORTED)
722/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500723void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600724png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
725 png_charp profile, int profile_len)
726{
727#ifdef PNG_USE_LOCAL_ARRAYS
728 PNG_iCCP;
729#endif
730 png_size_t name_len;
731 png_charp new_name;
732 compression_state comp;
733
734 png_debug(1, "in png_write_iCCP\n");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600735
736 comp.num_output_ptr = 0;
737 comp.max_output_ptr = 0;
738 comp.output_ptr = NULL;
739 comp.input = NULL;
740 comp.input_len = 0;
741
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600742 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
743 &new_name)) == 0)
744 {
745 png_warning(png_ptr, "Empty keyword in iCCP chunk");
746 return;
747 }
748
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600749 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600750 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600751
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500752 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600753 profile_len = 0;
754
755 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500756 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600757 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600758
759 /* make sure we include the NULL after the name and the compression type */
760 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
761 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600762 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600763 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
764
765 if (profile_len)
766 png_write_compressed_data_out(png_ptr, &comp);
767
768 png_write_chunk_end(png_ptr);
769 png_free(png_ptr, new_name);
770}
771#endif
772
773#if defined(PNG_WRITE_sPLT_SUPPORTED)
774/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600776png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600777{
778#ifdef PNG_USE_LOCAL_ARRAYS
779 PNG_sPLT;
780#endif
781 png_size_t name_len;
782 png_charp new_name;
783 png_byte entrybuf[10];
784 int entry_size = (spalette->depth == 8 ? 6 : 10);
785 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600786 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500787#ifdef PNG_NO_POINTER_INDEXING
788 int i;
789#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600790
791 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600792 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
793 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600794 {
795 png_warning(png_ptr, "Empty keyword in sPLT chunk");
796 return;
797 }
798
799 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500800 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600801 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600802 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600803 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600804
805 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500806#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600807 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
808 {
809 if (spalette->depth == 8)
810 {
811 entrybuf[0] = (png_byte)ep->red;
812 entrybuf[1] = (png_byte)ep->green;
813 entrybuf[2] = (png_byte)ep->blue;
814 entrybuf[3] = (png_byte)ep->alpha;
815 png_save_uint_16(entrybuf + 4, ep->frequency);
816 }
817 else
818 {
819 png_save_uint_16(entrybuf + 0, ep->red);
820 png_save_uint_16(entrybuf + 2, ep->green);
821 png_save_uint_16(entrybuf + 4, ep->blue);
822 png_save_uint_16(entrybuf + 6, ep->alpha);
823 png_save_uint_16(entrybuf + 8, ep->frequency);
824 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500825 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600826 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500827#else
828 ep=spalette->entries;
829 for (i=0; i>spalette->nentries; i++)
830 {
831 if (spalette->depth == 8)
832 {
833 entrybuf[0] = (png_byte)ep[i].red;
834 entrybuf[1] = (png_byte)ep[i].green;
835 entrybuf[2] = (png_byte)ep[i].blue;
836 entrybuf[3] = (png_byte)ep[i].alpha;
837 png_save_uint_16(entrybuf + 4, ep[i].frequency);
838 }
839 else
840 {
841 png_save_uint_16(entrybuf + 0, ep[i].red);
842 png_save_uint_16(entrybuf + 2, ep[i].green);
843 png_save_uint_16(entrybuf + 4, ep[i].blue);
844 png_save_uint_16(entrybuf + 6, ep[i].alpha);
845 png_save_uint_16(entrybuf + 8, ep[i].frequency);
846 }
847 png_write_chunk_data(png_ptr, entrybuf, entry_size);
848 }
849#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600850
851 png_write_chunk_end(png_ptr);
852 png_free(png_ptr, new_name);
853}
854#endif
855
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500856#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500857/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500858void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600859png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500860{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600861#ifdef PNG_USE_LOCAL_ARRAYS
862 PNG_sBIT;
863#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500864 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500866
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600868 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500869 if (color_type & PNG_COLOR_MASK_COLOR)
870 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600871 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500872
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500873 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
874 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600875 if (sbit->red == 0 || sbit->red > maxbits ||
876 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500877 sbit->blue == 0 || sbit->blue > maxbits)
878 {
879 png_warning(png_ptr, "Invalid sBIT depth specified");
880 return;
881 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 buf[0] = sbit->red;
883 buf[1] = sbit->green;
884 buf[2] = sbit->blue;
885 size = 3;
886 }
887 else
888 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500889 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
890 {
891 png_warning(png_ptr, "Invalid sBIT depth specified");
892 return;
893 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500894 buf[0] = sbit->gray;
895 size = 1;
896 }
897
898 if (color_type & PNG_COLOR_MASK_ALPHA)
899 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500900 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
901 {
902 png_warning(png_ptr, "Invalid sBIT depth specified");
903 return;
904 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500905 buf[size++] = sbit->alpha;
906 }
907
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600908 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500909}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500910#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500911
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500912#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500913/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600914#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500915void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600917 double red_x, double red_y, double green_x, double green_y,
918 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500919{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600920#ifdef PNG_USE_LOCAL_ARRAYS
921 PNG_cHRM;
922#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500923 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600924 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500925
Andreas Dilger47a0c421997-05-16 02:46:07 -0500926 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600927 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500928 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
929 white_x + white_y > 1.0)
930 {
931 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500932#if !defined(PNG_NO_CONSOLE_IO)
933 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600934#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500935 return;
936 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600937 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500938 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600939 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500940 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500941
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600942 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500943 {
944 png_warning(png_ptr, "Invalid cHRM red point specified");
945 return;
946 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600947 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500948 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600949 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500950 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500951
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600952 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500953 {
954 png_warning(png_ptr, "Invalid cHRM green point specified");
955 return;
956 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600957 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500958 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600959 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500960 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500961
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600962 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500963 {
964 png_warning(png_ptr, "Invalid cHRM blue point specified");
965 return;
966 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600967 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500968 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600969 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500970 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500971
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600972 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500973}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500974#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600975#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500976void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600977png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
978 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
979 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
980 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600981{
982#ifdef PNG_USE_LOCAL_ARRAYS
983 PNG_cHRM;
984#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600985 png_byte buf[32];
986
987 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600988 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
990 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600991 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500992#if !defined(PNG_NO_CONSOLE_IO)
993 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600994#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600995 return;
996 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500997 png_save_uint_32(buf, (png_uint_32)white_x);
998 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600999
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001000 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001002 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003 return;
1004 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001005 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1006 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001007
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001008 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001010 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011 return;
1012 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001013 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1014 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001015
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001016 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001017 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001018 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001019 return;
1020 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001021 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1022 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001023
1024 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1025}
1026#endif
1027#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001028
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001029#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001030/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001031void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001032png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001033 int num_trans, int color_type)
1034{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001035#ifdef PNG_USE_LOCAL_ARRAYS
1036 PNG_tRNS;
1037#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001038 png_byte buf[6];
1039
Andreas Dilger47a0c421997-05-16 02:46:07 -05001040 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001041 if (color_type == PNG_COLOR_TYPE_PALETTE)
1042 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001043 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001044 {
1045 png_warning(png_ptr,"Invalid number of transparent colors specified");
1046 return;
1047 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001048 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001049 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001050 }
1051 else if (color_type == PNG_COLOR_TYPE_GRAY)
1052 {
1053 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001054 if(tran->gray >= (1 << png_ptr->bit_depth))
1055 {
1056 png_warning(png_ptr,
1057 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1058 return;
1059 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001060 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001061 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001062 }
1063 else if (color_type == PNG_COLOR_TYPE_RGB)
1064 {
1065 /* three 16 bit values */
1066 png_save_uint_16(buf, tran->red);
1067 png_save_uint_16(buf + 2, tran->green);
1068 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001069 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1070 {
1071 png_warning(png_ptr,
1072 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1073 return;
1074 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001075 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001076 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001077 else
1078 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001079 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001080 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001081}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001082#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001083
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001084#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001085/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001086void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001087png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001088{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001089#ifdef PNG_USE_LOCAL_ARRAYS
1090 PNG_bKGD;
1091#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001092 png_byte buf[6];
1093
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001095 if (color_type == PNG_COLOR_TYPE_PALETTE)
1096 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001097 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001098#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001099 (png_ptr->num_palette ||
1100 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001101#endif
1102 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001103 {
1104 png_warning(png_ptr, "Invalid background palette index");
1105 return;
1106 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001107 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001108 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001109 }
1110 else if (color_type & PNG_COLOR_MASK_COLOR)
1111 {
1112 png_save_uint_16(buf, back->red);
1113 png_save_uint_16(buf + 2, back->green);
1114 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001115 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1116 {
1117 png_warning(png_ptr,
1118 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1119 return;
1120 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001121 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001122 }
1123 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001124 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001125 if(back->gray >= (1 << png_ptr->bit_depth))
1126 {
1127 png_warning(png_ptr,
1128 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1129 return;
1130 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001131 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001132 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001133 }
1134}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001135#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001136
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001137#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001138/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001139void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001140png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001141{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001142#ifdef PNG_USE_LOCAL_ARRAYS
1143 PNG_hIST;
1144#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001145 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001146 png_byte buf[3];
1147
Andreas Dilger47a0c421997-05-16 02:46:07 -05001148 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001149 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001150 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001151 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1152 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 png_warning(png_ptr, "Invalid number of histogram entries specified");
1154 return;
1155 }
1156
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001157 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001159 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001160 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001162 }
1163 png_write_chunk_end(png_ptr);
1164}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001165#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001166
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001167#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1168 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001169/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1170 * and if invalid, correct the keyword rather than discarding the entire
1171 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1172 * length, forbids leading or trailing whitespace, multiple internal spaces,
1173 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174 *
1175 * The new_key is allocated to hold the corrected keyword and must be freed
1176 * by the calling routine. This avoids problems with trying to write to
1177 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001178 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001179png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001180png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001181{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001182 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001183 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001184 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001185 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001186
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 png_debug(1, "in png_check_keyword\n");
1188 *new_key = NULL;
1189
1190 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001191 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001192 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001193 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001194 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001195
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1197
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001198 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1199 if (*new_key == NULL)
1200 {
1201 png_warning(png_ptr, "Out of memory while procesing keyword");
1202 return ((png_size_t)0);
1203 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001204
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001205 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001206 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001208 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001209 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001210#ifndef PNG_NO_STDIO
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001212
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001213 png_sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001214 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001215#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001216 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001217#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001218 *dp = ' ';
1219 }
1220 else
1221 {
1222 *dp = *kp;
1223 }
1224 }
1225 *dp = '\0';
1226
1227 /* Remove any trailing white space. */
1228 kp = *new_key + key_len - 1;
1229 if (*kp == ' ')
1230 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001231 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232
1233 while (*kp == ' ')
1234 {
1235 *(kp--) = '\0';
1236 key_len--;
1237 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001238 }
1239
1240 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241 kp = *new_key;
1242 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001243 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001244 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001245
1246 while (*kp == ' ')
1247 {
1248 kp++;
1249 key_len--;
1250 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001251 }
1252
Andreas Dilger47a0c421997-05-16 02:46:07 -05001253 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254
Andreas Dilger47a0c421997-05-16 02:46:07 -05001255 /* Remove multiple internal spaces. */
1256 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001258 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001259 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001260 *(dp++) = *kp;
1261 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001262 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001264 {
1265 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001266 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001267 }
1268 else
1269 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 *(dp++) = *kp;
1271 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001272 }
1273 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001274 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001275 if(kwarn)
1276 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001277
1278 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001279 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001280 png_free(png_ptr, *new_key);
1281 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001282 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001283 }
1284
1285 if (key_len > 79)
1286 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001287 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001288 new_key[79] = '\0';
1289 key_len = 79;
1290 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001291
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001292 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001293}
1294#endif
1295
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001296#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001297/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001298void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001299png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001300 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001301{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001302#ifdef PNG_USE_LOCAL_ARRAYS
1303 PNG_tEXt;
1304#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001305 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001306 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001307
Andreas Dilger47a0c421997-05-16 02:46:07 -05001308 png_debug(1, "in png_write_tEXt\n");
1309 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1310 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001311 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001312 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001314
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001316 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001317 else
1318 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001319
1320 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001321 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 -05001322 /*
1323 * We leave it to the application to meet PNG-1.0 requirements on the
1324 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1325 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001326 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001327 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001328 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001329 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001330 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001331
Guy Schalnat0d580581995-07-20 02:43:20 -05001332 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001333 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001334}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001335#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001336
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001337#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001339void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001340png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001342{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001343#ifdef PNG_USE_LOCAL_ARRAYS
1344 PNG_zTXt;
1345#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001346 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001347 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001348 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001349 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001350
Andreas Dilger47a0c421997-05-16 02:46:07 -05001351 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001352
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001353 comp.num_output_ptr = 0;
1354 comp.max_output_ptr = 0;
1355 comp.output_ptr = NULL;
1356 comp.input = NULL;
1357 comp.input_len = 0;
1358
Andreas Dilger47a0c421997-05-16 02:46:07 -05001359 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001360 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001361 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001362 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001363 }
1364
Andreas Dilger47a0c421997-05-16 02:46:07 -05001365 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1366 {
1367 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1368 png_free(png_ptr, new_key);
1369 return;
1370 }
1371
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001372 text_len = png_strlen(text);
1373
Andreas Dilger47a0c421997-05-16 02:46:07 -05001374 png_free(png_ptr, new_key);
1375
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001376 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001377 text_len = png_text_compress(png_ptr, text, text_len, compression,
1378 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001379
1380 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001381 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1382 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001383 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001384 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001385 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001386 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001387 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001388 /* write the compressed data */
1389 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001390
Guy Schalnat0d580581995-07-20 02:43:20 -05001391 /* close the chunk */
1392 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001393}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001394#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001395
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001396#if defined(PNG_WRITE_iTXt_SUPPORTED)
1397/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001398void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001399png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001400 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001401{
1402#ifdef PNG_USE_LOCAL_ARRAYS
1403 PNG_iTXt;
1404#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001405 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001406 png_charp new_lang, new_key;
1407 png_byte cbuf[2];
1408 compression_state comp;
1409
1410 png_debug(1, "in png_write_iTXt\n");
1411
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001412 comp.num_output_ptr = 0;
1413 comp.max_output_ptr = 0;
1414 comp.output_ptr = NULL;
1415 comp.input = NULL;
1416
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001417 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1418 {
1419 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1420 return;
1421 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001422 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001423 {
1424 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001425 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001426 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001427 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001428
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001429 if (lang_key == NULL)
1430 lang_key_len = 0;
1431 else
1432 lang_key_len = png_strlen(lang_key);
1433
1434 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001435 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001436 else
1437 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001438
1439 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001440 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1441 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001442
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001443
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001444 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001445 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001446
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001447 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1448 (png_uint_32)(
1449 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1450 + key_len
1451 + lang_len
1452 + lang_key_len
1453 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001454
1455 /*
1456 * We leave it to the application to meet PNG-1.0 requirements on the
1457 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1458 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1459 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1460 */
1461 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001462
1463 /* set the compression flag */
1464 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1465 compression == PNG_TEXT_COMPRESSION_NONE)
1466 cbuf[0] = 0;
1467 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1468 cbuf[0] = 1;
1469 /* set the compression method */
1470 cbuf[1] = 0;
1471 png_write_chunk_data(png_ptr, cbuf, 2);
1472
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001473 cbuf[0] = 0;
1474 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1475 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 -06001476 png_write_compressed_data_out(png_ptr, &comp);
1477
1478 png_write_chunk_end(png_ptr);
1479 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001480 if (new_lang)
1481 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001482}
1483#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001484
1485#if defined(PNG_WRITE_oFFs_SUPPORTED)
1486/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001487void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001488png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001489 int unit_type)
1490{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001491#ifdef PNG_USE_LOCAL_ARRAYS
1492 PNG_oFFs;
1493#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001494 png_byte buf[9];
1495
1496 png_debug(1, "in png_write_oFFs\n");
1497 if (unit_type >= PNG_OFFSET_LAST)
1498 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1499
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001500 png_save_int_32(buf, x_offset);
1501 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001502 buf[8] = (png_byte)unit_type;
1503
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001504 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001505}
1506#endif
1507
1508#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001509/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001510void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001511png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1512 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1513{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001514#ifdef PNG_USE_LOCAL_ARRAYS
1515 PNG_pCAL;
1516#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001517 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001518 png_uint_32p params_len;
1519 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001520 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001521 int i;
1522
1523 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1524 if (type >= PNG_EQUATION_LAST)
1525 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1526
1527 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001528 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001529 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001530 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001531 total_len = purpose_len + units_len + 10;
1532
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001533 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001534 *png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001535
1536 /* Find the length of each parameter, making sure we don't count the
1537 null terminator for the last parameter. */
1538 for (i = 0; i < nparams; i++)
1539 {
1540 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001541 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001542 total_len += (png_size_t)params_len[i];
1543 }
1544
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001545 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001546 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001547 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001548 png_save_int_32(buf, X0);
1549 png_save_int_32(buf + 4, X1);
1550 buf[8] = (png_byte)type;
1551 buf[9] = (png_byte)nparams;
1552 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1553 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1554
1555 png_free(png_ptr, new_purpose);
1556
1557 for (i = 0; i < nparams; i++)
1558 {
1559 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1560 (png_size_t)params_len[i]);
1561 }
1562
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001563 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001564 png_write_chunk_end(png_ptr);
1565}
1566#endif
1567
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001568#if defined(PNG_WRITE_sCAL_SUPPORTED)
1569/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001570#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001571void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001572png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001573{
1574#ifdef PNG_USE_LOCAL_ARRAYS
1575 PNG_sCAL;
1576#endif
1577 png_size_t total_len;
1578 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001579 png_byte bunit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001580
1581 png_debug(1, "in png_write_sCAL\n");
1582
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001583 png_sprintf(wbuf, "%12.12e", width);
1584 png_sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001585 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001586
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001587 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001588 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001589 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001590 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1591 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001592
1593 png_write_chunk_end(png_ptr);
1594}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001595#else
1596#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001597void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001598png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001599 png_charp height)
1600{
1601#ifdef PNG_USE_LOCAL_ARRAYS
1602 PNG_sCAL;
1603#endif
1604 png_size_t total_len;
1605 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001606 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001607
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001608 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001609
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001610 png_strcpy(wbuf,(const char *)width);
1611 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001612 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613
1614 png_debug1(3, "sCAL total length = %d\n", total_len);
1615 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001616 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001617 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1618 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001619
1620 png_write_chunk_end(png_ptr);
1621}
1622#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001623#endif
1624#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001625
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001626#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001627/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001628void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001629png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001630 png_uint_32 y_pixels_per_unit,
1631 int unit_type)
1632{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001633#ifdef PNG_USE_LOCAL_ARRAYS
1634 PNG_pHYs;
1635#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001636 png_byte buf[9];
1637
Andreas Dilger47a0c421997-05-16 02:46:07 -05001638 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001639 if (unit_type >= PNG_RESOLUTION_LAST)
1640 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1641
Guy Schalnat0d580581995-07-20 02:43:20 -05001642 png_save_uint_32(buf, x_pixels_per_unit);
1643 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001644 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001645
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001646 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001647}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001648#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001649
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001650#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001651/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1652 * or png_convert_from_time_t(), or fill in the structure yourself.
1653 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001654void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001655png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001656{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001657#ifdef PNG_USE_LOCAL_ARRAYS
1658 PNG_tIME;
1659#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001660 png_byte buf[7];
1661
Andreas Dilger47a0c421997-05-16 02:46:07 -05001662 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001663 if (mod_time->month > 12 || mod_time->month < 1 ||
1664 mod_time->day > 31 || mod_time->day < 1 ||
1665 mod_time->hour > 23 || mod_time->second > 60)
1666 {
1667 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1668 return;
1669 }
1670
Guy Schalnat0d580581995-07-20 02:43:20 -05001671 png_save_uint_16(buf, mod_time->year);
1672 buf[2] = mod_time->month;
1673 buf[3] = mod_time->day;
1674 buf[4] = mod_time->hour;
1675 buf[5] = mod_time->minute;
1676 buf[6] = mod_time->second;
1677
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001678 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001679}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001680#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001681
1682/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001683void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001684png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001685{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001686#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001687 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001688
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001689 /* start of interlace block */
1690 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001691
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001692 /* offset to next interlace block */
1693 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001694
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001695 /* start of interlace block in the y direction */
1696 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001697
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001698 /* offset to next interlace block in the y direction */
1699 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001700#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001701
Andreas Dilger47a0c421997-05-16 02:46:07 -05001702 png_size_t buf_size;
1703
1704 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001705 buf_size = (png_size_t)(PNG_ROWBYTES(
1706 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001707
Guy Schalnat0d580581995-07-20 02:43:20 -05001708 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001709 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001710 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001711
1712 /* set up filtering buffer, if using this filter */
1713 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001714 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001715 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001716 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001717 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001718 }
1719
Andreas Dilger47a0c421997-05-16 02:46:07 -05001720 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001721 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1722 {
1723 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001724 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001725 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001726
1727 if (png_ptr->do_filter & PNG_FILTER_UP)
1728 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001729 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001730 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001731 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001732 }
1733
1734 if (png_ptr->do_filter & PNG_FILTER_AVG)
1735 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001736 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1737 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001738 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001739 }
1740
1741 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1742 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001743 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001744 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001745 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001746 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001747 }
1748
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001749#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001750 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001751 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001752 {
1753 if (!(png_ptr->transformations & PNG_INTERLACE))
1754 {
1755 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1756 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001757 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1758 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001759 }
1760 else
1761 {
1762 png_ptr->num_rows = png_ptr->height;
1763 png_ptr->usr_width = png_ptr->width;
1764 }
1765 }
1766 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001767#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001768 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001769 png_ptr->num_rows = png_ptr->height;
1770 png_ptr->usr_width = png_ptr->width;
1771 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001772 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1773 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001774}
1775
Andreas Dilger47a0c421997-05-16 02:46:07 -05001776/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001777void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001778png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001779{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001780#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001781 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001782
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001783 /* start of interlace block */
1784 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001785
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001786 /* offset to next interlace block */
1787 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001788
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001789 /* start of interlace block in the y direction */
1790 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001791
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001792 /* offset to next interlace block in the y direction */
1793 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001794#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001795
Guy Schalnat0d580581995-07-20 02:43:20 -05001796 int ret;
1797
Andreas Dilger47a0c421997-05-16 02:46:07 -05001798 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001799 /* next row */
1800 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001801
Guy Schalnat0d580581995-07-20 02:43:20 -05001802 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001803 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001804 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001805
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001806#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001807 /* if interlaced, go to next pass */
1808 if (png_ptr->interlaced)
1809 {
1810 png_ptr->row_number = 0;
1811 if (png_ptr->transformations & PNG_INTERLACE)
1812 {
1813 png_ptr->pass++;
1814 }
1815 else
1816 {
1817 /* loop until we find a non-zero width or height pass */
1818 do
1819 {
1820 png_ptr->pass++;
1821 if (png_ptr->pass >= 7)
1822 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001823 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 png_pass_inc[png_ptr->pass] - 1 -
1825 png_pass_start[png_ptr->pass]) /
1826 png_pass_inc[png_ptr->pass];
1827 png_ptr->num_rows = (png_ptr->height +
1828 png_pass_yinc[png_ptr->pass] - 1 -
1829 png_pass_ystart[png_ptr->pass]) /
1830 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001831 if (png_ptr->transformations & PNG_INTERLACE)
1832 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001833 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1834
1835 }
1836
Guy Schalnate5a37791996-06-05 15:50:50 -05001837 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001838 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001839 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001840 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001841 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001842 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1843 png_ptr->usr_bit_depth,png_ptr->width))+1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001844 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001845 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001846 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001847#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001848
1849 /* if we get here, we've just written the last row, so we need
1850 to flush the compressor */
1851 do
1852 {
1853 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001854 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001855 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001856 if (ret == Z_OK)
1857 {
1858 /* check to see if we need more room */
1859 if (!(png_ptr->zstream.avail_out))
1860 {
1861 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1862 png_ptr->zstream.next_out = png_ptr->zbuf;
1863 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1864 }
1865 }
1866 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001867 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001868 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001869 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001870 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001871 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001872 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001873 } while (ret != Z_STREAM_END);
1874
1875 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001876 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001877 {
1878 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001879 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001880 }
1881
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001882 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001883 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001884}
1885
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001886#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001887/* Pick out the correct pixels for the interlace pass.
1888 * The basic idea here is to go through the row with a source
1889 * pointer and a destination pointer (sp and dp), and copy the
1890 * correct pixels for the pass. As the row gets compacted,
1891 * sp will always be >= dp, so we should never overwrite anything.
1892 * See the default: case for the easiest code to understand.
1893 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001894void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001895png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001896{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001897#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001898 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001899
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001900 /* start of interlace block */
1901 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001902
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001903 /* offset to next interlace block */
1904 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001905#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001906
Andreas Dilger47a0c421997-05-16 02:46:07 -05001907 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001908 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001909#if defined(PNG_USELESS_TESTS_SUPPORTED)
1910 if (row != NULL && row_info != NULL && pass < 6)
1911#else
1912 if (pass < 6)
1913#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001914 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001915 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001916 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001917 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001918 case 1:
1919 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001920 png_bytep sp;
1921 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001922 int shift;
1923 int d;
1924 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001925 png_uint_32 i;
1926 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001927
1928 dp = row;
1929 d = 0;
1930 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001931 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 i += png_pass_inc[pass])
1933 {
1934 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001935 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 d |= (value << shift);
1937
1938 if (shift == 0)
1939 {
1940 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001941 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001942 d = 0;
1943 }
1944 else
1945 shift--;
1946
1947 }
1948 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001949 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001950 break;
1951 }
1952 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001953 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001954 png_bytep sp;
1955 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001956 int shift;
1957 int d;
1958 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001959 png_uint_32 i;
1960 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001961
1962 dp = row;
1963 shift = 6;
1964 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001965 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001966 i += png_pass_inc[pass])
1967 {
1968 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001969 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 d |= (value << shift);
1971
1972 if (shift == 0)
1973 {
1974 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001975 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001976 d = 0;
1977 }
1978 else
1979 shift -= 2;
1980 }
1981 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001982 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001983 break;
1984 }
1985 case 4:
1986 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001987 png_bytep sp;
1988 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001989 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001990 int d;
1991 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001992 png_uint_32 i;
1993 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001994
1995 dp = row;
1996 shift = 4;
1997 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001998 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001999 i += png_pass_inc[pass])
2000 {
2001 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002002 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002003 d |= (value << shift);
2004
2005 if (shift == 0)
2006 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002007 shift = 4;
2008 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002009 d = 0;
2010 }
2011 else
2012 shift -= 4;
2013 }
2014 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002015 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002016 break;
2017 }
2018 default:
2019 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002020 png_bytep sp;
2021 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002022 png_uint_32 i;
2023 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002024 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002025
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002026 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002027 dp = row;
2028 /* find out how many bytes each pixel takes up */
2029 pixel_bytes = (row_info->pixel_depth >> 3);
2030 /* loop through the row, only looking at the pixels that
2031 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002032 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002033 i += png_pass_inc[pass])
2034 {
2035 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002036 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002037 /* move the pixel */
2038 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002039 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002040 /* next pixel */
2041 dp += pixel_bytes;
2042 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002043 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002044 }
2045 }
2046 /* set new row width */
2047 row_info->width = (row_info->width +
2048 png_pass_inc[pass] - 1 -
2049 png_pass_start[pass]) /
2050 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002051 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2052 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002053 }
2054}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002055#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002056
Andreas Dilger47a0c421997-05-16 02:46:07 -05002057/* This filters the row, chooses which filter to use, if it has not already
2058 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002059 * chosen filter.
2060 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002061#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002062#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002063#define PNG_LOMASK ((png_uint_32)0xffffL)
2064#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002065void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002066png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002067{
Guy Schalnate5a37791996-06-05 15:50:50 -05002068 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002069 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002070 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002071 png_uint_32 row_bytes = row_info->rowbytes;
2072#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2073 int num_p_filters = (int)png_ptr->num_prev_filters;
2074#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002075
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002077 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002078 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002079
2080 prev_row = png_ptr->prev_row;
2081 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002082 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002083
Andreas Dilger47a0c421997-05-16 02:46:07 -05002084 /* The prediction method we use is to find which method provides the
2085 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002086 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002087 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002088 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002089 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002090 * predictive" method (not implemented yet), which does test compressions
2091 * of lines using different filter methods, and then chooses the
2092 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002093 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002094 *
2095 * GRR 980525: consider also
2096 * (1) minimum sum of absolute differences from running average (i.e.,
2097 * keep running sum of non-absolute differences & count of bytes)
2098 * [track dispersion, too? restart average if dispersion too large?]
2099 * (1b) minimum sum of absolute differences from sliding average, probably
2100 * with window size <= deflate window (usually 32K)
2101 * (2) minimum sum of squared differences from zero or running average
2102 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002103 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002104
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002105
Guy Schalnate5a37791996-06-05 15:50:50 -05002106 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002107 * that has been chosen, as it doesn't actually do anything to the data.
2108 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002109 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002110 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002111 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002112 png_bytep rp;
2113 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002114 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002115 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002116
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002117 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002118 {
2119 v = *rp;
2120 sum += (v < 128) ? v : 256 - v;
2121 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002122
2123#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2124 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2125 {
2126 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002127 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002128 sumlo = sum & PNG_LOMASK;
2129 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2130
2131 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002132 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002133 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002134 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002136 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002138 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139 PNG_WEIGHT_SHIFT;
2140 }
2141 }
2142
2143 /* Factor in the cost of this filter (this is here for completeness,
2144 * but it makes no sense to have a "cost" for the NONE filter, as
2145 * it has the minimum possible computational cost - none).
2146 */
2147 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2148 PNG_COST_SHIFT;
2149 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2150 PNG_COST_SHIFT;
2151
2152 if (sumhi > PNG_HIMASK)
2153 sum = PNG_MAXSUM;
2154 else
2155 sum = (sumhi << PNG_HISHIFT) + sumlo;
2156 }
2157#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002158 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002159 }
2160
Guy Schalnate5a37791996-06-05 15:50:50 -05002161 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002162 if (filter_to_do == PNG_FILTER_SUB)
2163 /* it's the only filter so no testing is needed */
2164 {
2165 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002166 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002167 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2168 i++, rp++, dp++)
2169 {
2170 *dp = *rp;
2171 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002172 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002173 i++, rp++, lp++, dp++)
2174 {
2175 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2176 }
2177 best_row = png_ptr->sub_row;
2178 }
2179
2180 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002181 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002182 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002183 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002184 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002185 int v;
2186
2187#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002188 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002189 * would reduce the sum of this filter, so that we can do the
2190 * early exit comparison without scaling the sum each time.
2191 */
2192 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2193 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002194 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002195 png_uint_32 lmhi, lmlo;
2196 lmlo = lmins & PNG_LOMASK;
2197 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2198
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002199 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002200 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002201 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002202 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002203 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002204 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002205 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002206 PNG_WEIGHT_SHIFT;
2207 }
2208 }
2209
2210 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2211 PNG_COST_SHIFT;
2212 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2213 PNG_COST_SHIFT;
2214
2215 if (lmhi > PNG_HIMASK)
2216 lmins = PNG_MAXSUM;
2217 else
2218 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2219 }
2220#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002221
Guy Schalnate5a37791996-06-05 15:50:50 -05002222 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2223 i++, rp++, dp++)
2224 {
2225 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002226
Guy Schalnate5a37791996-06-05 15:50:50 -05002227 sum += (v < 128) ? v : 256 - v;
2228 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002229 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002230 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002231 {
2232 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002233
Guy Schalnate5a37791996-06-05 15:50:50 -05002234 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002235
2236 if (sum > lmins) /* We are already worse, don't continue. */
2237 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002238 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002239
2240#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2241 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2242 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002243 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002244 png_uint_32 sumhi, sumlo;
2245 sumlo = sum & PNG_LOMASK;
2246 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2247
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002248 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002249 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002250 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002252 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002254 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 PNG_WEIGHT_SHIFT;
2256 }
2257 }
2258
2259 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2260 PNG_COST_SHIFT;
2261 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2262 PNG_COST_SHIFT;
2263
2264 if (sumhi > PNG_HIMASK)
2265 sum = PNG_MAXSUM;
2266 else
2267 sum = (sumhi << PNG_HISHIFT) + sumlo;
2268 }
2269#endif
2270
Guy Schalnate5a37791996-06-05 15:50:50 -05002271 if (sum < mins)
2272 {
2273 mins = sum;
2274 best_row = png_ptr->sub_row;
2275 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002276 }
2277
Guy Schalnate5a37791996-06-05 15:50:50 -05002278 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002279 if (filter_to_do == PNG_FILTER_UP)
2280 {
2281 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002282 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002283
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002284 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002285 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002286 i++, rp++, pp++, dp++)
2287 {
2288 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2289 }
2290 best_row = png_ptr->up_row;
2291 }
2292
2293 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002294 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002295 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002296 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002297 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002298 int v;
2299
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002300
Andreas Dilger47a0c421997-05-16 02:46:07 -05002301#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2302 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2303 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002304 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002305 png_uint_32 lmhi, lmlo;
2306 lmlo = lmins & PNG_LOMASK;
2307 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2308
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002309 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002311 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002312 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002313 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002315 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002316 PNG_WEIGHT_SHIFT;
2317 }
2318 }
2319
2320 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2321 PNG_COST_SHIFT;
2322 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2323 PNG_COST_SHIFT;
2324
2325 if (lmhi > PNG_HIMASK)
2326 lmins = PNG_MAXSUM;
2327 else
2328 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2329 }
2330#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002331
2332 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002333 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002334 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002335 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002336
2337 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002338
2339 if (sum > lmins) /* We are already worse, don't continue. */
2340 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002341 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002342
2343#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2344 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2345 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002346 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002347 png_uint_32 sumhi, sumlo;
2348 sumlo = sum & PNG_LOMASK;
2349 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2350
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002351 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002355 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002357 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 PNG_WEIGHT_SHIFT;
2359 }
2360 }
2361
2362 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2363 PNG_COST_SHIFT;
2364 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2365 PNG_COST_SHIFT;
2366
2367 if (sumhi > PNG_HIMASK)
2368 sum = PNG_MAXSUM;
2369 else
2370 sum = (sumhi << PNG_HISHIFT) + sumlo;
2371 }
2372#endif
2373
Guy Schalnate5a37791996-06-05 15:50:50 -05002374 if (sum < mins)
2375 {
2376 mins = sum;
2377 best_row = png_ptr->up_row;
2378 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002379 }
2380
Guy Schalnate5a37791996-06-05 15:50:50 -05002381 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002382 if (filter_to_do == PNG_FILTER_AVG)
2383 {
2384 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002385 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002386 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002387 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002388 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002389 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002390 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002391 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002392 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002393 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2394 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002395 }
2396 best_row = png_ptr->avg_row;
2397 }
2398
2399 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002400 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002401 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002402 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002403 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002404 int v;
2405
2406#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2407 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2408 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002409 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002410 png_uint_32 lmhi, lmlo;
2411 lmlo = lmins & PNG_LOMASK;
2412 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2413
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002414 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002415 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002416 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002417 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002418 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002419 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002420 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002421 PNG_WEIGHT_SHIFT;
2422 }
2423 }
2424
2425 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2426 PNG_COST_SHIFT;
2427 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2428 PNG_COST_SHIFT;
2429
2430 if (lmhi > PNG_HIMASK)
2431 lmins = PNG_MAXSUM;
2432 else
2433 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2434 }
2435#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002436
2437 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002438 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002439 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002440 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002441
2442 sum += (v < 128) ? v : 256 - v;
2443 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002444 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002445 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002446 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002448
2449 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450
2451 if (sum > lmins) /* We are already worse, don't continue. */
2452 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002453 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002454
2455#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2456 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2457 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002458 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002459 png_uint_32 sumhi, sumlo;
2460 sumlo = sum & PNG_LOMASK;
2461 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2462
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002463 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002464 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002465 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002467 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002469 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002470 PNG_WEIGHT_SHIFT;
2471 }
2472 }
2473
2474 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2475 PNG_COST_SHIFT;
2476 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2477 PNG_COST_SHIFT;
2478
2479 if (sumhi > PNG_HIMASK)
2480 sum = PNG_MAXSUM;
2481 else
2482 sum = (sumhi << PNG_HISHIFT) + sumlo;
2483 }
2484#endif
2485
Guy Schalnate5a37791996-06-05 15:50:50 -05002486 if (sum < mins)
2487 {
2488 mins = sum;
2489 best_row = png_ptr->avg_row;
2490 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002491 }
2492
Andreas Dilger47a0c421997-05-16 02:46:07 -05002493 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002494 if (filter_to_do == PNG_FILTER_PAETH)
2495 {
2496 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002497 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002498 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002499 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002500 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002501 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002502 }
2503
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002504 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002505 {
2506 int a, b, c, pa, pb, pc, p;
2507
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002508 b = *pp++;
2509 c = *cp++;
2510 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002511
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002512 p = b - c;
2513 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002514
2515#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 pa = abs(p);
2517 pb = abs(pc);
2518 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002519#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002520 pa = p < 0 ? -p : p;
2521 pb = pc < 0 ? -pc : pc;
2522 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002523#endif
2524
2525 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2526
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002527 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002528 }
2529 best_row = png_ptr->paeth_row;
2530 }
2531
2532 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002533 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002534 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002535 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002536 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002537 int v;
2538
2539#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2540 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2541 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002542 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002543 png_uint_32 lmhi, lmlo;
2544 lmlo = lmins & PNG_LOMASK;
2545 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2546
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002547 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002548 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002549 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002550 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002551 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002552 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002553 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002554 PNG_WEIGHT_SHIFT;
2555 }
2556 }
2557
2558 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2559 PNG_COST_SHIFT;
2560 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2561 PNG_COST_SHIFT;
2562
2563 if (lmhi > PNG_HIMASK)
2564 lmins = PNG_MAXSUM;
2565 else
2566 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2567 }
2568#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002569
2570 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002571 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002572 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002573 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002574
2575 sum += (v < 128) ? v : 256 - v;
2576 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002577
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002578 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002579 {
2580 int a, b, c, pa, pb, pc, p;
2581
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002582 b = *pp++;
2583 c = *cp++;
2584 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002585
2586#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002587 p = b - c;
2588 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002589#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002590 pa = abs(p);
2591 pb = abs(pc);
2592 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002593#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002594 pa = p < 0 ? -p : p;
2595 pb = pc < 0 ? -pc : pc;
2596 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002597#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002598 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2599#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002600 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002601 pa = abs(p - a);
2602 pb = abs(p - b);
2603 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002604 if (pa <= pb && pa <= pc)
2605 p = a;
2606 else if (pb <= pc)
2607 p = b;
2608 else
2609 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002610#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002611
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002612 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002613
2614 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002615
2616 if (sum > lmins) /* We are already worse, don't continue. */
2617 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002618 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002619
2620#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2621 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2622 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002623 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002624 png_uint_32 sumhi, sumlo;
2625 sumlo = sum & PNG_LOMASK;
2626 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2627
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002628 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002629 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002630 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002631 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002632 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002633 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002634 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002635 PNG_WEIGHT_SHIFT;
2636 }
2637 }
2638
2639 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2640 PNG_COST_SHIFT;
2641 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2642 PNG_COST_SHIFT;
2643
2644 if (sumhi > PNG_HIMASK)
2645 sum = PNG_MAXSUM;
2646 else
2647 sum = (sumhi << PNG_HISHIFT) + sumlo;
2648 }
2649#endif
2650
Guy Schalnate5a37791996-06-05 15:50:50 -05002651 if (sum < mins)
2652 {
2653 best_row = png_ptr->paeth_row;
2654 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002655 }
2656
Andreas Dilger47a0c421997-05-16 02:46:07 -05002657 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002658
Guy Schalnate5a37791996-06-05 15:50:50 -05002659 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002660
2661#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2662 /* Save the type of filter we picked this time for future calculations */
2663 if (png_ptr->num_prev_filters > 0)
2664 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002665 int j;
2666 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002667 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002668 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002669 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002670 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002671 }
2672#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002673}
2674
2675
Andreas Dilger47a0c421997-05-16 02:46:07 -05002676/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002677void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002678png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2679{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002680 png_debug(1, "in png_write_filtered_row\n");
2681 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002682 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002683
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002684 png_ptr->zstream.next_in = filtered_row;
2685 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002686 /* repeat until we have compressed all the data */
2687 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002688 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002689 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002690
Guy Schalnate5a37791996-06-05 15:50:50 -05002691 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002692 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002693 /* check for compression errors */
2694 if (ret != Z_OK)
2695 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002696 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002697 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002698 else
2699 png_error(png_ptr, "zlib error");
2700 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002701
Guy Schalnate5a37791996-06-05 15:50:50 -05002702 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002703 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002704 {
2705 /* write the IDAT and reset the zlib output buffer */
2706 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002707 png_ptr->zstream.next_out = png_ptr->zbuf;
2708 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002709 }
2710 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002711 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002712
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002713 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002714 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002715 {
2716 png_bytep tptr;
2717
2718 tptr = png_ptr->prev_row;
2719 png_ptr->prev_row = png_ptr->row_buf;
2720 png_ptr->row_buf = tptr;
2721 }
2722
Guy Schalnate5a37791996-06-05 15:50:50 -05002723 /* finish row - updates counters and flushes zlib if last row */
2724 png_write_finish_row(png_ptr);
2725
2726#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2727 png_ptr->flush_rows++;
2728
2729 if (png_ptr->flush_dist > 0 &&
2730 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002731 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002732 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002733 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002734#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002735}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002736#endif /* PNG_WRITE_SUPPORTED */