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