media/libpng/pngread.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 /* pngread.c - read 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 * This file contains routines that an application calls directly to
michael@0 14 * read a PNG file or stream.
michael@0 15 */
michael@0 16
michael@0 17 #include "pngpriv.h"
michael@0 18 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
michael@0 19 # include <errno.h>
michael@0 20 #endif
michael@0 21
michael@0 22 #ifdef PNG_READ_SUPPORTED
michael@0 23
michael@0 24 /* Create a PNG structure for reading, and allocate any memory needed. */
michael@0 25 PNG_FUNCTION(png_structp,PNGAPI
michael@0 26 png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
michael@0 27 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
michael@0 28 {
michael@0 29 #ifndef PNG_USER_MEM_SUPPORTED
michael@0 30 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
michael@0 31 error_fn, warn_fn, NULL, NULL, NULL);
michael@0 32 #else
michael@0 33 return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
michael@0 34 warn_fn, NULL, NULL, NULL);
michael@0 35 }
michael@0 36
michael@0 37 /* Alternate create PNG structure for reading, and allocate any memory
michael@0 38 * needed.
michael@0 39 */
michael@0 40 PNG_FUNCTION(png_structp,PNGAPI
michael@0 41 png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
michael@0 42 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
michael@0 43 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
michael@0 44 {
michael@0 45 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
michael@0 46 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
michael@0 47 #endif /* PNG_USER_MEM_SUPPORTED */
michael@0 48
michael@0 49 if (png_ptr != NULL)
michael@0 50 {
michael@0 51 png_ptr->mode = PNG_IS_READ_STRUCT;
michael@0 52
michael@0 53 /* Added in libpng-1.6.0; this can be used to detect a read structure if
michael@0 54 * required (it will be zero in a write structure.)
michael@0 55 */
michael@0 56 # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
michael@0 58 # endif
michael@0 59
michael@0 60 # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
michael@0 61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
michael@0 62
michael@0 63 /* In stable builds only warn if an application error can be completely
michael@0 64 * handled.
michael@0 65 */
michael@0 66 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
michael@0 67 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
michael@0 68 # endif
michael@0 69 # endif
michael@0 70
michael@0 71 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
michael@0 72 * do it itself) avoiding setting the default function if it is not
michael@0 73 * required.
michael@0 74 */
michael@0 75 png_set_read_fn(png_ptr, NULL, NULL);
michael@0 76 }
michael@0 77
michael@0 78 return png_ptr;
michael@0 79 }
michael@0 80
michael@0 81
michael@0 82 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 83 /* Read the information before the actual image data. This has been
michael@0 84 * changed in v0.90 to allow reading a file that already has the magic
michael@0 85 * bytes read from the stream. You can tell libpng how many bytes have
michael@0 86 * been read from the beginning of the stream (up to the maximum of 8)
michael@0 87 * via png_set_sig_bytes(), and we will only check the remaining bytes
michael@0 88 * here. The application can then have access to the signature bytes we
michael@0 89 * read if it is determined that this isn't a valid PNG file.
michael@0 90 */
michael@0 91 void PNGAPI
michael@0 92 png_read_info(png_structrp png_ptr, png_inforp info_ptr)
michael@0 93 {
michael@0 94 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
michael@0 95 int keep;
michael@0 96 #endif
michael@0 97
michael@0 98 png_debug(1, "in png_read_info");
michael@0 99
michael@0 100 if (png_ptr == NULL || info_ptr == NULL)
michael@0 101 return;
michael@0 102
michael@0 103 /* Read and check the PNG file signature. */
michael@0 104 png_read_sig(png_ptr, info_ptr);
michael@0 105
michael@0 106 for (;;)
michael@0 107 {
michael@0 108 png_uint_32 length = png_read_chunk_header(png_ptr);
michael@0 109 png_uint_32 chunk_name = png_ptr->chunk_name;
michael@0 110
michael@0 111 /* IDAT logic needs to happen here to simplify getting the two flags
michael@0 112 * right.
michael@0 113 */
michael@0 114 if (chunk_name == png_IDAT)
michael@0 115 {
michael@0 116 if (!(png_ptr->mode & PNG_HAVE_IHDR))
michael@0 117 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
michael@0 118
michael@0 119 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
michael@0 120 !(png_ptr->mode & PNG_HAVE_PLTE))
michael@0 121 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
michael@0 122
michael@0 123 else if (png_ptr->mode & PNG_AFTER_IDAT)
michael@0 124 png_chunk_benign_error(png_ptr, "Too many IDATs found");
michael@0 125
michael@0 126 png_ptr->mode |= PNG_HAVE_IDAT;
michael@0 127 }
michael@0 128
michael@0 129 else if (png_ptr->mode & PNG_HAVE_IDAT)
michael@0 130 png_ptr->mode |= PNG_AFTER_IDAT;
michael@0 131
michael@0 132 /* This should be a binary subdivision search or a hash for
michael@0 133 * matching the chunk name rather than a linear search.
michael@0 134 */
michael@0 135 if (chunk_name == png_IHDR)
michael@0 136 png_handle_IHDR(png_ptr, info_ptr, length);
michael@0 137
michael@0 138 else if (chunk_name == png_IEND)
michael@0 139 png_handle_IEND(png_ptr, info_ptr, length);
michael@0 140
michael@0 141 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
michael@0 142 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
michael@0 143 {
michael@0 144 png_handle_unknown(png_ptr, info_ptr, length, keep);
michael@0 145
michael@0 146 if (chunk_name == png_PLTE)
michael@0 147 png_ptr->mode |= PNG_HAVE_PLTE;
michael@0 148
michael@0 149 else if (chunk_name == png_IDAT)
michael@0 150 {
michael@0 151 png_ptr->idat_size = 0; /* It has been consumed */
michael@0 152 break;
michael@0 153 }
michael@0 154 }
michael@0 155 #endif
michael@0 156 else if (chunk_name == png_PLTE)
michael@0 157 png_handle_PLTE(png_ptr, info_ptr, length);
michael@0 158
michael@0 159 else if (chunk_name == png_IDAT)
michael@0 160 {
michael@0 161 #ifdef PNG_READ_APNG_SUPPORTED
michael@0 162 png_have_info(png_ptr, info_ptr);
michael@0 163 #endif
michael@0 164 png_ptr->idat_size = length;
michael@0 165 break;
michael@0 166 }
michael@0 167
michael@0 168 #ifdef PNG_READ_bKGD_SUPPORTED
michael@0 169 else if (chunk_name == png_bKGD)
michael@0 170 png_handle_bKGD(png_ptr, info_ptr, length);
michael@0 171 #endif
michael@0 172
michael@0 173 #ifdef PNG_READ_cHRM_SUPPORTED
michael@0 174 else if (chunk_name == png_cHRM)
michael@0 175 png_handle_cHRM(png_ptr, info_ptr, length);
michael@0 176 #endif
michael@0 177
michael@0 178 #ifdef PNG_READ_gAMA_SUPPORTED
michael@0 179 else if (chunk_name == png_gAMA)
michael@0 180 png_handle_gAMA(png_ptr, info_ptr, length);
michael@0 181 #endif
michael@0 182
michael@0 183 #ifdef PNG_READ_hIST_SUPPORTED
michael@0 184 else if (chunk_name == png_hIST)
michael@0 185 png_handle_hIST(png_ptr, info_ptr, length);
michael@0 186 #endif
michael@0 187
michael@0 188 #ifdef PNG_READ_oFFs_SUPPORTED
michael@0 189 else if (chunk_name == png_oFFs)
michael@0 190 png_handle_oFFs(png_ptr, info_ptr, length);
michael@0 191 #endif
michael@0 192
michael@0 193 #ifdef PNG_READ_pCAL_SUPPORTED
michael@0 194 else if (chunk_name == png_pCAL)
michael@0 195 png_handle_pCAL(png_ptr, info_ptr, length);
michael@0 196 #endif
michael@0 197
michael@0 198 #ifdef PNG_READ_sCAL_SUPPORTED
michael@0 199 else if (chunk_name == png_sCAL)
michael@0 200 png_handle_sCAL(png_ptr, info_ptr, length);
michael@0 201 #endif
michael@0 202
michael@0 203 #ifdef PNG_READ_pHYs_SUPPORTED
michael@0 204 else if (chunk_name == png_pHYs)
michael@0 205 png_handle_pHYs(png_ptr, info_ptr, length);
michael@0 206 #endif
michael@0 207
michael@0 208 #ifdef PNG_READ_sBIT_SUPPORTED
michael@0 209 else if (chunk_name == png_sBIT)
michael@0 210 png_handle_sBIT(png_ptr, info_ptr, length);
michael@0 211 #endif
michael@0 212
michael@0 213 #ifdef PNG_READ_sRGB_SUPPORTED
michael@0 214 else if (chunk_name == png_sRGB)
michael@0 215 png_handle_sRGB(png_ptr, info_ptr, length);
michael@0 216 #endif
michael@0 217
michael@0 218 #ifdef PNG_READ_iCCP_SUPPORTED
michael@0 219 else if (chunk_name == png_iCCP)
michael@0 220 png_handle_iCCP(png_ptr, info_ptr, length);
michael@0 221 #endif
michael@0 222
michael@0 223 #ifdef PNG_READ_sPLT_SUPPORTED
michael@0 224 else if (chunk_name == png_sPLT)
michael@0 225 png_handle_sPLT(png_ptr, info_ptr, length);
michael@0 226 #endif
michael@0 227
michael@0 228 #ifdef PNG_READ_tEXt_SUPPORTED
michael@0 229 else if (chunk_name == png_tEXt)
michael@0 230 png_handle_tEXt(png_ptr, info_ptr, length);
michael@0 231 #endif
michael@0 232
michael@0 233 #ifdef PNG_READ_tIME_SUPPORTED
michael@0 234 else if (chunk_name == png_tIME)
michael@0 235 png_handle_tIME(png_ptr, info_ptr, length);
michael@0 236 #endif
michael@0 237
michael@0 238 #ifdef PNG_READ_tRNS_SUPPORTED
michael@0 239 else if (chunk_name == png_tRNS)
michael@0 240 png_handle_tRNS(png_ptr, info_ptr, length);
michael@0 241 #endif
michael@0 242
michael@0 243 #ifdef PNG_READ_zTXt_SUPPORTED
michael@0 244 else if (chunk_name == png_zTXt)
michael@0 245 png_handle_zTXt(png_ptr, info_ptr, length);
michael@0 246 #endif
michael@0 247
michael@0 248 #ifdef PNG_READ_iTXt_SUPPORTED
michael@0 249 else if (chunk_name == png_iTXt)
michael@0 250 png_handle_iTXt(png_ptr, info_ptr, length);
michael@0 251 #endif
michael@0 252
michael@0 253 #ifdef PNG_READ_APNG_SUPPORTED
michael@0 254 else if (chunk_name == png_acTL)
michael@0 255 png_handle_acTL(png_ptr, info_ptr, length);
michael@0 256
michael@0 257 else if (chunk_name == png_fcTL)
michael@0 258 png_handle_fcTL(png_ptr, info_ptr, length);
michael@0 259
michael@0 260 else if (chunk_name == png_fdAT)
michael@0 261 png_handle_fdAT(png_ptr, info_ptr, length);
michael@0 262 #endif
michael@0 263
michael@0 264 else
michael@0 265 png_handle_unknown(png_ptr, info_ptr, length,
michael@0 266 PNG_HANDLE_CHUNK_AS_DEFAULT);
michael@0 267 }
michael@0 268 }
michael@0 269 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
michael@0 270
michael@0 271 #ifdef PNG_READ_APNG_SUPPORTED
michael@0 272 void PNGAPI
michael@0 273 png_read_frame_head(png_structp png_ptr, png_infop info_ptr)
michael@0 274 {
michael@0 275 png_byte have_chunk_after_DAT; /* after IDAT or after fdAT */
michael@0 276
michael@0 277 png_debug(0, "Reading frame head");
michael@0 278
michael@0 279 if (!(png_ptr->mode & PNG_HAVE_acTL))
michael@0 280 png_error(png_ptr, "attempt to png_read_frame_head() but "
michael@0 281 "no acTL present");
michael@0 282
michael@0 283 /* do nothing for the main IDAT */
michael@0 284 if (png_ptr->num_frames_read == 0)
michael@0 285 return;
michael@0 286
michael@0 287 png_read_reset(png_ptr);
michael@0 288 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
michael@0 289 png_ptr->mode &= ~PNG_HAVE_fcTL;
michael@0 290
michael@0 291 have_chunk_after_DAT = 0;
michael@0 292 for (;;)
michael@0 293 {
michael@0 294 png_uint_32 length = png_read_chunk_header(png_ptr);
michael@0 295
michael@0 296 if (png_ptr->chunk_name == png_IDAT)
michael@0 297 {
michael@0 298 /* discard trailing IDATs for the first frame */
michael@0 299 if (have_chunk_after_DAT || png_ptr->num_frames_read > 1)
michael@0 300 png_error(png_ptr, "png_read_frame_head(): out of place IDAT");
michael@0 301 png_crc_finish(png_ptr, length);
michael@0 302 }
michael@0 303
michael@0 304 else if (png_ptr->chunk_name == png_fcTL)
michael@0 305 {
michael@0 306 png_handle_fcTL(png_ptr, info_ptr, length);
michael@0 307 have_chunk_after_DAT = 1;
michael@0 308 }
michael@0 309
michael@0 310 else if (png_ptr->chunk_name == png_fdAT)
michael@0 311 {
michael@0 312 png_ensure_sequence_number(png_ptr, length);
michael@0 313
michael@0 314 /* discard trailing fdATs for frames other than the first */
michael@0 315 if (!have_chunk_after_DAT && png_ptr->num_frames_read > 1)
michael@0 316 png_crc_finish(png_ptr, length - 4);
michael@0 317 else if(png_ptr->mode & PNG_HAVE_fcTL)
michael@0 318 {
michael@0 319 png_ptr->idat_size = length - 4;
michael@0 320 png_ptr->mode |= PNG_HAVE_IDAT;
michael@0 321
michael@0 322 break;
michael@0 323 }
michael@0 324 else
michael@0 325 png_error(png_ptr, "png_read_frame_head(): out of place fdAT");
michael@0 326 }
michael@0 327 else
michael@0 328 {
michael@0 329 png_warning(png_ptr, "Skipped (ignored) a chunk "
michael@0 330 "between APNG chunks");
michael@0 331 png_crc_finish(png_ptr, length);
michael@0 332 }
michael@0 333 }
michael@0 334 }
michael@0 335 #endif /* PNG_READ_APNG_SUPPORTED */
michael@0 336
michael@0 337 /* Optional call to update the users info_ptr structure */
michael@0 338 void PNGAPI
michael@0 339 png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
michael@0 340 {
michael@0 341 png_debug(1, "in png_read_update_info");
michael@0 342
michael@0 343 if (png_ptr != NULL)
michael@0 344 {
michael@0 345 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
michael@0 346 {
michael@0 347 png_read_start_row(png_ptr);
michael@0 348
michael@0 349 # ifdef PNG_READ_TRANSFORMS_SUPPORTED
michael@0 350 png_read_transform_info(png_ptr, info_ptr);
michael@0 351 # else
michael@0 352 PNG_UNUSED(info_ptr)
michael@0 353 # endif
michael@0 354 }
michael@0 355
michael@0 356 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
michael@0 357 else
michael@0 358 png_app_error(png_ptr,
michael@0 359 "png_read_update_info/png_start_read_image: duplicate call");
michael@0 360 }
michael@0 361 }
michael@0 362
michael@0 363 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 364 /* Initialize palette, background, etc, after transformations
michael@0 365 * are set, but before any reading takes place. This allows
michael@0 366 * the user to obtain a gamma-corrected palette, for example.
michael@0 367 * If the user doesn't call this, we will do it ourselves.
michael@0 368 */
michael@0 369 void PNGAPI
michael@0 370 png_start_read_image(png_structrp png_ptr)
michael@0 371 {
michael@0 372 png_debug(1, "in png_start_read_image");
michael@0 373
michael@0 374 if (png_ptr != NULL)
michael@0 375 {
michael@0 376 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
michael@0 377 png_read_start_row(png_ptr);
michael@0 378
michael@0 379 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
michael@0 380 else
michael@0 381 png_app_error(png_ptr,
michael@0 382 "png_start_read_image/png_read_update_info: duplicate call");
michael@0 383 }
michael@0 384 }
michael@0 385 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
michael@0 386
michael@0 387 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 388 #ifdef PNG_MNG_FEATURES_SUPPORTED
michael@0 389 /* Undoes intrapixel differencing,
michael@0 390 * NOTE: this is apparently only supported in the 'sequential' reader.
michael@0 391 */
michael@0 392 static void
michael@0 393 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
michael@0 394 {
michael@0 395 png_debug(1, "in png_do_read_intrapixel");
michael@0 396
michael@0 397 if (
michael@0 398 (row_info->color_type & PNG_COLOR_MASK_COLOR))
michael@0 399 {
michael@0 400 int bytes_per_pixel;
michael@0 401 png_uint_32 row_width = row_info->width;
michael@0 402
michael@0 403 if (row_info->bit_depth == 8)
michael@0 404 {
michael@0 405 png_bytep rp;
michael@0 406 png_uint_32 i;
michael@0 407
michael@0 408 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
michael@0 409 bytes_per_pixel = 3;
michael@0 410
michael@0 411 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
michael@0 412 bytes_per_pixel = 4;
michael@0 413
michael@0 414 else
michael@0 415 return;
michael@0 416
michael@0 417 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
michael@0 418 {
michael@0 419 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
michael@0 420 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
michael@0 421 }
michael@0 422 }
michael@0 423 else if (row_info->bit_depth == 16)
michael@0 424 {
michael@0 425 png_bytep rp;
michael@0 426 png_uint_32 i;
michael@0 427
michael@0 428 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
michael@0 429 bytes_per_pixel = 6;
michael@0 430
michael@0 431 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
michael@0 432 bytes_per_pixel = 8;
michael@0 433
michael@0 434 else
michael@0 435 return;
michael@0 436
michael@0 437 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
michael@0 438 {
michael@0 439 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
michael@0 440 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
michael@0 441 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
michael@0 442 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
michael@0 443 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
michael@0 444 *(rp ) = (png_byte)((red >> 8) & 0xff);
michael@0 445 *(rp + 1) = (png_byte)(red & 0xff);
michael@0 446 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
michael@0 447 *(rp + 5) = (png_byte)(blue & 0xff);
michael@0 448 }
michael@0 449 }
michael@0 450 }
michael@0 451 }
michael@0 452 #endif /* PNG_MNG_FEATURES_SUPPORTED */
michael@0 453
michael@0 454 void PNGAPI
michael@0 455 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
michael@0 456 {
michael@0 457 png_row_info row_info;
michael@0 458
michael@0 459 if (png_ptr == NULL)
michael@0 460 return;
michael@0 461
michael@0 462 png_debug2(1, "in png_read_row (row %lu, pass %d)",
michael@0 463 (unsigned long)png_ptr->row_number, png_ptr->pass);
michael@0 464
michael@0 465 /* png_read_start_row sets the information (in particular iwidth) for this
michael@0 466 * interlace pass.
michael@0 467 */
michael@0 468 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
michael@0 469 png_read_start_row(png_ptr);
michael@0 470
michael@0 471 /* 1.5.6: row_info moved out of png_struct to a local here. */
michael@0 472 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
michael@0 473 row_info.color_type = png_ptr->color_type;
michael@0 474 row_info.bit_depth = png_ptr->bit_depth;
michael@0 475 row_info.channels = png_ptr->channels;
michael@0 476 row_info.pixel_depth = png_ptr->pixel_depth;
michael@0 477 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
michael@0 478
michael@0 479 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
michael@0 480 {
michael@0 481 /* Check for transforms that have been set but were defined out */
michael@0 482 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
michael@0 483 if (png_ptr->transformations & PNG_INVERT_MONO)
michael@0 484 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
michael@0 485 #endif
michael@0 486
michael@0 487 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
michael@0 488 if (png_ptr->transformations & PNG_FILLER)
michael@0 489 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
michael@0 490 #endif
michael@0 491
michael@0 492 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
michael@0 493 !defined(PNG_READ_PACKSWAP_SUPPORTED)
michael@0 494 if (png_ptr->transformations & PNG_PACKSWAP)
michael@0 495 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
michael@0 496 #endif
michael@0 497
michael@0 498 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
michael@0 499 if (png_ptr->transformations & PNG_PACK)
michael@0 500 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
michael@0 501 #endif
michael@0 502
michael@0 503 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
michael@0 504 if (png_ptr->transformations & PNG_SHIFT)
michael@0 505 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
michael@0 506 #endif
michael@0 507
michael@0 508 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
michael@0 509 if (png_ptr->transformations & PNG_BGR)
michael@0 510 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
michael@0 511 #endif
michael@0 512
michael@0 513 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
michael@0 514 if (png_ptr->transformations & PNG_SWAP_BYTES)
michael@0 515 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
michael@0 516 #endif
michael@0 517 }
michael@0 518
michael@0 519 #ifdef PNG_READ_INTERLACING_SUPPORTED
michael@0 520 /* If interlaced and we do not need a new row, combine row and return.
michael@0 521 * Notice that the pixels we have from previous rows have been transformed
michael@0 522 * already; we can only combine like with like (transformed or
michael@0 523 * untransformed) and, because of the libpng API for interlaced images, this
michael@0 524 * means we must transform before de-interlacing.
michael@0 525 */
michael@0 526 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
michael@0 527 {
michael@0 528 switch (png_ptr->pass)
michael@0 529 {
michael@0 530 case 0:
michael@0 531 if (png_ptr->row_number & 0x07)
michael@0 532 {
michael@0 533 if (dsp_row != NULL)
michael@0 534 png_combine_row(png_ptr, dsp_row, 1/*display*/);
michael@0 535 png_read_finish_row(png_ptr);
michael@0 536 return;
michael@0 537 }
michael@0 538 break;
michael@0 539
michael@0 540 case 1:
michael@0 541 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
michael@0 542 {
michael@0 543 if (dsp_row != NULL)
michael@0 544 png_combine_row(png_ptr, dsp_row, 1/*display*/);
michael@0 545
michael@0 546 png_read_finish_row(png_ptr);
michael@0 547 return;
michael@0 548 }
michael@0 549 break;
michael@0 550
michael@0 551 case 2:
michael@0 552 if ((png_ptr->row_number & 0x07) != 4)
michael@0 553 {
michael@0 554 if (dsp_row != NULL && (png_ptr->row_number & 4))
michael@0 555 png_combine_row(png_ptr, dsp_row, 1/*display*/);
michael@0 556
michael@0 557 png_read_finish_row(png_ptr);
michael@0 558 return;
michael@0 559 }
michael@0 560 break;
michael@0 561
michael@0 562 case 3:
michael@0 563 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
michael@0 564 {
michael@0 565 if (dsp_row != NULL)
michael@0 566 png_combine_row(png_ptr, dsp_row, 1/*display*/);
michael@0 567
michael@0 568 png_read_finish_row(png_ptr);
michael@0 569 return;
michael@0 570 }
michael@0 571 break;
michael@0 572
michael@0 573 case 4:
michael@0 574 if ((png_ptr->row_number & 3) != 2)
michael@0 575 {
michael@0 576 if (dsp_row != NULL && (png_ptr->row_number & 2))
michael@0 577 png_combine_row(png_ptr, dsp_row, 1/*display*/);
michael@0 578
michael@0 579 png_read_finish_row(png_ptr);
michael@0 580 return;
michael@0 581 }
michael@0 582 break;
michael@0 583
michael@0 584 case 5:
michael@0 585 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
michael@0 586 {
michael@0 587 if (dsp_row != NULL)
michael@0 588 png_combine_row(png_ptr, dsp_row, 1/*display*/);
michael@0 589
michael@0 590 png_read_finish_row(png_ptr);
michael@0 591 return;
michael@0 592 }
michael@0 593 break;
michael@0 594
michael@0 595 default:
michael@0 596 case 6:
michael@0 597 if (!(png_ptr->row_number & 1))
michael@0 598 {
michael@0 599 png_read_finish_row(png_ptr);
michael@0 600 return;
michael@0 601 }
michael@0 602 break;
michael@0 603 }
michael@0 604 }
michael@0 605 #endif
michael@0 606
michael@0 607 if (!(png_ptr->mode & PNG_HAVE_IDAT))
michael@0 608 png_error(png_ptr, "Invalid attempt to read row data");
michael@0 609
michael@0 610 /* Fill the row with IDAT data: */
michael@0 611 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
michael@0 612
michael@0 613 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
michael@0 614 {
michael@0 615 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
michael@0 616 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
michael@0 617 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
michael@0 618 else
michael@0 619 png_error(png_ptr, "bad adaptive filter value");
michael@0 620 }
michael@0 621
michael@0 622 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
michael@0 623 * 1.5.6, while the buffer really is this big in current versions of libpng
michael@0 624 * it may not be in the future, so this was changed just to copy the
michael@0 625 * interlaced count:
michael@0 626 */
michael@0 627 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
michael@0 628
michael@0 629 #ifdef PNG_MNG_FEATURES_SUPPORTED
michael@0 630 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
michael@0 631 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
michael@0 632 {
michael@0 633 /* Intrapixel differencing */
michael@0 634 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
michael@0 635 }
michael@0 636 #endif
michael@0 637
michael@0 638 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
michael@0 639 if (png_ptr->transformations)
michael@0 640 png_do_read_transformations(png_ptr, &row_info);
michael@0 641 #endif
michael@0 642
michael@0 643 /* The transformed pixel depth should match the depth now in row_info. */
michael@0 644 if (png_ptr->transformed_pixel_depth == 0)
michael@0 645 {
michael@0 646 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
michael@0 647 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
michael@0 648 png_error(png_ptr, "sequential row overflow");
michael@0 649 }
michael@0 650
michael@0 651 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
michael@0 652 png_error(png_ptr, "internal sequential row size calculation error");
michael@0 653
michael@0 654 #ifdef PNG_READ_INTERLACING_SUPPORTED
michael@0 655 /* Blow up interlaced rows to full size */
michael@0 656 if (png_ptr->interlaced &&
michael@0 657 (png_ptr->transformations & PNG_INTERLACE))
michael@0 658 {
michael@0 659 if (png_ptr->pass < 6)
michael@0 660 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
michael@0 661 png_ptr->transformations);
michael@0 662
michael@0 663 if (dsp_row != NULL)
michael@0 664 png_combine_row(png_ptr, dsp_row, 1/*display*/);
michael@0 665
michael@0 666 if (row != NULL)
michael@0 667 png_combine_row(png_ptr, row, 0/*row*/);
michael@0 668 }
michael@0 669
michael@0 670 else
michael@0 671 #endif
michael@0 672 {
michael@0 673 if (row != NULL)
michael@0 674 png_combine_row(png_ptr, row, -1/*ignored*/);
michael@0 675
michael@0 676 if (dsp_row != NULL)
michael@0 677 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
michael@0 678 }
michael@0 679 png_read_finish_row(png_ptr);
michael@0 680
michael@0 681 if (png_ptr->read_row_fn != NULL)
michael@0 682 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
michael@0 683
michael@0 684 }
michael@0 685 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
michael@0 686
michael@0 687 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 688 /* Read one or more rows of image data. If the image is interlaced,
michael@0 689 * and png_set_interlace_handling() has been called, the rows need to
michael@0 690 * contain the contents of the rows from the previous pass. If the
michael@0 691 * image has alpha or transparency, and png_handle_alpha()[*] has been
michael@0 692 * called, the rows contents must be initialized to the contents of the
michael@0 693 * screen.
michael@0 694 *
michael@0 695 * "row" holds the actual image, and pixels are placed in it
michael@0 696 * as they arrive. If the image is displayed after each pass, it will
michael@0 697 * appear to "sparkle" in. "display_row" can be used to display a
michael@0 698 * "chunky" progressive image, with finer detail added as it becomes
michael@0 699 * available. If you do not want this "chunky" display, you may pass
michael@0 700 * NULL for display_row. If you do not want the sparkle display, and
michael@0 701 * you have not called png_handle_alpha(), you may pass NULL for rows.
michael@0 702 * If you have called png_handle_alpha(), and the image has either an
michael@0 703 * alpha channel or a transparency chunk, you must provide a buffer for
michael@0 704 * rows. In this case, you do not have to provide a display_row buffer
michael@0 705 * also, but you may. If the image is not interlaced, or if you have
michael@0 706 * not called png_set_interlace_handling(), the display_row buffer will
michael@0 707 * be ignored, so pass NULL to it.
michael@0 708 *
michael@0 709 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
michael@0 710 */
michael@0 711
michael@0 712 void PNGAPI
michael@0 713 png_read_rows(png_structrp png_ptr, png_bytepp row,
michael@0 714 png_bytepp display_row, png_uint_32 num_rows)
michael@0 715 {
michael@0 716 png_uint_32 i;
michael@0 717 png_bytepp rp;
michael@0 718 png_bytepp dp;
michael@0 719
michael@0 720 png_debug(1, "in png_read_rows");
michael@0 721
michael@0 722 if (png_ptr == NULL)
michael@0 723 return;
michael@0 724
michael@0 725 rp = row;
michael@0 726 dp = display_row;
michael@0 727 if (rp != NULL && dp != NULL)
michael@0 728 for (i = 0; i < num_rows; i++)
michael@0 729 {
michael@0 730 png_bytep rptr = *rp++;
michael@0 731 png_bytep dptr = *dp++;
michael@0 732
michael@0 733 png_read_row(png_ptr, rptr, dptr);
michael@0 734 }
michael@0 735
michael@0 736 else if (rp != NULL)
michael@0 737 for (i = 0; i < num_rows; i++)
michael@0 738 {
michael@0 739 png_bytep rptr = *rp;
michael@0 740 png_read_row(png_ptr, rptr, NULL);
michael@0 741 rp++;
michael@0 742 }
michael@0 743
michael@0 744 else if (dp != NULL)
michael@0 745 for (i = 0; i < num_rows; i++)
michael@0 746 {
michael@0 747 png_bytep dptr = *dp;
michael@0 748 png_read_row(png_ptr, NULL, dptr);
michael@0 749 dp++;
michael@0 750 }
michael@0 751 }
michael@0 752 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
michael@0 753
michael@0 754 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 755 /* Read the entire image. If the image has an alpha channel or a tRNS
michael@0 756 * chunk, and you have called png_handle_alpha()[*], you will need to
michael@0 757 * initialize the image to the current image that PNG will be overlaying.
michael@0 758 * We set the num_rows again here, in case it was incorrectly set in
michael@0 759 * png_read_start_row() by a call to png_read_update_info() or
michael@0 760 * png_start_read_image() if png_set_interlace_handling() wasn't called
michael@0 761 * prior to either of these functions like it should have been. You can
michael@0 762 * only call this function once. If you desire to have an image for
michael@0 763 * each pass of a interlaced image, use png_read_rows() instead.
michael@0 764 *
michael@0 765 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
michael@0 766 */
michael@0 767 void PNGAPI
michael@0 768 png_read_image(png_structrp png_ptr, png_bytepp image)
michael@0 769 {
michael@0 770 png_uint_32 i, image_height;
michael@0 771 int pass, j;
michael@0 772 png_bytepp rp;
michael@0 773
michael@0 774 png_debug(1, "in png_read_image");
michael@0 775
michael@0 776 if (png_ptr == NULL)
michael@0 777 return;
michael@0 778
michael@0 779 #ifdef PNG_READ_INTERLACING_SUPPORTED
michael@0 780 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
michael@0 781 {
michael@0 782 pass = png_set_interlace_handling(png_ptr);
michael@0 783 /* And make sure transforms are initialized. */
michael@0 784 png_start_read_image(png_ptr);
michael@0 785 }
michael@0 786 else
michael@0 787 {
michael@0 788 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
michael@0 789 {
michael@0 790 /* Caller called png_start_read_image or png_read_update_info without
michael@0 791 * first turning on the PNG_INTERLACE transform. We can fix this here,
michael@0 792 * but the caller should do it!
michael@0 793 */
michael@0 794 png_warning(png_ptr, "Interlace handling should be turned on when "
michael@0 795 "using png_read_image");
michael@0 796 /* Make sure this is set correctly */
michael@0 797 png_ptr->num_rows = png_ptr->height;
michael@0 798 }
michael@0 799
michael@0 800 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
michael@0 801 * the above error case.
michael@0 802 */
michael@0 803 pass = png_set_interlace_handling(png_ptr);
michael@0 804 }
michael@0 805 #else
michael@0 806 if (png_ptr->interlaced)
michael@0 807 png_error(png_ptr,
michael@0 808 "Cannot read interlaced image -- interlace handler disabled");
michael@0 809
michael@0 810 pass = 1;
michael@0 811 #endif
michael@0 812
michael@0 813 image_height=png_ptr->height;
michael@0 814
michael@0 815 for (j = 0; j < pass; j++)
michael@0 816 {
michael@0 817 rp = image;
michael@0 818 for (i = 0; i < image_height; i++)
michael@0 819 {
michael@0 820 png_read_row(png_ptr, *rp, NULL);
michael@0 821 rp++;
michael@0 822 }
michael@0 823 }
michael@0 824 }
michael@0 825 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
michael@0 826
michael@0 827 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 828 /* Read the end of the PNG file. Will not read past the end of the
michael@0 829 * file, will verify the end is accurate, and will read any comments
michael@0 830 * or time information at the end of the file, if info is not NULL.
michael@0 831 */
michael@0 832 void PNGAPI
michael@0 833 png_read_end(png_structrp png_ptr, png_inforp info_ptr)
michael@0 834 {
michael@0 835 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
michael@0 836 int keep;
michael@0 837 #endif
michael@0 838
michael@0 839 png_debug(1, "in png_read_end");
michael@0 840
michael@0 841 if (png_ptr == NULL)
michael@0 842 return;
michael@0 843
michael@0 844 /* If png_read_end is called in the middle of reading the rows there may
michael@0 845 * still be pending IDAT data and an owned zstream. Deal with this here.
michael@0 846 */
michael@0 847 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
michael@0 848 if (!png_chunk_unknown_handling(png_ptr, png_IDAT))
michael@0 849 #endif
michael@0 850 png_read_finish_IDAT(png_ptr);
michael@0 851
michael@0 852 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
michael@0 853 /* Report invalid palette index; added at libng-1.5.10 */
michael@0 854 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
michael@0 855 png_ptr->num_palette_max > png_ptr->num_palette)
michael@0 856 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
michael@0 857 #endif
michael@0 858
michael@0 859 do
michael@0 860 {
michael@0 861 png_uint_32 length = png_read_chunk_header(png_ptr);
michael@0 862 png_uint_32 chunk_name = png_ptr->chunk_name;
michael@0 863
michael@0 864 if (chunk_name == png_IEND)
michael@0 865 png_handle_IEND(png_ptr, info_ptr, length);
michael@0 866
michael@0 867 else if (chunk_name == png_IHDR)
michael@0 868 png_handle_IHDR(png_ptr, info_ptr, length);
michael@0 869
michael@0 870 else if (info_ptr == NULL)
michael@0 871 png_crc_finish(png_ptr, length);
michael@0 872
michael@0 873 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
michael@0 874 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
michael@0 875 {
michael@0 876 if (chunk_name == png_IDAT)
michael@0 877 {
michael@0 878 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
michael@0 879 png_benign_error(png_ptr, "Too many IDATs found");
michael@0 880 }
michael@0 881 png_handle_unknown(png_ptr, info_ptr, length, keep);
michael@0 882 if (chunk_name == png_PLTE)
michael@0 883 png_ptr->mode |= PNG_HAVE_PLTE;
michael@0 884 }
michael@0 885 #endif
michael@0 886
michael@0 887 else if (chunk_name == png_IDAT)
michael@0 888 {
michael@0 889 /* Zero length IDATs are legal after the last IDAT has been
michael@0 890 * read, but not after other chunks have been read.
michael@0 891 */
michael@0 892 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
michael@0 893 png_benign_error(png_ptr, "Too many IDATs found");
michael@0 894
michael@0 895 png_crc_finish(png_ptr, length);
michael@0 896 }
michael@0 897 else if (chunk_name == png_PLTE)
michael@0 898 png_handle_PLTE(png_ptr, info_ptr, length);
michael@0 899
michael@0 900 #ifdef PNG_READ_bKGD_SUPPORTED
michael@0 901 else if (chunk_name == png_bKGD)
michael@0 902 png_handle_bKGD(png_ptr, info_ptr, length);
michael@0 903 #endif
michael@0 904
michael@0 905 #ifdef PNG_READ_cHRM_SUPPORTED
michael@0 906 else if (chunk_name == png_cHRM)
michael@0 907 png_handle_cHRM(png_ptr, info_ptr, length);
michael@0 908 #endif
michael@0 909
michael@0 910 #ifdef PNG_READ_gAMA_SUPPORTED
michael@0 911 else if (chunk_name == png_gAMA)
michael@0 912 png_handle_gAMA(png_ptr, info_ptr, length);
michael@0 913 #endif
michael@0 914
michael@0 915 #ifdef PNG_READ_hIST_SUPPORTED
michael@0 916 else if (chunk_name == png_hIST)
michael@0 917 png_handle_hIST(png_ptr, info_ptr, length);
michael@0 918 #endif
michael@0 919
michael@0 920 #ifdef PNG_READ_oFFs_SUPPORTED
michael@0 921 else if (chunk_name == png_oFFs)
michael@0 922 png_handle_oFFs(png_ptr, info_ptr, length);
michael@0 923 #endif
michael@0 924
michael@0 925 #ifdef PNG_READ_pCAL_SUPPORTED
michael@0 926 else if (chunk_name == png_pCAL)
michael@0 927 png_handle_pCAL(png_ptr, info_ptr, length);
michael@0 928 #endif
michael@0 929
michael@0 930 #ifdef PNG_READ_sCAL_SUPPORTED
michael@0 931 else if (chunk_name == png_sCAL)
michael@0 932 png_handle_sCAL(png_ptr, info_ptr, length);
michael@0 933 #endif
michael@0 934
michael@0 935 #ifdef PNG_READ_pHYs_SUPPORTED
michael@0 936 else if (chunk_name == png_pHYs)
michael@0 937 png_handle_pHYs(png_ptr, info_ptr, length);
michael@0 938 #endif
michael@0 939
michael@0 940 #ifdef PNG_READ_sBIT_SUPPORTED
michael@0 941 else if (chunk_name == png_sBIT)
michael@0 942 png_handle_sBIT(png_ptr, info_ptr, length);
michael@0 943 #endif
michael@0 944
michael@0 945 #ifdef PNG_READ_sRGB_SUPPORTED
michael@0 946 else if (chunk_name == png_sRGB)
michael@0 947 png_handle_sRGB(png_ptr, info_ptr, length);
michael@0 948 #endif
michael@0 949
michael@0 950 #ifdef PNG_READ_iCCP_SUPPORTED
michael@0 951 else if (chunk_name == png_iCCP)
michael@0 952 png_handle_iCCP(png_ptr, info_ptr, length);
michael@0 953 #endif
michael@0 954
michael@0 955 #ifdef PNG_READ_sPLT_SUPPORTED
michael@0 956 else if (chunk_name == png_sPLT)
michael@0 957 png_handle_sPLT(png_ptr, info_ptr, length);
michael@0 958 #endif
michael@0 959
michael@0 960 #ifdef PNG_READ_tEXt_SUPPORTED
michael@0 961 else if (chunk_name == png_tEXt)
michael@0 962 png_handle_tEXt(png_ptr, info_ptr, length);
michael@0 963 #endif
michael@0 964
michael@0 965 #ifdef PNG_READ_tIME_SUPPORTED
michael@0 966 else if (chunk_name == png_tIME)
michael@0 967 png_handle_tIME(png_ptr, info_ptr, length);
michael@0 968 #endif
michael@0 969
michael@0 970 #ifdef PNG_READ_tRNS_SUPPORTED
michael@0 971 else if (chunk_name == png_tRNS)
michael@0 972 png_handle_tRNS(png_ptr, info_ptr, length);
michael@0 973 #endif
michael@0 974
michael@0 975 #ifdef PNG_READ_zTXt_SUPPORTED
michael@0 976 else if (chunk_name == png_zTXt)
michael@0 977 png_handle_zTXt(png_ptr, info_ptr, length);
michael@0 978 #endif
michael@0 979
michael@0 980 #ifdef PNG_READ_iTXt_SUPPORTED
michael@0 981 else if (chunk_name == png_iTXt)
michael@0 982 png_handle_iTXt(png_ptr, info_ptr, length);
michael@0 983 #endif
michael@0 984
michael@0 985 else
michael@0 986 png_handle_unknown(png_ptr, info_ptr, length,
michael@0 987 PNG_HANDLE_CHUNK_AS_DEFAULT);
michael@0 988 } while (!(png_ptr->mode & PNG_HAVE_IEND));
michael@0 989 }
michael@0 990 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
michael@0 991
michael@0 992 /* Free all memory used in the read struct */
michael@0 993 static void
michael@0 994 png_read_destroy(png_structrp png_ptr)
michael@0 995 {
michael@0 996 png_debug(1, "in png_read_destroy");
michael@0 997
michael@0 998 #ifdef PNG_READ_GAMMA_SUPPORTED
michael@0 999 png_destroy_gamma_table(png_ptr);
michael@0 1000 #endif
michael@0 1001
michael@0 1002 png_free(png_ptr, png_ptr->big_row_buf);
michael@0 1003 png_free(png_ptr, png_ptr->big_prev_row);
michael@0 1004 png_free(png_ptr, png_ptr->read_buffer);
michael@0 1005
michael@0 1006 #ifdef PNG_READ_QUANTIZE_SUPPORTED
michael@0 1007 png_free(png_ptr, png_ptr->palette_lookup);
michael@0 1008 png_free(png_ptr, png_ptr->quantize_index);
michael@0 1009 #endif
michael@0 1010
michael@0 1011 if (png_ptr->free_me & PNG_FREE_PLTE)
michael@0 1012 png_zfree(png_ptr, png_ptr->palette);
michael@0 1013 png_ptr->free_me &= ~PNG_FREE_PLTE;
michael@0 1014
michael@0 1015 #if defined(PNG_tRNS_SUPPORTED) || \
michael@0 1016 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
michael@0 1017 if (png_ptr->free_me & PNG_FREE_TRNS)
michael@0 1018 png_free(png_ptr, png_ptr->trans_alpha);
michael@0 1019 png_ptr->free_me &= ~PNG_FREE_TRNS;
michael@0 1020 #endif
michael@0 1021
michael@0 1022 inflateEnd(&png_ptr->zstream);
michael@0 1023
michael@0 1024 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
michael@0 1025 png_free(png_ptr, png_ptr->save_buffer);
michael@0 1026 #endif
michael@0 1027
michael@0 1028 #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\
michael@0 1029 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
michael@0 1030 png_free(png_ptr, png_ptr->unknown_chunk.data);
michael@0 1031 #endif
michael@0 1032
michael@0 1033 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
michael@0 1034 png_free(png_ptr, png_ptr->chunk_list);
michael@0 1035 #endif
michael@0 1036
michael@0 1037 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
michael@0 1038 * callbacks are still set at this point. They are required to complete the
michael@0 1039 * destruction of the png_struct itself.
michael@0 1040 */
michael@0 1041 }
michael@0 1042
michael@0 1043 /* Free all memory used by the read */
michael@0 1044 void PNGAPI
michael@0 1045 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
michael@0 1046 png_infopp end_info_ptr_ptr)
michael@0 1047 {
michael@0 1048 png_structrp png_ptr = NULL;
michael@0 1049
michael@0 1050 png_debug(1, "in png_destroy_read_struct");
michael@0 1051
michael@0 1052 if (png_ptr_ptr != NULL)
michael@0 1053 png_ptr = *png_ptr_ptr;
michael@0 1054
michael@0 1055 if (png_ptr == NULL)
michael@0 1056 return;
michael@0 1057
michael@0 1058 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
michael@0 1059 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
michael@0 1060 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
michael@0 1061 */
michael@0 1062 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
michael@0 1063 png_destroy_info_struct(png_ptr, info_ptr_ptr);
michael@0 1064
michael@0 1065 *png_ptr_ptr = NULL;
michael@0 1066 png_read_destroy(png_ptr);
michael@0 1067 png_destroy_png_struct(png_ptr);
michael@0 1068 }
michael@0 1069
michael@0 1070 void PNGAPI
michael@0 1071 png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
michael@0 1072 {
michael@0 1073 if (png_ptr == NULL)
michael@0 1074 return;
michael@0 1075
michael@0 1076 png_ptr->read_row_fn = read_row_fn;
michael@0 1077 }
michael@0 1078
michael@0 1079
michael@0 1080 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
michael@0 1081 #ifdef PNG_INFO_IMAGE_SUPPORTED
michael@0 1082 void PNGAPI
michael@0 1083 png_read_png(png_structrp png_ptr, png_inforp info_ptr,
michael@0 1084 int transforms,
michael@0 1085 voidp params)
michael@0 1086 {
michael@0 1087 if (png_ptr == NULL || info_ptr == NULL)
michael@0 1088 return;
michael@0 1089
michael@0 1090 /* png_read_info() gives us all of the information from the
michael@0 1091 * PNG file before the first IDAT (image data chunk).
michael@0 1092 */
michael@0 1093 png_read_info(png_ptr, info_ptr);
michael@0 1094 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
michael@0 1095 png_error(png_ptr, "Image is too high to process with png_read_png()");
michael@0 1096
michael@0 1097 /* -------------- image transformations start here ------------------- */
michael@0 1098 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
michael@0 1099 * is not implemented. This will only happen in de-configured (non-default)
michael@0 1100 * libpng builds. The results can be unexpected - png_read_png may return
michael@0 1101 * short or mal-formed rows because the transform is skipped.
michael@0 1102 */
michael@0 1103
michael@0 1104 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
michael@0 1105 */
michael@0 1106 if (transforms & PNG_TRANSFORM_SCALE_16)
michael@0 1107 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
michael@0 1108 * did in earlier versions, while "scale_16" is now more accurate.
michael@0 1109 */
michael@0 1110 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
michael@0 1111 png_set_scale_16(png_ptr);
michael@0 1112 #else
michael@0 1113 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
michael@0 1114 #endif
michael@0 1115
michael@0 1116 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
michael@0 1117 * latter by doing SCALE first. This is ok and allows apps not to check for
michael@0 1118 * which is supported to get the right answer.
michael@0 1119 */
michael@0 1120 if (transforms & PNG_TRANSFORM_STRIP_16)
michael@0 1121 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
michael@0 1122 png_set_strip_16(png_ptr);
michael@0 1123 #else
michael@0 1124 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
michael@0 1125 #endif
michael@0 1126
michael@0 1127 /* Strip alpha bytes from the input data without combining with
michael@0 1128 * the background (not recommended).
michael@0 1129 */
michael@0 1130 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
michael@0 1131 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
michael@0 1132 png_set_strip_alpha(png_ptr);
michael@0 1133 #else
michael@0 1134 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
michael@0 1135 #endif
michael@0 1136
michael@0 1137 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
michael@0 1138 * byte into separate bytes (useful for paletted and grayscale images).
michael@0 1139 */
michael@0 1140 if (transforms & PNG_TRANSFORM_PACKING)
michael@0 1141 #ifdef PNG_READ_PACK_SUPPORTED
michael@0 1142 png_set_packing(png_ptr);
michael@0 1143 #else
michael@0 1144 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
michael@0 1145 #endif
michael@0 1146
michael@0 1147 /* Change the order of packed pixels to least significant bit first
michael@0 1148 * (not useful if you are using png_set_packing).
michael@0 1149 */
michael@0 1150 if (transforms & PNG_TRANSFORM_PACKSWAP)
michael@0 1151 #ifdef PNG_READ_PACKSWAP_SUPPORTED
michael@0 1152 png_set_packswap(png_ptr);
michael@0 1153 #else
michael@0 1154 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
michael@0 1155 #endif
michael@0 1156
michael@0 1157 /* Expand paletted colors into true RGB triplets
michael@0 1158 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
michael@0 1159 * Expand paletted or RGB images with transparency to full alpha
michael@0 1160 * channels so the data will be available as RGBA quartets.
michael@0 1161 */
michael@0 1162 if (transforms & PNG_TRANSFORM_EXPAND)
michael@0 1163 #ifdef PNG_READ_EXPAND_SUPPORTED
michael@0 1164 png_set_expand(png_ptr);
michael@0 1165 #else
michael@0 1166 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
michael@0 1167 #endif
michael@0 1168
michael@0 1169 /* We don't handle background color or gamma transformation or quantizing.
michael@0 1170 */
michael@0 1171
michael@0 1172 /* Invert monochrome files to have 0 as white and 1 as black
michael@0 1173 */
michael@0 1174 if (transforms & PNG_TRANSFORM_INVERT_MONO)
michael@0 1175 #ifdef PNG_READ_INVERT_SUPPORTED
michael@0 1176 png_set_invert_mono(png_ptr);
michael@0 1177 #else
michael@0 1178 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
michael@0 1179 #endif
michael@0 1180
michael@0 1181 /* If you want to shift the pixel values from the range [0,255] or
michael@0 1182 * [0,65535] to the original [0,7] or [0,31], or whatever range the
michael@0 1183 * colors were originally in:
michael@0 1184 */
michael@0 1185 if (transforms & PNG_TRANSFORM_SHIFT)
michael@0 1186 #ifdef PNG_READ_SHIFT_SUPPORTED
michael@0 1187 if (info_ptr->valid & PNG_INFO_sBIT)
michael@0 1188 png_set_shift(png_ptr, &info_ptr->sig_bit);
michael@0 1189 #else
michael@0 1190 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
michael@0 1191 #endif
michael@0 1192
michael@0 1193 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
michael@0 1194 if (transforms & PNG_TRANSFORM_BGR)
michael@0 1195 #ifdef PNG_READ_BGR_SUPPORTED
michael@0 1196 png_set_bgr(png_ptr);
michael@0 1197 #else
michael@0 1198 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
michael@0 1199 #endif
michael@0 1200
michael@0 1201 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
michael@0 1202 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
michael@0 1203 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
michael@0 1204 png_set_swap_alpha(png_ptr);
michael@0 1205 #else
michael@0 1206 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
michael@0 1207 #endif
michael@0 1208
michael@0 1209 /* Swap bytes of 16-bit files to least significant byte first */
michael@0 1210 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
michael@0 1211 #ifdef PNG_READ_SWAP_SUPPORTED
michael@0 1212 png_set_swap(png_ptr);
michael@0 1213 #else
michael@0 1214 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
michael@0 1215 #endif
michael@0 1216
michael@0 1217 /* Added at libpng-1.2.41 */
michael@0 1218 /* Invert the alpha channel from opacity to transparency */
michael@0 1219 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
michael@0 1220 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
michael@0 1221 png_set_invert_alpha(png_ptr);
michael@0 1222 #else
michael@0 1223 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
michael@0 1224 #endif
michael@0 1225
michael@0 1226 /* Added at libpng-1.2.41 */
michael@0 1227 /* Expand grayscale image to RGB */
michael@0 1228 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
michael@0 1229 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
michael@0 1230 png_set_gray_to_rgb(png_ptr);
michael@0 1231 #else
michael@0 1232 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
michael@0 1233 #endif
michael@0 1234
michael@0 1235 /* Added at libpng-1.5.4 */
michael@0 1236 if (transforms & PNG_TRANSFORM_EXPAND_16)
michael@0 1237 #ifdef PNG_READ_EXPAND_16_SUPPORTED
michael@0 1238 png_set_expand_16(png_ptr);
michael@0 1239 #else
michael@0 1240 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
michael@0 1241 #endif
michael@0 1242
michael@0 1243 /* We don't handle adding filler bytes */
michael@0 1244
michael@0 1245 /* We use png_read_image and rely on that for interlace handling, but we also
michael@0 1246 * call png_read_update_info therefore must turn on interlace handling now:
michael@0 1247 */
michael@0 1248 (void)png_set_interlace_handling(png_ptr);
michael@0 1249
michael@0 1250 /* Optional call to gamma correct and add the background to the palette
michael@0 1251 * and update info structure. REQUIRED if you are expecting libpng to
michael@0 1252 * update the palette for you (i.e., you selected such a transform above).
michael@0 1253 */
michael@0 1254 png_read_update_info(png_ptr, info_ptr);
michael@0 1255
michael@0 1256 /* -------------- image transformations end here ------------------- */
michael@0 1257
michael@0 1258 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
michael@0 1259 if (info_ptr->row_pointers == NULL)
michael@0 1260 {
michael@0 1261 png_uint_32 iptr;
michael@0 1262
michael@0 1263 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
michael@0 1264 info_ptr->height * (sizeof (png_bytep))));
michael@0 1265
michael@0 1266 for (iptr=0; iptr<info_ptr->height; iptr++)
michael@0 1267 info_ptr->row_pointers[iptr] = NULL;
michael@0 1268
michael@0 1269 info_ptr->free_me |= PNG_FREE_ROWS;
michael@0 1270
michael@0 1271 for (iptr = 0; iptr < info_ptr->height; iptr++)
michael@0 1272 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
michael@0 1273 png_malloc(png_ptr, info_ptr->rowbytes));
michael@0 1274 }
michael@0 1275
michael@0 1276 png_read_image(png_ptr, info_ptr->row_pointers);
michael@0 1277 info_ptr->valid |= PNG_INFO_IDAT;
michael@0 1278
michael@0 1279 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
michael@0 1280 png_read_end(png_ptr, info_ptr);
michael@0 1281
michael@0 1282 PNG_UNUSED(params)
michael@0 1283 }
michael@0 1284 #endif /* PNG_INFO_IMAGE_SUPPORTED */
michael@0 1285 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
michael@0 1286
michael@0 1287 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
michael@0 1288 /* SIMPLIFIED READ
michael@0 1289 *
michael@0 1290 * This code currently relies on the sequential reader, though it could easily
michael@0 1291 * be made to work with the progressive one.
michael@0 1292 */
michael@0 1293 /* Arguments to png_image_finish_read: */
michael@0 1294
michael@0 1295 /* Encoding of PNG data (used by the color-map code) */
michael@0 1296 # define P_NOTSET 0 /* File encoding not yet known */
michael@0 1297 # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
michael@0 1298 # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
michael@0 1299 # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
michael@0 1300 # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
michael@0 1301
michael@0 1302 /* Color-map processing: after libpng has run on the PNG image further
michael@0 1303 * processing may be needed to conver the data to color-map indicies.
michael@0 1304 */
michael@0 1305 #define PNG_CMAP_NONE 0
michael@0 1306 #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
michael@0 1307 #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
michael@0 1308 #define PNG_CMAP_RGB 3 /* Process RGB data */
michael@0 1309 #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
michael@0 1310
michael@0 1311 /* The following document where the background is for each processing case. */
michael@0 1312 #define PNG_CMAP_NONE_BACKGROUND 256
michael@0 1313 #define PNG_CMAP_GA_BACKGROUND 231
michael@0 1314 #define PNG_CMAP_TRANS_BACKGROUND 254
michael@0 1315 #define PNG_CMAP_RGB_BACKGROUND 256
michael@0 1316 #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
michael@0 1317
michael@0 1318 typedef struct
michael@0 1319 {
michael@0 1320 /* Arguments: */
michael@0 1321 png_imagep image;
michael@0 1322 png_voidp buffer;
michael@0 1323 png_int_32 row_stride;
michael@0 1324 png_voidp colormap;
michael@0 1325 png_const_colorp background;
michael@0 1326 /* Local variables: */
michael@0 1327 png_voidp local_row;
michael@0 1328 png_voidp first_row;
michael@0 1329 ptrdiff_t row_bytes; /* step between rows */
michael@0 1330 int file_encoding; /* E_ values above */
michael@0 1331 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
michael@0 1332 int colormap_processing; /* PNG_CMAP_ values above */
michael@0 1333 } png_image_read_control;
michael@0 1334
michael@0 1335 /* Do all the *safe* initialization - 'safe' means that png_error won't be
michael@0 1336 * called, so setting up the jmp_buf is not required. This means that anything
michael@0 1337 * called from here must *not* call png_malloc - it has to call png_malloc_warn
michael@0 1338 * instead so that control is returned safely back to this routine.
michael@0 1339 */
michael@0 1340 static int
michael@0 1341 png_image_read_init(png_imagep image)
michael@0 1342 {
michael@0 1343 if (image->opaque == NULL)
michael@0 1344 {
michael@0 1345 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
michael@0 1346 png_safe_error, png_safe_warning);
michael@0 1347
michael@0 1348 /* And set the rest of the structure to NULL to ensure that the various
michael@0 1349 * fields are consistent.
michael@0 1350 */
michael@0 1351 memset(image, 0, (sizeof *image));
michael@0 1352 image->version = PNG_IMAGE_VERSION;
michael@0 1353
michael@0 1354 if (png_ptr != NULL)
michael@0 1355 {
michael@0 1356 png_infop info_ptr = png_create_info_struct(png_ptr);
michael@0 1357
michael@0 1358 if (info_ptr != NULL)
michael@0 1359 {
michael@0 1360 png_controlp control = png_voidcast(png_controlp,
michael@0 1361 png_malloc_warn(png_ptr, (sizeof *control)));
michael@0 1362
michael@0 1363 if (control != NULL)
michael@0 1364 {
michael@0 1365 memset(control, 0, (sizeof *control));
michael@0 1366
michael@0 1367 control->png_ptr = png_ptr;
michael@0 1368 control->info_ptr = info_ptr;
michael@0 1369 control->for_write = 0;
michael@0 1370
michael@0 1371 image->opaque = control;
michael@0 1372 return 1;
michael@0 1373 }
michael@0 1374
michael@0 1375 /* Error clean up */
michael@0 1376 png_destroy_info_struct(png_ptr, &info_ptr);
michael@0 1377 }
michael@0 1378
michael@0 1379 png_destroy_read_struct(&png_ptr, NULL, NULL);
michael@0 1380 }
michael@0 1381
michael@0 1382 return png_image_error(image, "png_image_read: out of memory");
michael@0 1383 }
michael@0 1384
michael@0 1385 return png_image_error(image, "png_image_read: opaque pointer not NULL");
michael@0 1386 }
michael@0 1387
michael@0 1388 /* Utility to find the base format of a PNG file from a png_struct. */
michael@0 1389 static png_uint_32
michael@0 1390 png_image_format(png_structrp png_ptr)
michael@0 1391 {
michael@0 1392 png_uint_32 format = 0;
michael@0 1393
michael@0 1394 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
michael@0 1395 format |= PNG_FORMAT_FLAG_COLOR;
michael@0 1396
michael@0 1397 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
michael@0 1398 format |= PNG_FORMAT_FLAG_ALPHA;
michael@0 1399
michael@0 1400 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
michael@0 1401 * sets the png_struct fields; that's all we are interested in here. The
michael@0 1402 * precise interaction with an app call to png_set_tRNS and PNG file reading
michael@0 1403 * is unclear.
michael@0 1404 */
michael@0 1405 else if (png_ptr->num_trans > 0)
michael@0 1406 format |= PNG_FORMAT_FLAG_ALPHA;
michael@0 1407
michael@0 1408 if (png_ptr->bit_depth == 16)
michael@0 1409 format |= PNG_FORMAT_FLAG_LINEAR;
michael@0 1410
michael@0 1411 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
michael@0 1412 format |= PNG_FORMAT_FLAG_COLORMAP;
michael@0 1413
michael@0 1414 return format;
michael@0 1415 }
michael@0 1416
michael@0 1417 /* Is the given gamma significantly different from sRGB? The test is the same
michael@0 1418 * one used in pngrtran.c when deciding whether to do gamma correction. The
michael@0 1419 * arithmetic optimizes the division by using the fact that the inverse of the
michael@0 1420 * file sRGB gamma is 2.2
michael@0 1421 */
michael@0 1422 static int
michael@0 1423 png_gamma_not_sRGB(png_fixed_point g)
michael@0 1424 {
michael@0 1425 if (g < PNG_FP_1)
michael@0 1426 {
michael@0 1427 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
michael@0 1428 if (g == 0)
michael@0 1429 return 0;
michael@0 1430
michael@0 1431 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
michael@0 1432 }
michael@0 1433
michael@0 1434 return 1;
michael@0 1435 }
michael@0 1436
michael@0 1437 /* Do the main body of a 'png_image_begin_read' function; read the PNG file
michael@0 1438 * header and fill in all the information. This is executed in a safe context,
michael@0 1439 * unlike the init routine above.
michael@0 1440 */
michael@0 1441 static int
michael@0 1442 png_image_read_header(png_voidp argument)
michael@0 1443 {
michael@0 1444 png_imagep image = png_voidcast(png_imagep, argument);
michael@0 1445 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 1446 png_inforp info_ptr = image->opaque->info_ptr;
michael@0 1447
michael@0 1448 png_set_benign_errors(png_ptr, 1/*warn*/);
michael@0 1449 png_read_info(png_ptr, info_ptr);
michael@0 1450
michael@0 1451 /* Do this the fast way; just read directly out of png_struct. */
michael@0 1452 image->width = png_ptr->width;
michael@0 1453 image->height = png_ptr->height;
michael@0 1454
michael@0 1455 {
michael@0 1456 png_uint_32 format = png_image_format(png_ptr);
michael@0 1457
michael@0 1458 image->format = format;
michael@0 1459
michael@0 1460 #ifdef PNG_COLORSPACE_SUPPORTED
michael@0 1461 /* Does the colorspace match sRGB? If there is no color endpoint
michael@0 1462 * (colorant) information assume yes, otherwise require the
michael@0 1463 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
michael@0 1464 * colorspace has been determined to be invalid ignore it.
michael@0 1465 */
michael@0 1466 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
michael@0 1467 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
michael@0 1468 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
michael@0 1469 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
michael@0 1470 #endif
michael@0 1471 }
michael@0 1472
michael@0 1473 /* We need the maximum number of entries regardless of the format the
michael@0 1474 * application sets here.
michael@0 1475 */
michael@0 1476 {
michael@0 1477 png_uint_32 cmap_entries;
michael@0 1478
michael@0 1479 switch (png_ptr->color_type)
michael@0 1480 {
michael@0 1481 case PNG_COLOR_TYPE_GRAY:
michael@0 1482 cmap_entries = 1U << png_ptr->bit_depth;
michael@0 1483 break;
michael@0 1484
michael@0 1485 case PNG_COLOR_TYPE_PALETTE:
michael@0 1486 cmap_entries = png_ptr->num_palette;
michael@0 1487 break;
michael@0 1488
michael@0 1489 default:
michael@0 1490 cmap_entries = 256;
michael@0 1491 break;
michael@0 1492 }
michael@0 1493
michael@0 1494 if (cmap_entries > 256)
michael@0 1495 cmap_entries = 256;
michael@0 1496
michael@0 1497 image->colormap_entries = cmap_entries;
michael@0 1498 }
michael@0 1499
michael@0 1500 return 1;
michael@0 1501 }
michael@0 1502
michael@0 1503 #ifdef PNG_STDIO_SUPPORTED
michael@0 1504 int PNGAPI
michael@0 1505 png_image_begin_read_from_stdio(png_imagep image, FILE* file)
michael@0 1506 {
michael@0 1507 if (image != NULL && image->version == PNG_IMAGE_VERSION)
michael@0 1508 {
michael@0 1509 if (file != NULL)
michael@0 1510 {
michael@0 1511 if (png_image_read_init(image))
michael@0 1512 {
michael@0 1513 /* This is slightly evil, but png_init_io doesn't do anything other
michael@0 1514 * than this and we haven't changed the standard IO functions so
michael@0 1515 * this saves a 'safe' function.
michael@0 1516 */
michael@0 1517 image->opaque->png_ptr->io_ptr = file;
michael@0 1518 return png_safe_execute(image, png_image_read_header, image);
michael@0 1519 }
michael@0 1520 }
michael@0 1521
michael@0 1522 else
michael@0 1523 return png_image_error(image,
michael@0 1524 "png_image_begin_read_from_stdio: invalid argument");
michael@0 1525 }
michael@0 1526
michael@0 1527 else if (image != NULL)
michael@0 1528 return png_image_error(image,
michael@0 1529 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
michael@0 1530
michael@0 1531 return 0;
michael@0 1532 }
michael@0 1533
michael@0 1534 int PNGAPI
michael@0 1535 png_image_begin_read_from_file(png_imagep image, const char *file_name)
michael@0 1536 {
michael@0 1537 if (image != NULL && image->version == PNG_IMAGE_VERSION)
michael@0 1538 {
michael@0 1539 if (file_name != NULL)
michael@0 1540 {
michael@0 1541 FILE *fp = fopen(file_name, "rb");
michael@0 1542
michael@0 1543 if (fp != NULL)
michael@0 1544 {
michael@0 1545 if (png_image_read_init(image))
michael@0 1546 {
michael@0 1547 image->opaque->png_ptr->io_ptr = fp;
michael@0 1548 image->opaque->owned_file = 1;
michael@0 1549 return png_safe_execute(image, png_image_read_header, image);
michael@0 1550 }
michael@0 1551
michael@0 1552 /* Clean up: just the opened file. */
michael@0 1553 (void)fclose(fp);
michael@0 1554 }
michael@0 1555
michael@0 1556 else
michael@0 1557 return png_image_error(image, strerror(errno));
michael@0 1558 }
michael@0 1559
michael@0 1560 else
michael@0 1561 return png_image_error(image,
michael@0 1562 "png_image_begin_read_from_file: invalid argument");
michael@0 1563 }
michael@0 1564
michael@0 1565 else if (image != NULL)
michael@0 1566 return png_image_error(image,
michael@0 1567 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
michael@0 1568
michael@0 1569 return 0;
michael@0 1570 }
michael@0 1571 #endif /* PNG_STDIO_SUPPORTED */
michael@0 1572
michael@0 1573 static void PNGCBAPI
michael@0 1574 png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
michael@0 1575 {
michael@0 1576 if (png_ptr != NULL)
michael@0 1577 {
michael@0 1578 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
michael@0 1579 if (image != NULL)
michael@0 1580 {
michael@0 1581 png_controlp cp = image->opaque;
michael@0 1582 if (cp != NULL)
michael@0 1583 {
michael@0 1584 png_const_bytep memory = cp->memory;
michael@0 1585 png_size_t size = cp->size;
michael@0 1586
michael@0 1587 if (memory != NULL && size >= need)
michael@0 1588 {
michael@0 1589 memcpy(out, memory, need);
michael@0 1590 cp->memory = memory + need;
michael@0 1591 cp->size = size - need;
michael@0 1592 return;
michael@0 1593 }
michael@0 1594
michael@0 1595 png_error(png_ptr, "read beyond end of data");
michael@0 1596 }
michael@0 1597 }
michael@0 1598
michael@0 1599 png_error(png_ptr, "invalid memory read");
michael@0 1600 }
michael@0 1601 }
michael@0 1602
michael@0 1603 int PNGAPI png_image_begin_read_from_memory(png_imagep image,
michael@0 1604 png_const_voidp memory, png_size_t size)
michael@0 1605 {
michael@0 1606 if (image != NULL && image->version == PNG_IMAGE_VERSION)
michael@0 1607 {
michael@0 1608 if (memory != NULL && size > 0)
michael@0 1609 {
michael@0 1610 if (png_image_read_init(image))
michael@0 1611 {
michael@0 1612 /* Now set the IO functions to read from the memory buffer and
michael@0 1613 * store it into io_ptr. Again do this in-place to avoid calling a
michael@0 1614 * libpng function that requires error handling.
michael@0 1615 */
michael@0 1616 image->opaque->memory = png_voidcast(png_const_bytep, memory);
michael@0 1617 image->opaque->size = size;
michael@0 1618 image->opaque->png_ptr->io_ptr = image;
michael@0 1619 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
michael@0 1620
michael@0 1621 return png_safe_execute(image, png_image_read_header, image);
michael@0 1622 }
michael@0 1623 }
michael@0 1624
michael@0 1625 else
michael@0 1626 return png_image_error(image,
michael@0 1627 "png_image_begin_read_from_memory: invalid argument");
michael@0 1628 }
michael@0 1629
michael@0 1630 else if (image != NULL)
michael@0 1631 return png_image_error(image,
michael@0 1632 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
michael@0 1633
michael@0 1634 return 0;
michael@0 1635 }
michael@0 1636
michael@0 1637 /* Utility function to skip chunks that are not used by the simplified image
michael@0 1638 * read functions and an appropriate macro to call it.
michael@0 1639 */
michael@0 1640 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
michael@0 1641 static void
michael@0 1642 png_image_skip_unused_chunks(png_structrp png_ptr)
michael@0 1643 {
michael@0 1644 /* Prepare the reader to ignore all recognized chunks whose data will not
michael@0 1645 * be used, i.e., all chunks recognized by libpng except for those
michael@0 1646 * involved in basic image reading:
michael@0 1647 *
michael@0 1648 * IHDR, PLTE, IDAT, IEND
michael@0 1649 *
michael@0 1650 * Or image data handling:
michael@0 1651 *
michael@0 1652 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
michael@0 1653 *
michael@0 1654 * This provides a small performance improvement and eliminates any
michael@0 1655 * potential vulnerability to security problems in the unused chunks.
michael@0 1656 *
michael@0 1657 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
michael@0 1658 * too. This allows the simplified API to be compiled without iCCP support,
michael@0 1659 * however if the support is there the chunk is still checked to detect
michael@0 1660 * errors (which are unfortunately quite common.)
michael@0 1661 */
michael@0 1662 {
michael@0 1663 static PNG_CONST png_byte chunks_to_process[] = {
michael@0 1664 98, 75, 71, 68, '\0', /* bKGD */
michael@0 1665 99, 72, 82, 77, '\0', /* cHRM */
michael@0 1666 103, 65, 77, 65, '\0', /* gAMA */
michael@0 1667 # ifdef PNG_READ_iCCP_SUPPORTED
michael@0 1668 105, 67, 67, 80, '\0', /* iCCP */
michael@0 1669 # endif
michael@0 1670 115, 66, 73, 84, '\0', /* sBIT */
michael@0 1671 115, 82, 71, 66, '\0', /* sRGB */
michael@0 1672 };
michael@0 1673
michael@0 1674 /* Ignore unknown chunks and all other chunks except for the
michael@0 1675 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
michael@0 1676 */
michael@0 1677 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
michael@0 1678 NULL, -1);
michael@0 1679
michael@0 1680 /* But do not ignore image data handling chunks */
michael@0 1681 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
michael@0 1682 chunks_to_process, (sizeof chunks_to_process)/5);
michael@0 1683 }
michael@0 1684 }
michael@0 1685
michael@0 1686 # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
michael@0 1687 #else
michael@0 1688 # define PNG_SKIP_CHUNKS(p) ((void)0)
michael@0 1689 #endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
michael@0 1690
michael@0 1691 /* The following macro gives the exact rounded answer for all values in the
michael@0 1692 * range 0..255 (it actually divides by 51.2, but the rounding still generates
michael@0 1693 * the correct numbers 0..5
michael@0 1694 */
michael@0 1695 #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
michael@0 1696
michael@0 1697 /* Utility functions to make particular color-maps */
michael@0 1698 static void
michael@0 1699 set_file_encoding(png_image_read_control *display)
michael@0 1700 {
michael@0 1701 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
michael@0 1702 if (png_gamma_significant(g))
michael@0 1703 {
michael@0 1704 if (png_gamma_not_sRGB(g))
michael@0 1705 {
michael@0 1706 display->file_encoding = P_FILE;
michael@0 1707 display->gamma_to_linear = png_reciprocal(g);
michael@0 1708 }
michael@0 1709
michael@0 1710 else
michael@0 1711 display->file_encoding = P_sRGB;
michael@0 1712 }
michael@0 1713
michael@0 1714 else
michael@0 1715 display->file_encoding = P_LINEAR8;
michael@0 1716 }
michael@0 1717
michael@0 1718 static unsigned int
michael@0 1719 decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
michael@0 1720 {
michael@0 1721 if (encoding == P_FILE) /* double check */
michael@0 1722 encoding = display->file_encoding;
michael@0 1723
michael@0 1724 if (encoding == P_NOTSET) /* must be the file encoding */
michael@0 1725 {
michael@0 1726 set_file_encoding(display);
michael@0 1727 encoding = display->file_encoding;
michael@0 1728 }
michael@0 1729
michael@0 1730 switch (encoding)
michael@0 1731 {
michael@0 1732 case P_FILE:
michael@0 1733 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
michael@0 1734 break;
michael@0 1735
michael@0 1736 case P_sRGB:
michael@0 1737 value = png_sRGB_table[value];
michael@0 1738 break;
michael@0 1739
michael@0 1740 case P_LINEAR:
michael@0 1741 break;
michael@0 1742
michael@0 1743 case P_LINEAR8:
michael@0 1744 value *= 257;
michael@0 1745 break;
michael@0 1746
michael@0 1747 default:
michael@0 1748 png_error(display->image->opaque->png_ptr,
michael@0 1749 "unexpected encoding (internal error)");
michael@0 1750 break;
michael@0 1751 }
michael@0 1752
michael@0 1753 return value;
michael@0 1754 }
michael@0 1755
michael@0 1756 static png_uint_32
michael@0 1757 png_colormap_compose(png_image_read_control *display,
michael@0 1758 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
michael@0 1759 png_uint_32 background, int encoding)
michael@0 1760 {
michael@0 1761 /* The file value is composed on the background, the background has the given
michael@0 1762 * encoding and so does the result, the file is encoded with P_FILE and the
michael@0 1763 * file and alpha are 8-bit values. The (output) encoding will always be
michael@0 1764 * P_LINEAR or P_sRGB.
michael@0 1765 */
michael@0 1766 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
michael@0 1767 png_uint_32 b = decode_gamma(display, background, encoding);
michael@0 1768
michael@0 1769 /* The alpha is always an 8-bit value (it comes from the palette), the value
michael@0 1770 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
michael@0 1771 */
michael@0 1772 f = f * alpha + b * (255-alpha);
michael@0 1773
michael@0 1774 if (encoding == P_LINEAR)
michael@0 1775 {
michael@0 1776 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
michael@0 1777 * accurate, it divides by 255.00000005937181414556, with no overflow.)
michael@0 1778 */
michael@0 1779 f *= 257; /* Now scaled by 65535 */
michael@0 1780 f += f >> 16;
michael@0 1781 f = (f+32768) >> 16;
michael@0 1782 }
michael@0 1783
michael@0 1784 else /* P_sRGB */
michael@0 1785 f = PNG_sRGB_FROM_LINEAR(f);
michael@0 1786
michael@0 1787 return f;
michael@0 1788 }
michael@0 1789
michael@0 1790 /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
michael@0 1791 * be 8-bit.
michael@0 1792 */
michael@0 1793 static void
michael@0 1794 png_create_colormap_entry(png_image_read_control *display,
michael@0 1795 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
michael@0 1796 png_uint_32 alpha, int encoding)
michael@0 1797 {
michael@0 1798 png_imagep image = display->image;
michael@0 1799 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
michael@0 1800 P_LINEAR : P_sRGB;
michael@0 1801 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
michael@0 1802 (red != green || green != blue);
michael@0 1803
michael@0 1804 if (ip > 255)
michael@0 1805 png_error(image->opaque->png_ptr, "color-map index out of range");
michael@0 1806
michael@0 1807 /* Update the cache with whether the file gamma is significantly different
michael@0 1808 * from sRGB.
michael@0 1809 */
michael@0 1810 if (encoding == P_FILE)
michael@0 1811 {
michael@0 1812 if (display->file_encoding == P_NOTSET)
michael@0 1813 set_file_encoding(display);
michael@0 1814
michael@0 1815 /* Note that the cached value may be P_FILE too, but if it is then the
michael@0 1816 * gamma_to_linear member has been set.
michael@0 1817 */
michael@0 1818 encoding = display->file_encoding;
michael@0 1819 }
michael@0 1820
michael@0 1821 if (encoding == P_FILE)
michael@0 1822 {
michael@0 1823 png_fixed_point g = display->gamma_to_linear;
michael@0 1824
michael@0 1825 red = png_gamma_16bit_correct(red*257, g);
michael@0 1826 green = png_gamma_16bit_correct(green*257, g);
michael@0 1827 blue = png_gamma_16bit_correct(blue*257, g);
michael@0 1828
michael@0 1829 if (convert_to_Y || output_encoding == P_LINEAR)
michael@0 1830 {
michael@0 1831 alpha *= 257;
michael@0 1832 encoding = P_LINEAR;
michael@0 1833 }
michael@0 1834
michael@0 1835 else
michael@0 1836 {
michael@0 1837 red = PNG_sRGB_FROM_LINEAR(red * 255);
michael@0 1838 green = PNG_sRGB_FROM_LINEAR(green * 255);
michael@0 1839 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
michael@0 1840 encoding = P_sRGB;
michael@0 1841 }
michael@0 1842 }
michael@0 1843
michael@0 1844 else if (encoding == P_LINEAR8)
michael@0 1845 {
michael@0 1846 /* This encoding occurs quite frequently in test cases because PngSuite
michael@0 1847 * includes a gAMA 1.0 chunk with most images.
michael@0 1848 */
michael@0 1849 red *= 257;
michael@0 1850 green *= 257;
michael@0 1851 blue *= 257;
michael@0 1852 alpha *= 257;
michael@0 1853 encoding = P_LINEAR;
michael@0 1854 }
michael@0 1855
michael@0 1856 else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR))
michael@0 1857 {
michael@0 1858 /* The values are 8-bit sRGB values, but must be converted to 16-bit
michael@0 1859 * linear.
michael@0 1860 */
michael@0 1861 red = png_sRGB_table[red];
michael@0 1862 green = png_sRGB_table[green];
michael@0 1863 blue = png_sRGB_table[blue];
michael@0 1864 alpha *= 257;
michael@0 1865 encoding = P_LINEAR;
michael@0 1866 }
michael@0 1867
michael@0 1868 /* This is set if the color isn't gray but the output is. */
michael@0 1869 if (encoding == P_LINEAR)
michael@0 1870 {
michael@0 1871 if (convert_to_Y)
michael@0 1872 {
michael@0 1873 /* NOTE: these values are copied from png_do_rgb_to_gray */
michael@0 1874 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
michael@0 1875 (png_uint_32)2366 * blue;
michael@0 1876
michael@0 1877 if (output_encoding == P_LINEAR)
michael@0 1878 y = (y + 16384) >> 15;
michael@0 1879
michael@0 1880 else
michael@0 1881 {
michael@0 1882 /* y is scaled by 32768, we need it scaled by 255: */
michael@0 1883 y = (y + 128) >> 8;
michael@0 1884 y *= 255;
michael@0 1885 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
michael@0 1886 encoding = P_sRGB;
michael@0 1887 }
michael@0 1888
michael@0 1889 blue = red = green = y;
michael@0 1890 }
michael@0 1891
michael@0 1892 else if (output_encoding == P_sRGB)
michael@0 1893 {
michael@0 1894 red = PNG_sRGB_FROM_LINEAR(red * 255);
michael@0 1895 green = PNG_sRGB_FROM_LINEAR(green * 255);
michael@0 1896 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
michael@0 1897 alpha = PNG_DIV257(alpha);
michael@0 1898 encoding = P_sRGB;
michael@0 1899 }
michael@0 1900 }
michael@0 1901
michael@0 1902 if (encoding != output_encoding)
michael@0 1903 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
michael@0 1904
michael@0 1905 /* Store the value. */
michael@0 1906 {
michael@0 1907 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
michael@0 1908 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
michael@0 1909 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
michael@0 1910 # else
michael@0 1911 # define afirst 0
michael@0 1912 # endif
michael@0 1913 # ifdef PNG_FORMAT_BGR_SUPPORTED
michael@0 1914 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
michael@0 1915 # else
michael@0 1916 # define bgr 0
michael@0 1917 # endif
michael@0 1918
michael@0 1919 if (output_encoding == P_LINEAR)
michael@0 1920 {
michael@0 1921 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
michael@0 1922
michael@0 1923 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
michael@0 1924
michael@0 1925 /* The linear 16-bit values must be pre-multiplied by the alpha channel
michael@0 1926 * value, if less than 65535 (this is, effectively, composite on black
michael@0 1927 * if the alpha channel is removed.)
michael@0 1928 */
michael@0 1929 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
michael@0 1930 {
michael@0 1931 case 4:
michael@0 1932 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
michael@0 1933 /* FALL THROUGH */
michael@0 1934
michael@0 1935 case 3:
michael@0 1936 if (alpha < 65535)
michael@0 1937 {
michael@0 1938 if (alpha > 0)
michael@0 1939 {
michael@0 1940 blue = (blue * alpha + 32767U)/65535U;
michael@0 1941 green = (green * alpha + 32767U)/65535U;
michael@0 1942 red = (red * alpha + 32767U)/65535U;
michael@0 1943 }
michael@0 1944
michael@0 1945 else
michael@0 1946 red = green = blue = 0;
michael@0 1947 }
michael@0 1948 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
michael@0 1949 entry[afirst + 1] = (png_uint_16)green;
michael@0 1950 entry[afirst + bgr] = (png_uint_16)red;
michael@0 1951 break;
michael@0 1952
michael@0 1953 case 2:
michael@0 1954 entry[1 ^ afirst] = (png_uint_16)alpha;
michael@0 1955 /* FALL THROUGH */
michael@0 1956
michael@0 1957 case 1:
michael@0 1958 if (alpha < 65535)
michael@0 1959 {
michael@0 1960 if (alpha > 0)
michael@0 1961 green = (green * alpha + 32767U)/65535U;
michael@0 1962
michael@0 1963 else
michael@0 1964 green = 0;
michael@0 1965 }
michael@0 1966 entry[afirst] = (png_uint_16)green;
michael@0 1967 break;
michael@0 1968
michael@0 1969 default:
michael@0 1970 break;
michael@0 1971 }
michael@0 1972 }
michael@0 1973
michael@0 1974 else /* output encoding is P_sRGB */
michael@0 1975 {
michael@0 1976 png_bytep entry = png_voidcast(png_bytep, display->colormap);
michael@0 1977
michael@0 1978 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
michael@0 1979
michael@0 1980 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
michael@0 1981 {
michael@0 1982 case 4:
michael@0 1983 entry[afirst ? 0 : 3] = (png_byte)alpha;
michael@0 1984 case 3:
michael@0 1985 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
michael@0 1986 entry[afirst + 1] = (png_byte)green;
michael@0 1987 entry[afirst + bgr] = (png_byte)red;
michael@0 1988 break;
michael@0 1989
michael@0 1990 case 2:
michael@0 1991 entry[1 ^ afirst] = (png_byte)alpha;
michael@0 1992 case 1:
michael@0 1993 entry[afirst] = (png_byte)green;
michael@0 1994 break;
michael@0 1995
michael@0 1996 default:
michael@0 1997 break;
michael@0 1998 }
michael@0 1999 }
michael@0 2000
michael@0 2001 # ifdef afirst
michael@0 2002 # undef afirst
michael@0 2003 # endif
michael@0 2004 # ifdef bgr
michael@0 2005 # undef bgr
michael@0 2006 # endif
michael@0 2007 }
michael@0 2008 }
michael@0 2009
michael@0 2010 static int
michael@0 2011 make_gray_file_colormap(png_image_read_control *display)
michael@0 2012 {
michael@0 2013 unsigned int i;
michael@0 2014
michael@0 2015 for (i=0; i<256; ++i)
michael@0 2016 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
michael@0 2017
michael@0 2018 return i;
michael@0 2019 }
michael@0 2020
michael@0 2021 static int
michael@0 2022 make_gray_colormap(png_image_read_control *display)
michael@0 2023 {
michael@0 2024 unsigned int i;
michael@0 2025
michael@0 2026 for (i=0; i<256; ++i)
michael@0 2027 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
michael@0 2028
michael@0 2029 return i;
michael@0 2030 }
michael@0 2031 #define PNG_GRAY_COLORMAP_ENTRIES 256
michael@0 2032
michael@0 2033 static int
michael@0 2034 make_ga_colormap(png_image_read_control *display)
michael@0 2035 {
michael@0 2036 unsigned int i, a;
michael@0 2037
michael@0 2038 /* Alpha is retained, the output will be a color-map with entries
michael@0 2039 * selected by six levels of alpha. One transparent entry, 6 gray
michael@0 2040 * levels for all the intermediate alpha values, leaving 230 entries
michael@0 2041 * for the opaque grays. The color-map entries are the six values
michael@0 2042 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
michael@0 2043 * relevant entry.
michael@0 2044 *
michael@0 2045 * if (alpha > 229) // opaque
michael@0 2046 * {
michael@0 2047 * // The 231 entries are selected to make the math below work:
michael@0 2048 * base = 0;
michael@0 2049 * entry = (231 * gray + 128) >> 8;
michael@0 2050 * }
michael@0 2051 * else if (alpha < 26) // transparent
michael@0 2052 * {
michael@0 2053 * base = 231;
michael@0 2054 * entry = 0;
michael@0 2055 * }
michael@0 2056 * else // partially opaque
michael@0 2057 * {
michael@0 2058 * base = 226 + 6 * PNG_DIV51(alpha);
michael@0 2059 * entry = PNG_DIV51(gray);
michael@0 2060 * }
michael@0 2061 */
michael@0 2062 i = 0;
michael@0 2063 while (i < 231)
michael@0 2064 {
michael@0 2065 unsigned int gray = (i * 256 + 115) / 231;
michael@0 2066 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
michael@0 2067 }
michael@0 2068
michael@0 2069 /* 255 is used here for the component values for consistency with the code
michael@0 2070 * that undoes premultiplication in pngwrite.c.
michael@0 2071 */
michael@0 2072 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
michael@0 2073
michael@0 2074 for (a=1; a<5; ++a)
michael@0 2075 {
michael@0 2076 unsigned int g;
michael@0 2077
michael@0 2078 for (g=0; g<6; ++g)
michael@0 2079 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
michael@0 2080 P_sRGB);
michael@0 2081 }
michael@0 2082
michael@0 2083 return i;
michael@0 2084 }
michael@0 2085
michael@0 2086 #define PNG_GA_COLORMAP_ENTRIES 256
michael@0 2087
michael@0 2088 static int
michael@0 2089 make_rgb_colormap(png_image_read_control *display)
michael@0 2090 {
michael@0 2091 unsigned int i, r;
michael@0 2092
michael@0 2093 /* Build a 6x6x6 opaque RGB cube */
michael@0 2094 for (i=r=0; r<6; ++r)
michael@0 2095 {
michael@0 2096 unsigned int g;
michael@0 2097
michael@0 2098 for (g=0; g<6; ++g)
michael@0 2099 {
michael@0 2100 unsigned int b;
michael@0 2101
michael@0 2102 for (b=0; b<6; ++b)
michael@0 2103 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
michael@0 2104 P_sRGB);
michael@0 2105 }
michael@0 2106 }
michael@0 2107
michael@0 2108 return i;
michael@0 2109 }
michael@0 2110
michael@0 2111 #define PNG_RGB_COLORMAP_ENTRIES 216
michael@0 2112
michael@0 2113 /* Return a palette index to the above palette given three 8-bit sRGB values. */
michael@0 2114 #define PNG_RGB_INDEX(r,g,b) \
michael@0 2115 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
michael@0 2116
michael@0 2117 static int
michael@0 2118 png_image_read_colormap(png_voidp argument)
michael@0 2119 {
michael@0 2120 png_image_read_control *display =
michael@0 2121 png_voidcast(png_image_read_control*, argument);
michael@0 2122 const png_imagep image = display->image;
michael@0 2123
michael@0 2124 const png_structrp png_ptr = image->opaque->png_ptr;
michael@0 2125 const png_uint_32 output_format = image->format;
michael@0 2126 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
michael@0 2127 P_LINEAR : P_sRGB;
michael@0 2128
michael@0 2129 unsigned int cmap_entries;
michael@0 2130 unsigned int output_processing; /* Output processing option */
michael@0 2131 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
michael@0 2132
michael@0 2133 /* Background information; the background color and the index of this color
michael@0 2134 * in the color-map if it exists (else 256).
michael@0 2135 */
michael@0 2136 unsigned int background_index = 256;
michael@0 2137 png_uint_32 back_r, back_g, back_b;
michael@0 2138
michael@0 2139 /* Flags to accumulate things that need to be done to the input. */
michael@0 2140 int expand_tRNS = 0;
michael@0 2141
michael@0 2142 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
michael@0 2143 * very difficult to do, the results look awful, and it is difficult to see
michael@0 2144 * what possible use it is because the application can't control the
michael@0 2145 * color-map.
michael@0 2146 */
michael@0 2147 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
michael@0 2148 png_ptr->num_trans > 0) /* alpha in input */ &&
michael@0 2149 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
michael@0 2150 {
michael@0 2151 if (output_encoding == P_LINEAR) /* compose on black */
michael@0 2152 back_b = back_g = back_r = 0;
michael@0 2153
michael@0 2154 else if (display->background == NULL /* no way to remove it */)
michael@0 2155 png_error(png_ptr,
michael@0 2156 "a background color must be supplied to remove alpha/transparency");
michael@0 2157
michael@0 2158 /* Get a copy of the background color (this avoids repeating the checks
michael@0 2159 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
michael@0 2160 * output format.
michael@0 2161 */
michael@0 2162 else
michael@0 2163 {
michael@0 2164 back_g = display->background->green;
michael@0 2165 if (output_format & PNG_FORMAT_FLAG_COLOR)
michael@0 2166 {
michael@0 2167 back_r = display->background->red;
michael@0 2168 back_b = display->background->blue;
michael@0 2169 }
michael@0 2170 else
michael@0 2171 back_b = back_r = back_g;
michael@0 2172 }
michael@0 2173 }
michael@0 2174
michael@0 2175 else if (output_encoding == P_LINEAR)
michael@0 2176 back_b = back_r = back_g = 65535;
michael@0 2177
michael@0 2178 else
michael@0 2179 back_b = back_r = back_g = 255;
michael@0 2180
michael@0 2181 /* Default the input file gamma if required - this is necessary because
michael@0 2182 * libpng assumes that if no gamma information is present the data is in the
michael@0 2183 * output format, but the simplified API deduces the gamma from the input
michael@0 2184 * format.
michael@0 2185 */
michael@0 2186 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
michael@0 2187 {
michael@0 2188 /* Do this directly, not using the png_colorspace functions, to ensure
michael@0 2189 * that it happens even if the colorspace is invalid (though probably if
michael@0 2190 * it is the setting will be ignored) Note that the same thing can be
michael@0 2191 * achieved at the application interface with png_set_gAMA.
michael@0 2192 */
michael@0 2193 if (png_ptr->bit_depth == 16 &&
michael@0 2194 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
michael@0 2195 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
michael@0 2196
michael@0 2197 else
michael@0 2198 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
michael@0 2199
michael@0 2200 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
michael@0 2201 }
michael@0 2202
michael@0 2203 /* Decide what to do based on the PNG color type of the input data. The
michael@0 2204 * utility function png_create_colormap_entry deals with most aspects of the
michael@0 2205 * output transformations; this code works out how to produce bytes of
michael@0 2206 * color-map entries from the original format.
michael@0 2207 */
michael@0 2208 switch (png_ptr->color_type)
michael@0 2209 {
michael@0 2210 case PNG_COLOR_TYPE_GRAY:
michael@0 2211 if (png_ptr->bit_depth <= 8)
michael@0 2212 {
michael@0 2213 /* There at most 256 colors in the output, regardless of
michael@0 2214 * transparency.
michael@0 2215 */
michael@0 2216 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
michael@0 2217
michael@0 2218 cmap_entries = 1U << png_ptr->bit_depth;
michael@0 2219 if (cmap_entries > image->colormap_entries)
michael@0 2220 png_error(png_ptr, "gray[8] color-map: too few entries");
michael@0 2221
michael@0 2222 step = 255 / (cmap_entries - 1);
michael@0 2223 output_processing = PNG_CMAP_NONE;
michael@0 2224
michael@0 2225 /* If there is a tRNS chunk then this either selects a transparent
michael@0 2226 * value or, if the output has no alpha, the background color.
michael@0 2227 */
michael@0 2228 if (png_ptr->num_trans > 0)
michael@0 2229 {
michael@0 2230 trans = png_ptr->trans_color.gray;
michael@0 2231
michael@0 2232 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
michael@0 2233 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
michael@0 2234 }
michael@0 2235
michael@0 2236 /* png_create_colormap_entry just takes an RGBA and writes the
michael@0 2237 * corresponding color-map entry using the format from 'image',
michael@0 2238 * including the required conversion to sRGB or linear as
michael@0 2239 * appropriate. The input values are always either sRGB (if the
michael@0 2240 * gamma correction flag is 0) or 0..255 scaled file encoded values
michael@0 2241 * (if the function must gamma correct them).
michael@0 2242 */
michael@0 2243 for (i=val=0; i<cmap_entries; ++i, val += step)
michael@0 2244 {
michael@0 2245 /* 'i' is a file value. While this will result in duplicated
michael@0 2246 * entries for 8-bit non-sRGB encoded files it is necessary to
michael@0 2247 * have non-gamma corrected values to do tRNS handling.
michael@0 2248 */
michael@0 2249 if (i != trans)
michael@0 2250 png_create_colormap_entry(display, i, val, val, val, 255,
michael@0 2251 P_FILE/*8-bit with file gamma*/);
michael@0 2252
michael@0 2253 /* Else this entry is transparent. The colors don't matter if
michael@0 2254 * there is an alpha channel (back_alpha == 0), but it does no
michael@0 2255 * harm to pass them in; the values are not set above so this
michael@0 2256 * passes in white.
michael@0 2257 *
michael@0 2258 * NOTE: this preserves the full precision of the application
michael@0 2259 * supplied background color when it is used.
michael@0 2260 */
michael@0 2261 else
michael@0 2262 png_create_colormap_entry(display, i, back_r, back_g, back_b,
michael@0 2263 back_alpha, output_encoding);
michael@0 2264 }
michael@0 2265
michael@0 2266 /* We need libpng to preserve the original encoding. */
michael@0 2267 data_encoding = P_FILE;
michael@0 2268
michael@0 2269 /* The rows from libpng, while technically gray values, are now also
michael@0 2270 * color-map indicies; however, they may need to be expanded to 1
michael@0 2271 * byte per pixel. This is what png_set_packing does (i.e., it
michael@0 2272 * unpacks the bit values into bytes.)
michael@0 2273 */
michael@0 2274 if (png_ptr->bit_depth < 8)
michael@0 2275 png_set_packing(png_ptr);
michael@0 2276 }
michael@0 2277
michael@0 2278 else /* bit depth is 16 */
michael@0 2279 {
michael@0 2280 /* The 16-bit input values can be converted directly to 8-bit gamma
michael@0 2281 * encoded values; however, if a tRNS chunk is present 257 color-map
michael@0 2282 * entries are required. This means that the extra entry requires
michael@0 2283 * special processing; add an alpha channel, sacrifice gray level
michael@0 2284 * 254 and convert transparent (alpha==0) entries to that.
michael@0 2285 *
michael@0 2286 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
michael@0 2287 * same time to minimize quality loss. If a tRNS chunk is present
michael@0 2288 * this means libpng must handle it too; otherwise it is impossible
michael@0 2289 * to do the exact match on the 16-bit value.
michael@0 2290 *
michael@0 2291 * If the output has no alpha channel *and* the background color is
michael@0 2292 * gray then it is possible to let libpng handle the substitution by
michael@0 2293 * ensuring that the corresponding gray level matches the background
michael@0 2294 * color exactly.
michael@0 2295 */
michael@0 2296 data_encoding = P_sRGB;
michael@0 2297
michael@0 2298 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
michael@0 2299 png_error(png_ptr, "gray[16] color-map: too few entries");
michael@0 2300
michael@0 2301 cmap_entries = make_gray_colormap(display);
michael@0 2302
michael@0 2303 if (png_ptr->num_trans > 0)
michael@0 2304 {
michael@0 2305 unsigned int back_alpha;
michael@0 2306
michael@0 2307 if (output_format & PNG_FORMAT_FLAG_ALPHA)
michael@0 2308 back_alpha = 0;
michael@0 2309
michael@0 2310 else
michael@0 2311 {
michael@0 2312 if (back_r == back_g && back_g == back_b)
michael@0 2313 {
michael@0 2314 /* Background is gray; no special processing will be
michael@0 2315 * required.
michael@0 2316 */
michael@0 2317 png_color_16 c;
michael@0 2318 png_uint_32 gray = back_g;
michael@0 2319
michael@0 2320 if (output_encoding == P_LINEAR)
michael@0 2321 {
michael@0 2322 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
michael@0 2323
michael@0 2324 /* And make sure the corresponding palette entry
michael@0 2325 * matches.
michael@0 2326 */
michael@0 2327 png_create_colormap_entry(display, gray, back_g, back_g,
michael@0 2328 back_g, 65535, P_LINEAR);
michael@0 2329 }
michael@0 2330
michael@0 2331 /* The background passed to libpng, however, must be the
michael@0 2332 * sRGB value.
michael@0 2333 */
michael@0 2334 c.index = 0; /*unused*/
michael@0 2335 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
michael@0 2336
michael@0 2337 /* NOTE: does this work without expanding tRNS to alpha?
michael@0 2338 * It should be the color->gray case below apparently
michael@0 2339 * doesn't.
michael@0 2340 */
michael@0 2341 png_set_background_fixed(png_ptr, &c,
michael@0 2342 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
michael@0 2343 0/*gamma: not used*/);
michael@0 2344
michael@0 2345 output_processing = PNG_CMAP_NONE;
michael@0 2346 break;
michael@0 2347 }
michael@0 2348
michael@0 2349 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
michael@0 2350 }
michael@0 2351
michael@0 2352 /* output_processing means that the libpng-processed row will be
michael@0 2353 * 8-bit GA and it has to be processing to single byte color-map
michael@0 2354 * values. Entry 254 is replaced by either a completely
michael@0 2355 * transparent entry or by the background color at full
michael@0 2356 * precision (and the background color is not a simple gray leve
michael@0 2357 * in this case.)
michael@0 2358 */
michael@0 2359 expand_tRNS = 1;
michael@0 2360 output_processing = PNG_CMAP_TRANS;
michael@0 2361 background_index = 254;
michael@0 2362
michael@0 2363 /* And set (overwrite) color-map entry 254 to the actual
michael@0 2364 * background color at full precision.
michael@0 2365 */
michael@0 2366 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
michael@0 2367 back_alpha, output_encoding);
michael@0 2368 }
michael@0 2369
michael@0 2370 else
michael@0 2371 output_processing = PNG_CMAP_NONE;
michael@0 2372 }
michael@0 2373 break;
michael@0 2374
michael@0 2375 case PNG_COLOR_TYPE_GRAY_ALPHA:
michael@0 2376 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
michael@0 2377 * of 65536 combinations. If, however, the alpha channel is to be
michael@0 2378 * removed there are only 256 possibilities if the background is gray.
michael@0 2379 * (Otherwise there is a subset of the 65536 possibilities defined by
michael@0 2380 * the triangle between black, white and the background color.)
michael@0 2381 *
michael@0 2382 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
michael@0 2383 * worry about tRNS matching - tRNS is ignored if there is an alpha
michael@0 2384 * channel.
michael@0 2385 */
michael@0 2386 data_encoding = P_sRGB;
michael@0 2387
michael@0 2388 if (output_format & PNG_FORMAT_FLAG_ALPHA)
michael@0 2389 {
michael@0 2390 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
michael@0 2391 png_error(png_ptr, "gray+alpha color-map: too few entries");
michael@0 2392
michael@0 2393 cmap_entries = make_ga_colormap(display);
michael@0 2394
michael@0 2395 background_index = PNG_CMAP_GA_BACKGROUND;
michael@0 2396 output_processing = PNG_CMAP_GA;
michael@0 2397 }
michael@0 2398
michael@0 2399 else /* alpha is removed */
michael@0 2400 {
michael@0 2401 /* Alpha must be removed as the PNG data is processed when the
michael@0 2402 * background is a color because the G and A channels are
michael@0 2403 * independent and the vector addition (non-parallel vectors) is a
michael@0 2404 * 2-D problem.
michael@0 2405 *
michael@0 2406 * This can be reduced to the same algorithm as above by making a
michael@0 2407 * colormap containing gray levels (for the opaque grays), a
michael@0 2408 * background entry (for a transparent pixel) and a set of four six
michael@0 2409 * level color values, one set for each intermediate alpha value.
michael@0 2410 * See the comments in make_ga_colormap for how this works in the
michael@0 2411 * per-pixel processing.
michael@0 2412 *
michael@0 2413 * If the background is gray, however, we only need a 256 entry gray
michael@0 2414 * level color map. It is sufficient to make the entry generated
michael@0 2415 * for the background color be exactly the color specified.
michael@0 2416 */
michael@0 2417 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
michael@0 2418 (back_r == back_g && back_g == back_b))
michael@0 2419 {
michael@0 2420 /* Background is gray; no special processing will be required. */
michael@0 2421 png_color_16 c;
michael@0 2422 png_uint_32 gray = back_g;
michael@0 2423
michael@0 2424 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
michael@0 2425 png_error(png_ptr, "gray-alpha color-map: too few entries");
michael@0 2426
michael@0 2427 cmap_entries = make_gray_colormap(display);
michael@0 2428
michael@0 2429 if (output_encoding == P_LINEAR)
michael@0 2430 {
michael@0 2431 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
michael@0 2432
michael@0 2433 /* And make sure the corresponding palette entry matches. */
michael@0 2434 png_create_colormap_entry(display, gray, back_g, back_g,
michael@0 2435 back_g, 65535, P_LINEAR);
michael@0 2436 }
michael@0 2437
michael@0 2438 /* The background passed to libpng, however, must be the sRGB
michael@0 2439 * value.
michael@0 2440 */
michael@0 2441 c.index = 0; /*unused*/
michael@0 2442 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
michael@0 2443
michael@0 2444 png_set_background_fixed(png_ptr, &c,
michael@0 2445 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
michael@0 2446 0/*gamma: not used*/);
michael@0 2447
michael@0 2448 output_processing = PNG_CMAP_NONE;
michael@0 2449 }
michael@0 2450
michael@0 2451 else
michael@0 2452 {
michael@0 2453 png_uint_32 i, a;
michael@0 2454
michael@0 2455 /* This is the same as png_make_ga_colormap, above, except that
michael@0 2456 * the entries are all opaque.
michael@0 2457 */
michael@0 2458 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
michael@0 2459 png_error(png_ptr, "ga-alpha color-map: too few entries");
michael@0 2460
michael@0 2461 i = 0;
michael@0 2462 while (i < 231)
michael@0 2463 {
michael@0 2464 png_uint_32 gray = (i * 256 + 115) / 231;
michael@0 2465 png_create_colormap_entry(display, i++, gray, gray, gray,
michael@0 2466 255, P_sRGB);
michael@0 2467 }
michael@0 2468
michael@0 2469 /* NOTE: this preserves the full precision of the application
michael@0 2470 * background color.
michael@0 2471 */
michael@0 2472 background_index = i;
michael@0 2473 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
michael@0 2474 output_encoding == P_LINEAR ? 65535U : 255U, output_encoding);
michael@0 2475
michael@0 2476 /* For non-opaque input composite on the sRGB background - this
michael@0 2477 * requires inverting the encoding for each component. The input
michael@0 2478 * is still converted to the sRGB encoding because this is a
michael@0 2479 * reasonable approximate to the logarithmic curve of human
michael@0 2480 * visual sensitivity, at least over the narrow range which PNG
michael@0 2481 * represents. Consequently 'G' is always sRGB encoded, while
michael@0 2482 * 'A' is linear. We need the linear background colors.
michael@0 2483 */
michael@0 2484 if (output_encoding == P_sRGB) /* else already linear */
michael@0 2485 {
michael@0 2486 /* This may produce a value not exactly matching the
michael@0 2487 * background, but that's ok because these numbers are only
michael@0 2488 * used when alpha != 0
michael@0 2489 */
michael@0 2490 back_r = png_sRGB_table[back_r];
michael@0 2491 back_g = png_sRGB_table[back_g];
michael@0 2492 back_b = png_sRGB_table[back_b];
michael@0 2493 }
michael@0 2494
michael@0 2495 for (a=1; a<5; ++a)
michael@0 2496 {
michael@0 2497 unsigned int g;
michael@0 2498
michael@0 2499 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
michael@0 2500 * by an 8-bit alpha value (0..255).
michael@0 2501 */
michael@0 2502 png_uint_32 alpha = 51 * a;
michael@0 2503 png_uint_32 back_rx = (255-alpha) * back_r;
michael@0 2504 png_uint_32 back_gx = (255-alpha) * back_g;
michael@0 2505 png_uint_32 back_bx = (255-alpha) * back_b;
michael@0 2506
michael@0 2507 for (g=0; g<6; ++g)
michael@0 2508 {
michael@0 2509 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
michael@0 2510
michael@0 2511 png_create_colormap_entry(display, i++,
michael@0 2512 PNG_sRGB_FROM_LINEAR(gray + back_rx),
michael@0 2513 PNG_sRGB_FROM_LINEAR(gray + back_gx),
michael@0 2514 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
michael@0 2515 }
michael@0 2516 }
michael@0 2517
michael@0 2518 cmap_entries = i;
michael@0 2519 output_processing = PNG_CMAP_GA;
michael@0 2520 }
michael@0 2521 }
michael@0 2522 break;
michael@0 2523
michael@0 2524 case PNG_COLOR_TYPE_RGB:
michael@0 2525 case PNG_COLOR_TYPE_RGB_ALPHA:
michael@0 2526 /* Exclude the case where the output is gray; we can always handle this
michael@0 2527 * with the cases above.
michael@0 2528 */
michael@0 2529 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
michael@0 2530 {
michael@0 2531 /* The color-map will be grayscale, so we may as well convert the
michael@0 2532 * input RGB values to a simple grayscale and use the grayscale
michael@0 2533 * code above.
michael@0 2534 *
michael@0 2535 * NOTE: calling this apparently damages the recognition of the
michael@0 2536 * transparent color in background color handling; call
michael@0 2537 * png_set_tRNS_to_alpha before png_set_background_fixed.
michael@0 2538 */
michael@0 2539 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
michael@0 2540 -1);
michael@0 2541 data_encoding = P_sRGB;
michael@0 2542
michael@0 2543 /* The output will now be one or two 8-bit gray or gray+alpha
michael@0 2544 * channels. The more complex case arises when the input has alpha.
michael@0 2545 */
michael@0 2546 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
michael@0 2547 png_ptr->num_trans > 0) &&
michael@0 2548 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
michael@0 2549 {
michael@0 2550 /* Both input and output have an alpha channel, so no background
michael@0 2551 * processing is required; just map the GA bytes to the right
michael@0 2552 * color-map entry.
michael@0 2553 */
michael@0 2554 expand_tRNS = 1;
michael@0 2555
michael@0 2556 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
michael@0 2557 png_error(png_ptr, "rgb[ga] color-map: too few entries");
michael@0 2558
michael@0 2559 cmap_entries = make_ga_colormap(display);
michael@0 2560 background_index = PNG_CMAP_GA_BACKGROUND;
michael@0 2561 output_processing = PNG_CMAP_GA;
michael@0 2562 }
michael@0 2563
michael@0 2564 else
michael@0 2565 {
michael@0 2566 /* Either the input or the output has no alpha channel, so there
michael@0 2567 * will be no non-opaque pixels in the color-map; it will just be
michael@0 2568 * grayscale.
michael@0 2569 */
michael@0 2570 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
michael@0 2571 png_error(png_ptr, "rgb[gray] color-map: too few entries");
michael@0 2572
michael@0 2573 /* Ideally this code would use libpng to do the gamma correction,
michael@0 2574 * but if an input alpha channel is to be removed we will hit the
michael@0 2575 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
michael@0 2576 * correction bug). Fix this by dropping the gamma correction in
michael@0 2577 * this case and doing it in the palette; this will result in
michael@0 2578 * duplicate palette entries, but that's better than the
michael@0 2579 * alternative of double gamma correction.
michael@0 2580 */
michael@0 2581 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
michael@0 2582 png_ptr->num_trans > 0) &&
michael@0 2583 png_gamma_not_sRGB(png_ptr->colorspace.gamma))
michael@0 2584 {
michael@0 2585 cmap_entries = make_gray_file_colormap(display);
michael@0 2586 data_encoding = P_FILE;
michael@0 2587 }
michael@0 2588
michael@0 2589 else
michael@0 2590 cmap_entries = make_gray_colormap(display);
michael@0 2591
michael@0 2592 /* But if the input has alpha or transparency it must be removed
michael@0 2593 */
michael@0 2594 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
michael@0 2595 png_ptr->num_trans > 0)
michael@0 2596 {
michael@0 2597 png_color_16 c;
michael@0 2598 png_uint_32 gray = back_g;
michael@0 2599
michael@0 2600 /* We need to ensure that the application background exists in
michael@0 2601 * the colormap and that completely transparent pixels map to
michael@0 2602 * it. Achieve this simply by ensuring that the entry
michael@0 2603 * selected for the background really is the background color.
michael@0 2604 */
michael@0 2605 if (data_encoding == P_FILE) /* from the fixup above */
michael@0 2606 {
michael@0 2607 /* The app supplied a gray which is in output_encoding, we
michael@0 2608 * need to convert it to a value of the input (P_FILE)
michael@0 2609 * encoding then set this palette entry to the required
michael@0 2610 * output encoding.
michael@0 2611 */
michael@0 2612 if (output_encoding == P_sRGB)
michael@0 2613 gray = png_sRGB_table[gray]; /* now P_LINEAR */
michael@0 2614
michael@0 2615 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
michael@0 2616 png_ptr->colorspace.gamma)); /* now P_FILE */
michael@0 2617
michael@0 2618 /* And make sure the corresponding palette entry contains
michael@0 2619 * exactly the required sRGB value.
michael@0 2620 */
michael@0 2621 png_create_colormap_entry(display, gray, back_g, back_g,
michael@0 2622 back_g, 0/*unused*/, output_encoding);
michael@0 2623 }
michael@0 2624
michael@0 2625 else if (output_encoding == P_LINEAR)
michael@0 2626 {
michael@0 2627 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
michael@0 2628
michael@0 2629 /* And make sure the corresponding palette entry matches.
michael@0 2630 */
michael@0 2631 png_create_colormap_entry(display, gray, back_g, back_g,
michael@0 2632 back_g, 0/*unused*/, P_LINEAR);
michael@0 2633 }
michael@0 2634
michael@0 2635 /* The background passed to libpng, however, must be the
michael@0 2636 * output (normally sRGB) value.
michael@0 2637 */
michael@0 2638 c.index = 0; /*unused*/
michael@0 2639 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
michael@0 2640
michael@0 2641 /* NOTE: the following is apparently a bug in libpng. Without
michael@0 2642 * it the transparent color recognition in
michael@0 2643 * png_set_background_fixed seems to go wrong.
michael@0 2644 */
michael@0 2645 expand_tRNS = 1;
michael@0 2646 png_set_background_fixed(png_ptr, &c,
michael@0 2647 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
michael@0 2648 0/*gamma: not used*/);
michael@0 2649 }
michael@0 2650
michael@0 2651 output_processing = PNG_CMAP_NONE;
michael@0 2652 }
michael@0 2653 }
michael@0 2654
michael@0 2655 else /* output is color */
michael@0 2656 {
michael@0 2657 /* We could use png_quantize here so long as there is no transparent
michael@0 2658 * color or alpha; png_quantize ignores alpha. Easier overall just
michael@0 2659 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
michael@0 2660 * Consequently we always want libpng to produce sRGB data.
michael@0 2661 */
michael@0 2662 data_encoding = P_sRGB;
michael@0 2663
michael@0 2664 /* Is there any transparency or alpha? */
michael@0 2665 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
michael@0 2666 png_ptr->num_trans > 0)
michael@0 2667 {
michael@0 2668 /* Is there alpha in the output too? If so all four channels are
michael@0 2669 * processed into a special RGB cube with alpha support.
michael@0 2670 */
michael@0 2671 if (output_format & PNG_FORMAT_FLAG_ALPHA)
michael@0 2672 {
michael@0 2673 png_uint_32 r;
michael@0 2674
michael@0 2675 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
michael@0 2676 png_error(png_ptr, "rgb+alpha color-map: too few entries");
michael@0 2677
michael@0 2678 cmap_entries = make_rgb_colormap(display);
michael@0 2679
michael@0 2680 /* Add a transparent entry. */
michael@0 2681 png_create_colormap_entry(display, cmap_entries, 255, 255,
michael@0 2682 255, 0, P_sRGB);
michael@0 2683
michael@0 2684 /* This is stored as the background index for the processing
michael@0 2685 * algorithm.
michael@0 2686 */
michael@0 2687 background_index = cmap_entries++;
michael@0 2688
michael@0 2689 /* Add 27 r,g,b entries each with alpha 0.5. */
michael@0 2690 for (r=0; r<256; r = (r << 1) | 0x7f)
michael@0 2691 {
michael@0 2692 png_uint_32 g;
michael@0 2693
michael@0 2694 for (g=0; g<256; g = (g << 1) | 0x7f)
michael@0 2695 {
michael@0 2696 png_uint_32 b;
michael@0 2697
michael@0 2698 /* This generates components with the values 0, 127 and
michael@0 2699 * 255
michael@0 2700 */
michael@0 2701 for (b=0; b<256; b = (b << 1) | 0x7f)
michael@0 2702 png_create_colormap_entry(display, cmap_entries++,
michael@0 2703 r, g, b, 128, P_sRGB);
michael@0 2704 }
michael@0 2705 }
michael@0 2706
michael@0 2707 expand_tRNS = 1;
michael@0 2708 output_processing = PNG_CMAP_RGB_ALPHA;
michael@0 2709 }
michael@0 2710
michael@0 2711 else
michael@0 2712 {
michael@0 2713 /* Alpha/transparency must be removed. The background must
michael@0 2714 * exist in the color map (achieved by setting adding it after
michael@0 2715 * the 666 color-map). If the standard processing code will
michael@0 2716 * pick up this entry automatically that's all that is
michael@0 2717 * required; libpng can be called to do the background
michael@0 2718 * processing.
michael@0 2719 */
michael@0 2720 unsigned int sample_size =
michael@0 2721 PNG_IMAGE_SAMPLE_SIZE(output_format);
michael@0 2722 png_uint_32 r, g, b; /* sRGB background */
michael@0 2723
michael@0 2724 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
michael@0 2725 png_error(png_ptr, "rgb-alpha color-map: too few entries");
michael@0 2726
michael@0 2727 cmap_entries = make_rgb_colormap(display);
michael@0 2728
michael@0 2729 png_create_colormap_entry(display, cmap_entries, back_r,
michael@0 2730 back_g, back_b, 0/*unused*/, output_encoding);
michael@0 2731
michael@0 2732 if (output_encoding == P_LINEAR)
michael@0 2733 {
michael@0 2734 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
michael@0 2735 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
michael@0 2736 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
michael@0 2737 }
michael@0 2738
michael@0 2739 else
michael@0 2740 {
michael@0 2741 r = back_r;
michael@0 2742 g = back_g;
michael@0 2743 b = back_g;
michael@0 2744 }
michael@0 2745
michael@0 2746 /* Compare the newly-created color-map entry with the one the
michael@0 2747 * PNG_CMAP_RGB algorithm will use. If the two entries don't
michael@0 2748 * match, add the new one and set this as the background
michael@0 2749 * index.
michael@0 2750 */
michael@0 2751 if (memcmp((png_const_bytep)display->colormap +
michael@0 2752 sample_size * cmap_entries,
michael@0 2753 (png_const_bytep)display->colormap +
michael@0 2754 sample_size * PNG_RGB_INDEX(r,g,b),
michael@0 2755 sample_size) != 0)
michael@0 2756 {
michael@0 2757 /* The background color must be added. */
michael@0 2758 background_index = cmap_entries++;
michael@0 2759
michael@0 2760 /* Add 27 r,g,b entries each with created by composing with
michael@0 2761 * the background at alpha 0.5.
michael@0 2762 */
michael@0 2763 for (r=0; r<256; r = (r << 1) | 0x7f)
michael@0 2764 {
michael@0 2765 for (g=0; g<256; g = (g << 1) | 0x7f)
michael@0 2766 {
michael@0 2767 /* This generates components with the values 0, 127
michael@0 2768 * and 255
michael@0 2769 */
michael@0 2770 for (b=0; b<256; b = (b << 1) | 0x7f)
michael@0 2771 png_create_colormap_entry(display, cmap_entries++,
michael@0 2772 png_colormap_compose(display, r, P_sRGB, 128,
michael@0 2773 back_r, output_encoding),
michael@0 2774 png_colormap_compose(display, g, P_sRGB, 128,
michael@0 2775 back_g, output_encoding),
michael@0 2776 png_colormap_compose(display, b, P_sRGB, 128,
michael@0 2777 back_b, output_encoding),
michael@0 2778 0/*unused*/, output_encoding);
michael@0 2779 }
michael@0 2780 }
michael@0 2781
michael@0 2782 expand_tRNS = 1;
michael@0 2783 output_processing = PNG_CMAP_RGB_ALPHA;
michael@0 2784 }
michael@0 2785
michael@0 2786 else /* background color is in the standard color-map */
michael@0 2787 {
michael@0 2788 png_color_16 c;
michael@0 2789
michael@0 2790 c.index = 0; /*unused*/
michael@0 2791 c.red = (png_uint_16)back_r;
michael@0 2792 c.gray = c.green = (png_uint_16)back_g;
michael@0 2793 c.blue = (png_uint_16)back_b;
michael@0 2794
michael@0 2795 png_set_background_fixed(png_ptr, &c,
michael@0 2796 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
michael@0 2797 0/*gamma: not used*/);
michael@0 2798
michael@0 2799 output_processing = PNG_CMAP_RGB;
michael@0 2800 }
michael@0 2801 }
michael@0 2802 }
michael@0 2803
michael@0 2804 else /* no alpha or transparency in the input */
michael@0 2805 {
michael@0 2806 /* Alpha in the output is irrelevant, simply map the opaque input
michael@0 2807 * pixels to the 6x6x6 color-map.
michael@0 2808 */
michael@0 2809 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
michael@0 2810 png_error(png_ptr, "rgb color-map: too few entries");
michael@0 2811
michael@0 2812 cmap_entries = make_rgb_colormap(display);
michael@0 2813 output_processing = PNG_CMAP_RGB;
michael@0 2814 }
michael@0 2815 }
michael@0 2816 break;
michael@0 2817
michael@0 2818 case PNG_COLOR_TYPE_PALETTE:
michael@0 2819 /* It's already got a color-map. It may be necessary to eliminate the
michael@0 2820 * tRNS entries though.
michael@0 2821 */
michael@0 2822 {
michael@0 2823 unsigned int num_trans = png_ptr->num_trans;
michael@0 2824 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
michael@0 2825 png_const_colorp colormap = png_ptr->palette;
michael@0 2826 const int do_background = trans != NULL &&
michael@0 2827 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
michael@0 2828 unsigned int i;
michael@0 2829
michael@0 2830 /* Just in case: */
michael@0 2831 if (trans == NULL)
michael@0 2832 num_trans = 0;
michael@0 2833
michael@0 2834 output_processing = PNG_CMAP_NONE;
michael@0 2835 data_encoding = P_FILE; /* Don't change from color-map indicies */
michael@0 2836 cmap_entries = png_ptr->num_palette;
michael@0 2837 if (cmap_entries > 256)
michael@0 2838 cmap_entries = 256;
michael@0 2839
michael@0 2840 if (cmap_entries > image->colormap_entries)
michael@0 2841 png_error(png_ptr, "palette color-map: too few entries");
michael@0 2842
michael@0 2843 for (i=0; i < cmap_entries; ++i)
michael@0 2844 {
michael@0 2845 if (do_background && i < num_trans && trans[i] < 255)
michael@0 2846 {
michael@0 2847 if (trans[i] == 0)
michael@0 2848 png_create_colormap_entry(display, i, back_r, back_g,
michael@0 2849 back_b, 0, output_encoding);
michael@0 2850
michael@0 2851 else
michael@0 2852 {
michael@0 2853 /* Must compose the PNG file color in the color-map entry
michael@0 2854 * on the sRGB color in 'back'.
michael@0 2855 */
michael@0 2856 png_create_colormap_entry(display, i,
michael@0 2857 png_colormap_compose(display, colormap[i].red, P_FILE,
michael@0 2858 trans[i], back_r, output_encoding),
michael@0 2859 png_colormap_compose(display, colormap[i].green, P_FILE,
michael@0 2860 trans[i], back_g, output_encoding),
michael@0 2861 png_colormap_compose(display, colormap[i].blue, P_FILE,
michael@0 2862 trans[i], back_b, output_encoding),
michael@0 2863 output_encoding == P_LINEAR ? trans[i] * 257U :
michael@0 2864 trans[i],
michael@0 2865 output_encoding);
michael@0 2866 }
michael@0 2867 }
michael@0 2868
michael@0 2869 else
michael@0 2870 png_create_colormap_entry(display, i, colormap[i].red,
michael@0 2871 colormap[i].green, colormap[i].blue,
michael@0 2872 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
michael@0 2873 }
michael@0 2874
michael@0 2875 /* The PNG data may have indicies packed in fewer than 8 bits, it
michael@0 2876 * must be expanded if so.
michael@0 2877 */
michael@0 2878 if (png_ptr->bit_depth < 8)
michael@0 2879 png_set_packing(png_ptr);
michael@0 2880 }
michael@0 2881 break;
michael@0 2882
michael@0 2883 default:
michael@0 2884 png_error(png_ptr, "invalid PNG color type");
michael@0 2885 /*NOT REACHED*/
michael@0 2886 break;
michael@0 2887 }
michael@0 2888
michael@0 2889 /* Now deal with the output processing */
michael@0 2890 if (expand_tRNS && png_ptr->num_trans > 0 &&
michael@0 2891 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
michael@0 2892 png_set_tRNS_to_alpha(png_ptr);
michael@0 2893
michael@0 2894 switch (data_encoding)
michael@0 2895 {
michael@0 2896 default:
michael@0 2897 png_error(png_ptr, "bad data option (internal error)");
michael@0 2898 break;
michael@0 2899
michael@0 2900 case P_sRGB:
michael@0 2901 /* Change to 8-bit sRGB */
michael@0 2902 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
michael@0 2903 /* FALL THROUGH */
michael@0 2904
michael@0 2905 case P_FILE:
michael@0 2906 if (png_ptr->bit_depth > 8)
michael@0 2907 png_set_scale_16(png_ptr);
michael@0 2908 break;
michael@0 2909 }
michael@0 2910
michael@0 2911 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
michael@0 2912 png_error(png_ptr, "color map overflow (BAD internal error)");
michael@0 2913
michael@0 2914 image->colormap_entries = cmap_entries;
michael@0 2915
michael@0 2916 /* Double check using the recorded background index */
michael@0 2917 switch (output_processing)
michael@0 2918 {
michael@0 2919 case PNG_CMAP_NONE:
michael@0 2920 if (background_index != PNG_CMAP_NONE_BACKGROUND)
michael@0 2921 goto bad_background;
michael@0 2922 break;
michael@0 2923
michael@0 2924 case PNG_CMAP_GA:
michael@0 2925 if (background_index != PNG_CMAP_GA_BACKGROUND)
michael@0 2926 goto bad_background;
michael@0 2927 break;
michael@0 2928
michael@0 2929 case PNG_CMAP_TRANS:
michael@0 2930 if (background_index >= cmap_entries ||
michael@0 2931 background_index != PNG_CMAP_TRANS_BACKGROUND)
michael@0 2932 goto bad_background;
michael@0 2933 break;
michael@0 2934
michael@0 2935 case PNG_CMAP_RGB:
michael@0 2936 if (background_index != PNG_CMAP_RGB_BACKGROUND)
michael@0 2937 goto bad_background;
michael@0 2938 break;
michael@0 2939
michael@0 2940 case PNG_CMAP_RGB_ALPHA:
michael@0 2941 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
michael@0 2942 goto bad_background;
michael@0 2943 break;
michael@0 2944
michael@0 2945 default:
michael@0 2946 png_error(png_ptr, "bad processing option (internal error)");
michael@0 2947
michael@0 2948 bad_background:
michael@0 2949 png_error(png_ptr, "bad background index (internal error)");
michael@0 2950 }
michael@0 2951
michael@0 2952 display->colormap_processing = output_processing;
michael@0 2953
michael@0 2954 return 1/*ok*/;
michael@0 2955 }
michael@0 2956
michael@0 2957 /* The final part of the color-map read called from png_image_finish_read. */
michael@0 2958 static int
michael@0 2959 png_image_read_and_map(png_voidp argument)
michael@0 2960 {
michael@0 2961 png_image_read_control *display = png_voidcast(png_image_read_control*,
michael@0 2962 argument);
michael@0 2963 png_imagep image = display->image;
michael@0 2964 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 2965 int passes;
michael@0 2966
michael@0 2967 /* Called when the libpng data must be transformed into the color-mapped
michael@0 2968 * form. There is a local row buffer in display->local and this routine must
michael@0 2969 * do the interlace handling.
michael@0 2970 */
michael@0 2971 switch (png_ptr->interlaced)
michael@0 2972 {
michael@0 2973 case PNG_INTERLACE_NONE:
michael@0 2974 passes = 1;
michael@0 2975 break;
michael@0 2976
michael@0 2977 case PNG_INTERLACE_ADAM7:
michael@0 2978 passes = PNG_INTERLACE_ADAM7_PASSES;
michael@0 2979 break;
michael@0 2980
michael@0 2981 default:
michael@0 2982 png_error(png_ptr, "unknown interlace type");
michael@0 2983 }
michael@0 2984
michael@0 2985 {
michael@0 2986 png_uint_32 height = image->height;
michael@0 2987 png_uint_32 width = image->width;
michael@0 2988 int proc = display->colormap_processing;
michael@0 2989 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
michael@0 2990 ptrdiff_t step_row = display->row_bytes;
michael@0 2991 int pass;
michael@0 2992
michael@0 2993 for (pass = 0; pass < passes; ++pass)
michael@0 2994 {
michael@0 2995 unsigned int startx, stepx, stepy;
michael@0 2996 png_uint_32 y;
michael@0 2997
michael@0 2998 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
michael@0 2999 {
michael@0 3000 /* The row may be empty for a short image: */
michael@0 3001 if (PNG_PASS_COLS(width, pass) == 0)
michael@0 3002 continue;
michael@0 3003
michael@0 3004 startx = PNG_PASS_START_COL(pass);
michael@0 3005 stepx = PNG_PASS_COL_OFFSET(pass);
michael@0 3006 y = PNG_PASS_START_ROW(pass);
michael@0 3007 stepy = PNG_PASS_ROW_OFFSET(pass);
michael@0 3008 }
michael@0 3009
michael@0 3010 else
michael@0 3011 {
michael@0 3012 y = 0;
michael@0 3013 startx = 0;
michael@0 3014 stepx = stepy = 1;
michael@0 3015 }
michael@0 3016
michael@0 3017 for (; y<height; y += stepy)
michael@0 3018 {
michael@0 3019 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
michael@0 3020 png_bytep outrow = first_row + y * step_row;
michael@0 3021 png_const_bytep end_row = outrow + width;
michael@0 3022
michael@0 3023 /* Read read the libpng data into the temporary buffer. */
michael@0 3024 png_read_row(png_ptr, inrow, NULL);
michael@0 3025
michael@0 3026 /* Now process the row according to the processing option, note
michael@0 3027 * that the caller verifies that the format of the libpng output
michael@0 3028 * data is as required.
michael@0 3029 */
michael@0 3030 outrow += startx;
michael@0 3031 switch (proc)
michael@0 3032 {
michael@0 3033 case PNG_CMAP_GA:
michael@0 3034 for (; outrow < end_row; outrow += stepx)
michael@0 3035 {
michael@0 3036 /* The data is always in the PNG order */
michael@0 3037 unsigned int gray = *inrow++;
michael@0 3038 unsigned int alpha = *inrow++;
michael@0 3039 unsigned int entry;
michael@0 3040
michael@0 3041 /* NOTE: this code is copied as a comment in
michael@0 3042 * make_ga_colormap above. Please update the
michael@0 3043 * comment if you change this code!
michael@0 3044 */
michael@0 3045 if (alpha > 229) /* opaque */
michael@0 3046 {
michael@0 3047 entry = (231 * gray + 128) >> 8;
michael@0 3048 }
michael@0 3049 else if (alpha < 26) /* transparent */
michael@0 3050 {
michael@0 3051 entry = 231;
michael@0 3052 }
michael@0 3053 else /* partially opaque */
michael@0 3054 {
michael@0 3055 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
michael@0 3056 }
michael@0 3057
michael@0 3058 *outrow = (png_byte)entry;
michael@0 3059 }
michael@0 3060 break;
michael@0 3061
michael@0 3062 case PNG_CMAP_TRANS:
michael@0 3063 for (; outrow < end_row; outrow += stepx)
michael@0 3064 {
michael@0 3065 png_byte gray = *inrow++;
michael@0 3066 png_byte alpha = *inrow++;
michael@0 3067
michael@0 3068 if (alpha == 0)
michael@0 3069 *outrow = PNG_CMAP_TRANS_BACKGROUND;
michael@0 3070
michael@0 3071 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
michael@0 3072 *outrow = gray;
michael@0 3073
michael@0 3074 else
michael@0 3075 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
michael@0 3076 }
michael@0 3077 break;
michael@0 3078
michael@0 3079 case PNG_CMAP_RGB:
michael@0 3080 for (; outrow < end_row; outrow += stepx)
michael@0 3081 {
michael@0 3082 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
michael@0 3083 inrow += 3;
michael@0 3084 }
michael@0 3085 break;
michael@0 3086
michael@0 3087 case PNG_CMAP_RGB_ALPHA:
michael@0 3088 for (; outrow < end_row; outrow += stepx)
michael@0 3089 {
michael@0 3090 unsigned int alpha = inrow[3];
michael@0 3091
michael@0 3092 /* Because the alpha entries only hold alpha==0.5 values
michael@0 3093 * split the processing at alpha==0.25 (64) and 0.75
michael@0 3094 * (196).
michael@0 3095 */
michael@0 3096
michael@0 3097 if (alpha >= 196)
michael@0 3098 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
michael@0 3099 inrow[2]);
michael@0 3100
michael@0 3101 else if (alpha < 64)
michael@0 3102 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
michael@0 3103
michael@0 3104 else
michael@0 3105 {
michael@0 3106 /* Likewise there are three entries for each of r, g
michael@0 3107 * and b. We could select the entry by popcount on
michael@0 3108 * the top two bits on those architectures that
michael@0 3109 * support it, this is what the code below does,
michael@0 3110 * crudely.
michael@0 3111 */
michael@0 3112 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
michael@0 3113
michael@0 3114 /* Here are how the values map:
michael@0 3115 *
michael@0 3116 * 0x00 .. 0x3f -> 0
michael@0 3117 * 0x40 .. 0xbf -> 1
michael@0 3118 * 0xc0 .. 0xff -> 2
michael@0 3119 *
michael@0 3120 * So, as above with the explicit alpha checks, the
michael@0 3121 * breakpoints are at 64 and 196.
michael@0 3122 */
michael@0 3123 if (inrow[0] & 0x80) back_i += 9; /* red */
michael@0 3124 if (inrow[0] & 0x40) back_i += 9;
michael@0 3125 if (inrow[0] & 0x80) back_i += 3; /* green */
michael@0 3126 if (inrow[0] & 0x40) back_i += 3;
michael@0 3127 if (inrow[0] & 0x80) back_i += 1; /* blue */
michael@0 3128 if (inrow[0] & 0x40) back_i += 1;
michael@0 3129
michael@0 3130 *outrow = (png_byte)back_i;
michael@0 3131 }
michael@0 3132
michael@0 3133 inrow += 4;
michael@0 3134 }
michael@0 3135 break;
michael@0 3136
michael@0 3137 default:
michael@0 3138 break;
michael@0 3139 }
michael@0 3140 }
michael@0 3141 }
michael@0 3142 }
michael@0 3143
michael@0 3144 return 1;
michael@0 3145 }
michael@0 3146
michael@0 3147 static int
michael@0 3148 png_image_read_colormapped(png_voidp argument)
michael@0 3149 {
michael@0 3150 png_image_read_control *display = png_voidcast(png_image_read_control*,
michael@0 3151 argument);
michael@0 3152 png_imagep image = display->image;
michael@0 3153 png_controlp control = image->opaque;
michael@0 3154 png_structrp png_ptr = control->png_ptr;
michael@0 3155 png_inforp info_ptr = control->info_ptr;
michael@0 3156
michael@0 3157 int passes = 0; /* As a flag */
michael@0 3158
michael@0 3159 PNG_SKIP_CHUNKS(png_ptr);
michael@0 3160
michael@0 3161 /* Update the 'info' structure and make sure the result is as required; first
michael@0 3162 * make sure to turn on the interlace handling if it will be required
michael@0 3163 * (because it can't be turned on *after* the call to png_read_update_info!)
michael@0 3164 */
michael@0 3165 if (display->colormap_processing == PNG_CMAP_NONE)
michael@0 3166 passes = png_set_interlace_handling(png_ptr);
michael@0 3167
michael@0 3168 png_read_update_info(png_ptr, info_ptr);
michael@0 3169
michael@0 3170 /* The expected output can be deduced from the colormap_processing option. */
michael@0 3171 switch (display->colormap_processing)
michael@0 3172 {
michael@0 3173 case PNG_CMAP_NONE:
michael@0 3174 /* Output must be one channel and one byte per pixel, the output
michael@0 3175 * encoding can be anything.
michael@0 3176 */
michael@0 3177 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
michael@0 3178 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
michael@0 3179 info_ptr->bit_depth == 8)
michael@0 3180 break;
michael@0 3181
michael@0 3182 goto bad_output;
michael@0 3183
michael@0 3184 case PNG_CMAP_TRANS:
michael@0 3185 case PNG_CMAP_GA:
michael@0 3186 /* Output must be two channels and the 'G' one must be sRGB, the latter
michael@0 3187 * can be checked with an exact number because it should have been set
michael@0 3188 * to this number above!
michael@0 3189 */
michael@0 3190 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
michael@0 3191 info_ptr->bit_depth == 8 &&
michael@0 3192 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
michael@0 3193 image->colormap_entries == 256)
michael@0 3194 break;
michael@0 3195
michael@0 3196 goto bad_output;
michael@0 3197
michael@0 3198 case PNG_CMAP_RGB:
michael@0 3199 /* Output must be 8-bit sRGB encoded RGB */
michael@0 3200 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
michael@0 3201 info_ptr->bit_depth == 8 &&
michael@0 3202 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
michael@0 3203 image->colormap_entries == 216)
michael@0 3204 break;
michael@0 3205
michael@0 3206 goto bad_output;
michael@0 3207
michael@0 3208 case PNG_CMAP_RGB_ALPHA:
michael@0 3209 /* Output must be 8-bit sRGB encoded RGBA */
michael@0 3210 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
michael@0 3211 info_ptr->bit_depth == 8 &&
michael@0 3212 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
michael@0 3213 image->colormap_entries == 244 /* 216 + 1 + 27 */)
michael@0 3214 break;
michael@0 3215
michael@0 3216 /* goto bad_output; */
michael@0 3217 /* FALL THROUGH */
michael@0 3218
michael@0 3219 default:
michael@0 3220 bad_output:
michael@0 3221 png_error(png_ptr, "bad color-map processing (internal error)");
michael@0 3222 }
michael@0 3223
michael@0 3224 /* Now read the rows. Do this here if it is possible to read directly into
michael@0 3225 * the output buffer, otherwise allocate a local row buffer of the maximum
michael@0 3226 * size libpng requires and call the relevant processing routine safely.
michael@0 3227 */
michael@0 3228 {
michael@0 3229 png_voidp first_row = display->buffer;
michael@0 3230 ptrdiff_t row_bytes = display->row_stride;
michael@0 3231
michael@0 3232 /* The following expression is designed to work correctly whether it gives
michael@0 3233 * a signed or an unsigned result.
michael@0 3234 */
michael@0 3235 if (row_bytes < 0)
michael@0 3236 {
michael@0 3237 char *ptr = png_voidcast(char*, first_row);
michael@0 3238 ptr += (image->height-1) * (-row_bytes);
michael@0 3239 first_row = png_voidcast(png_voidp, ptr);
michael@0 3240 }
michael@0 3241
michael@0 3242 display->first_row = first_row;
michael@0 3243 display->row_bytes = row_bytes;
michael@0 3244 }
michael@0 3245
michael@0 3246 if (passes == 0)
michael@0 3247 {
michael@0 3248 int result;
michael@0 3249 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
michael@0 3250
michael@0 3251 display->local_row = row;
michael@0 3252 result = png_safe_execute(image, png_image_read_and_map, display);
michael@0 3253 display->local_row = NULL;
michael@0 3254 png_free(png_ptr, row);
michael@0 3255
michael@0 3256 return result;
michael@0 3257 }
michael@0 3258
michael@0 3259 else
michael@0 3260 {
michael@0 3261 png_alloc_size_t row_bytes = display->row_bytes;
michael@0 3262
michael@0 3263 while (--passes >= 0)
michael@0 3264 {
michael@0 3265 png_uint_32 y = image->height;
michael@0 3266 png_bytep row = png_voidcast(png_bytep, display->first_row);
michael@0 3267
michael@0 3268 while (y-- > 0)
michael@0 3269 {
michael@0 3270 png_read_row(png_ptr, row, NULL);
michael@0 3271 row += row_bytes;
michael@0 3272 }
michael@0 3273 }
michael@0 3274
michael@0 3275 return 1;
michael@0 3276 }
michael@0 3277 }
michael@0 3278
michael@0 3279 /* Just the row reading part of png_image_read. */
michael@0 3280 static int
michael@0 3281 png_image_read_composite(png_voidp argument)
michael@0 3282 {
michael@0 3283 png_image_read_control *display = png_voidcast(png_image_read_control*,
michael@0 3284 argument);
michael@0 3285 png_imagep image = display->image;
michael@0 3286 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 3287 int passes;
michael@0 3288
michael@0 3289 switch (png_ptr->interlaced)
michael@0 3290 {
michael@0 3291 case PNG_INTERLACE_NONE:
michael@0 3292 passes = 1;
michael@0 3293 break;
michael@0 3294
michael@0 3295 case PNG_INTERLACE_ADAM7:
michael@0 3296 passes = PNG_INTERLACE_ADAM7_PASSES;
michael@0 3297 break;
michael@0 3298
michael@0 3299 default:
michael@0 3300 png_error(png_ptr, "unknown interlace type");
michael@0 3301 }
michael@0 3302
michael@0 3303 {
michael@0 3304 png_uint_32 height = image->height;
michael@0 3305 png_uint_32 width = image->width;
michael@0 3306 ptrdiff_t step_row = display->row_bytes;
michael@0 3307 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
michael@0 3308 int pass;
michael@0 3309
michael@0 3310 for (pass = 0; pass < passes; ++pass)
michael@0 3311 {
michael@0 3312 unsigned int startx, stepx, stepy;
michael@0 3313 png_uint_32 y;
michael@0 3314
michael@0 3315 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
michael@0 3316 {
michael@0 3317 /* The row may be empty for a short image: */
michael@0 3318 if (PNG_PASS_COLS(width, pass) == 0)
michael@0 3319 continue;
michael@0 3320
michael@0 3321 startx = PNG_PASS_START_COL(pass) * channels;
michael@0 3322 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
michael@0 3323 y = PNG_PASS_START_ROW(pass);
michael@0 3324 stepy = PNG_PASS_ROW_OFFSET(pass);
michael@0 3325 }
michael@0 3326
michael@0 3327 else
michael@0 3328 {
michael@0 3329 y = 0;
michael@0 3330 startx = 0;
michael@0 3331 stepx = channels;
michael@0 3332 stepy = 1;
michael@0 3333 }
michael@0 3334
michael@0 3335 for (; y<height; y += stepy)
michael@0 3336 {
michael@0 3337 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
michael@0 3338 png_bytep outrow;
michael@0 3339 png_const_bytep end_row;
michael@0 3340
michael@0 3341 /* Read the row, which is packed: */
michael@0 3342 png_read_row(png_ptr, inrow, NULL);
michael@0 3343
michael@0 3344 outrow = png_voidcast(png_bytep, display->first_row);
michael@0 3345 outrow += y * step_row;
michael@0 3346 end_row = outrow + width * channels;
michael@0 3347
michael@0 3348 /* Now do the composition on each pixel in this row. */
michael@0 3349 outrow += startx;
michael@0 3350 for (; outrow < end_row; outrow += stepx)
michael@0 3351 {
michael@0 3352 png_byte alpha = inrow[channels];
michael@0 3353
michael@0 3354 if (alpha > 0) /* else no change to the output */
michael@0 3355 {
michael@0 3356 unsigned int c;
michael@0 3357
michael@0 3358 for (c=0; c<channels; ++c)
michael@0 3359 {
michael@0 3360 png_uint_32 component = inrow[c];
michael@0 3361
michael@0 3362 if (alpha < 255) /* else just use component */
michael@0 3363 {
michael@0 3364 /* This is PNG_OPTIMIZED_ALPHA, the component value
michael@0 3365 * is a linear 8-bit value. Combine this with the
michael@0 3366 * current outrow[c] value which is sRGB encoded.
michael@0 3367 * Arithmetic here is 16-bits to preserve the output
michael@0 3368 * values correctly.
michael@0 3369 */
michael@0 3370 component *= 257*255; /* =65535 */
michael@0 3371 component += (255-alpha)*png_sRGB_table[outrow[c]];
michael@0 3372
michael@0 3373 /* So 'component' is scaled by 255*65535 and is
michael@0 3374 * therefore appropriate for the sRGB to linear
michael@0 3375 * conversion table.
michael@0 3376 */
michael@0 3377 component = PNG_sRGB_FROM_LINEAR(component);
michael@0 3378 }
michael@0 3379
michael@0 3380 outrow[c] = (png_byte)component;
michael@0 3381 }
michael@0 3382 }
michael@0 3383
michael@0 3384 inrow += channels+1; /* components and alpha channel */
michael@0 3385 }
michael@0 3386 }
michael@0 3387 }
michael@0 3388 }
michael@0 3389
michael@0 3390 return 1;
michael@0 3391 }
michael@0 3392
michael@0 3393 /* The do_local_background case; called when all the following transforms are to
michael@0 3394 * be done:
michael@0 3395 *
michael@0 3396 * PNG_RGB_TO_GRAY
michael@0 3397 * PNG_COMPOSITE
michael@0 3398 * PNG_GAMMA
michael@0 3399 *
michael@0 3400 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
michael@0 3401 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
michael@0 3402 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
michael@0 3403 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
michael@0 3404 * the removal or pre-multiplication of the alpha channel.
michael@0 3405 */
michael@0 3406 static int
michael@0 3407 png_image_read_background(png_voidp argument)
michael@0 3408 {
michael@0 3409 png_image_read_control *display = png_voidcast(png_image_read_control*,
michael@0 3410 argument);
michael@0 3411 png_imagep image = display->image;
michael@0 3412 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 3413 png_inforp info_ptr = image->opaque->info_ptr;
michael@0 3414 png_uint_32 height = image->height;
michael@0 3415 png_uint_32 width = image->width;
michael@0 3416 int pass, passes;
michael@0 3417
michael@0 3418 /* Double check the convoluted logic below. We expect to get here with
michael@0 3419 * libpng doing rgb to gray and gamma correction but background processing
michael@0 3420 * left to the png_image_read_background function. The rows libpng produce
michael@0 3421 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
michael@0 3422 */
michael@0 3423 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
michael@0 3424 png_error(png_ptr, "lost rgb to gray");
michael@0 3425
michael@0 3426 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
michael@0 3427 png_error(png_ptr, "unexpected compose");
michael@0 3428
michael@0 3429 if (png_get_channels(png_ptr, info_ptr) != 2)
michael@0 3430 png_error(png_ptr, "lost/gained channels");
michael@0 3431
michael@0 3432 /* Expect the 8-bit case to always remove the alpha channel */
michael@0 3433 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
michael@0 3434 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
michael@0 3435 png_error(png_ptr, "unexpected 8-bit transformation");
michael@0 3436
michael@0 3437 switch (png_ptr->interlaced)
michael@0 3438 {
michael@0 3439 case PNG_INTERLACE_NONE:
michael@0 3440 passes = 1;
michael@0 3441 break;
michael@0 3442
michael@0 3443 case PNG_INTERLACE_ADAM7:
michael@0 3444 passes = PNG_INTERLACE_ADAM7_PASSES;
michael@0 3445 break;
michael@0 3446
michael@0 3447 default:
michael@0 3448 png_error(png_ptr, "unknown interlace type");
michael@0 3449 }
michael@0 3450
michael@0 3451 /* Use direct access to info_ptr here because otherwise the simplified API
michael@0 3452 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
michael@0 3453 * checking the value after libpng expansions, not the original value in the
michael@0 3454 * PNG.
michael@0 3455 */
michael@0 3456 switch (info_ptr->bit_depth)
michael@0 3457 {
michael@0 3458 default:
michael@0 3459 png_error(png_ptr, "unexpected bit depth");
michael@0 3460 break;
michael@0 3461
michael@0 3462 case 8:
michael@0 3463 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
michael@0 3464 * to be removed by composing on a background: either the row if
michael@0 3465 * display->background is NULL or display->background->green if not.
michael@0 3466 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
michael@0 3467 */
michael@0 3468 {
michael@0 3469 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
michael@0 3470 ptrdiff_t step_row = display->row_bytes;
michael@0 3471
michael@0 3472 for (pass = 0; pass < passes; ++pass)
michael@0 3473 {
michael@0 3474 png_bytep row = png_voidcast(png_bytep,
michael@0 3475 display->first_row);
michael@0 3476 unsigned int startx, stepx, stepy;
michael@0 3477 png_uint_32 y;
michael@0 3478
michael@0 3479 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
michael@0 3480 {
michael@0 3481 /* The row may be empty for a short image: */
michael@0 3482 if (PNG_PASS_COLS(width, pass) == 0)
michael@0 3483 continue;
michael@0 3484
michael@0 3485 startx = PNG_PASS_START_COL(pass);
michael@0 3486 stepx = PNG_PASS_COL_OFFSET(pass);
michael@0 3487 y = PNG_PASS_START_ROW(pass);
michael@0 3488 stepy = PNG_PASS_ROW_OFFSET(pass);
michael@0 3489 }
michael@0 3490
michael@0 3491 else
michael@0 3492 {
michael@0 3493 y = 0;
michael@0 3494 startx = 0;
michael@0 3495 stepx = stepy = 1;
michael@0 3496 }
michael@0 3497
michael@0 3498 if (display->background == NULL)
michael@0 3499 {
michael@0 3500 for (; y<height; y += stepy)
michael@0 3501 {
michael@0 3502 png_bytep inrow = png_voidcast(png_bytep,
michael@0 3503 display->local_row);
michael@0 3504 png_bytep outrow = first_row + y * step_row;
michael@0 3505 png_const_bytep end_row = outrow + width;
michael@0 3506
michael@0 3507 /* Read the row, which is packed: */
michael@0 3508 png_read_row(png_ptr, inrow, NULL);
michael@0 3509
michael@0 3510 /* Now do the composition on each pixel in this row. */
michael@0 3511 outrow += startx;
michael@0 3512 for (; outrow < end_row; outrow += stepx)
michael@0 3513 {
michael@0 3514 png_byte alpha = inrow[1];
michael@0 3515
michael@0 3516 if (alpha > 0) /* else no change to the output */
michael@0 3517 {
michael@0 3518 png_uint_32 component = inrow[0];
michael@0 3519
michael@0 3520 if (alpha < 255) /* else just use component */
michael@0 3521 {
michael@0 3522 /* Since PNG_OPTIMIZED_ALPHA was not set it is
michael@0 3523 * necessary to invert the sRGB transfer
michael@0 3524 * function and multiply the alpha out.
michael@0 3525 */
michael@0 3526 component = png_sRGB_table[component] * alpha;
michael@0 3527 component += png_sRGB_table[outrow[0]] *
michael@0 3528 (255-alpha);
michael@0 3529 component = PNG_sRGB_FROM_LINEAR(component);
michael@0 3530 }
michael@0 3531
michael@0 3532 outrow[0] = (png_byte)component;
michael@0 3533 }
michael@0 3534
michael@0 3535 inrow += 2; /* gray and alpha channel */
michael@0 3536 }
michael@0 3537 }
michael@0 3538 }
michael@0 3539
michael@0 3540 else /* constant background value */
michael@0 3541 {
michael@0 3542 png_byte background8 = display->background->green;
michael@0 3543 png_uint_16 background = png_sRGB_table[background8];
michael@0 3544
michael@0 3545 for (; y<height; y += stepy)
michael@0 3546 {
michael@0 3547 png_bytep inrow = png_voidcast(png_bytep,
michael@0 3548 display->local_row);
michael@0 3549 png_bytep outrow = first_row + y * step_row;
michael@0 3550 png_const_bytep end_row = outrow + width;
michael@0 3551
michael@0 3552 /* Read the row, which is packed: */
michael@0 3553 png_read_row(png_ptr, inrow, NULL);
michael@0 3554
michael@0 3555 /* Now do the composition on each pixel in this row. */
michael@0 3556 outrow += startx;
michael@0 3557 for (; outrow < end_row; outrow += stepx)
michael@0 3558 {
michael@0 3559 png_byte alpha = inrow[1];
michael@0 3560
michael@0 3561 if (alpha > 0) /* else use background */
michael@0 3562 {
michael@0 3563 png_uint_32 component = inrow[0];
michael@0 3564
michael@0 3565 if (alpha < 255) /* else just use component */
michael@0 3566 {
michael@0 3567 component = png_sRGB_table[component] * alpha;
michael@0 3568 component += background * (255-alpha);
michael@0 3569 component = PNG_sRGB_FROM_LINEAR(component);
michael@0 3570 }
michael@0 3571
michael@0 3572 outrow[0] = (png_byte)component;
michael@0 3573 }
michael@0 3574
michael@0 3575 else
michael@0 3576 outrow[0] = background8;
michael@0 3577
michael@0 3578 inrow += 2; /* gray and alpha channel */
michael@0 3579 }
michael@0 3580
michael@0 3581 row += display->row_bytes;
michael@0 3582 }
michael@0 3583 }
michael@0 3584 }
michael@0 3585 }
michael@0 3586 break;
michael@0 3587
michael@0 3588 case 16:
michael@0 3589 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
michael@0 3590 * still be done and, maybe, the alpha channel removed. This code also
michael@0 3591 * handles the alpha-first option.
michael@0 3592 */
michael@0 3593 {
michael@0 3594 png_uint_16p first_row = png_voidcast(png_uint_16p,
michael@0 3595 display->first_row);
michael@0 3596 /* The division by two is safe because the caller passed in a
michael@0 3597 * stride which was multiplied by 2 (below) to get row_bytes.
michael@0 3598 */
michael@0 3599 ptrdiff_t step_row = display->row_bytes / 2;
michael@0 3600 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
michael@0 3601 unsigned int outchannels = 1+preserve_alpha;
michael@0 3602 int swap_alpha = 0;
michael@0 3603
michael@0 3604 # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
michael@0 3605 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
michael@0 3606 swap_alpha = 1;
michael@0 3607 # endif
michael@0 3608
michael@0 3609 for (pass = 0; pass < passes; ++pass)
michael@0 3610 {
michael@0 3611 unsigned int startx, stepx, stepy;
michael@0 3612 png_uint_32 y;
michael@0 3613
michael@0 3614 /* The 'x' start and step are adjusted to output components here.
michael@0 3615 */
michael@0 3616 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
michael@0 3617 {
michael@0 3618 /* The row may be empty for a short image: */
michael@0 3619 if (PNG_PASS_COLS(width, pass) == 0)
michael@0 3620 continue;
michael@0 3621
michael@0 3622 startx = PNG_PASS_START_COL(pass) * outchannels;
michael@0 3623 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
michael@0 3624 y = PNG_PASS_START_ROW(pass);
michael@0 3625 stepy = PNG_PASS_ROW_OFFSET(pass);
michael@0 3626 }
michael@0 3627
michael@0 3628 else
michael@0 3629 {
michael@0 3630 y = 0;
michael@0 3631 startx = 0;
michael@0 3632 stepx = outchannels;
michael@0 3633 stepy = 1;
michael@0 3634 }
michael@0 3635
michael@0 3636 for (; y<height; y += stepy)
michael@0 3637 {
michael@0 3638 png_const_uint_16p inrow;
michael@0 3639 png_uint_16p outrow = first_row + y*step_row;
michael@0 3640 png_uint_16p end_row = outrow + width * outchannels;
michael@0 3641
michael@0 3642 /* Read the row, which is packed: */
michael@0 3643 png_read_row(png_ptr, png_voidcast(png_bytep,
michael@0 3644 display->local_row), NULL);
michael@0 3645 inrow = png_voidcast(png_const_uint_16p, display->local_row);
michael@0 3646
michael@0 3647 /* Now do the pre-multiplication on each pixel in this row.
michael@0 3648 */
michael@0 3649 outrow += startx;
michael@0 3650 for (; outrow < end_row; outrow += stepx)
michael@0 3651 {
michael@0 3652 png_uint_32 component = inrow[0];
michael@0 3653 png_uint_16 alpha = inrow[1];
michael@0 3654
michael@0 3655 if (alpha > 0) /* else 0 */
michael@0 3656 {
michael@0 3657 if (alpha < 65535) /* else just use component */
michael@0 3658 {
michael@0 3659 component *= alpha;
michael@0 3660 component += 32767;
michael@0 3661 component /= 65535;
michael@0 3662 }
michael@0 3663 }
michael@0 3664
michael@0 3665 else
michael@0 3666 component = 0;
michael@0 3667
michael@0 3668 outrow[swap_alpha] = (png_uint_16)component;
michael@0 3669 if (preserve_alpha)
michael@0 3670 outrow[1 ^ swap_alpha] = alpha;
michael@0 3671
michael@0 3672 inrow += 2; /* components and alpha channel */
michael@0 3673 }
michael@0 3674 }
michael@0 3675 }
michael@0 3676 }
michael@0 3677 break;
michael@0 3678 }
michael@0 3679
michael@0 3680 return 1;
michael@0 3681 }
michael@0 3682
michael@0 3683 /* The guts of png_image_finish_read as a png_safe_execute callback. */
michael@0 3684 static int
michael@0 3685 png_image_read_direct(png_voidp argument)
michael@0 3686 {
michael@0 3687 png_image_read_control *display = png_voidcast(png_image_read_control*,
michael@0 3688 argument);
michael@0 3689 png_imagep image = display->image;
michael@0 3690 png_structrp png_ptr = image->opaque->png_ptr;
michael@0 3691 png_inforp info_ptr = image->opaque->info_ptr;
michael@0 3692
michael@0 3693 png_uint_32 format = image->format;
michael@0 3694 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
michael@0 3695 int do_local_compose = 0;
michael@0 3696 int do_local_background = 0; /* to avoid double gamma correction bug */
michael@0 3697 int passes = 0;
michael@0 3698
michael@0 3699 /* Add transforms to ensure the correct output format is produced then check
michael@0 3700 * that the required implementation support is there. Always expand; always
michael@0 3701 * need 8 bits minimum, no palette and expanded tRNS.
michael@0 3702 */
michael@0 3703 png_set_expand(png_ptr);
michael@0 3704
michael@0 3705 /* Now check the format to see if it was modified. */
michael@0 3706 {
michael@0 3707 png_uint_32 base_format = png_image_format(png_ptr) &
michael@0 3708 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
michael@0 3709 png_uint_32 change = format ^ base_format;
michael@0 3710 png_fixed_point output_gamma;
michael@0 3711 int mode; /* alpha mode */
michael@0 3712
michael@0 3713 /* Do this first so that we have a record if rgb to gray is happening. */
michael@0 3714 if (change & PNG_FORMAT_FLAG_COLOR)
michael@0 3715 {
michael@0 3716 /* gray<->color transformation required. */
michael@0 3717 if (format & PNG_FORMAT_FLAG_COLOR)
michael@0 3718 png_set_gray_to_rgb(png_ptr);
michael@0 3719
michael@0 3720 else
michael@0 3721 {
michael@0 3722 /* libpng can't do both rgb to gray and
michael@0 3723 * background/pre-multiplication if there is also significant gamma
michael@0 3724 * correction, because both operations require linear colors and
michael@0 3725 * the code only supports one transform doing the gamma correction.
michael@0 3726 * Handle this by doing the pre-multiplication or background
michael@0 3727 * operation in this code, if necessary.
michael@0 3728 *
michael@0 3729 * TODO: fix this by rewriting pngrtran.c (!)
michael@0 3730 *
michael@0 3731 * For the moment (given that fixing this in pngrtran.c is an
michael@0 3732 * enormous change) 'do_local_background' is used to indicate that
michael@0 3733 * the problem exists.
michael@0 3734 */
michael@0 3735 if (base_format & PNG_FORMAT_FLAG_ALPHA)
michael@0 3736 do_local_background = 1/*maybe*/;
michael@0 3737
michael@0 3738 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
michael@0 3739 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
michael@0 3740 }
michael@0 3741
michael@0 3742 change &= ~PNG_FORMAT_FLAG_COLOR;
michael@0 3743 }
michael@0 3744
michael@0 3745 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
michael@0 3746 */
michael@0 3747 {
michael@0 3748 png_fixed_point input_gamma_default;
michael@0 3749
michael@0 3750 if ((base_format & PNG_FORMAT_FLAG_LINEAR) &&
michael@0 3751 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
michael@0 3752 input_gamma_default = PNG_GAMMA_LINEAR;
michael@0 3753 else
michael@0 3754 input_gamma_default = PNG_DEFAULT_sRGB;
michael@0 3755
michael@0 3756 /* Call png_set_alpha_mode to set the default for the input gamma; the
michael@0 3757 * output gamma is set by a second call below.
michael@0 3758 */
michael@0 3759 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
michael@0 3760 }
michael@0 3761
michael@0 3762 if (linear)
michael@0 3763 {
michael@0 3764 /* If there *is* an alpha channel in the input it must be multiplied
michael@0 3765 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
michael@0 3766 */
michael@0 3767 if (base_format & PNG_FORMAT_FLAG_ALPHA)
michael@0 3768 mode = PNG_ALPHA_STANDARD; /* associated alpha */
michael@0 3769
michael@0 3770 else
michael@0 3771 mode = PNG_ALPHA_PNG;
michael@0 3772
michael@0 3773 output_gamma = PNG_GAMMA_LINEAR;
michael@0 3774 }
michael@0 3775
michael@0 3776 else
michael@0 3777 {
michael@0 3778 mode = PNG_ALPHA_PNG;
michael@0 3779 output_gamma = PNG_DEFAULT_sRGB;
michael@0 3780 }
michael@0 3781
michael@0 3782 /* If 'do_local_background' is set check for the presence of gamma
michael@0 3783 * correction; this is part of the work-round for the libpng bug
michael@0 3784 * described above.
michael@0 3785 *
michael@0 3786 * TODO: fix libpng and remove this.
michael@0 3787 */
michael@0 3788 if (do_local_background)
michael@0 3789 {
michael@0 3790 png_fixed_point gtest;
michael@0 3791
michael@0 3792 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
michael@0 3793 * gamma correction, the screen gamma hasn't been set on png_struct
michael@0 3794 * yet; it's set below. png_struct::gamma, however, is set to the
michael@0 3795 * final value.
michael@0 3796 */
michael@0 3797 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
michael@0 3798 PNG_FP_1) && !png_gamma_significant(gtest))
michael@0 3799 do_local_background = 0;
michael@0 3800
michael@0 3801 else if (mode == PNG_ALPHA_STANDARD)
michael@0 3802 {
michael@0 3803 do_local_background = 2/*required*/;
michael@0 3804 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
michael@0 3805 }
michael@0 3806
michael@0 3807 /* else leave as 1 for the checks below */
michael@0 3808 }
michael@0 3809
michael@0 3810 /* If the bit-depth changes then handle that here. */
michael@0 3811 if (change & PNG_FORMAT_FLAG_LINEAR)
michael@0 3812 {
michael@0 3813 if (linear /*16-bit output*/)
michael@0 3814 png_set_expand_16(png_ptr);
michael@0 3815
michael@0 3816 else /* 8-bit output */
michael@0 3817 png_set_scale_16(png_ptr);
michael@0 3818
michael@0 3819 change &= ~PNG_FORMAT_FLAG_LINEAR;
michael@0 3820 }
michael@0 3821
michael@0 3822 /* Now the background/alpha channel changes. */
michael@0 3823 if (change & PNG_FORMAT_FLAG_ALPHA)
michael@0 3824 {
michael@0 3825 /* Removing an alpha channel requires composition for the 8-bit
michael@0 3826 * formats; for the 16-bit it is already done, above, by the
michael@0 3827 * pre-multiplication and the channel just needs to be stripped.
michael@0 3828 */
michael@0 3829 if (base_format & PNG_FORMAT_FLAG_ALPHA)
michael@0 3830 {
michael@0 3831 /* If RGB->gray is happening the alpha channel must be left and the
michael@0 3832 * operation completed locally.
michael@0 3833 *
michael@0 3834 * TODO: fix libpng and remove this.
michael@0 3835 */
michael@0 3836 if (do_local_background)
michael@0 3837 do_local_background = 2/*required*/;
michael@0 3838
michael@0 3839 /* 16-bit output: just remove the channel */
michael@0 3840 else if (linear) /* compose on black (well, pre-multiply) */
michael@0 3841 png_set_strip_alpha(png_ptr);
michael@0 3842
michael@0 3843 /* 8-bit output: do an appropriate compose */
michael@0 3844 else if (display->background != NULL)
michael@0 3845 {
michael@0 3846 png_color_16 c;
michael@0 3847
michael@0 3848 c.index = 0; /*unused*/
michael@0 3849 c.red = display->background->red;
michael@0 3850 c.green = display->background->green;
michael@0 3851 c.blue = display->background->blue;
michael@0 3852 c.gray = display->background->green;
michael@0 3853
michael@0 3854 /* This is always an 8-bit sRGB value, using the 'green' channel
michael@0 3855 * for gray is much better than calculating the luminance here;
michael@0 3856 * we can get off-by-one errors in that calculation relative to
michael@0 3857 * the app expectations and that will show up in transparent
michael@0 3858 * pixels.
michael@0 3859 */
michael@0 3860 png_set_background_fixed(png_ptr, &c,
michael@0 3861 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
michael@0 3862 0/*gamma: not used*/);
michael@0 3863 }
michael@0 3864
michael@0 3865 else /* compose on row: implemented below. */
michael@0 3866 {
michael@0 3867 do_local_compose = 1;
michael@0 3868 /* This leaves the alpha channel in the output, so it has to be
michael@0 3869 * removed by the code below. Set the encoding to the 'OPTIMIZE'
michael@0 3870 * one so the code only has to hack on the pixels that require
michael@0 3871 * composition.
michael@0 3872 */
michael@0 3873 mode = PNG_ALPHA_OPTIMIZED;
michael@0 3874 }
michael@0 3875 }
michael@0 3876
michael@0 3877 else /* output needs an alpha channel */
michael@0 3878 {
michael@0 3879 /* This is tricky because it happens before the swap operation has
michael@0 3880 * been accomplished; however, the swap does *not* swap the added
michael@0 3881 * alpha channel (weird API), so it must be added in the correct
michael@0 3882 * place.
michael@0 3883 */
michael@0 3884 png_uint_32 filler; /* opaque filler */
michael@0 3885 int where;
michael@0 3886
michael@0 3887 if (linear)
michael@0 3888 filler = 65535;
michael@0 3889
michael@0 3890 else
michael@0 3891 filler = 255;
michael@0 3892
michael@0 3893 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
michael@0 3894 if (format & PNG_FORMAT_FLAG_AFIRST)
michael@0 3895 {
michael@0 3896 where = PNG_FILLER_BEFORE;
michael@0 3897 change &= ~PNG_FORMAT_FLAG_AFIRST;
michael@0 3898 }
michael@0 3899
michael@0 3900 else
michael@0 3901 # endif
michael@0 3902 where = PNG_FILLER_AFTER;
michael@0 3903
michael@0 3904 png_set_add_alpha(png_ptr, filler, where);
michael@0 3905 }
michael@0 3906
michael@0 3907 /* This stops the (irrelevant) call to swap_alpha below. */
michael@0 3908 change &= ~PNG_FORMAT_FLAG_ALPHA;
michael@0 3909 }
michael@0 3910
michael@0 3911 /* Now set the alpha mode correctly; this is always done, even if there is
michael@0 3912 * no alpha channel in either the input or the output because it correctly
michael@0 3913 * sets the output gamma.
michael@0 3914 */
michael@0 3915 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
michael@0 3916
michael@0 3917 # ifdef PNG_FORMAT_BGR_SUPPORTED
michael@0 3918 if (change & PNG_FORMAT_FLAG_BGR)
michael@0 3919 {
michael@0 3920 /* Check only the output format; PNG is never BGR; don't do this if
michael@0 3921 * the output is gray, but fix up the 'format' value in that case.
michael@0 3922 */
michael@0 3923 if (format & PNG_FORMAT_FLAG_COLOR)
michael@0 3924 png_set_bgr(png_ptr);
michael@0 3925
michael@0 3926 else
michael@0 3927 format &= ~PNG_FORMAT_FLAG_BGR;
michael@0 3928
michael@0 3929 change &= ~PNG_FORMAT_FLAG_BGR;
michael@0 3930 }
michael@0 3931 # endif
michael@0 3932
michael@0 3933 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
michael@0 3934 if (change & PNG_FORMAT_FLAG_AFIRST)
michael@0 3935 {
michael@0 3936 /* Only relevant if there is an alpha channel - it's particularly
michael@0 3937 * important to handle this correctly because do_local_compose may
michael@0 3938 * be set above and then libpng will keep the alpha channel for this
michael@0 3939 * code to remove.
michael@0 3940 */
michael@0 3941 if (format & PNG_FORMAT_FLAG_ALPHA)
michael@0 3942 {
michael@0 3943 /* Disable this if doing a local background,
michael@0 3944 * TODO: remove this when local background is no longer required.
michael@0 3945 */
michael@0 3946 if (do_local_background != 2)
michael@0 3947 png_set_swap_alpha(png_ptr);
michael@0 3948 }
michael@0 3949
michael@0 3950 else
michael@0 3951 format &= ~PNG_FORMAT_FLAG_AFIRST;
michael@0 3952
michael@0 3953 change &= ~PNG_FORMAT_FLAG_AFIRST;
michael@0 3954 }
michael@0 3955 # endif
michael@0 3956
michael@0 3957 /* If the *output* is 16-bit then we need to check for a byte-swap on this
michael@0 3958 * architecture.
michael@0 3959 */
michael@0 3960 if (linear)
michael@0 3961 {
michael@0 3962 PNG_CONST png_uint_16 le = 0x0001;
michael@0 3963
michael@0 3964 if (*(png_const_bytep)&le)
michael@0 3965 png_set_swap(png_ptr);
michael@0 3966 }
michael@0 3967
michael@0 3968 /* If change is not now 0 some transformation is missing - error out. */
michael@0 3969 if (change)
michael@0 3970 png_error(png_ptr, "png_read_image: unsupported transformation");
michael@0 3971 }
michael@0 3972
michael@0 3973 PNG_SKIP_CHUNKS(png_ptr);
michael@0 3974
michael@0 3975 /* Update the 'info' structure and make sure the result is as required; first
michael@0 3976 * make sure to turn on the interlace handling if it will be required
michael@0 3977 * (because it can't be turned on *after* the call to png_read_update_info!)
michael@0 3978 *
michael@0 3979 * TODO: remove the do_local_background fixup below.
michael@0 3980 */
michael@0 3981 if (!do_local_compose && do_local_background != 2)
michael@0 3982 passes = png_set_interlace_handling(png_ptr);
michael@0 3983
michael@0 3984 png_read_update_info(png_ptr, info_ptr);
michael@0 3985
michael@0 3986 {
michael@0 3987 png_uint_32 info_format = 0;
michael@0 3988
michael@0 3989 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
michael@0 3990 info_format |= PNG_FORMAT_FLAG_COLOR;
michael@0 3991
michael@0 3992 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
michael@0 3993 {
michael@0 3994 /* do_local_compose removes this channel below. */
michael@0 3995 if (!do_local_compose)
michael@0 3996 {
michael@0 3997 /* do_local_background does the same if required. */
michael@0 3998 if (do_local_background != 2 ||
michael@0 3999 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
michael@0 4000 info_format |= PNG_FORMAT_FLAG_ALPHA;
michael@0 4001 }
michael@0 4002 }
michael@0 4003
michael@0 4004 else if (do_local_compose) /* internal error */
michael@0 4005 png_error(png_ptr, "png_image_read: alpha channel lost");
michael@0 4006
michael@0 4007 if (info_ptr->bit_depth == 16)
michael@0 4008 info_format |= PNG_FORMAT_FLAG_LINEAR;
michael@0 4009
michael@0 4010 # ifdef PNG_FORMAT_BGR_SUPPORTED
michael@0 4011 if (png_ptr->transformations & PNG_BGR)
michael@0 4012 info_format |= PNG_FORMAT_FLAG_BGR;
michael@0 4013 # endif
michael@0 4014
michael@0 4015 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
michael@0 4016 if (do_local_background == 2)
michael@0 4017 {
michael@0 4018 if (format & PNG_FORMAT_FLAG_AFIRST)
michael@0 4019 info_format |= PNG_FORMAT_FLAG_AFIRST;
michael@0 4020 }
michael@0 4021
michael@0 4022 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
michael@0 4023 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
michael@0 4024 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
michael@0 4025 {
michael@0 4026 if (do_local_background == 2)
michael@0 4027 png_error(png_ptr, "unexpected alpha swap transformation");
michael@0 4028
michael@0 4029 info_format |= PNG_FORMAT_FLAG_AFIRST;
michael@0 4030 }
michael@0 4031 # endif
michael@0 4032
michael@0 4033 /* This is actually an internal error. */
michael@0 4034 if (info_format != format)
michael@0 4035 png_error(png_ptr, "png_read_image: invalid transformations");
michael@0 4036 }
michael@0 4037
michael@0 4038 /* Now read the rows. If do_local_compose is set then it is necessary to use
michael@0 4039 * a local row buffer. The output will be GA, RGBA or BGRA and must be
michael@0 4040 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
michael@0 4041 * display acts as a flag.
michael@0 4042 */
michael@0 4043 {
michael@0 4044 png_voidp first_row = display->buffer;
michael@0 4045 ptrdiff_t row_bytes = display->row_stride;
michael@0 4046
michael@0 4047 if (linear)
michael@0 4048 row_bytes *= 2;
michael@0 4049
michael@0 4050 /* The following expression is designed to work correctly whether it gives
michael@0 4051 * a signed or an unsigned result.
michael@0 4052 */
michael@0 4053 if (row_bytes < 0)
michael@0 4054 {
michael@0 4055 char *ptr = png_voidcast(char*, first_row);
michael@0 4056 ptr += (image->height-1) * (-row_bytes);
michael@0 4057 first_row = png_voidcast(png_voidp, ptr);
michael@0 4058 }
michael@0 4059
michael@0 4060 display->first_row = first_row;
michael@0 4061 display->row_bytes = row_bytes;
michael@0 4062 }
michael@0 4063
michael@0 4064 if (do_local_compose)
michael@0 4065 {
michael@0 4066 int result;
michael@0 4067 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
michael@0 4068
michael@0 4069 display->local_row = row;
michael@0 4070 result = png_safe_execute(image, png_image_read_composite, display);
michael@0 4071 display->local_row = NULL;
michael@0 4072 png_free(png_ptr, row);
michael@0 4073
michael@0 4074 return result;
michael@0 4075 }
michael@0 4076
michael@0 4077 else if (do_local_background == 2)
michael@0 4078 {
michael@0 4079 int result;
michael@0 4080 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
michael@0 4081
michael@0 4082 display->local_row = row;
michael@0 4083 result = png_safe_execute(image, png_image_read_background, display);
michael@0 4084 display->local_row = NULL;
michael@0 4085 png_free(png_ptr, row);
michael@0 4086
michael@0 4087 return result;
michael@0 4088 }
michael@0 4089
michael@0 4090 else
michael@0 4091 {
michael@0 4092 png_alloc_size_t row_bytes = display->row_bytes;
michael@0 4093
michael@0 4094 while (--passes >= 0)
michael@0 4095 {
michael@0 4096 png_uint_32 y = image->height;
michael@0 4097 png_bytep row = png_voidcast(png_bytep, display->first_row);
michael@0 4098
michael@0 4099 while (y-- > 0)
michael@0 4100 {
michael@0 4101 png_read_row(png_ptr, row, NULL);
michael@0 4102 row += row_bytes;
michael@0 4103 }
michael@0 4104 }
michael@0 4105
michael@0 4106 return 1;
michael@0 4107 }
michael@0 4108 }
michael@0 4109
michael@0 4110 int PNGAPI
michael@0 4111 png_image_finish_read(png_imagep image, png_const_colorp background,
michael@0 4112 void *buffer, png_int_32 row_stride, void *colormap)
michael@0 4113 {
michael@0 4114 if (image != NULL && image->version == PNG_IMAGE_VERSION)
michael@0 4115 {
michael@0 4116 png_uint_32 check;
michael@0 4117
michael@0 4118 if (row_stride == 0)
michael@0 4119 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
michael@0 4120
michael@0 4121 if (row_stride < 0)
michael@0 4122 check = -row_stride;
michael@0 4123
michael@0 4124 else
michael@0 4125 check = row_stride;
michael@0 4126
michael@0 4127 if (image->opaque != NULL && buffer != NULL &&
michael@0 4128 check >= PNG_IMAGE_ROW_STRIDE(*image))
michael@0 4129 {
michael@0 4130 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
michael@0 4131 (image->colormap_entries > 0 && colormap != NULL))
michael@0 4132 {
michael@0 4133 int result;
michael@0 4134 png_image_read_control display;
michael@0 4135
michael@0 4136 memset(&display, 0, (sizeof display));
michael@0 4137 display.image = image;
michael@0 4138 display.buffer = buffer;
michael@0 4139 display.row_stride = row_stride;
michael@0 4140 display.colormap = colormap;
michael@0 4141 display.background = background;
michael@0 4142 display.local_row = NULL;
michael@0 4143
michael@0 4144 /* Choose the correct 'end' routine; for the color-map case all the
michael@0 4145 * setup has already been done.
michael@0 4146 */
michael@0 4147 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
michael@0 4148 result =
michael@0 4149 png_safe_execute(image, png_image_read_colormap, &display) &&
michael@0 4150 png_safe_execute(image, png_image_read_colormapped, &display);
michael@0 4151
michael@0 4152 else
michael@0 4153 result =
michael@0 4154 png_safe_execute(image, png_image_read_direct, &display);
michael@0 4155
michael@0 4156 png_image_free(image);
michael@0 4157 return result;
michael@0 4158 }
michael@0 4159
michael@0 4160 else
michael@0 4161 return png_image_error(image,
michael@0 4162 "png_image_finish_read[color-map]: no color-map");
michael@0 4163 }
michael@0 4164
michael@0 4165 else
michael@0 4166 return png_image_error(image,
michael@0 4167 "png_image_finish_read: invalid argument");
michael@0 4168 }
michael@0 4169
michael@0 4170 else if (image != NULL)
michael@0 4171 return png_image_error(image,
michael@0 4172 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
michael@0 4173
michael@0 4174 return 0;
michael@0 4175 }
michael@0 4176
michael@0 4177 #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
michael@0 4178 #endif /* PNG_READ_SUPPORTED */

mercurial