blob: da3107999b1d795a9f94f526b52ec3c19b0e60c4 [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-Pehrson3097f612001-05-07 14:52:45 -05004 * libpng 1.2.0beta2 - May 7, 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"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050013#ifdef PNG_WRITE_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
18 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050019void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Andreas Dilger47a0c421997-05-16 02:46:07 -050028#if defined(PNG_WRITE_pCAL_SUPPORTED)
29/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050033void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050034png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
41#endif
42
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050047void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Andreas Dilger47a0c421997-05-16 02:46:07 -050054/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 * representing the chunk name. The array must be at least 4 bytes in
56 * length, and does not need to be null terminated. To be safe, pass the
57 * pre-defined chunk names here, and if you need a new one, define it
58 * where the others are defined. The length is the length of the data.
59 * All the data must be present. If that is not possible, use the
60 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
61 * functions instead.
62 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050063void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050066{
Andreas Dilger47a0c421997-05-16 02:46:07 -050067 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 png_write_chunk_data(png_ptr, data, length);
69 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050070}
71
Andreas Dilger47a0c421997-05-16 02:46:07 -050072/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073 * The total_length is the sum of the lengths of all the data you will be
74 * passing in png_write_chunk_data().
75 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050076void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
78 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050079{
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050081 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050082
Guy Schalnat0d580581995-07-20 02:43:20 -050083 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050084 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
86
Guy Schalnat0d580581995-07-20 02:43:20 -050087 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050089 /* reset the crc and run it over the chunk name */
90 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050092}
93
Andreas Dilger47a0c421997-05-16 02:46:07 -050094/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095 * Note that multiple calls to this function are allowed, and that the
96 * sum of the lengths from these calls *must* add up to the total_length
97 * given to png_write_chunk_start().
98 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050099void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 /* write the data, and run the CRC over it */
103 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 {
105 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600106 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 }
108}
109
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600112png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 png_byte buf[4];
115
Guy Schalnat0d580581995-07-20 02:43:20 -0500116 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118
119 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500120}
121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600122/* Simple function to write the signature. If we have already written
123 * the magic bytes of the signature, or more likely, the PNG stream is
124 * being embedded into another stream and doesn't need its own signature,
125 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600126 * bytes have already been written.
127 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500128void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600129png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600132 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600133 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 (png_size_t)8 - png_ptr->sig_bytes);
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600135 if(png_ptr->sig_bytes < 3)
136 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137}
138
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600139#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600140/*
141 * This pair of functions encapsulates the operation of (a) compressing a
142 * text string, and (b) issuing it later as a series of chunk data writes.
143 * The compression_state structure is shared context for these functions
144 * set up by the caller in order to make the whole mess thread-safe.
145 */
146
147typedef struct
148{
149 char *input; /* the uncompressed input data */
150 int input_len; /* its length */
151 int num_output_ptr; /* number of output pointers used */
152 int max_output_ptr; /* size of output_ptr */
153 png_charpp output_ptr; /* array of pointers to output */
154} compression_state;
155
156/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500157static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600158png_text_compress(png_structp png_ptr,
159 png_charp text, png_size_t text_len, int compression,
160 compression_state *comp)
161{
162 int ret;
163
164 comp->num_output_ptr = comp->max_output_ptr = 0;
165 comp->output_ptr = NULL;
166 comp->input = NULL;
167
168 /* we may just want to pass the text right through */
169 if (compression == PNG_TEXT_COMPRESSION_NONE)
170 {
171 comp->input = text;
172 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500173 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600174 }
175
176 if (compression >= PNG_TEXT_COMPRESSION_LAST)
177 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500178#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600179 char msg[50];
180 sprintf(msg, "Unknown compression type %d", compression);
181 png_warning(png_ptr, msg);
182#else
183 png_warning(png_ptr, "Unknown compression type");
184#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600185 }
186
187 /* We can't write the chunk until we find out how much data we have,
188 * which means we need to run the compressor first and save the
189 * output. This shouldn't be a problem, as the vast majority of
190 * comments should be reasonable, but we will set up an array of
191 * malloc'd pointers to be sure.
192 *
193 * If we knew the application was well behaved, we could simplify this
194 * greatly by assuming we can always malloc an output buffer large
195 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
196 * and malloc this directly. The only time this would be a bad idea is
197 * if we can't malloc more than 64K and we have 64K of random input
198 * data, or if the input string is incredibly large (although this
199 * wouldn't cause a failure, just a slowdown due to swapping).
200 */
201
202 /* set up the compression buffers */
203 png_ptr->zstream.avail_in = (uInt)text_len;
204 png_ptr->zstream.next_in = (Bytef *)text;
205 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
206 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
207
208 /* this is the same compression loop as in png_write_row() */
209 do
210 {
211 /* compress the data */
212 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
213 if (ret != Z_OK)
214 {
215 /* error */
216 if (png_ptr->zstream.msg != NULL)
217 png_error(png_ptr, png_ptr->zstream.msg);
218 else
219 png_error(png_ptr, "zlib error");
220 }
221 /* check to see if we need more room */
222 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
223 {
224 /* make sure the output array has room */
225 if (comp->num_output_ptr >= comp->max_output_ptr)
226 {
227 int old_max;
228
229 old_max = comp->max_output_ptr;
230 comp->max_output_ptr = comp->num_output_ptr + 4;
231 if (comp->output_ptr != NULL)
232 {
233 png_charpp old_ptr;
234
235 old_ptr = comp->output_ptr;
236 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
237 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500238 png_memcpy(comp->output_ptr, old_ptr, old_max
239 * sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600240 png_free(png_ptr, old_ptr);
241 }
242 else
243 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
244 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
245 }
246
247 /* save the data */
248 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
249 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500250 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
251 png_ptr->zbuf_size);
252 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600253
254 /* and reset the buffer */
255 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
256 png_ptr->zstream.next_out = png_ptr->zbuf;
257 }
258 /* continue until we don't have any more to compress */
259 } while (png_ptr->zstream.avail_in);
260
261 /* finish the compression */
262 do
263 {
264 /* tell zlib we are finished */
265 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500266
267 if (ret == Z_OK)
268 {
269 /* check to see if we need more room */
270 if (!(png_ptr->zstream.avail_out))
271 {
272 /* check to make sure our output array has room */
273 if (comp->num_output_ptr >= comp->max_output_ptr)
274 {
275 int old_max;
276
277 old_max = comp->max_output_ptr;
278 comp->max_output_ptr = comp->num_output_ptr + 4;
279 if (comp->output_ptr != NULL)
280 {
281 png_charpp old_ptr;
282
283 old_ptr = comp->output_ptr;
284 /* This could be optimized to realloc() */
285 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
286 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500287 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500288 old_max * sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500289 png_free(png_ptr, old_ptr);
290 }
291 else
292 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
293 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
294 }
295
296 /* save off the data */
297 comp->output_ptr[comp->num_output_ptr] =
298 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500299 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
300 png_ptr->zbuf_size);
301 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500302
303 /* and reset the buffer pointers */
304 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
305 png_ptr->zstream.next_out = png_ptr->zbuf;
306 }
307 }
308 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600309 {
310 /* we got an error */
311 if (png_ptr->zstream.msg != NULL)
312 png_error(png_ptr, png_ptr->zstream.msg);
313 else
314 png_error(png_ptr, "zlib error");
315 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600316 } while (ret != Z_STREAM_END);
317
318 /* text length is number of buffers plus last buffer */
319 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
320 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
321 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
322
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500323 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600324}
325
326/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500327static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600328png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
329{
330 int i;
331
332 /* handle the no-compression case */
333 if (comp->input)
334 {
335 png_write_chunk_data(png_ptr, (png_bytep)comp->input, comp->input_len);
336 return;
337 }
338
339 /* write saved output buffers, if any */
340 for (i = 0; i < comp->num_output_ptr; i++)
341 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600342 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
343 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600344 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500345 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600346 }
347 if (comp->max_output_ptr != 0)
348 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500349 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600350 /* write anything left in zbuf */
351 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
352 png_write_chunk_data(png_ptr, png_ptr->zbuf,
353 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
354
355 /* reset zlib for another zTXt/iTXt or the image data */
356 deflateReset(&png_ptr->zstream);
357
358}
359#endif
360
Guy Schalnat0d580581995-07-20 02:43:20 -0500361/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600362 * information. Note that the rest of this code depends upon this
363 * information being correct.
364 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500365void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600366png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500367 int bit_depth, int color_type, int compression_type, int filter_type,
368 int interlace_type)
369{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600370#ifdef PNG_USE_LOCAL_ARRAYS
371 PNG_IHDR;
372#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500373 png_byte buf[13]; /* buffer to store the IHDR info */
374
Andreas Dilger47a0c421997-05-16 02:46:07 -0500375 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500376 /* Check that we have valid input data from the application info */
377 switch (color_type)
378 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500380 switch (bit_depth)
381 {
382 case 1:
383 case 2:
384 case 4:
385 case 8:
386 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 }
389 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500390 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500391 if (bit_depth != 8 && bit_depth != 16)
392 png_error(png_ptr, "Invalid bit depth for RGB image");
393 png_ptr->channels = 3;
394 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500396 switch (bit_depth)
397 {
398 case 1:
399 case 2:
400 case 4:
401 case 8: png_ptr->channels = 1; break;
402 default: png_error(png_ptr, "Invalid bit depth for paletted image");
403 }
404 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500406 if (bit_depth != 8 && bit_depth != 16)
407 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
408 png_ptr->channels = 2;
409 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500411 if (bit_depth != 8 && bit_depth != 16)
412 png_error(png_ptr, "Invalid bit depth for RGBA image");
413 png_ptr->channels = 4;
414 break;
415 default:
416 png_error(png_ptr, "Invalid image color type specified");
417 }
418
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500420 {
421 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500422 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500423 }
424
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600425 /* Write filter_method 64 (intrapixel differencing) only if
426 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
427 * 2. Libpng did not write a PNG signature (this filter_method is only
428 * used in PNG datastreams that are embedded in MNG datastreams) and
429 * 3. The application called png_permit_mng_features with a mask that
430 * included PNG_FLAG_MNG_FILTER_64 and
431 * 4. The filter_method is 64 and
432 * 5. The color_type is RGB or RGBA
433 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600434 if (
435#if defined(PNG_MNG_FEATURES_SUPPORTED)
436 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600437 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500438 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600439 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600440 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
441#endif
442 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500443 {
444 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500446 }
447
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600448#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500449 if (interlace_type != PNG_INTERLACE_NONE &&
450 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500451 {
452 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500454 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600455#else
456 interlace_type=PNG_INTERLACE_NONE;
457#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500458
459 /* save off the relevent information */
460 png_ptr->bit_depth = (png_byte)bit_depth;
461 png_ptr->color_type = (png_byte)color_type;
462 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600463 png_ptr->filter_type = (png_byte)filter_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500464 png_ptr->width = width;
465 png_ptr->height = height;
466
467 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500468 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500469 /* set the usr info, so any transformations can modify it */
470 png_ptr->usr_width = png_ptr->width;
471 png_ptr->usr_bit_depth = png_ptr->bit_depth;
472 png_ptr->usr_channels = png_ptr->channels;
473
Guy Schalnat0d580581995-07-20 02:43:20 -0500474 /* pack the header information into the buffer */
475 png_save_uint_32(buf, width);
476 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600477 buf[8] = (png_byte)bit_depth;
478 buf[9] = (png_byte)color_type;
479 buf[10] = (png_byte)compression_type;
480 buf[11] = (png_byte)filter_type;
481 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500482
483 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600484 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500485
Andreas Dilger47a0c421997-05-16 02:46:07 -0500486 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600487 png_ptr->zstream.zalloc = png_zalloc;
488 png_ptr->zstream.zfree = png_zfree;
489 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500490 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500491 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500492 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
493 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500495 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500496 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500497 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500500 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500501 png_ptr->zlib_strategy = Z_FILTERED;
502 else
503 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
504 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500505 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500506 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500507 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500508 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500509 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500510 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500511 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500512 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600513 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514 png_ptr->zlib_method, png_ptr->zlib_window_bits,
515 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600516 png_ptr->zstream.next_out = png_ptr->zbuf;
517 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500518
Guy Schalnate5a37791996-06-05 15:50:50 -0500519 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500520}
521
522/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500523 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600524 * structure.
525 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500526void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500527png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500528{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600529#ifdef PNG_USE_LOCAL_ARRAYS
530 PNG_PLTE;
531#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500532 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600533 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500534 png_byte buf[3];
535
Andreas Dilger47a0c421997-05-16 02:46:07 -0500536 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500537 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500538#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600539 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500540#endif
541 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500542 {
543 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500544 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500545 png_error(png_ptr, "Invalid number of colors in palette");
546 }
547 else
548 {
549 png_warning(png_ptr, "Invalid number of colors in palette");
550 return;
551 }
552 }
553
554 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
555 {
556 png_warning(png_ptr,
557 "Ignoring request to write a PLTE chunk in grayscale PNG");
558 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500559 }
560
Andreas Dilger47a0c421997-05-16 02:46:07 -0500561 png_ptr->num_palette = (png_uint_16)num_pal;
562 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500563
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600564 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500565#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500566 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500567 {
568 buf[0] = pal_ptr->red;
569 buf[1] = pal_ptr->green;
570 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500572 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500573#else
574 /* This is a little slower but some buggy compilers need to do this instead */
575 pal_ptr=palette;
576 for (i = 0; i < num_pal; i++)
577 {
578 buf[0] = pal_ptr[i].red;
579 buf[1] = pal_ptr[i].green;
580 buf[2] = pal_ptr[i].blue;
581 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
582 }
583#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500584 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500585 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500586}
587
588/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500589void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500590png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500591{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600592#ifdef PNG_USE_LOCAL_ARRAYS
593 PNG_IDAT;
594#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500595 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600596 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500597 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500598}
599
600/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500601void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600602png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500603{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600604#ifdef PNG_USE_LOCAL_ARRAYS
605 PNG_IEND;
606#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500607 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600608 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600609 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500610}
611
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500612#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500613/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600614#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500615void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500616png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500617{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600618#ifdef PNG_USE_LOCAL_ARRAYS
619 PNG_gAMA;
620#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500621 png_uint_32 igamma;
622 png_byte buf[4];
623
Andreas Dilger47a0c421997-05-16 02:46:07 -0500624 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600625 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600626 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500627 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600628 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500629}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500630#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600631#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500632void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600633png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600634{
635#ifdef PNG_USE_LOCAL_ARRAYS
636 PNG_gAMA;
637#endif
638 png_byte buf[4];
639
640 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600641 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600642 png_save_uint_32(buf, file_gamma);
643 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
644}
645#endif
646#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500647
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600648#if defined(PNG_WRITE_sRGB_SUPPORTED)
649/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500650void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600651png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600652{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600653#ifdef PNG_USE_LOCAL_ARRAYS
654 PNG_sRGB;
655#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600656 png_byte buf[1];
657
658 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600659 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600660 png_warning(png_ptr,
661 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600662 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600663 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600664}
665#endif
666
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600667#if defined(PNG_WRITE_iCCP_SUPPORTED)
668/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500669void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600670png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
671 png_charp profile, int profile_len)
672{
673#ifdef PNG_USE_LOCAL_ARRAYS
674 PNG_iCCP;
675#endif
676 png_size_t name_len;
677 png_charp new_name;
678 compression_state comp;
679
680 png_debug(1, "in png_write_iCCP\n");
681 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
682 &new_name)) == 0)
683 {
684 png_warning(png_ptr, "Empty keyword in iCCP chunk");
685 return;
686 }
687
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600688 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600689 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600690
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500691 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600692 profile_len = 0;
693
694 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500695 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600696 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600697
698 /* make sure we include the NULL after the name and the compression type */
699 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
700 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600701 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600702 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
703
704 if (profile_len)
705 png_write_compressed_data_out(png_ptr, &comp);
706
707 png_write_chunk_end(png_ptr);
708 png_free(png_ptr, new_name);
709}
710#endif
711
712#if defined(PNG_WRITE_sPLT_SUPPORTED)
713/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500714void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600715png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600716{
717#ifdef PNG_USE_LOCAL_ARRAYS
718 PNG_sPLT;
719#endif
720 png_size_t name_len;
721 png_charp new_name;
722 png_byte entrybuf[10];
723 int entry_size = (spalette->depth == 8 ? 6 : 10);
724 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600725 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500726#ifdef PNG_NO_POINTER_INDEXING
727 int i;
728#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600729
730 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600731 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
732 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600733 {
734 png_warning(png_ptr, "Empty keyword in sPLT chunk");
735 return;
736 }
737
738 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500739 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600740 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600741 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600742 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600743
744 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500745#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600746 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
747 {
748 if (spalette->depth == 8)
749 {
750 entrybuf[0] = (png_byte)ep->red;
751 entrybuf[1] = (png_byte)ep->green;
752 entrybuf[2] = (png_byte)ep->blue;
753 entrybuf[3] = (png_byte)ep->alpha;
754 png_save_uint_16(entrybuf + 4, ep->frequency);
755 }
756 else
757 {
758 png_save_uint_16(entrybuf + 0, ep->red);
759 png_save_uint_16(entrybuf + 2, ep->green);
760 png_save_uint_16(entrybuf + 4, ep->blue);
761 png_save_uint_16(entrybuf + 6, ep->alpha);
762 png_save_uint_16(entrybuf + 8, ep->frequency);
763 }
764 png_write_chunk_data(png_ptr, entrybuf, entry_size);
765 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500766#else
767 ep=spalette->entries;
768 for (i=0; i>spalette->nentries; i++)
769 {
770 if (spalette->depth == 8)
771 {
772 entrybuf[0] = (png_byte)ep[i].red;
773 entrybuf[1] = (png_byte)ep[i].green;
774 entrybuf[2] = (png_byte)ep[i].blue;
775 entrybuf[3] = (png_byte)ep[i].alpha;
776 png_save_uint_16(entrybuf + 4, ep[i].frequency);
777 }
778 else
779 {
780 png_save_uint_16(entrybuf + 0, ep[i].red);
781 png_save_uint_16(entrybuf + 2, ep[i].green);
782 png_save_uint_16(entrybuf + 4, ep[i].blue);
783 png_save_uint_16(entrybuf + 6, ep[i].alpha);
784 png_save_uint_16(entrybuf + 8, ep[i].frequency);
785 }
786 png_write_chunk_data(png_ptr, entrybuf, entry_size);
787 }
788#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600789
790 png_write_chunk_end(png_ptr);
791 png_free(png_ptr, new_name);
792}
793#endif
794
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500795#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500796/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500797void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600798png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500799{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600800#ifdef PNG_USE_LOCAL_ARRAYS
801 PNG_sBIT;
802#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500803 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500804 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500805
Andreas Dilger47a0c421997-05-16 02:46:07 -0500806 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600807 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500808 if (color_type & PNG_COLOR_MASK_COLOR)
809 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600810 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500811
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500812 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
813 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600814 if (sbit->red == 0 || sbit->red > maxbits ||
815 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500816 sbit->blue == 0 || sbit->blue > maxbits)
817 {
818 png_warning(png_ptr, "Invalid sBIT depth specified");
819 return;
820 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500821 buf[0] = sbit->red;
822 buf[1] = sbit->green;
823 buf[2] = sbit->blue;
824 size = 3;
825 }
826 else
827 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500828 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
829 {
830 png_warning(png_ptr, "Invalid sBIT depth specified");
831 return;
832 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500833 buf[0] = sbit->gray;
834 size = 1;
835 }
836
837 if (color_type & PNG_COLOR_MASK_ALPHA)
838 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500839 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
840 {
841 png_warning(png_ptr, "Invalid sBIT depth specified");
842 return;
843 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500844 buf[size++] = sbit->alpha;
845 }
846
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600847 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500848}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500849#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500850
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500851#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500852/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600853#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500854void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500855png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600856 double red_x, double red_y, double green_x, double green_y,
857 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500858{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600859#ifdef PNG_USE_LOCAL_ARRAYS
860 PNG_cHRM;
861#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500862 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600863 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500864
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600866 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500867 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
868 white_x + white_y > 1.0)
869 {
870 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500871#if !defined(PNG_NO_CONSOLE_IO)
872 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600873#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500874 return;
875 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600876 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500877 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600878 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500879 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500880
881 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
882 red_x + red_y > 1.0)
883 {
884 png_warning(png_ptr, "Invalid cHRM red point specified");
885 return;
886 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600887 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500888 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600889 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500890 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500891
892 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
893 green_x + green_y > 1.0)
894 {
895 png_warning(png_ptr, "Invalid cHRM green point specified");
896 return;
897 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600898 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500899 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600900 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500901 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500902
903 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
904 blue_x + blue_y > 1.0)
905 {
906 png_warning(png_ptr, "Invalid cHRM blue point specified");
907 return;
908 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600909 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500910 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600911 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500912 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500913
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600914 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500915}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500916#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600917#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500918void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600919png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
920 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
921 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
922 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600923{
924#ifdef PNG_USE_LOCAL_ARRAYS
925 PNG_cHRM;
926#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600927 png_byte buf[32];
928
929 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600930 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600931 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
932 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600933 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500934#if !defined(PNG_NO_CONSOLE_IO)
935 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600936#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600937 return;
938 }
939 png_save_uint_32(buf, white_x);
940 png_save_uint_32(buf + 4, white_y);
941
942 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
943 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600944 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600945 return;
946 }
947 png_save_uint_32(buf + 8, red_x);
948 png_save_uint_32(buf + 12, red_y);
949
950 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
951 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600952 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600953 return;
954 }
955 png_save_uint_32(buf + 16, green_x);
956 png_save_uint_32(buf + 20, green_y);
957
958 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
959 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600960 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600961 return;
962 }
963 png_save_uint_32(buf + 24, blue_x);
964 png_save_uint_32(buf + 28, blue_y);
965
966 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
967}
968#endif
969#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500970
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500971#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500972/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500973void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600974png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500975 int num_trans, int color_type)
976{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600977#ifdef PNG_USE_LOCAL_ARRAYS
978 PNG_tRNS;
979#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500980 png_byte buf[6];
981
Andreas Dilger47a0c421997-05-16 02:46:07 -0500982 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500983 if (color_type == PNG_COLOR_TYPE_PALETTE)
984 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600985 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500986 {
987 png_warning(png_ptr,"Invalid number of transparent colors specified");
988 return;
989 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500990 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600991 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500992 }
993 else if (color_type == PNG_COLOR_TYPE_GRAY)
994 {
995 /* one 16 bit value */
996 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600997 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500998 }
999 else if (color_type == PNG_COLOR_TYPE_RGB)
1000 {
1001 /* three 16 bit values */
1002 png_save_uint_16(buf, tran->red);
1003 png_save_uint_16(buf + 2, tran->green);
1004 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001005 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001006 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001007 else
1008 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001009 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001010 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001011}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001012#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001013
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001014#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001015/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001016void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001017png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001018{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001019#ifdef PNG_USE_LOCAL_ARRAYS
1020 PNG_bKGD;
1021#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001022 png_byte buf[6];
1023
Andreas Dilger47a0c421997-05-16 02:46:07 -05001024 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001025 if (color_type == PNG_COLOR_TYPE_PALETTE)
1026 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001027 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001028#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001029 (png_ptr->num_palette ||
1030 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001031#endif
1032 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001033 {
1034 png_warning(png_ptr, "Invalid background palette index");
1035 return;
1036 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001037 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001038 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001039 }
1040 else if (color_type & PNG_COLOR_MASK_COLOR)
1041 {
1042 png_save_uint_16(buf, back->red);
1043 png_save_uint_16(buf + 2, back->green);
1044 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001045 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001046 }
1047 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001048 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001049 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001050 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001051 }
1052}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001053#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001054
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001055#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001056/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001057void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001058png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001059{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001060#ifdef PNG_USE_LOCAL_ARRAYS
1061 PNG_hIST;
1062#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001063 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001064 png_byte buf[3];
1065
Andreas Dilger47a0c421997-05-16 02:46:07 -05001066 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001067 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001068 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001069 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1070 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001071 png_warning(png_ptr, "Invalid number of histogram entries specified");
1072 return;
1073 }
1074
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001075 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001076 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001077 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001078 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001079 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001080 }
1081 png_write_chunk_end(png_ptr);
1082}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001083#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001084
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001085#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1086 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001087/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1088 * and if invalid, correct the keyword rather than discarding the entire
1089 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1090 * length, forbids leading or trailing whitespace, multiple internal spaces,
1091 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 *
1093 * The new_key is allocated to hold the corrected keyword and must be freed
1094 * by the calling routine. This avoids problems with trying to write to
1095 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001096 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001097png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001098png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001099{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001101 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001102 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001103 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001104
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105 png_debug(1, "in png_check_keyword\n");
1106 *new_key = NULL;
1107
1108 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001109 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001110 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001111 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001112 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1115
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001116 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001117
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001118 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001121 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001123#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001125
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001126 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001127 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001128#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001129 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001130#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001131 *dp = ' ';
1132 }
1133 else
1134 {
1135 *dp = *kp;
1136 }
1137 }
1138 *dp = '\0';
1139
1140 /* Remove any trailing white space. */
1141 kp = *new_key + key_len - 1;
1142 if (*kp == ' ')
1143 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001144 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145
1146 while (*kp == ' ')
1147 {
1148 *(kp--) = '\0';
1149 key_len--;
1150 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001151 }
1152
1153 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 kp = *new_key;
1155 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001156 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001157 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158
1159 while (*kp == ' ')
1160 {
1161 kp++;
1162 key_len--;
1163 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001164 }
1165
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001167
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 /* Remove multiple internal spaces. */
1169 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001170 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001171 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173 *(dp++) = *kp;
1174 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001175 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001176 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001177 {
1178 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001179 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001180 }
1181 else
1182 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001183 *(dp++) = *kp;
1184 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185 }
1186 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001188 if(kwarn)
1189 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001190
1191 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001192 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001193 png_free(png_ptr, *new_key);
1194 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001195 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 }
1197
1198 if (key_len > 79)
1199 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001200 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001201 new_key[79] = '\0';
1202 key_len = 79;
1203 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001204
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001205 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001206}
1207#endif
1208
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001209#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001210/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001211void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001212png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001213 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001214{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001215#ifdef PNG_USE_LOCAL_ARRAYS
1216 PNG_tEXt;
1217#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001218 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001219 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001220
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 png_debug(1, "in png_write_tEXt\n");
1222 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1223 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001224 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001225 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001227
Andreas Dilger47a0c421997-05-16 02:46:07 -05001228 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001229 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001230 else
1231 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001232
1233 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001234 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 -05001235 /*
1236 * We leave it to the application to meet PNG-1.0 requirements on the
1237 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1238 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001239 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001240 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001241 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001242 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001243 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001244
Guy Schalnat0d580581995-07-20 02:43:20 -05001245 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001247}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001248#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001249
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001250#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001252void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001253png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001254 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001255{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001256#ifdef PNG_USE_LOCAL_ARRAYS
1257 PNG_zTXt;
1258#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001260 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001261 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001262 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001263
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001265
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001267 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001268 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001269 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001270 }
1271
Andreas Dilger47a0c421997-05-16 02:46:07 -05001272 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1273 {
1274 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1275 png_free(png_ptr, new_key);
1276 return;
1277 }
1278
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001279 text_len = png_strlen(text);
1280
Andreas Dilger47a0c421997-05-16 02:46:07 -05001281 png_free(png_ptr, new_key);
1282
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001283 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001284 text_len = png_text_compress(png_ptr, text, text_len, compression,
1285 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001286
1287 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001288 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1289 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001290 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001291 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001292 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001293 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001295 /* write the compressed data */
1296 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001297
Guy Schalnat0d580581995-07-20 02:43:20 -05001298 /* close the chunk */
1299 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001300}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001301#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001302
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001303#if defined(PNG_WRITE_iTXt_SUPPORTED)
1304/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001305void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001306png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001307 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001308{
1309#ifdef PNG_USE_LOCAL_ARRAYS
1310 PNG_iTXt;
1311#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001312 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001313 png_charp new_lang, new_key;
1314 png_byte cbuf[2];
1315 compression_state comp;
1316
1317 png_debug(1, "in png_write_iTXt\n");
1318
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001319 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1320 {
1321 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1322 return;
1323 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001324 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1325 &new_lang))==0)
1326 {
1327 png_warning(png_ptr, "Empty language field in iTXt chunk");
1328 return;
1329 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001330 lang_key_len = png_strlen(lang_key);
1331 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001332
1333 if (text == NULL || *text == '\0')
1334 text_len = 0;
1335
1336 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001337 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1338 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001339
1340 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001341 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001342
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001343 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1344 (png_uint_32)(
1345 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1346 + key_len
1347 + lang_len
1348 + lang_key_len
1349 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001350
1351 /*
1352 * We leave it to the application to meet PNG-1.0 requirements on the
1353 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1354 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1355 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1356 */
1357 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001358
1359 /* set the compression flag */
1360 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1361 compression == PNG_TEXT_COMPRESSION_NONE)
1362 cbuf[0] = 0;
1363 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1364 cbuf[0] = 1;
1365 /* set the compression method */
1366 cbuf[1] = 0;
1367 png_write_chunk_data(png_ptr, cbuf, 2);
1368
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001369 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001370 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001371 png_write_chunk_data(png_ptr, '\0', 1);
1372
1373 png_write_compressed_data_out(png_ptr, &comp);
1374
1375 png_write_chunk_end(png_ptr);
1376 png_free(png_ptr, new_key);
1377 png_free(png_ptr, new_lang);
1378}
1379#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001380
1381#if defined(PNG_WRITE_oFFs_SUPPORTED)
1382/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001383void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001384png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1385 png_uint_32 y_offset,
1386 int unit_type)
1387{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001388#ifdef PNG_USE_LOCAL_ARRAYS
1389 PNG_oFFs;
1390#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001391 png_byte buf[9];
1392
1393 png_debug(1, "in png_write_oFFs\n");
1394 if (unit_type >= PNG_OFFSET_LAST)
1395 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1396
1397 png_save_uint_32(buf, x_offset);
1398 png_save_uint_32(buf + 4, y_offset);
1399 buf[8] = (png_byte)unit_type;
1400
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001401 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001402}
1403#endif
1404
1405#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001406/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001407void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001408png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1409 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1410{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001411#ifdef PNG_USE_LOCAL_ARRAYS
1412 PNG_pCAL;
1413#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001414 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001415 png_uint_32p params_len;
1416 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001417 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 int i;
1419
1420 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1421 if (type >= PNG_EQUATION_LAST)
1422 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1423
1424 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001425 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001426 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001427 png_debug1(3, "pCAL units length = %d\n", units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001428 total_len = purpose_len + units_len + 10;
1429
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001430 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1431 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001432
1433 /* Find the length of each parameter, making sure we don't count the
1434 null terminator for the last parameter. */
1435 for (i = 0; i < nparams; i++)
1436 {
1437 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001438 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001439 total_len += (png_size_t)params_len[i];
1440 }
1441
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001442 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001443 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001444 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001445 png_save_int_32(buf, X0);
1446 png_save_int_32(buf + 4, X1);
1447 buf[8] = (png_byte)type;
1448 buf[9] = (png_byte)nparams;
1449 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1450 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1451
1452 png_free(png_ptr, new_purpose);
1453
1454 for (i = 0; i < nparams; i++)
1455 {
1456 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1457 (png_size_t)params_len[i]);
1458 }
1459
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001460 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001461 png_write_chunk_end(png_ptr);
1462}
1463#endif
1464
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001465#if defined(PNG_WRITE_sCAL_SUPPORTED)
1466/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001467#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001468void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001469png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001470{
1471#ifdef PNG_USE_LOCAL_ARRAYS
1472 PNG_sCAL;
1473#endif
1474 png_size_t total_len;
1475 char wbuf[32], hbuf[32];
1476
1477 png_debug(1, "in png_write_sCAL\n");
1478
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001479#if defined(_WIN32_WCE)
1480/* sprintf() function is not supported on WindowsCE */
1481 {
1482 wchar_t wc_buf[32];
1483 swprintf(wc_buf, TEXT("%12.12e"), width);
1484 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1485 swprintf(wc_buf, TEXT("%12.12e"), height);
1486 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1487 }
1488#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001489 sprintf(wbuf, "%12.12e", width);
1490 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001491#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001492 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001493
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001494 png_debug1(3, "sCAL total length = %d\n", total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001495 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001496 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001497 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1498 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001499
1500 png_write_chunk_end(png_ptr);
1501}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001502#else
1503#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001504void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001505png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001506 png_charp height)
1507{
1508#ifdef PNG_USE_LOCAL_ARRAYS
1509 PNG_sCAL;
1510#endif
1511 png_size_t total_len;
1512 char wbuf[32], hbuf[32];
1513
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001514 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001515
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001516 png_strcpy(wbuf,(const char *)width);
1517 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001518 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001519
1520 png_debug1(3, "sCAL total length = %d\n", total_len);
1521 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001522 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001523 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1524 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001525
1526 png_write_chunk_end(png_ptr);
1527}
1528#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001529#endif
1530#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001531
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001532#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001533/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001534void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001535png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001536 png_uint_32 y_pixels_per_unit,
1537 int unit_type)
1538{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001539#ifdef PNG_USE_LOCAL_ARRAYS
1540 PNG_pHYs;
1541#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001542 png_byte buf[9];
1543
Andreas Dilger47a0c421997-05-16 02:46:07 -05001544 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001545 if (unit_type >= PNG_RESOLUTION_LAST)
1546 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1547
Guy Schalnat0d580581995-07-20 02:43:20 -05001548 png_save_uint_32(buf, x_pixels_per_unit);
1549 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001550 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001551
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001552 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001553}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001554#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001555
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001556#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001557/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1558 * or png_convert_from_time_t(), or fill in the structure yourself.
1559 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001560void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001561png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001562{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001563#ifdef PNG_USE_LOCAL_ARRAYS
1564 PNG_tIME;
1565#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001566 png_byte buf[7];
1567
Andreas Dilger47a0c421997-05-16 02:46:07 -05001568 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001569 if (mod_time->month > 12 || mod_time->month < 1 ||
1570 mod_time->day > 31 || mod_time->day < 1 ||
1571 mod_time->hour > 23 || mod_time->second > 60)
1572 {
1573 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1574 return;
1575 }
1576
Guy Schalnat0d580581995-07-20 02:43:20 -05001577 png_save_uint_16(buf, mod_time->year);
1578 buf[2] = mod_time->month;
1579 buf[3] = mod_time->day;
1580 buf[4] = mod_time->hour;
1581 buf[5] = mod_time->minute;
1582 buf[6] = mod_time->second;
1583
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001584 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001585}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001586#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001587
1588/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001589void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001590png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001591{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001592#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001593 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001594
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001595 /* start of interlace block */
1596 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001597
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001598 /* offset to next interlace block */
1599 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001600
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001601 /* start of interlace block in the y direction */
1602 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001603
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001604 /* offset to next interlace block in the y direction */
1605 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001606#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001607
Andreas Dilger47a0c421997-05-16 02:46:07 -05001608 png_size_t buf_size;
1609
1610 png_debug(1, "in png_write_start_row\n");
1611 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1612 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1613
Guy Schalnat0d580581995-07-20 02:43:20 -05001614 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001615 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001616 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001617
1618 /* set up filtering buffer, if using this filter */
1619 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001620 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001621 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001622 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001623 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001624 }
1625
Andreas Dilger47a0c421997-05-16 02:46:07 -05001626 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001627 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1628 {
1629 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001630 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001631 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001632
1633 if (png_ptr->do_filter & PNG_FILTER_UP)
1634 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001635 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001636 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001637 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001638 }
1639
1640 if (png_ptr->do_filter & PNG_FILTER_AVG)
1641 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001642 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1643 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001644 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001645 }
1646
1647 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1648 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001649 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001650 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001651 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001652 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001653 }
1654
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001655#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001656 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001657 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001658 {
1659 if (!(png_ptr->transformations & PNG_INTERLACE))
1660 {
1661 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1662 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001663 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1664 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001665 }
1666 else
1667 {
1668 png_ptr->num_rows = png_ptr->height;
1669 png_ptr->usr_width = png_ptr->width;
1670 }
1671 }
1672 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001673#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001674 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001675 png_ptr->num_rows = png_ptr->height;
1676 png_ptr->usr_width = png_ptr->width;
1677 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001678 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1679 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001680}
1681
Andreas Dilger47a0c421997-05-16 02:46:07 -05001682/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001683void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001684png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001685{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001686#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001687 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001688
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001689 /* start of interlace block */
1690 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001691
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001692 /* offset to next interlace block */
1693 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001694
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001695 /* start of interlace block in the y direction */
1696 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001697
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001698 /* offset to next interlace block in the y direction */
1699 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001700#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001701
Guy Schalnat0d580581995-07-20 02:43:20 -05001702 int ret;
1703
Andreas Dilger47a0c421997-05-16 02:46:07 -05001704 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001705 /* next row */
1706 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001707
Guy Schalnat0d580581995-07-20 02:43:20 -05001708 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001709 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001710 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001711
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001712#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001713 /* if interlaced, go to next pass */
1714 if (png_ptr->interlaced)
1715 {
1716 png_ptr->row_number = 0;
1717 if (png_ptr->transformations & PNG_INTERLACE)
1718 {
1719 png_ptr->pass++;
1720 }
1721 else
1722 {
1723 /* loop until we find a non-zero width or height pass */
1724 do
1725 {
1726 png_ptr->pass++;
1727 if (png_ptr->pass >= 7)
1728 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001729 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001730 png_pass_inc[png_ptr->pass] - 1 -
1731 png_pass_start[png_ptr->pass]) /
1732 png_pass_inc[png_ptr->pass];
1733 png_ptr->num_rows = (png_ptr->height +
1734 png_pass_yinc[png_ptr->pass] - 1 -
1735 png_pass_ystart[png_ptr->pass]) /
1736 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001737 if (png_ptr->transformations & PNG_INTERLACE)
1738 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001739 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1740
1741 }
1742
Guy Schalnate5a37791996-06-05 15:50:50 -05001743 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001744 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001745 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001746 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001747 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001748 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001749 (png_uint_32)png_ptr->usr_bit_depth *
1750 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001751 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001752 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001753 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001754#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001755
1756 /* if we get here, we've just written the last row, so we need
1757 to flush the compressor */
1758 do
1759 {
1760 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001761 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001762 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001763 if (ret == Z_OK)
1764 {
1765 /* check to see if we need more room */
1766 if (!(png_ptr->zstream.avail_out))
1767 {
1768 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1769 png_ptr->zstream.next_out = png_ptr->zbuf;
1770 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1771 }
1772 }
1773 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001774 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001775 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001776 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001777 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001778 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001779 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001780 } while (ret != Z_STREAM_END);
1781
1782 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001783 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001784 {
1785 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001786 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001787 }
1788
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001789 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001790}
1791
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001792#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001793/* Pick out the correct pixels for the interlace pass.
1794 * The basic idea here is to go through the row with a source
1795 * pointer and a destination pointer (sp and dp), and copy the
1796 * correct pixels for the pass. As the row gets compacted,
1797 * sp will always be >= dp, so we should never overwrite anything.
1798 * See the default: case for the easiest code to understand.
1799 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001800void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001801png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001802{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001803#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001804 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001805
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001806 /* start of interlace block */
1807 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001808
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001809 /* offset to next interlace block */
1810 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001811#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001812
Andreas Dilger47a0c421997-05-16 02:46:07 -05001813 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001814 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001815#if defined(PNG_USELESS_TESTS_SUPPORTED)
1816 if (row != NULL && row_info != NULL && pass < 6)
1817#else
1818 if (pass < 6)
1819#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001820 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001821 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001822 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001823 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 case 1:
1825 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001826 png_bytep sp;
1827 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001828 int shift;
1829 int d;
1830 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001831 png_uint_32 i;
1832 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001833
1834 dp = row;
1835 d = 0;
1836 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001837 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001838 i += png_pass_inc[pass])
1839 {
1840 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001841 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001842 d |= (value << shift);
1843
1844 if (shift == 0)
1845 {
1846 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001847 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 d = 0;
1849 }
1850 else
1851 shift--;
1852
1853 }
1854 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001855 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001856 break;
1857 }
1858 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001859 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001860 png_bytep sp;
1861 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001862 int shift;
1863 int d;
1864 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001865 png_uint_32 i;
1866 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001867
1868 dp = row;
1869 shift = 6;
1870 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001871 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001872 i += png_pass_inc[pass])
1873 {
1874 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001875 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001876 d |= (value << shift);
1877
1878 if (shift == 0)
1879 {
1880 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001881 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001882 d = 0;
1883 }
1884 else
1885 shift -= 2;
1886 }
1887 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001888 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001889 break;
1890 }
1891 case 4:
1892 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001893 png_bytep sp;
1894 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001895 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001896 int d;
1897 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001898 png_uint_32 i;
1899 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001900
1901 dp = row;
1902 shift = 4;
1903 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001904 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001905 i += png_pass_inc[pass])
1906 {
1907 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001908 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001909 d |= (value << shift);
1910
1911 if (shift == 0)
1912 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001913 shift = 4;
1914 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001915 d = 0;
1916 }
1917 else
1918 shift -= 4;
1919 }
1920 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001921 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001922 break;
1923 }
1924 default:
1925 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001926 png_bytep sp;
1927 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001928 png_uint_32 i;
1929 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001930 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001931
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001932 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001933 dp = row;
1934 /* find out how many bytes each pixel takes up */
1935 pixel_bytes = (row_info->pixel_depth >> 3);
1936 /* loop through the row, only looking at the pixels that
1937 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001938 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001939 i += png_pass_inc[pass])
1940 {
1941 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001942 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001943 /* move the pixel */
1944 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001945 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001946 /* next pixel */
1947 dp += pixel_bytes;
1948 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001949 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001950 }
1951 }
1952 /* set new row width */
1953 row_info->width = (row_info->width +
1954 png_pass_inc[pass] - 1 -
1955 png_pass_start[pass]) /
1956 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001957 row_info->rowbytes = ((row_info->width *
1958 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001959 }
1960}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001961#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001962
Andreas Dilger47a0c421997-05-16 02:46:07 -05001963/* This filters the row, chooses which filter to use, if it has not already
1964 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001965 * chosen filter.
1966 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001967#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001968#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001969#define PNG_LOMASK ((png_uint_32)0xffffL)
1970#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001971void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001972png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001973{
Guy Schalnate5a37791996-06-05 15:50:50 -05001974 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001975 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001976 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001977 png_uint_32 row_bytes = row_info->rowbytes;
1978#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1979 int num_p_filters = (int)png_ptr->num_prev_filters;
1980#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001981
Andreas Dilger47a0c421997-05-16 02:46:07 -05001982 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001983 /* find out how many bytes offset each pixel is */
1984 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001985
1986 prev_row = png_ptr->prev_row;
1987 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001988 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001989
Andreas Dilger47a0c421997-05-16 02:46:07 -05001990 /* The prediction method we use is to find which method provides the
1991 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001992 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001993 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001994 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001995 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001996 * predictive" method (not implemented yet), which does test compressions
1997 * of lines using different filter methods, and then chooses the
1998 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001999 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002000 *
2001 * GRR 980525: consider also
2002 * (1) minimum sum of absolute differences from running average (i.e.,
2003 * keep running sum of non-absolute differences & count of bytes)
2004 * [track dispersion, too? restart average if dispersion too large?]
2005 * (1b) minimum sum of absolute differences from sliding average, probably
2006 * with window size <= deflate window (usually 32K)
2007 * (2) minimum sum of squared differences from zero or running average
2008 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002009 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002010
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002011
Guy Schalnate5a37791996-06-05 15:50:50 -05002012 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002013 * that has been chosen, as it doesn't actually do anything to the data.
2014 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002015 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002016 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002017 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002018 png_bytep rp;
2019 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002020 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002021 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002022
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002023 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002024 {
2025 v = *rp;
2026 sum += (v < 128) ? v : 256 - v;
2027 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002028
2029#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2030 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2031 {
2032 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002033 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002034 sumlo = sum & PNG_LOMASK;
2035 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2036
2037 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002038 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002039 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002040 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002041 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002042 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002043 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002044 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002045 PNG_WEIGHT_SHIFT;
2046 }
2047 }
2048
2049 /* Factor in the cost of this filter (this is here for completeness,
2050 * but it makes no sense to have a "cost" for the NONE filter, as
2051 * it has the minimum possible computational cost - none).
2052 */
2053 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2054 PNG_COST_SHIFT;
2055 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2056 PNG_COST_SHIFT;
2057
2058 if (sumhi > PNG_HIMASK)
2059 sum = PNG_MAXSUM;
2060 else
2061 sum = (sumhi << PNG_HISHIFT) + sumlo;
2062 }
2063#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002064 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002065 }
2066
Guy Schalnate5a37791996-06-05 15:50:50 -05002067 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002068 if (filter_to_do == PNG_FILTER_SUB)
2069 /* it's the only filter so no testing is needed */
2070 {
2071 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002072 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002073 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2074 i++, rp++, dp++)
2075 {
2076 *dp = *rp;
2077 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002078 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002079 i++, rp++, lp++, dp++)
2080 {
2081 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2082 }
2083 best_row = png_ptr->sub_row;
2084 }
2085
2086 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002087 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002088 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002089 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002090 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002091 int v;
2092
2093#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002094 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095 * would reduce the sum of this filter, so that we can do the
2096 * early exit comparison without scaling the sum each time.
2097 */
2098 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2099 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002100 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002101 png_uint_32 lmhi, lmlo;
2102 lmlo = lmins & PNG_LOMASK;
2103 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2104
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002105 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002106 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002107 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002108 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002109 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002110 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002111 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112 PNG_WEIGHT_SHIFT;
2113 }
2114 }
2115
2116 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2117 PNG_COST_SHIFT;
2118 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2119 PNG_COST_SHIFT;
2120
2121 if (lmhi > PNG_HIMASK)
2122 lmins = PNG_MAXSUM;
2123 else
2124 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2125 }
2126#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002127
Guy Schalnate5a37791996-06-05 15:50:50 -05002128 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2129 i++, rp++, dp++)
2130 {
2131 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002132
Guy Schalnate5a37791996-06-05 15:50:50 -05002133 sum += (v < 128) ? v : 256 - v;
2134 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002135 for (lp = row_buf + 1; i < row_info->rowbytes;
2136 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002137 {
2138 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002139
Guy Schalnate5a37791996-06-05 15:50:50 -05002140 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141
2142 if (sum > lmins) /* We are already worse, don't continue. */
2143 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002144 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145
2146#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2147 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2148 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002149 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002150 png_uint_32 sumhi, sumlo;
2151 sumlo = sum & PNG_LOMASK;
2152 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2153
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002154 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002155 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002156 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002157 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002158 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002159 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002160 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002161 PNG_WEIGHT_SHIFT;
2162 }
2163 }
2164
2165 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2166 PNG_COST_SHIFT;
2167 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2168 PNG_COST_SHIFT;
2169
2170 if (sumhi > PNG_HIMASK)
2171 sum = PNG_MAXSUM;
2172 else
2173 sum = (sumhi << PNG_HISHIFT) + sumlo;
2174 }
2175#endif
2176
Guy Schalnate5a37791996-06-05 15:50:50 -05002177 if (sum < mins)
2178 {
2179 mins = sum;
2180 best_row = png_ptr->sub_row;
2181 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002182 }
2183
Guy Schalnate5a37791996-06-05 15:50:50 -05002184 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002185 if (filter_to_do == PNG_FILTER_UP)
2186 {
2187 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002188 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002189
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002190 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002191 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002192 i++, rp++, pp++, dp++)
2193 {
2194 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2195 }
2196 best_row = png_ptr->up_row;
2197 }
2198
2199 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002200 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002201 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002202 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002203 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002204 int v;
2205
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002206
Andreas Dilger47a0c421997-05-16 02:46:07 -05002207#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2208 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2209 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002210 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002211 png_uint_32 lmhi, lmlo;
2212 lmlo = lmins & PNG_LOMASK;
2213 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2214
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002215 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002216 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002217 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002218 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002219 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002220 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002221 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002222 PNG_WEIGHT_SHIFT;
2223 }
2224 }
2225
2226 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2227 PNG_COST_SHIFT;
2228 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2229 PNG_COST_SHIFT;
2230
2231 if (lmhi > PNG_HIMASK)
2232 lmins = PNG_MAXSUM;
2233 else
2234 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2235 }
2236#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002237
2238 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002239 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002240 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002241 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002242
2243 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002244
2245 if (sum > lmins) /* We are already worse, don't continue. */
2246 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002247 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248
2249#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2250 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2251 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002252 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 png_uint_32 sumhi, sumlo;
2254 sumlo = sum & PNG_LOMASK;
2255 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2256
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002257 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002258 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002260 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 PNG_WEIGHT_SHIFT;
2265 }
2266 }
2267
2268 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2269 PNG_COST_SHIFT;
2270 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2271 PNG_COST_SHIFT;
2272
2273 if (sumhi > PNG_HIMASK)
2274 sum = PNG_MAXSUM;
2275 else
2276 sum = (sumhi << PNG_HISHIFT) + sumlo;
2277 }
2278#endif
2279
Guy Schalnate5a37791996-06-05 15:50:50 -05002280 if (sum < mins)
2281 {
2282 mins = sum;
2283 best_row = png_ptr->up_row;
2284 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002285 }
2286
Guy Schalnate5a37791996-06-05 15:50:50 -05002287 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002288 if (filter_to_do == PNG_FILTER_AVG)
2289 {
2290 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002291 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002292 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002293 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002294 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002295 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002296 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002297 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002298 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002299 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2300 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002301 }
2302 best_row = png_ptr->avg_row;
2303 }
2304
2305 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002306 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002307 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002309 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310 int v;
2311
2312#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2313 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2314 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002315 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002316 png_uint_32 lmhi, lmlo;
2317 lmlo = lmins & PNG_LOMASK;
2318 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2319
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002320 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002321 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002322 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002323 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002324 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002325 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002326 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002327 PNG_WEIGHT_SHIFT;
2328 }
2329 }
2330
2331 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2332 PNG_COST_SHIFT;
2333 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2334 PNG_COST_SHIFT;
2335
2336 if (lmhi > PNG_HIMASK)
2337 lmins = PNG_MAXSUM;
2338 else
2339 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2340 }
2341#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002342
2343 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002344 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002345 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002346 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002347
2348 sum += (v < 128) ? v : 256 - v;
2349 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002350 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002351 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002352 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002354
2355 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356
2357 if (sum > lmins) /* We are already worse, don't continue. */
2358 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002359 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360
2361#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2362 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2363 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002364 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002365 png_uint_32 sumhi, sumlo;
2366 sumlo = sum & PNG_LOMASK;
2367 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2368
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002369 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002370 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002372 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002373 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002374 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002375 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002376 PNG_WEIGHT_SHIFT;
2377 }
2378 }
2379
2380 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2381 PNG_COST_SHIFT;
2382 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2383 PNG_COST_SHIFT;
2384
2385 if (sumhi > PNG_HIMASK)
2386 sum = PNG_MAXSUM;
2387 else
2388 sum = (sumhi << PNG_HISHIFT) + sumlo;
2389 }
2390#endif
2391
Guy Schalnate5a37791996-06-05 15:50:50 -05002392 if (sum < mins)
2393 {
2394 mins = sum;
2395 best_row = png_ptr->avg_row;
2396 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002397 }
2398
Andreas Dilger47a0c421997-05-16 02:46:07 -05002399 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002400 if (filter_to_do == PNG_FILTER_PAETH)
2401 {
2402 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002403 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002404 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002405 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002406 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002407 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002408 }
2409
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002411 {
2412 int a, b, c, pa, pb, pc, p;
2413
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002414 b = *pp++;
2415 c = *cp++;
2416 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002417
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002418 p = b - c;
2419 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002420
2421#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002422 pa = abs(p);
2423 pb = abs(pc);
2424 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002425#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002426 pa = p < 0 ? -p : p;
2427 pb = pc < 0 ? -pc : pc;
2428 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002429#endif
2430
2431 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2432
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002433 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002434 }
2435 best_row = png_ptr->paeth_row;
2436 }
2437
2438 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002439 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002440 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002441 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002442 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002443 int v;
2444
2445#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2446 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2447 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002448 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002449 png_uint_32 lmhi, lmlo;
2450 lmlo = lmins & PNG_LOMASK;
2451 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2452
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002453 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002454 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002455 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002456 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002457 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 PNG_WEIGHT_SHIFT;
2461 }
2462 }
2463
2464 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2465 PNG_COST_SHIFT;
2466 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2467 PNG_COST_SHIFT;
2468
2469 if (lmhi > PNG_HIMASK)
2470 lmins = PNG_MAXSUM;
2471 else
2472 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2473 }
2474#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002475
2476 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002477 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002478 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002479 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002480
2481 sum += (v < 128) ? v : 256 - v;
2482 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002483
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002484 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002485 {
2486 int a, b, c, pa, pb, pc, p;
2487
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002488 b = *pp++;
2489 c = *cp++;
2490 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002491
2492#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002493 p = b - c;
2494 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002495#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002496 pa = abs(p);
2497 pb = abs(pc);
2498 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002499#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 pa = p < 0 ? -p : p;
2501 pb = pc < 0 ? -pc : pc;
2502 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002503#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002504 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2505#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002507 pa = abs(p - a);
2508 pb = abs(p - b);
2509 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002510 if (pa <= pb && pa <= pc)
2511 p = a;
2512 else if (pb <= pc)
2513 p = b;
2514 else
2515 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002516#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002517
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002519
2520 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002521
2522 if (sum > lmins) /* We are already worse, don't continue. */
2523 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002524 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002525
2526#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2527 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2528 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002529 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002530 png_uint_32 sumhi, sumlo;
2531 sumlo = sum & PNG_LOMASK;
2532 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2533
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002534 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002535 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002536 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002537 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002538 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002539 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002540 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002541 PNG_WEIGHT_SHIFT;
2542 }
2543 }
2544
2545 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2546 PNG_COST_SHIFT;
2547 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2548 PNG_COST_SHIFT;
2549
2550 if (sumhi > PNG_HIMASK)
2551 sum = PNG_MAXSUM;
2552 else
2553 sum = (sumhi << PNG_HISHIFT) + sumlo;
2554 }
2555#endif
2556
Guy Schalnate5a37791996-06-05 15:50:50 -05002557 if (sum < mins)
2558 {
2559 best_row = png_ptr->paeth_row;
2560 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002561 }
2562
Andreas Dilger47a0c421997-05-16 02:46:07 -05002563 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002564
Guy Schalnate5a37791996-06-05 15:50:50 -05002565 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002566
2567#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2568 /* Save the type of filter we picked this time for future calculations */
2569 if (png_ptr->num_prev_filters > 0)
2570 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002571 int j;
2572 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002573 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002574 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002575 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002576 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002577 }
2578#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002579}
2580
2581
Andreas Dilger47a0c421997-05-16 02:46:07 -05002582/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002583void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002584png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2585{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002586 png_debug(1, "in png_write_filtered_row\n");
2587 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002588 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002589
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002590 png_ptr->zstream.next_in = filtered_row;
2591 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002592 /* repeat until we have compressed all the data */
2593 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002594 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002595 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002596
Guy Schalnate5a37791996-06-05 15:50:50 -05002597 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002598 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002599 /* check for compression errors */
2600 if (ret != Z_OK)
2601 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002602 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002603 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002604 else
2605 png_error(png_ptr, "zlib error");
2606 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002607
Guy Schalnate5a37791996-06-05 15:50:50 -05002608 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002609 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002610 {
2611 /* write the IDAT and reset the zlib output buffer */
2612 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002613 png_ptr->zstream.next_out = png_ptr->zbuf;
2614 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002615 }
2616 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002617 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002618
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002619 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002620 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002621 {
2622 png_bytep tptr;
2623
2624 tptr = png_ptr->prev_row;
2625 png_ptr->prev_row = png_ptr->row_buf;
2626 png_ptr->row_buf = tptr;
2627 }
2628
Guy Schalnate5a37791996-06-05 15:50:50 -05002629 /* finish row - updates counters and flushes zlib if last row */
2630 png_write_finish_row(png_ptr);
2631
2632#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2633 png_ptr->flush_rows++;
2634
2635 if (png_ptr->flush_dist > 0 &&
2636 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002637 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002638 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002639 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002640#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002641}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002642#endif /* PNG_WRITE_SUPPORTED */