istereo

annotate libs/libpng/pngwutil.c @ 36:834503dcb486

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