media/libpng/pngwrite.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1
michael@0 2 /* pngwrite.c - general routines to write a PNG file
michael@0 3 *
michael@0 4 * Last changed in libpng 1.6.10 [March 6, 2014]
michael@0 5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
michael@0 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
michael@0 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
michael@0 8 *
michael@0 9 * This code is released under the libpng license.
michael@0 10 * For conditions of distribution and use, see the disclaimer
michael@0 11 * and license in png.h
michael@0 12 */
michael@0 13
michael@0 14 #include "pngpriv.h"
michael@0 15 #if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
michael@0 16 # include <errno.h>
michael@0 17 #endif
michael@0 18
michael@0 19 #ifdef PNG_WRITE_SUPPORTED
michael@0 20
michael@0 21 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
michael@0 22 /* Write out all the unknown chunks for the current given location */
michael@0 23 static void
michael@0 24 write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
michael@0 25 unsigned int where)
michael@0 26 {
michael@0 27 if (info_ptr->unknown_chunks_num)
michael@0 28 {
michael@0 29 png_const_unknown_chunkp up;
michael@0 30
michael@0 31 png_debug(5, "writing extra chunks");
michael@0 32
michael@0 33 for (up = info_ptr->unknown_chunks;
michael@0 34 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
michael@0 35 ++up)
michael@0 36 if (up->location & where)
michael@0 37 {
michael@0 38 /* If per-chunk unknown chunk handling is enabled use it, otherwise
michael@0 39 * just write the chunks the application has set.
michael@0 40 */
michael@0 41 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
michael@0 42 int keep = png_handle_as_unknown(png_ptr, up->name);
michael@0 43
michael@0 44 /* NOTE: this code is radically different from the read side in the
michael@0 45 * matter of handling an ancillary unknown chunk. In the read side
michael@0 46 * the default behavior is to discard it, in the code below the default
michael@0 47 * behavior is to write it. Critical chunks are, however, only
michael@0 48 * written if explicitly listed or if the default is set to write all
michael@0 49 * unknown chunks.
michael@0 50 *
michael@0 51 * The default handling is also slightly weird - it is not possible to
michael@0 52 * stop the writing of all unsafe-to-copy chunks!
michael@0 53 *
michael@0 54 * TODO: REVIEW: this would seem to be a bug.
michael@0 55 */
michael@0 56 if (keep != PNG_HANDLE_CHUNK_NEVER &&
michael@0 57 ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
michael@0 58 keep == PNG_HANDLE_CHUNK_ALWAYS ||
michael@0 59 (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
michael@0 60 png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
michael@0 61 #endif
michael@0 62 {
michael@0 63 /* TODO: review, what is wrong with a zero length unknown chunk? */
michael@0 64 if (up->size == 0)
michael@0 65 png_warning(png_ptr, "Writing zero-length unknown chunk");
michael@0 66
michael@0 67 png_write_chunk(png_ptr, up->name, up->data, up->size);
michael@0 68 }
michael@0 69 }
michael@0 70 }
michael@0 71 }
michael@0 72 #endif /* PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED */
michael@0 73
michael@0 74 /* Writes all the PNG information. This is the suggested way to use the
michael@0 75 * library. If you have a new chunk to add, make a function to write it,
michael@0 76 * and put it in the correct location here. If you want the chunk written
michael@0 77 * after the image data, put it in png_write_end(). I strongly encourage
michael@0 78 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
michael@0 79 * the chunk, as that will keep the code from breaking if you want to just
michael@0 80 * write a plain PNG file. If you have long comments, I suggest writing
michael@0 81 * them in png_write_end(), and compressing them.
michael@0 82 */
michael@0 83 void PNGAPI
michael@0 84 png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
michael@0 85 {
michael@0 86 png_debug(1, "in png_write_info_before_PLTE");
michael@0 87
michael@0 88 if (png_ptr == NULL || info_ptr == NULL)
michael@0 89 return;
michael@0 90
michael@0 91 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
michael@0 92 {
michael@0 93 /* Write PNG signature */
michael@0 94 png_write_sig(png_ptr);
michael@0 95
michael@0 96 #ifdef PNG_MNG_FEATURES_SUPPORTED
michael@0 97 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
michael@0 98 (png_ptr->mng_features_permitted))
michael@0 99 {
michael@0 100 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
michael@0 101 png_ptr->mng_features_permitted = 0;
michael@0 102 }
michael@0 103 #endif
michael@0 104
michael@0 105 /* Write IHDR information. */
michael@0 106 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
michael@0 107 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
michael@0 108 info_ptr->filter_type,
michael@0 109 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
michael@0 110 info_ptr->interlace_type
michael@0 111 #else
michael@0 112 0
michael@0 113 #endif
michael@0 114 );
michael@0 115
michael@0 116 /* The rest of these check to see if the valid field has the appropriate
michael@0 117 * flag set, and if it does, writes the chunk.
michael@0 118 *
michael@0 119 * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
michael@0 120 * the chunks will be written if the WRITE routine is there and information
michael@0 121 * is available in the COLORSPACE. (See png_colorspace_sync_info in png.c
michael@0 122 * for where the valid flags get set.)
michael@0 123 *
michael@0 124 * Under certain circumstances the colorspace can be invalidated without
michael@0 125 * syncing the info_struct 'valid' flags; this happens if libpng detects and
michael@0 126 * error and calls png_error while the color space is being set, yet the
michael@0 127 * application continues writing the PNG. So check the 'invalid' flag here
michael@0 128 * too.
michael@0 129 */
michael@0 130 #ifdef PNG_WRITE_APNG_SUPPORTED
michael@0 131 if (info_ptr->valid & PNG_INFO_acTL)
michael@0 132 png_write_acTL(png_ptr, info_ptr->num_frames, info_ptr->num_plays);
michael@0 133 #endif
michael@0 134 #ifdef PNG_GAMMA_SUPPORTED
michael@0 135 # ifdef PNG_WRITE_gAMA_SUPPORTED
michael@0 136 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
michael@0 137 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) &&
michael@0 138 (info_ptr->valid & PNG_INFO_gAMA))
michael@0 139 png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
michael@0 140 # endif
michael@0 141 #endif
michael@0 142
michael@0 143 #ifdef PNG_COLORSPACE_SUPPORTED
michael@0 144 /* Write only one of sRGB or an ICC profile. If a profile was supplied
michael@0 145 * and it matches one of the known sRGB ones issue a warning.
michael@0 146 */
michael@0 147 # ifdef PNG_WRITE_iCCP_SUPPORTED
michael@0 148 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
michael@0 149 (info_ptr->valid & PNG_INFO_iCCP))
michael@0 150 {
michael@0 151 # ifdef PNG_WRITE_sRGB_SUPPORTED
michael@0 152 if (info_ptr->valid & PNG_INFO_sRGB)
michael@0 153 png_app_warning(png_ptr,
michael@0 154 "profile matches sRGB but writing iCCP instead");
michael@0 155 # endif
michael@0 156
michael@0 157 png_write_iCCP(png_ptr, info_ptr->iccp_name,
michael@0 158 info_ptr->iccp_profile);
michael@0 159 }
michael@0 160 # ifdef PNG_WRITE_sRGB_SUPPORTED
michael@0 161 else
michael@0 162 # endif
michael@0 163 # endif
michael@0 164
michael@0 165 # ifdef PNG_WRITE_sRGB_SUPPORTED
michael@0 166 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
michael@0 167 (info_ptr->valid & PNG_INFO_sRGB))
michael@0 168 png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
michael@0 169 # endif /* WRITE_sRGB */
michael@0 170 #endif /* COLORSPACE */
michael@0 171
michael@0 172 #ifdef PNG_WRITE_sBIT_SUPPORTED
michael@0 173 if (info_ptr->valid & PNG_INFO_sBIT)
michael@0 174 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
michael@0 175 #endif
michael@0 176
michael@0 177 #ifdef PNG_COLORSPACE_SUPPORTED
michael@0 178 # ifdef PNG_WRITE_cHRM_SUPPORTED
michael@0 179 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
michael@0 180 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) &&
michael@0 181 (info_ptr->valid & PNG_INFO_cHRM))
michael@0 182 png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
michael@0 183 # endif
michael@0 184 #endif
michael@0 185
michael@0 186 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
michael@0 187 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
michael@0 188 #endif
michael@0 189
michael@0 190 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 void PNGAPI
michael@0 195 png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
michael@0 196 {
michael@0 197 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
michael@0 198 int i;
michael@0 199 #endif
michael@0 200
michael@0 201 png_debug(1, "in png_write_info");
michael@0 202
michael@0 203 if (png_ptr == NULL || info_ptr == NULL)
michael@0 204 return;
michael@0 205
michael@0 206 png_write_info_before_PLTE(png_ptr, info_ptr);
michael@0 207
michael@0 208 if (info_ptr->valid & PNG_INFO_PLTE)
michael@0 209 png_write_PLTE(png_ptr, info_ptr->palette,
michael@0 210 (png_uint_32)info_ptr->num_palette);
michael@0 211
michael@0 212 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
michael@0 213 png_error(png_ptr, "Valid palette required for paletted images");
michael@0 214
michael@0 215 #ifdef PNG_WRITE_tRNS_SUPPORTED
michael@0 216 if (info_ptr->valid & PNG_INFO_tRNS)
michael@0 217 {
michael@0 218 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
michael@0 219 /* Invert the alpha channel (in tRNS) */
michael@0 220 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
michael@0 221 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
michael@0 222 {
michael@0 223 int j;
michael@0 224 for (j = 0; j<(int)info_ptr->num_trans; j++)
michael@0 225 info_ptr->trans_alpha[j] =
michael@0 226 (png_byte)(255 - info_ptr->trans_alpha[j]);
michael@0 227 }
michael@0 228 #endif
michael@0 229 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
michael@0 230 info_ptr->num_trans, info_ptr->color_type);
michael@0 231 }
michael@0 232 #endif
michael@0 233 #ifdef PNG_WRITE_bKGD_SUPPORTED
michael@0 234 if (info_ptr->valid & PNG_INFO_bKGD)
michael@0 235 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
michael@0 236 #endif
michael@0 237
michael@0 238 #ifdef PNG_WRITE_hIST_SUPPORTED
michael@0 239 if (info_ptr->valid & PNG_INFO_hIST)
michael@0 240 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
michael@0 241 #endif
michael@0 242
michael@0 243 #ifdef PNG_WRITE_oFFs_SUPPORTED
michael@0 244 if (info_ptr->valid & PNG_INFO_oFFs)
michael@0 245 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
michael@0 246 info_ptr->offset_unit_type);
michael@0 247 #endif
michael@0 248
michael@0 249 #ifdef PNG_WRITE_pCAL_SUPPORTED
michael@0 250 if (info_ptr->valid & PNG_INFO_pCAL)
michael@0 251 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
michael@0 252 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
michael@0 253 info_ptr->pcal_units, info_ptr->pcal_params);
michael@0 254 #endif
michael@0 255
michael@0 256 #ifdef PNG_WRITE_sCAL_SUPPORTED
michael@0 257 if (info_ptr->valid & PNG_INFO_sCAL)
michael@0 258 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
michael@0 259 info_ptr->scal_s_width, info_ptr->scal_s_height);
michael@0 260 #endif /* sCAL */
michael@0 261
michael@0 262 #ifdef PNG_WRITE_pHYs_SUPPORTED
michael@0 263 if (info_ptr->valid & PNG_INFO_pHYs)
michael@0 264 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
michael@0 265 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
michael@0 266 #endif /* pHYs */
michael@0 267
michael@0 268 #ifdef PNG_WRITE_tIME_SUPPORTED
michael@0 269 if (info_ptr->valid & PNG_INFO_tIME)
michael@0 270 {
michael@0 271 png_write_tIME(png_ptr, &(info_ptr->mod_time));
michael@0 272 png_ptr->mode |= PNG_WROTE_tIME;
michael@0 273 }
michael@0 274 #endif /* tIME */
michael@0 275
michael@0 276 #ifdef PNG_WRITE_sPLT_SUPPORTED
michael@0 277 if (info_ptr->valid & PNG_INFO_sPLT)
michael@0 278 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
michael@0 279 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
michael@0 280 #endif /* sPLT */
michael@0 281
michael@0 282 #ifdef PNG_WRITE_TEXT_SUPPORTED
michael@0 283 /* Check to see if we need to write text chunks */
michael@0 284 for (i = 0; i < info_ptr->num_text; i++)
michael@0 285 {
michael@0 286 png_debug2(2, "Writing header text chunk %d, type %d", i,
michael@0 287 info_ptr->text[i].compression);
michael@0 288 /* An internationalized chunk? */
michael@0 289 if (info_ptr->text[i].compression > 0)
michael@0 290 {
michael@0 291 #ifdef PNG_WRITE_iTXt_SUPPORTED
michael@0 292 /* Write international chunk */
michael@0 293 png_write_iTXt(png_ptr,
michael@0 294 info_ptr->text[i].compression,
michael@0 295 info_ptr->text[i].key,
michael@0 296 info_ptr->text[i].lang,
michael@0 297 info_ptr->text[i].lang_key,
michael@0 298 info_ptr->text[i].text);
michael@0 299 #else
michael@0 300 png_warning(png_ptr, "Unable to write international text");
michael@0 301 #endif
michael@0 302 /* Mark this chunk as written */
michael@0 303 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
michael@0 304 }
michael@0 305
michael@0 306 /* If we want a compressed text chunk */
michael@0 307 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
michael@0 308 {
michael@0 309 #ifdef PNG_WRITE_zTXt_SUPPORTED
michael@0 310 /* Write compressed chunk */
michael@0 311 png_write_zTXt(png_ptr, info_ptr->text[i].key,
michael@0 312 info_ptr->text[i].text, 0,
michael@0 313 info_ptr->text[i].compression);
michael@0 314 #else
michael@0 315 png_warning(png_ptr, "Unable to write compressed text");
michael@0 316 #endif
michael@0 317 /* Mark this chunk as written */
michael@0 318 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
michael@0 319 }
michael@0 320
michael@0 321 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
michael@0 322 {
michael@0 323 #ifdef PNG_WRITE_tEXt_SUPPORTED
michael@0 324 /* Write uncompressed chunk */
michael@0 325 png_write_tEXt(png_ptr, info_ptr->text[i].key,
michael@0 326 info_ptr->text[i].text,
michael@0 327 0);
michael@0 328 /* Mark this chunk as written */
michael@0 329 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
michael@0 330 #else
michael@0 331 /* Can't get here */
michael@0 332 png_warning(png_ptr, "Unable to write uncompressed text");
michael@0 333 #endif
michael@0 334 }
michael@0 335 }
michael@0 336 #endif /* tEXt */
michael@0 337
michael@0 338 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
michael@0 339 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
michael@0 340 #endif
michael@0 341 }
michael@0 342
michael@0 343 /* Writes the end of the PNG file. If you don't want to write comments or
michael@0 344 * time information, you can pass NULL for info. If you already wrote these
michael@0 345 * in png_write_info(), do not write them again here. If you have long
michael@0 346 * comments, I suggest writing them here, and compressing them.
michael@0 347 */
michael@0 348 void PNGAPI
michael@0 349 png_write_end(png_structrp png_ptr, png_inforp info_ptr)
michael@0 350 {
michael@0 351 png_debug(1, "in png_write_end");
michael@0 352
michael@0 353 if (png_ptr == NULL)
michael@0 354 return;
michael@0 355
michael@0 356 if (!(png_ptr->mode & PNG_HAVE_IDAT))
michael@0 357 png_error(png_ptr, "No IDATs written into file");
michael@0 358
michael@0 359 #ifdef PNG_WRITE_APNG_SUPPORTED
michael@0 360 if (png_ptr->num_frames_written != png_ptr->num_frames_to_write)
michael@0 361 png_error(png_ptr, "Not enough frames written");
michael@0 362 #endif
michael@0 363
michael@0 364 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
michael@0 365 if (png_ptr->num_palette_max > png_ptr->num_palette)
michael@0 366 png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
michael@0 367 #endif
michael@0 368
michael@0 369 /* See if user wants us to write information chunks */
michael@0 370 if (info_ptr != NULL)
michael@0 371 {
michael@0 372 #ifdef PNG_WRITE_TEXT_SUPPORTED
michael@0 373 int i; /* local index variable */
michael@0 374 #endif
michael@0 375 #ifdef PNG_WRITE_tIME_SUPPORTED
michael@0 376 /* Check to see if user has supplied a time chunk */
michael@0 377 if ((info_ptr->valid & PNG_INFO_tIME) &&
michael@0 378 !(png_ptr->mode & PNG_WROTE_tIME))
michael@0 379 png_write_tIME(png_ptr, &(info_ptr->mod_time));
michael@0 380
michael@0 381 #endif
michael@0 382 #ifdef PNG_WRITE_TEXT_SUPPORTED
michael@0 383 /* Loop through comment chunks */
michael@0 384 for (i = 0; i < info_ptr->num_text; i++)
michael@0 385 {
michael@0 386 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
michael@0 387 info_ptr->text[i].compression);
michael@0 388 /* An internationalized chunk? */
michael@0 389 if (info_ptr->text[i].compression > 0)
michael@0 390 {
michael@0 391 #ifdef PNG_WRITE_iTXt_SUPPORTED
michael@0 392 /* Write international chunk */
michael@0 393 png_write_iTXt(png_ptr,
michael@0 394 info_ptr->text[i].compression,
michael@0 395 info_ptr->text[i].key,
michael@0 396 info_ptr->text[i].lang,
michael@0 397 info_ptr->text[i].lang_key,
michael@0 398 info_ptr->text[i].text);
michael@0 399 #else
michael@0 400 png_warning(png_ptr, "Unable to write international text");
michael@0 401 #endif
michael@0 402 /* Mark this chunk as written */
michael@0 403 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
michael@0 404 }
michael@0 405
michael@0 406 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
michael@0 407 {
michael@0 408 #ifdef PNG_WRITE_zTXt_SUPPORTED
michael@0 409 /* Write compressed chunk */
michael@0 410 png_write_zTXt(png_ptr, info_ptr->text[i].key,
michael@0 411 info_ptr->text[i].text, 0,
michael@0 412 info_ptr->text[i].compression);
michael@0 413 #else
michael@0 414 png_warning(png_ptr, "Unable to write compressed text");
michael@0 415 #endif
michael@0 416 /* Mark this chunk as written */
michael@0 417 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
michael@0 418 }
michael@0 419
michael@0 420 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
michael@0 421 {
michael@0 422 #ifdef PNG_WRITE_tEXt_SUPPORTED
michael@0 423 /* Write uncompressed chunk */
michael@0 424 png_write_tEXt(png_ptr, info_ptr->text[i].key,
michael@0 425 info_ptr->text[i].text, 0);
michael@0 426 #else
michael@0 427 png_warning(png_ptr, "Unable to write uncompressed text");
michael@0 428 #endif
michael@0 429
michael@0 430 /* Mark this chunk as written */
michael@0 431 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
michael@0 432 }
michael@0 433 }
michael@0 434 #endif
michael@0 435 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
michael@0 436 write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
michael@0 437 #endif
michael@0 438 }
michael@0 439
michael@0 440 png_ptr->mode |= PNG_AFTER_IDAT;
michael@0 441
michael@0 442 /* Write end of PNG file */
michael@0 443 png_write_IEND(png_ptr);
michael@0 444 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
michael@0 445 * and restored again in libpng-1.2.30, may cause some applications that
michael@0 446 * do not set png_ptr->output_flush_fn to crash. If your application
michael@0 447 * experiences a problem, please try building libpng with
michael@0 448 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
michael@0 449 * png-mng-implement at lists.sf.net .
michael@0 450 */
michael@0 451 #ifdef PNG_WRITE_FLUSH_SUPPORTED
michael@0 452 # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
michael@0 453 png_flush(png_ptr);
michael@0 454 # endif
michael@0 455 #endif
michael@0 456 }
michael@0 457
michael@0 458 #ifdef PNG_CONVERT_tIME_SUPPORTED
michael@0 459 void PNGAPI
michael@0 460 png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
michael@0 461 {
michael@0 462 png_debug(1, "in png_convert_from_struct_tm");
michael@0 463
michael@0 464 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
michael@0 465 ptime->month = (png_byte)(ttime->tm_mon + 1);
michael@0 466 ptime->day = (png_byte)ttime->tm_mday;
michael@0 467 ptime->hour = (png_byte)ttime->tm_hour;
michael@0 468 ptime->minute = (png_byte)ttime->tm_min;
michael@0 469 ptime->second = (png_byte)ttime->tm_sec;
michael@0 470 }
michael@0 471
michael@0 472 void PNGAPI
michael@0 473 png_convert_from_time_t(png_timep ptime, time_t ttime)
michael@0 474 {
michael@0 475 struct tm *tbuf;
michael@0 476
michael@0 477 png_debug(1, "in png_convert_from_time_t");
michael@0 478
michael@0 479 tbuf = gmtime(&ttime);
michael@0 480 png_convert_from_struct_tm(ptime, tbuf);
michael@0 481 }
michael@0 482 #endif
michael@0 483
michael@0 484 /* Initialize png_ptr structure, and allocate any memory needed */
michael@0 485 PNG_FUNCTION(png_structp,PNGAPI
michael@0 486 png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
michael@0 487 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
michael@0 488 {
michael@0 489 #ifndef PNG_USER_MEM_SUPPORTED
michael@0 490 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
michael@0 491 error_fn, warn_fn, NULL, NULL, NULL);
michael@0 492 #else
michael@0 493 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
michael@0 494 warn_fn, NULL, NULL, NULL);
michael@0 495 }
michael@0 496
michael@0 497 /* Alternate initialize png_ptr structure, and allocate any memory needed */
michael@0 498 PNG_FUNCTION(png_structp,PNGAPI
michael@0 499 png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
michael@0 500 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
michael@0 501 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
michael@0 502 {
michael@0 503 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
michael@0 504 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
michael@0 505 #endif /* PNG_USER_MEM_SUPPORTED */
michael@0 506 if (png_ptr != NULL)
michael@0 507 {
michael@0 508 /* Set the zlib control values to defaults; they can be overridden by the
michael@0 509 * application after the struct has been created.
michael@0 510 */
michael@0 511 png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
michael@0 512
michael@0 513 /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
michael@0 514 * pngwutil.c defaults it according to whether or not filters will be
michael@0 515 * used, and ignores this setting.
michael@0 516 */
michael@0 517 png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
michael@0 518 png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
michael@0 519 png_ptr->zlib_mem_level = 8;
michael@0 520 png_ptr->zlib_window_bits = 15;
michael@0 521 png_ptr->zlib_method = 8;
michael@0 522
michael@0 523 #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
michael@0 524 png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
michael@0 525 png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
michael@0 526 png_ptr->zlib_text_mem_level = 8;
michael@0 527 png_ptr->zlib_text_window_bits = 15;
michael@0 528 png_ptr->zlib_text_method = 8;
michael@0 529 #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
michael@0 530
michael@0 531 /* This is a highly dubious configuration option; by default it is off,
michael@0 532 * but it may be appropriate for private builds that are testing
michael@0 533 * extensions not conformant to the current specification, or of
michael@0 534 * applications that must not fail to write at all costs!
michael@0 535 */
michael@0 536 #ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
michael@0 537 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
michael@0 538 /* In stable builds only warn if an application error can be completely
michael@0 539 * handled.
michael@0 540 */
michael@0 541 #endif
michael@0 542
michael@0 543 /* App warnings are warnings in release (or release candidate) builds but
michael@0 544 * are errors during development.
michael@0 545 */
michael@0 546 #if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
michael@0 547 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
michael@0 548 #endif
michael@0 549
michael@0 550 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
michael@0 551 * do it itself) avoiding setting the default function if it is not
michael@0 552 * required.
michael@0 553 */
michael@0 554 png_set_write_fn(png_ptr, NULL, NULL, NULL);
michael@0 555 }
michael@0 556
michael@0 557 return png_ptr;
michael@0 558 }
michael@0 559
michael@0 560
michael@0 561 /* Write a few rows of image data. If the image is interlaced,
michael@0 562 * either you will have to write the 7 sub images, or, if you
michael@0 563 * have called png_set_interlace_handling(), you will have to
michael@0 564 * "write" the image seven times.
michael@0 565 */
michael@0 566 void PNGAPI
michael@0 567 png_write_rows(png_structrp png_ptr, png_bytepp row,
michael@0 568 png_uint_32 num_rows)
michael@0 569 {
michael@0 570 png_uint_32 i; /* row counter */
michael@0 571 png_bytepp rp; /* row pointer */
michael@0 572
michael@0 573 png_debug(1, "in png_write_rows");
michael@0 574
michael@0 575 if (png_ptr == NULL)
michael@0 576 return;
michael@0 577
michael@0 578 /* Loop through the rows */
michael@0 579 for (i = 0, rp = row; i < num_rows; i++, rp++)
michael@0 580 {
michael@0 581 png_write_row(png_ptr, *rp);
michael@0 582 }
michael@0 583 }
michael@0 584
michael@0 585 /* Write the image. You only need to call this function once, even
michael@0 586 * if you are writing an interlaced image.
michael@0 587 */
michael@0 588 void PNGAPI
michael@0 589 png_write_image(png_structrp png_ptr, png_bytepp image)
michael@0 590 {
michael@0 591 png_uint_32 i; /* row index */
michael@0 592 int pass, num_pass; /* pass variables */
michael@0 593 png_bytepp rp; /* points to current row */
michael@0 594
michael@0 595 if (png_ptr == NULL)
michael@0 596 return;
michael@0 597
michael@0 598 png_debug(1, "in png_write_image");
michael@0 599
michael@0 600 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
michael@0 601 /* Initialize interlace handling. If image is not interlaced,
michael@0 602 * this will set pass to 1
michael@0 603 */
michael@0 604 num_pass = png_set_interlace_handling(png_ptr);
michael@0 605 #else
michael@0 606 num_pass = 1;
michael@0 607 #endif
michael@0 608 /* Loop through passes */
michael@0 609 for (pass = 0; pass < num_pass; pass++)
michael@0 610 {
michael@0 611 /* Loop through image */
michael@0 612 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
michael@0 613 {
michael@0 614 png_write_row(png_ptr, *rp);
michael@0 615 }
michael@0 616 }
michael@0 617 }
michael@0 618
michael@0 619 #ifdef PNG_MNG_FEATURES_SUPPORTED
michael@0 620 /* Performs intrapixel differencing */
michael@0 621 static void
michael@0 622 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
michael@0 623 {
michael@0 624 png_debug(1, "in png_do_write_intrapixel");
michael@0 625
michael@0 626 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
michael@0 627 {
michael@0 628 int bytes_per_pixel;
michael@0 629 png_uint_32 row_width = row_info->width;
michael@0 630 if (row_info->bit_depth == 8)
michael@0 631 {
michael@0 632 png_bytep rp;
michael@0 633 png_uint_32 i;
michael@0 634
michael@0 635 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
michael@0 636 bytes_per_pixel = 3;
michael@0 637
michael@0 638 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
michael@0 639 bytes_per_pixel = 4;
michael@0 640
michael@0 641 else
michael@0 642 return;
michael@0 643
michael@0 644 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
michael@0 645 {
michael@0 646 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
michael@0 647 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
michael@0 648 }
michael@0 649 }
michael@0 650
michael@0 651 #ifdef PNG_WRITE_16BIT_SUPPORTED
michael@0 652 else if (row_info->bit_depth == 16)
michael@0 653 {
michael@0 654 png_bytep rp;
michael@0 655 png_uint_32 i;
michael@0 656
michael@0 657 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
michael@0 658 bytes_per_pixel = 6;
michael@0 659
michael@0 660 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
michael@0 661 bytes_per_pixel = 8;
michael@0 662
michael@0 663 else
michael@0 664 return;
michael@0 665
michael@0 666 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
michael@0 667 {
michael@0 668 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
michael@0 669 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
michael@0 670 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
michael@0 671 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
michael@0 672 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
michael@0 673 *(rp ) = (png_byte)((red >> 8) & 0xff);
michael@0 674 *(rp + 1) = (png_byte)(red & 0xff);
michael@0 675 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
michael@0 676 *(rp + 5) = (png_byte)(blue & 0xff);
michael@0 677 }
michael@0 678 }
michael@0 679 #endif /* PNG_WRITE_16BIT_SUPPORTED */
michael@0 680 }
michael@0 681 }
michael@0 682 #endif /* PNG_MNG_FEATURES_SUPPORTED */
michael@0 683
michael@0 684 /* Called by user to write a row of image data */
michael@0 685 void PNGAPI
michael@0 686 png_write_row(png_structrp png_ptr, png_const_bytep row)
michael@0 687 {
michael@0 688 /* 1.5.6: moved from png_struct to be a local structure: */
michael@0 689 png_row_info row_info;
michael@0 690
michael@0 691 if (png_ptr == NULL)
michael@0 692 return;
michael@0 693
michael@0 694 png_debug2(1, "in png_write_row (row %u, pass %d)",
michael@0 695 png_ptr->row_number, png_ptr->pass);
michael@0 696
michael@0 697 /* Initialize transformations and other stuff if first time */
michael@0 698 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
michael@0 699 {
michael@0 700 /* Make sure we wrote the header info */
michael@0 701 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
michael@0 702 png_error(png_ptr,
michael@0 703 "png_write_info was never called before png_write_row");
michael@0 704
michael@0 705 /* Check for transforms that have been set but were defined out */
michael@0 706 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
michael@0 707 if (png_ptr->transformations & PNG_INVERT_MONO)
michael@0 708 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
michael@0 709 #endif
michael@0 710
michael@0 711 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
michael@0 712 if (png_ptr->transformations & PNG_FILLER)
michael@0 713 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
michael@0 714 #endif
michael@0 715 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
michael@0 716 defined(PNG_READ_PACKSWAP_SUPPORTED)
michael@0 717 if (png_ptr->transformations & PNG_PACKSWAP)
michael@0 718 png_warning(png_ptr,
michael@0 719 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
michael@0 720 #endif
michael@0 721
michael@0 722 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
michael@0 723 if (png_ptr->transformations & PNG_PACK)
michael@0 724 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
michael@0 725 #endif
michael@0 726
michael@0 727 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
michael@0 728 if (png_ptr->transformations & PNG_SHIFT)
michael@0 729 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
michael@0 730 #endif
michael@0 731
michael@0 732 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
michael@0 733 if (png_ptr->transformations & PNG_BGR)
michael@0 734 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
michael@0 735 #endif
michael@0 736
michael@0 737 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
michael@0 738 if (png_ptr->transformations & PNG_SWAP_BYTES)
michael@0 739 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
michael@0 740 #endif
michael@0 741
michael@0 742 png_write_start_row(png_ptr);
michael@0 743 }
michael@0 744
michael@0 745 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
michael@0 746 /* If interlaced and not interested in row, return */
michael@0 747 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
michael@0 748 {
michael@0 749 switch (png_ptr->pass)
michael@0 750 {
michael@0 751 case 0:
michael@0 752 if (png_ptr->row_number & 0x07)
michael@0 753 {
michael@0 754 png_write_finish_row(png_ptr);
michael@0 755 return;
michael@0 756 }
michael@0 757 break;
michael@0 758
michael@0 759 case 1:
michael@0 760 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
michael@0 761 {
michael@0 762 png_write_finish_row(png_ptr);
michael@0 763 return;
michael@0 764 }
michael@0 765 break;
michael@0 766
michael@0 767 case 2:
michael@0 768 if ((png_ptr->row_number & 0x07) != 4)
michael@0 769 {
michael@0 770 png_write_finish_row(png_ptr);
michael@0 771 return;
michael@0 772 }
michael@0 773 break;
michael@0 774
michael@0 775 case 3:
michael@0 776 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
michael@0 777 {
michael@0 778 png_write_finish_row(png_ptr);
michael@0 779 return;
michael@0 780 }
michael@0 781 break;
michael@0 782
michael@0 783 case 4:
michael@0 784 if ((png_ptr->row_number & 0x03) != 2)
michael@0 785 {
michael@0 786 png_write_finish_row(png_ptr);
michael@0 787 return;
michael@0 788 }
michael@0 789 break;
michael@0 790
michael@0 791 case 5:
michael@0 792 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
michael@0 793 {
michael@0 794 png_write_finish_row(png_ptr);
michael@0 795 return;
michael@0 796 }
michael@0 797 break;
michael@0 798
michael@0 799 case 6:
michael@0 800 if (!(png_ptr->row_number & 0x01))
michael@0 801 {
michael@0 802 png_write_finish_row(png_ptr);
michael@0 803 return;
michael@0 804 }
michael@0 805 break;
michael@0 806
michael@0 807 default: /* error: ignore it */
michael@0 808 break;
michael@0 809 }
michael@0 810 }
michael@0 811 #endif
michael@0 812
michael@0 813 /* Set up row info for transformations */
michael@0 814 row_info.color_type = png_ptr->color_type;
michael@0 815 row_info.width = png_ptr->usr_width;
michael@0 816 row_info.channels = png_ptr->usr_channels;
michael@0 817 row_info.bit_depth = png_ptr->usr_bit_depth;
michael@0 818 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
michael@0 819 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
michael@0 820
michael@0 821 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
michael@0 822 png_debug1(3, "row_info->width = %u", row_info.width);
michael@0 823 png_debug1(3, "row_info->channels = %d", row_info.channels);
michael@0 824 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
michael@0 825 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
michael@0 826 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
michael@0 827
michael@0 828 /* Copy user's row into buffer, leaving room for filter byte. */
michael@0 829 memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
michael@0 830
michael@0 831 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
michael@0 832 /* Handle interlacing */
michael@0 833 if (png_ptr->interlaced && png_ptr->pass < 6 &&
michael@0 834 (png_ptr->transformations & PNG_INTERLACE))
michael@0 835 {
michael@0 836 png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
michael@0 837 /* This should always get caught above, but still ... */
michael@0 838 if (!(row_info.width))
michael@0 839 {
michael@0 840 png_write_finish_row(png_ptr);
michael@0 841 return;
michael@0 842 }
michael@0 843 }
michael@0 844 #endif
michael@0 845
michael@0 846 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
michael@0 847 /* Handle other transformations */
michael@0 848 if (png_ptr->transformations)
michael@0 849 png_do_write_transformations(png_ptr, &row_info);
michael@0 850 #endif
michael@0 851
michael@0 852 /* At this point the row_info pixel depth must match the 'transformed' depth,
michael@0 853 * which is also the output depth.
michael@0 854 */
michael@0 855 if (row_info.pixel_depth != png_ptr->pixel_depth ||
michael@0 856 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
michael@0 857 png_error(png_ptr, "internal write transform logic error");
michael@0 858
michael@0 859 #ifdef PNG_MNG_FEATURES_SUPPORTED
michael@0 860 /* Write filter_method 64 (intrapixel differencing) only if
michael@0 861 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
michael@0 862 * 2. Libpng did not write a PNG signature (this filter_method is only
michael@0 863 * used in PNG datastreams that are embedded in MNG datastreams) and
michael@0 864 * 3. The application called png_permit_mng_features with a mask that
michael@0 865 * included PNG_FLAG_MNG_FILTER_64 and
michael@0 866 * 4. The filter_method is 64 and
michael@0 867 * 5. The color_type is RGB or RGBA
michael@0 868 */
michael@0 869 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
michael@0 870 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
michael@0 871 {
michael@0 872 /* Intrapixel differencing */
michael@0 873 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
michael@0 874 }
michael@0 875 #endif
michael@0 876
michael@0 877 /* Added at libpng-1.5.10 */
michael@0 878 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
michael@0 879 /* Check for out-of-range palette index */
michael@0 880 if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
michael@0 881 png_ptr->num_palette_max >= 0)
michael@0 882 png_do_check_palette_indexes(png_ptr, &row_info);
michael@0 883 #endif
michael@0 884
michael@0 885 /* Find a filter if necessary, filter the row and write it out. */
michael@0 886 png_write_find_filter(png_ptr, &row_info);
michael@0 887
michael@0 888 if (png_ptr->write_row_fn != NULL)
michael@0 889 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
michael@0 890 }
michael@0 891
michael@0 892 #ifdef PNG_WRITE_FLUSH_SUPPORTED
michael@0 893 /* Set the automatic flush interval or 0 to turn flushing off */
michael@0 894 void PNGAPI
michael@0 895 png_set_flush(png_structrp png_ptr, int nrows)
michael@0 896 {
michael@0 897 png_debug(1, "in png_set_flush");
michael@0 898
michael@0 899 if (png_ptr == NULL)
michael@0 900 return;
michael@0 901
michael@0 902 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
michael@0 903 }
michael@0 904
michael@0 905 /* Flush the current output buffers now */
michael@0 906 void PNGAPI
michael@0 907 png_write_flush(png_structrp png_ptr)
michael@0 908 {
michael@0 909 png_debug(1, "in png_write_flush");
michael@0 910
michael@0 911 if (png_ptr == NULL)
michael@0 912 return;
michael@0 913
michael@0 914 /* We have already written out all of the data */
michael@0 915 if (png_ptr->row_number >= png_ptr->num_rows)
michael@0 916 return;
michael@0 917
michael@0 918 png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
michael@0 919 png_ptr->flush_rows = 0;
michael@0 920 png_flush(png_ptr);
michael@0 921 }
michael@0 922 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
michael@0 923
michael@0 924 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
michael@0 925 static void png_reset_filter_heuristics(png_structrp png_ptr);/* forward decl */
michael@0 926 #endif
michael@0 927
michael@0 928 /* Free any memory used in png_ptr struct without freeing the struct itself. */
michael@0 929 static void
michael@0 930 png_write_destroy(png_structrp png_ptr)
michael@0 931 {
michael@0 932 png_debug(1, "in png_write_destroy");
michael@0 933
michael@0 934 /* Free any memory zlib uses */
michael@0 935 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
michael@0 936 deflateEnd(&png_ptr->zstream);
michael@0 937
michael@0 938 /* Free our memory. png_free checks NULL for us. */
michael@0 939 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
michael@0 940 png_free(png_ptr, png_ptr->row_buf);
michael@0 941 #ifdef PNG_WRITE_FILTER_SUPPORTED
michael@0 942 png_free(png_ptr, png_ptr->prev_row);
michael@0 943 png_free(png_ptr, png_ptr->sub_row);
michael@0 944 png_free(png_ptr, png_ptr->up_row);
michael@0 945 png_free(png_ptr, png_ptr->avg_row);
michael@0 946 png_free(png_ptr, png_ptr->paeth_row);
michael@0 947 #endif
michael@0 948
michael@0 949 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
michael@0 950 /* Use this to save a little code space, it doesn't free the filter_costs */
michael@0 951 png_reset_filter_heuristics(png_ptr);
michael@0 952 png_free(png_ptr, png_ptr->filter_costs);
michael@0 953 png_free(png_ptr, png_ptr->inv_filter_costs);
michael@0 954 #endif
michael@0 955
michael@0 956 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
michael@0 957 png_free(png_ptr, png_ptr->chunk_list);
michael@0 958 #endif
michael@0 959
michael@0 960 /* The error handling and memory handling information is left intact at this
michael@0 961 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
michael@0 962 * for how this happens.
michael@0 963 */
michael@0 964 }
michael@0 965
michael@0 966 /* Free all memory used by the write.
michael@0 967 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
michael@0 968 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
michael@0 969 * the passed in info_structs but it would quietly fail to free any of the data
michael@0 970 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
michael@0 971 * has no png_ptr.)
michael@0 972 */
michael@0 973 void PNGAPI
michael@0 974 png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
michael@0 975 {
michael@0 976 png_debug(1, "in png_destroy_write_struct");
michael@0 977
michael@0 978 if (png_ptr_ptr != NULL)
michael@0 979 {
michael@0 980 png_structrp png_ptr = *png_ptr_ptr;
michael@0 981
michael@0 982 if (png_ptr != NULL) /* added in libpng 1.6.0 */
michael@0 983 {
michael@0 984 png_destroy_info_struct(png_ptr, info_ptr_ptr);
michael@0 985
michael@0 986 *png_ptr_ptr = NULL;
michael@0 987 png_write_destroy(png_ptr);
michael@0 988 png_destroy_png_struct(png_ptr);
michael@0 989 }
michael@0 990 }
michael@0 991 }
michael@0 992
michael@0 993 /* Allow the application to select one or more row filters to use. */
michael@0 994 void PNGAPI
michael@0 995 png_set_filter(png_structrp png_ptr, int method, int filters)
michael@0 996 {
michael@0 997 png_debug(1, "in png_set_filter");
michael@0 998
michael@0 999 if (png_ptr == NULL)
michael@0 1000 return;
michael@0 1001
michael@0 1002 #ifdef PNG_MNG_FEATURES_SUPPORTED
michael@0 1003 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
michael@0 1004 (method == PNG_INTRAPIXEL_DIFFERENCING))
michael@0 1005 method = PNG_FILTER_TYPE_BASE;
michael@0 1006
michael@0 1007 #endif
michael@0 1008 if (method == PNG_FILTER_TYPE_BASE)
michael@0 1009 {
michael@0 1010 switch (filters & (PNG_ALL_FILTERS | 0x07))
michael@0 1011 {
michael@0 1012 #ifdef PNG_WRITE_FILTER_SUPPORTED
michael@0 1013 case 5:
michael@0 1014 case 6:
michael@0 1015 case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
michael@0 1016 /* FALL THROUGH */
michael@0 1017 #endif /* PNG_WRITE_FILTER_SUPPORTED */
michael@0 1018 case PNG_FILTER_VALUE_NONE:
michael@0 1019 png_ptr->do_filter = PNG_FILTER_NONE; break;
michael@0 1020
michael@0 1021 #ifdef PNG_WRITE_FILTER_SUPPORTED
michael@0 1022 case PNG_FILTER_VALUE_SUB:
michael@0 1023 png_ptr->do_filter = PNG_FILTER_SUB; break;
michael@0 1024
michael@0 1025 case PNG_FILTER_VALUE_UP:
michael@0 1026 png_ptr->do_filter = PNG_FILTER_UP; break;
michael@0 1027
michael@0 1028 case PNG_FILTER_VALUE_AVG:
michael@0 1029 png_ptr->do_filter = PNG_FILTER_AVG; break;
michael@0 1030
michael@0 1031 case PNG_FILTER_VALUE_PAETH:
michael@0 1032 png_ptr->do_filter = PNG_FILTER_PAETH; break;
michael@0 1033
michael@0 1034 default:
michael@0 1035 png_ptr->do_filter = (png_byte)filters; break;
michael@0 1036 #else
michael@0 1037 default:
michael@0 1038 png_app_error(png_ptr, "Unknown row filter for method 0");
michael@0 1039 #endif /* PNG_WRITE_FILTER_SUPPORTED */
michael@0 1040 }
michael@0 1041
michael@0 1042 /* If we have allocated the row_buf, this means we have already started
michael@0 1043 * with the image and we should have allocated all of the filter buffers
michael@0 1044 * that have been selected. If prev_row isn't already allocated, then
michael@0 1045 * it is too late to start using the filters that need it, since we
michael@0 1046 * will be missing the data in the previous row. If an application
michael@0 1047 * wants to start and stop using particular filters during compression,
michael@0 1048 * it should start out with all of the filters, and then add and
michael@0 1049 * remove them after the start of compression.
michael@0 1050 */
michael@0 1051 if (png_ptr->row_buf != NULL)
michael@0 1052 {
michael@0 1053 #ifdef PNG_WRITE_FILTER_SUPPORTED
michael@0 1054 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
michael@0 1055 {
michael@0 1056 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
michael@0 1057 (png_ptr->rowbytes + 1));
michael@0 1058 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
michael@0 1059 }
michael@0 1060
michael@0 1061 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
michael@0 1062 {
michael@0 1063 if (png_ptr->prev_row == NULL)
michael@0 1064 {
michael@0 1065 png_warning(png_ptr, "Can't add Up filter after starting");
michael@0 1066 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
michael@0 1067 ~PNG_FILTER_UP);
michael@0 1068 }
michael@0 1069
michael@0 1070 else
michael@0 1071 {
michael@0 1072 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
michael@0 1073 (png_ptr->rowbytes + 1));
michael@0 1074 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
michael@0 1075 }
michael@0 1076 }
michael@0 1077
michael@0 1078 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
michael@0 1079 {
michael@0 1080 if (png_ptr->prev_row == NULL)
michael@0 1081 {
michael@0 1082 png_warning(png_ptr, "Can't add Average filter after starting");
michael@0 1083 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
michael@0 1084 ~PNG_FILTER_AVG);
michael@0 1085 }
michael@0 1086
michael@0 1087 else
michael@0 1088 {
michael@0 1089 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
michael@0 1090 (png_ptr->rowbytes + 1));
michael@0 1091 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
michael@0 1092 }
michael@0 1093 }
michael@0 1094
michael@0 1095 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
michael@0 1096 png_ptr->paeth_row == NULL)
michael@0 1097 {
michael@0 1098 if (png_ptr->prev_row == NULL)
michael@0 1099 {
michael@0 1100 png_warning(png_ptr, "Can't add Paeth filter after starting");
michael@0 1101 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
michael@0 1102 }
michael@0 1103
michael@0 1104 else
michael@0 1105 {
michael@0 1106 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
michael@0 1107 (png_ptr->rowbytes + 1));
michael@0 1108 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
michael@0 1109 }
michael@0 1110 }
michael@0 1111
michael@0 1112 if (png_ptr->do_filter == PNG_NO_FILTERS)
michael@0 1113 #endif /* PNG_WRITE_FILTER_SUPPORTED */
michael@0 1114 png_ptr->do_filter = PNG_FILTER_NONE;
michael@0 1115 }
michael@0 1116 }
michael@0 1117 else
michael@0 1118 png_error(png_ptr, "Unknown custom filter method");
michael@0 1119 }
michael@0 1120
michael@0 1121 /* This allows us to influence the way in which libpng chooses the "best"
michael@0 1122 * filter for the current scanline. While the "minimum-sum-of-absolute-
michael@0 1123 * differences metric is relatively fast and effective, there is some
michael@0 1124 * question as to whether it can be improved upon by trying to keep the
michael@0 1125 * filtered data going to zlib more consistent, hopefully resulting in
michael@0 1126 * better compression.
michael@0 1127 */
michael@0 1128 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
michael@0 1129 /* Convenience reset API. */
michael@0 1130 static void
michael@0 1131 png_reset_filter_heuristics(png_structrp png_ptr)
michael@0 1132 {
michael@0 1133 /* Clear out any old values in the 'weights' - this must be done because if
michael@0 1134 * the app calls set_filter_heuristics multiple times with different
michael@0 1135 * 'num_weights' values we would otherwise potentially have wrong sized
michael@0 1136 * arrays.
michael@0 1137 */
michael@0 1138 png_ptr->num_prev_filters = 0;
michael@0 1139 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
michael@0 1140 if (png_ptr->prev_filters != NULL)
michael@0 1141 {
michael@0 1142 png_bytep old = png_ptr->prev_filters;
michael@0 1143 png_ptr->prev_filters = NULL;
michael@0 1144 png_free(png_ptr, old);
michael@0 1145 }
michael@0 1146 if (png_ptr->filter_weights != NULL)
michael@0 1147 {
michael@0 1148 png_uint_16p old = png_ptr->filter_weights;
michael@0 1149 png_ptr->filter_weights = NULL;
michael@0 1150 png_free(png_ptr, old);
michael@0 1151 }
michael@0 1152
michael@0 1153 if (png_ptr->inv_filter_weights != NULL)
michael@0 1154 {
michael@0 1155 png_uint_16p old = png_ptr->inv_filter_weights;
michael@0 1156 png_ptr->inv_filter_weights = NULL;
michael@0 1157 png_free(png_ptr, old);
michael@0 1158 }
michael@0 1159
michael@0 1160 /* Leave the filter_costs - this array is fixed size. */
michael@0 1161 }
michael@0 1162
michael@0 1163 static int
michael@0 1164 png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
michael@0 1165 int num_weights)
michael@0 1166 {
michael@0 1167 if (png_ptr == NULL)
michael@0 1168 return 0;
michael@0 1169
michael@0 1170 /* Clear out the arrays */
michael@0 1171 png_reset_filter_heuristics(png_ptr);
michael@0 1172
michael@0 1173 /* Check arguments; the 'reset' function makes the correct settings for the
michael@0 1174 * unweighted case, but we must handle the weight case by initializing the
michael@0 1175 * arrays for the caller.
michael@0 1176 */
michael@0 1177 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
michael@0 1178 {
michael@0 1179 int i;
michael@0 1180
michael@0 1181 if (num_weights > 0)
michael@0 1182 {
michael@0 1183 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
michael@0 1184 (png_uint_32)((sizeof (png_byte)) * num_weights));
michael@0 1185
michael@0 1186 /* To make sure that the weighting starts out fairly */
michael@0 1187 for (i = 0; i < num_weights; i++)
michael@0 1188 {
michael@0 1189 png_ptr->prev_filters[i] = 255;
michael@0 1190 }
michael@0 1191
michael@0 1192 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
michael@0 1193 (png_uint_32)((sizeof (png_uint_16)) * num_weights));
michael@0 1194
michael@0 1195 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
michael@0 1196 (png_uint_32)((sizeof (png_uint_16)) * num_weights));
michael@0 1197
michael@0 1198 for (i = 0; i < num_weights; i++)
michael@0 1199 {
michael@0 1200 png_ptr->inv_filter_weights[i] =
michael@0 1201 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
michael@0 1202 }
michael@0 1203
michael@0 1204 /* Safe to set this now */
michael@0 1205 png_ptr->num_prev_filters = (png_byte)num_weights;
michael@0 1206 }
michael@0 1207
michael@0 1208 /* If, in the future, there are other filter methods, this would
michael@0 1209 * need to be based on png_ptr->filter.
michael@0 1210 */
michael@0 1211 if (png_ptr->filter_costs == NULL)
michael@0 1212 {
michael@0 1213 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
michael@0 1214 (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
michael@0 1215
michael@0 1216 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
michael@0 1217 (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
michael@0 1218 }
michael@0 1219
michael@0 1220 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
michael@0 1221 {
michael@0 1222 png_ptr->inv_filter_costs[i] =
michael@0 1223 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
michael@0 1224 }
michael@0 1225
michael@0 1226 /* All the arrays are inited, safe to set this: */
michael@0 1227 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
michael@0 1228
michael@0 1229 /* Return the 'ok' code. */
michael@0 1230 return 1;
michael@0 1231 }
michael@0 1232 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
michael@0 1233 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
michael@0 1234 {
michael@0 1235 return 1;
michael@0 1236 }
michael@0 1237 else
michael@0 1238 {
michael@0 1239 png_warning(png_ptr, "Unknown filter heuristic method");
michael@0 1240 return 0;
michael@0 1241 }
michael@0 1242 }
michael@0 1243
michael@0 1244 /* Provide floating and fixed point APIs */
michael@0 1245 #ifdef PNG_FLOATING_POINT_SUPPORTED
michael@0 1246 void PNGAPI
michael@0 1247 png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
michael@0 1248 int num_weights, png_const_doublep filter_weights,
michael@0 1249 png_const_doublep filter_costs)
michael@0 1250 {
michael@0 1251 png_debug(1, "in png_set_filter_heuristics");
michael@0 1252
michael@0 1253 /* The internal API allocates all the arrays and ensures that the elements of
michael@0 1254 * those arrays are set to the default value.
michael@0 1255 */
michael@0 1256 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
michael@0 1257 return;
michael@0 1258
michael@0 1259 /* If using the weighted method copy in the weights. */
michael@0 1260 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
michael@0 1261 {
michael@0 1262 int i;
michael@0 1263 for (i = 0; i < num_weights; i++)
michael@0 1264 {
michael@0 1265 if (filter_weights[i] <= 0.0)
michael@0 1266 {
michael@0 1267 png_ptr->inv_filter_weights[i] =
michael@0 1268 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
michael@0 1269 }
michael@0 1270
michael@0 1271 else
michael@0 1272 {
michael@0 1273 png_ptr->inv_filter_weights[i] =
michael@0 1274 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
michael@0 1275
michael@0 1276 png_ptr->filter_weights[i] =
michael@0 1277 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
michael@0 1278 }
michael@0 1279 }
michael@0 1280
michael@0 1281 /* Here is where we set the relative costs of the different filters. We
michael@0 1282 * should take the desired compression level into account when setting
michael@0 1283 * the costs, so that Paeth, for instance, has a high relative cost at low
michael@0 1284 * compression levels, while it has a lower relative cost at higher
michael@0 1285 * compression settings. The filter types are in order of increasing
michael@0 1286 * relative cost, so it would be possible to do this with an algorithm.
michael@0 1287 */
michael@0 1288 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
michael@0 1289 {
michael@0 1290 png_ptr->inv_filter_costs[i] =
michael@0 1291 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
michael@0 1292
michael@0 1293 png_ptr->filter_costs[i] =
michael@0 1294 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
michael@0 1295 }
michael@0 1296 }
michael@0 1297 }
michael@0 1298 #endif /* FLOATING_POINT */
michael@0 1299
michael@0 1300 #ifdef PNG_FIXED_POINT_SUPPORTED
michael@0 1301 void PNGAPI
michael@0 1302 png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
michael@0 1303 int num_weights, png_const_fixed_point_p filter_weights,
michael@0 1304 png_const_fixed_point_p filter_costs)
michael@0 1305 {
michael@0 1306 png_debug(1, "in png_set_filter_heuristics_fixed");
michael@0 1307
michael@0 1308 /* The internal API allocates all the arrays and ensures that the elements of
michael@0 1309 * those arrays are set to the default value.
michael@0 1310 */
michael@0 1311 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
michael@0 1312 return;
michael@0 1313
michael@0 1314 /* If using the weighted method copy in the weights. */
michael@0 1315 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
michael@0 1316 {
michael@0 1317 int i;
michael@0 1318 for (i = 0; i < num_weights; i++)
michael@0 1319 {
michael@0 1320 if (filter_weights[i] <= 0)
michael@0 1321 {
michael@0 1322 png_ptr->inv_filter_weights[i] =
michael@0 1323 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
michael@0 1324 }
michael@0 1325
michael@0 1326 else
michael@0 1327 {
michael@0 1328 png_ptr->inv_filter_weights[i] = (png_uint_16)
michael@0 1329 ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
michael@0 1330
michael@0 1331 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
michael@0 1332 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
michael@0 1333 }
michael@0 1334 }
michael@0 1335
michael@0 1336 /* Here is where we set the relative costs of the different filters. We
michael@0 1337 * should take the desired compression level into account when setting
michael@0 1338 * the costs, so that Paeth, for instance, has a high relative cost at low
michael@0 1339 * compression levels, while it has a lower relative cost at higher
michael@0 1340 * compression settings. The filter types are in order of increasing
michael@0 1341 * relative cost, so it would be possible to do this with an algorithm.
michael@0 1342 */
michael@0 1343 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
michael@0 1344 if (filter_costs[i] >= PNG_FP_1)
michael@0 1345 {
michael@0 1346 png_uint_32 tmp;
michael@0 1347
michael@0 1348 /* Use a 32 bit unsigned temporary here because otherwise the
michael@0 1349 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
michael@0 1350 * and this will get the wrong answer on division.
michael@0 1351 */
michael@0 1352 tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
michael@0 1353 tmp /= filter_costs[i];
michael@0 1354
michael@0 1355 png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
michael@0 1356
michael@0 1357 tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
michael@0 1358 tmp /= PNG_FP_1;
michael@0 1359
michael@0 1360 png_ptr->filter_costs[i] = (png_uint_16)tmp;
michael@0 1361 }
michael@0 1362 }
michael@0 1363 }
michael@0 1364 #endif /* FIXED_POINT */
michael@0 1365 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
michael@0 1366
michael@0 1367 void PNGAPI
michael@0 1368 png_set_compression_level(png_structrp png_ptr, int level)
michael@0 1369 {
michael@0 1370 png_debug(1, "in png_set_compression_level");
michael@0 1371
michael@0 1372 if (png_ptr == NULL)
michael@0 1373 return;
michael@0 1374
michael@0 1375 png_ptr->zlib_level = level;
michael@0 1376 }
michael@0 1377
michael@0 1378 void PNGAPI
michael@0 1379 png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
michael@0 1380 {
michael@0 1381 png_debug(1, "in png_set_compression_mem_level");
michael@0 1382
michael@0 1383 if (png_ptr == NULL)
michael@0 1384 return;
michael@0 1385
michael@0 1386 png_ptr->zlib_mem_level = mem_level;
michael@0 1387 }
michael@0 1388
michael@0 1389 void PNGAPI
michael@0 1390 png_set_compression_strategy(png_structrp png_ptr, int strategy)
michael@0 1391 {
michael@0 1392 png_debug(1, "in png_set_compression_strategy");
michael@0 1393
michael@0 1394 if (png_ptr == NULL)
michael@0 1395 return;
michael@0 1396
michael@0 1397 /* The flag setting here prevents the libpng dynamic selection of strategy.
michael@0 1398 */
michael@0 1399 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
michael@0 1400 png_ptr->zlib_strategy = strategy;
michael@0 1401 }
michael@0 1402
michael@0 1403 /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
michael@0 1404 * smaller value of window_bits if it can do so safely.
michael@0 1405 */
michael@0 1406 void PNGAPI
michael@0 1407 png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
michael@0 1408 {
michael@0 1409 if (png_ptr == NULL)
michael@0 1410 return;
michael@0 1411
michael@0 1412 /* Prior to 1.6.0 this would warn but then set the window_bits value, this
michael@0 1413 * meant that negative window bits values could be selected which would cause
michael@0 1414 * libpng to write a non-standard PNG file with raw deflate or gzip
michael@0 1415 * compressed IDAT or ancillary chunks. Such files can be read and there is
michael@0 1416 * no warning on read, so this seems like a very bad idea.
michael@0 1417 */
michael@0 1418 if (window_bits > 15)
michael@0 1419 {
michael@0 1420 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
michael@0 1421 window_bits = 15;
michael@0 1422 }
michael@0 1423
michael@0 1424 else if (window_bits < 8)
michael@0 1425 {
michael@0 1426 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
michael@0 1427 window_bits = 8;
michael@0 1428 }
michael@0 1429
michael@0 1430 png_ptr->zlib_window_bits = window_bits;
michael@0 1431 }
michael@0 1432
michael@0 1433 void PNGAPI
michael@0 1434 png_set_compression_method(png_structrp png_ptr, int method)
michael@0 1435 {
michael@0 1436 png_debug(1, "in png_set_compression_method");
michael@0 1437
michael@0 1438 if (png_ptr == NULL)
michael@0 1439 return;
michael@0 1440
michael@0 1441 /* This would produce an invalid PNG file if it worked, but it doesn't and
michael@0 1442 * deflate will fault it, so it is harmless to just warn here.
michael@0 1443 */
michael@0 1444 if (method != 8)
michael@0 1445 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
michael@0 1446
michael@0 1447 png_ptr->zlib_method = method;
michael@0 1448 }
michael@0 1449
michael@0 1450 /* The following were added to libpng-1.5.4 */
michael@0 1451 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
michael@0 1452 void PNGAPI
michael@0 1453 png_set_text_compression_level(png_structrp png_ptr, int level)
michael@0 1454 {
michael@0 1455 png_debug(1, "in png_set_text_compression_level");
michael@0 1456
michael@0 1457 if (png_ptr == NULL)
michael@0 1458 return;
michael@0 1459
michael@0 1460 png_ptr->zlib_text_level = level;
michael@0 1461 }
michael@0 1462
michael@0 1463 void PNGAPI
michael@0 1464 png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
michael@0 1465 {
michael@0 1466 png_debug(1, "in png_set_text_compression_mem_level");
michael@0 1467
michael@0 1468 if (png_ptr == NULL)
michael@0 1469 return;
michael@0 1470
michael@0 1471 png_ptr->zlib_text_mem_level = mem_level;
michael@0 1472 }
michael@0 1473
michael@0 1474 void PNGAPI
michael@0 1475 png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
michael@0 1476 {
michael@0 1477 png_debug(1, "in png_set_text_compression_strategy");
michael@0 1478
michael@0 1479 if (png_ptr == NULL)
michael@0 1480 return;
michael@0 1481
michael@0 1482 png_ptr->zlib_text_strategy = strategy;
michael@0 1483 }
michael@0 1484
michael@0 1485 /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
michael@0 1486 * smaller value of window_bits if it can do so safely.
michael@0 1487 */
michael@0 1488 void PNGAPI
michael@0 1489 png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
michael@0 1490 {
michael@0 1491 if (png_ptr == NULL)
michael@0 1492 return;
michael@0 1493
michael@0 1494 if (window_bits > 15)
michael@0 1495 {
michael@0 1496 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
michael@0 1497 window_bits = 15;
michael@0 1498 }
michael@0 1499
michael@0 1500 else if (window_bits < 8)
michael@0 1501 {
michael@0 1502 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
michael@0 1503 window_bits = 8;
michael@0 1504 }
michael@0 1505
michael@0 1506 png_ptr->zlib_text_window_bits = window_bits;
michael@0 1507 }
michael@0 1508
michael@0 1509 void PNGAPI
michael@0 1510 png_set_text_compression_method(png_structrp png_ptr, int method)
michael@0 1511 {
michael@0 1512 png_debug(1, "in png_set_text_compression_method");
michael@0 1513
michael@0 1514 if (png_ptr == NULL)
michael@0 1515 return;
michael@0 1516
michael@0 1517 if (method != 8)
michael@0 1518 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
michael@0 1519
michael@0 1520 png_ptr->zlib_text_method = method;
michael@0 1521 }
michael@0 1522 #endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
michael@0 1523 /* end of API added to libpng-1.5.4 */
michael@0 1524
michael@0 1525 void PNGAPI
michael@0 1526 png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
michael@0 1527 {
michael@0 1528 if (png_ptr == NULL)
michael@0 1529 return;
michael@0 1530
michael@0 1531 png_ptr->write_row_fn = write_row_fn;
michael@0 1532 }
michael@0 1533
michael@0 1534 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
michael@0 1535 void PNGAPI
michael@0 1536 png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
michael@0 1537 write_user_transform_fn)
michael@0 1538 {
michael@0 1539 png_debug(1, "in png_set_write_user_transform_fn");
michael@0 1540
michael@0 1541 if (png_ptr == NULL)
michael@0 1542 return;
michael@0 1543
michael@0 1544 png_ptr->transformations |= PNG_USER_TRANSFORM;
michael@0 1545 png_ptr->write_user_transform_fn = write_user_transform_fn;
michael@0 1546 }
michael@0 1547 #endif
michael@0 1548
michael@0 1549
michael@0 1550 #ifdef PNG_INFO_IMAGE_SUPPORTED
michael@0 1551 void PNGAPI
michael@0 1552 png_write_png(png_structrp png_ptr, png_inforp info_ptr,
michael@0 1553 int transforms, voidp params)
michael@0 1554 {
michael@0 1555 if (png_ptr == NULL || info_ptr == NULL)
michael@0 1556 return;
michael@0 1557
michael@0 1558 if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
michael@0 1559 {
michael@0 1560 png_app_error(png_ptr, "no rows for png_write_image to write");
michael@0 1561 return;
michael@0 1562 }
michael@0 1563
michael@0 1564 /* Write the file header information. */
michael@0 1565 png_write_info(png_ptr, info_ptr);
michael@0 1566
michael@0 1567 /* ------ these transformations don't touch the info structure ------- */
michael@0 1568
michael@0 1569 /* Invert monochrome pixels */
michael@0 1570 if (transforms & PNG_TRANSFORM_INVERT_MONO)
michael@0 1571 #ifdef PNG_WRITE_INVERT_SUPPORTED
michael@0 1572 png_set_invert_mono(png_ptr);
michael@0 1573 #else
michael@0 1574 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
michael@0 1575 #endif
michael@0 1576
michael@0 1577 /* Shift the pixels up to a legal bit depth and fill in
michael@0 1578 * as appropriate to correctly scale the image.
michael@0 1579 */
michael@0 1580 if (transforms & PNG_TRANSFORM_SHIFT)
michael@0 1581 #ifdef PNG_WRITE_SHIFT_SUPPORTED
michael@0 1582 if (info_ptr->valid & PNG_INFO_sBIT)
michael@0 1583 png_set_shift(png_ptr, &info_ptr->sig_bit);
michael@0 1584 #else
michael@0 1585 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
michael@0 1586 #endif
michael@0 1587
michael@0 1588 /* Pack pixels into bytes */
michael@0 1589 if (transforms & PNG_TRANSFORM_PACKING)
michael@0 1590 #ifdef PNG_WRITE_PACK_SUPPORTED
michael@0 1591 png_set_packing(png_ptr);
michael@0 1592 #else
michael@0 1593 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
michael@0 1594 #endif
michael@0 1595
michael@0 1596 /* Swap location of alpha bytes from ARGB to RGBA */
michael@0 1597 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
michael@0 1598 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
michael@0 1599 png_set_swap_alpha(png_ptr);
michael@0 1600 #else
michael@0 1601 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
michael@0 1602 #endif
michael@0 1603
michael@0 1604 /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
michael@0 1605 * RGB, note that the code expects the input color type to be G or RGB; no
michael@0 1606 * alpha channel.
michael@0 1607 */
michael@0 1608 if (transforms &
michael@0 1609 (PNG_TRANSFORM_STRIP_FILLER_AFTER|PNG_TRANSFORM_STRIP_FILLER_BEFORE))
michael@0 1610 {
michael@0 1611 #ifdef PNG_WRITE_FILLER_SUPPORTED
michael@0 1612 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
michael@0 1613 {
michael@0 1614 if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
michael@0 1615 png_app_error(png_ptr,
michael@0 1616 "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
michael@0 1617
michael@0 1618 /* Continue if ignored - this is the pre-1.6.10 behavior */
michael@0 1619 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
michael@0 1620 }
michael@0 1621
michael@0 1622 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
michael@0 1623 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
michael@0 1624 #else
michael@0 1625 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
michael@0 1626 #endif
michael@0 1627 }
michael@0 1628
michael@0 1629 /* Flip BGR pixels to RGB */
michael@0 1630 if (transforms & PNG_TRANSFORM_BGR)
michael@0 1631 #ifdef PNG_WRITE_BGR_SUPPORTED
michael@0 1632 png_set_bgr(png_ptr);
michael@0 1633 #else
michael@0 1634 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
michael@0 1635 #endif
michael@0 1636
michael@0 1637 /* Swap bytes of 16-bit files to most significant byte first */
michael@0 1638 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
michael@0 1639 #ifdef PNG_WRITE_SWAP_SUPPORTED
michael@0 1640 png_set_swap(png_ptr);
michael@0 1641 #else
michael@0 1642 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
michael@0 1643 #endif
michael@0 1644
michael@0 1645 /* Swap bits of 1, 2, 4 bit packed pixel formats */
michael@0 1646 if (transforms & PNG_TRANSFORM_PACKSWAP)
michael@0 1647 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
michael@0 1648 png_set_packswap(png_ptr);
michael@0 1649 #else
michael@0 1650 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
michael@0 1651 #endif
michael@0 1652
michael@0 1653 /* Invert the alpha channel from opacity to transparency */
michael@0 1654 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
michael@0 1655 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
michael@0 1656 png_set_invert_alpha(png_ptr);
michael@0 1657 #else
michael@0 1658 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
michael@0 1659 #endif
michael@0 1660
michael@0 1661 /* ----------------------- end of transformations ------------------- */
michael@0 1662
michael@0 1663 /* Write the bits */
michael@0 1664 png_write_image(png_ptr, info_ptr->row_pointers);
michael@0 1665
michael@0 1666 /* It is REQUIRED to call this to finish writing the rest of the file */
michael@0 1667 png_write_end(png_ptr, info_ptr);
michael@0 1668
michael@0 1669 PNG_UNUSED(params)
michael@0 1670 }
michael@0 1671 #endif
michael@0 1672
michael@0 1673
michael@0 1674 #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
michael@0 1675 #ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
michael@0 1676 /* Initialize the write structure - general purpose utility. */
michael@0 1677 static int
michael@0 1678 png_image_write_init(png_imagep image)
michael@0 1679 {
michael@0 1680 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
michael@0 1681 png_safe_error, png_safe_warning);
michael@0 1682
michael@0 1683 if (png_ptr != NULL)
michael@0 1684 {
michael@0 1685 png_infop info_ptr = png_create_info_struct(png_ptr);
michael@0 1686
michael@0 1687 if (info_ptr != NULL)
michael@0 1688 {
michael@0 1689 png_controlp control = png_voidcast(png_controlp,
michael@0 1690 png_malloc_warn(png_ptr, (sizeof *control)));
michael@0 1691
michael@0 1692 if (control != NULL)
michael@0 1693 {
michael@0 1694 memset(control, 0, (sizeof *control));
michael@0 1695
michael@0 1696 control->png_ptr = png_ptr;
michael@0 1697 control->info_ptr = info_ptr;
michael@0 1698 control->for_write = 1;
michael@0 1699
michael@0 1700 image->opaque = control;
michael@0 1701 return 1;
michael@0 1702 }
michael@0 1703
michael@0 1704 /* Error clean up */
michael@0 1705 png_destroy_info_struct(png_ptr, &info_ptr);
michael@0 1706 }
michael@0 1707
michael@0 1708 png_destroy_write_struct(&png_ptr, NULL);
michael@0 1709 }
michael@0 1710
michael@0 1711 return png_image_error(image, "png_image_write_: out of memory");
michael@0 1712 }
michael@0 1713
michael@0 1714 /* Arguments to png_image_write_main: */
michael@0 1715 typedef struct
michael@0 1716 {
michael@0 1717 /* Arguments: */
michael@0 1718 png_imagep image;
michael@0 1719 png_const_voidp buffer;
michael@0 1720 png_int_32 row_stride;
michael@0 1721 png_const_voidp colormap;
michael@0 1722 int convert_to_8bit;
michael@0 1723 /* Local variables: */
michael@0 1724 png_const_voidp first_row;
michael@0 1725 ptrdiff_t row_bytes;
michael@0 1726 png_voidp local_row;
michael@0 1727 } png_image_write_control;
michael@0 1728
michael@0 1729 /* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
michael@0 1730 * do any necessary byte swapping. The component order is defined by the
michael@0 1731 * png_image format value.
michael@0 1732 */
michael@0 1733 static int
michael@0 1734 png_write_image_16bit(png_voidp argument)
michael@0 1735 {
michael@0 1736 png_image_write_control *display = png_voidcast(png_image_write_control*,
michael@0 1737 argument);
michael@0 1738 png_imagep image = display->image;
michael@0 1739 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 1740
michael@0 1741 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
michael@0 1742 display->first_row);
michael@0 1743 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
michael@0 1744 png_uint_16p row_end;
michael@0 1745 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
michael@0 1746 int aindex = 0;
michael@0 1747 png_uint_32 y = image->height;
michael@0 1748
michael@0 1749 if (image->format & PNG_FORMAT_FLAG_ALPHA)
michael@0 1750 {
michael@0 1751 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
michael@0 1752 if (image->format & PNG_FORMAT_FLAG_AFIRST)
michael@0 1753 {
michael@0 1754 aindex = -1;
michael@0 1755 ++input_row; /* To point to the first component */
michael@0 1756 ++output_row;
michael@0 1757 }
michael@0 1758
michael@0 1759 else
michael@0 1760 # endif
michael@0 1761 aindex = channels;
michael@0 1762 }
michael@0 1763
michael@0 1764 else
michael@0 1765 png_error(png_ptr, "png_write_image: internal call error");
michael@0 1766
michael@0 1767 /* Work out the output row end and count over this, note that the increment
michael@0 1768 * above to 'row' means that row_end can actually be beyond the end of the
michael@0 1769 * row; this is correct.
michael@0 1770 */
michael@0 1771 row_end = output_row + image->width * (channels+1);
michael@0 1772
michael@0 1773 while (y-- > 0)
michael@0 1774 {
michael@0 1775 png_const_uint_16p in_ptr = input_row;
michael@0 1776 png_uint_16p out_ptr = output_row;
michael@0 1777
michael@0 1778 while (out_ptr < row_end)
michael@0 1779 {
michael@0 1780 const png_uint_16 alpha = in_ptr[aindex];
michael@0 1781 png_uint_32 reciprocal = 0;
michael@0 1782 int c;
michael@0 1783
michael@0 1784 out_ptr[aindex] = alpha;
michael@0 1785
michael@0 1786 /* Calculate a reciprocal. The correct calculation is simply
michael@0 1787 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
michael@0 1788 * allows correct rounding by adding .5 before the shift. 'reciprocal'
michael@0 1789 * is only initialized when required.
michael@0 1790 */
michael@0 1791 if (alpha > 0 && alpha < 65535)
michael@0 1792 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
michael@0 1793
michael@0 1794 c = channels;
michael@0 1795 do /* always at least one channel */
michael@0 1796 {
michael@0 1797 png_uint_16 component = *in_ptr++;
michael@0 1798
michael@0 1799 /* The following gives 65535 for an alpha of 0, which is fine,
michael@0 1800 * otherwise if 0/0 is represented as some other value there is more
michael@0 1801 * likely to be a discontinuity which will probably damage
michael@0 1802 * compression when moving from a fully transparent area to a
michael@0 1803 * nearly transparent one. (The assumption here is that opaque
michael@0 1804 * areas tend not to be 0 intensity.)
michael@0 1805 */
michael@0 1806 if (component >= alpha)
michael@0 1807 component = 65535;
michael@0 1808
michael@0 1809 /* component<alpha, so component/alpha is less than one and
michael@0 1810 * component*reciprocal is less than 2^31.
michael@0 1811 */
michael@0 1812 else if (component > 0 && alpha < 65535)
michael@0 1813 {
michael@0 1814 png_uint_32 calc = component * reciprocal;
michael@0 1815 calc += 16384; /* round to nearest */
michael@0 1816 component = (png_uint_16)(calc >> 15);
michael@0 1817 }
michael@0 1818
michael@0 1819 *out_ptr++ = component;
michael@0 1820 }
michael@0 1821 while (--c > 0);
michael@0 1822
michael@0 1823 /* Skip to next component (skip the intervening alpha channel) */
michael@0 1824 ++in_ptr;
michael@0 1825 ++out_ptr;
michael@0 1826 }
michael@0 1827
michael@0 1828 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
michael@0 1829 input_row += display->row_bytes/(sizeof (png_uint_16));
michael@0 1830 }
michael@0 1831
michael@0 1832 return 1;
michael@0 1833 }
michael@0 1834
michael@0 1835 /* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
michael@0 1836 * is present it must be removed from the components, the components are then
michael@0 1837 * written in sRGB encoding. No components are added or removed.
michael@0 1838 *
michael@0 1839 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
michael@0 1840 * calculation can be done to 15 bits of accuracy; however, the output needs to
michael@0 1841 * be scaled in the range 0..255*65535, so include that scaling here.
michael@0 1842 */
michael@0 1843 #define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
michael@0 1844
michael@0 1845 static png_byte
michael@0 1846 png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
michael@0 1847 png_uint_32 reciprocal/*from the above macro*/)
michael@0 1848 {
michael@0 1849 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
michael@0 1850 * is represented as some other value there is more likely to be a
michael@0 1851 * discontinuity which will probably damage compression when moving from a
michael@0 1852 * fully transparent area to a nearly transparent one. (The assumption here
michael@0 1853 * is that opaque areas tend not to be 0 intensity.)
michael@0 1854 *
michael@0 1855 * There is a rounding problem here; if alpha is less than 128 it will end up
michael@0 1856 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
michael@0 1857 * output change for this too.
michael@0 1858 */
michael@0 1859 if (component >= alpha || alpha < 128)
michael@0 1860 return 255;
michael@0 1861
michael@0 1862 /* component<alpha, so component/alpha is less than one and
michael@0 1863 * component*reciprocal is less than 2^31.
michael@0 1864 */
michael@0 1865 else if (component > 0)
michael@0 1866 {
michael@0 1867 /* The test is that alpha/257 (rounded) is less than 255, the first value
michael@0 1868 * that becomes 255 is 65407.
michael@0 1869 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
michael@0 1870 * be exact!) [Could also test reciprocal != 0]
michael@0 1871 */
michael@0 1872 if (alpha < 65407)
michael@0 1873 {
michael@0 1874 component *= reciprocal;
michael@0 1875 component += 64; /* round to nearest */
michael@0 1876 component >>= 7;
michael@0 1877 }
michael@0 1878
michael@0 1879 else
michael@0 1880 component *= 255;
michael@0 1881
michael@0 1882 /* Convert the component to sRGB. */
michael@0 1883 return (png_byte)PNG_sRGB_FROM_LINEAR(component);
michael@0 1884 }
michael@0 1885
michael@0 1886 else
michael@0 1887 return 0;
michael@0 1888 }
michael@0 1889
michael@0 1890 static int
michael@0 1891 png_write_image_8bit(png_voidp argument)
michael@0 1892 {
michael@0 1893 png_image_write_control *display = png_voidcast(png_image_write_control*,
michael@0 1894 argument);
michael@0 1895 png_imagep image = display->image;
michael@0 1896 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 1897
michael@0 1898 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
michael@0 1899 display->first_row);
michael@0 1900 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
michael@0 1901 png_uint_32 y = image->height;
michael@0 1902 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
michael@0 1903
michael@0 1904 if (image->format & PNG_FORMAT_FLAG_ALPHA)
michael@0 1905 {
michael@0 1906 png_bytep row_end;
michael@0 1907 int aindex;
michael@0 1908
michael@0 1909 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
michael@0 1910 if (image->format & PNG_FORMAT_FLAG_AFIRST)
michael@0 1911 {
michael@0 1912 aindex = -1;
michael@0 1913 ++input_row; /* To point to the first component */
michael@0 1914 ++output_row;
michael@0 1915 }
michael@0 1916
michael@0 1917 else
michael@0 1918 # endif
michael@0 1919 aindex = channels;
michael@0 1920
michael@0 1921 /* Use row_end in place of a loop counter: */
michael@0 1922 row_end = output_row + image->width * (channels+1);
michael@0 1923
michael@0 1924 while (y-- > 0)
michael@0 1925 {
michael@0 1926 png_const_uint_16p in_ptr = input_row;
michael@0 1927 png_bytep out_ptr = output_row;
michael@0 1928
michael@0 1929 while (out_ptr < row_end)
michael@0 1930 {
michael@0 1931 png_uint_16 alpha = in_ptr[aindex];
michael@0 1932 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
michael@0 1933 png_uint_32 reciprocal = 0;
michael@0 1934 int c;
michael@0 1935
michael@0 1936 /* Scale and write the alpha channel. */
michael@0 1937 out_ptr[aindex] = alphabyte;
michael@0 1938
michael@0 1939 if (alphabyte > 0 && alphabyte < 255)
michael@0 1940 reciprocal = UNP_RECIPROCAL(alpha);
michael@0 1941
michael@0 1942 c = channels;
michael@0 1943 do /* always at least one channel */
michael@0 1944 *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
michael@0 1945 while (--c > 0);
michael@0 1946
michael@0 1947 /* Skip to next component (skip the intervening alpha channel) */
michael@0 1948 ++in_ptr;
michael@0 1949 ++out_ptr;
michael@0 1950 } /* while out_ptr < row_end */
michael@0 1951
michael@0 1952 png_write_row(png_ptr, png_voidcast(png_const_bytep,
michael@0 1953 display->local_row));
michael@0 1954 input_row += display->row_bytes/(sizeof (png_uint_16));
michael@0 1955 } /* while y */
michael@0 1956 }
michael@0 1957
michael@0 1958 else
michael@0 1959 {
michael@0 1960 /* No alpha channel, so the row_end really is the end of the row and it
michael@0 1961 * is sufficient to loop over the components one by one.
michael@0 1962 */
michael@0 1963 png_bytep row_end = output_row + image->width * channels;
michael@0 1964
michael@0 1965 while (y-- > 0)
michael@0 1966 {
michael@0 1967 png_const_uint_16p in_ptr = input_row;
michael@0 1968 png_bytep out_ptr = output_row;
michael@0 1969
michael@0 1970 while (out_ptr < row_end)
michael@0 1971 {
michael@0 1972 png_uint_32 component = *in_ptr++;
michael@0 1973
michael@0 1974 component *= 255;
michael@0 1975 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
michael@0 1976 }
michael@0 1977
michael@0 1978 png_write_row(png_ptr, output_row);
michael@0 1979 input_row += display->row_bytes/(sizeof (png_uint_16));
michael@0 1980 }
michael@0 1981 }
michael@0 1982
michael@0 1983 return 1;
michael@0 1984 }
michael@0 1985
michael@0 1986 static void
michael@0 1987 png_image_set_PLTE(png_image_write_control *display)
michael@0 1988 {
michael@0 1989 const png_imagep image = display->image;
michael@0 1990 const void *cmap = display->colormap;
michael@0 1991 const int entries = image->colormap_entries > 256 ? 256 :
michael@0 1992 (int)image->colormap_entries;
michael@0 1993
michael@0 1994 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
michael@0 1995 const png_uint_32 format = image->format;
michael@0 1996 const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
michael@0 1997
michael@0 1998 # if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
michael@0 1999 defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
michael@0 2000 const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
michael@0 2001 (format & PNG_FORMAT_FLAG_ALPHA) != 0;
michael@0 2002 # else
michael@0 2003 # define afirst 0
michael@0 2004 # endif
michael@0 2005
michael@0 2006 # ifdef PNG_FORMAT_BGR_SUPPORTED
michael@0 2007 const int bgr = (format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
michael@0 2008 # else
michael@0 2009 # define bgr 0
michael@0 2010 # endif
michael@0 2011
michael@0 2012 int i, num_trans;
michael@0 2013 png_color palette[256];
michael@0 2014 png_byte tRNS[256];
michael@0 2015
michael@0 2016 memset(tRNS, 255, (sizeof tRNS));
michael@0 2017 memset(palette, 0, (sizeof palette));
michael@0 2018
michael@0 2019 for (i=num_trans=0; i<entries; ++i)
michael@0 2020 {
michael@0 2021 /* This gets automatically converted to sRGB with reversal of the
michael@0 2022 * pre-multiplication if the color-map has an alpha channel.
michael@0 2023 */
michael@0 2024 if (format & PNG_FORMAT_FLAG_LINEAR)
michael@0 2025 {
michael@0 2026 png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
michael@0 2027
michael@0 2028 entry += i * channels;
michael@0 2029
michael@0 2030 if (channels & 1) /* no alpha */
michael@0 2031 {
michael@0 2032 if (channels >= 3) /* RGB */
michael@0 2033 {
michael@0 2034 palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
michael@0 2035 entry[(2 ^ bgr)]);
michael@0 2036 palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
michael@0 2037 entry[1]);
michael@0 2038 palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
michael@0 2039 entry[bgr]);
michael@0 2040 }
michael@0 2041
michael@0 2042 else /* Gray */
michael@0 2043 palette[i].blue = palette[i].red = palette[i].green =
michael@0 2044 (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
michael@0 2045 }
michael@0 2046
michael@0 2047 else /* alpha */
michael@0 2048 {
michael@0 2049 png_uint_16 alpha = entry[afirst ? 0 : channels-1];
michael@0 2050 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
michael@0 2051 png_uint_32 reciprocal = 0;
michael@0 2052
michael@0 2053 /* Calculate a reciprocal, as in the png_write_image_8bit code above
michael@0 2054 * this is designed to produce a value scaled to 255*65535 when
michael@0 2055 * divided by 128 (i.e. asr 7).
michael@0 2056 */
michael@0 2057 if (alphabyte > 0 && alphabyte < 255)
michael@0 2058 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
michael@0 2059
michael@0 2060 tRNS[i] = alphabyte;
michael@0 2061 if (alphabyte < 255)
michael@0 2062 num_trans = i+1;
michael@0 2063
michael@0 2064 if (channels >= 3) /* RGB */
michael@0 2065 {
michael@0 2066 palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
michael@0 2067 alpha, reciprocal);
michael@0 2068 palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
michael@0 2069 reciprocal);
michael@0 2070 palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
michael@0 2071 reciprocal);
michael@0 2072 }
michael@0 2073
michael@0 2074 else /* gray */
michael@0 2075 palette[i].blue = palette[i].red = palette[i].green =
michael@0 2076 png_unpremultiply(entry[afirst], alpha, reciprocal);
michael@0 2077 }
michael@0 2078 }
michael@0 2079
michael@0 2080 else /* Color-map has sRGB values */
michael@0 2081 {
michael@0 2082 png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
michael@0 2083
michael@0 2084 entry += i * channels;
michael@0 2085
michael@0 2086 switch (channels)
michael@0 2087 {
michael@0 2088 case 4:
michael@0 2089 tRNS[i] = entry[afirst ? 0 : 3];
michael@0 2090 if (tRNS[i] < 255)
michael@0 2091 num_trans = i+1;
michael@0 2092 /* FALL THROUGH */
michael@0 2093 case 3:
michael@0 2094 palette[i].blue = entry[afirst + (2 ^ bgr)];
michael@0 2095 palette[i].green = entry[afirst + 1];
michael@0 2096 palette[i].red = entry[afirst + bgr];
michael@0 2097 break;
michael@0 2098
michael@0 2099 case 2:
michael@0 2100 tRNS[i] = entry[1 ^ afirst];
michael@0 2101 if (tRNS[i] < 255)
michael@0 2102 num_trans = i+1;
michael@0 2103 /* FALL THROUGH */
michael@0 2104 case 1:
michael@0 2105 palette[i].blue = palette[i].red = palette[i].green =
michael@0 2106 entry[afirst];
michael@0 2107 break;
michael@0 2108
michael@0 2109 default:
michael@0 2110 break;
michael@0 2111 }
michael@0 2112 }
michael@0 2113 }
michael@0 2114
michael@0 2115 # ifdef afirst
michael@0 2116 # undef afirst
michael@0 2117 # endif
michael@0 2118 # ifdef bgr
michael@0 2119 # undef bgr
michael@0 2120 # endif
michael@0 2121
michael@0 2122 png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
michael@0 2123 entries);
michael@0 2124
michael@0 2125 if (num_trans > 0)
michael@0 2126 png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
michael@0 2127 num_trans, NULL);
michael@0 2128
michael@0 2129 image->colormap_entries = entries;
michael@0 2130 }
michael@0 2131
michael@0 2132 static int
michael@0 2133 png_image_write_main(png_voidp argument)
michael@0 2134 {
michael@0 2135 png_image_write_control *display = png_voidcast(png_image_write_control*,
michael@0 2136 argument);
michael@0 2137 png_imagep image = display->image;
michael@0 2138 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 2139 png_inforp info_ptr = image->opaque->info_ptr;
michael@0 2140 png_uint_32 format = image->format;
michael@0 2141
michael@0 2142 int colormap = (format & PNG_FORMAT_FLAG_COLORMAP) != 0;
michael@0 2143 int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR) != 0; /* input */
michael@0 2144 int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0;
michael@0 2145 int write_16bit = linear && !colormap && !display->convert_to_8bit;
michael@0 2146
michael@0 2147 # ifdef PNG_BENIGN_ERRORS_SUPPORTED
michael@0 2148 /* Make sure we error out on any bad situation */
michael@0 2149 png_set_benign_errors(png_ptr, 0/*error*/);
michael@0 2150 # endif
michael@0 2151
michael@0 2152 /* Default the 'row_stride' parameter if required. */
michael@0 2153 if (display->row_stride == 0)
michael@0 2154 display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
michael@0 2155
michael@0 2156 /* Set the required transforms then write the rows in the correct order. */
michael@0 2157 if (format & PNG_FORMAT_FLAG_COLORMAP)
michael@0 2158 {
michael@0 2159 if (display->colormap != NULL && image->colormap_entries > 0)
michael@0 2160 {
michael@0 2161 png_uint_32 entries = image->colormap_entries;
michael@0 2162
michael@0 2163 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
michael@0 2164 entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
michael@0 2165 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
michael@0 2166 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
michael@0 2167
michael@0 2168 png_image_set_PLTE(display);
michael@0 2169 }
michael@0 2170
michael@0 2171 else
michael@0 2172 png_error(image->opaque->png_ptr,
michael@0 2173 "no color-map for color-mapped image");
michael@0 2174 }
michael@0 2175
michael@0 2176 else
michael@0 2177 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
michael@0 2178 write_16bit ? 16 : 8,
michael@0 2179 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
michael@0 2180 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
michael@0 2181 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
michael@0 2182
michael@0 2183 /* Counter-intuitively the data transformations must be called *after*
michael@0 2184 * png_write_info, not before as in the read code, but the 'set' functions
michael@0 2185 * must still be called before. Just set the color space information, never
michael@0 2186 * write an interlaced image.
michael@0 2187 */
michael@0 2188
michael@0 2189 if (write_16bit)
michael@0 2190 {
michael@0 2191 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
michael@0 2192 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
michael@0 2193
michael@0 2194 if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
michael@0 2195 png_set_cHRM_fixed(png_ptr, info_ptr,
michael@0 2196 /* color x y */
michael@0 2197 /* white */ 31270, 32900,
michael@0 2198 /* red */ 64000, 33000,
michael@0 2199 /* green */ 30000, 60000,
michael@0 2200 /* blue */ 15000, 6000
michael@0 2201 );
michael@0 2202 }
michael@0 2203
michael@0 2204 else if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
michael@0 2205 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
michael@0 2206
michael@0 2207 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
michael@0 2208 * space must still be gamma encoded.
michael@0 2209 */
michael@0 2210 else
michael@0 2211 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
michael@0 2212
michael@0 2213 /* Write the file header. */
michael@0 2214 png_write_info(png_ptr, info_ptr);
michael@0 2215
michael@0 2216 /* Now set up the data transformations (*after* the header is written),
michael@0 2217 * remove the handled transformations from the 'format' flags for checking.
michael@0 2218 *
michael@0 2219 * First check for a little endian system if writing 16 bit files.
michael@0 2220 */
michael@0 2221 if (write_16bit)
michael@0 2222 {
michael@0 2223 PNG_CONST png_uint_16 le = 0x0001;
michael@0 2224
michael@0 2225 if (*(png_const_bytep)&le)
michael@0 2226 png_set_swap(png_ptr);
michael@0 2227 }
michael@0 2228
michael@0 2229 # ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
michael@0 2230 if (format & PNG_FORMAT_FLAG_BGR)
michael@0 2231 {
michael@0 2232 if (!colormap && (format & PNG_FORMAT_FLAG_COLOR) != 0)
michael@0 2233 png_set_bgr(png_ptr);
michael@0 2234 format &= ~PNG_FORMAT_FLAG_BGR;
michael@0 2235 }
michael@0 2236 # endif
michael@0 2237
michael@0 2238 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
michael@0 2239 if (format & PNG_FORMAT_FLAG_AFIRST)
michael@0 2240 {
michael@0 2241 if (!colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
michael@0 2242 png_set_swap_alpha(png_ptr);
michael@0 2243 format &= ~PNG_FORMAT_FLAG_AFIRST;
michael@0 2244 }
michael@0 2245 # endif
michael@0 2246
michael@0 2247 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
michael@0 2248 * above, but the application data is still byte packed.
michael@0 2249 */
michael@0 2250 if (colormap && image->colormap_entries <= 16)
michael@0 2251 png_set_packing(png_ptr);
michael@0 2252
michael@0 2253 /* That should have handled all (both) the transforms. */
michael@0 2254 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
michael@0 2255 PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
michael@0 2256 png_error(png_ptr, "png_write_image: unsupported transformation");
michael@0 2257
michael@0 2258 {
michael@0 2259 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
michael@0 2260 ptrdiff_t row_bytes = display->row_stride;
michael@0 2261
michael@0 2262 if (linear)
michael@0 2263 row_bytes *= (sizeof (png_uint_16));
michael@0 2264
michael@0 2265 if (row_bytes < 0)
michael@0 2266 row += (image->height-1) * (-row_bytes);
michael@0 2267
michael@0 2268 display->first_row = row;
michael@0 2269 display->row_bytes = row_bytes;
michael@0 2270 }
michael@0 2271
michael@0 2272 /* Apply 'fast' options if the flag is set. */
michael@0 2273 if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
michael@0 2274 {
michael@0 2275 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
michael@0 2276 /* NOTE: determined by experiment using pngstest, this reflects some
michael@0 2277 * balance between the time to write the image once and the time to read
michael@0 2278 * it about 50 times. The speed-up in pngstest was about 10-20% of the
michael@0 2279 * total (user) time on a heavily loaded system.
michael@0 2280 */
michael@0 2281 png_set_compression_level(png_ptr, 3);
michael@0 2282 }
michael@0 2283
michael@0 2284 /* Check for the cases that currently require a pre-transform on the row
michael@0 2285 * before it is written. This only applies when the input is 16-bit and
michael@0 2286 * either there is an alpha channel or it is converted to 8-bit.
michael@0 2287 */
michael@0 2288 if ((linear && alpha) || (!colormap && display->convert_to_8bit))
michael@0 2289 {
michael@0 2290 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
michael@0 2291 png_get_rowbytes(png_ptr, info_ptr)));
michael@0 2292 int result;
michael@0 2293
michael@0 2294 display->local_row = row;
michael@0 2295 if (write_16bit)
michael@0 2296 result = png_safe_execute(image, png_write_image_16bit, display);
michael@0 2297 else
michael@0 2298 result = png_safe_execute(image, png_write_image_8bit, display);
michael@0 2299 display->local_row = NULL;
michael@0 2300
michael@0 2301 png_free(png_ptr, row);
michael@0 2302
michael@0 2303 /* Skip the 'write_end' on error: */
michael@0 2304 if (!result)
michael@0 2305 return 0;
michael@0 2306 }
michael@0 2307
michael@0 2308 /* Otherwise this is the case where the input is in a format currently
michael@0 2309 * supported by the rest of the libpng write code; call it directly.
michael@0 2310 */
michael@0 2311 else
michael@0 2312 {
michael@0 2313 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
michael@0 2314 ptrdiff_t row_bytes = display->row_bytes;
michael@0 2315 png_uint_32 y = image->height;
michael@0 2316
michael@0 2317 while (y-- > 0)
michael@0 2318 {
michael@0 2319 png_write_row(png_ptr, row);
michael@0 2320 row += row_bytes;
michael@0 2321 }
michael@0 2322 }
michael@0 2323
michael@0 2324 png_write_end(png_ptr, info_ptr);
michael@0 2325 return 1;
michael@0 2326 }
michael@0 2327
michael@0 2328 int PNGAPI
michael@0 2329 png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
michael@0 2330 const void *buffer, png_int_32 row_stride, const void *colormap)
michael@0 2331 {
michael@0 2332 /* Write the image to the given (FILE*). */
michael@0 2333 if (image != NULL && image->version == PNG_IMAGE_VERSION)
michael@0 2334 {
michael@0 2335 if (file != NULL)
michael@0 2336 {
michael@0 2337 if (png_image_write_init(image))
michael@0 2338 {
michael@0 2339 png_image_write_control display;
michael@0 2340 int result;
michael@0 2341
michael@0 2342 /* This is slightly evil, but png_init_io doesn't do anything other
michael@0 2343 * than this and we haven't changed the standard IO functions so
michael@0 2344 * this saves a 'safe' function.
michael@0 2345 */
michael@0 2346 image->opaque->png_ptr->io_ptr = file;
michael@0 2347
michael@0 2348 memset(&display, 0, (sizeof display));
michael@0 2349 display.image = image;
michael@0 2350 display.buffer = buffer;
michael@0 2351 display.row_stride = row_stride;
michael@0 2352 display.colormap = colormap;
michael@0 2353 display.convert_to_8bit = convert_to_8bit;
michael@0 2354
michael@0 2355 result = png_safe_execute(image, png_image_write_main, &display);
michael@0 2356 png_image_free(image);
michael@0 2357 return result;
michael@0 2358 }
michael@0 2359
michael@0 2360 else
michael@0 2361 return 0;
michael@0 2362 }
michael@0 2363
michael@0 2364 else
michael@0 2365 return png_image_error(image,
michael@0 2366 "png_image_write_to_stdio: invalid argument");
michael@0 2367 }
michael@0 2368
michael@0 2369 else if (image != NULL)
michael@0 2370 return png_image_error(image,
michael@0 2371 "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
michael@0 2372
michael@0 2373 else
michael@0 2374 return 0;
michael@0 2375 }
michael@0 2376
michael@0 2377 int PNGAPI
michael@0 2378 png_image_write_to_file(png_imagep image, const char *file_name,
michael@0 2379 int convert_to_8bit, const void *buffer, png_int_32 row_stride,
michael@0 2380 const void *colormap)
michael@0 2381 {
michael@0 2382 /* Write the image to the named file. */
michael@0 2383 if (image != NULL && image->version == PNG_IMAGE_VERSION)
michael@0 2384 {
michael@0 2385 if (file_name != NULL)
michael@0 2386 {
michael@0 2387 FILE *fp = fopen(file_name, "wb");
michael@0 2388
michael@0 2389 if (fp != NULL)
michael@0 2390 {
michael@0 2391 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
michael@0 2392 row_stride, colormap))
michael@0 2393 {
michael@0 2394 int error; /* from fflush/fclose */
michael@0 2395
michael@0 2396 /* Make sure the file is flushed correctly. */
michael@0 2397 if (fflush(fp) == 0 && ferror(fp) == 0)
michael@0 2398 {
michael@0 2399 if (fclose(fp) == 0)
michael@0 2400 return 1;
michael@0 2401
michael@0 2402 error = errno; /* from fclose */
michael@0 2403 }
michael@0 2404
michael@0 2405 else
michael@0 2406 {
michael@0 2407 error = errno; /* from fflush or ferror */
michael@0 2408 (void)fclose(fp);
michael@0 2409 }
michael@0 2410
michael@0 2411 (void)remove(file_name);
michael@0 2412 /* The image has already been cleaned up; this is just used to
michael@0 2413 * set the error (because the original write succeeded).
michael@0 2414 */
michael@0 2415 return png_image_error(image, strerror(error));
michael@0 2416 }
michael@0 2417
michael@0 2418 else
michael@0 2419 {
michael@0 2420 /* Clean up: just the opened file. */
michael@0 2421 (void)fclose(fp);
michael@0 2422 (void)remove(file_name);
michael@0 2423 return 0;
michael@0 2424 }
michael@0 2425 }
michael@0 2426
michael@0 2427 else
michael@0 2428 return png_image_error(image, strerror(errno));
michael@0 2429 }
michael@0 2430
michael@0 2431 else
michael@0 2432 return png_image_error(image,
michael@0 2433 "png_image_write_to_file: invalid argument");
michael@0 2434 }
michael@0 2435
michael@0 2436 else if (image != NULL)
michael@0 2437 return png_image_error(image,
michael@0 2438 "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
michael@0 2439
michael@0 2440 else
michael@0 2441 return 0;
michael@0 2442 }
michael@0 2443 #endif /* PNG_STDIO_SUPPORTED */
michael@0 2444 #endif /* SIMPLIFIED_WRITE */
michael@0 2445
michael@0 2446 #ifdef PNG_WRITE_APNG_SUPPORTED
michael@0 2447 void PNGAPI
michael@0 2448 png_write_frame_head(png_structp png_ptr, png_infop info_ptr,
michael@0 2449 png_bytepp row_pointers, png_uint_32 width, png_uint_32 height,
michael@0 2450 png_uint_32 x_offset, png_uint_32 y_offset,
michael@0 2451 png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
michael@0 2452 png_byte blend_op)
michael@0 2453 {
michael@0 2454 png_debug(1, "in png_write_frame_head");
michael@0 2455
michael@0 2456 /* there is a chance this has been set after png_write_info was called,
michael@0 2457 * so it would be set but not written. is there a way to be sure? */
michael@0 2458 if (!(info_ptr->valid & PNG_INFO_acTL))
michael@0 2459 png_error(png_ptr, "png_write_frame_head(): acTL not set");
michael@0 2460
michael@0 2461 png_write_reset(png_ptr);
michael@0 2462
michael@0 2463 png_write_reinit(png_ptr, info_ptr, width, height);
michael@0 2464
michael@0 2465 if ( !(png_ptr->num_frames_written == 0 &&
michael@0 2466 (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) ) )
michael@0 2467 png_write_fcTL(png_ptr, width, height, x_offset, y_offset,
michael@0 2468 delay_num, delay_den, dispose_op, blend_op);
michael@0 2469
michael@0 2470 PNG_UNUSED(row_pointers)
michael@0 2471 }
michael@0 2472
michael@0 2473 void PNGAPI
michael@0 2474 png_write_frame_tail(png_structp png_ptr, png_infop info_ptr)
michael@0 2475 {
michael@0 2476 png_debug(1, "in png_write_frame_tail");
michael@0 2477
michael@0 2478 png_ptr->num_frames_written++;
michael@0 2479
michael@0 2480 PNG_UNUSED(info_ptr)
michael@0 2481 }
michael@0 2482 #endif /* PNG_WRITE_APNG_SUPPORTED */
michael@0 2483 #endif /* PNG_WRITE_SUPPORTED */

mercurial