blob: e02d7389995363c81b0ffa72157be108ddd63276 [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
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050054/* Simple function to write the signature. If we have already written
55 * the magic bytes of the signature, or more likely, the PNG stream is
56 * being embedded into another stream and doesn't need its own signature,
57 * we should call png_set_sig_bytes() to tell libpng how many of the
58 * bytes have already been written.
59 */
60void PNGAPI
61png_write_sig(png_structp png_ptr)
62{
63 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
64 /* write the rest of the 8 byte signature */
65 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
66 (png_size_t)8 - png_ptr->sig_bytes);
67 if(png_ptr->sig_bytes < 3)
68 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
69}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * representing the chunk name. The array must be at least 4 bytes in
73 * length, and does not need to be null terminated. To be safe, pass the
74 * pre-defined chunk names here, and if you need a new one, define it
75 * where the others are defined. The length is the length of the data.
76 * All the data must be present. If that is not possible, use the
77 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
78 * functions instead.
79 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050080void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060081png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050082 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050083{
Andreas Dilger47a0c421997-05-16 02:46:07 -050084 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060085 png_write_chunk_data(png_ptr, data, length);
86 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050087}
88
Andreas Dilger47a0c421997-05-16 02:46:07 -050089/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060090 * The total_length is the sum of the lengths of all the data you will be
91 * passing in png_write_chunk_data().
92 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050093void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060094png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
95 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050096{
Andreas Dilger47a0c421997-05-16 02:46:07 -050097 png_byte buf[4];
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050098 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name,
99 (unsigned long) length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100
Guy Schalnat0d580581995-07-20 02:43:20 -0500101 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 png_save_uint_32(buf, length);
103 png_write_data(png_ptr, buf, (png_size_t)4);
104
Guy Schalnat0d580581995-07-20 02:43:20 -0500105 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500106 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 /* reset the crc and run it over the chunk name */
108 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500110}
111
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600113 * Note that multiple calls to this function are allowed, and that the
114 * sum of the lengths from these calls *must* add up to the total_length
115 * given to png_write_chunk_start().
116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500117void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500119{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120 /* write the data, and run the CRC over it */
121 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500122 {
123 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600124 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500125 }
126}
127
Andreas Dilger47a0c421997-05-16 02:46:07 -0500128/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500129void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600130png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500131{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 png_byte buf[4];
133
Guy Schalnat0d580581995-07-20 02:43:20 -0500134 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136
137 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500138}
139
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600140#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600141/*
142 * This pair of functions encapsulates the operation of (a) compressing a
143 * text string, and (b) issuing it later as a series of chunk data writes.
144 * The compression_state structure is shared context for these functions
145 * set up by the caller in order to make the whole mess thread-safe.
146 */
147
148typedef struct
149{
150 char *input; /* the uncompressed input data */
151 int input_len; /* its length */
152 int num_output_ptr; /* number of output pointers used */
153 int max_output_ptr; /* size of output_ptr */
154 png_charpp output_ptr; /* array of pointers to output */
155} compression_state;
156
157/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500158static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600159png_text_compress(png_structp png_ptr,
160 png_charp text, png_size_t text_len, int compression,
161 compression_state *comp)
162{
163 int ret;
164
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600165 comp->num_output_ptr = 0;
166 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600167 comp->output_ptr = NULL;
168 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600169 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600170
171 /* we may just want to pass the text right through */
172 if (compression == PNG_TEXT_COMPRESSION_NONE)
173 {
174 comp->input = text;
175 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500176 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600177 }
178
179 if (compression >= PNG_TEXT_COMPRESSION_LAST)
180 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500181#ifndef PNG_NO_STDIO
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600182 char msg[50];
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500183 png_sprintf(msg, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600184 png_warning(png_ptr, msg);
185#else
186 png_warning(png_ptr, "Unknown compression type");
187#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600188 }
189
190 /* We can't write the chunk until we find out how much data we have,
191 * which means we need to run the compressor first and save the
192 * output. This shouldn't be a problem, as the vast majority of
193 * comments should be reasonable, but we will set up an array of
194 * malloc'd pointers to be sure.
195 *
196 * If we knew the application was well behaved, we could simplify this
197 * greatly by assuming we can always malloc an output buffer large
198 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
199 * and malloc this directly. The only time this would be a bad idea is
200 * if we can't malloc more than 64K and we have 64K of random input
201 * data, or if the input string is incredibly large (although this
202 * wouldn't cause a failure, just a slowdown due to swapping).
203 */
204
205 /* set up the compression buffers */
206 png_ptr->zstream.avail_in = (uInt)text_len;
207 png_ptr->zstream.next_in = (Bytef *)text;
208 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
209 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
210
211 /* this is the same compression loop as in png_write_row() */
212 do
213 {
214 /* compress the data */
215 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
216 if (ret != Z_OK)
217 {
218 /* error */
219 if (png_ptr->zstream.msg != NULL)
220 png_error(png_ptr, png_ptr->zstream.msg);
221 else
222 png_error(png_ptr, "zlib error");
223 }
224 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600225 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600226 {
227 /* make sure the output array has room */
228 if (comp->num_output_ptr >= comp->max_output_ptr)
229 {
230 int old_max;
231
232 old_max = comp->max_output_ptr;
233 comp->max_output_ptr = comp->num_output_ptr + 4;
234 if (comp->output_ptr != NULL)
235 {
236 png_charpp old_ptr;
237
238 old_ptr = comp->output_ptr;
239 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500240 (png_uint_32)(comp->max_output_ptr *
241 png_sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500242 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500243 * png_sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600244 png_free(png_ptr, old_ptr);
245 }
246 else
247 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500248 (png_uint_32)(comp->max_output_ptr *
249 png_sizeof (png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600250 }
251
252 /* save the data */
253 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
254 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500255 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
256 png_ptr->zbuf_size);
257 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600258
259 /* and reset the buffer */
260 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
261 png_ptr->zstream.next_out = png_ptr->zbuf;
262 }
263 /* continue until we don't have any more to compress */
264 } while (png_ptr->zstream.avail_in);
265
266 /* finish the compression */
267 do
268 {
269 /* tell zlib we are finished */
270 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500271
272 if (ret == Z_OK)
273 {
274 /* check to see if we need more room */
275 if (!(png_ptr->zstream.avail_out))
276 {
277 /* check to make sure our output array has room */
278 if (comp->num_output_ptr >= comp->max_output_ptr)
279 {
280 int old_max;
281
282 old_max = comp->max_output_ptr;
283 comp->max_output_ptr = comp->num_output_ptr + 4;
284 if (comp->output_ptr != NULL)
285 {
286 png_charpp old_ptr;
287
288 old_ptr = comp->output_ptr;
289 /* This could be optimized to realloc() */
290 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500291 (png_uint_32)(comp->max_output_ptr *
292 png_sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500293 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500294 old_max * png_sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500295 png_free(png_ptr, old_ptr);
296 }
297 else
298 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500299 (png_uint_32)(comp->max_output_ptr *
300 png_sizeof (png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500301 }
302
303 /* save off the data */
304 comp->output_ptr[comp->num_output_ptr] =
305 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500306 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
307 png_ptr->zbuf_size);
308 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500309
310 /* and reset the buffer pointers */
311 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
312 png_ptr->zstream.next_out = png_ptr->zbuf;
313 }
314 }
315 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600316 {
317 /* we got an error */
318 if (png_ptr->zstream.msg != NULL)
319 png_error(png_ptr, png_ptr->zstream.msg);
320 else
321 png_error(png_ptr, "zlib error");
322 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600323 } while (ret != Z_STREAM_END);
324
325 /* text length is number of buffers plus last buffer */
326 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
327 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
328 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
329
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500330 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600331}
332
333/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500334static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600335png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
336{
337 int i;
338
339 /* handle the no-compression case */
340 if (comp->input)
341 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500342 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
343 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600344 return;
345 }
346
347 /* write saved output buffers, if any */
348 for (i = 0; i < comp->num_output_ptr; i++)
349 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600350 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
351 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600352 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500353 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600354 }
355 if (comp->max_output_ptr != 0)
356 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500357 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600358 /* write anything left in zbuf */
359 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
360 png_write_chunk_data(png_ptr, png_ptr->zbuf,
361 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
362
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600363 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600364 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600365 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366}
367#endif
368
Guy Schalnat0d580581995-07-20 02:43:20 -0500369/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600370 * information. Note that the rest of this code depends upon this
371 * information being correct.
372 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500373void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600374png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500375 int bit_depth, int color_type, int compression_type, int filter_type,
376 int interlace_type)
377{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600378#ifdef PNG_USE_LOCAL_ARRAYS
379 PNG_IHDR;
380#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500381 png_byte buf[13]; /* buffer to store the IHDR info */
382
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500384 /* Check that we have valid input data from the application info */
385 switch (color_type)
386 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 switch (bit_depth)
389 {
390 case 1:
391 case 2:
392 case 4:
393 case 8:
394 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500396 }
397 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500398 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500399 if (bit_depth != 8 && bit_depth != 16)
400 png_error(png_ptr, "Invalid bit depth for RGB image");
401 png_ptr->channels = 3;
402 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500404 switch (bit_depth)
405 {
406 case 1:
407 case 2:
408 case 4:
409 case 8: png_ptr->channels = 1; break;
410 default: png_error(png_ptr, "Invalid bit depth for paletted image");
411 }
412 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500414 if (bit_depth != 8 && bit_depth != 16)
415 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
416 png_ptr->channels = 2;
417 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500418 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500419 if (bit_depth != 8 && bit_depth != 16)
420 png_error(png_ptr, "Invalid bit depth for RGBA image");
421 png_ptr->channels = 4;
422 break;
423 default:
424 png_error(png_ptr, "Invalid image color type specified");
425 }
426
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500428 {
429 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500431 }
432
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600433 /* Write filter_method 64 (intrapixel differencing) only if
434 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
435 * 2. Libpng did not write a PNG signature (this filter_method is only
436 * used in PNG datastreams that are embedded in MNG datastreams) and
437 * 3. The application called png_permit_mng_features with a mask that
438 * included PNG_FLAG_MNG_FILTER_64 and
439 * 4. The filter_method is 64 and
440 * 5. The color_type is RGB or RGBA
441 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600442 if (
443#if defined(PNG_MNG_FEATURES_SUPPORTED)
444 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600445 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500446 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600447 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600448 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
449#endif
450 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500451 {
452 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500454 }
455
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600456#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500457 if (interlace_type != PNG_INTERLACE_NONE &&
458 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500459 {
460 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500461 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500462 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600463#else
464 interlace_type=PNG_INTERLACE_NONE;
465#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500466
467 /* save off the relevent information */
468 png_ptr->bit_depth = (png_byte)bit_depth;
469 png_ptr->color_type = (png_byte)color_type;
470 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500471#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600472 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500473#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500474 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500475 png_ptr->width = width;
476 png_ptr->height = height;
477
478 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500479 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500480 /* set the usr info, so any transformations can modify it */
481 png_ptr->usr_width = png_ptr->width;
482 png_ptr->usr_bit_depth = png_ptr->bit_depth;
483 png_ptr->usr_channels = png_ptr->channels;
484
Guy Schalnat0d580581995-07-20 02:43:20 -0500485 /* pack the header information into the buffer */
486 png_save_uint_32(buf, width);
487 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600488 buf[8] = (png_byte)bit_depth;
489 buf[9] = (png_byte)color_type;
490 buf[10] = (png_byte)compression_type;
491 buf[11] = (png_byte)filter_type;
492 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500493
494 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600495 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500496
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600498 png_ptr->zstream.zalloc = png_zalloc;
499 png_ptr->zstream.zfree = png_zfree;
500 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500501 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500502 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
504 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500505 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500506 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500507 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500508 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500509 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500510 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500511 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500512 png_ptr->zlib_strategy = Z_FILTERED;
513 else
514 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
515 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500516 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500517 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500518 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500519 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500520 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500521 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500522 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500523 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600524 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500525 png_ptr->zlib_method, png_ptr->zlib_window_bits,
526 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600527 png_ptr->zstream.next_out = png_ptr->zbuf;
528 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600529 /* libpng is not interested in zstream.data_type */
530 /* set it to a predefined value, to avoid its evaluation inside zlib */
531 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500532
Guy Schalnate5a37791996-06-05 15:50:50 -0500533 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500534}
535
536/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500537 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600538 * structure.
539 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500540void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500542{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600543#ifdef PNG_USE_LOCAL_ARRAYS
544 PNG_PLTE;
545#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600547 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500548 png_byte buf[3];
549
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500551 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500552#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600553 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500554#endif
555 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500556 {
557 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500558 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500559 png_error(png_ptr, "Invalid number of colors in palette");
560 }
561 else
562 {
563 png_warning(png_ptr, "Invalid number of colors in palette");
564 return;
565 }
566 }
567
568 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
569 {
570 png_warning(png_ptr,
571 "Ignoring request to write a PLTE chunk in grayscale PNG");
572 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500573 }
574
Andreas Dilger47a0c421997-05-16 02:46:07 -0500575 png_ptr->num_palette = (png_uint_16)num_pal;
576 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500577
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600578 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500579#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500581 {
582 buf[0] = pal_ptr->red;
583 buf[1] = pal_ptr->green;
584 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500586 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500587#else
588 /* This is a little slower but some buggy compilers need to do this instead */
589 pal_ptr=palette;
590 for (i = 0; i < num_pal; i++)
591 {
592 buf[0] = pal_ptr[i].red;
593 buf[1] = pal_ptr[i].green;
594 buf[2] = pal_ptr[i].blue;
595 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
596 }
597#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500599 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500600}
601
602/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500603void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500604png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500605{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600606#ifdef PNG_USE_LOCAL_ARRAYS
607 PNG_IDAT;
608#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500609 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500610
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500611 /* Optimize the CMF field in the zlib stream. */
612 /* This hack of the zlib stream is compliant to the stream specification. */
613 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
614 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
615 {
616 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
617 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
618 {
619 /* Avoid memory underflows and multiplication overflows. */
620 /* The conditions below are practically always satisfied;
621 however, they still must be checked. */
622 if (length >= 2 &&
623 png_ptr->height < 16384 && png_ptr->width < 16384)
624 {
625 png_uint_32 uncompressed_idat_size = png_ptr->height *
626 ((png_ptr->width *
627 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
628 unsigned int z_cinfo = z_cmf >> 4;
629 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
630 while (uncompressed_idat_size <= half_z_window_size &&
631 half_z_window_size >= 256)
632 {
633 z_cinfo--;
634 half_z_window_size >>= 1;
635 }
636 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
637 if (data[0] != (png_byte)z_cmf)
638 {
639 data[0] = (png_byte)z_cmf;
640 data[1] &= 0xe0;
641 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
642 }
643 }
644 }
645 else
646 png_error(png_ptr,
647 "Invalid zlib compression method or flags in IDAT");
648 }
649
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600650 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500651 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500652}
653
654/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500655void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600656png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500657{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600658#ifdef PNG_USE_LOCAL_ARRAYS
659 PNG_IEND;
660#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500661 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500662 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500663 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600664 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500665}
666
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500667#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500668/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600669#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500670void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500671png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500672{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600673#ifdef PNG_USE_LOCAL_ARRAYS
674 PNG_gAMA;
675#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 png_uint_32 igamma;
677 png_byte buf[4];
678
Andreas Dilger47a0c421997-05-16 02:46:07 -0500679 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600680 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600681 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500682 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600683 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500684}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500685#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600686#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500687void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600688png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600689{
690#ifdef PNG_USE_LOCAL_ARRAYS
691 PNG_gAMA;
692#endif
693 png_byte buf[4];
694
695 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600696 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500697 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600698 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
699}
700#endif
701#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500702
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600703#if defined(PNG_WRITE_sRGB_SUPPORTED)
704/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500705void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600706png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600707{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600708#ifdef PNG_USE_LOCAL_ARRAYS
709 PNG_sRGB;
710#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600711 png_byte buf[1];
712
713 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600714 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600715 png_warning(png_ptr,
716 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600717 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600718 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600719}
720#endif
721
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600722#if defined(PNG_WRITE_iCCP_SUPPORTED)
723/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500724void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600725png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
726 png_charp profile, int profile_len)
727{
728#ifdef PNG_USE_LOCAL_ARRAYS
729 PNG_iCCP;
730#endif
731 png_size_t name_len;
732 png_charp new_name;
733 compression_state comp;
734
735 png_debug(1, "in png_write_iCCP\n");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600736
737 comp.num_output_ptr = 0;
738 comp.max_output_ptr = 0;
739 comp.output_ptr = NULL;
740 comp.input = NULL;
741 comp.input_len = 0;
742
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600743 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
744 &new_name)) == 0)
745 {
746 png_warning(png_ptr, "Empty keyword in iCCP chunk");
747 return;
748 }
749
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600750 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600751 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600752
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500753 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600754 profile_len = 0;
755
756 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500757 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600758 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600759
760 /* make sure we include the NULL after the name and the compression type */
761 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
762 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600763 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600764 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
765
766 if (profile_len)
767 png_write_compressed_data_out(png_ptr, &comp);
768
769 png_write_chunk_end(png_ptr);
770 png_free(png_ptr, new_name);
771}
772#endif
773
774#if defined(PNG_WRITE_sPLT_SUPPORTED)
775/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500776void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600777png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600778{
779#ifdef PNG_USE_LOCAL_ARRAYS
780 PNG_sPLT;
781#endif
782 png_size_t name_len;
783 png_charp new_name;
784 png_byte entrybuf[10];
785 int entry_size = (spalette->depth == 8 ? 6 : 10);
786 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600787 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500788#ifdef PNG_NO_POINTER_INDEXING
789 int i;
790#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600791
792 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600793 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
794 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600795 {
796 png_warning(png_ptr, "Empty keyword in sPLT chunk");
797 return;
798 }
799
800 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500801 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600802 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600803 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600804 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600805
806 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500807#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600808 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
809 {
810 if (spalette->depth == 8)
811 {
812 entrybuf[0] = (png_byte)ep->red;
813 entrybuf[1] = (png_byte)ep->green;
814 entrybuf[2] = (png_byte)ep->blue;
815 entrybuf[3] = (png_byte)ep->alpha;
816 png_save_uint_16(entrybuf + 4, ep->frequency);
817 }
818 else
819 {
820 png_save_uint_16(entrybuf + 0, ep->red);
821 png_save_uint_16(entrybuf + 2, ep->green);
822 png_save_uint_16(entrybuf + 4, ep->blue);
823 png_save_uint_16(entrybuf + 6, ep->alpha);
824 png_save_uint_16(entrybuf + 8, ep->frequency);
825 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500826 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600827 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500828#else
829 ep=spalette->entries;
830 for (i=0; i>spalette->nentries; i++)
831 {
832 if (spalette->depth == 8)
833 {
834 entrybuf[0] = (png_byte)ep[i].red;
835 entrybuf[1] = (png_byte)ep[i].green;
836 entrybuf[2] = (png_byte)ep[i].blue;
837 entrybuf[3] = (png_byte)ep[i].alpha;
838 png_save_uint_16(entrybuf + 4, ep[i].frequency);
839 }
840 else
841 {
842 png_save_uint_16(entrybuf + 0, ep[i].red);
843 png_save_uint_16(entrybuf + 2, ep[i].green);
844 png_save_uint_16(entrybuf + 4, ep[i].blue);
845 png_save_uint_16(entrybuf + 6, ep[i].alpha);
846 png_save_uint_16(entrybuf + 8, ep[i].frequency);
847 }
848 png_write_chunk_data(png_ptr, entrybuf, entry_size);
849 }
850#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600851
852 png_write_chunk_end(png_ptr);
853 png_free(png_ptr, new_name);
854}
855#endif
856
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500857#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500858/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500859void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600860png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500861{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600862#ifdef PNG_USE_LOCAL_ARRAYS
863 PNG_sBIT;
864#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500865 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500866 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500867
Andreas Dilger47a0c421997-05-16 02:46:07 -0500868 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600869 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500870 if (color_type & PNG_COLOR_MASK_COLOR)
871 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600872 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500873
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500874 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
875 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600876 if (sbit->red == 0 || sbit->red > maxbits ||
877 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500878 sbit->blue == 0 || sbit->blue > maxbits)
879 {
880 png_warning(png_ptr, "Invalid sBIT depth specified");
881 return;
882 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500883 buf[0] = sbit->red;
884 buf[1] = sbit->green;
885 buf[2] = sbit->blue;
886 size = 3;
887 }
888 else
889 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500890 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
891 {
892 png_warning(png_ptr, "Invalid sBIT depth specified");
893 return;
894 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500895 buf[0] = sbit->gray;
896 size = 1;
897 }
898
899 if (color_type & PNG_COLOR_MASK_ALPHA)
900 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500901 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
902 {
903 png_warning(png_ptr, "Invalid sBIT depth specified");
904 return;
905 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500906 buf[size++] = sbit->alpha;
907 }
908
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600909 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500910}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500911#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500912
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500913#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500914/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600915#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500916void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500917png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600918 double red_x, double red_y, double green_x, double green_y,
919 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500920{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600921#ifdef PNG_USE_LOCAL_ARRAYS
922 PNG_cHRM;
923#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500924 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600925 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500926
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600928 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500929 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
930 white_x + white_y > 1.0)
931 {
932 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500933#if !defined(PNG_NO_CONSOLE_IO)
934 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600935#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500936 return;
937 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600938 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500939 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600940 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500941 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500942
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600943 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500944 {
945 png_warning(png_ptr, "Invalid cHRM red point specified");
946 return;
947 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600948 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500949 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600950 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500951 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500952
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600953 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500954 {
955 png_warning(png_ptr, "Invalid cHRM green point specified");
956 return;
957 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600958 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500959 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600960 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500961 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500962
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600963 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500964 {
965 png_warning(png_ptr, "Invalid cHRM blue point specified");
966 return;
967 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600968 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500969 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600970 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500971 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500972
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600973 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500974}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500975#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600976#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500977void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600978png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
979 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
980 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
981 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600982{
983#ifdef PNG_USE_LOCAL_ARRAYS
984 PNG_cHRM;
985#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600986 png_byte buf[32];
987
988 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600989 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600990 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
991 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600992 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500993#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500994 fprintf(stderr,"white_x=%ld, white_y=%ld\n",(unsigned long)white_x,
995 (unsigned long)white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600996#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600997 return;
998 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500999 png_save_uint_32(buf, (png_uint_32)white_x);
1000 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001002 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001004 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005 return;
1006 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001007 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1008 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001010 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001012 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001013 return;
1014 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001015 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1016 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001017
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001018 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001019 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001020 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001021 return;
1022 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001023 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1024 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001025
1026 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1027}
1028#endif
1029#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001030
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001031#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001032/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001033void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001034png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001035 int num_trans, int color_type)
1036{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001037#ifdef PNG_USE_LOCAL_ARRAYS
1038 PNG_tRNS;
1039#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001040 png_byte buf[6];
1041
Andreas Dilger47a0c421997-05-16 02:46:07 -05001042 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001043 if (color_type == PNG_COLOR_TYPE_PALETTE)
1044 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001045 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001046 {
1047 png_warning(png_ptr,"Invalid number of transparent colors specified");
1048 return;
1049 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001050 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001051 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001052 }
1053 else if (color_type == PNG_COLOR_TYPE_GRAY)
1054 {
1055 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001056 if(tran->gray >= (1 << png_ptr->bit_depth))
1057 {
1058 png_warning(png_ptr,
1059 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1060 return;
1061 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001062 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001063 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001064 }
1065 else if (color_type == PNG_COLOR_TYPE_RGB)
1066 {
1067 /* three 16 bit values */
1068 png_save_uint_16(buf, tran->red);
1069 png_save_uint_16(buf + 2, tran->green);
1070 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001071 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1072 {
1073 png_warning(png_ptr,
1074 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1075 return;
1076 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001077 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001078 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001079 else
1080 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001081 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001082 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001083}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001084#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001085
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001086#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001087/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001088void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001089png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001090{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001091#ifdef PNG_USE_LOCAL_ARRAYS
1092 PNG_bKGD;
1093#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001094 png_byte buf[6];
1095
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001097 if (color_type == PNG_COLOR_TYPE_PALETTE)
1098 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001099 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001100#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001101 (png_ptr->num_palette ||
1102 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001103#endif
1104 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001105 {
1106 png_warning(png_ptr, "Invalid background palette index");
1107 return;
1108 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001109 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001110 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001111 }
1112 else if (color_type & PNG_COLOR_MASK_COLOR)
1113 {
1114 png_save_uint_16(buf, back->red);
1115 png_save_uint_16(buf + 2, back->green);
1116 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001117 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1118 {
1119 png_warning(png_ptr,
1120 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1121 return;
1122 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001123 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001124 }
1125 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001126 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001127 if(back->gray >= (1 << png_ptr->bit_depth))
1128 {
1129 png_warning(png_ptr,
1130 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1131 return;
1132 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001133 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001134 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001135 }
1136}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001137#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001138
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001139#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001140/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001141void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001142png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001143{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001144#ifdef PNG_USE_LOCAL_ARRAYS
1145 PNG_hIST;
1146#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001147 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001148 png_byte buf[3];
1149
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001151 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001152 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001153 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1154 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001155 png_warning(png_ptr, "Invalid number of histogram entries specified");
1156 return;
1157 }
1158
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001159 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001160 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001161 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001162 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001163 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001164 }
1165 png_write_chunk_end(png_ptr);
1166}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001167#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001168
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001169#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1170 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001171/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1172 * and if invalid, correct the keyword rather than discarding the entire
1173 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1174 * length, forbids leading or trailing whitespace, multiple internal spaces,
1175 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001176 *
1177 * The new_key is allocated to hold the corrected keyword and must be freed
1178 * by the calling routine. This avoids problems with trying to write to
1179 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001180 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001181png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001182png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001183{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001184 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001185 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001186 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001187 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001188
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189 png_debug(1, "in png_check_keyword\n");
1190 *new_key = NULL;
1191
1192 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001193 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001194 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001195 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001196 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001197
Andreas Dilger47a0c421997-05-16 02:46:07 -05001198 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1199
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001200 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1201 if (*new_key == NULL)
1202 {
1203 png_warning(png_ptr, "Out of memory while procesing keyword");
1204 return ((png_size_t)0);
1205 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001206
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001208 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001209 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001210 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001212#ifndef PNG_NO_STDIO
Andreas Dilger47a0c421997-05-16 02:46:07 -05001213 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001214
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001215 png_sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001216 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001217#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001218 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001219#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001220 *dp = ' ';
1221 }
1222 else
1223 {
1224 *dp = *kp;
1225 }
1226 }
1227 *dp = '\0';
1228
1229 /* Remove any trailing white space. */
1230 kp = *new_key + key_len - 1;
1231 if (*kp == ' ')
1232 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001233 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234
1235 while (*kp == ' ')
1236 {
1237 *(kp--) = '\0';
1238 key_len--;
1239 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001240 }
1241
1242 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001243 kp = *new_key;
1244 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001245 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001246 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247
1248 while (*kp == ' ')
1249 {
1250 kp++;
1251 key_len--;
1252 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001253 }
1254
Andreas Dilger47a0c421997-05-16 02:46:07 -05001255 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001256
Andreas Dilger47a0c421997-05-16 02:46:07 -05001257 /* Remove multiple internal spaces. */
1258 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001259 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001260 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001261 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001262 *(dp++) = *kp;
1263 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001264 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001265 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001266 {
1267 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001268 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001269 }
1270 else
1271 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001272 *(dp++) = *kp;
1273 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001274 }
1275 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001277 if(kwarn)
1278 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001279
1280 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001281 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001282 png_free(png_ptr, *new_key);
1283 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001284 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001285 }
1286
1287 if (key_len > 79)
1288 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001289 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001290 new_key[79] = '\0';
1291 key_len = 79;
1292 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001293
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001294 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001295}
1296#endif
1297
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001298#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001299/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001300void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001301png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001302 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001303{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001304#ifdef PNG_USE_LOCAL_ARRAYS
1305 PNG_tEXt;
1306#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001308 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001309
Andreas Dilger47a0c421997-05-16 02:46:07 -05001310 png_debug(1, "in png_write_tEXt\n");
1311 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1312 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001313 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001314 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001316
Andreas Dilger47a0c421997-05-16 02:46:07 -05001317 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001318 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001319 else
1320 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001321
1322 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001323 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 -05001324 /*
1325 * We leave it to the application to meet PNG-1.0 requirements on the
1326 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1327 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001328 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001329 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001330 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001331 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001332 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001333
Guy Schalnat0d580581995-07-20 02:43:20 -05001334 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001335 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001336}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001337#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001338
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001339#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001340/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001341void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001342png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001343 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001344{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001345#ifdef PNG_USE_LOCAL_ARRAYS
1346 PNG_zTXt;
1347#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001348 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001349 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001350 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001351 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001352
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001354
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001355 comp.num_output_ptr = 0;
1356 comp.max_output_ptr = 0;
1357 comp.output_ptr = NULL;
1358 comp.input = NULL;
1359 comp.input_len = 0;
1360
Andreas Dilger47a0c421997-05-16 02:46:07 -05001361 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001362 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001363 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001364 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001365 }
1366
Andreas Dilger47a0c421997-05-16 02:46:07 -05001367 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1368 {
1369 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1370 png_free(png_ptr, new_key);
1371 return;
1372 }
1373
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001374 text_len = png_strlen(text);
1375
Andreas Dilger47a0c421997-05-16 02:46:07 -05001376 png_free(png_ptr, new_key);
1377
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001378 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001379 text_len = png_text_compress(png_ptr, text, text_len, compression,
1380 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001381
1382 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001383 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1384 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001385 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001387 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001388 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001389 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001390 /* write the compressed data */
1391 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001392
Guy Schalnat0d580581995-07-20 02:43:20 -05001393 /* close the chunk */
1394 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001395}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001396#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001397
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001398#if defined(PNG_WRITE_iTXt_SUPPORTED)
1399/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001400void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001401png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001402 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001403{
1404#ifdef PNG_USE_LOCAL_ARRAYS
1405 PNG_iTXt;
1406#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001407 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001408 png_charp new_lang, new_key;
1409 png_byte cbuf[2];
1410 compression_state comp;
1411
1412 png_debug(1, "in png_write_iTXt\n");
1413
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001414 comp.num_output_ptr = 0;
1415 comp.max_output_ptr = 0;
1416 comp.output_ptr = NULL;
1417 comp.input = NULL;
1418
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001419 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1420 {
1421 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1422 return;
1423 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001424 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001425 {
1426 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001427 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001428 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001429 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001430
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001431 if (lang_key == NULL)
1432 lang_key_len = 0;
1433 else
1434 lang_key_len = png_strlen(lang_key);
1435
1436 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001437 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001438 else
1439 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001440
1441 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001442 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1443 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001444
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001445
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001446 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001447 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001448
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001449 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1450 (png_uint_32)(
1451 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1452 + key_len
1453 + lang_len
1454 + lang_key_len
1455 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001456
1457 /*
1458 * We leave it to the application to meet PNG-1.0 requirements on the
1459 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1460 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1461 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1462 */
1463 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001464
1465 /* set the compression flag */
1466 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1467 compression == PNG_TEXT_COMPRESSION_NONE)
1468 cbuf[0] = 0;
1469 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1470 cbuf[0] = 1;
1471 /* set the compression method */
1472 cbuf[1] = 0;
1473 png_write_chunk_data(png_ptr, cbuf, 2);
1474
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001475 cbuf[0] = 0;
1476 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1477 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 -06001478 png_write_compressed_data_out(png_ptr, &comp);
1479
1480 png_write_chunk_end(png_ptr);
1481 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001482 if (new_lang)
1483 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001484}
1485#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001486
1487#if defined(PNG_WRITE_oFFs_SUPPORTED)
1488/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001489void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001490png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001491 int unit_type)
1492{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001493#ifdef PNG_USE_LOCAL_ARRAYS
1494 PNG_oFFs;
1495#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001496 png_byte buf[9];
1497
1498 png_debug(1, "in png_write_oFFs\n");
1499 if (unit_type >= PNG_OFFSET_LAST)
1500 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1501
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001502 png_save_int_32(buf, x_offset);
1503 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001504 buf[8] = (png_byte)unit_type;
1505
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001506 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001507}
1508#endif
1509
1510#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001511/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001512void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001513png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1514 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1515{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001516#ifdef PNG_USE_LOCAL_ARRAYS
1517 PNG_pCAL;
1518#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001519 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001520 png_uint_32p params_len;
1521 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001522 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001523 int i;
1524
1525 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1526 if (type >= PNG_EQUATION_LAST)
1527 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1528
1529 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001530 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001531 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001532 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001533 total_len = purpose_len + units_len + 10;
1534
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001535 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001536 *png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001537
1538 /* Find the length of each parameter, making sure we don't count the
1539 null terminator for the last parameter. */
1540 for (i = 0; i < nparams; i++)
1541 {
1542 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001543 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
1544 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001545 total_len += (png_size_t)params_len[i];
1546 }
1547
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001548 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001549 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001550 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001551 png_save_int_32(buf, X0);
1552 png_save_int_32(buf + 4, X1);
1553 buf[8] = (png_byte)type;
1554 buf[9] = (png_byte)nparams;
1555 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1556 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1557
1558 png_free(png_ptr, new_purpose);
1559
1560 for (i = 0; i < nparams; i++)
1561 {
1562 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1563 (png_size_t)params_len[i]);
1564 }
1565
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001566 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001567 png_write_chunk_end(png_ptr);
1568}
1569#endif
1570
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001571#if defined(PNG_WRITE_sCAL_SUPPORTED)
1572/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001573#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001574void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001575png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001576{
1577#ifdef PNG_USE_LOCAL_ARRAYS
1578 PNG_sCAL;
1579#endif
1580 png_size_t total_len;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001581 char buf[64];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001582
1583 png_debug(1, "in png_write_sCAL\n");
1584
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001585 buf[0] = (char)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001586
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001587 png_sprintf(buf + 1, "%12.12e", width);
1588 total_len = 1 + png_strlen(buf + 1) + 1;
1589 png_sprintf(buf + total_len, "%12.12e", height);
1590 total_len += png_strlen(buf + total_len);
1591 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1592 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001593}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001594#else
1595#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001596void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001597png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001598 png_charp height)
1599{
1600#ifdef PNG_USE_LOCAL_ARRAYS
1601 PNG_sCAL;
1602#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001603 png_byte buf[64];
1604 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001605
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001606 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001607
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001608 wlen = png_strlen(width);
1609 hlen = png_strlen(height);
1610 total_len = wlen + hlen + 2;
1611 if (total_len > 64)
1612 {
1613 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1614 return;
1615 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001616
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001617 buf[0] = (png_byte)unit;
1618 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1619 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001620
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001621 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1622 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001623}
1624#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001625#endif
1626#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001628#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001629/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001630void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001631png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001632 png_uint_32 y_pixels_per_unit,
1633 int unit_type)
1634{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001635#ifdef PNG_USE_LOCAL_ARRAYS
1636 PNG_pHYs;
1637#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001638 png_byte buf[9];
1639
Andreas Dilger47a0c421997-05-16 02:46:07 -05001640 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001641 if (unit_type >= PNG_RESOLUTION_LAST)
1642 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1643
Guy Schalnat0d580581995-07-20 02:43:20 -05001644 png_save_uint_32(buf, x_pixels_per_unit);
1645 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001646 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001647
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001648 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001649}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001650#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001651
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001652#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001653/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1654 * or png_convert_from_time_t(), or fill in the structure yourself.
1655 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001656void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001657png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001658{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001659#ifdef PNG_USE_LOCAL_ARRAYS
1660 PNG_tIME;
1661#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001662 png_byte buf[7];
1663
Andreas Dilger47a0c421997-05-16 02:46:07 -05001664 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001665 if (mod_time->month > 12 || mod_time->month < 1 ||
1666 mod_time->day > 31 || mod_time->day < 1 ||
1667 mod_time->hour > 23 || mod_time->second > 60)
1668 {
1669 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1670 return;
1671 }
1672
Guy Schalnat0d580581995-07-20 02:43:20 -05001673 png_save_uint_16(buf, mod_time->year);
1674 buf[2] = mod_time->month;
1675 buf[3] = mod_time->day;
1676 buf[4] = mod_time->hour;
1677 buf[5] = mod_time->minute;
1678 buf[6] = mod_time->second;
1679
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001680 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001681}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001682#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001683
1684/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001685void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001686png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001687{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001688#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001689 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001690
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001691 /* start of interlace block */
1692 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001693
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001694 /* offset to next interlace block */
1695 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001696
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001697 /* start of interlace block in the y direction */
1698 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001699
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001700 /* offset to next interlace block in the y direction */
1701 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001702#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001703
Andreas Dilger47a0c421997-05-16 02:46:07 -05001704 png_size_t buf_size;
1705
1706 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001707 buf_size = (png_size_t)(PNG_ROWBYTES(
1708 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001709
Guy Schalnat0d580581995-07-20 02:43:20 -05001710 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001711 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001712 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001713
1714 /* set up filtering buffer, if using this filter */
1715 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001716 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001717 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001718 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001719 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001720 }
1721
Andreas Dilger47a0c421997-05-16 02:46:07 -05001722 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001723 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1724 {
1725 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001726 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001727 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001728
1729 if (png_ptr->do_filter & PNG_FILTER_UP)
1730 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001731 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001732 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001733 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001734 }
1735
1736 if (png_ptr->do_filter & PNG_FILTER_AVG)
1737 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001738 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1739 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001740 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001741 }
1742
1743 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1744 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001745 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001746 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001747 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001748 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001749 }
1750
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001751#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001752 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001753 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001754 {
1755 if (!(png_ptr->transformations & PNG_INTERLACE))
1756 {
1757 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1758 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001759 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1760 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001761 }
1762 else
1763 {
1764 png_ptr->num_rows = png_ptr->height;
1765 png_ptr->usr_width = png_ptr->width;
1766 }
1767 }
1768 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001769#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001770 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001771 png_ptr->num_rows = png_ptr->height;
1772 png_ptr->usr_width = png_ptr->width;
1773 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001774 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1775 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001776}
1777
Andreas Dilger47a0c421997-05-16 02:46:07 -05001778/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001779void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001780png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001781{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001782#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001783 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001784
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001785 /* start of interlace block */
1786 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001787
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001788 /* offset to next interlace block */
1789 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001790
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001791 /* start of interlace block in the y direction */
1792 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001793
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001794 /* offset to next interlace block in the y direction */
1795 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001796#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001797
Guy Schalnat0d580581995-07-20 02:43:20 -05001798 int ret;
1799
Andreas Dilger47a0c421997-05-16 02:46:07 -05001800 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001801 /* next row */
1802 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001803
Guy Schalnat0d580581995-07-20 02:43:20 -05001804 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001805 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001806 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001807
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001808#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001809 /* if interlaced, go to next pass */
1810 if (png_ptr->interlaced)
1811 {
1812 png_ptr->row_number = 0;
1813 if (png_ptr->transformations & PNG_INTERLACE)
1814 {
1815 png_ptr->pass++;
1816 }
1817 else
1818 {
1819 /* loop until we find a non-zero width or height pass */
1820 do
1821 {
1822 png_ptr->pass++;
1823 if (png_ptr->pass >= 7)
1824 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001825 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001826 png_pass_inc[png_ptr->pass] - 1 -
1827 png_pass_start[png_ptr->pass]) /
1828 png_pass_inc[png_ptr->pass];
1829 png_ptr->num_rows = (png_ptr->height +
1830 png_pass_yinc[png_ptr->pass] - 1 -
1831 png_pass_ystart[png_ptr->pass]) /
1832 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001833 if (png_ptr->transformations & PNG_INTERLACE)
1834 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001835 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1836
1837 }
1838
Guy Schalnate5a37791996-06-05 15:50:50 -05001839 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001840 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001841 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001842 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001843 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001844 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1845 png_ptr->usr_bit_depth,png_ptr->width))+1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001846 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001847 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001849#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001850
1851 /* if we get here, we've just written the last row, so we need
1852 to flush the compressor */
1853 do
1854 {
1855 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001856 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001857 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001858 if (ret == Z_OK)
1859 {
1860 /* check to see if we need more room */
1861 if (!(png_ptr->zstream.avail_out))
1862 {
1863 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1864 png_ptr->zstream.next_out = png_ptr->zbuf;
1865 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1866 }
1867 }
1868 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001869 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001870 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001871 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001872 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001873 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001874 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001875 } while (ret != Z_STREAM_END);
1876
1877 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001878 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001879 {
1880 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001881 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001882 }
1883
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001884 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001885 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001886}
1887
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001888#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001889/* Pick out the correct pixels for the interlace pass.
1890 * The basic idea here is to go through the row with a source
1891 * pointer and a destination pointer (sp and dp), and copy the
1892 * correct pixels for the pass. As the row gets compacted,
1893 * sp will always be >= dp, so we should never overwrite anything.
1894 * See the default: case for the easiest code to understand.
1895 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001896void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001897png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001898{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001899#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001900 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001901
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001902 /* start of interlace block */
1903 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001904
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001905 /* offset to next interlace block */
1906 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001907#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001908
Andreas Dilger47a0c421997-05-16 02:46:07 -05001909 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001910 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001911#if defined(PNG_USELESS_TESTS_SUPPORTED)
1912 if (row != NULL && row_info != NULL && pass < 6)
1913#else
1914 if (pass < 6)
1915#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001916 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001917 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001918 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001919 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001920 case 1:
1921 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001922 png_bytep sp;
1923 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001924 int shift;
1925 int d;
1926 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001927 png_uint_32 i;
1928 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001929
1930 dp = row;
1931 d = 0;
1932 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001933 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001934 i += png_pass_inc[pass])
1935 {
1936 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001937 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001938 d |= (value << shift);
1939
1940 if (shift == 0)
1941 {
1942 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001943 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001944 d = 0;
1945 }
1946 else
1947 shift--;
1948
1949 }
1950 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001951 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001952 break;
1953 }
1954 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001955 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001956 png_bytep sp;
1957 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001958 int shift;
1959 int d;
1960 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001961 png_uint_32 i;
1962 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001963
1964 dp = row;
1965 shift = 6;
1966 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001967 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001968 i += png_pass_inc[pass])
1969 {
1970 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001971 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001972 d |= (value << shift);
1973
1974 if (shift == 0)
1975 {
1976 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001977 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001978 d = 0;
1979 }
1980 else
1981 shift -= 2;
1982 }
1983 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001984 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 break;
1986 }
1987 case 4:
1988 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001989 png_bytep sp;
1990 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001991 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001992 int d;
1993 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001994 png_uint_32 i;
1995 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001996
1997 dp = row;
1998 shift = 4;
1999 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002000 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002001 i += png_pass_inc[pass])
2002 {
2003 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002004 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002005 d |= (value << shift);
2006
2007 if (shift == 0)
2008 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002009 shift = 4;
2010 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002011 d = 0;
2012 }
2013 else
2014 shift -= 4;
2015 }
2016 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002017 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002018 break;
2019 }
2020 default:
2021 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002022 png_bytep sp;
2023 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002024 png_uint_32 i;
2025 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002026 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002027
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002028 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002029 dp = row;
2030 /* find out how many bytes each pixel takes up */
2031 pixel_bytes = (row_info->pixel_depth >> 3);
2032 /* loop through the row, only looking at the pixels that
2033 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002034 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002035 i += png_pass_inc[pass])
2036 {
2037 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002038 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002039 /* move the pixel */
2040 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002041 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002042 /* next pixel */
2043 dp += pixel_bytes;
2044 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002045 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002046 }
2047 }
2048 /* set new row width */
2049 row_info->width = (row_info->width +
2050 png_pass_inc[pass] - 1 -
2051 png_pass_start[pass]) /
2052 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002053 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2054 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002055 }
2056}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002057#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002058
Andreas Dilger47a0c421997-05-16 02:46:07 -05002059/* This filters the row, chooses which filter to use, if it has not already
2060 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002061 * chosen filter.
2062 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002063#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002064#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002065#define PNG_LOMASK ((png_uint_32)0xffffL)
2066#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002067void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002068png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002069{
Guy Schalnate5a37791996-06-05 15:50:50 -05002070 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002071 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002072 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002073 png_uint_32 row_bytes = row_info->rowbytes;
2074#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2075 int num_p_filters = (int)png_ptr->num_prev_filters;
2076#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002077
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002079 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002080 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002081
2082 prev_row = png_ptr->prev_row;
2083 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002084 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002085
Andreas Dilger47a0c421997-05-16 02:46:07 -05002086 /* The prediction method we use is to find which method provides the
2087 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002088 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002089 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002090 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002091 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002092 * predictive" method (not implemented yet), which does test compressions
2093 * of lines using different filter methods, and then chooses the
2094 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002096 *
2097 * GRR 980525: consider also
2098 * (1) minimum sum of absolute differences from running average (i.e.,
2099 * keep running sum of non-absolute differences & count of bytes)
2100 * [track dispersion, too? restart average if dispersion too large?]
2101 * (1b) minimum sum of absolute differences from sliding average, probably
2102 * with window size <= deflate window (usually 32K)
2103 * (2) minimum sum of squared differences from zero or running average
2104 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002105 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002106
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002107
Guy Schalnate5a37791996-06-05 15:50:50 -05002108 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002109 * that has been chosen, as it doesn't actually do anything to the data.
2110 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002111 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002112 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002113 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002114 png_bytep rp;
2115 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002116 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002117 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002118
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002119 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002120 {
2121 v = *rp;
2122 sum += (v < 128) ? v : 256 - v;
2123 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002124
2125#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2126 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2127 {
2128 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002129 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002130 sumlo = sum & PNG_LOMASK;
2131 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2132
2133 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002134 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002136 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002138 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002140 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141 PNG_WEIGHT_SHIFT;
2142 }
2143 }
2144
2145 /* Factor in the cost of this filter (this is here for completeness,
2146 * but it makes no sense to have a "cost" for the NONE filter, as
2147 * it has the minimum possible computational cost - none).
2148 */
2149 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2150 PNG_COST_SHIFT;
2151 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2152 PNG_COST_SHIFT;
2153
2154 if (sumhi > PNG_HIMASK)
2155 sum = PNG_MAXSUM;
2156 else
2157 sum = (sumhi << PNG_HISHIFT) + sumlo;
2158 }
2159#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002160 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002161 }
2162
Guy Schalnate5a37791996-06-05 15:50:50 -05002163 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002164 if (filter_to_do == PNG_FILTER_SUB)
2165 /* it's the only filter so no testing is needed */
2166 {
2167 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002168 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002169 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2170 i++, rp++, dp++)
2171 {
2172 *dp = *rp;
2173 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002174 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002175 i++, rp++, lp++, dp++)
2176 {
2177 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2178 }
2179 best_row = png_ptr->sub_row;
2180 }
2181
2182 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002183 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002184 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002185 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002186 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002187 int v;
2188
2189#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002190 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002191 * would reduce the sum of this filter, so that we can do the
2192 * early exit comparison without scaling the sum each time.
2193 */
2194 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2195 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002196 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002197 png_uint_32 lmhi, lmlo;
2198 lmlo = lmins & PNG_LOMASK;
2199 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2200
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002201 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002202 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002203 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002204 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002205 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002206 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002207 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002208 PNG_WEIGHT_SHIFT;
2209 }
2210 }
2211
2212 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2213 PNG_COST_SHIFT;
2214 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2215 PNG_COST_SHIFT;
2216
2217 if (lmhi > PNG_HIMASK)
2218 lmins = PNG_MAXSUM;
2219 else
2220 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2221 }
2222#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002223
Guy Schalnate5a37791996-06-05 15:50:50 -05002224 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2225 i++, rp++, dp++)
2226 {
2227 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002228
Guy Schalnate5a37791996-06-05 15:50:50 -05002229 sum += (v < 128) ? v : 256 - v;
2230 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002231 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002232 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002233 {
2234 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002235
Guy Schalnate5a37791996-06-05 15:50:50 -05002236 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002237
2238 if (sum > lmins) /* We are already worse, don't continue. */
2239 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002240 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002241
2242#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2243 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2244 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002245 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002246 png_uint_32 sumhi, sumlo;
2247 sumlo = sum & PNG_LOMASK;
2248 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2249
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002250 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002252 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002254 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002256 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 PNG_WEIGHT_SHIFT;
2258 }
2259 }
2260
2261 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2262 PNG_COST_SHIFT;
2263 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2264 PNG_COST_SHIFT;
2265
2266 if (sumhi > PNG_HIMASK)
2267 sum = PNG_MAXSUM;
2268 else
2269 sum = (sumhi << PNG_HISHIFT) + sumlo;
2270 }
2271#endif
2272
Guy Schalnate5a37791996-06-05 15:50:50 -05002273 if (sum < mins)
2274 {
2275 mins = sum;
2276 best_row = png_ptr->sub_row;
2277 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002278 }
2279
Guy Schalnate5a37791996-06-05 15:50:50 -05002280 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002281 if (filter_to_do == PNG_FILTER_UP)
2282 {
2283 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002284 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002285
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002286 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002287 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002288 i++, rp++, pp++, dp++)
2289 {
2290 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2291 }
2292 best_row = png_ptr->up_row;
2293 }
2294
2295 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002296 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002297 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002298 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002299 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300 int v;
2301
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002302
Andreas Dilger47a0c421997-05-16 02:46:07 -05002303#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2304 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2305 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002306 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002307 png_uint_32 lmhi, lmlo;
2308 lmlo = lmins & PNG_LOMASK;
2309 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2310
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002311 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002312 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002313 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002315 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002316 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002317 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002318 PNG_WEIGHT_SHIFT;
2319 }
2320 }
2321
2322 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2323 PNG_COST_SHIFT;
2324 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2325 PNG_COST_SHIFT;
2326
2327 if (lmhi > PNG_HIMASK)
2328 lmins = PNG_MAXSUM;
2329 else
2330 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2331 }
2332#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002333
2334 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002335 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002336 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002337 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002338
2339 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002340
2341 if (sum > lmins) /* We are already worse, don't continue. */
2342 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002343 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002344
2345#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2346 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2347 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002348 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349 png_uint_32 sumhi, sumlo;
2350 sumlo = sum & PNG_LOMASK;
2351 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2352
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002355 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002357 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002359 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360 PNG_WEIGHT_SHIFT;
2361 }
2362 }
2363
2364 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2365 PNG_COST_SHIFT;
2366 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2367 PNG_COST_SHIFT;
2368
2369 if (sumhi > PNG_HIMASK)
2370 sum = PNG_MAXSUM;
2371 else
2372 sum = (sumhi << PNG_HISHIFT) + sumlo;
2373 }
2374#endif
2375
Guy Schalnate5a37791996-06-05 15:50:50 -05002376 if (sum < mins)
2377 {
2378 mins = sum;
2379 best_row = png_ptr->up_row;
2380 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002381 }
2382
Guy Schalnate5a37791996-06-05 15:50:50 -05002383 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002384 if (filter_to_do == PNG_FILTER_AVG)
2385 {
2386 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002387 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002388 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002389 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002390 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002391 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002392 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002393 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002394 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002395 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2396 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002397 }
2398 best_row = png_ptr->avg_row;
2399 }
2400
2401 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002402 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002403 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002404 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002405 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002406 int v;
2407
2408#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2409 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2410 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002411 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002412 png_uint_32 lmhi, lmlo;
2413 lmlo = lmins & PNG_LOMASK;
2414 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2415
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002416 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002417 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002418 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002419 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002420 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002421 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002422 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002423 PNG_WEIGHT_SHIFT;
2424 }
2425 }
2426
2427 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2428 PNG_COST_SHIFT;
2429 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2430 PNG_COST_SHIFT;
2431
2432 if (lmhi > PNG_HIMASK)
2433 lmins = PNG_MAXSUM;
2434 else
2435 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2436 }
2437#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002438
2439 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002440 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002441 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002442 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002443
2444 sum += (v < 128) ? v : 256 - v;
2445 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002446 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002447 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002448 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002450
2451 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002452
2453 if (sum > lmins) /* We are already worse, don't continue. */
2454 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002455 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002456
2457#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2458 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2459 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002460 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002461 png_uint_32 sumhi, sumlo;
2462 sumlo = sum & PNG_LOMASK;
2463 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2464
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002465 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002467 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002469 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002470 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002471 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472 PNG_WEIGHT_SHIFT;
2473 }
2474 }
2475
2476 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2477 PNG_COST_SHIFT;
2478 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2479 PNG_COST_SHIFT;
2480
2481 if (sumhi > PNG_HIMASK)
2482 sum = PNG_MAXSUM;
2483 else
2484 sum = (sumhi << PNG_HISHIFT) + sumlo;
2485 }
2486#endif
2487
Guy Schalnate5a37791996-06-05 15:50:50 -05002488 if (sum < mins)
2489 {
2490 mins = sum;
2491 best_row = png_ptr->avg_row;
2492 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002493 }
2494
Andreas Dilger47a0c421997-05-16 02:46:07 -05002495 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002496 if (filter_to_do == PNG_FILTER_PAETH)
2497 {
2498 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002499 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002500 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002501 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002502 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002503 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002504 }
2505
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002507 {
2508 int a, b, c, pa, pb, pc, p;
2509
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002510 b = *pp++;
2511 c = *cp++;
2512 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002513
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002514 p = b - c;
2515 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002516
2517#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 pa = abs(p);
2519 pb = abs(pc);
2520 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002521#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002522 pa = p < 0 ? -p : p;
2523 pb = pc < 0 ? -pc : pc;
2524 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002525#endif
2526
2527 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2528
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002529 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002530 }
2531 best_row = png_ptr->paeth_row;
2532 }
2533
2534 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002535 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002536 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002537 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002538 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002539 int v;
2540
2541#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2542 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2543 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002544 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002545 png_uint_32 lmhi, lmlo;
2546 lmlo = lmins & PNG_LOMASK;
2547 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2548
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002549 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002550 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002551 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002552 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002553 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002554 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002555 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002556 PNG_WEIGHT_SHIFT;
2557 }
2558 }
2559
2560 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2561 PNG_COST_SHIFT;
2562 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2563 PNG_COST_SHIFT;
2564
2565 if (lmhi > PNG_HIMASK)
2566 lmins = PNG_MAXSUM;
2567 else
2568 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2569 }
2570#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002571
2572 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002573 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002574 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002575 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002576
2577 sum += (v < 128) ? v : 256 - v;
2578 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002579
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002581 {
2582 int a, b, c, pa, pb, pc, p;
2583
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002584 b = *pp++;
2585 c = *cp++;
2586 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002587
2588#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002589 p = b - c;
2590 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002591#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 pa = abs(p);
2593 pb = abs(pc);
2594 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002595#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002596 pa = p < 0 ? -p : p;
2597 pb = pc < 0 ? -pc : pc;
2598 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002599#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002600 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2601#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002602 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002603 pa = abs(p - a);
2604 pb = abs(p - b);
2605 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002606 if (pa <= pb && pa <= pc)
2607 p = a;
2608 else if (pb <= pc)
2609 p = b;
2610 else
2611 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002612#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002613
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002614 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002615
2616 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002617
2618 if (sum > lmins) /* We are already worse, don't continue. */
2619 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002620 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002621
2622#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2623 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2624 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002625 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002626 png_uint_32 sumhi, sumlo;
2627 sumlo = sum & PNG_LOMASK;
2628 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2629
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002630 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002631 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002632 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002633 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002634 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002635 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002636 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002637 PNG_WEIGHT_SHIFT;
2638 }
2639 }
2640
2641 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2642 PNG_COST_SHIFT;
2643 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2644 PNG_COST_SHIFT;
2645
2646 if (sumhi > PNG_HIMASK)
2647 sum = PNG_MAXSUM;
2648 else
2649 sum = (sumhi << PNG_HISHIFT) + sumlo;
2650 }
2651#endif
2652
Guy Schalnate5a37791996-06-05 15:50:50 -05002653 if (sum < mins)
2654 {
2655 best_row = png_ptr->paeth_row;
2656 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002657 }
2658
Andreas Dilger47a0c421997-05-16 02:46:07 -05002659 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002660
Guy Schalnate5a37791996-06-05 15:50:50 -05002661 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002662
2663#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2664 /* Save the type of filter we picked this time for future calculations */
2665 if (png_ptr->num_prev_filters > 0)
2666 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002667 int j;
2668 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002669 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002670 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002671 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002672 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002673 }
2674#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002675}
2676
2677
Andreas Dilger47a0c421997-05-16 02:46:07 -05002678/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002679void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002680png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2681{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002682 png_debug(1, "in png_write_filtered_row\n");
2683 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002684 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002685
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002686 png_ptr->zstream.next_in = filtered_row;
2687 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002688 /* repeat until we have compressed all the data */
2689 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002690 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002691 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002692
Guy Schalnate5a37791996-06-05 15:50:50 -05002693 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002694 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002695 /* check for compression errors */
2696 if (ret != Z_OK)
2697 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002698 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002699 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002700 else
2701 png_error(png_ptr, "zlib error");
2702 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002703
Guy Schalnate5a37791996-06-05 15:50:50 -05002704 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002705 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002706 {
2707 /* write the IDAT and reset the zlib output buffer */
2708 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002709 png_ptr->zstream.next_out = png_ptr->zbuf;
2710 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002711 }
2712 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002713 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002714
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002715 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002716 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002717 {
2718 png_bytep tptr;
2719
2720 tptr = png_ptr->prev_row;
2721 png_ptr->prev_row = png_ptr->row_buf;
2722 png_ptr->row_buf = tptr;
2723 }
2724
Guy Schalnate5a37791996-06-05 15:50:50 -05002725 /* finish row - updates counters and flushes zlib if last row */
2726 png_write_finish_row(png_ptr);
2727
2728#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2729 png_ptr->flush_rows++;
2730
2731 if (png_ptr->flush_dist > 0 &&
2732 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002733 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002734 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002735 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002736#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002737}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002738#endif /* PNG_WRITE_SUPPORTED */