blob: b1273e58ec21fcc1f3d34e10524018d549760621 [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-Pehrson75294572000-05-06 14:09:57 -05004 * libpng 1.0.7beta11 - May 6, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06008 * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050018void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
21 buf[0] = (png_byte)((i >> 24) & 0xff);
22 buf[1] = (png_byte)((i >> 16) & 0xff);
23 buf[2] = (png_byte)((i >> 8) & 0xff);
24 buf[3] = (png_byte)(i & 0xff);
25}
26
Andreas Dilger47a0c421997-05-16 02:46:07 -050027#if defined(PNG_WRITE_pCAL_SUPPORTED)
28/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050032void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40#endif
41
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042/* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
45 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050046void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050048{
49 buf[0] = (png_byte)((i >> 8) & 0xff);
50 buf[1] = (png_byte)(i & 0xff);
51}
52
Andreas Dilger47a0c421997-05-16 02:46:07 -050053/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
60 * functions instead.
61 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050062void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050065{
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050069}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
74 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050075void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050078{
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 png_byte buf[4];
80 png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name, length);
81
Guy Schalnat0d580581995-07-20 02:43:20 -050082 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050083 png_save_uint_32(buf, length);
84 png_write_data(png_ptr, buf, (png_size_t)4);
85
Guy Schalnat0d580581995-07-20 02:43:20 -050086 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050088 /* reset the crc and run it over the chunk name */
89 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050091}
92
Andreas Dilger47a0c421997-05-16 02:46:07 -050093/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060094 * Note that multiple calls to this function are allowed, and that the
95 * sum of the lengths from these calls *must* add up to the total_length
96 * given to png_write_chunk_start().
97 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050098void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050099png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 /* write the data, and run the CRC over it */
102 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103 {
104 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600105 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 }
107}
108
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500110void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600111png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 png_byte buf[4];
114
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117
118 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119}
120
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600121/* Simple function to write the signature. If we have already written
122 * the magic bytes of the signature, or more likely, the PNG stream is
123 * being embedded into another stream and doesn't need its own signature,
124 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600125 * bytes have already been written.
126 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500127void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600128png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500129{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600130 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600132 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 (png_size_t)8 - png_ptr->sig_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500134}
135
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600136#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600137/*
138 * This pair of functions encapsulates the operation of (a) compressing a
139 * text string, and (b) issuing it later as a series of chunk data writes.
140 * The compression_state structure is shared context for these functions
141 * set up by the caller in order to make the whole mess thread-safe.
142 */
143
144typedef struct
145{
146 char *input; /* the uncompressed input data */
147 int input_len; /* its length */
148 int num_output_ptr; /* number of output pointers used */
149 int max_output_ptr; /* size of output_ptr */
150 png_charpp output_ptr; /* array of pointers to output */
151} compression_state;
152
153/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500154static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600155png_text_compress(png_structp png_ptr,
156 png_charp text, png_size_t text_len, int compression,
157 compression_state *comp)
158{
159 int ret;
160
161 comp->num_output_ptr = comp->max_output_ptr = 0;
162 comp->output_ptr = NULL;
163 comp->input = NULL;
164
165 /* we may just want to pass the text right through */
166 if (compression == PNG_TEXT_COMPRESSION_NONE)
167 {
168 comp->input = text;
169 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500170 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600171 }
172
173 if (compression >= PNG_TEXT_COMPRESSION_LAST)
174 {
175#if !defined(PNG_NO_STDIO)
176 char msg[50];
177 sprintf(msg, "Unknown compression type %d", compression);
178 png_warning(png_ptr, msg);
179#else
180 png_warning(png_ptr, "Unknown compression type");
181#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600182 }
183
184 /* We can't write the chunk until we find out how much data we have,
185 * which means we need to run the compressor first and save the
186 * output. This shouldn't be a problem, as the vast majority of
187 * comments should be reasonable, but we will set up an array of
188 * malloc'd pointers to be sure.
189 *
190 * If we knew the application was well behaved, we could simplify this
191 * greatly by assuming we can always malloc an output buffer large
192 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
193 * and malloc this directly. The only time this would be a bad idea is
194 * if we can't malloc more than 64K and we have 64K of random input
195 * data, or if the input string is incredibly large (although this
196 * wouldn't cause a failure, just a slowdown due to swapping).
197 */
198
199 /* set up the compression buffers */
200 png_ptr->zstream.avail_in = (uInt)text_len;
201 png_ptr->zstream.next_in = (Bytef *)text;
202 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
203 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
204
205 /* this is the same compression loop as in png_write_row() */
206 do
207 {
208 /* compress the data */
209 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
210 if (ret != Z_OK)
211 {
212 /* error */
213 if (png_ptr->zstream.msg != NULL)
214 png_error(png_ptr, png_ptr->zstream.msg);
215 else
216 png_error(png_ptr, "zlib error");
217 }
218 /* check to see if we need more room */
219 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
220 {
221 /* make sure the output array has room */
222 if (comp->num_output_ptr >= comp->max_output_ptr)
223 {
224 int old_max;
225
226 old_max = comp->max_output_ptr;
227 comp->max_output_ptr = comp->num_output_ptr + 4;
228 if (comp->output_ptr != NULL)
229 {
230 png_charpp old_ptr;
231
232 old_ptr = comp->output_ptr;
233 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
234 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
235 png_memcpy(comp->output_ptr, old_ptr,
236 old_max * sizeof (png_charp));
237 png_free(png_ptr, old_ptr);
238 }
239 else
240 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
241 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
242 }
243
244 /* save the data */
245 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
246 (png_uint_32)png_ptr->zbuf_size);
247 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
248 png_ptr->zbuf_size);
249 comp->num_output_ptr++;
250
251 /* and reset the buffer */
252 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
253 png_ptr->zstream.next_out = png_ptr->zbuf;
254 }
255 /* continue until we don't have any more to compress */
256 } while (png_ptr->zstream.avail_in);
257
258 /* finish the compression */
259 do
260 {
261 /* tell zlib we are finished */
262 ret = deflate(&png_ptr->zstream, Z_FINISH);
263 if (ret != Z_OK && ret != Z_STREAM_END)
264 {
265 /* we got an error */
266 if (png_ptr->zstream.msg != NULL)
267 png_error(png_ptr, png_ptr->zstream.msg);
268 else
269 png_error(png_ptr, "zlib error");
270 }
271
272 /* check to see if we need more room */
273 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
274 {
275 /* check to make sure our output array has room */
276 if (comp->num_output_ptr >= comp->max_output_ptr)
277 {
278 int old_max;
279
280 old_max = comp->max_output_ptr;
281 comp->max_output_ptr = comp->num_output_ptr + 4;
282 if (comp->output_ptr != NULL)
283 {
284 png_charpp old_ptr;
285
286 old_ptr = comp->output_ptr;
287 /* This could be optimized to realloc() */
288 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
289 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
290 png_memcpy(comp->output_ptr, old_ptr,
291 old_max * sizeof (png_charp));
292 png_free(png_ptr, old_ptr);
293 }
294 else
295 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
296 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
297 }
298
299 /* save off the data */
300 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
301 (png_uint_32)png_ptr->zbuf_size);
302 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
303 png_ptr->zbuf_size);
304 comp->num_output_ptr++;
305
306 /* and reset the buffer pointers */
307 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
308 png_ptr->zstream.next_out = png_ptr->zbuf;
309 }
310 } while (ret != Z_STREAM_END);
311
312 /* text length is number of buffers plus last buffer */
313 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
314 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
315 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
316
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500317 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600318}
319
320/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500321static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600322png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
323{
324 int i;
325
326 /* handle the no-compression case */
327 if (comp->input)
328 {
329 png_write_chunk_data(png_ptr, (png_bytep)comp->input, comp->input_len);
330 return;
331 }
332
333 /* write saved output buffers, if any */
334 for (i = 0; i < comp->num_output_ptr; i++)
335 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
337 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600338 png_free(png_ptr, comp->output_ptr[i]);
339 }
340 if (comp->max_output_ptr != 0)
341 png_free(png_ptr, comp->output_ptr);
342 /* write anything left in zbuf */
343 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
344 png_write_chunk_data(png_ptr, png_ptr->zbuf,
345 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
346
347 /* reset zlib for another zTXt/iTXt or the image data */
348 deflateReset(&png_ptr->zstream);
349
350}
351#endif
352
Guy Schalnat0d580581995-07-20 02:43:20 -0500353/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600354 * information. Note that the rest of this code depends upon this
355 * information being correct.
356 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500357void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600358png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500359 int bit_depth, int color_type, int compression_type, int filter_type,
360 int interlace_type)
361{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600362#ifdef PNG_USE_LOCAL_ARRAYS
363 PNG_IHDR;
364#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500365 png_byte buf[13]; /* buffer to store the IHDR info */
366
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500368 /* Check that we have valid input data from the application info */
369 switch (color_type)
370 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500371 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500372 switch (bit_depth)
373 {
374 case 1:
375 case 2:
376 case 4:
377 case 8:
378 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500380 }
381 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500383 if (bit_depth != 8 && bit_depth != 16)
384 png_error(png_ptr, "Invalid bit depth for RGB image");
385 png_ptr->channels = 3;
386 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 switch (bit_depth)
389 {
390 case 1:
391 case 2:
392 case 4:
393 case 8: png_ptr->channels = 1; break;
394 default: png_error(png_ptr, "Invalid bit depth for paletted image");
395 }
396 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500398 if (bit_depth != 8 && bit_depth != 16)
399 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
400 png_ptr->channels = 2;
401 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500403 if (bit_depth != 8 && bit_depth != 16)
404 png_error(png_ptr, "Invalid bit depth for RGBA image");
405 png_ptr->channels = 4;
406 break;
407 default:
408 png_error(png_ptr, "Invalid image color type specified");
409 }
410
Andreas Dilger47a0c421997-05-16 02:46:07 -0500411 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500412 {
413 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500415 }
416
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 {
419 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500421 }
422
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600423#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 if (interlace_type != PNG_INTERLACE_NONE &&
425 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 {
427 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600430#else
431 interlace_type=PNG_INTERLACE_NONE;
432#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500433
434 /* save off the relevent information */
435 png_ptr->bit_depth = (png_byte)bit_depth;
436 png_ptr->color_type = (png_byte)color_type;
437 png_ptr->interlaced = (png_byte)interlace_type;
438 png_ptr->width = width;
439 png_ptr->height = height;
440
441 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500442 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500443 /* set the usr info, so any transformations can modify it */
444 png_ptr->usr_width = png_ptr->width;
445 png_ptr->usr_bit_depth = png_ptr->bit_depth;
446 png_ptr->usr_channels = png_ptr->channels;
447
Guy Schalnat0d580581995-07-20 02:43:20 -0500448 /* pack the header information into the buffer */
449 png_save_uint_32(buf, width);
450 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600451 buf[8] = (png_byte)bit_depth;
452 buf[9] = (png_byte)color_type;
453 buf[10] = (png_byte)compression_type;
454 buf[11] = (png_byte)filter_type;
455 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500456
457 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600458 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500459
Andreas Dilger47a0c421997-05-16 02:46:07 -0500460 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600461 png_ptr->zstream.zalloc = png_zalloc;
462 png_ptr->zstream.zfree = png_zfree;
463 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500464 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500465 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500466 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
467 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500468 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500469 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500470 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500471 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500472 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500473 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500474 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500475 png_ptr->zlib_strategy = Z_FILTERED;
476 else
477 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
478 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500480 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500481 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500482 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500483 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500484 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500485 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500486 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600487 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 png_ptr->zlib_method, png_ptr->zlib_window_bits,
489 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600490 png_ptr->zstream.next_out = png_ptr->zbuf;
491 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500492
Guy Schalnate5a37791996-06-05 15:50:50 -0500493 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500494}
495
496/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500497 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600498 * structure.
499 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500500void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500502{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600503#ifdef PNG_USE_LOCAL_ARRAYS
504 PNG_PLTE;
505#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500506 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600507 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500508 png_byte buf[3];
509
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500511 if ((
512#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
513 !png_ptr->empty_plte_permitted &&
514#endif
515 num_pal == 0) || num_pal > 256)
516 {
517 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
518 {
519 png_error(png_ptr, "Invalid number of colors in palette");
520 }
521 else
522 {
523 png_warning(png_ptr, "Invalid number of colors in palette");
524 return;
525 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500526 }
527
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 png_ptr->num_palette = (png_uint_16)num_pal;
529 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500530
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600531 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500532 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500533 {
534 buf[0] = pal_ptr->red;
535 buf[1] = pal_ptr->green;
536 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500537 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500538 }
539 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500540 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500541}
542
543/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500544void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500546{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600547#ifdef PNG_USE_LOCAL_ARRAYS
548 PNG_IDAT;
549#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600551 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500553}
554
555/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500556void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600557png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500558{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600559#ifdef PNG_USE_LOCAL_ARRAYS
560 PNG_IEND;
561#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500562 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600563 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600564 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500565}
566
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500567#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500568/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600569#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500570void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500572{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600573#ifdef PNG_USE_LOCAL_ARRAYS
574 PNG_gAMA;
575#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 png_uint_32 igamma;
577 png_byte buf[4];
578
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600580 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600581 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500582 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600583 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500584}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500585#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600586#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500587void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600588png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600589{
590#ifdef PNG_USE_LOCAL_ARRAYS
591 PNG_gAMA;
592#endif
593 png_byte buf[4];
594
595 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600596 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600597 png_save_uint_32(buf, file_gamma);
598 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
599}
600#endif
601#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500602
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600603#if defined(PNG_WRITE_sRGB_SUPPORTED)
604/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500605void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600606png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600607{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600608#ifdef PNG_USE_LOCAL_ARRAYS
609 PNG_sRGB;
610#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600611 png_byte buf[1];
612
613 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600614 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600615 png_warning(png_ptr,
616 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600617 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600618 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600619}
620#endif
621
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600622#if defined(PNG_WRITE_iCCP_SUPPORTED)
623/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500624void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600625png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
626 png_charp profile, int profile_len)
627{
628#ifdef PNG_USE_LOCAL_ARRAYS
629 PNG_iCCP;
630#endif
631 png_size_t name_len;
632 png_charp new_name;
633 compression_state comp;
634
635 png_debug(1, "in png_write_iCCP\n");
636 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
637 &new_name)) == 0)
638 {
639 png_warning(png_ptr, "Empty keyword in iCCP chunk");
640 return;
641 }
642
643 if (compression_type)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600644 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600645
646 if (profile == NULL || *profile == '\0')
647 profile_len = 0;
648
649 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500650 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
651 PNG_TEXT_COMPRESSION_zTXt, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600652
653 /* make sure we include the NULL after the name and the compression type */
654 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
655 (png_uint_32)name_len+profile_len+2);
656 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
657
658 if (profile_len)
659 png_write_compressed_data_out(png_ptr, &comp);
660
661 png_write_chunk_end(png_ptr);
662 png_free(png_ptr, new_name);
663}
664#endif
665
666#if defined(PNG_WRITE_sPLT_SUPPORTED)
667/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500668void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600669png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600670{
671#ifdef PNG_USE_LOCAL_ARRAYS
672 PNG_sPLT;
673#endif
674 png_size_t name_len;
675 png_charp new_name;
676 png_byte entrybuf[10];
677 int entry_size = (spalette->depth == 8 ? 6 : 10);
678 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600679 png_sPLT_entryp ep;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600680
681 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600682 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
683 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600684 {
685 png_warning(png_ptr, "Empty keyword in sPLT chunk");
686 return;
687 }
688
689 /* make sure we include the NULL after the name */
690 png_write_chunk_start(png_ptr, (png_bytep) png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600691 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600692 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600693 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600694
695 /* loop through each palette entry, writing appropriately */
696 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
697 {
698 if (spalette->depth == 8)
699 {
700 entrybuf[0] = (png_byte)ep->red;
701 entrybuf[1] = (png_byte)ep->green;
702 entrybuf[2] = (png_byte)ep->blue;
703 entrybuf[3] = (png_byte)ep->alpha;
704 png_save_uint_16(entrybuf + 4, ep->frequency);
705 }
706 else
707 {
708 png_save_uint_16(entrybuf + 0, ep->red);
709 png_save_uint_16(entrybuf + 2, ep->green);
710 png_save_uint_16(entrybuf + 4, ep->blue);
711 png_save_uint_16(entrybuf + 6, ep->alpha);
712 png_save_uint_16(entrybuf + 8, ep->frequency);
713 }
714 png_write_chunk_data(png_ptr, entrybuf, entry_size);
715 }
716
717 png_write_chunk_end(png_ptr);
718 png_free(png_ptr, new_name);
719}
720#endif
721
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500722#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500723/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500724void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600725png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500726{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600727#ifdef PNG_USE_LOCAL_ARRAYS
728 PNG_sBIT;
729#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500730 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500732
Andreas Dilger47a0c421997-05-16 02:46:07 -0500733 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600734 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500735 if (color_type & PNG_COLOR_MASK_COLOR)
736 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600737 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500738
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500739 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
740 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600741 if (sbit->red == 0 || sbit->red > maxbits ||
742 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500743 sbit->blue == 0 || sbit->blue > maxbits)
744 {
745 png_warning(png_ptr, "Invalid sBIT depth specified");
746 return;
747 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 buf[0] = sbit->red;
749 buf[1] = sbit->green;
750 buf[2] = sbit->blue;
751 size = 3;
752 }
753 else
754 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500755 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
756 {
757 png_warning(png_ptr, "Invalid sBIT depth specified");
758 return;
759 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500760 buf[0] = sbit->gray;
761 size = 1;
762 }
763
764 if (color_type & PNG_COLOR_MASK_ALPHA)
765 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500766 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
767 {
768 png_warning(png_ptr, "Invalid sBIT depth specified");
769 return;
770 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500771 buf[size++] = sbit->alpha;
772 }
773
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600774 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500775}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500776#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500777
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500778#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500779/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600780#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500781void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500782png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600783 double red_x, double red_y, double green_x, double green_y,
784 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500785{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600786#ifdef PNG_USE_LOCAL_ARRAYS
787 PNG_cHRM;
788#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500789 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600790 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500791
Andreas Dilger47a0c421997-05-16 02:46:07 -0500792 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600793 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500794 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
795 white_x + white_y > 1.0)
796 {
797 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600798#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600799 printf("white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600800#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500801 return;
802 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600803 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500804 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600805 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500806 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500807
808 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
809 red_x + red_y > 1.0)
810 {
811 png_warning(png_ptr, "Invalid cHRM red point specified");
812 return;
813 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600814 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500815 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600816 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500817 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500818
819 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
820 green_x + green_y > 1.0)
821 {
822 png_warning(png_ptr, "Invalid cHRM green point specified");
823 return;
824 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600825 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500826 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600827 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500828 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500829
830 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
831 blue_x + blue_y > 1.0)
832 {
833 png_warning(png_ptr, "Invalid cHRM blue point specified");
834 return;
835 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600836 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600838 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500839 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500840
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600841 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500842}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500843#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600844#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500845void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600846png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
847 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
848 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
849 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600850{
851#ifdef PNG_USE_LOCAL_ARRAYS
852 PNG_cHRM;
853#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600854 png_byte buf[32];
855
856 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600857 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600858 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
859 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600860 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600861#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600862 printf("white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600863#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600864 return;
865 }
866 png_save_uint_32(buf, white_x);
867 png_save_uint_32(buf + 4, white_y);
868
869 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
870 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600871 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600872 return;
873 }
874 png_save_uint_32(buf + 8, red_x);
875 png_save_uint_32(buf + 12, red_y);
876
877 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
878 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600879 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600880 return;
881 }
882 png_save_uint_32(buf + 16, green_x);
883 png_save_uint_32(buf + 20, green_y);
884
885 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
886 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600887 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600888 return;
889 }
890 png_save_uint_32(buf + 24, blue_x);
891 png_save_uint_32(buf + 28, blue_y);
892
893 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
894}
895#endif
896#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500897
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500898#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500899/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500900void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600901png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500902 int num_trans, int color_type)
903{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600904#ifdef PNG_USE_LOCAL_ARRAYS
905 PNG_tRNS;
906#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500907 png_byte buf[6];
908
Andreas Dilger47a0c421997-05-16 02:46:07 -0500909 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500910 if (color_type == PNG_COLOR_TYPE_PALETTE)
911 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600912 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500913 {
914 png_warning(png_ptr,"Invalid number of transparent colors specified");
915 return;
916 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500917 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600918 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500919 }
920 else if (color_type == PNG_COLOR_TYPE_GRAY)
921 {
922 /* one 16 bit value */
923 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600924 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500925 }
926 else if (color_type == PNG_COLOR_TYPE_RGB)
927 {
928 /* three 16 bit values */
929 png_save_uint_16(buf, tran->red);
930 png_save_uint_16(buf + 2, tran->green);
931 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600932 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500933 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500934 else
935 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600936 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500937 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500938}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500939#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500940
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500941#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500942/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500943void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600944png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500945{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600946#ifdef PNG_USE_LOCAL_ARRAYS
947 PNG_bKGD;
948#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500949 png_byte buf[6];
950
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500952 if (color_type == PNG_COLOR_TYPE_PALETTE)
953 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500954 if (
955#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
956 (!png_ptr->empty_plte_permitted ||
957 (png_ptr->empty_plte_permitted && png_ptr->num_palette)) &&
958#endif
959 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500960 {
961 png_warning(png_ptr, "Invalid background palette index");
962 return;
963 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500964 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600965 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500966 }
967 else if (color_type & PNG_COLOR_MASK_COLOR)
968 {
969 png_save_uint_16(buf, back->red);
970 png_save_uint_16(buf + 2, back->green);
971 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600972 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500973 }
974 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600975 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500976 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600977 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500978 }
979}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500980#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500981
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500982#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500983/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500984void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600985png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500986{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600987#ifdef PNG_USE_LOCAL_ARRAYS
988 PNG_hIST;
989#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600990 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500991 png_byte buf[3];
992
Andreas Dilger47a0c421997-05-16 02:46:07 -0500993 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600994 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500995 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
997 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500998 png_warning(png_ptr, "Invalid number of histogram entries specified");
999 return;
1000 }
1001
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001002 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001004 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001005 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001007 }
1008 png_write_chunk_end(png_ptr);
1009}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001010#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001011
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001012#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1013 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001014/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1015 * and if invalid, correct the keyword rather than discarding the entire
1016 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1017 * length, forbids leading or trailing whitespace, multiple internal spaces,
1018 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001019 *
1020 * The new_key is allocated to hold the corrected keyword and must be freed
1021 * by the calling routine. This avoids problems with trying to write to
1022 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001023 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001024png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001025png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001026{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001027 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001028 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001029 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001030
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031 png_debug(1, "in png_check_keyword\n");
1032 *new_key = NULL;
1033
1034 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001035 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001036 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001037 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001038 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001039
Andreas Dilger47a0c421997-05-16 02:46:07 -05001040 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1041
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001042 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001043
1044 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001045 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001047 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001049#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001050 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001051
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001052 sprintf(msg, "invalid keyword character 0x%02X", *kp);
1053 png_chunk_warning(png_ptr, msg);
1054#else
1055 png_chunk_warning(png_ptr, "invalid character in keyword");
1056#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001057 *dp = ' ';
1058 }
1059 else
1060 {
1061 *dp = *kp;
1062 }
1063 }
1064 *dp = '\0';
1065
1066 /* Remove any trailing white space. */
1067 kp = *new_key + key_len - 1;
1068 if (*kp == ' ')
1069 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001070 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001071
1072 while (*kp == ' ')
1073 {
1074 *(kp--) = '\0';
1075 key_len--;
1076 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001077 }
1078
1079 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001080 kp = *new_key;
1081 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001082 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001083 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001084
1085 while (*kp == ' ')
1086 {
1087 kp++;
1088 key_len--;
1089 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001090 }
1091
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001093
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 /* Remove multiple internal spaces. */
1095 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001096 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001098 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001099 *(dp++) = *kp;
1100 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001101 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001103 {
1104 key_len--;
1105 }
1106 else
1107 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 *(dp++) = *kp;
1109 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001110 }
1111 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001112 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001113
1114 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001116 png_free(png_ptr, *new_key);
1117 *new_key=NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001118 png_chunk_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 }
1120
1121 if (key_len > 79)
1122 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001123 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124 new_key[79] = '\0';
1125 key_len = 79;
1126 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001127
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001128 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001129}
1130#endif
1131
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001132#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001133/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001134void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001135png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001136 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001137{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001138#ifdef PNG_USE_LOCAL_ARRAYS
1139 PNG_tEXt;
1140#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001142 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001143
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 png_debug(1, "in png_write_tEXt\n");
1145 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1146 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001147 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001148 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001150
Andreas Dilger47a0c421997-05-16 02:46:07 -05001151 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001152 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001153 else
1154 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001155
1156 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001157 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 -05001158 /*
1159 * We leave it to the application to meet PNG-1.0 requirements on the
1160 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1161 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001162 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001163 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001164 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001165 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001167
Guy Schalnat0d580581995-07-20 02:43:20 -05001168 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001170}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001171#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001172
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001173#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001175void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001176png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001178{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001179#ifdef PNG_USE_LOCAL_ARRAYS
1180 PNG_zTXt;
1181#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001182 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001183 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001184 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001185 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001186
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001188
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001190 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001191 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001192 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001193 }
1194
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1196 {
1197 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1198 png_free(png_ptr, new_key);
1199 return;
1200 }
1201
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001202 text_len = png_strlen(text);
1203
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 png_free(png_ptr, new_key);
1205
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001206 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001207 text_len = png_text_compress(png_ptr, text, text_len, compression,
1208 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001209
1210 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001211 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1212 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001213 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001215 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001216 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001218 /* write the compressed data */
1219 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001220
Guy Schalnat0d580581995-07-20 02:43:20 -05001221 /* close the chunk */
1222 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001223}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001224#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001225
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001226#if defined(PNG_WRITE_iTXt_SUPPORTED)
1227/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001228void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001229png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001230 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001231{
1232#ifdef PNG_USE_LOCAL_ARRAYS
1233 PNG_iTXt;
1234#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001235 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001236 png_charp new_lang, new_key;
1237 png_byte cbuf[2];
1238 compression_state comp;
1239
1240 png_debug(1, "in png_write_iTXt\n");
1241
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001242 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1243 {
1244 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1245 return;
1246 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001247 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1248 &new_lang))==0)
1249 {
1250 png_warning(png_ptr, "Empty language field in iTXt chunk");
1251 return;
1252 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001253 lang_key_len = png_strlen(lang_key);
1254 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001255
1256 if (text == NULL || *text == '\0')
1257 text_len = 0;
1258
1259 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001260 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1261 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001262
1263 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001264 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001265
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001266 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1267 (png_uint_32)(
1268 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1269 + key_len
1270 + lang_len
1271 + lang_key_len
1272 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001273
1274 /*
1275 * We leave it to the application to meet PNG-1.0 requirements on the
1276 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1277 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1278 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1279 */
1280 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001281
1282 /* set the compression flag */
1283 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1284 compression == PNG_TEXT_COMPRESSION_NONE)
1285 cbuf[0] = 0;
1286 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1287 cbuf[0] = 1;
1288 /* set the compression method */
1289 cbuf[1] = 0;
1290 png_write_chunk_data(png_ptr, cbuf, 2);
1291
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001292 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001293 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001294 png_write_chunk_data(png_ptr, '\0', 1);
1295
1296 png_write_compressed_data_out(png_ptr, &comp);
1297
1298 png_write_chunk_end(png_ptr);
1299 png_free(png_ptr, new_key);
1300 png_free(png_ptr, new_lang);
1301}
1302#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303
1304#if defined(PNG_WRITE_oFFs_SUPPORTED)
1305/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001306void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1308 png_uint_32 y_offset,
1309 int unit_type)
1310{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001311#ifdef PNG_USE_LOCAL_ARRAYS
1312 PNG_oFFs;
1313#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001314 png_byte buf[9];
1315
1316 png_debug(1, "in png_write_oFFs\n");
1317 if (unit_type >= PNG_OFFSET_LAST)
1318 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1319
1320 png_save_uint_32(buf, x_offset);
1321 png_save_uint_32(buf + 4, y_offset);
1322 buf[8] = (png_byte)unit_type;
1323
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001324 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001325}
1326#endif
1327
1328#if defined(PNG_WRITE_pCAL_SUPPORTED)
1329/* write the pCAL chunk (png-scivis-19970203) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001330void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001331png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1332 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1333{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001334#ifdef PNG_USE_LOCAL_ARRAYS
1335 PNG_pCAL;
1336#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001337 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338 png_uint_32p params_len;
1339 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001340 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 int i;
1342
1343 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1344 if (type >= PNG_EQUATION_LAST)
1345 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1346
1347 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1348 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1349 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1350 png_debug1(3, "pCAL units length = %d\n", units_len);
1351 total_len = purpose_len + units_len + 10;
1352
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001353 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1354 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001355
1356 /* Find the length of each parameter, making sure we don't count the
1357 null terminator for the last parameter. */
1358 for (i = 0; i < nparams; i++)
1359 {
1360 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1361 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
1362 total_len += (png_size_t)params_len[i];
1363 }
1364
1365 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001366 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001367 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001368 png_save_int_32(buf, X0);
1369 png_save_int_32(buf + 4, X1);
1370 buf[8] = (png_byte)type;
1371 buf[9] = (png_byte)nparams;
1372 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1373 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1374
1375 png_free(png_ptr, new_purpose);
1376
1377 for (i = 0; i < nparams; i++)
1378 {
1379 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1380 (png_size_t)params_len[i]);
1381 }
1382
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001383 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001384 png_write_chunk_end(png_ptr);
1385}
1386#endif
1387
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001388#if defined(PNG_WRITE_sCAL_SUPPORTED)
1389/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001390#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001391void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001392png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001393{
1394#ifdef PNG_USE_LOCAL_ARRAYS
1395 PNG_sCAL;
1396#endif
1397 png_size_t total_len;
1398 char wbuf[32], hbuf[32];
1399
1400 png_debug(1, "in png_write_sCAL\n");
1401
1402 sprintf(wbuf, "%12.12e", width);
1403 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001404 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001405
1406 png_debug1(3, "sCAL total length = %d\n", total_len);
1407 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001408 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001409 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1410 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1411
1412 png_write_chunk_end(png_ptr);
1413}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001414#else
1415#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001416void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001417png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001418 png_charp height)
1419{
1420#ifdef PNG_USE_LOCAL_ARRAYS
1421 PNG_sCAL;
1422#endif
1423 png_size_t total_len;
1424 char wbuf[32], hbuf[32];
1425
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001426 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001427
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001428 strcpy(wbuf,(const char *)width);
1429 strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001430 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001431
1432 png_debug1(3, "sCAL total length = %d\n", total_len);
1433 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001434 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001435 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1436 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1437
1438 png_write_chunk_end(png_ptr);
1439}
1440#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001441#endif
1442#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001443
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001444#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001445/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001446void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001447png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001448 png_uint_32 y_pixels_per_unit,
1449 int unit_type)
1450{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001451#ifdef PNG_USE_LOCAL_ARRAYS
1452 PNG_pHYs;
1453#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001454 png_byte buf[9];
1455
Andreas Dilger47a0c421997-05-16 02:46:07 -05001456 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001457 if (unit_type >= PNG_RESOLUTION_LAST)
1458 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1459
Guy Schalnat0d580581995-07-20 02:43:20 -05001460 png_save_uint_32(buf, x_pixels_per_unit);
1461 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001462 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001463
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001464 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001465}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001466#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001467
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001468#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001469/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1470 * or png_convert_from_time_t(), or fill in the structure yourself.
1471 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001472void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001473png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001474{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001475#ifdef PNG_USE_LOCAL_ARRAYS
1476 PNG_tIME;
1477#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001478 png_byte buf[7];
1479
Andreas Dilger47a0c421997-05-16 02:46:07 -05001480 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001481 if (mod_time->month > 12 || mod_time->month < 1 ||
1482 mod_time->day > 31 || mod_time->day < 1 ||
1483 mod_time->hour > 23 || mod_time->second > 60)
1484 {
1485 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1486 return;
1487 }
1488
Guy Schalnat0d580581995-07-20 02:43:20 -05001489 png_save_uint_16(buf, mod_time->year);
1490 buf[2] = mod_time->month;
1491 buf[3] = mod_time->day;
1492 buf[4] = mod_time->hour;
1493 buf[5] = mod_time->minute;
1494 buf[6] = mod_time->second;
1495
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001496 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001497}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001498#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001499
1500/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001501void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001502png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001503{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001504#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001505 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001506
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001507 /* start of interlace block */
1508 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001509
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001510 /* offset to next interlace block */
1511 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001512
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001513 /* start of interlace block in the y direction */
1514 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001515
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001516 /* offset to next interlace block in the y direction */
1517 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001518#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001519
Andreas Dilger47a0c421997-05-16 02:46:07 -05001520 png_size_t buf_size;
1521
1522 png_debug(1, "in png_write_start_row\n");
1523 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1524 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1525
Guy Schalnat0d580581995-07-20 02:43:20 -05001526 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001527 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001528 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001529
1530 /* set up filtering buffer, if using this filter */
1531 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001532 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001533 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001534 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001535 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001536 }
1537
Andreas Dilger47a0c421997-05-16 02:46:07 -05001538 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001539 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1540 {
1541 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001542 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001543 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001544
1545 if (png_ptr->do_filter & PNG_FILTER_UP)
1546 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001547 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001548 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001549 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001550 }
1551
1552 if (png_ptr->do_filter & PNG_FILTER_AVG)
1553 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001554 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1555 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001556 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001557 }
1558
1559 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1560 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001561 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001562 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001563 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001564 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001565 }
1566
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001567#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001568 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001569 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001570 {
1571 if (!(png_ptr->transformations & PNG_INTERLACE))
1572 {
1573 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1574 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001575 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1576 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001577 }
1578 else
1579 {
1580 png_ptr->num_rows = png_ptr->height;
1581 png_ptr->usr_width = png_ptr->width;
1582 }
1583 }
1584 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001585#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001586 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001587 png_ptr->num_rows = png_ptr->height;
1588 png_ptr->usr_width = png_ptr->width;
1589 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001590 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1591 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001592}
1593
Andreas Dilger47a0c421997-05-16 02:46:07 -05001594/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001595void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001596png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001597{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001598#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001599 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001600
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001601 /* start of interlace block */
1602 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001603
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001604 /* offset to next interlace block */
1605 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001606
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001607 /* start of interlace block in the y direction */
1608 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001609
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001610 /* offset to next interlace block in the y direction */
1611 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001612#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613
Guy Schalnat0d580581995-07-20 02:43:20 -05001614 int ret;
1615
Andreas Dilger47a0c421997-05-16 02:46:07 -05001616 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001617 /* next row */
1618 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001619
Guy Schalnat0d580581995-07-20 02:43:20 -05001620 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001621 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001622 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001623
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001624#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001625 /* if interlaced, go to next pass */
1626 if (png_ptr->interlaced)
1627 {
1628 png_ptr->row_number = 0;
1629 if (png_ptr->transformations & PNG_INTERLACE)
1630 {
1631 png_ptr->pass++;
1632 }
1633 else
1634 {
1635 /* loop until we find a non-zero width or height pass */
1636 do
1637 {
1638 png_ptr->pass++;
1639 if (png_ptr->pass >= 7)
1640 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001641 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001642 png_pass_inc[png_ptr->pass] - 1 -
1643 png_pass_start[png_ptr->pass]) /
1644 png_pass_inc[png_ptr->pass];
1645 png_ptr->num_rows = (png_ptr->height +
1646 png_pass_yinc[png_ptr->pass] - 1 -
1647 png_pass_ystart[png_ptr->pass]) /
1648 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001649 if (png_ptr->transformations & PNG_INTERLACE)
1650 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001651 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1652
1653 }
1654
Guy Schalnate5a37791996-06-05 15:50:50 -05001655 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001656 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001657 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001658 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001659 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001660 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001661 (png_uint_32)png_ptr->usr_bit_depth *
1662 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001663 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001664 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001665 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001666#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001667
1668 /* if we get here, we've just written the last row, so we need
1669 to flush the compressor */
1670 do
1671 {
1672 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001673 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001674 /* check for an error */
1675 if (ret != Z_OK && ret != Z_STREAM_END)
1676 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001677 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001678 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001679 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001680 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001681 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001682 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001683 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001684 {
1685 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001686 png_ptr->zstream.next_out = png_ptr->zbuf;
1687 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001688 }
1689 } while (ret != Z_STREAM_END);
1690
1691 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001692 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001693 {
1694 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001695 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001696 }
1697
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001698 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001699}
1700
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001701#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001702/* Pick out the correct pixels for the interlace pass.
1703 * The basic idea here is to go through the row with a source
1704 * pointer and a destination pointer (sp and dp), and copy the
1705 * correct pixels for the pass. As the row gets compacted,
1706 * sp will always be >= dp, so we should never overwrite anything.
1707 * See the default: case for the easiest code to understand.
1708 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001709void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001710png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001711{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001712#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001713 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001714
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001715 /* start of interlace block */
1716 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001717
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001718 /* offset to next interlace block */
1719 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001720#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001721
Andreas Dilger47a0c421997-05-16 02:46:07 -05001722 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001723 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001724#if defined(PNG_USELESS_TESTS_SUPPORTED)
1725 if (row != NULL && row_info != NULL && pass < 6)
1726#else
1727 if (pass < 6)
1728#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001729 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001730 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001731 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001732 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001733 case 1:
1734 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001735 png_bytep sp;
1736 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001737 int shift;
1738 int d;
1739 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001740 png_uint_32 i;
1741 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001742
1743 dp = row;
1744 d = 0;
1745 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001746 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001747 i += png_pass_inc[pass])
1748 {
1749 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001750 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001751 d |= (value << shift);
1752
1753 if (shift == 0)
1754 {
1755 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001756 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001757 d = 0;
1758 }
1759 else
1760 shift--;
1761
1762 }
1763 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001764 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001765 break;
1766 }
1767 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001768 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001769 png_bytep sp;
1770 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001771 int shift;
1772 int d;
1773 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001774 png_uint_32 i;
1775 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001776
1777 dp = row;
1778 shift = 6;
1779 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001780 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001781 i += png_pass_inc[pass])
1782 {
1783 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001784 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001785 d |= (value << shift);
1786
1787 if (shift == 0)
1788 {
1789 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001790 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001791 d = 0;
1792 }
1793 else
1794 shift -= 2;
1795 }
1796 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001797 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001798 break;
1799 }
1800 case 4:
1801 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001802 png_bytep sp;
1803 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001804 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001805 int d;
1806 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001807 png_uint_32 i;
1808 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001809
1810 dp = row;
1811 shift = 4;
1812 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001813 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001814 i += png_pass_inc[pass])
1815 {
1816 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001817 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001818 d |= (value << shift);
1819
1820 if (shift == 0)
1821 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001822 shift = 4;
1823 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 d = 0;
1825 }
1826 else
1827 shift -= 4;
1828 }
1829 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001830 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001831 break;
1832 }
1833 default:
1834 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001835 png_bytep sp;
1836 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001837 png_uint_32 i;
1838 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001839 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001840
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001841 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001842 dp = row;
1843 /* find out how many bytes each pixel takes up */
1844 pixel_bytes = (row_info->pixel_depth >> 3);
1845 /* loop through the row, only looking at the pixels that
1846 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001847 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 i += png_pass_inc[pass])
1849 {
1850 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001851 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 /* move the pixel */
1853 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001854 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001855 /* next pixel */
1856 dp += pixel_bytes;
1857 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001858 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001859 }
1860 }
1861 /* set new row width */
1862 row_info->width = (row_info->width +
1863 png_pass_inc[pass] - 1 -
1864 png_pass_start[pass]) /
1865 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001866 row_info->rowbytes = ((row_info->width *
1867 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001868 }
1869}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001870#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001871
Andreas Dilger47a0c421997-05-16 02:46:07 -05001872/* This filters the row, chooses which filter to use, if it has not already
1873 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001874 * chosen filter.
1875 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001876#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001877#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001878#define PNG_LOMASK ((png_uint_32)0xffffL)
1879#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001880void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001881png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001882{
Guy Schalnate5a37791996-06-05 15:50:50 -05001883 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001884 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001885 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001886 png_uint_32 row_bytes = row_info->rowbytes;
1887#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1888 int num_p_filters = (int)png_ptr->num_prev_filters;
1889#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001890
Andreas Dilger47a0c421997-05-16 02:46:07 -05001891 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001892 /* find out how many bytes offset each pixel is */
1893 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001894
1895 prev_row = png_ptr->prev_row;
1896 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001897 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001898
Andreas Dilger47a0c421997-05-16 02:46:07 -05001899 /* The prediction method we use is to find which method provides the
1900 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001901 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001902 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001903 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001904 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001905 * predictive" method (not implemented yet), which does test compressions
1906 * of lines using different filter methods, and then chooses the
1907 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001908 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001909 *
1910 * GRR 980525: consider also
1911 * (1) minimum sum of absolute differences from running average (i.e.,
1912 * keep running sum of non-absolute differences & count of bytes)
1913 * [track dispersion, too? restart average if dispersion too large?]
1914 * (1b) minimum sum of absolute differences from sliding average, probably
1915 * with window size <= deflate window (usually 32K)
1916 * (2) minimum sum of squared differences from zero or running average
1917 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001918 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001919
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001920
Guy Schalnate5a37791996-06-05 15:50:50 -05001921 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001922 * that has been chosen, as it doesn't actually do anything to the data.
1923 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001924 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001925 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001927 png_bytep rp;
1928 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001929 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001930 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001931
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001932 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001933 {
1934 v = *rp;
1935 sum += (v < 128) ? v : 256 - v;
1936 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001937
1938#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1939 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1940 {
1941 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001942 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001943 sumlo = sum & PNG_LOMASK;
1944 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1945
1946 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001947 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001948 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001949 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001950 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001951 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001952 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001953 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001954 PNG_WEIGHT_SHIFT;
1955 }
1956 }
1957
1958 /* Factor in the cost of this filter (this is here for completeness,
1959 * but it makes no sense to have a "cost" for the NONE filter, as
1960 * it has the minimum possible computational cost - none).
1961 */
1962 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1963 PNG_COST_SHIFT;
1964 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1965 PNG_COST_SHIFT;
1966
1967 if (sumhi > PNG_HIMASK)
1968 sum = PNG_MAXSUM;
1969 else
1970 sum = (sumhi << PNG_HISHIFT) + sumlo;
1971 }
1972#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001973 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001974 }
1975
Guy Schalnate5a37791996-06-05 15:50:50 -05001976 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001977 if (filter_to_do == PNG_FILTER_SUB)
1978 /* it's the only filter so no testing is needed */
1979 {
1980 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001981 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001982 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1983 i++, rp++, dp++)
1984 {
1985 *dp = *rp;
1986 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001987 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001988 i++, rp++, lp++, dp++)
1989 {
1990 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
1991 }
1992 best_row = png_ptr->sub_row;
1993 }
1994
1995 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001996 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001997 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001998 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001999 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002000 int v;
2001
2002#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002003 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002004 * would reduce the sum of this filter, so that we can do the
2005 * early exit comparison without scaling the sum each time.
2006 */
2007 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2008 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002009 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002010 png_uint_32 lmhi, lmlo;
2011 lmlo = lmins & PNG_LOMASK;
2012 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2013
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002014 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002015 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002016 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002017 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002018 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002019 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002020 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002021 PNG_WEIGHT_SHIFT;
2022 }
2023 }
2024
2025 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2026 PNG_COST_SHIFT;
2027 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2028 PNG_COST_SHIFT;
2029
2030 if (lmhi > PNG_HIMASK)
2031 lmins = PNG_MAXSUM;
2032 else
2033 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2034 }
2035#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002036
Guy Schalnate5a37791996-06-05 15:50:50 -05002037 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2038 i++, rp++, dp++)
2039 {
2040 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002041
Guy Schalnate5a37791996-06-05 15:50:50 -05002042 sum += (v < 128) ? v : 256 - v;
2043 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002044 for (lp = row_buf + 1; i < row_info->rowbytes;
2045 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002046 {
2047 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002048
Guy Schalnate5a37791996-06-05 15:50:50 -05002049 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002050
2051 if (sum > lmins) /* We are already worse, don't continue. */
2052 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002053 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002054
2055#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2056 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2057 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002058 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002059 png_uint_32 sumhi, sumlo;
2060 sumlo = sum & PNG_LOMASK;
2061 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2062
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002063 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002064 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002065 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002066 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002067 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002068 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002069 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002070 PNG_WEIGHT_SHIFT;
2071 }
2072 }
2073
2074 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2075 PNG_COST_SHIFT;
2076 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2077 PNG_COST_SHIFT;
2078
2079 if (sumhi > PNG_HIMASK)
2080 sum = PNG_MAXSUM;
2081 else
2082 sum = (sumhi << PNG_HISHIFT) + sumlo;
2083 }
2084#endif
2085
Guy Schalnate5a37791996-06-05 15:50:50 -05002086 if (sum < mins)
2087 {
2088 mins = sum;
2089 best_row = png_ptr->sub_row;
2090 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002091 }
2092
Guy Schalnate5a37791996-06-05 15:50:50 -05002093 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002094 if (filter_to_do == PNG_FILTER_UP)
2095 {
2096 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002097 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002098
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002099 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002100 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002101 i++, rp++, pp++, dp++)
2102 {
2103 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2104 }
2105 best_row = png_ptr->up_row;
2106 }
2107
2108 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002109 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002110 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002111 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002112 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113 int v;
2114
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002115
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2117 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2118 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002119 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002120 png_uint_32 lmhi, lmlo;
2121 lmlo = lmins & PNG_LOMASK;
2122 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2123
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002124 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002125 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002126 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002127 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002128 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002129 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002130 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002131 PNG_WEIGHT_SHIFT;
2132 }
2133 }
2134
2135 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2136 PNG_COST_SHIFT;
2137 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2138 PNG_COST_SHIFT;
2139
2140 if (lmhi > PNG_HIMASK)
2141 lmins = PNG_MAXSUM;
2142 else
2143 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2144 }
2145#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002146
2147 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002148 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002149 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002150 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002151
2152 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002153
2154 if (sum > lmins) /* We are already worse, don't continue. */
2155 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002156 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002157
2158#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2159 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2160 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002161 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002162 png_uint_32 sumhi, sumlo;
2163 sumlo = sum & PNG_LOMASK;
2164 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2165
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002166 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002167 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002168 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002169 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002170 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002171 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002172 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002173 PNG_WEIGHT_SHIFT;
2174 }
2175 }
2176
2177 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2178 PNG_COST_SHIFT;
2179 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2180 PNG_COST_SHIFT;
2181
2182 if (sumhi > PNG_HIMASK)
2183 sum = PNG_MAXSUM;
2184 else
2185 sum = (sumhi << PNG_HISHIFT) + sumlo;
2186 }
2187#endif
2188
Guy Schalnate5a37791996-06-05 15:50:50 -05002189 if (sum < mins)
2190 {
2191 mins = sum;
2192 best_row = png_ptr->up_row;
2193 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002194 }
2195
Guy Schalnate5a37791996-06-05 15:50:50 -05002196 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002197 if (filter_to_do == PNG_FILTER_AVG)
2198 {
2199 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002200 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002201 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002202 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002203 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002204 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002205 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002206 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002207 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002208 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2209 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002210 }
2211 best_row = png_ptr->avg_row;
2212 }
2213
2214 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002215 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002216 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002217 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002218 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002219 int v;
2220
2221#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2222 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2223 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002224 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002225 png_uint_32 lmhi, lmlo;
2226 lmlo = lmins & PNG_LOMASK;
2227 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2228
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002229 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002230 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002231 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002232 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002233 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002234 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002235 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002236 PNG_WEIGHT_SHIFT;
2237 }
2238 }
2239
2240 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2241 PNG_COST_SHIFT;
2242 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2243 PNG_COST_SHIFT;
2244
2245 if (lmhi > PNG_HIMASK)
2246 lmins = PNG_MAXSUM;
2247 else
2248 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2249 }
2250#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002251
2252 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002253 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002254 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002255 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002256
2257 sum += (v < 128) ? v : 256 - v;
2258 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002260 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002261 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002262 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002263
2264 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002265
2266 if (sum > lmins) /* We are already worse, don't continue. */
2267 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002268 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002269
2270#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2271 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2272 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002273 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002274 png_uint_32 sumhi, sumlo;
2275 sumlo = sum & PNG_LOMASK;
2276 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2277
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002278 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002279 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002280 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002281 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002282 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002283 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002284 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002285 PNG_WEIGHT_SHIFT;
2286 }
2287 }
2288
2289 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2290 PNG_COST_SHIFT;
2291 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2292 PNG_COST_SHIFT;
2293
2294 if (sumhi > PNG_HIMASK)
2295 sum = PNG_MAXSUM;
2296 else
2297 sum = (sumhi << PNG_HISHIFT) + sumlo;
2298 }
2299#endif
2300
Guy Schalnate5a37791996-06-05 15:50:50 -05002301 if (sum < mins)
2302 {
2303 mins = sum;
2304 best_row = png_ptr->avg_row;
2305 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002306 }
2307
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002309 if (filter_to_do == PNG_FILTER_PAETH)
2310 {
2311 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002312 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002313 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002314 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002315 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002316 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002317 }
2318
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002319 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002320 {
2321 int a, b, c, pa, pb, pc, p;
2322
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002323 b = *pp++;
2324 c = *cp++;
2325 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002326
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002327 p = b - c;
2328 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002329
2330#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002331 pa = abs(p);
2332 pb = abs(pc);
2333 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002334#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002335 pa = p < 0 ? -p : p;
2336 pb = pc < 0 ? -pc : pc;
2337 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002338#endif
2339
2340 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2341
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002342 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002343 }
2344 best_row = png_ptr->paeth_row;
2345 }
2346
2347 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002348 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002349 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002350 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002351 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 int v;
2353
2354#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2355 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2356 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002357 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 png_uint_32 lmhi, lmlo;
2359 lmlo = lmins & PNG_LOMASK;
2360 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2361
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002362 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002363 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002364 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002365 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002366 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002367 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002368 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 PNG_WEIGHT_SHIFT;
2370 }
2371 }
2372
2373 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2374 PNG_COST_SHIFT;
2375 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2376 PNG_COST_SHIFT;
2377
2378 if (lmhi > PNG_HIMASK)
2379 lmins = PNG_MAXSUM;
2380 else
2381 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2382 }
2383#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002384
2385 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002386 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002387 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002388 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002389
2390 sum += (v < 128) ? v : 256 - v;
2391 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002392
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002393 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002394 {
2395 int a, b, c, pa, pb, pc, p;
2396
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002397 b = *pp++;
2398 c = *cp++;
2399 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002400
2401#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002402 p = b - c;
2403 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002404#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002405 pa = abs(p);
2406 pb = abs(pc);
2407 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002408#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002409 pa = p < 0 ? -p : p;
2410 pb = pc < 0 ? -pc : pc;
2411 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002412#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002413 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2414#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002415 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002416 pa = abs(p - a);
2417 pb = abs(p - b);
2418 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002419 if (pa <= pb && pa <= pc)
2420 p = a;
2421 else if (pb <= pc)
2422 p = b;
2423 else
2424 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002425#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002426
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002427 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002428
2429 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002430
2431 if (sum > lmins) /* We are already worse, don't continue. */
2432 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002433 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002434
2435#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2436 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2437 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002438 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002439 png_uint_32 sumhi, sumlo;
2440 sumlo = sum & PNG_LOMASK;
2441 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2442
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002443 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002444 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002445 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002446 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002448 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450 PNG_WEIGHT_SHIFT;
2451 }
2452 }
2453
2454 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2455 PNG_COST_SHIFT;
2456 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2457 PNG_COST_SHIFT;
2458
2459 if (sumhi > PNG_HIMASK)
2460 sum = PNG_MAXSUM;
2461 else
2462 sum = (sumhi << PNG_HISHIFT) + sumlo;
2463 }
2464#endif
2465
Guy Schalnate5a37791996-06-05 15:50:50 -05002466 if (sum < mins)
2467 {
2468 best_row = png_ptr->paeth_row;
2469 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002470 }
2471
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002473
Guy Schalnate5a37791996-06-05 15:50:50 -05002474 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002475
2476#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2477 /* Save the type of filter we picked this time for future calculations */
2478 if (png_ptr->num_prev_filters > 0)
2479 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002480 int j;
2481 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002482 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002483 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002484 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002485 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002486 }
2487#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002488}
2489
2490
Andreas Dilger47a0c421997-05-16 02:46:07 -05002491/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002492void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002493png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2494{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002495 png_debug(1, "in png_write_filtered_row\n");
2496 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002497 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002498 png_ptr->zstream.next_in = filtered_row;
2499 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002500 /* repeat until we have compressed all the data */
2501 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002502 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002503 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002504
Guy Schalnate5a37791996-06-05 15:50:50 -05002505 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002506 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002507 /* check for compression errors */
2508 if (ret != Z_OK)
2509 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002511 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002512 else
2513 png_error(png_ptr, "zlib error");
2514 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002515
Guy Schalnate5a37791996-06-05 15:50:50 -05002516 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002518 {
2519 /* write the IDAT and reset the zlib output buffer */
2520 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002521 png_ptr->zstream.next_out = png_ptr->zbuf;
2522 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002523 }
2524 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002525 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002526
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002527 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002528 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002529 {
2530 png_bytep tptr;
2531
2532 tptr = png_ptr->prev_row;
2533 png_ptr->prev_row = png_ptr->row_buf;
2534 png_ptr->row_buf = tptr;
2535 }
2536
Guy Schalnate5a37791996-06-05 15:50:50 -05002537 /* finish row - updates counters and flushes zlib if last row */
2538 png_write_finish_row(png_ptr);
2539
2540#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2541 png_ptr->flush_rows++;
2542
2543 if (png_ptr->flush_dist > 0 &&
2544 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002545 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002546 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002547 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002548#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002549}