dbf-halloween2015

annotate libs/libpng/pngwutil.c @ 4:4316c0c879e9

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