blob: 4d669da71f6507409ca4c894e93bd1a8cadff1ac [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-Pehrson2ad31ae2000-12-15 08:54:42 -06004 * libpng 1.0.9beta5 - December 15, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonf5ed0e12000-11-18 18:19:14 -06006 * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050018void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
21 buf[0] = (png_byte)((i >> 24) & 0xff);
22 buf[1] = (png_byte)((i >> 16) & 0xff);
23 buf[2] = (png_byte)((i >> 8) & 0xff);
24 buf[3] = (png_byte)(i & 0xff);
25}
26
Andreas Dilger47a0c421997-05-16 02:46:07 -050027#if defined(PNG_WRITE_pCAL_SUPPORTED)
28/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050032void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40#endif
41
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042/* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
45 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050046void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050048{
49 buf[0] = (png_byte)((i >> 8) & 0xff);
50 buf[1] = (png_byte)(i & 0xff);
51}
52
Andreas Dilger47a0c421997-05-16 02:46:07 -050053/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
60 * functions instead.
61 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050062void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050065{
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050069}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
74 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050075void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050078{
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050080 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050081
Guy Schalnat0d580581995-07-20 02:43:20 -050082 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050083 png_save_uint_32(buf, length);
84 png_write_data(png_ptr, buf, (png_size_t)4);
85
Guy Schalnat0d580581995-07-20 02:43:20 -050086 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050088 /* reset the crc and run it over the chunk name */
89 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050091}
92
Andreas Dilger47a0c421997-05-16 02:46:07 -050093/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060094 * Note that multiple calls to this function are allowed, and that the
95 * sum of the lengths from these calls *must* add up to the total_length
96 * given to png_write_chunk_start().
97 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050098void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050099png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 /* write the data, and run the CRC over it */
102 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103 {
104 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600105 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 }
107}
108
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500110void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600111png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 png_byte buf[4];
114
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117
118 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119}
120
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600121/* Simple function to write the signature. If we have already written
122 * the magic bytes of the signature, or more likely, the PNG stream is
123 * being embedded into another stream and doesn't need its own signature,
124 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600125 * bytes have already been written.
126 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500127void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600128png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500129{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600130 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600132 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 (png_size_t)8 - png_ptr->sig_bytes);
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 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500175#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600176 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);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500263
264 if (ret == Z_OK)
265 {
266 /* check to see if we need more room */
267 if (!(png_ptr->zstream.avail_out))
268 {
269 /* check to make sure our output array has room */
270 if (comp->num_output_ptr >= comp->max_output_ptr)
271 {
272 int old_max;
273
274 old_max = comp->max_output_ptr;
275 comp->max_output_ptr = comp->num_output_ptr + 4;
276 if (comp->output_ptr != NULL)
277 {
278 png_charpp old_ptr;
279
280 old_ptr = comp->output_ptr;
281 /* This could be optimized to realloc() */
282 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
283 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
284 png_memcpy(comp->output_ptr, old_ptr,
285 old_max * sizeof (png_charp));
286 png_free(png_ptr, old_ptr);
287 }
288 else
289 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
290 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
291 }
292
293 /* save off the data */
294 comp->output_ptr[comp->num_output_ptr] =
295 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
296 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
297 png_ptr->zbuf_size);
298 comp->num_output_ptr++;
299
300 /* and reset the buffer pointers */
301 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
302 png_ptr->zstream.next_out = png_ptr->zbuf;
303 }
304 }
305 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306 {
307 /* we got an error */
308 if (png_ptr->zstream.msg != NULL)
309 png_error(png_ptr, png_ptr->zstream.msg);
310 else
311 png_error(png_ptr, "zlib error");
312 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600313 } while (ret != Z_STREAM_END);
314
315 /* text length is number of buffers plus last buffer */
316 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
317 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
318 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
319
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500320 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600321}
322
323/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500324static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600325png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
326{
327 int i;
328
329 /* handle the no-compression case */
330 if (comp->input)
331 {
332 png_write_chunk_data(png_ptr, (png_bytep)comp->input, comp->input_len);
333 return;
334 }
335
336 /* write saved output buffers, if any */
337 for (i = 0; i < comp->num_output_ptr; i++)
338 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600339 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
340 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600341 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500342 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600343 }
344 if (comp->max_output_ptr != 0)
345 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500346 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600347 /* write anything left in zbuf */
348 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
349 png_write_chunk_data(png_ptr, png_ptr->zbuf,
350 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
351
352 /* reset zlib for another zTXt/iTXt or the image data */
353 deflateReset(&png_ptr->zstream);
354
355}
356#endif
357
Guy Schalnat0d580581995-07-20 02:43:20 -0500358/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600359 * information. Note that the rest of this code depends upon this
360 * information being correct.
361 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500362void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600363png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500364 int bit_depth, int color_type, int compression_type, int filter_type,
365 int interlace_type)
366{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600367#ifdef PNG_USE_LOCAL_ARRAYS
368 PNG_IHDR;
369#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500370 png_byte buf[13]; /* buffer to store the IHDR info */
371
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500373 /* Check that we have valid input data from the application info */
374 switch (color_type)
375 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500377 switch (bit_depth)
378 {
379 case 1:
380 case 2:
381 case 4:
382 case 8:
383 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500385 }
386 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 if (bit_depth != 8 && bit_depth != 16)
389 png_error(png_ptr, "Invalid bit depth for RGB image");
390 png_ptr->channels = 3;
391 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500393 switch (bit_depth)
394 {
395 case 1:
396 case 2:
397 case 4:
398 case 8: png_ptr->channels = 1; break;
399 default: png_error(png_ptr, "Invalid bit depth for paletted image");
400 }
401 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 case PNG_COLOR_TYPE_GRAY_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 grayscale+alpha image");
405 png_ptr->channels = 2;
406 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500408 if (bit_depth != 8 && bit_depth != 16)
409 png_error(png_ptr, "Invalid bit depth for RGBA image");
410 png_ptr->channels = 4;
411 break;
412 default:
413 png_error(png_ptr, "Invalid image color type specified");
414 }
415
Andreas Dilger47a0c421997-05-16 02:46:07 -0500416 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500417 {
418 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500420 }
421
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600422 if (
423#if defined(PNG_MNG_FEATURES_SUPPORTED)
424 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
425 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
426#endif
427 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500428 {
429 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500431 }
432
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600433#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500434 if (interlace_type != PNG_INTERLACE_NONE &&
435 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500436 {
437 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500438 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500439 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600440#else
441 interlace_type=PNG_INTERLACE_NONE;
442#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500443
444 /* save off the relevent information */
445 png_ptr->bit_depth = (png_byte)bit_depth;
446 png_ptr->color_type = (png_byte)color_type;
447 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600448 png_ptr->filter_type = (png_byte)filter_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500449 png_ptr->width = width;
450 png_ptr->height = height;
451
452 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500454 /* set the usr info, so any transformations can modify it */
455 png_ptr->usr_width = png_ptr->width;
456 png_ptr->usr_bit_depth = png_ptr->bit_depth;
457 png_ptr->usr_channels = png_ptr->channels;
458
Guy Schalnat0d580581995-07-20 02:43:20 -0500459 /* pack the header information into the buffer */
460 png_save_uint_32(buf, width);
461 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600462 buf[8] = (png_byte)bit_depth;
463 buf[9] = (png_byte)color_type;
464 buf[10] = (png_byte)compression_type;
465 buf[11] = (png_byte)filter_type;
466 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500467
468 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600469 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500470
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600472 png_ptr->zstream.zalloc = png_zalloc;
473 png_ptr->zstream.zfree = png_zfree;
474 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500475 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500476 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500477 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
478 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500480 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500481 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500482 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500483 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500484 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500485 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500486 png_ptr->zlib_strategy = Z_FILTERED;
487 else
488 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
489 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500490 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500491 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500492 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500493 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500495 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500496 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500497 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600498 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 png_ptr->zlib_method, png_ptr->zlib_window_bits,
500 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600501 png_ptr->zstream.next_out = png_ptr->zbuf;
502 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500503
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500505}
506
507/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500508 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600509 * structure.
510 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500511void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500512png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500513{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600514#ifdef PNG_USE_LOCAL_ARRAYS
515 PNG_PLTE;
516#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500517 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600518 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 png_byte buf[3];
520
Andreas Dilger47a0c421997-05-16 02:46:07 -0500521 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500522 if ((
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600523#if defined(PNG_MNG_FEATURES_SUPPORTED)
524 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500525#endif
526 num_pal == 0) || num_pal > 256)
527 {
528 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
529 {
530 png_error(png_ptr, "Invalid number of colors in palette");
531 }
532 else
533 {
534 png_warning(png_ptr, "Invalid number of colors in palette");
535 return;
536 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500537 }
538
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539 png_ptr->num_palette = (png_uint_16)num_pal;
540 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500541
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600542 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500543#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500544 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500545 {
546 buf[0] = pal_ptr->red;
547 buf[1] = pal_ptr->green;
548 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500550 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500551#else
552 /* This is a little slower but some buggy compilers need to do this instead */
553 pal_ptr=palette;
554 for (i = 0; i < num_pal; i++)
555 {
556 buf[0] = pal_ptr[i].red;
557 buf[1] = pal_ptr[i].green;
558 buf[2] = pal_ptr[i].blue;
559 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
560 }
561#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500562 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500563 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500564}
565
566/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500567void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500568png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500569{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600570#ifdef PNG_USE_LOCAL_ARRAYS
571 PNG_IDAT;
572#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500573 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600574 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500575 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500576}
577
578/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500579void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600580png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500581{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600582#ifdef PNG_USE_LOCAL_ARRAYS
583 PNG_IEND;
584#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600586 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600587 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500588}
589
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500590#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500591/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600592#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500593void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500595{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600596#ifdef PNG_USE_LOCAL_ARRAYS
597 PNG_gAMA;
598#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500599 png_uint_32 igamma;
600 png_byte buf[4];
601
Andreas Dilger47a0c421997-05-16 02:46:07 -0500602 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600603 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600604 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500605 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600606 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500607}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500608#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600609#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500610void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600611png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600612{
613#ifdef PNG_USE_LOCAL_ARRAYS
614 PNG_gAMA;
615#endif
616 png_byte buf[4];
617
618 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600619 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600620 png_save_uint_32(buf, file_gamma);
621 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
622}
623#endif
624#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500625
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600626#if defined(PNG_WRITE_sRGB_SUPPORTED)
627/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500628void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600629png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600630{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600631#ifdef PNG_USE_LOCAL_ARRAYS
632 PNG_sRGB;
633#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600634 png_byte buf[1];
635
636 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600637 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600638 png_warning(png_ptr,
639 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600640 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600641 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600642}
643#endif
644
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600645#if defined(PNG_WRITE_iCCP_SUPPORTED)
646/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500647void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600648png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
649 png_charp profile, int profile_len)
650{
651#ifdef PNG_USE_LOCAL_ARRAYS
652 PNG_iCCP;
653#endif
654 png_size_t name_len;
655 png_charp new_name;
656 compression_state comp;
657
658 png_debug(1, "in png_write_iCCP\n");
659 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
660 &new_name)) == 0)
661 {
662 png_warning(png_ptr, "Empty keyword in iCCP chunk");
663 return;
664 }
665
Glenn Randers-Pehrsonf5ed0e12000-11-18 18:19:14 -0600666 if (compression_type)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600667 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600668
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500669 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600670 profile_len = 0;
671
672 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500673 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrsonf5ed0e12000-11-18 18:19:14 -0600674 PNG_TEXT_COMPRESSION_zTXt, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600675
676 /* make sure we include the NULL after the name and the compression type */
677 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
678 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600679 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600680 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
681
682 if (profile_len)
683 png_write_compressed_data_out(png_ptr, &comp);
684
685 png_write_chunk_end(png_ptr);
686 png_free(png_ptr, new_name);
687}
688#endif
689
690#if defined(PNG_WRITE_sPLT_SUPPORTED)
691/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500692void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600693png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600694{
695#ifdef PNG_USE_LOCAL_ARRAYS
696 PNG_sPLT;
697#endif
698 png_size_t name_len;
699 png_charp new_name;
700 png_byte entrybuf[10];
701 int entry_size = (spalette->depth == 8 ? 6 : 10);
702 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600703 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500704#ifdef PNG_NO_POINTER_INDEXING
705 int i;
706#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600707
708 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600709 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
710 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600711 {
712 png_warning(png_ptr, "Empty keyword in sPLT chunk");
713 return;
714 }
715
716 /* make sure we include the NULL after the name */
717 png_write_chunk_start(png_ptr, (png_bytep) png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600718 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600719 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600720 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600721
722 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500723#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600724 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
725 {
726 if (spalette->depth == 8)
727 {
728 entrybuf[0] = (png_byte)ep->red;
729 entrybuf[1] = (png_byte)ep->green;
730 entrybuf[2] = (png_byte)ep->blue;
731 entrybuf[3] = (png_byte)ep->alpha;
732 png_save_uint_16(entrybuf + 4, ep->frequency);
733 }
734 else
735 {
736 png_save_uint_16(entrybuf + 0, ep->red);
737 png_save_uint_16(entrybuf + 2, ep->green);
738 png_save_uint_16(entrybuf + 4, ep->blue);
739 png_save_uint_16(entrybuf + 6, ep->alpha);
740 png_save_uint_16(entrybuf + 8, ep->frequency);
741 }
742 png_write_chunk_data(png_ptr, entrybuf, entry_size);
743 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500744#else
745 ep=spalette->entries;
746 for (i=0; i>spalette->nentries; i++)
747 {
748 if (spalette->depth == 8)
749 {
750 entrybuf[0] = (png_byte)ep[i].red;
751 entrybuf[1] = (png_byte)ep[i].green;
752 entrybuf[2] = (png_byte)ep[i].blue;
753 entrybuf[3] = (png_byte)ep[i].alpha;
754 png_save_uint_16(entrybuf + 4, ep[i].frequency);
755 }
756 else
757 {
758 png_save_uint_16(entrybuf + 0, ep[i].red);
759 png_save_uint_16(entrybuf + 2, ep[i].green);
760 png_save_uint_16(entrybuf + 4, ep[i].blue);
761 png_save_uint_16(entrybuf + 6, ep[i].alpha);
762 png_save_uint_16(entrybuf + 8, ep[i].frequency);
763 }
764 png_write_chunk_data(png_ptr, entrybuf, entry_size);
765 }
766#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600767
768 png_write_chunk_end(png_ptr);
769 png_free(png_ptr, new_name);
770}
771#endif
772
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500773#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500774/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600776png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500777{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600778#ifdef PNG_USE_LOCAL_ARRAYS
779 PNG_sBIT;
780#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500781 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500782 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500783
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600785 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500786 if (color_type & PNG_COLOR_MASK_COLOR)
787 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600788 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500789
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500790 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
791 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600792 if (sbit->red == 0 || sbit->red > maxbits ||
793 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500794 sbit->blue == 0 || sbit->blue > maxbits)
795 {
796 png_warning(png_ptr, "Invalid sBIT depth specified");
797 return;
798 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 buf[0] = sbit->red;
800 buf[1] = sbit->green;
801 buf[2] = sbit->blue;
802 size = 3;
803 }
804 else
805 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500806 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
807 {
808 png_warning(png_ptr, "Invalid sBIT depth specified");
809 return;
810 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500811 buf[0] = sbit->gray;
812 size = 1;
813 }
814
815 if (color_type & PNG_COLOR_MASK_ALPHA)
816 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500817 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
818 {
819 png_warning(png_ptr, "Invalid sBIT depth specified");
820 return;
821 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500822 buf[size++] = sbit->alpha;
823 }
824
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600825 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500826}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500827#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500828
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500829#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500830/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600831#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500832void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500833png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600834 double red_x, double red_y, double green_x, double green_y,
835 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500836{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600837#ifdef PNG_USE_LOCAL_ARRAYS
838 PNG_cHRM;
839#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500840 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600841 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500842
Andreas Dilger47a0c421997-05-16 02:46:07 -0500843 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600844 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500845 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
846 white_x + white_y > 1.0)
847 {
848 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500849#if !defined(PNG_NO_CONSOLE_IO)
850 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600851#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500852 return;
853 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600854 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500855 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600856 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500857 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500858
859 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
860 red_x + red_y > 1.0)
861 {
862 png_warning(png_ptr, "Invalid cHRM red point specified");
863 return;
864 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600865 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500866 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600867 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500868 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500869
870 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
871 green_x + green_y > 1.0)
872 {
873 png_warning(png_ptr, "Invalid cHRM green point specified");
874 return;
875 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600876 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500877 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600878 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500879 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500880
881 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
882 blue_x + blue_y > 1.0)
883 {
884 png_warning(png_ptr, "Invalid cHRM blue point specified");
885 return;
886 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600887 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500888 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600889 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500890 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500891
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600892 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500893}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500894#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600895#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500896void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600897png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
898 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
899 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
900 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600901{
902#ifdef PNG_USE_LOCAL_ARRAYS
903 PNG_cHRM;
904#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600905 png_byte buf[32];
906
907 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600908 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600909 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
910 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600911 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500912#if !defined(PNG_NO_CONSOLE_IO)
913 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600914#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600915 return;
916 }
917 png_save_uint_32(buf, white_x);
918 png_save_uint_32(buf + 4, white_y);
919
920 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
921 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600922 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600923 return;
924 }
925 png_save_uint_32(buf + 8, red_x);
926 png_save_uint_32(buf + 12, red_y);
927
928 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
929 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600930 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600931 return;
932 }
933 png_save_uint_32(buf + 16, green_x);
934 png_save_uint_32(buf + 20, green_y);
935
936 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
937 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600938 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600939 return;
940 }
941 png_save_uint_32(buf + 24, blue_x);
942 png_save_uint_32(buf + 28, blue_y);
943
944 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
945}
946#endif
947#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500948
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500949#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500950/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500951void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600952png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500953 int num_trans, int color_type)
954{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600955#ifdef PNG_USE_LOCAL_ARRAYS
956 PNG_tRNS;
957#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500958 png_byte buf[6];
959
Andreas Dilger47a0c421997-05-16 02:46:07 -0500960 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500961 if (color_type == PNG_COLOR_TYPE_PALETTE)
962 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600963 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500964 {
965 png_warning(png_ptr,"Invalid number of transparent colors specified");
966 return;
967 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500968 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600969 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500970 }
971 else if (color_type == PNG_COLOR_TYPE_GRAY)
972 {
973 /* one 16 bit value */
974 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600975 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500976 }
977 else if (color_type == PNG_COLOR_TYPE_RGB)
978 {
979 /* three 16 bit values */
980 png_save_uint_16(buf, tran->red);
981 png_save_uint_16(buf + 2, tran->green);
982 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600983 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500984 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500985 else
986 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600987 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500988 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500989}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500990#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500991
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500992#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500993/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500994void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600995png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500996{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600997#ifdef PNG_USE_LOCAL_ARRAYS
998 PNG_bKGD;
999#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001000 png_byte buf[6];
1001
Andreas Dilger47a0c421997-05-16 02:46:07 -05001002 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001003 if (color_type == PNG_COLOR_TYPE_PALETTE)
1004 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001005 if (
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001006#if defined(PNG_MNG_FEATURES_SUPPORTED)
1007 (png_ptr->num_palette ||
1008 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001009#endif
1010 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001011 {
1012 png_warning(png_ptr, "Invalid background palette index");
1013 return;
1014 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001015 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001016 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001017 }
1018 else if (color_type & PNG_COLOR_MASK_COLOR)
1019 {
1020 png_save_uint_16(buf, back->red);
1021 png_save_uint_16(buf + 2, back->green);
1022 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001023 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001024 }
1025 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001026 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001027 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001028 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001029 }
1030}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001031#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001032
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001033#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001034/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001035void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001036png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001037{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001038#ifdef PNG_USE_LOCAL_ARRAYS
1039 PNG_hIST;
1040#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001041 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001042 png_byte buf[3];
1043
Andreas Dilger47a0c421997-05-16 02:46:07 -05001044 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001045 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001046 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001047 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1048 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001049 png_warning(png_ptr, "Invalid number of histogram entries specified");
1050 return;
1051 }
1052
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001053 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001054 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001055 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001056 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001057 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001058 }
1059 png_write_chunk_end(png_ptr);
1060}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001061#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001062
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001063#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1064 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001065/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1066 * and if invalid, correct the keyword rather than discarding the entire
1067 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1068 * length, forbids leading or trailing whitespace, multiple internal spaces,
1069 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001070 *
1071 * The new_key is allocated to hold the corrected keyword and must be freed
1072 * by the calling routine. This avoids problems with trying to write to
1073 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001074 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001075png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001076png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001077{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001078 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001079 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001080 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001081 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001082
Andreas Dilger47a0c421997-05-16 02:46:07 -05001083 png_debug(1, "in png_check_keyword\n");
1084 *new_key = NULL;
1085
1086 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001087 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001088 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001089 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001090 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001091
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1093
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001094 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001095
1096 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001098 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001099 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001101#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001103
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001104 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001105 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001106#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001107 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001108#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109 *dp = ' ';
1110 }
1111 else
1112 {
1113 *dp = *kp;
1114 }
1115 }
1116 *dp = '\0';
1117
1118 /* Remove any trailing white space. */
1119 kp = *new_key + key_len - 1;
1120 if (*kp == ' ')
1121 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001122 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123
1124 while (*kp == ' ')
1125 {
1126 *(kp--) = '\0';
1127 key_len--;
1128 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001129 }
1130
1131 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 kp = *new_key;
1133 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001134 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001135 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001136
1137 while (*kp == ' ')
1138 {
1139 kp++;
1140 key_len--;
1141 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001142 }
1143
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001145
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146 /* Remove multiple internal spaces. */
1147 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001148 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001150 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001151 *(dp++) = *kp;
1152 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001153 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001155 {
1156 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001157 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001158 }
1159 else
1160 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 *(dp++) = *kp;
1162 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001163 }
1164 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001166 if(kwarn)
1167 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001168
1169 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001170 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001171 png_free(png_ptr, *new_key);
1172 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001173 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174 }
1175
1176 if (key_len > 79)
1177 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001178 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001179 new_key[79] = '\0';
1180 key_len = 79;
1181 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001182
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001183 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001184}
1185#endif
1186
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001187#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001188/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001189void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001190png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001191 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001192{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001193#ifdef PNG_USE_LOCAL_ARRAYS
1194 PNG_tEXt;
1195#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001197 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001198
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199 png_debug(1, "in png_write_tEXt\n");
1200 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1201 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001202 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001203 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001205
Andreas Dilger47a0c421997-05-16 02:46:07 -05001206 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001208 else
1209 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210
1211 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001212 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 -05001213 /*
1214 * We leave it to the application to meet PNG-1.0 requirements on the
1215 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1216 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001217 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001218 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001219 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001220 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001222
Guy Schalnat0d580581995-07-20 02:43:20 -05001223 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001225}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001226#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001227
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001228#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001230void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001231png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001233{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001234#ifdef PNG_USE_LOCAL_ARRAYS
1235 PNG_zTXt;
1236#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001238 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001239 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001240 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001241
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001243
Andreas Dilger47a0c421997-05-16 02:46:07 -05001244 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001245 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001246 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001248 }
1249
Andreas Dilger47a0c421997-05-16 02:46:07 -05001250 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1251 {
1252 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1253 png_free(png_ptr, new_key);
1254 return;
1255 }
1256
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001257 text_len = png_strlen(text);
1258
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 png_free(png_ptr, new_key);
1260
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001261 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001262 text_len = png_text_compress(png_ptr, text, text_len, compression,
1263 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001264
1265 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001266 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1267 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001268 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001269 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001270 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001271 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001272 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001273 /* write the compressed data */
1274 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001275
Guy Schalnat0d580581995-07-20 02:43:20 -05001276 /* close the chunk */
1277 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001278}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001279#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001280
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001281#if defined(PNG_WRITE_iTXt_SUPPORTED)
1282/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001283void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001284png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001285 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001286{
1287#ifdef PNG_USE_LOCAL_ARRAYS
1288 PNG_iTXt;
1289#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001290 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001291 png_charp new_lang, new_key;
1292 png_byte cbuf[2];
1293 compression_state comp;
1294
1295 png_debug(1, "in png_write_iTXt\n");
1296
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001297 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1298 {
1299 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1300 return;
1301 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001302 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1303 &new_lang))==0)
1304 {
1305 png_warning(png_ptr, "Empty language field in iTXt chunk");
1306 return;
1307 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001308 lang_key_len = png_strlen(lang_key);
1309 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001310
1311 if (text == NULL || *text == '\0')
1312 text_len = 0;
1313
1314 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001315 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1316 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001317
1318 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001319 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001320
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001321 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1322 (png_uint_32)(
1323 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1324 + key_len
1325 + lang_len
1326 + lang_key_len
1327 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001328
1329 /*
1330 * We leave it to the application to meet PNG-1.0 requirements on the
1331 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1332 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1333 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1334 */
1335 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001336
1337 /* set the compression flag */
1338 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1339 compression == PNG_TEXT_COMPRESSION_NONE)
1340 cbuf[0] = 0;
1341 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1342 cbuf[0] = 1;
1343 /* set the compression method */
1344 cbuf[1] = 0;
1345 png_write_chunk_data(png_ptr, cbuf, 2);
1346
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001347 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001348 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001349 png_write_chunk_data(png_ptr, '\0', 1);
1350
1351 png_write_compressed_data_out(png_ptr, &comp);
1352
1353 png_write_chunk_end(png_ptr);
1354 png_free(png_ptr, new_key);
1355 png_free(png_ptr, new_lang);
1356}
1357#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001358
1359#if defined(PNG_WRITE_oFFs_SUPPORTED)
1360/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001361void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001362png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1363 png_uint_32 y_offset,
1364 int unit_type)
1365{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001366#ifdef PNG_USE_LOCAL_ARRAYS
1367 PNG_oFFs;
1368#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001369 png_byte buf[9];
1370
1371 png_debug(1, "in png_write_oFFs\n");
1372 if (unit_type >= PNG_OFFSET_LAST)
1373 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1374
1375 png_save_uint_32(buf, x_offset);
1376 png_save_uint_32(buf + 4, y_offset);
1377 buf[8] = (png_byte)unit_type;
1378
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001379 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001380}
1381#endif
1382
1383#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001384/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001385void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1387 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1388{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001389#ifdef PNG_USE_LOCAL_ARRAYS
1390 PNG_pCAL;
1391#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001392 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001393 png_uint_32p params_len;
1394 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001395 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001396 int i;
1397
1398 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1399 if (type >= PNG_EQUATION_LAST)
1400 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1401
1402 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1403 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1404 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1405 png_debug1(3, "pCAL units length = %d\n", units_len);
1406 total_len = purpose_len + units_len + 10;
1407
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001408 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1409 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001410
1411 /* Find the length of each parameter, making sure we don't count the
1412 null terminator for the last parameter. */
1413 for (i = 0; i < nparams; i++)
1414 {
1415 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001416 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001417 total_len += (png_size_t)params_len[i];
1418 }
1419
1420 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001421 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001422 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001423 png_save_int_32(buf, X0);
1424 png_save_int_32(buf + 4, X1);
1425 buf[8] = (png_byte)type;
1426 buf[9] = (png_byte)nparams;
1427 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1428 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1429
1430 png_free(png_ptr, new_purpose);
1431
1432 for (i = 0; i < nparams; i++)
1433 {
1434 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1435 (png_size_t)params_len[i]);
1436 }
1437
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001438 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001439 png_write_chunk_end(png_ptr);
1440}
1441#endif
1442
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001443#if defined(PNG_WRITE_sCAL_SUPPORTED)
1444/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001445#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001446void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001447png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001448{
1449#ifdef PNG_USE_LOCAL_ARRAYS
1450 PNG_sCAL;
1451#endif
1452 png_size_t total_len;
1453 char wbuf[32], hbuf[32];
1454
1455 png_debug(1, "in png_write_sCAL\n");
1456
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001457#if defined(_WIN32_WCE)
1458/* sprintf() function is not supported on WindowsCE */
1459 {
1460 wchar_t wc_buf[32];
1461 swprintf(wc_buf, TEXT("%12.12e"), width);
1462 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1463 swprintf(wc_buf, TEXT("%12.12e"), height);
1464 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1465 }
1466#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001467 sprintf(wbuf, "%12.12e", width);
1468 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001469#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001470 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001471
1472 png_debug1(3, "sCAL total length = %d\n", total_len);
1473 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001474 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001475 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1476 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1477
1478 png_write_chunk_end(png_ptr);
1479}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001480#else
1481#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001482void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001483png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001484 png_charp height)
1485{
1486#ifdef PNG_USE_LOCAL_ARRAYS
1487 PNG_sCAL;
1488#endif
1489 png_size_t total_len;
1490 char wbuf[32], hbuf[32];
1491
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001492 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001493
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001494 strcpy(wbuf,(const char *)width);
1495 strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001496 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001497
1498 png_debug1(3, "sCAL total length = %d\n", total_len);
1499 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001500 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001501 png_write_chunk_data(png_ptr, (png_bytep)wbuf, strlen(wbuf)+1);
1502 png_write_chunk_data(png_ptr, (png_bytep)hbuf, strlen(hbuf));
1503
1504 png_write_chunk_end(png_ptr);
1505}
1506#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001507#endif
1508#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001509
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001510#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001511/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001512void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001513png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001514 png_uint_32 y_pixels_per_unit,
1515 int unit_type)
1516{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001517#ifdef PNG_USE_LOCAL_ARRAYS
1518 PNG_pHYs;
1519#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001520 png_byte buf[9];
1521
Andreas Dilger47a0c421997-05-16 02:46:07 -05001522 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001523 if (unit_type >= PNG_RESOLUTION_LAST)
1524 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1525
Guy Schalnat0d580581995-07-20 02:43:20 -05001526 png_save_uint_32(buf, x_pixels_per_unit);
1527 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001528 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001529
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001530 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001531}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001532#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001533
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001534#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001535/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1536 * or png_convert_from_time_t(), or fill in the structure yourself.
1537 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001538void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001539png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001540{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001541#ifdef PNG_USE_LOCAL_ARRAYS
1542 PNG_tIME;
1543#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001544 png_byte buf[7];
1545
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001547 if (mod_time->month > 12 || mod_time->month < 1 ||
1548 mod_time->day > 31 || mod_time->day < 1 ||
1549 mod_time->hour > 23 || mod_time->second > 60)
1550 {
1551 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1552 return;
1553 }
1554
Guy Schalnat0d580581995-07-20 02:43:20 -05001555 png_save_uint_16(buf, mod_time->year);
1556 buf[2] = mod_time->month;
1557 buf[3] = mod_time->day;
1558 buf[4] = mod_time->hour;
1559 buf[5] = mod_time->minute;
1560 buf[6] = mod_time->second;
1561
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001562 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001563}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001564#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001565
1566/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001567void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001568png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001569{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001570#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001571 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001572
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001573 /* start of interlace block */
1574 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001575
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001576 /* offset to next interlace block */
1577 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001578
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001579 /* start of interlace block in the y direction */
1580 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001581
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001582 /* offset to next interlace block in the y direction */
1583 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001584#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001585
Andreas Dilger47a0c421997-05-16 02:46:07 -05001586 png_size_t buf_size;
1587
1588 png_debug(1, "in png_write_start_row\n");
1589 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1590 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1591
Guy Schalnat0d580581995-07-20 02:43:20 -05001592 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001593 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001594 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001595
1596 /* set up filtering buffer, if using this filter */
1597 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001598 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001599 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001600 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001601 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001602 }
1603
Andreas Dilger47a0c421997-05-16 02:46:07 -05001604 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001605 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1606 {
1607 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001608 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001609 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001610
1611 if (png_ptr->do_filter & PNG_FILTER_UP)
1612 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001613 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001614 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001615 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001616 }
1617
1618 if (png_ptr->do_filter & PNG_FILTER_AVG)
1619 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001620 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1621 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001622 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001623 }
1624
1625 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1626 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001627 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001628 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001629 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001630 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001631 }
1632
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001633#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001634 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001635 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001636 {
1637 if (!(png_ptr->transformations & PNG_INTERLACE))
1638 {
1639 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1640 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001641 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1642 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001643 }
1644 else
1645 {
1646 png_ptr->num_rows = png_ptr->height;
1647 png_ptr->usr_width = png_ptr->width;
1648 }
1649 }
1650 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001651#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001652 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001653 png_ptr->num_rows = png_ptr->height;
1654 png_ptr->usr_width = png_ptr->width;
1655 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001656 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1657 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001658}
1659
Andreas Dilger47a0c421997-05-16 02:46:07 -05001660/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001661void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001662png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001663{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001664#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001665 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001666
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001667 /* start of interlace block */
1668 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001669
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001670 /* offset to next interlace block */
1671 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001672
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001673 /* start of interlace block in the y direction */
1674 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001675
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001676 /* offset to next interlace block in the y direction */
1677 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001678#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001679
Guy Schalnat0d580581995-07-20 02:43:20 -05001680 int ret;
1681
Andreas Dilger47a0c421997-05-16 02:46:07 -05001682 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001683 /* next row */
1684 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001685
Guy Schalnat0d580581995-07-20 02:43:20 -05001686 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001687 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001688 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001689
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001690#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001691 /* if interlaced, go to next pass */
1692 if (png_ptr->interlaced)
1693 {
1694 png_ptr->row_number = 0;
1695 if (png_ptr->transformations & PNG_INTERLACE)
1696 {
1697 png_ptr->pass++;
1698 }
1699 else
1700 {
1701 /* loop until we find a non-zero width or height pass */
1702 do
1703 {
1704 png_ptr->pass++;
1705 if (png_ptr->pass >= 7)
1706 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001707 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001708 png_pass_inc[png_ptr->pass] - 1 -
1709 png_pass_start[png_ptr->pass]) /
1710 png_pass_inc[png_ptr->pass];
1711 png_ptr->num_rows = (png_ptr->height +
1712 png_pass_yinc[png_ptr->pass] - 1 -
1713 png_pass_ystart[png_ptr->pass]) /
1714 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001715 if (png_ptr->transformations & PNG_INTERLACE)
1716 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001717 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1718
1719 }
1720
Guy Schalnate5a37791996-06-05 15:50:50 -05001721 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001722 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001723 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001724 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001725 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001726 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001727 (png_uint_32)png_ptr->usr_bit_depth *
1728 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001729 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001730 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001731 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001732#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001733
1734 /* if we get here, we've just written the last row, so we need
1735 to flush the compressor */
1736 do
1737 {
1738 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001739 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001740 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001741 if (ret == Z_OK)
1742 {
1743 /* check to see if we need more room */
1744 if (!(png_ptr->zstream.avail_out))
1745 {
1746 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1747 png_ptr->zstream.next_out = png_ptr->zbuf;
1748 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1749 }
1750 }
1751 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001752 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001753 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001754 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001755 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001756 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001757 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001758 } while (ret != Z_STREAM_END);
1759
1760 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001761 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001762 {
1763 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001764 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001765 }
1766
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001767 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001768}
1769
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001770#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001771/* Pick out the correct pixels for the interlace pass.
1772 * The basic idea here is to go through the row with a source
1773 * pointer and a destination pointer (sp and dp), and copy the
1774 * correct pixels for the pass. As the row gets compacted,
1775 * sp will always be >= dp, so we should never overwrite anything.
1776 * See the default: case for the easiest code to understand.
1777 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001778void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001779png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001780{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001781#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001782 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001783
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001784 /* start of interlace block */
1785 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001786
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001787 /* offset to next interlace block */
1788 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001789#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001790
Andreas Dilger47a0c421997-05-16 02:46:07 -05001791 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001792 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001793#if defined(PNG_USELESS_TESTS_SUPPORTED)
1794 if (row != NULL && row_info != NULL && pass < 6)
1795#else
1796 if (pass < 6)
1797#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001798 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001799 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001800 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001801 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001802 case 1:
1803 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001804 png_bytep sp;
1805 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001806 int shift;
1807 int d;
1808 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001809 png_uint_32 i;
1810 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001811
1812 dp = row;
1813 d = 0;
1814 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001815 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 i += png_pass_inc[pass])
1817 {
1818 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001819 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001820 d |= (value << shift);
1821
1822 if (shift == 0)
1823 {
1824 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001825 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001826 d = 0;
1827 }
1828 else
1829 shift--;
1830
1831 }
1832 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001833 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001834 break;
1835 }
1836 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001837 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001838 png_bytep sp;
1839 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001840 int shift;
1841 int d;
1842 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001843 png_uint_32 i;
1844 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001845
1846 dp = row;
1847 shift = 6;
1848 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001849 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001850 i += png_pass_inc[pass])
1851 {
1852 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001853 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001854 d |= (value << shift);
1855
1856 if (shift == 0)
1857 {
1858 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001859 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001860 d = 0;
1861 }
1862 else
1863 shift -= 2;
1864 }
1865 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001866 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001867 break;
1868 }
1869 case 4:
1870 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001871 png_bytep sp;
1872 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001873 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001874 int d;
1875 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001876 png_uint_32 i;
1877 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001878
1879 dp = row;
1880 shift = 4;
1881 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001882 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001883 i += png_pass_inc[pass])
1884 {
1885 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001886 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001887 d |= (value << shift);
1888
1889 if (shift == 0)
1890 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001891 shift = 4;
1892 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001893 d = 0;
1894 }
1895 else
1896 shift -= 4;
1897 }
1898 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001899 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001900 break;
1901 }
1902 default:
1903 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001904 png_bytep sp;
1905 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001906 png_uint_32 i;
1907 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001908 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001909
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001910 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001911 dp = row;
1912 /* find out how many bytes each pixel takes up */
1913 pixel_bytes = (row_info->pixel_depth >> 3);
1914 /* loop through the row, only looking at the pixels that
1915 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001916 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001917 i += png_pass_inc[pass])
1918 {
1919 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001920 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001921 /* move the pixel */
1922 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001923 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001924 /* next pixel */
1925 dp += pixel_bytes;
1926 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001927 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 }
1929 }
1930 /* set new row width */
1931 row_info->width = (row_info->width +
1932 png_pass_inc[pass] - 1 -
1933 png_pass_start[pass]) /
1934 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001935 row_info->rowbytes = ((row_info->width *
1936 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001937 }
1938}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001939#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001940
Andreas Dilger47a0c421997-05-16 02:46:07 -05001941/* This filters the row, chooses which filter to use, if it has not already
1942 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001943 * chosen filter.
1944 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001945#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001946#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001947#define PNG_LOMASK ((png_uint_32)0xffffL)
1948#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001949void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05001950png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001951{
Guy Schalnate5a37791996-06-05 15:50:50 -05001952 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001953 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001954 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001955 png_uint_32 row_bytes = row_info->rowbytes;
1956#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1957 int num_p_filters = (int)png_ptr->num_prev_filters;
1958#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001959
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001961 /* find out how many bytes offset each pixel is */
1962 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001963
1964 prev_row = png_ptr->prev_row;
1965 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001966 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001967
Andreas Dilger47a0c421997-05-16 02:46:07 -05001968 /* The prediction method we use is to find which method provides the
1969 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001970 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001971 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001972 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001973 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001974 * predictive" method (not implemented yet), which does test compressions
1975 * of lines using different filter methods, and then chooses the
1976 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001977 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001978 *
1979 * GRR 980525: consider also
1980 * (1) minimum sum of absolute differences from running average (i.e.,
1981 * keep running sum of non-absolute differences & count of bytes)
1982 * [track dispersion, too? restart average if dispersion too large?]
1983 * (1b) minimum sum of absolute differences from sliding average, probably
1984 * with window size <= deflate window (usually 32K)
1985 * (2) minimum sum of squared differences from zero or running average
1986 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001987 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001988
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001989
Guy Schalnate5a37791996-06-05 15:50:50 -05001990 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001991 * that has been chosen, as it doesn't actually do anything to the data.
1992 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001993 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001994 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001995 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001996 png_bytep rp;
1997 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001998 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001999 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002000
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002001 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002002 {
2003 v = *rp;
2004 sum += (v < 128) ? v : 256 - v;
2005 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002006
2007#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2008 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2009 {
2010 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002011 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002012 sumlo = sum & PNG_LOMASK;
2013 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2014
2015 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002016 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002017 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002018 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002019 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002020 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002021 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002022 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002023 PNG_WEIGHT_SHIFT;
2024 }
2025 }
2026
2027 /* Factor in the cost of this filter (this is here for completeness,
2028 * but it makes no sense to have a "cost" for the NONE filter, as
2029 * it has the minimum possible computational cost - none).
2030 */
2031 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2032 PNG_COST_SHIFT;
2033 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2034 PNG_COST_SHIFT;
2035
2036 if (sumhi > PNG_HIMASK)
2037 sum = PNG_MAXSUM;
2038 else
2039 sum = (sumhi << PNG_HISHIFT) + sumlo;
2040 }
2041#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002042 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002043 }
2044
Guy Schalnate5a37791996-06-05 15:50:50 -05002045 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002046 if (filter_to_do == PNG_FILTER_SUB)
2047 /* it's the only filter so no testing is needed */
2048 {
2049 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002050 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002051 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2052 i++, rp++, dp++)
2053 {
2054 *dp = *rp;
2055 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002056 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002057 i++, rp++, lp++, dp++)
2058 {
2059 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2060 }
2061 best_row = png_ptr->sub_row;
2062 }
2063
2064 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002065 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002066 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002067 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002068 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002069 int v;
2070
2071#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002072 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002073 * would reduce the sum of this filter, so that we can do the
2074 * early exit comparison without scaling the sum each time.
2075 */
2076 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2077 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002078 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002079 png_uint_32 lmhi, lmlo;
2080 lmlo = lmins & PNG_LOMASK;
2081 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2082
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002083 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002084 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002085 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002086 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002087 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002088 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002089 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002090 PNG_WEIGHT_SHIFT;
2091 }
2092 }
2093
2094 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2095 PNG_COST_SHIFT;
2096 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2097 PNG_COST_SHIFT;
2098
2099 if (lmhi > PNG_HIMASK)
2100 lmins = PNG_MAXSUM;
2101 else
2102 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2103 }
2104#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002105
Guy Schalnate5a37791996-06-05 15:50:50 -05002106 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2107 i++, rp++, dp++)
2108 {
2109 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002110
Guy Schalnate5a37791996-06-05 15:50:50 -05002111 sum += (v < 128) ? v : 256 - v;
2112 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002113 for (lp = row_buf + 1; i < row_info->rowbytes;
2114 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002115 {
2116 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002117
Guy Schalnate5a37791996-06-05 15:50:50 -05002118 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002119
2120 if (sum > lmins) /* We are already worse, don't continue. */
2121 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002122 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002123
2124#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2125 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2126 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002127 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002128 png_uint_32 sumhi, sumlo;
2129 sumlo = sum & PNG_LOMASK;
2130 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2131
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002132 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002133 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002134 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002136 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002138 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139 PNG_WEIGHT_SHIFT;
2140 }
2141 }
2142
2143 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2144 PNG_COST_SHIFT;
2145 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2146 PNG_COST_SHIFT;
2147
2148 if (sumhi > PNG_HIMASK)
2149 sum = PNG_MAXSUM;
2150 else
2151 sum = (sumhi << PNG_HISHIFT) + sumlo;
2152 }
2153#endif
2154
Guy Schalnate5a37791996-06-05 15:50:50 -05002155 if (sum < mins)
2156 {
2157 mins = sum;
2158 best_row = png_ptr->sub_row;
2159 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002160 }
2161
Guy Schalnate5a37791996-06-05 15:50:50 -05002162 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002163 if (filter_to_do == PNG_FILTER_UP)
2164 {
2165 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002166 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002167
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002168 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002169 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002170 i++, rp++, pp++, dp++)
2171 {
2172 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2173 }
2174 best_row = png_ptr->up_row;
2175 }
2176
2177 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002178 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002179 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002180 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002181 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 int v;
2183
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002184
Andreas Dilger47a0c421997-05-16 02:46:07 -05002185#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2186 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2187 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002188 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002189 png_uint_32 lmhi, lmlo;
2190 lmlo = lmins & PNG_LOMASK;
2191 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2192
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002193 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002197 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002199 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002200 PNG_WEIGHT_SHIFT;
2201 }
2202 }
2203
2204 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2205 PNG_COST_SHIFT;
2206 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2207 PNG_COST_SHIFT;
2208
2209 if (lmhi > PNG_HIMASK)
2210 lmins = PNG_MAXSUM;
2211 else
2212 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2213 }
2214#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002215
2216 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002217 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002218 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002219 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002220
2221 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002222
2223 if (sum > lmins) /* We are already worse, don't continue. */
2224 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002225 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002226
2227#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2228 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2229 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002230 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002231 png_uint_32 sumhi, sumlo;
2232 sumlo = sum & PNG_LOMASK;
2233 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2234
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002235 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002236 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002237 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002238 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002239 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002240 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002241 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002242 PNG_WEIGHT_SHIFT;
2243 }
2244 }
2245
2246 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2247 PNG_COST_SHIFT;
2248 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2249 PNG_COST_SHIFT;
2250
2251 if (sumhi > PNG_HIMASK)
2252 sum = PNG_MAXSUM;
2253 else
2254 sum = (sumhi << PNG_HISHIFT) + sumlo;
2255 }
2256#endif
2257
Guy Schalnate5a37791996-06-05 15:50:50 -05002258 if (sum < mins)
2259 {
2260 mins = sum;
2261 best_row = png_ptr->up_row;
2262 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002263 }
2264
Guy Schalnate5a37791996-06-05 15:50:50 -05002265 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002266 if (filter_to_do == PNG_FILTER_AVG)
2267 {
2268 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002269 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002270 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002271 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002272 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002273 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002274 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002275 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002276 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002277 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2278 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002279 }
2280 best_row = png_ptr->avg_row;
2281 }
2282
2283 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002284 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002285 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002286 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002287 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002288 int v;
2289
2290#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2291 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2292 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002293 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002294 png_uint_32 lmhi, lmlo;
2295 lmlo = lmins & PNG_LOMASK;
2296 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2297
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002298 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002299 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002300 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002301 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002302 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002303 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002304 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002305 PNG_WEIGHT_SHIFT;
2306 }
2307 }
2308
2309 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2310 PNG_COST_SHIFT;
2311 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2312 PNG_COST_SHIFT;
2313
2314 if (lmhi > PNG_HIMASK)
2315 lmins = PNG_MAXSUM;
2316 else
2317 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2318 }
2319#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002320
2321 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002322 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002323 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002324 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002325
2326 sum += (v < 128) ? v : 256 - v;
2327 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002328 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002329 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002330 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002331 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002332
2333 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002334
2335 if (sum > lmins) /* We are already worse, don't continue. */
2336 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002337 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002338
2339#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2340 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2341 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002342 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002343 png_uint_32 sumhi, sumlo;
2344 sumlo = sum & PNG_LOMASK;
2345 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2346
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002349 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002350 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002351 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354 PNG_WEIGHT_SHIFT;
2355 }
2356 }
2357
2358 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2359 PNG_COST_SHIFT;
2360 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2361 PNG_COST_SHIFT;
2362
2363 if (sumhi > PNG_HIMASK)
2364 sum = PNG_MAXSUM;
2365 else
2366 sum = (sumhi << PNG_HISHIFT) + sumlo;
2367 }
2368#endif
2369
Guy Schalnate5a37791996-06-05 15:50:50 -05002370 if (sum < mins)
2371 {
2372 mins = sum;
2373 best_row = png_ptr->avg_row;
2374 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002375 }
2376
Andreas Dilger47a0c421997-05-16 02:46:07 -05002377 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002378 if (filter_to_do == PNG_FILTER_PAETH)
2379 {
2380 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002381 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002382 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002383 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002384 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002385 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002386 }
2387
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002388 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002389 {
2390 int a, b, c, pa, pb, pc, p;
2391
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002392 b = *pp++;
2393 c = *cp++;
2394 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002395
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002396 p = b - c;
2397 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002398
2399#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002400 pa = abs(p);
2401 pb = abs(pc);
2402 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002403#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002404 pa = p < 0 ? -p : p;
2405 pb = pc < 0 ? -pc : pc;
2406 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002407#endif
2408
2409 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2410
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002411 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002412 }
2413 best_row = png_ptr->paeth_row;
2414 }
2415
2416 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002417 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002418 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002419 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002420 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002421 int v;
2422
2423#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2424 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2425 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002426 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002427 png_uint_32 lmhi, lmlo;
2428 lmlo = lmins & PNG_LOMASK;
2429 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2430
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002431 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002432 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002433 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002434 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002435 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002436 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002437 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002438 PNG_WEIGHT_SHIFT;
2439 }
2440 }
2441
2442 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2443 PNG_COST_SHIFT;
2444 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2445 PNG_COST_SHIFT;
2446
2447 if (lmhi > PNG_HIMASK)
2448 lmins = PNG_MAXSUM;
2449 else
2450 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2451 }
2452#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002453
2454 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002455 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002456 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002457 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002458
2459 sum += (v < 128) ? v : 256 - v;
2460 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002461
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002462 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002463 {
2464 int a, b, c, pa, pb, pc, p;
2465
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002466 b = *pp++;
2467 c = *cp++;
2468 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002469
2470#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002471 p = b - c;
2472 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002473#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002474 pa = abs(p);
2475 pb = abs(pc);
2476 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002477#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002478 pa = p < 0 ? -p : p;
2479 pb = pc < 0 ? -pc : pc;
2480 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002481#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002482 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2483#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002484 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002485 pa = abs(p - a);
2486 pb = abs(p - b);
2487 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002488 if (pa <= pb && pa <= pc)
2489 p = a;
2490 else if (pb <= pc)
2491 p = b;
2492 else
2493 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002494#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002495
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002496 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002497
2498 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002499
2500 if (sum > lmins) /* We are already worse, don't continue. */
2501 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002502 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002503
2504#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2505 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2506 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002507 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002508 png_uint_32 sumhi, sumlo;
2509 sumlo = sum & PNG_LOMASK;
2510 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2511
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002512 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002513 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002514 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002515 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002519 PNG_WEIGHT_SHIFT;
2520 }
2521 }
2522
2523 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2524 PNG_COST_SHIFT;
2525 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2526 PNG_COST_SHIFT;
2527
2528 if (sumhi > PNG_HIMASK)
2529 sum = PNG_MAXSUM;
2530 else
2531 sum = (sumhi << PNG_HISHIFT) + sumlo;
2532 }
2533#endif
2534
Guy Schalnate5a37791996-06-05 15:50:50 -05002535 if (sum < mins)
2536 {
2537 best_row = png_ptr->paeth_row;
2538 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002539 }
2540
Andreas Dilger47a0c421997-05-16 02:46:07 -05002541 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002542
Guy Schalnate5a37791996-06-05 15:50:50 -05002543 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002544
2545#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2546 /* Save the type of filter we picked this time for future calculations */
2547 if (png_ptr->num_prev_filters > 0)
2548 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002549 int j;
2550 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002551 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002552 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002553 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002554 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002555 }
2556#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002557}
2558
2559
Andreas Dilger47a0c421997-05-16 02:46:07 -05002560/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002561void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002562png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2563{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002564 png_debug(1, "in png_write_filtered_row\n");
2565 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002566 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002567
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002568 png_ptr->zstream.next_in = filtered_row;
2569 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002570 /* repeat until we have compressed all the data */
2571 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002572 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002573 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002574
Guy Schalnate5a37791996-06-05 15:50:50 -05002575 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002576 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002577 /* check for compression errors */
2578 if (ret != Z_OK)
2579 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002580 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002581 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002582 else
2583 png_error(png_ptr, "zlib error");
2584 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002585
Guy Schalnate5a37791996-06-05 15:50:50 -05002586 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002587 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002588 {
2589 /* write the IDAT and reset the zlib output buffer */
2590 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002591 png_ptr->zstream.next_out = png_ptr->zbuf;
2592 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002593 }
2594 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002595 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002596
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002597 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002598 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002599 {
2600 png_bytep tptr;
2601
2602 tptr = png_ptr->prev_row;
2603 png_ptr->prev_row = png_ptr->row_buf;
2604 png_ptr->row_buf = tptr;
2605 }
2606
Guy Schalnate5a37791996-06-05 15:50:50 -05002607 /* finish row - updates counters and flushes zlib if last row */
2608 png_write_finish_row(png_ptr);
2609
2610#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2611 png_ptr->flush_rows++;
2612
2613 if (png_ptr->flush_dist > 0 &&
2614 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002615 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002616 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002617 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002618#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002619}