1
2/* pngwutil.c - utilities to write a PNG file
3 *
4 * Last changed in libpng 1.2.20 Septhember 3, 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 */
10
11#define PNG_INTERNAL
12#include "png.h"
13#ifdef PNG_WRITE_SUPPORTED
14
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.
18 */
19void PNGAPI
20png_save_uint_32(png_bytep buf, png_uint_32 i)
21{
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);
26}
27
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.
31 */
32void PNGAPI
33png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40
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.
44 */
45void PNGAPI
46png_save_uint_16(png_bytep buf, unsigned int i)
47{
48 buf[0] = (png_byte)((i >> 8) & 0xff);
49 buf[1] = (png_byte)(i & 0xff);
50}
51
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.
60 */
61void PNGAPI
62png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
63 png_bytep data, png_size_t length)
64{
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);
69}
70
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().
74 */
75void PNGAPI
76png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
78{
79 png_byte buf[4];
80 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
81 if(png_ptr == NULL) return;
82
83 /* write the length */
84 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
86
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);
92}
93
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().
98 */
99void PNGAPI
100png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
101{
102 /* write the data, and run the CRC over it */
103 if(png_ptr == NULL) return;
104 if (data != NULL && length > 0)
105 {
106 png_calculate_crc(png_ptr, data, length);
107 png_write_data(png_ptr, data, length);
108 }
109}
110
111/* Finish a chunk started with png_write_chunk_start(). */
112void PNGAPI
113png_write_chunk_end(png_structp png_ptr)
114{
115 png_byte buf[4];
116
117 if(png_ptr == NULL) return;
118
119 /* write the crc */
120 png_save_uint_32(buf, png_ptr->crc);
121
122 png_write_data(png_ptr, buf, (png_size_t)4);
123}
124
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.
130 */
131void /* PRIVATE */
132png_write_sig(png_structp png_ptr)
133{
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;
140}
141
142#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
143/*
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.
148 */
149
150typedef struct
151{
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;
158
159/* compress given text into storage in the png_ptr structure */
160static int /* PRIVATE */
161png_text_compress(png_structp png_ptr,
162 png_charp text, png_size_t text_len, int compression,
163 compression_state *comp)
164{
165 int ret;
166
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;
172
173 /* we may just want to pass the text right through */
174 if (compression == PNG_TEXT_COMPRESSION_NONE)
175 {
176 comp->input = text;
177 comp->input_len = text_len;
178 return((int)text_len);
179 }
180
181 if (compression >= PNG_TEXT_COMPRESSION_LAST)
182 {
183#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
184 char msg[50];
185 png_snprintf(msg, 50, "Unknown compression type %d", compression);
186 png_warning(png_ptr, msg);
187#else
188 png_warning(png_ptr, "Unknown compression type");
189#endif
190 }
191
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.
197 *
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).
205 */
206
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;
212
213 /* this is the same compression loop as in png_write_row() */
214 do
215 {
216 /* compress the data */
217 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
218 if (ret != Z_OK)
219 {
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");
225 }
226 /* check to see if we need more room */
227 if (!(png_ptr->zstream.avail_out))
228 {
229 /* make sure the output array has room */
230 if (comp->num_output_ptr >= comp->max_output_ptr)
231 {
232 int old_max;
233
234 old_max = comp->max_output_ptr;
235 comp->max_output_ptr = comp->num_output_ptr + 4;
236 if (comp->output_ptr != NULL)
237 {
238 png_charpp old_ptr;
239
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);
247 }
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)));
252 }
253
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++;
260
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;
264 }
265 /* continue until we don't have any more to compress */
266 } while (png_ptr->zstream.avail_in);
267
268 /* finish the compression */
269 do
270 {
271 /* tell zlib we are finished */
272 ret = deflate(&png_ptr->zstream, Z_FINISH);
273
274 if (ret == Z_OK)
275 {
276 /* check to see if we need more room */
277 if (!(png_ptr->zstream.avail_out))
278 {
279 /* check to make sure our output array has room */
280 if (comp->num_output_ptr >= comp->max_output_ptr)
281 {
282 int old_max;
283
284 old_max = comp->max_output_ptr;
285 comp->max_output_ptr = comp->num_output_ptr + 4;
286 if (comp->output_ptr != NULL)
287 {
288 png_charpp old_ptr;
289
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);
298 }
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)));
303 }
304
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++;
311
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;
315 }
316 }
317 else if (ret != Z_STREAM_END)
318 {
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");
324 }
325 } while (ret != Z_STREAM_END);
326
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;
331
332 return((int)text_len);
333}
334
335/* ship the compressed text out via chunk writes */
336static void /* PRIVATE */
337png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
338{
339 int i;
340
341 /* handle the no-compression case */
342 if (comp->input)
343 {
344 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
345 (png_size_t)comp->input_len);
346 return;
347 }
348
349 /* write saved output buffers, if any */
350 for (i = 0; i < comp->num_output_ptr; i++)
351 {
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;
356 }
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);
364
365 /* reset zlib for another zTXt/iTXt or image data */
366 deflateReset(&png_ptr->zstream);
367 png_ptr->zstream.data_type = Z_BINARY;
368}
369#endif
370
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.
374 */
375void /* PRIVATE */
376png_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)
379{
380#ifdef PNG_USE_LOCAL_ARRAYS
381 PNG_IHDR;
382#endif
383 png_byte buf[13]; /* buffer to store the IHDR info */
384
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)
388 {
389 case PNG_COLOR_TYPE_GRAY:
390 switch (bit_depth)
391 {
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");
398 }
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)
407 {
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");
413 }
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");
427 }
428
429 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
430 {
431 png_warning(png_ptr, "Invalid compression type specified");
432 compression_type = PNG_COMPRESSION_TYPE_BASE;
433 }
434
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
443 */
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)
453 {
454 png_warning(png_ptr, "Invalid filter type specified");
455 filter_type = PNG_FILTER_TYPE_BASE;
456 }
457
458#ifdef PNG_WRITE_INTERLACING_SUPPORTED
459 if (interlace_type != PNG_INTERLACE_NONE &&
460 interlace_type != PNG_INTERLACE_ADAM7)
461 {
462 png_warning(png_ptr, "Invalid interlace type specified");
463 interlace_type = PNG_INTERLACE_ADAM7;
464 }
465#else
466 interlace_type=PNG_INTERLACE_NONE;
467#endif
468
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;
479
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;
486
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;
495
496 /* write the chunk */
497 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
498
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))
504 {
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;
510 }
511 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
512 {
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;
517 }
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;
535
536 png_ptr->mode = PNG_HAVE_IHDR;
537}
538
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.
542 */
543void /* PRIVATE */
544png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
545{
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];
552
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)
559 {
560 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
561 {
562 png_error(png_ptr, "Invalid number of colors in palette");
563 }
564 else
565 {
566 png_warning(png_ptr, "Invalid number of colors in palette");
567 return;
568 }
569 }
570
571 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
572 {
573 png_warning(png_ptr,
574 "Ignoring request to write a PLTE chunk in grayscale PNG");
575 return;
576 }
577
578 png_ptr->num_palette = (png_uint_16)num_pal;
579 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
580
581 png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
582#ifndef PNG_NO_POINTER_INDEXING
583 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
584 {
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);
589 }
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++)
594 {
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);
599 }
600#endif
601 png_write_chunk_end(png_ptr);
602 png_ptr->mode |= PNG_HAVE_PLTE;
603}
604
605/* write an IDAT chunk */
606void /* PRIVATE */
607png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
608{
609#ifdef PNG_USE_LOCAL_ARRAYS
610 PNG_IDAT;
611#endif
612 png_debug(1, "in png_write_IDAT\n");
613
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)
618 {
619 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
620 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
621 {
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)
627 {
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)
635 {
636 z_cinfo--;
637 half_z_window_size >>= 1;
638 }
639 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
640 if (data[0] != (png_byte)z_cmf)
641 {
642 data[0] = (png_byte)z_cmf;
643 data[1] &= 0xe0;
644 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
645 }
646 }
647 }
648 else
649 png_error(png_ptr,
650 "Invalid zlib compression method or flags in IDAT");
651 }
652
653 png_write_chunk(png_ptr, png_IDAT, data, length);
654 png_ptr->mode |= PNG_HAVE_IDAT;
655}
656
657/* write an IEND chunk */
658void /* PRIVATE */
659png_write_IEND(png_structp png_ptr)
660{
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_IEND, png_bytep_NULL,
666 (png_size_t)0);
667 png_ptr->mode |= PNG_HAVE_IEND;
668}
669
670#if defined(PNG_WRITE_gAMA_SUPPORTED)
671/* write a gAMA chunk */
672#ifdef PNG_FLOATING_POINT_SUPPORTED
673void /* PRIVATE */
674png_write_gAMA(png_structp png_ptr, double file_gamma)
675{
676#ifdef PNG_USE_LOCAL_ARRAYS
677 PNG_gAMA;
678#endif
679 png_uint_32 igamma;
680 png_byte buf[4];
681
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_gAMA, buf, (png_size_t)4);
687}
688#endif
689#ifdef PNG_FIXED_POINT_SUPPORTED
690void /* PRIVATE */
691png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
692{
693#ifdef PNG_USE_LOCAL_ARRAYS
694 PNG_gAMA;
695#endif
696 png_byte buf[4];
697
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_gAMA, buf, (png_size_t)4);
702}
703#endif
704#endif
705
706#if defined(PNG_WRITE_sRGB_SUPPORTED)
707/* write a sRGB chunk */
708void /* PRIVATE */
709png_write_sRGB(png_structp png_ptr, int srgb_intent)
710{
711#ifdef PNG_USE_LOCAL_ARRAYS
712 PNG_sRGB;
713#endif
714 png_byte buf[1];
715
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_sRGB, buf, (png_size_t)1);
722}
723#endif
724
725#if defined(PNG_WRITE_iCCP_SUPPORTED)
726/* write an iCCP chunk */
727void /* PRIVATE */
728png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
729 png_charp profile, int profile_len)
730{
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;
738
739 png_debug(1, "in png_write_iCCP\n");
740
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;
746
747 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
748 &new_name)) == 0)
749 {
750 png_warning(png_ptr, "Empty keyword in iCCP chunk");
751 return;
752 }
753
754 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
755 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
756
757 if (profile == NULL)
758 profile_len = 0;
759
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)) );
766
767 if (profile_len < embedded_profile_len)
768 {
769 png_warning(png_ptr,
770 "Embedded profile length too large in iCCP chunk");
771 return;
772 }
773
774 if (profile_len > embedded_profile_len)
775 {
776 png_warning(png_ptr,
777 "Truncating profile to actual length in iCCP chunk");
778 profile_len = embedded_profile_len;
779 }
780
781 if (profile_len)
782 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
783 PNG_COMPRESSION_TYPE_BASE, &comp);
784
785 /* make sure we include the NULL after the name and the compression type */
786 png_write_chunk_start(png_ptr, 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);
790
791 if (profile_len)
792 png_write_compressed_data_out(png_ptr, &comp);
793
794 png_write_chunk_end(png_ptr);
795 png_free(png_ptr, new_name);
796}
797#endif
798
799#if defined(PNG_WRITE_sPLT_SUPPORTED)
800/* write a sPLT chunk */
801void /* PRIVATE */
802png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
803{
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
816
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)
820 {
821 png_warning(png_ptr, "Empty keyword in sPLT chunk");
822 return;
823 }
824
825 /* make sure we include the NULL after the name */
826 png_write_chunk_start(png_ptr, 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);
830
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++)
834 {
835 if (spalette->depth == 8)
836 {
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);
842 }
843 else
844 {
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);
850 }
851 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
852 }
853#else
854 ep=spalette->entries;
855 for (i=0; i>spalette->nentries; i++)
856 {
857 if (spalette->depth == 8)
858 {
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);
864 }
865 else
866 {
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);
872 }
873 png_write_chunk_data(png_ptr, entrybuf, entry_size);
874 }
875#endif
876
877 png_write_chunk_end(png_ptr);
878 png_free(png_ptr, new_name);
879}
880#endif
881
882#if defined(PNG_WRITE_sBIT_SUPPORTED)
883/* write the sBIT chunk */
884void /* PRIVATE */
885png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
886{
887#ifdef PNG_USE_LOCAL_ARRAYS
888 PNG_sBIT;
889#endif
890 png_byte buf[4];
891 png_size_t size;
892
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)
896 {
897 png_byte maxbits;
898
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)
904 {
905 png_warning(png_ptr, "Invalid sBIT depth specified");
906 return;
907 }
908 buf[0] = sbit->red;
909 buf[1] = sbit->green;
910 buf[2] = sbit->blue;
911 size = 3;
912 }
913 else
914 {
915 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
916 {
917 png_warning(png_ptr, "Invalid sBIT depth specified");
918 return;
919 }
920 buf[0] = sbit->gray;
921 size = 1;
922 }
923
924 if (color_type & PNG_COLOR_MASK_ALPHA)
925 {
926 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
927 {
928 png_warning(png_ptr, "Invalid sBIT depth specified");
929 return;
930 }
931 buf[size++] = sbit->alpha;
932 }
933
934 png_write_chunk(png_ptr, png_sBIT, buf, size);
935}
936#endif
937
938#if defined(PNG_WRITE_cHRM_SUPPORTED)
939/* write the cHRM chunk */
940#ifdef PNG_FLOATING_POINT_SUPPORTED
941void /* PRIVATE */
942png_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)
945{
946#ifdef PNG_USE_LOCAL_ARRAYS
947 PNG_cHRM;
948#endif
949 png_byte buf[32];
950 png_uint_32 itemp;
951
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)
956 {
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;
962 }
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);
967
968 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
969 {
970 png_warning(png_ptr, "Invalid cHRM red point specified");
971 return;
972 }
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);
977
978 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
979 {
980 png_warning(png_ptr, "Invalid cHRM green point specified");
981 return;
982 }
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);
987
988 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
989 {
990 png_warning(png_ptr, "Invalid cHRM blue point specified");
991 return;
992 }
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);
997
998 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
999}
1000#endif
1001#ifdef PNG_FIXED_POINT_SUPPORTED
1002void /* PRIVATE */
1003png_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)
1007{
1008#ifdef PNG_USE_LOCAL_ARRAYS
1009 PNG_cHRM;
1010#endif
1011 png_byte buf[32];
1012
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)
1016 {
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;
1022 }
1023 png_save_uint_32(buf, (png_uint_32)white_x);
1024 png_save_uint_32(buf + 4, (png_uint_32)white_y);
1025
1026 if (red_x + red_y > 100000L)
1027 {
1028 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
1029 return;
1030 }
1031 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1032 png_save_uint_32(buf + 12, (png_uint_32)red_y);
1033
1034 if (green_x + green_y > 100000L)
1035 {
1036 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
1037 return;
1038 }
1039 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1040 png_save_uint_32(buf + 20, (png_uint_32)green_y);
1041
1042 if (blue_x + blue_y > 100000L)
1043 {
1044 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
1045 return;
1046 }
1047 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1048 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
1049
1050 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
1051}
1052#endif
1053#endif
1054
1055#if defined(PNG_WRITE_tRNS_SUPPORTED)
1056/* write the tRNS chunk */
1057void /* PRIVATE */
1058png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
1059 int num_trans, int color_type)
1060{
1061#ifdef PNG_USE_LOCAL_ARRAYS
1062 PNG_tRNS;
1063#endif
1064 png_byte buf[6];
1065
1066 png_debug(1, "in png_write_tRNS\n");
1067 if (color_type == PNG_COLOR_TYPE_PALETTE)
1068 {
1069 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1070 {
1071 png_warning(png_ptr,"Invalid number of transparent colors specified");
1072 return;
1073 }
1074 /* write the chunk out as it is */
1075 png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
1076 }
1077 else if (color_type == PNG_COLOR_TYPE_GRAY)
1078 {
1079 /* one 16 bit value */
1080 if(tran->gray >= (1 << png_ptr->bit_depth))
1081 {
1082 png_warning(png_ptr,
1083 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1084 return;
1085 }
1086 png_save_uint_16(buf, tran->gray);
1087 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
1088 }
1089 else if (color_type == PNG_COLOR_TYPE_RGB)
1090 {
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]))
1096 {
1097 png_warning(png_ptr,
1098 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1099 return;
1100 }
1101 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
1102 }
1103 else
1104 {
1105 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
1106 }
1107}
1108#endif
1109
1110#if defined(PNG_WRITE_bKGD_SUPPORTED)
1111/* write the background chunk */
1112void /* PRIVATE */
1113png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
1114{
1115#ifdef PNG_USE_LOCAL_ARRAYS
1116 PNG_bKGD;
1117#endif
1118 png_byte buf[6];
1119
1120 png_debug(1, "in png_write_bKGD\n");
1121 if (color_type == PNG_COLOR_TYPE_PALETTE)
1122 {
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)
1129 {
1130 png_warning(png_ptr, "Invalid background palette index");
1131 return;
1132 }
1133 buf[0] = back->index;
1134 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
1135 }
1136 else if (color_type & PNG_COLOR_MASK_COLOR)
1137 {
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]))
1142 {
1143 png_warning(png_ptr,
1144 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1145 return;
1146 }
1147 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
1148 }
1149 else
1150 {
1151 if(back->gray >= (1 << png_ptr->bit_depth))
1152 {
1153 png_warning(png_ptr,
1154 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1155 return;
1156 }
1157 png_save_uint_16(buf, back->gray);
1158 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
1159 }
1160}
1161#endif
1162
1163#if defined(PNG_WRITE_hIST_SUPPORTED)
1164/* write the histogram */
1165void /* PRIVATE */
1166png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
1167{
1168#ifdef PNG_USE_LOCAL_ARRAYS
1169 PNG_hIST;
1170#endif
1171 int i;
1172 png_byte buf[3];
1173
1174 png_debug(1, "in png_write_hIST\n");
1175 if (num_hist > (int)png_ptr->num_palette)
1176 {
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;
1181 }
1182
1183 png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
1184 for (i = 0; i < num_hist; i++)
1185 {
1186 png_save_uint_16(buf, hist[i]);
1187 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
1188 }
1189 png_write_chunk_end(png_ptr);
1190}
1191#endif
1192
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.
1200 *
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.
1204 */
1205png_size_t /* PRIVATE */
1206png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
1207{
1208 png_size_t key_len;
1209 png_charp kp, dp;
1210 int kflag;
1211 int kwarn=0;
1212
1213 png_debug(1, "in png_check_keyword\n");
1214 *new_key = NULL;
1215
1216 if (key == NULL || (key_len = png_strlen(key)) == 0)
1217 {
1218 png_warning(png_ptr, "zero length keyword");
1219 return ((png_size_t)0);
1220 }
1221
1222 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1223
1224 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1225 if (*new_key == NULL)
1226 {
1227 png_warning(png_ptr, "Out of memory while procesing keyword");
1228 return ((png_size_t)0);
1229 }
1230
1231 /* Replace non-printing characters with a blank and print a warning */
1232 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
1233 {
1234 if ((png_byte)*kp < 0x20 ||
1235 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
1236 {
1237#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
1238 char msg[40];
1239
1240 png_snprintf(msg, 40,
1241 "invalid keyword character 0x%02X", (png_byte)*kp);
1242 png_warning(png_ptr, msg);
1243#else
1244 png_warning(png_ptr, "invalid character in keyword");
1245#endif
1246 *dp = ' ';
1247 }
1248 else
1249 {
1250 *dp = *kp;
1251 }
1252 }
1253 *dp = '\0';
1254
1255 /* Remove any trailing white space. */
1256 kp = *new_key + key_len - 1;
1257 if (*kp == ' ')
1258 {
1259 png_warning(png_ptr, "trailing spaces removed from keyword");
1260
1261 while (*kp == ' ')
1262 {
1263 *(kp--) = '\0';
1264 key_len--;
1265 }
1266 }
1267
1268 /* Remove any leading white space. */
1269 kp = *new_key;
1270 if (*kp == ' ')
1271 {
1272 png_warning(png_ptr, "leading spaces removed from keyword");
1273
1274 while (*kp == ' ')
1275 {
1276 kp++;
1277 key_len--;
1278 }
1279 }
1280
1281 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
1282
1283 /* Remove multiple internal spaces. */
1284 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
1285 {
1286 if (*kp == ' ' && kflag == 0)
1287 {
1288 *(dp++) = *kp;
1289 kflag = 1;
1290 }
1291 else if (*kp == ' ')
1292 {
1293 key_len--;
1294 kwarn=1;
1295 }
1296 else
1297 {
1298 *(dp++) = *kp;
1299 kflag = 0;
1300 }
1301 }
1302 *dp = '\0';
1303 if(kwarn)
1304 png_warning(png_ptr, "extra interior spaces removed from keyword");
1305
1306 if (key_len == 0)
1307 {
1308 png_free(png_ptr, *new_key);
1309 *new_key=NULL;
1310 png_warning(png_ptr, "Zero length keyword");
1311 }
1312
1313 if (key_len > 79)
1314 {
1315 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
1316 new_key[79] = '\0';
1317 key_len = 79;
1318 }
1319
1320 return (key_len);
1321}
1322#endif
1323
1324#if defined(PNG_WRITE_tEXt_SUPPORTED)
1325/* write a tEXt chunk */
1326void /* PRIVATE */
1327png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
1328 png_size_t text_len)
1329{
1330#ifdef PNG_USE_LOCAL_ARRAYS
1331 PNG_tEXt;
1332#endif
1333 png_size_t key_len;
1334 png_charp new_key;
1335
1336 png_debug(1, "in png_write_tEXt\n");
1337 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1338 {
1339 png_warning(png_ptr, "Empty keyword in tEXt chunk");
1340 return;
1341 }
1342
1343 if (text == NULL || *text == '\0')
1344 text_len = 0;
1345 else
1346 text_len = png_strlen(text);
1347
1348 /* make sure we include the 0 after the key */
1349 png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
1350 /*
1351 * We leave it to the application to meet PNG-1.0 requirements on the
1352 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1353 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1354 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1355 */
1356 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1357 if (text_len)
1358 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
1359
1360 png_write_chunk_end(png_ptr);
1361 png_free(png_ptr, new_key);
1362}
1363#endif
1364
1365#if defined(PNG_WRITE_zTXt_SUPPORTED)
1366/* write a compressed text chunk */
1367void /* PRIVATE */
1368png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
1369 png_size_t text_len, int compression)
1370{
1371#ifdef PNG_USE_LOCAL_ARRAYS
1372 PNG_zTXt;
1373#endif
1374 png_size_t key_len;
1375 char buf[1];
1376 png_charp new_key;
1377 compression_state comp;
1378
1379 png_debug(1, "in png_write_zTXt\n");
1380
1381 comp.num_output_ptr = 0;
1382 comp.max_output_ptr = 0;
1383 comp.output_ptr = NULL;
1384 comp.input = NULL;
1385 comp.input_len = 0;
1386
1387 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1388 {
1389 png_warning(png_ptr, "Empty keyword in zTXt chunk");
1390 return;
1391 }
1392
1393 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1394 {
1395 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1396 png_free(png_ptr, new_key);
1397 return;
1398 }
1399
1400 text_len = png_strlen(text);
1401
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);
1405
1406 /* write start of chunk */
1407 png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)
1408 (key_len+text_len+2));
1409 /* write key */
1410 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1411 png_free(png_ptr, new_key);
1412
1413 buf[0] = (png_byte)compression;
1414 /* write compression */
1415 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
1416 /* write the compressed data */
1417 png_write_compressed_data_out(png_ptr, &comp);
1418
1419 /* close the chunk */
1420 png_write_chunk_end(png_ptr);
1421}
1422#endif
1423
1424#if defined(PNG_WRITE_iTXt_SUPPORTED)
1425/* write an iTXt chunk */
1426void /* PRIVATE */
1427png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
1428 png_charp lang, png_charp lang_key, png_charp text)
1429{
1430#ifdef PNG_USE_LOCAL_ARRAYS
1431 PNG_iTXt;
1432#endif
1433 png_size_t lang_len, key_len, lang_key_len, text_len;
1434 png_charp new_lang, new_key;
1435 png_byte cbuf[2];
1436 compression_state comp;
1437
1438 png_debug(1, "in png_write_iTXt\n");
1439
1440 comp.num_output_ptr = 0;
1441 comp.max_output_ptr = 0;
1442 comp.output_ptr = NULL;
1443 comp.input = NULL;
1444
1445 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1446 {
1447 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1448 return;
1449 }
1450 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
1451 {
1452 png_warning(png_ptr, "Empty language field in iTXt chunk");
1453 new_lang = NULL;
1454 lang_len = 0;
1455 }
1456
1457 if (lang_key == NULL)
1458 lang_key_len = 0;
1459 else
1460 lang_key_len = png_strlen(lang_key);
1461
1462 if (text == NULL)
1463 text_len = 0;
1464 else
1465 text_len = png_strlen(text);
1466
1467 /* compute the compressed data; do it now for the length */
1468 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1469 &comp);
1470
1471
1472 /* make sure we include the compression flag, the compression byte,
1473 * and the NULs after the key, lang, and lang_key parts */
1474
1475 png_write_chunk_start(png_ptr, png_iTXt,
1476 (png_uint_32)(
1477 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1478 + key_len
1479 + lang_len
1480 + lang_key_len
1481 + text_len));
1482
1483 /*
1484 * We leave it to the application to meet PNG-1.0 requirements on the
1485 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1486 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1487 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1488 */
1489 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1490
1491 /* set the compression flag */
1492 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1493 compression == PNG_TEXT_COMPRESSION_NONE)
1494 cbuf[0] = 0;
1495 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1496 cbuf[0] = 1;
1497 /* set the compression method */
1498 cbuf[1] = 0;
1499 png_write_chunk_data(png_ptr, cbuf, 2);
1500
1501 cbuf[0] = 0;
1502 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1503 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
1504 png_write_compressed_data_out(png_ptr, &comp);
1505
1506 png_write_chunk_end(png_ptr);
1507 png_free(png_ptr, new_key);
1508 if (new_lang)
1509 png_free(png_ptr, new_lang);
1510}
1511#endif
1512
1513#if defined(PNG_WRITE_oFFs_SUPPORTED)
1514/* write the oFFs chunk */
1515void /* PRIVATE */
1516png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
1517 int unit_type)
1518{
1519#ifdef PNG_USE_LOCAL_ARRAYS
1520 PNG_oFFs;
1521#endif
1522 png_byte buf[9];
1523
1524 png_debug(1, "in png_write_oFFs\n");
1525 if (unit_type >= PNG_OFFSET_LAST)
1526 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1527
1528 png_save_int_32(buf, x_offset);
1529 png_save_int_32(buf + 4, y_offset);
1530 buf[8] = (png_byte)unit_type;
1531
1532 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
1533}
1534#endif
1535#if defined(PNG_WRITE_pCAL_SUPPORTED)
1536/* write the pCAL chunk (described in the PNG extensions document) */
1537void /* PRIVATE */
1538png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1539 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1540{
1541#ifdef PNG_USE_LOCAL_ARRAYS
1542 PNG_pCAL;
1543#endif
1544 png_size_t purpose_len, units_len, total_len;
1545 png_uint_32p params_len;
1546 png_byte buf[10];
1547 png_charp new_purpose;
1548 int i;
1549
1550 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1551 if (type >= PNG_EQUATION_LAST)
1552 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1553
1554 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1555 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
1556 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1557 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
1558 total_len = purpose_len + units_len + 10;
1559
1560 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1561 *png_sizeof(png_uint_32)));
1562
1563 /* Find the length of each parameter, making sure we don't count the
1564 null terminator for the last parameter. */
1565 for (i = 0; i < nparams; i++)
1566 {
1567 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1568 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
1569 total_len += (png_size_t)params_len[i];
1570 }
1571
1572 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
1573 png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
1574 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
1575 png_save_int_32(buf, X0);
1576 png_save_int_32(buf + 4, X1);
1577 buf[8] = (png_byte)type;
1578 buf[9] = (png_byte)nparams;
1579 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1580 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1581
1582 png_free(png_ptr, new_purpose);
1583
1584 for (i = 0; i < nparams; i++)
1585 {
1586 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1587 (png_size_t)params_len[i]);
1588 }
1589
1590 png_free(png_ptr, params_len);
1591 png_write_chunk_end(png_ptr);
1592}
1593#endif
1594
1595#if defined(PNG_WRITE_sCAL_SUPPORTED)
1596/* write the sCAL chunk */
1597#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
1598void /* PRIVATE */
1599png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
1600{
1601#ifdef PNG_USE_LOCAL_ARRAYS
1602 PNG_sCAL;
1603#endif
1604 char buf[64];
1605 png_size_t total_len;
1606
1607 png_debug(1, "in png_write_sCAL\n");
1608
1609 buf[0] = (char)unit;
1610#if defined(_WIN32_WCE)
1611/* sprintf() function is not supported on WindowsCE */
1612 {
1613 wchar_t wc_buf[32];
1614 size_t wc_len;
1615 swprintf(wc_buf, TEXT("%12.12e"), width);
1616 wc_len = wcslen(wc_buf);
1617 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
1618 total_len = wc_len + 2;
1619 swprintf(wc_buf, TEXT("%12.12e"), height);
1620 wc_len = wcslen(wc_buf);
1621 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
1622 NULL, NULL);
1623 total_len += wc_len;
1624 }
1625#else
1626 png_snprintf(buf + 1, 63, "%12.12e", width);
1627 total_len = 1 + png_strlen(buf + 1) + 1;
1628 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
1629 total_len += png_strlen(buf + total_len);
1630#endif
1631
1632 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1633 png_write_chunk(png_ptr, png_sCAL, (png_bytep)buf, total_len);
1634}
1635#else
1636#ifdef PNG_FIXED_POINT_SUPPORTED
1637void /* PRIVATE */
1638png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
1639 png_charp height)
1640{
1641#ifdef PNG_USE_LOCAL_ARRAYS
1642 PNG_sCAL;
1643#endif
1644 png_byte buf[64];
1645 png_size_t wlen, hlen, total_len;
1646
1647 png_debug(1, "in png_write_sCAL_s\n");
1648
1649 wlen = png_strlen(width);
1650 hlen = png_strlen(height);
1651 total_len = wlen + hlen + 2;
1652 if (total_len > 64)
1653 {
1654 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1655 return;
1656 }
1657
1658 buf[0] = (png_byte)unit;
1659 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1660 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
1661
1662 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1663 png_write_chunk(png_ptr, png_sCAL, buf, total_len);
1664}
1665#endif
1666#endif
1667#endif
1668
1669#if defined(PNG_WRITE_pHYs_SUPPORTED)
1670/* write the pHYs chunk */
1671void /* PRIVATE */
1672png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
1673 png_uint_32 y_pixels_per_unit,
1674 int unit_type)
1675{
1676#ifdef PNG_USE_LOCAL_ARRAYS
1677 PNG_pHYs;
1678#endif
1679 png_byte buf[9];
1680
1681 png_debug(1, "in png_write_pHYs\n");
1682 if (unit_type >= PNG_RESOLUTION_LAST)
1683 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1684
1685 png_save_uint_32(buf, x_pixels_per_unit);
1686 png_save_uint_32(buf + 4, y_pixels_per_unit);
1687 buf[8] = (png_byte)unit_type;
1688
1689 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
1690}
1691#endif
1692
1693#if defined(PNG_WRITE_tIME_SUPPORTED)
1694/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1695 * or png_convert_from_time_t(), or fill in the structure yourself.
1696 */
1697void /* PRIVATE */
1698png_write_tIME(png_structp png_ptr, png_timep mod_time)
1699{
1700#ifdef PNG_USE_LOCAL_ARRAYS
1701 PNG_tIME;
1702#endif
1703 png_byte buf[7];
1704
1705 png_debug(1, "in png_write_tIME\n");
1706 if (mod_time->month > 12 || mod_time->month < 1 ||
1707 mod_time->day > 31 || mod_time->day < 1 ||
1708 mod_time->hour > 23 || mod_time->second > 60)
1709 {
1710 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1711 return;
1712 }
1713
1714 png_save_uint_16(buf, mod_time->year);
1715 buf[2] = mod_time->month;
1716 buf[3] = mod_time->day;
1717 buf[4] = mod_time->hour;
1718 buf[5] = mod_time->minute;
1719 buf[6] = mod_time->second;
1720
1721 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
1722}
1723#endif
1724
1725/* initializes the row writing capability of libpng */
1726void /* PRIVATE */
1727png_write_start_row(png_structp png_ptr)
1728{
1729#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1730#ifdef PNG_USE_LOCAL_ARRAYS
1731 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1732
1733 /* start of interlace block */
1734 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1735
1736 /* offset to next interlace block */
1737 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1738
1739 /* start of interlace block in the y direction */
1740 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1741
1742 /* offset to next interlace block in the y direction */
1743 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1744#endif
1745#endif
1746
1747 png_size_t buf_size;
1748
1749 png_debug(1, "in png_write_start_row\n");
1750 buf_size = (png_size_t)(PNG_ROWBYTES(
1751 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
1752
1753 /* set up row buffer */
1754 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1755 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1756
1757#ifndef PNG_NO_WRITE_FILTERING
1758 /* set up filtering buffer, if using this filter */
1759 if (png_ptr->do_filter & PNG_FILTER_SUB)
1760 {
1761 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1762 (png_ptr->rowbytes + 1));
1763 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1764 }
1765
1766 /* We only need to keep the previous row if we are using one of these. */
1767 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1768 {
1769 /* set up previous row buffer */
1770 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1771 png_memset(png_ptr->prev_row, 0, buf_size);
1772
1773 if (png_ptr->do_filter & PNG_FILTER_UP)
1774 {
1775 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1776 (png_ptr->rowbytes + 1));
1777 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1778 }
1779
1780 if (png_ptr->do_filter & PNG_FILTER_AVG)
1781 {
1782 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1783 (png_ptr->rowbytes + 1));
1784 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1785 }
1786
1787 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1788 {
1789 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1790 (png_ptr->rowbytes + 1));
1791 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1792 }
1793#endif /* PNG_NO_WRITE_FILTERING */
1794 }
1795
1796#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1797 /* if interlaced, we need to set up width and height of pass */
1798 if (png_ptr->interlaced)
1799 {
1800 if (!(png_ptr->transformations & PNG_INTERLACE))
1801 {
1802 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1803 png_pass_ystart[0]) / png_pass_yinc[0];
1804 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1805 png_pass_start[0]) / png_pass_inc[0];
1806 }
1807 else
1808 {
1809 png_ptr->num_rows = png_ptr->height;
1810 png_ptr->usr_width = png_ptr->width;
1811 }
1812 }
1813 else
1814#endif
1815 {
1816 png_ptr->num_rows = png_ptr->height;
1817 png_ptr->usr_width = png_ptr->width;
1818 }
1819 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1820 png_ptr->zstream.next_out = png_ptr->zbuf;
1821}
1822
1823/* Internal use only. Called when finished processing a row of data. */
1824void /* PRIVATE */
1825png_write_finish_row(png_structp png_ptr)
1826{
1827#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1828#ifdef PNG_USE_LOCAL_ARRAYS
1829 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1830
1831 /* start of interlace block */
1832 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1833
1834 /* offset to next interlace block */
1835 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1836
1837 /* start of interlace block in the y direction */
1838 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1839
1840 /* offset to next interlace block in the y direction */
1841 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1842#endif
1843#endif
1844
1845 int ret;
1846
1847 png_debug(1, "in png_write_finish_row\n");
1848 /* next row */
1849 png_ptr->row_number++;
1850
1851 /* see if we are done */
1852 if (png_ptr->row_number < png_ptr->num_rows)
1853 return;
1854
1855#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1856 /* if interlaced, go to next pass */
1857 if (png_ptr->interlaced)
1858 {
1859 png_ptr->row_number = 0;
1860 if (png_ptr->transformations & PNG_INTERLACE)
1861 {
1862 png_ptr->pass++;
1863 }
1864 else
1865 {
1866 /* loop until we find a non-zero width or height pass */
1867 do
1868 {
1869 png_ptr->pass++;
1870 if (png_ptr->pass >= 7)
1871 break;
1872 png_ptr->usr_width = (png_ptr->width +
1873 png_pass_inc[png_ptr->pass] - 1 -
1874 png_pass_start[png_ptr->pass]) /
1875 png_pass_inc[png_ptr->pass];
1876 png_ptr->num_rows = (png_ptr->height +
1877 png_pass_yinc[png_ptr->pass] - 1 -
1878 png_pass_ystart[png_ptr->pass]) /
1879 png_pass_yinc[png_ptr->pass];
1880 if (png_ptr->transformations & PNG_INTERLACE)
1881 break;
1882 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1883
1884 }
1885
1886 /* reset the row above the image for the next pass */
1887 if (png_ptr->pass < 7)
1888 {
1889 if (png_ptr->prev_row != NULL)
1890 png_memset(png_ptr->prev_row, 0,
1891 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1892 png_ptr->usr_bit_depth,png_ptr->width))+1);
1893 return;
1894 }
1895 }
1896#endif
1897
1898 /* if we get here, we've just written the last row, so we need
1899 to flush the compressor */
1900 do
1901 {
1902 /* tell the compressor we are done */
1903 ret = deflate(&png_ptr->zstream, Z_FINISH);
1904 /* check for an error */
1905 if (ret == Z_OK)
1906 {
1907 /* check to see if we need more room */
1908 if (!(png_ptr->zstream.avail_out))
1909 {
1910 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1911 png_ptr->zstream.next_out = png_ptr->zbuf;
1912 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1913 }
1914 }
1915 else if (ret != Z_STREAM_END)
1916 {
1917 if (png_ptr->zstream.msg != NULL)
1918 png_error(png_ptr, png_ptr->zstream.msg);
1919 else
1920 png_error(png_ptr, "zlib error");
1921 }
1922 } while (ret != Z_STREAM_END);
1923
1924 /* write any extra space */
1925 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
1926 {
1927 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
1928 png_ptr->zstream.avail_out);
1929 }
1930
1931 deflateReset(&png_ptr->zstream);
1932 png_ptr->zstream.data_type = Z_BINARY;
1933}
1934
1935#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
1936/* Pick out the correct pixels for the interlace pass.
1937 * The basic idea here is to go through the row with a source
1938 * pointer and a destination pointer (sp and dp), and copy the
1939 * correct pixels for the pass. As the row gets compacted,
1940 * sp will always be >= dp, so we should never overwrite anything.
1941 * See the default: case for the easiest code to understand.
1942 */
1943void /* PRIVATE */
1944png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
1945{
1946#ifdef PNG_USE_LOCAL_ARRAYS
1947 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1948
1949 /* start of interlace block */
1950 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1951
1952 /* offset to next interlace block */
1953 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1954#endif
1955
1956 png_debug(1, "in png_do_write_interlace\n");
1957 /* we don't have to do anything on the last pass (6) */
1958#if defined(PNG_USELESS_TESTS_SUPPORTED)
1959 if (row != NULL && row_info != NULL && pass < 6)
1960#else
1961 if (pass < 6)
1962#endif
1963 {
1964 /* each pixel depth is handled separately */
1965 switch (row_info->pixel_depth)
1966 {
1967 case 1:
1968 {
1969 png_bytep sp;
1970 png_bytep dp;
1971 int shift;
1972 int d;
1973 int value;
1974 png_uint_32 i;
1975 png_uint_32 row_width = row_info->width;
1976
1977 dp = row;
1978 d = 0;
1979 shift = 7;
1980 for (i = png_pass_start[pass]; i < row_width;
1981 i += png_pass_inc[pass])
1982 {
1983 sp = row + (png_size_t)(i >> 3);
1984 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
1985 d |= (value << shift);
1986
1987 if (shift == 0)
1988 {
1989 shift = 7;
1990 *dp++ = (png_byte)d;
1991 d = 0;
1992 }
1993 else
1994 shift--;
1995
1996 }
1997 if (shift != 7)
1998 *dp = (png_byte)d;
1999 break;
2000 }
2001 case 2:
2002 {
2003 png_bytep sp;
2004 png_bytep dp;
2005 int shift;
2006 int d;
2007 int value;
2008 png_uint_32 i;
2009 png_uint_32 row_width = row_info->width;
2010
2011 dp = row;
2012 shift = 6;
2013 d = 0;
2014 for (i = png_pass_start[pass]; i < row_width;
2015 i += png_pass_inc[pass])
2016 {
2017 sp = row + (png_size_t)(i >> 2);
2018 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
2019 d |= (value << shift);
2020
2021 if (shift == 0)
2022 {
2023 shift = 6;
2024 *dp++ = (png_byte)d;
2025 d = 0;
2026 }
2027 else
2028 shift -= 2;
2029 }
2030 if (shift != 6)
2031 *dp = (png_byte)d;
2032 break;
2033 }
2034 case 4:
2035 {
2036 png_bytep sp;
2037 png_bytep dp;
2038 int shift;
2039 int d;
2040 int value;
2041 png_uint_32 i;
2042 png_uint_32 row_width = row_info->width;
2043
2044 dp = row;
2045 shift = 4;
2046 d = 0;
2047 for (i = png_pass_start[pass]; i < row_width;
2048 i += png_pass_inc[pass])
2049 {
2050 sp = row + (png_size_t)(i >> 1);
2051 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
2052 d |= (value << shift);
2053
2054 if (shift == 0)
2055 {
2056 shift = 4;
2057 *dp++ = (png_byte)d;
2058 d = 0;
2059 }
2060 else
2061 shift -= 4;
2062 }
2063 if (shift != 4)
2064 *dp = (png_byte)d;
2065 break;
2066 }
2067 default:
2068 {
2069 png_bytep sp;
2070 png_bytep dp;
2071 png_uint_32 i;
2072 png_uint_32 row_width = row_info->width;
2073 png_size_t pixel_bytes;
2074
2075 /* start at the beginning */
2076 dp = row;
2077 /* find out how many bytes each pixel takes up */
2078 pixel_bytes = (row_info->pixel_depth >> 3);
2079 /* loop through the row, only looking at the pixels that
2080 matter */
2081 for (i = png_pass_start[pass]; i < row_width;
2082 i += png_pass_inc[pass])
2083 {
2084 /* find out where the original pixel is */
2085 sp = row + (png_size_t)i * pixel_bytes;
2086 /* move the pixel */
2087 if (dp != sp)
2088 png_memcpy(dp, sp, pixel_bytes);
2089 /* next pixel */
2090 dp += pixel_bytes;
2091 }
2092 break;
2093 }
2094 }
2095 /* set new row width */
2096 row_info->width = (row_info->width +
2097 png_pass_inc[pass] - 1 -
2098 png_pass_start[pass]) /
2099 png_pass_inc[pass];
2100 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2101 row_info->width);
2102 }
2103}
2104#endif
2105
2106/* This filters the row, chooses which filter to use, if it has not already
2107 * been specified by the application, and then writes the row out with the
2108 * chosen filter.
2109 */
2110#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
2111#define PNG_HISHIFT 10
2112#define PNG_LOMASK ((png_uint_32)0xffffL)
2113#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2114void /* PRIVATE */
2115png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
2116{
2117 png_bytep best_row;
2118#ifndef PNG_NO_WRITE_FILTER
2119 png_bytep prev_row, row_buf;
2120 png_uint_32 mins, bpp;
2121 png_byte filter_to_do = png_ptr->do_filter;
2122 png_uint_32 row_bytes = row_info->rowbytes;
2123#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2124 int num_p_filters = (int)png_ptr->num_prev_filters;
2125#endif
2126
2127 png_debug(1, "in png_write_find_filter\n");
2128 /* find out how many bytes offset each pixel is */
2129 bpp = (row_info->pixel_depth + 7) >> 3;
2130
2131 prev_row = png_ptr->prev_row;
2132#endif
2133 best_row = png_ptr->row_buf;
2134#ifndef PNG_NO_WRITE_FILTER
2135 row_buf = best_row;
2136 mins = PNG_MAXSUM;
2137
2138 /* The prediction method we use is to find which method provides the
2139 * smallest value when summing the absolute values of the distances
2140 * from zero, using anything >= 128 as negative numbers. This is known
2141 * as the "minimum sum of absolute differences" heuristic. Other
2142 * heuristics are the "weighted minimum sum of absolute differences"
2143 * (experimental and can in theory improve compression), and the "zlib
2144 * predictive" method (not implemented yet), which does test compressions
2145 * of lines using different filter methods, and then chooses the
2146 * (series of) filter(s) that give minimum compressed data size (VERY
2147 * computationally expensive).
2148 *
2149 * GRR 980525: consider also
2150 * (1) minimum sum of absolute differences from running average (i.e.,
2151 * keep running sum of non-absolute differences & count of bytes)
2152 * [track dispersion, too? restart average if dispersion too large?]
2153 * (1b) minimum sum of absolute differences from sliding average, probably
2154 * with window size <= deflate window (usually 32K)
2155 * (2) minimum sum of squared differences from zero or running average
2156 * (i.e., ~ root-mean-square approach)
2157 */
2158
2159
2160 /* We don't need to test the 'no filter' case if this is the only filter
2161 * that has been chosen, as it doesn't actually do anything to the data.
2162 */
2163 if ((filter_to_do & PNG_FILTER_NONE) &&
2164 filter_to_do != PNG_FILTER_NONE)
2165 {
2166 png_bytep rp;
2167 png_uint_32 sum = 0;
2168 png_uint_32 i;
2169 int v;
2170
2171 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2172 {
2173 v = *rp;
2174 sum += (v < 128) ? v : 256 - v;
2175 }
2176
2177#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2178 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2179 {
2180 png_uint_32 sumhi, sumlo;
2181 int j;
2182 sumlo = sum & PNG_LOMASK;
2183 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2184
2185 /* Reduce the sum if we match any of the previous rows */
2186 for (j = 0; j < num_p_filters; j++)
2187 {
2188 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2189 {
2190 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2191 PNG_WEIGHT_SHIFT;
2192 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2193 PNG_WEIGHT_SHIFT;
2194 }
2195 }
2196
2197 /* Factor in the cost of this filter (this is here for completeness,
2198 * but it makes no sense to have a "cost" for the NONE filter, as
2199 * it has the minimum possible computational cost - none).
2200 */
2201 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2202 PNG_COST_SHIFT;
2203 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2204 PNG_COST_SHIFT;
2205
2206 if (sumhi > PNG_HIMASK)
2207 sum = PNG_MAXSUM;
2208 else
2209 sum = (sumhi << PNG_HISHIFT) + sumlo;
2210 }
2211#endif
2212 mins = sum;
2213 }
2214
2215 /* sub filter */
2216 if (filter_to_do == PNG_FILTER_SUB)
2217 /* it's the only filter so no testing is needed */
2218 {
2219 png_bytep rp, lp, dp;
2220 png_uint_32 i;
2221 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2222 i++, rp++, dp++)
2223 {
2224 *dp = *rp;
2225 }
2226 for (lp = row_buf + 1; i < row_bytes;
2227 i++, rp++, lp++, dp++)
2228 {
2229 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2230 }
2231 best_row = png_ptr->sub_row;
2232 }
2233
2234 else if (filter_to_do & PNG_FILTER_SUB)
2235 {
2236 png_bytep rp, dp, lp;
2237 png_uint_32 sum = 0, lmins = mins;
2238 png_uint_32 i;
2239 int v;
2240
2241#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2242 /* We temporarily increase the "minimum sum" by the factor we
2243 * would reduce the sum of this filter, so that we can do the
2244 * early exit comparison without scaling the sum each time.
2245 */
2246 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2247 {
2248 int j;
2249 png_uint_32 lmhi, lmlo;
2250 lmlo = lmins & PNG_LOMASK;
2251 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2252
2253 for (j = 0; j < num_p_filters; j++)
2254 {
2255 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2256 {
2257 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2258 PNG_WEIGHT_SHIFT;
2259 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2260 PNG_WEIGHT_SHIFT;
2261 }
2262 }
2263
2264 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2265 PNG_COST_SHIFT;
2266 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2267 PNG_COST_SHIFT;
2268
2269 if (lmhi > PNG_HIMASK)
2270 lmins = PNG_MAXSUM;
2271 else
2272 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2273 }
2274#endif
2275
2276 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2277 i++, rp++, dp++)
2278 {
2279 v = *dp = *rp;
2280
2281 sum += (v < 128) ? v : 256 - v;
2282 }
2283 for (lp = row_buf + 1; i < row_bytes;
2284 i++, rp++, lp++, dp++)
2285 {
2286 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2287
2288 sum += (v < 128) ? v : 256 - v;
2289
2290 if (sum > lmins) /* We are already worse, don't continue. */
2291 break;
2292 }
2293
2294#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2295 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2296 {
2297 int j;
2298 png_uint_32 sumhi, sumlo;
2299 sumlo = sum & PNG_LOMASK;
2300 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2301
2302 for (j = 0; j < num_p_filters; j++)
2303 {
2304 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2305 {
2306 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
2307 PNG_WEIGHT_SHIFT;
2308 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
2309 PNG_WEIGHT_SHIFT;
2310 }
2311 }
2312
2313 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2314 PNG_COST_SHIFT;
2315 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2316 PNG_COST_SHIFT;
2317
2318 if (sumhi > PNG_HIMASK)
2319 sum = PNG_MAXSUM;
2320 else
2321 sum = (sumhi << PNG_HISHIFT) + sumlo;
2322 }
2323#endif
2324
2325 if (sum < mins)
2326 {
2327 mins = sum;
2328 best_row = png_ptr->sub_row;
2329 }
2330 }
2331
2332 /* up filter */
2333 if (filter_to_do == PNG_FILTER_UP)
2334 {
2335 png_bytep rp, dp, pp;
2336 png_uint_32 i;
2337
2338 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2339 pp = prev_row + 1; i < row_bytes;
2340 i++, rp++, pp++, dp++)
2341 {
2342 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2343 }
2344 best_row = png_ptr->up_row;
2345 }
2346
2347 else if (filter_to_do & PNG_FILTER_UP)
2348 {
2349 png_bytep rp, dp, pp;
2350 png_uint_32 sum = 0, lmins = mins;
2351 png_uint_32 i;
2352 int v;
2353
2354
2355#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2356 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2357 {
2358 int j;
2359 png_uint_32 lmhi, lmlo;
2360 lmlo = lmins & PNG_LOMASK;
2361 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2362
2363 for (j = 0; j < num_p_filters; j++)
2364 {
2365 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2366 {
2367 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2368 PNG_WEIGHT_SHIFT;
2369 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2370 PNG_WEIGHT_SHIFT;
2371 }
2372 }
2373
2374 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2375 PNG_COST_SHIFT;
2376 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2377 PNG_COST_SHIFT;
2378
2379 if (lmhi > PNG_HIMASK)
2380 lmins = PNG_MAXSUM;
2381 else
2382 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2383 }
2384#endif
2385
2386 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2387 pp = prev_row + 1; i < row_bytes; i++)
2388 {
2389 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2390
2391 sum += (v < 128) ? v : 256 - v;
2392
2393 if (sum > lmins) /* We are already worse, don't continue. */
2394 break;
2395 }
2396
2397#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2398 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2399 {
2400 int j;
2401 png_uint_32 sumhi, sumlo;
2402 sumlo = sum & PNG_LOMASK;
2403 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2404
2405 for (j = 0; j < num_p_filters; j++)
2406 {
2407 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2408 {
2409 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2410 PNG_WEIGHT_SHIFT;
2411 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2412 PNG_WEIGHT_SHIFT;
2413 }
2414 }
2415
2416 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2417 PNG_COST_SHIFT;
2418 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2419 PNG_COST_SHIFT;
2420
2421 if (sumhi > PNG_HIMASK)
2422 sum = PNG_MAXSUM;
2423 else
2424 sum = (sumhi << PNG_HISHIFT) + sumlo;
2425 }
2426#endif
2427
2428 if (sum < mins)
2429 {
2430 mins = sum;
2431 best_row = png_ptr->up_row;
2432 }
2433 }
2434
2435 /* avg filter */
2436 if (filter_to_do == PNG_FILTER_AVG)
2437 {
2438 png_bytep rp, dp, pp, lp;
2439 png_uint_32 i;
2440 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2441 pp = prev_row + 1; i < bpp; i++)
2442 {
2443 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2444 }
2445 for (lp = row_buf + 1; i < row_bytes; i++)
2446 {
2447 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2448 & 0xff);
2449 }
2450 best_row = png_ptr->avg_row;
2451 }
2452
2453 else if (filter_to_do & PNG_FILTER_AVG)
2454 {
2455 png_bytep rp, dp, pp, lp;
2456 png_uint_32 sum = 0, lmins = mins;
2457 png_uint_32 i;
2458 int v;
2459
2460#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2461 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2462 {
2463 int j;
2464 png_uint_32 lmhi, lmlo;
2465 lmlo = lmins & PNG_LOMASK;
2466 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2467
2468 for (j = 0; j < num_p_filters; j++)
2469 {
2470 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
2471 {
2472 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2473 PNG_WEIGHT_SHIFT;
2474 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2475 PNG_WEIGHT_SHIFT;
2476 }
2477 }
2478
2479 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2480 PNG_COST_SHIFT;
2481 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2482 PNG_COST_SHIFT;
2483
2484 if (lmhi > PNG_HIMASK)
2485 lmins = PNG_MAXSUM;
2486 else
2487 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2488 }
2489#endif
2490
2491 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2492 pp = prev_row + 1; i < bpp; i++)
2493 {
2494 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2495
2496 sum += (v < 128) ? v : 256 - v;
2497 }
2498 for (lp = row_buf + 1; i < row_bytes; i++)
2499 {
2500 v = *dp++ =
2501 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2502
2503 sum += (v < 128) ? v : 256 - v;
2504
2505 if (sum > lmins) /* We are already worse, don't continue. */
2506 break;
2507 }
2508
2509#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2510 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2511 {
2512 int j;
2513 png_uint_32 sumhi, sumlo;
2514 sumlo = sum & PNG_LOMASK;
2515 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2516
2517 for (j = 0; j < num_p_filters; j++)
2518 {
2519 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2520 {
2521 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2522 PNG_WEIGHT_SHIFT;
2523 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2524 PNG_WEIGHT_SHIFT;
2525 }
2526 }
2527
2528 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2529 PNG_COST_SHIFT;
2530 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2531 PNG_COST_SHIFT;
2532
2533 if (sumhi > PNG_HIMASK)
2534 sum = PNG_MAXSUM;
2535 else
2536 sum = (sumhi << PNG_HISHIFT) + sumlo;
2537 }
2538#endif
2539
2540 if (sum < mins)
2541 {
2542 mins = sum;
2543 best_row = png_ptr->avg_row;
2544 }
2545 }
2546
2547 /* Paeth filter */
2548 if (filter_to_do == PNG_FILTER_PAETH)
2549 {
2550 png_bytep rp, dp, pp, cp, lp;
2551 png_uint_32 i;
2552 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2553 pp = prev_row + 1; i < bpp; i++)
2554 {
2555 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2556 }
2557
2558 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2559 {
2560 int a, b, c, pa, pb, pc, p;
2561
2562 b = *pp++;
2563 c = *cp++;
2564 a = *lp++;
2565
2566 p = b - c;
2567 pc = a - c;
2568
2569#ifdef PNG_USE_ABS
2570 pa = abs(p);
2571 pb = abs(pc);
2572 pc = abs(p + pc);
2573#else
2574 pa = p < 0 ? -p : p;
2575 pb = pc < 0 ? -pc : pc;
2576 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2577#endif
2578
2579 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2580
2581 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2582 }
2583 best_row = png_ptr->paeth_row;
2584 }
2585
2586 else if (filter_to_do & PNG_FILTER_PAETH)
2587 {
2588 png_bytep rp, dp, pp, cp, lp;
2589 png_uint_32 sum = 0, lmins = mins;
2590 png_uint_32 i;
2591 int v;
2592
2593#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2594 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2595 {
2596 int j;
2597 png_uint_32 lmhi, lmlo;
2598 lmlo = lmins & PNG_LOMASK;
2599 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2600
2601 for (j = 0; j < num_p_filters; j++)
2602 {
2603 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2604 {
2605 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2606 PNG_WEIGHT_SHIFT;
2607 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2608 PNG_WEIGHT_SHIFT;
2609 }
2610 }
2611
2612 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2613 PNG_COST_SHIFT;
2614 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2615 PNG_COST_SHIFT;
2616
2617 if (lmhi > PNG_HIMASK)
2618 lmins = PNG_MAXSUM;
2619 else
2620 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2621 }
2622#endif
2623
2624 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2625 pp = prev_row + 1; i < bpp; i++)
2626 {
2627 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2628
2629 sum += (v < 128) ? v : 256 - v;
2630 }
2631
2632 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2633 {
2634 int a, b, c, pa, pb, pc, p;
2635
2636 b = *pp++;
2637 c = *cp++;
2638 a = *lp++;
2639
2640#ifndef PNG_SLOW_PAETH
2641 p = b - c;
2642 pc = a - c;
2643#ifdef PNG_USE_ABS
2644 pa = abs(p);
2645 pb = abs(pc);
2646 pc = abs(p + pc);
2647#else
2648 pa = p < 0 ? -p : p;
2649 pb = pc < 0 ? -pc : pc;
2650 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2651#endif
2652 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2653#else /* PNG_SLOW_PAETH */
2654 p = a + b - c;
2655 pa = abs(p - a);
2656 pb = abs(p - b);
2657 pc = abs(p - c);
2658 if (pa <= pb && pa <= pc)
2659 p = a;
2660 else if (pb <= pc)
2661 p = b;
2662 else
2663 p = c;
2664#endif /* PNG_SLOW_PAETH */
2665
2666 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2667
2668 sum += (v < 128) ? v : 256 - v;
2669
2670 if (sum > lmins) /* We are already worse, don't continue. */
2671 break;
2672 }
2673
2674#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2675 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2676 {
2677 int j;
2678 png_uint_32 sumhi, sumlo;
2679 sumlo = sum & PNG_LOMASK;
2680 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2681
2682 for (j = 0; j < num_p_filters; j++)
2683 {
2684 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2685 {
2686 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2687 PNG_WEIGHT_SHIFT;
2688 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2689 PNG_WEIGHT_SHIFT;
2690 }
2691 }
2692
2693 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2694 PNG_COST_SHIFT;
2695 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2696 PNG_COST_SHIFT;
2697
2698 if (sumhi > PNG_HIMASK)
2699 sum = PNG_MAXSUM;
2700 else
2701 sum = (sumhi << PNG_HISHIFT) + sumlo;
2702 }
2703#endif
2704
2705 if (sum < mins)
2706 {
2707 best_row = png_ptr->paeth_row;
2708 }
2709 }
2710#endif /* PNG_NO_WRITE_FILTER */
2711 /* Do the actual writing of the filtered row data from the chosen filter. */
2712
2713 png_write_filtered_row(png_ptr, best_row);
2714
2715#ifndef PNG_NO_WRITE_FILTER
2716#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2717 /* Save the type of filter we picked this time for future calculations */
2718 if (png_ptr->num_prev_filters > 0)
2719 {
2720 int j;
2721 for (j = 1; j < num_p_filters; j++)
2722 {
2723 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2724 }
2725 png_ptr->prev_filters[j] = best_row[0];
2726 }
2727#endif
2728#endif /* PNG_NO_WRITE_FILTER */
2729}
2730
2731
2732/* Do the actual writing of a previously filtered row. */
2733void /* PRIVATE */
2734png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2735{
2736 png_debug(1, "in png_write_filtered_row\n");
2737 png_debug1(2, "filter = %d\n", filtered_row[0]);
2738 /* set up the zlib input buffer */
2739
2740 png_ptr->zstream.next_in = filtered_row;
2741 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
2742 /* repeat until we have compressed all the data */
2743 do
2744 {
2745 int ret; /* return of zlib */
2746
2747 /* compress the data */
2748 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
2749 /* check for compression errors */
2750 if (ret != Z_OK)
2751 {
2752 if (png_ptr->zstream.msg != NULL)
2753 png_error(png_ptr, png_ptr->zstream.msg);
2754 else
2755 png_error(png_ptr, "zlib error");
2756 }
2757
2758 /* see if it is time to write another IDAT */
2759 if (!(png_ptr->zstream.avail_out))
2760 {
2761 /* write the IDAT and reset the zlib output buffer */
2762 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2763 png_ptr->zstream.next_out = png_ptr->zbuf;
2764 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2765 }
2766 /* repeat until all data has been compressed */
2767 } while (png_ptr->zstream.avail_in);
2768
2769 /* swap the current and previous rows */
2770 if (png_ptr->prev_row != NULL)
2771 {
2772 png_bytep tptr;
2773
2774 tptr = png_ptr->prev_row;
2775 png_ptr->prev_row = png_ptr->row_buf;
2776 png_ptr->row_buf = tptr;
2777 }
2778
2779 /* finish row - updates counters and flushes zlib if last row */
2780 png_write_finish_row(png_ptr);
2781
2782#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2783 png_ptr->flush_rows++;
2784
2785 if (png_ptr->flush_dist > 0 &&
2786 png_ptr->flush_rows >= png_ptr->flush_dist)
2787 {
2788 png_write_flush(png_ptr);
2789 }
2790#endif
2791}
2792#endif /* PNG_WRITE_SUPPORTED */
2793