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