blob: 8a5c2acf5e33e6b5f3b0cbd476b42b4bfd48e4cc [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-Pehrson104622b2000-05-29 08:58:03 -05004 * libpng 1.0.7beta15 - May 29, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06008 * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
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];
80 png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name, length);
81
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 {
175#if !defined(PNG_NO_STDIO)
176 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]);
342 }
343 if (comp->max_output_ptr != 0)
344 png_free(png_ptr, comp->output_ptr);
345 /* write anything left in zbuf */
346 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
347 png_write_chunk_data(png_ptr, png_ptr->zbuf,
348 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
349
350 /* reset zlib for another zTXt/iTXt or the image data */
351 deflateReset(&png_ptr->zstream);
352
353}
354#endif
355
Guy Schalnat0d580581995-07-20 02:43:20 -0500356/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600357 * information. Note that the rest of this code depends upon this
358 * information being correct.
359 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500360void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600361png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500362 int bit_depth, int color_type, int compression_type, int filter_type,
363 int interlace_type)
364{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600365#ifdef PNG_USE_LOCAL_ARRAYS
366 PNG_IHDR;
367#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 png_byte buf[13]; /* buffer to store the IHDR info */
369
Andreas Dilger47a0c421997-05-16 02:46:07 -0500370 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500371 /* Check that we have valid input data from the application info */
372 switch (color_type)
373 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500375 switch (bit_depth)
376 {
377 case 1:
378 case 2:
379 case 4:
380 case 8:
381 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500383 }
384 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500386 if (bit_depth != 8 && bit_depth != 16)
387 png_error(png_ptr, "Invalid bit depth for RGB image");
388 png_ptr->channels = 3;
389 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500390 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500391 switch (bit_depth)
392 {
393 case 1:
394 case 2:
395 case 4:
396 case 8: png_ptr->channels = 1; break;
397 default: png_error(png_ptr, "Invalid bit depth for paletted image");
398 }
399 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500401 if (bit_depth != 8 && bit_depth != 16)
402 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
403 png_ptr->channels = 2;
404 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500406 if (bit_depth != 8 && bit_depth != 16)
407 png_error(png_ptr, "Invalid bit depth for RGBA image");
408 png_ptr->channels = 4;
409 break;
410 default:
411 png_error(png_ptr, "Invalid image color type specified");
412 }
413
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500415 {
416 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 }
419
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500421 {
422 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500424 }
425
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600426#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 if (interlace_type != PNG_INTERLACE_NONE &&
428 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 {
430 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500431 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500432 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600433#else
434 interlace_type=PNG_INTERLACE_NONE;
435#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500436
437 /* save off the relevent information */
438 png_ptr->bit_depth = (png_byte)bit_depth;
439 png_ptr->color_type = (png_byte)color_type;
440 png_ptr->interlaced = (png_byte)interlace_type;
441 png_ptr->width = width;
442 png_ptr->height = height;
443
444 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500446 /* set the usr info, so any transformations can modify it */
447 png_ptr->usr_width = png_ptr->width;
448 png_ptr->usr_bit_depth = png_ptr->bit_depth;
449 png_ptr->usr_channels = png_ptr->channels;
450
Guy Schalnat0d580581995-07-20 02:43:20 -0500451 /* pack the header information into the buffer */
452 png_save_uint_32(buf, width);
453 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600454 buf[8] = (png_byte)bit_depth;
455 buf[9] = (png_byte)color_type;
456 buf[10] = (png_byte)compression_type;
457 buf[11] = (png_byte)filter_type;
458 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500459
460 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600461 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500462
Andreas Dilger47a0c421997-05-16 02:46:07 -0500463 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600464 png_ptr->zstream.zalloc = png_zalloc;
465 png_ptr->zstream.zfree = png_zfree;
466 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500467 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500468 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
470 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500472 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500473 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500474 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500475 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500476 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500477 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500478 png_ptr->zlib_strategy = Z_FILTERED;
479 else
480 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
481 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500482 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500483 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500484 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500485 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500486 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500487 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500488 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500489 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600490 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 png_ptr->zlib_method, png_ptr->zlib_window_bits,
492 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600493 png_ptr->zstream.next_out = png_ptr->zbuf;
494 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500495
Guy Schalnate5a37791996-06-05 15:50:50 -0500496 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500497}
498
499/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500500 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600501 * structure.
502 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500503void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500505{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600506#ifdef PNG_USE_LOCAL_ARRAYS
507 PNG_PLTE;
508#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500509 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600510 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500511 png_byte buf[3];
512
Andreas Dilger47a0c421997-05-16 02:46:07 -0500513 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500514 if ((
515#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
516 !png_ptr->empty_plte_permitted &&
517#endif
518 num_pal == 0) || num_pal > 256)
519 {
520 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
521 {
522 png_error(png_ptr, "Invalid number of colors in palette");
523 }
524 else
525 {
526 png_warning(png_ptr, "Invalid number of colors in palette");
527 return;
528 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500529 }
530
Andreas Dilger47a0c421997-05-16 02:46:07 -0500531 png_ptr->num_palette = (png_uint_16)num_pal;
532 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500533
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600534 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500536 {
537 buf[0] = pal_ptr->red;
538 buf[1] = pal_ptr->green;
539 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500541 }
542 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500543 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500544}
545
546/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500547void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500548png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500549{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600550#ifdef PNG_USE_LOCAL_ARRAYS
551 PNG_IDAT;
552#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500553 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600554 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500555 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500556}
557
558/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500559void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600560png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500561{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600562#ifdef PNG_USE_LOCAL_ARRAYS
563 PNG_IEND;
564#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600566 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500568}
569
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500570#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500571/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600572#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500573void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500575{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600576#ifdef PNG_USE_LOCAL_ARRAYS
577 PNG_gAMA;
578#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500579 png_uint_32 igamma;
580 png_byte buf[4];
581
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600583 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600584 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500585 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600586 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500587}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500588#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600589#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500590void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600591png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600592{
593#ifdef PNG_USE_LOCAL_ARRAYS
594 PNG_gAMA;
595#endif
596 png_byte buf[4];
597
598 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600599 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600600 png_save_uint_32(buf, file_gamma);
601 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
602}
603#endif
604#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500605
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600606#if defined(PNG_WRITE_sRGB_SUPPORTED)
607/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500608void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600609png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600610{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600611#ifdef PNG_USE_LOCAL_ARRAYS
612 PNG_sRGB;
613#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600614 png_byte buf[1];
615
616 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600617 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600618 png_warning(png_ptr,
619 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600620 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600621 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600622}
623#endif
624
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600625#if defined(PNG_WRITE_iCCP_SUPPORTED)
626/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500627void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600628png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
629 png_charp profile, int profile_len)
630{
631#ifdef PNG_USE_LOCAL_ARRAYS
632 PNG_iCCP;
633#endif
634 png_size_t name_len;
635 png_charp new_name;
636 compression_state comp;
637
638 png_debug(1, "in png_write_iCCP\n");
639 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
640 &new_name)) == 0)
641 {
642 png_warning(png_ptr, "Empty keyword in iCCP chunk");
643 return;
644 }
645
646 if (compression_type)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600647 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600648
649 if (profile == NULL || *profile == '\0')
650 profile_len = 0;
651
652 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500653 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
654 PNG_TEXT_COMPRESSION_zTXt, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600655
656 /* make sure we include the NULL after the name and the compression type */
657 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
658 (png_uint_32)name_len+profile_len+2);
659 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
660
661 if (profile_len)
662 png_write_compressed_data_out(png_ptr, &comp);
663
664 png_write_chunk_end(png_ptr);
665 png_free(png_ptr, new_name);
666}
667#endif
668
669#if defined(PNG_WRITE_sPLT_SUPPORTED)
670/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500671void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600672png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600673{
674#ifdef PNG_USE_LOCAL_ARRAYS
675 PNG_sPLT;
676#endif
677 png_size_t name_len;
678 png_charp new_name;
679 png_byte entrybuf[10];
680 int entry_size = (spalette->depth == 8 ? 6 : 10);
681 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600682 png_sPLT_entryp ep;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600683
684 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600685 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
686 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600687 {
688 png_warning(png_ptr, "Empty keyword in sPLT chunk");
689 return;
690 }
691
692 /* make sure we include the NULL after the name */
693 png_write_chunk_start(png_ptr, (png_bytep) png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600694 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600695 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600696 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600697
698 /* loop through each palette entry, writing appropriately */
699 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
700 {
701 if (spalette->depth == 8)
702 {
703 entrybuf[0] = (png_byte)ep->red;
704 entrybuf[1] = (png_byte)ep->green;
705 entrybuf[2] = (png_byte)ep->blue;
706 entrybuf[3] = (png_byte)ep->alpha;
707 png_save_uint_16(entrybuf + 4, ep->frequency);
708 }
709 else
710 {
711 png_save_uint_16(entrybuf + 0, ep->red);
712 png_save_uint_16(entrybuf + 2, ep->green);
713 png_save_uint_16(entrybuf + 4, ep->blue);
714 png_save_uint_16(entrybuf + 6, ep->alpha);
715 png_save_uint_16(entrybuf + 8, ep->frequency);
716 }
717 png_write_chunk_data(png_ptr, entrybuf, entry_size);
718 }
719
720 png_write_chunk_end(png_ptr);
721 png_free(png_ptr, new_name);
722}
723#endif
724
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500725#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500726/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500727void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600728png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500729{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600730#ifdef PNG_USE_LOCAL_ARRAYS
731 PNG_sBIT;
732#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500733 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500735
Andreas Dilger47a0c421997-05-16 02:46:07 -0500736 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600737 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500738 if (color_type & PNG_COLOR_MASK_COLOR)
739 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600740 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500741
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500742 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
743 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600744 if (sbit->red == 0 || sbit->red > maxbits ||
745 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500746 sbit->blue == 0 || sbit->blue > maxbits)
747 {
748 png_warning(png_ptr, "Invalid sBIT depth specified");
749 return;
750 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500751 buf[0] = sbit->red;
752 buf[1] = sbit->green;
753 buf[2] = sbit->blue;
754 size = 3;
755 }
756 else
757 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500758 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
759 {
760 png_warning(png_ptr, "Invalid sBIT depth specified");
761 return;
762 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500763 buf[0] = sbit->gray;
764 size = 1;
765 }
766
767 if (color_type & PNG_COLOR_MASK_ALPHA)
768 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500769 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
770 {
771 png_warning(png_ptr, "Invalid sBIT depth specified");
772 return;
773 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500774 buf[size++] = sbit->alpha;
775 }
776
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600777 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500778}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500779#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500780
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500781#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500782/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600783#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500784void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500785png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600786 double red_x, double red_y, double green_x, double green_y,
787 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500788{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600789#ifdef PNG_USE_LOCAL_ARRAYS
790 PNG_cHRM;
791#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500792 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600793 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500794
Andreas Dilger47a0c421997-05-16 02:46:07 -0500795 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600796 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500797 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
798 white_x + white_y > 1.0)
799 {
800 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600801#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600802 printf("white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600803#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500804 return;
805 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600806 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500807 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600808 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500809 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500810
811 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
812 red_x + red_y > 1.0)
813 {
814 png_warning(png_ptr, "Invalid cHRM red point specified");
815 return;
816 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600817 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500818 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600819 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500820 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500821
822 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
823 green_x + green_y > 1.0)
824 {
825 png_warning(png_ptr, "Invalid cHRM green point specified");
826 return;
827 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600828 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500829 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600830 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500831 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500832
833 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
834 blue_x + blue_y > 1.0)
835 {
836 png_warning(png_ptr, "Invalid cHRM blue point specified");
837 return;
838 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600839 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500840 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600841 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500842 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500843
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600844 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500845}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500846#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600847#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500848void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600849png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
850 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
851 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
852 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600853{
854#ifdef PNG_USE_LOCAL_ARRAYS
855 PNG_cHRM;
856#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600857 png_byte buf[32];
858
859 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600860 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600861 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
862 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600863 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600864#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600865 printf("white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600866#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867 return;
868 }
869 png_save_uint_32(buf, white_x);
870 png_save_uint_32(buf + 4, white_y);
871
872 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
873 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600874 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600875 return;
876 }
877 png_save_uint_32(buf + 8, red_x);
878 png_save_uint_32(buf + 12, red_y);
879
880 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
881 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600882 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600883 return;
884 }
885 png_save_uint_32(buf + 16, green_x);
886 png_save_uint_32(buf + 20, green_y);
887
888 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
889 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600890 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600891 return;
892 }
893 png_save_uint_32(buf + 24, blue_x);
894 png_save_uint_32(buf + 28, blue_y);
895
896 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
897}
898#endif
899#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500900
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500901#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500902/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500903void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600904png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500905 int num_trans, int color_type)
906{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600907#ifdef PNG_USE_LOCAL_ARRAYS
908 PNG_tRNS;
909#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500910 png_byte buf[6];
911
Andreas Dilger47a0c421997-05-16 02:46:07 -0500912 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500913 if (color_type == PNG_COLOR_TYPE_PALETTE)
914 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600915 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500916 {
917 png_warning(png_ptr,"Invalid number of transparent colors specified");
918 return;
919 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500920 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600921 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500922 }
923 else if (color_type == PNG_COLOR_TYPE_GRAY)
924 {
925 /* one 16 bit value */
926 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600927 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500928 }
929 else if (color_type == PNG_COLOR_TYPE_RGB)
930 {
931 /* three 16 bit values */
932 png_save_uint_16(buf, tran->red);
933 png_save_uint_16(buf + 2, tran->green);
934 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600935 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500936 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500937 else
938 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600939 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500940 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500941}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500942#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500943
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500944#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500945/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500946void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600947png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500948{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600949#ifdef PNG_USE_LOCAL_ARRAYS
950 PNG_bKGD;
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_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500955 if (color_type == PNG_COLOR_TYPE_PALETTE)
956 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500957 if (
958#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
959 (!png_ptr->empty_plte_permitted ||
960 (png_ptr->empty_plte_permitted && png_ptr->num_palette)) &&
961#endif
962 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500963 {
964 png_warning(png_ptr, "Invalid background palette index");
965 return;
966 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500967 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600968 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500969 }
970 else if (color_type & PNG_COLOR_MASK_COLOR)
971 {
972 png_save_uint_16(buf, back->red);
973 png_save_uint_16(buf + 2, back->green);
974 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600975 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500976 }
977 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600978 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500979 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600980 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500981 }
982}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500983#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500984
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500985#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500986/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500987void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600988png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500989{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600990#ifdef PNG_USE_LOCAL_ARRAYS
991 PNG_hIST;
992#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600993 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500994 png_byte buf[3];
995
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600997 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500998 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500999 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1000 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001001 png_warning(png_ptr, "Invalid number of histogram entries specified");
1002 return;
1003 }
1004
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001005 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001007 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001008 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001009 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001010 }
1011 png_write_chunk_end(png_ptr);
1012}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001013#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001014
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001015#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1016 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001017/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1018 * and if invalid, correct the keyword rather than discarding the entire
1019 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1020 * length, forbids leading or trailing whitespace, multiple internal spaces,
1021 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001022 *
1023 * The new_key is allocated to hold the corrected keyword and must be freed
1024 * by the calling routine. This avoids problems with trying to write to
1025 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001026 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001027png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001028png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001029{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001030 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001031 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001032 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001033
Andreas Dilger47a0c421997-05-16 02:46:07 -05001034 png_debug(1, "in png_check_keyword\n");
1035 *new_key = NULL;
1036
1037 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001038 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001039 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001040 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001041 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001042
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1044
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001045 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046
1047 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001049 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001050 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001052#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001053 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001054
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001055 sprintf(msg, "invalid keyword character 0x%02X", *kp);
1056 png_chunk_warning(png_ptr, msg);
1057#else
1058 png_chunk_warning(png_ptr, "invalid character in keyword");
1059#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 *dp = ' ';
1061 }
1062 else
1063 {
1064 *dp = *kp;
1065 }
1066 }
1067 *dp = '\0';
1068
1069 /* Remove any trailing white space. */
1070 kp = *new_key + key_len - 1;
1071 if (*kp == ' ')
1072 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001073 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001074
1075 while (*kp == ' ')
1076 {
1077 *(kp--) = '\0';
1078 key_len--;
1079 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001080 }
1081
1082 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001083 kp = *new_key;
1084 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001085 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001086 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001087
1088 while (*kp == ' ')
1089 {
1090 kp++;
1091 key_len--;
1092 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001093 }
1094
Andreas Dilger47a0c421997-05-16 02:46:07 -05001095 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001096
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 /* Remove multiple internal spaces. */
1098 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001099 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001101 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 *(dp++) = *kp;
1103 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001104 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001106 {
1107 key_len--;
1108 }
1109 else
1110 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001111 *(dp++) = *kp;
1112 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113 }
1114 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001116
1117 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001118 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001119 png_free(png_ptr, *new_key);
1120 *new_key=NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001121 png_chunk_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 }
1123
1124 if (key_len > 79)
1125 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001126 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001127 new_key[79] = '\0';
1128 key_len = 79;
1129 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001130
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001131 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001132}
1133#endif
1134
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001135#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001136/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001137void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001138png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001139 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001140{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001141#ifdef PNG_USE_LOCAL_ARRAYS
1142 PNG_tEXt;
1143#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001145 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001146
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 png_debug(1, "in png_write_tEXt\n");
1148 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1149 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001150 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001151 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001153
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001155 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001156 else
1157 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001158
1159 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001160 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 -05001161 /*
1162 * We leave it to the application to meet PNG-1.0 requirements on the
1163 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1164 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001165 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001166 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001167 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001168 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001170
Guy Schalnat0d580581995-07-20 02:43:20 -05001171 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001173}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001174#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001175
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001176#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001178void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001179png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001180 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001181{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001182#ifdef PNG_USE_LOCAL_ARRAYS
1183 PNG_zTXt;
1184#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001185 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001186 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001187 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001188 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001189
Andreas Dilger47a0c421997-05-16 02:46:07 -05001190 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001191
Andreas Dilger47a0c421997-05-16 02:46:07 -05001192 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001193 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001194 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001196 }
1197
Andreas Dilger47a0c421997-05-16 02:46:07 -05001198 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1199 {
1200 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1201 png_free(png_ptr, new_key);
1202 return;
1203 }
1204
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001205 text_len = png_strlen(text);
1206
Andreas Dilger47a0c421997-05-16 02:46:07 -05001207 png_free(png_ptr, new_key);
1208
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001209 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001210 text_len = png_text_compress(png_ptr, text, text_len, compression,
1211 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001212
1213 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001214 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1215 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001216 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001218 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001219 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001220 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001221 /* write the compressed data */
1222 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001223
Guy Schalnat0d580581995-07-20 02:43:20 -05001224 /* close the chunk */
1225 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001226}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001227#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001228
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001229#if defined(PNG_WRITE_iTXt_SUPPORTED)
1230/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001231void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001232png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001233 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001234{
1235#ifdef PNG_USE_LOCAL_ARRAYS
1236 PNG_iTXt;
1237#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001238 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001239 png_charp new_lang, new_key;
1240 png_byte cbuf[2];
1241 compression_state comp;
1242
1243 png_debug(1, "in png_write_iTXt\n");
1244
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001245 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1246 {
1247 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1248 return;
1249 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001250 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1251 &new_lang))==0)
1252 {
1253 png_warning(png_ptr, "Empty language field in iTXt chunk");
1254 return;
1255 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001256 lang_key_len = png_strlen(lang_key);
1257 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001258
1259 if (text == NULL || *text == '\0')
1260 text_len = 0;
1261
1262 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001263 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1264 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001265
1266 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001267 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001268
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001269 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1270 (png_uint_32)(
1271 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1272 + key_len
1273 + lang_len
1274 + lang_key_len
1275 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001276
1277 /*
1278 * We leave it to the application to meet PNG-1.0 requirements on the
1279 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1280 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1281 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1282 */
1283 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001284
1285 /* set the compression flag */
1286 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1287 compression == PNG_TEXT_COMPRESSION_NONE)
1288 cbuf[0] = 0;
1289 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1290 cbuf[0] = 1;
1291 /* set the compression method */
1292 cbuf[1] = 0;
1293 png_write_chunk_data(png_ptr, cbuf, 2);
1294
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001295 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001296 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001297 png_write_chunk_data(png_ptr, '\0', 1);
1298
1299 png_write_compressed_data_out(png_ptr, &comp);
1300
1301 png_write_chunk_end(png_ptr);
1302 png_free(png_ptr, new_key);
1303 png_free(png_ptr, new_lang);
1304}
1305#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001306
1307#if defined(PNG_WRITE_oFFs_SUPPORTED)
1308/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001309void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001310png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1311 png_uint_32 y_offset,
1312 int unit_type)
1313{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001314#ifdef PNG_USE_LOCAL_ARRAYS
1315 PNG_oFFs;
1316#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001317 png_byte buf[9];
1318
1319 png_debug(1, "in png_write_oFFs\n");
1320 if (unit_type >= PNG_OFFSET_LAST)
1321 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1322
1323 png_save_uint_32(buf, x_offset);
1324 png_save_uint_32(buf + 4, y_offset);
1325 buf[8] = (png_byte)unit_type;
1326
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001327 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001328}
1329#endif
1330
1331#if defined(PNG_WRITE_pCAL_SUPPORTED)
1332/* write the pCAL chunk (png-scivis-19970203) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001333void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1335 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1336{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001337#ifdef PNG_USE_LOCAL_ARRAYS
1338 PNG_pCAL;
1339#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001340 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 png_uint_32p params_len;
1342 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001343 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 int i;
1345
1346 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1347 if (type >= PNG_EQUATION_LAST)
1348 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1349
1350 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1351 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1352 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1353 png_debug1(3, "pCAL units length = %d\n", units_len);
1354 total_len = purpose_len + units_len + 10;
1355
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001356 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1357 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001358
1359 /* Find the length of each parameter, making sure we don't count the
1360 null terminator for the last parameter. */
1361 for (i = 0; i < nparams; i++)
1362 {
1363 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1364 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
1365 total_len += (png_size_t)params_len[i];
1366 }
1367
1368 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001369 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001370 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001371 png_save_int_32(buf, X0);
1372 png_save_int_32(buf + 4, X1);
1373 buf[8] = (png_byte)type;
1374 buf[9] = (png_byte)nparams;
1375 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1376 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1377
1378 png_free(png_ptr, new_purpose);
1379
1380 for (i = 0; i < nparams; i++)
1381 {
1382 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1383 (png_size_t)params_len[i]);
1384 }
1385
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001386 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001387 png_write_chunk_end(png_ptr);
1388}
1389#endif
1390
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001391#if defined(PNG_WRITE_sCAL_SUPPORTED)
1392/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001393#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001394void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001395png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001396{
1397#ifdef PNG_USE_LOCAL_ARRAYS
1398 PNG_sCAL;
1399#endif
1400 png_size_t total_len;
1401 char wbuf[32], hbuf[32];
1402
1403 png_debug(1, "in png_write_sCAL\n");
1404
1405 sprintf(wbuf, "%12.12e", width);
1406 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001407 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001408
1409 png_debug1(3, "sCAL total length = %d\n", total_len);
1410 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001411 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001412 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1413 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1414
1415 png_write_chunk_end(png_ptr);
1416}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001417#else
1418#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001419void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001420png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001421 png_charp height)
1422{
1423#ifdef PNG_USE_LOCAL_ARRAYS
1424 PNG_sCAL;
1425#endif
1426 png_size_t total_len;
1427 char wbuf[32], hbuf[32];
1428
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001429 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001430
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001431 strcpy(wbuf,(const char *)width);
1432 strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001433 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001434
1435 png_debug1(3, "sCAL total length = %d\n", total_len);
1436 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001437 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001438 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1439 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1440
1441 png_write_chunk_end(png_ptr);
1442}
1443#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001444#endif
1445#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001446
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001447#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001448/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001449void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001450png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001451 png_uint_32 y_pixels_per_unit,
1452 int unit_type)
1453{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001454#ifdef PNG_USE_LOCAL_ARRAYS
1455 PNG_pHYs;
1456#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001457 png_byte buf[9];
1458
Andreas Dilger47a0c421997-05-16 02:46:07 -05001459 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001460 if (unit_type >= PNG_RESOLUTION_LAST)
1461 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1462
Guy Schalnat0d580581995-07-20 02:43:20 -05001463 png_save_uint_32(buf, x_pixels_per_unit);
1464 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001465 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001466
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001467 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001468}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001469#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001470
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001471#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001472/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1473 * or png_convert_from_time_t(), or fill in the structure yourself.
1474 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001475void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001476png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001477{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001478#ifdef PNG_USE_LOCAL_ARRAYS
1479 PNG_tIME;
1480#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001481 png_byte buf[7];
1482
Andreas Dilger47a0c421997-05-16 02:46:07 -05001483 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001484 if (mod_time->month > 12 || mod_time->month < 1 ||
1485 mod_time->day > 31 || mod_time->day < 1 ||
1486 mod_time->hour > 23 || mod_time->second > 60)
1487 {
1488 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1489 return;
1490 }
1491
Guy Schalnat0d580581995-07-20 02:43:20 -05001492 png_save_uint_16(buf, mod_time->year);
1493 buf[2] = mod_time->month;
1494 buf[3] = mod_time->day;
1495 buf[4] = mod_time->hour;
1496 buf[5] = mod_time->minute;
1497 buf[6] = mod_time->second;
1498
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001499 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001500}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001501#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001502
1503/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001504void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001505png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001506{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001507#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001508 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001509
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001510 /* start of interlace block */
1511 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001512
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001513 /* offset to next interlace block */
1514 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001515
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001516 /* start of interlace block in the y direction */
1517 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001518
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001519 /* offset to next interlace block in the y direction */
1520 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001521#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001522
Andreas Dilger47a0c421997-05-16 02:46:07 -05001523 png_size_t buf_size;
1524
1525 png_debug(1, "in png_write_start_row\n");
1526 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1527 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1528
Guy Schalnat0d580581995-07-20 02:43:20 -05001529 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001530 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001531 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001532
1533 /* set up filtering buffer, if using this filter */
1534 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001535 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001536 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001537 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001538 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001539 }
1540
Andreas Dilger47a0c421997-05-16 02:46:07 -05001541 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001542 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1543 {
1544 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001545 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001547
1548 if (png_ptr->do_filter & PNG_FILTER_UP)
1549 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001550 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001551 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001552 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001553 }
1554
1555 if (png_ptr->do_filter & PNG_FILTER_AVG)
1556 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001557 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1558 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001559 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001560 }
1561
1562 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1563 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001564 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001565 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001566 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001567 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001568 }
1569
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001570#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001571 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001572 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001573 {
1574 if (!(png_ptr->transformations & PNG_INTERLACE))
1575 {
1576 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1577 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001578 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1579 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001580 }
1581 else
1582 {
1583 png_ptr->num_rows = png_ptr->height;
1584 png_ptr->usr_width = png_ptr->width;
1585 }
1586 }
1587 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001588#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001589 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001590 png_ptr->num_rows = png_ptr->height;
1591 png_ptr->usr_width = png_ptr->width;
1592 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001593 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1594 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001595}
1596
Andreas Dilger47a0c421997-05-16 02:46:07 -05001597/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001598void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001599png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001600{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001601#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001602 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001603
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001604 /* start of interlace block */
1605 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001606
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001607 /* offset to next interlace block */
1608 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001609
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001610 /* start of interlace block in the y direction */
1611 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001612
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001613 /* offset to next interlace block in the y direction */
1614 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001615#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001616
Guy Schalnat0d580581995-07-20 02:43:20 -05001617 int ret;
1618
Andreas Dilger47a0c421997-05-16 02:46:07 -05001619 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001620 /* next row */
1621 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001622
Guy Schalnat0d580581995-07-20 02:43:20 -05001623 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001624 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001625 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001626
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001627#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001628 /* if interlaced, go to next pass */
1629 if (png_ptr->interlaced)
1630 {
1631 png_ptr->row_number = 0;
1632 if (png_ptr->transformations & PNG_INTERLACE)
1633 {
1634 png_ptr->pass++;
1635 }
1636 else
1637 {
1638 /* loop until we find a non-zero width or height pass */
1639 do
1640 {
1641 png_ptr->pass++;
1642 if (png_ptr->pass >= 7)
1643 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001644 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001645 png_pass_inc[png_ptr->pass] - 1 -
1646 png_pass_start[png_ptr->pass]) /
1647 png_pass_inc[png_ptr->pass];
1648 png_ptr->num_rows = (png_ptr->height +
1649 png_pass_yinc[png_ptr->pass] - 1 -
1650 png_pass_ystart[png_ptr->pass]) /
1651 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001652 if (png_ptr->transformations & PNG_INTERLACE)
1653 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001654 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1655
1656 }
1657
Guy Schalnate5a37791996-06-05 15:50:50 -05001658 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001659 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001660 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001661 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001662 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001663 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001664 (png_uint_32)png_ptr->usr_bit_depth *
1665 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001666 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001667 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001668 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001669#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001670
1671 /* if we get here, we've just written the last row, so we need
1672 to flush the compressor */
1673 do
1674 {
1675 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001676 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001677 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001678 if (ret == Z_OK)
1679 {
1680 /* check to see if we need more room */
1681 if (!(png_ptr->zstream.avail_out))
1682 {
1683 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1684 png_ptr->zstream.next_out = png_ptr->zbuf;
1685 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1686 }
1687 }
1688 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001689 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001690 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001691 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001692 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001693 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001694 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001695 } while (ret != Z_STREAM_END);
1696
1697 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001698 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001699 {
1700 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001701 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001702 }
1703
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001704 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001705}
1706
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001707#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001708/* Pick out the correct pixels for the interlace pass.
1709 * The basic idea here is to go through the row with a source
1710 * pointer and a destination pointer (sp and dp), and copy the
1711 * correct pixels for the pass. As the row gets compacted,
1712 * sp will always be >= dp, so we should never overwrite anything.
1713 * See the default: case for the easiest code to understand.
1714 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001715void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001716png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001717{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001718#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001719 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001720
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001721 /* start of interlace block */
1722 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001723
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001724 /* offset to next interlace block */
1725 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001726#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001727
Andreas Dilger47a0c421997-05-16 02:46:07 -05001728 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001729 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001730#if defined(PNG_USELESS_TESTS_SUPPORTED)
1731 if (row != NULL && row_info != NULL && pass < 6)
1732#else
1733 if (pass < 6)
1734#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001735 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001736 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001737 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001738 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001739 case 1:
1740 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001741 png_bytep sp;
1742 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001743 int shift;
1744 int d;
1745 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001746 png_uint_32 i;
1747 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001748
1749 dp = row;
1750 d = 0;
1751 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001752 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001753 i += png_pass_inc[pass])
1754 {
1755 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001756 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001757 d |= (value << shift);
1758
1759 if (shift == 0)
1760 {
1761 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001762 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001763 d = 0;
1764 }
1765 else
1766 shift--;
1767
1768 }
1769 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001770 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001771 break;
1772 }
1773 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001774 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001775 png_bytep sp;
1776 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001777 int shift;
1778 int d;
1779 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001780 png_uint_32 i;
1781 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001782
1783 dp = row;
1784 shift = 6;
1785 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001786 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001787 i += png_pass_inc[pass])
1788 {
1789 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001790 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001791 d |= (value << shift);
1792
1793 if (shift == 0)
1794 {
1795 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001796 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001797 d = 0;
1798 }
1799 else
1800 shift -= 2;
1801 }
1802 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001803 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001804 break;
1805 }
1806 case 4:
1807 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001808 png_bytep sp;
1809 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001810 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001811 int d;
1812 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001813 png_uint_32 i;
1814 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001815
1816 dp = row;
1817 shift = 4;
1818 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001819 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001820 i += png_pass_inc[pass])
1821 {
1822 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001823 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 d |= (value << shift);
1825
1826 if (shift == 0)
1827 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001828 shift = 4;
1829 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001830 d = 0;
1831 }
1832 else
1833 shift -= 4;
1834 }
1835 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001836 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001837 break;
1838 }
1839 default:
1840 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001841 png_bytep sp;
1842 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001843 png_uint_32 i;
1844 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001845 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001846
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001847 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 dp = row;
1849 /* find out how many bytes each pixel takes up */
1850 pixel_bytes = (row_info->pixel_depth >> 3);
1851 /* loop through the row, only looking at the pixels that
1852 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001853 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001854 i += png_pass_inc[pass])
1855 {
1856 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001857 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001858 /* move the pixel */
1859 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001860 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001861 /* next pixel */
1862 dp += pixel_bytes;
1863 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001864 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001865 }
1866 }
1867 /* set new row width */
1868 row_info->width = (row_info->width +
1869 png_pass_inc[pass] - 1 -
1870 png_pass_start[pass]) /
1871 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001872 row_info->rowbytes = ((row_info->width *
1873 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001874 }
1875}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001876#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001877
Andreas Dilger47a0c421997-05-16 02:46:07 -05001878/* This filters the row, chooses which filter to use, if it has not already
1879 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001880 * chosen filter.
1881 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001882#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001883#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001884#define PNG_LOMASK ((png_uint_32)0xffffL)
1885#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001886void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001887png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001888{
Guy Schalnate5a37791996-06-05 15:50:50 -05001889 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001890 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001891 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001892 png_uint_32 row_bytes = row_info->rowbytes;
1893#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1894 int num_p_filters = (int)png_ptr->num_prev_filters;
1895#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001896
Andreas Dilger47a0c421997-05-16 02:46:07 -05001897 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001898 /* find out how many bytes offset each pixel is */
1899 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001900
1901 prev_row = png_ptr->prev_row;
1902 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001903 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001904
Andreas Dilger47a0c421997-05-16 02:46:07 -05001905 /* The prediction method we use is to find which method provides the
1906 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001907 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001908 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001909 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001910 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001911 * predictive" method (not implemented yet), which does test compressions
1912 * of lines using different filter methods, and then chooses the
1913 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001914 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001915 *
1916 * GRR 980525: consider also
1917 * (1) minimum sum of absolute differences from running average (i.e.,
1918 * keep running sum of non-absolute differences & count of bytes)
1919 * [track dispersion, too? restart average if dispersion too large?]
1920 * (1b) minimum sum of absolute differences from sliding average, probably
1921 * with window size <= deflate window (usually 32K)
1922 * (2) minimum sum of squared differences from zero or running average
1923 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001924 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001925
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001926
Guy Schalnate5a37791996-06-05 15:50:50 -05001927 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001928 * that has been chosen, as it doesn't actually do anything to the data.
1929 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001930 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001931 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001933 png_bytep rp;
1934 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001935 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001936 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001937
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001938 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001939 {
1940 v = *rp;
1941 sum += (v < 128) ? v : 256 - v;
1942 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001943
1944#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1945 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1946 {
1947 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001948 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001949 sumlo = sum & PNG_LOMASK;
1950 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1951
1952 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001953 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001954 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001955 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001956 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001957 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001958 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001959 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960 PNG_WEIGHT_SHIFT;
1961 }
1962 }
1963
1964 /* Factor in the cost of this filter (this is here for completeness,
1965 * but it makes no sense to have a "cost" for the NONE filter, as
1966 * it has the minimum possible computational cost - none).
1967 */
1968 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1969 PNG_COST_SHIFT;
1970 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1971 PNG_COST_SHIFT;
1972
1973 if (sumhi > PNG_HIMASK)
1974 sum = PNG_MAXSUM;
1975 else
1976 sum = (sumhi << PNG_HISHIFT) + sumlo;
1977 }
1978#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001979 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001980 }
1981
Guy Schalnate5a37791996-06-05 15:50:50 -05001982 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001983 if (filter_to_do == PNG_FILTER_SUB)
1984 /* it's the only filter so no testing is needed */
1985 {
1986 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001987 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001988 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1989 i++, rp++, dp++)
1990 {
1991 *dp = *rp;
1992 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001993 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001994 i++, rp++, lp++, dp++)
1995 {
1996 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
1997 }
1998 best_row = png_ptr->sub_row;
1999 }
2000
2001 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002002 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002003 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002004 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002005 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002006 int v;
2007
2008#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002009 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002010 * would reduce the sum of this filter, so that we can do the
2011 * early exit comparison without scaling the sum each time.
2012 */
2013 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2014 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002015 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002016 png_uint_32 lmhi, lmlo;
2017 lmlo = lmins & PNG_LOMASK;
2018 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2019
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002020 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002021 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002022 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002023 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002024 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002025 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002026 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002027 PNG_WEIGHT_SHIFT;
2028 }
2029 }
2030
2031 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2032 PNG_COST_SHIFT;
2033 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2034 PNG_COST_SHIFT;
2035
2036 if (lmhi > PNG_HIMASK)
2037 lmins = PNG_MAXSUM;
2038 else
2039 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2040 }
2041#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002042
Guy Schalnate5a37791996-06-05 15:50:50 -05002043 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2044 i++, rp++, dp++)
2045 {
2046 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002047
Guy Schalnate5a37791996-06-05 15:50:50 -05002048 sum += (v < 128) ? v : 256 - v;
2049 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002050 for (lp = row_buf + 1; i < row_info->rowbytes;
2051 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002052 {
2053 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002054
Guy Schalnate5a37791996-06-05 15:50:50 -05002055 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002056
2057 if (sum > lmins) /* We are already worse, don't continue. */
2058 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002059 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002060
2061#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2062 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2063 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002064 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002065 png_uint_32 sumhi, sumlo;
2066 sumlo = sum & PNG_LOMASK;
2067 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2068
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002069 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002070 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002071 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002072 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002073 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002074 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002075 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076 PNG_WEIGHT_SHIFT;
2077 }
2078 }
2079
2080 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2081 PNG_COST_SHIFT;
2082 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2083 PNG_COST_SHIFT;
2084
2085 if (sumhi > PNG_HIMASK)
2086 sum = PNG_MAXSUM;
2087 else
2088 sum = (sumhi << PNG_HISHIFT) + sumlo;
2089 }
2090#endif
2091
Guy Schalnate5a37791996-06-05 15:50:50 -05002092 if (sum < mins)
2093 {
2094 mins = sum;
2095 best_row = png_ptr->sub_row;
2096 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002097 }
2098
Guy Schalnate5a37791996-06-05 15:50:50 -05002099 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002100 if (filter_to_do == PNG_FILTER_UP)
2101 {
2102 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002103 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002104
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002105 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002106 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002107 i++, rp++, pp++, dp++)
2108 {
2109 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2110 }
2111 best_row = png_ptr->up_row;
2112 }
2113
2114 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002115 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002116 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002117 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002118 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002119 int v;
2120
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002121
Andreas Dilger47a0c421997-05-16 02:46:07 -05002122#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2123 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2124 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002125 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002126 png_uint_32 lmhi, lmlo;
2127 lmlo = lmins & PNG_LOMASK;
2128 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2129
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002130 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002131 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002132 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002133 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002134 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002136 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 PNG_WEIGHT_SHIFT;
2138 }
2139 }
2140
2141 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2142 PNG_COST_SHIFT;
2143 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2144 PNG_COST_SHIFT;
2145
2146 if (lmhi > PNG_HIMASK)
2147 lmins = PNG_MAXSUM;
2148 else
2149 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2150 }
2151#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002152
2153 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002154 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002155 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002156 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002157
2158 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002159
2160 if (sum > lmins) /* We are already worse, don't continue. */
2161 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002162 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002163
2164#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2165 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2166 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002167 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168 png_uint_32 sumhi, sumlo;
2169 sumlo = sum & PNG_LOMASK;
2170 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2171
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002172 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002173 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002174 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002176 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002177 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002178 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002179 PNG_WEIGHT_SHIFT;
2180 }
2181 }
2182
2183 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2184 PNG_COST_SHIFT;
2185 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2186 PNG_COST_SHIFT;
2187
2188 if (sumhi > PNG_HIMASK)
2189 sum = PNG_MAXSUM;
2190 else
2191 sum = (sumhi << PNG_HISHIFT) + sumlo;
2192 }
2193#endif
2194
Guy Schalnate5a37791996-06-05 15:50:50 -05002195 if (sum < mins)
2196 {
2197 mins = sum;
2198 best_row = png_ptr->up_row;
2199 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002200 }
2201
Guy Schalnate5a37791996-06-05 15:50:50 -05002202 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002203 if (filter_to_do == PNG_FILTER_AVG)
2204 {
2205 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002206 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002207 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002208 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002209 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002210 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002211 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002212 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002213 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002214 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2215 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002216 }
2217 best_row = png_ptr->avg_row;
2218 }
2219
2220 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002221 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002222 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002223 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002224 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002225 int v;
2226
2227#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2228 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2229 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002230 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002231 png_uint_32 lmhi, lmlo;
2232 lmlo = lmins & PNG_LOMASK;
2233 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2234
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002235 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002236 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002237 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002238 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002239 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002240 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002241 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002242 PNG_WEIGHT_SHIFT;
2243 }
2244 }
2245
2246 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2247 PNG_COST_SHIFT;
2248 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2249 PNG_COST_SHIFT;
2250
2251 if (lmhi > PNG_HIMASK)
2252 lmins = PNG_MAXSUM;
2253 else
2254 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2255 }
2256#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002257
2258 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002260 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002262
2263 sum += (v < 128) ? v : 256 - v;
2264 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002265 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002266 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002267 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002268 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002269
2270 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002271
2272 if (sum > lmins) /* We are already worse, don't continue. */
2273 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002274 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002275
2276#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2277 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2278 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002279 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002280 png_uint_32 sumhi, sumlo;
2281 sumlo = sum & PNG_LOMASK;
2282 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2283
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002284 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002285 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002286 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002287 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002288 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002289 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002290 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002291 PNG_WEIGHT_SHIFT;
2292 }
2293 }
2294
2295 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2296 PNG_COST_SHIFT;
2297 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2298 PNG_COST_SHIFT;
2299
2300 if (sumhi > PNG_HIMASK)
2301 sum = PNG_MAXSUM;
2302 else
2303 sum = (sumhi << PNG_HISHIFT) + sumlo;
2304 }
2305#endif
2306
Guy Schalnate5a37791996-06-05 15:50:50 -05002307 if (sum < mins)
2308 {
2309 mins = sum;
2310 best_row = png_ptr->avg_row;
2311 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002312 }
2313
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002315 if (filter_to_do == PNG_FILTER_PAETH)
2316 {
2317 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002318 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002319 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002320 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002321 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002322 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002323 }
2324
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002325 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002326 {
2327 int a, b, c, pa, pb, pc, p;
2328
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002329 b = *pp++;
2330 c = *cp++;
2331 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002332
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002333 p = b - c;
2334 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002335
2336#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002337 pa = abs(p);
2338 pb = abs(pc);
2339 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002340#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002341 pa = p < 0 ? -p : p;
2342 pb = pc < 0 ? -pc : pc;
2343 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002344#endif
2345
2346 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2347
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002348 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002349 }
2350 best_row = png_ptr->paeth_row;
2351 }
2352
2353 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002354 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002355 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002357 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 int v;
2359
2360#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2361 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2362 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002363 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002364 png_uint_32 lmhi, lmlo;
2365 lmlo = lmins & PNG_LOMASK;
2366 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2367
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002368 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002370 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002371 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002372 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002373 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002374 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002375 PNG_WEIGHT_SHIFT;
2376 }
2377 }
2378
2379 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2380 PNG_COST_SHIFT;
2381 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2382 PNG_COST_SHIFT;
2383
2384 if (lmhi > PNG_HIMASK)
2385 lmins = PNG_MAXSUM;
2386 else
2387 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2388 }
2389#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002390
2391 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002392 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002393 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002394 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002395
2396 sum += (v < 128) ? v : 256 - v;
2397 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002398
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002399 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002400 {
2401 int a, b, c, pa, pb, pc, p;
2402
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002403 b = *pp++;
2404 c = *cp++;
2405 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002406
2407#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 p = b - c;
2409 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002410#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002411 pa = abs(p);
2412 pb = abs(pc);
2413 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002414#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002415 pa = p < 0 ? -p : p;
2416 pb = pc < 0 ? -pc : pc;
2417 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002418#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002419 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2420#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002421 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002422 pa = abs(p - a);
2423 pb = abs(p - b);
2424 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002425 if (pa <= pb && pa <= pc)
2426 p = a;
2427 else if (pb <= pc)
2428 p = b;
2429 else
2430 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002431#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002432
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002433 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002434
2435 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002436
2437 if (sum > lmins) /* We are already worse, don't continue. */
2438 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002439 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002440
2441#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2442 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2443 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002444 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002445 png_uint_32 sumhi, sumlo;
2446 sumlo = sum & PNG_LOMASK;
2447 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2448
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002451 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002452 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002453 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002454 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002455 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002456 PNG_WEIGHT_SHIFT;
2457 }
2458 }
2459
2460 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2461 PNG_COST_SHIFT;
2462 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2463 PNG_COST_SHIFT;
2464
2465 if (sumhi > PNG_HIMASK)
2466 sum = PNG_MAXSUM;
2467 else
2468 sum = (sumhi << PNG_HISHIFT) + sumlo;
2469 }
2470#endif
2471
Guy Schalnate5a37791996-06-05 15:50:50 -05002472 if (sum < mins)
2473 {
2474 best_row = png_ptr->paeth_row;
2475 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002476 }
2477
Andreas Dilger47a0c421997-05-16 02:46:07 -05002478 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002479
Guy Schalnate5a37791996-06-05 15:50:50 -05002480 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002481
2482#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2483 /* Save the type of filter we picked this time for future calculations */
2484 if (png_ptr->num_prev_filters > 0)
2485 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002486 int j;
2487 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002488 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002489 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002490 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002491 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002492 }
2493#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002494}
2495
2496
Andreas Dilger47a0c421997-05-16 02:46:07 -05002497/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002498void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002499png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2500{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002501 png_debug(1, "in png_write_filtered_row\n");
2502 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002503 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002504 png_ptr->zstream.next_in = filtered_row;
2505 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002506 /* repeat until we have compressed all the data */
2507 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002508 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002509 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002510
Guy Schalnate5a37791996-06-05 15:50:50 -05002511 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002512 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002513 /* check for compression errors */
2514 if (ret != Z_OK)
2515 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002516 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002517 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002518 else
2519 png_error(png_ptr, "zlib error");
2520 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002521
Guy Schalnate5a37791996-06-05 15:50:50 -05002522 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002523 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002524 {
2525 /* write the IDAT and reset the zlib output buffer */
2526 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002527 png_ptr->zstream.next_out = png_ptr->zbuf;
2528 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002529 }
2530 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002531 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002532
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002533 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002534 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002535 {
2536 png_bytep tptr;
2537
2538 tptr = png_ptr->prev_row;
2539 png_ptr->prev_row = png_ptr->row_buf;
2540 png_ptr->row_buf = tptr;
2541 }
2542
Guy Schalnate5a37791996-06-05 15:50:50 -05002543 /* finish row - updates counters and flushes zlib if last row */
2544 png_write_finish_row(png_ptr);
2545
2546#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2547 png_ptr->flush_rows++;
2548
2549 if (png_ptr->flush_dist > 0 &&
2550 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002551 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002552 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002553 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002554#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002555}