2 /* pngwutil.c - utilities to write a PNG file
\r
4 * Last changed in libpng 1.2.30 [August 15, 2008]
\r
5 * For conditions of distribution and use, see copyright notice in png.h
\r
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
\r
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
\r
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
\r
11 #define PNG_INTERNAL
\r
13 #ifdef PNG_WRITE_SUPPORTED
\r
15 /* Place a 32-bit number into a buffer in PNG byte order. We work
\r
16 * with unsigned numbers for convenience, although one supported
\r
17 * ancillary chunk uses signed (two's complement) numbers.
\r
20 png_save_uint_32(png_bytep buf, png_uint_32 i)
\r
22 buf[0] = (png_byte)((i >> 24) & 0xff);
\r
23 buf[1] = (png_byte)((i >> 16) & 0xff);
\r
24 buf[2] = (png_byte)((i >> 8) & 0xff);
\r
25 buf[3] = (png_byte)(i & 0xff);
\r
28 /* The png_save_int_32 function assumes integers are stored in two's
\r
29 * complement format. If this isn't the case, then this routine needs to
\r
30 * be modified to write data in two's complement format.
\r
33 png_save_int_32(png_bytep buf, png_int_32 i)
\r
35 buf[0] = (png_byte)((i >> 24) & 0xff);
\r
36 buf[1] = (png_byte)((i >> 16) & 0xff);
\r
37 buf[2] = (png_byte)((i >> 8) & 0xff);
\r
38 buf[3] = (png_byte)(i & 0xff);
\r
41 /* Place a 16-bit number into a buffer in PNG byte order.
\r
42 * The parameter is declared unsigned int, not png_uint_16,
\r
43 * just to avoid potential problems on pre-ANSI C compilers.
\r
46 png_save_uint_16(png_bytep buf, unsigned int i)
\r
48 buf[0] = (png_byte)((i >> 8) & 0xff);
\r
49 buf[1] = (png_byte)(i & 0xff);
\r
52 /* Simple function to write the signature. If we have already written
\r
53 * the magic bytes of the signature, or more likely, the PNG stream is
\r
54 * being embedded into another stream and doesn't need its own signature,
\r
55 * we should call png_set_sig_bytes() to tell libpng how many of the
\r
56 * bytes have already been written.
\r
59 png_write_sig(png_structp png_ptr)
\r
61 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
\r
63 /* write the rest of the 8 byte signature */
\r
64 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
\r
65 (png_size_t)(8 - png_ptr->sig_bytes));
\r
66 if (png_ptr->sig_bytes < 3)
\r
67 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
\r
70 /* Write a PNG chunk all at once. The type is an array of ASCII characters
\r
71 * representing the chunk name. The array must be at least 4 bytes in
\r
72 * length, and does not need to be null terminated. To be safe, pass the
\r
73 * pre-defined chunk names here, and if you need a new one, define it
\r
74 * where the others are defined. The length is the length of the data.
\r
75 * All the data must be present. If that is not possible, use the
\r
76 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
\r
77 * functions instead.
\r
80 png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
\r
81 png_bytep data, png_size_t length)
\r
83 if (png_ptr == NULL) return;
\r
84 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
\r
85 png_write_chunk_data(png_ptr, data, (png_size_t)length);
\r
86 png_write_chunk_end(png_ptr);
\r
89 /* Write the start of a PNG chunk. The type is the chunk type.
\r
90 * The total_length is the sum of the lengths of all the data you will be
\r
91 * passing in png_write_chunk_data().
\r
94 png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
\r
99 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
\r
100 (unsigned long)length);
\r
101 if (png_ptr == NULL) return;
\r
103 /* write the length and the chunk name */
\r
104 png_save_uint_32(buf, length);
\r
105 png_memcpy(buf + 4, chunk_name, 4);
\r
106 png_write_data(png_ptr, buf, (png_size_t)8);
\r
107 /* put the chunk name into png_ptr->chunk_name */
\r
108 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
\r
109 /* reset the crc and run it over the chunk name */
\r
110 png_reset_crc(png_ptr);
\r
111 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
\r
114 /* Write the data of a PNG chunk started with png_write_chunk_start().
\r
115 * Note that multiple calls to this function are allowed, and that the
\r
116 * sum of the lengths from these calls *must* add up to the total_length
\r
117 * given to png_write_chunk_start().
\r
120 png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
\r
122 /* write the data, and run the CRC over it */
\r
123 if (png_ptr == NULL) return;
\r
124 if (data != NULL && length > 0)
\r
126 png_write_data(png_ptr, data, length);
\r
127 /* update the CRC after writing the data,
\r
128 * in case that the user I/O routine alters it.
\r
130 png_calculate_crc(png_ptr, data, length);
\r
134 /* Finish a chunk started with png_write_chunk_start(). */
\r
136 png_write_chunk_end(png_structp png_ptr)
\r
140 if (png_ptr == NULL) return;
\r
142 /* write the crc in a single operation */
\r
143 png_save_uint_32(buf, png_ptr->crc);
\r
145 png_write_data(png_ptr, buf, (png_size_t)4);
\r
148 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
\r
150 * This pair of functions encapsulates the operation of (a) compressing a
\r
151 * text string, and (b) issuing it later as a series of chunk data writes.
\r
152 * The compression_state structure is shared context for these functions
\r
153 * set up by the caller in order to make the whole mess thread-safe.
\r
158 char *input; /* the uncompressed input data */
\r
159 int input_len; /* its length */
\r
160 int num_output_ptr; /* number of output pointers used */
\r
161 int max_output_ptr; /* size of output_ptr */
\r
162 png_charpp output_ptr; /* array of pointers to output */
\r
163 } compression_state;
\r
165 /* compress given text into storage in the png_ptr structure */
\r
166 static int /* PRIVATE */
\r
167 png_text_compress(png_structp png_ptr,
\r
168 png_charp text, png_size_t text_len, int compression,
\r
169 compression_state *comp)
\r
173 comp->num_output_ptr = 0;
\r
174 comp->max_output_ptr = 0;
\r
175 comp->output_ptr = NULL;
\r
176 comp->input = NULL;
\r
177 comp->input_len = 0;
\r
179 /* we may just want to pass the text right through */
\r
180 if (compression == PNG_TEXT_COMPRESSION_NONE)
\r
182 comp->input = text;
\r
183 comp->input_len = text_len;
\r
184 return((int)text_len);
\r
187 if (compression >= PNG_TEXT_COMPRESSION_LAST)
\r
189 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
\r
191 png_snprintf(msg, 50, "Unknown compression type %d", compression);
\r
192 png_warning(png_ptr, msg);
\r
194 png_warning(png_ptr, "Unknown compression type");
\r
198 /* We can't write the chunk until we find out how much data we have,
\r
199 * which means we need to run the compressor first and save the
\r
200 * output. This shouldn't be a problem, as the vast majority of
\r
201 * comments should be reasonable, but we will set up an array of
\r
202 * malloc'd pointers to be sure.
\r
204 * If we knew the application was well behaved, we could simplify this
\r
205 * greatly by assuming we can always malloc an output buffer large
\r
206 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
\r
207 * and malloc this directly. The only time this would be a bad idea is
\r
208 * if we can't malloc more than 64K and we have 64K of random input
\r
209 * data, or if the input string is incredibly large (although this
\r
210 * wouldn't cause a failure, just a slowdown due to swapping).
\r
213 /* set up the compression buffers */
\r
214 png_ptr->zstream.avail_in = (uInt)text_len;
\r
215 png_ptr->zstream.next_in = (Bytef *)text;
\r
216 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
217 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
\r
219 /* this is the same compression loop as in png_write_row() */
\r
222 /* compress the data */
\r
223 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
\r
227 if (png_ptr->zstream.msg != NULL)
\r
228 png_error(png_ptr, png_ptr->zstream.msg);
\r
230 png_error(png_ptr, "zlib error");
\r
232 /* check to see if we need more room */
\r
233 if (!(png_ptr->zstream.avail_out))
\r
235 /* make sure the output array has room */
\r
236 if (comp->num_output_ptr >= comp->max_output_ptr)
\r
240 old_max = comp->max_output_ptr;
\r
241 comp->max_output_ptr = comp->num_output_ptr + 4;
\r
242 if (comp->output_ptr != NULL)
\r
244 png_charpp old_ptr;
\r
246 old_ptr = comp->output_ptr;
\r
247 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
\r
249 (comp->max_output_ptr * png_sizeof(png_charpp)));
\r
250 png_memcpy(comp->output_ptr, old_ptr, old_max
\r
251 * png_sizeof(png_charp));
\r
252 png_free(png_ptr, old_ptr);
\r
255 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
\r
257 (comp->max_output_ptr * png_sizeof(png_charp)));
\r
260 /* save the data */
\r
261 comp->output_ptr[comp->num_output_ptr] =
\r
262 (png_charp)png_malloc(png_ptr,
\r
263 (png_uint_32)png_ptr->zbuf_size);
\r
264 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
\r
265 png_ptr->zbuf_size);
\r
266 comp->num_output_ptr++;
\r
268 /* and reset the buffer */
\r
269 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
270 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
272 /* continue until we don't have any more to compress */
\r
273 } while (png_ptr->zstream.avail_in);
\r
275 /* finish the compression */
\r
278 /* tell zlib we are finished */
\r
279 ret = deflate(&png_ptr->zstream, Z_FINISH);
\r
283 /* check to see if we need more room */
\r
284 if (!(png_ptr->zstream.avail_out))
\r
286 /* check to make sure our output array has room */
\r
287 if (comp->num_output_ptr >= comp->max_output_ptr)
\r
291 old_max = comp->max_output_ptr;
\r
292 comp->max_output_ptr = comp->num_output_ptr + 4;
\r
293 if (comp->output_ptr != NULL)
\r
295 png_charpp old_ptr;
\r
297 old_ptr = comp->output_ptr;
\r
298 /* This could be optimized to realloc() */
\r
299 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
\r
300 (png_uint_32)(comp->max_output_ptr *
\r
301 png_sizeof(png_charp)));
\r
302 png_memcpy(comp->output_ptr, old_ptr,
\r
303 old_max * png_sizeof(png_charp));
\r
304 png_free(png_ptr, old_ptr);
\r
307 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
\r
308 (png_uint_32)(comp->max_output_ptr *
\r
309 png_sizeof(png_charp)));
\r
312 /* save off the data */
\r
313 comp->output_ptr[comp->num_output_ptr] =
\r
314 (png_charp)png_malloc(png_ptr,
\r
315 (png_uint_32)png_ptr->zbuf_size);
\r
316 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
\r
317 png_ptr->zbuf_size);
\r
318 comp->num_output_ptr++;
\r
320 /* and reset the buffer pointers */
\r
321 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
322 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
325 else if (ret != Z_STREAM_END)
\r
327 /* we got an error */
\r
328 if (png_ptr->zstream.msg != NULL)
\r
329 png_error(png_ptr, png_ptr->zstream.msg);
\r
331 png_error(png_ptr, "zlib error");
\r
333 } while (ret != Z_STREAM_END);
\r
335 /* text length is number of buffers plus last buffer */
\r
336 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
\r
337 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
\r
338 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
\r
340 return((int)text_len);
\r
343 /* ship the compressed text out via chunk writes */
\r
344 static void /* PRIVATE */
\r
345 png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
\r
349 /* handle the no-compression case */
\r
352 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
\r
353 (png_size_t)comp->input_len);
\r
357 /* write saved output buffers, if any */
\r
358 for (i = 0; i < comp->num_output_ptr; i++)
\r
360 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
\r
361 (png_size_t)png_ptr->zbuf_size);
\r
362 png_free(png_ptr, comp->output_ptr[i]);
\r
363 comp->output_ptr[i]=NULL;
\r
365 if (comp->max_output_ptr != 0)
\r
366 png_free(png_ptr, comp->output_ptr);
\r
367 comp->output_ptr=NULL;
\r
368 /* write anything left in zbuf */
\r
369 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
\r
370 png_write_chunk_data(png_ptr, png_ptr->zbuf,
\r
371 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
\r
373 /* reset zlib for another zTXt/iTXt or image data */
\r
374 deflateReset(&png_ptr->zstream);
\r
375 png_ptr->zstream.data_type = Z_BINARY;
\r
379 /* Write the IHDR chunk, and update the png_struct with the necessary
\r
380 * information. Note that the rest of this code depends upon this
\r
381 * information being correct.
\r
384 png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
\r
385 int bit_depth, int color_type, int compression_type, int filter_type,
\r
386 int interlace_type)
\r
388 #ifdef PNG_USE_LOCAL_ARRAYS
\r
393 png_byte buf[13]; /* buffer to store the IHDR info */
\r
395 png_debug(1, "in png_write_IHDR\n");
\r
396 /* Check that we have valid input data from the application info */
\r
397 switch (color_type)
\r
399 case PNG_COLOR_TYPE_GRAY:
\r
406 case 16: png_ptr->channels = 1; break;
\r
407 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
\r
410 case PNG_COLOR_TYPE_RGB:
\r
411 if (bit_depth != 8 && bit_depth != 16)
\r
412 png_error(png_ptr, "Invalid bit depth for RGB image");
\r
413 png_ptr->channels = 3;
\r
415 case PNG_COLOR_TYPE_PALETTE:
\r
421 case 8: png_ptr->channels = 1; break;
\r
422 default: png_error(png_ptr, "Invalid bit depth for paletted image");
\r
425 case PNG_COLOR_TYPE_GRAY_ALPHA:
\r
426 if (bit_depth != 8 && bit_depth != 16)
\r
427 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
\r
428 png_ptr->channels = 2;
\r
430 case PNG_COLOR_TYPE_RGB_ALPHA:
\r
431 if (bit_depth != 8 && bit_depth != 16)
\r
432 png_error(png_ptr, "Invalid bit depth for RGBA image");
\r
433 png_ptr->channels = 4;
\r
436 png_error(png_ptr, "Invalid image color type specified");
\r
439 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
\r
441 png_warning(png_ptr, "Invalid compression type specified");
\r
442 compression_type = PNG_COMPRESSION_TYPE_BASE;
\r
445 /* Write filter_method 64 (intrapixel differencing) only if
\r
446 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
\r
447 * 2. Libpng did not write a PNG signature (this filter_method is only
\r
448 * used in PNG datastreams that are embedded in MNG datastreams) and
\r
449 * 3. The application called png_permit_mng_features with a mask that
\r
450 * included PNG_FLAG_MNG_FILTER_64 and
\r
451 * 4. The filter_method is 64 and
\r
452 * 5. The color_type is RGB or RGBA
\r
455 #if defined(PNG_MNG_FEATURES_SUPPORTED)
\r
456 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
\r
457 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
\r
458 (color_type == PNG_COLOR_TYPE_RGB ||
\r
459 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
\r
460 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
\r
462 filter_type != PNG_FILTER_TYPE_BASE)
\r
464 png_warning(png_ptr, "Invalid filter type specified");
\r
465 filter_type = PNG_FILTER_TYPE_BASE;
\r
468 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
\r
469 if (interlace_type != PNG_INTERLACE_NONE &&
\r
470 interlace_type != PNG_INTERLACE_ADAM7)
\r
472 png_warning(png_ptr, "Invalid interlace type specified");
\r
473 interlace_type = PNG_INTERLACE_ADAM7;
\r
476 interlace_type=PNG_INTERLACE_NONE;
\r
479 /* save off the relevent information */
\r
480 png_ptr->bit_depth = (png_byte)bit_depth;
\r
481 png_ptr->color_type = (png_byte)color_type;
\r
482 png_ptr->interlaced = (png_byte)interlace_type;
\r
483 #if defined(PNG_MNG_FEATURES_SUPPORTED)
\r
484 png_ptr->filter_type = (png_byte)filter_type;
\r
486 png_ptr->compression_type = (png_byte)compression_type;
\r
487 png_ptr->width = width;
\r
488 png_ptr->height = height;
\r
490 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
\r
491 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
\r
492 /* set the usr info, so any transformations can modify it */
\r
493 png_ptr->usr_width = png_ptr->width;
\r
494 png_ptr->usr_bit_depth = png_ptr->bit_depth;
\r
495 png_ptr->usr_channels = png_ptr->channels;
\r
497 /* pack the header information into the buffer */
\r
498 png_save_uint_32(buf, width);
\r
499 png_save_uint_32(buf + 4, height);
\r
500 buf[8] = (png_byte)bit_depth;
\r
501 buf[9] = (png_byte)color_type;
\r
502 buf[10] = (png_byte)compression_type;
\r
503 buf[11] = (png_byte)filter_type;
\r
504 buf[12] = (png_byte)interlace_type;
\r
506 /* write the chunk */
\r
507 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
\r
509 /* initialize zlib with PNG info */
\r
510 png_ptr->zstream.zalloc = png_zalloc;
\r
511 png_ptr->zstream.zfree = png_zfree;
\r
512 png_ptr->zstream.opaque = (voidpf)png_ptr;
\r
513 if (!(png_ptr->do_filter))
\r
515 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
\r
516 png_ptr->bit_depth < 8)
\r
517 png_ptr->do_filter = PNG_FILTER_NONE;
\r
519 png_ptr->do_filter = PNG_ALL_FILTERS;
\r
521 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
\r
523 if (png_ptr->do_filter != PNG_FILTER_NONE)
\r
524 png_ptr->zlib_strategy = Z_FILTERED;
\r
526 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
\r
528 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
\r
529 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
\r
530 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
\r
531 png_ptr->zlib_mem_level = 8;
\r
532 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
\r
533 png_ptr->zlib_window_bits = 15;
\r
534 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
\r
535 png_ptr->zlib_method = 8;
\r
536 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
\r
537 png_ptr->zlib_method, png_ptr->zlib_window_bits,
\r
538 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
\r
541 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
\r
542 "zlib failed to initialize compressor -- version error");
\r
543 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
\r
544 "zlib failed to initialize compressor -- stream error");
\r
545 if (ret == Z_MEM_ERROR) png_error(png_ptr,
\r
546 "zlib failed to initialize compressor -- mem error");
\r
547 png_error(png_ptr, "zlib failed to initialize compressor");
\r
549 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
550 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
551 /* libpng is not interested in zstream.data_type */
\r
552 /* set it to a predefined value, to avoid its evaluation inside zlib */
\r
553 png_ptr->zstream.data_type = Z_BINARY;
\r
555 png_ptr->mode = PNG_HAVE_IHDR;
\r
558 /* write the palette. We are careful not to trust png_color to be in the
\r
559 * correct order for PNG, so people can redefine it to any convenient
\r
563 png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
\r
565 #ifdef PNG_USE_LOCAL_ARRAYS
\r
569 png_colorp pal_ptr;
\r
572 png_debug(1, "in png_write_PLTE\n");
\r
574 #if defined(PNG_MNG_FEATURES_SUPPORTED)
\r
575 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
\r
577 num_pal == 0) || num_pal > 256)
\r
579 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
\r
581 png_error(png_ptr, "Invalid number of colors in palette");
\r
585 png_warning(png_ptr, "Invalid number of colors in palette");
\r
590 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
\r
592 png_warning(png_ptr,
\r
593 "Ignoring request to write a PLTE chunk in grayscale PNG");
\r
597 png_ptr->num_palette = (png_uint_16)num_pal;
\r
598 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
\r
600 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
\r
601 (png_uint_32)(num_pal * 3));
\r
602 #ifndef PNG_NO_POINTER_INDEXING
\r
603 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
\r
605 buf[0] = pal_ptr->red;
\r
606 buf[1] = pal_ptr->green;
\r
607 buf[2] = pal_ptr->blue;
\r
608 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
\r
611 /* This is a little slower but some buggy compilers need to do this instead */
\r
613 for (i = 0; i < num_pal; i++)
\r
615 buf[0] = pal_ptr[i].red;
\r
616 buf[1] = pal_ptr[i].green;
\r
617 buf[2] = pal_ptr[i].blue;
\r
618 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
\r
621 png_write_chunk_end(png_ptr);
\r
622 png_ptr->mode |= PNG_HAVE_PLTE;
\r
625 /* write an IDAT chunk */
\r
627 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
\r
629 #ifdef PNG_USE_LOCAL_ARRAYS
\r
632 png_debug(1, "in png_write_IDAT\n");
\r
634 /* Optimize the CMF field in the zlib stream. */
\r
635 /* This hack of the zlib stream is compliant to the stream specification. */
\r
636 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
\r
637 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
\r
639 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
\r
640 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
\r
642 /* Avoid memory underflows and multiplication overflows. */
\r
643 /* The conditions below are practically always satisfied;
\r
644 however, they still must be checked. */
\r
646 png_ptr->height < 16384 && png_ptr->width < 16384)
\r
648 png_uint_32 uncompressed_idat_size = png_ptr->height *
\r
650 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
\r
651 unsigned int z_cinfo = z_cmf >> 4;
\r
652 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
\r
653 while (uncompressed_idat_size <= half_z_window_size &&
\r
654 half_z_window_size >= 256)
\r
657 half_z_window_size >>= 1;
\r
659 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
\r
660 if (data[0] != (png_byte)z_cmf)
\r
662 data[0] = (png_byte)z_cmf;
\r
664 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
\r
670 "Invalid zlib compression method or flags in IDAT");
\r
673 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
\r
674 png_ptr->mode |= PNG_HAVE_IDAT;
\r
677 /* write an IEND chunk */
\r
679 png_write_IEND(png_structp png_ptr)
\r
681 #ifdef PNG_USE_LOCAL_ARRAYS
\r
684 png_debug(1, "in png_write_IEND\n");
\r
685 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
\r
687 png_ptr->mode |= PNG_HAVE_IEND;
\r
690 #if defined(PNG_WRITE_gAMA_SUPPORTED)
\r
691 /* write a gAMA chunk */
\r
692 #ifdef PNG_FLOATING_POINT_SUPPORTED
\r
694 png_write_gAMA(png_structp png_ptr, double file_gamma)
\r
696 #ifdef PNG_USE_LOCAL_ARRAYS
\r
699 png_uint_32 igamma;
\r
702 png_debug(1, "in png_write_gAMA\n");
\r
703 /* file_gamma is saved in 1/100,000ths */
\r
704 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
\r
705 png_save_uint_32(buf, igamma);
\r
706 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
\r
709 #ifdef PNG_FIXED_POINT_SUPPORTED
\r
711 png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
\r
713 #ifdef PNG_USE_LOCAL_ARRAYS
\r
718 png_debug(1, "in png_write_gAMA\n");
\r
719 /* file_gamma is saved in 1/100,000ths */
\r
720 png_save_uint_32(buf, (png_uint_32)file_gamma);
\r
721 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
\r
726 #if defined(PNG_WRITE_sRGB_SUPPORTED)
\r
727 /* write a sRGB chunk */
\r
729 png_write_sRGB(png_structp png_ptr, int srgb_intent)
\r
731 #ifdef PNG_USE_LOCAL_ARRAYS
\r
736 png_debug(1, "in png_write_sRGB\n");
\r
737 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
\r
738 png_warning(png_ptr,
\r
739 "Invalid sRGB rendering intent specified");
\r
740 buf[0]=(png_byte)srgb_intent;
\r
741 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
\r
745 #if defined(PNG_WRITE_iCCP_SUPPORTED)
\r
746 /* write an iCCP chunk */
\r
748 png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
\r
749 png_charp profile, int profile_len)
\r
751 #ifdef PNG_USE_LOCAL_ARRAYS
\r
754 png_size_t name_len;
\r
755 png_charp new_name;
\r
756 compression_state comp;
\r
757 int embedded_profile_len = 0;
\r
759 png_debug(1, "in png_write_iCCP\n");
\r
761 comp.num_output_ptr = 0;
\r
762 comp.max_output_ptr = 0;
\r
763 comp.output_ptr = NULL;
\r
765 comp.input_len = 0;
\r
767 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
\r
770 png_warning(png_ptr, "Empty keyword in iCCP chunk");
\r
774 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
\r
775 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
\r
777 if (profile == NULL)
\r
780 if (profile_len > 3)
\r
781 embedded_profile_len =
\r
782 ((*( (png_bytep)profile ))<<24) |
\r
783 ((*( (png_bytep)profile + 1))<<16) |
\r
784 ((*( (png_bytep)profile + 2))<< 8) |
\r
785 ((*( (png_bytep)profile + 3)) );
\r
787 if (profile_len < embedded_profile_len)
\r
789 png_warning(png_ptr,
\r
790 "Embedded profile length too large in iCCP chunk");
\r
794 if (profile_len > embedded_profile_len)
\r
796 png_warning(png_ptr,
\r
797 "Truncating profile to actual length in iCCP chunk");
\r
798 profile_len = embedded_profile_len;
\r
802 profile_len = png_text_compress(png_ptr, profile,
\r
803 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
\r
805 /* make sure we include the NULL after the name and the compression type */
\r
806 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
\r
807 (png_uint_32)(name_len + profile_len + 2));
\r
808 new_name[name_len + 1] = 0x00;
\r
809 png_write_chunk_data(png_ptr, (png_bytep)new_name,
\r
810 (png_size_t)(name_len + 2));
\r
813 png_write_compressed_data_out(png_ptr, &comp);
\r
815 png_write_chunk_end(png_ptr);
\r
816 png_free(png_ptr, new_name);
\r
820 #if defined(PNG_WRITE_sPLT_SUPPORTED)
\r
821 /* write a sPLT chunk */
\r
823 png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
\r
825 #ifdef PNG_USE_LOCAL_ARRAYS
\r
828 png_size_t name_len;
\r
829 png_charp new_name;
\r
830 png_byte entrybuf[10];
\r
831 int entry_size = (spalette->depth == 8 ? 6 : 10);
\r
832 int palette_size = entry_size * spalette->nentries;
\r
833 png_sPLT_entryp ep;
\r
834 #ifdef PNG_NO_POINTER_INDEXING
\r
838 png_debug(1, "in png_write_sPLT\n");
\r
839 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
\r
840 spalette->name, &new_name))==0)
\r
842 png_warning(png_ptr, "Empty keyword in sPLT chunk");
\r
846 /* make sure we include the NULL after the name */
\r
847 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
\r
848 (png_uint_32)(name_len + 2 + palette_size));
\r
849 png_write_chunk_data(png_ptr, (png_bytep)new_name,
\r
850 (png_size_t)(name_len + 1));
\r
851 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
\r
853 /* loop through each palette entry, writing appropriately */
\r
854 #ifndef PNG_NO_POINTER_INDEXING
\r
855 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
\r
857 if (spalette->depth == 8)
\r
859 entrybuf[0] = (png_byte)ep->red;
\r
860 entrybuf[1] = (png_byte)ep->green;
\r
861 entrybuf[2] = (png_byte)ep->blue;
\r
862 entrybuf[3] = (png_byte)ep->alpha;
\r
863 png_save_uint_16(entrybuf + 4, ep->frequency);
\r
867 png_save_uint_16(entrybuf + 0, ep->red);
\r
868 png_save_uint_16(entrybuf + 2, ep->green);
\r
869 png_save_uint_16(entrybuf + 4, ep->blue);
\r
870 png_save_uint_16(entrybuf + 6, ep->alpha);
\r
871 png_save_uint_16(entrybuf + 8, ep->frequency);
\r
873 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
\r
876 ep=spalette->entries;
\r
877 for (i=0; i>spalette->nentries; i++)
\r
879 if (spalette->depth == 8)
\r
881 entrybuf[0] = (png_byte)ep[i].red;
\r
882 entrybuf[1] = (png_byte)ep[i].green;
\r
883 entrybuf[2] = (png_byte)ep[i].blue;
\r
884 entrybuf[3] = (png_byte)ep[i].alpha;
\r
885 png_save_uint_16(entrybuf + 4, ep[i].frequency);
\r
889 png_save_uint_16(entrybuf + 0, ep[i].red);
\r
890 png_save_uint_16(entrybuf + 2, ep[i].green);
\r
891 png_save_uint_16(entrybuf + 4, ep[i].blue);
\r
892 png_save_uint_16(entrybuf + 6, ep[i].alpha);
\r
893 png_save_uint_16(entrybuf + 8, ep[i].frequency);
\r
895 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
\r
899 png_write_chunk_end(png_ptr);
\r
900 png_free(png_ptr, new_name);
\r
904 #if defined(PNG_WRITE_sBIT_SUPPORTED)
\r
905 /* write the sBIT chunk */
\r
907 png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
\r
909 #ifdef PNG_USE_LOCAL_ARRAYS
\r
915 png_debug(1, "in png_write_sBIT\n");
\r
916 /* make sure we don't depend upon the order of PNG_COLOR_8 */
\r
917 if (color_type & PNG_COLOR_MASK_COLOR)
\r
921 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
\r
922 png_ptr->usr_bit_depth);
\r
923 if (sbit->red == 0 || sbit->red > maxbits ||
\r
924 sbit->green == 0 || sbit->green > maxbits ||
\r
925 sbit->blue == 0 || sbit->blue > maxbits)
\r
927 png_warning(png_ptr, "Invalid sBIT depth specified");
\r
930 buf[0] = sbit->red;
\r
931 buf[1] = sbit->green;
\r
932 buf[2] = sbit->blue;
\r
937 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
\r
939 png_warning(png_ptr, "Invalid sBIT depth specified");
\r
942 buf[0] = sbit->gray;
\r
946 if (color_type & PNG_COLOR_MASK_ALPHA)
\r
948 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
\r
950 png_warning(png_ptr, "Invalid sBIT depth specified");
\r
953 buf[size++] = sbit->alpha;
\r
956 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
\r
960 #if defined(PNG_WRITE_cHRM_SUPPORTED)
\r
961 /* write the cHRM chunk */
\r
962 #ifdef PNG_FLOATING_POINT_SUPPORTED
\r
964 png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
\r
965 double red_x, double red_y, double green_x, double green_y,
\r
966 double blue_x, double blue_y)
\r
968 #ifdef PNG_USE_LOCAL_ARRAYS
\r
974 png_debug(1, "in png_write_cHRM\n");
\r
975 /* each value is saved in 1/100,000ths */
\r
976 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
\r
977 white_x + white_y > 1.0)
\r
979 png_warning(png_ptr, "Invalid cHRM white point specified");
\r
980 #if !defined(PNG_NO_CONSOLE_IO)
\r
981 fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y);
\r
985 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
\r
986 png_save_uint_32(buf, itemp);
\r
987 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
\r
988 png_save_uint_32(buf + 4, itemp);
\r
990 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
\r
992 png_warning(png_ptr, "Invalid cHRM red point specified");
\r
995 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
\r
996 png_save_uint_32(buf + 8, itemp);
\r
997 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
\r
998 png_save_uint_32(buf + 12, itemp);
\r
1000 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
\r
1002 png_warning(png_ptr, "Invalid cHRM green point specified");
\r
1005 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
\r
1006 png_save_uint_32(buf + 16, itemp);
\r
1007 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
\r
1008 png_save_uint_32(buf + 20, itemp);
\r
1010 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
\r
1012 png_warning(png_ptr, "Invalid cHRM blue point specified");
\r
1015 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
\r
1016 png_save_uint_32(buf + 24, itemp);
\r
1017 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
\r
1018 png_save_uint_32(buf + 28, itemp);
\r
1020 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
\r
1023 #ifdef PNG_FIXED_POINT_SUPPORTED
\r
1024 void /* PRIVATE */
\r
1025 png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
\r
1026 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
\r
1027 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
\r
1028 png_fixed_point blue_y)
\r
1030 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1035 png_debug(1, "in png_write_cHRM\n");
\r
1036 /* each value is saved in 1/100,000ths */
\r
1037 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
\r
1039 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
\r
1040 #if !defined(PNG_NO_CONSOLE_IO)
\r
1041 fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x,
\r
1042 (unsigned long)white_y);
\r
1046 png_save_uint_32(buf, (png_uint_32)white_x);
\r
1047 png_save_uint_32(buf + 4, (png_uint_32)white_y);
\r
1049 if (red_x + red_y > 100000L)
\r
1051 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
\r
1054 png_save_uint_32(buf + 8, (png_uint_32)red_x);
\r
1055 png_save_uint_32(buf + 12, (png_uint_32)red_y);
\r
1057 if (green_x + green_y > 100000L)
\r
1059 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
\r
1062 png_save_uint_32(buf + 16, (png_uint_32)green_x);
\r
1063 png_save_uint_32(buf + 20, (png_uint_32)green_y);
\r
1065 if (blue_x + blue_y > 100000L)
\r
1067 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
\r
1070 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
\r
1071 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
\r
1073 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
\r
1078 #if defined(PNG_WRITE_tRNS_SUPPORTED)
\r
1079 /* write the tRNS chunk */
\r
1080 void /* PRIVATE */
\r
1081 png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
\r
1082 int num_trans, int color_type)
\r
1084 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1089 png_debug(1, "in png_write_tRNS\n");
\r
1090 if (color_type == PNG_COLOR_TYPE_PALETTE)
\r
1092 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
\r
1094 png_warning(png_ptr, "Invalid number of transparent colors specified");
\r
1097 /* write the chunk out as it is */
\r
1098 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
\r
1099 (png_size_t)num_trans);
\r
1101 else if (color_type == PNG_COLOR_TYPE_GRAY)
\r
1103 /* one 16 bit value */
\r
1104 if (tran->gray >= (1 << png_ptr->bit_depth))
\r
1106 png_warning(png_ptr,
\r
1107 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
\r
1110 png_save_uint_16(buf, tran->gray);
\r
1111 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
\r
1113 else if (color_type == PNG_COLOR_TYPE_RGB)
\r
1115 /* three 16 bit values */
\r
1116 png_save_uint_16(buf, tran->red);
\r
1117 png_save_uint_16(buf + 2, tran->green);
\r
1118 png_save_uint_16(buf + 4, tran->blue);
\r
1119 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
\r
1121 png_warning(png_ptr,
\r
1122 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
\r
1125 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
\r
1129 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
\r
1134 #if defined(PNG_WRITE_bKGD_SUPPORTED)
\r
1135 /* write the background chunk */
\r
1136 void /* PRIVATE */
\r
1137 png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
\r
1139 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1144 png_debug(1, "in png_write_bKGD\n");
\r
1145 if (color_type == PNG_COLOR_TYPE_PALETTE)
\r
1148 #if defined(PNG_MNG_FEATURES_SUPPORTED)
\r
1149 (png_ptr->num_palette ||
\r
1150 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
\r
1152 back->index > png_ptr->num_palette)
\r
1154 png_warning(png_ptr, "Invalid background palette index");
\r
1157 buf[0] = back->index;
\r
1158 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
\r
1160 else if (color_type & PNG_COLOR_MASK_COLOR)
\r
1162 png_save_uint_16(buf, back->red);
\r
1163 png_save_uint_16(buf + 2, back->green);
\r
1164 png_save_uint_16(buf + 4, back->blue);
\r
1165 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
\r
1167 png_warning(png_ptr,
\r
1168 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
\r
1171 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
\r
1175 if (back->gray >= (1 << png_ptr->bit_depth))
\r
1177 png_warning(png_ptr,
\r
1178 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
\r
1181 png_save_uint_16(buf, back->gray);
\r
1182 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
\r
1187 #if defined(PNG_WRITE_hIST_SUPPORTED)
\r
1188 /* write the histogram */
\r
1189 void /* PRIVATE */
\r
1190 png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
\r
1192 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1198 png_debug(1, "in png_write_hIST\n");
\r
1199 if (num_hist > (int)png_ptr->num_palette)
\r
1201 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
\r
1202 png_ptr->num_palette);
\r
1203 png_warning(png_ptr, "Invalid number of histogram entries specified");
\r
1207 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
\r
1208 (png_uint_32)(num_hist * 2));
\r
1209 for (i = 0; i < num_hist; i++)
\r
1211 png_save_uint_16(buf, hist[i]);
\r
1212 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
\r
1214 png_write_chunk_end(png_ptr);
\r
1218 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
\r
1219 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
\r
1220 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
\r
1221 * and if invalid, correct the keyword rather than discarding the entire
\r
1222 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
\r
1223 * length, forbids leading or trailing whitespace, multiple internal spaces,
\r
1224 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
\r
1226 * The new_key is allocated to hold the corrected keyword and must be freed
\r
1227 * by the calling routine. This avoids problems with trying to write to
\r
1228 * static keywords without having to have duplicate copies of the strings.
\r
1230 png_size_t /* PRIVATE */
\r
1231 png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
\r
1233 png_size_t key_len;
\r
1238 png_debug(1, "in png_check_keyword\n");
\r
1241 if (key == NULL || (key_len = png_strlen(key)) == 0)
\r
1243 png_warning(png_ptr, "zero length keyword");
\r
1244 return ((png_size_t)0);
\r
1247 png_debug1(2, "Keyword to be checked is '%s'\n", key);
\r
1249 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
\r
1250 if (*new_key == NULL)
\r
1252 png_warning(png_ptr, "Out of memory while procesing keyword");
\r
1253 return ((png_size_t)0);
\r
1256 /* Replace non-printing characters with a blank and print a warning */
\r
1257 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
\r
1259 if ((png_byte)*kp < 0x20 ||
\r
1260 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
\r
1262 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
\r
1265 png_snprintf(msg, 40,
\r
1266 "invalid keyword character 0x%02X", (png_byte)*kp);
\r
1267 png_warning(png_ptr, msg);
\r
1269 png_warning(png_ptr, "invalid character in keyword");
\r
1280 /* Remove any trailing white space. */
\r
1281 kp = *new_key + key_len - 1;
\r
1284 png_warning(png_ptr, "trailing spaces removed from keyword");
\r
1286 while (*kp == ' ')
\r
1293 /* Remove any leading white space. */
\r
1297 png_warning(png_ptr, "leading spaces removed from keyword");
\r
1299 while (*kp == ' ')
\r
1306 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
\r
1308 /* Remove multiple internal spaces. */
\r
1309 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
\r
1311 if (*kp == ' ' && kflag == 0)
\r
1316 else if (*kp == ' ')
\r
1329 png_warning(png_ptr, "extra interior spaces removed from keyword");
\r
1333 png_free(png_ptr, *new_key);
\r
1335 png_warning(png_ptr, "Zero length keyword");
\r
1340 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
\r
1341 new_key[79] = '\0';
\r
1349 #if defined(PNG_WRITE_tEXt_SUPPORTED)
\r
1350 /* write a tEXt chunk */
\r
1351 void /* PRIVATE */
\r
1352 png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
\r
1353 png_size_t text_len)
\r
1355 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1358 png_size_t key_len;
\r
1359 png_charp new_key;
\r
1361 png_debug(1, "in png_write_tEXt\n");
\r
1362 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
\r
1364 png_warning(png_ptr, "Empty keyword in tEXt chunk");
\r
1368 if (text == NULL || *text == '\0')
\r
1371 text_len = png_strlen(text);
\r
1373 /* make sure we include the 0 after the key */
\r
1374 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
\r
1375 (png_uint_32)(key_len + text_len + 1));
\r
1377 * We leave it to the application to meet PNG-1.0 requirements on the
\r
1378 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
\r
1379 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
\r
1380 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
\r
1382 png_write_chunk_data(png_ptr, (png_bytep)new_key,
\r
1383 (png_size_t)(key_len + 1));
\r
1385 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
\r
1387 png_write_chunk_end(png_ptr);
\r
1388 png_free(png_ptr, new_key);
\r
1392 #if defined(PNG_WRITE_zTXt_SUPPORTED)
\r
1393 /* write a compressed text chunk */
\r
1394 void /* PRIVATE */
\r
1395 png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
\r
1396 png_size_t text_len, int compression)
\r
1398 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1401 png_size_t key_len;
\r
1403 png_charp new_key;
\r
1404 compression_state comp;
\r
1406 png_debug(1, "in png_write_zTXt\n");
\r
1408 comp.num_output_ptr = 0;
\r
1409 comp.max_output_ptr = 0;
\r
1410 comp.output_ptr = NULL;
\r
1411 comp.input = NULL;
\r
1412 comp.input_len = 0;
\r
1414 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
\r
1416 png_warning(png_ptr, "Empty keyword in zTXt chunk");
\r
1417 png_free(png_ptr, new_key);
\r
1421 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
\r
1423 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
\r
1424 png_free(png_ptr, new_key);
\r
1428 text_len = png_strlen(text);
\r
1430 /* compute the compressed data; do it now for the length */
\r
1431 text_len = png_text_compress(png_ptr, text, text_len, compression,
\r
1434 /* write start of chunk */
\r
1435 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
\r
1436 (png_uint_32)(key_len+text_len + 2));
\r
1438 png_write_chunk_data(png_ptr, (png_bytep)new_key,
\r
1439 (png_size_t)(key_len + 1));
\r
1440 png_free(png_ptr, new_key);
\r
1442 buf[0] = (png_byte)compression;
\r
1443 /* write compression */
\r
1444 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
\r
1445 /* write the compressed data */
\r
1446 png_write_compressed_data_out(png_ptr, &comp);
\r
1448 /* close the chunk */
\r
1449 png_write_chunk_end(png_ptr);
\r
1453 #if defined(PNG_WRITE_iTXt_SUPPORTED)
\r
1454 /* write an iTXt chunk */
\r
1455 void /* PRIVATE */
\r
1456 png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
\r
1457 png_charp lang, png_charp lang_key, png_charp text)
\r
1459 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1462 png_size_t lang_len, key_len, lang_key_len, text_len;
\r
1463 png_charp new_lang, new_key;
\r
1465 compression_state comp;
\r
1467 png_debug(1, "in png_write_iTXt\n");
\r
1469 comp.num_output_ptr = 0;
\r
1470 comp.max_output_ptr = 0;
\r
1471 comp.output_ptr = NULL;
\r
1472 comp.input = NULL;
\r
1474 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
\r
1476 png_warning(png_ptr, "Empty keyword in iTXt chunk");
\r
1479 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
\r
1481 png_warning(png_ptr, "Empty language field in iTXt chunk");
\r
1486 if (lang_key == NULL)
\r
1489 lang_key_len = png_strlen(lang_key);
\r
1494 text_len = png_strlen(text);
\r
1496 /* compute the compressed data; do it now for the length */
\r
1497 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
\r
1501 /* make sure we include the compression flag, the compression byte,
\r
1502 * and the NULs after the key, lang, and lang_key parts */
\r
1504 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
\r
1506 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
\r
1513 * We leave it to the application to meet PNG-1.0 requirements on the
\r
1514 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
\r
1515 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
\r
1516 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
\r
1518 png_write_chunk_data(png_ptr, (png_bytep)new_key,
\r
1519 (png_size_t)(key_len + 1));
\r
1521 /* set the compression flag */
\r
1522 if (compression == PNG_ITXT_COMPRESSION_NONE || \
\r
1523 compression == PNG_TEXT_COMPRESSION_NONE)
\r
1525 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
\r
1527 /* set the compression method */
\r
1529 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
\r
1532 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
\r
1533 (png_size_t)(lang_len + 1));
\r
1534 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
\r
1535 (png_size_t)(lang_key_len + 1));
\r
1536 png_write_compressed_data_out(png_ptr, &comp);
\r
1538 png_write_chunk_end(png_ptr);
\r
1539 png_free(png_ptr, new_key);
\r
1540 png_free(png_ptr, new_lang);
\r
1544 #if defined(PNG_WRITE_oFFs_SUPPORTED)
\r
1545 /* write the oFFs chunk */
\r
1546 void /* PRIVATE */
\r
1547 png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
\r
1550 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1555 png_debug(1, "in png_write_oFFs\n");
\r
1556 if (unit_type >= PNG_OFFSET_LAST)
\r
1557 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
\r
1559 png_save_int_32(buf, x_offset);
\r
1560 png_save_int_32(buf + 4, y_offset);
\r
1561 buf[8] = (png_byte)unit_type;
\r
1563 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
\r
1566 #if defined(PNG_WRITE_pCAL_SUPPORTED)
\r
1567 /* write the pCAL chunk (described in the PNG extensions document) */
\r
1568 void /* PRIVATE */
\r
1569 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
\r
1570 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
\r
1572 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1575 png_size_t purpose_len, units_len, total_len;
\r
1576 png_uint_32p params_len;
\r
1578 png_charp new_purpose;
\r
1581 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
\r
1582 if (type >= PNG_EQUATION_LAST)
\r
1583 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
\r
1585 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
\r
1586 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
\r
1587 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
\r
1588 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
\r
1589 total_len = purpose_len + units_len + 10;
\r
1591 params_len = (png_uint_32p)png_malloc(png_ptr,
\r
1592 (png_uint_32)(nparams * png_sizeof(png_uint_32)));
\r
1594 /* Find the length of each parameter, making sure we don't count the
\r
1595 null terminator for the last parameter. */
\r
1596 for (i = 0; i < nparams; i++)
\r
1598 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
\r
1599 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
\r
1600 (unsigned long) params_len[i]);
\r
1601 total_len += (png_size_t)params_len[i];
\r
1604 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
\r
1605 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
\r
1606 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
\r
1607 (png_size_t)purpose_len);
\r
1608 png_save_int_32(buf, X0);
\r
1609 png_save_int_32(buf + 4, X1);
\r
1610 buf[8] = (png_byte)type;
\r
1611 buf[9] = (png_byte)nparams;
\r
1612 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
\r
1613 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
\r
1615 png_free(png_ptr, new_purpose);
\r
1617 for (i = 0; i < nparams; i++)
\r
1619 png_write_chunk_data(png_ptr, (png_bytep)params[i],
\r
1620 (png_size_t)params_len[i]);
\r
1623 png_free(png_ptr, params_len);
\r
1624 png_write_chunk_end(png_ptr);
\r
1628 #if defined(PNG_WRITE_sCAL_SUPPORTED)
\r
1629 /* write the sCAL chunk */
\r
1630 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
\r
1631 void /* PRIVATE */
\r
1632 png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
\r
1634 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1638 png_size_t total_len;
\r
1640 png_debug(1, "in png_write_sCAL\n");
\r
1642 buf[0] = (char)unit;
\r
1643 #if defined(_WIN32_WCE)
\r
1644 /* sprintf() function is not supported on WindowsCE */
\r
1646 wchar_t wc_buf[32];
\r
1648 swprintf(wc_buf, TEXT("%12.12e"), width);
\r
1649 wc_len = wcslen(wc_buf);
\r
1650 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
\r
1651 total_len = wc_len + 2;
\r
1652 swprintf(wc_buf, TEXT("%12.12e"), height);
\r
1653 wc_len = wcslen(wc_buf);
\r
1654 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
\r
1656 total_len += wc_len;
\r
1659 png_snprintf(buf + 1, 63, "%12.12e", width);
\r
1660 total_len = 1 + png_strlen(buf + 1) + 1;
\r
1661 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
\r
1662 total_len += png_strlen(buf + total_len);
\r
1665 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
\r
1666 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
\r
1669 #ifdef PNG_FIXED_POINT_SUPPORTED
\r
1670 void /* PRIVATE */
\r
1671 png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
\r
1674 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1678 png_size_t wlen, hlen, total_len;
\r
1680 png_debug(1, "in png_write_sCAL_s\n");
\r
1682 wlen = png_strlen(width);
\r
1683 hlen = png_strlen(height);
\r
1684 total_len = wlen + hlen + 2;
\r
1685 if (total_len > 64)
\r
1687 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
\r
1691 buf[0] = (png_byte)unit;
\r
1692 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
\r
1693 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
\r
1695 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
\r
1696 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
\r
1702 #if defined(PNG_WRITE_pHYs_SUPPORTED)
\r
1703 /* write the pHYs chunk */
\r
1704 void /* PRIVATE */
\r
1705 png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
\r
1706 png_uint_32 y_pixels_per_unit,
\r
1709 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1714 png_debug(1, "in png_write_pHYs\n");
\r
1715 if (unit_type >= PNG_RESOLUTION_LAST)
\r
1716 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
\r
1718 png_save_uint_32(buf, x_pixels_per_unit);
\r
1719 png_save_uint_32(buf + 4, y_pixels_per_unit);
\r
1720 buf[8] = (png_byte)unit_type;
\r
1722 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
\r
1726 #if defined(PNG_WRITE_tIME_SUPPORTED)
\r
1727 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
\r
1728 * or png_convert_from_time_t(), or fill in the structure yourself.
\r
1730 void /* PRIVATE */
\r
1731 png_write_tIME(png_structp png_ptr, png_timep mod_time)
\r
1733 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1738 png_debug(1, "in png_write_tIME\n");
\r
1739 if (mod_time->month > 12 || mod_time->month < 1 ||
\r
1740 mod_time->day > 31 || mod_time->day < 1 ||
\r
1741 mod_time->hour > 23 || mod_time->second > 60)
\r
1743 png_warning(png_ptr, "Invalid time specified for tIME chunk");
\r
1747 png_save_uint_16(buf, mod_time->year);
\r
1748 buf[2] = mod_time->month;
\r
1749 buf[3] = mod_time->day;
\r
1750 buf[4] = mod_time->hour;
\r
1751 buf[5] = mod_time->minute;
\r
1752 buf[6] = mod_time->second;
\r
1754 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
\r
1758 /* initializes the row writing capability of libpng */
\r
1759 void /* PRIVATE */
\r
1760 png_write_start_row(png_structp png_ptr)
\r
1762 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
\r
1763 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1764 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
\r
1766 /* start of interlace block */
\r
1767 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
\r
1769 /* offset to next interlace block */
\r
1770 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
\r
1772 /* start of interlace block in the y direction */
\r
1773 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
\r
1775 /* offset to next interlace block in the y direction */
\r
1776 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
\r
1780 png_size_t buf_size;
\r
1782 png_debug(1, "in png_write_start_row\n");
\r
1783 buf_size = (png_size_t)(PNG_ROWBYTES(
\r
1784 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
\r
1786 /* set up row buffer */
\r
1787 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
\r
1788 (png_uint_32)buf_size);
\r
1789 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
\r
1791 #ifndef PNG_NO_WRITE_FILTER
\r
1792 /* set up filtering buffer, if using this filter */
\r
1793 if (png_ptr->do_filter & PNG_FILTER_SUB)
\r
1795 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
\r
1796 (png_uint_32)(png_ptr->rowbytes + 1));
\r
1797 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
\r
1800 /* We only need to keep the previous row if we are using one of these. */
\r
1801 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
\r
1803 /* set up previous row buffer */
\r
1804 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
\r
1805 (png_uint_32)buf_size);
\r
1806 png_memset(png_ptr->prev_row, 0, buf_size);
\r
1808 if (png_ptr->do_filter & PNG_FILTER_UP)
\r
1810 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
\r
1811 (png_uint_32)(png_ptr->rowbytes + 1));
\r
1812 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
\r
1815 if (png_ptr->do_filter & PNG_FILTER_AVG)
\r
1817 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
\r
1818 (png_uint_32)(png_ptr->rowbytes + 1));
\r
1819 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
\r
1822 if (png_ptr->do_filter & PNG_FILTER_PAETH)
\r
1824 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
\r
1825 (png_uint_32)(png_ptr->rowbytes + 1));
\r
1826 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
\r
1829 #endif /* PNG_NO_WRITE_FILTER */
\r
1831 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
\r
1832 /* if interlaced, we need to set up width and height of pass */
\r
1833 if (png_ptr->interlaced)
\r
1835 if (!(png_ptr->transformations & PNG_INTERLACE))
\r
1837 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
\r
1838 png_pass_ystart[0]) / png_pass_yinc[0];
\r
1839 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
\r
1840 png_pass_start[0]) / png_pass_inc[0];
\r
1844 png_ptr->num_rows = png_ptr->height;
\r
1845 png_ptr->usr_width = png_ptr->width;
\r
1851 png_ptr->num_rows = png_ptr->height;
\r
1852 png_ptr->usr_width = png_ptr->width;
\r
1854 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
1855 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
1858 /* Internal use only. Called when finished processing a row of data. */
\r
1859 void /* PRIVATE */
\r
1860 png_write_finish_row(png_structp png_ptr)
\r
1862 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
\r
1863 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1864 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
\r
1866 /* start of interlace block */
\r
1867 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
\r
1869 /* offset to next interlace block */
\r
1870 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
\r
1872 /* start of interlace block in the y direction */
\r
1873 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
\r
1875 /* offset to next interlace block in the y direction */
\r
1876 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
\r
1882 png_debug(1, "in png_write_finish_row\n");
\r
1884 png_ptr->row_number++;
\r
1886 /* see if we are done */
\r
1887 if (png_ptr->row_number < png_ptr->num_rows)
\r
1890 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
\r
1891 /* if interlaced, go to next pass */
\r
1892 if (png_ptr->interlaced)
\r
1894 png_ptr->row_number = 0;
\r
1895 if (png_ptr->transformations & PNG_INTERLACE)
\r
1901 /* loop until we find a non-zero width or height pass */
\r
1905 if (png_ptr->pass >= 7)
\r
1907 png_ptr->usr_width = (png_ptr->width +
\r
1908 png_pass_inc[png_ptr->pass] - 1 -
\r
1909 png_pass_start[png_ptr->pass]) /
\r
1910 png_pass_inc[png_ptr->pass];
\r
1911 png_ptr->num_rows = (png_ptr->height +
\r
1912 png_pass_yinc[png_ptr->pass] - 1 -
\r
1913 png_pass_ystart[png_ptr->pass]) /
\r
1914 png_pass_yinc[png_ptr->pass];
\r
1915 if (png_ptr->transformations & PNG_INTERLACE)
\r
1917 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
\r
1921 /* reset the row above the image for the next pass */
\r
1922 if (png_ptr->pass < 7)
\r
1924 if (png_ptr->prev_row != NULL)
\r
1925 png_memset(png_ptr->prev_row, 0,
\r
1926 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
\r
1927 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
\r
1933 /* if we get here, we've just written the last row, so we need
\r
1934 to flush the compressor */
\r
1937 /* tell the compressor we are done */
\r
1938 ret = deflate(&png_ptr->zstream, Z_FINISH);
\r
1939 /* check for an error */
\r
1942 /* check to see if we need more room */
\r
1943 if (!(png_ptr->zstream.avail_out))
\r
1945 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
\r
1946 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
1947 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
1950 else if (ret != Z_STREAM_END)
\r
1952 if (png_ptr->zstream.msg != NULL)
\r
1953 png_error(png_ptr, png_ptr->zstream.msg);
\r
1955 png_error(png_ptr, "zlib error");
\r
1957 } while (ret != Z_STREAM_END);
\r
1959 /* write any extra space */
\r
1960 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
\r
1962 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
\r
1963 png_ptr->zstream.avail_out);
\r
1966 deflateReset(&png_ptr->zstream);
\r
1967 png_ptr->zstream.data_type = Z_BINARY;
\r
1970 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
\r
1971 /* Pick out the correct pixels for the interlace pass.
\r
1972 * The basic idea here is to go through the row with a source
\r
1973 * pointer and a destination pointer (sp and dp), and copy the
\r
1974 * correct pixels for the pass. As the row gets compacted,
\r
1975 * sp will always be >= dp, so we should never overwrite anything.
\r
1976 * See the default: case for the easiest code to understand.
\r
1978 void /* PRIVATE */
\r
1979 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
\r
1981 #ifdef PNG_USE_LOCAL_ARRAYS
\r
1982 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
\r
1984 /* start of interlace block */
\r
1985 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
\r
1987 /* offset to next interlace block */
\r
1988 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
\r
1991 png_debug(1, "in png_do_write_interlace\n");
\r
1992 /* we don't have to do anything on the last pass (6) */
\r
1993 #if defined(PNG_USELESS_TESTS_SUPPORTED)
\r
1994 if (row != NULL && row_info != NULL && pass < 6)
\r
1999 /* each pixel depth is handled separately */
\r
2000 switch (row_info->pixel_depth)
\r
2010 png_uint_32 row_width = row_info->width;
\r
2015 for (i = png_pass_start[pass]; i < row_width;
\r
2016 i += png_pass_inc[pass])
\r
2018 sp = row + (png_size_t)(i >> 3);
\r
2019 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
\r
2020 d |= (value << shift);
\r
2025 *dp++ = (png_byte)d;
\r
2033 *dp = (png_byte)d;
\r
2044 png_uint_32 row_width = row_info->width;
\r
2049 for (i = png_pass_start[pass]; i < row_width;
\r
2050 i += png_pass_inc[pass])
\r
2052 sp = row + (png_size_t)(i >> 2);
\r
2053 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
\r
2054 d |= (value << shift);
\r
2059 *dp++ = (png_byte)d;
\r
2066 *dp = (png_byte)d;
\r
2077 png_uint_32 row_width = row_info->width;
\r
2082 for (i = png_pass_start[pass]; i < row_width;
\r
2083 i += png_pass_inc[pass])
\r
2085 sp = row + (png_size_t)(i >> 1);
\r
2086 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
\r
2087 d |= (value << shift);
\r
2092 *dp++ = (png_byte)d;
\r
2099 *dp = (png_byte)d;
\r
2107 png_uint_32 row_width = row_info->width;
\r
2108 png_size_t pixel_bytes;
\r
2110 /* start at the beginning */
\r
2112 /* find out how many bytes each pixel takes up */
\r
2113 pixel_bytes = (row_info->pixel_depth >> 3);
\r
2114 /* loop through the row, only looking at the pixels that
\r
2116 for (i = png_pass_start[pass]; i < row_width;
\r
2117 i += png_pass_inc[pass])
\r
2119 /* find out where the original pixel is */
\r
2120 sp = row + (png_size_t)i * pixel_bytes;
\r
2121 /* move the pixel */
\r
2123 png_memcpy(dp, sp, pixel_bytes);
\r
2125 dp += pixel_bytes;
\r
2130 /* set new row width */
\r
2131 row_info->width = (row_info->width +
\r
2132 png_pass_inc[pass] - 1 -
\r
2133 png_pass_start[pass]) /
\r
2134 png_pass_inc[pass];
\r
2135 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
\r
2141 /* This filters the row, chooses which filter to use, if it has not already
\r
2142 * been specified by the application, and then writes the row out with the
\r
2145 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
\r
2146 #define PNG_HISHIFT 10
\r
2147 #define PNG_LOMASK ((png_uint_32)0xffffL)
\r
2148 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
\r
2149 void /* PRIVATE */
\r
2150 png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
\r
2152 png_bytep best_row;
\r
2153 #ifndef PNG_NO_WRITE_FILTER
\r
2154 png_bytep prev_row, row_buf;
\r
2155 png_uint_32 mins, bpp;
\r
2156 png_byte filter_to_do = png_ptr->do_filter;
\r
2157 png_uint_32 row_bytes = row_info->rowbytes;
\r
2158 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2159 int num_p_filters = (int)png_ptr->num_prev_filters;
\r
2162 png_debug(1, "in png_write_find_filter\n");
\r
2163 /* find out how many bytes offset each pixel is */
\r
2164 bpp = (row_info->pixel_depth + 7) >> 3;
\r
2166 prev_row = png_ptr->prev_row;
\r
2168 best_row = png_ptr->row_buf;
\r
2169 #ifndef PNG_NO_WRITE_FILTER
\r
2170 row_buf = best_row;
\r
2171 mins = PNG_MAXSUM;
\r
2173 /* The prediction method we use is to find which method provides the
\r
2174 * smallest value when summing the absolute values of the distances
\r
2175 * from zero, using anything >= 128 as negative numbers. This is known
\r
2176 * as the "minimum sum of absolute differences" heuristic. Other
\r
2177 * heuristics are the "weighted minimum sum of absolute differences"
\r
2178 * (experimental and can in theory improve compression), and the "zlib
\r
2179 * predictive" method (not implemented yet), which does test compressions
\r
2180 * of lines using different filter methods, and then chooses the
\r
2181 * (series of) filter(s) that give minimum compressed data size (VERY
\r
2182 * computationally expensive).
\r
2184 * GRR 980525: consider also
\r
2185 * (1) minimum sum of absolute differences from running average (i.e.,
\r
2186 * keep running sum of non-absolute differences & count of bytes)
\r
2187 * [track dispersion, too? restart average if dispersion too large?]
\r
2188 * (1b) minimum sum of absolute differences from sliding average, probably
\r
2189 * with window size <= deflate window (usually 32K)
\r
2190 * (2) minimum sum of squared differences from zero or running average
\r
2191 * (i.e., ~ root-mean-square approach)
\r
2195 /* We don't need to test the 'no filter' case if this is the only filter
\r
2196 * that has been chosen, as it doesn't actually do anything to the data.
\r
2198 if ((filter_to_do & PNG_FILTER_NONE) &&
\r
2199 filter_to_do != PNG_FILTER_NONE)
\r
2202 png_uint_32 sum = 0;
\r
2206 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
\r
2209 sum += (v < 128) ? v : 256 - v;
\r
2212 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2213 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2215 png_uint_32 sumhi, sumlo;
\r
2217 sumlo = sum & PNG_LOMASK;
\r
2218 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
\r
2220 /* Reduce the sum if we match any of the previous rows */
\r
2221 for (j = 0; j < num_p_filters; j++)
\r
2223 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
\r
2225 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
\r
2227 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
\r
2232 /* Factor in the cost of this filter (this is here for completeness,
\r
2233 * but it makes no sense to have a "cost" for the NONE filter, as
\r
2234 * it has the minimum possible computational cost - none).
\r
2236 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
\r
2238 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
\r
2241 if (sumhi > PNG_HIMASK)
\r
2244 sum = (sumhi << PNG_HISHIFT) + sumlo;
\r
2251 if (filter_to_do == PNG_FILTER_SUB)
\r
2252 /* it's the only filter so no testing is needed */
\r
2254 png_bytep rp, lp, dp;
\r
2256 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
\r
2261 for (lp = row_buf + 1; i < row_bytes;
\r
2262 i++, rp++, lp++, dp++)
\r
2264 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
\r
2266 best_row = png_ptr->sub_row;
\r
2269 else if (filter_to_do & PNG_FILTER_SUB)
\r
2271 png_bytep rp, dp, lp;
\r
2272 png_uint_32 sum = 0, lmins = mins;
\r
2276 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2277 /* We temporarily increase the "minimum sum" by the factor we
\r
2278 * would reduce the sum of this filter, so that we can do the
\r
2279 * early exit comparison without scaling the sum each time.
\r
2281 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2284 png_uint_32 lmhi, lmlo;
\r
2285 lmlo = lmins & PNG_LOMASK;
\r
2286 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
\r
2288 for (j = 0; j < num_p_filters; j++)
\r
2290 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
\r
2292 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
\r
2294 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
\r
2299 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
\r
2301 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
\r
2304 if (lmhi > PNG_HIMASK)
\r
2305 lmins = PNG_MAXSUM;
\r
2307 lmins = (lmhi << PNG_HISHIFT) + lmlo;
\r
2311 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
\r
2316 sum += (v < 128) ? v : 256 - v;
\r
2318 for (lp = row_buf + 1; i < row_bytes;
\r
2319 i++, rp++, lp++, dp++)
\r
2321 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
\r
2323 sum += (v < 128) ? v : 256 - v;
\r
2325 if (sum > lmins) /* We are already worse, don't continue. */
\r
2329 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2330 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2333 png_uint_32 sumhi, sumlo;
\r
2334 sumlo = sum & PNG_LOMASK;
\r
2335 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
\r
2337 for (j = 0; j < num_p_filters; j++)
\r
2339 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
\r
2341 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
\r
2343 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
\r
2348 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
\r
2350 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
\r
2353 if (sumhi > PNG_HIMASK)
\r
2356 sum = (sumhi << PNG_HISHIFT) + sumlo;
\r
2363 best_row = png_ptr->sub_row;
\r
2368 if (filter_to_do == PNG_FILTER_UP)
\r
2370 png_bytep rp, dp, pp;
\r
2373 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
\r
2374 pp = prev_row + 1; i < row_bytes;
\r
2375 i++, rp++, pp++, dp++)
\r
2377 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
\r
2379 best_row = png_ptr->up_row;
\r
2382 else if (filter_to_do & PNG_FILTER_UP)
\r
2384 png_bytep rp, dp, pp;
\r
2385 png_uint_32 sum = 0, lmins = mins;
\r
2390 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2391 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2394 png_uint_32 lmhi, lmlo;
\r
2395 lmlo = lmins & PNG_LOMASK;
\r
2396 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
\r
2398 for (j = 0; j < num_p_filters; j++)
\r
2400 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
\r
2402 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
\r
2404 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
\r
2409 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
\r
2411 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
\r
2414 if (lmhi > PNG_HIMASK)
\r
2415 lmins = PNG_MAXSUM;
\r
2417 lmins = (lmhi << PNG_HISHIFT) + lmlo;
\r
2421 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
\r
2422 pp = prev_row + 1; i < row_bytes; i++)
\r
2424 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
\r
2426 sum += (v < 128) ? v : 256 - v;
\r
2428 if (sum > lmins) /* We are already worse, don't continue. */
\r
2432 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2433 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2436 png_uint_32 sumhi, sumlo;
\r
2437 sumlo = sum & PNG_LOMASK;
\r
2438 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
\r
2440 for (j = 0; j < num_p_filters; j++)
\r
2442 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
\r
2444 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
\r
2446 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
\r
2451 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
\r
2453 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
\r
2456 if (sumhi > PNG_HIMASK)
\r
2459 sum = (sumhi << PNG_HISHIFT) + sumlo;
\r
2466 best_row = png_ptr->up_row;
\r
2471 if (filter_to_do == PNG_FILTER_AVG)
\r
2473 png_bytep rp, dp, pp, lp;
\r
2475 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
\r
2476 pp = prev_row + 1; i < bpp; i++)
\r
2478 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
\r
2480 for (lp = row_buf + 1; i < row_bytes; i++)
\r
2482 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
\r
2485 best_row = png_ptr->avg_row;
\r
2488 else if (filter_to_do & PNG_FILTER_AVG)
\r
2490 png_bytep rp, dp, pp, lp;
\r
2491 png_uint_32 sum = 0, lmins = mins;
\r
2495 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2496 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2499 png_uint_32 lmhi, lmlo;
\r
2500 lmlo = lmins & PNG_LOMASK;
\r
2501 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
\r
2503 for (j = 0; j < num_p_filters; j++)
\r
2505 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
\r
2507 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
\r
2509 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
\r
2514 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
\r
2516 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
\r
2519 if (lmhi > PNG_HIMASK)
\r
2520 lmins = PNG_MAXSUM;
\r
2522 lmins = (lmhi << PNG_HISHIFT) + lmlo;
\r
2526 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
\r
2527 pp = prev_row + 1; i < bpp; i++)
\r
2529 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
\r
2531 sum += (v < 128) ? v : 256 - v;
\r
2533 for (lp = row_buf + 1; i < row_bytes; i++)
\r
2536 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
\r
2538 sum += (v < 128) ? v : 256 - v;
\r
2540 if (sum > lmins) /* We are already worse, don't continue. */
\r
2544 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2545 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2548 png_uint_32 sumhi, sumlo;
\r
2549 sumlo = sum & PNG_LOMASK;
\r
2550 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
\r
2552 for (j = 0; j < num_p_filters; j++)
\r
2554 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
\r
2556 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
\r
2558 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
\r
2563 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
\r
2565 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
\r
2568 if (sumhi > PNG_HIMASK)
\r
2571 sum = (sumhi << PNG_HISHIFT) + sumlo;
\r
2578 best_row = png_ptr->avg_row;
\r
2582 /* Paeth filter */
\r
2583 if (filter_to_do == PNG_FILTER_PAETH)
\r
2585 png_bytep rp, dp, pp, cp, lp;
\r
2587 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
\r
2588 pp = prev_row + 1; i < bpp; i++)
\r
2590 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
\r
2593 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
\r
2595 int a, b, c, pa, pb, pc, p;
\r
2604 #ifdef PNG_USE_ABS
\r
2609 pa = p < 0 ? -p : p;
\r
2610 pb = pc < 0 ? -pc : pc;
\r
2611 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
\r
2614 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
\r
2616 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
\r
2618 best_row = png_ptr->paeth_row;
\r
2621 else if (filter_to_do & PNG_FILTER_PAETH)
\r
2623 png_bytep rp, dp, pp, cp, lp;
\r
2624 png_uint_32 sum = 0, lmins = mins;
\r
2628 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2629 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2632 png_uint_32 lmhi, lmlo;
\r
2633 lmlo = lmins & PNG_LOMASK;
\r
2634 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
\r
2636 for (j = 0; j < num_p_filters; j++)
\r
2638 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
\r
2640 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
\r
2642 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
\r
2647 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
\r
2649 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
\r
2652 if (lmhi > PNG_HIMASK)
\r
2653 lmins = PNG_MAXSUM;
\r
2655 lmins = (lmhi << PNG_HISHIFT) + lmlo;
\r
2659 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
\r
2660 pp = prev_row + 1; i < bpp; i++)
\r
2662 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
\r
2664 sum += (v < 128) ? v : 256 - v;
\r
2667 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
\r
2669 int a, b, c, pa, pb, pc, p;
\r
2675 #ifndef PNG_SLOW_PAETH
\r
2678 #ifdef PNG_USE_ABS
\r
2683 pa = p < 0 ? -p : p;
\r
2684 pb = pc < 0 ? -pc : pc;
\r
2685 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
\r
2687 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
\r
2688 #else /* PNG_SLOW_PAETH */
\r
2693 if (pa <= pb && pa <= pc)
\r
2695 else if (pb <= pc)
\r
2699 #endif /* PNG_SLOW_PAETH */
\r
2701 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
\r
2703 sum += (v < 128) ? v : 256 - v;
\r
2705 if (sum > lmins) /* We are already worse, don't continue. */
\r
2709 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2710 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
\r
2713 png_uint_32 sumhi, sumlo;
\r
2714 sumlo = sum & PNG_LOMASK;
\r
2715 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
\r
2717 for (j = 0; j < num_p_filters; j++)
\r
2719 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
\r
2721 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
\r
2723 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
\r
2728 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
\r
2730 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
\r
2733 if (sumhi > PNG_HIMASK)
\r
2736 sum = (sumhi << PNG_HISHIFT) + sumlo;
\r
2742 best_row = png_ptr->paeth_row;
\r
2745 #endif /* PNG_NO_WRITE_FILTER */
\r
2746 /* Do the actual writing of the filtered row data from the chosen filter. */
\r
2748 png_write_filtered_row(png_ptr, best_row);
\r
2750 #ifndef PNG_NO_WRITE_FILTER
\r
2751 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
\r
2752 /* Save the type of filter we picked this time for future calculations */
\r
2753 if (png_ptr->num_prev_filters > 0)
\r
2756 for (j = 1; j < num_p_filters; j++)
\r
2758 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
\r
2760 png_ptr->prev_filters[j] = best_row[0];
\r
2763 #endif /* PNG_NO_WRITE_FILTER */
\r
2767 /* Do the actual writing of a previously filtered row. */
\r
2768 void /* PRIVATE */
\r
2769 png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
\r
2771 png_debug(1, "in png_write_filtered_row\n");
\r
2772 png_debug1(2, "filter = %d\n", filtered_row[0]);
\r
2773 /* set up the zlib input buffer */
\r
2775 png_ptr->zstream.next_in = filtered_row;
\r
2776 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
\r
2777 /* repeat until we have compressed all the data */
\r
2780 int ret; /* return of zlib */
\r
2782 /* compress the data */
\r
2783 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
\r
2784 /* check for compression errors */
\r
2787 if (png_ptr->zstream.msg != NULL)
\r
2788 png_error(png_ptr, png_ptr->zstream.msg);
\r
2790 png_error(png_ptr, "zlib error");
\r
2793 /* see if it is time to write another IDAT */
\r
2794 if (!(png_ptr->zstream.avail_out))
\r
2796 /* write the IDAT and reset the zlib output buffer */
\r
2797 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
\r
2798 png_ptr->zstream.next_out = png_ptr->zbuf;
\r
2799 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
\r
2801 /* repeat until all data has been compressed */
\r
2802 } while (png_ptr->zstream.avail_in);
\r
2804 /* swap the current and previous rows */
\r
2805 if (png_ptr->prev_row != NULL)
\r
2809 tptr = png_ptr->prev_row;
\r
2810 png_ptr->prev_row = png_ptr->row_buf;
\r
2811 png_ptr->row_buf = tptr;
\r
2814 /* finish row - updates counters and flushes zlib if last row */
\r
2815 png_write_finish_row(png_ptr);
\r
2817 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
\r
2818 png_ptr->flush_rows++;
\r
2820 if (png_ptr->flush_dist > 0 &&
\r
2821 png_ptr->flush_rows >= png_ptr->flush_dist)
\r
2823 png_write_flush(png_ptr);
\r
2827 #endif /* PNG_WRITE_SUPPORTED */
\r