2
/* pngwutil.c - utilities to write a PNG file
4
* libpng version 1.2.7 - September 12, 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.)
13
#ifdef PNG_WRITE_SUPPORTED
15
/* Place a 32-bit number into a buffer in PNG byte order. We work
16
* with unsigned numbers for convenience, although one supported
17
* ancillary chunk uses signed (two's complement) numbers.
20
png_save_uint_32(png_bytep buf, png_uint_32 i)
22
buf[0] = (png_byte)((i >> 24) & 0xff);
23
buf[1] = (png_byte)((i >> 16) & 0xff);
24
buf[2] = (png_byte)((i >> 8) & 0xff);
25
buf[3] = (png_byte)(i & 0xff);
28
#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.
34
png_save_int_32(png_bytep buf, png_int_32 i)
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);
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.
48
png_save_uint_16(png_bytep buf, unsigned int i)
50
buf[0] = (png_byte)((i >> 8) & 0xff);
51
buf[1] = (png_byte)(i & 0xff);
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()
64
png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
65
png_bytep data, png_size_t length)
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);
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().
77
png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
81
png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
83
/* write the length */
84
png_save_uint_32(buf, length);
85
png_write_data(png_ptr, buf, (png_size_t)4);
87
/* write the chunk name */
88
png_write_data(png_ptr, chunk_name, (png_size_t)4);
89
/* reset the crc and run it over the chunk name */
90
png_reset_crc(png_ptr);
91
png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
94
/* Write the data of a PNG chunk started with png_write_chunk_start().
95
* Note that multiple calls to this function are allowed, and that the
96
* sum of the lengths from these calls *must* add up to the total_length
97
* given to png_write_chunk_start().
100
png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
102
/* write the data, and run the CRC over it */
103
if (data != NULL && length > 0)
105
png_calculate_crc(png_ptr, data, length);
106
png_write_data(png_ptr, data, length);
110
/* Finish a chunk started with png_write_chunk_start(). */
112
png_write_chunk_end(png_structp png_ptr)
117
png_save_uint_32(buf, png_ptr->crc);
119
png_write_data(png_ptr, buf, (png_size_t)4);
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.
129
png_write_sig(png_structp png_ptr)
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;
139
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
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.
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 */
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)
164
comp->num_output_ptr = comp->max_output_ptr = 0;
165
comp->output_ptr = NULL;
168
/* we may just want to pass the text right through */
169
if (compression == PNG_TEXT_COMPRESSION_NONE)
172
comp->input_len = text_len;
173
return((int)text_len);
176
if (compression >= PNG_TEXT_COMPRESSION_LAST)
178
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
180
sprintf(msg, "Unknown compression type %d", compression);
181
png_warning(png_ptr, msg);
183
png_warning(png_ptr, "Unknown compression type");
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.
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).
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;
208
/* this is the same compression loop as in png_write_row() */
211
/* compress the data */
212
ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
216
if (png_ptr->zstream.msg != NULL)
217
png_error(png_ptr, png_ptr->zstream.msg);
219
png_error(png_ptr, "zlib error");
221
/* check to see if we need more room */
222
if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
224
/* make sure the output array has room */
225
if (comp->num_output_ptr >= comp->max_output_ptr)
229
old_max = comp->max_output_ptr;
230
comp->max_output_ptr = comp->num_output_ptr + 4;
231
if (comp->output_ptr != NULL)
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);
244
comp->output_ptr = (png_charpp)png_malloc(png_ptr,
245
(png_uint_32)(comp->max_output_ptr *
246
png_sizeof (png_charp)));
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,
254
comp->num_output_ptr++;
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;
260
/* continue until we don't have any more to compress */
261
} while (png_ptr->zstream.avail_in);
263
/* finish the compression */
266
/* tell zlib we are finished */
267
ret = deflate(&png_ptr->zstream, Z_FINISH);
271
/* check to see if we need more room */
272
if (!(png_ptr->zstream.avail_out))
274
/* check to make sure our output array has room */
275
if (comp->num_output_ptr >= comp->max_output_ptr)
279
old_max = comp->max_output_ptr;
280
comp->max_output_ptr = comp->num_output_ptr + 4;
281
if (comp->output_ptr != NULL)
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);
295
comp->output_ptr = (png_charpp)png_malloc(png_ptr,
296
(png_uint_32)(comp->max_output_ptr *
297
png_sizeof (png_charp)));
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,
305
comp->num_output_ptr++;
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;
312
else if (ret != Z_STREAM_END)
314
/* we got an error */
315
if (png_ptr->zstream.msg != NULL)
316
png_error(png_ptr, png_ptr->zstream.msg);
318
png_error(png_ptr, "zlib error");
320
} while (ret != Z_STREAM_END);
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;
327
return((int)text_len);
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)
336
/* handle the no-compression case */
339
png_write_chunk_data(png_ptr, (png_bytep)comp->input,
340
(png_size_t)comp->input_len);
344
/* write saved output buffers, if any */
345
for (i = 0; i < comp->num_output_ptr; i++)
347
png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
349
png_free(png_ptr, comp->output_ptr[i]);
350
comp->output_ptr[i]=NULL;
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);
360
/* reset zlib for another zTXt/iTXt or the image data */
361
deflateReset(&png_ptr->zstream);
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.
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,
375
#ifdef PNG_USE_LOCAL_ARRAYS
378
png_byte buf[13]; /* buffer to store the IHDR info */
380
png_debug(1, "in png_write_IHDR\n");
381
/* Check that we have valid input data from the application info */
384
case PNG_COLOR_TYPE_GRAY:
391
case 16: png_ptr->channels = 1; break;
392
default: png_error(png_ptr,"Invalid bit depth for grayscale image");
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;
400
case PNG_COLOR_TYPE_PALETTE:
406
case 8: png_ptr->channels = 1; break;
407
default: png_error(png_ptr, "Invalid bit depth for paletted image");
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;
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;
421
png_error(png_ptr, "Invalid image color type specified");
424
if (compression_type != PNG_COMPRESSION_TYPE_BASE)
426
png_warning(png_ptr, "Invalid compression type specified");
427
compression_type = PNG_COMPRESSION_TYPE_BASE;
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
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)) &&
447
filter_type != PNG_FILTER_TYPE_BASE)
449
png_warning(png_ptr, "Invalid filter type specified");
450
filter_type = PNG_FILTER_TYPE_BASE;
453
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
454
if (interlace_type != PNG_INTERLACE_NONE &&
455
interlace_type != PNG_INTERLACE_ADAM7)
457
png_warning(png_ptr, "Invalid interlace type specified");
458
interlace_type = PNG_INTERLACE_ADAM7;
461
interlace_type=PNG_INTERLACE_NONE;
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;
471
png_ptr->compression_type = (png_byte)compression_type;
472
png_ptr->width = width;
473
png_ptr->height = height;
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;
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;
491
/* write the chunk */
492
png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
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))
500
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
501
png_ptr->bit_depth < 8)
502
png_ptr->do_filter = PNG_FILTER_NONE;
504
png_ptr->do_filter = PNG_ALL_FILTERS;
506
if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
508
if (png_ptr->do_filter != PNG_FILTER_NONE)
509
png_ptr->zlib_strategy = Z_FILTERED;
511
png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
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;
527
png_ptr->mode = PNG_HAVE_IHDR;
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
535
png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
537
#ifdef PNG_USE_LOCAL_ARRAYS
544
png_debug(1, "in png_write_PLTE\n");
546
#if defined(PNG_MNG_FEATURES_SUPPORTED)
547
!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
549
num_pal == 0) || num_pal > 256)
551
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
553
png_error(png_ptr, "Invalid number of colors in palette");
557
png_warning(png_ptr, "Invalid number of colors in palette");
562
if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
565
"Ignoring request to write a PLTE chunk in grayscale PNG");
569
png_ptr->num_palette = (png_uint_16)num_pal;
570
png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
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++)
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);
582
/* This is a little slower but some buggy compilers need to do this instead */
584
for (i = 0; i < num_pal; i++)
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);
592
png_write_chunk_end(png_ptr);
593
png_ptr->mode |= PNG_HAVE_PLTE;
596
/* write an IDAT chunk */
598
png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
600
#ifdef PNG_USE_LOCAL_ARRAYS
603
png_debug(1, "in png_write_IDAT\n");
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)
610
unsigned int z_cmf = data[0]; /* zlib compression method and flags */
611
if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
613
/* Avoid memory underflows and multiplication overflows. */
614
/* The conditions below are practically always satisfied;
615
however, they still must be checked. */
617
png_ptr->height < 16384 && png_ptr->width < 16384)
619
png_uint_32 uncompressed_idat_size = png_ptr->height *
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)
628
half_z_window_size >>= 1;
630
z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
631
if (data[0] != (png_byte)z_cmf)
633
data[0] = (png_byte)z_cmf;
635
data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
641
"Invalid zlib compression method or flags in IDAT");
644
png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
645
png_ptr->mode |= PNG_HAVE_IDAT;
648
/* write an IEND chunk */
650
png_write_IEND(png_structp png_ptr)
652
#ifdef PNG_USE_LOCAL_ARRAYS
655
png_debug(1, "in png_write_IEND\n");
656
png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
658
png_ptr->mode |= PNG_HAVE_IEND;
661
#if defined(PNG_WRITE_gAMA_SUPPORTED)
662
/* write a gAMA chunk */
663
#ifdef PNG_FLOATING_POINT_SUPPORTED
665
png_write_gAMA(png_structp png_ptr, double file_gamma)
667
#ifdef PNG_USE_LOCAL_ARRAYS
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);
680
#ifdef PNG_FIXED_POINT_SUPPORTED
682
png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
684
#ifdef PNG_USE_LOCAL_ARRAYS
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);
697
#if defined(PNG_WRITE_sRGB_SUPPORTED)
698
/* write a sRGB chunk */
700
png_write_sRGB(png_structp png_ptr, int srgb_intent)
702
#ifdef PNG_USE_LOCAL_ARRAYS
707
png_debug(1, "in png_write_sRGB\n");
708
if(srgb_intent >= PNG_sRGB_INTENT_LAST)
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);
716
#if defined(PNG_WRITE_iCCP_SUPPORTED)
717
/* write an iCCP chunk */
719
png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
720
png_charp profile, int profile_len)
722
#ifdef PNG_USE_LOCAL_ARRAYS
727
compression_state comp;
729
png_debug(1, "in png_write_iCCP\n");
730
if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
733
png_warning(png_ptr, "Empty keyword in iCCP chunk");
737
if (compression_type != PNG_COMPRESSION_TYPE_BASE)
738
png_warning(png_ptr, "Unknown compression type in iCCP chunk");
744
profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
745
PNG_COMPRESSION_TYPE_BASE, &comp);
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);
754
png_write_compressed_data_out(png_ptr, &comp);
756
png_write_chunk_end(png_ptr);
757
png_free(png_ptr, new_name);
761
#if defined(PNG_WRITE_sPLT_SUPPORTED)
762
/* write a sPLT chunk */
764
png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
766
#ifdef PNG_USE_LOCAL_ARRAYS
771
png_byte entrybuf[10];
772
int entry_size = (spalette->depth == 8 ? 6 : 10);
773
int palette_size = entry_size * spalette->nentries;
775
#ifdef PNG_NO_POINTER_INDEXING
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)
783
png_warning(png_ptr, "Empty keyword in sPLT chunk");
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);
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++)
797
if (spalette->depth == 8)
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);
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);
813
png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
816
ep=spalette->entries;
817
for (i=0; i>spalette->nentries; i++)
819
if (spalette->depth == 8)
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);
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);
835
png_write_chunk_data(png_ptr, entrybuf, entry_size);
839
png_write_chunk_end(png_ptr);
840
png_free(png_ptr, new_name);
844
#if defined(PNG_WRITE_sBIT_SUPPORTED)
845
/* write the sBIT chunk */
847
png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
849
#ifdef PNG_USE_LOCAL_ARRAYS
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)
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)
867
png_warning(png_ptr, "Invalid sBIT depth specified");
871
buf[1] = sbit->green;
877
if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
879
png_warning(png_ptr, "Invalid sBIT depth specified");
886
if (color_type & PNG_COLOR_MASK_ALPHA)
888
if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
890
png_warning(png_ptr, "Invalid sBIT depth specified");
893
buf[size++] = sbit->alpha;
896
png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
900
#if defined(PNG_WRITE_cHRM_SUPPORTED)
901
/* write the cHRM chunk */
902
#ifdef PNG_FLOATING_POINT_SUPPORTED
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)
908
#ifdef PNG_USE_LOCAL_ARRAYS
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)
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);
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);
930
if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
933
png_warning(png_ptr, "Invalid cHRM red point specified");
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);
941
if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
942
green_x + green_y > 1.0)
944
png_warning(png_ptr, "Invalid cHRM green point specified");
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);
952
if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
953
blue_x + blue_y > 1.0)
955
png_warning(png_ptr, "Invalid cHRM blue point specified");
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);
963
png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
966
#ifdef PNG_FIXED_POINT_SUPPORTED
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)
973
#ifdef PNG_USE_LOCAL_ARRAYS
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)
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);
988
png_save_uint_32(buf, (png_uint_32)white_x);
989
png_save_uint_32(buf + 4, (png_uint_32)white_y);
991
if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
993
png_warning(png_ptr, "Invalid cHRM fixed red point specified");
996
png_save_uint_32(buf + 8, (png_uint_32)red_x);
997
png_save_uint_32(buf + 12, (png_uint_32)red_y);
999
if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
1001
png_warning(png_ptr, "Invalid fixed cHRM green point specified");
1004
png_save_uint_32(buf + 16, (png_uint_32)green_x);
1005
png_save_uint_32(buf + 20, (png_uint_32)green_y);
1007
if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
1009
png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
1012
png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1013
png_save_uint_32(buf + 28, (png_uint_32)blue_y);
1015
png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1020
#if defined(PNG_WRITE_tRNS_SUPPORTED)
1021
/* write the tRNS chunk */
1023
png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
1024
int num_trans, int color_type)
1026
#ifdef PNG_USE_LOCAL_ARRAYS
1031
png_debug(1, "in png_write_tRNS\n");
1032
if (color_type == PNG_COLOR_TYPE_PALETTE)
1034
if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1036
png_warning(png_ptr,"Invalid number of transparent colors specified");
1039
/* write the chunk out as it is */
1040
png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
1042
else if (color_type == PNG_COLOR_TYPE_GRAY)
1044
/* one 16 bit value */
1045
if(tran->gray >= (1 << png_ptr->bit_depth))
1047
png_warning(png_ptr,
1048
"Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1051
png_save_uint_16(buf, tran->gray);
1052
png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
1054
else if (color_type == PNG_COLOR_TYPE_RGB)
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]))
1062
png_warning(png_ptr,
1063
"Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1066
png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
1070
png_warning(png_ptr, "Can't write tRNS with an alpha channel");
1075
#if defined(PNG_WRITE_bKGD_SUPPORTED)
1076
/* write the background chunk */
1078
png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
1080
#ifdef PNG_USE_LOCAL_ARRAYS
1085
png_debug(1, "in png_write_bKGD\n");
1086
if (color_type == PNG_COLOR_TYPE_PALETTE)
1089
#if defined(PNG_MNG_FEATURES_SUPPORTED)
1090
(png_ptr->num_palette ||
1091
(!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
1093
back->index > png_ptr->num_palette)
1095
png_warning(png_ptr, "Invalid background palette index");
1098
buf[0] = back->index;
1099
png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
1101
else if (color_type & PNG_COLOR_MASK_COLOR)
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]))
1108
png_warning(png_ptr,
1109
"Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1112
png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
1116
if(back->gray >= (1 << png_ptr->bit_depth))
1118
png_warning(png_ptr,
1119
"Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1122
png_save_uint_16(buf, back->gray);
1123
png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
1128
#if defined(PNG_WRITE_hIST_SUPPORTED)
1129
/* write the histogram */
1131
png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
1133
#ifdef PNG_USE_LOCAL_ARRAYS
1139
png_debug(1, "in png_write_hIST\n");
1140
if (num_hist > (int)png_ptr->num_palette)
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");
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++)
1151
png_save_uint_16(buf, hist[i]);
1152
png_write_chunk_data(png_ptr, buf, (png_size_t)2);
1154
png_write_chunk_end(png_ptr);
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.
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.
1170
png_size_t /* PRIVATE */
1171
png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
1178
png_debug(1, "in png_check_keyword\n");
1181
if (key == NULL || (key_len = png_strlen(key)) == 0)
1183
png_warning(png_ptr, "zero length keyword");
1184
return ((png_size_t)0);
1187
png_debug1(2, "Keyword to be checked is '%s'\n", key);
1189
*new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1190
if (*new_key == NULL)
1192
png_warning(png_ptr, "Out of memory while procesing keyword");
1193
return ((png_size_t)0);
1196
/* Replace non-printing characters with a blank and print a warning */
1197
for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
1199
if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
1201
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
1204
sprintf(msg, "invalid keyword character 0x%02X", *kp);
1205
png_warning(png_ptr, msg);
1207
png_warning(png_ptr, "invalid character in keyword");
1218
/* Remove any trailing white space. */
1219
kp = *new_key + key_len - 1;
1222
png_warning(png_ptr, "trailing spaces removed from keyword");
1231
/* Remove any leading white space. */
1235
png_warning(png_ptr, "leading spaces removed from keyword");
1244
png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
1246
/* Remove multiple internal spaces. */
1247
for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
1249
if (*kp == ' ' && kflag == 0)
1254
else if (*kp == ' ')
1267
png_warning(png_ptr, "extra interior spaces removed from keyword");
1271
png_free(png_ptr, *new_key);
1273
png_warning(png_ptr, "Zero length keyword");
1278
png_warning(png_ptr, "keyword length must be 1 - 79 characters");
1287
#if defined(PNG_WRITE_tEXt_SUPPORTED)
1288
/* write a tEXt chunk */
1290
png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
1291
png_size_t text_len)
1293
#ifdef PNG_USE_LOCAL_ARRAYS
1299
png_debug(1, "in png_write_tEXt\n");
1300
if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1302
png_warning(png_ptr, "Empty keyword in tEXt chunk");
1306
if (text == NULL || *text == '\0')
1309
text_len = png_strlen(text);
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);
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.
1319
png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1321
png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
1323
png_write_chunk_end(png_ptr);
1324
png_free(png_ptr, new_key);
1328
#if defined(PNG_WRITE_zTXt_SUPPORTED)
1329
/* write a compressed text chunk */
1331
png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
1332
png_size_t text_len, int compression)
1334
#ifdef PNG_USE_LOCAL_ARRAYS
1340
compression_state comp;
1342
png_debug(1, "in png_write_zTXt\n");
1344
if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1346
png_warning(png_ptr, "Empty keyword in zTXt chunk");
1350
if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1352
png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1353
png_free(png_ptr, new_key);
1357
text_len = png_strlen(text);
1359
png_free(png_ptr, new_key);
1361
/* compute the compressed data; do it now for the length */
1362
text_len = png_text_compress(png_ptr, text, text_len, compression,
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));
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);
1376
/* close the chunk */
1377
png_write_chunk_end(png_ptr);
1381
#if defined(PNG_WRITE_iTXt_SUPPORTED)
1382
/* write an iTXt chunk */
1384
png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
1385
png_charp lang, png_charp lang_key, png_charp text)
1387
#ifdef PNG_USE_LOCAL_ARRAYS
1390
png_size_t lang_len, key_len, lang_key_len, text_len;
1391
png_charp new_lang, new_key;
1393
compression_state comp;
1395
png_debug(1, "in png_write_iTXt\n");
1397
if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1399
png_warning(png_ptr, "Empty keyword in iTXt chunk");
1402
if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
1404
png_warning(png_ptr, "Empty language field in iTXt chunk");
1409
if (lang_key == NULL)
1412
lang_key_len = png_strlen(lang_key);
1417
text_len = png_strlen(text);
1419
/* compute the compressed data; do it now for the length */
1420
text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1424
/* make sure we include the compression flag, the compression byte,
1425
* and the NULs after the key, lang, and lang_key parts */
1427
png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1429
5 /* comp byte, comp flag, terminators for key, lang and lang_key */
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.
1441
png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
1443
/* set the compression flag */
1444
if (compression == PNG_ITXT_COMPRESSION_NONE || \
1445
compression == PNG_TEXT_COMPRESSION_NONE)
1447
else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1449
/* set the compression method */
1451
png_write_chunk_data(png_ptr, cbuf, 2);
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);
1458
png_write_chunk_end(png_ptr);
1459
png_free(png_ptr, new_key);
1461
png_free(png_ptr, new_lang);
1465
#if defined(PNG_WRITE_oFFs_SUPPORTED)
1466
/* write the oFFs chunk */
1468
png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
1471
#ifdef PNG_USE_LOCAL_ARRAYS
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");
1480
png_save_int_32(buf, x_offset);
1481
png_save_int_32(buf + 4, y_offset);
1482
buf[8] = (png_byte)unit_type;
1484
png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
1488
#if defined(PNG_WRITE_pCAL_SUPPORTED)
1489
/* write the pCAL chunk (described in the PNG extensions document) */
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)
1494
#ifdef PNG_USE_LOCAL_ARRAYS
1497
png_size_t purpose_len, units_len, total_len;
1498
png_uint_32p params_len;
1500
png_charp new_purpose;
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");
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;
1513
params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1514
*png_sizeof(png_uint_32)));
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++)
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];
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);
1535
png_free(png_ptr, new_purpose);
1537
for (i = 0; i < nparams; i++)
1539
png_write_chunk_data(png_ptr, (png_bytep)params[i],
1540
(png_size_t)params_len[i]);
1543
png_free(png_ptr, params_len);
1544
png_write_chunk_end(png_ptr);
1548
#if defined(PNG_WRITE_sCAL_SUPPORTED)
1549
/* write the sCAL chunk */
1550
#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
1552
png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
1554
#ifdef PNG_USE_LOCAL_ARRAYS
1557
png_size_t total_len;
1558
char wbuf[32], hbuf[32];
1559
png_byte bunit = unit;
1561
png_debug(1, "in png_write_sCAL\n");
1563
#if defined(_WIN32_WCE)
1564
/* sprintf() function is not supported on WindowsCE */
1567
swprintf(wc_buf, TEXT("%12.12e"), width);
1568
WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1569
swprintf(wc_buf, TEXT("%12.12e"), height);
1570
WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1573
sprintf(wbuf, "%12.12e", width);
1574
sprintf(hbuf, "%12.12e", height);
1576
total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1578
png_debug1(3, "sCAL total length = %d\n", (int)total_len);
1579
png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1580
png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
1581
png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1582
png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
1584
png_write_chunk_end(png_ptr);
1587
#ifdef PNG_FIXED_POINT_SUPPORTED
1589
png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
1592
#ifdef PNG_USE_LOCAL_ARRAYS
1595
png_size_t total_len;
1596
char wbuf[32], hbuf[32];
1597
png_byte bunit = unit;
1599
png_debug(1, "in png_write_sCAL_s\n");
1601
png_strcpy(wbuf,(const char *)width);
1602
png_strcpy(hbuf,(const char *)height);
1603
total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1605
png_debug1(3, "sCAL total length = %d\n", total_len);
1606
png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1607
png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
1608
png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1609
png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
1611
png_write_chunk_end(png_ptr);
1617
#if defined(PNG_WRITE_pHYs_SUPPORTED)
1618
/* write the pHYs chunk */
1620
png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
1621
png_uint_32 y_pixels_per_unit,
1624
#ifdef PNG_USE_LOCAL_ARRAYS
1629
png_debug(1, "in png_write_pHYs\n");
1630
if (unit_type >= PNG_RESOLUTION_LAST)
1631
png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1633
png_save_uint_32(buf, x_pixels_per_unit);
1634
png_save_uint_32(buf + 4, y_pixels_per_unit);
1635
buf[8] = (png_byte)unit_type;
1637
png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
1641
#if defined(PNG_WRITE_tIME_SUPPORTED)
1642
/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1643
* or png_convert_from_time_t(), or fill in the structure yourself.
1646
png_write_tIME(png_structp png_ptr, png_timep mod_time)
1648
#ifdef PNG_USE_LOCAL_ARRAYS
1653
png_debug(1, "in png_write_tIME\n");
1654
if (mod_time->month > 12 || mod_time->month < 1 ||
1655
mod_time->day > 31 || mod_time->day < 1 ||
1656
mod_time->hour > 23 || mod_time->second > 60)
1658
png_warning(png_ptr, "Invalid time specified for tIME chunk");
1662
png_save_uint_16(buf, mod_time->year);
1663
buf[2] = mod_time->month;
1664
buf[3] = mod_time->day;
1665
buf[4] = mod_time->hour;
1666
buf[5] = mod_time->minute;
1667
buf[6] = mod_time->second;
1669
png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
1673
/* initializes the row writing capability of libpng */
1675
png_write_start_row(png_structp png_ptr)
1677
#ifdef PNG_USE_LOCAL_ARRAYS
1678
/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1680
/* start of interlace block */
1681
int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1683
/* offset to next interlace block */
1684
int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1686
/* start of interlace block in the y direction */
1687
int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1689
/* offset to next interlace block in the y direction */
1690
int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1693
png_size_t buf_size;
1695
png_debug(1, "in png_write_start_row\n");
1696
buf_size = (png_size_t)(PNG_ROWBYTES(
1697
png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
1699
/* set up row buffer */
1700
png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1701
png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1703
/* set up filtering buffer, if using this filter */
1704
if (png_ptr->do_filter & PNG_FILTER_SUB)
1706
png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1707
(png_ptr->rowbytes + 1));
1708
png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1711
/* We only need to keep the previous row if we are using one of these. */
1712
if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1714
/* set up previous row buffer */
1715
png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1716
png_memset(png_ptr->prev_row, 0, buf_size);
1718
if (png_ptr->do_filter & PNG_FILTER_UP)
1720
png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
1721
(png_ptr->rowbytes + 1));
1722
png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1725
if (png_ptr->do_filter & PNG_FILTER_AVG)
1727
png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1728
(png_ptr->rowbytes + 1));
1729
png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1732
if (png_ptr->do_filter & PNG_FILTER_PAETH)
1734
png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
1735
(png_ptr->rowbytes + 1));
1736
png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1740
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1741
/* if interlaced, we need to set up width and height of pass */
1742
if (png_ptr->interlaced)
1744
if (!(png_ptr->transformations & PNG_INTERLACE))
1746
png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1747
png_pass_ystart[0]) / png_pass_yinc[0];
1748
png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1749
png_pass_start[0]) / png_pass_inc[0];
1753
png_ptr->num_rows = png_ptr->height;
1754
png_ptr->usr_width = png_ptr->width;
1760
png_ptr->num_rows = png_ptr->height;
1761
png_ptr->usr_width = png_ptr->width;
1763
png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1764
png_ptr->zstream.next_out = png_ptr->zbuf;
1767
/* Internal use only. Called when finished processing a row of data. */
1769
png_write_finish_row(png_structp png_ptr)
1771
#ifdef PNG_USE_LOCAL_ARRAYS
1772
/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1774
/* start of interlace block */
1775
int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1777
/* offset to next interlace block */
1778
int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1780
/* start of interlace block in the y direction */
1781
int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1783
/* offset to next interlace block in the y direction */
1784
int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1789
png_debug(1, "in png_write_finish_row\n");
1791
png_ptr->row_number++;
1793
/* see if we are done */
1794
if (png_ptr->row_number < png_ptr->num_rows)
1797
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1798
/* if interlaced, go to next pass */
1799
if (png_ptr->interlaced)
1801
png_ptr->row_number = 0;
1802
if (png_ptr->transformations & PNG_INTERLACE)
1808
/* loop until we find a non-zero width or height pass */
1812
if (png_ptr->pass >= 7)
1814
png_ptr->usr_width = (png_ptr->width +
1815
png_pass_inc[png_ptr->pass] - 1 -
1816
png_pass_start[png_ptr->pass]) /
1817
png_pass_inc[png_ptr->pass];
1818
png_ptr->num_rows = (png_ptr->height +
1819
png_pass_yinc[png_ptr->pass] - 1 -
1820
png_pass_ystart[png_ptr->pass]) /
1821
png_pass_yinc[png_ptr->pass];
1822
if (png_ptr->transformations & PNG_INTERLACE)
1824
} while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1828
/* reset the row above the image for the next pass */
1829
if (png_ptr->pass < 7)
1831
if (png_ptr->prev_row != NULL)
1832
png_memset(png_ptr->prev_row, 0,
1833
(png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1834
png_ptr->usr_bit_depth,png_ptr->width))+1);
1840
/* if we get here, we've just written the last row, so we need
1841
to flush the compressor */
1844
/* tell the compressor we are done */
1845
ret = deflate(&png_ptr->zstream, Z_FINISH);
1846
/* check for an error */
1849
/* check to see if we need more room */
1850
if (!(png_ptr->zstream.avail_out))
1852
png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1853
png_ptr->zstream.next_out = png_ptr->zbuf;
1854
png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1857
else if (ret != Z_STREAM_END)
1859
if (png_ptr->zstream.msg != NULL)
1860
png_error(png_ptr, png_ptr->zstream.msg);
1862
png_error(png_ptr, "zlib error");
1864
} while (ret != Z_STREAM_END);
1866
/* write any extra space */
1867
if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
1869
png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
1870
png_ptr->zstream.avail_out);
1873
deflateReset(&png_ptr->zstream);
1876
#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
1877
/* Pick out the correct pixels for the interlace pass.
1878
* The basic idea here is to go through the row with a source
1879
* pointer and a destination pointer (sp and dp), and copy the
1880
* correct pixels for the pass. As the row gets compacted,
1881
* sp will always be >= dp, so we should never overwrite anything.
1882
* See the default: case for the easiest code to understand.
1885
png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
1887
#ifdef PNG_USE_LOCAL_ARRAYS
1888
/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1890
/* start of interlace block */
1891
int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1893
/* offset to next interlace block */
1894
int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1897
png_debug(1, "in png_do_write_interlace\n");
1898
/* we don't have to do anything on the last pass (6) */
1899
#if defined(PNG_USELESS_TESTS_SUPPORTED)
1900
if (row != NULL && row_info != NULL && pass < 6)
1905
/* each pixel depth is handled separately */
1906
switch (row_info->pixel_depth)
1916
png_uint_32 row_width = row_info->width;
1921
for (i = png_pass_start[pass]; i < row_width;
1922
i += png_pass_inc[pass])
1924
sp = row + (png_size_t)(i >> 3);
1925
value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
1926
d |= (value << shift);
1931
*dp++ = (png_byte)d;
1950
png_uint_32 row_width = row_info->width;
1955
for (i = png_pass_start[pass]; i < row_width;
1956
i += png_pass_inc[pass])
1958
sp = row + (png_size_t)(i >> 2);
1959
value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
1960
d |= (value << shift);
1965
*dp++ = (png_byte)d;
1983
png_uint_32 row_width = row_info->width;
1988
for (i = png_pass_start[pass]; i < row_width;
1989
i += png_pass_inc[pass])
1991
sp = row + (png_size_t)(i >> 1);
1992
value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
1993
d |= (value << shift);
1998
*dp++ = (png_byte)d;
2013
png_uint_32 row_width = row_info->width;
2014
png_size_t pixel_bytes;
2016
/* start at the beginning */
2018
/* find out how many bytes each pixel takes up */
2019
pixel_bytes = (row_info->pixel_depth >> 3);
2020
/* loop through the row, only looking at the pixels that
2022
for (i = png_pass_start[pass]; i < row_width;
2023
i += png_pass_inc[pass])
2025
/* find out where the original pixel is */
2026
sp = row + (png_size_t)i * pixel_bytes;
2027
/* move the pixel */
2029
png_memcpy(dp, sp, pixel_bytes);
2036
/* set new row width */
2037
row_info->width = (row_info->width +
2038
png_pass_inc[pass] - 1 -
2039
png_pass_start[pass]) /
2041
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2047
/* This filters the row, chooses which filter to use, if it has not already
2048
* been specified by the application, and then writes the row out with the
2051
#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
2052
#define PNG_HISHIFT 10
2053
#define PNG_LOMASK ((png_uint_32)0xffffL)
2054
#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2056
png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
2058
png_bytep prev_row, best_row, row_buf;
2059
png_uint_32 mins, bpp;
2060
png_byte filter_to_do = png_ptr->do_filter;
2061
png_uint_32 row_bytes = row_info->rowbytes;
2062
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2063
int num_p_filters = (int)png_ptr->num_prev_filters;
2066
png_debug(1, "in png_write_find_filter\n");
2067
/* find out how many bytes offset each pixel is */
2068
bpp = (row_info->pixel_depth + 7) >> 3;
2070
prev_row = png_ptr->prev_row;
2071
best_row = row_buf = png_ptr->row_buf;
2074
/* The prediction method we use is to find which method provides the
2075
* smallest value when summing the absolute values of the distances
2076
* from zero, using anything >= 128 as negative numbers. This is known
2077
* as the "minimum sum of absolute differences" heuristic. Other
2078
* heuristics are the "weighted minimum sum of absolute differences"
2079
* (experimental and can in theory improve compression), and the "zlib
2080
* predictive" method (not implemented yet), which does test compressions
2081
* of lines using different filter methods, and then chooses the
2082
* (series of) filter(s) that give minimum compressed data size (VERY
2083
* computationally expensive).
2085
* GRR 980525: consider also
2086
* (1) minimum sum of absolute differences from running average (i.e.,
2087
* keep running sum of non-absolute differences & count of bytes)
2088
* [track dispersion, too? restart average if dispersion too large?]
2089
* (1b) minimum sum of absolute differences from sliding average, probably
2090
* with window size <= deflate window (usually 32K)
2091
* (2) minimum sum of squared differences from zero or running average
2092
* (i.e., ~ root-mean-square approach)
2096
/* We don't need to test the 'no filter' case if this is the only filter
2097
* that has been chosen, as it doesn't actually do anything to the data.
2099
if ((filter_to_do & PNG_FILTER_NONE) &&
2100
filter_to_do != PNG_FILTER_NONE)
2103
png_uint_32 sum = 0;
2107
for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2110
sum += (v < 128) ? v : 256 - v;
2113
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2114
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2116
png_uint_32 sumhi, sumlo;
2118
sumlo = sum & PNG_LOMASK;
2119
sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2121
/* Reduce the sum if we match any of the previous rows */
2122
for (j = 0; j < num_p_filters; j++)
2124
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2126
sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2128
sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2133
/* Factor in the cost of this filter (this is here for completeness,
2134
* but it makes no sense to have a "cost" for the NONE filter, as
2135
* it has the minimum possible computational cost - none).
2137
sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2139
sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2142
if (sumhi > PNG_HIMASK)
2145
sum = (sumhi << PNG_HISHIFT) + sumlo;
2152
if (filter_to_do == PNG_FILTER_SUB)
2153
/* it's the only filter so no testing is needed */
2155
png_bytep rp, lp, dp;
2157
for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2162
for (lp = row_buf + 1; i < row_bytes;
2163
i++, rp++, lp++, dp++)
2165
*dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2167
best_row = png_ptr->sub_row;
2170
else if (filter_to_do & PNG_FILTER_SUB)
2172
png_bytep rp, dp, lp;
2173
png_uint_32 sum = 0, lmins = mins;
2177
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2178
/* We temporarily increase the "minimum sum" by the factor we
2179
* would reduce the sum of this filter, so that we can do the
2180
* early exit comparison without scaling the sum each time.
2182
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2185
png_uint_32 lmhi, lmlo;
2186
lmlo = lmins & PNG_LOMASK;
2187
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2189
for (j = 0; j < num_p_filters; j++)
2191
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2193
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2195
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2200
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2202
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2205
if (lmhi > PNG_HIMASK)
2208
lmins = (lmhi << PNG_HISHIFT) + lmlo;
2212
for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2217
sum += (v < 128) ? v : 256 - v;
2219
for (lp = row_buf + 1; i < row_bytes;
2220
i++, rp++, lp++, dp++)
2222
v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2224
sum += (v < 128) ? v : 256 - v;
2226
if (sum > lmins) /* We are already worse, don't continue. */
2230
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2231
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2234
png_uint_32 sumhi, sumlo;
2235
sumlo = sum & PNG_LOMASK;
2236
sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2238
for (j = 0; j < num_p_filters; j++)
2240
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2242
sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
2244
sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
2249
sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2251
sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2254
if (sumhi > PNG_HIMASK)
2257
sum = (sumhi << PNG_HISHIFT) + sumlo;
2264
best_row = png_ptr->sub_row;
2269
if (filter_to_do == PNG_FILTER_UP)
2271
png_bytep rp, dp, pp;
2274
for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2275
pp = prev_row + 1; i < row_bytes;
2276
i++, rp++, pp++, dp++)
2278
*dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2280
best_row = png_ptr->up_row;
2283
else if (filter_to_do & PNG_FILTER_UP)
2285
png_bytep rp, dp, pp;
2286
png_uint_32 sum = 0, lmins = mins;
2291
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2292
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2295
png_uint_32 lmhi, lmlo;
2296
lmlo = lmins & PNG_LOMASK;
2297
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2299
for (j = 0; j < num_p_filters; j++)
2301
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2303
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2305
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2310
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2312
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2315
if (lmhi > PNG_HIMASK)
2318
lmins = (lmhi << PNG_HISHIFT) + lmlo;
2322
for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2323
pp = prev_row + 1; i < row_bytes; i++)
2325
v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2327
sum += (v < 128) ? v : 256 - v;
2329
if (sum > lmins) /* We are already worse, don't continue. */
2333
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2334
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2337
png_uint_32 sumhi, sumlo;
2338
sumlo = sum & PNG_LOMASK;
2339
sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2341
for (j = 0; j < num_p_filters; j++)
2343
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2345
sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2347
sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2352
sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2354
sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2357
if (sumhi > PNG_HIMASK)
2360
sum = (sumhi << PNG_HISHIFT) + sumlo;
2367
best_row = png_ptr->up_row;
2372
if (filter_to_do == PNG_FILTER_AVG)
2374
png_bytep rp, dp, pp, lp;
2376
for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2377
pp = prev_row + 1; i < bpp; i++)
2379
*dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2381
for (lp = row_buf + 1; i < row_bytes; i++)
2383
*dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2386
best_row = png_ptr->avg_row;
2389
else if (filter_to_do & PNG_FILTER_AVG)
2391
png_bytep rp, dp, pp, lp;
2392
png_uint_32 sum = 0, lmins = mins;
2396
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2397
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2400
png_uint_32 lmhi, lmlo;
2401
lmlo = lmins & PNG_LOMASK;
2402
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2404
for (j = 0; j < num_p_filters; j++)
2406
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
2408
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2410
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2415
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2417
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2420
if (lmhi > PNG_HIMASK)
2423
lmins = (lmhi << PNG_HISHIFT) + lmlo;
2427
for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2428
pp = prev_row + 1; i < bpp; i++)
2430
v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2432
sum += (v < 128) ? v : 256 - v;
2434
for (lp = row_buf + 1; i < row_bytes; i++)
2437
(png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2439
sum += (v < 128) ? v : 256 - v;
2441
if (sum > lmins) /* We are already worse, don't continue. */
2445
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2446
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2449
png_uint_32 sumhi, sumlo;
2450
sumlo = sum & PNG_LOMASK;
2451
sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2453
for (j = 0; j < num_p_filters; j++)
2455
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2457
sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2459
sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2464
sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2466
sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2469
if (sumhi > PNG_HIMASK)
2472
sum = (sumhi << PNG_HISHIFT) + sumlo;
2479
best_row = png_ptr->avg_row;
2484
if (filter_to_do == PNG_FILTER_PAETH)
2486
png_bytep rp, dp, pp, cp, lp;
2488
for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2489
pp = prev_row + 1; i < bpp; i++)
2491
*dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2494
for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2496
int a, b, c, pa, pb, pc, p;
2510
pa = p < 0 ? -p : p;
2511
pb = pc < 0 ? -pc : pc;
2512
pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2515
p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2517
*dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2519
best_row = png_ptr->paeth_row;
2522
else if (filter_to_do & PNG_FILTER_PAETH)
2524
png_bytep rp, dp, pp, cp, lp;
2525
png_uint_32 sum = 0, lmins = mins;
2529
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2530
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2533
png_uint_32 lmhi, lmlo;
2534
lmlo = lmins & PNG_LOMASK;
2535
lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2537
for (j = 0; j < num_p_filters; j++)
2539
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2541
lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2543
lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2548
lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2550
lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2553
if (lmhi > PNG_HIMASK)
2556
lmins = (lmhi << PNG_HISHIFT) + lmlo;
2560
for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2561
pp = prev_row + 1; i < bpp; i++)
2563
v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2565
sum += (v < 128) ? v : 256 - v;
2568
for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2570
int a, b, c, pa, pb, pc, p;
2576
#ifndef PNG_SLOW_PAETH
2584
pa = p < 0 ? -p : p;
2585
pb = pc < 0 ? -pc : pc;
2586
pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2588
p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2589
#else /* PNG_SLOW_PAETH */
2594
if (pa <= pb && pa <= pc)
2600
#endif /* PNG_SLOW_PAETH */
2602
v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2604
sum += (v < 128) ? v : 256 - v;
2606
if (sum > lmins) /* We are already worse, don't continue. */
2610
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2611
if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2614
png_uint_32 sumhi, sumlo;
2615
sumlo = sum & PNG_LOMASK;
2616
sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2618
for (j = 0; j < num_p_filters; j++)
2620
if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2622
sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2624
sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2629
sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2631
sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2634
if (sumhi > PNG_HIMASK)
2637
sum = (sumhi << PNG_HISHIFT) + sumlo;
2643
best_row = png_ptr->paeth_row;
2647
/* Do the actual writing of the filtered row data from the chosen filter. */
2649
png_write_filtered_row(png_ptr, best_row);
2651
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2652
/* Save the type of filter we picked this time for future calculations */
2653
if (png_ptr->num_prev_filters > 0)
2656
for (j = 1; j < num_p_filters; j++)
2658
png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2660
png_ptr->prev_filters[j] = best_row[0];
2666
/* Do the actual writing of a previously filtered row. */
2668
png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2670
png_debug(1, "in png_write_filtered_row\n");
2671
png_debug1(2, "filter = %d\n", filtered_row[0]);
2672
/* set up the zlib input buffer */
2674
png_ptr->zstream.next_in = filtered_row;
2675
png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
2676
/* repeat until we have compressed all the data */
2679
int ret; /* return of zlib */
2681
/* compress the data */
2682
ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
2683
/* check for compression errors */
2686
if (png_ptr->zstream.msg != NULL)
2687
png_error(png_ptr, png_ptr->zstream.msg);
2689
png_error(png_ptr, "zlib error");
2692
/* see if it is time to write another IDAT */
2693
if (!(png_ptr->zstream.avail_out))
2695
/* write the IDAT and reset the zlib output buffer */
2696
png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2697
png_ptr->zstream.next_out = png_ptr->zbuf;
2698
png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2700
/* repeat until all data has been compressed */
2701
} while (png_ptr->zstream.avail_in);
2703
/* swap the current and previous rows */
2704
if (png_ptr->prev_row != NULL)
2708
tptr = png_ptr->prev_row;
2709
png_ptr->prev_row = png_ptr->row_buf;
2710
png_ptr->row_buf = tptr;
2713
/* finish row - updates counters and flushes zlib if last row */
2714
png_write_finish_row(png_ptr);
2716
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2717
png_ptr->flush_rows++;
2719
if (png_ptr->flush_dist > 0 &&
2720
png_ptr->flush_rows >= png_ptr->flush_dist)
2722
png_write_flush(png_ptr);
2726
#endif /* PNG_WRITE_SUPPORTED */