blob: 4bbb7ad6fc5c5fedd30b4d55c66007e783787552 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwutil.c - utilities to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05004 * libpng 1.2.0beta1 - May 6, 2001
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -06006 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050018void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
21 buf[0] = (png_byte)((i >> 24) & 0xff);
22 buf[1] = (png_byte)((i >> 16) & 0xff);
23 buf[2] = (png_byte)((i >> 8) & 0xff);
24 buf[3] = (png_byte)(i & 0xff);
25}
26
Andreas Dilger47a0c421997-05-16 02:46:07 -050027#if defined(PNG_WRITE_pCAL_SUPPORTED)
28/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050032void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40#endif
41
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042/* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
45 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050046void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050048{
49 buf[0] = (png_byte)((i >> 8) & 0xff);
50 buf[1] = (png_byte)(i & 0xff);
51}
52
Andreas Dilger47a0c421997-05-16 02:46:07 -050053/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
60 * functions instead.
61 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050062void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050065{
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050069}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
74 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050075void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050078{
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050080 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050081
Guy Schalnat0d580581995-07-20 02:43:20 -050082 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050083 png_save_uint_32(buf, length);
84 png_write_data(png_ptr, buf, (png_size_t)4);
85
Guy Schalnat0d580581995-07-20 02:43:20 -050086 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050088 /* reset the crc and run it over the chunk name */
89 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050091}
92
Andreas Dilger47a0c421997-05-16 02:46:07 -050093/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060094 * Note that multiple calls to this function are allowed, and that the
95 * sum of the lengths from these calls *must* add up to the total_length
96 * given to png_write_chunk_start().
97 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050098void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050099png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 /* write the data, and run the CRC over it */
102 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103 {
104 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600105 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 }
107}
108
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500110void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600111png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 png_byte buf[4];
114
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117
118 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119}
120
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600121/* Simple function to write the signature. If we have already written
122 * the magic bytes of the signature, or more likely, the PNG stream is
123 * being embedded into another stream and doesn't need its own signature,
124 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600125 * bytes have already been written.
126 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500127void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600128png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500129{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600130 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600132 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 (png_size_t)8 - png_ptr->sig_bytes);
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600134 if(png_ptr->sig_bytes < 3)
135 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500136}
137
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600138#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600139/*
140 * This pair of functions encapsulates the operation of (a) compressing a
141 * text string, and (b) issuing it later as a series of chunk data writes.
142 * The compression_state structure is shared context for these functions
143 * set up by the caller in order to make the whole mess thread-safe.
144 */
145
146typedef struct
147{
148 char *input; /* the uncompressed input data */
149 int input_len; /* its length */
150 int num_output_ptr; /* number of output pointers used */
151 int max_output_ptr; /* size of output_ptr */
152 png_charpp output_ptr; /* array of pointers to output */
153} compression_state;
154
155/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500156static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600157png_text_compress(png_structp png_ptr,
158 png_charp text, png_size_t text_len, int compression,
159 compression_state *comp)
160{
161 int ret;
162
163 comp->num_output_ptr = comp->max_output_ptr = 0;
164 comp->output_ptr = NULL;
165 comp->input = NULL;
166
167 /* we may just want to pass the text right through */
168 if (compression == PNG_TEXT_COMPRESSION_NONE)
169 {
170 comp->input = text;
171 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500172 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600173 }
174
175 if (compression >= PNG_TEXT_COMPRESSION_LAST)
176 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500177#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600178 char msg[50];
179 sprintf(msg, "Unknown compression type %d", compression);
180 png_warning(png_ptr, msg);
181#else
182 png_warning(png_ptr, "Unknown compression type");
183#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600184 }
185
186 /* We can't write the chunk until we find out how much data we have,
187 * which means we need to run the compressor first and save the
188 * output. This shouldn't be a problem, as the vast majority of
189 * comments should be reasonable, but we will set up an array of
190 * malloc'd pointers to be sure.
191 *
192 * If we knew the application was well behaved, we could simplify this
193 * greatly by assuming we can always malloc an output buffer large
194 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
195 * and malloc this directly. The only time this would be a bad idea is
196 * if we can't malloc more than 64K and we have 64K of random input
197 * data, or if the input string is incredibly large (although this
198 * wouldn't cause a failure, just a slowdown due to swapping).
199 */
200
201 /* set up the compression buffers */
202 png_ptr->zstream.avail_in = (uInt)text_len;
203 png_ptr->zstream.next_in = (Bytef *)text;
204 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
205 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
206
207 /* this is the same compression loop as in png_write_row() */
208 do
209 {
210 /* compress the data */
211 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
212 if (ret != Z_OK)
213 {
214 /* error */
215 if (png_ptr->zstream.msg != NULL)
216 png_error(png_ptr, png_ptr->zstream.msg);
217 else
218 png_error(png_ptr, "zlib error");
219 }
220 /* check to see if we need more room */
221 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
222 {
223 /* make sure the output array has room */
224 if (comp->num_output_ptr >= comp->max_output_ptr)
225 {
226 int old_max;
227
228 old_max = comp->max_output_ptr;
229 comp->max_output_ptr = comp->num_output_ptr + 4;
230 if (comp->output_ptr != NULL)
231 {
232 png_charpp old_ptr;
233
234 old_ptr = comp->output_ptr;
235 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
236 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500237 png_memcpy(comp->output_ptr, old_ptr,
238 old_max * sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600239 png_free(png_ptr, old_ptr);
240 }
241 else
242 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
243 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
244 }
245
246 /* save the data */
247 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
248 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500249 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
250 png_ptr->zbuf_size);
251 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600252
253 /* and reset the buffer */
254 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
255 png_ptr->zstream.next_out = png_ptr->zbuf;
256 }
257 /* continue until we don't have any more to compress */
258 } while (png_ptr->zstream.avail_in);
259
260 /* finish the compression */
261 do
262 {
263 /* tell zlib we are finished */
264 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500265
266 if (ret == Z_OK)
267 {
268 /* check to see if we need more room */
269 if (!(png_ptr->zstream.avail_out))
270 {
271 /* check to make sure our output array has room */
272 if (comp->num_output_ptr >= comp->max_output_ptr)
273 {
274 int old_max;
275
276 old_max = comp->max_output_ptr;
277 comp->max_output_ptr = comp->num_output_ptr + 4;
278 if (comp->output_ptr != NULL)
279 {
280 png_charpp old_ptr;
281
282 old_ptr = comp->output_ptr;
283 /* This could be optimized to realloc() */
284 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
285 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500286 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500287 old_max * sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500288 png_free(png_ptr, old_ptr);
289 }
290 else
291 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
292 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
293 }
294
295 /* save off the data */
296 comp->output_ptr[comp->num_output_ptr] =
297 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500298 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
299 png_ptr->zbuf_size);
300 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500301
302 /* and reset the buffer pointers */
303 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
304 png_ptr->zstream.next_out = png_ptr->zbuf;
305 }
306 }
307 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600308 {
309 /* we got an error */
310 if (png_ptr->zstream.msg != NULL)
311 png_error(png_ptr, png_ptr->zstream.msg);
312 else
313 png_error(png_ptr, "zlib error");
314 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600315 } while (ret != Z_STREAM_END);
316
317 /* text length is number of buffers plus last buffer */
318 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
319 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
320 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
321
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500322 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600323}
324
325/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500326static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600327png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
328{
329 int i;
330
331 /* handle the no-compression case */
332 if (comp->input)
333 {
334 png_write_chunk_data(png_ptr, (png_bytep)comp->input, comp->input_len);
335 return;
336 }
337
338 /* write saved output buffers, if any */
339 for (i = 0; i < comp->num_output_ptr; i++)
340 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600341 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
342 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600343 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500344 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600345 }
346 if (comp->max_output_ptr != 0)
347 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500348 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600349 /* write anything left in zbuf */
350 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
351 png_write_chunk_data(png_ptr, png_ptr->zbuf,
352 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
353
354 /* reset zlib for another zTXt/iTXt or the image data */
355 deflateReset(&png_ptr->zstream);
356
357}
358#endif
359
Guy Schalnat0d580581995-07-20 02:43:20 -0500360/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600361 * information. Note that the rest of this code depends upon this
362 * information being correct.
363 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500364void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600365png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500366 int bit_depth, int color_type, int compression_type, int filter_type,
367 int interlace_type)
368{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600369#ifdef PNG_USE_LOCAL_ARRAYS
370 PNG_IHDR;
371#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 png_byte buf[13]; /* buffer to store the IHDR info */
373
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500375 /* Check that we have valid input data from the application info */
376 switch (color_type)
377 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500379 switch (bit_depth)
380 {
381 case 1:
382 case 2:
383 case 4:
384 case 8:
385 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500387 }
388 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500390 if (bit_depth != 8 && bit_depth != 16)
391 png_error(png_ptr, "Invalid bit depth for RGB image");
392 png_ptr->channels = 3;
393 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500395 switch (bit_depth)
396 {
397 case 1:
398 case 2:
399 case 4:
400 case 8: png_ptr->channels = 1; break;
401 default: png_error(png_ptr, "Invalid bit depth for paletted image");
402 }
403 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500405 if (bit_depth != 8 && bit_depth != 16)
406 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
407 png_ptr->channels = 2;
408 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500410 if (bit_depth != 8 && bit_depth != 16)
411 png_error(png_ptr, "Invalid bit depth for RGBA image");
412 png_ptr->channels = 4;
413 break;
414 default:
415 png_error(png_ptr, "Invalid image color type specified");
416 }
417
Andreas Dilger47a0c421997-05-16 02:46:07 -0500418 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500419 {
420 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500422 }
423
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600424 /* Write filter_method 64 (intrapixel differencing) only if
425 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
426 * 2. Libpng did not write a PNG signature (this filter_method is only
427 * used in PNG datastreams that are embedded in MNG datastreams) and
428 * 3. The application called png_permit_mng_features with a mask that
429 * included PNG_FLAG_MNG_FILTER_64 and
430 * 4. The filter_method is 64 and
431 * 5. The color_type is RGB or RGBA
432 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600433 if (
434#if defined(PNG_MNG_FEATURES_SUPPORTED)
435 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600436 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500437 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600438 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600439 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
440#endif
441 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500442 {
443 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500444 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500445 }
446
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600447#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 if (interlace_type != PNG_INTERLACE_NONE &&
449 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500450 {
451 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500453 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600454#else
455 interlace_type=PNG_INTERLACE_NONE;
456#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500457
458 /* save off the relevent information */
459 png_ptr->bit_depth = (png_byte)bit_depth;
460 png_ptr->color_type = (png_byte)color_type;
461 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600462 png_ptr->filter_type = (png_byte)filter_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500463 png_ptr->width = width;
464 png_ptr->height = height;
465
466 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500467 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500468 /* set the usr info, so any transformations can modify it */
469 png_ptr->usr_width = png_ptr->width;
470 png_ptr->usr_bit_depth = png_ptr->bit_depth;
471 png_ptr->usr_channels = png_ptr->channels;
472
Guy Schalnat0d580581995-07-20 02:43:20 -0500473 /* pack the header information into the buffer */
474 png_save_uint_32(buf, width);
475 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600476 buf[8] = (png_byte)bit_depth;
477 buf[9] = (png_byte)color_type;
478 buf[10] = (png_byte)compression_type;
479 buf[11] = (png_byte)filter_type;
480 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500481
482 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600483 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500484
Andreas Dilger47a0c421997-05-16 02:46:07 -0500485 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600486 png_ptr->zstream.zalloc = png_zalloc;
487 png_ptr->zstream.zfree = png_zfree;
488 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500489 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500490 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
492 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500493 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500494 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500495 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500496 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500497 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500498 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500499 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500500 png_ptr->zlib_strategy = Z_FILTERED;
501 else
502 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
503 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500505 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500507 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500509 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500511 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600512 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500513 png_ptr->zlib_method, png_ptr->zlib_window_bits,
514 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600515 png_ptr->zstream.next_out = png_ptr->zbuf;
516 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500517
Guy Schalnate5a37791996-06-05 15:50:50 -0500518 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500519}
520
521/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500522 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600523 * structure.
524 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500525void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500526png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500527{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600528#ifdef PNG_USE_LOCAL_ARRAYS
529 PNG_PLTE;
530#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500531 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600532 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500533 png_byte buf[3];
534
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500536 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500537#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600538 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500539#endif
540 num_pal == 0) || num_pal > 256)
541 {
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500542 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
543 {
544 png_error(png_ptr, "Invalid number of colors in palette");
545 }
546 else
547 {
548 png_warning(png_ptr, "Invalid number of colors in palette");
549 return;
550 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500551 }
552
Andreas Dilger47a0c421997-05-16 02:46:07 -0500553 png_ptr->num_palette = (png_uint_16)num_pal;
554 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500555
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600556 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500557#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500559 {
560 buf[0] = pal_ptr->red;
561 buf[1] = pal_ptr->green;
562 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500563 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500564 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500565#else
566 /* This is a little slower but some buggy compilers need to do this instead */
567 pal_ptr=palette;
568 for (i = 0; i < num_pal; i++)
569 {
570 buf[0] = pal_ptr[i].red;
571 buf[1] = pal_ptr[i].green;
572 buf[2] = pal_ptr[i].blue;
573 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
574 }
575#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500577 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500578}
579
580/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500581void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500583{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600584#ifdef PNG_USE_LOCAL_ARRAYS
585 PNG_IDAT;
586#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500587 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600588 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500589 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500590}
591
592/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500593void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600594png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500595{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600596#ifdef PNG_USE_LOCAL_ARRAYS
597 PNG_IEND;
598#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500599 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600600 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500602}
603
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500604#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500605/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600606#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500607void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500609{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600610#ifdef PNG_USE_LOCAL_ARRAYS
611 PNG_gAMA;
612#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500613 png_uint_32 igamma;
614 png_byte buf[4];
615
Andreas Dilger47a0c421997-05-16 02:46:07 -0500616 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600617 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600618 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500619 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600620 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500621}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500622#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600623#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500624void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600625png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600626{
627#ifdef PNG_USE_LOCAL_ARRAYS
628 PNG_gAMA;
629#endif
630 png_byte buf[4];
631
632 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600633 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600634 png_save_uint_32(buf, file_gamma);
635 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
636}
637#endif
638#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500639
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600640#if defined(PNG_WRITE_sRGB_SUPPORTED)
641/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500642void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600643png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600644{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600645#ifdef PNG_USE_LOCAL_ARRAYS
646 PNG_sRGB;
647#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600648 png_byte buf[1];
649
650 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600651 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600652 png_warning(png_ptr,
653 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600654 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600655 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600656}
657#endif
658
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600659#if defined(PNG_WRITE_iCCP_SUPPORTED)
660/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500661void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600662png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
663 png_charp profile, int profile_len)
664{
665#ifdef PNG_USE_LOCAL_ARRAYS
666 PNG_iCCP;
667#endif
668 png_size_t name_len;
669 png_charp new_name;
670 compression_state comp;
671
672 png_debug(1, "in png_write_iCCP\n");
673 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
674 &new_name)) == 0)
675 {
676 png_warning(png_ptr, "Empty keyword in iCCP chunk");
677 return;
678 }
679
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600680 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600681 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600682
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500683 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600684 profile_len = 0;
685
686 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500687 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600688 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600689
690 /* make sure we include the NULL after the name and the compression type */
691 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
692 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600693 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600694 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
695
696 if (profile_len)
697 png_write_compressed_data_out(png_ptr, &comp);
698
699 png_write_chunk_end(png_ptr);
700 png_free(png_ptr, new_name);
701}
702#endif
703
704#if defined(PNG_WRITE_sPLT_SUPPORTED)
705/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500706void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600707png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600708{
709#ifdef PNG_USE_LOCAL_ARRAYS
710 PNG_sPLT;
711#endif
712 png_size_t name_len;
713 png_charp new_name;
714 png_byte entrybuf[10];
715 int entry_size = (spalette->depth == 8 ? 6 : 10);
716 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600717 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500718#ifdef PNG_NO_POINTER_INDEXING
719 int i;
720#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600721
722 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600723 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
724 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600725 {
726 png_warning(png_ptr, "Empty keyword in sPLT chunk");
727 return;
728 }
729
730 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500731 png_write_chunk_start(png_ptr, (png_bytep) png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600732 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600733 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600734 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600735
736 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500737#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600738 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
739 {
740 if (spalette->depth == 8)
741 {
742 entrybuf[0] = (png_byte)ep->red;
743 entrybuf[1] = (png_byte)ep->green;
744 entrybuf[2] = (png_byte)ep->blue;
745 entrybuf[3] = (png_byte)ep->alpha;
746 png_save_uint_16(entrybuf + 4, ep->frequency);
747 }
748 else
749 {
750 png_save_uint_16(entrybuf + 0, ep->red);
751 png_save_uint_16(entrybuf + 2, ep->green);
752 png_save_uint_16(entrybuf + 4, ep->blue);
753 png_save_uint_16(entrybuf + 6, ep->alpha);
754 png_save_uint_16(entrybuf + 8, ep->frequency);
755 }
756 png_write_chunk_data(png_ptr, entrybuf, entry_size);
757 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500758#else
759 ep=spalette->entries;
760 for (i=0; i>spalette->nentries; i++)
761 {
762 if (spalette->depth == 8)
763 {
764 entrybuf[0] = (png_byte)ep[i].red;
765 entrybuf[1] = (png_byte)ep[i].green;
766 entrybuf[2] = (png_byte)ep[i].blue;
767 entrybuf[3] = (png_byte)ep[i].alpha;
768 png_save_uint_16(entrybuf + 4, ep[i].frequency);
769 }
770 else
771 {
772 png_save_uint_16(entrybuf + 0, ep[i].red);
773 png_save_uint_16(entrybuf + 2, ep[i].green);
774 png_save_uint_16(entrybuf + 4, ep[i].blue);
775 png_save_uint_16(entrybuf + 6, ep[i].alpha);
776 png_save_uint_16(entrybuf + 8, ep[i].frequency);
777 }
778 png_write_chunk_data(png_ptr, entrybuf, entry_size);
779 }
780#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600781
782 png_write_chunk_end(png_ptr);
783 png_free(png_ptr, new_name);
784}
785#endif
786
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500787#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500788/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500789void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600790png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500791{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600792#ifdef PNG_USE_LOCAL_ARRAYS
793 PNG_sBIT;
794#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500795 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500796 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500797
Andreas Dilger47a0c421997-05-16 02:46:07 -0500798 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600799 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500800 if (color_type & PNG_COLOR_MASK_COLOR)
801 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600802 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500803
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500804 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
805 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600806 if (sbit->red == 0 || sbit->red > maxbits ||
807 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500808 sbit->blue == 0 || sbit->blue > maxbits)
809 {
810 png_warning(png_ptr, "Invalid sBIT depth specified");
811 return;
812 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500813 buf[0] = sbit->red;
814 buf[1] = sbit->green;
815 buf[2] = sbit->blue;
816 size = 3;
817 }
818 else
819 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500820 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
821 {
822 png_warning(png_ptr, "Invalid sBIT depth specified");
823 return;
824 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500825 buf[0] = sbit->gray;
826 size = 1;
827 }
828
829 if (color_type & PNG_COLOR_MASK_ALPHA)
830 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500831 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
832 {
833 png_warning(png_ptr, "Invalid sBIT depth specified");
834 return;
835 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500836 buf[size++] = sbit->alpha;
837 }
838
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600839 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500840}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500841#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500842
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500843#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500844/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600845#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500846void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600848 double red_x, double red_y, double green_x, double green_y,
849 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500850{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600851#ifdef PNG_USE_LOCAL_ARRAYS
852 PNG_cHRM;
853#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500854 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600855 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500856
Andreas Dilger47a0c421997-05-16 02:46:07 -0500857 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600858 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500859 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
860 white_x + white_y > 1.0)
861 {
862 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500863#if !defined(PNG_NO_CONSOLE_IO)
864 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600865#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500866 return;
867 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600868 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500869 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600870 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500871 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500872
873 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
874 red_x + red_y > 1.0)
875 {
876 png_warning(png_ptr, "Invalid cHRM red point specified");
877 return;
878 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600879 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500880 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600881 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500883
884 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
885 green_x + green_y > 1.0)
886 {
887 png_warning(png_ptr, "Invalid cHRM green point specified");
888 return;
889 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600890 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500891 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600892 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500893 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500894
895 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
896 blue_x + blue_y > 1.0)
897 {
898 png_warning(png_ptr, "Invalid cHRM blue point specified");
899 return;
900 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600901 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500902 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600903 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500904 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500905
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600906 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500907}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500908#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600909#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500910void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600911png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
912 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
913 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
914 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600915{
916#ifdef PNG_USE_LOCAL_ARRAYS
917 PNG_cHRM;
918#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600919 png_byte buf[32];
920
921 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600922 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600923 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
924 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600925 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500926#if !defined(PNG_NO_CONSOLE_IO)
927 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600928#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600929 return;
930 }
931 png_save_uint_32(buf, white_x);
932 png_save_uint_32(buf + 4, white_y);
933
934 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
935 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600936 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600937 return;
938 }
939 png_save_uint_32(buf + 8, red_x);
940 png_save_uint_32(buf + 12, red_y);
941
942 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
943 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600944 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600945 return;
946 }
947 png_save_uint_32(buf + 16, green_x);
948 png_save_uint_32(buf + 20, green_y);
949
950 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
951 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600952 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600953 return;
954 }
955 png_save_uint_32(buf + 24, blue_x);
956 png_save_uint_32(buf + 28, blue_y);
957
958 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
959}
960#endif
961#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500962
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500963#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500964/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500965void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600966png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500967 int num_trans, int color_type)
968{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600969#ifdef PNG_USE_LOCAL_ARRAYS
970 PNG_tRNS;
971#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500972 png_byte buf[6];
973
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500975 if (color_type == PNG_COLOR_TYPE_PALETTE)
976 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600977 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500978 {
979 png_warning(png_ptr,"Invalid number of transparent colors specified");
980 return;
981 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500982 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600983 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500984 }
985 else if (color_type == PNG_COLOR_TYPE_GRAY)
986 {
987 /* one 16 bit value */
988 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600989 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500990 }
991 else if (color_type == PNG_COLOR_TYPE_RGB)
992 {
993 /* three 16 bit values */
994 png_save_uint_16(buf, tran->red);
995 png_save_uint_16(buf + 2, tran->green);
996 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600997 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500998 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500999 else
1000 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001001 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001002 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001003}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001004#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001005
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001006#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001007/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001008void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001009png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001010{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001011#ifdef PNG_USE_LOCAL_ARRAYS
1012 PNG_bKGD;
1013#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001014 png_byte buf[6];
1015
Andreas Dilger47a0c421997-05-16 02:46:07 -05001016 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001017 if (color_type == PNG_COLOR_TYPE_PALETTE)
1018 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001019 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001020#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001021 (png_ptr->num_palette ||
1022 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001023#endif
1024 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001025 {
1026 png_warning(png_ptr, "Invalid background palette index");
1027 return;
1028 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001029 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001030 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001031 }
1032 else if (color_type & PNG_COLOR_MASK_COLOR)
1033 {
1034 png_save_uint_16(buf, back->red);
1035 png_save_uint_16(buf + 2, back->green);
1036 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001037 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001038 }
1039 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001040 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001041 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001042 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001043 }
1044}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001045#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001046
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001047#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001048/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001049void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001050png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001051{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001052#ifdef PNG_USE_LOCAL_ARRAYS
1053 PNG_hIST;
1054#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001055 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001056 png_byte buf[3];
1057
Andreas Dilger47a0c421997-05-16 02:46:07 -05001058 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001059 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001060 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1062 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001063 png_warning(png_ptr, "Invalid number of histogram entries specified");
1064 return;
1065 }
1066
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001067 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001068 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001069 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001070 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001071 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001072 }
1073 png_write_chunk_end(png_ptr);
1074}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001075#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001076
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001077#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1078 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001079/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1080 * and if invalid, correct the keyword rather than discarding the entire
1081 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1082 * length, forbids leading or trailing whitespace, multiple internal spaces,
1083 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001084 *
1085 * The new_key is allocated to hold the corrected keyword and must be freed
1086 * by the calling routine. This avoids problems with trying to write to
1087 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001088 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001089png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001090png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001091{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001093 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001094 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001095 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001096
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 png_debug(1, "in png_check_keyword\n");
1098 *new_key = NULL;
1099
1100 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001101 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001102 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001103 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001104 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001105
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1107
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001108 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001109
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001110 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001111 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001112 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001113 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001115#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001116 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001117
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001118 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001119 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001120#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001121 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001122#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123 *dp = ' ';
1124 }
1125 else
1126 {
1127 *dp = *kp;
1128 }
1129 }
1130 *dp = '\0';
1131
1132 /* Remove any trailing white space. */
1133 kp = *new_key + key_len - 1;
1134 if (*kp == ' ')
1135 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001136 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137
1138 while (*kp == ' ')
1139 {
1140 *(kp--) = '\0';
1141 key_len--;
1142 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001143 }
1144
1145 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146 kp = *new_key;
1147 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001148 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001149 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150
1151 while (*kp == ' ')
1152 {
1153 kp++;
1154 key_len--;
1155 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001156 }
1157
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001159
Andreas Dilger47a0c421997-05-16 02:46:07 -05001160 /* Remove multiple internal spaces. */
1161 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001162 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001163 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001164 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165 *(dp++) = *kp;
1166 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001167 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001169 {
1170 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001171 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172 }
1173 else
1174 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001175 *(dp++) = *kp;
1176 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001177 }
1178 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001179 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001180 if(kwarn)
1181 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001182
1183 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001184 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001185 png_free(png_ptr, *new_key);
1186 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001187 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001188 }
1189
1190 if (key_len > 79)
1191 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001192 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001193 new_key[79] = '\0';
1194 key_len = 79;
1195 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001196
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001197 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198}
1199#endif
1200
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001201#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001202/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001203void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001204png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001206{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001207#ifdef PNG_USE_LOCAL_ARRAYS
1208 PNG_tEXt;
1209#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001210 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001211 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001212
Andreas Dilger47a0c421997-05-16 02:46:07 -05001213 png_debug(1, "in png_write_tEXt\n");
1214 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1215 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001216 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001217 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001218 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001219
Andreas Dilger47a0c421997-05-16 02:46:07 -05001220 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001221 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001222 else
1223 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001224
1225 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001226 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001227 /*
1228 * We leave it to the application to meet PNG-1.0 requirements on the
1229 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1230 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001231 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001232 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001233 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001234 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236
Guy Schalnat0d580581995-07-20 02:43:20 -05001237 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001238 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001239}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001240#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001241
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001242#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001243/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001244void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001245png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001247{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001248#ifdef PNG_USE_LOCAL_ARRAYS
1249 PNG_zTXt;
1250#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001252 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001253 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001254 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001255
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257
Andreas Dilger47a0c421997-05-16 02:46:07 -05001258 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001259 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001260 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001262 }
1263
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1265 {
1266 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1267 png_free(png_ptr, new_key);
1268 return;
1269 }
1270
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001271 text_len = png_strlen(text);
1272
Andreas Dilger47a0c421997-05-16 02:46:07 -05001273 png_free(png_ptr, new_key);
1274
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001275 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001276 text_len = png_text_compress(png_ptr, text, text_len, compression,
1277 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001278
1279 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001280 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1281 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001282 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001283 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001284 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001285 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001286 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001287 /* write the compressed data */
1288 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001289
Guy Schalnat0d580581995-07-20 02:43:20 -05001290 /* close the chunk */
1291 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001292}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001293#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001294
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001295#if defined(PNG_WRITE_iTXt_SUPPORTED)
1296/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001297void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001298png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001299 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001300{
1301#ifdef PNG_USE_LOCAL_ARRAYS
1302 PNG_iTXt;
1303#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001304 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001305 png_charp new_lang, new_key;
1306 png_byte cbuf[2];
1307 compression_state comp;
1308
1309 png_debug(1, "in png_write_iTXt\n");
1310
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001311 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1312 {
1313 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1314 return;
1315 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001316 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1317 &new_lang))==0)
1318 {
1319 png_warning(png_ptr, "Empty language field in iTXt chunk");
1320 return;
1321 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001322 lang_key_len = png_strlen(lang_key);
1323 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001324
1325 if (text == NULL || *text == '\0')
1326 text_len = 0;
1327
1328 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001329 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1330 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001331
1332 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001333 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001334
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001335 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1336 (png_uint_32)(
1337 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1338 + key_len
1339 + lang_len
1340 + lang_key_len
1341 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001342
1343 /*
1344 * We leave it to the application to meet PNG-1.0 requirements on the
1345 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1346 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1347 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1348 */
1349 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001350
1351 /* set the compression flag */
1352 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1353 compression == PNG_TEXT_COMPRESSION_NONE)
1354 cbuf[0] = 0;
1355 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1356 cbuf[0] = 1;
1357 /* set the compression method */
1358 cbuf[1] = 0;
1359 png_write_chunk_data(png_ptr, cbuf, 2);
1360
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001361 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001362 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001363 png_write_chunk_data(png_ptr, '\0', 1);
1364
1365 png_write_compressed_data_out(png_ptr, &comp);
1366
1367 png_write_chunk_end(png_ptr);
1368 png_free(png_ptr, new_key);
1369 png_free(png_ptr, new_lang);
1370}
1371#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372
1373#if defined(PNG_WRITE_oFFs_SUPPORTED)
1374/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001375void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001376png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1377 png_uint_32 y_offset,
1378 int unit_type)
1379{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001380#ifdef PNG_USE_LOCAL_ARRAYS
1381 PNG_oFFs;
1382#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383 png_byte buf[9];
1384
1385 png_debug(1, "in png_write_oFFs\n");
1386 if (unit_type >= PNG_OFFSET_LAST)
1387 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1388
1389 png_save_uint_32(buf, x_offset);
1390 png_save_uint_32(buf + 4, y_offset);
1391 buf[8] = (png_byte)unit_type;
1392
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001393 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001394}
1395#endif
1396
1397#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001398/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001399void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001400png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1401 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1402{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001403#ifdef PNG_USE_LOCAL_ARRAYS
1404 PNG_pCAL;
1405#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001406 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001407 png_uint_32p params_len;
1408 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001409 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001410 int i;
1411
1412 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1413 if (type >= PNG_EQUATION_LAST)
1414 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1415
1416 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001417 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001419 png_debug1(3, "pCAL units length = %d\n", units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001420 total_len = purpose_len + units_len + 10;
1421
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001422 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1423 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001424
1425 /* Find the length of each parameter, making sure we don't count the
1426 null terminator for the last parameter. */
1427 for (i = 0; i < nparams; i++)
1428 {
1429 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001430 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001431 total_len += (png_size_t)params_len[i];
1432 }
1433
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001434 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001435 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001436 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001437 png_save_int_32(buf, X0);
1438 png_save_int_32(buf + 4, X1);
1439 buf[8] = (png_byte)type;
1440 buf[9] = (png_byte)nparams;
1441 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1442 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1443
1444 png_free(png_ptr, new_purpose);
1445
1446 for (i = 0; i < nparams; i++)
1447 {
1448 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1449 (png_size_t)params_len[i]);
1450 }
1451
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001452 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001453 png_write_chunk_end(png_ptr);
1454}
1455#endif
1456
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457#if defined(PNG_WRITE_sCAL_SUPPORTED)
1458/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001459#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001460void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001461png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001462{
1463#ifdef PNG_USE_LOCAL_ARRAYS
1464 PNG_sCAL;
1465#endif
1466 png_size_t total_len;
1467 char wbuf[32], hbuf[32];
1468
1469 png_debug(1, "in png_write_sCAL\n");
1470
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001471#if defined(_WIN32_WCE)
1472/* sprintf() function is not supported on WindowsCE */
1473 {
1474 wchar_t wc_buf[32];
1475 swprintf(wc_buf, TEXT("%12.12e"), width);
1476 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1477 swprintf(wc_buf, TEXT("%12.12e"), height);
1478 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1479 }
1480#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001481 sprintf(wbuf, "%12.12e", width);
1482 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001483#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001484 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001485
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001486 png_debug1(3, "sCAL total length = %d\n", total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001487 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001488 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001489 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1490 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001491
1492 png_write_chunk_end(png_ptr);
1493}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001494#else
1495#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001496void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001497png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001498 png_charp height)
1499{
1500#ifdef PNG_USE_LOCAL_ARRAYS
1501 PNG_sCAL;
1502#endif
1503 png_size_t total_len;
1504 char wbuf[32], hbuf[32];
1505
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001506 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001507
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001508 strcpy(wbuf,(const char *)width);
1509 strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001510 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001511
1512 png_debug1(3, "sCAL total length = %d\n", total_len);
1513 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001514 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001515 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1516 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001517
1518 png_write_chunk_end(png_ptr);
1519}
1520#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001521#endif
1522#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001523
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001524#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001525/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001526void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001527png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001528 png_uint_32 y_pixels_per_unit,
1529 int unit_type)
1530{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001531#ifdef PNG_USE_LOCAL_ARRAYS
1532 PNG_pHYs;
1533#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001534 png_byte buf[9];
1535
Andreas Dilger47a0c421997-05-16 02:46:07 -05001536 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001537 if (unit_type >= PNG_RESOLUTION_LAST)
1538 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1539
Guy Schalnat0d580581995-07-20 02:43:20 -05001540 png_save_uint_32(buf, x_pixels_per_unit);
1541 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001542 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001543
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001544 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001545}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001546#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001547
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001548#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001549/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1550 * or png_convert_from_time_t(), or fill in the structure yourself.
1551 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001552void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001553png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001554{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001555#ifdef PNG_USE_LOCAL_ARRAYS
1556 PNG_tIME;
1557#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001558 png_byte buf[7];
1559
Andreas Dilger47a0c421997-05-16 02:46:07 -05001560 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001561 if (mod_time->month > 12 || mod_time->month < 1 ||
1562 mod_time->day > 31 || mod_time->day < 1 ||
1563 mod_time->hour > 23 || mod_time->second > 60)
1564 {
1565 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1566 return;
1567 }
1568
Guy Schalnat0d580581995-07-20 02:43:20 -05001569 png_save_uint_16(buf, mod_time->year);
1570 buf[2] = mod_time->month;
1571 buf[3] = mod_time->day;
1572 buf[4] = mod_time->hour;
1573 buf[5] = mod_time->minute;
1574 buf[6] = mod_time->second;
1575
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001576 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001577}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001578#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001579
1580/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001581void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001582png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001583{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001584#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001585 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001586
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001587 /* start of interlace block */
1588 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001589
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001590 /* offset to next interlace block */
1591 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001592
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001593 /* start of interlace block in the y direction */
1594 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001595
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001596 /* offset to next interlace block in the y direction */
1597 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001598#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001599
Andreas Dilger47a0c421997-05-16 02:46:07 -05001600 png_size_t buf_size;
1601
1602 png_debug(1, "in png_write_start_row\n");
1603 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1604 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1605
Guy Schalnat0d580581995-07-20 02:43:20 -05001606 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001607 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001608 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001609
1610 /* set up filtering buffer, if using this filter */
1611 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001612 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001613 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001614 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001615 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001616 }
1617
Andreas Dilger47a0c421997-05-16 02:46:07 -05001618 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001619 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1620 {
1621 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001622 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001623 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001624
1625 if (png_ptr->do_filter & PNG_FILTER_UP)
1626 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001627 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001628 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001629 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001630 }
1631
1632 if (png_ptr->do_filter & PNG_FILTER_AVG)
1633 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001634 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1635 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001636 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001637 }
1638
1639 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1640 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001641 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001642 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001643 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001644 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001645 }
1646
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001647#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001648 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001649 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001650 {
1651 if (!(png_ptr->transformations & PNG_INTERLACE))
1652 {
1653 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1654 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001655 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1656 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001657 }
1658 else
1659 {
1660 png_ptr->num_rows = png_ptr->height;
1661 png_ptr->usr_width = png_ptr->width;
1662 }
1663 }
1664 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001665#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001666 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001667 png_ptr->num_rows = png_ptr->height;
1668 png_ptr->usr_width = png_ptr->width;
1669 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001670 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1671 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001672}
1673
Andreas Dilger47a0c421997-05-16 02:46:07 -05001674/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001675void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001676png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001677{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001678#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001679 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001680
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001681 /* start of interlace block */
1682 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001683
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001684 /* offset to next interlace block */
1685 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001686
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001687 /* start of interlace block in the y direction */
1688 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001689
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001690 /* offset to next interlace block in the y direction */
1691 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001692#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001693
Guy Schalnat0d580581995-07-20 02:43:20 -05001694 int ret;
1695
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001697 /* next row */
1698 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001699
Guy Schalnat0d580581995-07-20 02:43:20 -05001700 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001701 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001702 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001703
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001704#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001705 /* if interlaced, go to next pass */
1706 if (png_ptr->interlaced)
1707 {
1708 png_ptr->row_number = 0;
1709 if (png_ptr->transformations & PNG_INTERLACE)
1710 {
1711 png_ptr->pass++;
1712 }
1713 else
1714 {
1715 /* loop until we find a non-zero width or height pass */
1716 do
1717 {
1718 png_ptr->pass++;
1719 if (png_ptr->pass >= 7)
1720 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001721 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001722 png_pass_inc[png_ptr->pass] - 1 -
1723 png_pass_start[png_ptr->pass]) /
1724 png_pass_inc[png_ptr->pass];
1725 png_ptr->num_rows = (png_ptr->height +
1726 png_pass_yinc[png_ptr->pass] - 1 -
1727 png_pass_ystart[png_ptr->pass]) /
1728 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001729 if (png_ptr->transformations & PNG_INTERLACE)
1730 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001731 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1732
1733 }
1734
Guy Schalnate5a37791996-06-05 15:50:50 -05001735 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001736 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001737 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001738 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001739 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001740 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001741 (png_uint_32)png_ptr->usr_bit_depth *
1742 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001743 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001744 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001745 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001746#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001747
1748 /* if we get here, we've just written the last row, so we need
1749 to flush the compressor */
1750 do
1751 {
1752 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001753 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001754 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001755 if (ret == Z_OK)
1756 {
1757 /* check to see if we need more room */
1758 if (!(png_ptr->zstream.avail_out))
1759 {
1760 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1761 png_ptr->zstream.next_out = png_ptr->zbuf;
1762 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1763 }
1764 }
1765 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001766 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001767 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001768 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001769 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001770 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001771 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001772 } while (ret != Z_STREAM_END);
1773
1774 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001775 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001776 {
1777 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001778 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001779 }
1780
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001781 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001782}
1783
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001784#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001785/* Pick out the correct pixels for the interlace pass.
1786 * The basic idea here is to go through the row with a source
1787 * pointer and a destination pointer (sp and dp), and copy the
1788 * correct pixels for the pass. As the row gets compacted,
1789 * sp will always be >= dp, so we should never overwrite anything.
1790 * See the default: case for the easiest code to understand.
1791 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001792void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001793png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001794{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001795#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001796 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001797
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001798 /* start of interlace block */
1799 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001800
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001801 /* offset to next interlace block */
1802 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001803#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001804
Andreas Dilger47a0c421997-05-16 02:46:07 -05001805 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001806 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001807#if defined(PNG_USELESS_TESTS_SUPPORTED)
1808 if (row != NULL && row_info != NULL && pass < 6)
1809#else
1810 if (pass < 6)
1811#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001812 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001813 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001814 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001815 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 case 1:
1817 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001818 png_bytep sp;
1819 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001820 int shift;
1821 int d;
1822 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001823 png_uint_32 i;
1824 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001825
1826 dp = row;
1827 d = 0;
1828 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001829 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001830 i += png_pass_inc[pass])
1831 {
1832 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001833 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001834 d |= (value << shift);
1835
1836 if (shift == 0)
1837 {
1838 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001839 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001840 d = 0;
1841 }
1842 else
1843 shift--;
1844
1845 }
1846 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001847 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 break;
1849 }
1850 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001851 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001852 png_bytep sp;
1853 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001854 int shift;
1855 int d;
1856 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001857 png_uint_32 i;
1858 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001859
1860 dp = row;
1861 shift = 6;
1862 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001863 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001864 i += png_pass_inc[pass])
1865 {
1866 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001867 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001868 d |= (value << shift);
1869
1870 if (shift == 0)
1871 {
1872 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001873 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001874 d = 0;
1875 }
1876 else
1877 shift -= 2;
1878 }
1879 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001880 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001881 break;
1882 }
1883 case 4:
1884 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001885 png_bytep sp;
1886 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001887 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001888 int d;
1889 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001890 png_uint_32 i;
1891 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001892
1893 dp = row;
1894 shift = 4;
1895 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001896 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001897 i += png_pass_inc[pass])
1898 {
1899 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001900 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001901 d |= (value << shift);
1902
1903 if (shift == 0)
1904 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001905 shift = 4;
1906 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001907 d = 0;
1908 }
1909 else
1910 shift -= 4;
1911 }
1912 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001913 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001914 break;
1915 }
1916 default:
1917 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001918 png_bytep sp;
1919 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001920 png_uint_32 i;
1921 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001922 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001923
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001924 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 dp = row;
1926 /* find out how many bytes each pixel takes up */
1927 pixel_bytes = (row_info->pixel_depth >> 3);
1928 /* loop through the row, only looking at the pixels that
1929 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001930 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001931 i += png_pass_inc[pass])
1932 {
1933 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001934 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001935 /* move the pixel */
1936 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001937 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001938 /* next pixel */
1939 dp += pixel_bytes;
1940 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001941 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001942 }
1943 }
1944 /* set new row width */
1945 row_info->width = (row_info->width +
1946 png_pass_inc[pass] - 1 -
1947 png_pass_start[pass]) /
1948 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001949 row_info->rowbytes = ((row_info->width *
1950 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001951 }
1952}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001953#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001954
Andreas Dilger47a0c421997-05-16 02:46:07 -05001955/* This filters the row, chooses which filter to use, if it has not already
1956 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001957 * chosen filter.
1958 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001959#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001961#define PNG_LOMASK ((png_uint_32)0xffffL)
1962#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001963void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001964png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001965{
Guy Schalnate5a37791996-06-05 15:50:50 -05001966 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001967 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001968 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001969 png_uint_32 row_bytes = row_info->rowbytes;
1970#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1971 int num_p_filters = (int)png_ptr->num_prev_filters;
1972#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001973
Andreas Dilger47a0c421997-05-16 02:46:07 -05001974 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001975 /* find out how many bytes offset each pixel is */
1976 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001977
1978 prev_row = png_ptr->prev_row;
1979 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001980 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001981
Andreas Dilger47a0c421997-05-16 02:46:07 -05001982 /* The prediction method we use is to find which method provides the
1983 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001984 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001985 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001986 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001987 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001988 * predictive" method (not implemented yet), which does test compressions
1989 * of lines using different filter methods, and then chooses the
1990 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001991 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001992 *
1993 * GRR 980525: consider also
1994 * (1) minimum sum of absolute differences from running average (i.e.,
1995 * keep running sum of non-absolute differences & count of bytes)
1996 * [track dispersion, too? restart average if dispersion too large?]
1997 * (1b) minimum sum of absolute differences from sliding average, probably
1998 * with window size <= deflate window (usually 32K)
1999 * (2) minimum sum of squared differences from zero or running average
2000 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002001 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002002
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002003
Guy Schalnate5a37791996-06-05 15:50:50 -05002004 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002005 * that has been chosen, as it doesn't actually do anything to the data.
2006 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002007 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002008 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002009 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002010 png_bytep rp;
2011 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002013 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002014
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002015 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002016 {
2017 v = *rp;
2018 sum += (v < 128) ? v : 256 - v;
2019 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002020
2021#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2022 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2023 {
2024 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002025 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002026 sumlo = sum & PNG_LOMASK;
2027 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2028
2029 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002030 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002031 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002032 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002034 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002035 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002036 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002037 PNG_WEIGHT_SHIFT;
2038 }
2039 }
2040
2041 /* Factor in the cost of this filter (this is here for completeness,
2042 * but it makes no sense to have a "cost" for the NONE filter, as
2043 * it has the minimum possible computational cost - none).
2044 */
2045 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2046 PNG_COST_SHIFT;
2047 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2048 PNG_COST_SHIFT;
2049
2050 if (sumhi > PNG_HIMASK)
2051 sum = PNG_MAXSUM;
2052 else
2053 sum = (sumhi << PNG_HISHIFT) + sumlo;
2054 }
2055#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002056 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002057 }
2058
Guy Schalnate5a37791996-06-05 15:50:50 -05002059 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002060 if (filter_to_do == PNG_FILTER_SUB)
2061 /* it's the only filter so no testing is needed */
2062 {
2063 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002064 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002065 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2066 i++, rp++, dp++)
2067 {
2068 *dp = *rp;
2069 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002070 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002071 i++, rp++, lp++, dp++)
2072 {
2073 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2074 }
2075 best_row = png_ptr->sub_row;
2076 }
2077
2078 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002079 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002080 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002081 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002082 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002083 int v;
2084
2085#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002086 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002087 * would reduce the sum of this filter, so that we can do the
2088 * early exit comparison without scaling the sum each time.
2089 */
2090 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2091 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002092 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002093 png_uint_32 lmhi, lmlo;
2094 lmlo = lmins & PNG_LOMASK;
2095 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2096
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002097 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002098 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002099 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002100 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002101 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002102 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002103 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002104 PNG_WEIGHT_SHIFT;
2105 }
2106 }
2107
2108 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2109 PNG_COST_SHIFT;
2110 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2111 PNG_COST_SHIFT;
2112
2113 if (lmhi > PNG_HIMASK)
2114 lmins = PNG_MAXSUM;
2115 else
2116 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2117 }
2118#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002119
Guy Schalnate5a37791996-06-05 15:50:50 -05002120 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2121 i++, rp++, dp++)
2122 {
2123 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002124
Guy Schalnate5a37791996-06-05 15:50:50 -05002125 sum += (v < 128) ? v : 256 - v;
2126 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002127 for (lp = row_buf + 1; i < row_info->rowbytes;
2128 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002129 {
2130 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002131
Guy Schalnate5a37791996-06-05 15:50:50 -05002132 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002133
2134 if (sum > lmins) /* We are already worse, don't continue. */
2135 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002136 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137
2138#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2139 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2140 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002141 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002142 png_uint_32 sumhi, sumlo;
2143 sumlo = sum & PNG_LOMASK;
2144 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2145
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002146 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002147 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002148 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002149 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002150 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002151 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002152 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002153 PNG_WEIGHT_SHIFT;
2154 }
2155 }
2156
2157 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2158 PNG_COST_SHIFT;
2159 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2160 PNG_COST_SHIFT;
2161
2162 if (sumhi > PNG_HIMASK)
2163 sum = PNG_MAXSUM;
2164 else
2165 sum = (sumhi << PNG_HISHIFT) + sumlo;
2166 }
2167#endif
2168
Guy Schalnate5a37791996-06-05 15:50:50 -05002169 if (sum < mins)
2170 {
2171 mins = sum;
2172 best_row = png_ptr->sub_row;
2173 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002174 }
2175
Guy Schalnate5a37791996-06-05 15:50:50 -05002176 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002177 if (filter_to_do == PNG_FILTER_UP)
2178 {
2179 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002180 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002181
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002182 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002183 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002184 i++, rp++, pp++, dp++)
2185 {
2186 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2187 }
2188 best_row = png_ptr->up_row;
2189 }
2190
2191 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002192 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002193 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 int v;
2197
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002198
Andreas Dilger47a0c421997-05-16 02:46:07 -05002199#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2200 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2201 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002202 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002203 png_uint_32 lmhi, lmlo;
2204 lmlo = lmins & PNG_LOMASK;
2205 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2206
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002207 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002208 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002209 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002210 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002211 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002212 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002213 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002214 PNG_WEIGHT_SHIFT;
2215 }
2216 }
2217
2218 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2219 PNG_COST_SHIFT;
2220 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2221 PNG_COST_SHIFT;
2222
2223 if (lmhi > PNG_HIMASK)
2224 lmins = PNG_MAXSUM;
2225 else
2226 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2227 }
2228#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002229
2230 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002231 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002232 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002233 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002234
2235 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002236
2237 if (sum > lmins) /* We are already worse, don't continue. */
2238 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002239 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002240
2241#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2242 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2243 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002244 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 png_uint_32 sumhi, sumlo;
2246 sumlo = sum & PNG_LOMASK;
2247 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2248
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002249 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002250 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002251 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002252 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002253 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002254 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002255 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002256 PNG_WEIGHT_SHIFT;
2257 }
2258 }
2259
2260 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2261 PNG_COST_SHIFT;
2262 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2263 PNG_COST_SHIFT;
2264
2265 if (sumhi > PNG_HIMASK)
2266 sum = PNG_MAXSUM;
2267 else
2268 sum = (sumhi << PNG_HISHIFT) + sumlo;
2269 }
2270#endif
2271
Guy Schalnate5a37791996-06-05 15:50:50 -05002272 if (sum < mins)
2273 {
2274 mins = sum;
2275 best_row = png_ptr->up_row;
2276 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002277 }
2278
Guy Schalnate5a37791996-06-05 15:50:50 -05002279 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002280 if (filter_to_do == PNG_FILTER_AVG)
2281 {
2282 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002283 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002284 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002285 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002286 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002287 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002288 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002289 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002290 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002291 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2292 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002293 }
2294 best_row = png_ptr->avg_row;
2295 }
2296
2297 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002298 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002299 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002302 int v;
2303
2304#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2305 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2306 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002307 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 png_uint_32 lmhi, lmlo;
2309 lmlo = lmins & PNG_LOMASK;
2310 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2311
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002312 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002313 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002314 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002315 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002316 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002317 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002318 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002319 PNG_WEIGHT_SHIFT;
2320 }
2321 }
2322
2323 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2324 PNG_COST_SHIFT;
2325 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2326 PNG_COST_SHIFT;
2327
2328 if (lmhi > PNG_HIMASK)
2329 lmins = PNG_MAXSUM;
2330 else
2331 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2332 }
2333#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002334
2335 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002336 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002337 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002338 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002339
2340 sum += (v < 128) ? v : 256 - v;
2341 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002342 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002343 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002344 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002345 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002346
2347 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348
2349 if (sum > lmins) /* We are already worse, don't continue. */
2350 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002351 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352
2353#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2354 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2355 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002356 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002357 png_uint_32 sumhi, sumlo;
2358 sumlo = sum & PNG_LOMASK;
2359 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2360
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002361 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002362 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002363 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002364 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002365 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002366 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002367 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002368 PNG_WEIGHT_SHIFT;
2369 }
2370 }
2371
2372 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2373 PNG_COST_SHIFT;
2374 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2375 PNG_COST_SHIFT;
2376
2377 if (sumhi > PNG_HIMASK)
2378 sum = PNG_MAXSUM;
2379 else
2380 sum = (sumhi << PNG_HISHIFT) + sumlo;
2381 }
2382#endif
2383
Guy Schalnate5a37791996-06-05 15:50:50 -05002384 if (sum < mins)
2385 {
2386 mins = sum;
2387 best_row = png_ptr->avg_row;
2388 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002389 }
2390
Andreas Dilger47a0c421997-05-16 02:46:07 -05002391 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002392 if (filter_to_do == PNG_FILTER_PAETH)
2393 {
2394 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002395 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002396 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002397 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002398 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002399 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002400 }
2401
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002402 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002403 {
2404 int a, b, c, pa, pb, pc, p;
2405
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002406 b = *pp++;
2407 c = *cp++;
2408 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002409
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 p = b - c;
2411 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002412
2413#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002414 pa = abs(p);
2415 pb = abs(pc);
2416 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002417#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002418 pa = p < 0 ? -p : p;
2419 pb = pc < 0 ? -pc : pc;
2420 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002421#endif
2422
2423 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2424
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002425 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002426 }
2427 best_row = png_ptr->paeth_row;
2428 }
2429
2430 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002431 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002432 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002433 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002434 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002435 int v;
2436
2437#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2438 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2439 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002440 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002441 png_uint_32 lmhi, lmlo;
2442 lmlo = lmins & PNG_LOMASK;
2443 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2444
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002445 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002446 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002448 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002451 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002452 PNG_WEIGHT_SHIFT;
2453 }
2454 }
2455
2456 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2457 PNG_COST_SHIFT;
2458 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2459 PNG_COST_SHIFT;
2460
2461 if (lmhi > PNG_HIMASK)
2462 lmins = PNG_MAXSUM;
2463 else
2464 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2465 }
2466#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002467
2468 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002469 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002470 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002471 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002472
2473 sum += (v < 128) ? v : 256 - v;
2474 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002475
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002476 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002477 {
2478 int a, b, c, pa, pb, pc, p;
2479
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002480 b = *pp++;
2481 c = *cp++;
2482 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002483
2484#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002485 p = b - c;
2486 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002487#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002488 pa = abs(p);
2489 pb = abs(pc);
2490 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002491#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002492 pa = p < 0 ? -p : p;
2493 pb = pc < 0 ? -pc : pc;
2494 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002495#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002496 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2497#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002498 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002499 pa = abs(p - a);
2500 pb = abs(p - b);
2501 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002502 if (pa <= pb && pa <= pc)
2503 p = a;
2504 else if (pb <= pc)
2505 p = b;
2506 else
2507 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002508#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002509
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002510 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002511
2512 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002513
2514 if (sum > lmins) /* We are already worse, don't continue. */
2515 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002516 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517
2518#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2519 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2520 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002521 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002522 png_uint_32 sumhi, sumlo;
2523 sumlo = sum & PNG_LOMASK;
2524 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2525
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002526 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002527 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002528 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002529 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002530 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002531 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002532 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002533 PNG_WEIGHT_SHIFT;
2534 }
2535 }
2536
2537 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2538 PNG_COST_SHIFT;
2539 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2540 PNG_COST_SHIFT;
2541
2542 if (sumhi > PNG_HIMASK)
2543 sum = PNG_MAXSUM;
2544 else
2545 sum = (sumhi << PNG_HISHIFT) + sumlo;
2546 }
2547#endif
2548
Guy Schalnate5a37791996-06-05 15:50:50 -05002549 if (sum < mins)
2550 {
2551 best_row = png_ptr->paeth_row;
2552 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002553 }
2554
Andreas Dilger47a0c421997-05-16 02:46:07 -05002555 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002556
Guy Schalnate5a37791996-06-05 15:50:50 -05002557 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002558
2559#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2560 /* Save the type of filter we picked this time for future calculations */
2561 if (png_ptr->num_prev_filters > 0)
2562 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 int j;
2564 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002565 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002566 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002567 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002568 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002569 }
2570#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002571}
2572
2573
Andreas Dilger47a0c421997-05-16 02:46:07 -05002574/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002575void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002576png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2577{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002578 png_debug(1, "in png_write_filtered_row\n");
2579 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002580 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002581
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002582 png_ptr->zstream.next_in = filtered_row;
2583 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002584 /* repeat until we have compressed all the data */
2585 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002586 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002587 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002588
Guy Schalnate5a37791996-06-05 15:50:50 -05002589 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002590 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002591 /* check for compression errors */
2592 if (ret != Z_OK)
2593 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002594 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002595 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002596 else
2597 png_error(png_ptr, "zlib error");
2598 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002599
Guy Schalnate5a37791996-06-05 15:50:50 -05002600 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002601 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002602 {
2603 /* write the IDAT and reset the zlib output buffer */
2604 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002605 png_ptr->zstream.next_out = png_ptr->zbuf;
2606 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002607 }
2608 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002609 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002610
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002611 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002612 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002613 {
2614 png_bytep tptr;
2615
2616 tptr = png_ptr->prev_row;
2617 png_ptr->prev_row = png_ptr->row_buf;
2618 png_ptr->row_buf = tptr;
2619 }
2620
Guy Schalnate5a37791996-06-05 15:50:50 -05002621 /* finish row - updates counters and flushes zlib if last row */
2622 png_write_finish_row(png_ptr);
2623
2624#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2625 png_ptr->flush_rows++;
2626
2627 if (png_ptr->flush_dist > 0 &&
2628 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002629 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002630 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002631 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002632#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002633}