vrshoot

annotate libs/libpng/pngwutil.c @ 0:b2f14e535253

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