blob: 2ac3332429252368af90607ed45c9398a8e25e9d [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-Pehrson166c5a31999-12-10 09:43:02 -06004 * libpng 1.0.5h - December 10, 1999
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06008 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Guy Schalnat0d580581995-07-20 02:43:20 -050018void
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 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050032void
33png_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 */
Guy Schalnat0d580581995-07-20 02:43:20 -050046void
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 */
Guy Schalnat0d580581995-07-20 02:43:20 -050062void
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 */
Guy Schalnat0d580581995-07-20 02:43:20 -050075void
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 */
Guy Schalnat0d580581995-07-20 02:43:20 -050098void
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(). */
Guy Schalnat0d580581995-07-20 02:43:20 -0500110void
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 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500127void
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-Pehrson166c5a31999-12-10 09:43:02 -0600136#if defined(PNG_WRITE_TEXT_SUPPORTED)
137/*
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 */
154static int
155png_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;
170 return(text_len);
171 }
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
182 compression = PNG_TEXT_COMPRESSION_zTXt;
183 }
184
185 /* We can't write the chunk until we find out how much data we have,
186 * which means we need to run the compressor first and save the
187 * output. This shouldn't be a problem, as the vast majority of
188 * comments should be reasonable, but we will set up an array of
189 * malloc'd pointers to be sure.
190 *
191 * If we knew the application was well behaved, we could simplify this
192 * greatly by assuming we can always malloc an output buffer large
193 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
194 * and malloc this directly. The only time this would be a bad idea is
195 * if we can't malloc more than 64K and we have 64K of random input
196 * data, or if the input string is incredibly large (although this
197 * wouldn't cause a failure, just a slowdown due to swapping).
198 */
199
200 /* set up the compression buffers */
201 png_ptr->zstream.avail_in = (uInt)text_len;
202 png_ptr->zstream.next_in = (Bytef *)text;
203 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
204 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
205
206 /* this is the same compression loop as in png_write_row() */
207 do
208 {
209 /* compress the data */
210 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
211 if (ret != Z_OK)
212 {
213 /* error */
214 if (png_ptr->zstream.msg != NULL)
215 png_error(png_ptr, png_ptr->zstream.msg);
216 else
217 png_error(png_ptr, "zlib error");
218 }
219 /* check to see if we need more room */
220 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
221 {
222 /* make sure the output array has room */
223 if (comp->num_output_ptr >= comp->max_output_ptr)
224 {
225 int old_max;
226
227 old_max = comp->max_output_ptr;
228 comp->max_output_ptr = comp->num_output_ptr + 4;
229 if (comp->output_ptr != NULL)
230 {
231 png_charpp old_ptr;
232
233 old_ptr = comp->output_ptr;
234 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
235 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
236 png_memcpy(comp->output_ptr, old_ptr,
237 old_max * sizeof (png_charp));
238 png_free(png_ptr, old_ptr);
239 }
240 else
241 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
242 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
243 }
244
245 /* save the data */
246 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
247 (png_uint_32)png_ptr->zbuf_size);
248 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
249 png_ptr->zbuf_size);
250 comp->num_output_ptr++;
251
252 /* and reset the buffer */
253 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
254 png_ptr->zstream.next_out = png_ptr->zbuf;
255 }
256 /* continue until we don't have any more to compress */
257 } while (png_ptr->zstream.avail_in);
258
259 /* finish the compression */
260 do
261 {
262 /* tell zlib we are finished */
263 ret = deflate(&png_ptr->zstream, Z_FINISH);
264 if (ret != Z_OK && ret != Z_STREAM_END)
265 {
266 /* we got an error */
267 if (png_ptr->zstream.msg != NULL)
268 png_error(png_ptr, png_ptr->zstream.msg);
269 else
270 png_error(png_ptr, "zlib error");
271 }
272
273 /* check to see if we need more room */
274 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
275 {
276 /* check to make sure our output array has room */
277 if (comp->num_output_ptr >= comp->max_output_ptr)
278 {
279 int old_max;
280
281 old_max = comp->max_output_ptr;
282 comp->max_output_ptr = comp->num_output_ptr + 4;
283 if (comp->output_ptr != NULL)
284 {
285 png_charpp old_ptr;
286
287 old_ptr = comp->output_ptr;
288 /* This could be optimized to realloc() */
289 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
290 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
291 png_memcpy(comp->output_ptr, old_ptr,
292 old_max * sizeof (png_charp));
293 png_free(png_ptr, old_ptr);
294 }
295 else
296 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
297 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
298 }
299
300 /* save off the data */
301 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
302 (png_uint_32)png_ptr->zbuf_size);
303 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
304 png_ptr->zbuf_size);
305 comp->num_output_ptr++;
306
307 /* and reset the buffer pointers */
308 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
309 png_ptr->zstream.next_out = png_ptr->zbuf;
310 }
311 } while (ret != Z_STREAM_END);
312
313 /* text length is number of buffers plus last buffer */
314 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
315 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
316 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
317
318 return(text_len);
319}
320
321/* ship the compressed text out via chunk writes */
322static void
323png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
324{
325 int i;
326
327 /* handle the no-compression case */
328 if (comp->input)
329 {
330 png_write_chunk_data(png_ptr, (png_bytep)comp->input, comp->input_len);
331 return;
332 }
333
334 /* write saved output buffers, if any */
335 for (i = 0; i < comp->num_output_ptr; i++)
336 {
337 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],png_ptr->zbuf_size);
338 png_free(png_ptr, comp->output_ptr[i]);
339 }
340 if (comp->max_output_ptr != 0)
341 png_free(png_ptr, comp->output_ptr);
342 /* write anything left in zbuf */
343 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
344 png_write_chunk_data(png_ptr, png_ptr->zbuf,
345 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
346
347 /* reset zlib for another zTXt/iTXt or the image data */
348 deflateReset(&png_ptr->zstream);
349
350}
351#endif
352
Guy Schalnat0d580581995-07-20 02:43:20 -0500353/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600354 * information. Note that the rest of this code depends upon this
355 * information being correct.
356 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500357void
Guy Schalnat6d764711995-12-19 03:22:19 -0600358png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500359 int bit_depth, int color_type, int compression_type, int filter_type,
360 int interlace_type)
361{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600362#ifdef PNG_USE_LOCAL_ARRAYS
363 PNG_IHDR;
364#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500365 png_byte buf[13]; /* buffer to store the IHDR info */
366
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500368 /* Check that we have valid input data from the application info */
369 switch (color_type)
370 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500371 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500372 switch (bit_depth)
373 {
374 case 1:
375 case 2:
376 case 4:
377 case 8:
378 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500380 }
381 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500383 if (bit_depth != 8 && bit_depth != 16)
384 png_error(png_ptr, "Invalid bit depth for RGB image");
385 png_ptr->channels = 3;
386 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 switch (bit_depth)
389 {
390 case 1:
391 case 2:
392 case 4:
393 case 8: png_ptr->channels = 1; break;
394 default: png_error(png_ptr, "Invalid bit depth for paletted image");
395 }
396 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500398 if (bit_depth != 8 && bit_depth != 16)
399 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
400 png_ptr->channels = 2;
401 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500403 if (bit_depth != 8 && bit_depth != 16)
404 png_error(png_ptr, "Invalid bit depth for RGBA image");
405 png_ptr->channels = 4;
406 break;
407 default:
408 png_error(png_ptr, "Invalid image color type specified");
409 }
410
Andreas Dilger47a0c421997-05-16 02:46:07 -0500411 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500412 {
413 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500415 }
416
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 {
419 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500421 }
422
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600423#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 if (interlace_type != PNG_INTERLACE_NONE &&
425 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 {
427 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600430#else
431 interlace_type=PNG_INTERLACE_NONE;
432#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500433
434 /* save off the relevent information */
435 png_ptr->bit_depth = (png_byte)bit_depth;
436 png_ptr->color_type = (png_byte)color_type;
437 png_ptr->interlaced = (png_byte)interlace_type;
438 png_ptr->width = width;
439 png_ptr->height = height;
440
441 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500442 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500443 /* set the usr info, so any transformations can modify it */
444 png_ptr->usr_width = png_ptr->width;
445 png_ptr->usr_bit_depth = png_ptr->bit_depth;
446 png_ptr->usr_channels = png_ptr->channels;
447
Guy Schalnat0d580581995-07-20 02:43:20 -0500448 /* pack the header information into the buffer */
449 png_save_uint_32(buf, width);
450 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600451 buf[8] = (png_byte)bit_depth;
452 buf[9] = (png_byte)color_type;
453 buf[10] = (png_byte)compression_type;
454 buf[11] = (png_byte)filter_type;
455 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500456
457 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600458 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500459
Andreas Dilger47a0c421997-05-16 02:46:07 -0500460 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600461 png_ptr->zstream.zalloc = png_zalloc;
462 png_ptr->zstream.zfree = png_zfree;
463 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500464 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500465 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500466 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
467 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500468 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500469 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500470 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500471 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500472 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500473 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500474 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500475 png_ptr->zlib_strategy = Z_FILTERED;
476 else
477 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
478 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500480 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500481 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500482 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500483 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500484 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500485 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500486 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600487 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 png_ptr->zlib_method, png_ptr->zlib_window_bits,
489 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600490 png_ptr->zstream.next_out = png_ptr->zbuf;
491 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500492
Guy Schalnate5a37791996-06-05 15:50:50 -0500493 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500494}
495
496/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500497 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600498 * structure.
499 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500500void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500502{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600503#ifdef PNG_USE_LOCAL_ARRAYS
504 PNG_PLTE;
505#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500506 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600507 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500508 png_byte buf[3];
509
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500511 if ((
512#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
513 !png_ptr->empty_plte_permitted &&
514#endif
515 num_pal == 0) || num_pal > 256)
516 {
517 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
518 {
519 png_error(png_ptr, "Invalid number of colors in palette");
520 }
521 else
522 {
523 png_warning(png_ptr, "Invalid number of colors in palette");
524 return;
525 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500526 }
527
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 png_ptr->num_palette = (png_uint_16)num_pal;
529 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500530
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600531 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500532 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500533 {
534 buf[0] = pal_ptr->red;
535 buf[1] = pal_ptr->green;
536 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500537 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500538 }
539 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500540 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500541}
542
543/* write an IDAT chunk */
544void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500546{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600547#ifdef PNG_USE_LOCAL_ARRAYS
548 PNG_IDAT;
549#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600551 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500553}
554
555/* write an IEND chunk */
556void
Guy Schalnat6d764711995-12-19 03:22:19 -0600557png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500558{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600559#ifdef PNG_USE_LOCAL_ARRAYS
560 PNG_IEND;
561#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500562 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600563 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600564 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500565}
566
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500567#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500568/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600569#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500570void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500572{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600573#ifdef PNG_USE_LOCAL_ARRAYS
574 PNG_gAMA;
575#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 png_uint_32 igamma;
577 png_byte buf[4];
578
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -0600580 /* file_gamma is saved in 1/1000000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600581 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500582 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600583 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500584}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500585#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600586void
587#ifdef PNG_FIXED_POINT_SUPPORTED
588png_write_gAMA_fixed(png_structp png_ptr, png_uint_32 file_gamma)
589{
590#ifdef PNG_USE_LOCAL_ARRAYS
591 PNG_gAMA;
592#endif
593 png_byte buf[4];
594
595 png_debug(1, "in png_write_gAMA\n");
596 /* file_gamma is saved in 1/1000000ths */
597 png_save_uint_32(buf, file_gamma);
598 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
599}
600#endif
601#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500602
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600603#if defined(PNG_WRITE_sRGB_SUPPORTED)
604/* write a sRGB chunk */
605void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600606png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600607{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600608#ifdef PNG_USE_LOCAL_ARRAYS
609 PNG_sRGB;
610#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600611 png_byte buf[1];
612
613 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600614 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600615 png_warning(png_ptr,
616 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600617 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600618 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600619}
620#endif
621
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600622#if defined(PNG_WRITE_iCCP_SUPPORTED)
623/* write an iCCP chunk */
624void
625png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
626 png_charp profile, int profile_len)
627{
628#ifdef PNG_USE_LOCAL_ARRAYS
629 PNG_iCCP;
630#endif
631 png_size_t name_len;
632 png_charp new_name;
633 compression_state comp;
634
635 png_debug(1, "in png_write_iCCP\n");
636 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
637 &new_name)) == 0)
638 {
639 png_warning(png_ptr, "Empty keyword in iCCP chunk");
640 return;
641 }
642
643 if (compression_type)
644 /* ignore */ ;
645
646 if (profile == NULL || *profile == '\0')
647 profile_len = 0;
648
649 if (profile_len)
650 profile_len = png_text_compress(png_ptr, profile, profile_len,
651 PNG_TEXT_COMPRESSION_zTXt, &comp);
652
653 /* make sure we include the NULL after the name and the compression type */
654 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
655 (png_uint_32)name_len+profile_len+2);
656 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
657
658 if (profile_len)
659 png_write_compressed_data_out(png_ptr, &comp);
660
661 png_write_chunk_end(png_ptr);
662 png_free(png_ptr, new_name);
663}
664#endif
665
666#if defined(PNG_WRITE_sPLT_SUPPORTED)
667/* write a sPLT chunk */
668void
669png_write_sPLT(png_structp png_ptr, png_spalette_p spalette)
670{
671#ifdef PNG_USE_LOCAL_ARRAYS
672 PNG_sPLT;
673#endif
674 png_size_t name_len;
675 png_charp new_name;
676 png_byte entrybuf[10];
677 int entry_size = (spalette->depth == 8 ? 6 : 10);
678 int palette_size = entry_size * spalette->nentries;
679 png_spalette_entryp ep;
680
681 png_debug(1, "in png_write_sPLT\n");
682 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr, spalette->name, &new_name))==0)
683 {
684 png_warning(png_ptr, "Empty keyword in sPLT chunk");
685 return;
686 }
687
688 /* make sure we include the NULL after the name */
689 png_write_chunk_start(png_ptr, (png_bytep) png_sPLT,
690 (png_uint_32)(name_len + 1 + palette_size));
691 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
692
693 /* loop through each palette entry, writing appropriately */
694 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
695 {
696 if (spalette->depth == 8)
697 {
698 entrybuf[0] = (png_byte)ep->red;
699 entrybuf[1] = (png_byte)ep->green;
700 entrybuf[2] = (png_byte)ep->blue;
701 entrybuf[3] = (png_byte)ep->alpha;
702 png_save_uint_16(entrybuf + 4, ep->frequency);
703 }
704 else
705 {
706 png_save_uint_16(entrybuf + 0, ep->red);
707 png_save_uint_16(entrybuf + 2, ep->green);
708 png_save_uint_16(entrybuf + 4, ep->blue);
709 png_save_uint_16(entrybuf + 6, ep->alpha);
710 png_save_uint_16(entrybuf + 8, ep->frequency);
711 }
712 png_write_chunk_data(png_ptr, entrybuf, entry_size);
713 }
714
715 png_write_chunk_end(png_ptr);
716 png_free(png_ptr, new_name);
717}
718#endif
719
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500720#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500721/* write the sBIT chunk */
722void
Guy Schalnat6d764711995-12-19 03:22:19 -0600723png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500724{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600725#ifdef PNG_USE_LOCAL_ARRAYS
726 PNG_sBIT;
727#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500728 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500729 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500730
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600732 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500733 if (color_type & PNG_COLOR_MASK_COLOR)
734 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600735 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500736
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500737 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
738 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600739 if (sbit->red == 0 || sbit->red > maxbits ||
740 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500741 sbit->blue == 0 || sbit->blue > maxbits)
742 {
743 png_warning(png_ptr, "Invalid sBIT depth specified");
744 return;
745 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500746 buf[0] = sbit->red;
747 buf[1] = sbit->green;
748 buf[2] = sbit->blue;
749 size = 3;
750 }
751 else
752 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500753 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
754 {
755 png_warning(png_ptr, "Invalid sBIT depth specified");
756 return;
757 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500758 buf[0] = sbit->gray;
759 size = 1;
760 }
761
762 if (color_type & PNG_COLOR_MASK_ALPHA)
763 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500764 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
765 {
766 png_warning(png_ptr, "Invalid sBIT depth specified");
767 return;
768 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500769 buf[size++] = sbit->alpha;
770 }
771
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600772 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500773}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500774#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500775
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500776#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500777/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600778#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500779void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500780png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600781 double red_x, double red_y, double green_x, double green_y,
782 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500783{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600784#ifdef PNG_USE_LOCAL_ARRAYS
785 PNG_cHRM;
786#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500787 png_uint_32 itemp;
788 png_byte buf[32];
789
Andreas Dilger47a0c421997-05-16 02:46:07 -0500790 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600791 /* each value is saved in 1/1000000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500792 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
793 white_x + white_y > 1.0)
794 {
795 png_warning(png_ptr, "Invalid cHRM white point specified");
796 return;
797 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600798 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600800 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500801 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500802
803 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
804 red_x + red_y > 1.0)
805 {
806 png_warning(png_ptr, "Invalid cHRM red point specified");
807 return;
808 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600809 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500810 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600811 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500812 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500813
814 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
815 green_x + green_y > 1.0)
816 {
817 png_warning(png_ptr, "Invalid cHRM green point specified");
818 return;
819 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600820 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500821 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600822 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500823 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500824
825 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
826 blue_x + blue_y > 1.0)
827 {
828 png_warning(png_ptr, "Invalid cHRM blue point specified");
829 return;
830 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600831 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500832 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600833 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500834 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500835
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600836 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500837}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500838#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600839#ifdef PNG_FIXED_POINT_SUPPORTED
840void
841png_write_cHRM_fixed(png_structp png_ptr, png_uint_32 white_x,
842 png_uint_32 white_y, png_uint_32 red_x, png_uint_32 red_y,
843 png_uint_32 green_x, png_uint_32 green_y, png_uint_32 blue_x,
844 png_uint_32 blue_y)
845{
846#ifdef PNG_USE_LOCAL_ARRAYS
847 PNG_cHRM;
848#endif
849 png_uint_32 itemp;
850 png_byte buf[32];
851
852 png_debug(1, "in png_write_cHRM\n");
853 /* each value is saved int 1/1000000ths */
854 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
855 {
856 png_warning(png_ptr, "Invalid cHRM white point specified");
857 return;
858 }
859 png_save_uint_32(buf, white_x);
860 png_save_uint_32(buf + 4, white_y);
861
862 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
863 {
864 png_warning(png_ptr, "Invalid cHRM red point specified");
865 return;
866 }
867 png_save_uint_32(buf + 8, red_x);
868 png_save_uint_32(buf + 12, red_y);
869
870 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
871 {
872 png_warning(png_ptr, "Invalid cHRM green point specified");
873 return;
874 }
875 png_save_uint_32(buf + 16, green_x);
876 png_save_uint_32(buf + 20, green_y);
877
878 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
879 {
880 png_warning(png_ptr, "Invalid cHRM blue point specified");
881 return;
882 }
883 png_save_uint_32(buf + 24, blue_x);
884 png_save_uint_32(buf + 28, blue_y);
885
886 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
887}
888#endif
889#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500890
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500891#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500892/* write the tRNS chunk */
893void
Guy Schalnat6d764711995-12-19 03:22:19 -0600894png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500895 int num_trans, int color_type)
896{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600897#ifdef PNG_USE_LOCAL_ARRAYS
898 PNG_tRNS;
899#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500900 png_byte buf[6];
901
Andreas Dilger47a0c421997-05-16 02:46:07 -0500902 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500903 if (color_type == PNG_COLOR_TYPE_PALETTE)
904 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600905 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500906 {
907 png_warning(png_ptr,"Invalid number of transparent colors specified");
908 return;
909 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500910 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600911 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500912 }
913 else if (color_type == PNG_COLOR_TYPE_GRAY)
914 {
915 /* one 16 bit value */
916 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600917 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500918 }
919 else if (color_type == PNG_COLOR_TYPE_RGB)
920 {
921 /* three 16 bit values */
922 png_save_uint_16(buf, tran->red);
923 png_save_uint_16(buf + 2, tran->green);
924 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600925 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500926 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500927 else
928 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600929 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500930 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500931}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500932#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500933
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500934#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500935/* write the background chunk */
936void
Guy Schalnat6d764711995-12-19 03:22:19 -0600937png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500938{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600939#ifdef PNG_USE_LOCAL_ARRAYS
940 PNG_bKGD;
941#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500942 png_byte buf[6];
943
Andreas Dilger47a0c421997-05-16 02:46:07 -0500944 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500945 if (color_type == PNG_COLOR_TYPE_PALETTE)
946 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500947 if (
948#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
949 (!png_ptr->empty_plte_permitted ||
950 (png_ptr->empty_plte_permitted && png_ptr->num_palette)) &&
951#endif
952 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500953 {
954 png_warning(png_ptr, "Invalid background palette index");
955 return;
956 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500957 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600958 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500959 }
960 else if (color_type & PNG_COLOR_MASK_COLOR)
961 {
962 png_save_uint_16(buf, back->red);
963 png_save_uint_16(buf + 2, back->green);
964 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600965 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500966 }
967 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600968 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500969 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600970 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500971 }
972}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500973#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500974
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500975#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500976/* write the histogram */
977void
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600978png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500979{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600980#ifdef PNG_USE_LOCAL_ARRAYS
981 PNG_hIST;
982#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600983 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500984 png_byte buf[3];
985
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600987 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500988 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500989 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
990 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500991 png_warning(png_ptr, "Invalid number of histogram entries specified");
992 return;
993 }
994
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600995 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500997 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600998 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500999 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001000 }
1001 png_write_chunk_end(png_ptr);
1002}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001003#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001004
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1006 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001007/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1008 * and if invalid, correct the keyword rather than discarding the entire
1009 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1010 * length, forbids leading or trailing whitespace, multiple internal spaces,
1011 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001012 *
1013 * The new_key is allocated to hold the corrected keyword and must be freed
1014 * by the calling routine. This avoids problems with trying to write to
1015 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001016 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017png_size_t
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001018png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001019{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001020 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001021 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001022 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001023
Andreas Dilger47a0c421997-05-16 02:46:07 -05001024 png_debug(1, "in png_check_keyword\n");
1025 *new_key = NULL;
1026
1027 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001028 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001029 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001030 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001031 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001032
Andreas Dilger47a0c421997-05-16 02:46:07 -05001033 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1034
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001035 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001036
1037 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001039 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001040 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001041 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001042#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001044
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001045 sprintf(msg, "invalid keyword character 0x%02X", *kp);
1046 png_chunk_warning(png_ptr, msg);
1047#else
1048 png_chunk_warning(png_ptr, "invalid character in keyword");
1049#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001050 *dp = ' ';
1051 }
1052 else
1053 {
1054 *dp = *kp;
1055 }
1056 }
1057 *dp = '\0';
1058
1059 /* Remove any trailing white space. */
1060 kp = *new_key + key_len - 1;
1061 if (*kp == ' ')
1062 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001063 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001064
1065 while (*kp == ' ')
1066 {
1067 *(kp--) = '\0';
1068 key_len--;
1069 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001070 }
1071
1072 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001073 kp = *new_key;
1074 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001075 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001076 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001077
1078 while (*kp == ' ')
1079 {
1080 kp++;
1081 key_len--;
1082 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001083 }
1084
Andreas Dilger47a0c421997-05-16 02:46:07 -05001085 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001086
Andreas Dilger47a0c421997-05-16 02:46:07 -05001087 /* Remove multiple internal spaces. */
1088 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001089 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001091 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 *(dp++) = *kp;
1093 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001094 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001095 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001096 {
1097 key_len--;
1098 }
1099 else
1100 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001101 *(dp++) = *kp;
1102 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001103 }
1104 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001106
1107 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001109 png_free(png_ptr, *new_key);
1110 *new_key=NULL;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001111 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001112 }
1113
1114 if (key_len > 79)
1115 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001116 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001117 new_key[79] = '\0';
1118 key_len = 79;
1119 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001121 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001122}
1123#endif
1124
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001125#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001126/* write a tEXt chunk */
1127void
Guy Schalnat6d764711995-12-19 03:22:19 -06001128png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001130{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001131#ifdef PNG_USE_LOCAL_ARRAYS
1132 PNG_tEXt;
1133#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001134 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001135 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001136
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 png_debug(1, "in png_write_tEXt\n");
1138 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1139 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001140 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001141 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001143
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001145 text_len = 0;
1146
1147 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001148 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 -05001149 /*
1150 * We leave it to the application to meet PNG-1.0 requirements on the
1151 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1152 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001153 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001154 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001155 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001156 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001158
Guy Schalnat0d580581995-07-20 02:43:20 -05001159 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001160 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001161}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001162#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001163
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001164#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165/* write a compressed text chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001166void
Guy Schalnat6d764711995-12-19 03:22:19 -06001167png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001169{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001170#ifdef PNG_USE_LOCAL_ARRAYS
1171 PNG_zTXt;
1172#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001174 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001175 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001176 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001177
Andreas Dilger47a0c421997-05-16 02:46:07 -05001178 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001179
Andreas Dilger47a0c421997-05-16 02:46:07 -05001180 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001181 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001182 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001183 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001184 }
1185
Andreas Dilger47a0c421997-05-16 02:46:07 -05001186 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1187 {
1188 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1189 png_free(png_ptr, new_key);
1190 return;
1191 }
1192
1193 png_free(png_ptr, new_key);
1194
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001195 /* compute the compressed data; do it now for the length */
1196 text_len = png_text_compress(png_ptr, text, text_len, compression, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001197
1198 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001199 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1200 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001201 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001203 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001204 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001206 /* write the compressed data */
1207 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001208
Guy Schalnat0d580581995-07-20 02:43:20 -05001209 /* close the chunk */
1210 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001211}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001212#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001213
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001214#if defined(PNG_WRITE_iTXt_SUPPORTED)
1215/* write an iTXt chunk */
1216void
1217png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
1218 png_charp lang, png_charp translated_key, png_charp text)
1219{
1220#ifdef PNG_USE_LOCAL_ARRAYS
1221 PNG_iTXt;
1222#endif
1223 png_size_t lang_len, key_len, translated_key_len, text_len;
1224 png_charp new_lang, new_key;
1225 png_byte cbuf[2];
1226 compression_state comp;
1227
1228 png_debug(1, "in png_write_iTXt\n");
1229
1230 translated_key_len = png_strlen(translated_key);
1231 text_len = png_strlen(text);
1232
1233 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1234 &new_lang))==0)
1235 {
1236 png_warning(png_ptr, "Empty language field in iTXt chunk");
1237 return;
1238 }
1239 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1240 {
1241 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1242 return;
1243 }
1244
1245 if (text == NULL || *text == '\0')
1246 text_len = 0;
1247
1248 /* compute the compressed data; do it now for the length */
1249 text_len = png_text_compress(png_ptr, text, text_len, compression, &comp);
1250
1251 /* make sure we include the compression flag, the compression byte,
1252 * and the NULs after the key, lang, and translated_key parts */
1253 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1254 (png_uint_32)(2 + lang_len+1 + key_len+1 + translated_key_len +
1255 text_len));
1256
1257 /* set the compression bits */
1258 if (compression == PNG_TEXT_COMPRESSION_NONE)
1259 {
1260 cbuf[0] = 0;
1261 cbuf[1] = 0;
1262 }
1263 else /* compression == PNG_TEXT_COMPRESSION_zTXt */
1264 {
1265 cbuf[0] = 1;
1266 cbuf[1] = 0;
1267 }
1268 png_write_chunk_data(png_ptr, cbuf, 2);
1269
1270 /*
1271 * We leave it to the application to meet PNG-1.0 requirements on the
1272 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1273 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1274 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1275 */
1276 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1277 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
1278 png_write_chunk_data(png_ptr, (png_bytep)translated_key,
1279 translated_key_len);
1280 png_write_chunk_data(png_ptr, '\0', 1);
1281
1282 png_write_compressed_data_out(png_ptr, &comp);
1283
1284 png_write_chunk_end(png_ptr);
1285 png_free(png_ptr, new_key);
1286 png_free(png_ptr, new_lang);
1287}
1288#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001289
1290#if defined(PNG_WRITE_oFFs_SUPPORTED)
1291/* write the oFFs chunk */
1292void
1293png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1294 png_uint_32 y_offset,
1295 int unit_type)
1296{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001297#ifdef PNG_USE_LOCAL_ARRAYS
1298 PNG_oFFs;
1299#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001300 png_byte buf[9];
1301
1302 png_debug(1, "in png_write_oFFs\n");
1303 if (unit_type >= PNG_OFFSET_LAST)
1304 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1305
1306 png_save_uint_32(buf, x_offset);
1307 png_save_uint_32(buf + 4, y_offset);
1308 buf[8] = (png_byte)unit_type;
1309
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001310 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001311}
1312#endif
1313
1314#if defined(PNG_WRITE_pCAL_SUPPORTED)
1315/* write the pCAL chunk (png-scivis-19970203) */
1316void
1317png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1318 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1319{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001320#ifdef PNG_USE_LOCAL_ARRAYS
1321 PNG_pCAL;
1322#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001323 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001324 png_uint_32p params_len;
1325 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001326 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001327 int i;
1328
1329 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1330 if (type >= PNG_EQUATION_LAST)
1331 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1332
1333 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1334 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1335 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1336 png_debug1(3, "pCAL units length = %d\n", units_len);
1337 total_len = purpose_len + units_len + 10;
1338
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001339 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1340 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341
1342 /* Find the length of each parameter, making sure we don't count the
1343 null terminator for the last parameter. */
1344 for (i = 0; i < nparams; i++)
1345 {
1346 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1347 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
1348 total_len += (png_size_t)params_len[i];
1349 }
1350
1351 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001352 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001353 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001354 png_save_int_32(buf, X0);
1355 png_save_int_32(buf + 4, X1);
1356 buf[8] = (png_byte)type;
1357 buf[9] = (png_byte)nparams;
1358 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1359 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1360
1361 png_free(png_ptr, new_purpose);
1362
1363 for (i = 0; i < nparams; i++)
1364 {
1365 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1366 (png_size_t)params_len[i]);
1367 }
1368
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001369 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001370 png_write_chunk_end(png_ptr);
1371}
1372#endif
1373
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001374#if defined(PNG_WRITE_sCAL_SUPPORTED)
1375/* write the sCAL chunk */
1376#ifdef PNG_FLOATING_POINT_SUPPORTED
1377void
1378png_write_sCAL(png_structp png_ptr, png_charp unit, double width,double height)
1379{
1380#ifdef PNG_USE_LOCAL_ARRAYS
1381 PNG_sCAL;
1382#endif
1383 png_size_t total_len;
1384 char wbuf[32], hbuf[32];
1385
1386 png_debug(1, "in png_write_sCAL\n");
1387
1388 sprintf(wbuf, "%12.12e", width);
1389 sprintf(hbuf, "%12.12e", height);
1390 total_len = png_strlen(unit)+1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1391
1392 png_debug1(3, "sCAL total length = %d\n", total_len);
1393 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1394 png_write_chunk_data(png_ptr, (png_bytep)unit, png_strlen(unit)+1);
1395 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1396 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1397
1398 png_write_chunk_end(png_ptr);
1399}
1400#endif
1401void
1402png_write_sCAL_s(png_structp png_ptr, png_charp unit, png_charp width,
1403 png_charp height)
1404{
1405#ifdef PNG_USE_LOCAL_ARRAYS
1406 PNG_sCAL;
1407#endif
1408 png_size_t total_len;
1409 char wbuf[32], hbuf[32];
1410
1411 png_debug(1, "in png_write_sCAL\n");
1412
1413 sprintf(wbuf, "%s", width);
1414 sprintf(hbuf, "%s", height);
1415 total_len = png_strlen(unit)+1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1416
1417 png_debug1(3, "sCAL total length = %d\n", total_len);
1418 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1419 png_write_chunk_data(png_ptr, (png_bytep)unit, png_strlen(unit)+1);
1420 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1421 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1422
1423 png_write_chunk_end(png_ptr);
1424}
1425#endif
1426
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001427#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001428/* write the pHYs chunk */
1429void
Guy Schalnat6d764711995-12-19 03:22:19 -06001430png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001431 png_uint_32 y_pixels_per_unit,
1432 int unit_type)
1433{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001434#ifdef PNG_USE_LOCAL_ARRAYS
1435 PNG_pHYs;
1436#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001437 png_byte buf[9];
1438
Andreas Dilger47a0c421997-05-16 02:46:07 -05001439 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001440 if (unit_type >= PNG_RESOLUTION_LAST)
1441 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1442
Guy Schalnat0d580581995-07-20 02:43:20 -05001443 png_save_uint_32(buf, x_pixels_per_unit);
1444 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001445 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001446
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001447 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001448}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001449#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001450
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001451#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001452/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1453 * or png_convert_from_time_t(), or fill in the structure yourself.
1454 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001455void
Guy Schalnat6d764711995-12-19 03:22:19 -06001456png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001457{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001458#ifdef PNG_USE_LOCAL_ARRAYS
1459 PNG_tIME;
1460#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001461 png_byte buf[7];
1462
Andreas Dilger47a0c421997-05-16 02:46:07 -05001463 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001464 if (mod_time->month > 12 || mod_time->month < 1 ||
1465 mod_time->day > 31 || mod_time->day < 1 ||
1466 mod_time->hour > 23 || mod_time->second > 60)
1467 {
1468 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1469 return;
1470 }
1471
Guy Schalnat0d580581995-07-20 02:43:20 -05001472 png_save_uint_16(buf, mod_time->year);
1473 buf[2] = mod_time->month;
1474 buf[3] = mod_time->day;
1475 buf[4] = mod_time->hour;
1476 buf[5] = mod_time->minute;
1477 buf[6] = mod_time->second;
1478
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001479 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001480}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001481#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001482
1483/* initializes the row writing capability of libpng */
1484void
Guy Schalnat6d764711995-12-19 03:22:19 -06001485png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001486{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001487#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001488 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001489
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001490 /* start of interlace block */
1491 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001492
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001493 /* offset to next interlace block */
1494 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001495
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001496 /* start of interlace block in the y direction */
1497 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001498
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001499 /* offset to next interlace block in the y direction */
1500 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001501#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001502
Andreas Dilger47a0c421997-05-16 02:46:07 -05001503 png_size_t buf_size;
1504
1505 png_debug(1, "in png_write_start_row\n");
1506 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1507 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1508
Guy Schalnat0d580581995-07-20 02:43:20 -05001509 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001510 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001511 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001512
1513 /* set up filtering buffer, if using this filter */
1514 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001515 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001516 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001517 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001518 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001519 }
1520
Andreas Dilger47a0c421997-05-16 02:46:07 -05001521 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001522 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1523 {
1524 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001525 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001526 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001527
1528 if (png_ptr->do_filter & PNG_FILTER_UP)
1529 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001530 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001531 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001532 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001533 }
1534
1535 if (png_ptr->do_filter & PNG_FILTER_AVG)
1536 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001537 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1538 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001539 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001540 }
1541
1542 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1543 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001544 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001545 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001547 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001548 }
1549
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001550#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001551 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001552 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001553 {
1554 if (!(png_ptr->transformations & PNG_INTERLACE))
1555 {
1556 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1557 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1559 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001560 }
1561 else
1562 {
1563 png_ptr->num_rows = png_ptr->height;
1564 png_ptr->usr_width = png_ptr->width;
1565 }
1566 }
1567 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001568#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001569 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001570 png_ptr->num_rows = png_ptr->height;
1571 png_ptr->usr_width = png_ptr->width;
1572 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001573 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1574 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001575}
1576
Andreas Dilger47a0c421997-05-16 02:46:07 -05001577/* Internal use only. Called when finished processing a row of data. */
Guy Schalnat0d580581995-07-20 02:43:20 -05001578void
Guy Schalnat6d764711995-12-19 03:22:19 -06001579png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001580{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001581#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001582 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001583
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001584 /* start of interlace block */
1585 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001586
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001587 /* offset to next interlace block */
1588 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001589
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001590 /* start of interlace block in the y direction */
1591 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001592
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001593 /* offset to next interlace block in the y direction */
1594 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001595#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001596
Guy Schalnat0d580581995-07-20 02:43:20 -05001597 int ret;
1598
Andreas Dilger47a0c421997-05-16 02:46:07 -05001599 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001600 /* next row */
1601 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001602
Guy Schalnat0d580581995-07-20 02:43:20 -05001603 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001604 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001605 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001606
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001607#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001608 /* if interlaced, go to next pass */
1609 if (png_ptr->interlaced)
1610 {
1611 png_ptr->row_number = 0;
1612 if (png_ptr->transformations & PNG_INTERLACE)
1613 {
1614 png_ptr->pass++;
1615 }
1616 else
1617 {
1618 /* loop until we find a non-zero width or height pass */
1619 do
1620 {
1621 png_ptr->pass++;
1622 if (png_ptr->pass >= 7)
1623 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001624 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001625 png_pass_inc[png_ptr->pass] - 1 -
1626 png_pass_start[png_ptr->pass]) /
1627 png_pass_inc[png_ptr->pass];
1628 png_ptr->num_rows = (png_ptr->height +
1629 png_pass_yinc[png_ptr->pass] - 1 -
1630 png_pass_ystart[png_ptr->pass]) /
1631 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001632 if (png_ptr->transformations & PNG_INTERLACE)
1633 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001634 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1635
1636 }
1637
Guy Schalnate5a37791996-06-05 15:50:50 -05001638 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001639 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001640 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001641 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001642 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001643 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001644 (png_uint_32)png_ptr->usr_bit_depth *
1645 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001646 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001647 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001648 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001649#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001650
1651 /* if we get here, we've just written the last row, so we need
1652 to flush the compressor */
1653 do
1654 {
1655 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001656 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001657 /* check for an error */
1658 if (ret != Z_OK && ret != Z_STREAM_END)
1659 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001660 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001661 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001662 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001663 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001664 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001665 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001666 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001667 {
1668 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001669 png_ptr->zstream.next_out = png_ptr->zbuf;
1670 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001671 }
1672 } while (ret != Z_STREAM_END);
1673
1674 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001675 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001676 {
1677 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001678 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001679 }
1680
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001681 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001682}
1683
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001684#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001685/* Pick out the correct pixels for the interlace pass.
1686 * The basic idea here is to go through the row with a source
1687 * pointer and a destination pointer (sp and dp), and copy the
1688 * correct pixels for the pass. As the row gets compacted,
1689 * sp will always be >= dp, so we should never overwrite anything.
1690 * See the default: case for the easiest code to understand.
1691 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001692void
Guy Schalnat6d764711995-12-19 03:22:19 -06001693png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001694{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001695#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001696 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001697
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001698 /* start of interlace block */
1699 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001700
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001701 /* offset to next interlace block */
1702 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001703#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001704
Andreas Dilger47a0c421997-05-16 02:46:07 -05001705 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001706 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001707#if defined(PNG_USELESS_TESTS_SUPPORTED)
1708 if (row != NULL && row_info != NULL && pass < 6)
1709#else
1710 if (pass < 6)
1711#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001712 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001713 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001714 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001715 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001716 case 1:
1717 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001718 png_bytep sp;
1719 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001720 int shift;
1721 int d;
1722 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001723 png_uint_32 i;
1724 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001725
1726 dp = row;
1727 d = 0;
1728 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001729 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001730 i += png_pass_inc[pass])
1731 {
1732 sp = row + (png_size_t)(i >> 3);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001733 value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001734 d |= (value << shift);
1735
1736 if (shift == 0)
1737 {
1738 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001739 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001740 d = 0;
1741 }
1742 else
1743 shift--;
1744
1745 }
1746 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001747 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001748 break;
1749 }
1750 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001751 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001752 png_bytep sp;
1753 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001754 int shift;
1755 int d;
1756 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001757 png_uint_32 i;
1758 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001759
1760 dp = row;
1761 shift = 6;
1762 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001763 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001764 i += png_pass_inc[pass])
1765 {
1766 sp = row + (png_size_t)(i >> 2);
1767 value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
1768 d |= (value << shift);
1769
1770 if (shift == 0)
1771 {
1772 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001773 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001774 d = 0;
1775 }
1776 else
1777 shift -= 2;
1778 }
1779 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001780 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001781 break;
1782 }
1783 case 4:
1784 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001785 png_bytep sp;
1786 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001787 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001788 int d;
1789 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001790 png_uint_32 i;
1791 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001792
1793 dp = row;
1794 shift = 4;
1795 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001796 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001797 i += png_pass_inc[pass])
1798 {
1799 sp = row + (png_size_t)(i >> 1);
1800 value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
1801 d |= (value << shift);
1802
1803 if (shift == 0)
1804 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001805 shift = 4;
1806 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001807 d = 0;
1808 }
1809 else
1810 shift -= 4;
1811 }
1812 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001813 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001814 break;
1815 }
1816 default:
1817 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001818 png_bytep sp;
1819 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001820 png_uint_32 i;
1821 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001822 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001823
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001824 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001825 dp = row;
1826 /* find out how many bytes each pixel takes up */
1827 pixel_bytes = (row_info->pixel_depth >> 3);
1828 /* loop through the row, only looking at the pixels that
1829 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001830 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001831 i += png_pass_inc[pass])
1832 {
1833 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001834 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001835 /* move the pixel */
1836 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001837 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001838 /* next pixel */
1839 dp += pixel_bytes;
1840 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001841 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001842 }
1843 }
1844 /* set new row width */
1845 row_info->width = (row_info->width +
1846 png_pass_inc[pass] - 1 -
1847 png_pass_start[pass]) /
1848 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001849 row_info->rowbytes = ((row_info->width *
1850 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001851 }
1852}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001853#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001854
Andreas Dilger47a0c421997-05-16 02:46:07 -05001855/* This filters the row, chooses which filter to use, if it has not already
1856 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001857 * chosen filter.
1858 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001859#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001860#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001861#define PNG_LOMASK ((png_uint_32)0xffffL)
1862#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Guy Schalnat0d580581995-07-20 02:43:20 -05001863void
Guy Schalnate5a37791996-06-05 15:50:50 -05001864png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001865{
Guy Schalnate5a37791996-06-05 15:50:50 -05001866 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001867 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001868 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001869 png_uint_32 row_bytes = row_info->rowbytes;
1870#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1871 int num_p_filters = (int)png_ptr->num_prev_filters;
1872#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001873
Andreas Dilger47a0c421997-05-16 02:46:07 -05001874 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001875 /* find out how many bytes offset each pixel is */
1876 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001877
1878 prev_row = png_ptr->prev_row;
1879 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001880 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001881
Andreas Dilger47a0c421997-05-16 02:46:07 -05001882 /* The prediction method we use is to find which method provides the
1883 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001884 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001885 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001886 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001887 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001888 * predictive" method (not implemented yet), which does test compressions
1889 * of lines using different filter methods, and then chooses the
1890 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001891 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001892 *
1893 * GRR 980525: consider also
1894 * (1) minimum sum of absolute differences from running average (i.e.,
1895 * keep running sum of non-absolute differences & count of bytes)
1896 * [track dispersion, too? restart average if dispersion too large?]
1897 * (1b) minimum sum of absolute differences from sliding average, probably
1898 * with window size <= deflate window (usually 32K)
1899 * (2) minimum sum of squared differences from zero or running average
1900 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001901 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001902
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001903
Guy Schalnate5a37791996-06-05 15:50:50 -05001904 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001905 * that has been chosen, as it doesn't actually do anything to the data.
1906 */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001907 if (filter_to_do & PNG_FILTER_NONE &&
1908 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001909 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001910 png_bytep rp;
1911 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001912 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001913 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001914
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001915 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001916 {
1917 v = *rp;
1918 sum += (v < 128) ? v : 256 - v;
1919 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001920
1921#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1922 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1923 {
1924 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001925 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001926 sumlo = sum & PNG_LOMASK;
1927 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1928
1929 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001930 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001931 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001932 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001933 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001934 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001935 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001936 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001937 PNG_WEIGHT_SHIFT;
1938 }
1939 }
1940
1941 /* Factor in the cost of this filter (this is here for completeness,
1942 * but it makes no sense to have a "cost" for the NONE filter, as
1943 * it has the minimum possible computational cost - none).
1944 */
1945 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1946 PNG_COST_SHIFT;
1947 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1948 PNG_COST_SHIFT;
1949
1950 if (sumhi > PNG_HIMASK)
1951 sum = PNG_MAXSUM;
1952 else
1953 sum = (sumhi << PNG_HISHIFT) + sumlo;
1954 }
1955#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001956 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001957 }
1958
Guy Schalnate5a37791996-06-05 15:50:50 -05001959 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001960 if (filter_to_do == PNG_FILTER_SUB)
1961 /* it's the only filter so no testing is needed */
1962 {
1963 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001964 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001965 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1966 i++, rp++, dp++)
1967 {
1968 *dp = *rp;
1969 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001970 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001971 i++, rp++, lp++, dp++)
1972 {
1973 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
1974 }
1975 best_row = png_ptr->sub_row;
1976 }
1977
1978 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001979 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001980 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001981 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001982 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001983 int v;
1984
1985#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001986 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05001987 * would reduce the sum of this filter, so that we can do the
1988 * early exit comparison without scaling the sum each time.
1989 */
1990 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1991 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001992 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001993 png_uint_32 lmhi, lmlo;
1994 lmlo = lmins & PNG_LOMASK;
1995 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1996
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001997 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001998 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001999 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002000 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002001 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002002 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002003 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002004 PNG_WEIGHT_SHIFT;
2005 }
2006 }
2007
2008 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2009 PNG_COST_SHIFT;
2010 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2011 PNG_COST_SHIFT;
2012
2013 if (lmhi > PNG_HIMASK)
2014 lmins = PNG_MAXSUM;
2015 else
2016 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2017 }
2018#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002019
Guy Schalnate5a37791996-06-05 15:50:50 -05002020 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2021 i++, rp++, dp++)
2022 {
2023 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002024
Guy Schalnate5a37791996-06-05 15:50:50 -05002025 sum += (v < 128) ? v : 256 - v;
2026 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002027 for (lp = row_buf + 1; i < row_info->rowbytes;
2028 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002029 {
2030 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002031
Guy Schalnate5a37791996-06-05 15:50:50 -05002032 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033
2034 if (sum > lmins) /* We are already worse, don't continue. */
2035 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002036 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002037
2038#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2039 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2040 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002041 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002042 png_uint_32 sumhi, sumlo;
2043 sumlo = sum & PNG_LOMASK;
2044 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2045
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002046 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002047 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002048 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002049 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002050 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002051 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002052 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002053 PNG_WEIGHT_SHIFT;
2054 }
2055 }
2056
2057 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2058 PNG_COST_SHIFT;
2059 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2060 PNG_COST_SHIFT;
2061
2062 if (sumhi > PNG_HIMASK)
2063 sum = PNG_MAXSUM;
2064 else
2065 sum = (sumhi << PNG_HISHIFT) + sumlo;
2066 }
2067#endif
2068
Guy Schalnate5a37791996-06-05 15:50:50 -05002069 if (sum < mins)
2070 {
2071 mins = sum;
2072 best_row = png_ptr->sub_row;
2073 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002074 }
2075
Guy Schalnate5a37791996-06-05 15:50:50 -05002076 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002077 if (filter_to_do == PNG_FILTER_UP)
2078 {
2079 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002080 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002081
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002082 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002083 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002084 i++, rp++, pp++, dp++)
2085 {
2086 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2087 }
2088 best_row = png_ptr->up_row;
2089 }
2090
2091 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002092 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002093 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002094 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002095 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002096 int v;
2097
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002098
Andreas Dilger47a0c421997-05-16 02:46:07 -05002099#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2100 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2101 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002102 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002103 png_uint_32 lmhi, lmlo;
2104 lmlo = lmins & PNG_LOMASK;
2105 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2106
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002107 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002108 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002109 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002110 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002111 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002113 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002114 PNG_WEIGHT_SHIFT;
2115 }
2116 }
2117
2118 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2119 PNG_COST_SHIFT;
2120 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2121 PNG_COST_SHIFT;
2122
2123 if (lmhi > PNG_HIMASK)
2124 lmins = PNG_MAXSUM;
2125 else
2126 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2127 }
2128#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002129
2130 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002131 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002132 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002133 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002134
2135 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002136
2137 if (sum > lmins) /* We are already worse, don't continue. */
2138 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002139 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002140
2141#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2142 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2143 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002144 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145 png_uint_32 sumhi, sumlo;
2146 sumlo = sum & PNG_LOMASK;
2147 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2148
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002149 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002150 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002151 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002152 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002153 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002154 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002155 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002156 PNG_WEIGHT_SHIFT;
2157 }
2158 }
2159
2160 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2161 PNG_COST_SHIFT;
2162 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2163 PNG_COST_SHIFT;
2164
2165 if (sumhi > PNG_HIMASK)
2166 sum = PNG_MAXSUM;
2167 else
2168 sum = (sumhi << PNG_HISHIFT) + sumlo;
2169 }
2170#endif
2171
Guy Schalnate5a37791996-06-05 15:50:50 -05002172 if (sum < mins)
2173 {
2174 mins = sum;
2175 best_row = png_ptr->up_row;
2176 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002177 }
2178
Guy Schalnate5a37791996-06-05 15:50:50 -05002179 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002180 if (filter_to_do == PNG_FILTER_AVG)
2181 {
2182 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002183 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002184 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002185 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002186 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002187 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002188 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002189 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002190 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002191 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2192 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002193 }
2194 best_row = png_ptr->avg_row;
2195 }
2196
2197 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002198 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002199 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002200 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002201 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002202 int v;
2203
2204#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2205 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2206 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002207 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002208 png_uint_32 lmhi, lmlo;
2209 lmlo = lmins & PNG_LOMASK;
2210 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2211
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002212 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002213 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002214 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002216 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002217 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002218 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002219 PNG_WEIGHT_SHIFT;
2220 }
2221 }
2222
2223 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2224 PNG_COST_SHIFT;
2225 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2226 PNG_COST_SHIFT;
2227
2228 if (lmhi > PNG_HIMASK)
2229 lmins = PNG_MAXSUM;
2230 else
2231 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2232 }
2233#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002234
2235 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002236 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002237 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002238 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002239
2240 sum += (v < 128) ? v : 256 - v;
2241 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002242 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002243 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002244 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002245 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002246
2247 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248
2249 if (sum > lmins) /* We are already worse, don't continue. */
2250 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002251 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002252
2253#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2254 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2255 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002256 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 png_uint_32 sumhi, sumlo;
2258 sumlo = sum & PNG_LOMASK;
2259 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2260
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002265 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002266 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002267 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002268 PNG_WEIGHT_SHIFT;
2269 }
2270 }
2271
2272 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2273 PNG_COST_SHIFT;
2274 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2275 PNG_COST_SHIFT;
2276
2277 if (sumhi > PNG_HIMASK)
2278 sum = PNG_MAXSUM;
2279 else
2280 sum = (sumhi << PNG_HISHIFT) + sumlo;
2281 }
2282#endif
2283
Guy Schalnate5a37791996-06-05 15:50:50 -05002284 if (sum < mins)
2285 {
2286 mins = sum;
2287 best_row = png_ptr->avg_row;
2288 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002289 }
2290
Andreas Dilger47a0c421997-05-16 02:46:07 -05002291 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002292 if (filter_to_do == PNG_FILTER_PAETH)
2293 {
2294 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002295 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002296 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002297 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002298 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002299 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002300 }
2301
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002302 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002303 {
2304 int a, b, c, pa, pb, pc, p;
2305
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002306 b = *pp++;
2307 c = *cp++;
2308 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002309
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002310 p = b - c;
2311 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002312
2313#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002314 pa = abs(p);
2315 pb = abs(pc);
2316 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002317#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002318 pa = p < 0 ? -p : p;
2319 pb = pc < 0 ? -pc : pc;
2320 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002321#endif
2322
2323 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2324
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002325 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002326 }
2327 best_row = png_ptr->paeth_row;
2328 }
2329
2330 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002331 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002332 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002333 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002334 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002335 int v;
2336
2337#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2338 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2339 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002340 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002341 png_uint_32 lmhi, lmlo;
2342 lmlo = lmins & PNG_LOMASK;
2343 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2344
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002345 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002346 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002349 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002350 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002351 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 PNG_WEIGHT_SHIFT;
2353 }
2354 }
2355
2356 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2357 PNG_COST_SHIFT;
2358 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2359 PNG_COST_SHIFT;
2360
2361 if (lmhi > PNG_HIMASK)
2362 lmins = PNG_MAXSUM;
2363 else
2364 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2365 }
2366#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002367
2368 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002369 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002370 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002372
2373 sum += (v < 128) ? v : 256 - v;
2374 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002375
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002376 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002377 {
2378 int a, b, c, pa, pb, pc, p;
2379
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002380 b = *pp++;
2381 c = *cp++;
2382 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002383
2384#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002385 p = b - c;
2386 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002387#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002388 pa = abs(p);
2389 pb = abs(pc);
2390 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002391#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002392 pa = p < 0 ? -p : p;
2393 pb = pc < 0 ? -pc : pc;
2394 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002395#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002396 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2397#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002398 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002399 pa = abs(p - a);
2400 pb = abs(p - b);
2401 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002402 if (pa <= pb && pa <= pc)
2403 p = a;
2404 else if (pb <= pc)
2405 p = b;
2406 else
2407 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002408#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002409
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002411
2412 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002413
2414 if (sum > lmins) /* We are already worse, don't continue. */
2415 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002416 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002417
2418#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2419 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2420 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002421 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002422 png_uint_32 sumhi, sumlo;
2423 sumlo = sum & PNG_LOMASK;
2424 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2425
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002426 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002427 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002428 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002429 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002430 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002431 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002432 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002433 PNG_WEIGHT_SHIFT;
2434 }
2435 }
2436
2437 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2438 PNG_COST_SHIFT;
2439 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2440 PNG_COST_SHIFT;
2441
2442 if (sumhi > PNG_HIMASK)
2443 sum = PNG_MAXSUM;
2444 else
2445 sum = (sumhi << PNG_HISHIFT) + sumlo;
2446 }
2447#endif
2448
Guy Schalnate5a37791996-06-05 15:50:50 -05002449 if (sum < mins)
2450 {
2451 best_row = png_ptr->paeth_row;
2452 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002453 }
2454
Andreas Dilger47a0c421997-05-16 02:46:07 -05002455 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002456
Guy Schalnate5a37791996-06-05 15:50:50 -05002457 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458
2459#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2460 /* Save the type of filter we picked this time for future calculations */
2461 if (png_ptr->num_prev_filters > 0)
2462 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002463 int j;
2464 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002465 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002466 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002467 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002468 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002469 }
2470#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002471}
2472
2473
Andreas Dilger47a0c421997-05-16 02:46:07 -05002474/* Do the actual writing of a previously filtered row. */
Guy Schalnate5a37791996-06-05 15:50:50 -05002475void
2476png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2477{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002478 png_debug(1, "in png_write_filtered_row\n");
2479 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002480 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002481 png_ptr->zstream.next_in = filtered_row;
2482 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002483 /* repeat until we have compressed all the data */
2484 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002485 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002486 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002487
Guy Schalnate5a37791996-06-05 15:50:50 -05002488 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002489 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002490 /* check for compression errors */
2491 if (ret != Z_OK)
2492 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002493 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002494 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002495 else
2496 png_error(png_ptr, "zlib error");
2497 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002498
Guy Schalnate5a37791996-06-05 15:50:50 -05002499 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002500 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002501 {
2502 /* write the IDAT and reset the zlib output buffer */
2503 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002504 png_ptr->zstream.next_out = png_ptr->zbuf;
2505 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002506 }
2507 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002508 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002509
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002510 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002511 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002512 {
2513 png_bytep tptr;
2514
2515 tptr = png_ptr->prev_row;
2516 png_ptr->prev_row = png_ptr->row_buf;
2517 png_ptr->row_buf = tptr;
2518 }
2519
Guy Schalnate5a37791996-06-05 15:50:50 -05002520 /* finish row - updates counters and flushes zlib if last row */
2521 png_write_finish_row(png_ptr);
2522
2523#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2524 png_ptr->flush_rows++;
2525
2526 if (png_ptr->flush_dist > 0 &&
2527 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002528 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002529 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002530 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002531#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002532}