| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "pngpriv.h" |
| 15 |
#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED |
| 16 |
# include <errno.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#ifdef PNG_WRITE_SUPPORTED |
| 20 |
|
| 21 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
| 22 |
|
| 23 |
static void |
| 24 |
write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr, |
| 25 |
unsigned int where) |
| 26 |
{ |
| 27 |
if (info_ptr->unknown_chunks_num != 0) |
| 28 |
{ |
| 29 |
png_const_unknown_chunkp up; |
| 30 |
|
| 31 |
png_debug(5, "writing extra chunks"); |
| 32 |
|
| 33 |
for (up = info_ptr->unknown_chunks; |
| 34 |
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; |
| 35 |
++up) |
| 36 |
if ((up->location & where) != 0) |
| 37 |
{ |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
| 42 |
int keep = png_handle_as_unknown(png_ptr, up->name); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if (keep != PNG_HANDLE_CHUNK_NEVER && |
| 57 |
((up->name[3] & 0x20) || |
| 58 |
keep == PNG_HANDLE_CHUNK_ALWAYS || |
| 59 |
(keep == PNG_HANDLE_CHUNK_AS_DEFAULT && |
| 60 |
png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS))) |
| 61 |
#endif |
| 62 |
{ |
| 63 |
|
| 64 |
if (up->size == 0) |
| 65 |
png_warning(png_ptr, "Writing zero-length unknown chunk"); |
| 66 |
|
| 67 |
png_write_chunk(png_ptr, up->name, up->data, up->size); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
void PNGAPI |
| 84 |
png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr) |
| 85 |
{ |
| 86 |
png_debug(1, "in png_write_info_before_PLTE"); |
| 87 |
|
| 88 |
if (png_ptr == NULL || info_ptr == NULL) |
| 89 |
return; |
| 90 |
|
| 91 |
if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0) |
| 92 |
{ |
| 93 |
|
| 94 |
png_write_sig(png_ptr); |
| 95 |
|
| 96 |
#ifdef PNG_MNG_FEATURES_SUPPORTED |
| 97 |
if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \ |
| 98 |
png_ptr->mng_features_permitted != 0) |
| 99 |
{ |
| 100 |
png_warning(png_ptr, |
| 101 |
"MNG features are not allowed in a PNG datastream"); |
| 102 |
png_ptr->mng_features_permitted = 0; |
| 103 |
} |
| 104 |
#endif |
| 105 |
|
| 106 |
|
| 107 |
png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, |
| 108 |
info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, |
| 109 |
info_ptr->filter_type, |
| 110 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED |
| 111 |
info_ptr->interlace_type |
| 112 |
#else |
| 113 |
0 |
| 114 |
#endif |
| 115 |
); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#ifdef PNG_GAMMA_SUPPORTED |
| 132 |
# ifdef PNG_WRITE_gAMA_SUPPORTED |
| 133 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && |
| 134 |
(info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 && |
| 135 |
(info_ptr->valid & PNG_INFO_gAMA) != 0) |
| 136 |
png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma); |
| 137 |
# endif |
| 138 |
#endif |
| 139 |
|
| 140 |
#ifdef PNG_COLORSPACE_SUPPORTED |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
# ifdef PNG_WRITE_iCCP_SUPPORTED |
| 145 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && |
| 146 |
(info_ptr->valid & PNG_INFO_iCCP) != 0) |
| 147 |
{ |
| 148 |
# ifdef PNG_WRITE_sRGB_SUPPORTED |
| 149 |
if ((info_ptr->valid & PNG_INFO_sRGB) != 0) |
| 150 |
png_app_warning(png_ptr, |
| 151 |
"profile matches sRGB but writing iCCP instead"); |
| 152 |
# endif |
| 153 |
|
| 154 |
png_write_iCCP(png_ptr, info_ptr->iccp_name, |
| 155 |
info_ptr->iccp_profile); |
| 156 |
} |
| 157 |
# ifdef PNG_WRITE_sRGB_SUPPORTED |
| 158 |
else |
| 159 |
# endif |
| 160 |
# endif |
| 161 |
|
| 162 |
# ifdef PNG_WRITE_sRGB_SUPPORTED |
| 163 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && |
| 164 |
(info_ptr->valid & PNG_INFO_sRGB) != 0) |
| 165 |
png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent); |
| 166 |
# endif |
| 167 |
#endif |
| 168 |
|
| 169 |
#ifdef PNG_WRITE_sBIT_SUPPORTED |
| 170 |
if ((info_ptr->valid & PNG_INFO_sBIT) != 0) |
| 171 |
png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); |
| 172 |
#endif |
| 173 |
|
| 174 |
#ifdef PNG_COLORSPACE_SUPPORTED |
| 175 |
# ifdef PNG_WRITE_cHRM_SUPPORTED |
| 176 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && |
| 177 |
(info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 && |
| 178 |
(info_ptr->valid & PNG_INFO_cHRM) != 0) |
| 179 |
png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy); |
| 180 |
# endif |
| 181 |
#endif |
| 182 |
|
| 183 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
| 184 |
write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR); |
| 185 |
#endif |
| 186 |
|
| 187 |
png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void PNGAPI |
| 192 |
png_write_info(png_structrp png_ptr, png_const_inforp info_ptr) |
| 193 |
{ |
| 194 |
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) |
| 195 |
int i; |
| 196 |
#endif |
| 197 |
|
| 198 |
png_debug(1, "in png_write_info"); |
| 199 |
|
| 200 |
if (png_ptr == NULL || info_ptr == NULL) |
| 201 |
return; |
| 202 |
|
| 203 |
png_write_info_before_PLTE(png_ptr, info_ptr); |
| 204 |
|
| 205 |
if ((info_ptr->valid & PNG_INFO_PLTE) != 0) |
| 206 |
png_write_PLTE(png_ptr, info_ptr->palette, |
| 207 |
(png_uint_32)info_ptr->num_palette); |
| 208 |
|
| 209 |
else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 210 |
png_error(png_ptr, "Valid palette required for paletted images"); |
| 211 |
|
| 212 |
#ifdef PNG_WRITE_tRNS_SUPPORTED |
| 213 |
if ((info_ptr->valid & PNG_INFO_tRNS) !=0) |
| 214 |
{ |
| 215 |
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
| 216 |
|
| 217 |
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 && |
| 218 |
info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 219 |
{ |
| 220 |
int j, jend; |
| 221 |
|
| 222 |
jend = info_ptr->num_trans; |
| 223 |
if (jend > PNG_MAX_PALETTE_LENGTH) |
| 224 |
jend = PNG_MAX_PALETTE_LENGTH; |
| 225 |
|
| 226 |
for (j = 0; j<jend; ++j) |
| 227 |
info_ptr->trans_alpha[j] = |
| 228 |
(png_byte)(255 - info_ptr->trans_alpha[j]); |
| 229 |
} |
| 230 |
#endif |
| 231 |
png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color), |
| 232 |
info_ptr->num_trans, info_ptr->color_type); |
| 233 |
} |
| 234 |
#endif |
| 235 |
#ifdef PNG_WRITE_bKGD_SUPPORTED |
| 236 |
if ((info_ptr->valid & PNG_INFO_bKGD) != 0) |
| 237 |
png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); |
| 238 |
#endif |
| 239 |
|
| 240 |
#ifdef PNG_WRITE_eXIf_SUPPORTED |
| 241 |
if ((info_ptr->valid & PNG_INFO_eXIf) != 0) |
| 242 |
{ |
| 243 |
png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif); |
| 244 |
png_ptr->mode |= PNG_WROTE_eXIf; |
| 245 |
} |
| 246 |
#endif |
| 247 |
|
| 248 |
#ifdef PNG_WRITE_hIST_SUPPORTED |
| 249 |
if ((info_ptr->valid & PNG_INFO_hIST) != 0) |
| 250 |
png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); |
| 251 |
#endif |
| 252 |
|
| 253 |
#ifdef PNG_WRITE_oFFs_SUPPORTED |
| 254 |
if ((info_ptr->valid & PNG_INFO_oFFs) != 0) |
| 255 |
png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, |
| 256 |
info_ptr->offset_unit_type); |
| 257 |
#endif |
| 258 |
|
| 259 |
#ifdef PNG_WRITE_pCAL_SUPPORTED |
| 260 |
if ((info_ptr->valid & PNG_INFO_pCAL) != 0) |
| 261 |
png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, |
| 262 |
info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, |
| 263 |
info_ptr->pcal_units, info_ptr->pcal_params); |
| 264 |
#endif |
| 265 |
|
| 266 |
#ifdef PNG_WRITE_sCAL_SUPPORTED |
| 267 |
if ((info_ptr->valid & PNG_INFO_sCAL) != 0) |
| 268 |
png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, |
| 269 |
info_ptr->scal_s_width, info_ptr->scal_s_height); |
| 270 |
#endif |
| 271 |
|
| 272 |
#ifdef PNG_WRITE_pHYs_SUPPORTED |
| 273 |
if ((info_ptr->valid & PNG_INFO_pHYs) != 0) |
| 274 |
png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, |
| 275 |
info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); |
| 276 |
#endif |
| 277 |
|
| 278 |
#ifdef PNG_WRITE_tIME_SUPPORTED |
| 279 |
if ((info_ptr->valid & PNG_INFO_tIME) != 0) |
| 280 |
{ |
| 281 |
png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
| 282 |
png_ptr->mode |= PNG_WROTE_tIME; |
| 283 |
} |
| 284 |
#endif |
| 285 |
|
| 286 |
#ifdef PNG_WRITE_sPLT_SUPPORTED |
| 287 |
if ((info_ptr->valid & PNG_INFO_sPLT) != 0) |
| 288 |
for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) |
| 289 |
png_write_sPLT(png_ptr, info_ptr->splt_palettes + i); |
| 290 |
#endif |
| 291 |
|
| 292 |
#ifdef PNG_WRITE_TEXT_SUPPORTED |
| 293 |
|
| 294 |
for (i = 0; i < info_ptr->num_text; i++) |
| 295 |
{ |
| 296 |
png_debug2(2, "Writing header text chunk %d, type %d", i, |
| 297 |
info_ptr->text[i].compression); |
| 298 |
|
| 299 |
if (info_ptr->text[i].compression > 0) |
| 300 |
{ |
| 301 |
#ifdef PNG_WRITE_iTXt_SUPPORTED |
| 302 |
|
| 303 |
png_write_iTXt(png_ptr, |
| 304 |
info_ptr->text[i].compression, |
| 305 |
info_ptr->text[i].key, |
| 306 |
info_ptr->text[i].lang, |
| 307 |
info_ptr->text[i].lang_key, |
| 308 |
info_ptr->text[i].text); |
| 309 |
|
| 310 |
if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
| 311 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
| 312 |
else |
| 313 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
| 314 |
#else |
| 315 |
png_warning(png_ptr, "Unable to write international text"); |
| 316 |
#endif |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt) |
| 321 |
{ |
| 322 |
#ifdef PNG_WRITE_zTXt_SUPPORTED |
| 323 |
|
| 324 |
png_write_zTXt(png_ptr, info_ptr->text[i].key, |
| 325 |
info_ptr->text[i].text, info_ptr->text[i].compression); |
| 326 |
|
| 327 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
| 328 |
#else |
| 329 |
png_warning(png_ptr, "Unable to write compressed text"); |
| 330 |
#endif |
| 331 |
} |
| 332 |
|
| 333 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
| 334 |
{ |
| 335 |
#ifdef PNG_WRITE_tEXt_SUPPORTED |
| 336 |
|
| 337 |
png_write_tEXt(png_ptr, info_ptr->text[i].key, |
| 338 |
info_ptr->text[i].text, |
| 339 |
0); |
| 340 |
|
| 341 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
| 342 |
#else |
| 343 |
|
| 344 |
png_warning(png_ptr, "Unable to write uncompressed text"); |
| 345 |
#endif |
| 346 |
} |
| 347 |
} |
| 348 |
#endif |
| 349 |
|
| 350 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
| 351 |
write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE); |
| 352 |
#endif |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
void PNGAPI |
| 361 |
png_write_end(png_structrp png_ptr, png_inforp info_ptr) |
| 362 |
{ |
| 363 |
png_debug(1, "in png_write_end"); |
| 364 |
|
| 365 |
if (png_ptr == NULL) |
| 366 |
return; |
| 367 |
|
| 368 |
if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) |
| 369 |
png_error(png_ptr, "No IDATs written into file"); |
| 370 |
|
| 371 |
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 372 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 373 |
png_ptr->num_palette_max >= png_ptr->num_palette) |
| 374 |
png_benign_error(png_ptr, "Wrote palette index exceeding num_palette"); |
| 375 |
#endif |
| 376 |
|
| 377 |
|
| 378 |
if (info_ptr != NULL) |
| 379 |
{ |
| 380 |
#ifdef PNG_WRITE_TEXT_SUPPORTED |
| 381 |
int i; |
| 382 |
#endif |
| 383 |
#ifdef PNG_WRITE_tIME_SUPPORTED |
| 384 |
|
| 385 |
if ((info_ptr->valid & PNG_INFO_tIME) != 0 && |
| 386 |
(png_ptr->mode & PNG_WROTE_tIME) == 0) |
| 387 |
png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
| 388 |
|
| 389 |
#endif |
| 390 |
#ifdef PNG_WRITE_TEXT_SUPPORTED |
| 391 |
|
| 392 |
for (i = 0; i < info_ptr->num_text; i++) |
| 393 |
{ |
| 394 |
png_debug2(2, "Writing trailer text chunk %d, type %d", i, |
| 395 |
info_ptr->text[i].compression); |
| 396 |
|
| 397 |
if (info_ptr->text[i].compression > 0) |
| 398 |
{ |
| 399 |
#ifdef PNG_WRITE_iTXt_SUPPORTED |
| 400 |
|
| 401 |
png_write_iTXt(png_ptr, |
| 402 |
info_ptr->text[i].compression, |
| 403 |
info_ptr->text[i].key, |
| 404 |
info_ptr->text[i].lang, |
| 405 |
info_ptr->text[i].lang_key, |
| 406 |
info_ptr->text[i].text); |
| 407 |
|
| 408 |
if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
| 409 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
| 410 |
else |
| 411 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
| 412 |
#else |
| 413 |
png_warning(png_ptr, "Unable to write international text"); |
| 414 |
#endif |
| 415 |
} |
| 416 |
|
| 417 |
else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) |
| 418 |
{ |
| 419 |
#ifdef PNG_WRITE_zTXt_SUPPORTED |
| 420 |
|
| 421 |
png_write_zTXt(png_ptr, info_ptr->text[i].key, |
| 422 |
info_ptr->text[i].text, info_ptr->text[i].compression); |
| 423 |
|
| 424 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
| 425 |
#else |
| 426 |
png_warning(png_ptr, "Unable to write compressed text"); |
| 427 |
#endif |
| 428 |
} |
| 429 |
|
| 430 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
| 431 |
{ |
| 432 |
#ifdef PNG_WRITE_tEXt_SUPPORTED |
| 433 |
|
| 434 |
png_write_tEXt(png_ptr, info_ptr->text[i].key, |
| 435 |
info_ptr->text[i].text, 0); |
| 436 |
|
| 437 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
| 438 |
#else |
| 439 |
png_warning(png_ptr, "Unable to write uncompressed text"); |
| 440 |
#endif |
| 441 |
} |
| 442 |
} |
| 443 |
#endif |
| 444 |
|
| 445 |
#ifdef PNG_WRITE_eXIf_SUPPORTED |
| 446 |
if ((info_ptr->valid & PNG_INFO_eXIf) != 0 && |
| 447 |
(png_ptr->mode & PNG_WROTE_eXIf) == 0) |
| 448 |
png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif); |
| 449 |
#endif |
| 450 |
|
| 451 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
| 452 |
write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT); |
| 453 |
#endif |
| 454 |
} |
| 455 |
|
| 456 |
png_ptr->mode |= PNG_AFTER_IDAT; |
| 457 |
|
| 458 |
|
| 459 |
png_write_IEND(png_ptr); |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
#ifdef PNG_WRITE_FLUSH_SUPPORTED |
| 469 |
# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED |
| 470 |
png_flush(png_ptr); |
| 471 |
# endif |
| 472 |
#endif |
| 473 |
} |
| 474 |
|
| 475 |
#ifdef PNG_CONVERT_tIME_SUPPORTED |
| 476 |
void PNGAPI |
| 477 |
png_convert_from_struct_tm(png_timep ptime, const struct tm * ttime) |
| 478 |
{ |
| 479 |
png_debug(1, "in png_convert_from_struct_tm"); |
| 480 |
|
| 481 |
ptime->year = (png_uint_16)(1900 + ttime->tm_year); |
| 482 |
ptime->month = (png_byte)(ttime->tm_mon + 1); |
| 483 |
ptime->day = (png_byte)ttime->tm_mday; |
| 484 |
ptime->hour = (png_byte)ttime->tm_hour; |
| 485 |
ptime->minute = (png_byte)ttime->tm_min; |
| 486 |
ptime->second = (png_byte)ttime->tm_sec; |
| 487 |
} |
| 488 |
|
| 489 |
void PNGAPI |
| 490 |
png_convert_from_time_t(png_timep ptime, time_t ttime) |
| 491 |
{ |
| 492 |
struct tm *tbuf; |
| 493 |
|
| 494 |
png_debug(1, "in png_convert_from_time_t"); |
| 495 |
|
| 496 |
tbuf = gmtime(&ttime); |
| 497 |
if (tbuf == NULL) |
| 498 |
{ |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
memset(ptime, 0, sizeof(*ptime)); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
png_convert_from_struct_tm(ptime, tbuf); |
| 508 |
} |
| 509 |
#endif |
| 510 |
|
| 511 |
|
| 512 |
PNG_FUNCTION(png_structp,PNGAPI |
| 513 |
png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr, |
| 514 |
png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) |
| 515 |
{ |
| 516 |
#ifndef PNG_USER_MEM_SUPPORTED |
| 517 |
png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
| 518 |
error_fn, warn_fn, NULL, NULL, NULL); |
| 519 |
#else |
| 520 |
return png_create_write_struct_2(user_png_ver, error_ptr, error_fn, |
| 521 |
warn_fn, NULL, NULL, NULL); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
PNG_FUNCTION(png_structp,PNGAPI |
| 526 |
png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, |
| 527 |
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, |
| 528 |
png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) |
| 529 |
{ |
| 530 |
png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
| 531 |
error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); |
| 532 |
#endif |
| 533 |
if (png_ptr != NULL) |
| 534 |
{ |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
png_ptr->zbuffer_size = PNG_ZBUF_SIZE; |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY; |
| 545 |
png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION; |
| 546 |
png_ptr->zlib_mem_level = 8; |
| 547 |
png_ptr->zlib_window_bits = 15; |
| 548 |
png_ptr->zlib_method = 8; |
| 549 |
|
| 550 |
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED |
| 551 |
png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY; |
| 552 |
png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION; |
| 553 |
png_ptr->zlib_text_mem_level = 8; |
| 554 |
png_ptr->zlib_text_window_bits = 15; |
| 555 |
png_ptr->zlib_text_method = 8; |
| 556 |
#endif |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; |
| 568 |
#endif |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
#if PNG_RELEASE_BUILD |
| 574 |
png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; |
| 575 |
#endif |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
png_set_write_fn(png_ptr, NULL, NULL, NULL); |
| 582 |
} |
| 583 |
|
| 584 |
return png_ptr; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
void PNGAPI |
| 594 |
png_write_rows(png_structrp png_ptr, png_bytepp row, |
| 595 |
png_uint_32 num_rows) |
| 596 |
{ |
| 597 |
png_uint_32 i; |
| 598 |
png_bytepp rp; |
| 599 |
|
| 600 |
png_debug(1, "in png_write_rows"); |
| 601 |
|
| 602 |
if (png_ptr == NULL) |
| 603 |
return; |
| 604 |
|
| 605 |
|
| 606 |
for (i = 0, rp = row; i < num_rows; i++, rp++) |
| 607 |
{ |
| 608 |
png_write_row(png_ptr, *rp); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
void PNGAPI |
| 616 |
png_write_image(png_structrp png_ptr, png_bytepp image) |
| 617 |
{ |
| 618 |
png_uint_32 i; |
| 619 |
int pass, num_pass; |
| 620 |
png_bytepp rp; |
| 621 |
|
| 622 |
if (png_ptr == NULL) |
| 623 |
return; |
| 624 |
|
| 625 |
png_debug(1, "in png_write_image"); |
| 626 |
|
| 627 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
num_pass = png_set_interlace_handling(png_ptr); |
| 632 |
#else |
| 633 |
num_pass = 1; |
| 634 |
#endif |
| 635 |
|
| 636 |
for (pass = 0; pass < num_pass; pass++) |
| 637 |
{ |
| 638 |
|
| 639 |
for (i = 0, rp = image; i < png_ptr->height; i++, rp++) |
| 640 |
{ |
| 641 |
png_write_row(png_ptr, *rp); |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
#ifdef PNG_MNG_FEATURES_SUPPORTED |
| 647 |
|
| 648 |
static void |
| 649 |
png_do_write_intrapixel(png_row_infop row_info, png_bytep row) |
| 650 |
{ |
| 651 |
png_debug(1, "in png_do_write_intrapixel"); |
| 652 |
|
| 653 |
if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 654 |
{ |
| 655 |
int bytes_per_pixel; |
| 656 |
png_uint_32 row_width = row_info->width; |
| 657 |
if (row_info->bit_depth == 8) |
| 658 |
{ |
| 659 |
png_bytep rp; |
| 660 |
png_uint_32 i; |
| 661 |
|
| 662 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 663 |
bytes_per_pixel = 3; |
| 664 |
|
| 665 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 666 |
bytes_per_pixel = 4; |
| 667 |
|
| 668 |
else |
| 669 |
return; |
| 670 |
|
| 671 |
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 672 |
{ |
| 673 |
*(rp) = (png_byte)(*rp - *(rp + 1)); |
| 674 |
*(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1)); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
#ifdef PNG_WRITE_16BIT_SUPPORTED |
| 679 |
else if (row_info->bit_depth == 16) |
| 680 |
{ |
| 681 |
png_bytep rp; |
| 682 |
png_uint_32 i; |
| 683 |
|
| 684 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 685 |
bytes_per_pixel = 6; |
| 686 |
|
| 687 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 688 |
bytes_per_pixel = 8; |
| 689 |
|
| 690 |
else |
| 691 |
return; |
| 692 |
|
| 693 |
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 694 |
{ |
| 695 |
png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1); |
| 696 |
png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3); |
| 697 |
png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5); |
| 698 |
png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); |
| 699 |
png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); |
| 700 |
*(rp ) = (png_byte)(red >> 8); |
| 701 |
*(rp + 1) = (png_byte)red; |
| 702 |
*(rp + 4) = (png_byte)(blue >> 8); |
| 703 |
*(rp + 5) = (png_byte)blue; |
| 704 |
} |
| 705 |
} |
| 706 |
#endif |
| 707 |
} |
| 708 |
} |
| 709 |
#endif |
| 710 |
|
| 711 |
|
| 712 |
void PNGAPI |
| 713 |
png_write_row(png_structrp png_ptr, png_const_bytep row) |
| 714 |
{ |
| 715 |
|
| 716 |
png_row_info row_info; |
| 717 |
|
| 718 |
png_debug2(1, "in png_write_row (row %u, pass %d)", |
| 719 |
png_ptr->row_number, png_ptr->pass); |
| 720 |
|
| 721 |
if (png_ptr == NULL) |
| 722 |
return; |
| 723 |
|
| 724 |
|
| 725 |
if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
| 726 |
{ |
| 727 |
|
| 728 |
if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0) |
| 729 |
png_error(png_ptr, |
| 730 |
"png_write_info was never called before png_write_row"); |
| 731 |
|
| 732 |
|
| 733 |
#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED) |
| 734 |
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
| 735 |
png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined"); |
| 736 |
#endif |
| 737 |
|
| 738 |
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED) |
| 739 |
if ((png_ptr->transformations & PNG_FILLER) != 0) |
| 740 |
png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined"); |
| 741 |
#endif |
| 742 |
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
| 743 |
defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 744 |
if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
| 745 |
png_warning(png_ptr, |
| 746 |
"PNG_WRITE_PACKSWAP_SUPPORTED is not defined"); |
| 747 |
#endif |
| 748 |
|
| 749 |
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED) |
| 750 |
if ((png_ptr->transformations & PNG_PACK) != 0) |
| 751 |
png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined"); |
| 752 |
#endif |
| 753 |
|
| 754 |
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED) |
| 755 |
if ((png_ptr->transformations & PNG_SHIFT) != 0) |
| 756 |
png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined"); |
| 757 |
#endif |
| 758 |
|
| 759 |
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED) |
| 760 |
if ((png_ptr->transformations & PNG_BGR) != 0) |
| 761 |
png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined"); |
| 762 |
#endif |
| 763 |
|
| 764 |
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED) |
| 765 |
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
| 766 |
png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined"); |
| 767 |
#endif |
| 768 |
|
| 769 |
png_write_start_row(png_ptr); |
| 770 |
} |
| 771 |
|
| 772 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED |
| 773 |
|
| 774 |
if (png_ptr->interlaced != 0 && |
| 775 |
(png_ptr->transformations & PNG_INTERLACE) != 0) |
| 776 |
{ |
| 777 |
switch (png_ptr->pass) |
| 778 |
{ |
| 779 |
case 0: |
| 780 |
if ((png_ptr->row_number & 0x07) != 0) |
| 781 |
{ |
| 782 |
png_write_finish_row(png_ptr); |
| 783 |
return; |
| 784 |
} |
| 785 |
break; |
| 786 |
|
| 787 |
case 1: |
| 788 |
if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5) |
| 789 |
{ |
| 790 |
png_write_finish_row(png_ptr); |
| 791 |
return; |
| 792 |
} |
| 793 |
break; |
| 794 |
|
| 795 |
case 2: |
| 796 |
if ((png_ptr->row_number & 0x07) != 4) |
| 797 |
{ |
| 798 |
png_write_finish_row(png_ptr); |
| 799 |
return; |
| 800 |
} |
| 801 |
break; |
| 802 |
|
| 803 |
case 3: |
| 804 |
if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3) |
| 805 |
{ |
| 806 |
png_write_finish_row(png_ptr); |
| 807 |
return; |
| 808 |
} |
| 809 |
break; |
| 810 |
|
| 811 |
case 4: |
| 812 |
if ((png_ptr->row_number & 0x03) != 2) |
| 813 |
{ |
| 814 |
png_write_finish_row(png_ptr); |
| 815 |
return; |
| 816 |
} |
| 817 |
break; |
| 818 |
|
| 819 |
case 5: |
| 820 |
if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2) |
| 821 |
{ |
| 822 |
png_write_finish_row(png_ptr); |
| 823 |
return; |
| 824 |
} |
| 825 |
break; |
| 826 |
|
| 827 |
case 6: |
| 828 |
if ((png_ptr->row_number & 0x01) == 0) |
| 829 |
{ |
| 830 |
png_write_finish_row(png_ptr); |
| 831 |
return; |
| 832 |
} |
| 833 |
break; |
| 834 |
|
| 835 |
default: |
| 836 |
break; |
| 837 |
} |
| 838 |
} |
| 839 |
#endif |
| 840 |
|
| 841 |
|
| 842 |
row_info.color_type = png_ptr->color_type; |
| 843 |
row_info.width = png_ptr->usr_width; |
| 844 |
row_info.channels = png_ptr->usr_channels; |
| 845 |
row_info.bit_depth = png_ptr->usr_bit_depth; |
| 846 |
row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels); |
| 847 |
row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); |
| 848 |
|
| 849 |
png_debug1(3, "row_info->color_type = %d", row_info.color_type); |
| 850 |
png_debug1(3, "row_info->width = %u", row_info.width); |
| 851 |
png_debug1(3, "row_info->channels = %d", row_info.channels); |
| 852 |
png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth); |
| 853 |
png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth); |
| 854 |
png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes); |
| 855 |
|
| 856 |
|
| 857 |
memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes); |
| 858 |
|
| 859 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED |
| 860 |
|
| 861 |
if (png_ptr->interlaced && png_ptr->pass < 6 && |
| 862 |
(png_ptr->transformations & PNG_INTERLACE) != 0) |
| 863 |
{ |
| 864 |
png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass); |
| 865 |
|
| 866 |
if (row_info.width == 0) |
| 867 |
{ |
| 868 |
png_write_finish_row(png_ptr); |
| 869 |
return; |
| 870 |
} |
| 871 |
} |
| 872 |
#endif |
| 873 |
|
| 874 |
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED |
| 875 |
|
| 876 |
if (png_ptr->transformations != 0) |
| 877 |
png_do_write_transformations(png_ptr, &row_info); |
| 878 |
#endif |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
if (row_info.pixel_depth != png_ptr->pixel_depth || |
| 884 |
row_info.pixel_depth != png_ptr->transformed_pixel_depth) |
| 885 |
png_error(png_ptr, "internal write transform logic error"); |
| 886 |
|
| 887 |
#ifdef PNG_MNG_FEATURES_SUPPORTED |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && |
| 898 |
(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
| 899 |
{ |
| 900 |
|
| 901 |
png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1); |
| 902 |
} |
| 903 |
#endif |
| 904 |
|
| 905 |
|
| 906 |
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 907 |
|
| 908 |
if (row_info.color_type == PNG_COLOR_TYPE_PALETTE && |
| 909 |
png_ptr->num_palette_max >= 0) |
| 910 |
png_do_check_palette_indexes(png_ptr, &row_info); |
| 911 |
#endif |
| 912 |
|
| 913 |
|
| 914 |
png_write_find_filter(png_ptr, &row_info); |
| 915 |
|
| 916 |
if (png_ptr->write_row_fn != NULL) |
| 917 |
(*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
| 918 |
} |
| 919 |
|
| 920 |
#ifdef PNG_WRITE_FLUSH_SUPPORTED |
| 921 |
|
| 922 |
void PNGAPI |
| 923 |
png_set_flush(png_structrp png_ptr, int nrows) |
| 924 |
{ |
| 925 |
png_debug(1, "in png_set_flush"); |
| 926 |
|
| 927 |
if (png_ptr == NULL) |
| 928 |
return; |
| 929 |
|
| 930 |
png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows); |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
void PNGAPI |
| 935 |
png_write_flush(png_structrp png_ptr) |
| 936 |
{ |
| 937 |
png_debug(1, "in png_write_flush"); |
| 938 |
|
| 939 |
if (png_ptr == NULL) |
| 940 |
return; |
| 941 |
|
| 942 |
|
| 943 |
if (png_ptr->row_number >= png_ptr->num_rows) |
| 944 |
return; |
| 945 |
|
| 946 |
png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH); |
| 947 |
png_ptr->flush_rows = 0; |
| 948 |
png_flush(png_ptr); |
| 949 |
} |
| 950 |
#endif |
| 951 |
|
| 952 |
|
| 953 |
static void |
| 954 |
png_write_destroy(png_structrp png_ptr) |
| 955 |
{ |
| 956 |
png_debug(1, "in png_write_destroy"); |
| 957 |
|
| 958 |
|
| 959 |
if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) |
| 960 |
deflateEnd(&png_ptr->zstream); |
| 961 |
|
| 962 |
|
| 963 |
png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list); |
| 964 |
png_free(png_ptr, png_ptr->row_buf); |
| 965 |
png_ptr->row_buf = NULL; |
| 966 |
#ifdef PNG_WRITE_FILTER_SUPPORTED |
| 967 |
png_free(png_ptr, png_ptr->prev_row); |
| 968 |
png_free(png_ptr, png_ptr->try_row); |
| 969 |
png_free(png_ptr, png_ptr->tst_row); |
| 970 |
png_ptr->prev_row = NULL; |
| 971 |
png_ptr->try_row = NULL; |
| 972 |
png_ptr->tst_row = NULL; |
| 973 |
#endif |
| 974 |
|
| 975 |
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
| 976 |
png_free(png_ptr, png_ptr->chunk_list); |
| 977 |
png_ptr->chunk_list = NULL; |
| 978 |
#endif |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
void PNGAPI |
| 994 |
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) |
| 995 |
{ |
| 996 |
png_debug(1, "in png_destroy_write_struct"); |
| 997 |
|
| 998 |
if (png_ptr_ptr != NULL) |
| 999 |
{ |
| 1000 |
png_structrp png_ptr = *png_ptr_ptr; |
| 1001 |
|
| 1002 |
if (png_ptr != NULL) |
| 1003 |
{ |
| 1004 |
png_destroy_info_struct(png_ptr, info_ptr_ptr); |
| 1005 |
|
| 1006 |
*png_ptr_ptr = NULL; |
| 1007 |
png_write_destroy(png_ptr); |
| 1008 |
png_destroy_png_struct(png_ptr); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
void PNGAPI |
| 1015 |
png_set_filter(png_structrp png_ptr, int method, int filters) |
| 1016 |
{ |
| 1017 |
png_debug(1, "in png_set_filter"); |
| 1018 |
|
| 1019 |
if (png_ptr == NULL) |
| 1020 |
return; |
| 1021 |
|
| 1022 |
#ifdef PNG_MNG_FEATURES_SUPPORTED |
| 1023 |
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && |
| 1024 |
(method == PNG_INTRAPIXEL_DIFFERENCING)) |
| 1025 |
method = PNG_FILTER_TYPE_BASE; |
| 1026 |
|
| 1027 |
#endif |
| 1028 |
if (method == PNG_FILTER_TYPE_BASE) |
| 1029 |
{ |
| 1030 |
switch (filters & (PNG_ALL_FILTERS | 0x07)) |
| 1031 |
{ |
| 1032 |
#ifdef PNG_WRITE_FILTER_SUPPORTED |
| 1033 |
case 5: |
| 1034 |
case 6: |
| 1035 |
case 7: png_app_error(png_ptr, "Unknown row filter for method 0"); |
| 1036 |
#endif |
| 1037 |
|
| 1038 |
case PNG_FILTER_VALUE_NONE: |
| 1039 |
png_ptr->do_filter = PNG_FILTER_NONE; break; |
| 1040 |
|
| 1041 |
#ifdef PNG_WRITE_FILTER_SUPPORTED |
| 1042 |
case PNG_FILTER_VALUE_SUB: |
| 1043 |
png_ptr->do_filter = PNG_FILTER_SUB; break; |
| 1044 |
|
| 1045 |
case PNG_FILTER_VALUE_UP: |
| 1046 |
png_ptr->do_filter = PNG_FILTER_UP; break; |
| 1047 |
|
| 1048 |
case PNG_FILTER_VALUE_AVG: |
| 1049 |
png_ptr->do_filter = PNG_FILTER_AVG; break; |
| 1050 |
|
| 1051 |
case PNG_FILTER_VALUE_PAETH: |
| 1052 |
png_ptr->do_filter = PNG_FILTER_PAETH; break; |
| 1053 |
|
| 1054 |
default: |
| 1055 |
png_ptr->do_filter = (png_byte)filters; break; |
| 1056 |
#else |
| 1057 |
default: |
| 1058 |
png_app_error(png_ptr, "Unknown row filter for method 0"); |
| 1059 |
#endif |
| 1060 |
} |
| 1061 |
|
| 1062 |
#ifdef PNG_WRITE_FILTER_SUPPORTED |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
if (png_ptr->row_buf != NULL) |
| 1077 |
{ |
| 1078 |
int num_filters; |
| 1079 |
png_alloc_size_t buf_size; |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
if (png_ptr->height == 1) |
| 1086 |
filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); |
| 1087 |
|
| 1088 |
if (png_ptr->width == 1) |
| 1089 |
filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH); |
| 1090 |
|
| 1091 |
if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0 |
| 1092 |
&& png_ptr->prev_row == NULL) |
| 1093 |
{ |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
png_app_warning(png_ptr, |
| 1098 |
"png_set_filter: UP/AVG/PAETH cannot be added after start"); |
| 1099 |
filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); |
| 1100 |
} |
| 1101 |
|
| 1102 |
num_filters = 0; |
| 1103 |
|
| 1104 |
if (filters & PNG_FILTER_SUB) |
| 1105 |
num_filters++; |
| 1106 |
|
| 1107 |
if (filters & PNG_FILTER_UP) |
| 1108 |
num_filters++; |
| 1109 |
|
| 1110 |
if (filters & PNG_FILTER_AVG) |
| 1111 |
num_filters++; |
| 1112 |
|
| 1113 |
if (filters & PNG_FILTER_PAETH) |
| 1114 |
num_filters++; |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth, |
| 1120 |
png_ptr->width) + 1; |
| 1121 |
|
| 1122 |
if (png_ptr->try_row == NULL) |
| 1123 |
png_ptr->try_row = png_voidcast(png_bytep, |
| 1124 |
png_malloc(png_ptr, buf_size)); |
| 1125 |
|
| 1126 |
if (num_filters > 1) |
| 1127 |
{ |
| 1128 |
if (png_ptr->tst_row == NULL) |
| 1129 |
png_ptr->tst_row = png_voidcast(png_bytep, |
| 1130 |
png_malloc(png_ptr, buf_size)); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
png_ptr->do_filter = (png_byte)filters; |
| 1134 |
#endif |
| 1135 |
} |
| 1136 |
else |
| 1137 |
png_error(png_ptr, "Unknown custom filter method"); |
| 1138 |
} |
| 1139 |
|
| 1140 |
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
| 1141 |
|
| 1142 |
#ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1143 |
void PNGAPI |
| 1144 |
png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method, |
| 1145 |
int num_weights, png_const_doublep filter_weights, |
| 1146 |
png_const_doublep filter_costs) |
| 1147 |
{ |
| 1148 |
PNG_UNUSED(png_ptr) |
| 1149 |
PNG_UNUSED(heuristic_method) |
| 1150 |
PNG_UNUSED(num_weights) |
| 1151 |
PNG_UNUSED(filter_weights) |
| 1152 |
PNG_UNUSED(filter_costs) |
| 1153 |
} |
| 1154 |
#endif |
| 1155 |
|
| 1156 |
#ifdef PNG_FIXED_POINT_SUPPORTED |
| 1157 |
void PNGAPI |
| 1158 |
png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method, |
| 1159 |
int num_weights, png_const_fixed_point_p filter_weights, |
| 1160 |
png_const_fixed_point_p filter_costs) |
| 1161 |
{ |
| 1162 |
PNG_UNUSED(png_ptr) |
| 1163 |
PNG_UNUSED(heuristic_method) |
| 1164 |
PNG_UNUSED(num_weights) |
| 1165 |
PNG_UNUSED(filter_weights) |
| 1166 |
PNG_UNUSED(filter_costs) |
| 1167 |
} |
| 1168 |
#endif |
| 1169 |
#endif |
| 1170 |
|
| 1171 |
#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED |
| 1172 |
void PNGAPI |
| 1173 |
png_set_compression_level(png_structrp png_ptr, int level) |
| 1174 |
{ |
| 1175 |
png_debug(1, "in png_set_compression_level"); |
| 1176 |
|
| 1177 |
if (png_ptr == NULL) |
| 1178 |
return; |
| 1179 |
|
| 1180 |
png_ptr->zlib_level = level; |
| 1181 |
} |
| 1182 |
|
| 1183 |
void PNGAPI |
| 1184 |
png_set_compression_mem_level(png_structrp png_ptr, int mem_level) |
| 1185 |
{ |
| 1186 |
png_debug(1, "in png_set_compression_mem_level"); |
| 1187 |
|
| 1188 |
if (png_ptr == NULL) |
| 1189 |
return; |
| 1190 |
|
| 1191 |
png_ptr->zlib_mem_level = mem_level; |
| 1192 |
} |
| 1193 |
|
| 1194 |
void PNGAPI |
| 1195 |
png_set_compression_strategy(png_structrp png_ptr, int strategy) |
| 1196 |
{ |
| 1197 |
png_debug(1, "in png_set_compression_strategy"); |
| 1198 |
|
| 1199 |
if (png_ptr == NULL) |
| 1200 |
return; |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; |
| 1205 |
png_ptr->zlib_strategy = strategy; |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
void PNGAPI |
| 1212 |
png_set_compression_window_bits(png_structrp png_ptr, int window_bits) |
| 1213 |
{ |
| 1214 |
png_debug(1, "in png_set_compression_window_bits"); |
| 1215 |
|
| 1216 |
if (png_ptr == NULL) |
| 1217 |
return; |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
if (window_bits > 15) |
| 1226 |
{ |
| 1227 |
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); |
| 1228 |
window_bits = 15; |
| 1229 |
} |
| 1230 |
|
| 1231 |
else if (window_bits < 8) |
| 1232 |
{ |
| 1233 |
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); |
| 1234 |
window_bits = 8; |
| 1235 |
} |
| 1236 |
|
| 1237 |
png_ptr->zlib_window_bits = window_bits; |
| 1238 |
} |
| 1239 |
|
| 1240 |
void PNGAPI |
| 1241 |
png_set_compression_method(png_structrp png_ptr, int method) |
| 1242 |
{ |
| 1243 |
png_debug(1, "in png_set_compression_method"); |
| 1244 |
|
| 1245 |
if (png_ptr == NULL) |
| 1246 |
return; |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
if (method != 8) |
| 1252 |
png_warning(png_ptr, "Only compression method 8 is supported by PNG"); |
| 1253 |
|
| 1254 |
png_ptr->zlib_method = method; |
| 1255 |
} |
| 1256 |
#endif |
| 1257 |
|
| 1258 |
|
| 1259 |
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED |
| 1260 |
void PNGAPI |
| 1261 |
png_set_text_compression_level(png_structrp png_ptr, int level) |
| 1262 |
{ |
| 1263 |
png_debug(1, "in png_set_text_compression_level"); |
| 1264 |
|
| 1265 |
if (png_ptr == NULL) |
| 1266 |
return; |
| 1267 |
|
| 1268 |
png_ptr->zlib_text_level = level; |
| 1269 |
} |
| 1270 |
|
| 1271 |
void PNGAPI |
| 1272 |
png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level) |
| 1273 |
{ |
| 1274 |
png_debug(1, "in png_set_text_compression_mem_level"); |
| 1275 |
|
| 1276 |
if (png_ptr == NULL) |
| 1277 |
return; |
| 1278 |
|
| 1279 |
png_ptr->zlib_text_mem_level = mem_level; |
| 1280 |
} |
| 1281 |
|
| 1282 |
void PNGAPI |
| 1283 |
png_set_text_compression_strategy(png_structrp png_ptr, int strategy) |
| 1284 |
{ |
| 1285 |
png_debug(1, "in png_set_text_compression_strategy"); |
| 1286 |
|
| 1287 |
if (png_ptr == NULL) |
| 1288 |
return; |
| 1289 |
|
| 1290 |
png_ptr->zlib_text_strategy = strategy; |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
void PNGAPI |
| 1297 |
png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits) |
| 1298 |
{ |
| 1299 |
png_debug(1, "in png_set_text_compression_window_bits"); |
| 1300 |
|
| 1301 |
if (png_ptr == NULL) |
| 1302 |
return; |
| 1303 |
|
| 1304 |
if (window_bits > 15) |
| 1305 |
{ |
| 1306 |
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); |
| 1307 |
window_bits = 15; |
| 1308 |
} |
| 1309 |
|
| 1310 |
else if (window_bits < 8) |
| 1311 |
{ |
| 1312 |
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); |
| 1313 |
window_bits = 8; |
| 1314 |
} |
| 1315 |
|
| 1316 |
png_ptr->zlib_text_window_bits = window_bits; |
| 1317 |
} |
| 1318 |
|
| 1319 |
void PNGAPI |
| 1320 |
png_set_text_compression_method(png_structrp png_ptr, int method) |
| 1321 |
{ |
| 1322 |
png_debug(1, "in png_set_text_compression_method"); |
| 1323 |
|
| 1324 |
if (png_ptr == NULL) |
| 1325 |
return; |
| 1326 |
|
| 1327 |
if (method != 8) |
| 1328 |
png_warning(png_ptr, "Only compression method 8 is supported by PNG"); |
| 1329 |
|
| 1330 |
png_ptr->zlib_text_method = method; |
| 1331 |
} |
| 1332 |
#endif |
| 1333 |
|
| 1334 |
|
| 1335 |
void PNGAPI |
| 1336 |
png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn) |
| 1337 |
{ |
| 1338 |
png_debug(1, "in png_set_write_status_fn"); |
| 1339 |
|
| 1340 |
if (png_ptr == NULL) |
| 1341 |
return; |
| 1342 |
|
| 1343 |
png_ptr->write_row_fn = write_row_fn; |
| 1344 |
} |
| 1345 |
|
| 1346 |
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
| 1347 |
void PNGAPI |
| 1348 |
png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr |
| 1349 |
write_user_transform_fn) |
| 1350 |
{ |
| 1351 |
png_debug(1, "in png_set_write_user_transform_fn"); |
| 1352 |
|
| 1353 |
if (png_ptr == NULL) |
| 1354 |
return; |
| 1355 |
|
| 1356 |
png_ptr->transformations |= PNG_USER_TRANSFORM; |
| 1357 |
png_ptr->write_user_transform_fn = write_user_transform_fn; |
| 1358 |
} |
| 1359 |
#endif |
| 1360 |
|
| 1361 |
|
| 1362 |
#ifdef PNG_INFO_IMAGE_SUPPORTED |
| 1363 |
void PNGAPI |
| 1364 |
png_write_png(png_structrp png_ptr, png_inforp info_ptr, |
| 1365 |
int transforms, voidp params) |
| 1366 |
{ |
| 1367 |
png_debug(1, "in png_write_png"); |
| 1368 |
|
| 1369 |
if (png_ptr == NULL || info_ptr == NULL) |
| 1370 |
return; |
| 1371 |
|
| 1372 |
if ((info_ptr->valid & PNG_INFO_IDAT) == 0) |
| 1373 |
{ |
| 1374 |
png_app_error(png_ptr, "no rows for png_write_image to write"); |
| 1375 |
return; |
| 1376 |
} |
| 1377 |
|
| 1378 |
|
| 1379 |
png_write_info(png_ptr, info_ptr); |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) |
| 1385 |
#ifdef PNG_WRITE_INVERT_SUPPORTED |
| 1386 |
png_set_invert_mono(png_ptr); |
| 1387 |
#else |
| 1388 |
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); |
| 1389 |
#endif |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
if ((transforms & PNG_TRANSFORM_SHIFT) != 0) |
| 1395 |
#ifdef PNG_WRITE_SHIFT_SUPPORTED |
| 1396 |
if ((info_ptr->valid & PNG_INFO_sBIT) != 0) |
| 1397 |
png_set_shift(png_ptr, &info_ptr->sig_bit); |
| 1398 |
#else |
| 1399 |
png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); |
| 1400 |
#endif |
| 1401 |
|
| 1402 |
|
| 1403 |
if ((transforms & PNG_TRANSFORM_PACKING) != 0) |
| 1404 |
#ifdef PNG_WRITE_PACK_SUPPORTED |
| 1405 |
png_set_packing(png_ptr); |
| 1406 |
#else |
| 1407 |
png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); |
| 1408 |
#endif |
| 1409 |
|
| 1410 |
|
| 1411 |
if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) |
| 1412 |
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
| 1413 |
png_set_swap_alpha(png_ptr); |
| 1414 |
#else |
| 1415 |
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); |
| 1416 |
#endif |
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER| |
| 1423 |
PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0) |
| 1424 |
{ |
| 1425 |
#ifdef PNG_WRITE_FILLER_SUPPORTED |
| 1426 |
if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0) |
| 1427 |
{ |
| 1428 |
if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0) |
| 1429 |
png_app_error(png_ptr, |
| 1430 |
"PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported"); |
| 1431 |
|
| 1432 |
|
| 1433 |
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); |
| 1434 |
} |
| 1435 |
|
| 1436 |
else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0) |
| 1437 |
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); |
| 1438 |
#else |
| 1439 |
png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported"); |
| 1440 |
#endif |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
if ((transforms & PNG_TRANSFORM_BGR) != 0) |
| 1445 |
#ifdef PNG_WRITE_BGR_SUPPORTED |
| 1446 |
png_set_bgr(png_ptr); |
| 1447 |
#else |
| 1448 |
png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); |
| 1449 |
#endif |
| 1450 |
|
| 1451 |
|
| 1452 |
if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) |
| 1453 |
#ifdef PNG_WRITE_SWAP_SUPPORTED |
| 1454 |
png_set_swap(png_ptr); |
| 1455 |
#else |
| 1456 |
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); |
| 1457 |
#endif |
| 1458 |
|
| 1459 |
|
| 1460 |
if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) |
| 1461 |
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED |
| 1462 |
png_set_packswap(png_ptr); |
| 1463 |
#else |
| 1464 |
png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); |
| 1465 |
#endif |
| 1466 |
|
| 1467 |
|
| 1468 |
if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) |
| 1469 |
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
| 1470 |
png_set_invert_alpha(png_ptr); |
| 1471 |
#else |
| 1472 |
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); |
| 1473 |
#endif |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
png_write_image(png_ptr, info_ptr->row_pointers); |
| 1479 |
|
| 1480 |
|
| 1481 |
png_write_end(png_ptr, info_ptr); |
| 1482 |
|
| 1483 |
PNG_UNUSED(params) |
| 1484 |
} |
| 1485 |
#endif |
| 1486 |
|
| 1487 |
|
| 1488 |
#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED |
| 1489 |
|
| 1490 |
static int |
| 1491 |
png_image_write_init(png_imagep image) |
| 1492 |
{ |
| 1493 |
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image, |
| 1494 |
png_safe_error, png_safe_warning); |
| 1495 |
|
| 1496 |
if (png_ptr != NULL) |
| 1497 |
{ |
| 1498 |
png_infop info_ptr = png_create_info_struct(png_ptr); |
| 1499 |
|
| 1500 |
if (info_ptr != NULL) |
| 1501 |
{ |
| 1502 |
png_controlp control = png_voidcast(png_controlp, |
| 1503 |
png_malloc_warn(png_ptr, (sizeof *control))); |
| 1504 |
|
| 1505 |
if (control != NULL) |
| 1506 |
{ |
| 1507 |
memset(control, 0, (sizeof *control)); |
| 1508 |
|
| 1509 |
control->png_ptr = png_ptr; |
| 1510 |
control->info_ptr = info_ptr; |
| 1511 |
control->for_write = 1; |
| 1512 |
|
| 1513 |
image->opaque = control; |
| 1514 |
return 1; |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
png_destroy_info_struct(png_ptr, &info_ptr); |
| 1519 |
} |
| 1520 |
|
| 1521 |
png_destroy_write_struct(&png_ptr, NULL); |
| 1522 |
} |
| 1523 |
|
| 1524 |
return png_image_error(image, "png_image_write_: out of memory"); |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
typedef struct |
| 1529 |
{ |
| 1530 |
|
| 1531 |
png_imagep image; |
| 1532 |
png_const_voidp buffer; |
| 1533 |
png_int_32 row_stride; |
| 1534 |
png_const_voidp colormap; |
| 1535 |
int convert_to_8bit; |
| 1536 |
|
| 1537 |
png_const_voidp first_row; |
| 1538 |
ptrdiff_t row_bytes; |
| 1539 |
png_voidp local_row; |
| 1540 |
|
| 1541 |
png_bytep memory; |
| 1542 |
png_alloc_size_t memory_bytes; |
| 1543 |
png_alloc_size_t output_bytes; |
| 1544 |
} png_image_write_control; |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
static int |
| 1551 |
png_write_image_16bit(png_voidp argument) |
| 1552 |
{ |
| 1553 |
png_image_write_control *display = png_voidcast(png_image_write_control*, |
| 1554 |
argument); |
| 1555 |
png_imagep image = display->image; |
| 1556 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 1557 |
|
| 1558 |
png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, |
| 1559 |
display->first_row); |
| 1560 |
png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row); |
| 1561 |
png_uint_16p row_end; |
| 1562 |
unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? |
| 1563 |
3 : 1; |
| 1564 |
int aindex = 0; |
| 1565 |
png_uint_32 y = image->height; |
| 1566 |
|
| 1567 |
if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 1568 |
{ |
| 1569 |
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED |
| 1570 |
if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0) |
| 1571 |
{ |
| 1572 |
aindex = -1; |
| 1573 |
++input_row; |
| 1574 |
++output_row; |
| 1575 |
} |
| 1576 |
else |
| 1577 |
aindex = (int)channels; |
| 1578 |
# else |
| 1579 |
aindex = (int)channels; |
| 1580 |
# endif |
| 1581 |
} |
| 1582 |
|
| 1583 |
else |
| 1584 |
png_error(png_ptr, "png_write_image: internal call error"); |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
row_end = output_row + image->width * (channels+1); |
| 1591 |
|
| 1592 |
for (; y > 0; --y) |
| 1593 |
{ |
| 1594 |
png_const_uint_16p in_ptr = input_row; |
| 1595 |
png_uint_16p out_ptr = output_row; |
| 1596 |
|
| 1597 |
while (out_ptr < row_end) |
| 1598 |
{ |
| 1599 |
png_uint_16 alpha = in_ptr[aindex]; |
| 1600 |
png_uint_32 reciprocal = 0; |
| 1601 |
int c; |
| 1602 |
|
| 1603 |
out_ptr[aindex] = alpha; |
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
if (alpha > 0 && alpha < 65535) |
| 1611 |
reciprocal = ((0xffff<<15)+(alpha>>1))/alpha; |
| 1612 |
|
| 1613 |
c = (int)channels; |
| 1614 |
do |
| 1615 |
{ |
| 1616 |
png_uint_16 component = *in_ptr++; |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
if (component >= alpha) |
| 1626 |
component = 65535; |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
else if (component > 0 && alpha < 65535) |
| 1632 |
{ |
| 1633 |
png_uint_32 calc = component * reciprocal; |
| 1634 |
calc += 16384; |
| 1635 |
component = (png_uint_16)(calc >> 15); |
| 1636 |
} |
| 1637 |
|
| 1638 |
*out_ptr++ = component; |
| 1639 |
} |
| 1640 |
while (--c > 0); |
| 1641 |
|
| 1642 |
|
| 1643 |
++in_ptr; |
| 1644 |
++out_ptr; |
| 1645 |
} |
| 1646 |
|
| 1647 |
png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row)); |
| 1648 |
input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16)); |
| 1649 |
} |
| 1650 |
|
| 1651 |
return 1; |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
# define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+((alpha)>>1))/(alpha)) |
| 1663 |
|
| 1664 |
static png_byte |
| 1665 |
png_unpremultiply(png_uint_32 component, png_uint_32 alpha, |
| 1666 |
png_uint_32 reciprocal) |
| 1667 |
{ |
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
if (component >= alpha || alpha < 128) |
| 1679 |
return 255; |
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
else if (component > 0) |
| 1685 |
{ |
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
if (alpha < 65407) |
| 1692 |
{ |
| 1693 |
component *= reciprocal; |
| 1694 |
component += 64; |
| 1695 |
component >>= 7; |
| 1696 |
} |
| 1697 |
|
| 1698 |
else |
| 1699 |
component *= 255; |
| 1700 |
|
| 1701 |
|
| 1702 |
return (png_byte)PNG_sRGB_FROM_LINEAR(component); |
| 1703 |
} |
| 1704 |
|
| 1705 |
else |
| 1706 |
return 0; |
| 1707 |
} |
| 1708 |
|
| 1709 |
static int |
| 1710 |
png_write_image_8bit(png_voidp argument) |
| 1711 |
{ |
| 1712 |
png_image_write_control *display = png_voidcast(png_image_write_control*, |
| 1713 |
argument); |
| 1714 |
png_imagep image = display->image; |
| 1715 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 1716 |
|
| 1717 |
png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, |
| 1718 |
display->first_row); |
| 1719 |
png_bytep output_row = png_voidcast(png_bytep, display->local_row); |
| 1720 |
png_uint_32 y = image->height; |
| 1721 |
unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? |
| 1722 |
3 : 1; |
| 1723 |
|
| 1724 |
if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 1725 |
{ |
| 1726 |
png_bytep row_end; |
| 1727 |
int aindex; |
| 1728 |
|
| 1729 |
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED |
| 1730 |
if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0) |
| 1731 |
{ |
| 1732 |
aindex = -1; |
| 1733 |
++input_row; |
| 1734 |
++output_row; |
| 1735 |
} |
| 1736 |
|
| 1737 |
else |
| 1738 |
# endif |
| 1739 |
aindex = (int)channels; |
| 1740 |
|
| 1741 |
|
| 1742 |
row_end = output_row + image->width * (channels+1); |
| 1743 |
|
| 1744 |
for (; y > 0; --y) |
| 1745 |
{ |
| 1746 |
png_const_uint_16p in_ptr = input_row; |
| 1747 |
png_bytep out_ptr = output_row; |
| 1748 |
|
| 1749 |
while (out_ptr < row_end) |
| 1750 |
{ |
| 1751 |
png_uint_16 alpha = in_ptr[aindex]; |
| 1752 |
png_byte alphabyte = (png_byte)PNG_DIV257(alpha); |
| 1753 |
png_uint_32 reciprocal = 0; |
| 1754 |
int c; |
| 1755 |
|
| 1756 |
|
| 1757 |
out_ptr[aindex] = alphabyte; |
| 1758 |
|
| 1759 |
if (alphabyte > 0 && alphabyte < 255) |
| 1760 |
reciprocal = UNP_RECIPROCAL(alpha); |
| 1761 |
|
| 1762 |
c = (int)channels; |
| 1763 |
do |
| 1764 |
*out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal); |
| 1765 |
while (--c > 0); |
| 1766 |
|
| 1767 |
|
| 1768 |
++in_ptr; |
| 1769 |
++out_ptr; |
| 1770 |
} |
| 1771 |
|
| 1772 |
png_write_row(png_ptr, png_voidcast(png_const_bytep, |
| 1773 |
display->local_row)); |
| 1774 |
input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16)); |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
else |
| 1779 |
{ |
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
png_bytep row_end = output_row + image->width * channels; |
| 1784 |
|
| 1785 |
for (; y > 0; --y) |
| 1786 |
{ |
| 1787 |
png_const_uint_16p in_ptr = input_row; |
| 1788 |
png_bytep out_ptr = output_row; |
| 1789 |
|
| 1790 |
while (out_ptr < row_end) |
| 1791 |
{ |
| 1792 |
png_uint_32 component = *in_ptr++; |
| 1793 |
|
| 1794 |
component *= 255; |
| 1795 |
*out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component); |
| 1796 |
} |
| 1797 |
|
| 1798 |
png_write_row(png_ptr, output_row); |
| 1799 |
input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16)); |
| 1800 |
} |
| 1801 |
} |
| 1802 |
|
| 1803 |
return 1; |
| 1804 |
} |
| 1805 |
|
| 1806 |
static void |
| 1807 |
png_image_set_PLTE(png_image_write_control *display) |
| 1808 |
{ |
| 1809 |
png_imagep image = display->image; |
| 1810 |
const void *cmap = display->colormap; |
| 1811 |
int entries = image->colormap_entries > 256 ? 256 : |
| 1812 |
(int)image->colormap_entries; |
| 1813 |
|
| 1814 |
|
| 1815 |
png_uint_32 format = image->format; |
| 1816 |
unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format); |
| 1817 |
|
| 1818 |
# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\ |
| 1819 |
defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED) |
| 1820 |
int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 && |
| 1821 |
(format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 1822 |
# else |
| 1823 |
# define afirst 0 |
| 1824 |
# endif |
| 1825 |
|
| 1826 |
# ifdef PNG_FORMAT_BGR_SUPPORTED |
| 1827 |
int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; |
| 1828 |
# else |
| 1829 |
# define bgr 0 |
| 1830 |
# endif |
| 1831 |
|
| 1832 |
int i, num_trans; |
| 1833 |
png_color palette[256]; |
| 1834 |
png_byte tRNS[256]; |
| 1835 |
|
| 1836 |
memset(tRNS, 255, (sizeof tRNS)); |
| 1837 |
memset(palette, 0, (sizeof palette)); |
| 1838 |
|
| 1839 |
for (i=num_trans=0; i<entries; ++i) |
| 1840 |
{ |
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
if ((format & PNG_FORMAT_FLAG_LINEAR) != 0) |
| 1845 |
{ |
| 1846 |
png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap); |
| 1847 |
|
| 1848 |
entry += (unsigned int)i * channels; |
| 1849 |
|
| 1850 |
if ((channels & 1) != 0) |
| 1851 |
{ |
| 1852 |
if (channels >= 3) |
| 1853 |
{ |
| 1854 |
palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 * |
| 1855 |
entry[(2 ^ bgr)]); |
| 1856 |
palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 * |
| 1857 |
entry[1]); |
| 1858 |
palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 * |
| 1859 |
entry[bgr]); |
| 1860 |
} |
| 1861 |
|
| 1862 |
else |
| 1863 |
palette[i].blue = palette[i].red = palette[i].green = |
| 1864 |
(png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry); |
| 1865 |
} |
| 1866 |
|
| 1867 |
else |
| 1868 |
{ |
| 1869 |
png_uint_16 alpha = entry[afirst ? 0 : channels-1]; |
| 1870 |
png_byte alphabyte = (png_byte)PNG_DIV257(alpha); |
| 1871 |
png_uint_32 reciprocal = 0; |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
if (alphabyte > 0 && alphabyte < 255) |
| 1878 |
reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha; |
| 1879 |
|
| 1880 |
tRNS[i] = alphabyte; |
| 1881 |
if (alphabyte < 255) |
| 1882 |
num_trans = i+1; |
| 1883 |
|
| 1884 |
if (channels >= 3) |
| 1885 |
{ |
| 1886 |
palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)], |
| 1887 |
alpha, reciprocal); |
| 1888 |
palette[i].green = png_unpremultiply(entry[afirst + 1], alpha, |
| 1889 |
reciprocal); |
| 1890 |
palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha, |
| 1891 |
reciprocal); |
| 1892 |
} |
| 1893 |
|
| 1894 |
else |
| 1895 |
palette[i].blue = palette[i].red = palette[i].green = |
| 1896 |
png_unpremultiply(entry[afirst], alpha, reciprocal); |
| 1897 |
} |
| 1898 |
} |
| 1899 |
|
| 1900 |
else |
| 1901 |
{ |
| 1902 |
png_const_bytep entry = png_voidcast(png_const_bytep, cmap); |
| 1903 |
|
| 1904 |
entry += (unsigned int)i * channels; |
| 1905 |
|
| 1906 |
switch (channels) |
| 1907 |
{ |
| 1908 |
case 4: |
| 1909 |
tRNS[i] = entry[afirst ? 0 : 3]; |
| 1910 |
if (tRNS[i] < 255) |
| 1911 |
num_trans = i+1; |
| 1912 |
|
| 1913 |
case 3: |
| 1914 |
palette[i].blue = entry[afirst + (2 ^ bgr)]; |
| 1915 |
palette[i].green = entry[afirst + 1]; |
| 1916 |
palette[i].red = entry[afirst + bgr]; |
| 1917 |
break; |
| 1918 |
|
| 1919 |
case 2: |
| 1920 |
tRNS[i] = entry[1 ^ afirst]; |
| 1921 |
if (tRNS[i] < 255) |
| 1922 |
num_trans = i+1; |
| 1923 |
|
| 1924 |
case 1: |
| 1925 |
palette[i].blue = palette[i].red = palette[i].green = |
| 1926 |
entry[afirst]; |
| 1927 |
break; |
| 1928 |
|
| 1929 |
default: |
| 1930 |
break; |
| 1931 |
} |
| 1932 |
} |
| 1933 |
} |
| 1934 |
|
| 1935 |
# ifdef afirst |
| 1936 |
# undef afirst |
| 1937 |
# endif |
| 1938 |
# ifdef bgr |
| 1939 |
# undef bgr |
| 1940 |
# endif |
| 1941 |
|
| 1942 |
png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette, |
| 1943 |
entries); |
| 1944 |
|
| 1945 |
if (num_trans > 0) |
| 1946 |
png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS, |
| 1947 |
num_trans, NULL); |
| 1948 |
|
| 1949 |
image->colormap_entries = (png_uint_32)entries; |
| 1950 |
} |
| 1951 |
|
| 1952 |
static int |
| 1953 |
png_image_write_main(png_voidp argument) |
| 1954 |
{ |
| 1955 |
png_image_write_control *display = png_voidcast(png_image_write_control*, |
| 1956 |
argument); |
| 1957 |
png_imagep image = display->image; |
| 1958 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 1959 |
png_inforp info_ptr = image->opaque->info_ptr; |
| 1960 |
png_uint_32 format = image->format; |
| 1961 |
|
| 1962 |
|
| 1963 |
int colormap = (format & PNG_FORMAT_FLAG_COLORMAP); |
| 1964 |
int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); |
| 1965 |
int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA); |
| 1966 |
int write_16bit = linear && (display->convert_to_8bit == 0); |
| 1967 |
|
| 1968 |
# ifdef PNG_BENIGN_ERRORS_SUPPORTED |
| 1969 |
|
| 1970 |
png_set_benign_errors(png_ptr, 0); |
| 1971 |
# endif |
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
{ |
| 1977 |
unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format); |
| 1978 |
|
| 1979 |
if (image->width <= 0x7fffffffU/channels) |
| 1980 |
{ |
| 1981 |
png_uint_32 check; |
| 1982 |
png_uint_32 png_row_stride = image->width * channels; |
| 1983 |
|
| 1984 |
if (display->row_stride == 0) |
| 1985 |
display->row_stride = (png_int_32)png_row_stride; |
| 1986 |
|
| 1987 |
if (display->row_stride < 0) |
| 1988 |
check = (png_uint_32)(-display->row_stride); |
| 1989 |
|
| 1990 |
else |
| 1991 |
check = (png_uint_32)display->row_stride; |
| 1992 |
|
| 1993 |
if (check >= png_row_stride) |
| 1994 |
{ |
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
if (image->height > 0xffffffffU/png_row_stride) |
| 2000 |
png_error(image->opaque->png_ptr, "memory image too large"); |
| 2001 |
} |
| 2002 |
|
| 2003 |
else |
| 2004 |
png_error(image->opaque->png_ptr, "supplied row stride too small"); |
| 2005 |
} |
| 2006 |
|
| 2007 |
else |
| 2008 |
png_error(image->opaque->png_ptr, "image row stride too large"); |
| 2009 |
} |
| 2010 |
|
| 2011 |
|
| 2012 |
if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0) |
| 2013 |
{ |
| 2014 |
if (display->colormap != NULL && image->colormap_entries > 0) |
| 2015 |
{ |
| 2016 |
png_uint_32 entries = image->colormap_entries; |
| 2017 |
|
| 2018 |
png_set_IHDR(png_ptr, info_ptr, image->width, image->height, |
| 2019 |
entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)), |
| 2020 |
PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, |
| 2021 |
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 2022 |
|
| 2023 |
png_image_set_PLTE(display); |
| 2024 |
} |
| 2025 |
|
| 2026 |
else |
| 2027 |
png_error(image->opaque->png_ptr, |
| 2028 |
"no color-map for color-mapped image"); |
| 2029 |
} |
| 2030 |
|
| 2031 |
else |
| 2032 |
png_set_IHDR(png_ptr, info_ptr, image->width, image->height, |
| 2033 |
write_16bit ? 16 : 8, |
| 2034 |
((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) + |
| 2035 |
((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0), |
| 2036 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
if (write_16bit != 0) |
| 2045 |
{ |
| 2046 |
|
| 2047 |
png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR); |
| 2048 |
|
| 2049 |
if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0) |
| 2050 |
png_set_cHRM_fixed(png_ptr, info_ptr, |
| 2051 |
|
| 2052 |
31270, 32900, |
| 2053 |
64000, 33000, |
| 2054 |
30000, 60000, |
| 2055 |
15000, 6000 |
| 2056 |
); |
| 2057 |
} |
| 2058 |
|
| 2059 |
else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0) |
| 2060 |
png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL); |
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
else |
| 2066 |
png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE); |
| 2067 |
|
| 2068 |
|
| 2069 |
png_write_info(png_ptr, info_ptr); |
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
if (write_16bit != 0) |
| 2077 |
{ |
| 2078 |
png_uint_16 le = 0x0001; |
| 2079 |
|
| 2080 |
if ((*(png_const_bytep) & le) != 0) |
| 2081 |
png_set_swap(png_ptr); |
| 2082 |
} |
| 2083 |
|
| 2084 |
# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED |
| 2085 |
if ((format & PNG_FORMAT_FLAG_BGR) != 0) |
| 2086 |
{ |
| 2087 |
if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0) |
| 2088 |
png_set_bgr(png_ptr); |
| 2089 |
format &= ~PNG_FORMAT_FLAG_BGR; |
| 2090 |
} |
| 2091 |
# endif |
| 2092 |
|
| 2093 |
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED |
| 2094 |
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) |
| 2095 |
{ |
| 2096 |
if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2097 |
png_set_swap_alpha(png_ptr); |
| 2098 |
format &= ~PNG_FORMAT_FLAG_AFIRST; |
| 2099 |
} |
| 2100 |
# endif |
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
if (colormap != 0 && image->colormap_entries <= 16) |
| 2106 |
png_set_packing(png_ptr); |
| 2107 |
|
| 2108 |
|
| 2109 |
if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR | |
| 2110 |
PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0) |
| 2111 |
png_error(png_ptr, "png_write_image: unsupported transformation"); |
| 2112 |
|
| 2113 |
{ |
| 2114 |
png_const_bytep row = png_voidcast(png_const_bytep, display->buffer); |
| 2115 |
ptrdiff_t row_bytes = display->row_stride; |
| 2116 |
|
| 2117 |
if (linear != 0) |
| 2118 |
row_bytes *= (sizeof (png_uint_16)); |
| 2119 |
|
| 2120 |
if (row_bytes < 0) |
| 2121 |
row += (image->height-1) * (-row_bytes); |
| 2122 |
|
| 2123 |
display->first_row = row; |
| 2124 |
display->row_bytes = row_bytes; |
| 2125 |
} |
| 2126 |
|
| 2127 |
|
| 2128 |
if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0) |
| 2129 |
{ |
| 2130 |
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS); |
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
# ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED |
| 2137 |
png_set_compression_level(png_ptr, 3); |
| 2138 |
# endif |
| 2139 |
} |
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
if ((linear != 0 && alpha != 0 ) || |
| 2146 |
(colormap == 0 && display->convert_to_8bit != 0)) |
| 2147 |
{ |
| 2148 |
png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 2149 |
png_get_rowbytes(png_ptr, info_ptr))); |
| 2150 |
int result; |
| 2151 |
|
| 2152 |
display->local_row = row; |
| 2153 |
if (write_16bit != 0) |
| 2154 |
result = png_safe_execute(image, png_write_image_16bit, display); |
| 2155 |
else |
| 2156 |
result = png_safe_execute(image, png_write_image_8bit, display); |
| 2157 |
display->local_row = NULL; |
| 2158 |
|
| 2159 |
png_free(png_ptr, row); |
| 2160 |
|
| 2161 |
|
| 2162 |
if (result == 0) |
| 2163 |
return 0; |
| 2164 |
} |
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
else |
| 2170 |
{ |
| 2171 |
png_const_bytep row = png_voidcast(png_const_bytep, display->first_row); |
| 2172 |
ptrdiff_t row_bytes = display->row_bytes; |
| 2173 |
png_uint_32 y = image->height; |
| 2174 |
|
| 2175 |
for (; y > 0; --y) |
| 2176 |
{ |
| 2177 |
png_write_row(png_ptr, row); |
| 2178 |
row += row_bytes; |
| 2179 |
} |
| 2180 |
} |
| 2181 |
|
| 2182 |
png_write_end(png_ptr, info_ptr); |
| 2183 |
return 1; |
| 2184 |
} |
| 2185 |
|
| 2186 |
|
| 2187 |
static void (PNGCBAPI |
| 2188 |
image_memory_write)(png_structp png_ptr, png_bytep data, size_t size) |
| 2189 |
{ |
| 2190 |
png_image_write_control *display = png_voidcast(png_image_write_control*, |
| 2191 |
png_ptr->io_ptr); |
| 2192 |
png_alloc_size_t ob = display->output_bytes; |
| 2193 |
|
| 2194 |
|
| 2195 |
if (size <= ((png_alloc_size_t)-1) - ob) |
| 2196 |
{ |
| 2197 |
|
| 2198 |
if (size > 0) |
| 2199 |
{ |
| 2200 |
if (display->memory_bytes >= ob+size) |
| 2201 |
memcpy(display->memory+ob, data, size); |
| 2202 |
|
| 2203 |
|
| 2204 |
display->output_bytes = ob+size; |
| 2205 |
} |
| 2206 |
} |
| 2207 |
|
| 2208 |
else |
| 2209 |
png_error(png_ptr, "png_image_write_to_memory: PNG too big"); |
| 2210 |
} |
| 2211 |
|
| 2212 |
static void (PNGCBAPI |
| 2213 |
image_memory_flush)(png_structp png_ptr) |
| 2214 |
{ |
| 2215 |
PNG_UNUSED(png_ptr) |
| 2216 |
} |
| 2217 |
|
| 2218 |
static int |
| 2219 |
png_image_write_memory(png_voidp argument) |
| 2220 |
{ |
| 2221 |
png_image_write_control *display = png_voidcast(png_image_write_control*, |
| 2222 |
argument); |
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
png_set_write_fn(display->image->opaque->png_ptr, display, |
| 2229 |
image_memory_write, image_memory_flush); |
| 2230 |
|
| 2231 |
return png_image_write_main(display); |
| 2232 |
} |
| 2233 |
|
| 2234 |
int PNGAPI |
| 2235 |
png_image_write_to_memory(png_imagep image, void *memory, |
| 2236 |
png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit, |
| 2237 |
const void *buffer, png_int_32 row_stride, const void *colormap) |
| 2238 |
{ |
| 2239 |
|
| 2240 |
if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 2241 |
{ |
| 2242 |
if (memory_bytes != NULL && buffer != NULL) |
| 2243 |
{ |
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
if (memory == NULL) |
| 2248 |
*memory_bytes = 0; |
| 2249 |
|
| 2250 |
if (png_image_write_init(image) != 0) |
| 2251 |
{ |
| 2252 |
png_image_write_control display; |
| 2253 |
int result; |
| 2254 |
|
| 2255 |
memset(&display, 0, (sizeof display)); |
| 2256 |
display.image = image; |
| 2257 |
display.buffer = buffer; |
| 2258 |
display.row_stride = row_stride; |
| 2259 |
display.colormap = colormap; |
| 2260 |
display.convert_to_8bit = convert_to_8bit; |
| 2261 |
display.memory = png_voidcast(png_bytep, memory); |
| 2262 |
display.memory_bytes = *memory_bytes; |
| 2263 |
display.output_bytes = 0; |
| 2264 |
|
| 2265 |
result = png_safe_execute(image, png_image_write_memory, &display); |
| 2266 |
png_image_free(image); |
| 2267 |
|
| 2268 |
|
| 2269 |
if (result) |
| 2270 |
{ |
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
if (memory != NULL && display.output_bytes > *memory_bytes) |
| 2275 |
result = 0; |
| 2276 |
|
| 2277 |
*memory_bytes = display.output_bytes; |
| 2278 |
} |
| 2279 |
|
| 2280 |
return result; |
| 2281 |
} |
| 2282 |
|
| 2283 |
else |
| 2284 |
return 0; |
| 2285 |
} |
| 2286 |
|
| 2287 |
else |
| 2288 |
return png_image_error(image, |
| 2289 |
"png_image_write_to_memory: invalid argument"); |
| 2290 |
} |
| 2291 |
|
| 2292 |
else if (image != NULL) |
| 2293 |
return png_image_error(image, |
| 2294 |
"png_image_write_to_memory: incorrect PNG_IMAGE_VERSION"); |
| 2295 |
|
| 2296 |
else |
| 2297 |
return 0; |
| 2298 |
} |
| 2299 |
|
| 2300 |
#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED |
| 2301 |
int PNGAPI |
| 2302 |
png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit, |
| 2303 |
const void *buffer, png_int_32 row_stride, const void *colormap) |
| 2304 |
{ |
| 2305 |
|
| 2306 |
if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 2307 |
{ |
| 2308 |
if (file != NULL && buffer != NULL) |
| 2309 |
{ |
| 2310 |
if (png_image_write_init(image) != 0) |
| 2311 |
{ |
| 2312 |
png_image_write_control display; |
| 2313 |
int result; |
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
image->opaque->png_ptr->io_ptr = file; |
| 2320 |
|
| 2321 |
memset(&display, 0, (sizeof display)); |
| 2322 |
display.image = image; |
| 2323 |
display.buffer = buffer; |
| 2324 |
display.row_stride = row_stride; |
| 2325 |
display.colormap = colormap; |
| 2326 |
display.convert_to_8bit = convert_to_8bit; |
| 2327 |
|
| 2328 |
result = png_safe_execute(image, png_image_write_main, &display); |
| 2329 |
png_image_free(image); |
| 2330 |
return result; |
| 2331 |
} |
| 2332 |
|
| 2333 |
else |
| 2334 |
return 0; |
| 2335 |
} |
| 2336 |
|
| 2337 |
else |
| 2338 |
return png_image_error(image, |
| 2339 |
"png_image_write_to_stdio: invalid argument"); |
| 2340 |
} |
| 2341 |
|
| 2342 |
else if (image != NULL) |
| 2343 |
return png_image_error(image, |
| 2344 |
"png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION"); |
| 2345 |
|
| 2346 |
else |
| 2347 |
return 0; |
| 2348 |
} |
| 2349 |
|
| 2350 |
int PNGAPI |
| 2351 |
png_image_write_to_file(png_imagep image, const char *file_name, |
| 2352 |
int convert_to_8bit, const void *buffer, png_int_32 row_stride, |
| 2353 |
const void *colormap) |
| 2354 |
{ |
| 2355 |
|
| 2356 |
if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 2357 |
{ |
| 2358 |
if (file_name != NULL && buffer != NULL) |
| 2359 |
{ |
| 2360 |
FILE *fp = fopen(file_name, "wb"); |
| 2361 |
|
| 2362 |
if (fp != NULL) |
| 2363 |
{ |
| 2364 |
if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer, |
| 2365 |
row_stride, colormap) != 0) |
| 2366 |
{ |
| 2367 |
int error; |
| 2368 |
|
| 2369 |
|
| 2370 |
if (fflush(fp) == 0 && ferror(fp) == 0) |
| 2371 |
{ |
| 2372 |
if (fclose(fp) == 0) |
| 2373 |
return 1; |
| 2374 |
|
| 2375 |
error = errno; |
| 2376 |
} |
| 2377 |
|
| 2378 |
else |
| 2379 |
{ |
| 2380 |
error = errno; |
| 2381 |
(void)fclose(fp); |
| 2382 |
} |
| 2383 |
|
| 2384 |
(void)remove(file_name); |
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
return png_image_error(image, strerror(error)); |
| 2389 |
} |
| 2390 |
|
| 2391 |
else |
| 2392 |
{ |
| 2393 |
|
| 2394 |
(void)fclose(fp); |
| 2395 |
(void)remove(file_name); |
| 2396 |
return 0; |
| 2397 |
} |
| 2398 |
} |
| 2399 |
|
| 2400 |
else |
| 2401 |
return png_image_error(image, strerror(errno)); |
| 2402 |
} |
| 2403 |
|
| 2404 |
else |
| 2405 |
return png_image_error(image, |
| 2406 |
"png_image_write_to_file: invalid argument"); |
| 2407 |
} |
| 2408 |
|
| 2409 |
else if (image != NULL) |
| 2410 |
return png_image_error(image, |
| 2411 |
"png_image_write_to_file: incorrect PNG_IMAGE_VERSION"); |
| 2412 |
|
| 2413 |
else |
| 2414 |
return 0; |
| 2415 |
} |
| 2416 |
#endif |
| 2417 |
#endif |
| 2418 |
#endif |