blob: 16dfd827d1e49559e2b33097ad8023e821175410 [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-Pehrsonbeb572e2006-08-19 13:59:24 -05004 * Last changed in libpng 1.4.0 July 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-Pehrsonbeb572e2006-08-19 13:59:24 -050013#include "pngpriv.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};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050064
65#ifdef PNG_IO_STATE_SUPPORTED
66 /* inform the I/O callback that the signature is being written */
67 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
68#endif
69
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050070 /* write the rest of the 8 byte signature */
71 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050072 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050073 if(png_ptr->sig_bytes < 3)
74 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
75}
76
Andreas Dilger47a0c421997-05-16 02:46:07 -050077/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078 * representing the chunk name. The array must be at least 4 bytes in
79 * length, and does not need to be null terminated. To be safe, pass the
80 * pre-defined chunk names here, and if you need a new one, define it
81 * where the others are defined. The length is the length of the data.
82 * All the data must be present. If that is not possible, use the
83 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
84 * functions instead.
85 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050086void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050089{
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -060090 if(png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060092 png_write_chunk_data(png_ptr, data, length);
93 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050094}
95
Andreas Dilger47a0c421997-05-16 02:46:07 -050096/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060097 * The total_length is the sum of the lengths of all the data you will be
98 * passing in png_write_chunk_data().
99 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500100void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
102 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500104 png_byte buf[8];
105 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
106 (unsigned long)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500107
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500108#ifdef PNG_IO_STATE_SUPPORTED
109 /* Inform the I/O callback that the chunk header is being written.
110 * PNG_IO_CHUNK_HDR requires a single I/O call.
111 */
112 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
113#endif
114
115 /* write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500117 png_memcpy(buf + 4, chunk_name, 4);
118 png_write_data(png_ptr, buf, 8);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500119
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500120 /* put the chunk name into png_ptr->chunk_name */
121 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
122
Guy Schalnat0d580581995-07-20 02:43:20 -0500123 /* reset the crc and run it over the chunk name */
124 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500125 png_calculate_crc(png_ptr, chunk_name, 4);
126
127#ifdef PNG_IO_STATE_SUPPORTED
128 /* Inform the I/O callback that chunk data will (possibly) be written.
129 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
130 */
131 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
132#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500133}
134
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600136 * Note that multiple calls to this function are allowed, and that the
137 * sum of the lengths from these calls *must* add up to the total_length
138 * given to png_write_chunk_start().
139 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500140void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500142{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500143 /* write the data, and run the CRC over it */
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600144 if(png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500145 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600147 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500148 /* update the CRC after writing the data,
149 * in case that the user I/O routine alters it.
150 */
151 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 }
153}
154
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500156void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600157png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 png_byte buf[4];
160
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500161#ifdef PNG_IO_STATE_SUPPORTED
162 /* Inform the I/O callback that the chunk CRC is being written.
163 * PNG_IO_CHUNK_CRC requires a single I/O function call.
164 */
165 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
166#endif
167
168 /* write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500169 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500171 png_write_data(png_ptr, buf, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500172}
173
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600174#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600175/*
176 * This pair of functions encapsulates the operation of (a) compressing a
177 * text string, and (b) issuing it later as a series of chunk data writes.
178 * The compression_state structure is shared context for these functions
179 * set up by the caller in order to make the whole mess thread-safe.
180 */
181
182typedef struct
183{
184 char *input; /* the uncompressed input data */
185 int input_len; /* its length */
186 int num_output_ptr; /* number of output pointers used */
187 int max_output_ptr; /* size of output_ptr */
188 png_charpp output_ptr; /* array of pointers to output */
189} compression_state;
190
191/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500192static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600193png_text_compress(png_structp png_ptr,
194 png_charp text, png_size_t text_len, int compression,
195 compression_state *comp)
196{
197 int ret;
198
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600199 comp->num_output_ptr = 0;
200 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600201 comp->output_ptr = NULL;
202 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600203 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600204
205 /* we may just want to pass the text right through */
206 if (compression == PNG_TEXT_COMPRESSION_NONE)
207 {
208 comp->input = text;
209 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500210 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600211 }
212
213 if (compression >= PNG_TEXT_COMPRESSION_LAST)
214 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500215#ifndef PNG_NO_STDIO
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600216 char msg[50];
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500217 png_sprintf(msg, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600218 png_warning(png_ptr, msg);
219#else
220 png_warning(png_ptr, "Unknown compression type");
221#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600222 }
223
224 /* We can't write the chunk until we find out how much data we have,
225 * which means we need to run the compressor first and save the
226 * output. This shouldn't be a problem, as the vast majority of
227 * comments should be reasonable, but we will set up an array of
228 * malloc'd pointers to be sure.
229 *
230 * If we knew the application was well behaved, we could simplify this
231 * greatly by assuming we can always malloc an output buffer large
232 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
233 * and malloc this directly. The only time this would be a bad idea is
234 * if we can't malloc more than 64K and we have 64K of random input
235 * data, or if the input string is incredibly large (although this
236 * wouldn't cause a failure, just a slowdown due to swapping).
237 */
238
239 /* set up the compression buffers */
240 png_ptr->zstream.avail_in = (uInt)text_len;
241 png_ptr->zstream.next_in = (Bytef *)text;
242 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
243 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
244
245 /* this is the same compression loop as in png_write_row() */
246 do
247 {
248 /* compress the data */
249 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
250 if (ret != Z_OK)
251 {
252 /* error */
253 if (png_ptr->zstream.msg != NULL)
254 png_error(png_ptr, png_ptr->zstream.msg);
255 else
256 png_error(png_ptr, "zlib error");
257 }
258 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600259 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600260 {
261 /* make sure the output array has room */
262 if (comp->num_output_ptr >= comp->max_output_ptr)
263 {
264 int old_max;
265
266 old_max = comp->max_output_ptr;
267 comp->max_output_ptr = comp->num_output_ptr + 4;
268 if (comp->output_ptr != NULL)
269 {
270 png_charpp old_ptr;
271
272 old_ptr = comp->output_ptr;
273 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500274 (png_size_t)(comp->max_output_ptr * sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500275 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500276 * sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600277 png_free(png_ptr, old_ptr);
278 }
279 else
280 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500281 (png_size_t)(comp->max_output_ptr * sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600282 }
283
284 /* save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500285 comp->output_ptr[comp->num_output_ptr] =
286 (png_charp)png_malloc(png_ptr, png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500287 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
288 png_ptr->zbuf_size);
289 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600290
291 /* and reset the buffer */
292 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
293 png_ptr->zstream.next_out = png_ptr->zbuf;
294 }
295 /* continue until we don't have any more to compress */
296 } while (png_ptr->zstream.avail_in);
297
298 /* finish the compression */
299 do
300 {
301 /* tell zlib we are finished */
302 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500303
304 if (ret == Z_OK)
305 {
306 /* check to see if we need more room */
307 if (!(png_ptr->zstream.avail_out))
308 {
309 /* check to make sure our output array has room */
310 if (comp->num_output_ptr >= comp->max_output_ptr)
311 {
312 int old_max;
313
314 old_max = comp->max_output_ptr;
315 comp->max_output_ptr = comp->num_output_ptr + 4;
316 if (comp->output_ptr != NULL)
317 {
318 png_charpp old_ptr;
319
320 old_ptr = comp->output_ptr;
321 /* This could be optimized to realloc() */
322 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500323 (png_size_t)(comp->max_output_ptr * sizeof(png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500324 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500325 old_max * sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500326 png_free(png_ptr, old_ptr);
327 }
328 else
329 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500330 (png_size_t)(comp->max_output_ptr * sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500331 }
332
333 /* save off the data */
334 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500335 (png_charp)png_malloc(png_ptr, png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500336 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
337 png_ptr->zbuf_size);
338 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500339
340 /* and reset the buffer pointers */
341 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
342 png_ptr->zstream.next_out = png_ptr->zbuf;
343 }
344 }
345 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600346 {
347 /* we got an error */
348 if (png_ptr->zstream.msg != NULL)
349 png_error(png_ptr, png_ptr->zstream.msg);
350 else
351 png_error(png_ptr, "zlib error");
352 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600353 } while (ret != Z_STREAM_END);
354
355 /* text length is number of buffers plus last buffer */
356 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
357 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
358 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
359
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500360 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361}
362
363/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500364static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
366{
367 int i;
368
369 /* handle the no-compression case */
370 if (comp->input)
371 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500372 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
373 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374 return;
375 }
376
377 /* write saved output buffers, if any */
378 for (i = 0; i < comp->num_output_ptr; i++)
379 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600380 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
381 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600382 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500383 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600384 }
385 if (comp->max_output_ptr != 0)
386 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500387 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388 /* write anything left in zbuf */
389 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
390 png_write_chunk_data(png_ptr, png_ptr->zbuf,
391 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
392
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600393 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600394 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600395 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600396}
397#endif
398
Guy Schalnat0d580581995-07-20 02:43:20 -0500399/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600400 * information. Note that the rest of this code depends upon this
401 * information being correct.
402 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500403void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600404png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 int bit_depth, int color_type, int compression_type, int filter_type,
406 int interlace_type)
407{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600408#ifdef PNG_USE_LOCAL_ARRAYS
409 PNG_IHDR;
410#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500411 png_byte buf[13]; /* buffer to store the IHDR info */
412
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500414 /* Check that we have valid input data from the application info */
415 switch (color_type)
416 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 switch (bit_depth)
419 {
420 case 1:
421 case 2:
422 case 4:
423 case 8:
424 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 }
427 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 if (bit_depth != 8 && bit_depth != 16)
430 png_error(png_ptr, "Invalid bit depth for RGB image");
431 png_ptr->channels = 3;
432 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500434 switch (bit_depth)
435 {
436 case 1:
437 case 2:
438 case 4:
439 case 8: png_ptr->channels = 1; break;
440 default: png_error(png_ptr, "Invalid bit depth for paletted image");
441 }
442 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500444 if (bit_depth != 8 && bit_depth != 16)
445 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
446 png_ptr->channels = 2;
447 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500449 if (bit_depth != 8 && bit_depth != 16)
450 png_error(png_ptr, "Invalid bit depth for RGBA image");
451 png_ptr->channels = 4;
452 break;
453 default:
454 png_error(png_ptr, "Invalid image color type specified");
455 }
456
Andreas Dilger47a0c421997-05-16 02:46:07 -0500457 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500458 {
459 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500460 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500461 }
462
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600463 /* Write filter_method 64 (intrapixel differencing) only if
464 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
465 * 2. Libpng did not write a PNG signature (this filter_method is only
466 * used in PNG datastreams that are embedded in MNG datastreams) and
467 * 3. The application called png_permit_mng_features with a mask that
468 * included PNG_FLAG_MNG_FILTER_64 and
469 * 4. The filter_method is 64 and
470 * 5. The color_type is RGB or RGBA
471 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600472 if (
473#if defined(PNG_MNG_FEATURES_SUPPORTED)
474 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600475 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500476 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600477 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600478 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
479#endif
480 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500481 {
482 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500483 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500484 }
485
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600486#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500487 if (interlace_type != PNG_INTERLACE_NONE &&
488 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500489 {
490 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500492 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600493#else
494 interlace_type=PNG_INTERLACE_NONE;
495#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500496
497 /* save off the relevent information */
498 png_ptr->bit_depth = (png_byte)bit_depth;
499 png_ptr->color_type = (png_byte)color_type;
500 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500501#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600502 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500503#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500504 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500505 png_ptr->width = width;
506 png_ptr->height = height;
507
508 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500509 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 /* set the usr info, so any transformations can modify it */
511 png_ptr->usr_width = png_ptr->width;
512 png_ptr->usr_bit_depth = png_ptr->bit_depth;
513 png_ptr->usr_channels = png_ptr->channels;
514
Guy Schalnat0d580581995-07-20 02:43:20 -0500515 /* pack the header information into the buffer */
516 png_save_uint_32(buf, width);
517 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600518 buf[8] = (png_byte)bit_depth;
519 buf[9] = (png_byte)color_type;
520 buf[10] = (png_byte)compression_type;
521 buf[11] = (png_byte)filter_type;
522 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500523
524 /* write the chunk */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500525 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, 13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500526
Andreas Dilger47a0c421997-05-16 02:46:07 -0500527 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600528 png_ptr->zstream.zalloc = png_zalloc;
529 png_ptr->zstream.zfree = png_zfree;
530 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500531 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500532 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
534 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500535 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500536 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500537 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500538 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500539 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500540 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500541 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500542 png_ptr->zlib_strategy = Z_FILTERED;
543 else
544 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
545 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500546 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500547 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500548 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500549 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500550 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500551 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500553 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600554 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 png_ptr->zlib_method, png_ptr->zlib_window_bits,
556 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600557 png_ptr->zstream.next_out = png_ptr->zbuf;
558 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600559 /* libpng is not interested in zstream.data_type */
560 /* set it to a predefined value, to avoid its evaluation inside zlib */
561 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500562
Guy Schalnate5a37791996-06-05 15:50:50 -0500563 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500564}
565
566/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500567 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600568 * structure.
569 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500570void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500572{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600573#ifdef PNG_USE_LOCAL_ARRAYS
574 PNG_PLTE;
575#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500576 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600577 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500578 png_byte buf[3];
579
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500581 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500582#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600583 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500584#endif
585 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500586 {
587 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500588 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500589 png_error(png_ptr, "Invalid number of colors in palette");
590 }
591 else
592 {
593 png_warning(png_ptr, "Invalid number of colors in palette");
594 return;
595 }
596 }
597
598 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
599 {
600 png_warning(png_ptr,
601 "Ignoring request to write a PLTE chunk in grayscale PNG");
602 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500603 }
604
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605 png_ptr->num_palette = (png_uint_16)num_pal;
606 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500607
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600608 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500609#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500611 {
612 buf[0] = pal_ptr->red;
613 buf[1] = pal_ptr->green;
614 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500615 png_write_chunk_data(png_ptr, buf, 3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500617#else
618 /* This is a little slower but some buggy compilers need to do this instead */
619 pal_ptr=palette;
620 for (i = 0; i < num_pal; i++)
621 {
622 buf[0] = pal_ptr[i].red;
623 buf[1] = pal_ptr[i].green;
624 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500625 png_write_chunk_data(png_ptr, buf, 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500626 }
627#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500628 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500629 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500630}
631
632/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500633void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500634png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500635{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600636#ifdef PNG_USE_LOCAL_ARRAYS
637 PNG_IDAT;
638#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500639 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500640
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500641 /* Optimize the CMF field in the zlib stream. */
642 /* This hack of the zlib stream is compliant to the stream specification. */
643 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
644 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
645 {
646 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
647 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
648 {
649 /* Avoid memory underflows and multiplication overflows. */
650 /* The conditions below are practically always satisfied;
651 however, they still must be checked. */
652 if (length >= 2 &&
653 png_ptr->height < 16384 && png_ptr->width < 16384)
654 {
655 png_uint_32 uncompressed_idat_size = png_ptr->height *
656 ((png_ptr->width *
657 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
658 unsigned int z_cinfo = z_cmf >> 4;
659 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
660 while (uncompressed_idat_size <= half_z_window_size &&
661 half_z_window_size >= 256)
662 {
663 z_cinfo--;
664 half_z_window_size >>= 1;
665 }
666 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
667 if (data[0] != (png_byte)z_cmf)
668 {
669 data[0] = (png_byte)z_cmf;
670 data[1] &= 0xe0;
671 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
672 }
673 }
674 }
675 else
676 png_error(png_ptr,
677 "Invalid zlib compression method or flags in IDAT");
678 }
679
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600680 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500681 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500682}
683
684/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500685void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600686png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500687{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600688#ifdef PNG_USE_LOCAL_ARRAYS
689 PNG_IEND;
690#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500691 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500692 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600693 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500694}
695
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500696#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500697/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600698#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500699void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500701{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600702#ifdef PNG_USE_LOCAL_ARRAYS
703 PNG_gAMA;
704#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500705 png_uint_32 igamma;
706 png_byte buf[4];
707
Andreas Dilger47a0c421997-05-16 02:46:07 -0500708 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600709 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600710 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500711 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500712 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500713}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500714#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600715#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500716void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600717png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600718{
719#ifdef PNG_USE_LOCAL_ARRAYS
720 PNG_gAMA;
721#endif
722 png_byte buf[4];
723
724 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600725 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500726 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500727 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, 4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600728}
729#endif
730#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500731
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600732#if defined(PNG_WRITE_sRGB_SUPPORTED)
733/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500734void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600735png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600736{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600737#ifdef PNG_USE_LOCAL_ARRAYS
738 PNG_sRGB;
739#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600740 png_byte buf[1];
741
742 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600743 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600744 png_warning(png_ptr,
745 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600746 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500747 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, 1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600748}
749#endif
750
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600751#if defined(PNG_WRITE_iCCP_SUPPORTED)
752/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500753void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600754png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
755 png_charp profile, int profile_len)
756{
757#ifdef PNG_USE_LOCAL_ARRAYS
758 PNG_iCCP;
759#endif
760 png_size_t name_len;
761 png_charp new_name;
762 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500763 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600764
765 png_debug(1, "in png_write_iCCP\n");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600766
767 comp.num_output_ptr = 0;
768 comp.max_output_ptr = 0;
769 comp.output_ptr = NULL;
770 comp.input = NULL;
771 comp.input_len = 0;
772
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600773 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
774 &new_name)) == 0)
775 {
776 png_warning(png_ptr, "Empty keyword in iCCP chunk");
777 return;
778 }
779
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600780 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600781 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600782
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500783 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600784 profile_len = 0;
785
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500786 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600787 embedded_profile_len =
788 ((*( (png_bytep)profile ))<<24) |
789 ((*( (png_bytep)profile+1))<<16) |
790 ((*( (png_bytep)profile+2))<< 8) |
791 ((*( (png_bytep)profile+3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500792
793 if (profile_len < embedded_profile_len)
794 {
795 png_warning(png_ptr,
796 "Embedded profile length too large in iCCP chunk");
797 return;
798 }
799
800 if (profile_len > embedded_profile_len)
801 {
802 png_warning(png_ptr,
803 "Truncating profile to actual length in iCCP chunk");
804 profile_len = embedded_profile_len;
805 }
806
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600807 if (profile_len)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500808 profile_len = png_text_compress(png_ptr, profile,
809 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600810
811 /* make sure we include the NULL after the name and the compression type */
812 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
813 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600814 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600815 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
816
817 if (profile_len)
818 png_write_compressed_data_out(png_ptr, &comp);
819
820 png_write_chunk_end(png_ptr);
821 png_free(png_ptr, new_name);
822}
823#endif
824
825#if defined(PNG_WRITE_sPLT_SUPPORTED)
826/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500827void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600828png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600829{
830#ifdef PNG_USE_LOCAL_ARRAYS
831 PNG_sPLT;
832#endif
833 png_size_t name_len;
834 png_charp new_name;
835 png_byte entrybuf[10];
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500836 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
837 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600838 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500839#ifdef PNG_NO_POINTER_INDEXING
840 int i;
841#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600842
843 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600844 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
845 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600846 {
847 png_warning(png_ptr, "Empty keyword in sPLT chunk");
848 return;
849 }
850
851 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500852 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500853 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600854 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600855 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600856
857 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500858#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600859 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
860 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500861 if (spalette->depth == 8)
862 {
863 entrybuf[0] = (png_byte)ep->red;
864 entrybuf[1] = (png_byte)ep->green;
865 entrybuf[2] = (png_byte)ep->blue;
866 entrybuf[3] = (png_byte)ep->alpha;
867 png_save_uint_16(entrybuf + 4, ep->frequency);
868 }
869 else
870 {
871 png_save_uint_16(entrybuf + 0, ep->red);
872 png_save_uint_16(entrybuf + 2, ep->green);
873 png_save_uint_16(entrybuf + 4, ep->blue);
874 png_save_uint_16(entrybuf + 6, ep->alpha);
875 png_save_uint_16(entrybuf + 8, ep->frequency);
876 }
877 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600878 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500879#else
880 ep=spalette->entries;
881 for (i=0; i>spalette->nentries; i++)
882 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500883 if (spalette->depth == 8)
884 {
885 entrybuf[0] = (png_byte)ep[i].red;
886 entrybuf[1] = (png_byte)ep[i].green;
887 entrybuf[2] = (png_byte)ep[i].blue;
888 entrybuf[3] = (png_byte)ep[i].alpha;
889 png_save_uint_16(entrybuf + 4, ep[i].frequency);
890 }
891 else
892 {
893 png_save_uint_16(entrybuf + 0, ep[i].red);
894 png_save_uint_16(entrybuf + 2, ep[i].green);
895 png_save_uint_16(entrybuf + 4, ep[i].blue);
896 png_save_uint_16(entrybuf + 6, ep[i].alpha);
897 png_save_uint_16(entrybuf + 8, ep[i].frequency);
898 }
899 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500900 }
901#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600902
903 png_write_chunk_end(png_ptr);
904 png_free(png_ptr, new_name);
905}
906#endif
907
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500908#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500909/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500910void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600911png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500912{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600913#ifdef PNG_USE_LOCAL_ARRAYS
914 PNG_sBIT;
915#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500916 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500917 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500918
Andreas Dilger47a0c421997-05-16 02:46:07 -0500919 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600920 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500921 if (color_type & PNG_COLOR_MASK_COLOR)
922 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600923 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500924
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500925 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
926 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600927 if (sbit->red == 0 || sbit->red > maxbits ||
928 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500929 sbit->blue == 0 || sbit->blue > maxbits)
930 {
931 png_warning(png_ptr, "Invalid sBIT depth specified");
932 return;
933 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500934 buf[0] = sbit->red;
935 buf[1] = sbit->green;
936 buf[2] = sbit->blue;
937 size = 3;
938 }
939 else
940 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500941 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
942 {
943 png_warning(png_ptr, "Invalid sBIT depth specified");
944 return;
945 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500946 buf[0] = sbit->gray;
947 size = 1;
948 }
949
950 if (color_type & PNG_COLOR_MASK_ALPHA)
951 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500952 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
953 {
954 png_warning(png_ptr, "Invalid sBIT depth specified");
955 return;
956 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500957 buf[size++] = sbit->alpha;
958 }
959
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600960 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500961}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500962#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500963
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500964#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500965/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600966#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500967void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500968png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600969 double red_x, double red_y, double green_x, double green_y,
970 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500971{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600972#ifdef PNG_USE_LOCAL_ARRAYS
973 PNG_cHRM;
974#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500975 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600976 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500977
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600979 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500980 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
981 white_x + white_y > 1.0)
982 {
983 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500984#if !defined(PNG_NO_CONSOLE_IO)
985 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600986#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500987 return;
988 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600989 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500990 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600991 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500992 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500993
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600994 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500995 {
996 png_warning(png_ptr, "Invalid cHRM red point specified");
997 return;
998 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600999 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001000 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001001 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001002 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001003
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001004 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001005 {
1006 png_warning(png_ptr, "Invalid cHRM green point specified");
1007 return;
1008 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001009 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001010 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001011 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001012 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001013
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001014 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001015 {
1016 png_warning(png_ptr, "Invalid cHRM blue point specified");
1017 return;
1018 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001019 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001020 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001021 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001022 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001023
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001024 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, 32);
Guy Schalnat0d580581995-07-20 02:43:20 -05001025}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001026#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001027#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001028void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001029png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1030 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1031 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1032 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001033{
1034#ifdef PNG_USE_LOCAL_ARRAYS
1035 PNG_cHRM;
1036#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001037 png_byte buf[32];
1038
1039 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001040 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001041 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
1042 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001043 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001044#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001045 fprintf(stderr,"white_x=%ld, white_y=%ld\n",(unsigned long)white_x,
1046 (unsigned long)white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001047#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001048 return;
1049 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001050 png_save_uint_32(buf, (png_uint_32)white_x);
1051 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001052
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001053 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001054 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001055 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001056 return;
1057 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001058 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1059 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001060
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001061 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001062 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001063 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001064 return;
1065 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001066 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1067 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001068
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001069 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001070 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001071 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001072 return;
1073 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001074 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1075 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001076
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001077 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, 32);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001078}
1079#endif
1080#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001081
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001082#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001083/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001084void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001085png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001086 int num_trans, int color_type)
1087{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001088#ifdef PNG_USE_LOCAL_ARRAYS
1089 PNG_tRNS;
1090#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001091 png_byte buf[6];
1092
Andreas Dilger47a0c421997-05-16 02:46:07 -05001093 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001094 if (color_type == PNG_COLOR_TYPE_PALETTE)
1095 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001096 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 {
1098 png_warning(png_ptr,"Invalid number of transparent colors specified");
1099 return;
1100 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001101 /* write the chunk out as it is */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001102 png_write_chunk(png_ptr, (png_bytep)png_tRNS,
1103 trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001104 }
1105 else if (color_type == PNG_COLOR_TYPE_GRAY)
1106 {
1107 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001108 if(tran->gray >= (1 << png_ptr->bit_depth))
1109 {
1110 png_warning(png_ptr,
1111 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1112 return;
1113 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001114 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001115 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001116 }
1117 else if (color_type == PNG_COLOR_TYPE_RGB)
1118 {
1119 /* three 16 bit values */
1120 png_save_uint_16(buf, tran->red);
1121 png_save_uint_16(buf + 2, tran->green);
1122 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001123 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001124 {
1125 png_warning(png_ptr,
1126 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1127 return;
1128 }
1129 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, 6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001130 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001131 else
1132 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001133 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001134 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001135}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001136#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001137
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001138#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001139/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001140void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001141png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001142{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001143#ifdef PNG_USE_LOCAL_ARRAYS
1144 PNG_bKGD;
1145#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001146 png_byte buf[6];
1147
Andreas Dilger47a0c421997-05-16 02:46:07 -05001148 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001149 if (color_type == PNG_COLOR_TYPE_PALETTE)
1150 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001151 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001152#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001153 (png_ptr->num_palette ||
1154 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001155#endif
1156 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001157 {
1158 png_warning(png_ptr, "Invalid background palette index");
1159 return;
1160 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001161 buf[0] = back->index;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001162 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001163 }
1164 else if (color_type & PNG_COLOR_MASK_COLOR)
1165 {
1166 png_save_uint_16(buf, back->red);
1167 png_save_uint_16(buf + 2, back->green);
1168 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001169 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001170 {
1171 png_warning(png_ptr,
1172 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1173 return;
1174 }
1175 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001176 }
1177 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001178 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001179 if(back->gray >= (1 << png_ptr->bit_depth))
1180 {
1181 png_warning(png_ptr,
1182 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1183 return;
1184 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001185 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001186 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001187 }
1188}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001189#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001190
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001191#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001192/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001193void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001194png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001195{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001196#ifdef PNG_USE_LOCAL_ARRAYS
1197 PNG_hIST;
1198#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001199 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001200 png_byte buf[3];
1201
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001203 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001204 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1206 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001207 png_warning(png_ptr, "Invalid number of histogram entries specified");
1208 return;
1209 }
1210
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001211 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
1212 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001213 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001214 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001215 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001216 png_write_chunk_data(png_ptr, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001217 }
1218 png_write_chunk_end(png_ptr);
1219}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001220#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001221
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001222#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1223 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001224/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1225 * and if invalid, correct the keyword rather than discarding the entire
1226 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1227 * length, forbids leading or trailing whitespace, multiple internal spaces,
1228 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 *
1230 * The new_key is allocated to hold the corrected keyword and must be freed
1231 * by the calling routine. This avoids problems with trying to write to
1232 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001234png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001235png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001238 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001239 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001240 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001241
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242 png_debug(1, "in png_check_keyword\n");
1243 *new_key = NULL;
1244
1245 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001247 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001248 return 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001249 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001250
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1252
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001253 *new_key = (png_charp)png_malloc_warn(png_ptr, key_len + 2);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001254 if (*new_key == NULL)
1255 {
1256 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001257 return 0;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001258 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001259
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001260 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001262 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001263 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001265#ifndef PNG_NO_STDIO
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001267
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001268 png_sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001269 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001270#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001271 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001272#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001273 *dp = ' ';
1274 }
1275 else
1276 {
1277 *dp = *kp;
1278 }
1279 }
1280 *dp = '\0';
1281
1282 /* Remove any trailing white space. */
1283 kp = *new_key + key_len - 1;
1284 if (*kp == ' ')
1285 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001286 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001287
1288 while (*kp == ' ')
1289 {
1290 *(kp--) = '\0';
1291 key_len--;
1292 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001293 }
1294
1295 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001296 kp = *new_key;
1297 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001298 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001299 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001300
1301 while (*kp == ' ')
1302 {
1303 kp++;
1304 key_len--;
1305 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001306 }
1307
Andreas Dilger47a0c421997-05-16 02:46:07 -05001308 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001309
Andreas Dilger47a0c421997-05-16 02:46:07 -05001310 /* Remove multiple internal spaces. */
1311 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001312 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001314 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 *(dp++) = *kp;
1316 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001317 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001318 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001319 {
1320 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001321 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001322 }
1323 else
1324 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001325 *(dp++) = *kp;
1326 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001327 }
1328 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001329 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001330 if(kwarn)
1331 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001332
1333 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001335 png_free(png_ptr, *new_key);
1336 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001337 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338 }
1339
1340 if (key_len > 79)
1341 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001342 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -06001343 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 key_len = 79;
1345 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001346
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001347 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001348}
1349#endif
1350
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001351#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001352/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001353void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001354png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001355 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001356{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001357#ifdef PNG_USE_LOCAL_ARRAYS
1358 PNG_tEXt;
1359#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001360 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001361 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001362
Andreas Dilger47a0c421997-05-16 02:46:07 -05001363 png_debug(1, "in png_write_tEXt\n");
1364 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1365 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001366 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001367 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001368 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001369
Andreas Dilger47a0c421997-05-16 02:46:07 -05001370 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001371 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001372 else
1373 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001374
1375 /* make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001376 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
1377 (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001378 /*
1379 * We leave it to the application to meet PNG-1.0 requirements on the
1380 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1381 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001382 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001383 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001384 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001385 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001387
Guy Schalnat0d580581995-07-20 02:43:20 -05001388 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001389 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001390}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001391#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001392
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001393#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001394/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001395void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001396png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001397 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001398{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001399#ifdef PNG_USE_LOCAL_ARRAYS
1400 PNG_zTXt;
1401#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001402 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001403 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001404 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001405 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001406
Andreas Dilger47a0c421997-05-16 02:46:07 -05001407 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001408
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001409 comp.num_output_ptr = 0;
1410 comp.max_output_ptr = 0;
1411 comp.output_ptr = NULL;
1412 comp.input = NULL;
1413 comp.input_len = 0;
1414
Andreas Dilger47a0c421997-05-16 02:46:07 -05001415 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001416 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001417 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001419 }
1420
Andreas Dilger47a0c421997-05-16 02:46:07 -05001421 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1422 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001423 png_write_tEXt(png_ptr, new_key, text, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001424 png_free(png_ptr, new_key);
1425 return;
1426 }
1427
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001428 text_len = png_strlen(text);
1429
Andreas Dilger47a0c421997-05-16 02:46:07 -05001430 png_free(png_ptr, new_key);
1431
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001432 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001433 text_len = png_text_compress(png_ptr, text, text_len, compression,
1434 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001435
1436 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001437 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1438 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001439 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001440 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001441 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001442 /* write compression */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001443 png_write_chunk_data(png_ptr, (png_bytep)buf, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001444 /* write the compressed data */
1445 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001446
Guy Schalnat0d580581995-07-20 02:43:20 -05001447 /* close the chunk */
1448 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001449}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001450#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001451
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001452#if defined(PNG_WRITE_iTXt_SUPPORTED)
1453/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001454void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001455png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001456 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457{
1458#ifdef PNG_USE_LOCAL_ARRAYS
1459 PNG_iTXt;
1460#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001461 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001462 png_charp new_lang, new_key;
1463 png_byte cbuf[2];
1464 compression_state comp;
1465
1466 png_debug(1, "in png_write_iTXt\n");
1467
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001468 comp.num_output_ptr = 0;
1469 comp.max_output_ptr = 0;
1470 comp.output_ptr = NULL;
1471 comp.input = NULL;
1472
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001473 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1474 {
1475 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1476 return;
1477 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001478 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001479 {
1480 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001481 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001482 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001483 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001484
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001485 if (lang_key == NULL)
1486 lang_key_len = 0;
1487 else
1488 lang_key_len = png_strlen(lang_key);
1489
1490 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001491 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001492 else
1493 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001494
1495 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001496 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1497 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001498
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001499
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001500 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001501 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001502
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001503 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1504 (png_uint_32)(
1505 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1506 + key_len
1507 + lang_len
1508 + lang_key_len
1509 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001510
1511 /*
1512 * We leave it to the application to meet PNG-1.0 requirements on the
1513 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1514 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1515 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1516 */
1517 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001518
1519 /* set the compression flag */
1520 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1521 compression == PNG_TEXT_COMPRESSION_NONE)
1522 cbuf[0] = 0;
1523 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1524 cbuf[0] = 1;
1525 /* set the compression method */
1526 cbuf[1] = 0;
1527 png_write_chunk_data(png_ptr, cbuf, 2);
1528
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001529 cbuf[0] = 0;
1530 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1531 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 -06001532 png_write_compressed_data_out(png_ptr, &comp);
1533
1534 png_write_chunk_end(png_ptr);
1535 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001536 if (new_lang)
1537 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001538}
1539#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001540
1541#if defined(PNG_WRITE_oFFs_SUPPORTED)
1542/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001543void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001544png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001545 int unit_type)
1546{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001547#ifdef PNG_USE_LOCAL_ARRAYS
1548 PNG_oFFs;
1549#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001550 png_byte buf[9];
1551
1552 png_debug(1, "in png_write_oFFs\n");
1553 if (unit_type >= PNG_OFFSET_LAST)
1554 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1555
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001556 png_save_int_32(buf, x_offset);
1557 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558 buf[8] = (png_byte)unit_type;
1559
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001560 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, 9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001561}
1562#endif
1563
1564#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001565/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001566void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001567png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1568 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1569{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001570#ifdef PNG_USE_LOCAL_ARRAYS
1571 PNG_pCAL;
1572#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001573 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001574 png_uint_32p params_len;
1575 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001576 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001577 int i;
1578
1579 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1580 if (type >= PNG_EQUATION_LAST)
1581 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1582
1583 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001584 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001585 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001586 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001587 total_len = purpose_len + units_len + 10;
1588
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001589 params_len = (png_uint_32p)png_malloc(png_ptr,
1590 (png_size_t)(nparams * sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001591
1592 /* Find the length of each parameter, making sure we don't count the
1593 null terminator for the last parameter. */
1594 for (i = 0; i < nparams; i++)
1595 {
1596 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001597 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
1598 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001599 total_len += (png_size_t)params_len[i];
1600 }
1601
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001602 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001603 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001604 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001605 png_save_int_32(buf, X0);
1606 png_save_int_32(buf + 4, X1);
1607 buf[8] = (png_byte)type;
1608 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001609 png_write_chunk_data(png_ptr, buf, 10);
1610 png_write_chunk_data(png_ptr, (png_bytep)units, units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001611
1612 png_free(png_ptr, new_purpose);
1613
1614 for (i = 0; i < nparams; i++)
1615 {
1616 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1617 (png_size_t)params_len[i]);
1618 }
1619
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001620 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001621 png_write_chunk_end(png_ptr);
1622}
1623#endif
1624
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001625#if defined(PNG_WRITE_sCAL_SUPPORTED)
1626/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001627#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001628void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001629png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001630{
1631#ifdef PNG_USE_LOCAL_ARRAYS
1632 PNG_sCAL;
1633#endif
1634 png_size_t total_len;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001635 char buf[64];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001636
1637 png_debug(1, "in png_write_sCAL\n");
1638
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001639 buf[0] = (char)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001640
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001641 png_sprintf(buf + 1, "%12.12e", width);
1642 total_len = 1 + png_strlen(buf + 1) + 1;
1643 png_sprintf(buf + total_len, "%12.12e", height);
1644 total_len += png_strlen(buf + total_len);
1645 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1646 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001647}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001648#else
1649#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001650void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001651png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001652 png_charp height)
1653{
1654#ifdef PNG_USE_LOCAL_ARRAYS
1655 PNG_sCAL;
1656#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001657 png_byte buf[64];
1658 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001659
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001660 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001661
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001662 wlen = png_strlen(width);
1663 hlen = png_strlen(height);
1664 total_len = wlen + hlen + 2;
1665 if (total_len > 64)
1666 {
1667 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1668 return;
1669 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001670
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001671 buf[0] = (png_byte)unit;
1672 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1673 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001674
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001675 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1676 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001677}
1678#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001679#endif
1680#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001681
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001682#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001683/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001684void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001685png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001686 png_uint_32 y_pixels_per_unit,
1687 int unit_type)
1688{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001689#ifdef PNG_USE_LOCAL_ARRAYS
1690 PNG_pHYs;
1691#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001692 png_byte buf[9];
1693
Andreas Dilger47a0c421997-05-16 02:46:07 -05001694 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001695 if (unit_type >= PNG_RESOLUTION_LAST)
1696 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1697
Guy Schalnat0d580581995-07-20 02:43:20 -05001698 png_save_uint_32(buf, x_pixels_per_unit);
1699 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001700 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001701
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001702 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, 9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001703}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001704#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001705
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001706#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001707/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1708 * or png_convert_from_time_t(), or fill in the structure yourself.
1709 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001710void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001711png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001712{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001713#ifdef PNG_USE_LOCAL_ARRAYS
1714 PNG_tIME;
1715#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001716 png_byte buf[7];
1717
Andreas Dilger47a0c421997-05-16 02:46:07 -05001718 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001719 if (mod_time->month > 12 || mod_time->month < 1 ||
1720 mod_time->day > 31 || mod_time->day < 1 ||
1721 mod_time->hour > 23 || mod_time->second > 60)
1722 {
1723 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1724 return;
1725 }
1726
Guy Schalnat0d580581995-07-20 02:43:20 -05001727 png_save_uint_16(buf, mod_time->year);
1728 buf[2] = mod_time->month;
1729 buf[3] = mod_time->day;
1730 buf[4] = mod_time->hour;
1731 buf[5] = mod_time->minute;
1732 buf[6] = mod_time->second;
1733
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001734 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, 7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001735}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001736#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001737
1738/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001739void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001740png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001741{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001742#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001743 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001744
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001745 /* start of interlace block */
1746 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001747
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001748 /* offset to next interlace block */
1749 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001750
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001751 /* start of interlace block in the y direction */
1752 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001753
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001754 /* offset to next interlace block in the y direction */
1755 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001756#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001757
Andreas Dilger47a0c421997-05-16 02:46:07 -05001758 png_size_t buf_size;
1759
1760 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001761 buf_size = (PNG_ROWBYTES(
1762 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763
Guy Schalnat0d580581995-07-20 02:43:20 -05001764 /* set up row buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001765 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001766 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001767
1768 /* set up filtering buffer, if using this filter */
1769 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001770 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001771 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001772 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001773 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001774 }
1775
Andreas Dilger47a0c421997-05-16 02:46:07 -05001776 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001777 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1778 {
1779 /* set up previous row buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001780 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001781 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001782
1783 if (png_ptr->do_filter & PNG_FILTER_UP)
1784 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001785 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001786 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001787 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001788 }
1789
1790 if (png_ptr->do_filter & PNG_FILTER_AVG)
1791 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001792 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1793 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001794 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001795 }
1796
1797 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1798 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001799 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001800 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001801 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001802 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001803 }
1804
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001805#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001806 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001807 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001808 {
1809 if (!(png_ptr->transformations & PNG_INTERLACE))
1810 {
1811 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1812 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001813 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1814 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001815 }
1816 else
1817 {
1818 png_ptr->num_rows = png_ptr->height;
1819 png_ptr->usr_width = png_ptr->width;
1820 }
1821 }
1822 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001823#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001824 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001825 png_ptr->num_rows = png_ptr->height;
1826 png_ptr->usr_width = png_ptr->width;
1827 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001828 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1829 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001830}
1831
Andreas Dilger47a0c421997-05-16 02:46:07 -05001832/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001833void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001834png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001835{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001836#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001837 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001838
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001839 /* start of interlace block */
1840 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001841
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001842 /* offset to next interlace block */
1843 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001844
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001845 /* start of interlace block in the y direction */
1846 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001847
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001848 /* offset to next interlace block in the y direction */
1849 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001850#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001851
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 int ret;
1853
Andreas Dilger47a0c421997-05-16 02:46:07 -05001854 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001855 /* next row */
1856 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001857
Guy Schalnat0d580581995-07-20 02:43:20 -05001858 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001859 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001860 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001861
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001862#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001863 /* if interlaced, go to next pass */
1864 if (png_ptr->interlaced)
1865 {
1866 png_ptr->row_number = 0;
1867 if (png_ptr->transformations & PNG_INTERLACE)
1868 {
1869 png_ptr->pass++;
1870 }
1871 else
1872 {
1873 /* loop until we find a non-zero width or height pass */
1874 do
1875 {
1876 png_ptr->pass++;
1877 if (png_ptr->pass >= 7)
1878 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001879 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001880 png_pass_inc[png_ptr->pass] - 1 -
1881 png_pass_start[png_ptr->pass]) /
1882 png_pass_inc[png_ptr->pass];
1883 png_ptr->num_rows = (png_ptr->height +
1884 png_pass_yinc[png_ptr->pass] - 1 -
1885 png_pass_ystart[png_ptr->pass]) /
1886 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001887 if (png_ptr->transformations & PNG_INTERLACE)
1888 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001889 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1890
1891 }
1892
Guy Schalnate5a37791996-06-05 15:50:50 -05001893 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001894 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001895 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001896 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001897 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001898 (PNG_ROWBYTES(png_ptr->usr_channels*
1899 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001900 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001901 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001902 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001903#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001904
1905 /* if we get here, we've just written the last row, so we need
1906 to flush the compressor */
1907 do
1908 {
1909 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001910 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001911 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001912 if (ret == Z_OK)
1913 {
1914 /* check to see if we need more room */
1915 if (!(png_ptr->zstream.avail_out))
1916 {
1917 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1918 png_ptr->zstream.next_out = png_ptr->zbuf;
1919 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1920 }
1921 }
1922 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001923 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001924 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001925 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001927 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001929 } while (ret != Z_STREAM_END);
1930
1931 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001932 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001933 {
1934 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001935 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 }
1937
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001938 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001939 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001940}
1941
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001942#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001943/* Pick out the correct pixels for the interlace pass.
1944 * The basic idea here is to go through the row with a source
1945 * pointer and a destination pointer (sp and dp), and copy the
1946 * correct pixels for the pass. As the row gets compacted,
1947 * sp will always be >= dp, so we should never overwrite anything.
1948 * See the default: case for the easiest code to understand.
1949 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001950void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001951png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001952{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001953#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001954 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001955
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001956 /* start of interlace block */
1957 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001958
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001959 /* offset to next interlace block */
1960 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001961#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001962
Andreas Dilger47a0c421997-05-16 02:46:07 -05001963 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001964 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001965#if defined(PNG_USELESS_TESTS_SUPPORTED)
1966 if (row != NULL && row_info != NULL && pass < 6)
1967#else
1968 if (pass < 6)
1969#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001971 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001972 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001973 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001974 case 1:
1975 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001976 png_bytep sp;
1977 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001978 int shift;
1979 int d;
1980 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001981 png_uint_32 i;
1982 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001983
1984 dp = row;
1985 d = 0;
1986 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001987 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001988 i += png_pass_inc[pass])
1989 {
1990 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001991 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001992 d |= (value << shift);
1993
1994 if (shift == 0)
1995 {
1996 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001997 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001998 d = 0;
1999 }
2000 else
2001 shift--;
2002
2003 }
2004 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002005 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002006 break;
2007 }
2008 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002009 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002010 png_bytep sp;
2011 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002012 int shift;
2013 int d;
2014 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002015 png_uint_32 i;
2016 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002017
2018 dp = row;
2019 shift = 6;
2020 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002021 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002022 i += png_pass_inc[pass])
2023 {
2024 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002025 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002026 d |= (value << shift);
2027
2028 if (shift == 0)
2029 {
2030 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002031 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002032 d = 0;
2033 }
2034 else
2035 shift -= 2;
2036 }
2037 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002038 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002039 break;
2040 }
2041 case 4:
2042 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002043 png_bytep sp;
2044 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002045 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002046 int d;
2047 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002048 png_uint_32 i;
2049 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002050
2051 dp = row;
2052 shift = 4;
2053 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002054 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002055 i += png_pass_inc[pass])
2056 {
2057 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002058 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002059 d |= (value << shift);
2060
2061 if (shift == 0)
2062 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002063 shift = 4;
2064 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002065 d = 0;
2066 }
2067 else
2068 shift -= 4;
2069 }
2070 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002071 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002072 break;
2073 }
2074 default:
2075 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002076 png_bytep sp;
2077 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002078 png_uint_32 i;
2079 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002080 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002081
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002082 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002083 dp = row;
2084 /* find out how many bytes each pixel takes up */
2085 pixel_bytes = (row_info->pixel_depth >> 3);
2086 /* loop through the row, only looking at the pixels that
2087 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002088 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002089 i += png_pass_inc[pass])
2090 {
2091 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002092 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002093 /* move the pixel */
2094 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002095 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002096 /* next pixel */
2097 dp += pixel_bytes;
2098 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002099 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002100 }
2101 }
2102 /* set new row width */
2103 row_info->width = (row_info->width +
2104 png_pass_inc[pass] - 1 -
2105 png_pass_start[pass]) /
2106 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002107 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2108 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002109 }
2110}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002111#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002112
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113/* This filters the row, chooses which filter to use, if it has not already
2114 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002115 * chosen filter.
2116 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002117#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002118#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002119#define PNG_LOMASK ((png_uint_32)0xffffL)
2120#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002121void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002122png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002123{
Guy Schalnate5a37791996-06-05 15:50:50 -05002124 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002125 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002126 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002127 png_uint_32 row_bytes = row_info->rowbytes;
2128#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2129 int num_p_filters = (int)png_ptr->num_prev_filters;
2130#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002131
Andreas Dilger47a0c421997-05-16 02:46:07 -05002132 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002133 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002134 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002135
2136 prev_row = png_ptr->prev_row;
2137 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002138 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002139
Andreas Dilger47a0c421997-05-16 02:46:07 -05002140 /* The prediction method we use is to find which method provides the
2141 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002142 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002143 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002144 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002146 * predictive" method (not implemented yet), which does test compressions
2147 * of lines using different filter methods, and then chooses the
2148 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002149 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002150 *
2151 * GRR 980525: consider also
2152 * (1) minimum sum of absolute differences from running average (i.e.,
2153 * keep running sum of non-absolute differences & count of bytes)
2154 * [track dispersion, too? restart average if dispersion too large?]
2155 * (1b) minimum sum of absolute differences from sliding average, probably
2156 * with window size <= deflate window (usually 32K)
2157 * (2) minimum sum of squared differences from zero or running average
2158 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002159 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002160
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002161
Guy Schalnate5a37791996-06-05 15:50:50 -05002162 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002163 * that has been chosen, as it doesn't actually do anything to the data.
2164 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002165 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002166 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002167 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002168 png_bytep rp;
2169 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002170 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002171 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002172
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002173 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002174 {
2175 v = *rp;
2176 sum += (v < 128) ? v : 256 - v;
2177 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002178
2179#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2180 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2181 {
2182 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002183 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 sumlo = sum & PNG_LOMASK;
2185 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2186
2187 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002188 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002189 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002190 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002191 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002192 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002193 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002194 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002195 PNG_WEIGHT_SHIFT;
2196 }
2197 }
2198
2199 /* Factor in the cost of this filter (this is here for completeness,
2200 * but it makes no sense to have a "cost" for the NONE filter, as
2201 * it has the minimum possible computational cost - none).
2202 */
2203 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2204 PNG_COST_SHIFT;
2205 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2206 PNG_COST_SHIFT;
2207
2208 if (sumhi > PNG_HIMASK)
2209 sum = PNG_MAXSUM;
2210 else
2211 sum = (sumhi << PNG_HISHIFT) + sumlo;
2212 }
2213#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002214 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002215 }
2216
Guy Schalnate5a37791996-06-05 15:50:50 -05002217 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002218 if (filter_to_do == PNG_FILTER_SUB)
2219 /* it's the only filter so no testing is needed */
2220 {
2221 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002222 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002223 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2224 i++, rp++, dp++)
2225 {
2226 *dp = *rp;
2227 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002228 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002229 i++, rp++, lp++, dp++)
2230 {
2231 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2232 }
2233 best_row = png_ptr->sub_row;
2234 }
2235
2236 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002237 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002238 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002239 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002240 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002241 int v;
2242
2243#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002244 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 * would reduce the sum of this filter, so that we can do the
2246 * early exit comparison without scaling the sum each time.
2247 */
2248 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2249 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002250 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 png_uint_32 lmhi, lmlo;
2252 lmlo = lmins & PNG_LOMASK;
2253 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2254
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002255 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002256 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002257 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002258 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002260 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 PNG_WEIGHT_SHIFT;
2263 }
2264 }
2265
2266 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2267 PNG_COST_SHIFT;
2268 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2269 PNG_COST_SHIFT;
2270
2271 if (lmhi > PNG_HIMASK)
2272 lmins = PNG_MAXSUM;
2273 else
2274 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2275 }
2276#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002277
Guy Schalnate5a37791996-06-05 15:50:50 -05002278 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2279 i++, rp++, dp++)
2280 {
2281 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002282
Guy Schalnate5a37791996-06-05 15:50:50 -05002283 sum += (v < 128) ? v : 256 - v;
2284 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002285 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002286 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002287 {
2288 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002289
Guy Schalnate5a37791996-06-05 15:50:50 -05002290 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002291
2292 if (sum > lmins) /* We are already worse, don't continue. */
2293 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002294 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295
2296#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2297 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2298 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002299 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300 png_uint_32 sumhi, sumlo;
2301 sumlo = sum & PNG_LOMASK;
2302 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2303
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002304 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002305 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002306 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002307 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002308 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002309 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002310 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002311 PNG_WEIGHT_SHIFT;
2312 }
2313 }
2314
2315 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2316 PNG_COST_SHIFT;
2317 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2318 PNG_COST_SHIFT;
2319
2320 if (sumhi > PNG_HIMASK)
2321 sum = PNG_MAXSUM;
2322 else
2323 sum = (sumhi << PNG_HISHIFT) + sumlo;
2324 }
2325#endif
2326
Guy Schalnate5a37791996-06-05 15:50:50 -05002327 if (sum < mins)
2328 {
2329 mins = sum;
2330 best_row = png_ptr->sub_row;
2331 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002332 }
2333
Guy Schalnate5a37791996-06-05 15:50:50 -05002334 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002335 if (filter_to_do == PNG_FILTER_UP)
2336 {
2337 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002338 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002339
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002340 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002341 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002342 i++, rp++, pp++, dp++)
2343 {
2344 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2345 }
2346 best_row = png_ptr->up_row;
2347 }
2348
2349 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002350 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002351 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354 int v;
2355
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002356
Andreas Dilger47a0c421997-05-16 02:46:07 -05002357#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2358 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2359 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002360 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002361 png_uint_32 lmhi, lmlo;
2362 lmlo = lmins & PNG_LOMASK;
2363 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2364
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002365 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002366 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002367 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002368 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002369 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002370 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002372 PNG_WEIGHT_SHIFT;
2373 }
2374 }
2375
2376 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2377 PNG_COST_SHIFT;
2378 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2379 PNG_COST_SHIFT;
2380
2381 if (lmhi > PNG_HIMASK)
2382 lmins = PNG_MAXSUM;
2383 else
2384 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2385 }
2386#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002387
2388 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002389 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002390 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002391 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002392
2393 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002394
2395 if (sum > lmins) /* We are already worse, don't continue. */
2396 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002397 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398
2399#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2400 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2401 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002402 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 png_uint_32 sumhi, sumlo;
2404 sumlo = sum & PNG_LOMASK;
2405 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2406
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002407 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002408 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002409 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002410 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002411 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002412 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002413 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002414 PNG_WEIGHT_SHIFT;
2415 }
2416 }
2417
2418 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2419 PNG_COST_SHIFT;
2420 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2421 PNG_COST_SHIFT;
2422
2423 if (sumhi > PNG_HIMASK)
2424 sum = PNG_MAXSUM;
2425 else
2426 sum = (sumhi << PNG_HISHIFT) + sumlo;
2427 }
2428#endif
2429
Guy Schalnate5a37791996-06-05 15:50:50 -05002430 if (sum < mins)
2431 {
2432 mins = sum;
2433 best_row = png_ptr->up_row;
2434 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002435 }
2436
Guy Schalnate5a37791996-06-05 15:50:50 -05002437 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002438 if (filter_to_do == PNG_FILTER_AVG)
2439 {
2440 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002441 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002442 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002443 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002444 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002445 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002446 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002448 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2450 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002451 }
2452 best_row = png_ptr->avg_row;
2453 }
2454
2455 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002456 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002457 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 int v;
2461
2462#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2463 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2464 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002465 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466 png_uint_32 lmhi, lmlo;
2467 lmlo = lmins & PNG_LOMASK;
2468 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2469
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002470 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002471 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002472 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002473 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002474 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002475 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002476 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002477 PNG_WEIGHT_SHIFT;
2478 }
2479 }
2480
2481 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2482 PNG_COST_SHIFT;
2483 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2484 PNG_COST_SHIFT;
2485
2486 if (lmhi > PNG_HIMASK)
2487 lmins = PNG_MAXSUM;
2488 else
2489 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2490 }
2491#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002492
2493 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002494 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002495 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002496 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002497
2498 sum += (v < 128) ? v : 256 - v;
2499 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002501 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002502 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002503 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002504
2505 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002506
2507 if (sum > lmins) /* We are already worse, don't continue. */
2508 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002509 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510
2511#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2512 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2513 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002514 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002515 png_uint_32 sumhi, sumlo;
2516 sumlo = sum & PNG_LOMASK;
2517 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2518
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002519 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002520 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002521 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002522 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002523 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002524 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002525 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002526 PNG_WEIGHT_SHIFT;
2527 }
2528 }
2529
2530 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2531 PNG_COST_SHIFT;
2532 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2533 PNG_COST_SHIFT;
2534
2535 if (sumhi > PNG_HIMASK)
2536 sum = PNG_MAXSUM;
2537 else
2538 sum = (sumhi << PNG_HISHIFT) + sumlo;
2539 }
2540#endif
2541
Guy Schalnate5a37791996-06-05 15:50:50 -05002542 if (sum < mins)
2543 {
2544 mins = sum;
2545 best_row = png_ptr->avg_row;
2546 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002547 }
2548
Andreas Dilger47a0c421997-05-16 02:46:07 -05002549 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002550 if (filter_to_do == PNG_FILTER_PAETH)
2551 {
2552 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002553 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002554 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002555 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002556 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002557 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002558 }
2559
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002560 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002561 {
2562 int a, b, c, pa, pb, pc, p;
2563
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002564 b = *pp++;
2565 c = *cp++;
2566 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002567
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002568 p = b - c;
2569 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002570
2571#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002572 pa = abs(p);
2573 pb = abs(pc);
2574 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002575#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002576 pa = p < 0 ? -p : p;
2577 pb = pc < 0 ? -pc : pc;
2578 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002579#endif
2580
2581 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2582
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002583 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002584 }
2585 best_row = png_ptr->paeth_row;
2586 }
2587
2588 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002589 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002590 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002591 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002593 int v;
2594
2595#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2596 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2597 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002598 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002599 png_uint_32 lmhi, lmlo;
2600 lmlo = lmins & PNG_LOMASK;
2601 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2602
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002603 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002604 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002605 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002606 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002607 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002608 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002609 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002610 PNG_WEIGHT_SHIFT;
2611 }
2612 }
2613
2614 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2615 PNG_COST_SHIFT;
2616 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2617 PNG_COST_SHIFT;
2618
2619 if (lmhi > PNG_HIMASK)
2620 lmins = PNG_MAXSUM;
2621 else
2622 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2623 }
2624#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002625
2626 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002627 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002628 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002629 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002630
2631 sum += (v < 128) ? v : 256 - v;
2632 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002633
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002634 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002635 {
2636 int a, b, c, pa, pb, pc, p;
2637
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002638 b = *pp++;
2639 c = *cp++;
2640 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002641
2642#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002643 p = b - c;
2644 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002645#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002646 pa = abs(p);
2647 pb = abs(pc);
2648 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002649#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002650 pa = p < 0 ? -p : p;
2651 pb = pc < 0 ? -pc : pc;
2652 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002653#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002654 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2655#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002656 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002657 pa = abs(p - a);
2658 pb = abs(p - b);
2659 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002660 if (pa <= pb && pa <= pc)
2661 p = a;
2662 else if (pb <= pc)
2663 p = b;
2664 else
2665 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002666#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002667
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002668 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002669
2670 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002671
2672 if (sum > lmins) /* We are already worse, don't continue. */
2673 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002674 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002675
2676#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2677 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2678 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002679 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002680 png_uint_32 sumhi, sumlo;
2681 sumlo = sum & PNG_LOMASK;
2682 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2683
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002684 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002685 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002686 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002687 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002688 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002689 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002690 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002691 PNG_WEIGHT_SHIFT;
2692 }
2693 }
2694
2695 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2696 PNG_COST_SHIFT;
2697 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2698 PNG_COST_SHIFT;
2699
2700 if (sumhi > PNG_HIMASK)
2701 sum = PNG_MAXSUM;
2702 else
2703 sum = (sumhi << PNG_HISHIFT) + sumlo;
2704 }
2705#endif
2706
Guy Schalnate5a37791996-06-05 15:50:50 -05002707 if (sum < mins)
2708 {
2709 best_row = png_ptr->paeth_row;
2710 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002711 }
2712
Andreas Dilger47a0c421997-05-16 02:46:07 -05002713 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002714
Guy Schalnate5a37791996-06-05 15:50:50 -05002715 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002716
2717#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2718 /* Save the type of filter we picked this time for future calculations */
2719 if (png_ptr->num_prev_filters > 0)
2720 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002721 int j;
2722 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002723 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002724 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002725 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002726 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002727 }
2728#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002729}
2730
2731
Andreas Dilger47a0c421997-05-16 02:46:07 -05002732/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002733void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002734png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2735{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002736 png_debug(1, "in png_write_filtered_row\n");
2737 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002738 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002739
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002740 png_ptr->zstream.next_in = filtered_row;
2741 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002742 /* repeat until we have compressed all the data */
2743 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002744 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002745 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002746
Guy Schalnate5a37791996-06-05 15:50:50 -05002747 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002748 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002749 /* check for compression errors */
2750 if (ret != Z_OK)
2751 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002752 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002753 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002754 else
2755 png_error(png_ptr, "zlib error");
2756 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002757
Guy Schalnate5a37791996-06-05 15:50:50 -05002758 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002759 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002760 {
2761 /* write the IDAT and reset the zlib output buffer */
2762 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002763 png_ptr->zstream.next_out = png_ptr->zbuf;
2764 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002765 }
2766 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002767 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002768
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002769 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002770 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002771 {
2772 png_bytep tptr;
2773
2774 tptr = png_ptr->prev_row;
2775 png_ptr->prev_row = png_ptr->row_buf;
2776 png_ptr->row_buf = tptr;
2777 }
2778
Guy Schalnate5a37791996-06-05 15:50:50 -05002779 /* finish row - updates counters and flushes zlib if last row */
2780 png_write_finish_row(png_ptr);
2781
2782#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2783 png_ptr->flush_rows++;
2784
2785 if (png_ptr->flush_dist > 0 &&
2786 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002787 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002788 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002789 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002790#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002791}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002792#endif /* PNG_WRITE_SUPPORTED */