nuclear@1: nuclear@1: /* pngrutil.c - utilities to read a PNG file nuclear@1: * nuclear@1: * Last changed in libpng 1.2.33 [October 31, 2008] nuclear@1: * For conditions of distribution and use, see copyright notice in png.h nuclear@1: * Copyright (c) 1998-2008 Glenn Randers-Pehrson nuclear@1: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) nuclear@1: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) nuclear@1: * nuclear@1: * This file contains routines that are only called from within nuclear@1: * libpng itself during the course of reading an image. nuclear@1: */ nuclear@1: nuclear@1: #define PNG_INTERNAL nuclear@1: #include "png.h" nuclear@1: #if defined(PNG_READ_SUPPORTED) nuclear@1: nuclear@1: #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500) nuclear@1: # define WIN32_WCE_OLD nuclear@1: #endif nuclear@1: nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: # if defined(WIN32_WCE_OLD) nuclear@1: /* strtod() function is not supported on WindowsCE */ nuclear@1: __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr) nuclear@1: { nuclear@1: double result = 0; nuclear@1: int len; nuclear@1: wchar_t *str, *end; nuclear@1: nuclear@1: len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0); nuclear@1: str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t)); nuclear@1: if ( NULL != str ) nuclear@1: { nuclear@1: MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len); nuclear@1: result = wcstod(str, &end); nuclear@1: len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL); nuclear@1: *endptr = (char *)nptr + (png_strlen(nptr) - len + 1); nuclear@1: png_free(png_ptr, str); nuclear@1: } nuclear@1: return result; nuclear@1: } nuclear@1: # else nuclear@1: # define png_strtod(p,a,b) strtod(a,b) nuclear@1: # endif nuclear@1: #endif nuclear@1: nuclear@1: png_uint_32 PNGAPI nuclear@1: png_get_uint_31(png_structp png_ptr, png_bytep buf) nuclear@1: { nuclear@1: #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED nuclear@1: png_uint_32 i = png_get_uint_32(buf); nuclear@1: #else nuclear@1: /* Avoid an extra function call by inlining the result. */ nuclear@1: png_uint_32 i = ((png_uint_32)(*buf) << 24) + nuclear@1: ((png_uint_32)(*(buf + 1)) << 16) + nuclear@1: ((png_uint_32)(*(buf + 2)) << 8) + nuclear@1: (png_uint_32)(*(buf + 3)); nuclear@1: #endif nuclear@1: if (i > PNG_UINT_31_MAX) nuclear@1: png_error(png_ptr, "PNG unsigned integer out of range."); nuclear@1: return (i); nuclear@1: } nuclear@1: #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED nuclear@1: /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ nuclear@1: png_uint_32 PNGAPI nuclear@1: png_get_uint_32(png_bytep buf) nuclear@1: { nuclear@1: png_uint_32 i = ((png_uint_32)(*buf) << 24) + nuclear@1: ((png_uint_32)(*(buf + 1)) << 16) + nuclear@1: ((png_uint_32)(*(buf + 2)) << 8) + nuclear@1: (png_uint_32)(*(buf + 3)); nuclear@1: nuclear@1: return (i); nuclear@1: } nuclear@1: nuclear@1: /* Grab a signed 32-bit integer from a buffer in big-endian format. The nuclear@1: * data is stored in the PNG file in two's complement format, and it is nuclear@1: * assumed that the machine format for signed integers is the same. */ nuclear@1: png_int_32 PNGAPI nuclear@1: png_get_int_32(png_bytep buf) nuclear@1: { nuclear@1: png_int_32 i = ((png_int_32)(*buf) << 24) + nuclear@1: ((png_int_32)(*(buf + 1)) << 16) + nuclear@1: ((png_int_32)(*(buf + 2)) << 8) + nuclear@1: (png_int_32)(*(buf + 3)); nuclear@1: nuclear@1: return (i); nuclear@1: } nuclear@1: nuclear@1: /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ nuclear@1: png_uint_16 PNGAPI nuclear@1: png_get_uint_16(png_bytep buf) nuclear@1: { nuclear@1: png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) + nuclear@1: (png_uint_16)(*(buf + 1))); nuclear@1: nuclear@1: return (i); nuclear@1: } nuclear@1: #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */ nuclear@1: nuclear@1: /* Read the chunk header (length + type name). nuclear@1: * Put the type name into png_ptr->chunk_name, and return the length. nuclear@1: */ nuclear@1: png_uint_32 /* PRIVATE */ nuclear@1: png_read_chunk_header(png_structp png_ptr) nuclear@1: { nuclear@1: png_byte buf[8]; nuclear@1: png_uint_32 length; nuclear@1: nuclear@1: /* read the length and the chunk name */ nuclear@1: png_read_data(png_ptr, buf, 8); nuclear@1: length = png_get_uint_31(png_ptr, buf); nuclear@1: nuclear@1: /* put the chunk name into png_ptr->chunk_name */ nuclear@1: png_memcpy(png_ptr->chunk_name, buf + 4, 4); nuclear@1: nuclear@1: png_debug2(0, "Reading %s chunk, length = %lu\n", nuclear@1: png_ptr->chunk_name, length); nuclear@1: nuclear@1: /* reset the crc and run it over the chunk name */ nuclear@1: png_reset_crc(png_ptr); nuclear@1: png_calculate_crc(png_ptr, png_ptr->chunk_name, 4); nuclear@1: nuclear@1: /* check to see if chunk name is valid */ nuclear@1: png_check_chunk_name(png_ptr, png_ptr->chunk_name); nuclear@1: nuclear@1: return length; nuclear@1: } nuclear@1: nuclear@1: /* Read data, and (optionally) run it through the CRC. */ nuclear@1: void /* PRIVATE */ nuclear@1: png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) nuclear@1: { nuclear@1: if (png_ptr == NULL) return; nuclear@1: png_read_data(png_ptr, buf, length); nuclear@1: png_calculate_crc(png_ptr, buf, length); nuclear@1: } nuclear@1: nuclear@1: /* Optionally skip data and then check the CRC. Depending on whether we nuclear@1: are reading a ancillary or critical chunk, and how the program has set nuclear@1: things up, we may calculate the CRC on the data and print a message. nuclear@1: Returns '1' if there was a CRC error, '0' otherwise. */ nuclear@1: int /* PRIVATE */ nuclear@1: png_crc_finish(png_structp png_ptr, png_uint_32 skip) nuclear@1: { nuclear@1: png_size_t i; nuclear@1: png_size_t istop = png_ptr->zbuf_size; nuclear@1: nuclear@1: for (i = (png_size_t)skip; i > istop; i -= istop) nuclear@1: { nuclear@1: png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); nuclear@1: } nuclear@1: if (i) nuclear@1: { nuclear@1: png_crc_read(png_ptr, png_ptr->zbuf, i); nuclear@1: } nuclear@1: nuclear@1: if (png_crc_error(png_ptr)) nuclear@1: { nuclear@1: if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */ nuclear@1: !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) || nuclear@1: (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */ nuclear@1: (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))) nuclear@1: { nuclear@1: png_chunk_warning(png_ptr, "CRC error"); nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: png_chunk_error(png_ptr, "CRC error"); nuclear@1: } nuclear@1: return (1); nuclear@1: } nuclear@1: nuclear@1: return (0); nuclear@1: } nuclear@1: nuclear@1: /* Compare the CRC stored in the PNG file with that calculated by libpng from nuclear@1: the data it has read thus far. */ nuclear@1: int /* PRIVATE */ nuclear@1: png_crc_error(png_structp png_ptr) nuclear@1: { nuclear@1: png_byte crc_bytes[4]; nuclear@1: png_uint_32 crc; nuclear@1: int need_crc = 1; nuclear@1: nuclear@1: if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ nuclear@1: { nuclear@1: if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == nuclear@1: (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) nuclear@1: need_crc = 0; nuclear@1: } nuclear@1: else /* critical */ nuclear@1: { nuclear@1: if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) nuclear@1: need_crc = 0; nuclear@1: } nuclear@1: nuclear@1: png_read_data(png_ptr, crc_bytes, 4); nuclear@1: nuclear@1: if (need_crc) nuclear@1: { nuclear@1: crc = png_get_uint_32(crc_bytes); nuclear@1: return ((int)(crc != png_ptr->crc)); nuclear@1: } nuclear@1: else nuclear@1: return (0); nuclear@1: } nuclear@1: nuclear@1: #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \ nuclear@1: defined(PNG_READ_iCCP_SUPPORTED) nuclear@1: /* nuclear@1: * Decompress trailing data in a chunk. The assumption is that chunkdata nuclear@1: * points at an allocated area holding the contents of a chunk with a nuclear@1: * trailing compressed part. What we get back is an allocated area nuclear@1: * holding the original prefix part and an uncompressed version of the nuclear@1: * trailing part (the malloc area passed in is freed). nuclear@1: */ nuclear@1: void /* PRIVATE */ nuclear@1: png_decompress_chunk(png_structp png_ptr, int comp_type, nuclear@1: png_size_t chunklength, nuclear@1: png_size_t prefix_size, png_size_t *newlength) nuclear@1: { nuclear@1: static PNG_CONST char msg[] = "Error decoding compressed text"; nuclear@1: png_charp text; nuclear@1: png_size_t text_size; nuclear@1: nuclear@1: if (comp_type == PNG_COMPRESSION_TYPE_BASE) nuclear@1: { nuclear@1: int ret = Z_OK; nuclear@1: png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size); nuclear@1: png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size); nuclear@1: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@1: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@1: nuclear@1: text_size = 0; nuclear@1: text = NULL; nuclear@1: nuclear@1: while (png_ptr->zstream.avail_in) nuclear@1: { nuclear@1: ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); nuclear@1: if (ret != Z_OK && ret != Z_STREAM_END) nuclear@1: { nuclear@1: if (png_ptr->zstream.msg != NULL) nuclear@1: png_warning(png_ptr, png_ptr->zstream.msg); nuclear@1: else nuclear@1: png_warning(png_ptr, msg); nuclear@1: inflateReset(&png_ptr->zstream); nuclear@1: png_ptr->zstream.avail_in = 0; nuclear@1: nuclear@1: if (text == NULL) nuclear@1: { nuclear@1: text_size = prefix_size + png_sizeof(msg) + 1; nuclear@1: text = (png_charp)png_malloc_warn(png_ptr, text_size); nuclear@1: if (text == NULL) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_error(png_ptr, "Not enough memory to decompress chunk"); nuclear@1: } nuclear@1: png_memcpy(text, png_ptr->chunkdata, prefix_size); nuclear@1: } nuclear@1: nuclear@1: text[text_size - 1] = 0x00; nuclear@1: nuclear@1: /* Copy what we can of the error message into the text chunk */ nuclear@1: text_size = (png_size_t)(chunklength - nuclear@1: (text - png_ptr->chunkdata) - 1); nuclear@1: if (text_size > png_sizeof(msg)) nuclear@1: text_size = png_sizeof(msg); nuclear@1: png_memcpy(text + prefix_size, msg, text_size); nuclear@1: break; nuclear@1: } nuclear@1: if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) nuclear@1: { nuclear@1: if (text == NULL) nuclear@1: { nuclear@1: text_size = prefix_size + nuclear@1: png_ptr->zbuf_size - png_ptr->zstream.avail_out; nuclear@1: text = (png_charp)png_malloc_warn(png_ptr, text_size + 1); nuclear@1: if (text == NULL) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_error(png_ptr, nuclear@1: "Not enough memory to decompress chunk."); nuclear@1: } nuclear@1: png_memcpy(text + prefix_size, png_ptr->zbuf, nuclear@1: text_size - prefix_size); nuclear@1: png_memcpy(text, png_ptr->chunkdata, prefix_size); nuclear@1: *(text + text_size) = 0x00; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: png_charp tmp; nuclear@1: nuclear@1: tmp = text; nuclear@1: text = (png_charp)png_malloc_warn(png_ptr, nuclear@1: (png_uint_32)(text_size + nuclear@1: png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1)); nuclear@1: if (text == NULL) nuclear@1: { nuclear@1: png_free(png_ptr, tmp); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_error(png_ptr, nuclear@1: "Not enough memory to decompress chunk.."); nuclear@1: } nuclear@1: png_memcpy(text, tmp, text_size); nuclear@1: png_free(png_ptr, tmp); nuclear@1: png_memcpy(text + text_size, png_ptr->zbuf, nuclear@1: (png_ptr->zbuf_size - png_ptr->zstream.avail_out)); nuclear@1: text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; nuclear@1: *(text + text_size) = 0x00; nuclear@1: } nuclear@1: if (ret == Z_STREAM_END) nuclear@1: break; nuclear@1: else nuclear@1: { nuclear@1: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@1: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: if (ret != Z_STREAM_END) nuclear@1: { nuclear@1: #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) nuclear@1: char umsg[52]; nuclear@1: nuclear@1: if (ret == Z_BUF_ERROR) nuclear@1: png_snprintf(umsg, 52, nuclear@1: "Buffer error in compressed datastream in %s chunk", nuclear@1: png_ptr->chunk_name); nuclear@1: else if (ret == Z_DATA_ERROR) nuclear@1: png_snprintf(umsg, 52, nuclear@1: "Data error in compressed datastream in %s chunk", nuclear@1: png_ptr->chunk_name); nuclear@1: else nuclear@1: png_snprintf(umsg, 52, nuclear@1: "Incomplete compressed datastream in %s chunk", nuclear@1: png_ptr->chunk_name); nuclear@1: png_warning(png_ptr, umsg); nuclear@1: #else nuclear@1: png_warning(png_ptr, nuclear@1: "Incomplete compressed datastream in chunk other than IDAT"); nuclear@1: #endif nuclear@1: text_size = prefix_size; nuclear@1: if (text == NULL) nuclear@1: { nuclear@1: text = (png_charp)png_malloc_warn(png_ptr, text_size+1); nuclear@1: if (text == NULL) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_error(png_ptr, "Not enough memory for text."); nuclear@1: } nuclear@1: png_memcpy(text, png_ptr->chunkdata, prefix_size); nuclear@1: } nuclear@1: *(text + text_size) = 0x00; nuclear@1: } nuclear@1: nuclear@1: inflateReset(&png_ptr->zstream); nuclear@1: png_ptr->zstream.avail_in = 0; nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = text; nuclear@1: *newlength=text_size; nuclear@1: } nuclear@1: else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */ nuclear@1: { nuclear@1: #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) nuclear@1: char umsg[50]; nuclear@1: nuclear@1: png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type); nuclear@1: png_warning(png_ptr, umsg); nuclear@1: #else nuclear@1: png_warning(png_ptr, "Unknown zTXt compression type"); nuclear@1: #endif nuclear@1: nuclear@1: *(png_ptr->chunkdata + prefix_size) = 0x00; nuclear@1: *newlength = prefix_size; nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: /* read and check the IDHR chunk */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_byte buf[13]; nuclear@1: png_uint_32 width, height; nuclear@1: int bit_depth, color_type, compression_type, filter_type; nuclear@1: int interlace_type; nuclear@1: nuclear@1: png_debug(1, "in png_handle_IHDR\n"); nuclear@1: nuclear@1: if (png_ptr->mode & PNG_HAVE_IHDR) nuclear@1: png_error(png_ptr, "Out of place IHDR"); nuclear@1: nuclear@1: /* check the length */ nuclear@1: if (length != 13) nuclear@1: png_error(png_ptr, "Invalid IHDR chunk"); nuclear@1: nuclear@1: png_ptr->mode |= PNG_HAVE_IHDR; nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 13); nuclear@1: png_crc_finish(png_ptr, 0); nuclear@1: nuclear@1: width = png_get_uint_31(png_ptr, buf); nuclear@1: height = png_get_uint_31(png_ptr, buf + 4); nuclear@1: bit_depth = buf[8]; nuclear@1: color_type = buf[9]; nuclear@1: compression_type = buf[10]; nuclear@1: filter_type = buf[11]; nuclear@1: interlace_type = buf[12]; nuclear@1: nuclear@1: /* set internal variables */ nuclear@1: png_ptr->width = width; nuclear@1: png_ptr->height = height; nuclear@1: png_ptr->bit_depth = (png_byte)bit_depth; nuclear@1: png_ptr->interlaced = (png_byte)interlace_type; nuclear@1: png_ptr->color_type = (png_byte)color_type; nuclear@1: #if defined(PNG_MNG_FEATURES_SUPPORTED) nuclear@1: png_ptr->filter_type = (png_byte)filter_type; nuclear@1: #endif nuclear@1: png_ptr->compression_type = (png_byte)compression_type; nuclear@1: nuclear@1: /* find number of channels */ nuclear@1: switch (png_ptr->color_type) nuclear@1: { nuclear@1: case PNG_COLOR_TYPE_GRAY: nuclear@1: case PNG_COLOR_TYPE_PALETTE: nuclear@1: png_ptr->channels = 1; nuclear@1: break; nuclear@1: case PNG_COLOR_TYPE_RGB: nuclear@1: png_ptr->channels = 3; nuclear@1: break; nuclear@1: case PNG_COLOR_TYPE_GRAY_ALPHA: nuclear@1: png_ptr->channels = 2; nuclear@1: break; nuclear@1: case PNG_COLOR_TYPE_RGB_ALPHA: nuclear@1: png_ptr->channels = 4; nuclear@1: break; nuclear@1: } nuclear@1: nuclear@1: /* set up other useful info */ nuclear@1: png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * nuclear@1: png_ptr->channels); nuclear@1: png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); nuclear@1: png_debug1(3, "bit_depth = %d\n", png_ptr->bit_depth); nuclear@1: png_debug1(3, "channels = %d\n", png_ptr->channels); nuclear@1: png_debug1(3, "rowbytes = %lu\n", png_ptr->rowbytes); nuclear@1: png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, nuclear@1: color_type, interlace_type, compression_type, filter_type); nuclear@1: } nuclear@1: nuclear@1: /* read and check the palette */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_color palette[PNG_MAX_PALETTE_LENGTH]; nuclear@1: int num, i; nuclear@1: #ifndef PNG_NO_POINTER_INDEXING nuclear@1: png_colorp pal_ptr; nuclear@1: #endif nuclear@1: nuclear@1: png_debug(1, "in png_handle_PLTE\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before PLTE"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid PLTE after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (png_ptr->mode & PNG_HAVE_PLTE) nuclear@1: png_error(png_ptr, "Duplicate PLTE chunk"); nuclear@1: nuclear@1: png_ptr->mode |= PNG_HAVE_PLTE; nuclear@1: nuclear@1: if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) nuclear@1: { nuclear@1: png_warning(png_ptr, nuclear@1: "Ignoring PLTE chunk in grayscale PNG"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) nuclear@1: if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) nuclear@1: { nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) nuclear@1: { nuclear@1: if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid palette chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: png_error(png_ptr, "Invalid palette chunk"); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: num = (int)length / 3; nuclear@1: nuclear@1: #ifndef PNG_NO_POINTER_INDEXING nuclear@1: for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) nuclear@1: { nuclear@1: png_byte buf[3]; nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 3); nuclear@1: pal_ptr->red = buf[0]; nuclear@1: pal_ptr->green = buf[1]; nuclear@1: pal_ptr->blue = buf[2]; nuclear@1: } nuclear@1: #else nuclear@1: for (i = 0; i < num; i++) nuclear@1: { nuclear@1: png_byte buf[3]; nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 3); nuclear@1: /* don't depend upon png_color being any order */ nuclear@1: palette[i].red = buf[0]; nuclear@1: palette[i].green = buf[1]; nuclear@1: palette[i].blue = buf[2]; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: /* If we actually NEED the PLTE chunk (ie for a paletted image), we do nuclear@1: whatever the normal CRC configuration tells us. However, if we nuclear@1: have an RGB image, the PLTE can be considered ancillary, so nuclear@1: we will act as though it is. */ nuclear@1: #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: #endif nuclear@1: { nuclear@1: png_crc_finish(png_ptr, 0); nuclear@1: } nuclear@1: #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) nuclear@1: else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ nuclear@1: { nuclear@1: /* If we don't want to use the data from an ancillary chunk, nuclear@1: we have two options: an error abort, or a warning and we nuclear@1: ignore the data in this chunk (which should be OK, since nuclear@1: it's considered ancillary for a RGB or RGBA image). */ nuclear@1: if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) nuclear@1: { nuclear@1: if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) nuclear@1: { nuclear@1: png_chunk_error(png_ptr, "CRC error"); nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: png_chunk_warning(png_ptr, "CRC error"); nuclear@1: return; nuclear@1: } nuclear@1: } nuclear@1: /* Otherwise, we (optionally) emit a warning and use the chunk. */ nuclear@1: else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) nuclear@1: { nuclear@1: png_chunk_warning(png_ptr, "CRC error"); nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: png_set_PLTE(png_ptr, info_ptr, palette, num); nuclear@1: nuclear@1: #if defined(PNG_READ_tRNS_SUPPORTED) nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: { nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) nuclear@1: { nuclear@1: if (png_ptr->num_trans > (png_uint_16)num) nuclear@1: { nuclear@1: png_warning(png_ptr, "Truncating incorrect tRNS chunk length"); nuclear@1: png_ptr->num_trans = (png_uint_16)num; nuclear@1: } nuclear@1: if (info_ptr->num_trans > (png_uint_16)num) nuclear@1: { nuclear@1: png_warning(png_ptr, "Truncating incorrect info tRNS chunk length"); nuclear@1: info_ptr->num_trans = (png_uint_16)num; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: } nuclear@1: nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_debug(1, "in png_handle_IEND\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) nuclear@1: { nuclear@1: png_error(png_ptr, "No image in file"); nuclear@1: } nuclear@1: nuclear@1: png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); nuclear@1: nuclear@1: if (length != 0) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect IEND chunk length"); nuclear@1: } nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: nuclear@1: info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */ nuclear@1: } nuclear@1: nuclear@1: #if defined(PNG_READ_gAMA_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_fixed_point igamma; nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: float file_gamma; nuclear@1: #endif nuclear@1: png_byte buf[4]; nuclear@1: nuclear@1: png_debug(1, "in png_handle_gAMA\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before gAMA"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid gAMA after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (png_ptr->mode & PNG_HAVE_PLTE) nuclear@1: /* Should be an error, but we can cope with it */ nuclear@1: png_warning(png_ptr, "Out of place gAMA chunk"); nuclear@1: nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) nuclear@1: #if defined(PNG_READ_sRGB_SUPPORTED) nuclear@1: && !(info_ptr->valid & PNG_INFO_sRGB) nuclear@1: #endif nuclear@1: ) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate gAMA chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (length != 4) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect gAMA chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 4); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: igamma = (png_fixed_point)png_get_uint_32(buf); nuclear@1: /* check for zero gamma */ nuclear@1: if (igamma == 0) nuclear@1: { nuclear@1: png_warning(png_ptr, nuclear@1: "Ignoring gAMA chunk with gamma=0"); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: #if defined(PNG_READ_sRGB_SUPPORTED) nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) nuclear@1: if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) nuclear@1: { nuclear@1: png_warning(png_ptr, nuclear@1: "Ignoring incorrect gAMA value when sRGB is also present"); nuclear@1: #ifndef PNG_NO_CONSOLE_IO nuclear@1: fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma); nuclear@1: #endif nuclear@1: return; nuclear@1: } nuclear@1: #endif /* PNG_READ_sRGB_SUPPORTED */ nuclear@1: nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: file_gamma = (float)igamma / (float)100000.0; nuclear@1: # ifdef PNG_READ_GAMMA_SUPPORTED nuclear@1: png_ptr->gamma = file_gamma; nuclear@1: # endif nuclear@1: png_set_gAMA(png_ptr, info_ptr, file_gamma); nuclear@1: #endif nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: png_set_gAMA_fixed(png_ptr, info_ptr, igamma); nuclear@1: #endif nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_sBIT_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_size_t truelen; nuclear@1: png_byte buf[4]; nuclear@1: nuclear@1: png_debug(1, "in png_handle_sBIT\n"); nuclear@1: nuclear@1: buf[0] = buf[1] = buf[2] = buf[3] = 0; nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before sBIT"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid sBIT after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (png_ptr->mode & PNG_HAVE_PLTE) nuclear@1: { nuclear@1: /* Should be an error, but we can cope with it */ nuclear@1: png_warning(png_ptr, "Out of place sBIT chunk"); nuclear@1: } nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate sBIT chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: truelen = 3; nuclear@1: else nuclear@1: truelen = (png_size_t)png_ptr->channels; nuclear@1: nuclear@1: if (length != truelen || length > 4) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect sBIT chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, truelen); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) nuclear@1: { nuclear@1: png_ptr->sig_bit.red = buf[0]; nuclear@1: png_ptr->sig_bit.green = buf[1]; nuclear@1: png_ptr->sig_bit.blue = buf[2]; nuclear@1: png_ptr->sig_bit.alpha = buf[3]; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: png_ptr->sig_bit.gray = buf[0]; nuclear@1: png_ptr->sig_bit.red = buf[0]; nuclear@1: png_ptr->sig_bit.green = buf[0]; nuclear@1: png_ptr->sig_bit.blue = buf[0]; nuclear@1: png_ptr->sig_bit.alpha = buf[1]; nuclear@1: } nuclear@1: png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_cHRM_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_byte buf[32]; nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; nuclear@1: #endif nuclear@1: png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, nuclear@1: int_y_green, int_x_blue, int_y_blue; nuclear@1: nuclear@1: png_uint_32 uint_x, uint_y; nuclear@1: nuclear@1: png_debug(1, "in png_handle_cHRM\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before cHRM"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid cHRM after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (png_ptr->mode & PNG_HAVE_PLTE) nuclear@1: /* Should be an error, but we can cope with it */ nuclear@1: png_warning(png_ptr, "Missing PLTE before cHRM"); nuclear@1: nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM) nuclear@1: #if defined(PNG_READ_sRGB_SUPPORTED) nuclear@1: && !(info_ptr->valid & PNG_INFO_sRGB) nuclear@1: #endif nuclear@1: ) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate cHRM chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (length != 32) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect cHRM chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 32); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: uint_x = png_get_uint_32(buf); nuclear@1: uint_y = png_get_uint_32(buf + 4); nuclear@1: if (uint_x > 80000L || uint_y > 80000L || nuclear@1: uint_x + uint_y > 100000L) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid cHRM white point"); nuclear@1: return; nuclear@1: } nuclear@1: int_x_white = (png_fixed_point)uint_x; nuclear@1: int_y_white = (png_fixed_point)uint_y; nuclear@1: nuclear@1: uint_x = png_get_uint_32(buf + 8); nuclear@1: uint_y = png_get_uint_32(buf + 12); nuclear@1: if (uint_x + uint_y > 100000L) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid cHRM red point"); nuclear@1: return; nuclear@1: } nuclear@1: int_x_red = (png_fixed_point)uint_x; nuclear@1: int_y_red = (png_fixed_point)uint_y; nuclear@1: nuclear@1: uint_x = png_get_uint_32(buf + 16); nuclear@1: uint_y = png_get_uint_32(buf + 20); nuclear@1: if (uint_x + uint_y > 100000L) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid cHRM green point"); nuclear@1: return; nuclear@1: } nuclear@1: int_x_green = (png_fixed_point)uint_x; nuclear@1: int_y_green = (png_fixed_point)uint_y; nuclear@1: nuclear@1: uint_x = png_get_uint_32(buf + 24); nuclear@1: uint_y = png_get_uint_32(buf + 28); nuclear@1: if (uint_x + uint_y > 100000L) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid cHRM blue point"); nuclear@1: return; nuclear@1: } nuclear@1: int_x_blue = (png_fixed_point)uint_x; nuclear@1: int_y_blue = (png_fixed_point)uint_y; nuclear@1: nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: white_x = (float)int_x_white / (float)100000.0; nuclear@1: white_y = (float)int_y_white / (float)100000.0; nuclear@1: red_x = (float)int_x_red / (float)100000.0; nuclear@1: red_y = (float)int_y_red / (float)100000.0; nuclear@1: green_x = (float)int_x_green / (float)100000.0; nuclear@1: green_y = (float)int_y_green / (float)100000.0; nuclear@1: blue_x = (float)int_x_blue / (float)100000.0; nuclear@1: blue_y = (float)int_y_blue / (float)100000.0; nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_sRGB_SUPPORTED) nuclear@1: if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB)) nuclear@1: { nuclear@1: if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) || nuclear@1: PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) || nuclear@1: PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) || nuclear@1: PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) || nuclear@1: PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) || nuclear@1: PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) || nuclear@1: PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) || nuclear@1: PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000)) nuclear@1: { nuclear@1: png_warning(png_ptr, nuclear@1: "Ignoring incorrect cHRM value when sRGB is also present"); nuclear@1: #ifndef PNG_NO_CONSOLE_IO nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n", nuclear@1: white_x, white_y, red_x, red_y); nuclear@1: fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n", nuclear@1: green_x, green_y, blue_x, blue_y); nuclear@1: #else nuclear@1: fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n", nuclear@1: int_x_white, int_y_white, int_x_red, int_y_red); nuclear@1: fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n", nuclear@1: int_x_green, int_y_green, int_x_blue, int_y_blue); nuclear@1: #endif nuclear@1: #endif /* PNG_NO_CONSOLE_IO */ nuclear@1: } nuclear@1: return; nuclear@1: } nuclear@1: #endif /* PNG_READ_sRGB_SUPPORTED */ nuclear@1: nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: png_set_cHRM(png_ptr, info_ptr, nuclear@1: white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); nuclear@1: #endif nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: png_set_cHRM_fixed(png_ptr, info_ptr, nuclear@1: int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, nuclear@1: int_y_green, int_x_blue, int_y_blue); nuclear@1: #endif nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_sRGB_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: int intent; nuclear@1: png_byte buf[1]; nuclear@1: nuclear@1: png_debug(1, "in png_handle_sRGB\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before sRGB"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid sRGB after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (png_ptr->mode & PNG_HAVE_PLTE) nuclear@1: /* Should be an error, but we can cope with it */ nuclear@1: png_warning(png_ptr, "Out of place sRGB chunk"); nuclear@1: nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate sRGB chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (length != 1) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect sRGB chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 1); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: intent = buf[0]; nuclear@1: /* check for bad intent */ nuclear@1: if (intent >= PNG_sRGB_INTENT_LAST) nuclear@1: { nuclear@1: png_warning(png_ptr, "Unknown sRGB intent"); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)) nuclear@1: { nuclear@1: png_fixed_point igamma; nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: igamma=info_ptr->int_gamma; nuclear@1: #else nuclear@1: # ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: igamma=(png_fixed_point)(info_ptr->gamma * 100000.); nuclear@1: # endif nuclear@1: #endif nuclear@1: if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) nuclear@1: { nuclear@1: png_warning(png_ptr, nuclear@1: "Ignoring incorrect gAMA value when sRGB is also present"); nuclear@1: #ifndef PNG_NO_CONSOLE_IO nuclear@1: # ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: fprintf(stderr, "incorrect gamma=(%d/100000)\n", nuclear@1: (int)png_ptr->int_gamma); nuclear@1: # else nuclear@1: # ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma); nuclear@1: # endif nuclear@1: # endif nuclear@1: #endif nuclear@1: } nuclear@1: } nuclear@1: #endif /* PNG_READ_gAMA_SUPPORTED */ nuclear@1: nuclear@1: #ifdef PNG_READ_cHRM_SUPPORTED nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) nuclear@1: if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) || nuclear@1: PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) || nuclear@1: PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) || nuclear@1: PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) || nuclear@1: PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) || nuclear@1: PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) || nuclear@1: PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) || nuclear@1: PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000)) nuclear@1: { nuclear@1: png_warning(png_ptr, nuclear@1: "Ignoring incorrect cHRM value when sRGB is also present"); nuclear@1: } nuclear@1: #endif /* PNG_FIXED_POINT_SUPPORTED */ nuclear@1: #endif /* PNG_READ_cHRM_SUPPORTED */ nuclear@1: nuclear@1: png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); nuclear@1: } nuclear@1: #endif /* PNG_READ_sRGB_SUPPORTED */ nuclear@1: nuclear@1: #if defined(PNG_READ_iCCP_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: /* Note: this does not properly handle chunks that are > 64K under DOS */ nuclear@1: { nuclear@1: png_byte compression_type; nuclear@1: png_bytep pC; nuclear@1: png_charp profile; nuclear@1: png_uint_32 skip = 0; nuclear@1: png_uint_32 profile_size, profile_length; nuclear@1: png_size_t slength, prefix_length, data_length; nuclear@1: nuclear@1: png_debug(1, "in png_handle_iCCP\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before iCCP"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid iCCP after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (png_ptr->mode & PNG_HAVE_PLTE) nuclear@1: /* Should be an error, but we can cope with it */ nuclear@1: png_warning(png_ptr, "Out of place iCCP chunk"); nuclear@1: nuclear@1: if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate iCCP chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: if (length > (png_uint_32)65535L) nuclear@1: { nuclear@1: png_warning(png_ptr, "iCCP chunk too large to fit in memory"); nuclear@1: skip = length - (png_uint_32)65535L; nuclear@1: length = (png_uint_32)65535L; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); nuclear@1: slength = (png_size_t)length; nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); nuclear@1: nuclear@1: if (png_crc_finish(png_ptr, skip)) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_ptr->chunkdata[slength] = 0x00; nuclear@1: nuclear@1: for (profile = png_ptr->chunkdata; *profile; profile++) nuclear@1: /* empty loop to find end of name */ ; nuclear@1: nuclear@1: ++profile; nuclear@1: nuclear@1: /* there should be at least one zero (the compression type byte) nuclear@1: following the separator, and we should be on it */ nuclear@1: if ( profile >= png_ptr->chunkdata + slength - 1) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_warning(png_ptr, "Malformed iCCP chunk"); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: /* compression_type should always be zero */ nuclear@1: compression_type = *profile++; nuclear@1: if (compression_type) nuclear@1: { nuclear@1: png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk"); nuclear@1: compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8 nuclear@1: wrote nonzero) */ nuclear@1: } nuclear@1: nuclear@1: prefix_length = profile - png_ptr->chunkdata; nuclear@1: png_decompress_chunk(png_ptr, compression_type, nuclear@1: slength, prefix_length, &data_length); nuclear@1: nuclear@1: profile_length = data_length - prefix_length; nuclear@1: nuclear@1: if ( prefix_length > data_length || profile_length < 4) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_warning(png_ptr, "Profile size field missing from iCCP chunk"); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: /* Check the profile_size recorded in the first 32 bits of the ICC profile */ nuclear@1: pC = (png_bytep)(png_ptr->chunkdata + prefix_length); nuclear@1: profile_size = ((*(pC ))<<24) | nuclear@1: ((*(pC + 1))<<16) | nuclear@1: ((*(pC + 2))<< 8) | nuclear@1: ((*(pC + 3)) ); nuclear@1: nuclear@1: if (profile_size < profile_length) nuclear@1: profile_length = profile_size; nuclear@1: nuclear@1: if (profile_size > profile_length) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_warning(png_ptr, "Ignoring truncated iCCP profile."); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata, nuclear@1: compression_type, png_ptr->chunkdata + prefix_length, profile_length); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: } nuclear@1: #endif /* PNG_READ_iCCP_SUPPORTED */ nuclear@1: nuclear@1: #if defined(PNG_READ_sPLT_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: /* Note: this does not properly handle chunks that are > 64K under DOS */ nuclear@1: { nuclear@1: png_bytep entry_start; nuclear@1: png_sPLT_t new_palette; nuclear@1: #ifdef PNG_NO_POINTER_INDEXING nuclear@1: png_sPLT_entryp pp; nuclear@1: #endif nuclear@1: int data_length, entry_size, i; nuclear@1: png_uint_32 skip = 0; nuclear@1: png_size_t slength; nuclear@1: nuclear@1: png_debug(1, "in png_handle_sPLT\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before sPLT"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid sPLT after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: if (length > (png_uint_32)65535L) nuclear@1: { nuclear@1: png_warning(png_ptr, "sPLT chunk too large to fit in memory"); nuclear@1: skip = length - (png_uint_32)65535L; nuclear@1: length = (png_uint_32)65535L; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); nuclear@1: slength = (png_size_t)length; nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); nuclear@1: nuclear@1: if (png_crc_finish(png_ptr, skip)) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_ptr->chunkdata[slength] = 0x00; nuclear@1: nuclear@1: for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++) nuclear@1: /* empty loop to find end of name */ ; nuclear@1: ++entry_start; nuclear@1: nuclear@1: /* a sample depth should follow the separator, and we should be on it */ nuclear@1: if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_warning(png_ptr, "malformed sPLT chunk"); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: new_palette.depth = *entry_start++; nuclear@1: entry_size = (new_palette.depth == 8 ? 6 : 10); nuclear@1: data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata)); nuclear@1: nuclear@1: /* integrity-check the data length */ nuclear@1: if (data_length % entry_size) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_warning(png_ptr, "sPLT chunk has bad length"); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: new_palette.nentries = (png_int_32) ( data_length / entry_size); nuclear@1: if ((png_uint_32) new_palette.nentries > nuclear@1: (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry))) nuclear@1: { nuclear@1: png_warning(png_ptr, "sPLT chunk too long"); nuclear@1: return; nuclear@1: } nuclear@1: new_palette.entries = (png_sPLT_entryp)png_malloc_warn( nuclear@1: png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); nuclear@1: if (new_palette.entries == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "sPLT chunk requires too much memory"); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: #ifndef PNG_NO_POINTER_INDEXING nuclear@1: for (i = 0; i < new_palette.nentries; i++) nuclear@1: { nuclear@1: png_sPLT_entryp pp = new_palette.entries + i; nuclear@1: nuclear@1: if (new_palette.depth == 8) nuclear@1: { nuclear@1: pp->red = *entry_start++; nuclear@1: pp->green = *entry_start++; nuclear@1: pp->blue = *entry_start++; nuclear@1: pp->alpha = *entry_start++; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: pp->red = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: pp->green = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: pp->blue = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: pp->alpha = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: } nuclear@1: pp->frequency = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: } nuclear@1: #else nuclear@1: pp = new_palette.entries; nuclear@1: for (i = 0; i < new_palette.nentries; i++) nuclear@1: { nuclear@1: nuclear@1: if (new_palette.depth == 8) nuclear@1: { nuclear@1: pp[i].red = *entry_start++; nuclear@1: pp[i].green = *entry_start++; nuclear@1: pp[i].blue = *entry_start++; nuclear@1: pp[i].alpha = *entry_start++; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: pp[i].red = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: pp[i].green = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: } nuclear@1: pp->frequency = png_get_uint_16(entry_start); entry_start += 2; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: /* discard all chunk data except the name and stash that */ nuclear@1: new_palette.name = png_ptr->chunkdata; nuclear@1: nuclear@1: png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_free(png_ptr, new_palette.entries); nuclear@1: } nuclear@1: #endif /* PNG_READ_sPLT_SUPPORTED */ nuclear@1: nuclear@1: #if defined(PNG_READ_tRNS_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; nuclear@1: nuclear@1: png_debug(1, "in png_handle_tRNS\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before tRNS"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid tRNS after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate tRNS chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) nuclear@1: { nuclear@1: png_byte buf[2]; nuclear@1: nuclear@1: if (length != 2) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect tRNS chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 2); nuclear@1: png_ptr->num_trans = 1; nuclear@1: png_ptr->trans_values.gray = png_get_uint_16(buf); nuclear@1: } nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) nuclear@1: { nuclear@1: png_byte buf[6]; nuclear@1: nuclear@1: if (length != 6) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect tRNS chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: png_crc_read(png_ptr, buf, (png_size_t)length); nuclear@1: png_ptr->num_trans = 1; nuclear@1: png_ptr->trans_values.red = png_get_uint_16(buf); nuclear@1: png_ptr->trans_values.green = png_get_uint_16(buf + 2); nuclear@1: png_ptr->trans_values.blue = png_get_uint_16(buf + 4); nuclear@1: } nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: { nuclear@1: if (!(png_ptr->mode & PNG_HAVE_PLTE)) nuclear@1: { nuclear@1: /* Should be an error, but we can cope with it. */ nuclear@1: png_warning(png_ptr, "Missing PLTE before tRNS"); nuclear@1: } nuclear@1: if (length > (png_uint_32)png_ptr->num_palette || nuclear@1: length > PNG_MAX_PALETTE_LENGTH) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect tRNS chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: if (length == 0) nuclear@1: { nuclear@1: png_warning(png_ptr, "Zero length tRNS chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: png_crc_read(png_ptr, readbuf, (png_size_t)length); nuclear@1: png_ptr->num_trans = (png_uint_16)length; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: { nuclear@1: png_ptr->num_trans = 0; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, nuclear@1: &(png_ptr->trans_values)); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_bKGD_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_size_t truelen; nuclear@1: png_byte buf[6]; nuclear@1: nuclear@1: png_debug(1, "in png_handle_bKGD\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before bKGD"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid bKGD after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && nuclear@1: !(png_ptr->mode & PNG_HAVE_PLTE)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Missing PLTE before bKGD"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate bKGD chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: truelen = 1; nuclear@1: else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) nuclear@1: truelen = 6; nuclear@1: else nuclear@1: truelen = 2; nuclear@1: nuclear@1: if (length != truelen) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect bKGD chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, truelen); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: /* We convert the index value into RGB components so that we can allow nuclear@1: * arbitrary RGB values for background when we have transparency, and nuclear@1: * so it is easy to determine the RGB values of the background color nuclear@1: * from the info_ptr struct. */ nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: { nuclear@1: png_ptr->background.index = buf[0]; nuclear@1: if (info_ptr && info_ptr->num_palette) nuclear@1: { nuclear@1: if (buf[0] > info_ptr->num_palette) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect bKGD chunk index value"); nuclear@1: return; nuclear@1: } nuclear@1: png_ptr->background.red = nuclear@1: (png_uint_16)png_ptr->palette[buf[0]].red; nuclear@1: png_ptr->background.green = nuclear@1: (png_uint_16)png_ptr->palette[buf[0]].green; nuclear@1: png_ptr->background.blue = nuclear@1: (png_uint_16)png_ptr->palette[buf[0]].blue; nuclear@1: } nuclear@1: } nuclear@1: else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ nuclear@1: { nuclear@1: png_ptr->background.red = nuclear@1: png_ptr->background.green = nuclear@1: png_ptr->background.blue = nuclear@1: png_ptr->background.gray = png_get_uint_16(buf); nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: png_ptr->background.red = png_get_uint_16(buf); nuclear@1: png_ptr->background.green = png_get_uint_16(buf + 2); nuclear@1: png_ptr->background.blue = png_get_uint_16(buf + 4); nuclear@1: } nuclear@1: nuclear@1: png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background)); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_hIST_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: unsigned int num, i; nuclear@1: png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; nuclear@1: nuclear@1: png_debug(1, "in png_handle_hIST\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before hIST"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid hIST after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (!(png_ptr->mode & PNG_HAVE_PLTE)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Missing PLTE before hIST"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate hIST chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: num = length / 2 ; nuclear@1: if (num != (unsigned int) png_ptr->num_palette || num > nuclear@1: (unsigned int) PNG_MAX_PALETTE_LENGTH) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect hIST chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: for (i = 0; i < num; i++) nuclear@1: { nuclear@1: png_byte buf[2]; nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 2); nuclear@1: readbuf[i] = png_get_uint_16(buf); nuclear@1: } nuclear@1: nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: png_set_hIST(png_ptr, info_ptr, readbuf); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_pHYs_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_byte buf[9]; nuclear@1: png_uint_32 res_x, res_y; nuclear@1: int unit_type; nuclear@1: nuclear@1: png_debug(1, "in png_handle_pHYs\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before pHYs"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid pHYs after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate pHYs chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (length != 9) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect pHYs chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 9); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: res_x = png_get_uint_32(buf); nuclear@1: res_y = png_get_uint_32(buf + 4); nuclear@1: unit_type = buf[8]; nuclear@1: png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_oFFs_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_byte buf[9]; nuclear@1: png_int_32 offset_x, offset_y; nuclear@1: int unit_type; nuclear@1: nuclear@1: png_debug(1, "in png_handle_oFFs\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before oFFs"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid oFFs after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate oFFs chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (length != 9) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect oFFs chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 9); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: offset_x = png_get_int_32(buf); nuclear@1: offset_y = png_get_int_32(buf + 4); nuclear@1: unit_type = buf[8]; nuclear@1: png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_pCAL_SUPPORTED) nuclear@1: /* read the pCAL chunk (described in the PNG Extensions document) */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_int_32 X0, X1; nuclear@1: png_byte type, nparams; nuclear@1: png_charp buf, units, endptr; nuclear@1: png_charpp params; nuclear@1: png_size_t slength; nuclear@1: int i; nuclear@1: nuclear@1: png_debug(1, "in png_handle_pCAL\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before pCAL"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid pCAL after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate pCAL chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n", nuclear@1: length + 1); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); nuclear@1: if (png_ptr->chunkdata == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "No memory for pCAL purpose."); nuclear@1: return; nuclear@1: } nuclear@1: slength = (png_size_t)length; nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); nuclear@1: nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */ nuclear@1: nuclear@1: png_debug(3, "Finding end of pCAL purpose string\n"); nuclear@1: for (buf = png_ptr->chunkdata; *buf; buf++) nuclear@1: /* empty loop */ ; nuclear@1: nuclear@1: endptr = png_ptr->chunkdata + slength; nuclear@1: nuclear@1: /* We need to have at least 12 bytes after the purpose string nuclear@1: in order to get the parameter information. */ nuclear@1: if (endptr <= buf + 12) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid pCAL data"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n"); nuclear@1: X0 = png_get_int_32((png_bytep)buf+1); nuclear@1: X1 = png_get_int_32((png_bytep)buf+5); nuclear@1: type = buf[9]; nuclear@1: nparams = buf[10]; nuclear@1: units = buf + 11; nuclear@1: nuclear@1: png_debug(3, "Checking pCAL equation type and number of parameters\n"); nuclear@1: /* Check that we have the right number of parameters for known nuclear@1: equation types. */ nuclear@1: if ((type == PNG_EQUATION_LINEAR && nparams != 2) || nuclear@1: (type == PNG_EQUATION_BASE_E && nparams != 3) || nuclear@1: (type == PNG_EQUATION_ARBITRARY && nparams != 3) || nuclear@1: (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid pCAL parameters for equation type"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: else if (type >= PNG_EQUATION_LAST) nuclear@1: { nuclear@1: png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); nuclear@1: } nuclear@1: nuclear@1: for (buf = units; *buf; buf++) nuclear@1: /* Empty loop to move past the units string. */ ; nuclear@1: nuclear@1: png_debug(3, "Allocating pCAL parameters array\n"); nuclear@1: params = (png_charpp)png_malloc_warn(png_ptr, nuclear@1: (png_uint_32)(nparams * png_sizeof(png_charp))) ; nuclear@1: if (params == NULL) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_warning(png_ptr, "No memory for pCAL params."); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: /* Get pointers to the start of each parameter string. */ nuclear@1: for (i = 0; i < (int)nparams; i++) nuclear@1: { nuclear@1: buf++; /* Skip the null string terminator from previous parameter. */ nuclear@1: nuclear@1: png_debug1(3, "Reading pCAL parameter %d\n", i); nuclear@1: for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++) nuclear@1: /* Empty loop to move past each parameter string */ ; nuclear@1: nuclear@1: /* Make sure we haven't run out of data yet */ nuclear@1: if (buf > endptr) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid pCAL data"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_free(png_ptr, params); nuclear@1: return; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams, nuclear@1: units, params); nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_free(png_ptr, params); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_sCAL_SUPPORTED) nuclear@1: /* read the sCAL chunk */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_charp ep; nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: double width, height; nuclear@1: png_charp vp; nuclear@1: #else nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: png_charp swidth, sheight; nuclear@1: #endif nuclear@1: #endif nuclear@1: png_size_t slength; nuclear@1: nuclear@1: png_debug(1, "in png_handle_sCAL\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before sCAL"); nuclear@1: else if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid sCAL after IDAT"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate sCAL chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n", nuclear@1: length + 1); nuclear@1: png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); nuclear@1: if (png_ptr->chunkdata == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "Out of memory while processing sCAL chunk"); nuclear@1: return; nuclear@1: } nuclear@1: slength = (png_size_t)length; nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); nuclear@1: nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */ nuclear@1: nuclear@1: ep = png_ptr->chunkdata + 1; /* skip unit byte */ nuclear@1: nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: width = png_strtod(png_ptr, ep, &vp); nuclear@1: if (*vp) nuclear@1: { nuclear@1: png_warning(png_ptr, "malformed width string in sCAL chunk"); nuclear@1: return; nuclear@1: } nuclear@1: #else nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); nuclear@1: if (swidth == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "Out of memory while processing sCAL chunk width"); nuclear@1: return; nuclear@1: } nuclear@1: png_memcpy(swidth, ep, (png_size_t)png_strlen(ep)); nuclear@1: #endif nuclear@1: #endif nuclear@1: nuclear@1: for (ep = png_ptr->chunkdata; *ep; ep++) nuclear@1: /* empty loop */ ; nuclear@1: ep++; nuclear@1: nuclear@1: if (png_ptr->chunkdata + slength < ep) nuclear@1: { nuclear@1: png_warning(png_ptr, "Truncated sCAL chunk"); nuclear@1: #if defined(PNG_FIXED_POINT_SUPPORTED) && \ nuclear@1: !defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@1: png_free(png_ptr, swidth); nuclear@1: #endif nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: height = png_strtod(png_ptr, ep, &vp); nuclear@1: if (*vp) nuclear@1: { nuclear@1: png_warning(png_ptr, "malformed height string in sCAL chunk"); nuclear@1: return; nuclear@1: } nuclear@1: #else nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); nuclear@1: if (sheight == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "Out of memory while processing sCAL chunk height"); nuclear@1: return; nuclear@1: } nuclear@1: png_memcpy(sheight, ep, (png_size_t)png_strlen(ep)); nuclear@1: #endif nuclear@1: #endif nuclear@1: nuclear@1: if (png_ptr->chunkdata + slength < ep nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: || width <= 0. || height <= 0. nuclear@1: #endif nuclear@1: ) nuclear@1: { nuclear@1: png_warning(png_ptr, "Invalid sCAL data"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@1: png_free(png_ptr, swidth); nuclear@1: png_free(png_ptr, sheight); nuclear@1: #endif nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@1: png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height); nuclear@1: #else nuclear@1: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@1: png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight); nuclear@1: #endif nuclear@1: #endif nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@1: png_free(png_ptr, swidth); nuclear@1: png_free(png_ptr, sheight); nuclear@1: #endif nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_tIME_SUPPORTED) nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_byte buf[7]; nuclear@1: png_time mod_time; nuclear@1: nuclear@1: png_debug(1, "in png_handle_tIME\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Out of place tIME chunk"); nuclear@1: else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Duplicate tIME chunk"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: nuclear@1: if (length != 7) nuclear@1: { nuclear@1: png_warning(png_ptr, "Incorrect tIME chunk length"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_crc_read(png_ptr, buf, 7); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: return; nuclear@1: nuclear@1: mod_time.second = buf[6]; nuclear@1: mod_time.minute = buf[5]; nuclear@1: mod_time.hour = buf[4]; nuclear@1: mod_time.day = buf[3]; nuclear@1: mod_time.month = buf[2]; nuclear@1: mod_time.year = png_get_uint_16(buf); nuclear@1: nuclear@1: png_set_tIME(png_ptr, info_ptr, &mod_time); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_tEXt_SUPPORTED) nuclear@1: /* Note: this does not properly handle chunks that are > 64K under DOS */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_textp text_ptr; nuclear@1: png_charp key; nuclear@1: png_charp text; nuclear@1: png_uint_32 skip = 0; nuclear@1: png_size_t slength; nuclear@1: int ret; nuclear@1: nuclear@1: png_debug(1, "in png_handle_tEXt\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before tEXt"); nuclear@1: nuclear@1: if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: if (length > (png_uint_32)65535L) nuclear@1: { nuclear@1: png_warning(png_ptr, "tEXt chunk too large to fit in memory"); nuclear@1: skip = length - (png_uint_32)65535L; nuclear@1: length = (png_uint_32)65535L; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); nuclear@1: if (png_ptr->chunkdata == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "No memory to process text chunk."); nuclear@1: return; nuclear@1: } nuclear@1: slength = (png_size_t)length; nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); nuclear@1: nuclear@1: if (png_crc_finish(png_ptr, skip)) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: key = png_ptr->chunkdata; nuclear@1: key[slength] = 0x00; nuclear@1: nuclear@1: for (text = key; *text; text++) nuclear@1: /* empty loop to find end of key */ ; nuclear@1: nuclear@1: if (text != key + slength) nuclear@1: text++; nuclear@1: nuclear@1: text_ptr = (png_textp)png_malloc_warn(png_ptr, nuclear@1: (png_uint_32)png_sizeof(png_text)); nuclear@1: if (text_ptr == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "Not enough memory to process text chunk."); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; nuclear@1: text_ptr->key = key; nuclear@1: #ifdef PNG_iTXt_SUPPORTED nuclear@1: text_ptr->lang = NULL; nuclear@1: text_ptr->lang_key = NULL; nuclear@1: text_ptr->itxt_length = 0; nuclear@1: #endif nuclear@1: text_ptr->text = text; nuclear@1: text_ptr->text_length = png_strlen(text); nuclear@1: nuclear@1: ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: png_free(png_ptr, text_ptr); nuclear@1: if (ret) nuclear@1: png_warning(png_ptr, "Insufficient memory to process text chunk."); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_zTXt_SUPPORTED) nuclear@1: /* note: this does not correctly handle chunks that are > 64K under DOS */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_textp text_ptr; nuclear@1: png_charp text; nuclear@1: int comp_type; nuclear@1: int ret; nuclear@1: png_size_t slength, prefix_len, data_len; nuclear@1: nuclear@1: png_debug(1, "in png_handle_zTXt\n"); nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before zTXt"); nuclear@1: nuclear@1: if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: /* We will no doubt have problems with chunks even half this size, but nuclear@1: there is no hard and fast rule to tell us where to stop. */ nuclear@1: if (length > (png_uint_32)65535L) nuclear@1: { nuclear@1: png_warning(png_ptr, "zTXt chunk too large to fit in memory"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); nuclear@1: if (png_ptr->chunkdata == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "Out of memory processing zTXt chunk."); nuclear@1: return; nuclear@1: } nuclear@1: slength = (png_size_t)length; nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_ptr->chunkdata[slength] = 0x00; nuclear@1: nuclear@1: for (text = png_ptr->chunkdata; *text; text++) nuclear@1: /* empty loop */ ; nuclear@1: nuclear@1: /* zTXt must have some text after the chunkdataword */ nuclear@1: if (text >= png_ptr->chunkdata + slength - 2) nuclear@1: { nuclear@1: png_warning(png_ptr, "Truncated zTXt chunk"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: comp_type = *(++text); nuclear@1: if (comp_type != PNG_TEXT_COMPRESSION_zTXt) nuclear@1: { nuclear@1: png_warning(png_ptr, "Unknown compression type in zTXt chunk"); nuclear@1: comp_type = PNG_TEXT_COMPRESSION_zTXt; nuclear@1: } nuclear@1: text++; /* skip the compression_method byte */ nuclear@1: } nuclear@1: prefix_len = text - png_ptr->chunkdata; nuclear@1: nuclear@1: png_decompress_chunk(png_ptr, comp_type, nuclear@1: (png_size_t)length, prefix_len, &data_len); nuclear@1: nuclear@1: text_ptr = (png_textp)png_malloc_warn(png_ptr, nuclear@1: (png_uint_32)png_sizeof(png_text)); nuclear@1: if (text_ptr == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "Not enough memory to process zTXt chunk."); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: text_ptr->compression = comp_type; nuclear@1: text_ptr->key = png_ptr->chunkdata; nuclear@1: #ifdef PNG_iTXt_SUPPORTED nuclear@1: text_ptr->lang = NULL; nuclear@1: text_ptr->lang_key = NULL; nuclear@1: text_ptr->itxt_length = 0; nuclear@1: #endif nuclear@1: text_ptr->text = png_ptr->chunkdata + prefix_len; nuclear@1: text_ptr->text_length = data_len; nuclear@1: nuclear@1: ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); nuclear@1: nuclear@1: png_free(png_ptr, text_ptr); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: if (ret) nuclear@1: png_error(png_ptr, "Insufficient memory to store zTXt chunk."); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_iTXt_SUPPORTED) nuclear@1: /* note: this does not correctly handle chunks that are > 64K under DOS */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_textp text_ptr; nuclear@1: png_charp key, lang, text, lang_key; nuclear@1: int comp_flag; nuclear@1: int comp_type = 0; nuclear@1: int ret; nuclear@1: png_size_t slength, prefix_len, data_len; nuclear@1: nuclear@1: png_debug(1, "in png_handle_iTXt\n"); nuclear@1: nuclear@1: if (!(png_ptr->mode & PNG_HAVE_IHDR)) nuclear@1: png_error(png_ptr, "Missing IHDR before iTXt"); nuclear@1: nuclear@1: if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: /* We will no doubt have problems with chunks even half this size, but nuclear@1: there is no hard and fast rule to tell us where to stop. */ nuclear@1: if (length > (png_uint_32)65535L) nuclear@1: { nuclear@1: png_warning(png_ptr, "iTXt chunk too large to fit in memory"); nuclear@1: png_crc_finish(png_ptr, length); nuclear@1: return; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); nuclear@1: if (png_ptr->chunkdata == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "No memory to process iTXt chunk."); nuclear@1: return; nuclear@1: } nuclear@1: slength = (png_size_t)length; nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); nuclear@1: if (png_crc_finish(png_ptr, 0)) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: png_ptr->chunkdata[slength] = 0x00; nuclear@1: nuclear@1: for (lang = png_ptr->chunkdata; *lang; lang++) nuclear@1: /* empty loop */ ; nuclear@1: lang++; /* skip NUL separator */ nuclear@1: nuclear@1: /* iTXt must have a language tag (possibly empty), two compression bytes, nuclear@1: translated keyword (possibly empty), and possibly some text after the nuclear@1: keyword */ nuclear@1: nuclear@1: if (lang >= png_ptr->chunkdata + slength - 3) nuclear@1: { nuclear@1: png_warning(png_ptr, "Truncated iTXt chunk"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: comp_flag = *lang++; nuclear@1: comp_type = *lang++; nuclear@1: } nuclear@1: nuclear@1: for (lang_key = lang; *lang_key; lang_key++) nuclear@1: /* empty loop */ ; nuclear@1: lang_key++; /* skip NUL separator */ nuclear@1: nuclear@1: if (lang_key >= png_ptr->chunkdata + slength) nuclear@1: { nuclear@1: png_warning(png_ptr, "Truncated iTXt chunk"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: for (text = lang_key; *text; text++) nuclear@1: /* empty loop */ ; nuclear@1: text++; /* skip NUL separator */ nuclear@1: if (text >= png_ptr->chunkdata + slength) nuclear@1: { nuclear@1: png_warning(png_ptr, "Malformed iTXt chunk"); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: prefix_len = text - png_ptr->chunkdata; nuclear@1: nuclear@1: key=png_ptr->chunkdata; nuclear@1: if (comp_flag) nuclear@1: png_decompress_chunk(png_ptr, comp_type, nuclear@1: (size_t)length, prefix_len, &data_len); nuclear@1: else nuclear@1: data_len = png_strlen(png_ptr->chunkdata + prefix_len); nuclear@1: text_ptr = (png_textp)png_malloc_warn(png_ptr, nuclear@1: (png_uint_32)png_sizeof(png_text)); nuclear@1: if (text_ptr == NULL) nuclear@1: { nuclear@1: png_warning(png_ptr, "Not enough memory to process iTXt chunk."); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: return; nuclear@1: } nuclear@1: text_ptr->compression = (int)comp_flag + 1; nuclear@1: text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key); nuclear@1: text_ptr->lang = png_ptr->chunkdata + (lang - key); nuclear@1: text_ptr->itxt_length = data_len; nuclear@1: text_ptr->text_length = 0; nuclear@1: text_ptr->key = png_ptr->chunkdata; nuclear@1: text_ptr->text = png_ptr->chunkdata + prefix_len; nuclear@1: nuclear@1: ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); nuclear@1: nuclear@1: png_free(png_ptr, text_ptr); nuclear@1: png_free(png_ptr, png_ptr->chunkdata); nuclear@1: png_ptr->chunkdata = NULL; nuclear@1: if (ret) nuclear@1: png_error(png_ptr, "Insufficient memory to store iTXt chunk."); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: /* This function is called when we haven't found a handler for a nuclear@1: chunk. If there isn't a problem with the chunk itself (ie bad nuclear@1: chunk name, CRC, or a critical chunk), the chunk is silently ignored nuclear@1: -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which nuclear@1: case it will be saved away to be written out later. */ nuclear@1: void /* PRIVATE */ nuclear@1: png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) nuclear@1: { nuclear@1: png_uint_32 skip = 0; nuclear@1: nuclear@1: png_debug(1, "in png_handle_unknown\n"); nuclear@1: nuclear@1: if (png_ptr->mode & PNG_HAVE_IDAT) nuclear@1: { nuclear@1: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@1: PNG_CONST PNG_IDAT; nuclear@1: #endif nuclear@1: if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */ nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: } nuclear@1: nuclear@1: if (!(png_ptr->chunk_name[0] & 0x20)) nuclear@1: { nuclear@1: #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) nuclear@1: if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != nuclear@1: PNG_HANDLE_CHUNK_ALWAYS nuclear@1: #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) nuclear@1: && png_ptr->read_user_chunk_fn == NULL nuclear@1: #endif nuclear@1: ) nuclear@1: #endif nuclear@1: png_chunk_error(png_ptr, "unknown critical chunk"); nuclear@1: } nuclear@1: nuclear@1: #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) nuclear@1: if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) || nuclear@1: (png_ptr->read_user_chunk_fn != NULL)) nuclear@1: { nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: if (length > (png_uint_32)65535L) nuclear@1: { nuclear@1: png_warning(png_ptr, "unknown chunk too large to fit in memory"); nuclear@1: skip = length - (png_uint_32)65535L; nuclear@1: length = (png_uint_32)65535L; nuclear@1: } nuclear@1: #endif nuclear@1: png_memcpy((png_charp)png_ptr->unknown_chunk.name, nuclear@1: (png_charp)png_ptr->chunk_name, nuclear@1: png_sizeof(png_ptr->unknown_chunk.name)); nuclear@1: png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0'; nuclear@1: png_ptr->unknown_chunk.size = (png_size_t)length; nuclear@1: if (length == 0) nuclear@1: png_ptr->unknown_chunk.data = NULL; nuclear@1: else nuclear@1: { nuclear@1: png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length); nuclear@1: png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length); nuclear@1: } nuclear@1: #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) nuclear@1: if (png_ptr->read_user_chunk_fn != NULL) nuclear@1: { nuclear@1: /* callback to user unknown chunk handler */ nuclear@1: int ret; nuclear@1: ret = (*(png_ptr->read_user_chunk_fn)) nuclear@1: (png_ptr, &png_ptr->unknown_chunk); nuclear@1: if (ret < 0) nuclear@1: png_chunk_error(png_ptr, "error in user chunk"); nuclear@1: if (ret == 0) nuclear@1: { nuclear@1: if (!(png_ptr->chunk_name[0] & 0x20)) nuclear@1: if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != nuclear@1: PNG_HANDLE_CHUNK_ALWAYS) nuclear@1: png_chunk_error(png_ptr, "unknown critical chunk"); nuclear@1: png_set_unknown_chunks(png_ptr, info_ptr, nuclear@1: &png_ptr->unknown_chunk, 1); nuclear@1: } nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); nuclear@1: png_free(png_ptr, png_ptr->unknown_chunk.data); nuclear@1: png_ptr->unknown_chunk.data = NULL; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: skip = length; nuclear@1: nuclear@1: png_crc_finish(png_ptr, skip); nuclear@1: nuclear@1: #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED) nuclear@1: info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */ nuclear@1: #endif nuclear@1: } nuclear@1: nuclear@1: /* This function is called to verify that a chunk name is valid. nuclear@1: This function can't have the "critical chunk check" incorporated nuclear@1: into it, since in the future we will need to be able to call user nuclear@1: functions to handle unknown critical chunks after we check that nuclear@1: the chunk name itself is valid. */ nuclear@1: nuclear@1: #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) nuclear@1: nuclear@1: void /* PRIVATE */ nuclear@1: png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name) nuclear@1: { nuclear@1: png_debug(1, "in png_check_chunk_name\n"); nuclear@1: if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) || nuclear@1: isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3])) nuclear@1: { nuclear@1: png_chunk_error(png_ptr, "invalid chunk type"); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* Combines the row recently read in with the existing pixels in the nuclear@1: row. This routine takes care of alpha and transparency if requested. nuclear@1: This routine also handles the two methods of progressive display nuclear@1: of interlaced images, depending on the mask value. nuclear@1: The mask value describes which pixels are to be combined with nuclear@1: the row. The pattern always repeats every 8 pixels, so just 8 nuclear@1: bits are needed. A one indicates the pixel is to be combined, nuclear@1: a zero indicates the pixel is to be skipped. This is in addition nuclear@1: to any alpha or transparency value associated with the pixel. If nuclear@1: you want all pixels to be combined, pass 0xff (255) in mask. */ nuclear@1: nuclear@1: void /* PRIVATE */ nuclear@1: png_combine_row(png_structp png_ptr, png_bytep row, int mask) nuclear@1: { nuclear@1: png_debug(1, "in png_combine_row\n"); nuclear@1: if (mask == 0xff) nuclear@1: { nuclear@1: png_memcpy(row, png_ptr->row_buf + 1, nuclear@1: PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width)); nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: switch (png_ptr->row_info.pixel_depth) nuclear@1: { nuclear@1: case 1: nuclear@1: { nuclear@1: png_bytep sp = png_ptr->row_buf + 1; nuclear@1: png_bytep dp = row; nuclear@1: int s_inc, s_start, s_end; nuclear@1: int m = 0x80; nuclear@1: int shift; nuclear@1: png_uint_32 i; nuclear@1: png_uint_32 row_width = png_ptr->width; nuclear@1: nuclear@1: #if defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@1: if (png_ptr->transformations & PNG_PACKSWAP) nuclear@1: { nuclear@1: s_start = 0; nuclear@1: s_end = 7; nuclear@1: s_inc = 1; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: { nuclear@1: s_start = 7; nuclear@1: s_end = 0; nuclear@1: s_inc = -1; nuclear@1: } nuclear@1: nuclear@1: shift = s_start; nuclear@1: nuclear@1: for (i = 0; i < row_width; i++) nuclear@1: { nuclear@1: if (m & mask) nuclear@1: { nuclear@1: int value; nuclear@1: nuclear@1: value = (*sp >> shift) & 0x01; nuclear@1: *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); nuclear@1: *dp |= (png_byte)(value << shift); nuclear@1: } nuclear@1: nuclear@1: if (shift == s_end) nuclear@1: { nuclear@1: shift = s_start; nuclear@1: sp++; nuclear@1: dp++; nuclear@1: } nuclear@1: else nuclear@1: shift += s_inc; nuclear@1: nuclear@1: if (m == 1) nuclear@1: m = 0x80; nuclear@1: else nuclear@1: m >>= 1; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: case 2: nuclear@1: { nuclear@1: png_bytep sp = png_ptr->row_buf + 1; nuclear@1: png_bytep dp = row; nuclear@1: int s_start, s_end, s_inc; nuclear@1: int m = 0x80; nuclear@1: int shift; nuclear@1: png_uint_32 i; nuclear@1: png_uint_32 row_width = png_ptr->width; nuclear@1: int value; nuclear@1: nuclear@1: #if defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@1: if (png_ptr->transformations & PNG_PACKSWAP) nuclear@1: { nuclear@1: s_start = 0; nuclear@1: s_end = 6; nuclear@1: s_inc = 2; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: { nuclear@1: s_start = 6; nuclear@1: s_end = 0; nuclear@1: s_inc = -2; nuclear@1: } nuclear@1: nuclear@1: shift = s_start; nuclear@1: nuclear@1: for (i = 0; i < row_width; i++) nuclear@1: { nuclear@1: if (m & mask) nuclear@1: { nuclear@1: value = (*sp >> shift) & 0x03; nuclear@1: *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); nuclear@1: *dp |= (png_byte)(value << shift); nuclear@1: } nuclear@1: nuclear@1: if (shift == s_end) nuclear@1: { nuclear@1: shift = s_start; nuclear@1: sp++; nuclear@1: dp++; nuclear@1: } nuclear@1: else nuclear@1: shift += s_inc; nuclear@1: if (m == 1) nuclear@1: m = 0x80; nuclear@1: else nuclear@1: m >>= 1; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: case 4: nuclear@1: { nuclear@1: png_bytep sp = png_ptr->row_buf + 1; nuclear@1: png_bytep dp = row; nuclear@1: int s_start, s_end, s_inc; nuclear@1: int m = 0x80; nuclear@1: int shift; nuclear@1: png_uint_32 i; nuclear@1: png_uint_32 row_width = png_ptr->width; nuclear@1: int value; nuclear@1: nuclear@1: #if defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@1: if (png_ptr->transformations & PNG_PACKSWAP) nuclear@1: { nuclear@1: s_start = 0; nuclear@1: s_end = 4; nuclear@1: s_inc = 4; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: { nuclear@1: s_start = 4; nuclear@1: s_end = 0; nuclear@1: s_inc = -4; nuclear@1: } nuclear@1: shift = s_start; nuclear@1: nuclear@1: for (i = 0; i < row_width; i++) nuclear@1: { nuclear@1: if (m & mask) nuclear@1: { nuclear@1: value = (*sp >> shift) & 0xf; nuclear@1: *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); nuclear@1: *dp |= (png_byte)(value << shift); nuclear@1: } nuclear@1: nuclear@1: if (shift == s_end) nuclear@1: { nuclear@1: shift = s_start; nuclear@1: sp++; nuclear@1: dp++; nuclear@1: } nuclear@1: else nuclear@1: shift += s_inc; nuclear@1: if (m == 1) nuclear@1: m = 0x80; nuclear@1: else nuclear@1: m >>= 1; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: default: nuclear@1: { nuclear@1: png_bytep sp = png_ptr->row_buf + 1; nuclear@1: png_bytep dp = row; nuclear@1: png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); nuclear@1: png_uint_32 i; nuclear@1: png_uint_32 row_width = png_ptr->width; nuclear@1: png_byte m = 0x80; nuclear@1: nuclear@1: nuclear@1: for (i = 0; i < row_width; i++) nuclear@1: { nuclear@1: if (m & mask) nuclear@1: { nuclear@1: png_memcpy(dp, sp, pixel_bytes); nuclear@1: } nuclear@1: nuclear@1: sp += pixel_bytes; nuclear@1: dp += pixel_bytes; nuclear@1: nuclear@1: if (m == 1) nuclear@1: m = 0x80; nuclear@1: else nuclear@1: m >>= 1; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: #ifdef PNG_READ_INTERLACING_SUPPORTED nuclear@1: /* OLD pre-1.0.9 interface: nuclear@1: void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, nuclear@1: png_uint_32 transformations) nuclear@1: */ nuclear@1: void /* PRIVATE */ nuclear@1: png_do_read_interlace(png_structp png_ptr) nuclear@1: { nuclear@1: png_row_infop row_info = &(png_ptr->row_info); nuclear@1: png_bytep row = png_ptr->row_buf + 1; nuclear@1: int pass = png_ptr->pass; nuclear@1: png_uint_32 transformations = png_ptr->transformations; nuclear@1: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@1: /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ nuclear@1: /* offset to next interlace block */ nuclear@1: PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; nuclear@1: #endif nuclear@1: nuclear@1: png_debug(1, "in png_do_read_interlace\n"); nuclear@1: if (row != NULL && row_info != NULL) nuclear@1: { nuclear@1: png_uint_32 final_width; nuclear@1: nuclear@1: final_width = row_info->width * png_pass_inc[pass]; nuclear@1: nuclear@1: switch (row_info->pixel_depth) nuclear@1: { nuclear@1: case 1: nuclear@1: { nuclear@1: png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); nuclear@1: png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); nuclear@1: int sshift, dshift; nuclear@1: int s_start, s_end, s_inc; nuclear@1: int jstop = png_pass_inc[pass]; nuclear@1: png_byte v; nuclear@1: png_uint_32 i; nuclear@1: int j; nuclear@1: nuclear@1: #if defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@1: if (transformations & PNG_PACKSWAP) nuclear@1: { nuclear@1: sshift = (int)((row_info->width + 7) & 0x07); nuclear@1: dshift = (int)((final_width + 7) & 0x07); nuclear@1: s_start = 7; nuclear@1: s_end = 0; nuclear@1: s_inc = -1; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: { nuclear@1: sshift = 7 - (int)((row_info->width + 7) & 0x07); nuclear@1: dshift = 7 - (int)((final_width + 7) & 0x07); nuclear@1: s_start = 0; nuclear@1: s_end = 7; nuclear@1: s_inc = 1; nuclear@1: } nuclear@1: nuclear@1: for (i = 0; i < row_info->width; i++) nuclear@1: { nuclear@1: v = (png_byte)((*sp >> sshift) & 0x01); nuclear@1: for (j = 0; j < jstop; j++) nuclear@1: { nuclear@1: *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); nuclear@1: *dp |= (png_byte)(v << dshift); nuclear@1: if (dshift == s_end) nuclear@1: { nuclear@1: dshift = s_start; nuclear@1: dp--; nuclear@1: } nuclear@1: else nuclear@1: dshift += s_inc; nuclear@1: } nuclear@1: if (sshift == s_end) nuclear@1: { nuclear@1: sshift = s_start; nuclear@1: sp--; nuclear@1: } nuclear@1: else nuclear@1: sshift += s_inc; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: case 2: nuclear@1: { nuclear@1: png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); nuclear@1: png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); nuclear@1: int sshift, dshift; nuclear@1: int s_start, s_end, s_inc; nuclear@1: int jstop = png_pass_inc[pass]; nuclear@1: png_uint_32 i; nuclear@1: nuclear@1: #if defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@1: if (transformations & PNG_PACKSWAP) nuclear@1: { nuclear@1: sshift = (int)(((row_info->width + 3) & 0x03) << 1); nuclear@1: dshift = (int)(((final_width + 3) & 0x03) << 1); nuclear@1: s_start = 6; nuclear@1: s_end = 0; nuclear@1: s_inc = -2; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: { nuclear@1: sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); nuclear@1: dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); nuclear@1: s_start = 0; nuclear@1: s_end = 6; nuclear@1: s_inc = 2; nuclear@1: } nuclear@1: nuclear@1: for (i = 0; i < row_info->width; i++) nuclear@1: { nuclear@1: png_byte v; nuclear@1: int j; nuclear@1: nuclear@1: v = (png_byte)((*sp >> sshift) & 0x03); nuclear@1: for (j = 0; j < jstop; j++) nuclear@1: { nuclear@1: *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); nuclear@1: *dp |= (png_byte)(v << dshift); nuclear@1: if (dshift == s_end) nuclear@1: { nuclear@1: dshift = s_start; nuclear@1: dp--; nuclear@1: } nuclear@1: else nuclear@1: dshift += s_inc; nuclear@1: } nuclear@1: if (sshift == s_end) nuclear@1: { nuclear@1: sshift = s_start; nuclear@1: sp--; nuclear@1: } nuclear@1: else nuclear@1: sshift += s_inc; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: case 4: nuclear@1: { nuclear@1: png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); nuclear@1: png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); nuclear@1: int sshift, dshift; nuclear@1: int s_start, s_end, s_inc; nuclear@1: png_uint_32 i; nuclear@1: int jstop = png_pass_inc[pass]; nuclear@1: nuclear@1: #if defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@1: if (transformations & PNG_PACKSWAP) nuclear@1: { nuclear@1: sshift = (int)(((row_info->width + 1) & 0x01) << 2); nuclear@1: dshift = (int)(((final_width + 1) & 0x01) << 2); nuclear@1: s_start = 4; nuclear@1: s_end = 0; nuclear@1: s_inc = -4; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: { nuclear@1: sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); nuclear@1: dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); nuclear@1: s_start = 0; nuclear@1: s_end = 4; nuclear@1: s_inc = 4; nuclear@1: } nuclear@1: nuclear@1: for (i = 0; i < row_info->width; i++) nuclear@1: { nuclear@1: png_byte v = (png_byte)((*sp >> sshift) & 0xf); nuclear@1: int j; nuclear@1: nuclear@1: for (j = 0; j < jstop; j++) nuclear@1: { nuclear@1: *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); nuclear@1: *dp |= (png_byte)(v << dshift); nuclear@1: if (dshift == s_end) nuclear@1: { nuclear@1: dshift = s_start; nuclear@1: dp--; nuclear@1: } nuclear@1: else nuclear@1: dshift += s_inc; nuclear@1: } nuclear@1: if (sshift == s_end) nuclear@1: { nuclear@1: sshift = s_start; nuclear@1: sp--; nuclear@1: } nuclear@1: else nuclear@1: sshift += s_inc; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: default: nuclear@1: { nuclear@1: png_size_t pixel_bytes = (row_info->pixel_depth >> 3); nuclear@1: png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes; nuclear@1: png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; nuclear@1: nuclear@1: int jstop = png_pass_inc[pass]; nuclear@1: png_uint_32 i; nuclear@1: nuclear@1: for (i = 0; i < row_info->width; i++) nuclear@1: { nuclear@1: png_byte v[8]; nuclear@1: int j; nuclear@1: nuclear@1: png_memcpy(v, sp, pixel_bytes); nuclear@1: for (j = 0; j < jstop; j++) nuclear@1: { nuclear@1: png_memcpy(dp, v, pixel_bytes); nuclear@1: dp -= pixel_bytes; nuclear@1: } nuclear@1: sp -= pixel_bytes; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: } nuclear@1: row_info->width = final_width; nuclear@1: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); nuclear@1: } nuclear@1: #if !defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@1: transformations = transformations; /* silence compiler warning */ nuclear@1: #endif nuclear@1: } nuclear@1: #endif /* PNG_READ_INTERLACING_SUPPORTED */ nuclear@1: nuclear@1: void /* PRIVATE */ nuclear@1: png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row, nuclear@1: png_bytep prev_row, int filter) nuclear@1: { nuclear@1: png_debug(1, "in png_read_filter_row\n"); nuclear@1: png_debug2(2, "row = %lu, filter = %d\n", png_ptr->row_number, filter); nuclear@1: switch (filter) nuclear@1: { nuclear@1: case PNG_FILTER_VALUE_NONE: nuclear@1: break; nuclear@1: case PNG_FILTER_VALUE_SUB: nuclear@1: { nuclear@1: png_uint_32 i; nuclear@1: png_uint_32 istop = row_info->rowbytes; nuclear@1: png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; nuclear@1: png_bytep rp = row + bpp; nuclear@1: png_bytep lp = row; nuclear@1: nuclear@1: for (i = bpp; i < istop; i++) nuclear@1: { nuclear@1: *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff); nuclear@1: rp++; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: case PNG_FILTER_VALUE_UP: nuclear@1: { nuclear@1: png_uint_32 i; nuclear@1: png_uint_32 istop = row_info->rowbytes; nuclear@1: png_bytep rp = row; nuclear@1: png_bytep pp = prev_row; nuclear@1: nuclear@1: for (i = 0; i < istop; i++) nuclear@1: { nuclear@1: *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); nuclear@1: rp++; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: case PNG_FILTER_VALUE_AVG: nuclear@1: { nuclear@1: png_uint_32 i; nuclear@1: png_bytep rp = row; nuclear@1: png_bytep pp = prev_row; nuclear@1: png_bytep lp = row; nuclear@1: png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; nuclear@1: png_uint_32 istop = row_info->rowbytes - bpp; nuclear@1: nuclear@1: for (i = 0; i < bpp; i++) nuclear@1: { nuclear@1: *rp = (png_byte)(((int)(*rp) + nuclear@1: ((int)(*pp++) / 2 )) & 0xff); nuclear@1: rp++; nuclear@1: } nuclear@1: nuclear@1: for (i = 0; i < istop; i++) nuclear@1: { nuclear@1: *rp = (png_byte)(((int)(*rp) + nuclear@1: (int)(*pp++ + *lp++) / 2 ) & 0xff); nuclear@1: rp++; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: case PNG_FILTER_VALUE_PAETH: nuclear@1: { nuclear@1: png_uint_32 i; nuclear@1: png_bytep rp = row; nuclear@1: png_bytep pp = prev_row; nuclear@1: png_bytep lp = row; nuclear@1: png_bytep cp = prev_row; nuclear@1: png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; nuclear@1: png_uint_32 istop=row_info->rowbytes - bpp; nuclear@1: nuclear@1: for (i = 0; i < bpp; i++) nuclear@1: { nuclear@1: *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); nuclear@1: rp++; nuclear@1: } nuclear@1: nuclear@1: for (i = 0; i < istop; i++) /* use leftover rp,pp */ nuclear@1: { nuclear@1: int a, b, c, pa, pb, pc, p; nuclear@1: nuclear@1: a = *lp++; nuclear@1: b = *pp++; nuclear@1: c = *cp++; nuclear@1: nuclear@1: p = b - c; nuclear@1: pc = a - c; nuclear@1: nuclear@1: #ifdef PNG_USE_ABS nuclear@1: pa = abs(p); nuclear@1: pb = abs(pc); nuclear@1: pc = abs(p + pc); nuclear@1: #else nuclear@1: pa = p < 0 ? -p : p; nuclear@1: pb = pc < 0 ? -pc : pc; nuclear@1: pc = (p + pc) < 0 ? -(p + pc) : p + pc; nuclear@1: #endif nuclear@1: nuclear@1: /* nuclear@1: if (pa <= pb && pa <= pc) nuclear@1: p = a; nuclear@1: else if (pb <= pc) nuclear@1: p = b; nuclear@1: else nuclear@1: p = c; nuclear@1: */ nuclear@1: nuclear@1: p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c; nuclear@1: nuclear@1: *rp = (png_byte)(((int)(*rp) + p) & 0xff); nuclear@1: rp++; nuclear@1: } nuclear@1: break; nuclear@1: } nuclear@1: default: nuclear@1: png_warning(png_ptr, "Ignoring bad adaptive filter type"); nuclear@1: *row = 0; nuclear@1: break; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: void /* PRIVATE */ nuclear@1: png_read_finish_row(png_structp png_ptr) nuclear@1: { nuclear@1: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@1: #ifdef PNG_READ_INTERLACING_SUPPORTED nuclear@1: /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ nuclear@1: nuclear@1: /* start of interlace block */ nuclear@1: PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; nuclear@1: nuclear@1: /* offset to next interlace block */ nuclear@1: PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; nuclear@1: nuclear@1: /* start of interlace block in the y direction */ nuclear@1: PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; nuclear@1: nuclear@1: /* offset to next interlace block in the y direction */ nuclear@1: PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; nuclear@1: #endif /* PNG_READ_INTERLACING_SUPPORTED */ nuclear@1: #endif nuclear@1: nuclear@1: png_debug(1, "in png_read_finish_row\n"); nuclear@1: png_ptr->row_number++; nuclear@1: if (png_ptr->row_number < png_ptr->num_rows) nuclear@1: return; nuclear@1: nuclear@1: #ifdef PNG_READ_INTERLACING_SUPPORTED nuclear@1: if (png_ptr->interlaced) nuclear@1: { nuclear@1: png_ptr->row_number = 0; nuclear@1: png_memset_check(png_ptr, png_ptr->prev_row, 0, nuclear@1: png_ptr->rowbytes + 1); nuclear@1: do nuclear@1: { nuclear@1: png_ptr->pass++; nuclear@1: if (png_ptr->pass >= 7) nuclear@1: break; nuclear@1: png_ptr->iwidth = (png_ptr->width + nuclear@1: png_pass_inc[png_ptr->pass] - 1 - nuclear@1: png_pass_start[png_ptr->pass]) / nuclear@1: png_pass_inc[png_ptr->pass]; nuclear@1: nuclear@1: png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, nuclear@1: png_ptr->iwidth) + 1; nuclear@1: nuclear@1: if (!(png_ptr->transformations & PNG_INTERLACE)) nuclear@1: { nuclear@1: png_ptr->num_rows = (png_ptr->height + nuclear@1: png_pass_yinc[png_ptr->pass] - 1 - nuclear@1: png_pass_ystart[png_ptr->pass]) / nuclear@1: png_pass_yinc[png_ptr->pass]; nuclear@1: if (!(png_ptr->num_rows)) nuclear@1: continue; nuclear@1: } nuclear@1: else /* if (png_ptr->transformations & PNG_INTERLACE) */ nuclear@1: break; nuclear@1: } while (png_ptr->iwidth == 0); nuclear@1: nuclear@1: if (png_ptr->pass < 7) nuclear@1: return; nuclear@1: } nuclear@1: #endif /* PNG_READ_INTERLACING_SUPPORTED */ nuclear@1: nuclear@1: if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) nuclear@1: { nuclear@1: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@1: PNG_CONST PNG_IDAT; nuclear@1: #endif nuclear@1: char extra; nuclear@1: int ret; nuclear@1: nuclear@1: png_ptr->zstream.next_out = (Byte *)&extra; nuclear@1: png_ptr->zstream.avail_out = (uInt)1; nuclear@1: for (;;) nuclear@1: { nuclear@1: if (!(png_ptr->zstream.avail_in)) nuclear@1: { nuclear@1: while (!png_ptr->idat_size) nuclear@1: { nuclear@1: png_byte chunk_length[4]; nuclear@1: nuclear@1: png_crc_finish(png_ptr, 0); nuclear@1: nuclear@1: png_read_data(png_ptr, chunk_length, 4); nuclear@1: png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length); nuclear@1: png_reset_crc(png_ptr); nuclear@1: png_crc_read(png_ptr, png_ptr->chunk_name, 4); nuclear@1: if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) nuclear@1: png_error(png_ptr, "Not enough image data"); nuclear@1: nuclear@1: } nuclear@1: png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; nuclear@1: png_ptr->zstream.next_in = png_ptr->zbuf; nuclear@1: if (png_ptr->zbuf_size > png_ptr->idat_size) nuclear@1: png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; nuclear@1: png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); nuclear@1: png_ptr->idat_size -= png_ptr->zstream.avail_in; nuclear@1: } nuclear@1: ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); nuclear@1: if (ret == Z_STREAM_END) nuclear@1: { nuclear@1: if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || nuclear@1: png_ptr->idat_size) nuclear@1: png_warning(png_ptr, "Extra compressed data"); nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; nuclear@1: break; nuclear@1: } nuclear@1: if (ret != Z_OK) nuclear@1: png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : nuclear@1: "Decompression Error"); nuclear@1: nuclear@1: if (!(png_ptr->zstream.avail_out)) nuclear@1: { nuclear@1: png_warning(png_ptr, "Extra compressed data."); nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; nuclear@1: break; nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: png_ptr->zstream.avail_out = 0; nuclear@1: } nuclear@1: nuclear@1: if (png_ptr->idat_size || png_ptr->zstream.avail_in) nuclear@1: png_warning(png_ptr, "Extra compression data"); nuclear@1: nuclear@1: inflateReset(&png_ptr->zstream); nuclear@1: nuclear@1: png_ptr->mode |= PNG_AFTER_IDAT; nuclear@1: } nuclear@1: nuclear@1: void /* PRIVATE */ nuclear@1: png_read_start_row(png_structp png_ptr) nuclear@1: { nuclear@1: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@1: #ifdef PNG_READ_INTERLACING_SUPPORTED nuclear@1: /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ nuclear@1: nuclear@1: /* start of interlace block */ nuclear@1: PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; nuclear@1: nuclear@1: /* offset to next interlace block */ nuclear@1: PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; nuclear@1: nuclear@1: /* start of interlace block in the y direction */ nuclear@1: PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; nuclear@1: nuclear@1: /* offset to next interlace block in the y direction */ nuclear@1: PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; nuclear@1: #endif nuclear@1: #endif nuclear@1: nuclear@1: int max_pixel_depth; nuclear@1: png_size_t row_bytes; nuclear@1: nuclear@1: png_debug(1, "in png_read_start_row\n"); nuclear@1: png_ptr->zstream.avail_in = 0; nuclear@1: png_init_read_transformations(png_ptr); nuclear@1: #ifdef PNG_READ_INTERLACING_SUPPORTED nuclear@1: if (png_ptr->interlaced) nuclear@1: { nuclear@1: if (!(png_ptr->transformations & PNG_INTERLACE)) nuclear@1: png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - nuclear@1: png_pass_ystart[0]) / png_pass_yinc[0]; nuclear@1: else nuclear@1: png_ptr->num_rows = png_ptr->height; nuclear@1: nuclear@1: png_ptr->iwidth = (png_ptr->width + nuclear@1: png_pass_inc[png_ptr->pass] - 1 - nuclear@1: png_pass_start[png_ptr->pass]) / nuclear@1: png_pass_inc[png_ptr->pass]; nuclear@1: nuclear@1: png_ptr->irowbytes = nuclear@1: PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1; nuclear@1: } nuclear@1: else nuclear@1: #endif /* PNG_READ_INTERLACING_SUPPORTED */ nuclear@1: { nuclear@1: png_ptr->num_rows = png_ptr->height; nuclear@1: png_ptr->iwidth = png_ptr->width; nuclear@1: png_ptr->irowbytes = png_ptr->rowbytes + 1; nuclear@1: } nuclear@1: max_pixel_depth = png_ptr->pixel_depth; nuclear@1: nuclear@1: #if defined(PNG_READ_PACK_SUPPORTED) nuclear@1: if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) nuclear@1: max_pixel_depth = 8; nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@1: if (png_ptr->transformations & PNG_EXPAND) nuclear@1: { nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: { nuclear@1: if (png_ptr->num_trans) nuclear@1: max_pixel_depth = 32; nuclear@1: else nuclear@1: max_pixel_depth = 24; nuclear@1: } nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) nuclear@1: { nuclear@1: if (max_pixel_depth < 8) nuclear@1: max_pixel_depth = 8; nuclear@1: if (png_ptr->num_trans) nuclear@1: max_pixel_depth *= 2; nuclear@1: } nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) nuclear@1: { nuclear@1: if (png_ptr->num_trans) nuclear@1: { nuclear@1: max_pixel_depth *= 4; nuclear@1: max_pixel_depth /= 3; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_FILLER_SUPPORTED) nuclear@1: if (png_ptr->transformations & (PNG_FILLER)) nuclear@1: { nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@1: max_pixel_depth = 32; nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) nuclear@1: { nuclear@1: if (max_pixel_depth <= 8) nuclear@1: max_pixel_depth = 16; nuclear@1: else nuclear@1: max_pixel_depth = 32; nuclear@1: } nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) nuclear@1: { nuclear@1: if (max_pixel_depth <= 32) nuclear@1: max_pixel_depth = 32; nuclear@1: else nuclear@1: max_pixel_depth = 64; nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) nuclear@1: if (png_ptr->transformations & PNG_GRAY_TO_RGB) nuclear@1: { nuclear@1: if ( nuclear@1: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@1: (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || nuclear@1: #endif nuclear@1: #if defined(PNG_READ_FILLER_SUPPORTED) nuclear@1: (png_ptr->transformations & (PNG_FILLER)) || nuclear@1: #endif nuclear@1: png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) nuclear@1: { nuclear@1: if (max_pixel_depth <= 16) nuclear@1: max_pixel_depth = 32; nuclear@1: else nuclear@1: max_pixel_depth = 64; nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: if (max_pixel_depth <= 8) nuclear@1: { nuclear@1: if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) nuclear@1: max_pixel_depth = 32; nuclear@1: else nuclear@1: max_pixel_depth = 24; nuclear@1: } nuclear@1: else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) nuclear@1: max_pixel_depth = 64; nuclear@1: else nuclear@1: max_pixel_depth = 48; nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ nuclear@1: defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) nuclear@1: if (png_ptr->transformations & PNG_USER_TRANSFORM) nuclear@1: { nuclear@1: int user_pixel_depth = png_ptr->user_transform_depth* nuclear@1: png_ptr->user_transform_channels; nuclear@1: if (user_pixel_depth > max_pixel_depth) nuclear@1: max_pixel_depth=user_pixel_depth; nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: /* align the width on the next larger 8 pixels. Mainly used nuclear@1: for interlacing */ nuclear@1: row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); nuclear@1: /* calculate the maximum bytes needed, adding a byte and a pixel nuclear@1: for safety's sake */ nuclear@1: row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + nuclear@1: 1 + ((max_pixel_depth + 7) >> 3); nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: if (row_bytes > (png_uint_32)65536L) nuclear@1: png_error(png_ptr, "This image requires a row greater than 64KB"); nuclear@1: #endif nuclear@1: nuclear@1: if (row_bytes + 64 > png_ptr->old_big_row_buf_size) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->big_row_buf); nuclear@1: png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64); nuclear@1: png_ptr->row_buf = png_ptr->big_row_buf+32; nuclear@1: png_ptr->old_big_row_buf_size = row_bytes+64; nuclear@1: } nuclear@1: nuclear@1: #ifdef PNG_MAX_MALLOC_64K nuclear@1: if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L) nuclear@1: png_error(png_ptr, "This image requires a row greater than 64KB"); nuclear@1: #endif nuclear@1: if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1)) nuclear@1: png_error(png_ptr, "Row has too many bytes to allocate in memory."); nuclear@1: nuclear@1: if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size) nuclear@1: { nuclear@1: png_free(png_ptr, png_ptr->prev_row); nuclear@1: png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)( nuclear@1: png_ptr->rowbytes + 1)); nuclear@1: png_ptr->old_prev_row_size = png_ptr->rowbytes+1; nuclear@1: } nuclear@1: nuclear@1: png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); nuclear@1: nuclear@1: png_debug1(3, "width = %lu,\n", png_ptr->width); nuclear@1: png_debug1(3, "height = %lu,\n", png_ptr->height); nuclear@1: png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth); nuclear@1: png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows); nuclear@1: png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes); nuclear@1: png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes); nuclear@1: nuclear@1: png_ptr->flags |= PNG_FLAG_ROW_INIT; nuclear@1: } nuclear@1: #endif /* PNG_READ_SUPPORTED */