blob: df578c7e831a0e22969e4aff2650857e5c48b8b3 [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-Pehrsond4366722000-06-04 14:29:29 -05004 * libpng 1.0.7beta16 - June 4, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06006 * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050018void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
21 buf[0] = (png_byte)((i >> 24) & 0xff);
22 buf[1] = (png_byte)((i >> 16) & 0xff);
23 buf[2] = (png_byte)((i >> 8) & 0xff);
24 buf[3] = (png_byte)(i & 0xff);
25}
26
Andreas Dilger47a0c421997-05-16 02:46:07 -050027#if defined(PNG_WRITE_pCAL_SUPPORTED)
28/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050032void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40#endif
41
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042/* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
45 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050046void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050048{
49 buf[0] = (png_byte)((i >> 8) & 0xff);
50 buf[1] = (png_byte)(i & 0xff);
51}
52
Andreas Dilger47a0c421997-05-16 02:46:07 -050053/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
60 * functions instead.
61 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050062void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050065{
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050069}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
74 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050075void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050078{
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 png_byte buf[4];
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);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500535#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500536 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500537 {
538 buf[0] = pal_ptr->red;
539 buf[1] = pal_ptr->green;
540 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500543#else
544 /* This is a little slower but some buggy compilers need to do this instead */
545 pal_ptr=palette;
546 for (i = 0; i < num_pal; i++)
547 {
548 buf[0] = pal_ptr[i].red;
549 buf[1] = pal_ptr[i].green;
550 buf[2] = pal_ptr[i].blue;
551 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
552 }
553#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500554 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500555 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500556}
557
558/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500559void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500560png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500561{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600562#ifdef PNG_USE_LOCAL_ARRAYS
563 PNG_IDAT;
564#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600566 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500567 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500568}
569
570/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500571void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600572png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500573{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600574#ifdef PNG_USE_LOCAL_ARRAYS
575 PNG_IEND;
576#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600578 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600579 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500580}
581
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500582#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500583/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600584#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500585void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500586png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500587{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600588#ifdef PNG_USE_LOCAL_ARRAYS
589 PNG_gAMA;
590#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500591 png_uint_32 igamma;
592 png_byte buf[4];
593
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600595 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600596 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500597 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600598 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500599}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500600#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600601#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500602void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600603png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600604{
605#ifdef PNG_USE_LOCAL_ARRAYS
606 PNG_gAMA;
607#endif
608 png_byte buf[4];
609
610 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600611 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600612 png_save_uint_32(buf, file_gamma);
613 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
614}
615#endif
616#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500617
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600618#if defined(PNG_WRITE_sRGB_SUPPORTED)
619/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500620void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600621png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600622{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600623#ifdef PNG_USE_LOCAL_ARRAYS
624 PNG_sRGB;
625#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600626 png_byte buf[1];
627
628 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600629 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600630 png_warning(png_ptr,
631 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600632 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600633 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600634}
635#endif
636
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600637#if defined(PNG_WRITE_iCCP_SUPPORTED)
638/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500639void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600640png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
641 png_charp profile, int profile_len)
642{
643#ifdef PNG_USE_LOCAL_ARRAYS
644 PNG_iCCP;
645#endif
646 png_size_t name_len;
647 png_charp new_name;
648 compression_state comp;
649
650 png_debug(1, "in png_write_iCCP\n");
651 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
652 &new_name)) == 0)
653 {
654 png_warning(png_ptr, "Empty keyword in iCCP chunk");
655 return;
656 }
657
658 if (compression_type)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600659 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600660
661 if (profile == NULL || *profile == '\0')
662 profile_len = 0;
663
664 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500665 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
666 PNG_TEXT_COMPRESSION_zTXt, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600667
668 /* make sure we include the NULL after the name and the compression type */
669 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
670 (png_uint_32)name_len+profile_len+2);
671 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
672
673 if (profile_len)
674 png_write_compressed_data_out(png_ptr, &comp);
675
676 png_write_chunk_end(png_ptr);
677 png_free(png_ptr, new_name);
678}
679#endif
680
681#if defined(PNG_WRITE_sPLT_SUPPORTED)
682/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500683void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600684png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600685{
686#ifdef PNG_USE_LOCAL_ARRAYS
687 PNG_sPLT;
688#endif
689 png_size_t name_len;
690 png_charp new_name;
691 png_byte entrybuf[10];
692 int entry_size = (spalette->depth == 8 ? 6 : 10);
693 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600694 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500695#ifdef PNG_NO_POINTER_INDEXING
696 int i;
697#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600698
699 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600700 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
701 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600702 {
703 png_warning(png_ptr, "Empty keyword in sPLT chunk");
704 return;
705 }
706
707 /* make sure we include the NULL after the name */
708 png_write_chunk_start(png_ptr, (png_bytep) png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600709 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600710 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600711 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600712
713 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500714#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600715 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
716 {
717 if (spalette->depth == 8)
718 {
719 entrybuf[0] = (png_byte)ep->red;
720 entrybuf[1] = (png_byte)ep->green;
721 entrybuf[2] = (png_byte)ep->blue;
722 entrybuf[3] = (png_byte)ep->alpha;
723 png_save_uint_16(entrybuf + 4, ep->frequency);
724 }
725 else
726 {
727 png_save_uint_16(entrybuf + 0, ep->red);
728 png_save_uint_16(entrybuf + 2, ep->green);
729 png_save_uint_16(entrybuf + 4, ep->blue);
730 png_save_uint_16(entrybuf + 6, ep->alpha);
731 png_save_uint_16(entrybuf + 8, ep->frequency);
732 }
733 png_write_chunk_data(png_ptr, entrybuf, entry_size);
734 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500735#else
736 ep=spalette->entries;
737 for (i=0; i>spalette->nentries; i++)
738 {
739 if (spalette->depth == 8)
740 {
741 entrybuf[0] = (png_byte)ep[i].red;
742 entrybuf[1] = (png_byte)ep[i].green;
743 entrybuf[2] = (png_byte)ep[i].blue;
744 entrybuf[3] = (png_byte)ep[i].alpha;
745 png_save_uint_16(entrybuf + 4, ep[i].frequency);
746 }
747 else
748 {
749 png_save_uint_16(entrybuf + 0, ep[i].red);
750 png_save_uint_16(entrybuf + 2, ep[i].green);
751 png_save_uint_16(entrybuf + 4, ep[i].blue);
752 png_save_uint_16(entrybuf + 6, ep[i].alpha);
753 png_save_uint_16(entrybuf + 8, ep[i].frequency);
754 }
755 png_write_chunk_data(png_ptr, entrybuf, entry_size);
756 }
757#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600758
759 png_write_chunk_end(png_ptr);
760 png_free(png_ptr, new_name);
761}
762#endif
763
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500764#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500765/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500766void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600767png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500768{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600769#ifdef PNG_USE_LOCAL_ARRAYS
770 PNG_sBIT;
771#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500772 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500773 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500774
Andreas Dilger47a0c421997-05-16 02:46:07 -0500775 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600776 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500777 if (color_type & PNG_COLOR_MASK_COLOR)
778 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600779 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500780
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500781 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
782 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600783 if (sbit->red == 0 || sbit->red > maxbits ||
784 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500785 sbit->blue == 0 || sbit->blue > maxbits)
786 {
787 png_warning(png_ptr, "Invalid sBIT depth specified");
788 return;
789 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500790 buf[0] = sbit->red;
791 buf[1] = sbit->green;
792 buf[2] = sbit->blue;
793 size = 3;
794 }
795 else
796 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500797 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
798 {
799 png_warning(png_ptr, "Invalid sBIT depth specified");
800 return;
801 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500802 buf[0] = sbit->gray;
803 size = 1;
804 }
805
806 if (color_type & PNG_COLOR_MASK_ALPHA)
807 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500808 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
809 {
810 png_warning(png_ptr, "Invalid sBIT depth specified");
811 return;
812 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500813 buf[size++] = sbit->alpha;
814 }
815
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600816 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500817}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500818#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500819
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500820#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500821/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600822#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500823void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500824png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 double red_x, double red_y, double green_x, double green_y,
826 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500827{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600828#ifdef PNG_USE_LOCAL_ARRAYS
829 PNG_cHRM;
830#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500831 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600832 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500833
Andreas Dilger47a0c421997-05-16 02:46:07 -0500834 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600835 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500836 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
837 white_x + white_y > 1.0)
838 {
839 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600840#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600841 printf("white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600842#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500843 return;
844 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600845 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500846 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600847 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500848 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500849
850 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
851 red_x + red_y > 1.0)
852 {
853 png_warning(png_ptr, "Invalid cHRM red point specified");
854 return;
855 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600856 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500857 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600858 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500859 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500860
861 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
862 green_x + green_y > 1.0)
863 {
864 png_warning(png_ptr, "Invalid cHRM green point specified");
865 return;
866 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600867 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500868 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600869 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500870 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500871
872 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
873 blue_x + blue_y > 1.0)
874 {
875 png_warning(png_ptr, "Invalid cHRM blue point specified");
876 return;
877 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600878 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500879 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600880 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500881 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500882
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600883 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500884}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500885#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600886#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500887void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600888png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
889 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
890 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
891 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600892{
893#ifdef PNG_USE_LOCAL_ARRAYS
894 PNG_cHRM;
895#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600896 png_byte buf[32];
897
898 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600899 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600900 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
901 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600902 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600903#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600904 printf("white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600905#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600906 return;
907 }
908 png_save_uint_32(buf, white_x);
909 png_save_uint_32(buf + 4, white_y);
910
911 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
912 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600913 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600914 return;
915 }
916 png_save_uint_32(buf + 8, red_x);
917 png_save_uint_32(buf + 12, red_y);
918
919 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
920 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600921 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600922 return;
923 }
924 png_save_uint_32(buf + 16, green_x);
925 png_save_uint_32(buf + 20, green_y);
926
927 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
928 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600929 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600930 return;
931 }
932 png_save_uint_32(buf + 24, blue_x);
933 png_save_uint_32(buf + 28, blue_y);
934
935 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
936}
937#endif
938#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500939
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500940#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500941/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500942void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600943png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500944 int num_trans, int color_type)
945{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600946#ifdef PNG_USE_LOCAL_ARRAYS
947 PNG_tRNS;
948#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500949 png_byte buf[6];
950
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500952 if (color_type == PNG_COLOR_TYPE_PALETTE)
953 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600954 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500955 {
956 png_warning(png_ptr,"Invalid number of transparent colors specified");
957 return;
958 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500959 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600960 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500961 }
962 else if (color_type == PNG_COLOR_TYPE_GRAY)
963 {
964 /* one 16 bit value */
965 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600966 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500967 }
968 else if (color_type == PNG_COLOR_TYPE_RGB)
969 {
970 /* three 16 bit values */
971 png_save_uint_16(buf, tran->red);
972 png_save_uint_16(buf + 2, tran->green);
973 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600974 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500975 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500976 else
977 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600978 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500979 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500980}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500981#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500982
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500983#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500984/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500985void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600986png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500987{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600988#ifdef PNG_USE_LOCAL_ARRAYS
989 PNG_bKGD;
990#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500991 png_byte buf[6];
992
Andreas Dilger47a0c421997-05-16 02:46:07 -0500993 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500994 if (color_type == PNG_COLOR_TYPE_PALETTE)
995 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500996 if (
997#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
998 (!png_ptr->empty_plte_permitted ||
999 (png_ptr->empty_plte_permitted && png_ptr->num_palette)) &&
1000#endif
1001 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001002 {
1003 png_warning(png_ptr, "Invalid background palette index");
1004 return;
1005 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001006 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001007 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001008 }
1009 else if (color_type & PNG_COLOR_MASK_COLOR)
1010 {
1011 png_save_uint_16(buf, back->red);
1012 png_save_uint_16(buf + 2, back->green);
1013 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001014 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001015 }
1016 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001017 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001018 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001019 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001020 }
1021}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001022#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001023
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001024#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001025/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001026void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001027png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001028{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001029#ifdef PNG_USE_LOCAL_ARRAYS
1030 PNG_hIST;
1031#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001032 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001033 png_byte buf[3];
1034
Andreas Dilger47a0c421997-05-16 02:46:07 -05001035 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001036 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1039 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001040 png_warning(png_ptr, "Invalid number of histogram entries specified");
1041 return;
1042 }
1043
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001044 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001045 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001046 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001047 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001049 }
1050 png_write_chunk_end(png_ptr);
1051}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001052#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001053
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001054#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1055 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001056/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1057 * and if invalid, correct the keyword rather than discarding the entire
1058 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1059 * length, forbids leading or trailing whitespace, multiple internal spaces,
1060 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 *
1062 * The new_key is allocated to hold the corrected keyword and must be freed
1063 * by the calling routine. This avoids problems with trying to write to
1064 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001065 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001066png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001067png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001068{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001069 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001070 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001071 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001072
Andreas Dilger47a0c421997-05-16 02:46:07 -05001073 png_debug(1, "in png_check_keyword\n");
1074 *new_key = NULL;
1075
1076 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001077 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001078 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001079 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001080 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001081
Andreas Dilger47a0c421997-05-16 02:46:07 -05001082 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1083
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001084 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001085
1086 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001087 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001088 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001089 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001091#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001093
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001094 sprintf(msg, "invalid keyword character 0x%02X", *kp);
1095 png_chunk_warning(png_ptr, msg);
1096#else
1097 png_chunk_warning(png_ptr, "invalid character in keyword");
1098#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001099 *dp = ' ';
1100 }
1101 else
1102 {
1103 *dp = *kp;
1104 }
1105 }
1106 *dp = '\0';
1107
1108 /* Remove any trailing white space. */
1109 kp = *new_key + key_len - 1;
1110 if (*kp == ' ')
1111 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001112 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001113
1114 while (*kp == ' ')
1115 {
1116 *(kp--) = '\0';
1117 key_len--;
1118 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001119 }
1120
1121 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 kp = *new_key;
1123 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001124 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001125 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126
1127 while (*kp == ' ')
1128 {
1129 kp++;
1130 key_len--;
1131 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001132 }
1133
Andreas Dilger47a0c421997-05-16 02:46:07 -05001134 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001135
Andreas Dilger47a0c421997-05-16 02:46:07 -05001136 /* Remove multiple internal spaces. */
1137 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001139 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001140 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141 *(dp++) = *kp;
1142 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001143 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001145 {
1146 key_len--;
1147 }
1148 else
1149 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 *(dp++) = *kp;
1151 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001152 }
1153 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001155
1156 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001158 png_free(png_ptr, *new_key);
1159 *new_key=NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001160 png_chunk_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 }
1162
1163 if (key_len > 79)
1164 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001165 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 new_key[79] = '\0';
1167 key_len = 79;
1168 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001169
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001170 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001171}
1172#endif
1173
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001174#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001175/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001176void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001177png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001178 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001179{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001180#ifdef PNG_USE_LOCAL_ARRAYS
1181 PNG_tEXt;
1182#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001183 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001184 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001185
Andreas Dilger47a0c421997-05-16 02:46:07 -05001186 png_debug(1, "in png_write_tEXt\n");
1187 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1188 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001189 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001190 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001191 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001192
Andreas Dilger47a0c421997-05-16 02:46:07 -05001193 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001194 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001195 else
1196 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001197
1198 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001199 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 -05001200 /*
1201 * We leave it to the application to meet PNG-1.0 requirements on the
1202 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1203 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001204 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001205 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001206 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001208 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001209
Guy Schalnat0d580581995-07-20 02:43:20 -05001210 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001212}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001213#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001214
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001215#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001216/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001217void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001218png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001219 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001220{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001221#ifdef PNG_USE_LOCAL_ARRAYS
1222 PNG_zTXt;
1223#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001225 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001226 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001227 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001228
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001230
Andreas Dilger47a0c421997-05-16 02:46:07 -05001231 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001232 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001233 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001235 }
1236
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1238 {
1239 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1240 png_free(png_ptr, new_key);
1241 return;
1242 }
1243
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001244 text_len = png_strlen(text);
1245
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 png_free(png_ptr, new_key);
1247
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001248 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001249 text_len = png_text_compress(png_ptr, text, text_len, compression,
1250 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001251
1252 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001253 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1254 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001255 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001257 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001258 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001260 /* write the compressed data */
1261 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001262
Guy Schalnat0d580581995-07-20 02:43:20 -05001263 /* close the chunk */
1264 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001265}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001266#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001267
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001268#if defined(PNG_WRITE_iTXt_SUPPORTED)
1269/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001270void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001271png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001272 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001273{
1274#ifdef PNG_USE_LOCAL_ARRAYS
1275 PNG_iTXt;
1276#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001277 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001278 png_charp new_lang, new_key;
1279 png_byte cbuf[2];
1280 compression_state comp;
1281
1282 png_debug(1, "in png_write_iTXt\n");
1283
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001284 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1285 {
1286 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1287 return;
1288 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001289 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1290 &new_lang))==0)
1291 {
1292 png_warning(png_ptr, "Empty language field in iTXt chunk");
1293 return;
1294 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001295 lang_key_len = png_strlen(lang_key);
1296 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001297
1298 if (text == NULL || *text == '\0')
1299 text_len = 0;
1300
1301 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001302 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1303 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001304
1305 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001306 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001307
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001308 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1309 (png_uint_32)(
1310 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1311 + key_len
1312 + lang_len
1313 + lang_key_len
1314 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001315
1316 /*
1317 * We leave it to the application to meet PNG-1.0 requirements on the
1318 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1319 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1320 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1321 */
1322 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001323
1324 /* set the compression flag */
1325 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1326 compression == PNG_TEXT_COMPRESSION_NONE)
1327 cbuf[0] = 0;
1328 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1329 cbuf[0] = 1;
1330 /* set the compression method */
1331 cbuf[1] = 0;
1332 png_write_chunk_data(png_ptr, cbuf, 2);
1333
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001334 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001335 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001336 png_write_chunk_data(png_ptr, '\0', 1);
1337
1338 png_write_compressed_data_out(png_ptr, &comp);
1339
1340 png_write_chunk_end(png_ptr);
1341 png_free(png_ptr, new_key);
1342 png_free(png_ptr, new_lang);
1343}
1344#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001345
1346#if defined(PNG_WRITE_oFFs_SUPPORTED)
1347/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001348void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001349png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1350 png_uint_32 y_offset,
1351 int unit_type)
1352{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001353#ifdef PNG_USE_LOCAL_ARRAYS
1354 PNG_oFFs;
1355#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001356 png_byte buf[9];
1357
1358 png_debug(1, "in png_write_oFFs\n");
1359 if (unit_type >= PNG_OFFSET_LAST)
1360 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1361
1362 png_save_uint_32(buf, x_offset);
1363 png_save_uint_32(buf + 4, y_offset);
1364 buf[8] = (png_byte)unit_type;
1365
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001366 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001367}
1368#endif
1369
1370#if defined(PNG_WRITE_pCAL_SUPPORTED)
1371/* write the pCAL chunk (png-scivis-19970203) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001372void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001373png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1374 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1375{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001376#ifdef PNG_USE_LOCAL_ARRAYS
1377 PNG_pCAL;
1378#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001379 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001380 png_uint_32p params_len;
1381 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001382 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383 int i;
1384
1385 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1386 if (type >= PNG_EQUATION_LAST)
1387 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1388
1389 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1390 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1391 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1392 png_debug1(3, "pCAL units length = %d\n", units_len);
1393 total_len = purpose_len + units_len + 10;
1394
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001395 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1396 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001397
1398 /* Find the length of each parameter, making sure we don't count the
1399 null terminator for the last parameter. */
1400 for (i = 0; i < nparams; i++)
1401 {
1402 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1403 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
1404 total_len += (png_size_t)params_len[i];
1405 }
1406
1407 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001408 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001409 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001410 png_save_int_32(buf, X0);
1411 png_save_int_32(buf + 4, X1);
1412 buf[8] = (png_byte)type;
1413 buf[9] = (png_byte)nparams;
1414 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1415 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1416
1417 png_free(png_ptr, new_purpose);
1418
1419 for (i = 0; i < nparams; i++)
1420 {
1421 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1422 (png_size_t)params_len[i]);
1423 }
1424
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001425 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001426 png_write_chunk_end(png_ptr);
1427}
1428#endif
1429
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001430#if defined(PNG_WRITE_sCAL_SUPPORTED)
1431/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001432#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001433void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001434png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001435{
1436#ifdef PNG_USE_LOCAL_ARRAYS
1437 PNG_sCAL;
1438#endif
1439 png_size_t total_len;
1440 char wbuf[32], hbuf[32];
1441
1442 png_debug(1, "in png_write_sCAL\n");
1443
1444 sprintf(wbuf, "%12.12e", width);
1445 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001446 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001447
1448 png_debug1(3, "sCAL total length = %d\n", total_len);
1449 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001450 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001451 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1452 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1453
1454 png_write_chunk_end(png_ptr);
1455}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001456#else
1457#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001458void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001459png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001460 png_charp height)
1461{
1462#ifdef PNG_USE_LOCAL_ARRAYS
1463 PNG_sCAL;
1464#endif
1465 png_size_t total_len;
1466 char wbuf[32], hbuf[32];
1467
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001468 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001469
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001470 strcpy(wbuf,(const char *)width);
1471 strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001472 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001473
1474 png_debug1(3, "sCAL total length = %d\n", total_len);
1475 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001476 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001477 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1478 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1479
1480 png_write_chunk_end(png_ptr);
1481}
1482#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001483#endif
1484#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001485
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001486#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001487/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001488void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001489png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001490 png_uint_32 y_pixels_per_unit,
1491 int unit_type)
1492{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001493#ifdef PNG_USE_LOCAL_ARRAYS
1494 PNG_pHYs;
1495#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001496 png_byte buf[9];
1497
Andreas Dilger47a0c421997-05-16 02:46:07 -05001498 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001499 if (unit_type >= PNG_RESOLUTION_LAST)
1500 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1501
Guy Schalnat0d580581995-07-20 02:43:20 -05001502 png_save_uint_32(buf, x_pixels_per_unit);
1503 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001504 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001505
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001506 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001507}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001508#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001509
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001510#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001511/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1512 * or png_convert_from_time_t(), or fill in the structure yourself.
1513 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001514void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001515png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001516{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001517#ifdef PNG_USE_LOCAL_ARRAYS
1518 PNG_tIME;
1519#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001520 png_byte buf[7];
1521
Andreas Dilger47a0c421997-05-16 02:46:07 -05001522 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001523 if (mod_time->month > 12 || mod_time->month < 1 ||
1524 mod_time->day > 31 || mod_time->day < 1 ||
1525 mod_time->hour > 23 || mod_time->second > 60)
1526 {
1527 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1528 return;
1529 }
1530
Guy Schalnat0d580581995-07-20 02:43:20 -05001531 png_save_uint_16(buf, mod_time->year);
1532 buf[2] = mod_time->month;
1533 buf[3] = mod_time->day;
1534 buf[4] = mod_time->hour;
1535 buf[5] = mod_time->minute;
1536 buf[6] = mod_time->second;
1537
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001538 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001539}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001540#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001541
1542/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001543void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001544png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001545{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001546#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001547 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001548
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001549 /* start of interlace block */
1550 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001551
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001552 /* offset to next interlace block */
1553 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001554
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001555 /* start of interlace block in the y direction */
1556 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001557
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001558 /* offset to next interlace block in the y direction */
1559 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001560#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001561
Andreas Dilger47a0c421997-05-16 02:46:07 -05001562 png_size_t buf_size;
1563
1564 png_debug(1, "in png_write_start_row\n");
1565 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1566 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1567
Guy Schalnat0d580581995-07-20 02:43:20 -05001568 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001569 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001570 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001571
1572 /* set up filtering buffer, if using this filter */
1573 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001574 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001575 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001576 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001577 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001578 }
1579
Andreas Dilger47a0c421997-05-16 02:46:07 -05001580 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001581 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1582 {
1583 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001584 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001585 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001586
1587 if (png_ptr->do_filter & PNG_FILTER_UP)
1588 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001589 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001590 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001591 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001592 }
1593
1594 if (png_ptr->do_filter & PNG_FILTER_AVG)
1595 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001596 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1597 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001598 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001599 }
1600
1601 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1602 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001603 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001604 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001605 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001606 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001607 }
1608
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001609#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001610 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001611 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001612 {
1613 if (!(png_ptr->transformations & PNG_INTERLACE))
1614 {
1615 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1616 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001617 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1618 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001619 }
1620 else
1621 {
1622 png_ptr->num_rows = png_ptr->height;
1623 png_ptr->usr_width = png_ptr->width;
1624 }
1625 }
1626 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001627#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001628 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001629 png_ptr->num_rows = png_ptr->height;
1630 png_ptr->usr_width = png_ptr->width;
1631 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001632 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1633 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001634}
1635
Andreas Dilger47a0c421997-05-16 02:46:07 -05001636/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001637void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001638png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001639{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001640#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001641 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001642
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001643 /* start of interlace block */
1644 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001645
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001646 /* offset to next interlace block */
1647 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001648
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001649 /* start of interlace block in the y direction */
1650 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001652 /* offset to next interlace block in the y direction */
1653 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001654#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001655
Guy Schalnat0d580581995-07-20 02:43:20 -05001656 int ret;
1657
Andreas Dilger47a0c421997-05-16 02:46:07 -05001658 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001659 /* next row */
1660 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001661
Guy Schalnat0d580581995-07-20 02:43:20 -05001662 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001663 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001664 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001665
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001666#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001667 /* if interlaced, go to next pass */
1668 if (png_ptr->interlaced)
1669 {
1670 png_ptr->row_number = 0;
1671 if (png_ptr->transformations & PNG_INTERLACE)
1672 {
1673 png_ptr->pass++;
1674 }
1675 else
1676 {
1677 /* loop until we find a non-zero width or height pass */
1678 do
1679 {
1680 png_ptr->pass++;
1681 if (png_ptr->pass >= 7)
1682 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001683 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001684 png_pass_inc[png_ptr->pass] - 1 -
1685 png_pass_start[png_ptr->pass]) /
1686 png_pass_inc[png_ptr->pass];
1687 png_ptr->num_rows = (png_ptr->height +
1688 png_pass_yinc[png_ptr->pass] - 1 -
1689 png_pass_ystart[png_ptr->pass]) /
1690 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001691 if (png_ptr->transformations & PNG_INTERLACE)
1692 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001693 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1694
1695 }
1696
Guy Schalnate5a37791996-06-05 15:50:50 -05001697 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001698 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001699 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001700 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001701 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001702 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001703 (png_uint_32)png_ptr->usr_bit_depth *
1704 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001705 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001706 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001707 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001708#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001709
1710 /* if we get here, we've just written the last row, so we need
1711 to flush the compressor */
1712 do
1713 {
1714 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001715 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001716 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001717 if (ret == Z_OK)
1718 {
1719 /* check to see if we need more room */
1720 if (!(png_ptr->zstream.avail_out))
1721 {
1722 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1723 png_ptr->zstream.next_out = png_ptr->zbuf;
1724 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1725 }
1726 }
1727 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001728 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001729 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001730 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001731 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001732 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001733 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001734 } while (ret != Z_STREAM_END);
1735
1736 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001737 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001738 {
1739 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001740 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001741 }
1742
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001743 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001744}
1745
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001746#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001747/* Pick out the correct pixels for the interlace pass.
1748 * The basic idea here is to go through the row with a source
1749 * pointer and a destination pointer (sp and dp), and copy the
1750 * correct pixels for the pass. As the row gets compacted,
1751 * sp will always be >= dp, so we should never overwrite anything.
1752 * See the default: case for the easiest code to understand.
1753 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001754void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001755png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001756{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001757#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001758 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001759
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001760 /* start of interlace block */
1761 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001762
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001763 /* offset to next interlace block */
1764 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001765#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001766
Andreas Dilger47a0c421997-05-16 02:46:07 -05001767 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001768 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001769#if defined(PNG_USELESS_TESTS_SUPPORTED)
1770 if (row != NULL && row_info != NULL && pass < 6)
1771#else
1772 if (pass < 6)
1773#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001774 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001775 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001776 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001777 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001778 case 1:
1779 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001780 png_bytep sp;
1781 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001782 int shift;
1783 int d;
1784 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001785 png_uint_32 i;
1786 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001787
1788 dp = row;
1789 d = 0;
1790 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001791 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001792 i += png_pass_inc[pass])
1793 {
1794 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001795 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001796 d |= (value << shift);
1797
1798 if (shift == 0)
1799 {
1800 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001801 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001802 d = 0;
1803 }
1804 else
1805 shift--;
1806
1807 }
1808 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001809 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001810 break;
1811 }
1812 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001813 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001814 png_bytep sp;
1815 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 int shift;
1817 int d;
1818 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001819 png_uint_32 i;
1820 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001821
1822 dp = row;
1823 shift = 6;
1824 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001825 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001826 i += png_pass_inc[pass])
1827 {
1828 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001829 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001830 d |= (value << shift);
1831
1832 if (shift == 0)
1833 {
1834 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001835 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001836 d = 0;
1837 }
1838 else
1839 shift -= 2;
1840 }
1841 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001842 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001843 break;
1844 }
1845 case 4:
1846 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001847 png_bytep sp;
1848 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001849 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001850 int d;
1851 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001852 png_uint_32 i;
1853 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001854
1855 dp = row;
1856 shift = 4;
1857 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001858 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001859 i += png_pass_inc[pass])
1860 {
1861 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001862 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001863 d |= (value << shift);
1864
1865 if (shift == 0)
1866 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001867 shift = 4;
1868 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001869 d = 0;
1870 }
1871 else
1872 shift -= 4;
1873 }
1874 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001875 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001876 break;
1877 }
1878 default:
1879 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001880 png_bytep sp;
1881 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001882 png_uint_32 i;
1883 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001884 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001885
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001886 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001887 dp = row;
1888 /* find out how many bytes each pixel takes up */
1889 pixel_bytes = (row_info->pixel_depth >> 3);
1890 /* loop through the row, only looking at the pixels that
1891 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001892 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001893 i += png_pass_inc[pass])
1894 {
1895 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001896 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001897 /* move the pixel */
1898 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001899 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001900 /* next pixel */
1901 dp += pixel_bytes;
1902 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001903 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001904 }
1905 }
1906 /* set new row width */
1907 row_info->width = (row_info->width +
1908 png_pass_inc[pass] - 1 -
1909 png_pass_start[pass]) /
1910 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001911 row_info->rowbytes = ((row_info->width *
1912 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001913 }
1914}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001915#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001916
Andreas Dilger47a0c421997-05-16 02:46:07 -05001917/* This filters the row, chooses which filter to use, if it has not already
1918 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001919 * chosen filter.
1920 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001921#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001922#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001923#define PNG_LOMASK ((png_uint_32)0xffffL)
1924#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001925void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001926png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001927{
Guy Schalnate5a37791996-06-05 15:50:50 -05001928 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001929 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001930 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001931 png_uint_32 row_bytes = row_info->rowbytes;
1932#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1933 int num_p_filters = (int)png_ptr->num_prev_filters;
1934#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001935
Andreas Dilger47a0c421997-05-16 02:46:07 -05001936 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001937 /* find out how many bytes offset each pixel is */
1938 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001939
1940 prev_row = png_ptr->prev_row;
1941 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001942 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001943
Andreas Dilger47a0c421997-05-16 02:46:07 -05001944 /* The prediction method we use is to find which method provides the
1945 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001946 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001947 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001948 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001949 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001950 * predictive" method (not implemented yet), which does test compressions
1951 * of lines using different filter methods, and then chooses the
1952 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001953 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001954 *
1955 * GRR 980525: consider also
1956 * (1) minimum sum of absolute differences from running average (i.e.,
1957 * keep running sum of non-absolute differences & count of bytes)
1958 * [track dispersion, too? restart average if dispersion too large?]
1959 * (1b) minimum sum of absolute differences from sliding average, probably
1960 * with window size <= deflate window (usually 32K)
1961 * (2) minimum sum of squared differences from zero or running average
1962 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001963 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001964
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001965
Guy Schalnate5a37791996-06-05 15:50:50 -05001966 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001967 * that has been chosen, as it doesn't actually do anything to the data.
1968 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001969 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001970 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001971 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001972 png_bytep rp;
1973 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001974 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001975 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001976
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001977 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001978 {
1979 v = *rp;
1980 sum += (v < 128) ? v : 256 - v;
1981 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001982
1983#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1984 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1985 {
1986 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001987 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001988 sumlo = sum & PNG_LOMASK;
1989 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1990
1991 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001992 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001993 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001994 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001995 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001996 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001997 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001998 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001999 PNG_WEIGHT_SHIFT;
2000 }
2001 }
2002
2003 /* Factor in the cost of this filter (this is here for completeness,
2004 * but it makes no sense to have a "cost" for the NONE filter, as
2005 * it has the minimum possible computational cost - none).
2006 */
2007 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2008 PNG_COST_SHIFT;
2009 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2010 PNG_COST_SHIFT;
2011
2012 if (sumhi > PNG_HIMASK)
2013 sum = PNG_MAXSUM;
2014 else
2015 sum = (sumhi << PNG_HISHIFT) + sumlo;
2016 }
2017#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002018 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002019 }
2020
Guy Schalnate5a37791996-06-05 15:50:50 -05002021 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002022 if (filter_to_do == PNG_FILTER_SUB)
2023 /* it's the only filter so no testing is needed */
2024 {
2025 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002026 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002027 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2028 i++, rp++, dp++)
2029 {
2030 *dp = *rp;
2031 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002032 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002033 i++, rp++, lp++, dp++)
2034 {
2035 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2036 }
2037 best_row = png_ptr->sub_row;
2038 }
2039
2040 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002041 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002042 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002043 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002044 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002045 int v;
2046
2047#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002048 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002049 * would reduce the sum of this filter, so that we can do the
2050 * early exit comparison without scaling the sum each time.
2051 */
2052 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2053 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002054 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002055 png_uint_32 lmhi, lmlo;
2056 lmlo = lmins & PNG_LOMASK;
2057 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2058
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002059 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002060 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002061 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002062 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002063 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002064 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002065 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002066 PNG_WEIGHT_SHIFT;
2067 }
2068 }
2069
2070 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2071 PNG_COST_SHIFT;
2072 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2073 PNG_COST_SHIFT;
2074
2075 if (lmhi > PNG_HIMASK)
2076 lmins = PNG_MAXSUM;
2077 else
2078 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2079 }
2080#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002081
Guy Schalnate5a37791996-06-05 15:50:50 -05002082 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2083 i++, rp++, dp++)
2084 {
2085 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002086
Guy Schalnate5a37791996-06-05 15:50:50 -05002087 sum += (v < 128) ? v : 256 - v;
2088 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002089 for (lp = row_buf + 1; i < row_info->rowbytes;
2090 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002091 {
2092 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002093
Guy Schalnate5a37791996-06-05 15:50:50 -05002094 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095
2096 if (sum > lmins) /* We are already worse, don't continue. */
2097 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002098 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002099
2100#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2101 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2102 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002103 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002104 png_uint_32 sumhi, sumlo;
2105 sumlo = sum & PNG_LOMASK;
2106 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2107
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002108 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002109 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002110 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002111 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002112 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002114 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002115 PNG_WEIGHT_SHIFT;
2116 }
2117 }
2118
2119 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2120 PNG_COST_SHIFT;
2121 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2122 PNG_COST_SHIFT;
2123
2124 if (sumhi > PNG_HIMASK)
2125 sum = PNG_MAXSUM;
2126 else
2127 sum = (sumhi << PNG_HISHIFT) + sumlo;
2128 }
2129#endif
2130
Guy Schalnate5a37791996-06-05 15:50:50 -05002131 if (sum < mins)
2132 {
2133 mins = sum;
2134 best_row = png_ptr->sub_row;
2135 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002136 }
2137
Guy Schalnate5a37791996-06-05 15:50:50 -05002138 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002139 if (filter_to_do == PNG_FILTER_UP)
2140 {
2141 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002142 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002143
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002144 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002145 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002146 i++, rp++, pp++, dp++)
2147 {
2148 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2149 }
2150 best_row = png_ptr->up_row;
2151 }
2152
2153 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002154 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002155 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002156 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002157 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002158 int v;
2159
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002160
Andreas Dilger47a0c421997-05-16 02:46:07 -05002161#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2162 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2163 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002164 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002165 png_uint_32 lmhi, lmlo;
2166 lmlo = lmins & PNG_LOMASK;
2167 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2168
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002169 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002170 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002171 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002172 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002173 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002174 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002175 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002176 PNG_WEIGHT_SHIFT;
2177 }
2178 }
2179
2180 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2181 PNG_COST_SHIFT;
2182 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2183 PNG_COST_SHIFT;
2184
2185 if (lmhi > PNG_HIMASK)
2186 lmins = PNG_MAXSUM;
2187 else
2188 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2189 }
2190#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002191
2192 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002193 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002194 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002196
2197 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198
2199 if (sum > lmins) /* We are already worse, don't continue. */
2200 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002201 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002202
2203#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2204 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2205 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002206 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002207 png_uint_32 sumhi, sumlo;
2208 sumlo = sum & PNG_LOMASK;
2209 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2210
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002211 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002212 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002213 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002214 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002215 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002216 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002217 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002218 PNG_WEIGHT_SHIFT;
2219 }
2220 }
2221
2222 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2223 PNG_COST_SHIFT;
2224 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2225 PNG_COST_SHIFT;
2226
2227 if (sumhi > PNG_HIMASK)
2228 sum = PNG_MAXSUM;
2229 else
2230 sum = (sumhi << PNG_HISHIFT) + sumlo;
2231 }
2232#endif
2233
Guy Schalnate5a37791996-06-05 15:50:50 -05002234 if (sum < mins)
2235 {
2236 mins = sum;
2237 best_row = png_ptr->up_row;
2238 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002239 }
2240
Guy Schalnate5a37791996-06-05 15:50:50 -05002241 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002242 if (filter_to_do == PNG_FILTER_AVG)
2243 {
2244 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002245 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002246 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002247 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002248 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002249 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002250 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002251 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002252 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002253 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2254 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002255 }
2256 best_row = png_ptr->avg_row;
2257 }
2258
2259 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002260 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002261 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 int v;
2265
2266#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2267 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2268 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002269 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002270 png_uint_32 lmhi, lmlo;
2271 lmlo = lmins & PNG_LOMASK;
2272 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2273
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002274 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002275 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002276 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002277 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002278 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002279 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002280 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002281 PNG_WEIGHT_SHIFT;
2282 }
2283 }
2284
2285 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2286 PNG_COST_SHIFT;
2287 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2288 PNG_COST_SHIFT;
2289
2290 if (lmhi > PNG_HIMASK)
2291 lmins = PNG_MAXSUM;
2292 else
2293 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2294 }
2295#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002296
2297 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002298 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002299 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002300 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002301
2302 sum += (v < 128) ? v : 256 - v;
2303 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002304 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002305 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002306 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002307 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002308
2309 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310
2311 if (sum > lmins) /* We are already worse, don't continue. */
2312 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002313 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314
2315#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2316 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2317 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002318 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002319 png_uint_32 sumhi, sumlo;
2320 sumlo = sum & PNG_LOMASK;
2321 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2322
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002323 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002324 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002325 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002326 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002327 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002328 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002329 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002330 PNG_WEIGHT_SHIFT;
2331 }
2332 }
2333
2334 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2335 PNG_COST_SHIFT;
2336 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2337 PNG_COST_SHIFT;
2338
2339 if (sumhi > PNG_HIMASK)
2340 sum = PNG_MAXSUM;
2341 else
2342 sum = (sumhi << PNG_HISHIFT) + sumlo;
2343 }
2344#endif
2345
Guy Schalnate5a37791996-06-05 15:50:50 -05002346 if (sum < mins)
2347 {
2348 mins = sum;
2349 best_row = png_ptr->avg_row;
2350 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002351 }
2352
Andreas Dilger47a0c421997-05-16 02:46:07 -05002353 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002354 if (filter_to_do == PNG_FILTER_PAETH)
2355 {
2356 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002357 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002358 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002359 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002360 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002361 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002362 }
2363
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002364 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002365 {
2366 int a, b, c, pa, pb, pc, p;
2367
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002368 b = *pp++;
2369 c = *cp++;
2370 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002371
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002372 p = b - c;
2373 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002374
2375#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002376 pa = abs(p);
2377 pb = abs(pc);
2378 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002379#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002380 pa = p < 0 ? -p : p;
2381 pb = pc < 0 ? -pc : pc;
2382 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002383#endif
2384
2385 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2386
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002387 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002388 }
2389 best_row = png_ptr->paeth_row;
2390 }
2391
2392 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002393 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002394 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002395 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002396 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002397 int v;
2398
2399#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2400 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2401 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002402 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 png_uint_32 lmhi, lmlo;
2404 lmlo = lmins & PNG_LOMASK;
2405 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2406
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002407 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002408 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002409 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002410 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002411 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002412 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002413 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002414 PNG_WEIGHT_SHIFT;
2415 }
2416 }
2417
2418 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2419 PNG_COST_SHIFT;
2420 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2421 PNG_COST_SHIFT;
2422
2423 if (lmhi > PNG_HIMASK)
2424 lmins = PNG_MAXSUM;
2425 else
2426 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2427 }
2428#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002429
2430 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002431 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002432 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002433 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002434
2435 sum += (v < 128) ? v : 256 - v;
2436 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002437
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002438 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002439 {
2440 int a, b, c, pa, pb, pc, p;
2441
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002442 b = *pp++;
2443 c = *cp++;
2444 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002445
2446#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 p = b - c;
2448 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002449#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002450 pa = abs(p);
2451 pb = abs(pc);
2452 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002453#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002454 pa = p < 0 ? -p : p;
2455 pb = pc < 0 ? -pc : pc;
2456 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002457#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002458 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2459#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002460 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002461 pa = abs(p - a);
2462 pb = abs(p - b);
2463 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002464 if (pa <= pb && pa <= pc)
2465 p = a;
2466 else if (pb <= pc)
2467 p = b;
2468 else
2469 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002470#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002471
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002472 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002473
2474 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002475
2476 if (sum > lmins) /* We are already worse, don't continue. */
2477 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002478 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002479
2480#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2481 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2482 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002483 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002484 png_uint_32 sumhi, sumlo;
2485 sumlo = sum & PNG_LOMASK;
2486 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2487
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002488 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002489 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002490 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002491 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002492 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002493 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002494 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002495 PNG_WEIGHT_SHIFT;
2496 }
2497 }
2498
2499 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2500 PNG_COST_SHIFT;
2501 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2502 PNG_COST_SHIFT;
2503
2504 if (sumhi > PNG_HIMASK)
2505 sum = PNG_MAXSUM;
2506 else
2507 sum = (sumhi << PNG_HISHIFT) + sumlo;
2508 }
2509#endif
2510
Guy Schalnate5a37791996-06-05 15:50:50 -05002511 if (sum < mins)
2512 {
2513 best_row = png_ptr->paeth_row;
2514 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002515 }
2516
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002518
Guy Schalnate5a37791996-06-05 15:50:50 -05002519 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002520
2521#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2522 /* Save the type of filter we picked this time for future calculations */
2523 if (png_ptr->num_prev_filters > 0)
2524 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002525 int j;
2526 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002527 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002528 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002529 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002530 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002531 }
2532#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002533}
2534
2535
Andreas Dilger47a0c421997-05-16 02:46:07 -05002536/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002537void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002538png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2539{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002540 png_debug(1, "in png_write_filtered_row\n");
2541 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002542 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002543 png_ptr->zstream.next_in = filtered_row;
2544 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002545 /* repeat until we have compressed all the data */
2546 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002547 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002548 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002549
Guy Schalnate5a37791996-06-05 15:50:50 -05002550 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002551 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002552 /* check for compression errors */
2553 if (ret != Z_OK)
2554 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002555 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002556 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002557 else
2558 png_error(png_ptr, "zlib error");
2559 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002560
Guy Schalnate5a37791996-06-05 15:50:50 -05002561 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002562 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002563 {
2564 /* write the IDAT and reset the zlib output buffer */
2565 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002566 png_ptr->zstream.next_out = png_ptr->zbuf;
2567 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002568 }
2569 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002570 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002571
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002572 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002573 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002574 {
2575 png_bytep tptr;
2576
2577 tptr = png_ptr->prev_row;
2578 png_ptr->prev_row = png_ptr->row_buf;
2579 png_ptr->row_buf = tptr;
2580 }
2581
Guy Schalnate5a37791996-06-05 15:50:50 -05002582 /* finish row - updates counters and flushes zlib if last row */
2583 png_write_finish_row(png_ptr);
2584
2585#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2586 png_ptr->flush_rows++;
2587
2588 if (png_ptr->flush_dist > 0 &&
2589 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002590 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002591 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002592 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002593#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002594}