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