core: use the default font for the main splash message if not requested otherwise
[fbsplash.git] / core / libs / libpng-1.2.18 / pngwutil.c
blob314bea20d1c82c67fff725ce7a6dcc4759aa3271
2 /* pngwutil.c - utilities to write a PNG file
4 * Last changed in libpng 1.2.15 January 5, 2007
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2007 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 */
11 #define PNG_INTERNAL
12 #include "png.h"
13 #ifdef PNG_WRITE_SUPPORTED
15 /* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
19 void PNGAPI
20 png_save_uint_32(png_bytep buf, png_uint_32 i)
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
28 /* The png_save_int_32 function assumes integers are stored in two's
29 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
32 void PNGAPI
33 png_save_int_32(png_bytep buf, png_int_32 i)
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);
41 /* Place a 16-bit number into a buffer in PNG byte order.
42 * The parameter is declared unsigned int, not png_uint_16,
43 * just to avoid potential problems on pre-ANSI C compilers.
45 void PNGAPI
46 png_save_uint_16(png_bytep buf, unsigned int i)
48 buf[0] = (png_byte)((i >> 8) & 0xff);
49 buf[1] = (png_byte)(i & 0xff);
52 /* Write a PNG chunk all at once. The type is an array of ASCII characters
53 * representing the chunk name. The array must be at least 4 bytes in
54 * length, and does not need to be null terminated. To be safe, pass the
55 * pre-defined chunk names here, and if you need a new one, define it
56 * where the others are defined. The length is the length of the data.
57 * All the data must be present. If that is not possible, use the
58 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
59 * functions instead.
61 void PNGAPI
62 png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
63 png_bytep data, png_size_t length)
65 if(png_ptr == NULL) return;
66 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
67 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
71 /* Write the start of a PNG chunk. The type is the chunk type.
72 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
75 void PNGAPI
76 png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
79 png_byte buf[4];
80 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
81 if(png_ptr == NULL) return;
83 /* write the length */
84 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
87 /* write the chunk name */
88 png_write_data(png_ptr, chunk_name, (png_size_t)4);
89 /* reset the crc and run it over the chunk name */
90 png_reset_crc(png_ptr);
91 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
94 /* Write the data of a PNG chunk started with png_write_chunk_start().
95 * Note that multiple calls to this function are allowed, and that the
96 * sum of the lengths from these calls *must* add up to the total_length
97 * given to png_write_chunk_start().
99 void PNGAPI
100 png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
102 /* write the data, and run the CRC over it */
103 if(png_ptr == NULL) return;
104 if (data != NULL && length > 0)
106 png_calculate_crc(png_ptr, data, length);
107 png_write_data(png_ptr, data, length);
111 /* Finish a chunk started with png_write_chunk_start(). */
112 void PNGAPI
113 png_write_chunk_end(png_structp png_ptr)
115 png_byte buf[4];
117 if(png_ptr == NULL) return;
119 /* write the crc */
120 png_save_uint_32(buf, png_ptr->crc);
122 png_write_data(png_ptr, buf, (png_size_t)4);
125 /* Simple function to write the signature. If we have already written
126 * the magic bytes of the signature, or more likely, the PNG stream is
127 * being embedded into another stream and doesn't need its own signature,
128 * we should call png_set_sig_bytes() to tell libpng how many of the
129 * bytes have already been written.
131 void /* PRIVATE */
132 png_write_sig(png_structp png_ptr)
134 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
135 /* write the rest of the 8 byte signature */
136 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
137 (png_size_t)8 - png_ptr->sig_bytes);
138 if(png_ptr->sig_bytes < 3)
139 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
142 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
144 * This pair of functions encapsulates the operation of (a) compressing a
145 * text string, and (b) issuing it later as a series of chunk data writes.
146 * The compression_state structure is shared context for these functions
147 * set up by the caller in order to make the whole mess thread-safe.
150 typedef struct
152 char *input; /* the uncompressed input data */
153 int input_len; /* its length */
154 int num_output_ptr; /* number of output pointers used */
155 int max_output_ptr; /* size of output_ptr */
156 png_charpp output_ptr; /* array of pointers to output */
157 } compression_state;
159 /* compress given text into storage in the png_ptr structure */
160 static int /* PRIVATE */
161 png_text_compress(png_structp png_ptr,
162 png_charp text, png_size_t text_len, int compression,
163 compression_state *comp)
165 int ret;
167 comp->num_output_ptr = 0;
168 comp->max_output_ptr = 0;
169 comp->output_ptr = NULL;
170 comp->input = NULL;
171 comp->input_len = 0;
173 /* we may just want to pass the text right through */
174 if (compression == PNG_TEXT_COMPRESSION_NONE)
176 comp->input = text;
177 comp->input_len = text_len;
178 return((int)text_len);
181 if (compression >= PNG_TEXT_COMPRESSION_LAST)
183 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
184 char msg[50];
185 sprintf(msg, "Unknown compression type %d", compression);
186 png_warning(png_ptr, msg);
187 #else
188 png_warning(png_ptr, "Unknown compression type");
189 #endif
192 /* We can't write the chunk until we find out how much data we have,
193 * which means we need to run the compressor first and save the
194 * output. This shouldn't be a problem, as the vast majority of
195 * comments should be reasonable, but we will set up an array of
196 * malloc'd pointers to be sure.
198 * If we knew the application was well behaved, we could simplify this
199 * greatly by assuming we can always malloc an output buffer large
200 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
201 * and malloc this directly. The only time this would be a bad idea is
202 * if we can't malloc more than 64K and we have 64K of random input
203 * data, or if the input string is incredibly large (although this
204 * wouldn't cause a failure, just a slowdown due to swapping).
207 /* set up the compression buffers */
208 png_ptr->zstream.avail_in = (uInt)text_len;
209 png_ptr->zstream.next_in = (Bytef *)text;
210 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
211 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
213 /* this is the same compression loop as in png_write_row() */
216 /* compress the data */
217 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
218 if (ret != Z_OK)
220 /* error */
221 if (png_ptr->zstream.msg != NULL)
222 png_error(png_ptr, png_ptr->zstream.msg);
223 else
224 png_error(png_ptr, "zlib error");
226 /* check to see if we need more room */
227 if (!(png_ptr->zstream.avail_out))
229 /* make sure the output array has room */
230 if (comp->num_output_ptr >= comp->max_output_ptr)
232 int old_max;
234 old_max = comp->max_output_ptr;
235 comp->max_output_ptr = comp->num_output_ptr + 4;
236 if (comp->output_ptr != NULL)
238 png_charpp old_ptr;
240 old_ptr = comp->output_ptr;
241 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
242 (png_uint_32)(comp->max_output_ptr *
243 png_sizeof (png_charpp)));
244 png_memcpy(comp->output_ptr, old_ptr, old_max
245 * png_sizeof (png_charp));
246 png_free(png_ptr, old_ptr);
248 else
249 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
250 (png_uint_32)(comp->max_output_ptr *
251 png_sizeof (png_charp)));
254 /* save the data */
255 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
256 (png_uint_32)png_ptr->zbuf_size);
257 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
258 png_ptr->zbuf_size);
259 comp->num_output_ptr++;
261 /* and reset the buffer */
262 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
263 png_ptr->zstream.next_out = png_ptr->zbuf;
265 /* continue until we don't have any more to compress */
266 } while (png_ptr->zstream.avail_in);
268 /* finish the compression */
271 /* tell zlib we are finished */
272 ret = deflate(&png_ptr->zstream, Z_FINISH);
274 if (ret == Z_OK)
276 /* check to see if we need more room */
277 if (!(png_ptr->zstream.avail_out))
279 /* check to make sure our output array has room */
280 if (comp->num_output_ptr >= comp->max_output_ptr)
282 int old_max;
284 old_max = comp->max_output_ptr;
285 comp->max_output_ptr = comp->num_output_ptr + 4;
286 if (comp->output_ptr != NULL)
288 png_charpp old_ptr;
290 old_ptr = comp->output_ptr;
291 /* This could be optimized to realloc() */
292 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
293 (png_uint_32)(comp->max_output_ptr *
294 png_sizeof (png_charpp)));
295 png_memcpy(comp->output_ptr, old_ptr,
296 old_max * png_sizeof (png_charp));
297 png_free(png_ptr, old_ptr);
299 else
300 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
301 (png_uint_32)(comp->max_output_ptr *
302 png_sizeof (png_charp)));
305 /* save off the data */
306 comp->output_ptr[comp->num_output_ptr] =
307 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
308 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
309 png_ptr->zbuf_size);
310 comp->num_output_ptr++;
312 /* and reset the buffer pointers */
313 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
314 png_ptr->zstream.next_out = png_ptr->zbuf;
317 else if (ret != Z_STREAM_END)
319 /* we got an error */
320 if (png_ptr->zstream.msg != NULL)
321 png_error(png_ptr, png_ptr->zstream.msg);
322 else
323 png_error(png_ptr, "zlib error");
325 } while (ret != Z_STREAM_END);
327 /* text length is number of buffers plus last buffer */
328 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
329 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
330 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
332 return((int)text_len);
335 /* ship the compressed text out via chunk writes */
336 static void /* PRIVATE */
337 png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
339 int i;
341 /* handle the no-compression case */
342 if (comp->input)
344 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
345 (png_size_t)comp->input_len);
346 return;
349 /* write saved output buffers, if any */
350 for (i = 0; i < comp->num_output_ptr; i++)
352 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
353 png_ptr->zbuf_size);
354 png_free(png_ptr, comp->output_ptr[i]);
355 comp->output_ptr[i]=NULL;
357 if (comp->max_output_ptr != 0)
358 png_free(png_ptr, comp->output_ptr);
359 comp->output_ptr=NULL;
360 /* write anything left in zbuf */
361 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
362 png_write_chunk_data(png_ptr, png_ptr->zbuf,
363 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
365 /* reset zlib for another zTXt/iTXt or image data */
366 deflateReset(&png_ptr->zstream);
367 png_ptr->zstream.data_type = Z_BINARY;
369 #endif
371 /* Write the IHDR chunk, and update the png_struct with the necessary
372 * information. Note that the rest of this code depends upon this
373 * information being correct.
375 void /* PRIVATE */
376 png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
377 int bit_depth, int color_type, int compression_type, int filter_type,
378 int interlace_type)
380 #ifdef PNG_USE_LOCAL_ARRAYS
381 PNG_IHDR;
382 #endif
383 png_byte buf[13]; /* buffer to store the IHDR info */
385 png_debug(1, "in png_write_IHDR\n");
386 /* Check that we have valid input data from the application info */
387 switch (color_type)
389 case PNG_COLOR_TYPE_GRAY:
390 switch (bit_depth)
392 case 1:
393 case 2:
394 case 4:
395 case 8:
396 case 16: png_ptr->channels = 1; break;
397 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
399 break;
400 case PNG_COLOR_TYPE_RGB:
401 if (bit_depth != 8 && bit_depth != 16)
402 png_error(png_ptr, "Invalid bit depth for RGB image");
403 png_ptr->channels = 3;
404 break;
405 case PNG_COLOR_TYPE_PALETTE:
406 switch (bit_depth)
408 case 1:
409 case 2:
410 case 4:
411 case 8: png_ptr->channels = 1; break;
412 default: png_error(png_ptr, "Invalid bit depth for paletted image");
414 break;
415 case PNG_COLOR_TYPE_GRAY_ALPHA:
416 if (bit_depth != 8 && bit_depth != 16)
417 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
418 png_ptr->channels = 2;
419 break;
420 case PNG_COLOR_TYPE_RGB_ALPHA:
421 if (bit_depth != 8 && bit_depth != 16)
422 png_error(png_ptr, "Invalid bit depth for RGBA image");
423 png_ptr->channels = 4;
424 break;
425 default:
426 png_error(png_ptr, "Invalid image color type specified");
429 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
431 png_warning(png_ptr, "Invalid compression type specified");
432 compression_type = PNG_COMPRESSION_TYPE_BASE;
435 /* Write filter_method 64 (intrapixel differencing) only if
436 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
437 * 2. Libpng did not write a PNG signature (this filter_method is only
438 * used in PNG datastreams that are embedded in MNG datastreams) and
439 * 3. The application called png_permit_mng_features with a mask that
440 * included PNG_FLAG_MNG_FILTER_64 and
441 * 4. The filter_method is 64 and
442 * 5. The color_type is RGB or RGBA
444 if (
445 #if defined(PNG_MNG_FEATURES_SUPPORTED)
446 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
447 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
448 (color_type == PNG_COLOR_TYPE_RGB ||
449 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
450 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
451 #endif
452 filter_type != PNG_FILTER_TYPE_BASE)
454 png_warning(png_ptr, "Invalid filter type specified");
455 filter_type = PNG_FILTER_TYPE_BASE;
458 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
459 if (interlace_type != PNG_INTERLACE_NONE &&
460 interlace_type != PNG_INTERLACE_ADAM7)
462 png_warning(png_ptr, "Invalid interlace type specified");
463 interlace_type = PNG_INTERLACE_ADAM7;
465 #else
466 interlace_type=PNG_INTERLACE_NONE;
467 #endif
469 /* save off the relevent information */
470 png_ptr->bit_depth = (png_byte)bit_depth;
471 png_ptr->color_type = (png_byte)color_type;
472 png_ptr->interlaced = (png_byte)interlace_type;
473 #if defined(PNG_MNG_FEATURES_SUPPORTED)
474 png_ptr->filter_type = (png_byte)filter_type;
475 #endif
476 png_ptr->compression_type = (png_byte)compression_type;
477 png_ptr->width = width;
478 png_ptr->height = height;
480 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
481 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
482 /* set the usr info, so any transformations can modify it */
483 png_ptr->usr_width = png_ptr->width;
484 png_ptr->usr_bit_depth = png_ptr->bit_depth;
485 png_ptr->usr_channels = png_ptr->channels;
487 /* pack the header information into the buffer */
488 png_save_uint_32(buf, width);
489 png_save_uint_32(buf + 4, height);
490 buf[8] = (png_byte)bit_depth;
491 buf[9] = (png_byte)color_type;
492 buf[10] = (png_byte)compression_type;
493 buf[11] = (png_byte)filter_type;
494 buf[12] = (png_byte)interlace_type;
496 /* write the chunk */
497 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
499 /* initialize zlib with PNG info */
500 png_ptr->zstream.zalloc = png_zalloc;
501 png_ptr->zstream.zfree = png_zfree;
502 png_ptr->zstream.opaque = (voidpf)png_ptr;
503 if (!(png_ptr->do_filter))
505 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
506 png_ptr->bit_depth < 8)
507 png_ptr->do_filter = PNG_FILTER_NONE;
508 else
509 png_ptr->do_filter = PNG_ALL_FILTERS;
511 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
513 if (png_ptr->do_filter != PNG_FILTER_NONE)
514 png_ptr->zlib_strategy = Z_FILTERED;
515 else
516 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
518 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
519 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
520 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
521 png_ptr->zlib_mem_level = 8;
522 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
523 png_ptr->zlib_window_bits = 15;
524 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
525 png_ptr->zlib_method = 8;
526 if (deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
527 png_ptr->zlib_method, png_ptr->zlib_window_bits,
528 png_ptr->zlib_mem_level, png_ptr->zlib_strategy) != Z_OK)
529 png_error(png_ptr, "zlib failed to initialize compressor");
530 png_ptr->zstream.next_out = png_ptr->zbuf;
531 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
532 /* libpng is not interested in zstream.data_type */
533 /* set it to a predefined value, to avoid its evaluation inside zlib */
534 png_ptr->zstream.data_type = Z_BINARY;
536 png_ptr->mode = PNG_HAVE_IHDR;
539 /* write the palette. We are careful not to trust png_color to be in the
540 * correct order for PNG, so people can redefine it to any convenient
541 * structure.
543 void /* PRIVATE */
544 png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
546 #ifdef PNG_USE_LOCAL_ARRAYS
547 PNG_PLTE;
548 #endif
549 png_uint_32 i;
550 png_colorp pal_ptr;
551 png_byte buf[3];
553 png_debug(1, "in png_write_PLTE\n");
554 if ((
555 #if defined(PNG_MNG_FEATURES_SUPPORTED)
556 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
557 #endif
558 num_pal == 0) || num_pal > 256)
560 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
562 png_error(png_ptr, "Invalid number of colors in palette");
564 else
566 png_warning(png_ptr, "Invalid number of colors in palette");
567 return;
571 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
573 png_warning(png_ptr,
574 "Ignoring request to write a PLTE chunk in grayscale PNG");
575 return;
578 png_ptr->num_palette = (png_uint_16)num_pal;
579 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
581 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
582 #ifndef PNG_NO_POINTER_INDEXING
583 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
585 buf[0] = pal_ptr->red;
586 buf[1] = pal_ptr->green;
587 buf[2] = pal_ptr->blue;
588 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
590 #else
591 /* This is a little slower but some buggy compilers need to do this instead */
592 pal_ptr=palette;
593 for (i = 0; i < num_pal; i++)
595 buf[0] = pal_ptr[i].red;
596 buf[1] = pal_ptr[i].green;
597 buf[2] = pal_ptr[i].blue;
598 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
600 #endif
601 png_write_chunk_end(png_ptr);
602 png_ptr->mode |= PNG_HAVE_PLTE;
605 /* write an IDAT chunk */
606 void /* PRIVATE */
607 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
609 #ifdef PNG_USE_LOCAL_ARRAYS
610 PNG_IDAT;
611 #endif
612 png_debug(1, "in png_write_IDAT\n");
614 /* Optimize the CMF field in the zlib stream. */
615 /* This hack of the zlib stream is compliant to the stream specification. */
616 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
617 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
619 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
620 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
622 /* Avoid memory underflows and multiplication overflows. */
623 /* The conditions below are practically always satisfied;
624 however, they still must be checked. */
625 if (length >= 2 &&
626 png_ptr->height < 16384 && png_ptr->width < 16384)
628 png_uint_32 uncompressed_idat_size = png_ptr->height *
629 ((png_ptr->width *
630 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
631 unsigned int z_cinfo = z_cmf >> 4;
632 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
633 while (uncompressed_idat_size <= half_z_window_size &&
634 half_z_window_size >= 256)
636 z_cinfo--;
637 half_z_window_size >>= 1;
639 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
640 if (data[0] != (png_byte)z_cmf)
642 data[0] = (png_byte)z_cmf;
643 data[1] &= 0xe0;
644 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
648 else
649 png_error(png_ptr,
650 "Invalid zlib compression method or flags in IDAT");
653 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
654 png_ptr->mode |= PNG_HAVE_IDAT;
657 /* write an IEND chunk */
658 void /* PRIVATE */
659 png_write_IEND(png_structp png_ptr)
661 #ifdef PNG_USE_LOCAL_ARRAYS
662 PNG_IEND;
663 #endif
664 png_debug(1, "in png_write_IEND\n");
665 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
666 (png_size_t)0);
667 png_ptr->mode |= PNG_HAVE_IEND;
670 #if defined(PNG_WRITE_gAMA_SUPPORTED)
671 /* write a gAMA chunk */
672 #ifdef PNG_FLOATING_POINT_SUPPORTED
673 void /* PRIVATE */
674 png_write_gAMA(png_structp png_ptr, double file_gamma)
676 #ifdef PNG_USE_LOCAL_ARRAYS
677 PNG_gAMA;
678 #endif
679 png_uint_32 igamma;
680 png_byte buf[4];
682 png_debug(1, "in png_write_gAMA\n");
683 /* file_gamma is saved in 1/100,000ths */
684 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
685 png_save_uint_32(buf, igamma);
686 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
688 #endif
689 #ifdef PNG_FIXED_POINT_SUPPORTED
690 void /* PRIVATE */
691 png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
693 #ifdef PNG_USE_LOCAL_ARRAYS
694 PNG_gAMA;
695 #endif
696 png_byte buf[4];
698 png_debug(1, "in png_write_gAMA\n");
699 /* file_gamma is saved in 1/100,000ths */
700 png_save_uint_32(buf, (png_uint_32)file_gamma);
701 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
703 #endif
704 #endif
706 #if defined(PNG_WRITE_sRGB_SUPPORTED)
707 /* write a sRGB chunk */
708 void /* PRIVATE */
709 png_write_sRGB(png_structp png_ptr, int srgb_intent)
711 #ifdef PNG_USE_LOCAL_ARRAYS
712 PNG_sRGB;
713 #endif
714 png_byte buf[1];
716 png_debug(1, "in png_write_sRGB\n");
717 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
718 png_warning(png_ptr,
719 "Invalid sRGB rendering intent specified");
720 buf[0]=(png_byte)srgb_intent;
721 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
723 #endif
725 #if defined(PNG_WRITE_iCCP_SUPPORTED)
726 /* write an iCCP chunk */
727 void /* PRIVATE */
728 png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
729 png_charp profile, int profile_len)
731 #ifdef PNG_USE_LOCAL_ARRAYS
732 PNG_iCCP;
733 #endif
734 png_size_t name_len;
735 png_charp new_name;
736 compression_state comp;
737 int embedded_profile_len = 0;
739 png_debug(1, "in png_write_iCCP\n");
741 comp.num_output_ptr = 0;
742 comp.max_output_ptr = 0;
743 comp.output_ptr = NULL;
744 comp.input = NULL;
745 comp.input_len = 0;
747 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
748 &new_name)) == 0)
750 png_warning(png_ptr, "Empty keyword in iCCP chunk");
751 return;
754 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
755 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
757 if (profile == NULL)
758 profile_len = 0;
760 if (profile_len > 3)
761 embedded_profile_len =
762 ((*( (png_bytep)profile ))<<24) |
763 ((*( (png_bytep)profile+1))<<16) |
764 ((*( (png_bytep)profile+2))<< 8) |
765 ((*( (png_bytep)profile+3)) );
767 if (profile_len < embedded_profile_len)
769 png_warning(png_ptr,
770 "Embedded profile length too large in iCCP chunk");
771 return;
774 if (profile_len > embedded_profile_len)
776 png_warning(png_ptr,
777 "Truncating profile to actual length in iCCP chunk");
778 profile_len = embedded_profile_len;
781 if (profile_len)
782 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
783 PNG_COMPRESSION_TYPE_BASE, &comp);
785 /* make sure we include the NULL after the name and the compression type */
786 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
787 (png_uint_32)name_len+profile_len+2);
788 new_name[name_len+1]=0x00;
789 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
791 if (profile_len)
792 png_write_compressed_data_out(png_ptr, &comp);
794 png_write_chunk_end(png_ptr);
795 png_free(png_ptr, new_name);
797 #endif
799 #if defined(PNG_WRITE_sPLT_SUPPORTED)
800 /* write a sPLT chunk */
801 void /* PRIVATE */
802 png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
804 #ifdef PNG_USE_LOCAL_ARRAYS
805 PNG_sPLT;
806 #endif
807 png_size_t name_len;
808 png_charp new_name;
809 png_byte entrybuf[10];
810 int entry_size = (spalette->depth == 8 ? 6 : 10);
811 int palette_size = entry_size * spalette->nentries;
812 png_sPLT_entryp ep;
813 #ifdef PNG_NO_POINTER_INDEXING
814 int i;
815 #endif
817 png_debug(1, "in png_write_sPLT\n");
818 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
819 spalette->name, &new_name))==0)
821 png_warning(png_ptr, "Empty keyword in sPLT chunk");
822 return;
825 /* make sure we include the NULL after the name */
826 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
827 (png_uint_32)(name_len + 2 + palette_size));
828 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
829 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
831 /* loop through each palette entry, writing appropriately */
832 #ifndef PNG_NO_POINTER_INDEXING
833 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
835 if (spalette->depth == 8)
837 entrybuf[0] = (png_byte)ep->red;
838 entrybuf[1] = (png_byte)ep->green;
839 entrybuf[2] = (png_byte)ep->blue;
840 entrybuf[3] = (png_byte)ep->alpha;
841 png_save_uint_16(entrybuf + 4, ep->frequency);
843 else
845 png_save_uint_16(entrybuf + 0, ep->red);
846 png_save_uint_16(entrybuf + 2, ep->green);
847 png_save_uint_16(entrybuf + 4, ep->blue);
848 png_save_uint_16(entrybuf + 6, ep->alpha);
849 png_save_uint_16(entrybuf + 8, ep->frequency);
851 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
853 #else
854 ep=spalette->entries;
855 for (i=0; i>spalette->nentries; i++)
857 if (spalette->depth == 8)
859 entrybuf[0] = (png_byte)ep[i].red;
860 entrybuf[1] = (png_byte)ep[i].green;
861 entrybuf[2] = (png_byte)ep[i].blue;
862 entrybuf[3] = (png_byte)ep[i].alpha;
863 png_save_uint_16(entrybuf + 4, ep[i].frequency);
865 else
867 png_save_uint_16(entrybuf + 0, ep[i].red);
868 png_save_uint_16(entrybuf + 2, ep[i].green);
869 png_save_uint_16(entrybuf + 4, ep[i].blue);
870 png_save_uint_16(entrybuf + 6, ep[i].alpha);
871 png_save_uint_16(entrybuf + 8, ep[i].frequency);
873 png_write_chunk_data(png_ptr, entrybuf, entry_size);
875 #endif
877 png_write_chunk_end(png_ptr);
878 png_free(png_ptr, new_name);
880 #endif
882 #if defined(PNG_WRITE_sBIT_SUPPORTED)
883 /* write the sBIT chunk */
884 void /* PRIVATE */
885 png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
887 #ifdef PNG_USE_LOCAL_ARRAYS
888 PNG_sBIT;
889 #endif
890 png_byte buf[4];
891 png_size_t size;
893 png_debug(1, "in png_write_sBIT\n");
894 /* make sure we don't depend upon the order of PNG_COLOR_8 */
895 if (color_type & PNG_COLOR_MASK_COLOR)
897 png_byte maxbits;
899 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
900 png_ptr->usr_bit_depth);
901 if (sbit->red == 0 || sbit->red > maxbits ||
902 sbit->green == 0 || sbit->green > maxbits ||
903 sbit->blue == 0 || sbit->blue > maxbits)
905 png_warning(png_ptr, "Invalid sBIT depth specified");
906 return;
908 buf[0] = sbit->red;
909 buf[1] = sbit->green;
910 buf[2] = sbit->blue;
911 size = 3;
913 else
915 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
917 png_warning(png_ptr, "Invalid sBIT depth specified");
918 return;
920 buf[0] = sbit->gray;
921 size = 1;
924 if (color_type & PNG_COLOR_MASK_ALPHA)
926 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
928 png_warning(png_ptr, "Invalid sBIT depth specified");
929 return;
931 buf[size++] = sbit->alpha;
934 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
936 #endif
938 #if defined(PNG_WRITE_cHRM_SUPPORTED)
939 /* write the cHRM chunk */
940 #ifdef PNG_FLOATING_POINT_SUPPORTED
941 void /* PRIVATE */
942 png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
943 double red_x, double red_y, double green_x, double green_y,
944 double blue_x, double blue_y)
946 #ifdef PNG_USE_LOCAL_ARRAYS
947 PNG_cHRM;
948 #endif
949 png_byte buf[32];
950 png_uint_32 itemp;
952 png_debug(1, "in png_write_cHRM\n");
953 /* each value is saved in 1/100,000ths */
954 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
955 white_x + white_y > 1.0)
957 png_warning(png_ptr, "Invalid cHRM white point specified");
958 #if !defined(PNG_NO_CONSOLE_IO)
959 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
960 #endif
961 return;
963 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
964 png_save_uint_32(buf, itemp);
965 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
966 png_save_uint_32(buf + 4, itemp);
968 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
970 png_warning(png_ptr, "Invalid cHRM red point specified");
971 return;
973 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
974 png_save_uint_32(buf + 8, itemp);
975 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
976 png_save_uint_32(buf + 12, itemp);
978 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
980 png_warning(png_ptr, "Invalid cHRM green point specified");
981 return;
983 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
984 png_save_uint_32(buf + 16, itemp);
985 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
986 png_save_uint_32(buf + 20, itemp);
988 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
990 png_warning(png_ptr, "Invalid cHRM blue point specified");
991 return;
993 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
994 png_save_uint_32(buf + 24, itemp);
995 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
996 png_save_uint_32(buf + 28, itemp);
998 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1000 #endif
1001 #ifdef PNG_FIXED_POINT_SUPPORTED
1002 void /* PRIVATE */
1003 png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1004 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1005 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1006 png_fixed_point blue_y)
1008 #ifdef PNG_USE_LOCAL_ARRAYS
1009 PNG_cHRM;
1010 #endif
1011 png_byte buf[32];
1013 png_debug(1, "in png_write_cHRM\n");
1014 /* each value is saved in 1/100,000ths */
1015 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
1017 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
1018 #if !defined(PNG_NO_CONSOLE_IO)
1019 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
1020 #endif
1021 return;
1023 png_save_uint_32(buf, (png_uint_32)white_x);
1024 png_save_uint_32(buf + 4, (png_uint_32)white_y);
1026 if (red_x + red_y > 100000L)
1028 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
1029 return;
1031 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1032 png_save_uint_32(buf + 12, (png_uint_32)red_y);
1034 if (green_x + green_y > 100000L)
1036 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
1037 return;
1039 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1040 png_save_uint_32(buf + 20, (png_uint_32)green_y);
1042 if (blue_x + blue_y > 100000L)
1044 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
1045 return;
1047 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1048 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
1050 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1052 #endif
1053 #endif
1055 #if defined(PNG_WRITE_tRNS_SUPPORTED)
1056 /* write the tRNS chunk */
1057 void /* PRIVATE */
1058 png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
1059 int num_trans, int color_type)
1061 #ifdef PNG_USE_LOCAL_ARRAYS
1062 PNG_tRNS;
1063 #endif
1064 png_byte buf[6];
1066 png_debug(1, "in png_write_tRNS\n");
1067 if (color_type == PNG_COLOR_TYPE_PALETTE)
1069 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1071 png_warning(png_ptr,"Invalid number of transparent colors specified");
1072 return;
1074 /* write the chunk out as it is */
1075 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
1077 else if (color_type == PNG_COLOR_TYPE_GRAY)
1079 /* one 16 bit value */
1080 if(tran->gray >= (1 << png_ptr->bit_depth))
1082 png_warning(png_ptr,
1083 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1084 return;
1086 png_save_uint_16(buf, tran->gray);
1087 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
1089 else if (color_type == PNG_COLOR_TYPE_RGB)
1091 /* three 16 bit values */
1092 png_save_uint_16(buf, tran->red);
1093 png_save_uint_16(buf + 2, tran->green);
1094 png_save_uint_16(buf + 4, tran->blue);
1095 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1097 png_warning(png_ptr,
1098 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1099 return;
1101 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
1103 else
1105 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
1108 #endif
1110 #if defined(PNG_WRITE_bKGD_SUPPORTED)
1111 /* write the background chunk */
1112 void /* PRIVATE */
1113 png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
1115 #ifdef PNG_USE_LOCAL_ARRAYS
1116 PNG_bKGD;
1117 #endif
1118 png_byte buf[6];
1120 png_debug(1, "in png_write_bKGD\n");
1121 if (color_type == PNG_COLOR_TYPE_PALETTE)
1123 if (
1124 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1125 (png_ptr->num_palette ||
1126 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
1127 #endif
1128 back->index > png_ptr->num_palette)
1130 png_warning(png_ptr, "Invalid background palette index");
1131 return;
1133 buf[0] = back->index;
1134 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
1136 else if (color_type & PNG_COLOR_MASK_COLOR)
1138 png_save_uint_16(buf, back->red);
1139 png_save_uint_16(buf + 2, back->green);
1140 png_save_uint_16(buf + 4, back->blue);
1141 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1143 png_warning(png_ptr,
1144 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1145 return;
1147 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
1149 else
1151 if(back->gray >= (1 << png_ptr->bit_depth))
1153 png_warning(png_ptr,
1154 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1155 return;
1157 png_save_uint_16(buf, back->gray);
1158 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
1161 #endif
1163 #if defined(PNG_WRITE_hIST_SUPPORTED)
1164 /* write the histogram */
1165 void /* PRIVATE */
1166 png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
1168 #ifdef PNG_USE_LOCAL_ARRAYS
1169 PNG_hIST;
1170 #endif
1171 int i;
1172 png_byte buf[3];
1174 png_debug(1, "in png_write_hIST\n");
1175 if (num_hist > (int)png_ptr->num_palette)
1177 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1178 png_ptr->num_palette);
1179 png_warning(png_ptr, "Invalid number of histogram entries specified");
1180 return;
1183 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
1184 for (i = 0; i < num_hist; i++)
1186 png_save_uint_16(buf, hist[i]);
1187 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
1189 png_write_chunk_end(png_ptr);
1191 #endif
1193 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1194 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
1195 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1196 * and if invalid, correct the keyword rather than discarding the entire
1197 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1198 * length, forbids leading or trailing whitespace, multiple internal spaces,
1199 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
1201 * The new_key is allocated to hold the corrected keyword and must be freed
1202 * by the calling routine. This avoids problems with trying to write to
1203 * static keywords without having to have duplicate copies of the strings.
1205 png_size_t /* PRIVATE */
1206 png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
1208 png_size_t key_len;
1209 png_charp kp, dp;
1210 int kflag;
1211 int kwarn=0;
1213 png_debug(1, "in png_check_keyword\n");
1214 *new_key = NULL;
1216 if (key == NULL || (key_len = png_strlen(key)) == 0)
1218 png_warning(png_ptr, "zero length keyword");
1219 return ((png_size_t)0);
1222 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1224 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1225 if (*new_key == NULL)
1227 png_warning(png_ptr, "Out of memory while procesing keyword");
1228 return ((png_size_t)0);
1231 /* Replace non-printing characters with a blank and print a warning */
1232 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
1234 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
1236 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
1237 char msg[40];
1239 sprintf(msg, "invalid keyword character 0x%02X", *kp);
1240 png_warning(png_ptr, msg);
1241 #else
1242 png_warning(png_ptr, "invalid character in keyword");
1243 #endif
1244 *dp = ' ';
1246 else
1248 *dp = *kp;
1251 *dp = '\0';
1253 /* Remove any trailing white space. */
1254 kp = *new_key + key_len - 1;
1255 if (*kp == ' ')
1257 png_warning(png_ptr, "trailing spaces removed from keyword");
1259 while (*kp == ' ')
1261 *(kp--) = '\0';
1262 key_len--;
1266 /* Remove any leading white space. */
1267 kp = *new_key;
1268 if (*kp == ' ')
1270 png_warning(png_ptr, "leading spaces removed from keyword");
1272 while (*kp == ' ')
1274 kp++;
1275 key_len--;
1279 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
1281 /* Remove multiple internal spaces. */
1282 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
1284 if (*kp == ' ' && kflag == 0)
1286 *(dp++) = *kp;
1287 kflag = 1;
1289 else if (*kp == ' ')
1291 key_len--;
1292 kwarn=1;
1294 else
1296 *(dp++) = *kp;
1297 kflag = 0;
1300 *dp = '\0';
1301 if(kwarn)
1302 png_warning(png_ptr, "extra interior spaces removed from keyword");
1304 if (key_len == 0)
1306 png_free(png_ptr, *new_key);
1307 *new_key=NULL;
1308 png_warning(png_ptr, "Zero length keyword");
1311 if (key_len > 79)
1313 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
1314 new_key[79] = '\0';
1315 key_len = 79;
1318 return (key_len);
1320 #endif
1322 #if defined(PNG_WRITE_tEXt_SUPPORTED)
1323 /* write a tEXt chunk */
1324 void /* PRIVATE */
1325 png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
1326 png_size_t text_len)
1328 #ifdef PNG_USE_LOCAL_ARRAYS
1329 PNG_tEXt;
1330 #endif
1331 png_size_t key_len;
1332 png_charp new_key;
1334 png_debug(1, "in png_write_tEXt\n");
1335 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1337 png_warning(png_ptr, "Empty keyword in tEXt chunk");
1338 return;
1341 if (text == NULL || *text == '\0')
1342 text_len = 0;
1343 else
1344 text_len = png_strlen(text);
1346 /* make sure we include the 0 after the key */
1347 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
1349 * We leave it to the application to meet PNG-1.0 requirements on the
1350 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1351 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1352 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1354 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1355 if (text_len)
1356 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
1358 png_write_chunk_end(png_ptr);
1359 png_free(png_ptr, new_key);
1361 #endif
1363 #if defined(PNG_WRITE_zTXt_SUPPORTED)
1364 /* write a compressed text chunk */
1365 void /* PRIVATE */
1366 png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
1367 png_size_t text_len, int compression)
1369 #ifdef PNG_USE_LOCAL_ARRAYS
1370 PNG_zTXt;
1371 #endif
1372 png_size_t key_len;
1373 char buf[1];
1374 png_charp new_key;
1375 compression_state comp;
1377 png_debug(1, "in png_write_zTXt\n");
1379 comp.num_output_ptr = 0;
1380 comp.max_output_ptr = 0;
1381 comp.output_ptr = NULL;
1382 comp.input = NULL;
1383 comp.input_len = 0;
1385 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1387 png_warning(png_ptr, "Empty keyword in zTXt chunk");
1388 return;
1391 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1393 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1394 png_free(png_ptr, new_key);
1395 return;
1398 text_len = png_strlen(text);
1400 png_free(png_ptr, new_key);
1402 /* compute the compressed data; do it now for the length */
1403 text_len = png_text_compress(png_ptr, text, text_len, compression,
1404 &comp);
1406 /* write start of chunk */
1407 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1408 (key_len+text_len+2));
1409 /* write key */
1410 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
1411 buf[0] = (png_byte)compression;
1412 /* write compression */
1413 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
1414 /* write the compressed data */
1415 png_write_compressed_data_out(png_ptr, &comp);
1417 /* close the chunk */
1418 png_write_chunk_end(png_ptr);
1420 #endif
1422 #if defined(PNG_WRITE_iTXt_SUPPORTED)
1423 /* write an iTXt chunk */
1424 void /* PRIVATE */
1425 png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
1426 png_charp lang, png_charp lang_key, png_charp text)
1428 #ifdef PNG_USE_LOCAL_ARRAYS
1429 PNG_iTXt;
1430 #endif
1431 png_size_t lang_len, key_len, lang_key_len, text_len;
1432 png_charp new_lang, new_key;
1433 png_byte cbuf[2];
1434 compression_state comp;
1436 png_debug(1, "in png_write_iTXt\n");
1438 comp.num_output_ptr = 0;
1439 comp.max_output_ptr = 0;
1440 comp.output_ptr = NULL;
1441 comp.input = NULL;
1443 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1445 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1446 return;
1448 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
1450 png_warning(png_ptr, "Empty language field in iTXt chunk");
1451 new_lang = NULL;
1452 lang_len = 0;
1455 if (lang_key == NULL)
1456 lang_key_len = 0;
1457 else
1458 lang_key_len = png_strlen(lang_key);
1460 if (text == NULL)
1461 text_len = 0;
1462 else
1463 text_len = png_strlen(text);
1465 /* compute the compressed data; do it now for the length */
1466 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1467 &comp);
1470 /* make sure we include the compression flag, the compression byte,
1471 * and the NULs after the key, lang, and lang_key parts */
1473 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1474 (png_uint_32)(
1475 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1476 + key_len
1477 + lang_len
1478 + lang_key_len
1479 + text_len));
1482 * We leave it to the application to meet PNG-1.0 requirements on the
1483 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1484 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1485 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1487 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1489 /* set the compression flag */
1490 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1491 compression == PNG_TEXT_COMPRESSION_NONE)
1492 cbuf[0] = 0;
1493 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1494 cbuf[0] = 1;
1495 /* set the compression method */
1496 cbuf[1] = 0;
1497 png_write_chunk_data(png_ptr, cbuf, 2);
1499 cbuf[0] = 0;
1500 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1501 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
1502 png_write_compressed_data_out(png_ptr, &comp);
1504 png_write_chunk_end(png_ptr);
1505 png_free(png_ptr, new_key);
1506 if (new_lang)
1507 png_free(png_ptr, new_lang);
1509 #endif
1511 #if defined(PNG_WRITE_oFFs_SUPPORTED)
1512 /* write the oFFs chunk */
1513 void /* PRIVATE */
1514 png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
1515 int unit_type)
1517 #ifdef PNG_USE_LOCAL_ARRAYS
1518 PNG_oFFs;
1519 #endif
1520 png_byte buf[9];
1522 png_debug(1, "in png_write_oFFs\n");
1523 if (unit_type >= PNG_OFFSET_LAST)
1524 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1526 png_save_int_32(buf, x_offset);
1527 png_save_int_32(buf + 4, y_offset);
1528 buf[8] = (png_byte)unit_type;
1530 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
1532 #endif
1534 #if defined(PNG_WRITE_pCAL_SUPPORTED)
1535 /* write the pCAL chunk (described in the PNG extensions document) */
1536 void /* PRIVATE */
1537 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1538 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1540 #ifdef PNG_USE_LOCAL_ARRAYS
1541 PNG_pCAL;
1542 #endif
1543 png_size_t purpose_len, units_len, total_len;
1544 png_uint_32p params_len;
1545 png_byte buf[10];
1546 png_charp new_purpose;
1547 int i;
1549 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1550 if (type >= PNG_EQUATION_LAST)
1551 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1553 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1554 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
1555 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1556 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
1557 total_len = purpose_len + units_len + 10;
1559 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1560 *png_sizeof(png_uint_32)));
1562 /* Find the length of each parameter, making sure we don't count the
1563 null terminator for the last parameter. */
1564 for (i = 0; i < nparams; i++)
1566 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1567 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
1568 total_len += (png_size_t)params_len[i];
1571 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
1572 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
1573 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
1574 png_save_int_32(buf, X0);
1575 png_save_int_32(buf + 4, X1);
1576 buf[8] = (png_byte)type;
1577 buf[9] = (png_byte)nparams;
1578 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1579 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1581 png_free(png_ptr, new_purpose);
1583 for (i = 0; i < nparams; i++)
1585 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1586 (png_size_t)params_len[i]);
1589 png_free(png_ptr, params_len);
1590 png_write_chunk_end(png_ptr);
1592 #endif
1594 #if defined(PNG_WRITE_sCAL_SUPPORTED)
1595 /* write the sCAL chunk */
1596 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
1597 void /* PRIVATE */
1598 png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
1600 #ifdef PNG_USE_LOCAL_ARRAYS
1601 PNG_sCAL;
1602 #endif
1603 char buf[64];
1604 png_size_t total_len;
1606 png_debug(1, "in png_write_sCAL\n");
1608 buf[0] = (char)unit;
1609 #if defined(_WIN32_WCE)
1610 /* sprintf() function is not supported on WindowsCE */
1612 wchar_t wc_buf[32];
1613 size_t wc_len;
1614 swprintf(wc_buf, TEXT("%12.12e"), width);
1615 wc_len = wcslen(wc_buf);
1616 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
1617 total_len = wc_len + 2;
1618 swprintf(wc_buf, TEXT("%12.12e"), height);
1619 wc_len = wcslen(wc_buf);
1620 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
1621 NULL, NULL);
1622 total_len += wc_len;
1624 #else
1625 sprintf(buf + 1, "%12.12e", width);
1626 total_len = 1 + png_strlen(buf + 1) + 1;
1627 sprintf(buf + total_len, "%12.12e", height);
1628 total_len += png_strlen(buf + total_len);
1629 #endif
1631 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1632 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
1634 #else
1635 #ifdef PNG_FIXED_POINT_SUPPORTED
1636 void /* PRIVATE */
1637 png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
1638 png_charp height)
1640 #ifdef PNG_USE_LOCAL_ARRAYS
1641 PNG_sCAL;
1642 #endif
1643 png_byte buf[64];
1644 png_size_t wlen, hlen, total_len;
1646 png_debug(1, "in png_write_sCAL_s\n");
1648 wlen = png_strlen(width);
1649 hlen = png_strlen(height);
1650 total_len = wlen + hlen + 2;
1651 if (total_len > 64)
1653 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1654 return;
1657 buf[0] = (png_byte)unit;
1658 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1659 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
1661 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1662 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
1664 #endif
1665 #endif
1666 #endif
1668 #if defined(PNG_WRITE_pHYs_SUPPORTED)
1669 /* write the pHYs chunk */
1670 void /* PRIVATE */
1671 png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
1672 png_uint_32 y_pixels_per_unit,
1673 int unit_type)
1675 #ifdef PNG_USE_LOCAL_ARRAYS
1676 PNG_pHYs;
1677 #endif
1678 png_byte buf[9];
1680 png_debug(1, "in png_write_pHYs\n");
1681 if (unit_type >= PNG_RESOLUTION_LAST)
1682 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1684 png_save_uint_32(buf, x_pixels_per_unit);
1685 png_save_uint_32(buf + 4, y_pixels_per_unit);
1686 buf[8] = (png_byte)unit_type;
1688 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
1690 #endif
1692 #if defined(PNG_WRITE_tIME_SUPPORTED)
1693 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
1694 * or png_convert_from_time_t(), or fill in the structure yourself.
1696 void /* PRIVATE */
1697 png_write_tIME(png_structp png_ptr, png_timep mod_time)
1699 #ifdef PNG_USE_LOCAL_ARRAYS
1700 PNG_tIME;
1701 #endif
1702 png_byte buf[7];
1704 png_debug(1, "in png_write_tIME\n");
1705 if (mod_time->month > 12 || mod_time->month < 1 ||
1706 mod_time->day > 31 || mod_time->day < 1 ||
1707 mod_time->hour > 23 || mod_time->second > 60)
1709 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1710 return;
1713 png_save_uint_16(buf, mod_time->year);
1714 buf[2] = mod_time->month;
1715 buf[3] = mod_time->day;
1716 buf[4] = mod_time->hour;
1717 buf[5] = mod_time->minute;
1718 buf[6] = mod_time->second;
1720 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
1722 #endif
1724 /* initializes the row writing capability of libpng */
1725 void /* PRIVATE */
1726 png_write_start_row(png_structp png_ptr)
1728 #ifdef PNG_USE_LOCAL_ARRAYS
1729 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1731 /* start of interlace block */
1732 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1734 /* offset to next interlace block */
1735 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1737 /* start of interlace block in the y direction */
1738 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1740 /* offset to next interlace block in the y direction */
1741 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1742 #endif
1744 png_size_t buf_size;
1746 png_debug(1, "in png_write_start_row\n");
1747 buf_size = (png_size_t)(PNG_ROWBYTES(
1748 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
1750 /* set up row buffer */
1751 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1752 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1754 /* set up filtering buffer, if using this filter */
1755 if (png_ptr->do_filter & PNG_FILTER_SUB)
1757 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1758 (png_ptr->rowbytes + 1));
1759 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1762 /* We only need to keep the previous row if we are using one of these. */
1763 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1765 /* set up previous row buffer */
1766 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1767 png_memset(png_ptr->prev_row, 0, buf_size);
1769 if (png_ptr->do_filter & PNG_FILTER_UP)
1771 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
1772 (png_ptr->rowbytes + 1));
1773 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1776 if (png_ptr->do_filter & PNG_FILTER_AVG)
1778 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1779 (png_ptr->rowbytes + 1));
1780 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1783 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1785 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
1786 (png_ptr->rowbytes + 1));
1787 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1791 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1792 /* if interlaced, we need to set up width and height of pass */
1793 if (png_ptr->interlaced)
1795 if (!(png_ptr->transformations & PNG_INTERLACE))
1797 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1798 png_pass_ystart[0]) / png_pass_yinc[0];
1799 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1800 png_pass_start[0]) / png_pass_inc[0];
1802 else
1804 png_ptr->num_rows = png_ptr->height;
1805 png_ptr->usr_width = png_ptr->width;
1808 else
1809 #endif
1811 png_ptr->num_rows = png_ptr->height;
1812 png_ptr->usr_width = png_ptr->width;
1814 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1815 png_ptr->zstream.next_out = png_ptr->zbuf;
1818 /* Internal use only. Called when finished processing a row of data. */
1819 void /* PRIVATE */
1820 png_write_finish_row(png_structp png_ptr)
1822 #ifdef PNG_USE_LOCAL_ARRAYS
1823 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1825 /* start of interlace block */
1826 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1828 /* offset to next interlace block */
1829 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1831 /* start of interlace block in the y direction */
1832 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1834 /* offset to next interlace block in the y direction */
1835 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1836 #endif
1838 int ret;
1840 png_debug(1, "in png_write_finish_row\n");
1841 /* next row */
1842 png_ptr->row_number++;
1844 /* see if we are done */
1845 if (png_ptr->row_number < png_ptr->num_rows)
1846 return;
1848 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1849 /* if interlaced, go to next pass */
1850 if (png_ptr->interlaced)
1852 png_ptr->row_number = 0;
1853 if (png_ptr->transformations & PNG_INTERLACE)
1855 png_ptr->pass++;
1857 else
1859 /* loop until we find a non-zero width or height pass */
1862 png_ptr->pass++;
1863 if (png_ptr->pass >= 7)
1864 break;
1865 png_ptr->usr_width = (png_ptr->width +
1866 png_pass_inc[png_ptr->pass] - 1 -
1867 png_pass_start[png_ptr->pass]) /
1868 png_pass_inc[png_ptr->pass];
1869 png_ptr->num_rows = (png_ptr->height +
1870 png_pass_yinc[png_ptr->pass] - 1 -
1871 png_pass_ystart[png_ptr->pass]) /
1872 png_pass_yinc[png_ptr->pass];
1873 if (png_ptr->transformations & PNG_INTERLACE)
1874 break;
1875 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1879 /* reset the row above the image for the next pass */
1880 if (png_ptr->pass < 7)
1882 if (png_ptr->prev_row != NULL)
1883 png_memset(png_ptr->prev_row, 0,
1884 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1885 png_ptr->usr_bit_depth,png_ptr->width))+1);
1886 return;
1889 #endif
1891 /* if we get here, we've just written the last row, so we need
1892 to flush the compressor */
1895 /* tell the compressor we are done */
1896 ret = deflate(&png_ptr->zstream, Z_FINISH);
1897 /* check for an error */
1898 if (ret == Z_OK)
1900 /* check to see if we need more room */
1901 if (!(png_ptr->zstream.avail_out))
1903 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1904 png_ptr->zstream.next_out = png_ptr->zbuf;
1905 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1908 else if (ret != Z_STREAM_END)
1910 if (png_ptr->zstream.msg != NULL)
1911 png_error(png_ptr, png_ptr->zstream.msg);
1912 else
1913 png_error(png_ptr, "zlib error");
1915 } while (ret != Z_STREAM_END);
1917 /* write any extra space */
1918 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
1920 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
1921 png_ptr->zstream.avail_out);
1924 deflateReset(&png_ptr->zstream);
1925 png_ptr->zstream.data_type = Z_BINARY;
1928 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
1929 /* Pick out the correct pixels for the interlace pass.
1930 * The basic idea here is to go through the row with a source
1931 * pointer and a destination pointer (sp and dp), and copy the
1932 * correct pixels for the pass. As the row gets compacted,
1933 * sp will always be >= dp, so we should never overwrite anything.
1934 * See the default: case for the easiest code to understand.
1936 void /* PRIVATE */
1937 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
1939 #ifdef PNG_USE_LOCAL_ARRAYS
1940 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1942 /* start of interlace block */
1943 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1945 /* offset to next interlace block */
1946 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1947 #endif
1949 png_debug(1, "in png_do_write_interlace\n");
1950 /* we don't have to do anything on the last pass (6) */
1951 #if defined(PNG_USELESS_TESTS_SUPPORTED)
1952 if (row != NULL && row_info != NULL && pass < 6)
1953 #else
1954 if (pass < 6)
1955 #endif
1957 /* each pixel depth is handled separately */
1958 switch (row_info->pixel_depth)
1960 case 1:
1962 png_bytep sp;
1963 png_bytep dp;
1964 int shift;
1965 int d;
1966 int value;
1967 png_uint_32 i;
1968 png_uint_32 row_width = row_info->width;
1970 dp = row;
1971 d = 0;
1972 shift = 7;
1973 for (i = png_pass_start[pass]; i < row_width;
1974 i += png_pass_inc[pass])
1976 sp = row + (png_size_t)(i >> 3);
1977 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
1978 d |= (value << shift);
1980 if (shift == 0)
1982 shift = 7;
1983 *dp++ = (png_byte)d;
1984 d = 0;
1986 else
1987 shift--;
1990 if (shift != 7)
1991 *dp = (png_byte)d;
1992 break;
1994 case 2:
1996 png_bytep sp;
1997 png_bytep dp;
1998 int shift;
1999 int d;
2000 int value;
2001 png_uint_32 i;
2002 png_uint_32 row_width = row_info->width;
2004 dp = row;
2005 shift = 6;
2006 d = 0;
2007 for (i = png_pass_start[pass]; i < row_width;
2008 i += png_pass_inc[pass])
2010 sp = row + (png_size_t)(i >> 2);
2011 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
2012 d |= (value << shift);
2014 if (shift == 0)
2016 shift = 6;
2017 *dp++ = (png_byte)d;
2018 d = 0;
2020 else
2021 shift -= 2;
2023 if (shift != 6)
2024 *dp = (png_byte)d;
2025 break;
2027 case 4:
2029 png_bytep sp;
2030 png_bytep dp;
2031 int shift;
2032 int d;
2033 int value;
2034 png_uint_32 i;
2035 png_uint_32 row_width = row_info->width;
2037 dp = row;
2038 shift = 4;
2039 d = 0;
2040 for (i = png_pass_start[pass]; i < row_width;
2041 i += png_pass_inc[pass])
2043 sp = row + (png_size_t)(i >> 1);
2044 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
2045 d |= (value << shift);
2047 if (shift == 0)
2049 shift = 4;
2050 *dp++ = (png_byte)d;
2051 d = 0;
2053 else
2054 shift -= 4;
2056 if (shift != 4)
2057 *dp = (png_byte)d;
2058 break;
2060 default:
2062 png_bytep sp;
2063 png_bytep dp;
2064 png_uint_32 i;
2065 png_uint_32 row_width = row_info->width;
2066 png_size_t pixel_bytes;
2068 /* start at the beginning */
2069 dp = row;
2070 /* find out how many bytes each pixel takes up */
2071 pixel_bytes = (row_info->pixel_depth >> 3);
2072 /* loop through the row, only looking at the pixels that
2073 matter */
2074 for (i = png_pass_start[pass]; i < row_width;
2075 i += png_pass_inc[pass])
2077 /* find out where the original pixel is */
2078 sp = row + (png_size_t)i * pixel_bytes;
2079 /* move the pixel */
2080 if (dp != sp)
2081 png_memcpy(dp, sp, pixel_bytes);
2082 /* next pixel */
2083 dp += pixel_bytes;
2085 break;
2088 /* set new row width */
2089 row_info->width = (row_info->width +
2090 png_pass_inc[pass] - 1 -
2091 png_pass_start[pass]) /
2092 png_pass_inc[pass];
2093 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2094 row_info->width);
2097 #endif
2099 /* This filters the row, chooses which filter to use, if it has not already
2100 * been specified by the application, and then writes the row out with the
2101 * chosen filter.
2103 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
2104 #define PNG_HISHIFT 10
2105 #define PNG_LOMASK ((png_uint_32)0xffffL)
2106 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2107 void /* PRIVATE */
2108 png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
2110 png_bytep prev_row, best_row, row_buf;
2111 png_uint_32 mins, bpp;
2112 png_byte filter_to_do = png_ptr->do_filter;
2113 png_uint_32 row_bytes = row_info->rowbytes;
2114 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2115 int num_p_filters = (int)png_ptr->num_prev_filters;
2116 #endif
2118 png_debug(1, "in png_write_find_filter\n");
2119 /* find out how many bytes offset each pixel is */
2120 bpp = (row_info->pixel_depth + 7) >> 3;
2122 prev_row = png_ptr->prev_row;
2123 best_row = row_buf = png_ptr->row_buf;
2124 mins = PNG_MAXSUM;
2126 /* The prediction method we use is to find which method provides the
2127 * smallest value when summing the absolute values of the distances
2128 * from zero, using anything >= 128 as negative numbers. This is known
2129 * as the "minimum sum of absolute differences" heuristic. Other
2130 * heuristics are the "weighted minimum sum of absolute differences"
2131 * (experimental and can in theory improve compression), and the "zlib
2132 * predictive" method (not implemented yet), which does test compressions
2133 * of lines using different filter methods, and then chooses the
2134 * (series of) filter(s) that give minimum compressed data size (VERY
2135 * computationally expensive).
2137 * GRR 980525: consider also
2138 * (1) minimum sum of absolute differences from running average (i.e.,
2139 * keep running sum of non-absolute differences & count of bytes)
2140 * [track dispersion, too? restart average if dispersion too large?]
2141 * (1b) minimum sum of absolute differences from sliding average, probably
2142 * with window size <= deflate window (usually 32K)
2143 * (2) minimum sum of squared differences from zero or running average
2144 * (i.e., ~ root-mean-square approach)
2148 /* We don't need to test the 'no filter' case if this is the only filter
2149 * that has been chosen, as it doesn't actually do anything to the data.
2151 if ((filter_to_do & PNG_FILTER_NONE) &&
2152 filter_to_do != PNG_FILTER_NONE)
2154 png_bytep rp;
2155 png_uint_32 sum = 0;
2156 png_uint_32 i;
2157 int v;
2159 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2161 v = *rp;
2162 sum += (v < 128) ? v : 256 - v;
2165 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2166 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2168 png_uint_32 sumhi, sumlo;
2169 int j;
2170 sumlo = sum & PNG_LOMASK;
2171 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2173 /* Reduce the sum if we match any of the previous rows */
2174 for (j = 0; j < num_p_filters; j++)
2176 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2178 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2179 PNG_WEIGHT_SHIFT;
2180 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2181 PNG_WEIGHT_SHIFT;
2185 /* Factor in the cost of this filter (this is here for completeness,
2186 * but it makes no sense to have a "cost" for the NONE filter, as
2187 * it has the minimum possible computational cost - none).
2189 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2190 PNG_COST_SHIFT;
2191 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2192 PNG_COST_SHIFT;
2194 if (sumhi > PNG_HIMASK)
2195 sum = PNG_MAXSUM;
2196 else
2197 sum = (sumhi << PNG_HISHIFT) + sumlo;
2199 #endif
2200 mins = sum;
2203 /* sub filter */
2204 if (filter_to_do == PNG_FILTER_SUB)
2205 /* it's the only filter so no testing is needed */
2207 png_bytep rp, lp, dp;
2208 png_uint_32 i;
2209 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2210 i++, rp++, dp++)
2212 *dp = *rp;
2214 for (lp = row_buf + 1; i < row_bytes;
2215 i++, rp++, lp++, dp++)
2217 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2219 best_row = png_ptr->sub_row;
2222 else if (filter_to_do & PNG_FILTER_SUB)
2224 png_bytep rp, dp, lp;
2225 png_uint_32 sum = 0, lmins = mins;
2226 png_uint_32 i;
2227 int v;
2229 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2230 /* We temporarily increase the "minimum sum" by the factor we
2231 * would reduce the sum of this filter, so that we can do the
2232 * early exit comparison without scaling the sum each time.
2234 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2236 int j;
2237 png_uint_32 lmhi, lmlo;
2238 lmlo = lmins & PNG_LOMASK;
2239 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2241 for (j = 0; j < num_p_filters; j++)
2243 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2245 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2246 PNG_WEIGHT_SHIFT;
2247 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2248 PNG_WEIGHT_SHIFT;
2252 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2253 PNG_COST_SHIFT;
2254 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2255 PNG_COST_SHIFT;
2257 if (lmhi > PNG_HIMASK)
2258 lmins = PNG_MAXSUM;
2259 else
2260 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2262 #endif
2264 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2265 i++, rp++, dp++)
2267 v = *dp = *rp;
2269 sum += (v < 128) ? v : 256 - v;
2271 for (lp = row_buf + 1; i < row_bytes;
2272 i++, rp++, lp++, dp++)
2274 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2276 sum += (v < 128) ? v : 256 - v;
2278 if (sum > lmins) /* We are already worse, don't continue. */
2279 break;
2282 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2283 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2285 int j;
2286 png_uint_32 sumhi, sumlo;
2287 sumlo = sum & PNG_LOMASK;
2288 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2290 for (j = 0; j < num_p_filters; j++)
2292 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2294 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
2295 PNG_WEIGHT_SHIFT;
2296 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
2297 PNG_WEIGHT_SHIFT;
2301 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2302 PNG_COST_SHIFT;
2303 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2304 PNG_COST_SHIFT;
2306 if (sumhi > PNG_HIMASK)
2307 sum = PNG_MAXSUM;
2308 else
2309 sum = (sumhi << PNG_HISHIFT) + sumlo;
2311 #endif
2313 if (sum < mins)
2315 mins = sum;
2316 best_row = png_ptr->sub_row;
2320 /* up filter */
2321 if (filter_to_do == PNG_FILTER_UP)
2323 png_bytep rp, dp, pp;
2324 png_uint_32 i;
2326 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2327 pp = prev_row + 1; i < row_bytes;
2328 i++, rp++, pp++, dp++)
2330 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2332 best_row = png_ptr->up_row;
2335 else if (filter_to_do & PNG_FILTER_UP)
2337 png_bytep rp, dp, pp;
2338 png_uint_32 sum = 0, lmins = mins;
2339 png_uint_32 i;
2340 int v;
2343 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2344 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2346 int j;
2347 png_uint_32 lmhi, lmlo;
2348 lmlo = lmins & PNG_LOMASK;
2349 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2351 for (j = 0; j < num_p_filters; j++)
2353 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2355 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2356 PNG_WEIGHT_SHIFT;
2357 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2358 PNG_WEIGHT_SHIFT;
2362 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2363 PNG_COST_SHIFT;
2364 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2365 PNG_COST_SHIFT;
2367 if (lmhi > PNG_HIMASK)
2368 lmins = PNG_MAXSUM;
2369 else
2370 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2372 #endif
2374 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2375 pp = prev_row + 1; i < row_bytes; i++)
2377 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2379 sum += (v < 128) ? v : 256 - v;
2381 if (sum > lmins) /* We are already worse, don't continue. */
2382 break;
2385 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2386 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2388 int j;
2389 png_uint_32 sumhi, sumlo;
2390 sumlo = sum & PNG_LOMASK;
2391 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2393 for (j = 0; j < num_p_filters; j++)
2395 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2397 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2398 PNG_WEIGHT_SHIFT;
2399 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2400 PNG_WEIGHT_SHIFT;
2404 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2405 PNG_COST_SHIFT;
2406 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2407 PNG_COST_SHIFT;
2409 if (sumhi > PNG_HIMASK)
2410 sum = PNG_MAXSUM;
2411 else
2412 sum = (sumhi << PNG_HISHIFT) + sumlo;
2414 #endif
2416 if (sum < mins)
2418 mins = sum;
2419 best_row = png_ptr->up_row;
2423 /* avg filter */
2424 if (filter_to_do == PNG_FILTER_AVG)
2426 png_bytep rp, dp, pp, lp;
2427 png_uint_32 i;
2428 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2429 pp = prev_row + 1; i < bpp; i++)
2431 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2433 for (lp = row_buf + 1; i < row_bytes; i++)
2435 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2436 & 0xff);
2438 best_row = png_ptr->avg_row;
2441 else if (filter_to_do & PNG_FILTER_AVG)
2443 png_bytep rp, dp, pp, lp;
2444 png_uint_32 sum = 0, lmins = mins;
2445 png_uint_32 i;
2446 int v;
2448 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2449 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2451 int j;
2452 png_uint_32 lmhi, lmlo;
2453 lmlo = lmins & PNG_LOMASK;
2454 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2456 for (j = 0; j < num_p_filters; j++)
2458 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
2460 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2461 PNG_WEIGHT_SHIFT;
2462 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2463 PNG_WEIGHT_SHIFT;
2467 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2468 PNG_COST_SHIFT;
2469 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2470 PNG_COST_SHIFT;
2472 if (lmhi > PNG_HIMASK)
2473 lmins = PNG_MAXSUM;
2474 else
2475 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2477 #endif
2479 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2480 pp = prev_row + 1; i < bpp; i++)
2482 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2484 sum += (v < 128) ? v : 256 - v;
2486 for (lp = row_buf + 1; i < row_bytes; i++)
2488 v = *dp++ =
2489 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2491 sum += (v < 128) ? v : 256 - v;
2493 if (sum > lmins) /* We are already worse, don't continue. */
2494 break;
2497 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2498 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2500 int j;
2501 png_uint_32 sumhi, sumlo;
2502 sumlo = sum & PNG_LOMASK;
2503 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2505 for (j = 0; j < num_p_filters; j++)
2507 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2509 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2510 PNG_WEIGHT_SHIFT;
2511 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2512 PNG_WEIGHT_SHIFT;
2516 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2517 PNG_COST_SHIFT;
2518 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2519 PNG_COST_SHIFT;
2521 if (sumhi > PNG_HIMASK)
2522 sum = PNG_MAXSUM;
2523 else
2524 sum = (sumhi << PNG_HISHIFT) + sumlo;
2526 #endif
2528 if (sum < mins)
2530 mins = sum;
2531 best_row = png_ptr->avg_row;
2535 /* Paeth filter */
2536 if (filter_to_do == PNG_FILTER_PAETH)
2538 png_bytep rp, dp, pp, cp, lp;
2539 png_uint_32 i;
2540 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2541 pp = prev_row + 1; i < bpp; i++)
2543 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2546 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2548 int a, b, c, pa, pb, pc, p;
2550 b = *pp++;
2551 c = *cp++;
2552 a = *lp++;
2554 p = b - c;
2555 pc = a - c;
2557 #ifdef PNG_USE_ABS
2558 pa = abs(p);
2559 pb = abs(pc);
2560 pc = abs(p + pc);
2561 #else
2562 pa = p < 0 ? -p : p;
2563 pb = pc < 0 ? -pc : pc;
2564 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2565 #endif
2567 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2569 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2571 best_row = png_ptr->paeth_row;
2574 else if (filter_to_do & PNG_FILTER_PAETH)
2576 png_bytep rp, dp, pp, cp, lp;
2577 png_uint_32 sum = 0, lmins = mins;
2578 png_uint_32 i;
2579 int v;
2581 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2582 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2584 int j;
2585 png_uint_32 lmhi, lmlo;
2586 lmlo = lmins & PNG_LOMASK;
2587 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2589 for (j = 0; j < num_p_filters; j++)
2591 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2593 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2594 PNG_WEIGHT_SHIFT;
2595 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2596 PNG_WEIGHT_SHIFT;
2600 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2601 PNG_COST_SHIFT;
2602 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2603 PNG_COST_SHIFT;
2605 if (lmhi > PNG_HIMASK)
2606 lmins = PNG_MAXSUM;
2607 else
2608 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2610 #endif
2612 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2613 pp = prev_row + 1; i < bpp; i++)
2615 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2617 sum += (v < 128) ? v : 256 - v;
2620 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2622 int a, b, c, pa, pb, pc, p;
2624 b = *pp++;
2625 c = *cp++;
2626 a = *lp++;
2628 #ifndef PNG_SLOW_PAETH
2629 p = b - c;
2630 pc = a - c;
2631 #ifdef PNG_USE_ABS
2632 pa = abs(p);
2633 pb = abs(pc);
2634 pc = abs(p + pc);
2635 #else
2636 pa = p < 0 ? -p : p;
2637 pb = pc < 0 ? -pc : pc;
2638 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2639 #endif
2640 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2641 #else /* PNG_SLOW_PAETH */
2642 p = a + b - c;
2643 pa = abs(p - a);
2644 pb = abs(p - b);
2645 pc = abs(p - c);
2646 if (pa <= pb && pa <= pc)
2647 p = a;
2648 else if (pb <= pc)
2649 p = b;
2650 else
2651 p = c;
2652 #endif /* PNG_SLOW_PAETH */
2654 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2656 sum += (v < 128) ? v : 256 - v;
2658 if (sum > lmins) /* We are already worse, don't continue. */
2659 break;
2662 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2663 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2665 int j;
2666 png_uint_32 sumhi, sumlo;
2667 sumlo = sum & PNG_LOMASK;
2668 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2670 for (j = 0; j < num_p_filters; j++)
2672 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2674 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2675 PNG_WEIGHT_SHIFT;
2676 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2677 PNG_WEIGHT_SHIFT;
2681 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2682 PNG_COST_SHIFT;
2683 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2684 PNG_COST_SHIFT;
2686 if (sumhi > PNG_HIMASK)
2687 sum = PNG_MAXSUM;
2688 else
2689 sum = (sumhi << PNG_HISHIFT) + sumlo;
2691 #endif
2693 if (sum < mins)
2695 best_row = png_ptr->paeth_row;
2699 /* Do the actual writing of the filtered row data from the chosen filter. */
2701 png_write_filtered_row(png_ptr, best_row);
2703 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2704 /* Save the type of filter we picked this time for future calculations */
2705 if (png_ptr->num_prev_filters > 0)
2707 int j;
2708 for (j = 1; j < num_p_filters; j++)
2710 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2712 png_ptr->prev_filters[j] = best_row[0];
2714 #endif
2718 /* Do the actual writing of a previously filtered row. */
2719 void /* PRIVATE */
2720 png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2722 png_debug(1, "in png_write_filtered_row\n");
2723 png_debug1(2, "filter = %d\n", filtered_row[0]);
2724 /* set up the zlib input buffer */
2726 png_ptr->zstream.next_in = filtered_row;
2727 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
2728 /* repeat until we have compressed all the data */
2731 int ret; /* return of zlib */
2733 /* compress the data */
2734 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
2735 /* check for compression errors */
2736 if (ret != Z_OK)
2738 if (png_ptr->zstream.msg != NULL)
2739 png_error(png_ptr, png_ptr->zstream.msg);
2740 else
2741 png_error(png_ptr, "zlib error");
2744 /* see if it is time to write another IDAT */
2745 if (!(png_ptr->zstream.avail_out))
2747 /* write the IDAT and reset the zlib output buffer */
2748 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2749 png_ptr->zstream.next_out = png_ptr->zbuf;
2750 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2752 /* repeat until all data has been compressed */
2753 } while (png_ptr->zstream.avail_in);
2755 /* swap the current and previous rows */
2756 if (png_ptr->prev_row != NULL)
2758 png_bytep tptr;
2760 tptr = png_ptr->prev_row;
2761 png_ptr->prev_row = png_ptr->row_buf;
2762 png_ptr->row_buf = tptr;
2765 /* finish row - updates counters and flushes zlib if last row */
2766 png_write_finish_row(png_ptr);
2768 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
2769 png_ptr->flush_rows++;
2771 if (png_ptr->flush_dist > 0 &&
2772 png_ptr->flush_rows >= png_ptr->flush_dist)
2774 png_write_flush(png_ptr);
2776 #endif
2778 #endif /* PNG_WRITE_SUPPORTED */