michael@0: michael@0: /* pngread.c - read a PNG file michael@0: * michael@0: * Last changed in libpng 1.6.10 [March 6, 2014] michael@0: * Copyright (c) 1998-2014 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: * michael@0: * This file contains routines that an application calls directly to michael@0: * read a PNG file or stream. michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) michael@0: # include michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_SUPPORTED michael@0: michael@0: /* Create a PNG structure for reading, and allocate any memory needed. */ michael@0: PNG_FUNCTION(png_structp,PNGAPI michael@0: png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr, michael@0: png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) michael@0: { michael@0: #ifndef PNG_USER_MEM_SUPPORTED michael@0: png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, michael@0: error_fn, warn_fn, NULL, NULL, NULL); michael@0: #else michael@0: return png_create_read_struct_2(user_png_ver, error_ptr, error_fn, michael@0: warn_fn, NULL, NULL, NULL); michael@0: } michael@0: michael@0: /* Alternate create PNG structure for reading, and allocate any memory michael@0: * needed. michael@0: */ michael@0: PNG_FUNCTION(png_structp,PNGAPI michael@0: png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, michael@0: png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, michael@0: png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) michael@0: { michael@0: png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, michael@0: error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); michael@0: #endif /* PNG_USER_MEM_SUPPORTED */ michael@0: michael@0: if (png_ptr != NULL) michael@0: { michael@0: png_ptr->mode = PNG_IS_READ_STRUCT; michael@0: michael@0: /* Added in libpng-1.6.0; this can be used to detect a read structure if michael@0: * required (it will be zero in a write structure.) michael@0: */ michael@0: # ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; michael@0: # endif michael@0: michael@0: # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED michael@0: png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; michael@0: michael@0: /* In stable builds only warn if an application error can be completely michael@0: * handled. michael@0: */ michael@0: # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC michael@0: png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; michael@0: # endif michael@0: # endif michael@0: michael@0: /* TODO: delay this, it can be done in png_init_io (if the app doesn't michael@0: * do it itself) avoiding setting the default function if it is not michael@0: * required. michael@0: */ michael@0: png_set_read_fn(png_ptr, NULL, NULL); michael@0: } michael@0: michael@0: return png_ptr; michael@0: } michael@0: michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: /* Read the information before the actual image data. This has been michael@0: * changed in v0.90 to allow reading a file that already has the magic michael@0: * bytes read from the stream. You can tell libpng how many bytes have michael@0: * been read from the beginning of the stream (up to the maximum of 8) michael@0: * via png_set_sig_bytes(), and we will only check the remaining bytes michael@0: * here. The application can then have access to the signature bytes we michael@0: * read if it is determined that this isn't a valid PNG file. michael@0: */ michael@0: void PNGAPI michael@0: png_read_info(png_structrp png_ptr, png_inforp info_ptr) michael@0: { michael@0: #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED michael@0: int keep; michael@0: #endif michael@0: michael@0: png_debug(1, "in png_read_info"); michael@0: michael@0: if (png_ptr == NULL || info_ptr == NULL) michael@0: return; michael@0: michael@0: /* Read and check the PNG file signature. */ michael@0: png_read_sig(png_ptr, info_ptr); michael@0: michael@0: for (;;) michael@0: { michael@0: png_uint_32 length = png_read_chunk_header(png_ptr); michael@0: png_uint_32 chunk_name = png_ptr->chunk_name; michael@0: michael@0: /* IDAT logic needs to happen here to simplify getting the two flags michael@0: * right. michael@0: */ michael@0: if (chunk_name == png_IDAT) michael@0: { michael@0: if (!(png_ptr->mode & PNG_HAVE_IHDR)) michael@0: png_chunk_error(png_ptr, "Missing IHDR before IDAT"); michael@0: michael@0: else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && michael@0: !(png_ptr->mode & PNG_HAVE_PLTE)) michael@0: png_chunk_error(png_ptr, "Missing PLTE before IDAT"); michael@0: michael@0: else if (png_ptr->mode & PNG_AFTER_IDAT) michael@0: png_chunk_benign_error(png_ptr, "Too many IDATs found"); michael@0: michael@0: png_ptr->mode |= PNG_HAVE_IDAT; michael@0: } michael@0: michael@0: else if (png_ptr->mode & PNG_HAVE_IDAT) michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: michael@0: /* This should be a binary subdivision search or a hash for michael@0: * matching the chunk name rather than a linear search. michael@0: */ michael@0: if (chunk_name == png_IHDR) michael@0: png_handle_IHDR(png_ptr, info_ptr, length); michael@0: michael@0: else if (chunk_name == png_IEND) michael@0: png_handle_IEND(png_ptr, info_ptr, length); michael@0: michael@0: #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED michael@0: else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) michael@0: { michael@0: png_handle_unknown(png_ptr, info_ptr, length, keep); michael@0: michael@0: if (chunk_name == png_PLTE) michael@0: png_ptr->mode |= PNG_HAVE_PLTE; michael@0: michael@0: else if (chunk_name == png_IDAT) michael@0: { michael@0: png_ptr->idat_size = 0; /* It has been consumed */ michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: else if (chunk_name == png_PLTE) michael@0: png_handle_PLTE(png_ptr, info_ptr, length); michael@0: michael@0: else if (chunk_name == png_IDAT) michael@0: { michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: png_have_info(png_ptr, info_ptr); michael@0: #endif michael@0: png_ptr->idat_size = length; michael@0: break; michael@0: } michael@0: michael@0: #ifdef PNG_READ_bKGD_SUPPORTED michael@0: else if (chunk_name == png_bKGD) michael@0: png_handle_bKGD(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_cHRM_SUPPORTED michael@0: else if (chunk_name == png_cHRM) michael@0: png_handle_cHRM(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_gAMA_SUPPORTED michael@0: else if (chunk_name == png_gAMA) michael@0: png_handle_gAMA(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_hIST_SUPPORTED michael@0: else if (chunk_name == png_hIST) michael@0: png_handle_hIST(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_oFFs_SUPPORTED michael@0: else if (chunk_name == png_oFFs) michael@0: png_handle_oFFs(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_pCAL_SUPPORTED michael@0: else if (chunk_name == png_pCAL) michael@0: png_handle_pCAL(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sCAL_SUPPORTED michael@0: else if (chunk_name == png_sCAL) michael@0: png_handle_sCAL(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_pHYs_SUPPORTED michael@0: else if (chunk_name == png_pHYs) michael@0: png_handle_pHYs(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sBIT_SUPPORTED michael@0: else if (chunk_name == png_sBIT) michael@0: png_handle_sBIT(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sRGB_SUPPORTED michael@0: else if (chunk_name == png_sRGB) michael@0: png_handle_sRGB(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_iCCP_SUPPORTED michael@0: else if (chunk_name == png_iCCP) michael@0: png_handle_iCCP(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sPLT_SUPPORTED michael@0: else if (chunk_name == png_sPLT) michael@0: png_handle_sPLT(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tEXt_SUPPORTED michael@0: else if (chunk_name == png_tEXt) michael@0: png_handle_tEXt(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tIME_SUPPORTED michael@0: else if (chunk_name == png_tIME) michael@0: png_handle_tIME(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tRNS_SUPPORTED michael@0: else if (chunk_name == png_tRNS) michael@0: png_handle_tRNS(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_zTXt_SUPPORTED michael@0: else if (chunk_name == png_zTXt) michael@0: png_handle_zTXt(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_iTXt_SUPPORTED michael@0: else if (chunk_name == png_iTXt) michael@0: png_handle_iTXt(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: else if (chunk_name == png_acTL) michael@0: png_handle_acTL(png_ptr, info_ptr, length); michael@0: michael@0: else if (chunk_name == png_fcTL) michael@0: png_handle_fcTL(png_ptr, info_ptr, length); michael@0: michael@0: else if (chunk_name == png_fdAT) michael@0: png_handle_fdAT(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: else michael@0: png_handle_unknown(png_ptr, info_ptr, length, michael@0: PNG_HANDLE_CHUNK_AS_DEFAULT); michael@0: } michael@0: } michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_APNG_SUPPORTED michael@0: void PNGAPI michael@0: png_read_frame_head(png_structp png_ptr, png_infop info_ptr) michael@0: { michael@0: png_byte have_chunk_after_DAT; /* after IDAT or after fdAT */ michael@0: michael@0: png_debug(0, "Reading frame head"); michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_acTL)) michael@0: png_error(png_ptr, "attempt to png_read_frame_head() but " michael@0: "no acTL present"); michael@0: michael@0: /* do nothing for the main IDAT */ michael@0: if (png_ptr->num_frames_read == 0) michael@0: return; michael@0: michael@0: png_read_reset(png_ptr); michael@0: png_ptr->flags &= ~PNG_FLAG_ROW_INIT; michael@0: png_ptr->mode &= ~PNG_HAVE_fcTL; michael@0: michael@0: have_chunk_after_DAT = 0; michael@0: for (;;) michael@0: { michael@0: png_uint_32 length = png_read_chunk_header(png_ptr); michael@0: michael@0: if (png_ptr->chunk_name == png_IDAT) michael@0: { michael@0: /* discard trailing IDATs for the first frame */ michael@0: if (have_chunk_after_DAT || png_ptr->num_frames_read > 1) michael@0: png_error(png_ptr, "png_read_frame_head(): out of place IDAT"); michael@0: png_crc_finish(png_ptr, length); michael@0: } michael@0: michael@0: else if (png_ptr->chunk_name == png_fcTL) michael@0: { michael@0: png_handle_fcTL(png_ptr, info_ptr, length); michael@0: have_chunk_after_DAT = 1; michael@0: } michael@0: michael@0: else if (png_ptr->chunk_name == png_fdAT) michael@0: { michael@0: png_ensure_sequence_number(png_ptr, length); michael@0: michael@0: /* discard trailing fdATs for frames other than the first */ michael@0: if (!have_chunk_after_DAT && png_ptr->num_frames_read > 1) michael@0: png_crc_finish(png_ptr, length - 4); michael@0: else if(png_ptr->mode & PNG_HAVE_fcTL) michael@0: { michael@0: png_ptr->idat_size = length - 4; michael@0: png_ptr->mode |= PNG_HAVE_IDAT; michael@0: michael@0: break; michael@0: } michael@0: else michael@0: png_error(png_ptr, "png_read_frame_head(): out of place fdAT"); michael@0: } michael@0: else michael@0: { michael@0: png_warning(png_ptr, "Skipped (ignored) a chunk " michael@0: "between APNG chunks"); michael@0: png_crc_finish(png_ptr, length); michael@0: } michael@0: } michael@0: } michael@0: #endif /* PNG_READ_APNG_SUPPORTED */ michael@0: michael@0: /* Optional call to update the users info_ptr structure */ michael@0: void PNGAPI michael@0: png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) michael@0: { michael@0: png_debug(1, "in png_read_update_info"); michael@0: michael@0: if (png_ptr != NULL) michael@0: { michael@0: if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) michael@0: { michael@0: png_read_start_row(png_ptr); michael@0: michael@0: # ifdef PNG_READ_TRANSFORMS_SUPPORTED michael@0: png_read_transform_info(png_ptr, info_ptr); michael@0: # else michael@0: PNG_UNUSED(info_ptr) michael@0: # endif michael@0: } michael@0: michael@0: /* New in 1.6.0 this avoids the bug of doing the initializations twice */ michael@0: else michael@0: png_app_error(png_ptr, michael@0: "png_read_update_info/png_start_read_image: duplicate call"); michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: /* Initialize palette, background, etc, after transformations michael@0: * are set, but before any reading takes place. This allows michael@0: * the user to obtain a gamma-corrected palette, for example. michael@0: * If the user doesn't call this, we will do it ourselves. michael@0: */ michael@0: void PNGAPI michael@0: png_start_read_image(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_start_read_image"); michael@0: michael@0: if (png_ptr != NULL) michael@0: { michael@0: if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) michael@0: png_read_start_row(png_ptr); michael@0: michael@0: /* New in 1.6.0 this avoids the bug of doing the initializations twice */ michael@0: else michael@0: png_app_error(png_ptr, michael@0: "png_start_read_image/png_read_update_info: duplicate call"); michael@0: } michael@0: } michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: /* Undoes intrapixel differencing, michael@0: * NOTE: this is apparently only supported in the 'sequential' reader. michael@0: */ michael@0: static void michael@0: png_do_read_intrapixel(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_debug(1, "in png_do_read_intrapixel"); michael@0: michael@0: if ( michael@0: (row_info->color_type & PNG_COLOR_MASK_COLOR)) michael@0: { michael@0: int bytes_per_pixel; michael@0: png_uint_32 row_width = row_info->width; michael@0: michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: png_bytep rp; michael@0: png_uint_32 i; michael@0: michael@0: if (row_info->color_type == PNG_COLOR_TYPE_RGB) michael@0: bytes_per_pixel = 3; michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: bytes_per_pixel = 4; michael@0: michael@0: else michael@0: return; michael@0: michael@0: for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) michael@0: { michael@0: *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff); michael@0: *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff); michael@0: } michael@0: } michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: png_bytep rp; michael@0: png_uint_32 i; michael@0: michael@0: if (row_info->color_type == PNG_COLOR_TYPE_RGB) michael@0: bytes_per_pixel = 6; michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: bytes_per_pixel = 8; michael@0: michael@0: else michael@0: return; michael@0: michael@0: for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) michael@0: { michael@0: png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); michael@0: png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); michael@0: png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); michael@0: png_uint_32 red = (s0 + s1 + 65536) & 0xffff; michael@0: png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; michael@0: *(rp ) = (png_byte)((red >> 8) & 0xff); michael@0: *(rp + 1) = (png_byte)(red & 0xff); michael@0: *(rp + 4) = (png_byte)((blue >> 8) & 0xff); michael@0: *(rp + 5) = (png_byte)(blue & 0xff); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif /* PNG_MNG_FEATURES_SUPPORTED */ michael@0: michael@0: void PNGAPI michael@0: png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row) michael@0: { michael@0: png_row_info row_info; michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_debug2(1, "in png_read_row (row %lu, pass %d)", michael@0: (unsigned long)png_ptr->row_number, png_ptr->pass); michael@0: michael@0: /* png_read_start_row sets the information (in particular iwidth) for this michael@0: * interlace pass. michael@0: */ michael@0: if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) michael@0: png_read_start_row(png_ptr); michael@0: michael@0: /* 1.5.6: row_info moved out of png_struct to a local here. */ michael@0: row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ michael@0: row_info.color_type = png_ptr->color_type; michael@0: row_info.bit_depth = png_ptr->bit_depth; michael@0: row_info.channels = png_ptr->channels; michael@0: row_info.pixel_depth = png_ptr->pixel_depth; michael@0: row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); michael@0: michael@0: if (png_ptr->row_number == 0 && png_ptr->pass == 0) michael@0: { michael@0: /* Check for transforms that have been set but were defined out */ michael@0: #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_INVERT_MONO) michael@0: png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_FILLER) michael@0: png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ michael@0: !defined(PNG_READ_PACKSWAP_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_PACKSWAP) michael@0: png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_PACK) michael@0: png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_SHIFT) michael@0: png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_BGR) michael@0: png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_SWAP_BYTES) michael@0: png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); michael@0: #endif michael@0: } michael@0: michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: /* If interlaced and we do not need a new row, combine row and return. michael@0: * Notice that the pixels we have from previous rows have been transformed michael@0: * already; we can only combine like with like (transformed or michael@0: * untransformed) and, because of the libpng API for interlaced images, this michael@0: * means we must transform before de-interlacing. michael@0: */ michael@0: if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) michael@0: { michael@0: switch (png_ptr->pass) michael@0: { michael@0: case 0: michael@0: if (png_ptr->row_number & 0x07) michael@0: { michael@0: if (dsp_row != NULL) michael@0: png_combine_row(png_ptr, dsp_row, 1/*display*/); michael@0: png_read_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 1: michael@0: if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) michael@0: { michael@0: if (dsp_row != NULL) michael@0: png_combine_row(png_ptr, dsp_row, 1/*display*/); michael@0: michael@0: png_read_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 2: michael@0: if ((png_ptr->row_number & 0x07) != 4) michael@0: { michael@0: if (dsp_row != NULL && (png_ptr->row_number & 4)) michael@0: png_combine_row(png_ptr, dsp_row, 1/*display*/); michael@0: michael@0: png_read_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 3: michael@0: if ((png_ptr->row_number & 3) || png_ptr->width < 3) michael@0: { michael@0: if (dsp_row != NULL) michael@0: png_combine_row(png_ptr, dsp_row, 1/*display*/); michael@0: michael@0: png_read_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 4: michael@0: if ((png_ptr->row_number & 3) != 2) michael@0: { michael@0: if (dsp_row != NULL && (png_ptr->row_number & 2)) michael@0: png_combine_row(png_ptr, dsp_row, 1/*display*/); michael@0: michael@0: png_read_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 5: michael@0: if ((png_ptr->row_number & 1) || png_ptr->width < 2) michael@0: { michael@0: if (dsp_row != NULL) michael@0: png_combine_row(png_ptr, dsp_row, 1/*display*/); michael@0: michael@0: png_read_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: case 6: michael@0: if (!(png_ptr->row_number & 1)) michael@0: { michael@0: png_read_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IDAT)) michael@0: png_error(png_ptr, "Invalid attempt to read row data"); michael@0: michael@0: /* Fill the row with IDAT data: */ michael@0: png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); michael@0: michael@0: if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) michael@0: { michael@0: if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) michael@0: png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, michael@0: png_ptr->prev_row + 1, png_ptr->row_buf[0]); michael@0: else michael@0: png_error(png_ptr, "bad adaptive filter value"); michael@0: } michael@0: michael@0: /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before michael@0: * 1.5.6, while the buffer really is this big in current versions of libpng michael@0: * it may not be in the future, so this was changed just to copy the michael@0: * interlaced count: michael@0: */ michael@0: memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); michael@0: michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && michael@0: (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) michael@0: { michael@0: /* Intrapixel differencing */ michael@0: png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_TRANSFORMS_SUPPORTED michael@0: if (png_ptr->transformations) michael@0: png_do_read_transformations(png_ptr, &row_info); michael@0: #endif michael@0: michael@0: /* The transformed pixel depth should match the depth now in row_info. */ michael@0: if (png_ptr->transformed_pixel_depth == 0) michael@0: { michael@0: png_ptr->transformed_pixel_depth = row_info.pixel_depth; michael@0: if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) michael@0: png_error(png_ptr, "sequential row overflow"); michael@0: } michael@0: michael@0: else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) michael@0: png_error(png_ptr, "internal sequential row size calculation error"); michael@0: michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: /* Blow up interlaced rows to full size */ michael@0: if (png_ptr->interlaced && michael@0: (png_ptr->transformations & PNG_INTERLACE)) michael@0: { michael@0: if (png_ptr->pass < 6) michael@0: png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, michael@0: png_ptr->transformations); michael@0: michael@0: if (dsp_row != NULL) michael@0: png_combine_row(png_ptr, dsp_row, 1/*display*/); michael@0: michael@0: if (row != NULL) michael@0: png_combine_row(png_ptr, row, 0/*row*/); michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: if (row != NULL) michael@0: png_combine_row(png_ptr, row, -1/*ignored*/); michael@0: michael@0: if (dsp_row != NULL) michael@0: png_combine_row(png_ptr, dsp_row, -1/*ignored*/); michael@0: } michael@0: png_read_finish_row(png_ptr); michael@0: michael@0: if (png_ptr->read_row_fn != NULL) michael@0: (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); michael@0: michael@0: } michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: /* Read one or more rows of image data. If the image is interlaced, michael@0: * and png_set_interlace_handling() has been called, the rows need to michael@0: * contain the contents of the rows from the previous pass. If the michael@0: * image has alpha or transparency, and png_handle_alpha()[*] has been michael@0: * called, the rows contents must be initialized to the contents of the michael@0: * screen. michael@0: * michael@0: * "row" holds the actual image, and pixels are placed in it michael@0: * as they arrive. If the image is displayed after each pass, it will michael@0: * appear to "sparkle" in. "display_row" can be used to display a michael@0: * "chunky" progressive image, with finer detail added as it becomes michael@0: * available. If you do not want this "chunky" display, you may pass michael@0: * NULL for display_row. If you do not want the sparkle display, and michael@0: * you have not called png_handle_alpha(), you may pass NULL for rows. michael@0: * If you have called png_handle_alpha(), and the image has either an michael@0: * alpha channel or a transparency chunk, you must provide a buffer for michael@0: * rows. In this case, you do not have to provide a display_row buffer michael@0: * also, but you may. If the image is not interlaced, or if you have michael@0: * not called png_set_interlace_handling(), the display_row buffer will michael@0: * be ignored, so pass NULL to it. michael@0: * michael@0: * [*] png_handle_alpha() does not exist yet, as of this version of libpng michael@0: */ michael@0: michael@0: void PNGAPI michael@0: png_read_rows(png_structrp png_ptr, png_bytepp row, michael@0: png_bytepp display_row, png_uint_32 num_rows) michael@0: { michael@0: png_uint_32 i; michael@0: png_bytepp rp; michael@0: png_bytepp dp; michael@0: michael@0: png_debug(1, "in png_read_rows"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: rp = row; michael@0: dp = display_row; michael@0: if (rp != NULL && dp != NULL) michael@0: for (i = 0; i < num_rows; i++) michael@0: { michael@0: png_bytep rptr = *rp++; michael@0: png_bytep dptr = *dp++; michael@0: michael@0: png_read_row(png_ptr, rptr, dptr); michael@0: } michael@0: michael@0: else if (rp != NULL) michael@0: for (i = 0; i < num_rows; i++) michael@0: { michael@0: png_bytep rptr = *rp; michael@0: png_read_row(png_ptr, rptr, NULL); michael@0: rp++; michael@0: } michael@0: michael@0: else if (dp != NULL) michael@0: for (i = 0; i < num_rows; i++) michael@0: { michael@0: png_bytep dptr = *dp; michael@0: png_read_row(png_ptr, NULL, dptr); michael@0: dp++; michael@0: } michael@0: } michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: /* Read the entire image. If the image has an alpha channel or a tRNS michael@0: * chunk, and you have called png_handle_alpha()[*], you will need to michael@0: * initialize the image to the current image that PNG will be overlaying. michael@0: * We set the num_rows again here, in case it was incorrectly set in michael@0: * png_read_start_row() by a call to png_read_update_info() or michael@0: * png_start_read_image() if png_set_interlace_handling() wasn't called michael@0: * prior to either of these functions like it should have been. You can michael@0: * only call this function once. If you desire to have an image for michael@0: * each pass of a interlaced image, use png_read_rows() instead. michael@0: * michael@0: * [*] png_handle_alpha() does not exist yet, as of this version of libpng michael@0: */ michael@0: void PNGAPI michael@0: png_read_image(png_structrp png_ptr, png_bytepp image) michael@0: { michael@0: png_uint_32 i, image_height; michael@0: int pass, j; michael@0: png_bytepp rp; michael@0: michael@0: png_debug(1, "in png_read_image"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: #ifdef PNG_READ_INTERLACING_SUPPORTED michael@0: if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) michael@0: { michael@0: pass = png_set_interlace_handling(png_ptr); michael@0: /* And make sure transforms are initialized. */ michael@0: png_start_read_image(png_ptr); michael@0: } michael@0: else michael@0: { michael@0: if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE)) michael@0: { michael@0: /* Caller called png_start_read_image or png_read_update_info without michael@0: * first turning on the PNG_INTERLACE transform. We can fix this here, michael@0: * but the caller should do it! michael@0: */ michael@0: png_warning(png_ptr, "Interlace handling should be turned on when " michael@0: "using png_read_image"); michael@0: /* Make sure this is set correctly */ michael@0: png_ptr->num_rows = png_ptr->height; michael@0: } michael@0: michael@0: /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in michael@0: * the above error case. michael@0: */ michael@0: pass = png_set_interlace_handling(png_ptr); michael@0: } michael@0: #else michael@0: if (png_ptr->interlaced) michael@0: png_error(png_ptr, michael@0: "Cannot read interlaced image -- interlace handler disabled"); michael@0: michael@0: pass = 1; michael@0: #endif michael@0: michael@0: image_height=png_ptr->height; michael@0: michael@0: for (j = 0; j < pass; j++) michael@0: { michael@0: rp = image; michael@0: for (i = 0; i < image_height; i++) michael@0: { michael@0: png_read_row(png_ptr, *rp, NULL); michael@0: rp++; michael@0: } michael@0: } michael@0: } michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: /* Read the end of the PNG file. Will not read past the end of the michael@0: * file, will verify the end is accurate, and will read any comments michael@0: * or time information at the end of the file, if info is not NULL. michael@0: */ michael@0: void PNGAPI michael@0: png_read_end(png_structrp png_ptr, png_inforp info_ptr) michael@0: { michael@0: #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED michael@0: int keep; michael@0: #endif michael@0: michael@0: png_debug(1, "in png_read_end"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* If png_read_end is called in the middle of reading the rows there may michael@0: * still be pending IDAT data and an owned zstream. Deal with this here. michael@0: */ michael@0: #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED michael@0: if (!png_chunk_unknown_handling(png_ptr, png_IDAT)) michael@0: #endif michael@0: png_read_finish_IDAT(png_ptr); michael@0: michael@0: #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED michael@0: /* Report invalid palette index; added at libng-1.5.10 */ michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && michael@0: png_ptr->num_palette_max > png_ptr->num_palette) michael@0: png_benign_error(png_ptr, "Read palette index exceeding num_palette"); michael@0: #endif michael@0: michael@0: do michael@0: { michael@0: png_uint_32 length = png_read_chunk_header(png_ptr); michael@0: png_uint_32 chunk_name = png_ptr->chunk_name; michael@0: michael@0: if (chunk_name == png_IEND) michael@0: png_handle_IEND(png_ptr, info_ptr, length); michael@0: michael@0: else if (chunk_name == png_IHDR) michael@0: png_handle_IHDR(png_ptr, info_ptr, length); michael@0: michael@0: else if (info_ptr == NULL) michael@0: png_crc_finish(png_ptr, length); michael@0: michael@0: #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED michael@0: else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) michael@0: { michael@0: if (chunk_name == png_IDAT) michael@0: { michael@0: if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) michael@0: png_benign_error(png_ptr, "Too many IDATs found"); michael@0: } michael@0: png_handle_unknown(png_ptr, info_ptr, length, keep); michael@0: if (chunk_name == png_PLTE) michael@0: png_ptr->mode |= PNG_HAVE_PLTE; michael@0: } michael@0: #endif michael@0: michael@0: else if (chunk_name == png_IDAT) michael@0: { michael@0: /* Zero length IDATs are legal after the last IDAT has been michael@0: * read, but not after other chunks have been read. michael@0: */ michael@0: if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) michael@0: png_benign_error(png_ptr, "Too many IDATs found"); michael@0: michael@0: png_crc_finish(png_ptr, length); michael@0: } michael@0: else if (chunk_name == png_PLTE) michael@0: png_handle_PLTE(png_ptr, info_ptr, length); michael@0: michael@0: #ifdef PNG_READ_bKGD_SUPPORTED michael@0: else if (chunk_name == png_bKGD) michael@0: png_handle_bKGD(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_cHRM_SUPPORTED michael@0: else if (chunk_name == png_cHRM) michael@0: png_handle_cHRM(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_gAMA_SUPPORTED michael@0: else if (chunk_name == png_gAMA) michael@0: png_handle_gAMA(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_hIST_SUPPORTED michael@0: else if (chunk_name == png_hIST) michael@0: png_handle_hIST(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_oFFs_SUPPORTED michael@0: else if (chunk_name == png_oFFs) michael@0: png_handle_oFFs(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_pCAL_SUPPORTED michael@0: else if (chunk_name == png_pCAL) michael@0: png_handle_pCAL(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sCAL_SUPPORTED michael@0: else if (chunk_name == png_sCAL) michael@0: png_handle_sCAL(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_pHYs_SUPPORTED michael@0: else if (chunk_name == png_pHYs) michael@0: png_handle_pHYs(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sBIT_SUPPORTED michael@0: else if (chunk_name == png_sBIT) michael@0: png_handle_sBIT(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sRGB_SUPPORTED michael@0: else if (chunk_name == png_sRGB) michael@0: png_handle_sRGB(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_iCCP_SUPPORTED michael@0: else if (chunk_name == png_iCCP) michael@0: png_handle_iCCP(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_sPLT_SUPPORTED michael@0: else if (chunk_name == png_sPLT) michael@0: png_handle_sPLT(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tEXt_SUPPORTED michael@0: else if (chunk_name == png_tEXt) michael@0: png_handle_tEXt(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tIME_SUPPORTED michael@0: else if (chunk_name == png_tIME) michael@0: png_handle_tIME(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_tRNS_SUPPORTED michael@0: else if (chunk_name == png_tRNS) michael@0: png_handle_tRNS(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_zTXt_SUPPORTED michael@0: else if (chunk_name == png_zTXt) michael@0: png_handle_zTXt(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_iTXt_SUPPORTED michael@0: else if (chunk_name == png_iTXt) michael@0: png_handle_iTXt(png_ptr, info_ptr, length); michael@0: #endif michael@0: michael@0: else michael@0: png_handle_unknown(png_ptr, info_ptr, length, michael@0: PNG_HANDLE_CHUNK_AS_DEFAULT); michael@0: } while (!(png_ptr->mode & PNG_HAVE_IEND)); michael@0: } michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: /* Free all memory used in the read struct */ michael@0: static void michael@0: png_read_destroy(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_read_destroy"); michael@0: michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: png_destroy_gamma_table(png_ptr); michael@0: #endif michael@0: michael@0: png_free(png_ptr, png_ptr->big_row_buf); michael@0: png_free(png_ptr, png_ptr->big_prev_row); michael@0: png_free(png_ptr, png_ptr->read_buffer); michael@0: michael@0: #ifdef PNG_READ_QUANTIZE_SUPPORTED michael@0: png_free(png_ptr, png_ptr->palette_lookup); michael@0: png_free(png_ptr, png_ptr->quantize_index); michael@0: #endif michael@0: michael@0: if (png_ptr->free_me & PNG_FREE_PLTE) michael@0: png_zfree(png_ptr, png_ptr->palette); michael@0: png_ptr->free_me &= ~PNG_FREE_PLTE; michael@0: michael@0: #if defined(PNG_tRNS_SUPPORTED) || \ michael@0: defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) michael@0: if (png_ptr->free_me & PNG_FREE_TRNS) michael@0: png_free(png_ptr, png_ptr->trans_alpha); michael@0: png_ptr->free_me &= ~PNG_FREE_TRNS; michael@0: #endif michael@0: michael@0: inflateEnd(&png_ptr->zstream); michael@0: michael@0: #ifdef PNG_PROGRESSIVE_READ_SUPPORTED michael@0: png_free(png_ptr, png_ptr->save_buffer); michael@0: #endif michael@0: michael@0: #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\ michael@0: defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) michael@0: png_free(png_ptr, png_ptr->unknown_chunk.data); michael@0: #endif michael@0: michael@0: #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED michael@0: png_free(png_ptr, png_ptr->chunk_list); michael@0: #endif michael@0: michael@0: /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error michael@0: * callbacks are still set at this point. They are required to complete the michael@0: * destruction of the png_struct itself. michael@0: */ michael@0: } michael@0: michael@0: /* Free all memory used by the read */ michael@0: void PNGAPI michael@0: png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, michael@0: png_infopp end_info_ptr_ptr) michael@0: { michael@0: png_structrp png_ptr = NULL; michael@0: michael@0: png_debug(1, "in png_destroy_read_struct"); michael@0: michael@0: if (png_ptr_ptr != NULL) michael@0: png_ptr = *png_ptr_ptr; michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* libpng 1.6.0: use the API to destroy info structs to ensure consistent michael@0: * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API. michael@0: * The extra was, apparently, unnecessary yet this hides memory leak bugs. michael@0: */ michael@0: png_destroy_info_struct(png_ptr, end_info_ptr_ptr); michael@0: png_destroy_info_struct(png_ptr, info_ptr_ptr); michael@0: michael@0: *png_ptr_ptr = NULL; michael@0: png_read_destroy(png_ptr); michael@0: png_destroy_png_struct(png_ptr); michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->read_row_fn = read_row_fn; michael@0: } michael@0: michael@0: michael@0: #ifdef PNG_SEQUENTIAL_READ_SUPPORTED michael@0: #ifdef PNG_INFO_IMAGE_SUPPORTED michael@0: void PNGAPI michael@0: png_read_png(png_structrp png_ptr, png_inforp info_ptr, michael@0: int transforms, michael@0: voidp params) michael@0: { michael@0: if (png_ptr == NULL || info_ptr == NULL) michael@0: return; michael@0: michael@0: /* png_read_info() gives us all of the information from the michael@0: * PNG file before the first IDAT (image data chunk). michael@0: */ michael@0: png_read_info(png_ptr, info_ptr); michael@0: if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep))) michael@0: png_error(png_ptr, "Image is too high to process with png_read_png()"); michael@0: michael@0: /* -------------- image transformations start here ------------------- */ michael@0: /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM michael@0: * is not implemented. This will only happen in de-configured (non-default) michael@0: * libpng builds. The results can be unexpected - png_read_png may return michael@0: * short or mal-formed rows because the transform is skipped. michael@0: */ michael@0: michael@0: /* Tell libpng to strip 16-bit/color files down to 8 bits per color. michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_SCALE_16) michael@0: /* Added at libpng-1.5.4. "strip_16" produces the same result that it michael@0: * did in earlier versions, while "scale_16" is now more accurate. michael@0: */ michael@0: #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED michael@0: png_set_scale_16(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported"); michael@0: #endif michael@0: michael@0: /* If both SCALE and STRIP are required pngrtran will effectively cancel the michael@0: * latter by doing SCALE first. This is ok and allows apps not to check for michael@0: * which is supported to get the right answer. michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_STRIP_16) michael@0: #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED michael@0: png_set_strip_16(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported"); michael@0: #endif michael@0: michael@0: /* Strip alpha bytes from the input data without combining with michael@0: * the background (not recommended). michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_STRIP_ALPHA) michael@0: #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED michael@0: png_set_strip_alpha(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported"); michael@0: #endif michael@0: michael@0: /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single michael@0: * byte into separate bytes (useful for paletted and grayscale images). michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_PACKING) michael@0: #ifdef PNG_READ_PACK_SUPPORTED michael@0: png_set_packing(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); michael@0: #endif michael@0: michael@0: /* Change the order of packed pixels to least significant bit first michael@0: * (not useful if you are using png_set_packing). michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_PACKSWAP) michael@0: #ifdef PNG_READ_PACKSWAP_SUPPORTED michael@0: png_set_packswap(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); michael@0: #endif michael@0: michael@0: /* Expand paletted colors into true RGB triplets michael@0: * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel michael@0: * Expand paletted or RGB images with transparency to full alpha michael@0: * channels so the data will be available as RGBA quartets. michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_EXPAND) michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: png_set_expand(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported"); michael@0: #endif michael@0: michael@0: /* We don't handle background color or gamma transformation or quantizing. michael@0: */ michael@0: michael@0: /* Invert monochrome files to have 0 as white and 1 as black michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_INVERT_MONO) michael@0: #ifdef PNG_READ_INVERT_SUPPORTED michael@0: png_set_invert_mono(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); michael@0: #endif michael@0: michael@0: /* If you want to shift the pixel values from the range [0,255] or michael@0: * [0,65535] to the original [0,7] or [0,31], or whatever range the michael@0: * colors were originally in: michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_SHIFT) michael@0: #ifdef PNG_READ_SHIFT_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_sBIT) michael@0: png_set_shift(png_ptr, &info_ptr->sig_bit); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); michael@0: #endif michael@0: michael@0: /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ michael@0: if (transforms & PNG_TRANSFORM_BGR) michael@0: #ifdef PNG_READ_BGR_SUPPORTED michael@0: png_set_bgr(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); michael@0: #endif michael@0: michael@0: /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ michael@0: if (transforms & PNG_TRANSFORM_SWAP_ALPHA) michael@0: #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED michael@0: png_set_swap_alpha(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); michael@0: #endif michael@0: michael@0: /* Swap bytes of 16-bit files to least significant byte first */ michael@0: if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) michael@0: #ifdef PNG_READ_SWAP_SUPPORTED michael@0: png_set_swap(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); michael@0: #endif michael@0: michael@0: /* Added at libpng-1.2.41 */ michael@0: /* Invert the alpha channel from opacity to transparency */ michael@0: if (transforms & PNG_TRANSFORM_INVERT_ALPHA) michael@0: #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED michael@0: png_set_invert_alpha(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); michael@0: #endif michael@0: michael@0: /* Added at libpng-1.2.41 */ michael@0: /* Expand grayscale image to RGB */ michael@0: if (transforms & PNG_TRANSFORM_GRAY_TO_RGB) michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: png_set_gray_to_rgb(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported"); michael@0: #endif michael@0: michael@0: /* Added at libpng-1.5.4 */ michael@0: if (transforms & PNG_TRANSFORM_EXPAND_16) michael@0: #ifdef PNG_READ_EXPAND_16_SUPPORTED michael@0: png_set_expand_16(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported"); michael@0: #endif michael@0: michael@0: /* We don't handle adding filler bytes */ michael@0: michael@0: /* We use png_read_image and rely on that for interlace handling, but we also michael@0: * call png_read_update_info therefore must turn on interlace handling now: michael@0: */ michael@0: (void)png_set_interlace_handling(png_ptr); michael@0: michael@0: /* Optional call to gamma correct and add the background to the palette michael@0: * and update info structure. REQUIRED if you are expecting libpng to michael@0: * update the palette for you (i.e., you selected such a transform above). michael@0: */ michael@0: png_read_update_info(png_ptr, info_ptr); michael@0: michael@0: /* -------------- image transformations end here ------------------- */ michael@0: michael@0: png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); michael@0: if (info_ptr->row_pointers == NULL) michael@0: { michael@0: png_uint_32 iptr; michael@0: michael@0: info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr, michael@0: info_ptr->height * (sizeof (png_bytep)))); michael@0: michael@0: for (iptr=0; iptrheight; iptr++) michael@0: info_ptr->row_pointers[iptr] = NULL; michael@0: michael@0: info_ptr->free_me |= PNG_FREE_ROWS; michael@0: michael@0: for (iptr = 0; iptr < info_ptr->height; iptr++) michael@0: info_ptr->row_pointers[iptr] = png_voidcast(png_bytep, michael@0: png_malloc(png_ptr, info_ptr->rowbytes)); michael@0: } michael@0: michael@0: png_read_image(png_ptr, info_ptr->row_pointers); michael@0: info_ptr->valid |= PNG_INFO_IDAT; michael@0: michael@0: /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ michael@0: png_read_end(png_ptr, info_ptr); michael@0: michael@0: PNG_UNUSED(params) michael@0: } michael@0: #endif /* PNG_INFO_IMAGE_SUPPORTED */ michael@0: #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ michael@0: michael@0: #ifdef PNG_SIMPLIFIED_READ_SUPPORTED michael@0: /* SIMPLIFIED READ michael@0: * michael@0: * This code currently relies on the sequential reader, though it could easily michael@0: * be made to work with the progressive one. michael@0: */ michael@0: /* Arguments to png_image_finish_read: */ michael@0: michael@0: /* Encoding of PNG data (used by the color-map code) */ michael@0: # define P_NOTSET 0 /* File encoding not yet known */ michael@0: # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */ michael@0: # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ michael@0: # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ michael@0: # define P_LINEAR8 4 /* 8-bit linear: only from a file value */ michael@0: michael@0: /* Color-map processing: after libpng has run on the PNG image further michael@0: * processing may be needed to conver the data to color-map indicies. michael@0: */ michael@0: #define PNG_CMAP_NONE 0 michael@0: #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ michael@0: #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ michael@0: #define PNG_CMAP_RGB 3 /* Process RGB data */ michael@0: #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ michael@0: michael@0: /* The following document where the background is for each processing case. */ michael@0: #define PNG_CMAP_NONE_BACKGROUND 256 michael@0: #define PNG_CMAP_GA_BACKGROUND 231 michael@0: #define PNG_CMAP_TRANS_BACKGROUND 254 michael@0: #define PNG_CMAP_RGB_BACKGROUND 256 michael@0: #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 michael@0: michael@0: typedef struct michael@0: { michael@0: /* Arguments: */ michael@0: png_imagep image; michael@0: png_voidp buffer; michael@0: png_int_32 row_stride; michael@0: png_voidp colormap; michael@0: png_const_colorp background; michael@0: /* Local variables: */ michael@0: png_voidp local_row; michael@0: png_voidp first_row; michael@0: ptrdiff_t row_bytes; /* step between rows */ michael@0: int file_encoding; /* E_ values above */ michael@0: png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */ michael@0: int colormap_processing; /* PNG_CMAP_ values above */ michael@0: } png_image_read_control; michael@0: michael@0: /* Do all the *safe* initialization - 'safe' means that png_error won't be michael@0: * called, so setting up the jmp_buf is not required. This means that anything michael@0: * called from here must *not* call png_malloc - it has to call png_malloc_warn michael@0: * instead so that control is returned safely back to this routine. michael@0: */ michael@0: static int michael@0: png_image_read_init(png_imagep image) michael@0: { michael@0: if (image->opaque == NULL) michael@0: { michael@0: png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, michael@0: png_safe_error, png_safe_warning); michael@0: michael@0: /* And set the rest of the structure to NULL to ensure that the various michael@0: * fields are consistent. michael@0: */ michael@0: memset(image, 0, (sizeof *image)); michael@0: image->version = PNG_IMAGE_VERSION; michael@0: michael@0: if (png_ptr != NULL) michael@0: { michael@0: png_infop info_ptr = png_create_info_struct(png_ptr); michael@0: michael@0: if (info_ptr != NULL) michael@0: { michael@0: png_controlp control = png_voidcast(png_controlp, michael@0: png_malloc_warn(png_ptr, (sizeof *control))); michael@0: michael@0: if (control != NULL) michael@0: { michael@0: memset(control, 0, (sizeof *control)); michael@0: michael@0: control->png_ptr = png_ptr; michael@0: control->info_ptr = info_ptr; michael@0: control->for_write = 0; michael@0: michael@0: image->opaque = control; michael@0: return 1; michael@0: } michael@0: michael@0: /* Error clean up */ michael@0: png_destroy_info_struct(png_ptr, &info_ptr); michael@0: } michael@0: michael@0: png_destroy_read_struct(&png_ptr, NULL, NULL); michael@0: } michael@0: michael@0: return png_image_error(image, "png_image_read: out of memory"); michael@0: } michael@0: michael@0: return png_image_error(image, "png_image_read: opaque pointer not NULL"); michael@0: } michael@0: michael@0: /* Utility to find the base format of a PNG file from a png_struct. */ michael@0: static png_uint_32 michael@0: png_image_format(png_structrp png_ptr) michael@0: { michael@0: png_uint_32 format = 0; michael@0: michael@0: if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) michael@0: format |= PNG_FORMAT_FLAG_COLOR; michael@0: michael@0: if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) michael@0: format |= PNG_FORMAT_FLAG_ALPHA; michael@0: michael@0: /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS michael@0: * sets the png_struct fields; that's all we are interested in here. The michael@0: * precise interaction with an app call to png_set_tRNS and PNG file reading michael@0: * is unclear. michael@0: */ michael@0: else if (png_ptr->num_trans > 0) michael@0: format |= PNG_FORMAT_FLAG_ALPHA; michael@0: michael@0: if (png_ptr->bit_depth == 16) michael@0: format |= PNG_FORMAT_FLAG_LINEAR; michael@0: michael@0: if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE) michael@0: format |= PNG_FORMAT_FLAG_COLORMAP; michael@0: michael@0: return format; michael@0: } michael@0: michael@0: /* Is the given gamma significantly different from sRGB? The test is the same michael@0: * one used in pngrtran.c when deciding whether to do gamma correction. The michael@0: * arithmetic optimizes the division by using the fact that the inverse of the michael@0: * file sRGB gamma is 2.2 michael@0: */ michael@0: static int michael@0: png_gamma_not_sRGB(png_fixed_point g) michael@0: { michael@0: if (g < PNG_FP_1) michael@0: { michael@0: /* An uninitialized gamma is assumed to be sRGB for the simplified API. */ michael@0: if (g == 0) michael@0: return 0; michael@0: michael@0: return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */); michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: /* Do the main body of a 'png_image_begin_read' function; read the PNG file michael@0: * header and fill in all the information. This is executed in a safe context, michael@0: * unlike the init routine above. michael@0: */ michael@0: static int michael@0: png_image_read_header(png_voidp argument) michael@0: { michael@0: png_imagep image = png_voidcast(png_imagep, argument); michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: png_inforp info_ptr = image->opaque->info_ptr; michael@0: michael@0: png_set_benign_errors(png_ptr, 1/*warn*/); michael@0: png_read_info(png_ptr, info_ptr); michael@0: michael@0: /* Do this the fast way; just read directly out of png_struct. */ michael@0: image->width = png_ptr->width; michael@0: image->height = png_ptr->height; michael@0: michael@0: { michael@0: png_uint_32 format = png_image_format(png_ptr); michael@0: michael@0: image->format = format; michael@0: michael@0: #ifdef PNG_COLORSPACE_SUPPORTED michael@0: /* Does the colorspace match sRGB? If there is no color endpoint michael@0: * (colorant) information assume yes, otherwise require the michael@0: * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the michael@0: * colorspace has been determined to be invalid ignore it. michael@0: */ michael@0: if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags michael@0: & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB| michael@0: PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS)) michael@0: image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; michael@0: #endif michael@0: } michael@0: michael@0: /* We need the maximum number of entries regardless of the format the michael@0: * application sets here. michael@0: */ michael@0: { michael@0: png_uint_32 cmap_entries; michael@0: michael@0: switch (png_ptr->color_type) michael@0: { michael@0: case PNG_COLOR_TYPE_GRAY: michael@0: cmap_entries = 1U << png_ptr->bit_depth; michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_PALETTE: michael@0: cmap_entries = png_ptr->num_palette; michael@0: break; michael@0: michael@0: default: michael@0: cmap_entries = 256; michael@0: break; michael@0: } michael@0: michael@0: if (cmap_entries > 256) michael@0: cmap_entries = 256; michael@0: michael@0: image->colormap_entries = cmap_entries; michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: #ifdef PNG_STDIO_SUPPORTED michael@0: int PNGAPI michael@0: png_image_begin_read_from_stdio(png_imagep image, FILE* file) michael@0: { michael@0: if (image != NULL && image->version == PNG_IMAGE_VERSION) michael@0: { michael@0: if (file != NULL) michael@0: { michael@0: if (png_image_read_init(image)) michael@0: { michael@0: /* This is slightly evil, but png_init_io doesn't do anything other michael@0: * than this and we haven't changed the standard IO functions so michael@0: * this saves a 'safe' function. michael@0: */ michael@0: image->opaque->png_ptr->io_ptr = file; michael@0: return png_safe_execute(image, png_image_read_header, image); michael@0: } michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, michael@0: "png_image_begin_read_from_stdio: invalid argument"); michael@0: } michael@0: michael@0: else if (image != NULL) michael@0: return png_image_error(image, michael@0: "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION"); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: int PNGAPI michael@0: png_image_begin_read_from_file(png_imagep image, const char *file_name) michael@0: { michael@0: if (image != NULL && image->version == PNG_IMAGE_VERSION) michael@0: { michael@0: if (file_name != NULL) michael@0: { michael@0: FILE *fp = fopen(file_name, "rb"); michael@0: michael@0: if (fp != NULL) michael@0: { michael@0: if (png_image_read_init(image)) michael@0: { michael@0: image->opaque->png_ptr->io_ptr = fp; michael@0: image->opaque->owned_file = 1; michael@0: return png_safe_execute(image, png_image_read_header, image); michael@0: } michael@0: michael@0: /* Clean up: just the opened file. */ michael@0: (void)fclose(fp); michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, strerror(errno)); michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, michael@0: "png_image_begin_read_from_file: invalid argument"); michael@0: } michael@0: michael@0: else if (image != NULL) michael@0: return png_image_error(image, michael@0: "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION"); michael@0: michael@0: return 0; michael@0: } michael@0: #endif /* PNG_STDIO_SUPPORTED */ michael@0: michael@0: static void PNGCBAPI michael@0: png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need) michael@0: { michael@0: if (png_ptr != NULL) michael@0: { michael@0: png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); michael@0: if (image != NULL) michael@0: { michael@0: png_controlp cp = image->opaque; michael@0: if (cp != NULL) michael@0: { michael@0: png_const_bytep memory = cp->memory; michael@0: png_size_t size = cp->size; michael@0: michael@0: if (memory != NULL && size >= need) michael@0: { michael@0: memcpy(out, memory, need); michael@0: cp->memory = memory + need; michael@0: cp->size = size - need; michael@0: return; michael@0: } michael@0: michael@0: png_error(png_ptr, "read beyond end of data"); michael@0: } michael@0: } michael@0: michael@0: png_error(png_ptr, "invalid memory read"); michael@0: } michael@0: } michael@0: michael@0: int PNGAPI png_image_begin_read_from_memory(png_imagep image, michael@0: png_const_voidp memory, png_size_t size) michael@0: { michael@0: if (image != NULL && image->version == PNG_IMAGE_VERSION) michael@0: { michael@0: if (memory != NULL && size > 0) michael@0: { michael@0: if (png_image_read_init(image)) michael@0: { michael@0: /* Now set the IO functions to read from the memory buffer and michael@0: * store it into io_ptr. Again do this in-place to avoid calling a michael@0: * libpng function that requires error handling. michael@0: */ michael@0: image->opaque->memory = png_voidcast(png_const_bytep, memory); michael@0: image->opaque->size = size; michael@0: image->opaque->png_ptr->io_ptr = image; michael@0: image->opaque->png_ptr->read_data_fn = png_image_memory_read; michael@0: michael@0: return png_safe_execute(image, png_image_read_header, image); michael@0: } michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, michael@0: "png_image_begin_read_from_memory: invalid argument"); michael@0: } michael@0: michael@0: else if (image != NULL) michael@0: return png_image_error(image, michael@0: "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION"); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /* Utility function to skip chunks that are not used by the simplified image michael@0: * read functions and an appropriate macro to call it. michael@0: */ michael@0: #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED michael@0: static void michael@0: png_image_skip_unused_chunks(png_structrp png_ptr) michael@0: { michael@0: /* Prepare the reader to ignore all recognized chunks whose data will not michael@0: * be used, i.e., all chunks recognized by libpng except for those michael@0: * involved in basic image reading: michael@0: * michael@0: * IHDR, PLTE, IDAT, IEND michael@0: * michael@0: * Or image data handling: michael@0: * michael@0: * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT. michael@0: * michael@0: * This provides a small performance improvement and eliminates any michael@0: * potential vulnerability to security problems in the unused chunks. michael@0: * michael@0: * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored michael@0: * too. This allows the simplified API to be compiled without iCCP support, michael@0: * however if the support is there the chunk is still checked to detect michael@0: * errors (which are unfortunately quite common.) michael@0: */ michael@0: { michael@0: static PNG_CONST png_byte chunks_to_process[] = { michael@0: 98, 75, 71, 68, '\0', /* bKGD */ michael@0: 99, 72, 82, 77, '\0', /* cHRM */ michael@0: 103, 65, 77, 65, '\0', /* gAMA */ michael@0: # ifdef PNG_READ_iCCP_SUPPORTED michael@0: 105, 67, 67, 80, '\0', /* iCCP */ michael@0: # endif michael@0: 115, 66, 73, 84, '\0', /* sBIT */ michael@0: 115, 82, 71, 66, '\0', /* sRGB */ michael@0: }; michael@0: michael@0: /* Ignore unknown chunks and all other chunks except for the michael@0: * IHDR, PLTE, tRNS, IDAT, and IEND chunks. michael@0: */ michael@0: png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, michael@0: NULL, -1); michael@0: michael@0: /* But do not ignore image data handling chunks */ michael@0: png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT, michael@0: chunks_to_process, (sizeof chunks_to_process)/5); michael@0: } michael@0: } michael@0: michael@0: # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) michael@0: #else michael@0: # define PNG_SKIP_CHUNKS(p) ((void)0) michael@0: #endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */ michael@0: michael@0: /* The following macro gives the exact rounded answer for all values in the michael@0: * range 0..255 (it actually divides by 51.2, but the rounding still generates michael@0: * the correct numbers 0..5 michael@0: */ michael@0: #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) michael@0: michael@0: /* Utility functions to make particular color-maps */ michael@0: static void michael@0: set_file_encoding(png_image_read_control *display) michael@0: { michael@0: png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma; michael@0: if (png_gamma_significant(g)) michael@0: { michael@0: if (png_gamma_not_sRGB(g)) michael@0: { michael@0: display->file_encoding = P_FILE; michael@0: display->gamma_to_linear = png_reciprocal(g); michael@0: } michael@0: michael@0: else michael@0: display->file_encoding = P_sRGB; michael@0: } michael@0: michael@0: else michael@0: display->file_encoding = P_LINEAR8; michael@0: } michael@0: michael@0: static unsigned int michael@0: decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) michael@0: { michael@0: if (encoding == P_FILE) /* double check */ michael@0: encoding = display->file_encoding; michael@0: michael@0: if (encoding == P_NOTSET) /* must be the file encoding */ michael@0: { michael@0: set_file_encoding(display); michael@0: encoding = display->file_encoding; michael@0: } michael@0: michael@0: switch (encoding) michael@0: { michael@0: case P_FILE: michael@0: value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); michael@0: break; michael@0: michael@0: case P_sRGB: michael@0: value = png_sRGB_table[value]; michael@0: break; michael@0: michael@0: case P_LINEAR: michael@0: break; michael@0: michael@0: case P_LINEAR8: michael@0: value *= 257; michael@0: break; michael@0: michael@0: default: michael@0: png_error(display->image->opaque->png_ptr, michael@0: "unexpected encoding (internal error)"); michael@0: break; michael@0: } michael@0: michael@0: return value; michael@0: } michael@0: michael@0: static png_uint_32 michael@0: png_colormap_compose(png_image_read_control *display, michael@0: png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, michael@0: png_uint_32 background, int encoding) michael@0: { michael@0: /* The file value is composed on the background, the background has the given michael@0: * encoding and so does the result, the file is encoded with P_FILE and the michael@0: * file and alpha are 8-bit values. The (output) encoding will always be michael@0: * P_LINEAR or P_sRGB. michael@0: */ michael@0: png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); michael@0: png_uint_32 b = decode_gamma(display, background, encoding); michael@0: michael@0: /* The alpha is always an 8-bit value (it comes from the palette), the value michael@0: * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. michael@0: */ michael@0: f = f * alpha + b * (255-alpha); michael@0: michael@0: if (encoding == P_LINEAR) michael@0: { michael@0: /* Scale to 65535; divide by 255, approximately (in fact this is extremely michael@0: * accurate, it divides by 255.00000005937181414556, with no overflow.) michael@0: */ michael@0: f *= 257; /* Now scaled by 65535 */ michael@0: f += f >> 16; michael@0: f = (f+32768) >> 16; michael@0: } michael@0: michael@0: else /* P_sRGB */ michael@0: f = PNG_sRGB_FROM_LINEAR(f); michael@0: michael@0: return f; michael@0: } michael@0: michael@0: /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must michael@0: * be 8-bit. michael@0: */ michael@0: static void michael@0: png_create_colormap_entry(png_image_read_control *display, michael@0: png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, michael@0: png_uint_32 alpha, int encoding) michael@0: { michael@0: png_imagep image = display->image; michael@0: const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ? michael@0: P_LINEAR : P_sRGB; michael@0: const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && michael@0: (red != green || green != blue); michael@0: michael@0: if (ip > 255) michael@0: png_error(image->opaque->png_ptr, "color-map index out of range"); michael@0: michael@0: /* Update the cache with whether the file gamma is significantly different michael@0: * from sRGB. michael@0: */ michael@0: if (encoding == P_FILE) michael@0: { michael@0: if (display->file_encoding == P_NOTSET) michael@0: set_file_encoding(display); michael@0: michael@0: /* Note that the cached value may be P_FILE too, but if it is then the michael@0: * gamma_to_linear member has been set. michael@0: */ michael@0: encoding = display->file_encoding; michael@0: } michael@0: michael@0: if (encoding == P_FILE) michael@0: { michael@0: png_fixed_point g = display->gamma_to_linear; michael@0: michael@0: red = png_gamma_16bit_correct(red*257, g); michael@0: green = png_gamma_16bit_correct(green*257, g); michael@0: blue = png_gamma_16bit_correct(blue*257, g); michael@0: michael@0: if (convert_to_Y || output_encoding == P_LINEAR) michael@0: { michael@0: alpha *= 257; michael@0: encoding = P_LINEAR; michael@0: } michael@0: michael@0: else michael@0: { michael@0: red = PNG_sRGB_FROM_LINEAR(red * 255); michael@0: green = PNG_sRGB_FROM_LINEAR(green * 255); michael@0: blue = PNG_sRGB_FROM_LINEAR(blue * 255); michael@0: encoding = P_sRGB; michael@0: } michael@0: } michael@0: michael@0: else if (encoding == P_LINEAR8) michael@0: { michael@0: /* This encoding occurs quite frequently in test cases because PngSuite michael@0: * includes a gAMA 1.0 chunk with most images. michael@0: */ michael@0: red *= 257; michael@0: green *= 257; michael@0: blue *= 257; michael@0: alpha *= 257; michael@0: encoding = P_LINEAR; michael@0: } michael@0: michael@0: else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR)) michael@0: { michael@0: /* The values are 8-bit sRGB values, but must be converted to 16-bit michael@0: * linear. michael@0: */ michael@0: red = png_sRGB_table[red]; michael@0: green = png_sRGB_table[green]; michael@0: blue = png_sRGB_table[blue]; michael@0: alpha *= 257; michael@0: encoding = P_LINEAR; michael@0: } michael@0: michael@0: /* This is set if the color isn't gray but the output is. */ michael@0: if (encoding == P_LINEAR) michael@0: { michael@0: if (convert_to_Y) michael@0: { michael@0: /* NOTE: these values are copied from png_do_rgb_to_gray */ michael@0: png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + michael@0: (png_uint_32)2366 * blue; michael@0: michael@0: if (output_encoding == P_LINEAR) michael@0: y = (y + 16384) >> 15; michael@0: michael@0: else michael@0: { michael@0: /* y is scaled by 32768, we need it scaled by 255: */ michael@0: y = (y + 128) >> 8; michael@0: y *= 255; michael@0: y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); michael@0: encoding = P_sRGB; michael@0: } michael@0: michael@0: blue = red = green = y; michael@0: } michael@0: michael@0: else if (output_encoding == P_sRGB) michael@0: { michael@0: red = PNG_sRGB_FROM_LINEAR(red * 255); michael@0: green = PNG_sRGB_FROM_LINEAR(green * 255); michael@0: blue = PNG_sRGB_FROM_LINEAR(blue * 255); michael@0: alpha = PNG_DIV257(alpha); michael@0: encoding = P_sRGB; michael@0: } michael@0: } michael@0: michael@0: if (encoding != output_encoding) michael@0: png_error(image->opaque->png_ptr, "bad encoding (internal error)"); michael@0: michael@0: /* Store the value. */ michael@0: { michael@0: # ifdef PNG_FORMAT_AFIRST_SUPPORTED michael@0: const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && michael@0: (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; michael@0: # else michael@0: # define afirst 0 michael@0: # endif michael@0: # ifdef PNG_FORMAT_BGR_SUPPORTED michael@0: const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0; michael@0: # else michael@0: # define bgr 0 michael@0: # endif michael@0: michael@0: if (output_encoding == P_LINEAR) michael@0: { michael@0: png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); michael@0: michael@0: entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); michael@0: michael@0: /* The linear 16-bit values must be pre-multiplied by the alpha channel michael@0: * value, if less than 65535 (this is, effectively, composite on black michael@0: * if the alpha channel is removed.) michael@0: */ michael@0: switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) michael@0: { michael@0: case 4: michael@0: entry[afirst ? 0 : 3] = (png_uint_16)alpha; michael@0: /* FALL THROUGH */ michael@0: michael@0: case 3: michael@0: if (alpha < 65535) michael@0: { michael@0: if (alpha > 0) michael@0: { michael@0: blue = (blue * alpha + 32767U)/65535U; michael@0: green = (green * alpha + 32767U)/65535U; michael@0: red = (red * alpha + 32767U)/65535U; michael@0: } michael@0: michael@0: else michael@0: red = green = blue = 0; michael@0: } michael@0: entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; michael@0: entry[afirst + 1] = (png_uint_16)green; michael@0: entry[afirst + bgr] = (png_uint_16)red; michael@0: break; michael@0: michael@0: case 2: michael@0: entry[1 ^ afirst] = (png_uint_16)alpha; michael@0: /* FALL THROUGH */ michael@0: michael@0: case 1: michael@0: if (alpha < 65535) michael@0: { michael@0: if (alpha > 0) michael@0: green = (green * alpha + 32767U)/65535U; michael@0: michael@0: else michael@0: green = 0; michael@0: } michael@0: entry[afirst] = (png_uint_16)green; michael@0: break; michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: else /* output encoding is P_sRGB */ michael@0: { michael@0: png_bytep entry = png_voidcast(png_bytep, display->colormap); michael@0: michael@0: entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); michael@0: michael@0: switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) michael@0: { michael@0: case 4: michael@0: entry[afirst ? 0 : 3] = (png_byte)alpha; michael@0: case 3: michael@0: entry[afirst + (2 ^ bgr)] = (png_byte)blue; michael@0: entry[afirst + 1] = (png_byte)green; michael@0: entry[afirst + bgr] = (png_byte)red; michael@0: break; michael@0: michael@0: case 2: michael@0: entry[1 ^ afirst] = (png_byte)alpha; michael@0: case 1: michael@0: entry[afirst] = (png_byte)green; michael@0: break; michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: # ifdef afirst michael@0: # undef afirst michael@0: # endif michael@0: # ifdef bgr michael@0: # undef bgr michael@0: # endif michael@0: } michael@0: } michael@0: michael@0: static int michael@0: make_gray_file_colormap(png_image_read_control *display) michael@0: { michael@0: unsigned int i; michael@0: michael@0: for (i=0; i<256; ++i) michael@0: png_create_colormap_entry(display, i, i, i, i, 255, P_FILE); michael@0: michael@0: return i; michael@0: } michael@0: michael@0: static int michael@0: make_gray_colormap(png_image_read_control *display) michael@0: { michael@0: unsigned int i; michael@0: michael@0: for (i=0; i<256; ++i) michael@0: png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB); michael@0: michael@0: return i; michael@0: } michael@0: #define PNG_GRAY_COLORMAP_ENTRIES 256 michael@0: michael@0: static int michael@0: make_ga_colormap(png_image_read_control *display) michael@0: { michael@0: unsigned int i, a; michael@0: michael@0: /* Alpha is retained, the output will be a color-map with entries michael@0: * selected by six levels of alpha. One transparent entry, 6 gray michael@0: * levels for all the intermediate alpha values, leaving 230 entries michael@0: * for the opaque grays. The color-map entries are the six values michael@0: * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the michael@0: * relevant entry. michael@0: * michael@0: * if (alpha > 229) // opaque michael@0: * { michael@0: * // The 231 entries are selected to make the math below work: michael@0: * base = 0; michael@0: * entry = (231 * gray + 128) >> 8; michael@0: * } michael@0: * else if (alpha < 26) // transparent michael@0: * { michael@0: * base = 231; michael@0: * entry = 0; michael@0: * } michael@0: * else // partially opaque michael@0: * { michael@0: * base = 226 + 6 * PNG_DIV51(alpha); michael@0: * entry = PNG_DIV51(gray); michael@0: * } michael@0: */ michael@0: i = 0; michael@0: while (i < 231) michael@0: { michael@0: unsigned int gray = (i * 256 + 115) / 231; michael@0: png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB); michael@0: } michael@0: michael@0: /* 255 is used here for the component values for consistency with the code michael@0: * that undoes premultiplication in pngwrite.c. michael@0: */ michael@0: png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB); michael@0: michael@0: for (a=1; a<5; ++a) michael@0: { michael@0: unsigned int g; michael@0: michael@0: for (g=0; g<6; ++g) michael@0: png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, michael@0: P_sRGB); michael@0: } michael@0: michael@0: return i; michael@0: } michael@0: michael@0: #define PNG_GA_COLORMAP_ENTRIES 256 michael@0: michael@0: static int michael@0: make_rgb_colormap(png_image_read_control *display) michael@0: { michael@0: unsigned int i, r; michael@0: michael@0: /* Build a 6x6x6 opaque RGB cube */ michael@0: for (i=r=0; r<6; ++r) michael@0: { michael@0: unsigned int g; michael@0: michael@0: for (g=0; g<6; ++g) michael@0: { michael@0: unsigned int b; michael@0: michael@0: for (b=0; b<6; ++b) michael@0: png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, michael@0: P_sRGB); michael@0: } michael@0: } michael@0: michael@0: return i; michael@0: } michael@0: michael@0: #define PNG_RGB_COLORMAP_ENTRIES 216 michael@0: michael@0: /* Return a palette index to the above palette given three 8-bit sRGB values. */ michael@0: #define PNG_RGB_INDEX(r,g,b) \ michael@0: ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) michael@0: michael@0: static int michael@0: png_image_read_colormap(png_voidp argument) michael@0: { michael@0: png_image_read_control *display = michael@0: png_voidcast(png_image_read_control*, argument); michael@0: const png_imagep image = display->image; michael@0: michael@0: const png_structrp png_ptr = image->opaque->png_ptr; michael@0: const png_uint_32 output_format = image->format; michael@0: const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ? michael@0: P_LINEAR : P_sRGB; michael@0: michael@0: unsigned int cmap_entries; michael@0: unsigned int output_processing; /* Output processing option */ michael@0: unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */ michael@0: michael@0: /* Background information; the background color and the index of this color michael@0: * in the color-map if it exists (else 256). michael@0: */ michael@0: unsigned int background_index = 256; michael@0: png_uint_32 back_r, back_g, back_b; michael@0: michael@0: /* Flags to accumulate things that need to be done to the input. */ michael@0: int expand_tRNS = 0; michael@0: michael@0: /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is michael@0: * very difficult to do, the results look awful, and it is difficult to see michael@0: * what possible use it is because the application can't control the michael@0: * color-map. michael@0: */ michael@0: if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || michael@0: png_ptr->num_trans > 0) /* alpha in input */ && michael@0: ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) michael@0: { michael@0: if (output_encoding == P_LINEAR) /* compose on black */ michael@0: back_b = back_g = back_r = 0; michael@0: michael@0: else if (display->background == NULL /* no way to remove it */) michael@0: png_error(png_ptr, michael@0: "a background color must be supplied to remove alpha/transparency"); michael@0: michael@0: /* Get a copy of the background color (this avoids repeating the checks michael@0: * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the michael@0: * output format. michael@0: */ michael@0: else michael@0: { michael@0: back_g = display->background->green; michael@0: if (output_format & PNG_FORMAT_FLAG_COLOR) michael@0: { michael@0: back_r = display->background->red; michael@0: back_b = display->background->blue; michael@0: } michael@0: else michael@0: back_b = back_r = back_g; michael@0: } michael@0: } michael@0: michael@0: else if (output_encoding == P_LINEAR) michael@0: back_b = back_r = back_g = 65535; michael@0: michael@0: else michael@0: back_b = back_r = back_g = 255; michael@0: michael@0: /* Default the input file gamma if required - this is necessary because michael@0: * libpng assumes that if no gamma information is present the data is in the michael@0: * output format, but the simplified API deduces the gamma from the input michael@0: * format. michael@0: */ michael@0: if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0) michael@0: { michael@0: /* Do this directly, not using the png_colorspace functions, to ensure michael@0: * that it happens even if the colorspace is invalid (though probably if michael@0: * it is the setting will be ignored) Note that the same thing can be michael@0: * achieved at the application interface with png_set_gAMA. michael@0: */ michael@0: if (png_ptr->bit_depth == 16 && michael@0: (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) michael@0: png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR; michael@0: michael@0: else michael@0: png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE; michael@0: michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; michael@0: } michael@0: michael@0: /* Decide what to do based on the PNG color type of the input data. The michael@0: * utility function png_create_colormap_entry deals with most aspects of the michael@0: * output transformations; this code works out how to produce bytes of michael@0: * color-map entries from the original format. michael@0: */ michael@0: switch (png_ptr->color_type) michael@0: { michael@0: case PNG_COLOR_TYPE_GRAY: michael@0: if (png_ptr->bit_depth <= 8) michael@0: { michael@0: /* There at most 256 colors in the output, regardless of michael@0: * transparency. michael@0: */ michael@0: unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0; michael@0: michael@0: cmap_entries = 1U << png_ptr->bit_depth; michael@0: if (cmap_entries > image->colormap_entries) michael@0: png_error(png_ptr, "gray[8] color-map: too few entries"); michael@0: michael@0: step = 255 / (cmap_entries - 1); michael@0: output_processing = PNG_CMAP_NONE; michael@0: michael@0: /* If there is a tRNS chunk then this either selects a transparent michael@0: * value or, if the output has no alpha, the background color. michael@0: */ michael@0: if (png_ptr->num_trans > 0) michael@0: { michael@0: trans = png_ptr->trans_color.gray; michael@0: michael@0: if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) michael@0: back_alpha = output_encoding == P_LINEAR ? 65535 : 255; michael@0: } michael@0: michael@0: /* png_create_colormap_entry just takes an RGBA and writes the michael@0: * corresponding color-map entry using the format from 'image', michael@0: * including the required conversion to sRGB or linear as michael@0: * appropriate. The input values are always either sRGB (if the michael@0: * gamma correction flag is 0) or 0..255 scaled file encoded values michael@0: * (if the function must gamma correct them). michael@0: */ michael@0: for (i=val=0; ibit_depth < 8) michael@0: png_set_packing(png_ptr); michael@0: } michael@0: michael@0: else /* bit depth is 16 */ michael@0: { michael@0: /* The 16-bit input values can be converted directly to 8-bit gamma michael@0: * encoded values; however, if a tRNS chunk is present 257 color-map michael@0: * entries are required. This means that the extra entry requires michael@0: * special processing; add an alpha channel, sacrifice gray level michael@0: * 254 and convert transparent (alpha==0) entries to that. michael@0: * michael@0: * Use libpng to chop the data to 8 bits. Convert it to sRGB at the michael@0: * same time to minimize quality loss. If a tRNS chunk is present michael@0: * this means libpng must handle it too; otherwise it is impossible michael@0: * to do the exact match on the 16-bit value. michael@0: * michael@0: * If the output has no alpha channel *and* the background color is michael@0: * gray then it is possible to let libpng handle the substitution by michael@0: * ensuring that the corresponding gray level matches the background michael@0: * color exactly. michael@0: */ michael@0: data_encoding = P_sRGB; michael@0: michael@0: if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) michael@0: png_error(png_ptr, "gray[16] color-map: too few entries"); michael@0: michael@0: cmap_entries = make_gray_colormap(display); michael@0: michael@0: if (png_ptr->num_trans > 0) michael@0: { michael@0: unsigned int back_alpha; michael@0: michael@0: if (output_format & PNG_FORMAT_FLAG_ALPHA) michael@0: back_alpha = 0; michael@0: michael@0: else michael@0: { michael@0: if (back_r == back_g && back_g == back_b) michael@0: { michael@0: /* Background is gray; no special processing will be michael@0: * required. michael@0: */ michael@0: png_color_16 c; michael@0: png_uint_32 gray = back_g; michael@0: michael@0: if (output_encoding == P_LINEAR) michael@0: { michael@0: gray = PNG_sRGB_FROM_LINEAR(gray * 255); michael@0: michael@0: /* And make sure the corresponding palette entry michael@0: * matches. michael@0: */ michael@0: png_create_colormap_entry(display, gray, back_g, back_g, michael@0: back_g, 65535, P_LINEAR); michael@0: } michael@0: michael@0: /* The background passed to libpng, however, must be the michael@0: * sRGB value. michael@0: */ michael@0: c.index = 0; /*unused*/ michael@0: c.gray = c.red = c.green = c.blue = (png_uint_16)gray; michael@0: michael@0: /* NOTE: does this work without expanding tRNS to alpha? michael@0: * It should be the color->gray case below apparently michael@0: * doesn't. michael@0: */ michael@0: png_set_background_fixed(png_ptr, &c, michael@0: PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, michael@0: 0/*gamma: not used*/); michael@0: michael@0: output_processing = PNG_CMAP_NONE; michael@0: break; michael@0: } michael@0: michael@0: back_alpha = output_encoding == P_LINEAR ? 65535 : 255; michael@0: } michael@0: michael@0: /* output_processing means that the libpng-processed row will be michael@0: * 8-bit GA and it has to be processing to single byte color-map michael@0: * values. Entry 254 is replaced by either a completely michael@0: * transparent entry or by the background color at full michael@0: * precision (and the background color is not a simple gray leve michael@0: * in this case.) michael@0: */ michael@0: expand_tRNS = 1; michael@0: output_processing = PNG_CMAP_TRANS; michael@0: background_index = 254; michael@0: michael@0: /* And set (overwrite) color-map entry 254 to the actual michael@0: * background color at full precision. michael@0: */ michael@0: png_create_colormap_entry(display, 254, back_r, back_g, back_b, michael@0: back_alpha, output_encoding); michael@0: } michael@0: michael@0: else michael@0: output_processing = PNG_CMAP_NONE; michael@0: } michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_GRAY_ALPHA: michael@0: /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum michael@0: * of 65536 combinations. If, however, the alpha channel is to be michael@0: * removed there are only 256 possibilities if the background is gray. michael@0: * (Otherwise there is a subset of the 65536 possibilities defined by michael@0: * the triangle between black, white and the background color.) michael@0: * michael@0: * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to michael@0: * worry about tRNS matching - tRNS is ignored if there is an alpha michael@0: * channel. michael@0: */ michael@0: data_encoding = P_sRGB; michael@0: michael@0: if (output_format & PNG_FORMAT_FLAG_ALPHA) michael@0: { michael@0: if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) michael@0: png_error(png_ptr, "gray+alpha color-map: too few entries"); michael@0: michael@0: cmap_entries = make_ga_colormap(display); michael@0: michael@0: background_index = PNG_CMAP_GA_BACKGROUND; michael@0: output_processing = PNG_CMAP_GA; michael@0: } michael@0: michael@0: else /* alpha is removed */ michael@0: { michael@0: /* Alpha must be removed as the PNG data is processed when the michael@0: * background is a color because the G and A channels are michael@0: * independent and the vector addition (non-parallel vectors) is a michael@0: * 2-D problem. michael@0: * michael@0: * This can be reduced to the same algorithm as above by making a michael@0: * colormap containing gray levels (for the opaque grays), a michael@0: * background entry (for a transparent pixel) and a set of four six michael@0: * level color values, one set for each intermediate alpha value. michael@0: * See the comments in make_ga_colormap for how this works in the michael@0: * per-pixel processing. michael@0: * michael@0: * If the background is gray, however, we only need a 256 entry gray michael@0: * level color map. It is sufficient to make the entry generated michael@0: * for the background color be exactly the color specified. michael@0: */ michael@0: if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || michael@0: (back_r == back_g && back_g == back_b)) michael@0: { michael@0: /* Background is gray; no special processing will be required. */ michael@0: png_color_16 c; michael@0: png_uint_32 gray = back_g; michael@0: michael@0: if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) michael@0: png_error(png_ptr, "gray-alpha color-map: too few entries"); michael@0: michael@0: cmap_entries = make_gray_colormap(display); michael@0: michael@0: if (output_encoding == P_LINEAR) michael@0: { michael@0: gray = PNG_sRGB_FROM_LINEAR(gray * 255); michael@0: michael@0: /* And make sure the corresponding palette entry matches. */ michael@0: png_create_colormap_entry(display, gray, back_g, back_g, michael@0: back_g, 65535, P_LINEAR); michael@0: } michael@0: michael@0: /* The background passed to libpng, however, must be the sRGB michael@0: * value. michael@0: */ michael@0: c.index = 0; /*unused*/ michael@0: c.gray = c.red = c.green = c.blue = (png_uint_16)gray; michael@0: michael@0: png_set_background_fixed(png_ptr, &c, michael@0: PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, michael@0: 0/*gamma: not used*/); michael@0: michael@0: output_processing = PNG_CMAP_NONE; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_uint_32 i, a; michael@0: michael@0: /* This is the same as png_make_ga_colormap, above, except that michael@0: * the entries are all opaque. michael@0: */ michael@0: if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) michael@0: png_error(png_ptr, "ga-alpha color-map: too few entries"); michael@0: michael@0: i = 0; michael@0: while (i < 231) michael@0: { michael@0: png_uint_32 gray = (i * 256 + 115) / 231; michael@0: png_create_colormap_entry(display, i++, gray, gray, gray, michael@0: 255, P_sRGB); michael@0: } michael@0: michael@0: /* NOTE: this preserves the full precision of the application michael@0: * background color. michael@0: */ michael@0: background_index = i; michael@0: png_create_colormap_entry(display, i++, back_r, back_g, back_b, michael@0: output_encoding == P_LINEAR ? 65535U : 255U, output_encoding); michael@0: michael@0: /* For non-opaque input composite on the sRGB background - this michael@0: * requires inverting the encoding for each component. The input michael@0: * is still converted to the sRGB encoding because this is a michael@0: * reasonable approximate to the logarithmic curve of human michael@0: * visual sensitivity, at least over the narrow range which PNG michael@0: * represents. Consequently 'G' is always sRGB encoded, while michael@0: * 'A' is linear. We need the linear background colors. michael@0: */ michael@0: if (output_encoding == P_sRGB) /* else already linear */ michael@0: { michael@0: /* This may produce a value not exactly matching the michael@0: * background, but that's ok because these numbers are only michael@0: * used when alpha != 0 michael@0: */ michael@0: back_r = png_sRGB_table[back_r]; michael@0: back_g = png_sRGB_table[back_g]; michael@0: back_b = png_sRGB_table[back_b]; michael@0: } michael@0: michael@0: for (a=1; a<5; ++a) michael@0: { michael@0: unsigned int g; michael@0: michael@0: /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled michael@0: * by an 8-bit alpha value (0..255). michael@0: */ michael@0: png_uint_32 alpha = 51 * a; michael@0: png_uint_32 back_rx = (255-alpha) * back_r; michael@0: png_uint_32 back_gx = (255-alpha) * back_g; michael@0: png_uint_32 back_bx = (255-alpha) * back_b; michael@0: michael@0: for (g=0; g<6; ++g) michael@0: { michael@0: png_uint_32 gray = png_sRGB_table[g*51] * alpha; michael@0: michael@0: png_create_colormap_entry(display, i++, michael@0: PNG_sRGB_FROM_LINEAR(gray + back_rx), michael@0: PNG_sRGB_FROM_LINEAR(gray + back_gx), michael@0: PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB); michael@0: } michael@0: } michael@0: michael@0: cmap_entries = i; michael@0: output_processing = PNG_CMAP_GA; michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_RGB: michael@0: case PNG_COLOR_TYPE_RGB_ALPHA: michael@0: /* Exclude the case where the output is gray; we can always handle this michael@0: * with the cases above. michael@0: */ michael@0: if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) michael@0: { michael@0: /* The color-map will be grayscale, so we may as well convert the michael@0: * input RGB values to a simple grayscale and use the grayscale michael@0: * code above. michael@0: * michael@0: * NOTE: calling this apparently damages the recognition of the michael@0: * transparent color in background color handling; call michael@0: * png_set_tRNS_to_alpha before png_set_background_fixed. michael@0: */ michael@0: png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, michael@0: -1); michael@0: data_encoding = P_sRGB; michael@0: michael@0: /* The output will now be one or two 8-bit gray or gray+alpha michael@0: * channels. The more complex case arises when the input has alpha. michael@0: */ michael@0: if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || michael@0: png_ptr->num_trans > 0) && michael@0: (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) michael@0: { michael@0: /* Both input and output have an alpha channel, so no background michael@0: * processing is required; just map the GA bytes to the right michael@0: * color-map entry. michael@0: */ michael@0: expand_tRNS = 1; michael@0: michael@0: if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) michael@0: png_error(png_ptr, "rgb[ga] color-map: too few entries"); michael@0: michael@0: cmap_entries = make_ga_colormap(display); michael@0: background_index = PNG_CMAP_GA_BACKGROUND; michael@0: output_processing = PNG_CMAP_GA; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* Either the input or the output has no alpha channel, so there michael@0: * will be no non-opaque pixels in the color-map; it will just be michael@0: * grayscale. michael@0: */ michael@0: if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) michael@0: png_error(png_ptr, "rgb[gray] color-map: too few entries"); michael@0: michael@0: /* Ideally this code would use libpng to do the gamma correction, michael@0: * but if an input alpha channel is to be removed we will hit the michael@0: * libpng bug in gamma+compose+rgb-to-gray (the double gamma michael@0: * correction bug). Fix this by dropping the gamma correction in michael@0: * this case and doing it in the palette; this will result in michael@0: * duplicate palette entries, but that's better than the michael@0: * alternative of double gamma correction. michael@0: */ michael@0: if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || michael@0: png_ptr->num_trans > 0) && michael@0: png_gamma_not_sRGB(png_ptr->colorspace.gamma)) michael@0: { michael@0: cmap_entries = make_gray_file_colormap(display); michael@0: data_encoding = P_FILE; michael@0: } michael@0: michael@0: else michael@0: cmap_entries = make_gray_colormap(display); michael@0: michael@0: /* But if the input has alpha or transparency it must be removed michael@0: */ michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || michael@0: png_ptr->num_trans > 0) michael@0: { michael@0: png_color_16 c; michael@0: png_uint_32 gray = back_g; michael@0: michael@0: /* We need to ensure that the application background exists in michael@0: * the colormap and that completely transparent pixels map to michael@0: * it. Achieve this simply by ensuring that the entry michael@0: * selected for the background really is the background color. michael@0: */ michael@0: if (data_encoding == P_FILE) /* from the fixup above */ michael@0: { michael@0: /* The app supplied a gray which is in output_encoding, we michael@0: * need to convert it to a value of the input (P_FILE) michael@0: * encoding then set this palette entry to the required michael@0: * output encoding. michael@0: */ michael@0: if (output_encoding == P_sRGB) michael@0: gray = png_sRGB_table[gray]; /* now P_LINEAR */ michael@0: michael@0: gray = PNG_DIV257(png_gamma_16bit_correct(gray, michael@0: png_ptr->colorspace.gamma)); /* now P_FILE */ michael@0: michael@0: /* And make sure the corresponding palette entry contains michael@0: * exactly the required sRGB value. michael@0: */ michael@0: png_create_colormap_entry(display, gray, back_g, back_g, michael@0: back_g, 0/*unused*/, output_encoding); michael@0: } michael@0: michael@0: else if (output_encoding == P_LINEAR) michael@0: { michael@0: gray = PNG_sRGB_FROM_LINEAR(gray * 255); michael@0: michael@0: /* And make sure the corresponding palette entry matches. michael@0: */ michael@0: png_create_colormap_entry(display, gray, back_g, back_g, michael@0: back_g, 0/*unused*/, P_LINEAR); michael@0: } michael@0: michael@0: /* The background passed to libpng, however, must be the michael@0: * output (normally sRGB) value. michael@0: */ michael@0: c.index = 0; /*unused*/ michael@0: c.gray = c.red = c.green = c.blue = (png_uint_16)gray; michael@0: michael@0: /* NOTE: the following is apparently a bug in libpng. Without michael@0: * it the transparent color recognition in michael@0: * png_set_background_fixed seems to go wrong. michael@0: */ michael@0: expand_tRNS = 1; michael@0: png_set_background_fixed(png_ptr, &c, michael@0: PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, michael@0: 0/*gamma: not used*/); michael@0: } michael@0: michael@0: output_processing = PNG_CMAP_NONE; michael@0: } michael@0: } michael@0: michael@0: else /* output is color */ michael@0: { michael@0: /* We could use png_quantize here so long as there is no transparent michael@0: * color or alpha; png_quantize ignores alpha. Easier overall just michael@0: * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. michael@0: * Consequently we always want libpng to produce sRGB data. michael@0: */ michael@0: data_encoding = P_sRGB; michael@0: michael@0: /* Is there any transparency or alpha? */ michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || michael@0: png_ptr->num_trans > 0) michael@0: { michael@0: /* Is there alpha in the output too? If so all four channels are michael@0: * processed into a special RGB cube with alpha support. michael@0: */ michael@0: if (output_format & PNG_FORMAT_FLAG_ALPHA) michael@0: { michael@0: png_uint_32 r; michael@0: michael@0: if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) michael@0: png_error(png_ptr, "rgb+alpha color-map: too few entries"); michael@0: michael@0: cmap_entries = make_rgb_colormap(display); michael@0: michael@0: /* Add a transparent entry. */ michael@0: png_create_colormap_entry(display, cmap_entries, 255, 255, michael@0: 255, 0, P_sRGB); michael@0: michael@0: /* This is stored as the background index for the processing michael@0: * algorithm. michael@0: */ michael@0: background_index = cmap_entries++; michael@0: michael@0: /* Add 27 r,g,b entries each with alpha 0.5. */ michael@0: for (r=0; r<256; r = (r << 1) | 0x7f) michael@0: { michael@0: png_uint_32 g; michael@0: michael@0: for (g=0; g<256; g = (g << 1) | 0x7f) michael@0: { michael@0: png_uint_32 b; michael@0: michael@0: /* This generates components with the values 0, 127 and michael@0: * 255 michael@0: */ michael@0: for (b=0; b<256; b = (b << 1) | 0x7f) michael@0: png_create_colormap_entry(display, cmap_entries++, michael@0: r, g, b, 128, P_sRGB); michael@0: } michael@0: } michael@0: michael@0: expand_tRNS = 1; michael@0: output_processing = PNG_CMAP_RGB_ALPHA; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* Alpha/transparency must be removed. The background must michael@0: * exist in the color map (achieved by setting adding it after michael@0: * the 666 color-map). If the standard processing code will michael@0: * pick up this entry automatically that's all that is michael@0: * required; libpng can be called to do the background michael@0: * processing. michael@0: */ michael@0: unsigned int sample_size = michael@0: PNG_IMAGE_SAMPLE_SIZE(output_format); michael@0: png_uint_32 r, g, b; /* sRGB background */ michael@0: michael@0: if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) michael@0: png_error(png_ptr, "rgb-alpha color-map: too few entries"); michael@0: michael@0: cmap_entries = make_rgb_colormap(display); michael@0: michael@0: png_create_colormap_entry(display, cmap_entries, back_r, michael@0: back_g, back_b, 0/*unused*/, output_encoding); michael@0: michael@0: if (output_encoding == P_LINEAR) michael@0: { michael@0: r = PNG_sRGB_FROM_LINEAR(back_r * 255); michael@0: g = PNG_sRGB_FROM_LINEAR(back_g * 255); michael@0: b = PNG_sRGB_FROM_LINEAR(back_b * 255); michael@0: } michael@0: michael@0: else michael@0: { michael@0: r = back_r; michael@0: g = back_g; michael@0: b = back_g; michael@0: } michael@0: michael@0: /* Compare the newly-created color-map entry with the one the michael@0: * PNG_CMAP_RGB algorithm will use. If the two entries don't michael@0: * match, add the new one and set this as the background michael@0: * index. michael@0: */ michael@0: if (memcmp((png_const_bytep)display->colormap + michael@0: sample_size * cmap_entries, michael@0: (png_const_bytep)display->colormap + michael@0: sample_size * PNG_RGB_INDEX(r,g,b), michael@0: sample_size) != 0) michael@0: { michael@0: /* The background color must be added. */ michael@0: background_index = cmap_entries++; michael@0: michael@0: /* Add 27 r,g,b entries each with created by composing with michael@0: * the background at alpha 0.5. michael@0: */ michael@0: for (r=0; r<256; r = (r << 1) | 0x7f) michael@0: { michael@0: for (g=0; g<256; g = (g << 1) | 0x7f) michael@0: { michael@0: /* This generates components with the values 0, 127 michael@0: * and 255 michael@0: */ michael@0: for (b=0; b<256; b = (b << 1) | 0x7f) michael@0: png_create_colormap_entry(display, cmap_entries++, michael@0: png_colormap_compose(display, r, P_sRGB, 128, michael@0: back_r, output_encoding), michael@0: png_colormap_compose(display, g, P_sRGB, 128, michael@0: back_g, output_encoding), michael@0: png_colormap_compose(display, b, P_sRGB, 128, michael@0: back_b, output_encoding), michael@0: 0/*unused*/, output_encoding); michael@0: } michael@0: } michael@0: michael@0: expand_tRNS = 1; michael@0: output_processing = PNG_CMAP_RGB_ALPHA; michael@0: } michael@0: michael@0: else /* background color is in the standard color-map */ michael@0: { michael@0: png_color_16 c; michael@0: michael@0: c.index = 0; /*unused*/ michael@0: c.red = (png_uint_16)back_r; michael@0: c.gray = c.green = (png_uint_16)back_g; michael@0: c.blue = (png_uint_16)back_b; michael@0: michael@0: png_set_background_fixed(png_ptr, &c, michael@0: PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, michael@0: 0/*gamma: not used*/); michael@0: michael@0: output_processing = PNG_CMAP_RGB; michael@0: } michael@0: } michael@0: } michael@0: michael@0: else /* no alpha or transparency in the input */ michael@0: { michael@0: /* Alpha in the output is irrelevant, simply map the opaque input michael@0: * pixels to the 6x6x6 color-map. michael@0: */ michael@0: if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) michael@0: png_error(png_ptr, "rgb color-map: too few entries"); michael@0: michael@0: cmap_entries = make_rgb_colormap(display); michael@0: output_processing = PNG_CMAP_RGB; michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_PALETTE: michael@0: /* It's already got a color-map. It may be necessary to eliminate the michael@0: * tRNS entries though. michael@0: */ michael@0: { michael@0: unsigned int num_trans = png_ptr->num_trans; michael@0: png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; michael@0: png_const_colorp colormap = png_ptr->palette; michael@0: const int do_background = trans != NULL && michael@0: (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; michael@0: unsigned int i; michael@0: michael@0: /* Just in case: */ michael@0: if (trans == NULL) michael@0: num_trans = 0; michael@0: michael@0: output_processing = PNG_CMAP_NONE; michael@0: data_encoding = P_FILE; /* Don't change from color-map indicies */ michael@0: cmap_entries = png_ptr->num_palette; michael@0: if (cmap_entries > 256) michael@0: cmap_entries = 256; michael@0: michael@0: if (cmap_entries > image->colormap_entries) michael@0: png_error(png_ptr, "palette color-map: too few entries"); michael@0: michael@0: for (i=0; i < cmap_entries; ++i) michael@0: { michael@0: if (do_background && i < num_trans && trans[i] < 255) michael@0: { michael@0: if (trans[i] == 0) michael@0: png_create_colormap_entry(display, i, back_r, back_g, michael@0: back_b, 0, output_encoding); michael@0: michael@0: else michael@0: { michael@0: /* Must compose the PNG file color in the color-map entry michael@0: * on the sRGB color in 'back'. michael@0: */ michael@0: png_create_colormap_entry(display, i, michael@0: png_colormap_compose(display, colormap[i].red, P_FILE, michael@0: trans[i], back_r, output_encoding), michael@0: png_colormap_compose(display, colormap[i].green, P_FILE, michael@0: trans[i], back_g, output_encoding), michael@0: png_colormap_compose(display, colormap[i].blue, P_FILE, michael@0: trans[i], back_b, output_encoding), michael@0: output_encoding == P_LINEAR ? trans[i] * 257U : michael@0: trans[i], michael@0: output_encoding); michael@0: } michael@0: } michael@0: michael@0: else michael@0: png_create_colormap_entry(display, i, colormap[i].red, michael@0: colormap[i].green, colormap[i].blue, michael@0: i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/); michael@0: } michael@0: michael@0: /* The PNG data may have indicies packed in fewer than 8 bits, it michael@0: * must be expanded if so. michael@0: */ michael@0: if (png_ptr->bit_depth < 8) michael@0: png_set_packing(png_ptr); michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "invalid PNG color type"); michael@0: /*NOT REACHED*/ michael@0: break; michael@0: } michael@0: michael@0: /* Now deal with the output processing */ michael@0: if (expand_tRNS && png_ptr->num_trans > 0 && michael@0: (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) michael@0: png_set_tRNS_to_alpha(png_ptr); michael@0: michael@0: switch (data_encoding) michael@0: { michael@0: default: michael@0: png_error(png_ptr, "bad data option (internal error)"); michael@0: break; michael@0: michael@0: case P_sRGB: michael@0: /* Change to 8-bit sRGB */ michael@0: png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); michael@0: /* FALL THROUGH */ michael@0: michael@0: case P_FILE: michael@0: if (png_ptr->bit_depth > 8) michael@0: png_set_scale_16(png_ptr); michael@0: break; michael@0: } michael@0: michael@0: if (cmap_entries > 256 || cmap_entries > image->colormap_entries) michael@0: png_error(png_ptr, "color map overflow (BAD internal error)"); michael@0: michael@0: image->colormap_entries = cmap_entries; michael@0: michael@0: /* Double check using the recorded background index */ michael@0: switch (output_processing) michael@0: { michael@0: case PNG_CMAP_NONE: michael@0: if (background_index != PNG_CMAP_NONE_BACKGROUND) michael@0: goto bad_background; michael@0: break; michael@0: michael@0: case PNG_CMAP_GA: michael@0: if (background_index != PNG_CMAP_GA_BACKGROUND) michael@0: goto bad_background; michael@0: break; michael@0: michael@0: case PNG_CMAP_TRANS: michael@0: if (background_index >= cmap_entries || michael@0: background_index != PNG_CMAP_TRANS_BACKGROUND) michael@0: goto bad_background; michael@0: break; michael@0: michael@0: case PNG_CMAP_RGB: michael@0: if (background_index != PNG_CMAP_RGB_BACKGROUND) michael@0: goto bad_background; michael@0: break; michael@0: michael@0: case PNG_CMAP_RGB_ALPHA: michael@0: if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) michael@0: goto bad_background; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "bad processing option (internal error)"); michael@0: michael@0: bad_background: michael@0: png_error(png_ptr, "bad background index (internal error)"); michael@0: } michael@0: michael@0: display->colormap_processing = output_processing; michael@0: michael@0: return 1/*ok*/; michael@0: } michael@0: michael@0: /* The final part of the color-map read called from png_image_finish_read. */ michael@0: static int michael@0: png_image_read_and_map(png_voidp argument) michael@0: { michael@0: png_image_read_control *display = png_voidcast(png_image_read_control*, michael@0: argument); michael@0: png_imagep image = display->image; michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: int passes; michael@0: michael@0: /* Called when the libpng data must be transformed into the color-mapped michael@0: * form. There is a local row buffer in display->local and this routine must michael@0: * do the interlace handling. michael@0: */ michael@0: switch (png_ptr->interlaced) michael@0: { michael@0: case PNG_INTERLACE_NONE: michael@0: passes = 1; michael@0: break; michael@0: michael@0: case PNG_INTERLACE_ADAM7: michael@0: passes = PNG_INTERLACE_ADAM7_PASSES; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "unknown interlace type"); michael@0: } michael@0: michael@0: { michael@0: png_uint_32 height = image->height; michael@0: png_uint_32 width = image->width; michael@0: int proc = display->colormap_processing; michael@0: png_bytep first_row = png_voidcast(png_bytep, display->first_row); michael@0: ptrdiff_t step_row = display->row_bytes; michael@0: int pass; michael@0: michael@0: for (pass = 0; pass < passes; ++pass) michael@0: { michael@0: unsigned int startx, stepx, stepy; michael@0: png_uint_32 y; michael@0: michael@0: if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) michael@0: { michael@0: /* The row may be empty for a short image: */ michael@0: if (PNG_PASS_COLS(width, pass) == 0) michael@0: continue; michael@0: michael@0: startx = PNG_PASS_START_COL(pass); michael@0: stepx = PNG_PASS_COL_OFFSET(pass); michael@0: y = PNG_PASS_START_ROW(pass); michael@0: stepy = PNG_PASS_ROW_OFFSET(pass); michael@0: } michael@0: michael@0: else michael@0: { michael@0: y = 0; michael@0: startx = 0; michael@0: stepx = stepy = 1; michael@0: } michael@0: michael@0: for (; ylocal_row); michael@0: png_bytep outrow = first_row + y * step_row; michael@0: png_const_bytep end_row = outrow + width; michael@0: michael@0: /* Read read the libpng data into the temporary buffer. */ michael@0: png_read_row(png_ptr, inrow, NULL); michael@0: michael@0: /* Now process the row according to the processing option, note michael@0: * that the caller verifies that the format of the libpng output michael@0: * data is as required. michael@0: */ michael@0: outrow += startx; michael@0: switch (proc) michael@0: { michael@0: case PNG_CMAP_GA: michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: /* The data is always in the PNG order */ michael@0: unsigned int gray = *inrow++; michael@0: unsigned int alpha = *inrow++; michael@0: unsigned int entry; michael@0: michael@0: /* NOTE: this code is copied as a comment in michael@0: * make_ga_colormap above. Please update the michael@0: * comment if you change this code! michael@0: */ michael@0: if (alpha > 229) /* opaque */ michael@0: { michael@0: entry = (231 * gray + 128) >> 8; michael@0: } michael@0: else if (alpha < 26) /* transparent */ michael@0: { michael@0: entry = 231; michael@0: } michael@0: else /* partially opaque */ michael@0: { michael@0: entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); michael@0: } michael@0: michael@0: *outrow = (png_byte)entry; michael@0: } michael@0: break; michael@0: michael@0: case PNG_CMAP_TRANS: michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: png_byte gray = *inrow++; michael@0: png_byte alpha = *inrow++; michael@0: michael@0: if (alpha == 0) michael@0: *outrow = PNG_CMAP_TRANS_BACKGROUND; michael@0: michael@0: else if (gray != PNG_CMAP_TRANS_BACKGROUND) michael@0: *outrow = gray; michael@0: michael@0: else michael@0: *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); michael@0: } michael@0: break; michael@0: michael@0: case PNG_CMAP_RGB: michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); michael@0: inrow += 3; michael@0: } michael@0: break; michael@0: michael@0: case PNG_CMAP_RGB_ALPHA: michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: unsigned int alpha = inrow[3]; michael@0: michael@0: /* Because the alpha entries only hold alpha==0.5 values michael@0: * split the processing at alpha==0.25 (64) and 0.75 michael@0: * (196). michael@0: */ michael@0: michael@0: if (alpha >= 196) michael@0: *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], michael@0: inrow[2]); michael@0: michael@0: else if (alpha < 64) michael@0: *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; michael@0: michael@0: else michael@0: { michael@0: /* Likewise there are three entries for each of r, g michael@0: * and b. We could select the entry by popcount on michael@0: * the top two bits on those architectures that michael@0: * support it, this is what the code below does, michael@0: * crudely. michael@0: */ michael@0: unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; michael@0: michael@0: /* Here are how the values map: michael@0: * michael@0: * 0x00 .. 0x3f -> 0 michael@0: * 0x40 .. 0xbf -> 1 michael@0: * 0xc0 .. 0xff -> 2 michael@0: * michael@0: * So, as above with the explicit alpha checks, the michael@0: * breakpoints are at 64 and 196. michael@0: */ michael@0: if (inrow[0] & 0x80) back_i += 9; /* red */ michael@0: if (inrow[0] & 0x40) back_i += 9; michael@0: if (inrow[0] & 0x80) back_i += 3; /* green */ michael@0: if (inrow[0] & 0x40) back_i += 3; michael@0: if (inrow[0] & 0x80) back_i += 1; /* blue */ michael@0: if (inrow[0] & 0x40) back_i += 1; michael@0: michael@0: *outrow = (png_byte)back_i; michael@0: } michael@0: michael@0: inrow += 4; michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: static int michael@0: png_image_read_colormapped(png_voidp argument) michael@0: { michael@0: png_image_read_control *display = png_voidcast(png_image_read_control*, michael@0: argument); michael@0: png_imagep image = display->image; michael@0: png_controlp control = image->opaque; michael@0: png_structrp png_ptr = control->png_ptr; michael@0: png_inforp info_ptr = control->info_ptr; michael@0: michael@0: int passes = 0; /* As a flag */ michael@0: michael@0: PNG_SKIP_CHUNKS(png_ptr); michael@0: michael@0: /* Update the 'info' structure and make sure the result is as required; first michael@0: * make sure to turn on the interlace handling if it will be required michael@0: * (because it can't be turned on *after* the call to png_read_update_info!) michael@0: */ michael@0: if (display->colormap_processing == PNG_CMAP_NONE) michael@0: passes = png_set_interlace_handling(png_ptr); michael@0: michael@0: png_read_update_info(png_ptr, info_ptr); michael@0: michael@0: /* The expected output can be deduced from the colormap_processing option. */ michael@0: switch (display->colormap_processing) michael@0: { michael@0: case PNG_CMAP_NONE: michael@0: /* Output must be one channel and one byte per pixel, the output michael@0: * encoding can be anything. michael@0: */ michael@0: if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || michael@0: info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && michael@0: info_ptr->bit_depth == 8) michael@0: break; michael@0: michael@0: goto bad_output; michael@0: michael@0: case PNG_CMAP_TRANS: michael@0: case PNG_CMAP_GA: michael@0: /* Output must be two channels and the 'G' one must be sRGB, the latter michael@0: * can be checked with an exact number because it should have been set michael@0: * to this number above! michael@0: */ michael@0: if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && michael@0: info_ptr->bit_depth == 8 && michael@0: png_ptr->screen_gamma == PNG_GAMMA_sRGB && michael@0: image->colormap_entries == 256) michael@0: break; michael@0: michael@0: goto bad_output; michael@0: michael@0: case PNG_CMAP_RGB: michael@0: /* Output must be 8-bit sRGB encoded RGB */ michael@0: if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && michael@0: info_ptr->bit_depth == 8 && michael@0: png_ptr->screen_gamma == PNG_GAMMA_sRGB && michael@0: image->colormap_entries == 216) michael@0: break; michael@0: michael@0: goto bad_output; michael@0: michael@0: case PNG_CMAP_RGB_ALPHA: michael@0: /* Output must be 8-bit sRGB encoded RGBA */ michael@0: if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && michael@0: info_ptr->bit_depth == 8 && michael@0: png_ptr->screen_gamma == PNG_GAMMA_sRGB && michael@0: image->colormap_entries == 244 /* 216 + 1 + 27 */) michael@0: break; michael@0: michael@0: /* goto bad_output; */ michael@0: /* FALL THROUGH */ michael@0: michael@0: default: michael@0: bad_output: michael@0: png_error(png_ptr, "bad color-map processing (internal error)"); michael@0: } michael@0: michael@0: /* Now read the rows. Do this here if it is possible to read directly into michael@0: * the output buffer, otherwise allocate a local row buffer of the maximum michael@0: * size libpng requires and call the relevant processing routine safely. michael@0: */ michael@0: { michael@0: png_voidp first_row = display->buffer; michael@0: ptrdiff_t row_bytes = display->row_stride; michael@0: michael@0: /* The following expression is designed to work correctly whether it gives michael@0: * a signed or an unsigned result. michael@0: */ michael@0: if (row_bytes < 0) michael@0: { michael@0: char *ptr = png_voidcast(char*, first_row); michael@0: ptr += (image->height-1) * (-row_bytes); michael@0: first_row = png_voidcast(png_voidp, ptr); michael@0: } michael@0: michael@0: display->first_row = first_row; michael@0: display->row_bytes = row_bytes; michael@0: } michael@0: michael@0: if (passes == 0) michael@0: { michael@0: int result; michael@0: png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); michael@0: michael@0: display->local_row = row; michael@0: result = png_safe_execute(image, png_image_read_and_map, display); michael@0: display->local_row = NULL; michael@0: png_free(png_ptr, row); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_alloc_size_t row_bytes = display->row_bytes; michael@0: michael@0: while (--passes >= 0) michael@0: { michael@0: png_uint_32 y = image->height; michael@0: png_bytep row = png_voidcast(png_bytep, display->first_row); michael@0: michael@0: while (y-- > 0) michael@0: { michael@0: png_read_row(png_ptr, row, NULL); michael@0: row += row_bytes; michael@0: } michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: /* Just the row reading part of png_image_read. */ michael@0: static int michael@0: png_image_read_composite(png_voidp argument) michael@0: { michael@0: png_image_read_control *display = png_voidcast(png_image_read_control*, michael@0: argument); michael@0: png_imagep image = display->image; michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: int passes; michael@0: michael@0: switch (png_ptr->interlaced) michael@0: { michael@0: case PNG_INTERLACE_NONE: michael@0: passes = 1; michael@0: break; michael@0: michael@0: case PNG_INTERLACE_ADAM7: michael@0: passes = PNG_INTERLACE_ADAM7_PASSES; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "unknown interlace type"); michael@0: } michael@0: michael@0: { michael@0: png_uint_32 height = image->height; michael@0: png_uint_32 width = image->width; michael@0: ptrdiff_t step_row = display->row_bytes; michael@0: unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1; michael@0: int pass; michael@0: michael@0: for (pass = 0; pass < passes; ++pass) michael@0: { michael@0: unsigned int startx, stepx, stepy; michael@0: png_uint_32 y; michael@0: michael@0: if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) michael@0: { michael@0: /* The row may be empty for a short image: */ michael@0: if (PNG_PASS_COLS(width, pass) == 0) michael@0: continue; michael@0: michael@0: startx = PNG_PASS_START_COL(pass) * channels; michael@0: stepx = PNG_PASS_COL_OFFSET(pass) * channels; michael@0: y = PNG_PASS_START_ROW(pass); michael@0: stepy = PNG_PASS_ROW_OFFSET(pass); michael@0: } michael@0: michael@0: else michael@0: { michael@0: y = 0; michael@0: startx = 0; michael@0: stepx = channels; michael@0: stepy = 1; michael@0: } michael@0: michael@0: for (; ylocal_row); michael@0: png_bytep outrow; michael@0: png_const_bytep end_row; michael@0: michael@0: /* Read the row, which is packed: */ michael@0: png_read_row(png_ptr, inrow, NULL); michael@0: michael@0: outrow = png_voidcast(png_bytep, display->first_row); michael@0: outrow += y * step_row; michael@0: end_row = outrow + width * channels; michael@0: michael@0: /* Now do the composition on each pixel in this row. */ michael@0: outrow += startx; michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: png_byte alpha = inrow[channels]; michael@0: michael@0: if (alpha > 0) /* else no change to the output */ michael@0: { michael@0: unsigned int c; michael@0: michael@0: for (c=0; cimage; michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: png_inforp info_ptr = image->opaque->info_ptr; michael@0: png_uint_32 height = image->height; michael@0: png_uint_32 width = image->width; michael@0: int pass, passes; michael@0: michael@0: /* Double check the convoluted logic below. We expect to get here with michael@0: * libpng doing rgb to gray and gamma correction but background processing michael@0: * left to the png_image_read_background function. The rows libpng produce michael@0: * might be 8 or 16-bit but should always have two channels; gray plus alpha. michael@0: */ michael@0: if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) michael@0: png_error(png_ptr, "lost rgb to gray"); michael@0: michael@0: if ((png_ptr->transformations & PNG_COMPOSE) != 0) michael@0: png_error(png_ptr, "unexpected compose"); michael@0: michael@0: if (png_get_channels(png_ptr, info_ptr) != 2) michael@0: png_error(png_ptr, "lost/gained channels"); michael@0: michael@0: /* Expect the 8-bit case to always remove the alpha channel */ michael@0: if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 && michael@0: (image->format & PNG_FORMAT_FLAG_ALPHA) != 0) michael@0: png_error(png_ptr, "unexpected 8-bit transformation"); michael@0: michael@0: switch (png_ptr->interlaced) michael@0: { michael@0: case PNG_INTERLACE_NONE: michael@0: passes = 1; michael@0: break; michael@0: michael@0: case PNG_INTERLACE_ADAM7: michael@0: passes = PNG_INTERLACE_ADAM7_PASSES; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "unknown interlace type"); michael@0: } michael@0: michael@0: /* Use direct access to info_ptr here because otherwise the simplified API michael@0: * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is michael@0: * checking the value after libpng expansions, not the original value in the michael@0: * PNG. michael@0: */ michael@0: switch (info_ptr->bit_depth) michael@0: { michael@0: default: michael@0: png_error(png_ptr, "unexpected bit depth"); michael@0: break; michael@0: michael@0: case 8: michael@0: /* 8-bit sRGB gray values with an alpha channel; the alpha channel is michael@0: * to be removed by composing on a background: either the row if michael@0: * display->background is NULL or display->background->green if not. michael@0: * Unlike the code above ALPHA_OPTIMIZED has *not* been done. michael@0: */ michael@0: { michael@0: png_bytep first_row = png_voidcast(png_bytep, display->first_row); michael@0: ptrdiff_t step_row = display->row_bytes; michael@0: michael@0: for (pass = 0; pass < passes; ++pass) michael@0: { michael@0: png_bytep row = png_voidcast(png_bytep, michael@0: display->first_row); michael@0: unsigned int startx, stepx, stepy; michael@0: png_uint_32 y; michael@0: michael@0: if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) michael@0: { michael@0: /* The row may be empty for a short image: */ michael@0: if (PNG_PASS_COLS(width, pass) == 0) michael@0: continue; michael@0: michael@0: startx = PNG_PASS_START_COL(pass); michael@0: stepx = PNG_PASS_COL_OFFSET(pass); michael@0: y = PNG_PASS_START_ROW(pass); michael@0: stepy = PNG_PASS_ROW_OFFSET(pass); michael@0: } michael@0: michael@0: else michael@0: { michael@0: y = 0; michael@0: startx = 0; michael@0: stepx = stepy = 1; michael@0: } michael@0: michael@0: if (display->background == NULL) michael@0: { michael@0: for (; ylocal_row); michael@0: png_bytep outrow = first_row + y * step_row; michael@0: png_const_bytep end_row = outrow + width; michael@0: michael@0: /* Read the row, which is packed: */ michael@0: png_read_row(png_ptr, inrow, NULL); michael@0: michael@0: /* Now do the composition on each pixel in this row. */ michael@0: outrow += startx; michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: png_byte alpha = inrow[1]; michael@0: michael@0: if (alpha > 0) /* else no change to the output */ michael@0: { michael@0: png_uint_32 component = inrow[0]; michael@0: michael@0: if (alpha < 255) /* else just use component */ michael@0: { michael@0: /* Since PNG_OPTIMIZED_ALPHA was not set it is michael@0: * necessary to invert the sRGB transfer michael@0: * function and multiply the alpha out. michael@0: */ michael@0: component = png_sRGB_table[component] * alpha; michael@0: component += png_sRGB_table[outrow[0]] * michael@0: (255-alpha); michael@0: component = PNG_sRGB_FROM_LINEAR(component); michael@0: } michael@0: michael@0: outrow[0] = (png_byte)component; michael@0: } michael@0: michael@0: inrow += 2; /* gray and alpha channel */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: else /* constant background value */ michael@0: { michael@0: png_byte background8 = display->background->green; michael@0: png_uint_16 background = png_sRGB_table[background8]; michael@0: michael@0: for (; ylocal_row); michael@0: png_bytep outrow = first_row + y * step_row; michael@0: png_const_bytep end_row = outrow + width; michael@0: michael@0: /* Read the row, which is packed: */ michael@0: png_read_row(png_ptr, inrow, NULL); michael@0: michael@0: /* Now do the composition on each pixel in this row. */ michael@0: outrow += startx; michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: png_byte alpha = inrow[1]; michael@0: michael@0: if (alpha > 0) /* else use background */ michael@0: { michael@0: png_uint_32 component = inrow[0]; michael@0: michael@0: if (alpha < 255) /* else just use component */ michael@0: { michael@0: component = png_sRGB_table[component] * alpha; michael@0: component += background * (255-alpha); michael@0: component = PNG_sRGB_FROM_LINEAR(component); michael@0: } michael@0: michael@0: outrow[0] = (png_byte)component; michael@0: } michael@0: michael@0: else michael@0: outrow[0] = background8; michael@0: michael@0: inrow += 2; /* gray and alpha channel */ michael@0: } michael@0: michael@0: row += display->row_bytes; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case 16: michael@0: /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must michael@0: * still be done and, maybe, the alpha channel removed. This code also michael@0: * handles the alpha-first option. michael@0: */ michael@0: { michael@0: png_uint_16p first_row = png_voidcast(png_uint_16p, michael@0: display->first_row); michael@0: /* The division by two is safe because the caller passed in a michael@0: * stride which was multiplied by 2 (below) to get row_bytes. michael@0: */ michael@0: ptrdiff_t step_row = display->row_bytes / 2; michael@0: int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; michael@0: unsigned int outchannels = 1+preserve_alpha; michael@0: int swap_alpha = 0; michael@0: michael@0: # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED michael@0: if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST)) michael@0: swap_alpha = 1; michael@0: # endif michael@0: michael@0: for (pass = 0; pass < passes; ++pass) michael@0: { michael@0: unsigned int startx, stepx, stepy; michael@0: png_uint_32 y; michael@0: michael@0: /* The 'x' start and step are adjusted to output components here. michael@0: */ michael@0: if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) michael@0: { michael@0: /* The row may be empty for a short image: */ michael@0: if (PNG_PASS_COLS(width, pass) == 0) michael@0: continue; michael@0: michael@0: startx = PNG_PASS_START_COL(pass) * outchannels; michael@0: stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; michael@0: y = PNG_PASS_START_ROW(pass); michael@0: stepy = PNG_PASS_ROW_OFFSET(pass); michael@0: } michael@0: michael@0: else michael@0: { michael@0: y = 0; michael@0: startx = 0; michael@0: stepx = outchannels; michael@0: stepy = 1; michael@0: } michael@0: michael@0: for (; ylocal_row), NULL); michael@0: inrow = png_voidcast(png_const_uint_16p, display->local_row); michael@0: michael@0: /* Now do the pre-multiplication on each pixel in this row. michael@0: */ michael@0: outrow += startx; michael@0: for (; outrow < end_row; outrow += stepx) michael@0: { michael@0: png_uint_32 component = inrow[0]; michael@0: png_uint_16 alpha = inrow[1]; michael@0: michael@0: if (alpha > 0) /* else 0 */ michael@0: { michael@0: if (alpha < 65535) /* else just use component */ michael@0: { michael@0: component *= alpha; michael@0: component += 32767; michael@0: component /= 65535; michael@0: } michael@0: } michael@0: michael@0: else michael@0: component = 0; michael@0: michael@0: outrow[swap_alpha] = (png_uint_16)component; michael@0: if (preserve_alpha) michael@0: outrow[1 ^ swap_alpha] = alpha; michael@0: michael@0: inrow += 2; /* components and alpha channel */ michael@0: } michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: /* The guts of png_image_finish_read as a png_safe_execute callback. */ michael@0: static int michael@0: png_image_read_direct(png_voidp argument) michael@0: { michael@0: png_image_read_control *display = png_voidcast(png_image_read_control*, michael@0: argument); michael@0: png_imagep image = display->image; michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: png_inforp info_ptr = image->opaque->info_ptr; michael@0: michael@0: png_uint_32 format = image->format; michael@0: int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; michael@0: int do_local_compose = 0; michael@0: int do_local_background = 0; /* to avoid double gamma correction bug */ michael@0: int passes = 0; michael@0: michael@0: /* Add transforms to ensure the correct output format is produced then check michael@0: * that the required implementation support is there. Always expand; always michael@0: * need 8 bits minimum, no palette and expanded tRNS. michael@0: */ michael@0: png_set_expand(png_ptr); michael@0: michael@0: /* Now check the format to see if it was modified. */ michael@0: { michael@0: png_uint_32 base_format = png_image_format(png_ptr) & michael@0: ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; michael@0: png_uint_32 change = format ^ base_format; michael@0: png_fixed_point output_gamma; michael@0: int mode; /* alpha mode */ michael@0: michael@0: /* Do this first so that we have a record if rgb to gray is happening. */ michael@0: if (change & PNG_FORMAT_FLAG_COLOR) michael@0: { michael@0: /* gray<->color transformation required. */ michael@0: if (format & PNG_FORMAT_FLAG_COLOR) michael@0: png_set_gray_to_rgb(png_ptr); michael@0: michael@0: else michael@0: { michael@0: /* libpng can't do both rgb to gray and michael@0: * background/pre-multiplication if there is also significant gamma michael@0: * correction, because both operations require linear colors and michael@0: * the code only supports one transform doing the gamma correction. michael@0: * Handle this by doing the pre-multiplication or background michael@0: * operation in this code, if necessary. michael@0: * michael@0: * TODO: fix this by rewriting pngrtran.c (!) michael@0: * michael@0: * For the moment (given that fixing this in pngrtran.c is an michael@0: * enormous change) 'do_local_background' is used to indicate that michael@0: * the problem exists. michael@0: */ michael@0: if (base_format & PNG_FORMAT_FLAG_ALPHA) michael@0: do_local_background = 1/*maybe*/; michael@0: michael@0: png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, michael@0: PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); michael@0: } michael@0: michael@0: change &= ~PNG_FORMAT_FLAG_COLOR; michael@0: } michael@0: michael@0: /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. michael@0: */ michael@0: { michael@0: png_fixed_point input_gamma_default; michael@0: michael@0: if ((base_format & PNG_FORMAT_FLAG_LINEAR) && michael@0: (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) michael@0: input_gamma_default = PNG_GAMMA_LINEAR; michael@0: else michael@0: input_gamma_default = PNG_DEFAULT_sRGB; michael@0: michael@0: /* Call png_set_alpha_mode to set the default for the input gamma; the michael@0: * output gamma is set by a second call below. michael@0: */ michael@0: png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); michael@0: } michael@0: michael@0: if (linear) michael@0: { michael@0: /* If there *is* an alpha channel in the input it must be multiplied michael@0: * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. michael@0: */ michael@0: if (base_format & PNG_FORMAT_FLAG_ALPHA) michael@0: mode = PNG_ALPHA_STANDARD; /* associated alpha */ michael@0: michael@0: else michael@0: mode = PNG_ALPHA_PNG; michael@0: michael@0: output_gamma = PNG_GAMMA_LINEAR; michael@0: } michael@0: michael@0: else michael@0: { michael@0: mode = PNG_ALPHA_PNG; michael@0: output_gamma = PNG_DEFAULT_sRGB; michael@0: } michael@0: michael@0: /* If 'do_local_background' is set check for the presence of gamma michael@0: * correction; this is part of the work-round for the libpng bug michael@0: * described above. michael@0: * michael@0: * TODO: fix libpng and remove this. michael@0: */ michael@0: if (do_local_background) michael@0: { michael@0: png_fixed_point gtest; michael@0: michael@0: /* This is 'png_gamma_threshold' from pngrtran.c; the test used for michael@0: * gamma correction, the screen gamma hasn't been set on png_struct michael@0: * yet; it's set below. png_struct::gamma, however, is set to the michael@0: * final value. michael@0: */ michael@0: if (png_muldiv(>est, output_gamma, png_ptr->colorspace.gamma, michael@0: PNG_FP_1) && !png_gamma_significant(gtest)) michael@0: do_local_background = 0; michael@0: michael@0: else if (mode == PNG_ALPHA_STANDARD) michael@0: { michael@0: do_local_background = 2/*required*/; michael@0: mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ michael@0: } michael@0: michael@0: /* else leave as 1 for the checks below */ michael@0: } michael@0: michael@0: /* If the bit-depth changes then handle that here. */ michael@0: if (change & PNG_FORMAT_FLAG_LINEAR) michael@0: { michael@0: if (linear /*16-bit output*/) michael@0: png_set_expand_16(png_ptr); michael@0: michael@0: else /* 8-bit output */ michael@0: png_set_scale_16(png_ptr); michael@0: michael@0: change &= ~PNG_FORMAT_FLAG_LINEAR; michael@0: } michael@0: michael@0: /* Now the background/alpha channel changes. */ michael@0: if (change & PNG_FORMAT_FLAG_ALPHA) michael@0: { michael@0: /* Removing an alpha channel requires composition for the 8-bit michael@0: * formats; for the 16-bit it is already done, above, by the michael@0: * pre-multiplication and the channel just needs to be stripped. michael@0: */ michael@0: if (base_format & PNG_FORMAT_FLAG_ALPHA) michael@0: { michael@0: /* If RGB->gray is happening the alpha channel must be left and the michael@0: * operation completed locally. michael@0: * michael@0: * TODO: fix libpng and remove this. michael@0: */ michael@0: if (do_local_background) michael@0: do_local_background = 2/*required*/; michael@0: michael@0: /* 16-bit output: just remove the channel */ michael@0: else if (linear) /* compose on black (well, pre-multiply) */ michael@0: png_set_strip_alpha(png_ptr); michael@0: michael@0: /* 8-bit output: do an appropriate compose */ michael@0: else if (display->background != NULL) michael@0: { michael@0: png_color_16 c; michael@0: michael@0: c.index = 0; /*unused*/ michael@0: c.red = display->background->red; michael@0: c.green = display->background->green; michael@0: c.blue = display->background->blue; michael@0: c.gray = display->background->green; michael@0: michael@0: /* This is always an 8-bit sRGB value, using the 'green' channel michael@0: * for gray is much better than calculating the luminance here; michael@0: * we can get off-by-one errors in that calculation relative to michael@0: * the app expectations and that will show up in transparent michael@0: * pixels. michael@0: */ michael@0: png_set_background_fixed(png_ptr, &c, michael@0: PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, michael@0: 0/*gamma: not used*/); michael@0: } michael@0: michael@0: else /* compose on row: implemented below. */ michael@0: { michael@0: do_local_compose = 1; michael@0: /* This leaves the alpha channel in the output, so it has to be michael@0: * removed by the code below. Set the encoding to the 'OPTIMIZE' michael@0: * one so the code only has to hack on the pixels that require michael@0: * composition. michael@0: */ michael@0: mode = PNG_ALPHA_OPTIMIZED; michael@0: } michael@0: } michael@0: michael@0: else /* output needs an alpha channel */ michael@0: { michael@0: /* This is tricky because it happens before the swap operation has michael@0: * been accomplished; however, the swap does *not* swap the added michael@0: * alpha channel (weird API), so it must be added in the correct michael@0: * place. michael@0: */ michael@0: png_uint_32 filler; /* opaque filler */ michael@0: int where; michael@0: michael@0: if (linear) michael@0: filler = 65535; michael@0: michael@0: else michael@0: filler = 255; michael@0: michael@0: # ifdef PNG_FORMAT_AFIRST_SUPPORTED michael@0: if (format & PNG_FORMAT_FLAG_AFIRST) michael@0: { michael@0: where = PNG_FILLER_BEFORE; michael@0: change &= ~PNG_FORMAT_FLAG_AFIRST; michael@0: } michael@0: michael@0: else michael@0: # endif michael@0: where = PNG_FILLER_AFTER; michael@0: michael@0: png_set_add_alpha(png_ptr, filler, where); michael@0: } michael@0: michael@0: /* This stops the (irrelevant) call to swap_alpha below. */ michael@0: change &= ~PNG_FORMAT_FLAG_ALPHA; michael@0: } michael@0: michael@0: /* Now set the alpha mode correctly; this is always done, even if there is michael@0: * no alpha channel in either the input or the output because it correctly michael@0: * sets the output gamma. michael@0: */ michael@0: png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); michael@0: michael@0: # ifdef PNG_FORMAT_BGR_SUPPORTED michael@0: if (change & PNG_FORMAT_FLAG_BGR) michael@0: { michael@0: /* Check only the output format; PNG is never BGR; don't do this if michael@0: * the output is gray, but fix up the 'format' value in that case. michael@0: */ michael@0: if (format & PNG_FORMAT_FLAG_COLOR) michael@0: png_set_bgr(png_ptr); michael@0: michael@0: else michael@0: format &= ~PNG_FORMAT_FLAG_BGR; michael@0: michael@0: change &= ~PNG_FORMAT_FLAG_BGR; michael@0: } michael@0: # endif michael@0: michael@0: # ifdef PNG_FORMAT_AFIRST_SUPPORTED michael@0: if (change & PNG_FORMAT_FLAG_AFIRST) michael@0: { michael@0: /* Only relevant if there is an alpha channel - it's particularly michael@0: * important to handle this correctly because do_local_compose may michael@0: * be set above and then libpng will keep the alpha channel for this michael@0: * code to remove. michael@0: */ michael@0: if (format & PNG_FORMAT_FLAG_ALPHA) michael@0: { michael@0: /* Disable this if doing a local background, michael@0: * TODO: remove this when local background is no longer required. michael@0: */ michael@0: if (do_local_background != 2) michael@0: png_set_swap_alpha(png_ptr); michael@0: } michael@0: michael@0: else michael@0: format &= ~PNG_FORMAT_FLAG_AFIRST; michael@0: michael@0: change &= ~PNG_FORMAT_FLAG_AFIRST; michael@0: } michael@0: # endif michael@0: michael@0: /* If the *output* is 16-bit then we need to check for a byte-swap on this michael@0: * architecture. michael@0: */ michael@0: if (linear) michael@0: { michael@0: PNG_CONST png_uint_16 le = 0x0001; michael@0: michael@0: if (*(png_const_bytep)&le) michael@0: png_set_swap(png_ptr); michael@0: } michael@0: michael@0: /* If change is not now 0 some transformation is missing - error out. */ michael@0: if (change) michael@0: png_error(png_ptr, "png_read_image: unsupported transformation"); michael@0: } michael@0: michael@0: PNG_SKIP_CHUNKS(png_ptr); michael@0: michael@0: /* Update the 'info' structure and make sure the result is as required; first michael@0: * make sure to turn on the interlace handling if it will be required michael@0: * (because it can't be turned on *after* the call to png_read_update_info!) michael@0: * michael@0: * TODO: remove the do_local_background fixup below. michael@0: */ michael@0: if (!do_local_compose && do_local_background != 2) michael@0: passes = png_set_interlace_handling(png_ptr); michael@0: michael@0: png_read_update_info(png_ptr, info_ptr); michael@0: michael@0: { michael@0: png_uint_32 info_format = 0; michael@0: michael@0: if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) michael@0: info_format |= PNG_FORMAT_FLAG_COLOR; michael@0: michael@0: if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) michael@0: { michael@0: /* do_local_compose removes this channel below. */ michael@0: if (!do_local_compose) michael@0: { michael@0: /* do_local_background does the same if required. */ michael@0: if (do_local_background != 2 || michael@0: (format & PNG_FORMAT_FLAG_ALPHA) != 0) michael@0: info_format |= PNG_FORMAT_FLAG_ALPHA; michael@0: } michael@0: } michael@0: michael@0: else if (do_local_compose) /* internal error */ michael@0: png_error(png_ptr, "png_image_read: alpha channel lost"); michael@0: michael@0: if (info_ptr->bit_depth == 16) michael@0: info_format |= PNG_FORMAT_FLAG_LINEAR; michael@0: michael@0: # ifdef PNG_FORMAT_BGR_SUPPORTED michael@0: if (png_ptr->transformations & PNG_BGR) michael@0: info_format |= PNG_FORMAT_FLAG_BGR; michael@0: # endif michael@0: michael@0: # ifdef PNG_FORMAT_AFIRST_SUPPORTED michael@0: if (do_local_background == 2) michael@0: { michael@0: if (format & PNG_FORMAT_FLAG_AFIRST) michael@0: info_format |= PNG_FORMAT_FLAG_AFIRST; michael@0: } michael@0: michael@0: if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || michael@0: ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && michael@0: (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) michael@0: { michael@0: if (do_local_background == 2) michael@0: png_error(png_ptr, "unexpected alpha swap transformation"); michael@0: michael@0: info_format |= PNG_FORMAT_FLAG_AFIRST; michael@0: } michael@0: # endif michael@0: michael@0: /* This is actually an internal error. */ michael@0: if (info_format != format) michael@0: png_error(png_ptr, "png_read_image: invalid transformations"); michael@0: } michael@0: michael@0: /* Now read the rows. If do_local_compose is set then it is necessary to use michael@0: * a local row buffer. The output will be GA, RGBA or BGRA and must be michael@0: * converted to G, RGB or BGR as appropriate. The 'local_row' member of the michael@0: * display acts as a flag. michael@0: */ michael@0: { michael@0: png_voidp first_row = display->buffer; michael@0: ptrdiff_t row_bytes = display->row_stride; michael@0: michael@0: if (linear) michael@0: row_bytes *= 2; michael@0: michael@0: /* The following expression is designed to work correctly whether it gives michael@0: * a signed or an unsigned result. michael@0: */ michael@0: if (row_bytes < 0) michael@0: { michael@0: char *ptr = png_voidcast(char*, first_row); michael@0: ptr += (image->height-1) * (-row_bytes); michael@0: first_row = png_voidcast(png_voidp, ptr); michael@0: } michael@0: michael@0: display->first_row = first_row; michael@0: display->row_bytes = row_bytes; michael@0: } michael@0: michael@0: if (do_local_compose) michael@0: { michael@0: int result; michael@0: png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); michael@0: michael@0: display->local_row = row; michael@0: result = png_safe_execute(image, png_image_read_composite, display); michael@0: display->local_row = NULL; michael@0: png_free(png_ptr, row); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: else if (do_local_background == 2) michael@0: { michael@0: int result; michael@0: png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); michael@0: michael@0: display->local_row = row; michael@0: result = png_safe_execute(image, png_image_read_background, display); michael@0: display->local_row = NULL; michael@0: png_free(png_ptr, row); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_alloc_size_t row_bytes = display->row_bytes; michael@0: michael@0: while (--passes >= 0) michael@0: { michael@0: png_uint_32 y = image->height; michael@0: png_bytep row = png_voidcast(png_bytep, display->first_row); michael@0: michael@0: while (y-- > 0) michael@0: { michael@0: png_read_row(png_ptr, row, NULL); michael@0: row += row_bytes; michael@0: } michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: int PNGAPI michael@0: png_image_finish_read(png_imagep image, png_const_colorp background, michael@0: void *buffer, png_int_32 row_stride, void *colormap) michael@0: { michael@0: if (image != NULL && image->version == PNG_IMAGE_VERSION) michael@0: { michael@0: png_uint_32 check; michael@0: michael@0: if (row_stride == 0) michael@0: row_stride = PNG_IMAGE_ROW_STRIDE(*image); michael@0: michael@0: if (row_stride < 0) michael@0: check = -row_stride; michael@0: michael@0: else michael@0: check = row_stride; michael@0: michael@0: if (image->opaque != NULL && buffer != NULL && michael@0: check >= PNG_IMAGE_ROW_STRIDE(*image)) michael@0: { michael@0: if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || michael@0: (image->colormap_entries > 0 && colormap != NULL)) michael@0: { michael@0: int result; michael@0: png_image_read_control display; michael@0: michael@0: memset(&display, 0, (sizeof display)); michael@0: display.image = image; michael@0: display.buffer = buffer; michael@0: display.row_stride = row_stride; michael@0: display.colormap = colormap; michael@0: display.background = background; michael@0: display.local_row = NULL; michael@0: michael@0: /* Choose the correct 'end' routine; for the color-map case all the michael@0: * setup has already been done. michael@0: */ michael@0: if (image->format & PNG_FORMAT_FLAG_COLORMAP) michael@0: result = michael@0: png_safe_execute(image, png_image_read_colormap, &display) && michael@0: png_safe_execute(image, png_image_read_colormapped, &display); michael@0: michael@0: else michael@0: result = michael@0: png_safe_execute(image, png_image_read_direct, &display); michael@0: michael@0: png_image_free(image); michael@0: return result; michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, michael@0: "png_image_finish_read[color-map]: no color-map"); michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, michael@0: "png_image_finish_read: invalid argument"); michael@0: } michael@0: michael@0: else if (image != NULL) michael@0: return png_image_error(image, michael@0: "png_image_finish_read: damaged PNG_IMAGE_VERSION"); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */ michael@0: #endif /* PNG_READ_SUPPORTED */