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