nuclear@0: nuclear@0: /* pngwutil.c - utilities to write a PNG file nuclear@0: * nuclear@0: * Last changed in libpng 1.2.30 [August 15, 2008] nuclear@0: * For conditions of distribution and use, see copyright notice in png.h nuclear@0: * Copyright (c) 1998-2008 Glenn Randers-Pehrson nuclear@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) nuclear@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) nuclear@0: */ nuclear@0: nuclear@0: #define PNG_INTERNAL nuclear@0: #include "png.h" nuclear@0: #ifdef PNG_WRITE_SUPPORTED nuclear@0: nuclear@0: /* Place a 32-bit number into a buffer in PNG byte order. We work nuclear@0: * with unsigned numbers for convenience, although one supported nuclear@0: * ancillary chunk uses signed (two's complement) numbers. nuclear@0: */ nuclear@0: void PNGAPI nuclear@0: png_save_uint_32(png_bytep buf, png_uint_32 i) nuclear@0: { nuclear@0: buf[0] = (png_byte)((i >> 24) & 0xff); nuclear@0: buf[1] = (png_byte)((i >> 16) & 0xff); nuclear@0: buf[2] = (png_byte)((i >> 8) & 0xff); nuclear@0: buf[3] = (png_byte)(i & 0xff); nuclear@0: } nuclear@0: nuclear@0: /* The png_save_int_32 function assumes integers are stored in two's nuclear@0: * complement format. If this isn't the case, then this routine needs to nuclear@0: * be modified to write data in two's complement format. nuclear@0: */ nuclear@0: void PNGAPI nuclear@0: png_save_int_32(png_bytep buf, png_int_32 i) nuclear@0: { nuclear@0: buf[0] = (png_byte)((i >> 24) & 0xff); nuclear@0: buf[1] = (png_byte)((i >> 16) & 0xff); nuclear@0: buf[2] = (png_byte)((i >> 8) & 0xff); nuclear@0: buf[3] = (png_byte)(i & 0xff); nuclear@0: } nuclear@0: nuclear@0: /* Place a 16-bit number into a buffer in PNG byte order. nuclear@0: * The parameter is declared unsigned int, not png_uint_16, nuclear@0: * just to avoid potential problems on pre-ANSI C compilers. nuclear@0: */ nuclear@0: void PNGAPI nuclear@0: png_save_uint_16(png_bytep buf, unsigned int i) nuclear@0: { nuclear@0: buf[0] = (png_byte)((i >> 8) & 0xff); nuclear@0: buf[1] = (png_byte)(i & 0xff); nuclear@0: } nuclear@0: nuclear@0: /* Simple function to write the signature. If we have already written nuclear@0: * the magic bytes of the signature, or more likely, the PNG stream is nuclear@0: * being embedded into another stream and doesn't need its own signature, nuclear@0: * we should call png_set_sig_bytes() to tell libpng how many of the nuclear@0: * bytes have already been written. nuclear@0: */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_sig(png_structp png_ptr) nuclear@0: { nuclear@0: png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; nuclear@0: nuclear@0: /* write the rest of the 8 byte signature */ nuclear@0: png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes], nuclear@0: (png_size_t)(8 - png_ptr->sig_bytes)); nuclear@0: if (png_ptr->sig_bytes < 3) nuclear@0: png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; nuclear@0: } nuclear@0: nuclear@0: /* Write a PNG chunk all at once. The type is an array of ASCII characters nuclear@0: * representing the chunk name. The array must be at least 4 bytes in nuclear@0: * length, and does not need to be null terminated. To be safe, pass the nuclear@0: * pre-defined chunk names here, and if you need a new one, define it nuclear@0: * where the others are defined. The length is the length of the data. nuclear@0: * All the data must be present. If that is not possible, use the nuclear@0: * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() nuclear@0: * functions instead. nuclear@0: */ nuclear@0: void PNGAPI nuclear@0: png_write_chunk(png_structp png_ptr, png_bytep chunk_name, nuclear@0: png_bytep data, png_size_t length) nuclear@0: { nuclear@0: if (png_ptr == NULL) return; nuclear@0: png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length); nuclear@0: png_write_chunk_data(png_ptr, data, (png_size_t)length); nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: } nuclear@0: nuclear@0: /* Write the start of a PNG chunk. The type is the chunk type. nuclear@0: * The total_length is the sum of the lengths of all the data you will be nuclear@0: * passing in png_write_chunk_data(). nuclear@0: */ nuclear@0: void PNGAPI nuclear@0: png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name, nuclear@0: png_uint_32 length) nuclear@0: { nuclear@0: png_byte buf[8]; nuclear@0: nuclear@0: png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name, nuclear@0: (unsigned long)length); nuclear@0: if (png_ptr == NULL) return; nuclear@0: nuclear@0: /* write the length and the chunk name */ nuclear@0: png_save_uint_32(buf, length); nuclear@0: png_memcpy(buf + 4, chunk_name, 4); nuclear@0: png_write_data(png_ptr, buf, (png_size_t)8); nuclear@0: /* put the chunk name into png_ptr->chunk_name */ nuclear@0: png_memcpy(png_ptr->chunk_name, chunk_name, 4); nuclear@0: /* reset the crc and run it over the chunk name */ nuclear@0: png_reset_crc(png_ptr); nuclear@0: png_calculate_crc(png_ptr, chunk_name, (png_size_t)4); nuclear@0: } nuclear@0: nuclear@0: /* Write the data of a PNG chunk started with png_write_chunk_start(). nuclear@0: * Note that multiple calls to this function are allowed, and that the nuclear@0: * sum of the lengths from these calls *must* add up to the total_length nuclear@0: * given to png_write_chunk_start(). nuclear@0: */ nuclear@0: void PNGAPI nuclear@0: png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@0: { nuclear@0: /* write the data, and run the CRC over it */ nuclear@0: if (png_ptr == NULL) return; nuclear@0: if (data != NULL && length > 0) nuclear@0: { nuclear@0: png_write_data(png_ptr, data, length); nuclear@0: /* update the CRC after writing the data, nuclear@0: * in case that the user I/O routine alters it. nuclear@0: */ nuclear@0: png_calculate_crc(png_ptr, data, length); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* Finish a chunk started with png_write_chunk_start(). */ nuclear@0: void PNGAPI nuclear@0: png_write_chunk_end(png_structp png_ptr) nuclear@0: { nuclear@0: png_byte buf[4]; nuclear@0: nuclear@0: if (png_ptr == NULL) return; nuclear@0: nuclear@0: /* write the crc in a single operation */ nuclear@0: png_save_uint_32(buf, png_ptr->crc); nuclear@0: nuclear@0: png_write_data(png_ptr, buf, (png_size_t)4); nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED) nuclear@0: /* nuclear@0: * This pair of functions encapsulates the operation of (a) compressing a nuclear@0: * text string, and (b) issuing it later as a series of chunk data writes. nuclear@0: * The compression_state structure is shared context for these functions nuclear@0: * set up by the caller in order to make the whole mess thread-safe. nuclear@0: */ nuclear@0: nuclear@0: typedef struct nuclear@0: { nuclear@0: char *input; /* the uncompressed input data */ nuclear@0: int input_len; /* its length */ nuclear@0: int num_output_ptr; /* number of output pointers used */ nuclear@0: int max_output_ptr; /* size of output_ptr */ nuclear@0: png_charpp output_ptr; /* array of pointers to output */ nuclear@0: } compression_state; nuclear@0: nuclear@0: /* compress given text into storage in the png_ptr structure */ nuclear@0: static int /* PRIVATE */ nuclear@0: png_text_compress(png_structp png_ptr, nuclear@0: png_charp text, png_size_t text_len, int compression, nuclear@0: compression_state *comp) nuclear@0: { nuclear@0: int ret; nuclear@0: nuclear@0: comp->num_output_ptr = 0; nuclear@0: comp->max_output_ptr = 0; nuclear@0: comp->output_ptr = NULL; nuclear@0: comp->input = NULL; nuclear@0: comp->input_len = 0; nuclear@0: nuclear@0: /* we may just want to pass the text right through */ nuclear@0: if (compression == PNG_TEXT_COMPRESSION_NONE) nuclear@0: { nuclear@0: comp->input = text; nuclear@0: comp->input_len = text_len; nuclear@0: return((int)text_len); nuclear@0: } nuclear@0: nuclear@0: if (compression >= PNG_TEXT_COMPRESSION_LAST) nuclear@0: { nuclear@0: #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) nuclear@0: char msg[50]; nuclear@0: png_snprintf(msg, 50, "Unknown compression type %d", compression); nuclear@0: png_warning(png_ptr, msg); nuclear@0: #else nuclear@0: png_warning(png_ptr, "Unknown compression type"); nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: /* We can't write the chunk until we find out how much data we have, nuclear@0: * which means we need to run the compressor first and save the nuclear@0: * output. This shouldn't be a problem, as the vast majority of nuclear@0: * comments should be reasonable, but we will set up an array of nuclear@0: * malloc'd pointers to be sure. nuclear@0: * nuclear@0: * If we knew the application was well behaved, we could simplify this nuclear@0: * greatly by assuming we can always malloc an output buffer large nuclear@0: * enough to hold the compressed text ((1001 * text_len / 1000) + 12) nuclear@0: * and malloc this directly. The only time this would be a bad idea is nuclear@0: * if we can't malloc more than 64K and we have 64K of random input nuclear@0: * data, or if the input string is incredibly large (although this nuclear@0: * wouldn't cause a failure, just a slowdown due to swapping). nuclear@0: */ nuclear@0: nuclear@0: /* set up the compression buffers */ nuclear@0: png_ptr->zstream.avail_in = (uInt)text_len; nuclear@0: png_ptr->zstream.next_in = (Bytef *)text; nuclear@0: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@0: png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf; nuclear@0: nuclear@0: /* this is the same compression loop as in png_write_row() */ nuclear@0: do nuclear@0: { nuclear@0: /* compress the data */ nuclear@0: ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); nuclear@0: if (ret != Z_OK) nuclear@0: { nuclear@0: /* error */ nuclear@0: if (png_ptr->zstream.msg != NULL) nuclear@0: png_error(png_ptr, png_ptr->zstream.msg); nuclear@0: else nuclear@0: png_error(png_ptr, "zlib error"); nuclear@0: } nuclear@0: /* check to see if we need more room */ nuclear@0: if (!(png_ptr->zstream.avail_out)) nuclear@0: { nuclear@0: /* make sure the output array has room */ nuclear@0: if (comp->num_output_ptr >= comp->max_output_ptr) nuclear@0: { nuclear@0: int old_max; nuclear@0: nuclear@0: old_max = comp->max_output_ptr; nuclear@0: comp->max_output_ptr = comp->num_output_ptr + 4; nuclear@0: if (comp->output_ptr != NULL) nuclear@0: { nuclear@0: png_charpp old_ptr; nuclear@0: nuclear@0: old_ptr = comp->output_ptr; nuclear@0: comp->output_ptr = (png_charpp)png_malloc(png_ptr, nuclear@0: (png_uint_32) nuclear@0: (comp->max_output_ptr * png_sizeof(png_charpp))); nuclear@0: png_memcpy(comp->output_ptr, old_ptr, old_max nuclear@0: * png_sizeof(png_charp)); nuclear@0: png_free(png_ptr, old_ptr); nuclear@0: } nuclear@0: else nuclear@0: comp->output_ptr = (png_charpp)png_malloc(png_ptr, nuclear@0: (png_uint_32) nuclear@0: (comp->max_output_ptr * png_sizeof(png_charp))); nuclear@0: } nuclear@0: nuclear@0: /* save the data */ nuclear@0: comp->output_ptr[comp->num_output_ptr] = nuclear@0: (png_charp)png_malloc(png_ptr, nuclear@0: (png_uint_32)png_ptr->zbuf_size); nuclear@0: png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, nuclear@0: png_ptr->zbuf_size); nuclear@0: comp->num_output_ptr++; nuclear@0: nuclear@0: /* and reset the buffer */ nuclear@0: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@0: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@0: } nuclear@0: /* continue until we don't have any more to compress */ nuclear@0: } while (png_ptr->zstream.avail_in); nuclear@0: nuclear@0: /* finish the compression */ nuclear@0: do nuclear@0: { nuclear@0: /* tell zlib we are finished */ nuclear@0: ret = deflate(&png_ptr->zstream, Z_FINISH); nuclear@0: nuclear@0: if (ret == Z_OK) nuclear@0: { nuclear@0: /* check to see if we need more room */ nuclear@0: if (!(png_ptr->zstream.avail_out)) nuclear@0: { nuclear@0: /* check to make sure our output array has room */ nuclear@0: if (comp->num_output_ptr >= comp->max_output_ptr) nuclear@0: { nuclear@0: int old_max; nuclear@0: nuclear@0: old_max = comp->max_output_ptr; nuclear@0: comp->max_output_ptr = comp->num_output_ptr + 4; nuclear@0: if (comp->output_ptr != NULL) nuclear@0: { nuclear@0: png_charpp old_ptr; nuclear@0: nuclear@0: old_ptr = comp->output_ptr; nuclear@0: /* This could be optimized to realloc() */ nuclear@0: comp->output_ptr = (png_charpp)png_malloc(png_ptr, nuclear@0: (png_uint_32)(comp->max_output_ptr * nuclear@0: png_sizeof(png_charp))); nuclear@0: png_memcpy(comp->output_ptr, old_ptr, nuclear@0: old_max * png_sizeof(png_charp)); nuclear@0: png_free(png_ptr, old_ptr); nuclear@0: } nuclear@0: else nuclear@0: comp->output_ptr = (png_charpp)png_malloc(png_ptr, nuclear@0: (png_uint_32)(comp->max_output_ptr * nuclear@0: png_sizeof(png_charp))); nuclear@0: } nuclear@0: nuclear@0: /* save off the data */ nuclear@0: comp->output_ptr[comp->num_output_ptr] = nuclear@0: (png_charp)png_malloc(png_ptr, nuclear@0: (png_uint_32)png_ptr->zbuf_size); nuclear@0: png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, nuclear@0: png_ptr->zbuf_size); nuclear@0: comp->num_output_ptr++; nuclear@0: nuclear@0: /* and reset the buffer pointers */ nuclear@0: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@0: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@0: } nuclear@0: } nuclear@0: else if (ret != Z_STREAM_END) nuclear@0: { nuclear@0: /* we got an error */ nuclear@0: if (png_ptr->zstream.msg != NULL) nuclear@0: png_error(png_ptr, png_ptr->zstream.msg); nuclear@0: else nuclear@0: png_error(png_ptr, "zlib error"); nuclear@0: } nuclear@0: } while (ret != Z_STREAM_END); nuclear@0: nuclear@0: /* text length is number of buffers plus last buffer */ nuclear@0: text_len = png_ptr->zbuf_size * comp->num_output_ptr; nuclear@0: if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) nuclear@0: text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out; nuclear@0: nuclear@0: return((int)text_len); nuclear@0: } nuclear@0: nuclear@0: /* ship the compressed text out via chunk writes */ nuclear@0: static void /* PRIVATE */ nuclear@0: png_write_compressed_data_out(png_structp png_ptr, compression_state *comp) nuclear@0: { nuclear@0: int i; nuclear@0: nuclear@0: /* handle the no-compression case */ nuclear@0: if (comp->input) nuclear@0: { nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)comp->input, nuclear@0: (png_size_t)comp->input_len); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: /* write saved output buffers, if any */ nuclear@0: for (i = 0; i < comp->num_output_ptr; i++) nuclear@0: { nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i], nuclear@0: (png_size_t)png_ptr->zbuf_size); nuclear@0: png_free(png_ptr, comp->output_ptr[i]); nuclear@0: comp->output_ptr[i]=NULL; nuclear@0: } nuclear@0: if (comp->max_output_ptr != 0) nuclear@0: png_free(png_ptr, comp->output_ptr); nuclear@0: comp->output_ptr=NULL; nuclear@0: /* write anything left in zbuf */ nuclear@0: if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size) nuclear@0: png_write_chunk_data(png_ptr, png_ptr->zbuf, nuclear@0: (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); nuclear@0: nuclear@0: /* reset zlib for another zTXt/iTXt or image data */ nuclear@0: deflateReset(&png_ptr->zstream); nuclear@0: png_ptr->zstream.data_type = Z_BINARY; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: /* Write the IHDR chunk, and update the png_struct with the necessary nuclear@0: * information. Note that the rest of this code depends upon this nuclear@0: * information being correct. nuclear@0: */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, nuclear@0: int bit_depth, int color_type, int compression_type, int filter_type, nuclear@0: int interlace_type) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_IHDR; nuclear@0: #endif nuclear@0: int ret; nuclear@0: nuclear@0: png_byte buf[13]; /* buffer to store the IHDR info */ nuclear@0: nuclear@0: png_debug(1, "in png_write_IHDR\n"); nuclear@0: /* Check that we have valid input data from the application info */ nuclear@0: switch (color_type) nuclear@0: { nuclear@0: case PNG_COLOR_TYPE_GRAY: nuclear@0: switch (bit_depth) nuclear@0: { nuclear@0: case 1: nuclear@0: case 2: nuclear@0: case 4: nuclear@0: case 8: nuclear@0: case 16: png_ptr->channels = 1; break; nuclear@0: default: png_error(png_ptr, "Invalid bit depth for grayscale image"); nuclear@0: } nuclear@0: break; nuclear@0: case PNG_COLOR_TYPE_RGB: nuclear@0: if (bit_depth != 8 && bit_depth != 16) nuclear@0: png_error(png_ptr, "Invalid bit depth for RGB image"); nuclear@0: png_ptr->channels = 3; nuclear@0: break; nuclear@0: case PNG_COLOR_TYPE_PALETTE: nuclear@0: switch (bit_depth) nuclear@0: { nuclear@0: case 1: nuclear@0: case 2: nuclear@0: case 4: nuclear@0: case 8: png_ptr->channels = 1; break; nuclear@0: default: png_error(png_ptr, "Invalid bit depth for paletted image"); nuclear@0: } nuclear@0: break; nuclear@0: case PNG_COLOR_TYPE_GRAY_ALPHA: nuclear@0: if (bit_depth != 8 && bit_depth != 16) nuclear@0: png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); nuclear@0: png_ptr->channels = 2; nuclear@0: break; nuclear@0: case PNG_COLOR_TYPE_RGB_ALPHA: nuclear@0: if (bit_depth != 8 && bit_depth != 16) nuclear@0: png_error(png_ptr, "Invalid bit depth for RGBA image"); nuclear@0: png_ptr->channels = 4; nuclear@0: break; nuclear@0: default: nuclear@0: png_error(png_ptr, "Invalid image color type specified"); nuclear@0: } nuclear@0: nuclear@0: if (compression_type != PNG_COMPRESSION_TYPE_BASE) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid compression type specified"); nuclear@0: compression_type = PNG_COMPRESSION_TYPE_BASE; nuclear@0: } nuclear@0: nuclear@0: /* Write filter_method 64 (intrapixel differencing) only if nuclear@0: * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and nuclear@0: * 2. Libpng did not write a PNG signature (this filter_method is only nuclear@0: * used in PNG datastreams that are embedded in MNG datastreams) and nuclear@0: * 3. The application called png_permit_mng_features with a mask that nuclear@0: * included PNG_FLAG_MNG_FILTER_64 and nuclear@0: * 4. The filter_method is 64 and nuclear@0: * 5. The color_type is RGB or RGBA nuclear@0: */ nuclear@0: if ( nuclear@0: #if defined(PNG_MNG_FEATURES_SUPPORTED) nuclear@0: !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && nuclear@0: ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && nuclear@0: (color_type == PNG_COLOR_TYPE_RGB || nuclear@0: color_type == PNG_COLOR_TYPE_RGB_ALPHA) && nuclear@0: (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && nuclear@0: #endif nuclear@0: filter_type != PNG_FILTER_TYPE_BASE) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid filter type specified"); nuclear@0: filter_type = PNG_FILTER_TYPE_BASE; nuclear@0: } nuclear@0: nuclear@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED nuclear@0: if (interlace_type != PNG_INTERLACE_NONE && nuclear@0: interlace_type != PNG_INTERLACE_ADAM7) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid interlace type specified"); nuclear@0: interlace_type = PNG_INTERLACE_ADAM7; nuclear@0: } nuclear@0: #else nuclear@0: interlace_type=PNG_INTERLACE_NONE; nuclear@0: #endif nuclear@0: nuclear@0: /* save off the relevent information */ nuclear@0: png_ptr->bit_depth = (png_byte)bit_depth; nuclear@0: png_ptr->color_type = (png_byte)color_type; nuclear@0: png_ptr->interlaced = (png_byte)interlace_type; nuclear@0: #if defined(PNG_MNG_FEATURES_SUPPORTED) nuclear@0: png_ptr->filter_type = (png_byte)filter_type; nuclear@0: #endif nuclear@0: png_ptr->compression_type = (png_byte)compression_type; nuclear@0: png_ptr->width = width; nuclear@0: png_ptr->height = height; nuclear@0: nuclear@0: png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); nuclear@0: png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); nuclear@0: /* set the usr info, so any transformations can modify it */ nuclear@0: png_ptr->usr_width = png_ptr->width; nuclear@0: png_ptr->usr_bit_depth = png_ptr->bit_depth; nuclear@0: png_ptr->usr_channels = png_ptr->channels; nuclear@0: nuclear@0: /* pack the header information into the buffer */ nuclear@0: png_save_uint_32(buf, width); nuclear@0: png_save_uint_32(buf + 4, height); nuclear@0: buf[8] = (png_byte)bit_depth; nuclear@0: buf[9] = (png_byte)color_type; nuclear@0: buf[10] = (png_byte)compression_type; nuclear@0: buf[11] = (png_byte)filter_type; nuclear@0: buf[12] = (png_byte)interlace_type; nuclear@0: nuclear@0: /* write the chunk */ nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13); nuclear@0: nuclear@0: /* initialize zlib with PNG info */ nuclear@0: png_ptr->zstream.zalloc = png_zalloc; nuclear@0: png_ptr->zstream.zfree = png_zfree; nuclear@0: png_ptr->zstream.opaque = (voidpf)png_ptr; nuclear@0: if (!(png_ptr->do_filter)) nuclear@0: { nuclear@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || nuclear@0: png_ptr->bit_depth < 8) nuclear@0: png_ptr->do_filter = PNG_FILTER_NONE; nuclear@0: else nuclear@0: png_ptr->do_filter = PNG_ALL_FILTERS; nuclear@0: } nuclear@0: if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY)) nuclear@0: { nuclear@0: if (png_ptr->do_filter != PNG_FILTER_NONE) nuclear@0: png_ptr->zlib_strategy = Z_FILTERED; nuclear@0: else nuclear@0: png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY; nuclear@0: } nuclear@0: if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL)) nuclear@0: png_ptr->zlib_level = Z_DEFAULT_COMPRESSION; nuclear@0: if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL)) nuclear@0: png_ptr->zlib_mem_level = 8; nuclear@0: if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS)) nuclear@0: png_ptr->zlib_window_bits = 15; nuclear@0: if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) nuclear@0: png_ptr->zlib_method = 8; nuclear@0: ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, nuclear@0: png_ptr->zlib_method, png_ptr->zlib_window_bits, nuclear@0: png_ptr->zlib_mem_level, png_ptr->zlib_strategy); nuclear@0: if (ret != Z_OK) nuclear@0: { nuclear@0: if (ret == Z_VERSION_ERROR) png_error(png_ptr, nuclear@0: "zlib failed to initialize compressor -- version error"); nuclear@0: if (ret == Z_STREAM_ERROR) png_error(png_ptr, nuclear@0: "zlib failed to initialize compressor -- stream error"); nuclear@0: if (ret == Z_MEM_ERROR) png_error(png_ptr, nuclear@0: "zlib failed to initialize compressor -- mem error"); nuclear@0: png_error(png_ptr, "zlib failed to initialize compressor"); nuclear@0: } nuclear@0: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@0: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@0: /* libpng is not interested in zstream.data_type */ nuclear@0: /* set it to a predefined value, to avoid its evaluation inside zlib */ nuclear@0: png_ptr->zstream.data_type = Z_BINARY; nuclear@0: nuclear@0: png_ptr->mode = PNG_HAVE_IHDR; nuclear@0: } nuclear@0: nuclear@0: /* write the palette. We are careful not to trust png_color to be in the nuclear@0: * correct order for PNG, so people can redefine it to any convenient nuclear@0: * structure. nuclear@0: */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_PLTE; nuclear@0: #endif nuclear@0: png_uint_32 i; nuclear@0: png_colorp pal_ptr; nuclear@0: png_byte buf[3]; nuclear@0: nuclear@0: png_debug(1, "in png_write_PLTE\n"); nuclear@0: if (( nuclear@0: #if defined(PNG_MNG_FEATURES_SUPPORTED) nuclear@0: !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && nuclear@0: #endif nuclear@0: num_pal == 0) || num_pal > 256) nuclear@0: { nuclear@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@0: { nuclear@0: png_error(png_ptr, "Invalid number of colors in palette"); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid number of colors in palette"); nuclear@0: return; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) nuclear@0: { nuclear@0: png_warning(png_ptr, nuclear@0: "Ignoring request to write a PLTE chunk in grayscale PNG"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: png_ptr->num_palette = (png_uint_16)num_pal; nuclear@0: png_debug1(3, "num_palette = %d\n", png_ptr->num_palette); nuclear@0: nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, nuclear@0: (png_uint_32)(num_pal * 3)); nuclear@0: #ifndef PNG_NO_POINTER_INDEXING nuclear@0: for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) nuclear@0: { nuclear@0: buf[0] = pal_ptr->red; nuclear@0: buf[1] = pal_ptr->green; nuclear@0: buf[2] = pal_ptr->blue; nuclear@0: png_write_chunk_data(png_ptr, buf, (png_size_t)3); nuclear@0: } nuclear@0: #else nuclear@0: /* This is a little slower but some buggy compilers need to do this instead */ nuclear@0: pal_ptr=palette; nuclear@0: for (i = 0; i < num_pal; i++) nuclear@0: { nuclear@0: buf[0] = pal_ptr[i].red; nuclear@0: buf[1] = pal_ptr[i].green; nuclear@0: buf[2] = pal_ptr[i].blue; nuclear@0: png_write_chunk_data(png_ptr, buf, (png_size_t)3); nuclear@0: } nuclear@0: #endif nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: png_ptr->mode |= PNG_HAVE_PLTE; nuclear@0: } nuclear@0: nuclear@0: /* write an IDAT chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_IDAT; nuclear@0: #endif nuclear@0: png_debug(1, "in png_write_IDAT\n"); nuclear@0: nuclear@0: /* Optimize the CMF field in the zlib stream. */ nuclear@0: /* This hack of the zlib stream is compliant to the stream specification. */ nuclear@0: if (!(png_ptr->mode & PNG_HAVE_IDAT) && nuclear@0: png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) nuclear@0: { nuclear@0: unsigned int z_cmf = data[0]; /* zlib compression method and flags */ nuclear@0: if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) nuclear@0: { nuclear@0: /* Avoid memory underflows and multiplication overflows. */ nuclear@0: /* The conditions below are practically always satisfied; nuclear@0: however, they still must be checked. */ nuclear@0: if (length >= 2 && nuclear@0: png_ptr->height < 16384 && png_ptr->width < 16384) nuclear@0: { nuclear@0: png_uint_32 uncompressed_idat_size = png_ptr->height * nuclear@0: ((png_ptr->width * nuclear@0: png_ptr->channels * png_ptr->bit_depth + 15) >> 3); nuclear@0: unsigned int z_cinfo = z_cmf >> 4; nuclear@0: unsigned int half_z_window_size = 1 << (z_cinfo + 7); nuclear@0: while (uncompressed_idat_size <= half_z_window_size && nuclear@0: half_z_window_size >= 256) nuclear@0: { nuclear@0: z_cinfo--; nuclear@0: half_z_window_size >>= 1; nuclear@0: } nuclear@0: z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); nuclear@0: if (data[0] != (png_byte)z_cmf) nuclear@0: { nuclear@0: data[0] = (png_byte)z_cmf; nuclear@0: data[1] &= 0xe0; nuclear@0: data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: else nuclear@0: png_error(png_ptr, nuclear@0: "Invalid zlib compression method or flags in IDAT"); nuclear@0: } nuclear@0: nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length); nuclear@0: png_ptr->mode |= PNG_HAVE_IDAT; nuclear@0: } nuclear@0: nuclear@0: /* write an IEND chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_IEND(png_structp png_ptr) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_IEND; nuclear@0: #endif nuclear@0: png_debug(1, "in png_write_IEND\n"); nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL, nuclear@0: (png_size_t)0); nuclear@0: png_ptr->mode |= PNG_HAVE_IEND; nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_gAMA_SUPPORTED) nuclear@0: /* write a gAMA chunk */ nuclear@0: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@0: void /* PRIVATE */ nuclear@0: png_write_gAMA(png_structp png_ptr, double file_gamma) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_gAMA; nuclear@0: #endif nuclear@0: png_uint_32 igamma; nuclear@0: png_byte buf[4]; nuclear@0: nuclear@0: png_debug(1, "in png_write_gAMA\n"); nuclear@0: /* file_gamma is saved in 1/100,000ths */ nuclear@0: igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf, igamma); nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); nuclear@0: } nuclear@0: #endif nuclear@0: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@0: void /* PRIVATE */ nuclear@0: png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_gAMA; nuclear@0: #endif nuclear@0: png_byte buf[4]; nuclear@0: nuclear@0: png_debug(1, "in png_write_gAMA\n"); nuclear@0: /* file_gamma is saved in 1/100,000ths */ nuclear@0: png_save_uint_32(buf, (png_uint_32)file_gamma); nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); nuclear@0: } nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_sRGB_SUPPORTED) nuclear@0: /* write a sRGB chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_sRGB(png_structp png_ptr, int srgb_intent) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_sRGB; nuclear@0: #endif nuclear@0: png_byte buf[1]; nuclear@0: nuclear@0: png_debug(1, "in png_write_sRGB\n"); nuclear@0: if (srgb_intent >= PNG_sRGB_INTENT_LAST) nuclear@0: png_warning(png_ptr, nuclear@0: "Invalid sRGB rendering intent specified"); nuclear@0: buf[0]=(png_byte)srgb_intent; nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_iCCP_SUPPORTED) nuclear@0: /* write an iCCP chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type, nuclear@0: png_charp profile, int profile_len) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_iCCP; nuclear@0: #endif nuclear@0: png_size_t name_len; nuclear@0: png_charp new_name; nuclear@0: compression_state comp; nuclear@0: int embedded_profile_len = 0; nuclear@0: nuclear@0: png_debug(1, "in png_write_iCCP\n"); nuclear@0: nuclear@0: comp.num_output_ptr = 0; nuclear@0: comp.max_output_ptr = 0; nuclear@0: comp.output_ptr = NULL; nuclear@0: comp.input = NULL; nuclear@0: comp.input_len = 0; nuclear@0: nuclear@0: if (name == NULL || (name_len = png_check_keyword(png_ptr, name, nuclear@0: &new_name)) == 0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Empty keyword in iCCP chunk"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: if (compression_type != PNG_COMPRESSION_TYPE_BASE) nuclear@0: png_warning(png_ptr, "Unknown compression type in iCCP chunk"); nuclear@0: nuclear@0: if (profile == NULL) nuclear@0: profile_len = 0; nuclear@0: nuclear@0: if (profile_len > 3) nuclear@0: embedded_profile_len = nuclear@0: ((*( (png_bytep)profile ))<<24) | nuclear@0: ((*( (png_bytep)profile + 1))<<16) | nuclear@0: ((*( (png_bytep)profile + 2))<< 8) | nuclear@0: ((*( (png_bytep)profile + 3)) ); nuclear@0: nuclear@0: if (profile_len < embedded_profile_len) nuclear@0: { nuclear@0: png_warning(png_ptr, nuclear@0: "Embedded profile length too large in iCCP chunk"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: if (profile_len > embedded_profile_len) nuclear@0: { nuclear@0: png_warning(png_ptr, nuclear@0: "Truncating profile to actual length in iCCP chunk"); nuclear@0: profile_len = embedded_profile_len; nuclear@0: } nuclear@0: nuclear@0: if (profile_len) nuclear@0: profile_len = png_text_compress(png_ptr, profile, nuclear@0: (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp); nuclear@0: nuclear@0: /* make sure we include the NULL after the name and the compression type */ nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_iCCP, nuclear@0: (png_uint_32)(name_len + profile_len + 2)); nuclear@0: new_name[name_len + 1] = 0x00; nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)new_name, nuclear@0: (png_size_t)(name_len + 2)); nuclear@0: nuclear@0: if (profile_len) nuclear@0: png_write_compressed_data_out(png_ptr, &comp); nuclear@0: nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: png_free(png_ptr, new_name); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_sPLT_SUPPORTED) nuclear@0: /* write a sPLT chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_sPLT; nuclear@0: #endif nuclear@0: png_size_t name_len; nuclear@0: png_charp new_name; nuclear@0: png_byte entrybuf[10]; nuclear@0: int entry_size = (spalette->depth == 8 ? 6 : 10); nuclear@0: int palette_size = entry_size * spalette->nentries; nuclear@0: png_sPLT_entryp ep; nuclear@0: #ifdef PNG_NO_POINTER_INDEXING nuclear@0: int i; nuclear@0: #endif nuclear@0: nuclear@0: png_debug(1, "in png_write_sPLT\n"); nuclear@0: if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr, nuclear@0: spalette->name, &new_name))==0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Empty keyword in sPLT chunk"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: /* make sure we include the NULL after the name */ nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_sPLT, nuclear@0: (png_uint_32)(name_len + 2 + palette_size)); nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)new_name, nuclear@0: (png_size_t)(name_len + 1)); nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1); nuclear@0: nuclear@0: /* loop through each palette entry, writing appropriately */ nuclear@0: #ifndef PNG_NO_POINTER_INDEXING nuclear@0: for (ep = spalette->entries; epentries + spalette->nentries; ep++) nuclear@0: { nuclear@0: if (spalette->depth == 8) nuclear@0: { nuclear@0: entrybuf[0] = (png_byte)ep->red; nuclear@0: entrybuf[1] = (png_byte)ep->green; nuclear@0: entrybuf[2] = (png_byte)ep->blue; nuclear@0: entrybuf[3] = (png_byte)ep->alpha; nuclear@0: png_save_uint_16(entrybuf + 4, ep->frequency); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: png_save_uint_16(entrybuf + 0, ep->red); nuclear@0: png_save_uint_16(entrybuf + 2, ep->green); nuclear@0: png_save_uint_16(entrybuf + 4, ep->blue); nuclear@0: png_save_uint_16(entrybuf + 6, ep->alpha); nuclear@0: png_save_uint_16(entrybuf + 8, ep->frequency); nuclear@0: } nuclear@0: png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); nuclear@0: } nuclear@0: #else nuclear@0: ep=spalette->entries; nuclear@0: for (i=0; i>spalette->nentries; i++) nuclear@0: { nuclear@0: if (spalette->depth == 8) nuclear@0: { nuclear@0: entrybuf[0] = (png_byte)ep[i].red; nuclear@0: entrybuf[1] = (png_byte)ep[i].green; nuclear@0: entrybuf[2] = (png_byte)ep[i].blue; nuclear@0: entrybuf[3] = (png_byte)ep[i].alpha; nuclear@0: png_save_uint_16(entrybuf + 4, ep[i].frequency); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: png_save_uint_16(entrybuf + 0, ep[i].red); nuclear@0: png_save_uint_16(entrybuf + 2, ep[i].green); nuclear@0: png_save_uint_16(entrybuf + 4, ep[i].blue); nuclear@0: png_save_uint_16(entrybuf + 6, ep[i].alpha); nuclear@0: png_save_uint_16(entrybuf + 8, ep[i].frequency); nuclear@0: } nuclear@0: png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: png_free(png_ptr, new_name); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_sBIT_SUPPORTED) nuclear@0: /* write the sBIT chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_sBIT; nuclear@0: #endif nuclear@0: png_byte buf[4]; nuclear@0: png_size_t size; nuclear@0: nuclear@0: png_debug(1, "in png_write_sBIT\n"); nuclear@0: /* make sure we don't depend upon the order of PNG_COLOR_8 */ nuclear@0: if (color_type & PNG_COLOR_MASK_COLOR) nuclear@0: { nuclear@0: png_byte maxbits; nuclear@0: nuclear@0: maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : nuclear@0: png_ptr->usr_bit_depth); nuclear@0: if (sbit->red == 0 || sbit->red > maxbits || nuclear@0: sbit->green == 0 || sbit->green > maxbits || nuclear@0: sbit->blue == 0 || sbit->blue > maxbits) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid sBIT depth specified"); nuclear@0: return; nuclear@0: } nuclear@0: buf[0] = sbit->red; nuclear@0: buf[1] = sbit->green; nuclear@0: buf[2] = sbit->blue; nuclear@0: size = 3; nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid sBIT depth specified"); nuclear@0: return; nuclear@0: } nuclear@0: buf[0] = sbit->gray; nuclear@0: size = 1; nuclear@0: } nuclear@0: nuclear@0: if (color_type & PNG_COLOR_MASK_ALPHA) nuclear@0: { nuclear@0: if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid sBIT depth specified"); nuclear@0: return; nuclear@0: } nuclear@0: buf[size++] = sbit->alpha; nuclear@0: } nuclear@0: nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_cHRM_SUPPORTED) nuclear@0: /* write the cHRM chunk */ nuclear@0: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@0: void /* PRIVATE */ nuclear@0: png_write_cHRM(png_structp png_ptr, double white_x, double white_y, nuclear@0: double red_x, double red_y, double green_x, double green_y, nuclear@0: double blue_x, double blue_y) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_cHRM; nuclear@0: #endif nuclear@0: png_byte buf[32]; nuclear@0: png_uint_32 itemp; nuclear@0: nuclear@0: png_debug(1, "in png_write_cHRM\n"); nuclear@0: /* each value is saved in 1/100,000ths */ nuclear@0: if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 || nuclear@0: white_x + white_y > 1.0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid cHRM white point specified"); nuclear@0: #if !defined(PNG_NO_CONSOLE_IO) nuclear@0: fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y); nuclear@0: #endif nuclear@0: return; nuclear@0: } nuclear@0: itemp = (png_uint_32)(white_x * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf, itemp); nuclear@0: itemp = (png_uint_32)(white_y * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf + 4, itemp); nuclear@0: nuclear@0: if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid cHRM red point specified"); nuclear@0: return; nuclear@0: } nuclear@0: itemp = (png_uint_32)(red_x * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf + 8, itemp); nuclear@0: itemp = (png_uint_32)(red_y * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf + 12, itemp); nuclear@0: nuclear@0: if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid cHRM green point specified"); nuclear@0: return; nuclear@0: } nuclear@0: itemp = (png_uint_32)(green_x * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf + 16, itemp); nuclear@0: itemp = (png_uint_32)(green_y * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf + 20, itemp); nuclear@0: nuclear@0: if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid cHRM blue point specified"); nuclear@0: return; nuclear@0: } nuclear@0: itemp = (png_uint_32)(blue_x * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf + 24, itemp); nuclear@0: itemp = (png_uint_32)(blue_y * 100000.0 + 0.5); nuclear@0: png_save_uint_32(buf + 28, itemp); nuclear@0: nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); nuclear@0: } nuclear@0: #endif nuclear@0: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@0: void /* PRIVATE */ nuclear@0: png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, nuclear@0: png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, nuclear@0: png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, nuclear@0: png_fixed_point blue_y) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_cHRM; nuclear@0: #endif nuclear@0: png_byte buf[32]; nuclear@0: nuclear@0: png_debug(1, "in png_write_cHRM\n"); nuclear@0: /* each value is saved in 1/100,000ths */ nuclear@0: if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid fixed cHRM white point specified"); nuclear@0: #if !defined(PNG_NO_CONSOLE_IO) nuclear@0: fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x, nuclear@0: (unsigned long)white_y); nuclear@0: #endif nuclear@0: return; nuclear@0: } nuclear@0: png_save_uint_32(buf, (png_uint_32)white_x); nuclear@0: png_save_uint_32(buf + 4, (png_uint_32)white_y); nuclear@0: nuclear@0: if (red_x + red_y > 100000L) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid cHRM fixed red point specified"); nuclear@0: return; nuclear@0: } nuclear@0: png_save_uint_32(buf + 8, (png_uint_32)red_x); nuclear@0: png_save_uint_32(buf + 12, (png_uint_32)red_y); nuclear@0: nuclear@0: if (green_x + green_y > 100000L) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid fixed cHRM green point specified"); nuclear@0: return; nuclear@0: } nuclear@0: png_save_uint_32(buf + 16, (png_uint_32)green_x); nuclear@0: png_save_uint_32(buf + 20, (png_uint_32)green_y); nuclear@0: nuclear@0: if (blue_x + blue_y > 100000L) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid fixed cHRM blue point specified"); nuclear@0: return; nuclear@0: } nuclear@0: png_save_uint_32(buf + 24, (png_uint_32)blue_x); nuclear@0: png_save_uint_32(buf + 28, (png_uint_32)blue_y); nuclear@0: nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); nuclear@0: } nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_tRNS_SUPPORTED) nuclear@0: /* write the tRNS chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran, nuclear@0: int num_trans, int color_type) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_tRNS; nuclear@0: #endif nuclear@0: png_byte buf[6]; nuclear@0: nuclear@0: png_debug(1, "in png_write_tRNS\n"); nuclear@0: if (color_type == PNG_COLOR_TYPE_PALETTE) nuclear@0: { nuclear@0: if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid number of transparent colors specified"); nuclear@0: return; nuclear@0: } nuclear@0: /* write the chunk out as it is */ nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, nuclear@0: (png_size_t)num_trans); nuclear@0: } nuclear@0: else if (color_type == PNG_COLOR_TYPE_GRAY) nuclear@0: { nuclear@0: /* one 16 bit value */ nuclear@0: if (tran->gray >= (1 << png_ptr->bit_depth)) nuclear@0: { nuclear@0: png_warning(png_ptr, nuclear@0: "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); nuclear@0: return; nuclear@0: } nuclear@0: png_save_uint_16(buf, tran->gray); nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2); nuclear@0: } nuclear@0: else if (color_type == PNG_COLOR_TYPE_RGB) nuclear@0: { nuclear@0: /* three 16 bit values */ nuclear@0: png_save_uint_16(buf, tran->red); nuclear@0: png_save_uint_16(buf + 2, tran->green); nuclear@0: png_save_uint_16(buf + 4, tran->blue); nuclear@0: if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) nuclear@0: { nuclear@0: png_warning(png_ptr, nuclear@0: "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); nuclear@0: return; nuclear@0: } nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: png_warning(png_ptr, "Can't write tRNS with an alpha channel"); nuclear@0: } nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_bKGD_SUPPORTED) nuclear@0: /* write the background chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_bKGD; nuclear@0: #endif nuclear@0: png_byte buf[6]; nuclear@0: nuclear@0: png_debug(1, "in png_write_bKGD\n"); nuclear@0: if (color_type == PNG_COLOR_TYPE_PALETTE) nuclear@0: { nuclear@0: if ( nuclear@0: #if defined(PNG_MNG_FEATURES_SUPPORTED) nuclear@0: (png_ptr->num_palette || nuclear@0: (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && nuclear@0: #endif nuclear@0: back->index > png_ptr->num_palette) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid background palette index"); nuclear@0: return; nuclear@0: } nuclear@0: buf[0] = back->index; nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1); nuclear@0: } nuclear@0: else if (color_type & PNG_COLOR_MASK_COLOR) nuclear@0: { nuclear@0: png_save_uint_16(buf, back->red); nuclear@0: png_save_uint_16(buf + 2, back->green); nuclear@0: png_save_uint_16(buf + 4, back->blue); nuclear@0: if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) nuclear@0: { nuclear@0: png_warning(png_ptr, nuclear@0: "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); nuclear@0: return; nuclear@0: } nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: if (back->gray >= (1 << png_ptr->bit_depth)) nuclear@0: { nuclear@0: png_warning(png_ptr, nuclear@0: "Ignoring attempt to write bKGD chunk out-of-range for bit_depth"); nuclear@0: return; nuclear@0: } nuclear@0: png_save_uint_16(buf, back->gray); nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2); nuclear@0: } nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_hIST_SUPPORTED) nuclear@0: /* write the histogram */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_hIST; nuclear@0: #endif nuclear@0: int i; nuclear@0: png_byte buf[3]; nuclear@0: nuclear@0: png_debug(1, "in png_write_hIST\n"); nuclear@0: if (num_hist > (int)png_ptr->num_palette) nuclear@0: { nuclear@0: png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist, nuclear@0: png_ptr->num_palette); nuclear@0: png_warning(png_ptr, "Invalid number of histogram entries specified"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_hIST, nuclear@0: (png_uint_32)(num_hist * 2)); nuclear@0: for (i = 0; i < num_hist; i++) nuclear@0: { nuclear@0: png_save_uint_16(buf, hist[i]); nuclear@0: png_write_chunk_data(png_ptr, buf, (png_size_t)2); nuclear@0: } nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ nuclear@0: defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) nuclear@0: /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, nuclear@0: * and if invalid, correct the keyword rather than discarding the entire nuclear@0: * chunk. The PNG 1.0 specification requires keywords 1-79 characters in nuclear@0: * length, forbids leading or trailing whitespace, multiple internal spaces, nuclear@0: * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. nuclear@0: * nuclear@0: * The new_key is allocated to hold the corrected keyword and must be freed nuclear@0: * by the calling routine. This avoids problems with trying to write to nuclear@0: * static keywords without having to have duplicate copies of the strings. nuclear@0: */ nuclear@0: png_size_t /* PRIVATE */ nuclear@0: png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key) nuclear@0: { nuclear@0: png_size_t key_len; nuclear@0: png_charp kp, dp; nuclear@0: int kflag; nuclear@0: int kwarn=0; nuclear@0: nuclear@0: png_debug(1, "in png_check_keyword\n"); nuclear@0: *new_key = NULL; nuclear@0: nuclear@0: if (key == NULL || (key_len = png_strlen(key)) == 0) nuclear@0: { nuclear@0: png_warning(png_ptr, "zero length keyword"); nuclear@0: return ((png_size_t)0); nuclear@0: } nuclear@0: nuclear@0: png_debug1(2, "Keyword to be checked is '%s'\n", key); nuclear@0: nuclear@0: *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2)); nuclear@0: if (*new_key == NULL) nuclear@0: { nuclear@0: png_warning(png_ptr, "Out of memory while procesing keyword"); nuclear@0: return ((png_size_t)0); nuclear@0: } nuclear@0: nuclear@0: /* Replace non-printing characters with a blank and print a warning */ nuclear@0: for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++) nuclear@0: { nuclear@0: if ((png_byte)*kp < 0x20 || nuclear@0: ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1)) nuclear@0: { nuclear@0: #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) nuclear@0: char msg[40]; nuclear@0: nuclear@0: png_snprintf(msg, 40, nuclear@0: "invalid keyword character 0x%02X", (png_byte)*kp); nuclear@0: png_warning(png_ptr, msg); nuclear@0: #else nuclear@0: png_warning(png_ptr, "invalid character in keyword"); nuclear@0: #endif nuclear@0: *dp = ' '; nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: *dp = *kp; nuclear@0: } nuclear@0: } nuclear@0: *dp = '\0'; nuclear@0: nuclear@0: /* Remove any trailing white space. */ nuclear@0: kp = *new_key + key_len - 1; nuclear@0: if (*kp == ' ') nuclear@0: { nuclear@0: png_warning(png_ptr, "trailing spaces removed from keyword"); nuclear@0: nuclear@0: while (*kp == ' ') nuclear@0: { nuclear@0: *(kp--) = '\0'; nuclear@0: key_len--; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* Remove any leading white space. */ nuclear@0: kp = *new_key; nuclear@0: if (*kp == ' ') nuclear@0: { nuclear@0: png_warning(png_ptr, "leading spaces removed from keyword"); nuclear@0: nuclear@0: while (*kp == ' ') nuclear@0: { nuclear@0: kp++; nuclear@0: key_len--; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp); nuclear@0: nuclear@0: /* Remove multiple internal spaces. */ nuclear@0: for (kflag = 0, dp = *new_key; *kp != '\0'; kp++) nuclear@0: { nuclear@0: if (*kp == ' ' && kflag == 0) nuclear@0: { nuclear@0: *(dp++) = *kp; nuclear@0: kflag = 1; nuclear@0: } nuclear@0: else if (*kp == ' ') nuclear@0: { nuclear@0: key_len--; nuclear@0: kwarn=1; nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: *(dp++) = *kp; nuclear@0: kflag = 0; nuclear@0: } nuclear@0: } nuclear@0: *dp = '\0'; nuclear@0: if (kwarn) nuclear@0: png_warning(png_ptr, "extra interior spaces removed from keyword"); nuclear@0: nuclear@0: if (key_len == 0) nuclear@0: { nuclear@0: png_free(png_ptr, *new_key); nuclear@0: *new_key=NULL; nuclear@0: png_warning(png_ptr, "Zero length keyword"); nuclear@0: } nuclear@0: nuclear@0: if (key_len > 79) nuclear@0: { nuclear@0: png_warning(png_ptr, "keyword length must be 1 - 79 characters"); nuclear@0: new_key[79] = '\0'; nuclear@0: key_len = 79; nuclear@0: } nuclear@0: nuclear@0: return (key_len); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_tEXt_SUPPORTED) nuclear@0: /* write a tEXt chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text, nuclear@0: png_size_t text_len) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_tEXt; nuclear@0: #endif nuclear@0: png_size_t key_len; nuclear@0: png_charp new_key; nuclear@0: nuclear@0: png_debug(1, "in png_write_tEXt\n"); nuclear@0: if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Empty keyword in tEXt chunk"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: if (text == NULL || *text == '\0') nuclear@0: text_len = 0; nuclear@0: else nuclear@0: text_len = png_strlen(text); nuclear@0: nuclear@0: /* make sure we include the 0 after the key */ nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, nuclear@0: (png_uint_32)(key_len + text_len + 1)); nuclear@0: /* nuclear@0: * We leave it to the application to meet PNG-1.0 requirements on the nuclear@0: * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of nuclear@0: * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. nuclear@0: * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. nuclear@0: */ nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)new_key, nuclear@0: (png_size_t)(key_len + 1)); nuclear@0: if (text_len) nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len); nuclear@0: nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: png_free(png_ptr, new_key); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_zTXt_SUPPORTED) nuclear@0: /* write a compressed text chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, nuclear@0: png_size_t text_len, int compression) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_zTXt; nuclear@0: #endif nuclear@0: png_size_t key_len; nuclear@0: char buf[1]; nuclear@0: png_charp new_key; nuclear@0: compression_state comp; nuclear@0: nuclear@0: png_debug(1, "in png_write_zTXt\n"); nuclear@0: nuclear@0: comp.num_output_ptr = 0; nuclear@0: comp.max_output_ptr = 0; nuclear@0: comp.output_ptr = NULL; nuclear@0: comp.input = NULL; nuclear@0: comp.input_len = 0; nuclear@0: nuclear@0: if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Empty keyword in zTXt chunk"); nuclear@0: png_free(png_ptr, new_key); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE) nuclear@0: { nuclear@0: png_write_tEXt(png_ptr, new_key, text, (png_size_t)0); nuclear@0: png_free(png_ptr, new_key); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: text_len = png_strlen(text); nuclear@0: nuclear@0: /* compute the compressed data; do it now for the length */ nuclear@0: text_len = png_text_compress(png_ptr, text, text_len, compression, nuclear@0: &comp); nuclear@0: nuclear@0: /* write start of chunk */ nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, nuclear@0: (png_uint_32)(key_len+text_len + 2)); nuclear@0: /* write key */ nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)new_key, nuclear@0: (png_size_t)(key_len + 1)); nuclear@0: png_free(png_ptr, new_key); nuclear@0: nuclear@0: buf[0] = (png_byte)compression; nuclear@0: /* write compression */ nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1); nuclear@0: /* write the compressed data */ nuclear@0: png_write_compressed_data_out(png_ptr, &comp); nuclear@0: nuclear@0: /* close the chunk */ nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_iTXt_SUPPORTED) nuclear@0: /* write an iTXt chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_iTXt(png_structp png_ptr, int compression, png_charp key, nuclear@0: png_charp lang, png_charp lang_key, png_charp text) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_iTXt; nuclear@0: #endif nuclear@0: png_size_t lang_len, key_len, lang_key_len, text_len; nuclear@0: png_charp new_lang, new_key; nuclear@0: png_byte cbuf[2]; nuclear@0: compression_state comp; nuclear@0: nuclear@0: png_debug(1, "in png_write_iTXt\n"); nuclear@0: nuclear@0: comp.num_output_ptr = 0; nuclear@0: comp.max_output_ptr = 0; nuclear@0: comp.output_ptr = NULL; nuclear@0: comp.input = NULL; nuclear@0: nuclear@0: if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Empty keyword in iTXt chunk"); nuclear@0: return; nuclear@0: } nuclear@0: if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0) nuclear@0: { nuclear@0: png_warning(png_ptr, "Empty language field in iTXt chunk"); nuclear@0: new_lang = NULL; nuclear@0: lang_len = 0; nuclear@0: } nuclear@0: nuclear@0: if (lang_key == NULL) nuclear@0: lang_key_len = 0; nuclear@0: else nuclear@0: lang_key_len = png_strlen(lang_key); nuclear@0: nuclear@0: if (text == NULL) nuclear@0: text_len = 0; nuclear@0: else nuclear@0: text_len = png_strlen(text); nuclear@0: nuclear@0: /* compute the compressed data; do it now for the length */ nuclear@0: text_len = png_text_compress(png_ptr, text, text_len, compression-2, nuclear@0: &comp); nuclear@0: nuclear@0: nuclear@0: /* make sure we include the compression flag, the compression byte, nuclear@0: * and the NULs after the key, lang, and lang_key parts */ nuclear@0: nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_iTXt, nuclear@0: (png_uint_32)( nuclear@0: 5 /* comp byte, comp flag, terminators for key, lang and lang_key */ nuclear@0: + key_len nuclear@0: + lang_len nuclear@0: + lang_key_len nuclear@0: + text_len)); nuclear@0: nuclear@0: /* nuclear@0: * We leave it to the application to meet PNG-1.0 requirements on the nuclear@0: * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of nuclear@0: * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. nuclear@0: * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. nuclear@0: */ nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)new_key, nuclear@0: (png_size_t)(key_len + 1)); nuclear@0: nuclear@0: /* set the compression flag */ nuclear@0: if (compression == PNG_ITXT_COMPRESSION_NONE || \ nuclear@0: compression == PNG_TEXT_COMPRESSION_NONE) nuclear@0: cbuf[0] = 0; nuclear@0: else /* compression == PNG_ITXT_COMPRESSION_zTXt */ nuclear@0: cbuf[0] = 1; nuclear@0: /* set the compression method */ nuclear@0: cbuf[1] = 0; nuclear@0: png_write_chunk_data(png_ptr, cbuf, (png_size_t)2); nuclear@0: nuclear@0: cbuf[0] = 0; nuclear@0: png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), nuclear@0: (png_size_t)(lang_len + 1)); nuclear@0: png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), nuclear@0: (png_size_t)(lang_key_len + 1)); nuclear@0: png_write_compressed_data_out(png_ptr, &comp); nuclear@0: nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: png_free(png_ptr, new_key); nuclear@0: png_free(png_ptr, new_lang); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_oFFs_SUPPORTED) nuclear@0: /* write the oFFs chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, nuclear@0: int unit_type) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_oFFs; nuclear@0: #endif nuclear@0: png_byte buf[9]; nuclear@0: nuclear@0: png_debug(1, "in png_write_oFFs\n"); nuclear@0: if (unit_type >= PNG_OFFSET_LAST) nuclear@0: png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); nuclear@0: nuclear@0: png_save_int_32(buf, x_offset); nuclear@0: png_save_int_32(buf + 4, y_offset); nuclear@0: buf[8] = (png_byte)unit_type; nuclear@0: nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9); nuclear@0: } nuclear@0: #endif nuclear@0: #if defined(PNG_WRITE_pCAL_SUPPORTED) nuclear@0: /* write the pCAL chunk (described in the PNG extensions document) */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, nuclear@0: png_int_32 X1, int type, int nparams, png_charp units, png_charpp params) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_pCAL; nuclear@0: #endif nuclear@0: png_size_t purpose_len, units_len, total_len; nuclear@0: png_uint_32p params_len; nuclear@0: png_byte buf[10]; nuclear@0: png_charp new_purpose; nuclear@0: int i; nuclear@0: nuclear@0: png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams); nuclear@0: if (type >= PNG_EQUATION_LAST) nuclear@0: png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); nuclear@0: nuclear@0: purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1; nuclear@0: png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len); nuclear@0: units_len = png_strlen(units) + (nparams == 0 ? 0 : 1); nuclear@0: png_debug1(3, "pCAL units length = %d\n", (int)units_len); nuclear@0: total_len = purpose_len + units_len + 10; nuclear@0: nuclear@0: params_len = (png_uint_32p)png_malloc(png_ptr, nuclear@0: (png_uint_32)(nparams * png_sizeof(png_uint_32))); nuclear@0: nuclear@0: /* Find the length of each parameter, making sure we don't count the nuclear@0: null terminator for the last parameter. */ nuclear@0: for (i = 0; i < nparams; i++) nuclear@0: { nuclear@0: params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1); nuclear@0: png_debug2(3, "pCAL parameter %d length = %lu\n", i, nuclear@0: (unsigned long) params_len[i]); nuclear@0: total_len += (png_size_t)params_len[i]; nuclear@0: } nuclear@0: nuclear@0: png_debug1(3, "pCAL total length = %d\n", (int)total_len); nuclear@0: png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len); nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)new_purpose, nuclear@0: (png_size_t)purpose_len); nuclear@0: png_save_int_32(buf, X0); nuclear@0: png_save_int_32(buf + 4, X1); nuclear@0: buf[8] = (png_byte)type; nuclear@0: buf[9] = (png_byte)nparams; nuclear@0: png_write_chunk_data(png_ptr, buf, (png_size_t)10); nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len); nuclear@0: nuclear@0: png_free(png_ptr, new_purpose); nuclear@0: nuclear@0: for (i = 0; i < nparams; i++) nuclear@0: { nuclear@0: png_write_chunk_data(png_ptr, (png_bytep)params[i], nuclear@0: (png_size_t)params_len[i]); nuclear@0: } nuclear@0: nuclear@0: png_free(png_ptr, params_len); nuclear@0: png_write_chunk_end(png_ptr); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_sCAL_SUPPORTED) nuclear@0: /* write the sCAL chunk */ nuclear@0: #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO) nuclear@0: void /* PRIVATE */ nuclear@0: png_write_sCAL(png_structp png_ptr, int unit, double width, double height) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_sCAL; nuclear@0: #endif nuclear@0: char buf[64]; nuclear@0: png_size_t total_len; nuclear@0: nuclear@0: png_debug(1, "in png_write_sCAL\n"); nuclear@0: nuclear@0: buf[0] = (char)unit; nuclear@0: #if defined(_WIN32_WCE) nuclear@0: /* sprintf() function is not supported on WindowsCE */ nuclear@0: { nuclear@0: wchar_t wc_buf[32]; nuclear@0: size_t wc_len; nuclear@0: swprintf(wc_buf, TEXT("%12.12e"), width); nuclear@0: wc_len = wcslen(wc_buf); nuclear@0: WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL); nuclear@0: total_len = wc_len + 2; nuclear@0: swprintf(wc_buf, TEXT("%12.12e"), height); nuclear@0: wc_len = wcslen(wc_buf); nuclear@0: WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len, nuclear@0: NULL, NULL); nuclear@0: total_len += wc_len; nuclear@0: } nuclear@0: #else nuclear@0: png_snprintf(buf + 1, 63, "%12.12e", width); nuclear@0: total_len = 1 + png_strlen(buf + 1) + 1; nuclear@0: png_snprintf(buf + total_len, 64-total_len, "%12.12e", height); nuclear@0: total_len += png_strlen(buf + total_len); nuclear@0: #endif nuclear@0: nuclear@0: png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len); nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len); nuclear@0: } nuclear@0: #else nuclear@0: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@0: void /* PRIVATE */ nuclear@0: png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width, nuclear@0: png_charp height) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_sCAL; nuclear@0: #endif nuclear@0: png_byte buf[64]; nuclear@0: png_size_t wlen, hlen, total_len; nuclear@0: nuclear@0: png_debug(1, "in png_write_sCAL_s\n"); nuclear@0: nuclear@0: wlen = png_strlen(width); nuclear@0: hlen = png_strlen(height); nuclear@0: total_len = wlen + hlen + 2; nuclear@0: if (total_len > 64) nuclear@0: { nuclear@0: png_warning(png_ptr, "Can't write sCAL (buffer too small)"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: buf[0] = (png_byte)unit; nuclear@0: png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */ nuclear@0: png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */ nuclear@0: nuclear@0: png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len); nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len); nuclear@0: } nuclear@0: #endif nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_pHYs_SUPPORTED) nuclear@0: /* write the pHYs chunk */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, nuclear@0: png_uint_32 y_pixels_per_unit, nuclear@0: int unit_type) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_pHYs; nuclear@0: #endif nuclear@0: png_byte buf[9]; nuclear@0: nuclear@0: png_debug(1, "in png_write_pHYs\n"); nuclear@0: if (unit_type >= PNG_RESOLUTION_LAST) nuclear@0: png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); nuclear@0: nuclear@0: png_save_uint_32(buf, x_pixels_per_unit); nuclear@0: png_save_uint_32(buf + 4, y_pixels_per_unit); nuclear@0: buf[8] = (png_byte)unit_type; nuclear@0: nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_tIME_SUPPORTED) nuclear@0: /* Write the tIME chunk. Use either png_convert_from_struct_tm() nuclear@0: * or png_convert_from_time_t(), or fill in the structure yourself. nuclear@0: */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_tIME(png_structp png_ptr, png_timep mod_time) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: PNG_tIME; nuclear@0: #endif nuclear@0: png_byte buf[7]; nuclear@0: nuclear@0: png_debug(1, "in png_write_tIME\n"); nuclear@0: if (mod_time->month > 12 || mod_time->month < 1 || nuclear@0: mod_time->day > 31 || mod_time->day < 1 || nuclear@0: mod_time->hour > 23 || mod_time->second > 60) nuclear@0: { nuclear@0: png_warning(png_ptr, "Invalid time specified for tIME chunk"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: png_save_uint_16(buf, mod_time->year); nuclear@0: buf[2] = mod_time->month; nuclear@0: buf[3] = mod_time->day; nuclear@0: buf[4] = mod_time->hour; nuclear@0: buf[5] = mod_time->minute; nuclear@0: buf[6] = mod_time->second; nuclear@0: nuclear@0: png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7); nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: /* initializes the row writing capability of libpng */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_start_row(png_structp png_ptr) nuclear@0: { nuclear@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ nuclear@0: nuclear@0: /* start of interlace block */ nuclear@0: int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; nuclear@0: nuclear@0: /* offset to next interlace block */ nuclear@0: int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; nuclear@0: nuclear@0: /* start of interlace block in the y direction */ nuclear@0: int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; nuclear@0: nuclear@0: /* offset to next interlace block in the y direction */ nuclear@0: int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: png_size_t buf_size; nuclear@0: nuclear@0: png_debug(1, "in png_write_start_row\n"); nuclear@0: buf_size = (png_size_t)(PNG_ROWBYTES( nuclear@0: png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1); nuclear@0: nuclear@0: /* set up row buffer */ nuclear@0: png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, nuclear@0: (png_uint_32)buf_size); nuclear@0: png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; nuclear@0: nuclear@0: #ifndef PNG_NO_WRITE_FILTER nuclear@0: /* set up filtering buffer, if using this filter */ nuclear@0: if (png_ptr->do_filter & PNG_FILTER_SUB) nuclear@0: { nuclear@0: png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, nuclear@0: (png_uint_32)(png_ptr->rowbytes + 1)); nuclear@0: png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; nuclear@0: } nuclear@0: nuclear@0: /* We only need to keep the previous row if we are using one of these. */ nuclear@0: if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) nuclear@0: { nuclear@0: /* set up previous row buffer */ nuclear@0: png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, nuclear@0: (png_uint_32)buf_size); nuclear@0: png_memset(png_ptr->prev_row, 0, buf_size); nuclear@0: nuclear@0: if (png_ptr->do_filter & PNG_FILTER_UP) nuclear@0: { nuclear@0: png_ptr->up_row = (png_bytep)png_malloc(png_ptr, nuclear@0: (png_uint_32)(png_ptr->rowbytes + 1)); nuclear@0: png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; nuclear@0: } nuclear@0: nuclear@0: if (png_ptr->do_filter & PNG_FILTER_AVG) nuclear@0: { nuclear@0: png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, nuclear@0: (png_uint_32)(png_ptr->rowbytes + 1)); nuclear@0: png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; nuclear@0: } nuclear@0: nuclear@0: if (png_ptr->do_filter & PNG_FILTER_PAETH) nuclear@0: { nuclear@0: png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, nuclear@0: (png_uint_32)(png_ptr->rowbytes + 1)); nuclear@0: png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; nuclear@0: } nuclear@0: } nuclear@0: #endif /* PNG_NO_WRITE_FILTER */ nuclear@0: nuclear@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED nuclear@0: /* if interlaced, we need to set up width and height of pass */ nuclear@0: if (png_ptr->interlaced) nuclear@0: { nuclear@0: if (!(png_ptr->transformations & PNG_INTERLACE)) nuclear@0: { nuclear@0: png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - nuclear@0: png_pass_ystart[0]) / png_pass_yinc[0]; nuclear@0: png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - nuclear@0: png_pass_start[0]) / png_pass_inc[0]; nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: png_ptr->num_rows = png_ptr->height; nuclear@0: png_ptr->usr_width = png_ptr->width; nuclear@0: } nuclear@0: } nuclear@0: else nuclear@0: #endif nuclear@0: { nuclear@0: png_ptr->num_rows = png_ptr->height; nuclear@0: png_ptr->usr_width = png_ptr->width; nuclear@0: } nuclear@0: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@0: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@0: } nuclear@0: nuclear@0: /* Internal use only. Called when finished processing a row of data. */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_finish_row(png_structp png_ptr) nuclear@0: { nuclear@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ nuclear@0: nuclear@0: /* start of interlace block */ nuclear@0: int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; nuclear@0: nuclear@0: /* offset to next interlace block */ nuclear@0: int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; nuclear@0: nuclear@0: /* start of interlace block in the y direction */ nuclear@0: int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; nuclear@0: nuclear@0: /* offset to next interlace block in the y direction */ nuclear@0: int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: int ret; nuclear@0: nuclear@0: png_debug(1, "in png_write_finish_row\n"); nuclear@0: /* next row */ nuclear@0: png_ptr->row_number++; nuclear@0: nuclear@0: /* see if we are done */ nuclear@0: if (png_ptr->row_number < png_ptr->num_rows) nuclear@0: return; nuclear@0: nuclear@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED nuclear@0: /* if interlaced, go to next pass */ nuclear@0: if (png_ptr->interlaced) nuclear@0: { nuclear@0: png_ptr->row_number = 0; nuclear@0: if (png_ptr->transformations & PNG_INTERLACE) nuclear@0: { nuclear@0: png_ptr->pass++; nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: /* loop until we find a non-zero width or height pass */ nuclear@0: do nuclear@0: { nuclear@0: png_ptr->pass++; nuclear@0: if (png_ptr->pass >= 7) nuclear@0: break; nuclear@0: png_ptr->usr_width = (png_ptr->width + nuclear@0: png_pass_inc[png_ptr->pass] - 1 - nuclear@0: png_pass_start[png_ptr->pass]) / nuclear@0: png_pass_inc[png_ptr->pass]; nuclear@0: png_ptr->num_rows = (png_ptr->height + nuclear@0: png_pass_yinc[png_ptr->pass] - 1 - nuclear@0: png_pass_ystart[png_ptr->pass]) / nuclear@0: png_pass_yinc[png_ptr->pass]; nuclear@0: if (png_ptr->transformations & PNG_INTERLACE) nuclear@0: break; nuclear@0: } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); nuclear@0: nuclear@0: } nuclear@0: nuclear@0: /* reset the row above the image for the next pass */ nuclear@0: if (png_ptr->pass < 7) nuclear@0: { nuclear@0: if (png_ptr->prev_row != NULL) nuclear@0: png_memset(png_ptr->prev_row, 0, nuclear@0: (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels* nuclear@0: png_ptr->usr_bit_depth, png_ptr->width)) + 1); nuclear@0: return; nuclear@0: } nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: /* if we get here, we've just written the last row, so we need nuclear@0: to flush the compressor */ nuclear@0: do nuclear@0: { nuclear@0: /* tell the compressor we are done */ nuclear@0: ret = deflate(&png_ptr->zstream, Z_FINISH); nuclear@0: /* check for an error */ nuclear@0: if (ret == Z_OK) nuclear@0: { nuclear@0: /* check to see if we need more room */ nuclear@0: if (!(png_ptr->zstream.avail_out)) nuclear@0: { nuclear@0: png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); nuclear@0: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@0: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@0: } nuclear@0: } nuclear@0: else if (ret != Z_STREAM_END) nuclear@0: { nuclear@0: if (png_ptr->zstream.msg != NULL) nuclear@0: png_error(png_ptr, png_ptr->zstream.msg); nuclear@0: else nuclear@0: png_error(png_ptr, "zlib error"); nuclear@0: } nuclear@0: } while (ret != Z_STREAM_END); nuclear@0: nuclear@0: /* write any extra space */ nuclear@0: if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) nuclear@0: { nuclear@0: png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - nuclear@0: png_ptr->zstream.avail_out); nuclear@0: } nuclear@0: nuclear@0: deflateReset(&png_ptr->zstream); nuclear@0: png_ptr->zstream.data_type = Z_BINARY; nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_INTERLACING_SUPPORTED) nuclear@0: /* Pick out the correct pixels for the interlace pass. nuclear@0: * The basic idea here is to go through the row with a source nuclear@0: * pointer and a destination pointer (sp and dp), and copy the nuclear@0: * correct pixels for the pass. As the row gets compacted, nuclear@0: * sp will always be >= dp, so we should never overwrite anything. nuclear@0: * See the default: case for the easiest code to understand. nuclear@0: */ nuclear@0: void /* PRIVATE */ nuclear@0: png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) nuclear@0: { nuclear@0: #ifdef PNG_USE_LOCAL_ARRAYS nuclear@0: /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ nuclear@0: nuclear@0: /* start of interlace block */ nuclear@0: int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; nuclear@0: nuclear@0: /* offset to next interlace block */ nuclear@0: int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; nuclear@0: #endif nuclear@0: nuclear@0: png_debug(1, "in png_do_write_interlace\n"); nuclear@0: /* we don't have to do anything on the last pass (6) */ nuclear@0: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@0: if (row != NULL && row_info != NULL && pass < 6) nuclear@0: #else nuclear@0: if (pass < 6) nuclear@0: #endif nuclear@0: { nuclear@0: /* each pixel depth is handled separately */ nuclear@0: switch (row_info->pixel_depth) nuclear@0: { nuclear@0: case 1: nuclear@0: { nuclear@0: png_bytep sp; nuclear@0: png_bytep dp; nuclear@0: int shift; nuclear@0: int d; nuclear@0: int value; nuclear@0: png_uint_32 i; nuclear@0: png_uint_32 row_width = row_info->width; nuclear@0: nuclear@0: dp = row; nuclear@0: d = 0; nuclear@0: shift = 7; nuclear@0: for (i = png_pass_start[pass]; i < row_width; nuclear@0: i += png_pass_inc[pass]) nuclear@0: { nuclear@0: sp = row + (png_size_t)(i >> 3); nuclear@0: value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01; nuclear@0: d |= (value << shift); nuclear@0: nuclear@0: if (shift == 0) nuclear@0: { nuclear@0: shift = 7; nuclear@0: *dp++ = (png_byte)d; nuclear@0: d = 0; nuclear@0: } nuclear@0: else nuclear@0: shift--; nuclear@0: nuclear@0: } nuclear@0: if (shift != 7) nuclear@0: *dp = (png_byte)d; nuclear@0: break; nuclear@0: } nuclear@0: case 2: nuclear@0: { nuclear@0: png_bytep sp; nuclear@0: png_bytep dp; nuclear@0: int shift; nuclear@0: int d; nuclear@0: int value; nuclear@0: png_uint_32 i; nuclear@0: png_uint_32 row_width = row_info->width; nuclear@0: nuclear@0: dp = row; nuclear@0: shift = 6; nuclear@0: d = 0; nuclear@0: for (i = png_pass_start[pass]; i < row_width; nuclear@0: i += png_pass_inc[pass]) nuclear@0: { nuclear@0: sp = row + (png_size_t)(i >> 2); nuclear@0: value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03; nuclear@0: d |= (value << shift); nuclear@0: nuclear@0: if (shift == 0) nuclear@0: { nuclear@0: shift = 6; nuclear@0: *dp++ = (png_byte)d; nuclear@0: d = 0; nuclear@0: } nuclear@0: else nuclear@0: shift -= 2; nuclear@0: } nuclear@0: if (shift != 6) nuclear@0: *dp = (png_byte)d; nuclear@0: break; nuclear@0: } nuclear@0: case 4: nuclear@0: { nuclear@0: png_bytep sp; nuclear@0: png_bytep dp; nuclear@0: int shift; nuclear@0: int d; nuclear@0: int value; nuclear@0: png_uint_32 i; nuclear@0: png_uint_32 row_width = row_info->width; nuclear@0: nuclear@0: dp = row; nuclear@0: shift = 4; nuclear@0: d = 0; nuclear@0: for (i = png_pass_start[pass]; i < row_width; nuclear@0: i += png_pass_inc[pass]) nuclear@0: { nuclear@0: sp = row + (png_size_t)(i >> 1); nuclear@0: value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f; nuclear@0: d |= (value << shift); nuclear@0: nuclear@0: if (shift == 0) nuclear@0: { nuclear@0: shift = 4; nuclear@0: *dp++ = (png_byte)d; nuclear@0: d = 0; nuclear@0: } nuclear@0: else nuclear@0: shift -= 4; nuclear@0: } nuclear@0: if (shift != 4) nuclear@0: *dp = (png_byte)d; nuclear@0: break; nuclear@0: } nuclear@0: default: nuclear@0: { nuclear@0: png_bytep sp; nuclear@0: png_bytep dp; nuclear@0: png_uint_32 i; nuclear@0: png_uint_32 row_width = row_info->width; nuclear@0: png_size_t pixel_bytes; nuclear@0: nuclear@0: /* start at the beginning */ nuclear@0: dp = row; nuclear@0: /* find out how many bytes each pixel takes up */ nuclear@0: pixel_bytes = (row_info->pixel_depth >> 3); nuclear@0: /* loop through the row, only looking at the pixels that nuclear@0: matter */ nuclear@0: for (i = png_pass_start[pass]; i < row_width; nuclear@0: i += png_pass_inc[pass]) nuclear@0: { nuclear@0: /* find out where the original pixel is */ nuclear@0: sp = row + (png_size_t)i * pixel_bytes; nuclear@0: /* move the pixel */ nuclear@0: if (dp != sp) nuclear@0: png_memcpy(dp, sp, pixel_bytes); nuclear@0: /* next pixel */ nuclear@0: dp += pixel_bytes; nuclear@0: } nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: /* set new row width */ nuclear@0: row_info->width = (row_info->width + nuclear@0: png_pass_inc[pass] - 1 - nuclear@0: png_pass_start[pass]) / nuclear@0: png_pass_inc[pass]; nuclear@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, nuclear@0: row_info->width); nuclear@0: } nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: /* This filters the row, chooses which filter to use, if it has not already nuclear@0: * been specified by the application, and then writes the row out with the nuclear@0: * chosen filter. nuclear@0: */ nuclear@0: #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) nuclear@0: #define PNG_HISHIFT 10 nuclear@0: #define PNG_LOMASK ((png_uint_32)0xffffL) nuclear@0: #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) nuclear@0: void /* PRIVATE */ nuclear@0: png_write_find_filter(png_structp png_ptr, png_row_infop row_info) nuclear@0: { nuclear@0: png_bytep best_row; nuclear@0: #ifndef PNG_NO_WRITE_FILTER nuclear@0: png_bytep prev_row, row_buf; nuclear@0: png_uint_32 mins, bpp; nuclear@0: png_byte filter_to_do = png_ptr->do_filter; nuclear@0: png_uint_32 row_bytes = row_info->rowbytes; nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: int num_p_filters = (int)png_ptr->num_prev_filters; nuclear@0: #endif nuclear@0: nuclear@0: png_debug(1, "in png_write_find_filter\n"); nuclear@0: /* find out how many bytes offset each pixel is */ nuclear@0: bpp = (row_info->pixel_depth + 7) >> 3; nuclear@0: nuclear@0: prev_row = png_ptr->prev_row; nuclear@0: #endif nuclear@0: best_row = png_ptr->row_buf; nuclear@0: #ifndef PNG_NO_WRITE_FILTER nuclear@0: row_buf = best_row; nuclear@0: mins = PNG_MAXSUM; nuclear@0: nuclear@0: /* The prediction method we use is to find which method provides the nuclear@0: * smallest value when summing the absolute values of the distances nuclear@0: * from zero, using anything >= 128 as negative numbers. This is known nuclear@0: * as the "minimum sum of absolute differences" heuristic. Other nuclear@0: * heuristics are the "weighted minimum sum of absolute differences" nuclear@0: * (experimental and can in theory improve compression), and the "zlib nuclear@0: * predictive" method (not implemented yet), which does test compressions nuclear@0: * of lines using different filter methods, and then chooses the nuclear@0: * (series of) filter(s) that give minimum compressed data size (VERY nuclear@0: * computationally expensive). nuclear@0: * nuclear@0: * GRR 980525: consider also nuclear@0: * (1) minimum sum of absolute differences from running average (i.e., nuclear@0: * keep running sum of non-absolute differences & count of bytes) nuclear@0: * [track dispersion, too? restart average if dispersion too large?] nuclear@0: * (1b) minimum sum of absolute differences from sliding average, probably nuclear@0: * with window size <= deflate window (usually 32K) nuclear@0: * (2) minimum sum of squared differences from zero or running average nuclear@0: * (i.e., ~ root-mean-square approach) nuclear@0: */ nuclear@0: nuclear@0: nuclear@0: /* We don't need to test the 'no filter' case if this is the only filter nuclear@0: * that has been chosen, as it doesn't actually do anything to the data. nuclear@0: */ nuclear@0: if ((filter_to_do & PNG_FILTER_NONE) && nuclear@0: filter_to_do != PNG_FILTER_NONE) nuclear@0: { nuclear@0: png_bytep rp; nuclear@0: png_uint_32 sum = 0; nuclear@0: png_uint_32 i; nuclear@0: int v; nuclear@0: nuclear@0: for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) nuclear@0: { nuclear@0: v = *rp; nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: png_uint_32 sumhi, sumlo; nuclear@0: int j; nuclear@0: sumlo = sum & PNG_LOMASK; nuclear@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ nuclear@0: nuclear@0: /* Reduce the sum if we match any of the previous rows */ nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) nuclear@0: { nuclear@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* Factor in the cost of this filter (this is here for completeness, nuclear@0: * but it makes no sense to have a "cost" for the NONE filter, as nuclear@0: * it has the minimum possible computational cost - none). nuclear@0: */ nuclear@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (sumhi > PNG_HIMASK) nuclear@0: sum = PNG_MAXSUM; nuclear@0: else nuclear@0: sum = (sumhi << PNG_HISHIFT) + sumlo; nuclear@0: } nuclear@0: #endif nuclear@0: mins = sum; nuclear@0: } nuclear@0: nuclear@0: /* sub filter */ nuclear@0: if (filter_to_do == PNG_FILTER_SUB) nuclear@0: /* it's the only filter so no testing is needed */ nuclear@0: { nuclear@0: png_bytep rp, lp, dp; nuclear@0: png_uint_32 i; nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; nuclear@0: i++, rp++, dp++) nuclear@0: { nuclear@0: *dp = *rp; nuclear@0: } nuclear@0: for (lp = row_buf + 1; i < row_bytes; nuclear@0: i++, rp++, lp++, dp++) nuclear@0: { nuclear@0: *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); nuclear@0: } nuclear@0: best_row = png_ptr->sub_row; nuclear@0: } nuclear@0: nuclear@0: else if (filter_to_do & PNG_FILTER_SUB) nuclear@0: { nuclear@0: png_bytep rp, dp, lp; nuclear@0: png_uint_32 sum = 0, lmins = mins; nuclear@0: png_uint_32 i; nuclear@0: int v; nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: /* We temporarily increase the "minimum sum" by the factor we nuclear@0: * would reduce the sum of this filter, so that we can do the nuclear@0: * early exit comparison without scaling the sum each time. nuclear@0: */ nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 lmhi, lmlo; nuclear@0: lmlo = lmins & PNG_LOMASK; nuclear@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) nuclear@0: { nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (lmhi > PNG_HIMASK) nuclear@0: lmins = PNG_MAXSUM; nuclear@0: else nuclear@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; nuclear@0: i++, rp++, dp++) nuclear@0: { nuclear@0: v = *dp = *rp; nuclear@0: nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: } nuclear@0: for (lp = row_buf + 1; i < row_bytes; nuclear@0: i++, rp++, lp++, dp++) nuclear@0: { nuclear@0: v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); nuclear@0: nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: nuclear@0: if (sum > lmins) /* We are already worse, don't continue. */ nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 sumhi, sumlo; nuclear@0: sumlo = sum & PNG_LOMASK; nuclear@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) nuclear@0: { nuclear@0: sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (sumhi > PNG_HIMASK) nuclear@0: sum = PNG_MAXSUM; nuclear@0: else nuclear@0: sum = (sumhi << PNG_HISHIFT) + sumlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: if (sum < mins) nuclear@0: { nuclear@0: mins = sum; nuclear@0: best_row = png_ptr->sub_row; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* up filter */ nuclear@0: if (filter_to_do == PNG_FILTER_UP) nuclear@0: { nuclear@0: png_bytep rp, dp, pp; nuclear@0: png_uint_32 i; nuclear@0: nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, nuclear@0: pp = prev_row + 1; i < row_bytes; nuclear@0: i++, rp++, pp++, dp++) nuclear@0: { nuclear@0: *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); nuclear@0: } nuclear@0: best_row = png_ptr->up_row; nuclear@0: } nuclear@0: nuclear@0: else if (filter_to_do & PNG_FILTER_UP) nuclear@0: { nuclear@0: png_bytep rp, dp, pp; nuclear@0: png_uint_32 sum = 0, lmins = mins; nuclear@0: png_uint_32 i; nuclear@0: int v; nuclear@0: nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 lmhi, lmlo; nuclear@0: lmlo = lmins & PNG_LOMASK; nuclear@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) nuclear@0: { nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (lmhi > PNG_HIMASK) nuclear@0: lmins = PNG_MAXSUM; nuclear@0: else nuclear@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, nuclear@0: pp = prev_row + 1; i < row_bytes; i++) nuclear@0: { nuclear@0: v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); nuclear@0: nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: nuclear@0: if (sum > lmins) /* We are already worse, don't continue. */ nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 sumhi, sumlo; nuclear@0: sumlo = sum & PNG_LOMASK; nuclear@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) nuclear@0: { nuclear@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (sumhi > PNG_HIMASK) nuclear@0: sum = PNG_MAXSUM; nuclear@0: else nuclear@0: sum = (sumhi << PNG_HISHIFT) + sumlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: if (sum < mins) nuclear@0: { nuclear@0: mins = sum; nuclear@0: best_row = png_ptr->up_row; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* avg filter */ nuclear@0: if (filter_to_do == PNG_FILTER_AVG) nuclear@0: { nuclear@0: png_bytep rp, dp, pp, lp; nuclear@0: png_uint_32 i; nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, nuclear@0: pp = prev_row + 1; i < bpp; i++) nuclear@0: { nuclear@0: *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); nuclear@0: } nuclear@0: for (lp = row_buf + 1; i < row_bytes; i++) nuclear@0: { nuclear@0: *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) nuclear@0: & 0xff); nuclear@0: } nuclear@0: best_row = png_ptr->avg_row; nuclear@0: } nuclear@0: nuclear@0: else if (filter_to_do & PNG_FILTER_AVG) nuclear@0: { nuclear@0: png_bytep rp, dp, pp, lp; nuclear@0: png_uint_32 sum = 0, lmins = mins; nuclear@0: png_uint_32 i; nuclear@0: int v; nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 lmhi, lmlo; nuclear@0: lmlo = lmins & PNG_LOMASK; nuclear@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) nuclear@0: { nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (lmhi > PNG_HIMASK) nuclear@0: lmins = PNG_MAXSUM; nuclear@0: else nuclear@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, nuclear@0: pp = prev_row + 1; i < bpp; i++) nuclear@0: { nuclear@0: v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); nuclear@0: nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: } nuclear@0: for (lp = row_buf + 1; i < row_bytes; i++) nuclear@0: { nuclear@0: v = *dp++ = nuclear@0: (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); nuclear@0: nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: nuclear@0: if (sum > lmins) /* We are already worse, don't continue. */ nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 sumhi, sumlo; nuclear@0: sumlo = sum & PNG_LOMASK; nuclear@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) nuclear@0: { nuclear@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (sumhi > PNG_HIMASK) nuclear@0: sum = PNG_MAXSUM; nuclear@0: else nuclear@0: sum = (sumhi << PNG_HISHIFT) + sumlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: if (sum < mins) nuclear@0: { nuclear@0: mins = sum; nuclear@0: best_row = png_ptr->avg_row; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* Paeth filter */ nuclear@0: if (filter_to_do == PNG_FILTER_PAETH) nuclear@0: { nuclear@0: png_bytep rp, dp, pp, cp, lp; nuclear@0: png_uint_32 i; nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, nuclear@0: pp = prev_row + 1; i < bpp; i++) nuclear@0: { nuclear@0: *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); nuclear@0: } nuclear@0: nuclear@0: for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) nuclear@0: { nuclear@0: int a, b, c, pa, pb, pc, p; nuclear@0: nuclear@0: b = *pp++; nuclear@0: c = *cp++; nuclear@0: a = *lp++; nuclear@0: nuclear@0: p = b - c; nuclear@0: pc = a - c; nuclear@0: nuclear@0: #ifdef PNG_USE_ABS nuclear@0: pa = abs(p); nuclear@0: pb = abs(pc); nuclear@0: pc = abs(p + pc); nuclear@0: #else nuclear@0: pa = p < 0 ? -p : p; nuclear@0: pb = pc < 0 ? -pc : pc; nuclear@0: pc = (p + pc) < 0 ? -(p + pc) : p + pc; nuclear@0: #endif nuclear@0: nuclear@0: p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; nuclear@0: nuclear@0: *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); nuclear@0: } nuclear@0: best_row = png_ptr->paeth_row; nuclear@0: } nuclear@0: nuclear@0: else if (filter_to_do & PNG_FILTER_PAETH) nuclear@0: { nuclear@0: png_bytep rp, dp, pp, cp, lp; nuclear@0: png_uint_32 sum = 0, lmins = mins; nuclear@0: png_uint_32 i; nuclear@0: int v; nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 lmhi, lmlo; nuclear@0: lmlo = lmins & PNG_LOMASK; nuclear@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) nuclear@0: { nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (lmhi > PNG_HIMASK) nuclear@0: lmins = PNG_MAXSUM; nuclear@0: else nuclear@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, nuclear@0: pp = prev_row + 1; i < bpp; i++) nuclear@0: { nuclear@0: v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); nuclear@0: nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: } nuclear@0: nuclear@0: for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) nuclear@0: { nuclear@0: int a, b, c, pa, pb, pc, p; nuclear@0: nuclear@0: b = *pp++; nuclear@0: c = *cp++; nuclear@0: a = *lp++; nuclear@0: nuclear@0: #ifndef PNG_SLOW_PAETH nuclear@0: p = b - c; nuclear@0: pc = a - c; nuclear@0: #ifdef PNG_USE_ABS nuclear@0: pa = abs(p); nuclear@0: pb = abs(pc); nuclear@0: pc = abs(p + pc); nuclear@0: #else nuclear@0: pa = p < 0 ? -p : p; nuclear@0: pb = pc < 0 ? -pc : pc; nuclear@0: pc = (p + pc) < 0 ? -(p + pc) : p + pc; nuclear@0: #endif nuclear@0: p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; nuclear@0: #else /* PNG_SLOW_PAETH */ nuclear@0: p = a + b - c; nuclear@0: pa = abs(p - a); nuclear@0: pb = abs(p - b); nuclear@0: pc = abs(p - c); nuclear@0: if (pa <= pb && pa <= pc) nuclear@0: p = a; nuclear@0: else if (pb <= pc) nuclear@0: p = b; nuclear@0: else nuclear@0: p = c; nuclear@0: #endif /* PNG_SLOW_PAETH */ nuclear@0: nuclear@0: v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); nuclear@0: nuclear@0: sum += (v < 128) ? v : 256 - v; nuclear@0: nuclear@0: if (sum > lmins) /* We are already worse, don't continue. */ nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) nuclear@0: { nuclear@0: int j; nuclear@0: png_uint_32 sumhi, sumlo; nuclear@0: sumlo = sum & PNG_LOMASK; nuclear@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; nuclear@0: nuclear@0: for (j = 0; j < num_p_filters; j++) nuclear@0: { nuclear@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) nuclear@0: { nuclear@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> nuclear@0: PNG_WEIGHT_SHIFT; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> nuclear@0: PNG_COST_SHIFT; nuclear@0: nuclear@0: if (sumhi > PNG_HIMASK) nuclear@0: sum = PNG_MAXSUM; nuclear@0: else nuclear@0: sum = (sumhi << PNG_HISHIFT) + sumlo; nuclear@0: } nuclear@0: #endif nuclear@0: nuclear@0: if (sum < mins) nuclear@0: { nuclear@0: best_row = png_ptr->paeth_row; nuclear@0: } nuclear@0: } nuclear@0: #endif /* PNG_NO_WRITE_FILTER */ nuclear@0: /* Do the actual writing of the filtered row data from the chosen filter. */ nuclear@0: nuclear@0: png_write_filtered_row(png_ptr, best_row); nuclear@0: nuclear@0: #ifndef PNG_NO_WRITE_FILTER nuclear@0: #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) nuclear@0: /* Save the type of filter we picked this time for future calculations */ nuclear@0: if (png_ptr->num_prev_filters > 0) nuclear@0: { nuclear@0: int j; nuclear@0: for (j = 1; j < num_p_filters; j++) nuclear@0: { nuclear@0: png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; nuclear@0: } nuclear@0: png_ptr->prev_filters[j] = best_row[0]; nuclear@0: } nuclear@0: #endif nuclear@0: #endif /* PNG_NO_WRITE_FILTER */ nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* Do the actual writing of a previously filtered row. */ nuclear@0: void /* PRIVATE */ nuclear@0: png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row) nuclear@0: { nuclear@0: png_debug(1, "in png_write_filtered_row\n"); nuclear@0: png_debug1(2, "filter = %d\n", filtered_row[0]); nuclear@0: /* set up the zlib input buffer */ nuclear@0: nuclear@0: png_ptr->zstream.next_in = filtered_row; nuclear@0: png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1; nuclear@0: /* repeat until we have compressed all the data */ nuclear@0: do nuclear@0: { nuclear@0: int ret; /* return of zlib */ nuclear@0: nuclear@0: /* compress the data */ nuclear@0: ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); nuclear@0: /* check for compression errors */ nuclear@0: if (ret != Z_OK) nuclear@0: { nuclear@0: if (png_ptr->zstream.msg != NULL) nuclear@0: png_error(png_ptr, png_ptr->zstream.msg); nuclear@0: else nuclear@0: png_error(png_ptr, "zlib error"); nuclear@0: } nuclear@0: nuclear@0: /* see if it is time to write another IDAT */ nuclear@0: if (!(png_ptr->zstream.avail_out)) nuclear@0: { nuclear@0: /* write the IDAT and reset the zlib output buffer */ nuclear@0: png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); nuclear@0: png_ptr->zstream.next_out = png_ptr->zbuf; nuclear@0: png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; nuclear@0: } nuclear@0: /* repeat until all data has been compressed */ nuclear@0: } while (png_ptr->zstream.avail_in); nuclear@0: nuclear@0: /* swap the current and previous rows */ nuclear@0: if (png_ptr->prev_row != NULL) nuclear@0: { nuclear@0: png_bytep tptr; nuclear@0: nuclear@0: tptr = png_ptr->prev_row; nuclear@0: png_ptr->prev_row = png_ptr->row_buf; nuclear@0: png_ptr->row_buf = tptr; nuclear@0: } nuclear@0: nuclear@0: /* finish row - updates counters and flushes zlib if last row */ nuclear@0: png_write_finish_row(png_ptr); nuclear@0: nuclear@0: #if defined(PNG_WRITE_FLUSH_SUPPORTED) nuclear@0: png_ptr->flush_rows++; nuclear@0: nuclear@0: if (png_ptr->flush_dist > 0 && nuclear@0: png_ptr->flush_rows >= png_ptr->flush_dist) nuclear@0: { nuclear@0: png_write_flush(png_ptr); nuclear@0: } nuclear@0: #endif nuclear@0: } nuclear@0: #endif /* PNG_WRITE_SUPPORTED */