merged libpng-1.2.6 to HEAD
[wxWidgets.git] / src / png / pngwutil.c
1
2 /* pngwutil.c - utilities to write a PNG file
3 *
4 * libpng version 1.2.6 - August 15, 2004
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2004 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 */
19 void /* PRIVATE */
20 png_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 #if defined(PNG_WRITE_pCAL_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
29 /* The png_save_int_32 function assumes integers are stored in two's
30 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
33 void /* PRIVATE */
34 png_save_int_32(png_bytep buf, png_int_32 i)
35 {
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40 }
41 #endif
42
43 /* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
47 void /* PRIVATE */
48 png_save_uint_16(png_bytep buf, unsigned int i)
49 {
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52 }
53
54 /* Write a PNG chunk all at once. The type is an array of ASCII characters
55 * representing the chunk name. The array must be at least 4 bytes in
56 * length, and does not need to be null terminated. To be safe, pass the
57 * pre-defined chunk names here, and if you need a new one, define it
58 * where the others are defined. The length is the length of the data.
59 * All the data must be present. If that is not possible, use the
60 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
61 * functions instead.
62 */
63 void PNGAPI
64 png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
65 png_bytep data, png_size_t length)
66 {
67 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
68 png_write_chunk_data(png_ptr, data, length);
69 png_write_chunk_end(png_ptr);
70 }
71
72 /* Write the start of a PNG chunk. The type is the chunk type.
73 * The total_length is the sum of the lengths of all the data you will be
74 * passing in png_write_chunk_data().
75 */
76 void PNGAPI
77 png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
78 png_uint_32 length)
79 {
80 png_byte buf[4];
81 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
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 */
99 void PNGAPI
100 png_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 (data != NULL && length > 0)
104 {
105 png_calculate_crc(png_ptr, data, length);
106 png_write_data(png_ptr, data, length);
107 }
108 }
109
110 /* Finish a chunk started with png_write_chunk_start(). */
111 void PNGAPI
112 png_write_chunk_end(png_structp png_ptr)
113 {
114 png_byte buf[4];
115
116 /* write the crc */
117 png_save_uint_32(buf, png_ptr->crc);
118
119 png_write_data(png_ptr, buf, (png_size_t)4);
120 }
121
122 /* Simple function to write the signature. If we have already written
123 * the magic bytes of the signature, or more likely, the PNG stream is
124 * being embedded into another stream and doesn't need its own signature,
125 * we should call png_set_sig_bytes() to tell libpng how many of the
126 * bytes have already been written.
127 */
128 void /* PRIVATE */
129 png_write_sig(png_structp png_ptr)
130 {
131 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
132 /* write the rest of the 8 byte signature */
133 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
134 (png_size_t)8 - png_ptr->sig_bytes);
135 if(png_ptr->sig_bytes < 3)
136 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
137 }
138
139 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
140 /*
141 * This pair of functions encapsulates the operation of (a) compressing a
142 * text string, and (b) issuing it later as a series of chunk data writes.
143 * The compression_state structure is shared context for these functions
144 * set up by the caller in order to make the whole mess thread-safe.
145 */
146
147 typedef struct
148 {
149 char *input; /* the uncompressed input data */
150 int input_len; /* its length */
151 int num_output_ptr; /* number of output pointers used */
152 int max_output_ptr; /* size of output_ptr */
153 png_charpp output_ptr; /* array of pointers to output */
154 } compression_state;
155
156 /* compress given text into storage in the png_ptr structure */
157 static int /* PRIVATE */
158 png_text_compress(png_structp png_ptr,
159 png_charp text, png_size_t text_len, int compression,
160 compression_state *comp)
161 {
162 int ret;
163
164 comp->num_output_ptr = comp->max_output_ptr = 0;
165 comp->output_ptr = NULL;
166 comp->input = NULL;
167
168 /* we may just want to pass the text right through */
169 if (compression == PNG_TEXT_COMPRESSION_NONE)
170 {
171 comp->input = text;
172 comp->input_len = text_len;
173 return((int)text_len);
174 }
175
176 if (compression >= PNG_TEXT_COMPRESSION_LAST)
177 {
178 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
179 char msg[50];
180 sprintf(msg, "Unknown compression type %d", compression);
181 png_warning(png_ptr, msg);
182 #else
183 png_warning(png_ptr, "Unknown compression type");
184 #endif
185 }
186
187 /* We can't write the chunk until we find out how much data we have,
188 * which means we need to run the compressor first and save the
189 * output. This shouldn't be a problem, as the vast majority of
190 * comments should be reasonable, but we will set up an array of
191 * malloc'd pointers to be sure.
192 *
193 * If we knew the application was well behaved, we could simplify this
194 * greatly by assuming we can always malloc an output buffer large
195 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
196 * and malloc this directly. The only time this would be a bad idea is
197 * if we can't malloc more than 64K and we have 64K of random input
198 * data, or if the input string is incredibly large (although this
199 * wouldn't cause a failure, just a slowdown due to swapping).
200 */
201
202 /* set up the compression buffers */
203 png_ptr->zstream.avail_in = (uInt)text_len;
204 png_ptr->zstream.next_in = (Bytef *)text;
205 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
206 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
207
208 /* this is the same compression loop as in png_write_row() */
209 do
210 {
211 /* compress the data */
212 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
213 if (ret != Z_OK)
214 {
215 /* error */
216 if (png_ptr->zstream.msg != NULL)
217 png_error(png_ptr, png_ptr->zstream.msg);
218 else
219 png_error(png_ptr, "zlib error");
220 }
221 /* check to see if we need more room */
222 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
223 {
224 /* make sure the output array has room */
225 if (comp->num_output_ptr >= comp->max_output_ptr)
226 {
227 int old_max;
228
229 old_max = comp->max_output_ptr;
230 comp->max_output_ptr = comp->num_output_ptr + 4;
231 if (comp->output_ptr != NULL)
232 {
233 png_charpp old_ptr;
234
235 old_ptr = comp->output_ptr;
236 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
237 (png_uint_32)(comp->max_output_ptr *
238 png_sizeof (png_charpp)));
239 png_memcpy(comp->output_ptr, old_ptr, old_max
240 * png_sizeof (png_charp));
241 png_free(png_ptr, old_ptr);
242 }
243 else
244 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
245 (png_uint_32)(comp->max_output_ptr *
246 png_sizeof (png_charp)));
247 }
248
249 /* save the data */
250 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
251 (png_uint_32)png_ptr->zbuf_size);
252 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
253 png_ptr->zbuf_size);
254 comp->num_output_ptr++;
255
256 /* and reset the buffer */
257 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
258 png_ptr->zstream.next_out = png_ptr->zbuf;
259 }
260 /* continue until we don't have any more to compress */
261 } while (png_ptr->zstream.avail_in);
262
263 /* finish the compression */
264 do
265 {
266 /* tell zlib we are finished */
267 ret = deflate(&png_ptr->zstream, Z_FINISH);
268
269 if (ret == Z_OK)
270 {
271 /* check to see if we need more room */
272 if (!(png_ptr->zstream.avail_out))
273 {
274 /* check to make sure our output array has room */
275 if (comp->num_output_ptr >= comp->max_output_ptr)
276 {
277 int old_max;
278
279 old_max = comp->max_output_ptr;
280 comp->max_output_ptr = comp->num_output_ptr + 4;
281 if (comp->output_ptr != NULL)
282 {
283 png_charpp old_ptr;
284
285 old_ptr = comp->output_ptr;
286 /* This could be optimized to realloc() */
287 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
288 (png_uint_32)(comp->max_output_ptr *
289 png_sizeof (png_charpp)));
290 png_memcpy(comp->output_ptr, old_ptr,
291 old_max * png_sizeof (png_charp));
292 png_free(png_ptr, old_ptr);
293 }
294 else
295 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
296 (png_uint_32)(comp->max_output_ptr *
297 png_sizeof (png_charp)));
298 }
299
300 /* save off the data */
301 comp->output_ptr[comp->num_output_ptr] =
302 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
303 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
304 png_ptr->zbuf_size);
305 comp->num_output_ptr++;
306
307 /* and reset the buffer pointers */
308 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
309 png_ptr->zstream.next_out = png_ptr->zbuf;
310 }
311 }
312 else if (ret != Z_STREAM_END)
313 {
314 /* we got an error */
315 if (png_ptr->zstream.msg != NULL)
316 png_error(png_ptr, png_ptr->zstream.msg);
317 else
318 png_error(png_ptr, "zlib error");
319 }
320 } while (ret != Z_STREAM_END);
321
322 /* text length is number of buffers plus last buffer */
323 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
324 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
325 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
326
327 return((int)text_len);
328 }
329
330 /* ship the compressed text out via chunk writes */
331 static void /* PRIVATE */
332 png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
333 {
334 int i;
335
336 /* handle the no-compression case */
337 if (comp->input)
338 {
339 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
340 (png_size_t)comp->input_len);
341 return;
342 }
343
344 /* write saved output buffers, if any */
345 for (i = 0; i < comp->num_output_ptr; i++)
346 {
347 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
348 png_ptr->zbuf_size);
349 png_free(png_ptr, comp->output_ptr[i]);
350 comp->output_ptr[i]=NULL;
351 }
352 if (comp->max_output_ptr != 0)
353 png_free(png_ptr, comp->output_ptr);
354 comp->output_ptr=NULL;
355 /* write anything left in zbuf */
356 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
357 png_write_chunk_data(png_ptr, png_ptr->zbuf,
358 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
359
360 /* reset zlib for another zTXt/iTXt or the image data */
361 deflateReset(&png_ptr->zstream);
362
363 }
364 #endif
365
366 /* Write the IHDR chunk, and update the png_struct with the necessary
367 * information. Note that the rest of this code depends upon this
368 * information being correct.
369 */
370 void /* PRIVATE */
371 png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
372 int bit_depth, int color_type, int compression_type, int filter_type,
373 int interlace_type)
374 {
375 #ifdef PNG_USE_LOCAL_ARRAYS
376 PNG_IHDR;
377 #endif
378 png_byte buf[13]; /* buffer to store the IHDR info */
379
380 png_debug(1, "in png_write_IHDR\n");
381 /* Check that we have valid input data from the application info */
382 switch (color_type)
383 {
384 case PNG_COLOR_TYPE_GRAY:
385 switch (bit_depth)
386 {
387 case 1:
388 case 2:
389 case 4:
390 case 8:
391 case 16: png_ptr->channels = 1; break;
392 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
393 }
394 break;
395 case PNG_COLOR_TYPE_RGB:
396 if (bit_depth != 8 && bit_depth != 16)
397 png_error(png_ptr, "Invalid bit depth for RGB image");
398 png_ptr->channels = 3;
399 break;
400 case PNG_COLOR_TYPE_PALETTE:
401 switch (bit_depth)
402 {
403 case 1:
404 case 2:
405 case 4:
406 case 8: png_ptr->channels = 1; break;
407 default: png_error(png_ptr, "Invalid bit depth for paletted image");
408 }
409 break;
410 case PNG_COLOR_TYPE_GRAY_ALPHA:
411 if (bit_depth != 8 && bit_depth != 16)
412 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
413 png_ptr->channels = 2;
414 break;
415 case PNG_COLOR_TYPE_RGB_ALPHA:
416 if (bit_depth != 8 && bit_depth != 16)
417 png_error(png_ptr, "Invalid bit depth for RGBA image");
418 png_ptr->channels = 4;
419 break;
420 default:
421 png_error(png_ptr, "Invalid image color type specified");
422 }
423
424 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
425 {
426 png_warning(png_ptr, "Invalid compression type specified");
427 compression_type = PNG_COMPRESSION_TYPE_BASE;
428 }
429
430 /* Write filter_method 64 (intrapixel differencing) only if
431 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
432 * 2. Libpng did not write a PNG signature (this filter_method is only
433 * used in PNG datastreams that are embedded in MNG datastreams) and
434 * 3. The application called png_permit_mng_features with a mask that
435 * included PNG_FLAG_MNG_FILTER_64 and
436 * 4. The filter_method is 64 and
437 * 5. The color_type is RGB or RGBA
438 */
439 if (
440 #if defined(PNG_MNG_FEATURES_SUPPORTED)
441 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
442 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
443 (color_type == PNG_COLOR_TYPE_RGB ||
444 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
445 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
446 #endif
447 filter_type != PNG_FILTER_TYPE_BASE)
448 {
449 png_warning(png_ptr, "Invalid filter type specified");
450 filter_type = PNG_FILTER_TYPE_BASE;
451 }
452
453 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
454 if (interlace_type != PNG_INTERLACE_NONE &&
455 interlace_type != PNG_INTERLACE_ADAM7)
456 {
457 png_warning(png_ptr, "Invalid interlace type specified");
458 interlace_type = PNG_INTERLACE_ADAM7;
459 }
460 #else
461 interlace_type=PNG_INTERLACE_NONE;
462 #endif
463
464 /* save off the relevent information */
465 png_ptr->bit_depth = (png_byte)bit_depth;
466 png_ptr->color_type = (png_byte)color_type;
467 png_ptr->interlaced = (png_byte)interlace_type;
468 #if defined(PNG_MNG_FEATURES_SUPPORTED)
469 png_ptr->filter_type = (png_byte)filter_type;
470 #endif
471 png_ptr->compression_type = (png_byte)compression_type;
472 png_ptr->width = width;
473 png_ptr->height = height;
474
475 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
476 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
477 /* set the usr info, so any transformations can modify it */
478 png_ptr->usr_width = png_ptr->width;
479 png_ptr->usr_bit_depth = png_ptr->bit_depth;
480 png_ptr->usr_channels = png_ptr->channels;
481
482 /* pack the header information into the buffer */
483 png_save_uint_32(buf, width);
484 png_save_uint_32(buf + 4, height);
485 buf[8] = (png_byte)bit_depth;
486 buf[9] = (png_byte)color_type;
487 buf[10] = (png_byte)compression_type;
488 buf[11] = (png_byte)filter_type;
489 buf[12] = (png_byte)interlace_type;
490
491 /* write the chunk */
492 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
493
494 /* initialize zlib with PNG info */
495 png_ptr->zstream.zalloc = png_zalloc;
496 png_ptr->zstream.zfree = png_zfree;
497 png_ptr->zstream.opaque = (voidpf)png_ptr;
498 if (!(png_ptr->do_filter))
499 {
500 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
501 png_ptr->bit_depth < 8)
502 png_ptr->do_filter = PNG_FILTER_NONE;
503 else
504 png_ptr->do_filter = PNG_ALL_FILTERS;
505 }
506 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
507 {
508 if (png_ptr->do_filter != PNG_FILTER_NONE)
509 png_ptr->zlib_strategy = Z_FILTERED;
510 else
511 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
512 }
513 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
514 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
515 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
516 png_ptr->zlib_mem_level = 8;
517 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
518 png_ptr->zlib_window_bits = 15;
519 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
520 png_ptr->zlib_method = 8;
521 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
522 png_ptr->zlib_method, png_ptr->zlib_window_bits,
523 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
524 png_ptr->zstream.next_out = png_ptr->zbuf;
525 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
526
527 png_ptr->mode = PNG_HAVE_IHDR;
528 }
529
530 /* write the palette. We are careful not to trust png_color to be in the
531 * correct order for PNG, so people can redefine it to any convenient
532 * structure.
533 */
534 void /* PRIVATE */
535 png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
536 {
537 #ifdef PNG_USE_LOCAL_ARRAYS
538 PNG_PLTE;
539 #endif
540 png_uint_32 i;
541 png_colorp pal_ptr;
542 png_byte buf[3];
543
544 png_debug(1, "in png_write_PLTE\n");
545 if ((
546 #if defined(PNG_MNG_FEATURES_SUPPORTED)
547 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
548 #endif
549 num_pal == 0) || num_pal > 256)
550 {
551 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
552 {
553 png_error(png_ptr, "Invalid number of colors in palette");
554 }
555 else
556 {
557 png_warning(png_ptr, "Invalid number of colors in palette");
558 return;
559 }
560 }
561
562 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
563 {
564 png_warning(png_ptr,
565 "Ignoring request to write a PLTE chunk in grayscale PNG");
566 return;
567 }
568
569 png_ptr->num_palette = (png_uint_16)num_pal;
570 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
571
572 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
573 #ifndef PNG_NO_POINTER_INDEXING
574 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
575 {
576 buf[0] = pal_ptr->red;
577 buf[1] = pal_ptr->green;
578 buf[2] = pal_ptr->blue;
579 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
580 }
581 #else
582 /* This is a little slower but some buggy compilers need to do this instead */
583 pal_ptr=palette;
584 for (i = 0; i < num_pal; i++)
585 {
586 buf[0] = pal_ptr[i].red;
587 buf[1] = pal_ptr[i].green;
588 buf[2] = pal_ptr[i].blue;
589 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
590 }
591 #endif
592 png_write_chunk_end(png_ptr);
593 png_ptr->mode |= PNG_HAVE_PLTE;
594 }
595
596 /* write an IDAT chunk */
597 void /* PRIVATE */
598 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
599 {
600 #ifdef PNG_USE_LOCAL_ARRAYS
601 PNG_IDAT;
602 #endif
603 png_debug(1, "in png_write_IDAT\n");
604
605 /* Optimize the CMF field in the zlib stream. */
606 /* This hack of the zlib stream is compliant to the stream specification. */
607 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
608 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
609 {
610 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
611 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
612 {
613 /* Avoid memory underflows and multiplication overflows. */
614 /* The conditions below are practically always satisfied;
615 however, they still must be checked. */
616 if (length >= 2 &&
617 png_ptr->height < 16384 && png_ptr->width < 16384)
618 {
619 png_uint_32 uncompressed_idat_size = png_ptr->height *
620 (PNG_ROWBYTES(png_ptr->channels*png_ptr->bit_depth,
621 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
622 unsigned int z_cinfo = z_cmf >> 4;
623 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
624 while (uncompressed_idat_size <= half_z_window_size &&
625 half_z_window_size >= 256)
626 {
627 z_cinfo--;
628 half_z_window_size >>= 1;
629 }
630 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
631 if (data[0] != (png_byte)z_cmf)
632 {
633 data[0] = (png_byte)z_cmf;
634 data[1] &= 0xe0;
635 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
636 }
637 }
638 }
639 else
640 png_error(png_ptr,
641 "Invalid zlib compression method or flags in IDAT");
642 }
643
644 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
645 png_ptr->mode |= PNG_HAVE_IDAT;
646 }
647
648 /* write an IEND chunk */
649 void /* PRIVATE */
650 png_write_IEND(png_structp png_ptr)
651 {
652 #ifdef PNG_USE_LOCAL_ARRAYS
653 PNG_IEND;
654 #endif
655 png_debug(1, "in png_write_IEND\n");
656 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
657 (png_size_t)0);
658 png_ptr->mode |= PNG_HAVE_IEND;
659 }
660
661 #if defined(PNG_WRITE_gAMA_SUPPORTED)
662 /* write a gAMA chunk */
663 #ifdef PNG_FLOATING_POINT_SUPPORTED
664 void /* PRIVATE */
665 png_write_gAMA(png_structp png_ptr, double file_gamma)
666 {
667 #ifdef PNG_USE_LOCAL_ARRAYS
668 PNG_gAMA;
669 #endif
670 png_uint_32 igamma;
671 png_byte buf[4];
672
673 png_debug(1, "in png_write_gAMA\n");
674 /* file_gamma is saved in 1/100,000ths */
675 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
676 png_save_uint_32(buf, igamma);
677 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
678 }
679 #endif
680 #ifdef PNG_FIXED_POINT_SUPPORTED
681 void /* PRIVATE */
682 png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
683 {
684 #ifdef PNG_USE_LOCAL_ARRAYS
685 PNG_gAMA;
686 #endif
687 png_byte buf[4];
688
689 png_debug(1, "in png_write_gAMA\n");
690 /* file_gamma is saved in 1/100,000ths */
691 png_save_uint_32(buf, (png_uint_32)file_gamma);
692 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
693 }
694 #endif
695 #endif
696
697 #if defined(PNG_WRITE_sRGB_SUPPORTED)
698 /* write a sRGB chunk */
699 void /* PRIVATE */
700 png_write_sRGB(png_structp png_ptr, int srgb_intent)
701 {
702 #ifdef PNG_USE_LOCAL_ARRAYS
703 PNG_sRGB;
704 #endif
705 png_byte buf[1];
706
707 png_debug(1, "in png_write_sRGB\n");
708 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
709 png_warning(png_ptr,
710 "Invalid sRGB rendering intent specified");
711 buf[0]=(png_byte)srgb_intent;
712 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
713 }
714 #endif
715
716 #if defined(PNG_WRITE_iCCP_SUPPORTED)
717 /* write an iCCP chunk */
718 void /* PRIVATE */
719 png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
720 png_charp profile, int profile_len)
721 {
722 #ifdef PNG_USE_LOCAL_ARRAYS
723 PNG_iCCP;
724 #endif
725 png_size_t name_len;
726 png_charp new_name;
727 compression_state comp;
728
729 png_debug(1, "in png_write_iCCP\n");
730 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
731 &new_name)) == 0)
732 {
733 png_warning(png_ptr, "Empty keyword in iCCP chunk");
734 return;
735 }
736
737 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
738 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
739
740 if (profile == NULL)
741 profile_len = 0;
742
743 if (profile_len)
744 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
745 PNG_COMPRESSION_TYPE_BASE, &comp);
746
747 /* make sure we include the NULL after the name and the compression type */
748 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
749 (png_uint_32)name_len+profile_len+2);
750 new_name[name_len+1]=0x00;
751 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
752
753 if (profile_len)
754 png_write_compressed_data_out(png_ptr, &comp);
755
756 png_write_chunk_end(png_ptr);
757 png_free(png_ptr, new_name);
758 }
759 #endif
760
761 #if defined(PNG_WRITE_sPLT_SUPPORTED)
762 /* write a sPLT chunk */
763 void /* PRIVATE */
764 png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
765 {
766 #ifdef PNG_USE_LOCAL_ARRAYS
767 PNG_sPLT;
768 #endif
769 png_size_t name_len;
770 png_charp new_name;
771 png_byte entrybuf[10];
772 int entry_size = (spalette->depth == 8 ? 6 : 10);
773 int palette_size = entry_size * spalette->nentries;
774 png_sPLT_entryp ep;
775 #ifdef PNG_NO_POINTER_INDEXING
776 int i;
777 #endif
778
779 png_debug(1, "in png_write_sPLT\n");
780 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
781 spalette->name, &new_name))==0)
782 {
783 png_warning(png_ptr, "Empty keyword in sPLT chunk");
784 return;
785 }
786
787 /* make sure we include the NULL after the name */
788 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
789 (png_uint_32)(name_len + 2 + palette_size));
790 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
791 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
792
793 /* loop through each palette entry, writing appropriately */
794 #ifndef PNG_NO_POINTER_INDEXING
795 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
796 {
797 if (spalette->depth == 8)
798 {
799 entrybuf[0] = (png_byte)ep->red;
800 entrybuf[1] = (png_byte)ep->green;
801 entrybuf[2] = (png_byte)ep->blue;
802 entrybuf[3] = (png_byte)ep->alpha;
803 png_save_uint_16(entrybuf + 4, ep->frequency);
804 }
805 else
806 {
807 png_save_uint_16(entrybuf + 0, ep->red);
808 png_save_uint_16(entrybuf + 2, ep->green);
809 png_save_uint_16(entrybuf + 4, ep->blue);
810 png_save_uint_16(entrybuf + 6, ep->alpha);
811 png_save_uint_16(entrybuf + 8, ep->frequency);
812 }
813 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
814 }
815 #else
816 ep=spalette->entries;
817 for (i=0; i>spalette->nentries; i++)
818 {
819 if (spalette->depth == 8)
820 {
821 entrybuf[0] = (png_byte)ep[i].red;
822 entrybuf[1] = (png_byte)ep[i].green;
823 entrybuf[2] = (png_byte)ep[i].blue;
824 entrybuf[3] = (png_byte)ep[i].alpha;
825 png_save_uint_16(entrybuf + 4, ep[i].frequency);
826 }
827 else
828 {
829 png_save_uint_16(entrybuf + 0, ep[i].red);
830 png_save_uint_16(entrybuf + 2, ep[i].green);
831 png_save_uint_16(entrybuf + 4, ep[i].blue);
832 png_save_uint_16(entrybuf + 6, ep[i].alpha);
833 png_save_uint_16(entrybuf + 8, ep[i].frequency);
834 }
835 png_write_chunk_data(png_ptr, entrybuf, entry_size);
836 }
837 #endif
838
839 png_write_chunk_end(png_ptr);
840 png_free(png_ptr, new_name);
841 }
842 #endif
843
844 #if defined(PNG_WRITE_sBIT_SUPPORTED)
845 /* write the sBIT chunk */
846 void /* PRIVATE */
847 png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
848 {
849 #ifdef PNG_USE_LOCAL_ARRAYS
850 PNG_sBIT;
851 #endif
852 png_byte buf[4];
853 png_size_t size;
854
855 png_debug(1, "in png_write_sBIT\n");
856 /* make sure we don't depend upon the order of PNG_COLOR_8 */
857 if (color_type & PNG_COLOR_MASK_COLOR)
858 {
859 png_byte maxbits;
860
861 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
862 png_ptr->usr_bit_depth);
863 if (sbit->red == 0 || sbit->red > maxbits ||
864 sbit->green == 0 || sbit->green > maxbits ||
865 sbit->blue == 0 || sbit->blue > maxbits)
866 {
867 png_warning(png_ptr, "Invalid sBIT depth specified");
868 return;
869 }
870 buf[0] = sbit->red;
871 buf[1] = sbit->green;
872 buf[2] = sbit->blue;
873 size = 3;
874 }
875 else
876 {
877 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
878 {
879 png_warning(png_ptr, "Invalid sBIT depth specified");
880 return;
881 }
882 buf[0] = sbit->gray;
883 size = 1;
884 }
885
886 if (color_type & PNG_COLOR_MASK_ALPHA)
887 {
888 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
889 {
890 png_warning(png_ptr, "Invalid sBIT depth specified");
891 return;
892 }
893 buf[size++] = sbit->alpha;
894 }
895
896 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
897 }
898 #endif
899
900 #if defined(PNG_WRITE_cHRM_SUPPORTED)
901 /* write the cHRM chunk */
902 #ifdef PNG_FLOATING_POINT_SUPPORTED
903 void /* PRIVATE */
904 png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
905 double red_x, double red_y, double green_x, double green_y,
906 double blue_x, double blue_y)
907 {
908 #ifdef PNG_USE_LOCAL_ARRAYS
909 PNG_cHRM;
910 #endif
911 png_byte buf[32];
912 png_uint_32 itemp;
913
914 png_debug(1, "in png_write_cHRM\n");
915 /* each value is saved in 1/100,000ths */
916 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
917 white_x + white_y > 1.0)
918 {
919 png_warning(png_ptr, "Invalid cHRM white point specified");
920 #if !defined(PNG_NO_CONSOLE_IO)
921 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
922 #endif
923 return;
924 }
925 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
926 png_save_uint_32(buf, itemp);
927 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
928 png_save_uint_32(buf + 4, itemp);
929
930 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
931 red_x + red_y > 1.0)
932 {
933 png_warning(png_ptr, "Invalid cHRM red point specified");
934 return;
935 }
936 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
937 png_save_uint_32(buf + 8, itemp);
938 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
939 png_save_uint_32(buf + 12, itemp);
940
941 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
942 green_x + green_y > 1.0)
943 {
944 png_warning(png_ptr, "Invalid cHRM green point specified");
945 return;
946 }
947 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
948 png_save_uint_32(buf + 16, itemp);
949 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
950 png_save_uint_32(buf + 20, itemp);
951
952 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
953 blue_x + blue_y > 1.0)
954 {
955 png_warning(png_ptr, "Invalid cHRM blue point specified");
956 return;
957 }
958 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
959 png_save_uint_32(buf + 24, itemp);
960 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
961 png_save_uint_32(buf + 28, itemp);
962
963 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
964 }
965 #endif
966 #ifdef PNG_FIXED_POINT_SUPPORTED
967 void /* PRIVATE */
968 png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
969 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
970 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
971 png_fixed_point blue_y)
972 {
973 #ifdef PNG_USE_LOCAL_ARRAYS
974 PNG_cHRM;
975 #endif
976 png_byte buf[32];
977
978 png_debug(1, "in png_write_cHRM\n");
979 /* each value is saved in 1/100,000ths */
980 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
981 {
982 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
983 #if !defined(PNG_NO_CONSOLE_IO)
984 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
985 #endif
986 return;
987 }
988 png_save_uint_32(buf, (png_uint_32)white_x);
989 png_save_uint_32(buf + 4, (png_uint_32)white_y);
990
991 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
992 {
993 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
994 return;
995 }
996 png_save_uint_32(buf + 8, (png_uint_32)red_x);
997 png_save_uint_32(buf + 12, (png_uint_32)red_y);
998
999 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
1000 {
1001 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
1002 return;
1003 }
1004 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1005 png_save_uint_32(buf + 20, (png_uint_32)green_y);
1006
1007 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
1008 {
1009 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
1010 return;
1011 }
1012 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1013 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
1014
1015 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1016 }
1017 #endif
1018 #endif
1019
1020 #if defined(PNG_WRITE_tRNS_SUPPORTED)
1021 /* write the tRNS chunk */
1022 void /* PRIVATE */
1023 png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
1024 int num_trans, int color_type)
1025 {
1026 #ifdef PNG_USE_LOCAL_ARRAYS
1027 PNG_tRNS;
1028 #endif
1029 png_byte buf[6];
1030
1031 png_debug(1, "in png_write_tRNS\n");
1032 if (color_type == PNG_COLOR_TYPE_PALETTE)
1033 {
1034 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1035 {
1036 png_warning(png_ptr,"Invalid number of transparent colors specified");
1037 return;
1038 }
1039 /* write the chunk out as it is */
1040 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
1041 }
1042 else if (color_type == PNG_COLOR_TYPE_GRAY)
1043 {
1044 /* one 16 bit value */
1045 if(tran->gray >= (1 << png_ptr->bit_depth))
1046 {
1047 png_warning(png_ptr,
1048 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1049 return;
1050 }
1051 png_save_uint_16(buf, tran->gray);
1052 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
1053 }
1054 else if (color_type == PNG_COLOR_TYPE_RGB)
1055 {
1056 /* three 16 bit values */
1057 png_save_uint_16(buf, tran->red);
1058 png_save_uint_16(buf + 2, tran->green);
1059 png_save_uint_16(buf + 4, tran->blue);
1060 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1061 {
1062 png_warning(png_ptr,
1063 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1064 return;
1065 }
1066 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
1067 }
1068 else
1069 {
1070 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
1071 }
1072 }
1073 #endif
1074
1075 #if defined(PNG_WRITE_bKGD_SUPPORTED)
1076 /* write the background chunk */
1077 void /* PRIVATE */
1078 png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
1079 {
1080 #ifdef PNG_USE_LOCAL_ARRAYS
1081 PNG_bKGD;
1082 #endif
1083 png_byte buf[6];
1084
1085 png_debug(1, "in png_write_bKGD\n");
1086 if (color_type == PNG_COLOR_TYPE_PALETTE)
1087 {
1088 if (
1089 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1090 (png_ptr->num_palette ||
1091 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
1092 #endif
1093 back->index > png_ptr->num_palette)
1094 {
1095 png_warning(png_ptr, "Invalid background palette index");
1096 return;
1097 }
1098 buf[0] = back->index;
1099 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
1100 }
1101 else if (color_type & PNG_COLOR_MASK_COLOR)
1102 {
1103 png_save_uint_16(buf, back->red);
1104 png_save_uint_16(buf + 2, back->green);
1105 png_save_uint_16(buf + 4, back->blue);
1106 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1107 {
1108 png_warning(png_ptr,
1109 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1110 return;
1111 }
1112 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
1113 }
1114 else
1115 {
1116 if(back->gray >= (1 << png_ptr->bit_depth))
1117 {
1118 png_warning(png_ptr,
1119 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1120 return;
1121 }
1122 png_save_uint_16(buf, back->gray);
1123 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
1124 }
1125 }
1126 #endif
1127
1128 #if defined(PNG_WRITE_hIST_SUPPORTED)
1129 /* write the histogram */
1130 void /* PRIVATE */
1131 png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
1132 {
1133 #ifdef PNG_USE_LOCAL_ARRAYS
1134 PNG_hIST;
1135 #endif
1136 int i;
1137 png_byte buf[3];
1138
1139 png_debug(1, "in png_write_hIST\n");
1140 if (num_hist > (int)png_ptr->num_palette)
1141 {
1142 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1143 png_ptr->num_palette);
1144 png_warning(png_ptr, "Invalid number of histogram entries specified");
1145 return;
1146 }
1147
1148 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
1149 for (i = 0; i < num_hist; i++)
1150 {
1151 png_save_uint_16(buf, hist[i]);
1152 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
1153 }
1154 png_write_chunk_end(png_ptr);
1155 }
1156 #endif
1157
1158 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1159 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
1160 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1161 * and if invalid, correct the keyword rather than discarding the entire
1162 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1163 * length, forbids leading or trailing whitespace, multiple internal spaces,
1164 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
1165 *
1166 * The new_key is allocated to hold the corrected keyword and must be freed
1167 * by the calling routine. This avoids problems with trying to write to
1168 * static keywords without having to have duplicate copies of the strings.
1169 */
1170 png_size_t /* PRIVATE */
1171 png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
1172 {
1173 png_size_t key_len;
1174 png_charp kp, dp;
1175 int kflag;
1176 int kwarn=0;
1177
1178 png_debug(1, "in png_check_keyword\n");
1179 *new_key = NULL;
1180
1181 if (key == NULL || (key_len = png_strlen(key)) == 0)
1182 {
1183 png_warning(png_ptr, "zero length keyword");
1184 return ((png_size_t)0);
1185 }
1186
1187 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1188
1189 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1190 if (*new_key == NULL)
1191 {
1192 png_warning(png_ptr, "Out of memory while procesing keyword");
1193 return ((png_size_t)0);
1194 }
1195
1196 /* Replace non-printing characters with a blank and print a warning */
1197 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
1198 {
1199 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
1200 {
1201 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
1202 char msg[40];
1203
1204 sprintf(msg, "invalid keyword character 0x%02X", *kp);
1205 png_warning(png_ptr, msg);
1206 #else
1207 png_warning(png_ptr, "invalid character in keyword");
1208 #endif
1209 *dp = ' ';
1210 }
1211 else
1212 {
1213 *dp = *kp;
1214 }
1215 }
1216 *dp = '\0';
1217
1218 /* Remove any trailing white space. */
1219 kp = *new_key + key_len - 1;
1220 if (*kp == ' ')
1221 {
1222 png_warning(png_ptr, "trailing spaces removed from keyword");
1223
1224 while (*kp == ' ')
1225 {
1226 *(kp--) = '\0';
1227 key_len--;
1228 }
1229 }
1230
1231 /* Remove any leading white space. */
1232 kp = *new_key;
1233 if (*kp == ' ')
1234 {
1235 png_warning(png_ptr, "leading spaces removed from keyword");
1236
1237 while (*kp == ' ')
1238 {
1239 kp++;
1240 key_len--;
1241 }
1242 }
1243
1244 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
1245
1246 /* Remove multiple internal spaces. */
1247 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
1248 {
1249 if (*kp == ' ' && kflag == 0)
1250 {
1251 *(dp++) = *kp;
1252 kflag = 1;
1253 }
1254 else if (*kp == ' ')
1255 {
1256 key_len--;
1257 kwarn=1;
1258 }
1259 else
1260 {
1261 *(dp++) = *kp;
1262 kflag = 0;
1263 }
1264 }
1265 *dp = '\0';
1266 if(kwarn)
1267 png_warning(png_ptr, "extra interior spaces removed from keyword");
1268
1269 if (key_len == 0)
1270 {
1271 png_free(png_ptr, *new_key);
1272 *new_key=NULL;
1273 png_warning(png_ptr, "Zero length keyword");
1274 }
1275
1276 if (key_len > 79)
1277 {
1278 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
1279 new_key[79] = '\0';
1280 key_len = 79;
1281 }
1282
1283 return (key_len);
1284 }
1285 #endif
1286
1287 #if defined(PNG_WRITE_tEXt_SUPPORTED)
1288 /* write a tEXt chunk */
1289 void /* PRIVATE */
1290 png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
1291 png_size_t text_len)
1292 {
1293 #ifdef PNG_USE_LOCAL_ARRAYS
1294 PNG_tEXt;
1295 #endif
1296 png_size_t key_len;
1297 png_charp new_key;
1298
1299 png_debug(1, "in png_write_tEXt\n");
1300 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1301 {
1302 png_warning(png_ptr, "Empty keyword in tEXt chunk");
1303 return;
1304 }
1305
1306 if (text == NULL || *text == '\0')
1307 text_len = 0;
1308 else
1309 text_len = png_strlen(text);
1310
1311 /* make sure we include the 0 after the key */
1312 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
1313 /*
1314 * We leave it to the application to meet PNG-1.0 requirements on the
1315 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1316 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1317 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1318 */
1319 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1320 if (text_len)
1321 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
1322
1323 png_write_chunk_end(png_ptr);
1324 png_free(png_ptr, new_key);
1325 }
1326 #endif
1327
1328 #if defined(PNG_WRITE_zTXt_SUPPORTED)
1329 /* write a compressed text chunk */
1330 void /* PRIVATE */
1331 png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
1332 png_size_t text_len, int compression)
1333 {
1334 #ifdef PNG_USE_LOCAL_ARRAYS
1335 PNG_zTXt;
1336 #endif
1337 png_size_t key_len;
1338 char buf[1];
1339 png_charp new_key;
1340 compression_state comp;
1341
1342 png_debug(1, "in png_write_zTXt\n");
1343
1344 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1345 {
1346 png_warning(png_ptr, "Empty keyword in zTXt chunk");
1347 return;
1348 }
1349
1350 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1351 {
1352 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1353 png_free(png_ptr, new_key);
1354 return;
1355 }
1356
1357 text_len = png_strlen(text);
1358
1359 png_free(png_ptr, new_key);
1360
1361 /* compute the compressed data; do it now for the length */
1362 text_len = png_text_compress(png_ptr, text, text_len, compression,
1363 &comp);
1364
1365 /* write start of chunk */
1366 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1367 (key_len+text_len+2));
1368 /* write key */
1369 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
1370 buf[0] = (png_byte)compression;
1371 /* write compression */
1372 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
1373 /* write the compressed data */
1374 png_write_compressed_data_out(png_ptr, &comp);
1375
1376 /* close the chunk */
1377 png_write_chunk_end(png_ptr);
1378 }
1379 #endif
1380
1381 #if defined(PNG_WRITE_iTXt_SUPPORTED)
1382 /* write an iTXt chunk */
1383 void /* PRIVATE */
1384 png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
1385 png_charp lang, png_charp lang_key, png_charp text)
1386 {
1387 #ifdef PNG_USE_LOCAL_ARRAYS
1388 PNG_iTXt;
1389 #endif
1390 png_size_t lang_len, key_len, lang_key_len, text_len;
1391 png_charp new_lang, new_key;
1392 png_byte cbuf[2];
1393 compression_state comp;
1394
1395 png_debug(1, "in png_write_iTXt\n");
1396
1397 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1398 {
1399 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1400 return;
1401 }
1402 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
1403 {
1404 png_warning(png_ptr, "Empty language field in iTXt chunk");
1405 new_lang = NULL;
1406 lang_len = 0;
1407 }
1408
1409 if (lang_key == NULL)
1410 lang_key_len = 0;
1411 else
1412 lang_key_len = png_strlen(lang_key);
1413
1414 if (text == NULL)
1415 text_len = 0;
1416 else
1417 text_len = png_strlen(text);
1418
1419 /* compute the compressed data; do it now for the length */
1420 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1421 &comp);
1422
1423
1424 /* make sure we include the compression flag, the compression byte,
1425 * and the NULs after the key, lang, and lang_key parts */
1426
1427 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1428 (png_uint_32)(
1429 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1430 + key_len
1431 + lang_len
1432 + lang_key_len
1433 + text_len));
1434
1435 /*
1436 * We leave it to the application to meet PNG-1.0 requirements on the
1437 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1438 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1439 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1440 */
1441 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1442
1443 /* set the compression flag */
1444 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1445 compression == PNG_TEXT_COMPRESSION_NONE)
1446 cbuf[0] = 0;
1447 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1448 cbuf[0] = 1;
1449 /* set the compression method */
1450 cbuf[1] = 0;
1451 png_write_chunk_data(png_ptr, cbuf, 2);
1452
1453 cbuf[0] = 0;
1454 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1455 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
1456 png_write_compressed_data_out(png_ptr, &comp);
1457
1458 png_write_chunk_end(png_ptr);
1459 png_free(png_ptr, new_key);
1460 if (new_lang)
1461 png_free(png_ptr, new_lang);
1462 }
1463 #endif
1464
1465 #if defined(PNG_WRITE_oFFs_SUPPORTED)
1466 /* write the oFFs chunk */
1467 void /* PRIVATE */
1468 png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
1469 int unit_type)
1470 {
1471 #ifdef PNG_USE_LOCAL_ARRAYS
1472 PNG_oFFs;
1473 #endif
1474 png_byte buf[9];
1475
1476 png_debug(1, "in png_write_oFFs\n");
1477 if (unit_type >= PNG_OFFSET_LAST)
1478 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1479
1480 png_save_int_32(buf, x_offset);
1481 png_save_int_32(buf + 4, y_offset);
1482 buf[8] = (png_byte)unit_type;
1483
1484 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
1485 }
1486 #endif
1487
1488 #if defined(PNG_WRITE_pCAL_SUPPORTED)
1489 /* write the pCAL chunk (described in the PNG extensions document) */
1490 void /* PRIVATE */
1491 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1492 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1493 {
1494 #ifdef PNG_USE_LOCAL_ARRAYS
1495 PNG_pCAL;
1496 #endif
1497 png_size_t purpose_len, units_len, total_len;
1498 png_uint_32p params_len;
1499 png_byte buf[10];
1500 png_charp new_purpose;
1501 int i;
1502
1503 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1504 if (type >= PNG_EQUATION_LAST)
1505 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1506
1507 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1508 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
1509 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1510 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
1511 total_len = purpose_len + units_len + 10;
1512
1513 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1514 *png_sizeof(png_uint_32)));
1515
1516 /* Find the length of each parameter, making sure we don't count the
1517 null terminator for the last parameter. */
1518 for (i = 0; i < nparams; i++)
1519 {
1520 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1521 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
1522 total_len += (png_size_t)params_len[i];
1523 }
1524
1525 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
1526 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
1527 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
1528 png_save_int_32(buf, X0);
1529 png_save_int_32(buf + 4, X1);
1530 buf[8] = (png_byte)type;
1531 buf[9] = (png_byte)nparams;
1532 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1533 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1534
1535 png_free(png_ptr, new_purpose);
1536
1537 for (i = 0; i < nparams; i++)
1538 {
1539 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1540 (png_size_t)params_len[i]);
1541 }
1542
1543 png_free(png_ptr, params_len);
1544 png_write_chunk_end(png_ptr);
1545 }
1546 #endif
1547
1548 #if defined(PNG_WRITE_sCAL_SUPPORTED)
1549 /* write the sCAL chunk */
1550 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
1551 void /* PRIVATE */
1552 png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
1553 {
1554 #ifdef PNG_USE_LOCAL_ARRAYS
1555 PNG_sCAL;
1556 #endif
1557 png_size_t total_len;
1558 char wbuf[32], hbuf[32];
1559
1560 png_debug(1, "in png_write_sCAL\n");
1561
1562 #if defined(_WIN32_WCE)
1563 /* sprintf() function is not supported on WindowsCE */
1564 {
1565 wchar_t wc_buf[32];
1566 swprintf(wc_buf, TEXT("%12.12e"), width);
1567 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1568 swprintf(wc_buf, TEXT("%12.12e"), height);
1569 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1570 }
1571 #else
1572 sprintf(wbuf, "%12.12e", width);
1573 sprintf(hbuf, "%12.12e", height);
1574 #endif
1575 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1576
1577 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
1578 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1579 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
1580 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1581 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
1582
1583 png_write_chunk_end(png_ptr);
1584 }
1585 #else
1586 #ifdef PNG_FIXED_POINT_SUPPORTED
1587 void /* PRIVATE */
1588 png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
1589 png_charp height)
1590 {
1591 #ifdef PNG_USE_LOCAL_ARRAYS
1592 PNG_sCAL;
1593 #endif
1594 png_size_t total_len;
1595 char wbuf[32], hbuf[32];
1596
1597 png_debug(1, "in png_write_sCAL_s\n");
1598
1599 png_strcpy(wbuf,(const char *)width);
1600 png_strcpy(hbuf,(const char *)height);
1601 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1602
1603 png_debug1(3, "sCAL total length = %d\n", total_len);
1604 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1605 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
1606 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1607 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
1608
1609 png_write_chunk_end(png_ptr);
1610 }
1611 #endif
1612 #endif
1613 #endif
1614
1615 #if defined(PNG_WRITE_pHYs_SUPPORTED)
1616 /* write the pHYs chunk */
1617 void /* PRIVATE */
1618 png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
1619 png_uint_32 y_pixels_per_unit,
1620 int unit_type)
1621 {
1622 #ifdef PNG_USE_LOCAL_ARRAYS
1623 PNG_pHYs;
1624 #endif
1625 png_byte buf[9];
1626
1627 png_debug(1, "in png_write_pHYs\n");
1628 if (unit_type >= PNG_RESOLUTION_LAST)
1629 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1630
1631 png_save_uint_32(buf, x_pixels_per_unit);
1632 png_save_uint_32(buf + 4, y_pixels_per_unit);
1633 buf[8] = (png_byte)unit_type;
1634
1635 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
1636 }
1637 #endif
1638
1639 #if defined(PNG_WRITE_tIME_SUPPORTED)
1640 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
1641 * or png_convert_from_time_t(), or fill in the structure yourself.
1642 */
1643 void /* PRIVATE */
1644 png_write_tIME(png_structp png_ptr, png_timep mod_time)
1645 {
1646 #ifdef PNG_USE_LOCAL_ARRAYS
1647 PNG_tIME;
1648 #endif
1649 png_byte buf[7];
1650
1651 png_debug(1, "in png_write_tIME\n");
1652 if (mod_time->month > 12 || mod_time->month < 1 ||
1653 mod_time->day > 31 || mod_time->day < 1 ||
1654 mod_time->hour > 23 || mod_time->second > 60)
1655 {
1656 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1657 return;
1658 }
1659
1660 png_save_uint_16(buf, mod_time->year);
1661 buf[2] = mod_time->month;
1662 buf[3] = mod_time->day;
1663 buf[4] = mod_time->hour;
1664 buf[5] = mod_time->minute;
1665 buf[6] = mod_time->second;
1666
1667 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
1668 }
1669 #endif
1670
1671 /* initializes the row writing capability of libpng */
1672 void /* PRIVATE */
1673 png_write_start_row(png_structp png_ptr)
1674 {
1675 #ifdef PNG_USE_LOCAL_ARRAYS
1676 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1677
1678 /* start of interlace block */
1679 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1680
1681 /* offset to next interlace block */
1682 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1683
1684 /* start of interlace block in the y direction */
1685 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1686
1687 /* offset to next interlace block in the y direction */
1688 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1689 #endif
1690
1691 png_size_t buf_size;
1692
1693 png_debug(1, "in png_write_start_row\n");
1694 buf_size = (png_size_t)(PNG_ROWBYTES(
1695 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
1696
1697 /* set up row buffer */
1698 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1699 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1700
1701 /* set up filtering buffer, if using this filter */
1702 if (png_ptr->do_filter & PNG_FILTER_SUB)
1703 {
1704 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1705 (png_ptr->rowbytes + 1));
1706 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1707 }
1708
1709 /* We only need to keep the previous row if we are using one of these. */
1710 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1711 {
1712 /* set up previous row buffer */
1713 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1714 png_memset(png_ptr->prev_row, 0, buf_size);
1715
1716 if (png_ptr->do_filter & PNG_FILTER_UP)
1717 {
1718 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
1719 (png_ptr->rowbytes + 1));
1720 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1721 }
1722
1723 if (png_ptr->do_filter & PNG_FILTER_AVG)
1724 {
1725 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1726 (png_ptr->rowbytes + 1));
1727 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1728 }
1729
1730 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1731 {
1732 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
1733 (png_ptr->rowbytes + 1));
1734 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1735 }
1736 }
1737
1738 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1739 /* if interlaced, we need to set up width and height of pass */
1740 if (png_ptr->interlaced)
1741 {
1742 if (!(png_ptr->transformations & PNG_INTERLACE))
1743 {
1744 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1745 png_pass_ystart[0]) / png_pass_yinc[0];
1746 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1747 png_pass_start[0]) / png_pass_inc[0];
1748 }
1749 else
1750 {
1751 png_ptr->num_rows = png_ptr->height;
1752 png_ptr->usr_width = png_ptr->width;
1753 }
1754 }
1755 else
1756 #endif
1757 {
1758 png_ptr->num_rows = png_ptr->height;
1759 png_ptr->usr_width = png_ptr->width;
1760 }
1761 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1762 png_ptr->zstream.next_out = png_ptr->zbuf;
1763 }
1764
1765 /* Internal use only. Called when finished processing a row of data. */
1766 void /* PRIVATE */
1767 png_write_finish_row(png_structp png_ptr)
1768 {
1769 #ifdef PNG_USE_LOCAL_ARRAYS
1770 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1771
1772 /* start of interlace block */
1773 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1774
1775 /* offset to next interlace block */
1776 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1777
1778 /* start of interlace block in the y direction */
1779 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1780
1781 /* offset to next interlace block in the y direction */
1782 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1783 #endif
1784
1785 int ret;
1786
1787 png_debug(1, "in png_write_finish_row\n");
1788 /* next row */
1789 png_ptr->row_number++;
1790
1791 /* see if we are done */
1792 if (png_ptr->row_number < png_ptr->num_rows)
1793 return;
1794
1795 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1796 /* if interlaced, go to next pass */
1797 if (png_ptr->interlaced)
1798 {
1799 png_ptr->row_number = 0;
1800 if (png_ptr->transformations & PNG_INTERLACE)
1801 {
1802 png_ptr->pass++;
1803 }
1804 else
1805 {
1806 /* loop until we find a non-zero width or height pass */
1807 do
1808 {
1809 png_ptr->pass++;
1810 if (png_ptr->pass >= 7)
1811 break;
1812 png_ptr->usr_width = (png_ptr->width +
1813 png_pass_inc[png_ptr->pass] - 1 -
1814 png_pass_start[png_ptr->pass]) /
1815 png_pass_inc[png_ptr->pass];
1816 png_ptr->num_rows = (png_ptr->height +
1817 png_pass_yinc[png_ptr->pass] - 1 -
1818 png_pass_ystart[png_ptr->pass]) /
1819 png_pass_yinc[png_ptr->pass];
1820 if (png_ptr->transformations & PNG_INTERLACE)
1821 break;
1822 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1823
1824 }
1825
1826 /* reset the row above the image for the next pass */
1827 if (png_ptr->pass < 7)
1828 {
1829 if (png_ptr->prev_row != NULL)
1830 png_memset(png_ptr->prev_row, 0,
1831 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1832 png_ptr->usr_bit_depth,png_ptr->width))+1);
1833 return;
1834 }
1835 }
1836 #endif
1837
1838 /* if we get here, we've just written the last row, so we need
1839 to flush the compressor */
1840 do
1841 {
1842 /* tell the compressor we are done */
1843 ret = deflate(&png_ptr->zstream, Z_FINISH);
1844 /* check for an error */
1845 if (ret == Z_OK)
1846 {
1847 /* check to see if we need more room */
1848 if (!(png_ptr->zstream.avail_out))
1849 {
1850 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1851 png_ptr->zstream.next_out = png_ptr->zbuf;
1852 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1853 }
1854 }
1855 else if (ret != Z_STREAM_END)
1856 {
1857 if (png_ptr->zstream.msg != NULL)
1858 png_error(png_ptr, png_ptr->zstream.msg);
1859 else
1860 png_error(png_ptr, "zlib error");
1861 }
1862 } while (ret != Z_STREAM_END);
1863
1864 /* write any extra space */
1865 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
1866 {
1867 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
1868 png_ptr->zstream.avail_out);
1869 }
1870
1871 deflateReset(&png_ptr->zstream);
1872 }
1873
1874 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
1875 /* Pick out the correct pixels for the interlace pass.
1876 * The basic idea here is to go through the row with a source
1877 * pointer and a destination pointer (sp and dp), and copy the
1878 * correct pixels for the pass. As the row gets compacted,
1879 * sp will always be >= dp, so we should never overwrite anything.
1880 * See the default: case for the easiest code to understand.
1881 */
1882 void /* PRIVATE */
1883 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
1884 {
1885 #ifdef PNG_USE_LOCAL_ARRAYS
1886 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1887
1888 /* start of interlace block */
1889 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1890
1891 /* offset to next interlace block */
1892 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1893 #endif
1894
1895 png_debug(1, "in png_do_write_interlace\n");
1896 /* we don't have to do anything on the last pass (6) */
1897 #if defined(PNG_USELESS_TESTS_SUPPORTED)
1898 if (row != NULL && row_info != NULL && pass < 6)
1899 #else
1900 if (pass < 6)
1901 #endif
1902 {
1903 /* each pixel depth is handled separately */
1904 switch (row_info->pixel_depth)
1905 {
1906 case 1:
1907 {
1908 png_bytep sp;
1909 png_bytep dp;
1910 int shift;
1911 int d;
1912 int value;
1913 png_uint_32 i;
1914 png_uint_32 row_width = row_info->width;
1915
1916 dp = row;
1917 d = 0;
1918 shift = 7;
1919 for (i = png_pass_start[pass]; i < row_width;
1920 i += png_pass_inc[pass])
1921 {
1922 sp = row + (png_size_t)(i >> 3);
1923 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
1924 d |= (value << shift);
1925
1926 if (shift == 0)
1927 {
1928 shift = 7;
1929 *dp++ = (png_byte)d;
1930 d = 0;
1931 }
1932 else
1933 shift--;
1934
1935 }
1936 if (shift != 7)
1937 *dp = (png_byte)d;
1938 break;
1939 }
1940 case 2:
1941 {
1942 png_bytep sp;
1943 png_bytep dp;
1944 int shift;
1945 int d;
1946 int value;
1947 png_uint_32 i;
1948 png_uint_32 row_width = row_info->width;
1949
1950 dp = row;
1951 shift = 6;
1952 d = 0;
1953 for (i = png_pass_start[pass]; i < row_width;
1954 i += png_pass_inc[pass])
1955 {
1956 sp = row + (png_size_t)(i >> 2);
1957 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
1958 d |= (value << shift);
1959
1960 if (shift == 0)
1961 {
1962 shift = 6;
1963 *dp++ = (png_byte)d;
1964 d = 0;
1965 }
1966 else
1967 shift -= 2;
1968 }
1969 if (shift != 6)
1970 *dp = (png_byte)d;
1971 break;
1972 }
1973 case 4:
1974 {
1975 png_bytep sp;
1976 png_bytep dp;
1977 int shift;
1978 int d;
1979 int value;
1980 png_uint_32 i;
1981 png_uint_32 row_width = row_info->width;
1982
1983 dp = row;
1984 shift = 4;
1985 d = 0;
1986 for (i = png_pass_start[pass]; i < row_width;
1987 i += png_pass_inc[pass])
1988 {
1989 sp = row + (png_size_t)(i >> 1);
1990 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
1991 d |= (value << shift);
1992
1993 if (shift == 0)
1994 {
1995 shift = 4;
1996 *dp++ = (png_byte)d;
1997 d = 0;
1998 }
1999 else
2000 shift -= 4;
2001 }
2002 if (shift != 4)
2003 *dp = (png_byte)d;
2004 break;
2005 }
2006 default:
2007 {
2008 png_bytep sp;
2009 png_bytep dp;
2010 png_uint_32 i;
2011 png_uint_32 row_width = row_info->width;
2012 png_size_t pixel_bytes;
2013
2014 /* start at the beginning */
2015 dp = row;
2016 /* find out how many bytes each pixel takes up */
2017 pixel_bytes = (row_info->pixel_depth >> 3);
2018 /* loop through the row, only looking at the pixels that
2019 matter */
2020 for (i = png_pass_start[pass]; i < row_width;
2021 i += png_pass_inc[pass])
2022 {
2023 /* find out where the original pixel is */
2024 sp = row + (png_size_t)i * pixel_bytes;
2025 /* move the pixel */
2026 if (dp != sp)
2027 png_memcpy(dp, sp, pixel_bytes);
2028 /* next pixel */
2029 dp += pixel_bytes;
2030 }
2031 break;
2032 }
2033 }
2034 /* set new row width */
2035 row_info->width = (row_info->width +
2036 png_pass_inc[pass] - 1 -
2037 png_pass_start[pass]) /
2038 png_pass_inc[pass];
2039 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2040 row_info->width);
2041 }
2042 }
2043 #endif
2044
2045 /* This filters the row, chooses which filter to use, if it has not already
2046 * been specified by the application, and then writes the row out with the
2047 * chosen filter.
2048 */
2049 #define PNG_MAXSUM (~((png_uint_32)0) >> 1)
2050 #define PNG_HISHIFT 10
2051 #define PNG_LOMASK ((png_uint_32)0xffffL)
2052 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2053 void /* PRIVATE */
2054 png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
2055 {
2056 png_bytep prev_row, best_row, row_buf;
2057 png_uint_32 mins, bpp;
2058 png_byte filter_to_do = png_ptr->do_filter;
2059 png_uint_32 row_bytes = row_info->rowbytes;
2060 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2061 int num_p_filters = (int)png_ptr->num_prev_filters;
2062 #endif
2063
2064 png_debug(1, "in png_write_find_filter\n");
2065 /* find out how many bytes offset each pixel is */
2066 bpp = (row_info->pixel_depth + 7) >> 3;
2067
2068 prev_row = png_ptr->prev_row;
2069 best_row = row_buf = png_ptr->row_buf;
2070 mins = PNG_MAXSUM;
2071
2072 /* The prediction method we use is to find which method provides the
2073 * smallest value when summing the absolute values of the distances
2074 * from zero, using anything >= 128 as negative numbers. This is known
2075 * as the "minimum sum of absolute differences" heuristic. Other
2076 * heuristics are the "weighted minimum sum of absolute differences"
2077 * (experimental and can in theory improve compression), and the "zlib
2078 * predictive" method (not implemented yet), which does test compressions
2079 * of lines using different filter methods, and then chooses the
2080 * (series of) filter(s) that give minimum compressed data size (VERY
2081 * computationally expensive).
2082 *
2083 * GRR 980525: consider also
2084 * (1) minimum sum of absolute differences from running average (i.e.,
2085 * keep running sum of non-absolute differences & count of bytes)
2086 * [track dispersion, too? restart average if dispersion too large?]
2087 * (1b) minimum sum of absolute differences from sliding average, probably
2088 * with window size <= deflate window (usually 32K)
2089 * (2) minimum sum of squared differences from zero or running average
2090 * (i.e., ~ root-mean-square approach)
2091 */
2092
2093
2094 /* We don't need to test the 'no filter' case if this is the only filter
2095 * that has been chosen, as it doesn't actually do anything to the data.
2096 */
2097 if ((filter_to_do & PNG_FILTER_NONE) &&
2098 filter_to_do != PNG_FILTER_NONE)
2099 {
2100 png_bytep rp;
2101 png_uint_32 sum = 0;
2102 png_uint_32 i;
2103 int v;
2104
2105 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2106 {
2107 v = *rp;
2108 sum += (v < 128) ? v : 256 - v;
2109 }
2110
2111 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2112 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2113 {
2114 png_uint_32 sumhi, sumlo;
2115 int j;
2116 sumlo = sum & PNG_LOMASK;
2117 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2118
2119 /* Reduce the sum if we match any of the previous rows */
2120 for (j = 0; j < num_p_filters; j++)
2121 {
2122 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2123 {
2124 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2125 PNG_WEIGHT_SHIFT;
2126 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2127 PNG_WEIGHT_SHIFT;
2128 }
2129 }
2130
2131 /* Factor in the cost of this filter (this is here for completeness,
2132 * but it makes no sense to have a "cost" for the NONE filter, as
2133 * it has the minimum possible computational cost - none).
2134 */
2135 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2136 PNG_COST_SHIFT;
2137 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2138 PNG_COST_SHIFT;
2139
2140 if (sumhi > PNG_HIMASK)
2141 sum = PNG_MAXSUM;
2142 else
2143 sum = (sumhi << PNG_HISHIFT) + sumlo;
2144 }
2145 #endif
2146 mins = sum;
2147 }
2148
2149 /* sub filter */
2150 if (filter_to_do == PNG_FILTER_SUB)
2151 /* it's the only filter so no testing is needed */
2152 {
2153 png_bytep rp, lp, dp;
2154 png_uint_32 i;
2155 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2156 i++, rp++, dp++)
2157 {
2158 *dp = *rp;
2159 }
2160 for (lp = row_buf + 1; i < row_bytes;
2161 i++, rp++, lp++, dp++)
2162 {
2163 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2164 }
2165 best_row = png_ptr->sub_row;
2166 }
2167
2168 else if (filter_to_do & PNG_FILTER_SUB)
2169 {
2170 png_bytep rp, dp, lp;
2171 png_uint_32 sum = 0, lmins = mins;
2172 png_uint_32 i;
2173 int v;
2174
2175 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2176 /* We temporarily increase the "minimum sum" by the factor we
2177 * would reduce the sum of this filter, so that we can do the
2178 * early exit comparison without scaling the sum each time.
2179 */
2180 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2181 {
2182 int j;
2183 png_uint_32 lmhi, lmlo;
2184 lmlo = lmins & PNG_LOMASK;
2185 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2186
2187 for (j = 0; j < num_p_filters; j++)
2188 {
2189 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2190 {
2191 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2192 PNG_WEIGHT_SHIFT;
2193 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2194 PNG_WEIGHT_SHIFT;
2195 }
2196 }
2197
2198 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2199 PNG_COST_SHIFT;
2200 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2201 PNG_COST_SHIFT;
2202
2203 if (lmhi > PNG_HIMASK)
2204 lmins = PNG_MAXSUM;
2205 else
2206 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2207 }
2208 #endif
2209
2210 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2211 i++, rp++, dp++)
2212 {
2213 v = *dp = *rp;
2214
2215 sum += (v < 128) ? v : 256 - v;
2216 }
2217 for (lp = row_buf + 1; i < row_bytes;
2218 i++, rp++, lp++, dp++)
2219 {
2220 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2221
2222 sum += (v < 128) ? v : 256 - v;
2223
2224 if (sum > lmins) /* We are already worse, don't continue. */
2225 break;
2226 }
2227
2228 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2229 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2230 {
2231 int j;
2232 png_uint_32 sumhi, sumlo;
2233 sumlo = sum & PNG_LOMASK;
2234 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2235
2236 for (j = 0; j < num_p_filters; j++)
2237 {
2238 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2239 {
2240 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
2241 PNG_WEIGHT_SHIFT;
2242 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
2243 PNG_WEIGHT_SHIFT;
2244 }
2245 }
2246
2247 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2248 PNG_COST_SHIFT;
2249 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2250 PNG_COST_SHIFT;
2251
2252 if (sumhi > PNG_HIMASK)
2253 sum = PNG_MAXSUM;
2254 else
2255 sum = (sumhi << PNG_HISHIFT) + sumlo;
2256 }
2257 #endif
2258
2259 if (sum < mins)
2260 {
2261 mins = sum;
2262 best_row = png_ptr->sub_row;
2263 }
2264 }
2265
2266 /* up filter */
2267 if (filter_to_do == PNG_FILTER_UP)
2268 {
2269 png_bytep rp, dp, pp;
2270 png_uint_32 i;
2271
2272 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2273 pp = prev_row + 1; i < row_bytes;
2274 i++, rp++, pp++, dp++)
2275 {
2276 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2277 }
2278 best_row = png_ptr->up_row;
2279 }
2280
2281 else if (filter_to_do & PNG_FILTER_UP)
2282 {
2283 png_bytep rp, dp, pp;
2284 png_uint_32 sum = 0, lmins = mins;
2285 png_uint_32 i;
2286 int v;
2287
2288
2289 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2290 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2291 {
2292 int j;
2293 png_uint_32 lmhi, lmlo;
2294 lmlo = lmins & PNG_LOMASK;
2295 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2296
2297 for (j = 0; j < num_p_filters; j++)
2298 {
2299 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2300 {
2301 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2302 PNG_WEIGHT_SHIFT;
2303 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2304 PNG_WEIGHT_SHIFT;
2305 }
2306 }
2307
2308 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2309 PNG_COST_SHIFT;
2310 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2311 PNG_COST_SHIFT;
2312
2313 if (lmhi > PNG_HIMASK)
2314 lmins = PNG_MAXSUM;
2315 else
2316 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2317 }
2318 #endif
2319
2320 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2321 pp = prev_row + 1; i < row_bytes; i++)
2322 {
2323 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2324
2325 sum += (v < 128) ? v : 256 - v;
2326
2327 if (sum > lmins) /* We are already worse, don't continue. */
2328 break;
2329 }
2330
2331 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2332 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2333 {
2334 int j;
2335 png_uint_32 sumhi, sumlo;
2336 sumlo = sum & PNG_LOMASK;
2337 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2338
2339 for (j = 0; j < num_p_filters; j++)
2340 {
2341 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2342 {
2343 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2344 PNG_WEIGHT_SHIFT;
2345 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2346 PNG_WEIGHT_SHIFT;
2347 }
2348 }
2349
2350 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2351 PNG_COST_SHIFT;
2352 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2353 PNG_COST_SHIFT;
2354
2355 if (sumhi > PNG_HIMASK)
2356 sum = PNG_MAXSUM;
2357 else
2358 sum = (sumhi << PNG_HISHIFT) + sumlo;
2359 }
2360 #endif
2361
2362 if (sum < mins)
2363 {
2364 mins = sum;
2365 best_row = png_ptr->up_row;
2366 }
2367 }
2368
2369 /* avg filter */
2370 if (filter_to_do == PNG_FILTER_AVG)
2371 {
2372 png_bytep rp, dp, pp, lp;
2373 png_uint_32 i;
2374 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2375 pp = prev_row + 1; i < bpp; i++)
2376 {
2377 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2378 }
2379 for (lp = row_buf + 1; i < row_bytes; i++)
2380 {
2381 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2382 & 0xff);
2383 }
2384 best_row = png_ptr->avg_row;
2385 }
2386
2387 else if (filter_to_do & PNG_FILTER_AVG)
2388 {
2389 png_bytep rp, dp, pp, lp;
2390 png_uint_32 sum = 0, lmins = mins;
2391 png_uint_32 i;
2392 int v;
2393
2394 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2395 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2396 {
2397 int j;
2398 png_uint_32 lmhi, lmlo;
2399 lmlo = lmins & PNG_LOMASK;
2400 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2401
2402 for (j = 0; j < num_p_filters; j++)
2403 {
2404 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
2405 {
2406 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2407 PNG_WEIGHT_SHIFT;
2408 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2409 PNG_WEIGHT_SHIFT;
2410 }
2411 }
2412
2413 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2414 PNG_COST_SHIFT;
2415 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2416 PNG_COST_SHIFT;
2417
2418 if (lmhi > PNG_HIMASK)
2419 lmins = PNG_MAXSUM;
2420 else
2421 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2422 }
2423 #endif
2424
2425 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2426 pp = prev_row + 1; i < bpp; i++)
2427 {
2428 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2429
2430 sum += (v < 128) ? v : 256 - v;
2431 }
2432 for (lp = row_buf + 1; i < row_bytes; i++)
2433 {
2434 v = *dp++ =
2435 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2436
2437 sum += (v < 128) ? v : 256 - v;
2438
2439 if (sum > lmins) /* We are already worse, don't continue. */
2440 break;
2441 }
2442
2443 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2444 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2445 {
2446 int j;
2447 png_uint_32 sumhi, sumlo;
2448 sumlo = sum & PNG_LOMASK;
2449 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2450
2451 for (j = 0; j < num_p_filters; j++)
2452 {
2453 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2454 {
2455 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2456 PNG_WEIGHT_SHIFT;
2457 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2458 PNG_WEIGHT_SHIFT;
2459 }
2460 }
2461
2462 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2463 PNG_COST_SHIFT;
2464 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2465 PNG_COST_SHIFT;
2466
2467 if (sumhi > PNG_HIMASK)
2468 sum = PNG_MAXSUM;
2469 else
2470 sum = (sumhi << PNG_HISHIFT) + sumlo;
2471 }
2472 #endif
2473
2474 if (sum < mins)
2475 {
2476 mins = sum;
2477 best_row = png_ptr->avg_row;
2478 }
2479 }
2480
2481 /* Paeth filter */
2482 if (filter_to_do == PNG_FILTER_PAETH)
2483 {
2484 png_bytep rp, dp, pp, cp, lp;
2485 png_uint_32 i;
2486 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2487 pp = prev_row + 1; i < bpp; i++)
2488 {
2489 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2490 }
2491
2492 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2493 {
2494 int a, b, c, pa, pb, pc, p;
2495
2496 b = *pp++;
2497 c = *cp++;
2498 a = *lp++;
2499
2500 p = b - c;
2501 pc = a - c;
2502
2503 #ifdef PNG_USE_ABS
2504 pa = abs(p);
2505 pb = abs(pc);
2506 pc = abs(p + pc);
2507 #else
2508 pa = p < 0 ? -p : p;
2509 pb = pc < 0 ? -pc : pc;
2510 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2511 #endif
2512
2513 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2514
2515 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2516 }
2517 best_row = png_ptr->paeth_row;
2518 }
2519
2520 else if (filter_to_do & PNG_FILTER_PAETH)
2521 {
2522 png_bytep rp, dp, pp, cp, lp;
2523 png_uint_32 sum = 0, lmins = mins;
2524 png_uint_32 i;
2525 int v;
2526
2527 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2528 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2529 {
2530 int j;
2531 png_uint_32 lmhi, lmlo;
2532 lmlo = lmins & PNG_LOMASK;
2533 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2534
2535 for (j = 0; j < num_p_filters; j++)
2536 {
2537 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2538 {
2539 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2540 PNG_WEIGHT_SHIFT;
2541 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2542 PNG_WEIGHT_SHIFT;
2543 }
2544 }
2545
2546 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2547 PNG_COST_SHIFT;
2548 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2549 PNG_COST_SHIFT;
2550
2551 if (lmhi > PNG_HIMASK)
2552 lmins = PNG_MAXSUM;
2553 else
2554 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2555 }
2556 #endif
2557
2558 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2559 pp = prev_row + 1; i < bpp; i++)
2560 {
2561 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2562
2563 sum += (v < 128) ? v : 256 - v;
2564 }
2565
2566 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2567 {
2568 int a, b, c, pa, pb, pc, p;
2569
2570 b = *pp++;
2571 c = *cp++;
2572 a = *lp++;
2573
2574 #ifndef PNG_SLOW_PAETH
2575 p = b - c;
2576 pc = a - c;
2577 #ifdef PNG_USE_ABS
2578 pa = abs(p);
2579 pb = abs(pc);
2580 pc = abs(p + pc);
2581 #else
2582 pa = p < 0 ? -p : p;
2583 pb = pc < 0 ? -pc : pc;
2584 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2585 #endif
2586 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2587 #else /* PNG_SLOW_PAETH */
2588 p = a + b - c;
2589 pa = abs(p - a);
2590 pb = abs(p - b);
2591 pc = abs(p - c);
2592 if (pa <= pb && pa <= pc)
2593 p = a;
2594 else if (pb <= pc)
2595 p = b;
2596 else
2597 p = c;
2598 #endif /* PNG_SLOW_PAETH */
2599
2600 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2601
2602 sum += (v < 128) ? v : 256 - v;
2603
2604 if (sum > lmins) /* We are already worse, don't continue. */
2605 break;
2606 }
2607
2608 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2609 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2610 {
2611 int j;
2612 png_uint_32 sumhi, sumlo;
2613 sumlo = sum & PNG_LOMASK;
2614 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2615
2616 for (j = 0; j < num_p_filters; j++)
2617 {
2618 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2619 {
2620 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2621 PNG_WEIGHT_SHIFT;
2622 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2623 PNG_WEIGHT_SHIFT;
2624 }
2625 }
2626
2627 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2628 PNG_COST_SHIFT;
2629 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2630 PNG_COST_SHIFT;
2631
2632 if (sumhi > PNG_HIMASK)
2633 sum = PNG_MAXSUM;
2634 else
2635 sum = (sumhi << PNG_HISHIFT) + sumlo;
2636 }
2637 #endif
2638
2639 if (sum < mins)
2640 {
2641 best_row = png_ptr->paeth_row;
2642 }
2643 }
2644
2645 /* Do the actual writing of the filtered row data from the chosen filter. */
2646
2647 png_write_filtered_row(png_ptr, best_row);
2648
2649 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2650 /* Save the type of filter we picked this time for future calculations */
2651 if (png_ptr->num_prev_filters > 0)
2652 {
2653 int j;
2654 for (j = 1; j < num_p_filters; j++)
2655 {
2656 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2657 }
2658 png_ptr->prev_filters[j] = best_row[0];
2659 }
2660 #endif
2661 }
2662
2663
2664 /* Do the actual writing of a previously filtered row. */
2665 void /* PRIVATE */
2666 png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2667 {
2668 png_debug(1, "in png_write_filtered_row\n");
2669 png_debug1(2, "filter = %d\n", filtered_row[0]);
2670 /* set up the zlib input buffer */
2671
2672 png_ptr->zstream.next_in = filtered_row;
2673 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
2674 /* repeat until we have compressed all the data */
2675 do
2676 {
2677 int ret; /* return of zlib */
2678
2679 /* compress the data */
2680 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
2681 /* check for compression errors */
2682 if (ret != Z_OK)
2683 {
2684 if (png_ptr->zstream.msg != NULL)
2685 png_error(png_ptr, png_ptr->zstream.msg);
2686 else
2687 png_error(png_ptr, "zlib error");
2688 }
2689
2690 /* see if it is time to write another IDAT */
2691 if (!(png_ptr->zstream.avail_out))
2692 {
2693 /* write the IDAT and reset the zlib output buffer */
2694 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2695 png_ptr->zstream.next_out = png_ptr->zbuf;
2696 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2697 }
2698 /* repeat until all data has been compressed */
2699 } while (png_ptr->zstream.avail_in);
2700
2701 /* swap the current and previous rows */
2702 if (png_ptr->prev_row != NULL)
2703 {
2704 png_bytep tptr;
2705
2706 tptr = png_ptr->prev_row;
2707 png_ptr->prev_row = png_ptr->row_buf;
2708 png_ptr->row_buf = tptr;
2709 }
2710
2711 /* finish row - updates counters and flushes zlib if last row */
2712 png_write_finish_row(png_ptr);
2713
2714 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
2715 png_ptr->flush_rows++;
2716
2717 if (png_ptr->flush_dist > 0 &&
2718 png_ptr->flush_rows >= png_ptr->flush_dist)
2719 {
2720 png_write_flush(png_ptr);
2721 }
2722 #endif
2723 }
2724 #endif /* PNG_WRITE_SUPPORTED */