blob: ed964ced8de301413aa23ff4ae3e3a64697f9456 [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-Pehrsone68f5a32001-05-14 09:20:53 -05004 * libpng 1.0.12beta1 - May 14, 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-Pehrson19095602001-03-14 07:08:39 -060013#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-Pehrsone1eff582001-04-14 20:15:41 -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-Pehrsone68f5a32001-05-14 09:20:53 -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-Pehrsone68f5a32001-05-14 09:20:53 -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-Pehrsone68f5a32001-05-14 09:20:53 -0500463#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600464 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500465#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500466 png_ptr->width = width;
467 png_ptr->height = height;
468
469 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500470 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 /* set the usr info, so any transformations can modify it */
472 png_ptr->usr_width = png_ptr->width;
473 png_ptr->usr_bit_depth = png_ptr->bit_depth;
474 png_ptr->usr_channels = png_ptr->channels;
475
Guy Schalnat0d580581995-07-20 02:43:20 -0500476 /* pack the header information into the buffer */
477 png_save_uint_32(buf, width);
478 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600479 buf[8] = (png_byte)bit_depth;
480 buf[9] = (png_byte)color_type;
481 buf[10] = (png_byte)compression_type;
482 buf[11] = (png_byte)filter_type;
483 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500484
485 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600486 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500487
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600489 png_ptr->zstream.zalloc = png_zalloc;
490 png_ptr->zstream.zfree = png_zfree;
491 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500492 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500493 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
495 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500496 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500497 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500500 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500501 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500503 png_ptr->zlib_strategy = Z_FILTERED;
504 else
505 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
506 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500507 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500508 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500509 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500510 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500511 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500512 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500514 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600515 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500516 png_ptr->zlib_method, png_ptr->zlib_window_bits,
517 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600518 png_ptr->zstream.next_out = png_ptr->zbuf;
519 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500520
Guy Schalnate5a37791996-06-05 15:50:50 -0500521 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500522}
523
524/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500525 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600526 * structure.
527 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500528void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500529png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500530{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600531#ifdef PNG_USE_LOCAL_ARRAYS
532 PNG_PLTE;
533#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500534 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600535 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500536 png_byte buf[3];
537
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500539 if ((
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -0600540#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500541 defined(PNG_WRITE_EMPTY_PLTE_PERMITTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600542 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500543#endif
544 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500545 {
546 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500547 {
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -0500548 png_error(png_ptr, "Invalid number of colors in palette");
549 }
550 else
551 {
552 png_warning(png_ptr, "Invalid number of colors in palette");
553 return;
554 }
555 }
556
557 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
558 {
559 png_warning(png_ptr,
560 "Ignoring request to write a PLTE chunk in grayscale PNG");
561 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500562 }
563
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564 png_ptr->num_palette = (png_uint_16)num_pal;
565 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500566
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600567 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500568#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500570 {
571 buf[0] = pal_ptr->red;
572 buf[1] = pal_ptr->green;
573 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500576#else
577 /* This is a little slower but some buggy compilers need to do this instead */
578 pal_ptr=palette;
579 for (i = 0; i < num_pal; i++)
580 {
581 buf[0] = pal_ptr[i].red;
582 buf[1] = pal_ptr[i].green;
583 buf[2] = pal_ptr[i].blue;
584 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
585 }
586#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500587 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500588 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500589}
590
591/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500592void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500593png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500594{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600595#ifdef PNG_USE_LOCAL_ARRAYS
596 PNG_IDAT;
597#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500598 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600599 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500600 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500601}
602
603/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600605png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500606{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600607#ifdef PNG_USE_LOCAL_ARRAYS
608 PNG_IEND;
609#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600611 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600612 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500613}
614
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500615#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500616/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600617#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500618void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500619png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500620{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600621#ifdef PNG_USE_LOCAL_ARRAYS
622 PNG_gAMA;
623#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500624 png_uint_32 igamma;
625 png_byte buf[4];
626
Andreas Dilger47a0c421997-05-16 02:46:07 -0500627 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600628 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600629 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500630 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600631 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500632}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500633#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600634#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500635void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600636png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600637{
638#ifdef PNG_USE_LOCAL_ARRAYS
639 PNG_gAMA;
640#endif
641 png_byte buf[4];
642
643 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600644 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600645 png_save_uint_32(buf, file_gamma);
646 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
647}
648#endif
649#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500650
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600651#if defined(PNG_WRITE_sRGB_SUPPORTED)
652/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500653void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600654png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600655{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600656#ifdef PNG_USE_LOCAL_ARRAYS
657 PNG_sRGB;
658#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600659 png_byte buf[1];
660
661 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600662 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600663 png_warning(png_ptr,
664 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600665 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600666 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600667}
668#endif
669
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600670#if defined(PNG_WRITE_iCCP_SUPPORTED)
671/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500672void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600673png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
674 png_charp profile, int profile_len)
675{
676#ifdef PNG_USE_LOCAL_ARRAYS
677 PNG_iCCP;
678#endif
679 png_size_t name_len;
680 png_charp new_name;
681 compression_state comp;
682
683 png_debug(1, "in png_write_iCCP\n");
684 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
685 &new_name)) == 0)
686 {
687 png_warning(png_ptr, "Empty keyword in iCCP chunk");
688 return;
689 }
690
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600691 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600692 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600693
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500694 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600695 profile_len = 0;
696
697 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500698 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600699 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600700
701 /* make sure we include the NULL after the name and the compression type */
702 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
703 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600704 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600705 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
706
707 if (profile_len)
708 png_write_compressed_data_out(png_ptr, &comp);
709
710 png_write_chunk_end(png_ptr);
711 png_free(png_ptr, new_name);
712}
713#endif
714
715#if defined(PNG_WRITE_sPLT_SUPPORTED)
716/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500717void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600718png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600719{
720#ifdef PNG_USE_LOCAL_ARRAYS
721 PNG_sPLT;
722#endif
723 png_size_t name_len;
724 png_charp new_name;
725 png_byte entrybuf[10];
726 int entry_size = (spalette->depth == 8 ? 6 : 10);
727 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600728 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500729#ifdef PNG_NO_POINTER_INDEXING
730 int i;
731#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600732
733 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600734 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
735 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600736 {
737 png_warning(png_ptr, "Empty keyword in sPLT chunk");
738 return;
739 }
740
741 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -0500742 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600743 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600744 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600745 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600746
747 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500748#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600749 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
750 {
751 if (spalette->depth == 8)
752 {
753 entrybuf[0] = (png_byte)ep->red;
754 entrybuf[1] = (png_byte)ep->green;
755 entrybuf[2] = (png_byte)ep->blue;
756 entrybuf[3] = (png_byte)ep->alpha;
757 png_save_uint_16(entrybuf + 4, ep->frequency);
758 }
759 else
760 {
761 png_save_uint_16(entrybuf + 0, ep->red);
762 png_save_uint_16(entrybuf + 2, ep->green);
763 png_save_uint_16(entrybuf + 4, ep->blue);
764 png_save_uint_16(entrybuf + 6, ep->alpha);
765 png_save_uint_16(entrybuf + 8, ep->frequency);
766 }
767 png_write_chunk_data(png_ptr, entrybuf, entry_size);
768 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500769#else
770 ep=spalette->entries;
771 for (i=0; i>spalette->nentries; i++)
772 {
773 if (spalette->depth == 8)
774 {
775 entrybuf[0] = (png_byte)ep[i].red;
776 entrybuf[1] = (png_byte)ep[i].green;
777 entrybuf[2] = (png_byte)ep[i].blue;
778 entrybuf[3] = (png_byte)ep[i].alpha;
779 png_save_uint_16(entrybuf + 4, ep[i].frequency);
780 }
781 else
782 {
783 png_save_uint_16(entrybuf + 0, ep[i].red);
784 png_save_uint_16(entrybuf + 2, ep[i].green);
785 png_save_uint_16(entrybuf + 4, ep[i].blue);
786 png_save_uint_16(entrybuf + 6, ep[i].alpha);
787 png_save_uint_16(entrybuf + 8, ep[i].frequency);
788 }
789 png_write_chunk_data(png_ptr, entrybuf, entry_size);
790 }
791#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600792
793 png_write_chunk_end(png_ptr);
794 png_free(png_ptr, new_name);
795}
796#endif
797
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500798#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500799/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500800void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600801png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500802{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600803#ifdef PNG_USE_LOCAL_ARRAYS
804 PNG_sBIT;
805#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500806 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500807 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500808
Andreas Dilger47a0c421997-05-16 02:46:07 -0500809 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600810 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500811 if (color_type & PNG_COLOR_MASK_COLOR)
812 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600813 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500814
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500815 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
816 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600817 if (sbit->red == 0 || sbit->red > maxbits ||
818 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500819 sbit->blue == 0 || sbit->blue > maxbits)
820 {
821 png_warning(png_ptr, "Invalid sBIT depth specified");
822 return;
823 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500824 buf[0] = sbit->red;
825 buf[1] = sbit->green;
826 buf[2] = sbit->blue;
827 size = 3;
828 }
829 else
830 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500831 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
832 {
833 png_warning(png_ptr, "Invalid sBIT depth specified");
834 return;
835 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500836 buf[0] = sbit->gray;
837 size = 1;
838 }
839
840 if (color_type & PNG_COLOR_MASK_ALPHA)
841 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500842 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
843 {
844 png_warning(png_ptr, "Invalid sBIT depth specified");
845 return;
846 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500847 buf[size++] = sbit->alpha;
848 }
849
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600850 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500851}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500852#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500853
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500854#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500855/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600856#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500857void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600859 double red_x, double red_y, double green_x, double green_y,
860 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500861{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600862#ifdef PNG_USE_LOCAL_ARRAYS
863 PNG_cHRM;
864#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500865 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600866 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500867
Andreas Dilger47a0c421997-05-16 02:46:07 -0500868 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600869 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500870 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
871 white_x + white_y > 1.0)
872 {
873 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500874#if !defined(PNG_NO_CONSOLE_IO)
875 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600876#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500877 return;
878 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600879 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500880 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600881 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500883
884 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
885 red_x + red_y > 1.0)
886 {
887 png_warning(png_ptr, "Invalid cHRM red point specified");
888 return;
889 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600890 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500891 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600892 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500893 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500894
895 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
896 green_x + green_y > 1.0)
897 {
898 png_warning(png_ptr, "Invalid cHRM green point specified");
899 return;
900 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600901 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500902 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600903 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500904 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500905
906 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
907 blue_x + blue_y > 1.0)
908 {
909 png_warning(png_ptr, "Invalid cHRM blue point specified");
910 return;
911 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600912 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500913 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600914 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500915 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500916
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600917 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500918}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500919#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600920#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500921void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600922png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
923 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
924 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
925 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600926{
927#ifdef PNG_USE_LOCAL_ARRAYS
928 PNG_cHRM;
929#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600930 png_byte buf[32];
931
932 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600933 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600934 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
935 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600936 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500937#if !defined(PNG_NO_CONSOLE_IO)
938 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600939#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600940 return;
941 }
942 png_save_uint_32(buf, white_x);
943 png_save_uint_32(buf + 4, white_y);
944
945 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
946 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600947 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600948 return;
949 }
950 png_save_uint_32(buf + 8, red_x);
951 png_save_uint_32(buf + 12, red_y);
952
953 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
954 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600955 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600956 return;
957 }
958 png_save_uint_32(buf + 16, green_x);
959 png_save_uint_32(buf + 20, green_y);
960
961 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
962 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600963 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600964 return;
965 }
966 png_save_uint_32(buf + 24, blue_x);
967 png_save_uint_32(buf + 28, blue_y);
968
969 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
970}
971#endif
972#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500973
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500974#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500975/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500976void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600977png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500978 int num_trans, int color_type)
979{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600980#ifdef PNG_USE_LOCAL_ARRAYS
981 PNG_tRNS;
982#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500983 png_byte buf[6];
984
Andreas Dilger47a0c421997-05-16 02:46:07 -0500985 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500986 if (color_type == PNG_COLOR_TYPE_PALETTE)
987 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600988 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500989 {
990 png_warning(png_ptr,"Invalid number of transparent colors specified");
991 return;
992 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500993 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600994 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500995 }
996 else if (color_type == PNG_COLOR_TYPE_GRAY)
997 {
998 /* one 16 bit value */
999 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001000 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001001 }
1002 else if (color_type == PNG_COLOR_TYPE_RGB)
1003 {
1004 /* three 16 bit values */
1005 png_save_uint_16(buf, tran->red);
1006 png_save_uint_16(buf + 2, tran->green);
1007 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001008 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001009 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001010 else
1011 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001012 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001013 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001014}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001015#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001016
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001017#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001018/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001019void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001020png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001021{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001022#ifdef PNG_USE_LOCAL_ARRAYS
1023 PNG_bKGD;
1024#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001025 png_byte buf[6];
1026
Andreas Dilger47a0c421997-05-16 02:46:07 -05001027 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001028 if (color_type == PNG_COLOR_TYPE_PALETTE)
1029 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001030 if (
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001031#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001032 defined(PNG_WRITE_EMPTY_PLTE_PERMITTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001033 (png_ptr->num_palette ||
1034 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001035#endif
1036 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 {
1038 png_warning(png_ptr, "Invalid background palette index");
1039 return;
1040 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001041 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001042 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001043 }
1044 else if (color_type & PNG_COLOR_MASK_COLOR)
1045 {
1046 png_save_uint_16(buf, back->red);
1047 png_save_uint_16(buf + 2, back->green);
1048 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001049 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001050 }
1051 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001052 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001053 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001054 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001055 }
1056}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001057#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001058
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001059#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001060/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001061void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001062png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001063{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001064#ifdef PNG_USE_LOCAL_ARRAYS
1065 PNG_hIST;
1066#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001067 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001068 png_byte buf[3];
1069
Andreas Dilger47a0c421997-05-16 02:46:07 -05001070 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001071 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001072 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001073 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1074 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001075 png_warning(png_ptr, "Invalid number of histogram entries specified");
1076 return;
1077 }
1078
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001079 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001080 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001081 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001082 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001083 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001084 }
1085 png_write_chunk_end(png_ptr);
1086}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001087#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001088
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001089#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1090 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001091/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1092 * and if invalid, correct the keyword rather than discarding the entire
1093 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1094 * length, forbids leading or trailing whitespace, multiple internal spaces,
1095 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 *
1097 * The new_key is allocated to hold the corrected keyword and must be freed
1098 * by the calling routine. This avoids problems with trying to write to
1099 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001100 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001101png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001102png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001103{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001104 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001105 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001106 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001107 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001108
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109 png_debug(1, "in png_check_keyword\n");
1110 *new_key = NULL;
1111
1112 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001114 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001115 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001116 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001117
Andreas Dilger47a0c421997-05-16 02:46:07 -05001118 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1119
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001120 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001122 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001124 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001125 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001127#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001128 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001129
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001130 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001131 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001132#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001133 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001134#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 *dp = ' ';
1136 }
1137 else
1138 {
1139 *dp = *kp;
1140 }
1141 }
1142 *dp = '\0';
1143
1144 /* Remove any trailing white space. */
1145 kp = *new_key + key_len - 1;
1146 if (*kp == ' ')
1147 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001148 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149
1150 while (*kp == ' ')
1151 {
1152 *(kp--) = '\0';
1153 key_len--;
1154 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001155 }
1156
1157 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158 kp = *new_key;
1159 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001160 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001161 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001162
1163 while (*kp == ' ')
1164 {
1165 kp++;
1166 key_len--;
1167 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001168 }
1169
Andreas Dilger47a0c421997-05-16 02:46:07 -05001170 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001171
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 /* Remove multiple internal spaces. */
1173 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001174 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001175 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001176 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177 *(dp++) = *kp;
1178 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001179 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001180 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001181 {
1182 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001183 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001184 }
1185 else
1186 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 *(dp++) = *kp;
1188 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189 }
1190 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001191 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001192 if(kwarn)
1193 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001194
1195 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001197 png_free(png_ptr, *new_key);
1198 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001199 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001200 }
1201
1202 if (key_len > 79)
1203 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001204 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 new_key[79] = '\0';
1206 key_len = 79;
1207 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001208
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001209 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210}
1211#endif
1212
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001213#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001214/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001215void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001216png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001218{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001219#ifdef PNG_USE_LOCAL_ARRAYS
1220 PNG_tEXt;
1221#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001223 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001224
Andreas Dilger47a0c421997-05-16 02:46:07 -05001225 png_debug(1, "in png_write_tEXt\n");
1226 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1227 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001228 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001229 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001230 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001231
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001234 else
1235 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236
1237 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001238 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 -05001239 /*
1240 * We leave it to the application to meet PNG-1.0 requirements on the
1241 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1242 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001243 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001244 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001245 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001248
Guy Schalnat0d580581995-07-20 02:43:20 -05001249 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001250 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001251}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001252#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001253
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001254#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001255/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001256void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001257png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001258 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001259{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001260#ifdef PNG_USE_LOCAL_ARRAYS
1261 PNG_zTXt;
1262#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001264 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001265 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001266 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001267
Andreas Dilger47a0c421997-05-16 02:46:07 -05001268 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001269
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001271 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001272 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001273 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001274 }
1275
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1277 {
1278 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1279 png_free(png_ptr, new_key);
1280 return;
1281 }
1282
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001283 text_len = png_strlen(text);
1284
Andreas Dilger47a0c421997-05-16 02:46:07 -05001285 png_free(png_ptr, new_key);
1286
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001287 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001288 text_len = png_text_compress(png_ptr, text, text_len, compression,
1289 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001290
1291 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001292 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1293 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001294 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001295 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001296 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001297 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001298 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001299 /* write the compressed data */
1300 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001301
Guy Schalnat0d580581995-07-20 02:43:20 -05001302 /* close the chunk */
1303 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001304}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001305#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001306
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001307#if defined(PNG_WRITE_iTXt_SUPPORTED)
1308/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001309void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001310png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001311 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001312{
1313#ifdef PNG_USE_LOCAL_ARRAYS
1314 PNG_iTXt;
1315#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001316 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001317 png_charp new_lang, new_key;
1318 png_byte cbuf[2];
1319 compression_state comp;
1320
1321 png_debug(1, "in png_write_iTXt\n");
1322
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001323 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1324 {
1325 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1326 return;
1327 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001328 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1329 &new_lang))==0)
1330 {
1331 png_warning(png_ptr, "Empty language field in iTXt chunk");
1332 return;
1333 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001334 lang_key_len = png_strlen(lang_key);
1335 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001336
1337 if (text == NULL || *text == '\0')
1338 text_len = 0;
1339
1340 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001341 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1342 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001343
1344 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001345 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001346
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001347 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1348 (png_uint_32)(
1349 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1350 + key_len
1351 + lang_len
1352 + lang_key_len
1353 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001354
1355 /*
1356 * We leave it to the application to meet PNG-1.0 requirements on the
1357 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1358 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1359 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1360 */
1361 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001362
1363 /* set the compression flag */
1364 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1365 compression == PNG_TEXT_COMPRESSION_NONE)
1366 cbuf[0] = 0;
1367 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1368 cbuf[0] = 1;
1369 /* set the compression method */
1370 cbuf[1] = 0;
1371 png_write_chunk_data(png_ptr, cbuf, 2);
1372
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001373 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001374 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001375 png_write_chunk_data(png_ptr, '\0', 1);
1376
1377 png_write_compressed_data_out(png_ptr, &comp);
1378
1379 png_write_chunk_end(png_ptr);
1380 png_free(png_ptr, new_key);
1381 png_free(png_ptr, new_lang);
1382}
1383#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001384
1385#if defined(PNG_WRITE_oFFs_SUPPORTED)
1386/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001387void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001388png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1389 png_uint_32 y_offset,
1390 int unit_type)
1391{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001392#ifdef PNG_USE_LOCAL_ARRAYS
1393 PNG_oFFs;
1394#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001395 png_byte buf[9];
1396
1397 png_debug(1, "in png_write_oFFs\n");
1398 if (unit_type >= PNG_OFFSET_LAST)
1399 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1400
1401 png_save_uint_32(buf, x_offset);
1402 png_save_uint_32(buf + 4, y_offset);
1403 buf[8] = (png_byte)unit_type;
1404
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001405 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001406}
1407#endif
1408
1409#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001410/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001411void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001412png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1413 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1414{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001415#ifdef PNG_USE_LOCAL_ARRAYS
1416 PNG_pCAL;
1417#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001418 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001419 png_uint_32p params_len;
1420 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001421 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001422 int i;
1423
1424 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1425 if (type >= PNG_EQUATION_LAST)
1426 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1427
1428 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001429 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001430 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001431 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001432 total_len = purpose_len + units_len + 10;
1433
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001434 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1435 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001436
1437 /* Find the length of each parameter, making sure we don't count the
1438 null terminator for the last parameter. */
1439 for (i = 0; i < nparams; i++)
1440 {
1441 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001442 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001443 total_len += (png_size_t)params_len[i];
1444 }
1445
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001446 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001447 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001448 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001449 png_save_int_32(buf, X0);
1450 png_save_int_32(buf + 4, X1);
1451 buf[8] = (png_byte)type;
1452 buf[9] = (png_byte)nparams;
1453 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1454 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1455
1456 png_free(png_ptr, new_purpose);
1457
1458 for (i = 0; i < nparams; i++)
1459 {
1460 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1461 (png_size_t)params_len[i]);
1462 }
1463
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001464 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001465 png_write_chunk_end(png_ptr);
1466}
1467#endif
1468
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001469#if defined(PNG_WRITE_sCAL_SUPPORTED)
1470/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001471#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001472void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001473png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001474{
1475#ifdef PNG_USE_LOCAL_ARRAYS
1476 PNG_sCAL;
1477#endif
1478 png_size_t total_len;
1479 char wbuf[32], hbuf[32];
1480
1481 png_debug(1, "in png_write_sCAL\n");
1482
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001483#if defined(_WIN32_WCE)
1484/* sprintf() function is not supported on WindowsCE */
1485 {
1486 wchar_t wc_buf[32];
1487 swprintf(wc_buf, TEXT("%12.12e"), width);
1488 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1489 swprintf(wc_buf, TEXT("%12.12e"), height);
1490 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1491 }
1492#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001493 sprintf(wbuf, "%12.12e", width);
1494 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001495#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001496 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001497
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001498 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001499 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001500 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrsone1644472001-03-23 05:06:56 -06001501 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1502 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001503
1504 png_write_chunk_end(png_ptr);
1505}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001506#else
1507#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001508void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001509png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001510 png_charp height)
1511{
1512#ifdef PNG_USE_LOCAL_ARRAYS
1513 PNG_sCAL;
1514#endif
1515 png_size_t total_len;
1516 char wbuf[32], hbuf[32];
1517
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001518 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001519
Glenn Randers-Pehrsone1644472001-03-23 05:06:56 -06001520 png_strcpy(wbuf,(const char *)width);
1521 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001522 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001523
1524 png_debug1(3, "sCAL total length = %d\n", total_len);
1525 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001526 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrsone1644472001-03-23 05:06:56 -06001527 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1528 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001529
1530 png_write_chunk_end(png_ptr);
1531}
1532#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001533#endif
1534#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001535
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001536#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001537/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001538void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001539png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001540 png_uint_32 y_pixels_per_unit,
1541 int unit_type)
1542{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001543#ifdef PNG_USE_LOCAL_ARRAYS
1544 PNG_pHYs;
1545#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001546 png_byte buf[9];
1547
Andreas Dilger47a0c421997-05-16 02:46:07 -05001548 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001549 if (unit_type >= PNG_RESOLUTION_LAST)
1550 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1551
Guy Schalnat0d580581995-07-20 02:43:20 -05001552 png_save_uint_32(buf, x_pixels_per_unit);
1553 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001554 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001555
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001556 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001557}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001558#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001559
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001560#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001561/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1562 * or png_convert_from_time_t(), or fill in the structure yourself.
1563 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001564void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001565png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001566{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001567#ifdef PNG_USE_LOCAL_ARRAYS
1568 PNG_tIME;
1569#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001570 png_byte buf[7];
1571
Andreas Dilger47a0c421997-05-16 02:46:07 -05001572 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001573 if (mod_time->month > 12 || mod_time->month < 1 ||
1574 mod_time->day > 31 || mod_time->day < 1 ||
1575 mod_time->hour > 23 || mod_time->second > 60)
1576 {
1577 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1578 return;
1579 }
1580
Guy Schalnat0d580581995-07-20 02:43:20 -05001581 png_save_uint_16(buf, mod_time->year);
1582 buf[2] = mod_time->month;
1583 buf[3] = mod_time->day;
1584 buf[4] = mod_time->hour;
1585 buf[5] = mod_time->minute;
1586 buf[6] = mod_time->second;
1587
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001588 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001589}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001590#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001591
1592/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001593void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001594png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001595{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001596#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001597 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001598
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001599 /* start of interlace block */
1600 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001601
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001602 /* offset to next interlace block */
1603 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001604
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001605 /* start of interlace block in the y direction */
1606 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001607
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001608 /* offset to next interlace block in the y direction */
1609 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001610#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001611
Andreas Dilger47a0c421997-05-16 02:46:07 -05001612 png_size_t buf_size;
1613
1614 png_debug(1, "in png_write_start_row\n");
1615 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1616 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1617
Guy Schalnat0d580581995-07-20 02:43:20 -05001618 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001619 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001620 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001621
1622 /* set up filtering buffer, if using this filter */
1623 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001624 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001625 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001626 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001627 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001628 }
1629
Andreas Dilger47a0c421997-05-16 02:46:07 -05001630 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001631 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1632 {
1633 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001634 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001635 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001636
1637 if (png_ptr->do_filter & PNG_FILTER_UP)
1638 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001639 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001640 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001641 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001642 }
1643
1644 if (png_ptr->do_filter & PNG_FILTER_AVG)
1645 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001646 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1647 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001648 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001649 }
1650
1651 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1652 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001653 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001654 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001655 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001656 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001657 }
1658
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001659#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001660 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001661 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001662 {
1663 if (!(png_ptr->transformations & PNG_INTERLACE))
1664 {
1665 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1666 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001667 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1668 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001669 }
1670 else
1671 {
1672 png_ptr->num_rows = png_ptr->height;
1673 png_ptr->usr_width = png_ptr->width;
1674 }
1675 }
1676 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001677#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001678 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001679 png_ptr->num_rows = png_ptr->height;
1680 png_ptr->usr_width = png_ptr->width;
1681 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001682 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1683 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001684}
1685
Andreas Dilger47a0c421997-05-16 02:46:07 -05001686/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001687void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001688png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001689{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001690#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001691 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001692
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001693 /* start of interlace block */
1694 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001695
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001696 /* offset to next interlace block */
1697 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001698
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001699 /* start of interlace block in the y direction */
1700 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001701
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001702 /* offset to next interlace block in the y direction */
1703 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001704#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001705
Guy Schalnat0d580581995-07-20 02:43:20 -05001706 int ret;
1707
Andreas Dilger47a0c421997-05-16 02:46:07 -05001708 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001709 /* next row */
1710 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001711
Guy Schalnat0d580581995-07-20 02:43:20 -05001712 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001713 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001714 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001715
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001716#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001717 /* if interlaced, go to next pass */
1718 if (png_ptr->interlaced)
1719 {
1720 png_ptr->row_number = 0;
1721 if (png_ptr->transformations & PNG_INTERLACE)
1722 {
1723 png_ptr->pass++;
1724 }
1725 else
1726 {
1727 /* loop until we find a non-zero width or height pass */
1728 do
1729 {
1730 png_ptr->pass++;
1731 if (png_ptr->pass >= 7)
1732 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001733 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001734 png_pass_inc[png_ptr->pass] - 1 -
1735 png_pass_start[png_ptr->pass]) /
1736 png_pass_inc[png_ptr->pass];
1737 png_ptr->num_rows = (png_ptr->height +
1738 png_pass_yinc[png_ptr->pass] - 1 -
1739 png_pass_ystart[png_ptr->pass]) /
1740 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001741 if (png_ptr->transformations & PNG_INTERLACE)
1742 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001743 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1744
1745 }
1746
Guy Schalnate5a37791996-06-05 15:50:50 -05001747 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001748 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001749 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001750 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001751 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001752 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001753 (png_uint_32)png_ptr->usr_bit_depth *
1754 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001755 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001756 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001757 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001758#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001759
1760 /* if we get here, we've just written the last row, so we need
1761 to flush the compressor */
1762 do
1763 {
1764 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001765 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001766 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001767 if (ret == Z_OK)
1768 {
1769 /* check to see if we need more room */
1770 if (!(png_ptr->zstream.avail_out))
1771 {
1772 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1773 png_ptr->zstream.next_out = png_ptr->zbuf;
1774 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1775 }
1776 }
1777 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001778 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001779 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001780 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001781 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001782 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001783 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001784 } while (ret != Z_STREAM_END);
1785
1786 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001787 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001788 {
1789 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001790 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001791 }
1792
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001793 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001794}
1795
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001796#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001797/* Pick out the correct pixels for the interlace pass.
1798 * The basic idea here is to go through the row with a source
1799 * pointer and a destination pointer (sp and dp), and copy the
1800 * correct pixels for the pass. As the row gets compacted,
1801 * sp will always be >= dp, so we should never overwrite anything.
1802 * See the default: case for the easiest code to understand.
1803 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001804void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001805png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001806{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001807#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001808 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001809
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001810 /* start of interlace block */
1811 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001812
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001813 /* offset to next interlace block */
1814 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001815#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001816
Andreas Dilger47a0c421997-05-16 02:46:07 -05001817 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001818 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001819#if defined(PNG_USELESS_TESTS_SUPPORTED)
1820 if (row != NULL && row_info != NULL && pass < 6)
1821#else
1822 if (pass < 6)
1823#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001825 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001826 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001827 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001828 case 1:
1829 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001830 png_bytep sp;
1831 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001832 int shift;
1833 int d;
1834 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001835 png_uint_32 i;
1836 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001837
1838 dp = row;
1839 d = 0;
1840 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001841 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001842 i += png_pass_inc[pass])
1843 {
1844 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001845 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001846 d |= (value << shift);
1847
1848 if (shift == 0)
1849 {
1850 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001851 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 d = 0;
1853 }
1854 else
1855 shift--;
1856
1857 }
1858 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001859 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001860 break;
1861 }
1862 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001863 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001864 png_bytep sp;
1865 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001866 int shift;
1867 int d;
1868 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001869 png_uint_32 i;
1870 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001871
1872 dp = row;
1873 shift = 6;
1874 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001875 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001876 i += png_pass_inc[pass])
1877 {
1878 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001879 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001880 d |= (value << shift);
1881
1882 if (shift == 0)
1883 {
1884 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001885 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001886 d = 0;
1887 }
1888 else
1889 shift -= 2;
1890 }
1891 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001892 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001893 break;
1894 }
1895 case 4:
1896 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001897 png_bytep sp;
1898 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001899 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001900 int d;
1901 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001902 png_uint_32 i;
1903 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001904
1905 dp = row;
1906 shift = 4;
1907 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001908 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001909 i += png_pass_inc[pass])
1910 {
1911 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001912 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001913 d |= (value << shift);
1914
1915 if (shift == 0)
1916 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001917 shift = 4;
1918 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001919 d = 0;
1920 }
1921 else
1922 shift -= 4;
1923 }
1924 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001925 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 break;
1927 }
1928 default:
1929 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001930 png_bytep sp;
1931 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001932 png_uint_32 i;
1933 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001934 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001935
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001936 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001937 dp = row;
1938 /* find out how many bytes each pixel takes up */
1939 pixel_bytes = (row_info->pixel_depth >> 3);
1940 /* loop through the row, only looking at the pixels that
1941 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001942 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001943 i += png_pass_inc[pass])
1944 {
1945 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001946 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001947 /* move the pixel */
1948 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001949 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001950 /* next pixel */
1951 dp += pixel_bytes;
1952 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001953 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001954 }
1955 }
1956 /* set new row width */
1957 row_info->width = (row_info->width +
1958 png_pass_inc[pass] - 1 -
1959 png_pass_start[pass]) /
1960 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001961 row_info->rowbytes = ((row_info->width *
1962 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001963 }
1964}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001965#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001966
Andreas Dilger47a0c421997-05-16 02:46:07 -05001967/* This filters the row, chooses which filter to use, if it has not already
1968 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001969 * chosen filter.
1970 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001971#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001972#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001973#define PNG_LOMASK ((png_uint_32)0xffffL)
1974#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001975void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001976png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001977{
Guy Schalnate5a37791996-06-05 15:50:50 -05001978 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001979 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001980 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001981 png_uint_32 row_bytes = row_info->rowbytes;
1982#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1983 int num_p_filters = (int)png_ptr->num_prev_filters;
1984#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001985
Andreas Dilger47a0c421997-05-16 02:46:07 -05001986 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001987 /* find out how many bytes offset each pixel is */
1988 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001989
1990 prev_row = png_ptr->prev_row;
1991 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001992 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001993
Andreas Dilger47a0c421997-05-16 02:46:07 -05001994 /* The prediction method we use is to find which method provides the
1995 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001996 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001997 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001998 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001999 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002000 * predictive" method (not implemented yet), which does test compressions
2001 * of lines using different filter methods, and then chooses the
2002 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002003 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002004 *
2005 * GRR 980525: consider also
2006 * (1) minimum sum of absolute differences from running average (i.e.,
2007 * keep running sum of non-absolute differences & count of bytes)
2008 * [track dispersion, too? restart average if dispersion too large?]
2009 * (1b) minimum sum of absolute differences from sliding average, probably
2010 * with window size <= deflate window (usually 32K)
2011 * (2) minimum sum of squared differences from zero or running average
2012 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002013 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002014
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002015
Guy Schalnate5a37791996-06-05 15:50:50 -05002016 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002017 * that has been chosen, as it doesn't actually do anything to the data.
2018 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002019 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002020 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002021 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002022 png_bytep rp;
2023 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002024 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002025 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002026
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002027 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002028 {
2029 v = *rp;
2030 sum += (v < 128) ? v : 256 - v;
2031 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002032
2033#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2034 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2035 {
2036 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002037 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002038 sumlo = sum & PNG_LOMASK;
2039 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2040
2041 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002042 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002043 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002044 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002045 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002046 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002047 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002048 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002049 PNG_WEIGHT_SHIFT;
2050 }
2051 }
2052
2053 /* Factor in the cost of this filter (this is here for completeness,
2054 * but it makes no sense to have a "cost" for the NONE filter, as
2055 * it has the minimum possible computational cost - none).
2056 */
2057 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2058 PNG_COST_SHIFT;
2059 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2060 PNG_COST_SHIFT;
2061
2062 if (sumhi > PNG_HIMASK)
2063 sum = PNG_MAXSUM;
2064 else
2065 sum = (sumhi << PNG_HISHIFT) + sumlo;
2066 }
2067#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002068 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002069 }
2070
Guy Schalnate5a37791996-06-05 15:50:50 -05002071 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002072 if (filter_to_do == PNG_FILTER_SUB)
2073 /* it's the only filter so no testing is needed */
2074 {
2075 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002076 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002077 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2078 i++, rp++, dp++)
2079 {
2080 *dp = *rp;
2081 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002082 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002083 i++, rp++, lp++, dp++)
2084 {
2085 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2086 }
2087 best_row = png_ptr->sub_row;
2088 }
2089
2090 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002091 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002092 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002093 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002094 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095 int v;
2096
2097#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002098 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002099 * would reduce the sum of this filter, so that we can do the
2100 * early exit comparison without scaling the sum each time.
2101 */
2102 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2103 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002104 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002105 png_uint_32 lmhi, lmlo;
2106 lmlo = lmins & PNG_LOMASK;
2107 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2108
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002109 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002110 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002111 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002113 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002114 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002115 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116 PNG_WEIGHT_SHIFT;
2117 }
2118 }
2119
2120 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2121 PNG_COST_SHIFT;
2122 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2123 PNG_COST_SHIFT;
2124
2125 if (lmhi > PNG_HIMASK)
2126 lmins = PNG_MAXSUM;
2127 else
2128 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2129 }
2130#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002131
Guy Schalnate5a37791996-06-05 15:50:50 -05002132 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2133 i++, rp++, dp++)
2134 {
2135 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002136
Guy Schalnate5a37791996-06-05 15:50:50 -05002137 sum += (v < 128) ? v : 256 - v;
2138 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002139 for (lp = row_buf + 1; i < row_info->rowbytes;
2140 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002141 {
2142 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002143
Guy Schalnate5a37791996-06-05 15:50:50 -05002144 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145
2146 if (sum > lmins) /* We are already worse, don't continue. */
2147 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002148 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002149
2150#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2151 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2152 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002153 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002154 png_uint_32 sumhi, sumlo;
2155 sumlo = sum & PNG_LOMASK;
2156 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2157
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002158 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002159 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002160 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002161 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002162 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002163 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002164 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002165 PNG_WEIGHT_SHIFT;
2166 }
2167 }
2168
2169 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2170 PNG_COST_SHIFT;
2171 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2172 PNG_COST_SHIFT;
2173
2174 if (sumhi > PNG_HIMASK)
2175 sum = PNG_MAXSUM;
2176 else
2177 sum = (sumhi << PNG_HISHIFT) + sumlo;
2178 }
2179#endif
2180
Guy Schalnate5a37791996-06-05 15:50:50 -05002181 if (sum < mins)
2182 {
2183 mins = sum;
2184 best_row = png_ptr->sub_row;
2185 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002186 }
2187
Guy Schalnate5a37791996-06-05 15:50:50 -05002188 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002189 if (filter_to_do == PNG_FILTER_UP)
2190 {
2191 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002192 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002193
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002194 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002196 i++, rp++, pp++, dp++)
2197 {
2198 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2199 }
2200 best_row = png_ptr->up_row;
2201 }
2202
2203 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002204 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002205 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002206 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002207 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002208 int v;
2209
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002210
Andreas Dilger47a0c421997-05-16 02:46:07 -05002211#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2212 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2213 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002214 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215 png_uint_32 lmhi, lmlo;
2216 lmlo = lmins & PNG_LOMASK;
2217 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2218
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002219 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002220 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002221 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002222 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002223 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002224 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002225 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002226 PNG_WEIGHT_SHIFT;
2227 }
2228 }
2229
2230 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2231 PNG_COST_SHIFT;
2232 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2233 PNG_COST_SHIFT;
2234
2235 if (lmhi > PNG_HIMASK)
2236 lmins = PNG_MAXSUM;
2237 else
2238 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2239 }
2240#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002241
2242 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002243 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002244 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002245 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002246
2247 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248
2249 if (sum > lmins) /* We are already worse, don't continue. */
2250 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002251 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002252
2253#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2254 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2255 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002256 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 png_uint_32 sumhi, sumlo;
2258 sumlo = sum & PNG_LOMASK;
2259 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2260
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002265 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002266 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002267 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002268 PNG_WEIGHT_SHIFT;
2269 }
2270 }
2271
2272 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2273 PNG_COST_SHIFT;
2274 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2275 PNG_COST_SHIFT;
2276
2277 if (sumhi > PNG_HIMASK)
2278 sum = PNG_MAXSUM;
2279 else
2280 sum = (sumhi << PNG_HISHIFT) + sumlo;
2281 }
2282#endif
2283
Guy Schalnate5a37791996-06-05 15:50:50 -05002284 if (sum < mins)
2285 {
2286 mins = sum;
2287 best_row = png_ptr->up_row;
2288 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002289 }
2290
Guy Schalnate5a37791996-06-05 15:50:50 -05002291 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002292 if (filter_to_do == PNG_FILTER_AVG)
2293 {
2294 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002295 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002296 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002297 pp = prev_row + 1; i < bpp; 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++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002300 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002302 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002303 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2304 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002305 }
2306 best_row = png_ptr->avg_row;
2307 }
2308
2309 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002310 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002311 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002312 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002313 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314 int v;
2315
2316#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2317 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2318 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002319 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002320 png_uint_32 lmhi, lmlo;
2321 lmlo = lmins & PNG_LOMASK;
2322 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2323
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002324 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002325 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002326 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002327 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002328 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002329 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002330 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002331 PNG_WEIGHT_SHIFT;
2332 }
2333 }
2334
2335 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2336 PNG_COST_SHIFT;
2337 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2338 PNG_COST_SHIFT;
2339
2340 if (lmhi > PNG_HIMASK)
2341 lmins = PNG_MAXSUM;
2342 else
2343 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2344 }
2345#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002346
2347 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002348 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002349 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002350 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002351
2352 sum += (v < 128) ? v : 256 - v;
2353 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002354 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002355 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002356 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002357 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002358
2359 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360
2361 if (sum > lmins) /* We are already worse, don't continue. */
2362 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002363 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002364
2365#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2366 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2367 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002368 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 png_uint_32 sumhi, sumlo;
2370 sumlo = sum & PNG_LOMASK;
2371 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2372
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002373 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002374 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002375 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002376 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002377 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002378 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002379 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002380 PNG_WEIGHT_SHIFT;
2381 }
2382 }
2383
2384 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2385 PNG_COST_SHIFT;
2386 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2387 PNG_COST_SHIFT;
2388
2389 if (sumhi > PNG_HIMASK)
2390 sum = PNG_MAXSUM;
2391 else
2392 sum = (sumhi << PNG_HISHIFT) + sumlo;
2393 }
2394#endif
2395
Guy Schalnate5a37791996-06-05 15:50:50 -05002396 if (sum < mins)
2397 {
2398 mins = sum;
2399 best_row = png_ptr->avg_row;
2400 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002401 }
2402
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002404 if (filter_to_do == PNG_FILTER_PAETH)
2405 {
2406 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002407 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002408 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002409 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002410 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002411 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002412 }
2413
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002414 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002415 {
2416 int a, b, c, pa, pb, pc, p;
2417
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002418 b = *pp++;
2419 c = *cp++;
2420 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002421
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002422 p = b - c;
2423 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002424
2425#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002426 pa = abs(p);
2427 pb = abs(pc);
2428 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002429#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002430 pa = p < 0 ? -p : p;
2431 pb = pc < 0 ? -pc : pc;
2432 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002433#endif
2434
2435 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2436
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002437 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002438 }
2439 best_row = png_ptr->paeth_row;
2440 }
2441
2442 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002443 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002444 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002445 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002446 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002447 int v;
2448
2449#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2450 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2451 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002452 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002453 png_uint_32 lmhi, lmlo;
2454 lmlo = lmins & PNG_LOMASK;
2455 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2456
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002457 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002461 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002462 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002463 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002464 PNG_WEIGHT_SHIFT;
2465 }
2466 }
2467
2468 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2469 PNG_COST_SHIFT;
2470 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2471 PNG_COST_SHIFT;
2472
2473 if (lmhi > PNG_HIMASK)
2474 lmins = PNG_MAXSUM;
2475 else
2476 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2477 }
2478#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002479
2480 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002481 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002482 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002483 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002484
2485 sum += (v < 128) ? v : 256 - v;
2486 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002487
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002488 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002489 {
2490 int a, b, c, pa, pb, pc, p;
2491
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002492 b = *pp++;
2493 c = *cp++;
2494 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002495
2496#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002497 p = b - c;
2498 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002499#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 pa = abs(p);
2501 pb = abs(pc);
2502 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002503#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002504 pa = p < 0 ? -p : p;
2505 pb = pc < 0 ? -pc : pc;
2506 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002507#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002508 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2509#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002510 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002511 pa = abs(p - a);
2512 pb = abs(p - b);
2513 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002514 if (pa <= pb && pa <= pc)
2515 p = a;
2516 else if (pb <= pc)
2517 p = b;
2518 else
2519 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002520#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002521
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002522 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002523
2524 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002525
2526 if (sum > lmins) /* We are already worse, don't continue. */
2527 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002528 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002529
2530#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2531 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2532 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002533 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002534 png_uint_32 sumhi, sumlo;
2535 sumlo = sum & PNG_LOMASK;
2536 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2537
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002538 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002539 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002540 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002541 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002542 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002543 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002544 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002545 PNG_WEIGHT_SHIFT;
2546 }
2547 }
2548
2549 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2550 PNG_COST_SHIFT;
2551 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2552 PNG_COST_SHIFT;
2553
2554 if (sumhi > PNG_HIMASK)
2555 sum = PNG_MAXSUM;
2556 else
2557 sum = (sumhi << PNG_HISHIFT) + sumlo;
2558 }
2559#endif
2560
Guy Schalnate5a37791996-06-05 15:50:50 -05002561 if (sum < mins)
2562 {
2563 best_row = png_ptr->paeth_row;
2564 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002565 }
2566
Andreas Dilger47a0c421997-05-16 02:46:07 -05002567 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002568
Guy Schalnate5a37791996-06-05 15:50:50 -05002569 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002570
2571#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2572 /* Save the type of filter we picked this time for future calculations */
2573 if (png_ptr->num_prev_filters > 0)
2574 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002575 int j;
2576 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002577 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002578 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002579 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002581 }
2582#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002583}
2584
2585
Andreas Dilger47a0c421997-05-16 02:46:07 -05002586/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002587void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002588png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2589{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002590 png_debug(1, "in png_write_filtered_row\n");
2591 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002592 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002593
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002594 png_ptr->zstream.next_in = filtered_row;
2595 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002596 /* repeat until we have compressed all the data */
2597 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002598 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002599 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002600
Guy Schalnate5a37791996-06-05 15:50:50 -05002601 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002602 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002603 /* check for compression errors */
2604 if (ret != Z_OK)
2605 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002606 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002607 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002608 else
2609 png_error(png_ptr, "zlib error");
2610 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002611
Guy Schalnate5a37791996-06-05 15:50:50 -05002612 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002613 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002614 {
2615 /* write the IDAT and reset the zlib output buffer */
2616 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002617 png_ptr->zstream.next_out = png_ptr->zbuf;
2618 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002619 }
2620 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002621 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002622
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002623 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002624 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002625 {
2626 png_bytep tptr;
2627
2628 tptr = png_ptr->prev_row;
2629 png_ptr->prev_row = png_ptr->row_buf;
2630 png_ptr->row_buf = tptr;
2631 }
2632
Guy Schalnate5a37791996-06-05 15:50:50 -05002633 /* finish row - updates counters and flushes zlib if last row */
2634 png_write_finish_row(png_ptr);
2635
2636#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2637 png_ptr->flush_rows++;
2638
2639 if (png_ptr->flush_dist > 0 &&
2640 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002641 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002642 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002643 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002644#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002645}
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06002646#endif /* PNG_WRITE_SUPPORTED */