michael@0: michael@0: /* pngwutil.c - utilities to write a PNG file michael@0: * michael@0: * Last changed in libpng 1.6.2 [April 25, 2013] michael@0: * Copyright (c) 1998-2013 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: michael@0: #ifdef PNG_WRITE_SUPPORTED michael@0: michael@0: #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED michael@0: /* Place a 32-bit number into a buffer in PNG byte order. We work michael@0: * with unsigned numbers for convenience, although one supported michael@0: * ancillary chunk uses signed (two's complement) numbers. michael@0: */ michael@0: void PNGAPI michael@0: png_save_uint_32(png_bytep buf, png_uint_32 i) michael@0: { michael@0: buf[0] = (png_byte)((i >> 24) & 0xff); michael@0: buf[1] = (png_byte)((i >> 16) & 0xff); michael@0: buf[2] = (png_byte)((i >> 8) & 0xff); michael@0: buf[3] = (png_byte)(i & 0xff); michael@0: } michael@0: michael@0: /* Place a 16-bit number into a buffer in PNG byte order. michael@0: * The parameter is declared unsigned int, not png_uint_16, michael@0: * just to avoid potential problems on pre-ANSI C compilers. michael@0: */ michael@0: void PNGAPI michael@0: png_save_uint_16(png_bytep buf, unsigned int i) michael@0: { michael@0: buf[0] = (png_byte)((i >> 8) & 0xff); michael@0: buf[1] = (png_byte)(i & 0xff); michael@0: } michael@0: #endif michael@0: michael@0: /* Simple function to write the signature. If we have already written michael@0: * the magic bytes of the signature, or more likely, the PNG stream is michael@0: * being embedded into another stream and doesn't need its own signature, michael@0: * we should call png_set_sig_bytes() to tell libpng how many of the michael@0: * bytes have already been written. michael@0: */ michael@0: void PNGAPI michael@0: png_write_sig(png_structrp png_ptr) michael@0: { michael@0: png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: /* Inform the I/O callback that the signature is being written */ michael@0: png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE; michael@0: #endif michael@0: michael@0: /* Write the rest of the 8 byte signature */ michael@0: png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes], michael@0: (png_size_t)(8 - png_ptr->sig_bytes)); michael@0: michael@0: if (png_ptr->sig_bytes < 3) michael@0: png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; michael@0: } michael@0: michael@0: /* Write the start of a PNG chunk. The type is the chunk type. michael@0: * The total_length is the sum of the lengths of all the data you will be michael@0: * passing in png_write_chunk_data(). michael@0: */ michael@0: static void michael@0: png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, michael@0: png_uint_32 length) michael@0: { michael@0: png_byte buf[8]; michael@0: michael@0: #if defined(PNG_DEBUG) && (PNG_DEBUG > 0) michael@0: PNG_CSTRING_FROM_CHUNK(buf, chunk_name); michael@0: png_debug2(0, "Writing %s chunk, length = %lu", buf, (unsigned long)length); michael@0: #endif michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: /* Inform the I/O callback that the chunk header is being written. michael@0: * PNG_IO_CHUNK_HDR requires a single I/O call. michael@0: */ michael@0: png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR; michael@0: #endif michael@0: michael@0: /* Write the length and the chunk name */ michael@0: png_save_uint_32(buf, length); michael@0: png_save_uint_32(buf + 4, chunk_name); michael@0: png_write_data(png_ptr, buf, 8); michael@0: michael@0: /* Put the chunk name into png_ptr->chunk_name */ michael@0: png_ptr->chunk_name = chunk_name; michael@0: michael@0: /* Reset the crc and run it over the chunk name */ michael@0: png_reset_crc(png_ptr); michael@0: michael@0: png_calculate_crc(png_ptr, buf + 4, 4); michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: /* Inform the I/O callback that chunk data will (possibly) be written. michael@0: * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls. michael@0: */ michael@0: png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA; michael@0: #endif michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string, michael@0: png_uint_32 length) michael@0: { michael@0: png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length); michael@0: } michael@0: michael@0: /* Write the data of a PNG chunk started with png_write_chunk_header(). michael@0: * Note that multiple calls to this function are allowed, and that the michael@0: * sum of the lengths from these calls *must* add up to the total_length michael@0: * given to png_write_chunk_header(). michael@0: */ michael@0: void PNGAPI michael@0: png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, michael@0: png_size_t length) michael@0: { michael@0: /* Write the data, and run the CRC over it */ michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: if (data != NULL && length > 0) michael@0: { michael@0: png_write_data(png_ptr, data, length); michael@0: michael@0: /* Update the CRC after writing the data, michael@0: * in case that the user I/O routine alters it. michael@0: */ michael@0: png_calculate_crc(png_ptr, data, length); michael@0: } michael@0: } michael@0: michael@0: /* Finish a chunk started with png_write_chunk_header(). */ michael@0: void PNGAPI michael@0: png_write_chunk_end(png_structrp png_ptr) michael@0: { michael@0: png_byte buf[4]; michael@0: michael@0: if (png_ptr == NULL) return; michael@0: michael@0: #ifdef PNG_IO_STATE_SUPPORTED michael@0: /* Inform the I/O callback that the chunk CRC is being written. michael@0: * PNG_IO_CHUNK_CRC requires a single I/O function call. michael@0: */ michael@0: png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC; michael@0: #endif michael@0: michael@0: /* Write the crc in a single operation */ michael@0: png_save_uint_32(buf, png_ptr->crc); michael@0: michael@0: png_write_data(png_ptr, buf, (png_size_t)4); michael@0: } michael@0: michael@0: /* Write a PNG chunk all at once. The type is an array of ASCII characters michael@0: * representing the chunk name. The array must be at least 4 bytes in michael@0: * length, and does not need to be null terminated. To be safe, pass the michael@0: * pre-defined chunk names here, and if you need a new one, define it michael@0: * where the others are defined. The length is the length of the data. michael@0: * All the data must be present. If that is not possible, use the michael@0: * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() michael@0: * functions instead. michael@0: */ michael@0: static void michael@0: png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, michael@0: png_const_bytep data, png_size_t length) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* On 64 bit architectures 'length' may not fit in a png_uint_32. */ michael@0: if (length > PNG_UINT_31_MAX) michael@0: png_error(png_ptr, "length exceeds PNG maxima"); michael@0: michael@0: png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length); michael@0: png_write_chunk_data(png_ptr, data, length); michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: michael@0: /* This is the API that calls the internal function above. */ michael@0: void PNGAPI michael@0: png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string, michael@0: png_const_bytep data, png_size_t length) michael@0: { michael@0: png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data, michael@0: length); michael@0: } michael@0: michael@0: /* This is used below to find the size of an image to pass to png_deflate_claim, michael@0: * so it only needs to be accurate if the size is less than 16384 bytes (the michael@0: * point at which a lower LZ window size can be used.) michael@0: */ michael@0: static png_alloc_size_t michael@0: png_image_size(png_structrp png_ptr) michael@0: { michael@0: /* Only return sizes up to the maximum of a png_uint_32, do this by limiting michael@0: * the width and height used to 15 bits. michael@0: */ michael@0: png_uint_32 h = png_ptr->height; michael@0: michael@0: if (png_ptr->rowbytes < 32768 && h < 32768) michael@0: { michael@0: if (png_ptr->interlaced) michael@0: { michael@0: /* Interlacing makes the image larger because of the replication of michael@0: * both the filter byte and the padding to a byte boundary. michael@0: */ michael@0: png_uint_32 w = png_ptr->width; michael@0: unsigned int pd = png_ptr->pixel_depth; michael@0: png_alloc_size_t cb_base; michael@0: int pass; michael@0: michael@0: for (cb_base=0, pass=0; pass<=6; ++pass) michael@0: { michael@0: png_uint_32 pw = PNG_PASS_COLS(w, pass); michael@0: michael@0: if (pw > 0) michael@0: cb_base += (PNG_ROWBYTES(pd, pw)+1) * PNG_PASS_ROWS(h, pass); michael@0: } michael@0: michael@0: return cb_base; michael@0: } michael@0: michael@0: else michael@0: return (png_ptr->rowbytes+1) * h; michael@0: } michael@0: michael@0: else michael@0: return 0xffffffffU; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED michael@0: /* This is the code to hack the first two bytes of the deflate stream (the michael@0: * deflate header) to correct the windowBits value to match the actual data michael@0: * size. Note that the second argument is the *uncompressed* size but the michael@0: * first argument is the *compressed* data (and it must be deflate michael@0: * compressed.) michael@0: */ michael@0: static void michael@0: optimize_cmf(png_bytep data, png_alloc_size_t data_size) michael@0: { michael@0: /* Optimize the CMF field in the zlib stream. The resultant zlib stream is michael@0: * still compliant to the stream specification. michael@0: */ michael@0: if (data_size <= 16384) /* else windowBits must be 15 */ michael@0: { michael@0: unsigned int z_cmf = data[0]; /* zlib compression method and flags */ michael@0: michael@0: if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) michael@0: { michael@0: unsigned int z_cinfo; michael@0: unsigned int half_z_window_size; michael@0: michael@0: z_cinfo = z_cmf >> 4; michael@0: half_z_window_size = 1U << (z_cinfo + 7); michael@0: michael@0: if (data_size <= half_z_window_size) /* else no change */ michael@0: { michael@0: unsigned int tmp; michael@0: michael@0: do michael@0: { michael@0: half_z_window_size >>= 1; michael@0: --z_cinfo; michael@0: } michael@0: while (z_cinfo > 0 && data_size <= half_z_window_size); michael@0: michael@0: z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); michael@0: michael@0: data[0] = (png_byte)z_cmf; michael@0: tmp = data[1] & 0xe0; michael@0: tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; michael@0: data[1] = (png_byte)tmp; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #else michael@0: # define optimize_cmf(dp,dl) ((void)0) michael@0: #endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */ michael@0: michael@0: /* Initialize the compressor for the appropriate type of compression. */ michael@0: static int michael@0: png_deflate_claim(png_structrp png_ptr, png_uint_32 owner, michael@0: png_alloc_size_t data_size) michael@0: { michael@0: if (png_ptr->zowner != 0) michael@0: { michael@0: char msg[64]; michael@0: michael@0: PNG_STRING_FROM_CHUNK(msg, owner); michael@0: msg[4] = ':'; michael@0: msg[5] = ' '; michael@0: PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner); michael@0: /* So the message that results is " using zstream"; this is an michael@0: * internal error, but is very useful for debugging. i18n requirements michael@0: * are minimal. michael@0: */ michael@0: (void)png_safecat(msg, (sizeof msg), 10, " using zstream"); michael@0: # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC michael@0: png_warning(png_ptr, msg); michael@0: michael@0: /* Attempt sane error recovery */ michael@0: if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */ michael@0: { michael@0: png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT"); michael@0: return Z_STREAM_ERROR; michael@0: } michael@0: michael@0: png_ptr->zowner = 0; michael@0: # else michael@0: png_error(png_ptr, msg); michael@0: # endif michael@0: } michael@0: michael@0: { michael@0: int level = png_ptr->zlib_level; michael@0: int method = png_ptr->zlib_method; michael@0: int windowBits = png_ptr->zlib_window_bits; michael@0: int memLevel = png_ptr->zlib_mem_level; michael@0: int strategy; /* set below */ michael@0: int ret; /* zlib return code */ michael@0: michael@0: if (owner == png_IDAT) michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) michael@0: strategy = png_ptr->zlib_strategy; michael@0: michael@0: else if (png_ptr->do_filter != PNG_FILTER_NONE) michael@0: strategy = PNG_Z_DEFAULT_STRATEGY; michael@0: michael@0: else michael@0: strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY; michael@0: } michael@0: michael@0: else michael@0: { michael@0: # ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED michael@0: level = png_ptr->zlib_text_level; michael@0: method = png_ptr->zlib_text_method; michael@0: windowBits = png_ptr->zlib_text_window_bits; michael@0: memLevel = png_ptr->zlib_text_mem_level; michael@0: strategy = png_ptr->zlib_text_strategy; michael@0: # else michael@0: /* If customization is not supported the values all come from the michael@0: * IDAT values except for the strategy, which is fixed to the michael@0: * default. (This is the pre-1.6.0 behavior too, although it was michael@0: * implemented in a very different way.) michael@0: */ michael@0: strategy = Z_DEFAULT_STRATEGY; michael@0: # endif michael@0: } michael@0: michael@0: /* Adjust 'windowBits' down if larger than 'data_size'; to stop this michael@0: * happening just pass 32768 as the data_size parameter. Notice that zlib michael@0: * requires an extra 262 bytes in the window in addition to the data to be michael@0: * able to see the whole of the data, so if data_size+262 takes us to the michael@0: * next windowBits size we need to fix up the value later. (Because even michael@0: * though deflate needs the extra window, inflate does not!) michael@0: */ michael@0: if (data_size <= 16384) michael@0: { michael@0: /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to michael@0: * work round a Microsoft Visual C misbehavior which, contrary to C-90, michael@0: * widens the result of the following shift to 64-bits if (and, michael@0: * apparently, only if) it is used in a test. michael@0: */ michael@0: unsigned int half_window_size = 1U << (windowBits-1); michael@0: michael@0: while (data_size + 262 <= half_window_size) michael@0: { michael@0: half_window_size >>= 1; michael@0: --windowBits; michael@0: } michael@0: } michael@0: michael@0: /* Check against the previous initialized values, if any. */ michael@0: if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) && michael@0: (png_ptr->zlib_set_level != level || michael@0: png_ptr->zlib_set_method != method || michael@0: png_ptr->zlib_set_window_bits != windowBits || michael@0: png_ptr->zlib_set_mem_level != memLevel || michael@0: png_ptr->zlib_set_strategy != strategy)) michael@0: { michael@0: if (deflateEnd(&png_ptr->zstream) != Z_OK) michael@0: png_warning(png_ptr, "deflateEnd failed (ignored)"); michael@0: michael@0: png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED; michael@0: } michael@0: michael@0: /* For safety clear out the input and output pointers (currently zlib michael@0: * doesn't use them on Init, but it might in the future). michael@0: */ michael@0: png_ptr->zstream.next_in = NULL; michael@0: png_ptr->zstream.avail_in = 0; michael@0: png_ptr->zstream.next_out = NULL; michael@0: png_ptr->zstream.avail_out = 0; michael@0: michael@0: /* Now initialize if required, setting the new parameters, otherwise just michael@0: * to a simple reset to the previous parameters. michael@0: */ michael@0: if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) michael@0: ret = deflateReset(&png_ptr->zstream); michael@0: michael@0: else michael@0: { michael@0: ret = deflateInit2(&png_ptr->zstream, level, method, windowBits, michael@0: memLevel, strategy); michael@0: michael@0: if (ret == Z_OK) michael@0: png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; michael@0: } michael@0: michael@0: /* The return code is from either deflateReset or deflateInit2; they have michael@0: * pretty much the same set of error codes. michael@0: */ michael@0: if (ret == Z_OK) michael@0: png_ptr->zowner = owner; michael@0: michael@0: else michael@0: png_zstream_error(png_ptr, ret); michael@0: michael@0: return ret; michael@0: } michael@0: } michael@0: michael@0: /* Clean up (or trim) a linked list of compression buffers. */ michael@0: void /* PRIVATE */ michael@0: png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp) michael@0: { michael@0: png_compression_bufferp list = *listp; michael@0: michael@0: if (list != NULL) michael@0: { michael@0: *listp = NULL; michael@0: michael@0: do michael@0: { michael@0: png_compression_bufferp next = list->next; michael@0: michael@0: png_free(png_ptr, list); michael@0: list = next; michael@0: } michael@0: while (list != NULL); michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED michael@0: /* This pair of functions encapsulates the operation of (a) compressing a michael@0: * text string, and (b) issuing it later as a series of chunk data writes. michael@0: * The compression_state structure is shared context for these functions michael@0: * set up by the caller to allow access to the relevant local variables. michael@0: * michael@0: * compression_buffer (new in 1.6.0) is just a linked list of zbuffer_size michael@0: * temporary buffers. From 1.6.0 it is retained in png_struct so that it will michael@0: * be correctly freed in the event of a write error (previous implementations michael@0: * just leaked memory.) michael@0: */ michael@0: typedef struct michael@0: { michael@0: png_const_bytep input; /* The uncompressed input data */ michael@0: png_alloc_size_t input_len; /* Its length */ michael@0: png_uint_32 output_len; /* Final compressed length */ michael@0: png_byte output[1024]; /* First block of output */ michael@0: } compression_state; michael@0: michael@0: static void michael@0: png_text_compress_init(compression_state *comp, png_const_bytep input, michael@0: png_alloc_size_t input_len) michael@0: { michael@0: comp->input = input; michael@0: comp->input_len = input_len; michael@0: comp->output_len = 0; michael@0: } michael@0: michael@0: /* Compress the data in the compression state input */ michael@0: static int michael@0: png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, michael@0: compression_state *comp, png_uint_32 prefix_len) michael@0: { michael@0: int ret; michael@0: michael@0: /* To find the length of the output it is necessary to first compress the michael@0: * input, the result is buffered rather than using the two-pass algorithm michael@0: * that is used on the inflate side; deflate is assumed to be slower and a michael@0: * PNG writer is assumed to have more memory available than a PNG reader. michael@0: * michael@0: * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an michael@0: * upper limit on the output size, but it is always bigger than the input michael@0: * size so it is likely to be more efficient to use this linked-list michael@0: * approach. michael@0: */ michael@0: ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len); michael@0: michael@0: if (ret != Z_OK) michael@0: return ret; michael@0: michael@0: /* Set up the compression buffers, we need a loop here to avoid overflowing a michael@0: * uInt. Use ZLIB_IO_MAX to limit the input. The output is always limited michael@0: * by the output buffer size, so there is no need to check that. Since this michael@0: * is ANSI-C we know that an 'int', hence a uInt, is always at least 16 bits michael@0: * in size. michael@0: */ michael@0: { michael@0: png_compression_bufferp *end = &png_ptr->zbuffer_list; michael@0: png_alloc_size_t input_len = comp->input_len; /* may be zero! */ michael@0: png_uint_32 output_len; michael@0: michael@0: /* zlib updates these for us: */ michael@0: png_ptr->zstream.next_in = PNGZ_INPUT_CAST(comp->input); michael@0: png_ptr->zstream.avail_in = 0; /* Set below */ michael@0: png_ptr->zstream.next_out = comp->output; michael@0: png_ptr->zstream.avail_out = (sizeof comp->output); michael@0: michael@0: output_len = png_ptr->zstream.avail_out; michael@0: michael@0: do michael@0: { michael@0: uInt avail_in = ZLIB_IO_MAX; michael@0: michael@0: if (avail_in > input_len) michael@0: avail_in = (uInt)input_len; michael@0: michael@0: input_len -= avail_in; michael@0: michael@0: png_ptr->zstream.avail_in = avail_in; michael@0: michael@0: if (png_ptr->zstream.avail_out == 0) michael@0: { michael@0: png_compression_buffer *next; michael@0: michael@0: /* Chunk data is limited to 2^31 bytes in length, so the prefix michael@0: * length must be counted here. michael@0: */ michael@0: if (output_len + prefix_len > PNG_UINT_31_MAX) michael@0: { michael@0: ret = Z_MEM_ERROR; michael@0: break; michael@0: } michael@0: michael@0: /* Need a new (malloc'ed) buffer, but there may be one present michael@0: * already. michael@0: */ michael@0: next = *end; michael@0: if (next == NULL) michael@0: { michael@0: next = png_voidcast(png_compression_bufferp, png_malloc_base michael@0: (png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr))); michael@0: michael@0: if (next == NULL) michael@0: { michael@0: ret = Z_MEM_ERROR; michael@0: break; michael@0: } michael@0: michael@0: /* Link in this buffer (so that it will be freed later) */ michael@0: next->next = NULL; michael@0: *end = next; michael@0: } michael@0: michael@0: png_ptr->zstream.next_out = next->output; michael@0: png_ptr->zstream.avail_out = png_ptr->zbuffer_size; michael@0: output_len += png_ptr->zstream.avail_out; michael@0: michael@0: /* Move 'end' to the next buffer pointer. */ michael@0: end = &next->next; michael@0: } michael@0: michael@0: /* Compress the data */ michael@0: ret = deflate(&png_ptr->zstream, michael@0: input_len > 0 ? Z_NO_FLUSH : Z_FINISH); michael@0: michael@0: /* Claw back input data that was not consumed (because avail_in is michael@0: * reset above every time round the loop). michael@0: */ michael@0: input_len += png_ptr->zstream.avail_in; michael@0: png_ptr->zstream.avail_in = 0; /* safety */ michael@0: } michael@0: while (ret == Z_OK); michael@0: michael@0: /* There may be some space left in the last output buffer, this needs to michael@0: * be subtracted from output_len. michael@0: */ michael@0: output_len -= png_ptr->zstream.avail_out; michael@0: png_ptr->zstream.avail_out = 0; /* safety */ michael@0: comp->output_len = output_len; michael@0: michael@0: /* Now double check the output length, put in a custom message if it is michael@0: * too long. Otherwise ensure the z_stream::msg pointer is set to michael@0: * something. michael@0: */ michael@0: if (output_len + prefix_len >= PNG_UINT_31_MAX) michael@0: { michael@0: png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long"); michael@0: ret = Z_MEM_ERROR; michael@0: } michael@0: michael@0: else michael@0: png_zstream_error(png_ptr, ret); michael@0: michael@0: /* Reset zlib for another zTXt/iTXt or image data */ michael@0: png_ptr->zowner = 0; michael@0: michael@0: /* The only success case is Z_STREAM_END, input_len must be 0, if not this michael@0: * is an internal error. michael@0: */ michael@0: if (ret == Z_STREAM_END && input_len == 0) michael@0: { michael@0: /* Fix up the deflate header, if required */ michael@0: optimize_cmf(comp->output, comp->input_len); michael@0: michael@0: /* But Z_OK is returned, not Z_STREAM_END; this allows the claim michael@0: * function above to return Z_STREAM_END on an error (though it never michael@0: * does in the current versions of zlib.) michael@0: */ michael@0: return Z_OK; michael@0: } michael@0: michael@0: else michael@0: return ret; michael@0: } michael@0: } michael@0: michael@0: /* Ship the compressed text out via chunk writes */ michael@0: static void michael@0: png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp) michael@0: { michael@0: png_uint_32 output_len = comp->output_len; michael@0: png_const_bytep output = comp->output; michael@0: png_uint_32 avail = (sizeof comp->output); michael@0: png_compression_buffer *next = png_ptr->zbuffer_list; michael@0: michael@0: for (;;) michael@0: { michael@0: if (avail > output_len) michael@0: avail = output_len; michael@0: michael@0: png_write_chunk_data(png_ptr, output, avail); michael@0: michael@0: output_len -= avail; michael@0: michael@0: if (output_len == 0 || next == NULL) michael@0: break; michael@0: michael@0: avail = png_ptr->zbuffer_size; michael@0: output = next->output; michael@0: next = next->next; michael@0: } michael@0: michael@0: /* This is an internal error; 'next' must have been NULL! */ michael@0: if (output_len > 0) michael@0: png_error(png_ptr, "error writing ancillary chunked compressed data"); michael@0: } michael@0: #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ michael@0: michael@0: #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ michael@0: defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) michael@0: /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, michael@0: * and if invalid, correct the keyword rather than discarding the entire michael@0: * chunk. The PNG 1.0 specification requires keywords 1-79 characters in michael@0: * length, forbids leading or trailing whitespace, multiple internal spaces, michael@0: * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. michael@0: * michael@0: * The 'new_key' buffer must be 80 characters in size (for the keyword plus a michael@0: * trailing '\0'). If this routine returns 0 then there was no keyword, or a michael@0: * valid one could not be generated, and the caller must png_error. michael@0: */ michael@0: static png_uint_32 michael@0: png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key) michael@0: { michael@0: png_const_charp orig_key = key; michael@0: png_uint_32 key_len = 0; michael@0: int bad_character = 0; michael@0: int space = 1; michael@0: michael@0: png_debug(1, "in png_check_keyword"); michael@0: michael@0: if (key == NULL) michael@0: { michael@0: *new_key = 0; michael@0: return 0; michael@0: } michael@0: michael@0: while (*key && key_len < 79) michael@0: { michael@0: png_byte ch = (png_byte)(0xff & *key++); michael@0: michael@0: if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/)) michael@0: *new_key++ = ch, ++key_len, space = 0; michael@0: michael@0: else if (!space) michael@0: { michael@0: /* A space or an invalid character when one wasn't seen immediately michael@0: * before; output just a space. michael@0: */ michael@0: *new_key++ = 32, ++key_len, space = 1; michael@0: michael@0: /* If the character was not a space then it is invalid. */ michael@0: if (ch != 32) michael@0: bad_character = ch; michael@0: } michael@0: michael@0: else if (!bad_character) michael@0: bad_character = ch; /* just skip it, record the first error */ michael@0: } michael@0: michael@0: if (key_len > 0 && space) /* trailing space */ michael@0: { michael@0: --key_len, --new_key; michael@0: if (!bad_character) michael@0: bad_character = 32; michael@0: } michael@0: michael@0: /* Terminate the keyword */ michael@0: *new_key = 0; michael@0: michael@0: if (key_len == 0) michael@0: return 0; michael@0: michael@0: /* Try to only output one warning per keyword: */ michael@0: if (*key) /* keyword too long */ michael@0: png_warning(png_ptr, "keyword truncated"); michael@0: michael@0: else if (bad_character) michael@0: { michael@0: PNG_WARNING_PARAMETERS(p) michael@0: michael@0: png_warning_parameter(p, 1, orig_key); michael@0: png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character); michael@0: michael@0: png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'"); michael@0: } michael@0: michael@0: return key_len; michael@0: } michael@0: #endif michael@0: michael@0: /* Write the IHDR chunk, and update the png_struct with the necessary michael@0: * information. Note that the rest of this code depends upon this michael@0: * information being correct. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height, michael@0: int bit_depth, int color_type, int compression_type, int filter_type, michael@0: int interlace_type) michael@0: { michael@0: png_byte buf[13]; /* Buffer to store the IHDR info */ michael@0: michael@0: png_debug(1, "in png_write_IHDR"); michael@0: michael@0: /* Check that we have valid input data from the application info */ michael@0: switch (color_type) michael@0: { michael@0: case PNG_COLOR_TYPE_GRAY: michael@0: switch (bit_depth) michael@0: { michael@0: case 1: michael@0: case 2: michael@0: case 4: michael@0: case 8: michael@0: #ifdef PNG_WRITE_16BIT_SUPPORTED michael@0: case 16: michael@0: #endif michael@0: png_ptr->channels = 1; break; michael@0: michael@0: default: michael@0: png_error(png_ptr, michael@0: "Invalid bit depth for grayscale image"); michael@0: } michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_RGB: michael@0: #ifdef PNG_WRITE_16BIT_SUPPORTED michael@0: if (bit_depth != 8 && bit_depth != 16) michael@0: #else michael@0: if (bit_depth != 8) michael@0: #endif michael@0: png_error(png_ptr, "Invalid bit depth for RGB image"); michael@0: michael@0: png_ptr->channels = 3; michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_PALETTE: michael@0: switch (bit_depth) michael@0: { michael@0: case 1: michael@0: case 2: michael@0: case 4: michael@0: case 8: michael@0: png_ptr->channels = 1; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "Invalid bit depth for paletted image"); michael@0: } michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_GRAY_ALPHA: michael@0: if (bit_depth != 8 && bit_depth != 16) michael@0: png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); michael@0: michael@0: png_ptr->channels = 2; michael@0: break; michael@0: michael@0: case PNG_COLOR_TYPE_RGB_ALPHA: michael@0: #ifdef PNG_WRITE_16BIT_SUPPORTED michael@0: if (bit_depth != 8 && bit_depth != 16) michael@0: #else michael@0: if (bit_depth != 8) michael@0: #endif michael@0: png_error(png_ptr, "Invalid bit depth for RGBA image"); michael@0: michael@0: png_ptr->channels = 4; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "Invalid image color type specified"); michael@0: } michael@0: michael@0: if (compression_type != PNG_COMPRESSION_TYPE_BASE) michael@0: { michael@0: png_warning(png_ptr, "Invalid compression type specified"); michael@0: compression_type = PNG_COMPRESSION_TYPE_BASE; michael@0: } michael@0: michael@0: /* Write filter_method 64 (intrapixel differencing) only if michael@0: * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and michael@0: * 2. Libpng did not write a PNG signature (this filter_method is only michael@0: * used in PNG datastreams that are embedded in MNG datastreams) and michael@0: * 3. The application called png_permit_mng_features with a mask that michael@0: * included PNG_FLAG_MNG_FILTER_64 and michael@0: * 4. The filter_method is 64 and michael@0: * 5. The color_type is RGB or RGBA michael@0: */ michael@0: if ( michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && michael@0: ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && michael@0: (color_type == PNG_COLOR_TYPE_RGB || michael@0: color_type == PNG_COLOR_TYPE_RGB_ALPHA) && michael@0: (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && michael@0: #endif michael@0: filter_type != PNG_FILTER_TYPE_BASE) michael@0: { michael@0: png_warning(png_ptr, "Invalid filter type specified"); michael@0: filter_type = PNG_FILTER_TYPE_BASE; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: if (interlace_type != PNG_INTERLACE_NONE && michael@0: interlace_type != PNG_INTERLACE_ADAM7) michael@0: { michael@0: png_warning(png_ptr, "Invalid interlace type specified"); michael@0: interlace_type = PNG_INTERLACE_ADAM7; michael@0: } michael@0: #else michael@0: interlace_type=PNG_INTERLACE_NONE; michael@0: #endif michael@0: michael@0: /* Save the relevent information */ michael@0: png_ptr->bit_depth = (png_byte)bit_depth; michael@0: png_ptr->color_type = (png_byte)color_type; michael@0: png_ptr->interlaced = (png_byte)interlace_type; michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: png_ptr->filter_type = (png_byte)filter_type; michael@0: #endif michael@0: png_ptr->compression_type = (png_byte)compression_type; michael@0: png_ptr->width = width; michael@0: png_ptr->height = height; michael@0: michael@0: png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); michael@0: png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); michael@0: /* Set the usr info, so any transformations can modify it */ michael@0: png_ptr->usr_width = png_ptr->width; michael@0: png_ptr->usr_bit_depth = png_ptr->bit_depth; michael@0: png_ptr->usr_channels = png_ptr->channels; michael@0: michael@0: /* Pack the header information into the buffer */ michael@0: png_save_uint_32(buf, width); michael@0: png_save_uint_32(buf + 4, height); michael@0: buf[8] = (png_byte)bit_depth; michael@0: buf[9] = (png_byte)color_type; michael@0: buf[10] = (png_byte)compression_type; michael@0: buf[11] = (png_byte)filter_type; michael@0: buf[12] = (png_byte)interlace_type; michael@0: michael@0: /* Write the chunk */ michael@0: png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13); michael@0: michael@0: #ifdef PNG_WRITE_APNG_SUPPORTED michael@0: png_ptr->first_frame_width = width; michael@0: png_ptr->first_frame_height = height; michael@0: #endif michael@0: michael@0: if (!(png_ptr->do_filter)) michael@0: { michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || michael@0: png_ptr->bit_depth < 8) michael@0: png_ptr->do_filter = PNG_FILTER_NONE; michael@0: michael@0: else michael@0: png_ptr->do_filter = PNG_ALL_FILTERS; michael@0: } michael@0: michael@0: png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */ michael@0: } michael@0: michael@0: /* Write the palette. We are careful not to trust png_color to be in the michael@0: * correct order for PNG, so people can redefine it to any convenient michael@0: * structure. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_write_PLTE(png_structrp png_ptr, png_const_colorp palette, michael@0: png_uint_32 num_pal) michael@0: { michael@0: png_uint_32 i; michael@0: png_const_colorp pal_ptr; michael@0: png_byte buf[3]; michael@0: michael@0: png_debug(1, "in png_write_PLTE"); michael@0: michael@0: if (( michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && michael@0: #endif michael@0: num_pal == 0) || num_pal > 256) michael@0: { michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: png_error(png_ptr, "Invalid number of colors in palette"); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_warning(png_ptr, "Invalid number of colors in palette"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) michael@0: { michael@0: png_warning(png_ptr, michael@0: "Ignoring request to write a PLTE chunk in grayscale PNG"); michael@0: michael@0: return; michael@0: } michael@0: michael@0: png_ptr->num_palette = (png_uint_16)num_pal; michael@0: png_debug1(3, "num_palette = %d", png_ptr->num_palette); michael@0: michael@0: png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3)); michael@0: #ifdef PNG_POINTER_INDEXING_SUPPORTED michael@0: michael@0: for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) michael@0: { michael@0: buf[0] = pal_ptr->red; michael@0: buf[1] = pal_ptr->green; michael@0: buf[2] = pal_ptr->blue; michael@0: png_write_chunk_data(png_ptr, buf, (png_size_t)3); michael@0: } michael@0: michael@0: #else michael@0: /* This is a little slower but some buggy compilers need to do this michael@0: * instead michael@0: */ michael@0: pal_ptr=palette; michael@0: michael@0: for (i = 0; i < num_pal; i++) michael@0: { michael@0: buf[0] = pal_ptr[i].red; michael@0: buf[1] = pal_ptr[i].green; michael@0: buf[2] = pal_ptr[i].blue; michael@0: png_write_chunk_data(png_ptr, buf, (png_size_t)3); michael@0: } michael@0: michael@0: #endif michael@0: png_write_chunk_end(png_ptr); michael@0: png_ptr->mode |= PNG_HAVE_PLTE; michael@0: } michael@0: michael@0: /* This is similar to png_text_compress, above, except that it does not require michael@0: * all of the data at once and, instead of buffering the compressed result, michael@0: * writes it as IDAT chunks. Unlike png_text_compress it *can* png_error out michael@0: * because it calls the write interface. As a result it does its own error michael@0: * reporting and does not return an error code. In the event of error it will michael@0: * just call png_error. The input data length may exceed 32-bits. The 'flush' michael@0: * parameter is exactly the same as that to deflate, with the following michael@0: * meanings: michael@0: * michael@0: * Z_NO_FLUSH: normal incremental output of compressed data michael@0: * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush michael@0: * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up michael@0: * michael@0: * The routine manages the acquire and release of the png_ptr->zstream by michael@0: * checking and (at the end) clearing png_ptr->zowner, it does some sanity michael@0: * checks on the 'mode' flags while doing this. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_compress_IDAT(png_structrp png_ptr, png_const_bytep input, michael@0: png_alloc_size_t input_len, int flush) michael@0: { michael@0: if (png_ptr->zowner != png_IDAT) michael@0: { michael@0: /* First time. Ensure we have a temporary buffer for compression and michael@0: * trim the buffer list if it has more than one entry to free memory. michael@0: * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been michael@0: * created at this point, but the check here is quick and safe. michael@0: */ michael@0: if (png_ptr->zbuffer_list == NULL) michael@0: { michael@0: png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp, michael@0: png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr))); michael@0: png_ptr->zbuffer_list->next = NULL; michael@0: } michael@0: michael@0: else michael@0: png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next); michael@0: michael@0: /* It is a terminal error if we can't claim the zstream. */ michael@0: if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK) michael@0: png_error(png_ptr, png_ptr->zstream.msg); michael@0: michael@0: /* The output state is maintained in png_ptr->zstream, so it must be michael@0: * initialized here after the claim. michael@0: */ michael@0: png_ptr->zstream.next_out = png_ptr->zbuffer_list->output; michael@0: png_ptr->zstream.avail_out = png_ptr->zbuffer_size; michael@0: } michael@0: michael@0: /* Now loop reading and writing until all the input is consumed or an error michael@0: * terminates the operation. The _out values are maintained across calls to michael@0: * this function, but the input must be reset each time. michael@0: */ michael@0: png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); michael@0: png_ptr->zstream.avail_in = 0; /* set below */ michael@0: for (;;) michael@0: { michael@0: int ret; michael@0: michael@0: /* INPUT: from the row data */ michael@0: uInt avail = ZLIB_IO_MAX; michael@0: michael@0: if (avail > input_len) michael@0: avail = (uInt)input_len; /* safe because of the check */ michael@0: michael@0: png_ptr->zstream.avail_in = avail; michael@0: input_len -= avail; michael@0: michael@0: ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush); michael@0: michael@0: /* Include as-yet unconsumed input */ michael@0: input_len += png_ptr->zstream.avail_in; michael@0: png_ptr->zstream.avail_in = 0; michael@0: michael@0: /* OUTPUT: write complete IDAT chunks when avail_out drops to zero, note michael@0: * that these two zstream fields are preserved across the calls, therefore michael@0: * there is no need to set these up on entry to the loop. michael@0: */ michael@0: if (png_ptr->zstream.avail_out == 0) michael@0: { michael@0: png_bytep data = png_ptr->zbuffer_list->output; michael@0: uInt size = png_ptr->zbuffer_size; michael@0: michael@0: /* Write an IDAT containing the data then reset the buffer. The michael@0: * first IDAT may need deflate header optimization. michael@0: */ michael@0: # ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED michael@0: if (!(png_ptr->mode & PNG_HAVE_IDAT) && michael@0: png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) michael@0: optimize_cmf(data, png_image_size(png_ptr)); michael@0: # endif michael@0: michael@0: # ifdef PNG_WRITE_APNG_SUPPORTED michael@0: if (png_ptr->num_frames_written == 0) michael@0: # endif michael@0: png_write_complete_chunk(png_ptr, png_IDAT, data, size); michael@0: # ifdef PNG_WRITE_APNG_SUPPORTED michael@0: else michael@0: png_write_fdAT(png_ptr, data, size); michael@0: # endif /* PNG_WRITE_APNG_SUPPORTED */ michael@0: michael@0: png_ptr->mode |= PNG_HAVE_IDAT; michael@0: michael@0: png_ptr->zstream.next_out = data; michael@0: png_ptr->zstream.avail_out = size; michael@0: michael@0: /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with michael@0: * the same flush parameter until it has finished output, for NO_FLUSH michael@0: * it doesn't matter. michael@0: */ michael@0: if (ret == Z_OK && flush != Z_NO_FLUSH) michael@0: continue; michael@0: } michael@0: michael@0: /* The order of these checks doesn't matter much; it just effect which michael@0: * possible error might be detected if multiple things go wrong at once. michael@0: */ michael@0: if (ret == Z_OK) /* most likely return code! */ michael@0: { michael@0: /* If all the input has been consumed then just return. If Z_FINISH michael@0: * was used as the flush parameter something has gone wrong if we get michael@0: * here. michael@0: */ michael@0: if (input_len == 0) michael@0: { michael@0: if (flush == Z_FINISH) michael@0: png_error(png_ptr, "Z_OK on Z_FINISH with output space"); michael@0: michael@0: return; michael@0: } michael@0: } michael@0: michael@0: else if (ret == Z_STREAM_END && flush == Z_FINISH) michael@0: { michael@0: /* This is the end of the IDAT data; any pending output must be michael@0: * flushed. For small PNG files we may still be at the beginning. michael@0: */ michael@0: png_bytep data = png_ptr->zbuffer_list->output; michael@0: uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out; michael@0: michael@0: # ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED michael@0: if (!(png_ptr->mode & PNG_HAVE_IDAT) && michael@0: png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) michael@0: optimize_cmf(data, png_image_size(png_ptr)); michael@0: # endif michael@0: michael@0: # ifdef PNG_WRITE_APNG_SUPPORTED michael@0: if (png_ptr->num_frames_written == 0) michael@0: # endif michael@0: png_write_complete_chunk(png_ptr, png_IDAT, data, size); michael@0: # ifdef PNG_WRITE_APNG_SUPPORTED michael@0: else michael@0: png_write_fdAT(png_ptr, data, size); michael@0: # endif /* PNG_WRITE_APNG_SUPPORTED */ michael@0: michael@0: png_ptr->zstream.avail_out = 0; michael@0: png_ptr->zstream.next_out = NULL; michael@0: png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT; michael@0: michael@0: png_ptr->zowner = 0; /* Release the stream */ michael@0: return; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This is an error condition. */ michael@0: png_zstream_error(png_ptr, ret); michael@0: png_error(png_ptr, png_ptr->zstream.msg); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Write an IEND chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_IEND(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_write_IEND"); michael@0: michael@0: png_write_complete_chunk(png_ptr, png_IEND, NULL, (png_size_t)0); michael@0: png_ptr->mode |= PNG_HAVE_IEND; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_gAMA_SUPPORTED michael@0: /* Write a gAMA chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma) michael@0: { michael@0: png_byte buf[4]; michael@0: michael@0: png_debug(1, "in png_write_gAMA"); michael@0: michael@0: /* file_gamma is saved in 1/100,000ths */ michael@0: png_save_uint_32(buf, (png_uint_32)file_gamma); michael@0: png_write_complete_chunk(png_ptr, png_gAMA, buf, (png_size_t)4); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_sRGB_SUPPORTED michael@0: /* Write a sRGB chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_sRGB(png_structrp png_ptr, int srgb_intent) michael@0: { michael@0: png_byte buf[1]; michael@0: michael@0: png_debug(1, "in png_write_sRGB"); michael@0: michael@0: if (srgb_intent >= PNG_sRGB_INTENT_LAST) michael@0: png_warning(png_ptr, michael@0: "Invalid sRGB rendering intent specified"); michael@0: michael@0: buf[0]=(png_byte)srgb_intent; michael@0: png_write_complete_chunk(png_ptr, png_sRGB, buf, (png_size_t)1); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_iCCP_SUPPORTED michael@0: /* Write an iCCP chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_iCCP(png_structrp png_ptr, png_const_charp name, michael@0: png_const_bytep profile) michael@0: { michael@0: png_uint_32 name_len; michael@0: png_uint_32 profile_len; michael@0: png_byte new_name[81]; /* 1 byte for the compression byte */ michael@0: compression_state comp; michael@0: michael@0: png_debug(1, "in png_write_iCCP"); michael@0: michael@0: /* These are all internal problems: the profile should have been checked michael@0: * before when it was stored. michael@0: */ michael@0: if (profile == NULL) michael@0: png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */ michael@0: michael@0: profile_len = png_get_uint_32(profile); michael@0: michael@0: if (profile_len < 132) michael@0: png_error(png_ptr, "ICC profile too short"); michael@0: michael@0: if (profile_len & 0x03) michael@0: png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)"); michael@0: michael@0: { michael@0: png_uint_32 embedded_profile_len = png_get_uint_32(profile); michael@0: michael@0: if (profile_len != embedded_profile_len) michael@0: png_error(png_ptr, "Profile length does not match profile"); michael@0: } michael@0: michael@0: name_len = png_check_keyword(png_ptr, name, new_name); michael@0: michael@0: if (name_len == 0) michael@0: png_error(png_ptr, "iCCP: invalid keyword"); michael@0: michael@0: new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE; michael@0: michael@0: /* Make sure we include the NULL after the name and the compression type */ michael@0: ++name_len; michael@0: michael@0: png_text_compress_init(&comp, profile, profile_len); michael@0: michael@0: /* Allow for keyword terminator and compression byte */ michael@0: if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK) michael@0: png_error(png_ptr, png_ptr->zstream.msg); michael@0: michael@0: png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len); michael@0: michael@0: png_write_chunk_data(png_ptr, new_name, name_len); michael@0: michael@0: png_write_compressed_data_out(png_ptr, &comp); michael@0: michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_sPLT_SUPPORTED michael@0: /* Write a sPLT chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette) michael@0: { michael@0: png_uint_32 name_len; michael@0: png_byte new_name[80]; michael@0: png_byte entrybuf[10]; michael@0: png_size_t entry_size = (spalette->depth == 8 ? 6 : 10); michael@0: png_size_t palette_size = entry_size * spalette->nentries; michael@0: png_sPLT_entryp ep; michael@0: #ifndef PNG_POINTER_INDEXING_SUPPORTED michael@0: int i; michael@0: #endif michael@0: michael@0: png_debug(1, "in png_write_sPLT"); michael@0: michael@0: name_len = png_check_keyword(png_ptr, spalette->name, new_name); michael@0: michael@0: if (name_len == 0) michael@0: png_error(png_ptr, "sPLT: invalid keyword"); michael@0: michael@0: /* Make sure we include the NULL after the name */ michael@0: png_write_chunk_header(png_ptr, png_sPLT, michael@0: (png_uint_32)(name_len + 2 + palette_size)); michael@0: michael@0: png_write_chunk_data(png_ptr, (png_bytep)new_name, michael@0: (png_size_t)(name_len + 1)); michael@0: michael@0: png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1); michael@0: michael@0: /* Loop through each palette entry, writing appropriately */ michael@0: #ifdef PNG_POINTER_INDEXING_SUPPORTED michael@0: for (ep = spalette->entries; epentries + spalette->nentries; ep++) michael@0: { michael@0: if (spalette->depth == 8) michael@0: { michael@0: entrybuf[0] = (png_byte)ep->red; michael@0: entrybuf[1] = (png_byte)ep->green; michael@0: entrybuf[2] = (png_byte)ep->blue; michael@0: entrybuf[3] = (png_byte)ep->alpha; michael@0: png_save_uint_16(entrybuf + 4, ep->frequency); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_save_uint_16(entrybuf + 0, ep->red); michael@0: png_save_uint_16(entrybuf + 2, ep->green); michael@0: png_save_uint_16(entrybuf + 4, ep->blue); michael@0: png_save_uint_16(entrybuf + 6, ep->alpha); michael@0: png_save_uint_16(entrybuf + 8, ep->frequency); michael@0: } michael@0: michael@0: png_write_chunk_data(png_ptr, entrybuf, entry_size); michael@0: } michael@0: #else michael@0: ep=spalette->entries; michael@0: for (i = 0; i>spalette->nentries; i++) michael@0: { michael@0: if (spalette->depth == 8) michael@0: { michael@0: entrybuf[0] = (png_byte)ep[i].red; michael@0: entrybuf[1] = (png_byte)ep[i].green; michael@0: entrybuf[2] = (png_byte)ep[i].blue; michael@0: entrybuf[3] = (png_byte)ep[i].alpha; michael@0: png_save_uint_16(entrybuf + 4, ep[i].frequency); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_save_uint_16(entrybuf + 0, ep[i].red); michael@0: png_save_uint_16(entrybuf + 2, ep[i].green); michael@0: png_save_uint_16(entrybuf + 4, ep[i].blue); michael@0: png_save_uint_16(entrybuf + 6, ep[i].alpha); michael@0: png_save_uint_16(entrybuf + 8, ep[i].frequency); michael@0: } michael@0: michael@0: png_write_chunk_data(png_ptr, entrybuf, entry_size); michael@0: } michael@0: #endif michael@0: michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_sBIT_SUPPORTED michael@0: /* Write the sBIT chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type) michael@0: { michael@0: png_byte buf[4]; michael@0: png_size_t size; michael@0: michael@0: png_debug(1, "in png_write_sBIT"); michael@0: michael@0: /* Make sure we don't depend upon the order of PNG_COLOR_8 */ michael@0: if (color_type & PNG_COLOR_MASK_COLOR) michael@0: { michael@0: png_byte maxbits; michael@0: michael@0: maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : michael@0: png_ptr->usr_bit_depth); michael@0: michael@0: if (sbit->red == 0 || sbit->red > maxbits || michael@0: sbit->green == 0 || sbit->green > maxbits || michael@0: sbit->blue == 0 || sbit->blue > maxbits) michael@0: { michael@0: png_warning(png_ptr, "Invalid sBIT depth specified"); michael@0: return; michael@0: } michael@0: michael@0: buf[0] = sbit->red; michael@0: buf[1] = sbit->green; michael@0: buf[2] = sbit->blue; michael@0: size = 3; michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) michael@0: { michael@0: png_warning(png_ptr, "Invalid sBIT depth specified"); michael@0: return; michael@0: } michael@0: michael@0: buf[0] = sbit->gray; michael@0: size = 1; michael@0: } michael@0: michael@0: if (color_type & PNG_COLOR_MASK_ALPHA) michael@0: { michael@0: if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) michael@0: { michael@0: png_warning(png_ptr, "Invalid sBIT depth specified"); michael@0: return; michael@0: } michael@0: michael@0: buf[size++] = sbit->alpha; michael@0: } michael@0: michael@0: png_write_complete_chunk(png_ptr, png_sBIT, buf, size); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_cHRM_SUPPORTED michael@0: /* Write the cHRM chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_cHRM_fixed(png_structrp png_ptr, const png_xy *xy) michael@0: { michael@0: png_byte buf[32]; michael@0: michael@0: png_debug(1, "in png_write_cHRM"); michael@0: michael@0: /* Each value is saved in 1/100,000ths */ michael@0: png_save_int_32(buf, xy->whitex); michael@0: png_save_int_32(buf + 4, xy->whitey); michael@0: michael@0: png_save_int_32(buf + 8, xy->redx); michael@0: png_save_int_32(buf + 12, xy->redy); michael@0: michael@0: png_save_int_32(buf + 16, xy->greenx); michael@0: png_save_int_32(buf + 20, xy->greeny); michael@0: michael@0: png_save_int_32(buf + 24, xy->bluex); michael@0: png_save_int_32(buf + 28, xy->bluey); michael@0: michael@0: png_write_complete_chunk(png_ptr, png_cHRM, buf, 32); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_tRNS_SUPPORTED michael@0: /* Write the tRNS chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha, michael@0: png_const_color_16p tran, int num_trans, int color_type) michael@0: { michael@0: png_byte buf[6]; michael@0: michael@0: png_debug(1, "in png_write_tRNS"); michael@0: michael@0: if (color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) michael@0: { michael@0: png_app_warning(png_ptr, michael@0: "Invalid number of transparent colors specified"); michael@0: return; michael@0: } michael@0: michael@0: /* Write the chunk out as it is */ michael@0: png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, michael@0: (png_size_t)num_trans); michael@0: } michael@0: michael@0: else if (color_type == PNG_COLOR_TYPE_GRAY) michael@0: { michael@0: /* One 16 bit value */ michael@0: if (tran->gray >= (1 << png_ptr->bit_depth)) michael@0: { michael@0: png_app_warning(png_ptr, michael@0: "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); michael@0: michael@0: return; michael@0: } michael@0: michael@0: png_save_uint_16(buf, tran->gray); michael@0: png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2); michael@0: } michael@0: michael@0: else if (color_type == PNG_COLOR_TYPE_RGB) michael@0: { michael@0: /* Three 16 bit values */ michael@0: png_save_uint_16(buf, tran->red); michael@0: png_save_uint_16(buf + 2, tran->green); michael@0: png_save_uint_16(buf + 4, tran->blue); michael@0: #ifdef PNG_WRITE_16BIT_SUPPORTED michael@0: if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) michael@0: #else michael@0: if (buf[0] | buf[2] | buf[4]) michael@0: #endif michael@0: { michael@0: png_app_warning(png_ptr, michael@0: "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); michael@0: return; michael@0: } michael@0: michael@0: png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_app_warning(png_ptr, "Can't write tRNS with an alpha channel"); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_bKGD_SUPPORTED michael@0: /* Write the background chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type) michael@0: { michael@0: png_byte buf[6]; michael@0: michael@0: png_debug(1, "in png_write_bKGD"); michael@0: michael@0: if (color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: if ( michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: (png_ptr->num_palette || michael@0: (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && michael@0: #endif michael@0: back->index >= png_ptr->num_palette) michael@0: { michael@0: png_warning(png_ptr, "Invalid background palette index"); michael@0: return; michael@0: } michael@0: michael@0: buf[0] = back->index; michael@0: png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1); michael@0: } michael@0: michael@0: else if (color_type & PNG_COLOR_MASK_COLOR) michael@0: { michael@0: png_save_uint_16(buf, back->red); michael@0: png_save_uint_16(buf + 2, back->green); michael@0: png_save_uint_16(buf + 4, back->blue); michael@0: #ifdef PNG_WRITE_16BIT_SUPPORTED michael@0: if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) michael@0: #else michael@0: if (buf[0] | buf[2] | buf[4]) michael@0: #endif michael@0: { michael@0: png_warning(png_ptr, michael@0: "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); michael@0: michael@0: return; michael@0: } michael@0: michael@0: png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6); michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (back->gray >= (1 << png_ptr->bit_depth)) michael@0: { michael@0: png_warning(png_ptr, michael@0: "Ignoring attempt to write bKGD chunk out-of-range for bit_depth"); michael@0: michael@0: return; michael@0: } michael@0: michael@0: png_save_uint_16(buf, back->gray); michael@0: png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)2); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_hIST_SUPPORTED michael@0: /* Write the histogram */ michael@0: void /* PRIVATE */ michael@0: png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist) michael@0: { michael@0: int i; michael@0: png_byte buf[3]; michael@0: michael@0: png_debug(1, "in png_write_hIST"); michael@0: michael@0: if (num_hist > (int)png_ptr->num_palette) michael@0: { michael@0: png_debug2(3, "num_hist = %d, num_palette = %d", num_hist, michael@0: png_ptr->num_palette); michael@0: michael@0: png_warning(png_ptr, "Invalid number of histogram entries specified"); michael@0: return; michael@0: } michael@0: michael@0: png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2)); michael@0: michael@0: for (i = 0; i < num_hist; i++) michael@0: { michael@0: png_save_uint_16(buf, hist[i]); michael@0: png_write_chunk_data(png_ptr, buf, (png_size_t)2); michael@0: } michael@0: michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_tEXt_SUPPORTED michael@0: /* Write a tEXt chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, michael@0: png_size_t text_len) michael@0: { michael@0: png_uint_32 key_len; michael@0: png_byte new_key[80]; michael@0: michael@0: png_debug(1, "in png_write_tEXt"); michael@0: michael@0: key_len = png_check_keyword(png_ptr, key, new_key); michael@0: michael@0: if (key_len == 0) michael@0: png_error(png_ptr, "tEXt: invalid keyword"); michael@0: michael@0: if (text == NULL || *text == '\0') michael@0: text_len = 0; michael@0: michael@0: else michael@0: text_len = strlen(text); michael@0: michael@0: if (text_len > PNG_UINT_31_MAX - (key_len+1)) michael@0: png_error(png_ptr, "tEXt: text too long"); michael@0: michael@0: /* Make sure we include the 0 after the key */ michael@0: png_write_chunk_header(png_ptr, png_tEXt, michael@0: (png_uint_32)/*checked above*/(key_len + text_len + 1)); michael@0: /* michael@0: * We leave it to the application to meet PNG-1.0 requirements on the michael@0: * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of michael@0: * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. michael@0: * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. michael@0: */ michael@0: png_write_chunk_data(png_ptr, new_key, key_len + 1); michael@0: michael@0: if (text_len) michael@0: png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len); michael@0: michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_zTXt_SUPPORTED michael@0: /* Write a compressed text chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, michael@0: png_size_t text_len, int compression) michael@0: { michael@0: png_uint_32 key_len; michael@0: png_byte new_key[81]; michael@0: compression_state comp; michael@0: michael@0: png_debug(1, "in png_write_zTXt"); michael@0: PNG_UNUSED(text_len) /* Always use strlen */ michael@0: michael@0: if (compression == PNG_TEXT_COMPRESSION_NONE) michael@0: { michael@0: png_write_tEXt(png_ptr, key, text, 0); michael@0: return; michael@0: } michael@0: michael@0: if (compression != PNG_TEXT_COMPRESSION_zTXt) michael@0: png_error(png_ptr, "zTXt: invalid compression type"); michael@0: michael@0: key_len = png_check_keyword(png_ptr, key, new_key); michael@0: michael@0: if (key_len == 0) michael@0: png_error(png_ptr, "zTXt: invalid keyword"); michael@0: michael@0: /* Add the compression method and 1 for the keyword separator. */ michael@0: new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE; michael@0: ++key_len; michael@0: michael@0: /* Compute the compressed data; do it now for the length */ michael@0: png_text_compress_init(&comp, (png_const_bytep)text, michael@0: text == NULL ? 0 : strlen(text)); michael@0: michael@0: if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK) michael@0: png_error(png_ptr, png_ptr->zstream.msg); michael@0: michael@0: /* Write start of chunk */ michael@0: png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len); michael@0: michael@0: /* Write key */ michael@0: png_write_chunk_data(png_ptr, new_key, key_len); michael@0: michael@0: /* Write the compressed data */ michael@0: png_write_compressed_data_out(png_ptr, &comp); michael@0: michael@0: /* Close the chunk */ michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_iTXt_SUPPORTED michael@0: /* Write an iTXt chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key, michael@0: png_const_charp lang, png_const_charp lang_key, png_const_charp text) michael@0: { michael@0: png_uint_32 key_len, prefix_len; michael@0: png_size_t lang_len, lang_key_len; michael@0: png_byte new_key[82]; michael@0: compression_state comp; michael@0: michael@0: png_debug(1, "in png_write_iTXt"); michael@0: michael@0: key_len = png_check_keyword(png_ptr, key, new_key); michael@0: michael@0: if (key_len == 0) michael@0: png_error(png_ptr, "iTXt: invalid keyword"); michael@0: michael@0: /* Set the compression flag */ michael@0: switch (compression) michael@0: { michael@0: case PNG_ITXT_COMPRESSION_NONE: michael@0: case PNG_TEXT_COMPRESSION_NONE: michael@0: compression = new_key[++key_len] = 0; /* no compression */ michael@0: break; michael@0: michael@0: case PNG_TEXT_COMPRESSION_zTXt: michael@0: case PNG_ITXT_COMPRESSION_zTXt: michael@0: compression = new_key[++key_len] = 1; /* compressed */ michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "iTXt: invalid compression"); michael@0: } michael@0: michael@0: new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE; michael@0: ++key_len; /* for the keywod separator */ michael@0: michael@0: /* We leave it to the application to meet PNG-1.0 requirements on the michael@0: * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of michael@0: * any non-Latin-1 characters except for NEWLINE. ISO PNG, however, michael@0: * specifies that the text is UTF-8 and this really doesn't require any michael@0: * checking. michael@0: * michael@0: * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. michael@0: * michael@0: * TODO: validate the language tag correctly (see the spec.) michael@0: */ michael@0: if (lang == NULL) lang = ""; /* empty language is valid */ michael@0: lang_len = strlen(lang)+1; michael@0: if (lang_key == NULL) lang_key = ""; /* may be empty */ michael@0: lang_key_len = strlen(lang_key)+1; michael@0: if (text == NULL) text = ""; /* may be empty */ michael@0: michael@0: prefix_len = key_len; michael@0: if (lang_len > PNG_UINT_31_MAX-prefix_len) michael@0: prefix_len = PNG_UINT_31_MAX; michael@0: else michael@0: prefix_len = (png_uint_32)(prefix_len + lang_len); michael@0: michael@0: if (lang_key_len > PNG_UINT_31_MAX-prefix_len) michael@0: prefix_len = PNG_UINT_31_MAX; michael@0: else michael@0: prefix_len = (png_uint_32)(prefix_len + lang_key_len); michael@0: michael@0: png_text_compress_init(&comp, (png_const_bytep)text, strlen(text)); michael@0: michael@0: if (compression) michael@0: { michael@0: if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK) michael@0: png_error(png_ptr, png_ptr->zstream.msg); michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (comp.input_len > PNG_UINT_31_MAX-prefix_len) michael@0: png_error(png_ptr, "iTXt: uncompressed text too long"); michael@0: michael@0: /* So the string will fit in a chunk: */ michael@0: comp.output_len = (png_uint_32)/*SAFE*/comp.input_len; michael@0: } michael@0: michael@0: png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len); michael@0: michael@0: png_write_chunk_data(png_ptr, new_key, key_len); michael@0: michael@0: png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len); michael@0: michael@0: png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len); michael@0: michael@0: if (compression) michael@0: png_write_compressed_data_out(png_ptr, &comp); michael@0: michael@0: else michael@0: png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.input_len); michael@0: michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_oFFs_SUPPORTED michael@0: /* Write the oFFs chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset, michael@0: int unit_type) michael@0: { michael@0: png_byte buf[9]; michael@0: michael@0: png_debug(1, "in png_write_oFFs"); michael@0: michael@0: if (unit_type >= PNG_OFFSET_LAST) michael@0: png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); michael@0: michael@0: png_save_int_32(buf, x_offset); michael@0: png_save_int_32(buf + 4, y_offset); michael@0: buf[8] = (png_byte)unit_type; michael@0: michael@0: png_write_complete_chunk(png_ptr, png_oFFs, buf, (png_size_t)9); michael@0: } michael@0: #endif michael@0: #ifdef PNG_WRITE_pCAL_SUPPORTED michael@0: /* Write the pCAL chunk (described in the PNG extensions document) */ michael@0: void /* PRIVATE */ michael@0: png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0, michael@0: png_int_32 X1, int type, int nparams, png_const_charp units, michael@0: png_charpp params) michael@0: { michael@0: png_uint_32 purpose_len; michael@0: png_size_t units_len, total_len; michael@0: png_size_tp params_len; michael@0: png_byte buf[10]; michael@0: png_byte new_purpose[80]; michael@0: int i; michael@0: michael@0: png_debug1(1, "in png_write_pCAL (%d parameters)", nparams); michael@0: michael@0: if (type >= PNG_EQUATION_LAST) michael@0: png_error(png_ptr, "Unrecognized equation type for pCAL chunk"); michael@0: michael@0: purpose_len = png_check_keyword(png_ptr, purpose, new_purpose); michael@0: michael@0: if (purpose_len == 0) michael@0: png_error(png_ptr, "pCAL: invalid keyword"); michael@0: michael@0: ++purpose_len; /* terminator */ michael@0: michael@0: png_debug1(3, "pCAL purpose length = %d", (int)purpose_len); michael@0: units_len = strlen(units) + (nparams == 0 ? 0 : 1); michael@0: png_debug1(3, "pCAL units length = %d", (int)units_len); michael@0: total_len = purpose_len + units_len + 10; michael@0: michael@0: params_len = (png_size_tp)png_malloc(png_ptr, michael@0: (png_alloc_size_t)(nparams * (sizeof (png_size_t)))); michael@0: michael@0: /* Find the length of each parameter, making sure we don't count the michael@0: * null terminator for the last parameter. michael@0: */ michael@0: for (i = 0; i < nparams; i++) michael@0: { michael@0: params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1); michael@0: png_debug2(3, "pCAL parameter %d length = %lu", i, michael@0: (unsigned long)params_len[i]); michael@0: total_len += params_len[i]; michael@0: } michael@0: michael@0: png_debug1(3, "pCAL total length = %d", (int)total_len); michael@0: png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len); michael@0: png_write_chunk_data(png_ptr, new_purpose, purpose_len); michael@0: png_save_int_32(buf, X0); michael@0: png_save_int_32(buf + 4, X1); michael@0: buf[8] = (png_byte)type; michael@0: buf[9] = (png_byte)nparams; michael@0: png_write_chunk_data(png_ptr, buf, (png_size_t)10); michael@0: png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len); michael@0: michael@0: for (i = 0; i < nparams; i++) michael@0: { michael@0: png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]); michael@0: } michael@0: michael@0: png_free(png_ptr, params_len); michael@0: png_write_chunk_end(png_ptr); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_sCAL_SUPPORTED michael@0: /* Write the sCAL chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width, michael@0: png_const_charp height) michael@0: { michael@0: png_byte buf[64]; michael@0: png_size_t wlen, hlen, total_len; michael@0: michael@0: png_debug(1, "in png_write_sCAL_s"); michael@0: michael@0: wlen = strlen(width); michael@0: hlen = strlen(height); michael@0: total_len = wlen + hlen + 2; michael@0: michael@0: if (total_len > 64) michael@0: { michael@0: png_warning(png_ptr, "Can't write sCAL (buffer too small)"); michael@0: return; michael@0: } michael@0: michael@0: buf[0] = (png_byte)unit; michael@0: memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */ michael@0: memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */ michael@0: michael@0: png_debug1(3, "sCAL total length = %u", (unsigned int)total_len); michael@0: png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_pHYs_SUPPORTED michael@0: /* Write the pHYs chunk */ michael@0: void /* PRIVATE */ michael@0: png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit, michael@0: png_uint_32 y_pixels_per_unit, michael@0: int unit_type) michael@0: { michael@0: png_byte buf[9]; michael@0: michael@0: png_debug(1, "in png_write_pHYs"); michael@0: michael@0: if (unit_type >= PNG_RESOLUTION_LAST) michael@0: png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); michael@0: michael@0: png_save_uint_32(buf, x_pixels_per_unit); michael@0: png_save_uint_32(buf + 4, y_pixels_per_unit); michael@0: buf[8] = (png_byte)unit_type; michael@0: michael@0: png_write_complete_chunk(png_ptr, png_pHYs, buf, (png_size_t)9); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_tIME_SUPPORTED michael@0: /* Write the tIME chunk. Use either png_convert_from_struct_tm() michael@0: * or png_convert_from_time_t(), or fill in the structure yourself. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_write_tIME(png_structrp png_ptr, png_const_timep mod_time) michael@0: { michael@0: png_byte buf[7]; michael@0: michael@0: png_debug(1, "in png_write_tIME"); michael@0: michael@0: if (mod_time->month > 12 || mod_time->month < 1 || michael@0: mod_time->day > 31 || mod_time->day < 1 || michael@0: mod_time->hour > 23 || mod_time->second > 60) michael@0: { michael@0: png_warning(png_ptr, "Invalid time specified for tIME chunk"); michael@0: return; michael@0: } michael@0: michael@0: png_save_uint_16(buf, mod_time->year); michael@0: buf[2] = mod_time->month; michael@0: buf[3] = mod_time->day; michael@0: buf[4] = mod_time->hour; michael@0: buf[5] = mod_time->minute; michael@0: buf[6] = mod_time->second; michael@0: michael@0: png_write_complete_chunk(png_ptr, png_tIME, buf, (png_size_t)7); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_APNG_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_write_acTL(png_structp png_ptr, michael@0: png_uint_32 num_frames, png_uint_32 num_plays) michael@0: { michael@0: png_byte buf[8]; michael@0: michael@0: png_debug(1, "in png_write_acTL"); michael@0: michael@0: png_ptr->num_frames_to_write = num_frames; michael@0: michael@0: if (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) michael@0: num_frames--; michael@0: michael@0: png_save_uint_32(buf, num_frames); michael@0: png_save_uint_32(buf + 4, num_plays); michael@0: michael@0: png_write_complete_chunk(png_ptr, png_acTL, buf, (png_size_t)8); michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_write_fcTL(png_structp png_ptr, png_uint_32 width, png_uint_32 height, michael@0: png_uint_32 x_offset, png_uint_32 y_offset, michael@0: png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op, michael@0: png_byte blend_op) michael@0: { michael@0: png_byte buf[26]; michael@0: michael@0: png_debug(1, "in png_write_fcTL"); michael@0: michael@0: if (png_ptr->num_frames_written == 0 && (x_offset != 0 || y_offset != 0)) michael@0: png_error(png_ptr, "x and/or y offset for the first frame aren't 0"); michael@0: if (png_ptr->num_frames_written == 0 && michael@0: (width != png_ptr->first_frame_width || michael@0: height != png_ptr->first_frame_height)) michael@0: png_error(png_ptr, "width and/or height in the first frame's fcTL " michael@0: "don't match the ones in IHDR"); michael@0: michael@0: /* more error checking */ michael@0: png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset, michael@0: delay_num, delay_den, dispose_op, blend_op); michael@0: michael@0: png_save_uint_32(buf, png_ptr->next_seq_num); michael@0: png_save_uint_32(buf + 4, width); michael@0: png_save_uint_32(buf + 8, height); michael@0: png_save_uint_32(buf + 12, x_offset); michael@0: png_save_uint_32(buf + 16, y_offset); michael@0: png_save_uint_16(buf + 20, delay_num); michael@0: png_save_uint_16(buf + 22, delay_den); michael@0: buf[24] = dispose_op; michael@0: buf[25] = blend_op; michael@0: michael@0: png_write_complete_chunk(png_ptr, png_fcTL, buf, (png_size_t)26); michael@0: michael@0: png_ptr->next_seq_num++; michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_write_fdAT(png_structp png_ptr, michael@0: png_const_bytep data, png_size_t length) michael@0: { michael@0: png_byte buf[4]; michael@0: michael@0: png_write_chunk_header(png_ptr, png_fdAT, (png_uint_32)(4 + length)); michael@0: michael@0: png_save_uint_32(buf, png_ptr->next_seq_num); michael@0: png_write_chunk_data(png_ptr, buf, 4); michael@0: michael@0: png_write_chunk_data(png_ptr, data, length); michael@0: michael@0: png_write_chunk_end(png_ptr); michael@0: michael@0: png_ptr->next_seq_num++; michael@0: } michael@0: #endif /* PNG_WRITE_APNG_SUPPORTED */ michael@0: michael@0: /* Initializes the row writing capability of libpng */ michael@0: void /* PRIVATE */ michael@0: png_write_start_row(png_structrp png_ptr) michael@0: { michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ michael@0: michael@0: /* Start of interlace block */ michael@0: static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; michael@0: michael@0: /* Offset to next interlace block */ michael@0: static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; michael@0: michael@0: /* Start of interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; michael@0: michael@0: /* Offset to next interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; michael@0: #endif michael@0: michael@0: png_alloc_size_t buf_size; michael@0: int usr_pixel_depth; michael@0: michael@0: png_debug(1, "in png_write_start_row"); michael@0: michael@0: usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth; michael@0: buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1; michael@0: michael@0: /* 1.5.6: added to allow checking in the row write code. */ michael@0: png_ptr->transformed_pixel_depth = png_ptr->pixel_depth; michael@0: png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth; michael@0: michael@0: /* Set up row buffer */ michael@0: png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size); michael@0: michael@0: png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; michael@0: michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: /* Set up filtering buffer, if using this filter */ michael@0: if (png_ptr->do_filter & PNG_FILTER_SUB) michael@0: { michael@0: png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1); michael@0: michael@0: png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; michael@0: } michael@0: michael@0: /* We only need to keep the previous row if we are using one of these. */ michael@0: if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) michael@0: { michael@0: /* Set up previous row buffer */ michael@0: png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size); michael@0: michael@0: if (png_ptr->do_filter & PNG_FILTER_UP) michael@0: { michael@0: png_ptr->up_row = (png_bytep)png_malloc(png_ptr, michael@0: png_ptr->rowbytes + 1); michael@0: michael@0: png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; michael@0: } michael@0: michael@0: if (png_ptr->do_filter & PNG_FILTER_AVG) michael@0: { michael@0: png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, michael@0: png_ptr->rowbytes + 1); michael@0: michael@0: png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; michael@0: } michael@0: michael@0: if (png_ptr->do_filter & PNG_FILTER_PAETH) michael@0: { michael@0: png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, michael@0: png_ptr->rowbytes + 1); michael@0: michael@0: png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; michael@0: } michael@0: } michael@0: #endif /* PNG_WRITE_FILTER_SUPPORTED */ michael@0: michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* If interlaced, we need to set up width and height of pass */ michael@0: if (png_ptr->interlaced) michael@0: { michael@0: if (!(png_ptr->transformations & PNG_INTERLACE)) michael@0: { michael@0: png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - michael@0: png_pass_ystart[0]) / png_pass_yinc[0]; michael@0: michael@0: png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - michael@0: png_pass_start[0]) / png_pass_inc[0]; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->num_rows = png_ptr->height; michael@0: png_ptr->usr_width = png_ptr->width; michael@0: } michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: png_ptr->num_rows = png_ptr->height; michael@0: png_ptr->usr_width = png_ptr->width; michael@0: } michael@0: } michael@0: michael@0: /* Internal use only. Called when finished processing a row of data. */ michael@0: void /* PRIVATE */ michael@0: png_write_finish_row(png_structrp png_ptr) michael@0: { michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ michael@0: michael@0: /* Start of interlace block */ michael@0: static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; michael@0: michael@0: /* Offset to next interlace block */ michael@0: static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; michael@0: michael@0: /* Start of interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; michael@0: michael@0: /* Offset to next interlace block in the y direction */ michael@0: static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; michael@0: #endif michael@0: michael@0: png_debug(1, "in png_write_finish_row"); michael@0: michael@0: /* Next row */ michael@0: png_ptr->row_number++; michael@0: michael@0: /* See if we are done */ michael@0: if (png_ptr->row_number < png_ptr->num_rows) michael@0: return; michael@0: michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* If interlaced, go to next pass */ michael@0: if (png_ptr->interlaced) michael@0: { michael@0: png_ptr->row_number = 0; michael@0: if (png_ptr->transformations & PNG_INTERLACE) michael@0: { michael@0: png_ptr->pass++; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* Loop until we find a non-zero width or height pass */ michael@0: do michael@0: { michael@0: png_ptr->pass++; michael@0: michael@0: if (png_ptr->pass >= 7) michael@0: break; michael@0: michael@0: png_ptr->usr_width = (png_ptr->width + michael@0: png_pass_inc[png_ptr->pass] - 1 - michael@0: png_pass_start[png_ptr->pass]) / michael@0: png_pass_inc[png_ptr->pass]; michael@0: michael@0: png_ptr->num_rows = (png_ptr->height + michael@0: png_pass_yinc[png_ptr->pass] - 1 - michael@0: png_pass_ystart[png_ptr->pass]) / michael@0: png_pass_yinc[png_ptr->pass]; michael@0: michael@0: if (png_ptr->transformations & PNG_INTERLACE) michael@0: break; michael@0: michael@0: } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); michael@0: michael@0: } michael@0: michael@0: /* Reset the row above the image for the next pass */ michael@0: if (png_ptr->pass < 7) michael@0: { michael@0: if (png_ptr->prev_row != NULL) michael@0: memset(png_ptr->prev_row, 0, michael@0: (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels* michael@0: png_ptr->usr_bit_depth, png_ptr->width)) + 1); michael@0: michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* If we get here, we've just written the last row, so we need michael@0: to flush the compressor */ michael@0: png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH); michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* Pick out the correct pixels for the interlace pass. michael@0: * The basic idea here is to go through the row with a source michael@0: * pointer and a destination pointer (sp and dp), and copy the michael@0: * correct pixels for the pass. As the row gets compacted, michael@0: * sp will always be >= dp, so we should never overwrite anything. michael@0: * See the default: case for the easiest code to understand. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) michael@0: { michael@0: /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ michael@0: michael@0: /* Start of interlace block */ michael@0: static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; michael@0: michael@0: /* Offset to next interlace block */ michael@0: static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; michael@0: michael@0: png_debug(1, "in png_do_write_interlace"); michael@0: michael@0: /* We don't have to do anything on the last pass (6) */ michael@0: if (pass < 6) michael@0: { michael@0: /* Each pixel depth is handled separately */ michael@0: switch (row_info->pixel_depth) michael@0: { michael@0: case 1: michael@0: { michael@0: png_bytep sp; michael@0: png_bytep dp; michael@0: int shift; michael@0: int d; michael@0: int value; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width = row_info->width; michael@0: michael@0: dp = row; michael@0: d = 0; michael@0: shift = 7; michael@0: michael@0: for (i = png_pass_start[pass]; i < row_width; michael@0: i += png_pass_inc[pass]) michael@0: { michael@0: sp = row + (png_size_t)(i >> 3); michael@0: value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01; michael@0: d |= (value << shift); michael@0: michael@0: if (shift == 0) michael@0: { michael@0: shift = 7; michael@0: *dp++ = (png_byte)d; michael@0: d = 0; michael@0: } michael@0: michael@0: else michael@0: shift--; michael@0: michael@0: } michael@0: if (shift != 7) michael@0: *dp = (png_byte)d; michael@0: michael@0: break; michael@0: } michael@0: michael@0: case 2: michael@0: { michael@0: png_bytep sp; michael@0: png_bytep dp; michael@0: int shift; michael@0: int d; michael@0: int value; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width = row_info->width; michael@0: michael@0: dp = row; michael@0: shift = 6; michael@0: d = 0; michael@0: michael@0: for (i = png_pass_start[pass]; i < row_width; michael@0: i += png_pass_inc[pass]) michael@0: { michael@0: sp = row + (png_size_t)(i >> 2); michael@0: value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03; michael@0: d |= (value << shift); michael@0: michael@0: if (shift == 0) michael@0: { michael@0: shift = 6; michael@0: *dp++ = (png_byte)d; michael@0: d = 0; michael@0: } michael@0: michael@0: else michael@0: shift -= 2; michael@0: } michael@0: if (shift != 6) michael@0: *dp = (png_byte)d; michael@0: michael@0: break; michael@0: } michael@0: michael@0: case 4: michael@0: { michael@0: png_bytep sp; michael@0: png_bytep dp; michael@0: int shift; michael@0: int d; michael@0: int value; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width = row_info->width; michael@0: michael@0: dp = row; michael@0: shift = 4; michael@0: d = 0; michael@0: for (i = png_pass_start[pass]; i < row_width; michael@0: i += png_pass_inc[pass]) michael@0: { michael@0: sp = row + (png_size_t)(i >> 1); michael@0: value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f; michael@0: d |= (value << shift); michael@0: michael@0: if (shift == 0) michael@0: { michael@0: shift = 4; michael@0: *dp++ = (png_byte)d; michael@0: d = 0; michael@0: } michael@0: michael@0: else michael@0: shift -= 4; michael@0: } michael@0: if (shift != 4) michael@0: *dp = (png_byte)d; michael@0: michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: { michael@0: png_bytep sp; michael@0: png_bytep dp; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width = row_info->width; michael@0: png_size_t pixel_bytes; michael@0: michael@0: /* Start at the beginning */ michael@0: dp = row; michael@0: michael@0: /* Find out how many bytes each pixel takes up */ michael@0: pixel_bytes = (row_info->pixel_depth >> 3); michael@0: michael@0: /* Loop through the row, only looking at the pixels that matter */ michael@0: for (i = png_pass_start[pass]; i < row_width; michael@0: i += png_pass_inc[pass]) michael@0: { michael@0: /* Find out where the original pixel is */ michael@0: sp = row + (png_size_t)i * pixel_bytes; michael@0: michael@0: /* Move the pixel */ michael@0: if (dp != sp) michael@0: memcpy(dp, sp, pixel_bytes); michael@0: michael@0: /* Next pixel */ michael@0: dp += pixel_bytes; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: /* Set new row width */ michael@0: row_info->width = (row_info->width + michael@0: png_pass_inc[pass] - 1 - michael@0: png_pass_start[pass]) / michael@0: png_pass_inc[pass]; michael@0: michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, michael@0: row_info->width); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* This filters the row, chooses which filter to use, if it has not already michael@0: * been specified by the application, and then writes the row out with the michael@0: * chosen filter. michael@0: */ michael@0: static void michael@0: png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, michael@0: png_size_t row_bytes); michael@0: michael@0: #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) michael@0: #define PNG_HISHIFT 10 michael@0: #define PNG_LOMASK ((png_uint_32)0xffffL) michael@0: #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) michael@0: void /* PRIVATE */ michael@0: png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) michael@0: { michael@0: png_bytep best_row; michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: png_bytep prev_row, row_buf; michael@0: png_uint_32 mins, bpp; michael@0: png_byte filter_to_do = png_ptr->do_filter; michael@0: png_size_t row_bytes = row_info->rowbytes; michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: int num_p_filters = png_ptr->num_prev_filters; michael@0: #endif michael@0: michael@0: png_debug(1, "in png_write_find_filter"); michael@0: michael@0: #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS) michael@0: { michael@0: /* These will never be selected so we need not test them. */ michael@0: filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH); michael@0: } michael@0: #endif michael@0: michael@0: /* Find out how many bytes offset each pixel is */ michael@0: bpp = (row_info->pixel_depth + 7) >> 3; michael@0: michael@0: prev_row = png_ptr->prev_row; michael@0: #endif michael@0: best_row = png_ptr->row_buf; michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: row_buf = best_row; michael@0: mins = PNG_MAXSUM; michael@0: michael@0: /* The prediction method we use is to find which method provides the michael@0: * smallest value when summing the absolute values of the distances michael@0: * from zero, using anything >= 128 as negative numbers. This is known michael@0: * as the "minimum sum of absolute differences" heuristic. Other michael@0: * heuristics are the "weighted minimum sum of absolute differences" michael@0: * (experimental and can in theory improve compression), and the "zlib michael@0: * predictive" method (not implemented yet), which does test compressions michael@0: * of lines using different filter methods, and then chooses the michael@0: * (series of) filter(s) that give minimum compressed data size (VERY michael@0: * computationally expensive). michael@0: * michael@0: * GRR 980525: consider also michael@0: * michael@0: * (1) minimum sum of absolute differences from running average (i.e., michael@0: * keep running sum of non-absolute differences & count of bytes) michael@0: * [track dispersion, too? restart average if dispersion too large?] michael@0: * michael@0: * (1b) minimum sum of absolute differences from sliding average, probably michael@0: * with window size <= deflate window (usually 32K) michael@0: * michael@0: * (2) minimum sum of squared differences from zero or running average michael@0: * (i.e., ~ root-mean-square approach) michael@0: */ michael@0: michael@0: michael@0: /* We don't need to test the 'no filter' case if this is the only filter michael@0: * that has been chosen, as it doesn't actually do anything to the data. michael@0: */ michael@0: if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE) michael@0: { michael@0: png_bytep rp; michael@0: png_uint_32 sum = 0; michael@0: png_size_t i; michael@0: int v; michael@0: michael@0: for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) michael@0: { michael@0: v = *rp; michael@0: sum += (v < 128) ? v : 256 - v; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: png_uint_32 sumhi, sumlo; michael@0: int j; michael@0: sumlo = sum & PNG_LOMASK; michael@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ michael@0: michael@0: /* Reduce the sum if we match any of the previous rows */ michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) michael@0: { michael@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: /* Factor in the cost of this filter (this is here for completeness, michael@0: * but it makes no sense to have a "cost" for the NONE filter, as michael@0: * it has the minimum possible computational cost - none). michael@0: */ michael@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (sumhi > PNG_HIMASK) michael@0: sum = PNG_MAXSUM; michael@0: michael@0: else michael@0: sum = (sumhi << PNG_HISHIFT) + sumlo; michael@0: } michael@0: #endif michael@0: mins = sum; michael@0: } michael@0: michael@0: /* Sub filter */ michael@0: if (filter_to_do == PNG_FILTER_SUB) michael@0: /* It's the only filter so no testing is needed */ michael@0: { michael@0: png_bytep rp, lp, dp; michael@0: png_size_t i; michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; michael@0: i++, rp++, dp++) michael@0: { michael@0: *dp = *rp; michael@0: } michael@0: michael@0: for (lp = row_buf + 1; i < row_bytes; michael@0: i++, rp++, lp++, dp++) michael@0: { michael@0: *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); michael@0: } michael@0: michael@0: best_row = png_ptr->sub_row; michael@0: } michael@0: michael@0: else if (filter_to_do & PNG_FILTER_SUB) michael@0: { michael@0: png_bytep rp, dp, lp; michael@0: png_uint_32 sum = 0, lmins = mins; michael@0: png_size_t i; michael@0: int v; michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: /* We temporarily increase the "minimum sum" by the factor we michael@0: * would reduce the sum of this filter, so that we can do the michael@0: * early exit comparison without scaling the sum each time. michael@0: */ michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 lmhi, lmlo; michael@0: lmlo = lmins & PNG_LOMASK; michael@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) michael@0: { michael@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (lmhi > PNG_HIMASK) michael@0: lmins = PNG_MAXSUM; michael@0: michael@0: else michael@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; michael@0: } michael@0: #endif michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; michael@0: i++, rp++, dp++) michael@0: { michael@0: v = *dp = *rp; michael@0: michael@0: sum += (v < 128) ? v : 256 - v; michael@0: } michael@0: michael@0: for (lp = row_buf + 1; i < row_bytes; michael@0: i++, rp++, lp++, dp++) michael@0: { michael@0: v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); michael@0: michael@0: sum += (v < 128) ? v : 256 - v; michael@0: michael@0: if (sum > lmins) /* We are already worse, don't continue. */ michael@0: break; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 sumhi, sumlo; michael@0: sumlo = sum & PNG_LOMASK; michael@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) michael@0: { michael@0: sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (sumhi > PNG_HIMASK) michael@0: sum = PNG_MAXSUM; michael@0: michael@0: else michael@0: sum = (sumhi << PNG_HISHIFT) + sumlo; michael@0: } michael@0: #endif michael@0: michael@0: if (sum < mins) michael@0: { michael@0: mins = sum; michael@0: best_row = png_ptr->sub_row; michael@0: } michael@0: } michael@0: michael@0: /* Up filter */ michael@0: if (filter_to_do == PNG_FILTER_UP) michael@0: { michael@0: png_bytep rp, dp, pp; michael@0: png_size_t i; michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, michael@0: pp = prev_row + 1; i < row_bytes; michael@0: i++, rp++, pp++, dp++) michael@0: { michael@0: *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); michael@0: } michael@0: michael@0: best_row = png_ptr->up_row; michael@0: } michael@0: michael@0: else if (filter_to_do & PNG_FILTER_UP) michael@0: { michael@0: png_bytep rp, dp, pp; michael@0: png_uint_32 sum = 0, lmins = mins; michael@0: png_size_t i; michael@0: int v; michael@0: michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 lmhi, lmlo; michael@0: lmlo = lmins & PNG_LOMASK; michael@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) michael@0: { michael@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (lmhi > PNG_HIMASK) michael@0: lmins = PNG_MAXSUM; michael@0: michael@0: else michael@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; michael@0: } michael@0: #endif michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, michael@0: pp = prev_row + 1; i < row_bytes; i++) michael@0: { michael@0: v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); michael@0: michael@0: sum += (v < 128) ? v : 256 - v; michael@0: michael@0: if (sum > lmins) /* We are already worse, don't continue. */ michael@0: break; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 sumhi, sumlo; michael@0: sumlo = sum & PNG_LOMASK; michael@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) michael@0: { michael@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (sumhi > PNG_HIMASK) michael@0: sum = PNG_MAXSUM; michael@0: michael@0: else michael@0: sum = (sumhi << PNG_HISHIFT) + sumlo; michael@0: } michael@0: #endif michael@0: michael@0: if (sum < mins) michael@0: { michael@0: mins = sum; michael@0: best_row = png_ptr->up_row; michael@0: } michael@0: } michael@0: michael@0: /* Avg filter */ michael@0: if (filter_to_do == PNG_FILTER_AVG) michael@0: { michael@0: png_bytep rp, dp, pp, lp; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, michael@0: pp = prev_row + 1; i < bpp; i++) michael@0: { michael@0: *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); michael@0: } michael@0: michael@0: for (lp = row_buf + 1; i < row_bytes; i++) michael@0: { michael@0: *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) michael@0: & 0xff); michael@0: } michael@0: best_row = png_ptr->avg_row; michael@0: } michael@0: michael@0: else if (filter_to_do & PNG_FILTER_AVG) michael@0: { michael@0: png_bytep rp, dp, pp, lp; michael@0: png_uint_32 sum = 0, lmins = mins; michael@0: png_size_t i; michael@0: int v; michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 lmhi, lmlo; michael@0: lmlo = lmins & PNG_LOMASK; michael@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) michael@0: { michael@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (lmhi > PNG_HIMASK) michael@0: lmins = PNG_MAXSUM; michael@0: michael@0: else michael@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; michael@0: } michael@0: #endif michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, michael@0: pp = prev_row + 1; i < bpp; i++) michael@0: { michael@0: v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); michael@0: michael@0: sum += (v < 128) ? v : 256 - v; michael@0: } michael@0: michael@0: for (lp = row_buf + 1; i < row_bytes; i++) michael@0: { michael@0: v = *dp++ = michael@0: (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); michael@0: michael@0: sum += (v < 128) ? v : 256 - v; michael@0: michael@0: if (sum > lmins) /* We are already worse, don't continue. */ michael@0: break; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 sumhi, sumlo; michael@0: sumlo = sum & PNG_LOMASK; michael@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) michael@0: { michael@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (sumhi > PNG_HIMASK) michael@0: sum = PNG_MAXSUM; michael@0: michael@0: else michael@0: sum = (sumhi << PNG_HISHIFT) + sumlo; michael@0: } michael@0: #endif michael@0: michael@0: if (sum < mins) michael@0: { michael@0: mins = sum; michael@0: best_row = png_ptr->avg_row; michael@0: } michael@0: } michael@0: michael@0: /* Paeth filter */ michael@0: if (filter_to_do == PNG_FILTER_PAETH) michael@0: { michael@0: png_bytep rp, dp, pp, cp, lp; michael@0: png_size_t i; michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, michael@0: pp = prev_row + 1; i < bpp; i++) michael@0: { michael@0: *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); michael@0: } michael@0: michael@0: for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) michael@0: { michael@0: int a, b, c, pa, pb, pc, p; michael@0: michael@0: b = *pp++; michael@0: c = *cp++; michael@0: a = *lp++; michael@0: michael@0: p = b - c; michael@0: pc = a - c; michael@0: michael@0: #ifdef PNG_USE_ABS michael@0: pa = abs(p); michael@0: pb = abs(pc); michael@0: pc = abs(p + pc); michael@0: #else michael@0: pa = p < 0 ? -p : p; michael@0: pb = pc < 0 ? -pc : pc; michael@0: pc = (p + pc) < 0 ? -(p + pc) : p + pc; michael@0: #endif michael@0: michael@0: p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; michael@0: michael@0: *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); michael@0: } michael@0: best_row = png_ptr->paeth_row; michael@0: } michael@0: michael@0: else if (filter_to_do & PNG_FILTER_PAETH) michael@0: { michael@0: png_bytep rp, dp, pp, cp, lp; michael@0: png_uint_32 sum = 0, lmins = mins; michael@0: png_size_t i; michael@0: int v; michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 lmhi, lmlo; michael@0: lmlo = lmins & PNG_LOMASK; michael@0: lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) michael@0: { michael@0: lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (lmhi > PNG_HIMASK) michael@0: lmins = PNG_MAXSUM; michael@0: michael@0: else michael@0: lmins = (lmhi << PNG_HISHIFT) + lmlo; michael@0: } michael@0: #endif michael@0: michael@0: for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, michael@0: pp = prev_row + 1; i < bpp; i++) michael@0: { michael@0: v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); michael@0: michael@0: sum += (v < 128) ? v : 256 - v; michael@0: } michael@0: michael@0: for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) michael@0: { michael@0: int a, b, c, pa, pb, pc, p; michael@0: michael@0: b = *pp++; michael@0: c = *cp++; michael@0: a = *lp++; michael@0: michael@0: #ifndef PNG_SLOW_PAETH michael@0: p = b - c; michael@0: pc = a - c; michael@0: #ifdef PNG_USE_ABS michael@0: pa = abs(p); michael@0: pb = abs(pc); michael@0: pc = abs(p + pc); michael@0: #else michael@0: pa = p < 0 ? -p : p; michael@0: pb = pc < 0 ? -pc : pc; michael@0: pc = (p + pc) < 0 ? -(p + pc) : p + pc; michael@0: #endif michael@0: p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; michael@0: #else /* PNG_SLOW_PAETH */ michael@0: p = a + b - c; michael@0: pa = abs(p - a); michael@0: pb = abs(p - b); michael@0: pc = abs(p - c); michael@0: michael@0: if (pa <= pb && pa <= pc) michael@0: p = a; michael@0: michael@0: else if (pb <= pc) michael@0: p = b; michael@0: michael@0: else michael@0: p = c; michael@0: #endif /* PNG_SLOW_PAETH */ michael@0: michael@0: v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); michael@0: michael@0: sum += (v < 128) ? v : 256 - v; michael@0: michael@0: if (sum > lmins) /* We are already worse, don't continue. */ michael@0: break; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int j; michael@0: png_uint_32 sumhi, sumlo; michael@0: sumlo = sum & PNG_LOMASK; michael@0: sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; michael@0: michael@0: for (j = 0; j < num_p_filters; j++) michael@0: { michael@0: if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) michael@0: { michael@0: sumlo = (sumlo * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_weights[j]) >> michael@0: PNG_WEIGHT_SHIFT; michael@0: } michael@0: } michael@0: michael@0: sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> michael@0: PNG_COST_SHIFT; michael@0: michael@0: if (sumhi > PNG_HIMASK) michael@0: sum = PNG_MAXSUM; michael@0: michael@0: else michael@0: sum = (sumhi << PNG_HISHIFT) + sumlo; michael@0: } michael@0: #endif michael@0: michael@0: if (sum < mins) michael@0: { michael@0: best_row = png_ptr->paeth_row; michael@0: } michael@0: } michael@0: #endif /* PNG_WRITE_FILTER_SUPPORTED */ michael@0: michael@0: /* Do the actual writing of the filtered row data from the chosen filter. */ michael@0: png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1); michael@0: michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: /* Save the type of filter we picked this time for future calculations */ michael@0: if (png_ptr->num_prev_filters > 0) michael@0: { michael@0: int j; michael@0: michael@0: for (j = 1; j < num_p_filters; j++) michael@0: { michael@0: png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; michael@0: } michael@0: michael@0: png_ptr->prev_filters[j] = best_row[0]; michael@0: } michael@0: #endif michael@0: #endif /* PNG_WRITE_FILTER_SUPPORTED */ michael@0: } michael@0: michael@0: michael@0: /* Do the actual writing of a previously filtered row. */ michael@0: static void michael@0: png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, michael@0: png_size_t full_row_length/*includes filter byte*/) michael@0: { michael@0: png_debug(1, "in png_write_filtered_row"); michael@0: michael@0: png_debug1(2, "filter = %d", filtered_row[0]); michael@0: michael@0: png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH); michael@0: michael@0: /* Swap the current and previous rows */ michael@0: if (png_ptr->prev_row != NULL) michael@0: { michael@0: png_bytep tptr; michael@0: michael@0: tptr = png_ptr->prev_row; michael@0: png_ptr->prev_row = png_ptr->row_buf; michael@0: png_ptr->row_buf = tptr; michael@0: } michael@0: michael@0: /* Finish row - updates counters and flushes zlib if last row */ michael@0: png_write_finish_row(png_ptr); michael@0: michael@0: #ifdef PNG_WRITE_FLUSH_SUPPORTED michael@0: png_ptr->flush_rows++; michael@0: michael@0: if (png_ptr->flush_dist > 0 && michael@0: png_ptr->flush_rows >= png_ptr->flush_dist) michael@0: { michael@0: png_write_flush(png_ptr); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_APNG_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_write_reset(png_structp png_ptr) michael@0: { michael@0: png_ptr->row_number = 0; michael@0: png_ptr->pass = 0; michael@0: png_ptr->mode &= ~PNG_HAVE_IDAT; michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_write_reinit(png_structp png_ptr, png_infop info_ptr, michael@0: png_uint_32 width, png_uint_32 height) michael@0: { michael@0: if (png_ptr->num_frames_written == 0 && michael@0: (width != png_ptr->first_frame_width || michael@0: height != png_ptr->first_frame_height)) michael@0: png_error(png_ptr, "width and/or height in the first frame's fcTL " michael@0: "don't match the ones in IHDR"); michael@0: if (width > png_ptr->first_frame_width || michael@0: height > png_ptr->first_frame_height) michael@0: png_error(png_ptr, "width and/or height for a frame greater than" michael@0: "the ones in IHDR"); michael@0: michael@0: png_set_IHDR(png_ptr, info_ptr, width, height, michael@0: info_ptr->bit_depth, info_ptr->color_type, michael@0: info_ptr->interlace_type, info_ptr->compression_type, michael@0: info_ptr->filter_type); michael@0: michael@0: png_ptr->width = width; michael@0: png_ptr->height = height; michael@0: png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); michael@0: png_ptr->usr_width = png_ptr->width; michael@0: } michael@0: #endif /* PNG_WRITE_APNG_SUPPORTED */ michael@0: #endif /* PNG_WRITE_SUPPORTED */