3dphotoshoot

annotate libs/libpng/pngwutil.c @ 14:06dc8b9b4f89

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