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