blob: ea58d15f7286ca9829f0706566c39632fc37c98d [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-Pehrson5e5c1e12000-11-10 12:26:19 -06004 * libpng 1.0.9beta1 - November 10, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06006 * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050018void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
21 buf[0] = (png_byte)((i >> 24) & 0xff);
22 buf[1] = (png_byte)((i >> 16) & 0xff);
23 buf[2] = (png_byte)((i >> 8) & 0xff);
24 buf[3] = (png_byte)(i & 0xff);
25}
26
Andreas Dilger47a0c421997-05-16 02:46:07 -050027#if defined(PNG_WRITE_pCAL_SUPPORTED)
28/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050032void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40#endif
41
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042/* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
45 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050046void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050048{
49 buf[0] = (png_byte)((i >> 8) & 0xff);
50 buf[1] = (png_byte)(i & 0xff);
51}
52
Andreas Dilger47a0c421997-05-16 02:46:07 -050053/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
60 * functions instead.
61 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050062void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050065{
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050069}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
74 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050075void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050078{
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050080 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050081
Guy Schalnat0d580581995-07-20 02:43:20 -050082 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050083 png_save_uint_32(buf, length);
84 png_write_data(png_ptr, buf, (png_size_t)4);
85
Guy Schalnat0d580581995-07-20 02:43:20 -050086 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050088 /* reset the crc and run it over the chunk name */
89 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050091}
92
Andreas Dilger47a0c421997-05-16 02:46:07 -050093/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060094 * Note that multiple calls to this function are allowed, and that the
95 * sum of the lengths from these calls *must* add up to the total_length
96 * given to png_write_chunk_start().
97 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050098void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050099png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 /* write the data, and run the CRC over it */
102 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103 {
104 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600105 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 }
107}
108
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500110void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600111png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 png_byte buf[4];
114
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117
118 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119}
120
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600121/* Simple function to write the signature. If we have already written
122 * the magic bytes of the signature, or more likely, the PNG stream is
123 * being embedded into another stream and doesn't need its own signature,
124 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600125 * bytes have already been written.
126 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500127void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600128png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500129{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600130 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600132 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 (png_size_t)8 - png_ptr->sig_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500134}
135
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600136#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600137/*
138 * This pair of functions encapsulates the operation of (a) compressing a
139 * text string, and (b) issuing it later as a series of chunk data writes.
140 * The compression_state structure is shared context for these functions
141 * set up by the caller in order to make the whole mess thread-safe.
142 */
143
144typedef struct
145{
146 char *input; /* the uncompressed input data */
147 int input_len; /* its length */
148 int num_output_ptr; /* number of output pointers used */
149 int max_output_ptr; /* size of output_ptr */
150 png_charpp output_ptr; /* array of pointers to output */
151} compression_state;
152
153/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500154static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600155png_text_compress(png_structp png_ptr,
156 png_charp text, png_size_t text_len, int compression,
157 compression_state *comp)
158{
159 int ret;
160
161 comp->num_output_ptr = comp->max_output_ptr = 0;
162 comp->output_ptr = NULL;
163 comp->input = NULL;
164
165 /* we may just want to pass the text right through */
166 if (compression == PNG_TEXT_COMPRESSION_NONE)
167 {
168 comp->input = text;
169 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500170 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600171 }
172
173 if (compression >= PNG_TEXT_COMPRESSION_LAST)
174 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500175#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600176 char msg[50];
177 sprintf(msg, "Unknown compression type %d", compression);
178 png_warning(png_ptr, msg);
179#else
180 png_warning(png_ptr, "Unknown compression type");
181#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600182 }
183
184 /* We can't write the chunk until we find out how much data we have,
185 * which means we need to run the compressor first and save the
186 * output. This shouldn't be a problem, as the vast majority of
187 * comments should be reasonable, but we will set up an array of
188 * malloc'd pointers to be sure.
189 *
190 * If we knew the application was well behaved, we could simplify this
191 * greatly by assuming we can always malloc an output buffer large
192 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
193 * and malloc this directly. The only time this would be a bad idea is
194 * if we can't malloc more than 64K and we have 64K of random input
195 * data, or if the input string is incredibly large (although this
196 * wouldn't cause a failure, just a slowdown due to swapping).
197 */
198
199 /* set up the compression buffers */
200 png_ptr->zstream.avail_in = (uInt)text_len;
201 png_ptr->zstream.next_in = (Bytef *)text;
202 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
203 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
204
205 /* this is the same compression loop as in png_write_row() */
206 do
207 {
208 /* compress the data */
209 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
210 if (ret != Z_OK)
211 {
212 /* error */
213 if (png_ptr->zstream.msg != NULL)
214 png_error(png_ptr, png_ptr->zstream.msg);
215 else
216 png_error(png_ptr, "zlib error");
217 }
218 /* check to see if we need more room */
219 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
220 {
221 /* make sure the output array has room */
222 if (comp->num_output_ptr >= comp->max_output_ptr)
223 {
224 int old_max;
225
226 old_max = comp->max_output_ptr;
227 comp->max_output_ptr = comp->num_output_ptr + 4;
228 if (comp->output_ptr != NULL)
229 {
230 png_charpp old_ptr;
231
232 old_ptr = comp->output_ptr;
233 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
234 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
235 png_memcpy(comp->output_ptr, old_ptr,
236 old_max * sizeof (png_charp));
237 png_free(png_ptr, old_ptr);
238 }
239 else
240 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
241 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
242 }
243
244 /* save the data */
245 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
246 (png_uint_32)png_ptr->zbuf_size);
247 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
248 png_ptr->zbuf_size);
249 comp->num_output_ptr++;
250
251 /* and reset the buffer */
252 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
253 png_ptr->zstream.next_out = png_ptr->zbuf;
254 }
255 /* continue until we don't have any more to compress */
256 } while (png_ptr->zstream.avail_in);
257
258 /* finish the compression */
259 do
260 {
261 /* tell zlib we are finished */
262 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500263
264 if (ret == Z_OK)
265 {
266 /* check to see if we need more room */
267 if (!(png_ptr->zstream.avail_out))
268 {
269 /* check to make sure our output array has room */
270 if (comp->num_output_ptr >= comp->max_output_ptr)
271 {
272 int old_max;
273
274 old_max = comp->max_output_ptr;
275 comp->max_output_ptr = comp->num_output_ptr + 4;
276 if (comp->output_ptr != NULL)
277 {
278 png_charpp old_ptr;
279
280 old_ptr = comp->output_ptr;
281 /* This could be optimized to realloc() */
282 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
283 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
284 png_memcpy(comp->output_ptr, old_ptr,
285 old_max * sizeof (png_charp));
286 png_free(png_ptr, old_ptr);
287 }
288 else
289 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
290 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
291 }
292
293 /* save off the data */
294 comp->output_ptr[comp->num_output_ptr] =
295 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
296 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
297 png_ptr->zbuf_size);
298 comp->num_output_ptr++;
299
300 /* and reset the buffer pointers */
301 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
302 png_ptr->zstream.next_out = png_ptr->zbuf;
303 }
304 }
305 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306 {
307 /* we got an error */
308 if (png_ptr->zstream.msg != NULL)
309 png_error(png_ptr, png_ptr->zstream.msg);
310 else
311 png_error(png_ptr, "zlib error");
312 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600313 } while (ret != Z_STREAM_END);
314
315 /* text length is number of buffers plus last buffer */
316 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
317 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
318 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
319
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500320 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600321}
322
323/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500324static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600325png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
326{
327 int i;
328
329 /* handle the no-compression case */
330 if (comp->input)
331 {
332 png_write_chunk_data(png_ptr, (png_bytep)comp->input, comp->input_len);
333 return;
334 }
335
336 /* write saved output buffers, if any */
337 for (i = 0; i < comp->num_output_ptr; i++)
338 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600339 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
340 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600341 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500342 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600343 }
344 if (comp->max_output_ptr != 0)
345 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500346 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600347 /* write anything left in zbuf */
348 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
349 png_write_chunk_data(png_ptr, png_ptr->zbuf,
350 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
351
352 /* reset zlib for another zTXt/iTXt or the image data */
353 deflateReset(&png_ptr->zstream);
354
355}
356#endif
357
Guy Schalnat0d580581995-07-20 02:43:20 -0500358/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600359 * information. Note that the rest of this code depends upon this
360 * information being correct.
361 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500362void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600363png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500364 int bit_depth, int color_type, int compression_type, int filter_type,
365 int interlace_type)
366{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600367#ifdef PNG_USE_LOCAL_ARRAYS
368 PNG_IHDR;
369#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500370 png_byte buf[13]; /* buffer to store the IHDR info */
371
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500373 /* Check that we have valid input data from the application info */
374 switch (color_type)
375 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500377 switch (bit_depth)
378 {
379 case 1:
380 case 2:
381 case 4:
382 case 8:
383 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500385 }
386 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 if (bit_depth != 8 && bit_depth != 16)
389 png_error(png_ptr, "Invalid bit depth for RGB image");
390 png_ptr->channels = 3;
391 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500393 switch (bit_depth)
394 {
395 case 1:
396 case 2:
397 case 4:
398 case 8: png_ptr->channels = 1; break;
399 default: png_error(png_ptr, "Invalid bit depth for paletted image");
400 }
401 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500403 if (bit_depth != 8 && bit_depth != 16)
404 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
405 png_ptr->channels = 2;
406 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500408 if (bit_depth != 8 && bit_depth != 16)
409 png_error(png_ptr, "Invalid bit depth for RGBA image");
410 png_ptr->channels = 4;
411 break;
412 default:
413 png_error(png_ptr, "Invalid image color type specified");
414 }
415
Andreas Dilger47a0c421997-05-16 02:46:07 -0500416 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500417 {
418 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500420 }
421
Andreas Dilger47a0c421997-05-16 02:46:07 -0500422 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500423 {
424 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 }
427
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600428#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 if (interlace_type != PNG_INTERLACE_NONE &&
430 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500431 {
432 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500434 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600435#else
436 interlace_type=PNG_INTERLACE_NONE;
437#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500438
439 /* save off the relevent information */
440 png_ptr->bit_depth = (png_byte)bit_depth;
441 png_ptr->color_type = (png_byte)color_type;
442 png_ptr->interlaced = (png_byte)interlace_type;
443 png_ptr->width = width;
444 png_ptr->height = height;
445
446 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500447 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500448 /* set the usr info, so any transformations can modify it */
449 png_ptr->usr_width = png_ptr->width;
450 png_ptr->usr_bit_depth = png_ptr->bit_depth;
451 png_ptr->usr_channels = png_ptr->channels;
452
Guy Schalnat0d580581995-07-20 02:43:20 -0500453 /* pack the header information into the buffer */
454 png_save_uint_32(buf, width);
455 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600456 buf[8] = (png_byte)bit_depth;
457 buf[9] = (png_byte)color_type;
458 buf[10] = (png_byte)compression_type;
459 buf[11] = (png_byte)filter_type;
460 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500461
462 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600463 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500464
Andreas Dilger47a0c421997-05-16 02:46:07 -0500465 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600466 png_ptr->zstream.zalloc = png_zalloc;
467 png_ptr->zstream.zfree = png_zfree;
468 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500469 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500470 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
472 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500473 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500474 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500475 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500476 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500477 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500478 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500480 png_ptr->zlib_strategy = Z_FILTERED;
481 else
482 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
483 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500484 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500485 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500486 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500487 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500488 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500489 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500490 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500491 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600492 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493 png_ptr->zlib_method, png_ptr->zlib_window_bits,
494 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600495 png_ptr->zstream.next_out = png_ptr->zbuf;
496 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500497
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500499}
500
501/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500502 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600503 * structure.
504 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500505void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500506png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500507{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600508#ifdef PNG_USE_LOCAL_ARRAYS
509 PNG_PLTE;
510#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500511 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600512 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500513 png_byte buf[3];
514
Andreas Dilger47a0c421997-05-16 02:46:07 -0500515 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500516 if ((
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600517#if defined(PNG_MNG_FEATURES_SUPPORTED)
518 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500519#endif
520 num_pal == 0) || num_pal > 256)
521 {
522 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
523 {
524 png_error(png_ptr, "Invalid number of colors in palette");
525 }
526 else
527 {
528 png_warning(png_ptr, "Invalid number of colors in palette");
529 return;
530 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500531 }
532
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533 png_ptr->num_palette = (png_uint_16)num_pal;
534 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500535
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600536 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500537#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500539 {
540 buf[0] = pal_ptr->red;
541 buf[1] = pal_ptr->green;
542 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500544 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500545#else
546 /* This is a little slower but some buggy compilers need to do this instead */
547 pal_ptr=palette;
548 for (i = 0; i < num_pal; i++)
549 {
550 buf[0] = pal_ptr[i].red;
551 buf[1] = pal_ptr[i].green;
552 buf[2] = pal_ptr[i].blue;
553 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
554 }
555#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500556 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500557 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500558}
559
560/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500561void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500562png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500563{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600564#ifdef PNG_USE_LOCAL_ARRAYS
565 PNG_IDAT;
566#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500567 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600568 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500569 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500570}
571
572/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500573void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600574png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500575{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600576#ifdef PNG_USE_LOCAL_ARRAYS
577 PNG_IEND;
578#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600580 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600581 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500582}
583
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500584#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500585/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600586#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500587void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500589{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600590#ifdef PNG_USE_LOCAL_ARRAYS
591 PNG_gAMA;
592#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500593 png_uint_32 igamma;
594 png_byte buf[4];
595
Andreas Dilger47a0c421997-05-16 02:46:07 -0500596 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600597 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600598 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500599 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600600 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500601}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500602#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600603#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600605png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600606{
607#ifdef PNG_USE_LOCAL_ARRAYS
608 PNG_gAMA;
609#endif
610 png_byte buf[4];
611
612 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600613 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600614 png_save_uint_32(buf, file_gamma);
615 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
616}
617#endif
618#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500619
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600620#if defined(PNG_WRITE_sRGB_SUPPORTED)
621/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500622void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600623png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600624{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600625#ifdef PNG_USE_LOCAL_ARRAYS
626 PNG_sRGB;
627#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600628 png_byte buf[1];
629
630 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600631 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600632 png_warning(png_ptr,
633 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600634 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600635 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600636}
637#endif
638
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600639#if defined(PNG_WRITE_iCCP_SUPPORTED)
640/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500641void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600642png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
643 png_charp profile, int profile_len)
644{
645#ifdef PNG_USE_LOCAL_ARRAYS
646 PNG_iCCP;
647#endif
648 png_size_t name_len;
649 png_charp new_name;
650 compression_state comp;
651
652 png_debug(1, "in png_write_iCCP\n");
653 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
654 &new_name)) == 0)
655 {
656 png_warning(png_ptr, "Empty keyword in iCCP chunk");
657 return;
658 }
659
660 if (compression_type)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600661 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600662
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500663 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600664 profile_len = 0;
665
666 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500667 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
668 PNG_TEXT_COMPRESSION_zTXt, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600669
670 /* make sure we include the NULL after the name and the compression type */
671 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
672 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600673 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600674 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
675
676 if (profile_len)
677 png_write_compressed_data_out(png_ptr, &comp);
678
679 png_write_chunk_end(png_ptr);
680 png_free(png_ptr, new_name);
681}
682#endif
683
684#if defined(PNG_WRITE_sPLT_SUPPORTED)
685/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500686void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600687png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600688{
689#ifdef PNG_USE_LOCAL_ARRAYS
690 PNG_sPLT;
691#endif
692 png_size_t name_len;
693 png_charp new_name;
694 png_byte entrybuf[10];
695 int entry_size = (spalette->depth == 8 ? 6 : 10);
696 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600697 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500698#ifdef PNG_NO_POINTER_INDEXING
699 int i;
700#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600701
702 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600703 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
704 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600705 {
706 png_warning(png_ptr, "Empty keyword in sPLT chunk");
707 return;
708 }
709
710 /* make sure we include the NULL after the name */
711 png_write_chunk_start(png_ptr, (png_bytep) png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600712 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600713 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600714 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600715
716 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500717#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600718 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
719 {
720 if (spalette->depth == 8)
721 {
722 entrybuf[0] = (png_byte)ep->red;
723 entrybuf[1] = (png_byte)ep->green;
724 entrybuf[2] = (png_byte)ep->blue;
725 entrybuf[3] = (png_byte)ep->alpha;
726 png_save_uint_16(entrybuf + 4, ep->frequency);
727 }
728 else
729 {
730 png_save_uint_16(entrybuf + 0, ep->red);
731 png_save_uint_16(entrybuf + 2, ep->green);
732 png_save_uint_16(entrybuf + 4, ep->blue);
733 png_save_uint_16(entrybuf + 6, ep->alpha);
734 png_save_uint_16(entrybuf + 8, ep->frequency);
735 }
736 png_write_chunk_data(png_ptr, entrybuf, entry_size);
737 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500738#else
739 ep=spalette->entries;
740 for (i=0; i>spalette->nentries; i++)
741 {
742 if (spalette->depth == 8)
743 {
744 entrybuf[0] = (png_byte)ep[i].red;
745 entrybuf[1] = (png_byte)ep[i].green;
746 entrybuf[2] = (png_byte)ep[i].blue;
747 entrybuf[3] = (png_byte)ep[i].alpha;
748 png_save_uint_16(entrybuf + 4, ep[i].frequency);
749 }
750 else
751 {
752 png_save_uint_16(entrybuf + 0, ep[i].red);
753 png_save_uint_16(entrybuf + 2, ep[i].green);
754 png_save_uint_16(entrybuf + 4, ep[i].blue);
755 png_save_uint_16(entrybuf + 6, ep[i].alpha);
756 png_save_uint_16(entrybuf + 8, ep[i].frequency);
757 }
758 png_write_chunk_data(png_ptr, entrybuf, entry_size);
759 }
760#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600761
762 png_write_chunk_end(png_ptr);
763 png_free(png_ptr, new_name);
764}
765#endif
766
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500767#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500768/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500769void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600770png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500771{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600772#ifdef PNG_USE_LOCAL_ARRAYS
773 PNG_sBIT;
774#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500775 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500776 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500777
Andreas Dilger47a0c421997-05-16 02:46:07 -0500778 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600779 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500780 if (color_type & PNG_COLOR_MASK_COLOR)
781 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600782 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500783
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500784 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
785 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600786 if (sbit->red == 0 || sbit->red > maxbits ||
787 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500788 sbit->blue == 0 || sbit->blue > maxbits)
789 {
790 png_warning(png_ptr, "Invalid sBIT depth specified");
791 return;
792 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500793 buf[0] = sbit->red;
794 buf[1] = sbit->green;
795 buf[2] = sbit->blue;
796 size = 3;
797 }
798 else
799 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500800 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
801 {
802 png_warning(png_ptr, "Invalid sBIT depth specified");
803 return;
804 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500805 buf[0] = sbit->gray;
806 size = 1;
807 }
808
809 if (color_type & PNG_COLOR_MASK_ALPHA)
810 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500811 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
812 {
813 png_warning(png_ptr, "Invalid sBIT depth specified");
814 return;
815 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500816 buf[size++] = sbit->alpha;
817 }
818
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600819 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500820}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500821#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500822
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500823#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500824/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600825#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500826void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600828 double red_x, double red_y, double green_x, double green_y,
829 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500830{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600831#ifdef PNG_USE_LOCAL_ARRAYS
832 PNG_cHRM;
833#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500834 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600835 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500836
Andreas Dilger47a0c421997-05-16 02:46:07 -0500837 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600838 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500839 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
840 white_x + white_y > 1.0)
841 {
842 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500843#if !defined(PNG_NO_CONSOLE_IO)
844 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600845#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500846 return;
847 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600848 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500849 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600850 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500851 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500852
853 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
854 red_x + red_y > 1.0)
855 {
856 png_warning(png_ptr, "Invalid cHRM red point specified");
857 return;
858 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600859 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500860 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600861 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500862 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500863
864 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
865 green_x + green_y > 1.0)
866 {
867 png_warning(png_ptr, "Invalid cHRM green point specified");
868 return;
869 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600870 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500871 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600872 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500873 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500874
875 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
876 blue_x + blue_y > 1.0)
877 {
878 png_warning(png_ptr, "Invalid cHRM blue point specified");
879 return;
880 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600881 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600883 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500884 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500885
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600886 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500887}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500888#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600889#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500890void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600891png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
892 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
893 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
894 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600895{
896#ifdef PNG_USE_LOCAL_ARRAYS
897 PNG_cHRM;
898#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600899 png_byte buf[32];
900
901 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600902 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600903 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
904 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600905 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500906#if !defined(PNG_NO_CONSOLE_IO)
907 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600908#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600909 return;
910 }
911 png_save_uint_32(buf, white_x);
912 png_save_uint_32(buf + 4, white_y);
913
914 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
915 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600916 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600917 return;
918 }
919 png_save_uint_32(buf + 8, red_x);
920 png_save_uint_32(buf + 12, red_y);
921
922 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
923 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600924 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600925 return;
926 }
927 png_save_uint_32(buf + 16, green_x);
928 png_save_uint_32(buf + 20, green_y);
929
930 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
931 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600932 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600933 return;
934 }
935 png_save_uint_32(buf + 24, blue_x);
936 png_save_uint_32(buf + 28, blue_y);
937
938 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
939}
940#endif
941#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500942
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500943#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500944/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500945void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600946png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500947 int num_trans, int color_type)
948{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600949#ifdef PNG_USE_LOCAL_ARRAYS
950 PNG_tRNS;
951#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500952 png_byte buf[6];
953
Andreas Dilger47a0c421997-05-16 02:46:07 -0500954 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500955 if (color_type == PNG_COLOR_TYPE_PALETTE)
956 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600957 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500958 {
959 png_warning(png_ptr,"Invalid number of transparent colors specified");
960 return;
961 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500962 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600963 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500964 }
965 else if (color_type == PNG_COLOR_TYPE_GRAY)
966 {
967 /* one 16 bit value */
968 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600969 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500970 }
971 else if (color_type == PNG_COLOR_TYPE_RGB)
972 {
973 /* three 16 bit values */
974 png_save_uint_16(buf, tran->red);
975 png_save_uint_16(buf + 2, tran->green);
976 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600977 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500978 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500979 else
980 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600981 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500982 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500983}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500984#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500985
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500986#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500987/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500988void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600989png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500990{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600991#ifdef PNG_USE_LOCAL_ARRAYS
992 PNG_bKGD;
993#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500994 png_byte buf[6];
995
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500997 if (color_type == PNG_COLOR_TYPE_PALETTE)
998 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500999 if (
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001000#if defined(PNG_MNG_FEATURES_SUPPORTED)
1001 (png_ptr->num_palette ||
1002 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001003#endif
1004 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001005 {
1006 png_warning(png_ptr, "Invalid background palette index");
1007 return;
1008 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001009 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001010 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001011 }
1012 else if (color_type & PNG_COLOR_MASK_COLOR)
1013 {
1014 png_save_uint_16(buf, back->red);
1015 png_save_uint_16(buf + 2, back->green);
1016 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001017 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001018 }
1019 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001020 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001021 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001022 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001023 }
1024}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001025#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001026
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001027#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001028/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001029void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001030png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001031{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001032#ifdef PNG_USE_LOCAL_ARRAYS
1033 PNG_hIST;
1034#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001035 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001036 png_byte buf[3];
1037
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001039 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001040 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001041 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1042 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001043 png_warning(png_ptr, "Invalid number of histogram entries specified");
1044 return;
1045 }
1046
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001047 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001049 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001050 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001052 }
1053 png_write_chunk_end(png_ptr);
1054}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001055#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001056
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001057#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1058 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001059/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1060 * and if invalid, correct the keyword rather than discarding the entire
1061 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1062 * length, forbids leading or trailing whitespace, multiple internal spaces,
1063 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001064 *
1065 * The new_key is allocated to hold the corrected keyword and must be freed
1066 * by the calling routine. This avoids problems with trying to write to
1067 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001068 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001069png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001070png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001071{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001072 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001073 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001074 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001075
Andreas Dilger47a0c421997-05-16 02:46:07 -05001076 png_debug(1, "in png_check_keyword\n");
1077 *new_key = NULL;
1078
1079 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001080 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001081 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001082 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001083 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001084
Andreas Dilger47a0c421997-05-16 02:46:07 -05001085 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1086
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001087 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001088
1089 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001091 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001092 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001093 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001094#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001095 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001096
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001097 sprintf(msg, "invalid keyword character 0x%02X", *kp);
1098 png_chunk_warning(png_ptr, msg);
1099#else
1100 png_chunk_warning(png_ptr, "invalid character in keyword");
1101#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 *dp = ' ';
1103 }
1104 else
1105 {
1106 *dp = *kp;
1107 }
1108 }
1109 *dp = '\0';
1110
1111 /* Remove any trailing white space. */
1112 kp = *new_key + key_len - 1;
1113 if (*kp == ' ')
1114 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001115 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001116
1117 while (*kp == ' ')
1118 {
1119 *(kp--) = '\0';
1120 key_len--;
1121 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001122 }
1123
1124 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 kp = *new_key;
1126 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001127 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001128 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129
1130 while (*kp == ' ')
1131 {
1132 kp++;
1133 key_len--;
1134 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001135 }
1136
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138
Andreas Dilger47a0c421997-05-16 02:46:07 -05001139 /* Remove multiple internal spaces. */
1140 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001141 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001143 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 *(dp++) = *kp;
1145 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001146 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001148 {
1149 key_len--;
1150 }
1151 else
1152 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001153 *(dp++) = *kp;
1154 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001155 }
1156 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001158
1159 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001160 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001161 png_free(png_ptr, *new_key);
1162 *new_key=NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001163 png_chunk_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164 }
1165
1166 if (key_len > 79)
1167 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001168 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169 new_key[79] = '\0';
1170 key_len = 79;
1171 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001173 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001174}
1175#endif
1176
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001177#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001178/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001179void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001180png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001181 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001182{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001183#ifdef PNG_USE_LOCAL_ARRAYS
1184 PNG_tEXt;
1185#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001186 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001187 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001188
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189 png_debug(1, "in png_write_tEXt\n");
1190 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1191 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001192 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001193 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001195
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001197 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001198 else
1199 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001200
1201 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001202 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001203 /*
1204 * We leave it to the application to meet PNG-1.0 requirements on the
1205 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1206 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001207 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001208 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001209 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001212
Guy Schalnat0d580581995-07-20 02:43:20 -05001213 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001215}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001216#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001217
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001218#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001219/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001220void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001221png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001223{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001224#ifdef PNG_USE_LOCAL_ARRAYS
1225 PNG_zTXt;
1226#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001227 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001228 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001229 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001230 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001231
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001235 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001236 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001238 }
1239
Andreas Dilger47a0c421997-05-16 02:46:07 -05001240 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1241 {
1242 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1243 png_free(png_ptr, new_key);
1244 return;
1245 }
1246
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001247 text_len = png_strlen(text);
1248
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 png_free(png_ptr, new_key);
1250
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001251 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001252 text_len = png_text_compress(png_ptr, text, text_len, compression,
1253 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001254
1255 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001256 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1257 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001258 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001260 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001261 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001262 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001263 /* write the compressed data */
1264 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001265
Guy Schalnat0d580581995-07-20 02:43:20 -05001266 /* close the chunk */
1267 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001268}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001269#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001270
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001271#if defined(PNG_WRITE_iTXt_SUPPORTED)
1272/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001273void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001274png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001275 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001276{
1277#ifdef PNG_USE_LOCAL_ARRAYS
1278 PNG_iTXt;
1279#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001280 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001281 png_charp new_lang, new_key;
1282 png_byte cbuf[2];
1283 compression_state comp;
1284
1285 png_debug(1, "in png_write_iTXt\n");
1286
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001287 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1288 {
1289 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1290 return;
1291 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001292 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1293 &new_lang))==0)
1294 {
1295 png_warning(png_ptr, "Empty language field in iTXt chunk");
1296 return;
1297 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001298 lang_key_len = png_strlen(lang_key);
1299 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001300
1301 if (text == NULL || *text == '\0')
1302 text_len = 0;
1303
1304 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001305 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1306 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001307
1308 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001309 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001310
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001311 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1312 (png_uint_32)(
1313 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1314 + key_len
1315 + lang_len
1316 + lang_key_len
1317 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001318
1319 /*
1320 * We leave it to the application to meet PNG-1.0 requirements on the
1321 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1322 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1323 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1324 */
1325 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001326
1327 /* set the compression flag */
1328 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1329 compression == PNG_TEXT_COMPRESSION_NONE)
1330 cbuf[0] = 0;
1331 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1332 cbuf[0] = 1;
1333 /* set the compression method */
1334 cbuf[1] = 0;
1335 png_write_chunk_data(png_ptr, cbuf, 2);
1336
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001337 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001338 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001339 png_write_chunk_data(png_ptr, '\0', 1);
1340
1341 png_write_compressed_data_out(png_ptr, &comp);
1342
1343 png_write_chunk_end(png_ptr);
1344 png_free(png_ptr, new_key);
1345 png_free(png_ptr, new_lang);
1346}
1347#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001348
1349#if defined(PNG_WRITE_oFFs_SUPPORTED)
1350/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001351void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001352png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1353 png_uint_32 y_offset,
1354 int unit_type)
1355{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001356#ifdef PNG_USE_LOCAL_ARRAYS
1357 PNG_oFFs;
1358#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001359 png_byte buf[9];
1360
1361 png_debug(1, "in png_write_oFFs\n");
1362 if (unit_type >= PNG_OFFSET_LAST)
1363 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1364
1365 png_save_uint_32(buf, x_offset);
1366 png_save_uint_32(buf + 4, y_offset);
1367 buf[8] = (png_byte)unit_type;
1368
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001369 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001370}
1371#endif
1372
1373#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001374/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001375void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001376png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1377 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1378{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001379#ifdef PNG_USE_LOCAL_ARRAYS
1380 PNG_pCAL;
1381#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001382 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383 png_uint_32p params_len;
1384 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001385 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 int i;
1387
1388 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1389 if (type >= PNG_EQUATION_LAST)
1390 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1391
1392 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1393 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1394 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1395 png_debug1(3, "pCAL units length = %d\n", units_len);
1396 total_len = purpose_len + units_len + 10;
1397
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001398 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1399 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001400
1401 /* Find the length of each parameter, making sure we don't count the
1402 null terminator for the last parameter. */
1403 for (i = 0; i < nparams; i++)
1404 {
1405 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001406 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001407 total_len += (png_size_t)params_len[i];
1408 }
1409
1410 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001411 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001412 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 png_save_int_32(buf, X0);
1414 png_save_int_32(buf + 4, X1);
1415 buf[8] = (png_byte)type;
1416 buf[9] = (png_byte)nparams;
1417 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1418 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1419
1420 png_free(png_ptr, new_purpose);
1421
1422 for (i = 0; i < nparams; i++)
1423 {
1424 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1425 (png_size_t)params_len[i]);
1426 }
1427
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001428 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001429 png_write_chunk_end(png_ptr);
1430}
1431#endif
1432
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001433#if defined(PNG_WRITE_sCAL_SUPPORTED)
1434/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001435#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001436void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001437png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001438{
1439#ifdef PNG_USE_LOCAL_ARRAYS
1440 PNG_sCAL;
1441#endif
1442 png_size_t total_len;
1443 char wbuf[32], hbuf[32];
1444
1445 png_debug(1, "in png_write_sCAL\n");
1446
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001447#if defined(_WIN32_WCE)
1448/* sprintf() function is not supported on WindowsCE */
1449 {
1450 wchar_t wc_buf[32];
1451 swprintf(wc_buf, TEXT("%12.12e"), width);
1452 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1453 swprintf(wc_buf, TEXT("%12.12e"), height);
1454 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1455 }
1456#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457 sprintf(wbuf, "%12.12e", width);
1458 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001459#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001460 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001461
1462 png_debug1(3, "sCAL total length = %d\n", total_len);
1463 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001464 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001465 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1466 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1467
1468 png_write_chunk_end(png_ptr);
1469}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001470#else
1471#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001472void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001473png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001474 png_charp height)
1475{
1476#ifdef PNG_USE_LOCAL_ARRAYS
1477 PNG_sCAL;
1478#endif
1479 png_size_t total_len;
1480 char wbuf[32], hbuf[32];
1481
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001482 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001483
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001484 strcpy(wbuf,(const char *)width);
1485 strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001486 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001487
1488 png_debug1(3, "sCAL total length = %d\n", total_len);
1489 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001490 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001491 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1492 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1493
1494 png_write_chunk_end(png_ptr);
1495}
1496#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001497#endif
1498#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001499
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001500#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001501/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001502void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001503png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001504 png_uint_32 y_pixels_per_unit,
1505 int unit_type)
1506{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001507#ifdef PNG_USE_LOCAL_ARRAYS
1508 PNG_pHYs;
1509#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001510 png_byte buf[9];
1511
Andreas Dilger47a0c421997-05-16 02:46:07 -05001512 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001513 if (unit_type >= PNG_RESOLUTION_LAST)
1514 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1515
Guy Schalnat0d580581995-07-20 02:43:20 -05001516 png_save_uint_32(buf, x_pixels_per_unit);
1517 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001518 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001519
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001520 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001521}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001522#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001523
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001524#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001525/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1526 * or png_convert_from_time_t(), or fill in the structure yourself.
1527 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001528void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001529png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001530{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001531#ifdef PNG_USE_LOCAL_ARRAYS
1532 PNG_tIME;
1533#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001534 png_byte buf[7];
1535
Andreas Dilger47a0c421997-05-16 02:46:07 -05001536 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001537 if (mod_time->month > 12 || mod_time->month < 1 ||
1538 mod_time->day > 31 || mod_time->day < 1 ||
1539 mod_time->hour > 23 || mod_time->second > 60)
1540 {
1541 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1542 return;
1543 }
1544
Guy Schalnat0d580581995-07-20 02:43:20 -05001545 png_save_uint_16(buf, mod_time->year);
1546 buf[2] = mod_time->month;
1547 buf[3] = mod_time->day;
1548 buf[4] = mod_time->hour;
1549 buf[5] = mod_time->minute;
1550 buf[6] = mod_time->second;
1551
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001552 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001553}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001554#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001555
1556/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001557void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001558png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001559{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001560#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001561 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001562
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001563 /* start of interlace block */
1564 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001565
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001566 /* offset to next interlace block */
1567 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001568
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001569 /* start of interlace block in the y direction */
1570 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001571
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001572 /* offset to next interlace block in the y direction */
1573 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001574#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001575
Andreas Dilger47a0c421997-05-16 02:46:07 -05001576 png_size_t buf_size;
1577
1578 png_debug(1, "in png_write_start_row\n");
1579 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1580 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1581
Guy Schalnat0d580581995-07-20 02:43:20 -05001582 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001583 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001584 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001585
1586 /* set up filtering buffer, if using this filter */
1587 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001588 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001589 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001590 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001591 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001592 }
1593
Andreas Dilger47a0c421997-05-16 02:46:07 -05001594 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001595 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1596 {
1597 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001598 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001599 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001600
1601 if (png_ptr->do_filter & PNG_FILTER_UP)
1602 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001603 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001604 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001605 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001606 }
1607
1608 if (png_ptr->do_filter & PNG_FILTER_AVG)
1609 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001610 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1611 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001612 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001613 }
1614
1615 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1616 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001617 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001618 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001619 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001620 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001621 }
1622
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001623#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001624 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001625 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001626 {
1627 if (!(png_ptr->transformations & PNG_INTERLACE))
1628 {
1629 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1630 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001631 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1632 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001633 }
1634 else
1635 {
1636 png_ptr->num_rows = png_ptr->height;
1637 png_ptr->usr_width = png_ptr->width;
1638 }
1639 }
1640 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001641#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001642 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001643 png_ptr->num_rows = png_ptr->height;
1644 png_ptr->usr_width = png_ptr->width;
1645 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001646 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1647 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001648}
1649
Andreas Dilger47a0c421997-05-16 02:46:07 -05001650/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001651void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001652png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001653{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001654#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001655 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001656
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001657 /* start of interlace block */
1658 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001659
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001660 /* offset to next interlace block */
1661 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001662
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001663 /* start of interlace block in the y direction */
1664 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001665
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001666 /* offset to next interlace block in the y direction */
1667 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001668#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001669
Guy Schalnat0d580581995-07-20 02:43:20 -05001670 int ret;
1671
Andreas Dilger47a0c421997-05-16 02:46:07 -05001672 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001673 /* next row */
1674 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001675
Guy Schalnat0d580581995-07-20 02:43:20 -05001676 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001677 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001678 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001679
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001680#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001681 /* if interlaced, go to next pass */
1682 if (png_ptr->interlaced)
1683 {
1684 png_ptr->row_number = 0;
1685 if (png_ptr->transformations & PNG_INTERLACE)
1686 {
1687 png_ptr->pass++;
1688 }
1689 else
1690 {
1691 /* loop until we find a non-zero width or height pass */
1692 do
1693 {
1694 png_ptr->pass++;
1695 if (png_ptr->pass >= 7)
1696 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001697 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001698 png_pass_inc[png_ptr->pass] - 1 -
1699 png_pass_start[png_ptr->pass]) /
1700 png_pass_inc[png_ptr->pass];
1701 png_ptr->num_rows = (png_ptr->height +
1702 png_pass_yinc[png_ptr->pass] - 1 -
1703 png_pass_ystart[png_ptr->pass]) /
1704 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001705 if (png_ptr->transformations & PNG_INTERLACE)
1706 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001707 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1708
1709 }
1710
Guy Schalnate5a37791996-06-05 15:50:50 -05001711 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001712 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001713 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001714 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001715 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001716 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001717 (png_uint_32)png_ptr->usr_bit_depth *
1718 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001719 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001720 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001721 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001722#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001723
1724 /* if we get here, we've just written the last row, so we need
1725 to flush the compressor */
1726 do
1727 {
1728 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001729 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001730 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001731 if (ret == Z_OK)
1732 {
1733 /* check to see if we need more room */
1734 if (!(png_ptr->zstream.avail_out))
1735 {
1736 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1737 png_ptr->zstream.next_out = png_ptr->zbuf;
1738 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1739 }
1740 }
1741 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001742 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001743 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001744 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001745 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001746 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001747 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001748 } while (ret != Z_STREAM_END);
1749
1750 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001751 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001752 {
1753 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001754 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001755 }
1756
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001757 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001758}
1759
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001760#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001761/* Pick out the correct pixels for the interlace pass.
1762 * The basic idea here is to go through the row with a source
1763 * pointer and a destination pointer (sp and dp), and copy the
1764 * correct pixels for the pass. As the row gets compacted,
1765 * sp will always be >= dp, so we should never overwrite anything.
1766 * See the default: case for the easiest code to understand.
1767 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001768void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001769png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001770{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001771#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001772 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001773
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001774 /* start of interlace block */
1775 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001776
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001777 /* offset to next interlace block */
1778 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001779#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001780
Andreas Dilger47a0c421997-05-16 02:46:07 -05001781 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001782 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001783#if defined(PNG_USELESS_TESTS_SUPPORTED)
1784 if (row != NULL && row_info != NULL && pass < 6)
1785#else
1786 if (pass < 6)
1787#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001788 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001789 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001790 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001791 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001792 case 1:
1793 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001794 png_bytep sp;
1795 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001796 int shift;
1797 int d;
1798 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001799 png_uint_32 i;
1800 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001801
1802 dp = row;
1803 d = 0;
1804 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001805 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001806 i += png_pass_inc[pass])
1807 {
1808 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001809 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001810 d |= (value << shift);
1811
1812 if (shift == 0)
1813 {
1814 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001815 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 d = 0;
1817 }
1818 else
1819 shift--;
1820
1821 }
1822 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001823 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 break;
1825 }
1826 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001827 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001828 png_bytep sp;
1829 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001830 int shift;
1831 int d;
1832 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001833 png_uint_32 i;
1834 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001835
1836 dp = row;
1837 shift = 6;
1838 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001839 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001840 i += png_pass_inc[pass])
1841 {
1842 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001843 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001844 d |= (value << shift);
1845
1846 if (shift == 0)
1847 {
1848 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001849 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001850 d = 0;
1851 }
1852 else
1853 shift -= 2;
1854 }
1855 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001856 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001857 break;
1858 }
1859 case 4:
1860 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001861 png_bytep sp;
1862 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001863 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001864 int d;
1865 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001866 png_uint_32 i;
1867 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001868
1869 dp = row;
1870 shift = 4;
1871 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001872 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001873 i += png_pass_inc[pass])
1874 {
1875 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001876 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001877 d |= (value << shift);
1878
1879 if (shift == 0)
1880 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001881 shift = 4;
1882 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001883 d = 0;
1884 }
1885 else
1886 shift -= 4;
1887 }
1888 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001889 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001890 break;
1891 }
1892 default:
1893 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001894 png_bytep sp;
1895 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001896 png_uint_32 i;
1897 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001898 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001899
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001900 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001901 dp = row;
1902 /* find out how many bytes each pixel takes up */
1903 pixel_bytes = (row_info->pixel_depth >> 3);
1904 /* loop through the row, only looking at the pixels that
1905 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001906 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001907 i += png_pass_inc[pass])
1908 {
1909 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001910 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001911 /* move the pixel */
1912 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001913 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001914 /* next pixel */
1915 dp += pixel_bytes;
1916 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001917 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001918 }
1919 }
1920 /* set new row width */
1921 row_info->width = (row_info->width +
1922 png_pass_inc[pass] - 1 -
1923 png_pass_start[pass]) /
1924 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001925 row_info->rowbytes = ((row_info->width *
1926 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001927 }
1928}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001929#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001930
Andreas Dilger47a0c421997-05-16 02:46:07 -05001931/* This filters the row, chooses which filter to use, if it has not already
1932 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001933 * chosen filter.
1934 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001935#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001936#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001937#define PNG_LOMASK ((png_uint_32)0xffffL)
1938#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001939void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001940png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001941{
Guy Schalnate5a37791996-06-05 15:50:50 -05001942 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001943 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001944 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001945 png_uint_32 row_bytes = row_info->rowbytes;
1946#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1947 int num_p_filters = (int)png_ptr->num_prev_filters;
1948#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001949
Andreas Dilger47a0c421997-05-16 02:46:07 -05001950 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001951 /* find out how many bytes offset each pixel is */
1952 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001953
1954 prev_row = png_ptr->prev_row;
1955 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001956 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001957
Andreas Dilger47a0c421997-05-16 02:46:07 -05001958 /* The prediction method we use is to find which method provides the
1959 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001960 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001961 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001962 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001963 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001964 * predictive" method (not implemented yet), which does test compressions
1965 * of lines using different filter methods, and then chooses the
1966 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001967 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001968 *
1969 * GRR 980525: consider also
1970 * (1) minimum sum of absolute differences from running average (i.e.,
1971 * keep running sum of non-absolute differences & count of bytes)
1972 * [track dispersion, too? restart average if dispersion too large?]
1973 * (1b) minimum sum of absolute differences from sliding average, probably
1974 * with window size <= deflate window (usually 32K)
1975 * (2) minimum sum of squared differences from zero or running average
1976 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001977 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001978
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001979
Guy Schalnate5a37791996-06-05 15:50:50 -05001980 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001981 * that has been chosen, as it doesn't actually do anything to the data.
1982 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001983 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001984 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001986 png_bytep rp;
1987 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001988 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001989 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001990
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001991 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001992 {
1993 v = *rp;
1994 sum += (v < 128) ? v : 256 - v;
1995 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001996
1997#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1998 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1999 {
2000 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002001 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002002 sumlo = sum & PNG_LOMASK;
2003 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2004
2005 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002006 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002007 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002008 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002009 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002010 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002011 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002013 PNG_WEIGHT_SHIFT;
2014 }
2015 }
2016
2017 /* Factor in the cost of this filter (this is here for completeness,
2018 * but it makes no sense to have a "cost" for the NONE filter, as
2019 * it has the minimum possible computational cost - none).
2020 */
2021 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2022 PNG_COST_SHIFT;
2023 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2024 PNG_COST_SHIFT;
2025
2026 if (sumhi > PNG_HIMASK)
2027 sum = PNG_MAXSUM;
2028 else
2029 sum = (sumhi << PNG_HISHIFT) + sumlo;
2030 }
2031#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002032 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002033 }
2034
Guy Schalnate5a37791996-06-05 15:50:50 -05002035 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002036 if (filter_to_do == PNG_FILTER_SUB)
2037 /* it's the only filter so no testing is needed */
2038 {
2039 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002040 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002041 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2042 i++, rp++, dp++)
2043 {
2044 *dp = *rp;
2045 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002046 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002047 i++, rp++, lp++, dp++)
2048 {
2049 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2050 }
2051 best_row = png_ptr->sub_row;
2052 }
2053
2054 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002055 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002056 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002057 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002058 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002059 int v;
2060
2061#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002062 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002063 * would reduce the sum of this filter, so that we can do the
2064 * early exit comparison without scaling the sum each time.
2065 */
2066 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2067 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002068 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002069 png_uint_32 lmhi, lmlo;
2070 lmlo = lmins & PNG_LOMASK;
2071 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2072
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002073 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002074 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002075 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002077 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002079 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002080 PNG_WEIGHT_SHIFT;
2081 }
2082 }
2083
2084 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2085 PNG_COST_SHIFT;
2086 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2087 PNG_COST_SHIFT;
2088
2089 if (lmhi > PNG_HIMASK)
2090 lmins = PNG_MAXSUM;
2091 else
2092 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2093 }
2094#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002095
Guy Schalnate5a37791996-06-05 15:50:50 -05002096 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2097 i++, rp++, dp++)
2098 {
2099 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002100
Guy Schalnate5a37791996-06-05 15:50:50 -05002101 sum += (v < 128) ? v : 256 - v;
2102 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002103 for (lp = row_buf + 1; i < row_info->rowbytes;
2104 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002105 {
2106 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002107
Guy Schalnate5a37791996-06-05 15:50:50 -05002108 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002109
2110 if (sum > lmins) /* We are already worse, don't continue. */
2111 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002112 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113
2114#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2115 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2116 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002117 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002118 png_uint_32 sumhi, sumlo;
2119 sumlo = sum & PNG_LOMASK;
2120 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2121
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002122 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002123 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002124 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002125 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002126 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002127 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002128 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002129 PNG_WEIGHT_SHIFT;
2130 }
2131 }
2132
2133 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2134 PNG_COST_SHIFT;
2135 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2136 PNG_COST_SHIFT;
2137
2138 if (sumhi > PNG_HIMASK)
2139 sum = PNG_MAXSUM;
2140 else
2141 sum = (sumhi << PNG_HISHIFT) + sumlo;
2142 }
2143#endif
2144
Guy Schalnate5a37791996-06-05 15:50:50 -05002145 if (sum < mins)
2146 {
2147 mins = sum;
2148 best_row = png_ptr->sub_row;
2149 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002150 }
2151
Guy Schalnate5a37791996-06-05 15:50:50 -05002152 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002153 if (filter_to_do == PNG_FILTER_UP)
2154 {
2155 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002156 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002157
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002158 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002159 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002160 i++, rp++, pp++, dp++)
2161 {
2162 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2163 }
2164 best_row = png_ptr->up_row;
2165 }
2166
2167 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002168 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002169 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002170 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002171 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002172 int v;
2173
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002174
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2176 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2177 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002178 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002179 png_uint_32 lmhi, lmlo;
2180 lmlo = lmins & PNG_LOMASK;
2181 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2182
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002183 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002185 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002187 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002188 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002189 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002190 PNG_WEIGHT_SHIFT;
2191 }
2192 }
2193
2194 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2195 PNG_COST_SHIFT;
2196 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2197 PNG_COST_SHIFT;
2198
2199 if (lmhi > PNG_HIMASK)
2200 lmins = PNG_MAXSUM;
2201 else
2202 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2203 }
2204#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002205
2206 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002207 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002208 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002209 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002210
2211 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002212
2213 if (sum > lmins) /* We are already worse, don't continue. */
2214 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002215 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002216
2217#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2218 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2219 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002220 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002221 png_uint_32 sumhi, sumlo;
2222 sumlo = sum & PNG_LOMASK;
2223 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2224
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002225 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002226 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002227 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002228 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002229 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002230 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002231 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002232 PNG_WEIGHT_SHIFT;
2233 }
2234 }
2235
2236 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2237 PNG_COST_SHIFT;
2238 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2239 PNG_COST_SHIFT;
2240
2241 if (sumhi > PNG_HIMASK)
2242 sum = PNG_MAXSUM;
2243 else
2244 sum = (sumhi << PNG_HISHIFT) + sumlo;
2245 }
2246#endif
2247
Guy Schalnate5a37791996-06-05 15:50:50 -05002248 if (sum < mins)
2249 {
2250 mins = sum;
2251 best_row = png_ptr->up_row;
2252 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002253 }
2254
Guy Schalnate5a37791996-06-05 15:50:50 -05002255 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002256 if (filter_to_do == PNG_FILTER_AVG)
2257 {
2258 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002260 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002262 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002264 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002265 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002266 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002267 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2268 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002269 }
2270 best_row = png_ptr->avg_row;
2271 }
2272
2273 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002274 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002275 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002276 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002277 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002278 int v;
2279
2280#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2281 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2282 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002283 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002284 png_uint_32 lmhi, lmlo;
2285 lmlo = lmins & PNG_LOMASK;
2286 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2287
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002288 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002289 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002290 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002291 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002292 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002293 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002294 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295 PNG_WEIGHT_SHIFT;
2296 }
2297 }
2298
2299 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2300 PNG_COST_SHIFT;
2301 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2302 PNG_COST_SHIFT;
2303
2304 if (lmhi > PNG_HIMASK)
2305 lmins = PNG_MAXSUM;
2306 else
2307 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2308 }
2309#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002310
2311 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002312 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002313 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002314 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002315
2316 sum += (v < 128) ? v : 256 - v;
2317 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002318 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002319 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002320 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002321 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002322
2323 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002324
2325 if (sum > lmins) /* We are already worse, don't continue. */
2326 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002327 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002328
2329#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2330 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2331 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002332 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002333 png_uint_32 sumhi, sumlo;
2334 sumlo = sum & PNG_LOMASK;
2335 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2336
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002337 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002338 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002339 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002340 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002341 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002342 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002343 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002344 PNG_WEIGHT_SHIFT;
2345 }
2346 }
2347
2348 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2349 PNG_COST_SHIFT;
2350 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2351 PNG_COST_SHIFT;
2352
2353 if (sumhi > PNG_HIMASK)
2354 sum = PNG_MAXSUM;
2355 else
2356 sum = (sumhi << PNG_HISHIFT) + sumlo;
2357 }
2358#endif
2359
Guy Schalnate5a37791996-06-05 15:50:50 -05002360 if (sum < mins)
2361 {
2362 mins = sum;
2363 best_row = png_ptr->avg_row;
2364 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002365 }
2366
Andreas Dilger47a0c421997-05-16 02:46:07 -05002367 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002368 if (filter_to_do == PNG_FILTER_PAETH)
2369 {
2370 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002372 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002373 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002374 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002375 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002376 }
2377
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002378 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002379 {
2380 int a, b, c, pa, pb, pc, p;
2381
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002382 b = *pp++;
2383 c = *cp++;
2384 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002385
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002386 p = b - c;
2387 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002388
2389#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002390 pa = abs(p);
2391 pb = abs(pc);
2392 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002393#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002394 pa = p < 0 ? -p : p;
2395 pb = pc < 0 ? -pc : pc;
2396 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002397#endif
2398
2399 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2400
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002401 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002402 }
2403 best_row = png_ptr->paeth_row;
2404 }
2405
2406 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002407 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002408 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 int v;
2412
2413#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2414 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2415 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002416 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002417 png_uint_32 lmhi, lmlo;
2418 lmlo = lmins & PNG_LOMASK;
2419 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2420
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002421 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002422 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002423 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002424 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002425 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002426 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002427 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002428 PNG_WEIGHT_SHIFT;
2429 }
2430 }
2431
2432 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2433 PNG_COST_SHIFT;
2434 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2435 PNG_COST_SHIFT;
2436
2437 if (lmhi > PNG_HIMASK)
2438 lmins = PNG_MAXSUM;
2439 else
2440 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2441 }
2442#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002443
2444 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002445 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002446 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002448
2449 sum += (v < 128) ? v : 256 - v;
2450 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002451
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002452 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002453 {
2454 int a, b, c, pa, pb, pc, p;
2455
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002456 b = *pp++;
2457 c = *cp++;
2458 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002459
2460#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002461 p = b - c;
2462 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002463#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002464 pa = abs(p);
2465 pb = abs(pc);
2466 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002467#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002468 pa = p < 0 ? -p : p;
2469 pb = pc < 0 ? -pc : pc;
2470 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002471#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002472 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2473#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002474 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002475 pa = abs(p - a);
2476 pb = abs(p - b);
2477 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002478 if (pa <= pb && pa <= pc)
2479 p = a;
2480 else if (pb <= pc)
2481 p = b;
2482 else
2483 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002484#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002485
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002486 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002487
2488 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002489
2490 if (sum > lmins) /* We are already worse, don't continue. */
2491 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002492 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002493
2494#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2495 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2496 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002497 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002498 png_uint_32 sumhi, sumlo;
2499 sumlo = sum & PNG_LOMASK;
2500 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2501
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002502 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002503 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002504 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002505 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002507 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002508 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002509 PNG_WEIGHT_SHIFT;
2510 }
2511 }
2512
2513 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2514 PNG_COST_SHIFT;
2515 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2516 PNG_COST_SHIFT;
2517
2518 if (sumhi > PNG_HIMASK)
2519 sum = PNG_MAXSUM;
2520 else
2521 sum = (sumhi << PNG_HISHIFT) + sumlo;
2522 }
2523#endif
2524
Guy Schalnate5a37791996-06-05 15:50:50 -05002525 if (sum < mins)
2526 {
2527 best_row = png_ptr->paeth_row;
2528 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002529 }
2530
Andreas Dilger47a0c421997-05-16 02:46:07 -05002531 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002532
Guy Schalnate5a37791996-06-05 15:50:50 -05002533 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002534
2535#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2536 /* Save the type of filter we picked this time for future calculations */
2537 if (png_ptr->num_prev_filters > 0)
2538 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002539 int j;
2540 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002541 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002542 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002543 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002544 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002545 }
2546#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002547}
2548
2549
Andreas Dilger47a0c421997-05-16 02:46:07 -05002550/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002551void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002552png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2553{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002554 png_debug(1, "in png_write_filtered_row\n");
2555 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002556 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002557
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002558 png_ptr->zstream.next_in = filtered_row;
2559 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002560 /* repeat until we have compressed all the data */
2561 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002562 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002563 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002564
Guy Schalnate5a37791996-06-05 15:50:50 -05002565 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002566 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002567 /* check for compression errors */
2568 if (ret != Z_OK)
2569 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002570 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002571 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002572 else
2573 png_error(png_ptr, "zlib error");
2574 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002575
Guy Schalnate5a37791996-06-05 15:50:50 -05002576 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002577 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002578 {
2579 /* write the IDAT and reset the zlib output buffer */
2580 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002581 png_ptr->zstream.next_out = png_ptr->zbuf;
2582 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002583 }
2584 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002585 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002586
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002587 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002588 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002589 {
2590 png_bytep tptr;
2591
2592 tptr = png_ptr->prev_row;
2593 png_ptr->prev_row = png_ptr->row_buf;
2594 png_ptr->row_buf = tptr;
2595 }
2596
Guy Schalnate5a37791996-06-05 15:50:50 -05002597 /* finish row - updates counters and flushes zlib if last row */
2598 png_write_finish_row(png_ptr);
2599
2600#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2601 png_ptr->flush_rows++;
2602
2603 if (png_ptr->flush_dist > 0 &&
2604 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002605 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002606 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002607 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002608#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002609}