blob: 6889f2706b192b08adb864dc1dc6f254b4a78227 [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-Pehrsone1eff582001-04-14 20:15:41 -05004 * libpng 1.0.11beta3 - April 15, 2001
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -06006 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -060013#ifdef PNG_WRITE_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
18 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050019void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Andreas Dilger47a0c421997-05-16 02:46:07 -050028#if defined(PNG_WRITE_pCAL_SUPPORTED)
29/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050033void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050034png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
41#endif
42
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050047void /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Andreas Dilger47a0c421997-05-16 02:46:07 -050054/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 * representing the chunk name. The array must be at least 4 bytes in
56 * length, and does not need to be null terminated. To be safe, pass the
57 * pre-defined chunk names here, and if you need a new one, define it
58 * where the others are defined. The length is the length of the data.
59 * All the data must be present. If that is not possible, use the
60 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
61 * functions instead.
62 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050063void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050066{
Andreas Dilger47a0c421997-05-16 02:46:07 -050067 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 png_write_chunk_data(png_ptr, data, length);
69 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050070}
71
Andreas Dilger47a0c421997-05-16 02:46:07 -050072/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073 * The total_length is the sum of the lengths of all the data you will be
74 * passing in png_write_chunk_data().
75 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050076void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
78 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050079{
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050081 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050082
Guy Schalnat0d580581995-07-20 02:43:20 -050083 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050084 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
86
Guy Schalnat0d580581995-07-20 02:43:20 -050087 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050089 /* reset the crc and run it over the chunk name */
90 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050092}
93
Andreas Dilger47a0c421997-05-16 02:46:07 -050094/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095 * Note that multiple calls to this function are allowed, and that the
96 * sum of the lengths from these calls *must* add up to the total_length
97 * given to png_write_chunk_start().
98 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050099void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 /* write the data, and run the CRC over it */
103 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 {
105 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600106 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 }
108}
109
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600112png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 png_byte buf[4];
115
Guy Schalnat0d580581995-07-20 02:43:20 -0500116 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118
119 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500120}
121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600122/* Simple function to write the signature. If we have already written
123 * the magic bytes of the signature, or more likely, the PNG stream is
124 * being embedded into another stream and doesn't need its own signature,
125 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600126 * bytes have already been written.
127 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500128void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600129png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600132 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600133 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 (png_size_t)8 - png_ptr->sig_bytes);
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600135 if(png_ptr->sig_bytes < 3)
136 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137}
138
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600139#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600140/*
141 * This pair of functions encapsulates the operation of (a) compressing a
142 * text string, and (b) issuing it later as a series of chunk data writes.
143 * The compression_state structure is shared context for these functions
144 * set up by the caller in order to make the whole mess thread-safe.
145 */
146
147typedef struct
148{
149 char *input; /* the uncompressed input data */
150 int input_len; /* its length */
151 int num_output_ptr; /* number of output pointers used */
152 int max_output_ptr; /* size of output_ptr */
153 png_charpp output_ptr; /* array of pointers to output */
154} compression_state;
155
156/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500157static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600158png_text_compress(png_structp png_ptr,
159 png_charp text, png_size_t text_len, int compression,
160 compression_state *comp)
161{
162 int ret;
163
164 comp->num_output_ptr = comp->max_output_ptr = 0;
165 comp->output_ptr = NULL;
166 comp->input = NULL;
167
168 /* we may just want to pass the text right through */
169 if (compression == PNG_TEXT_COMPRESSION_NONE)
170 {
171 comp->input = text;
172 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500173 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600174 }
175
176 if (compression >= PNG_TEXT_COMPRESSION_LAST)
177 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500178#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600179 char msg[50];
180 sprintf(msg, "Unknown compression type %d", compression);
181 png_warning(png_ptr, msg);
182#else
183 png_warning(png_ptr, "Unknown compression type");
184#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600185 }
186
187 /* We can't write the chunk until we find out how much data we have,
188 * which means we need to run the compressor first and save the
189 * output. This shouldn't be a problem, as the vast majority of
190 * comments should be reasonable, but we will set up an array of
191 * malloc'd pointers to be sure.
192 *
193 * If we knew the application was well behaved, we could simplify this
194 * greatly by assuming we can always malloc an output buffer large
195 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
196 * and malloc this directly. The only time this would be a bad idea is
197 * if we can't malloc more than 64K and we have 64K of random input
198 * data, or if the input string is incredibly large (although this
199 * wouldn't cause a failure, just a slowdown due to swapping).
200 */
201
202 /* set up the compression buffers */
203 png_ptr->zstream.avail_in = (uInt)text_len;
204 png_ptr->zstream.next_in = (Bytef *)text;
205 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
206 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
207
208 /* this is the same compression loop as in png_write_row() */
209 do
210 {
211 /* compress the data */
212 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
213 if (ret != Z_OK)
214 {
215 /* error */
216 if (png_ptr->zstream.msg != NULL)
217 png_error(png_ptr, png_ptr->zstream.msg);
218 else
219 png_error(png_ptr, "zlib error");
220 }
221 /* check to see if we need more room */
222 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
223 {
224 /* make sure the output array has room */
225 if (comp->num_output_ptr >= comp->max_output_ptr)
226 {
227 int old_max;
228
229 old_max = comp->max_output_ptr;
230 comp->max_output_ptr = comp->num_output_ptr + 4;
231 if (comp->output_ptr != NULL)
232 {
233 png_charpp old_ptr;
234
235 old_ptr = comp->output_ptr;
236 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
237 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500238 if (comp->output_ptr == (png_charpp)NULL)
239 {
240 png_warning (png_ptr, "Cannot allocate compression buffer");
241 comp->output_ptr=old_ptr;
242 }
243 png_memcpy(comp->output_ptr, old_ptr, old_max
244 * sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600245 png_free(png_ptr, old_ptr);
246 }
247 else
248 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
249 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500250 if (comp->output_ptr == (png_charpp)NULL)
251 {
252 png_warning (png_ptr, "Cannot allocate compression buffer");
253 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600254 }
255
256 /* save the data */
257 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
258 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500259 if (comp->output_ptr[comp->num_output_ptr] == (png_charp)NULL)
260 png_warning (png_ptr, "Cannot allocate compression buffer");
261 else
262 {
263 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
264 png_ptr->zbuf_size);
265 comp->num_output_ptr++;
266 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600267
268 /* and reset the buffer */
269 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
270 png_ptr->zstream.next_out = png_ptr->zbuf;
271 }
272 /* continue until we don't have any more to compress */
273 } while (png_ptr->zstream.avail_in);
274
275 /* finish the compression */
276 do
277 {
278 /* tell zlib we are finished */
279 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500280
281 if (ret == Z_OK)
282 {
283 /* check to see if we need more room */
284 if (!(png_ptr->zstream.avail_out))
285 {
286 /* check to make sure our output array has room */
287 if (comp->num_output_ptr >= comp->max_output_ptr)
288 {
289 int old_max;
290
291 old_max = comp->max_output_ptr;
292 comp->max_output_ptr = comp->num_output_ptr + 4;
293 if (comp->output_ptr != NULL)
294 {
295 png_charpp old_ptr;
296
297 old_ptr = comp->output_ptr;
298 /* This could be optimized to realloc() */
299 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
300 (png_uint_32)(comp->max_output_ptr * sizeof (png_charpp)));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500301 if (comp->output_ptr != (png_charpp)NULL)
302 png_memcpy(comp->output_ptr, old_ptr,
303 old_max * sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500304 png_free(png_ptr, old_ptr);
305 }
306 else
307 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
308 (png_uint_32)(comp->max_output_ptr * sizeof (png_charp)));
309 }
310
311 /* save off the data */
312 comp->output_ptr[comp->num_output_ptr] =
313 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500314 if (comp->output_ptr[comp->num_output_ptr] == (png_charp)NULL)
315 png_warning (png_ptr, "Cannot allocate compression buffer");
316 else
317 {
318 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
319 png_ptr->zbuf_size);
320 comp->num_output_ptr++;
321 }
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500322
323 /* and reset the buffer pointers */
324 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
325 png_ptr->zstream.next_out = png_ptr->zbuf;
326 }
327 }
328 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600329 {
330 /* we got an error */
331 if (png_ptr->zstream.msg != NULL)
332 png_error(png_ptr, png_ptr->zstream.msg);
333 else
334 png_error(png_ptr, "zlib error");
335 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600336 } while (ret != Z_STREAM_END);
337
338 /* text length is number of buffers plus last buffer */
339 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
340 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
341 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
342
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500343 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600344}
345
346/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500347static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600348png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
349{
350 int i;
351
352 /* handle the no-compression case */
353 if (comp->input)
354 {
355 png_write_chunk_data(png_ptr, (png_bytep)comp->input, comp->input_len);
356 return;
357 }
358
359 /* write saved output buffers, if any */
360 for (i = 0; i < comp->num_output_ptr; i++)
361 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600362 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
363 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600364 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500365 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366 }
367 if (comp->max_output_ptr != 0)
368 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500369 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600370 /* write anything left in zbuf */
371 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
372 png_write_chunk_data(png_ptr, png_ptr->zbuf,
373 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
374
375 /* reset zlib for another zTXt/iTXt or the image data */
376 deflateReset(&png_ptr->zstream);
377
378}
379#endif
380
Guy Schalnat0d580581995-07-20 02:43:20 -0500381/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600382 * information. Note that the rest of this code depends upon this
383 * information being correct.
384 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500385void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600386png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500387 int bit_depth, int color_type, int compression_type, int filter_type,
388 int interlace_type)
389{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600390#ifdef PNG_USE_LOCAL_ARRAYS
391 PNG_IHDR;
392#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500393 png_byte buf[13]; /* buffer to store the IHDR info */
394
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500396 /* Check that we have valid input data from the application info */
397 switch (color_type)
398 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500399 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500400 switch (bit_depth)
401 {
402 case 1:
403 case 2:
404 case 4:
405 case 8:
406 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500408 }
409 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500411 if (bit_depth != 8 && bit_depth != 16)
412 png_error(png_ptr, "Invalid bit depth for RGB image");
413 png_ptr->channels = 3;
414 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500416 switch (bit_depth)
417 {
418 case 1:
419 case 2:
420 case 4:
421 case 8: png_ptr->channels = 1; break;
422 default: png_error(png_ptr, "Invalid bit depth for paletted image");
423 }
424 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 if (bit_depth != 8 && bit_depth != 16)
427 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
428 png_ptr->channels = 2;
429 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500431 if (bit_depth != 8 && bit_depth != 16)
432 png_error(png_ptr, "Invalid bit depth for RGBA image");
433 png_ptr->channels = 4;
434 break;
435 default:
436 png_error(png_ptr, "Invalid image color type specified");
437 }
438
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500440 {
441 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500442 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500443 }
444
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600445 /* Write filter_method 64 (intrapixel differencing) only if
446 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
447 * 2. Libpng did not write a PNG signature (this filter_method is only
448 * used in PNG datastreams that are embedded in MNG datastreams) and
449 * 3. The application called png_permit_mng_features with a mask that
450 * included PNG_FLAG_MNG_FILTER_64 and
451 * 4. The filter_method is 64 and
452 * 5. The color_type is RGB or RGBA
453 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600454 if (
455#if defined(PNG_MNG_FEATURES_SUPPORTED)
456 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600457 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
458 (color_type == PNG_COLOR_TYPE_RGB ||
459 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600460 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
461#endif
462 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500463 {
464 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500465 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500466 }
467
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600468#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469 if (interlace_type != PNG_INTERLACE_NONE &&
470 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 {
472 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500473 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500474 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600475#else
476 interlace_type=PNG_INTERLACE_NONE;
477#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500478
479 /* save off the relevent information */
480 png_ptr->bit_depth = (png_byte)bit_depth;
481 png_ptr->color_type = (png_byte)color_type;
482 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600483 png_ptr->filter_type = (png_byte)filter_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500484 png_ptr->width = width;
485 png_ptr->height = height;
486
487 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500489 /* set the usr info, so any transformations can modify it */
490 png_ptr->usr_width = png_ptr->width;
491 png_ptr->usr_bit_depth = png_ptr->bit_depth;
492 png_ptr->usr_channels = png_ptr->channels;
493
Guy Schalnat0d580581995-07-20 02:43:20 -0500494 /* pack the header information into the buffer */
495 png_save_uint_32(buf, width);
496 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600497 buf[8] = (png_byte)bit_depth;
498 buf[9] = (png_byte)color_type;
499 buf[10] = (png_byte)compression_type;
500 buf[11] = (png_byte)filter_type;
501 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500502
503 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600504 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500505
Andreas Dilger47a0c421997-05-16 02:46:07 -0500506 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600507 png_ptr->zstream.zalloc = png_zalloc;
508 png_ptr->zstream.zfree = png_zfree;
509 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500511 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500512 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
513 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500514 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500515 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500516 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500517 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500518 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500519 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500520 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500521 png_ptr->zlib_strategy = Z_FILTERED;
522 else
523 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
524 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500525 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500526 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500527 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500528 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500529 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500530 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500531 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500532 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600533 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500534 png_ptr->zlib_method, png_ptr->zlib_window_bits,
535 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600536 png_ptr->zstream.next_out = png_ptr->zbuf;
537 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500538
Guy Schalnate5a37791996-06-05 15:50:50 -0500539 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500540}
541
542/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500543 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600544 * structure.
545 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500546void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500547png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500548{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600549#ifdef PNG_USE_LOCAL_ARRAYS
550 PNG_PLTE;
551#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500552 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600553 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500554 png_byte buf[3];
555
Andreas Dilger47a0c421997-05-16 02:46:07 -0500556 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500557 if ((
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -0600558#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
559 defined (PNG_WRITE_EMPTY_PLTE_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600560 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500561#endif
562 num_pal == 0) || num_pal > 256)
563 {
564 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
565 {
566 png_error(png_ptr, "Invalid number of colors in palette");
567 }
568 else
569 {
570 png_warning(png_ptr, "Invalid number of colors in palette");
571 return;
572 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500573 }
574
Andreas Dilger47a0c421997-05-16 02:46:07 -0500575 png_ptr->num_palette = (png_uint_16)num_pal;
576 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500577
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600578 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500579#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500581 {
582 buf[0] = pal_ptr->red;
583 buf[1] = pal_ptr->green;
584 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500586 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500587#else
588 /* This is a little slower but some buggy compilers need to do this instead */
589 pal_ptr=palette;
590 for (i = 0; i < num_pal; i++)
591 {
592 buf[0] = pal_ptr[i].red;
593 buf[1] = pal_ptr[i].green;
594 buf[2] = pal_ptr[i].blue;
595 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
596 }
597#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500599 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500600}
601
602/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500603void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500604png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500605{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600606#ifdef PNG_USE_LOCAL_ARRAYS
607 PNG_IDAT;
608#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500609 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600610 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500611 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500612}
613
614/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500615void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600616png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500617{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600618#ifdef PNG_USE_LOCAL_ARRAYS
619 PNG_IEND;
620#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500621 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600622 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600623 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500624}
625
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500626#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500627/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600628#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500629void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500630png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500631{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600632#ifdef PNG_USE_LOCAL_ARRAYS
633 PNG_gAMA;
634#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500635 png_uint_32 igamma;
636 png_byte buf[4];
637
Andreas Dilger47a0c421997-05-16 02:46:07 -0500638 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600639 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600640 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500641 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600642 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500643}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500644#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600645#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500646void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600647png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600648{
649#ifdef PNG_USE_LOCAL_ARRAYS
650 PNG_gAMA;
651#endif
652 png_byte buf[4];
653
654 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600655 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600656 png_save_uint_32(buf, file_gamma);
657 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
658}
659#endif
660#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500661
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600662#if defined(PNG_WRITE_sRGB_SUPPORTED)
663/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500664void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600665png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600666{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600667#ifdef PNG_USE_LOCAL_ARRAYS
668 PNG_sRGB;
669#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600670 png_byte buf[1];
671
672 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600673 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600674 png_warning(png_ptr,
675 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600676 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600677 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600678}
679#endif
680
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600681#if defined(PNG_WRITE_iCCP_SUPPORTED)
682/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500683void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600684png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
685 png_charp profile, int profile_len)
686{
687#ifdef PNG_USE_LOCAL_ARRAYS
688 PNG_iCCP;
689#endif
690 png_size_t name_len;
691 png_charp new_name;
692 compression_state comp;
693
694 png_debug(1, "in png_write_iCCP\n");
695 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
696 &new_name)) == 0)
697 {
698 png_warning(png_ptr, "Empty keyword in iCCP chunk");
699 return;
700 }
701
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600702 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600703 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600704
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500705 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600706 profile_len = 0;
707
708 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500709 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600710 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600711
712 /* make sure we include the NULL after the name and the compression type */
713 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
714 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600715 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600716 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
717
718 if (profile_len)
719 png_write_compressed_data_out(png_ptr, &comp);
720
721 png_write_chunk_end(png_ptr);
722 png_free(png_ptr, new_name);
723}
724#endif
725
726#if defined(PNG_WRITE_sPLT_SUPPORTED)
727/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500728void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600729png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600730{
731#ifdef PNG_USE_LOCAL_ARRAYS
732 PNG_sPLT;
733#endif
734 png_size_t name_len;
735 png_charp new_name;
736 png_byte entrybuf[10];
737 int entry_size = (spalette->depth == 8 ? 6 : 10);
738 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600739 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500740#ifdef PNG_NO_POINTER_INDEXING
741 int i;
742#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600743
744 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600745 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
746 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600747 {
748 png_warning(png_ptr, "Empty keyword in sPLT chunk");
749 return;
750 }
751
752 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -0500753 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600754 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600755 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600756 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600757
758 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500759#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600760 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
761 {
762 if (spalette->depth == 8)
763 {
764 entrybuf[0] = (png_byte)ep->red;
765 entrybuf[1] = (png_byte)ep->green;
766 entrybuf[2] = (png_byte)ep->blue;
767 entrybuf[3] = (png_byte)ep->alpha;
768 png_save_uint_16(entrybuf + 4, ep->frequency);
769 }
770 else
771 {
772 png_save_uint_16(entrybuf + 0, ep->red);
773 png_save_uint_16(entrybuf + 2, ep->green);
774 png_save_uint_16(entrybuf + 4, ep->blue);
775 png_save_uint_16(entrybuf + 6, ep->alpha);
776 png_save_uint_16(entrybuf + 8, ep->frequency);
777 }
778 png_write_chunk_data(png_ptr, entrybuf, entry_size);
779 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500780#else
781 ep=spalette->entries;
782 for (i=0; i>spalette->nentries; i++)
783 {
784 if (spalette->depth == 8)
785 {
786 entrybuf[0] = (png_byte)ep[i].red;
787 entrybuf[1] = (png_byte)ep[i].green;
788 entrybuf[2] = (png_byte)ep[i].blue;
789 entrybuf[3] = (png_byte)ep[i].alpha;
790 png_save_uint_16(entrybuf + 4, ep[i].frequency);
791 }
792 else
793 {
794 png_save_uint_16(entrybuf + 0, ep[i].red);
795 png_save_uint_16(entrybuf + 2, ep[i].green);
796 png_save_uint_16(entrybuf + 4, ep[i].blue);
797 png_save_uint_16(entrybuf + 6, ep[i].alpha);
798 png_save_uint_16(entrybuf + 8, ep[i].frequency);
799 }
800 png_write_chunk_data(png_ptr, entrybuf, entry_size);
801 }
802#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600803
804 png_write_chunk_end(png_ptr);
805 png_free(png_ptr, new_name);
806}
807#endif
808
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500809#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500810/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500811void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600812png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500813{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600814#ifdef PNG_USE_LOCAL_ARRAYS
815 PNG_sBIT;
816#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500817 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500818 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500819
Andreas Dilger47a0c421997-05-16 02:46:07 -0500820 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600821 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500822 if (color_type & PNG_COLOR_MASK_COLOR)
823 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600824 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500825
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500826 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
827 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600828 if (sbit->red == 0 || sbit->red > maxbits ||
829 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500830 sbit->blue == 0 || sbit->blue > maxbits)
831 {
832 png_warning(png_ptr, "Invalid sBIT depth specified");
833 return;
834 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500835 buf[0] = sbit->red;
836 buf[1] = sbit->green;
837 buf[2] = sbit->blue;
838 size = 3;
839 }
840 else
841 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500842 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
843 {
844 png_warning(png_ptr, "Invalid sBIT depth specified");
845 return;
846 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500847 buf[0] = sbit->gray;
848 size = 1;
849 }
850
851 if (color_type & PNG_COLOR_MASK_ALPHA)
852 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500853 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
854 {
855 png_warning(png_ptr, "Invalid sBIT depth specified");
856 return;
857 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500858 buf[size++] = sbit->alpha;
859 }
860
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600861 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500862}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500863#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500864
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500865#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500866/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500868void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500869png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600870 double red_x, double red_y, double green_x, double green_y,
871 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500872{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600873#ifdef PNG_USE_LOCAL_ARRAYS
874 PNG_cHRM;
875#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500876 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600877 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500878
Andreas Dilger47a0c421997-05-16 02:46:07 -0500879 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600880 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500881 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
882 white_x + white_y > 1.0)
883 {
884 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500885#if !defined(PNG_NO_CONSOLE_IO)
886 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600887#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500888 return;
889 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600890 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500891 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600892 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500893 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500894
895 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
896 red_x + red_y > 1.0)
897 {
898 png_warning(png_ptr, "Invalid cHRM red point specified");
899 return;
900 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600901 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500902 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600903 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500904 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500905
906 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
907 green_x + green_y > 1.0)
908 {
909 png_warning(png_ptr, "Invalid cHRM green point specified");
910 return;
911 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600912 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500913 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600914 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500915 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500916
917 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
918 blue_x + blue_y > 1.0)
919 {
920 png_warning(png_ptr, "Invalid cHRM blue point specified");
921 return;
922 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600923 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500924 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600925 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500926 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500927
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600928 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500929}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500930#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600931#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500932void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600933png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
934 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
935 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
936 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600937{
938#ifdef PNG_USE_LOCAL_ARRAYS
939 PNG_cHRM;
940#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600941 png_byte buf[32];
942
943 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600944 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600945 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
946 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600947 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500948#if !defined(PNG_NO_CONSOLE_IO)
949 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600950#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600951 return;
952 }
953 png_save_uint_32(buf, white_x);
954 png_save_uint_32(buf + 4, white_y);
955
956 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
957 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600958 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600959 return;
960 }
961 png_save_uint_32(buf + 8, red_x);
962 png_save_uint_32(buf + 12, red_y);
963
964 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
965 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600966 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600967 return;
968 }
969 png_save_uint_32(buf + 16, green_x);
970 png_save_uint_32(buf + 20, green_y);
971
972 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
973 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600974 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600975 return;
976 }
977 png_save_uint_32(buf + 24, blue_x);
978 png_save_uint_32(buf + 28, blue_y);
979
980 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
981}
982#endif
983#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500984
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500985#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500986/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500987void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600988png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500989 int num_trans, int color_type)
990{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600991#ifdef PNG_USE_LOCAL_ARRAYS
992 PNG_tRNS;
993#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500994 png_byte buf[6];
995
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500997 if (color_type == PNG_COLOR_TYPE_PALETTE)
998 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600999 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001000 {
1001 png_warning(png_ptr,"Invalid number of transparent colors specified");
1002 return;
1003 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001004 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001005 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001006 }
1007 else if (color_type == PNG_COLOR_TYPE_GRAY)
1008 {
1009 /* one 16 bit value */
1010 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001011 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001012 }
1013 else if (color_type == PNG_COLOR_TYPE_RGB)
1014 {
1015 /* three 16 bit values */
1016 png_save_uint_16(buf, tran->red);
1017 png_save_uint_16(buf + 2, tran->green);
1018 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001019 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001020 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001021 else
1022 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001023 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001024 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001025}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001026#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001027
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001028#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001029/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001030void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001031png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001032{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001033#ifdef PNG_USE_LOCAL_ARRAYS
1034 PNG_bKGD;
1035#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001036 png_byte buf[6];
1037
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001039 if (color_type == PNG_COLOR_TYPE_PALETTE)
1040 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001041 if (
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001042#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
1043 defined (PNG_WRITE_EMPTY_PLTE_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001044 (png_ptr->num_palette ||
1045 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001046#endif
1047 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001048 {
1049 png_warning(png_ptr, "Invalid background palette index");
1050 return;
1051 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001052 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001053 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001054 }
1055 else if (color_type & PNG_COLOR_MASK_COLOR)
1056 {
1057 png_save_uint_16(buf, back->red);
1058 png_save_uint_16(buf + 2, back->green);
1059 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001060 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001061 }
1062 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001063 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001064 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001065 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001066 }
1067}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001068#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001069
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001070#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001071/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001072void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001073png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001074{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001075#ifdef PNG_USE_LOCAL_ARRAYS
1076 PNG_hIST;
1077#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001078 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001079 png_byte buf[3];
1080
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001082 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001083 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001084 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1085 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001086 png_warning(png_ptr, "Invalid number of histogram entries specified");
1087 return;
1088 }
1089
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001090 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001091 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001092 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001093 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001095 }
1096 png_write_chunk_end(png_ptr);
1097}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001098#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001099
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001100#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1101 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001102/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1103 * and if invalid, correct the keyword rather than discarding the entire
1104 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1105 * length, forbids leading or trailing whitespace, multiple internal spaces,
1106 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001107 *
1108 * The new_key is allocated to hold the corrected keyword and must be freed
1109 * by the calling routine. This avoids problems with trying to write to
1110 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001111 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001112png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001113png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001114{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001116 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001117 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001118 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001119
Andreas Dilger47a0c421997-05-16 02:46:07 -05001120 png_debug(1, "in png_check_keyword\n");
1121 *new_key = NULL;
1122
1123 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001124 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001125 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001126 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001127 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001128
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1130
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001131 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001132
1133 if (*new_key == (png_charp)NULL)
1134 {
1135 png_warning(png_ptr, "Could not allocate new key; keyword not checked");
1136 return key_len;
1137 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138
1139 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001140 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001141 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001142 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001144#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001146
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001147 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001148 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001149#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001150 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001151#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 *dp = ' ';
1153 }
1154 else
1155 {
1156 *dp = *kp;
1157 }
1158 }
1159 *dp = '\0';
1160
1161 /* Remove any trailing white space. */
1162 kp = *new_key + key_len - 1;
1163 if (*kp == ' ')
1164 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001165 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166
1167 while (*kp == ' ')
1168 {
1169 *(kp--) = '\0';
1170 key_len--;
1171 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172 }
1173
1174 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001175 kp = *new_key;
1176 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001177 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001178 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001179
1180 while (*kp == ' ')
1181 {
1182 kp++;
1183 key_len--;
1184 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185 }
1186
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001188
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189 /* Remove multiple internal spaces. */
1190 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001191 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001192 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001193 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194 *(dp++) = *kp;
1195 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001196 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001197 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198 {
1199 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001200 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001201 }
1202 else
1203 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 *(dp++) = *kp;
1205 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001206 }
1207 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001208 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001209 if(kwarn)
1210 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001211
1212 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001213 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001214 png_free(png_ptr, *new_key);
1215 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001216 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217 }
1218
1219 if (key_len > 79)
1220 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001221 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222 new_key[79] = '\0';
1223 key_len = 79;
1224 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001225
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001226 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001227}
1228#endif
1229
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001230#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001231/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001232void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001233png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001235{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001236#ifdef PNG_USE_LOCAL_ARRAYS
1237 PNG_tEXt;
1238#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001239 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001240 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001241
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242 png_debug(1, "in png_write_tEXt\n");
1243 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1244 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001245 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001246 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001248
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001250 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001251 else
1252 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001253
1254 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001255 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 -05001256 /*
1257 * We leave it to the application to meet PNG-1.0 requirements on the
1258 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1259 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001260 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001261 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001262 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001263 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001265
Guy Schalnat0d580581995-07-20 02:43:20 -05001266 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001267 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001268}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001269#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001270
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001271#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001272/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001273void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001274png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001275 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001276{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001277#ifdef PNG_USE_LOCAL_ARRAYS
1278 PNG_zTXt;
1279#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001280 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001281 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001282 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001283 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001284
Andreas Dilger47a0c421997-05-16 02:46:07 -05001285 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001286
Andreas Dilger47a0c421997-05-16 02:46:07 -05001287 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001288 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001289 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001290 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001291 }
1292
Andreas Dilger47a0c421997-05-16 02:46:07 -05001293 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1294 {
1295 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1296 png_free(png_ptr, new_key);
1297 return;
1298 }
1299
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001300 text_len = png_strlen(text);
1301
Andreas Dilger47a0c421997-05-16 02:46:07 -05001302 png_free(png_ptr, new_key);
1303
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001304 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001305 text_len = png_text_compress(png_ptr, text, text_len, compression,
1306 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001307
1308 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001309 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1310 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001311 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001312 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001313 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001314 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001316 /* write the compressed data */
1317 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001318
Guy Schalnat0d580581995-07-20 02:43:20 -05001319 /* close the chunk */
1320 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001321}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001322#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001323
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001324#if defined(PNG_WRITE_iTXt_SUPPORTED)
1325/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001326void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001327png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001328 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001329{
1330#ifdef PNG_USE_LOCAL_ARRAYS
1331 PNG_iTXt;
1332#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001333 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001334 png_charp new_lang, new_key;
1335 png_byte cbuf[2];
1336 compression_state comp;
1337
1338 png_debug(1, "in png_write_iTXt\n");
1339
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001340 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1341 {
1342 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1343 return;
1344 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001345 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang,
1346 &new_lang))==0)
1347 {
1348 png_warning(png_ptr, "Empty language field in iTXt chunk");
1349 return;
1350 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001351 lang_key_len = png_strlen(lang_key);
1352 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001353
1354 if (text == NULL || *text == '\0')
1355 text_len = 0;
1356
1357 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001358 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1359 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001360
1361 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001362 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001363
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001364 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1365 (png_uint_32)(
1366 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1367 + key_len
1368 + lang_len
1369 + lang_key_len
1370 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001371
1372 /*
1373 * We leave it to the application to meet PNG-1.0 requirements on the
1374 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1375 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1376 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1377 */
1378 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001379
1380 /* set the compression flag */
1381 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1382 compression == PNG_TEXT_COMPRESSION_NONE)
1383 cbuf[0] = 0;
1384 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1385 cbuf[0] = 1;
1386 /* set the compression method */
1387 cbuf[1] = 0;
1388 png_write_chunk_data(png_ptr, cbuf, 2);
1389
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001390 png_write_chunk_data(png_ptr, (png_bytep)new_lang, lang_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001391 png_write_chunk_data(png_ptr, (png_bytep)lang_key, lang_key_len+1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001392 png_write_chunk_data(png_ptr, '\0', 1);
1393
1394 png_write_compressed_data_out(png_ptr, &comp);
1395
1396 png_write_chunk_end(png_ptr);
1397 png_free(png_ptr, new_key);
1398 png_free(png_ptr, new_lang);
1399}
1400#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001401
1402#if defined(PNG_WRITE_oFFs_SUPPORTED)
1403/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001404void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001405png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
1406 png_uint_32 y_offset,
1407 int unit_type)
1408{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001409#ifdef PNG_USE_LOCAL_ARRAYS
1410 PNG_oFFs;
1411#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001412 png_byte buf[9];
1413
1414 png_debug(1, "in png_write_oFFs\n");
1415 if (unit_type >= PNG_OFFSET_LAST)
1416 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1417
1418 png_save_uint_32(buf, x_offset);
1419 png_save_uint_32(buf + 4, y_offset);
1420 buf[8] = (png_byte)unit_type;
1421
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001422 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001423}
1424#endif
1425
1426#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001427/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001428void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001429png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1430 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1431{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001432#ifdef PNG_USE_LOCAL_ARRAYS
1433 PNG_pCAL;
1434#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001435 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001436 png_uint_32p params_len;
1437 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001438 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001439 int i;
1440
1441 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1442 if (type >= PNG_EQUATION_LAST)
1443 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1444
1445 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1446 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1447 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1448 png_debug1(3, "pCAL units length = %d\n", units_len);
1449 total_len = purpose_len + units_len + 10;
1450
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001451 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1452 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001453
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001454 if (params_len == (png_uint_32p)NULL)
1455 {
1456 png_warning (png_ptr, "Could not allocate params for pCAL");
1457 return;
1458 }
1459
Andreas Dilger47a0c421997-05-16 02:46:07 -05001460 /* Find the length of each parameter, making sure we don't count the
1461 null terminator for the last parameter. */
1462 for (i = 0; i < nparams; i++)
1463 {
1464 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001465 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001466 total_len += (png_size_t)params_len[i];
1467 }
1468
1469 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001470 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001471 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001472 png_save_int_32(buf, X0);
1473 png_save_int_32(buf + 4, X1);
1474 buf[8] = (png_byte)type;
1475 buf[9] = (png_byte)nparams;
1476 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1477 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1478
1479 png_free(png_ptr, new_purpose);
1480
1481 for (i = 0; i < nparams; i++)
1482 {
1483 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1484 (png_size_t)params_len[i]);
1485 }
1486
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001487 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001488 png_write_chunk_end(png_ptr);
1489}
1490#endif
1491
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001492#if defined(PNG_WRITE_sCAL_SUPPORTED)
1493/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001494#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001495void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001496png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001497{
1498#ifdef PNG_USE_LOCAL_ARRAYS
1499 PNG_sCAL;
1500#endif
1501 png_size_t total_len;
1502 char wbuf[32], hbuf[32];
1503
1504 png_debug(1, "in png_write_sCAL\n");
1505
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001506#if defined(_WIN32_WCE)
1507/* sprintf() function is not supported on WindowsCE */
1508 {
1509 wchar_t wc_buf[32];
1510 swprintf(wc_buf, TEXT("%12.12e"), width);
1511 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1512 swprintf(wc_buf, TEXT("%12.12e"), height);
1513 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1514 }
1515#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001516 sprintf(wbuf, "%12.12e", width);
1517 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001518#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001519 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001520
1521 png_debug1(3, "sCAL total length = %d\n", total_len);
1522 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001523 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrsone1644472001-03-23 05:06:56 -06001524 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1525 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001526
1527 png_write_chunk_end(png_ptr);
1528}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001529#else
1530#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001531void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001532png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001533 png_charp height)
1534{
1535#ifdef PNG_USE_LOCAL_ARRAYS
1536 PNG_sCAL;
1537#endif
1538 png_size_t total_len;
1539 char wbuf[32], hbuf[32];
1540
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001541 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001542
Glenn Randers-Pehrsone1644472001-03-23 05:06:56 -06001543 png_strcpy(wbuf,(const char *)width);
1544 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001545 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001546
1547 png_debug1(3, "sCAL total length = %d\n", total_len);
1548 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001549 png_write_chunk_data(png_ptr, (png_bytep)&unit, 1);
Glenn Randers-Pehrsone1644472001-03-23 05:06:56 -06001550 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1551 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001552
1553 png_write_chunk_end(png_ptr);
1554}
1555#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001556#endif
1557#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001558
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001559#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001560/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001561void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001562png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001563 png_uint_32 y_pixels_per_unit,
1564 int unit_type)
1565{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001566#ifdef PNG_USE_LOCAL_ARRAYS
1567 PNG_pHYs;
1568#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001569 png_byte buf[9];
1570
Andreas Dilger47a0c421997-05-16 02:46:07 -05001571 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001572 if (unit_type >= PNG_RESOLUTION_LAST)
1573 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1574
Guy Schalnat0d580581995-07-20 02:43:20 -05001575 png_save_uint_32(buf, x_pixels_per_unit);
1576 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001577 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001578
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001579 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001580}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001581#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001582
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001583#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001584/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1585 * or png_convert_from_time_t(), or fill in the structure yourself.
1586 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001587void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001588png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001589{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001590#ifdef PNG_USE_LOCAL_ARRAYS
1591 PNG_tIME;
1592#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001593 png_byte buf[7];
1594
Andreas Dilger47a0c421997-05-16 02:46:07 -05001595 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001596 if (mod_time->month > 12 || mod_time->month < 1 ||
1597 mod_time->day > 31 || mod_time->day < 1 ||
1598 mod_time->hour > 23 || mod_time->second > 60)
1599 {
1600 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1601 return;
1602 }
1603
Guy Schalnat0d580581995-07-20 02:43:20 -05001604 png_save_uint_16(buf, mod_time->year);
1605 buf[2] = mod_time->month;
1606 buf[3] = mod_time->day;
1607 buf[4] = mod_time->hour;
1608 buf[5] = mod_time->minute;
1609 buf[6] = mod_time->second;
1610
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001611 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001612}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001613#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001614
1615/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001616void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001617png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001618{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001619#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001620 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001621
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001622 /* start of interlace block */
1623 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001624
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001625 /* offset to next interlace block */
1626 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001628 /* start of interlace block in the y direction */
1629 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001630
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001631 /* offset to next interlace block in the y direction */
1632 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001633#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001634
Andreas Dilger47a0c421997-05-16 02:46:07 -05001635 png_size_t buf_size;
1636
1637 png_debug(1, "in png_write_start_row\n");
1638 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1639 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1640
Guy Schalnat0d580581995-07-20 02:43:20 -05001641 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001642 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001643 if (png_ptr->row_buf == (png_bytep)NULL)
1644 png_error(png_ptr, "Could not allocate row buffer");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001645 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001646
1647 /* set up filtering buffer, if using this filter */
1648 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001649 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001650 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001651 (png_ptr->rowbytes + 1));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001652 if (png_ptr->sub_row == (png_bytep)NULL)
1653 png_error(png_ptr, "Could not allocate sub row buffer");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001654 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001655 }
1656
Andreas Dilger47a0c421997-05-16 02:46:07 -05001657 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001658 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1659 {
1660 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001661 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001662 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001663
1664 if (png_ptr->do_filter & PNG_FILTER_UP)
1665 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001666 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001667 (png_ptr->rowbytes + 1));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001668 if (png_ptr->up_row == (png_bytep)NULL)
1669 png_error(png_ptr, "Could not allocate up row buffer");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001670 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001671 }
1672
1673 if (png_ptr->do_filter & PNG_FILTER_AVG)
1674 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001675 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1676 (png_ptr->rowbytes + 1));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001677 if (png_ptr->avg_row == (png_bytep)NULL)
1678 png_error(png_ptr, "Could not allocate avg row buffer");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001679 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001680 }
1681
1682 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1683 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001684 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001685 (png_ptr->rowbytes + 1));
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -05001686 if (png_ptr->paeth_row == (png_bytep)NULL)
1687 png_error(png_ptr, "Could not allocate paeth row buffer");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001688 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001689 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001690 }
1691
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001692#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001693 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001694 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001695 {
1696 if (!(png_ptr->transformations & PNG_INTERLACE))
1697 {
1698 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1699 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001700 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1701 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001702 }
1703 else
1704 {
1705 png_ptr->num_rows = png_ptr->height;
1706 png_ptr->usr_width = png_ptr->width;
1707 }
1708 }
1709 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001710#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001711 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001712 png_ptr->num_rows = png_ptr->height;
1713 png_ptr->usr_width = png_ptr->width;
1714 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001715 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1716 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001717}
1718
Andreas Dilger47a0c421997-05-16 02:46:07 -05001719/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001720void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001721png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001722{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001723#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001724 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001725
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001726 /* start of interlace block */
1727 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001728
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001729 /* offset to next interlace block */
1730 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001731
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001732 /* start of interlace block in the y direction */
1733 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001734
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001735 /* offset to next interlace block in the y direction */
1736 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001737#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001738
Guy Schalnat0d580581995-07-20 02:43:20 -05001739 int ret;
1740
Andreas Dilger47a0c421997-05-16 02:46:07 -05001741 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001742 /* next row */
1743 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001744
Guy Schalnat0d580581995-07-20 02:43:20 -05001745 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001746 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001747 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001748
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001749#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001750 /* if interlaced, go to next pass */
1751 if (png_ptr->interlaced)
1752 {
1753 png_ptr->row_number = 0;
1754 if (png_ptr->transformations & PNG_INTERLACE)
1755 {
1756 png_ptr->pass++;
1757 }
1758 else
1759 {
1760 /* loop until we find a non-zero width or height pass */
1761 do
1762 {
1763 png_ptr->pass++;
1764 if (png_ptr->pass >= 7)
1765 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001766 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001767 png_pass_inc[png_ptr->pass] - 1 -
1768 png_pass_start[png_ptr->pass]) /
1769 png_pass_inc[png_ptr->pass];
1770 png_ptr->num_rows = (png_ptr->height +
1771 png_pass_yinc[png_ptr->pass] - 1 -
1772 png_pass_ystart[png_ptr->pass]) /
1773 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001774 if (png_ptr->transformations & PNG_INTERLACE)
1775 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001776 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1777
1778 }
1779
Guy Schalnate5a37791996-06-05 15:50:50 -05001780 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001781 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001782 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001783 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001784 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001785 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001786 (png_uint_32)png_ptr->usr_bit_depth *
1787 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001788 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001789 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001790 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001791#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001792
1793 /* if we get here, we've just written the last row, so we need
1794 to flush the compressor */
1795 do
1796 {
1797 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001798 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001799 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001800 if (ret == Z_OK)
1801 {
1802 /* check to see if we need more room */
1803 if (!(png_ptr->zstream.avail_out))
1804 {
1805 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1806 png_ptr->zstream.next_out = png_ptr->zbuf;
1807 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1808 }
1809 }
1810 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001811 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001812 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001813 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001814 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001815 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001817 } while (ret != Z_STREAM_END);
1818
1819 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001820 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001821 {
1822 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001823 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 }
1825
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001826 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001827}
1828
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001829#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001830/* Pick out the correct pixels for the interlace pass.
1831 * The basic idea here is to go through the row with a source
1832 * pointer and a destination pointer (sp and dp), and copy the
1833 * correct pixels for the pass. As the row gets compacted,
1834 * sp will always be >= dp, so we should never overwrite anything.
1835 * See the default: case for the easiest code to understand.
1836 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001837void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001838png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001839{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001840#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001841 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001842
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001843 /* start of interlace block */
1844 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001845
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001846 /* offset to next interlace block */
1847 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001848#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001849
Andreas Dilger47a0c421997-05-16 02:46:07 -05001850 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001851 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001852#if defined(PNG_USELESS_TESTS_SUPPORTED)
1853 if (row != NULL && row_info != NULL && pass < 6)
1854#else
1855 if (pass < 6)
1856#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001857 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001858 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001859 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001860 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001861 case 1:
1862 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001863 png_bytep sp;
1864 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001865 int shift;
1866 int d;
1867 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001868 png_uint_32 i;
1869 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001870
1871 dp = row;
1872 d = 0;
1873 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001874 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001875 i += png_pass_inc[pass])
1876 {
1877 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001878 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001879 d |= (value << shift);
1880
1881 if (shift == 0)
1882 {
1883 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001884 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001885 d = 0;
1886 }
1887 else
1888 shift--;
1889
1890 }
1891 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001892 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001893 break;
1894 }
1895 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001896 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001897 png_bytep sp;
1898 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001899 int shift;
1900 int d;
1901 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001902 png_uint_32 i;
1903 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001904
1905 dp = row;
1906 shift = 6;
1907 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001908 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001909 i += png_pass_inc[pass])
1910 {
1911 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001912 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001913 d |= (value << shift);
1914
1915 if (shift == 0)
1916 {
1917 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001918 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001919 d = 0;
1920 }
1921 else
1922 shift -= 2;
1923 }
1924 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001925 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 break;
1927 }
1928 case 4:
1929 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001930 png_bytep sp;
1931 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001932 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001933 int d;
1934 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001935 png_uint_32 i;
1936 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001937
1938 dp = row;
1939 shift = 4;
1940 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001941 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001942 i += png_pass_inc[pass])
1943 {
1944 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001945 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001946 d |= (value << shift);
1947
1948 if (shift == 0)
1949 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001950 shift = 4;
1951 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001952 d = 0;
1953 }
1954 else
1955 shift -= 4;
1956 }
1957 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001958 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001959 break;
1960 }
1961 default:
1962 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001963 png_bytep sp;
1964 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001965 png_uint_32 i;
1966 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001967 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001968
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001969 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 dp = row;
1971 /* find out how many bytes each pixel takes up */
1972 pixel_bytes = (row_info->pixel_depth >> 3);
1973 /* loop through the row, only looking at the pixels that
1974 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001975 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001976 i += png_pass_inc[pass])
1977 {
1978 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001979 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001980 /* move the pixel */
1981 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001982 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001983 /* next pixel */
1984 dp += pixel_bytes;
1985 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001986 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001987 }
1988 }
1989 /* set new row width */
1990 row_info->width = (row_info->width +
1991 png_pass_inc[pass] - 1 -
1992 png_pass_start[pass]) /
1993 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001994 row_info->rowbytes = ((row_info->width *
1995 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001996 }
1997}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001998#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001999
Andreas Dilger47a0c421997-05-16 02:46:07 -05002000/* This filters the row, chooses which filter to use, if it has not already
2001 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002002 * chosen filter.
2003 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002004#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002005#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002006#define PNG_LOMASK ((png_uint_32)0xffffL)
2007#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002008void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002009png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002010{
Guy Schalnate5a37791996-06-05 15:50:50 -05002011 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002012 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002013 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002014 png_uint_32 row_bytes = row_info->rowbytes;
2015#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2016 int num_p_filters = (int)png_ptr->num_prev_filters;
2017#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002018
Andreas Dilger47a0c421997-05-16 02:46:07 -05002019 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002020 /* find out how many bytes offset each pixel is */
2021 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05002022
2023 prev_row = png_ptr->prev_row;
2024 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002025 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002026
Andreas Dilger47a0c421997-05-16 02:46:07 -05002027 /* The prediction method we use is to find which method provides the
2028 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002029 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002030 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002031 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002032 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002033 * predictive" method (not implemented yet), which does test compressions
2034 * of lines using different filter methods, and then chooses the
2035 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002036 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002037 *
2038 * GRR 980525: consider also
2039 * (1) minimum sum of absolute differences from running average (i.e.,
2040 * keep running sum of non-absolute differences & count of bytes)
2041 * [track dispersion, too? restart average if dispersion too large?]
2042 * (1b) minimum sum of absolute differences from sliding average, probably
2043 * with window size <= deflate window (usually 32K)
2044 * (2) minimum sum of squared differences from zero or running average
2045 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002046 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002047
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002048
Guy Schalnate5a37791996-06-05 15:50:50 -05002049 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002050 * that has been chosen, as it doesn't actually do anything to the data.
2051 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002052 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002053 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002054 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002055 png_bytep rp;
2056 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002057 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002058 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002059
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002060 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002061 {
2062 v = *rp;
2063 sum += (v < 128) ? v : 256 - v;
2064 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002065
2066#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2067 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2068 {
2069 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002070 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002071 sumlo = sum & PNG_LOMASK;
2072 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2073
2074 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002075 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002077 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002079 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002080 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002081 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002082 PNG_WEIGHT_SHIFT;
2083 }
2084 }
2085
2086 /* Factor in the cost of this filter (this is here for completeness,
2087 * but it makes no sense to have a "cost" for the NONE filter, as
2088 * it has the minimum possible computational cost - none).
2089 */
2090 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2091 PNG_COST_SHIFT;
2092 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2093 PNG_COST_SHIFT;
2094
2095 if (sumhi > PNG_HIMASK)
2096 sum = PNG_MAXSUM;
2097 else
2098 sum = (sumhi << PNG_HISHIFT) + sumlo;
2099 }
2100#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002101 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002102 }
2103
Guy Schalnate5a37791996-06-05 15:50:50 -05002104 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002105 if (filter_to_do == PNG_FILTER_SUB)
2106 /* it's the only filter so no testing is needed */
2107 {
2108 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002109 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002110 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2111 i++, rp++, dp++)
2112 {
2113 *dp = *rp;
2114 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002115 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002116 i++, rp++, lp++, dp++)
2117 {
2118 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2119 }
2120 best_row = png_ptr->sub_row;
2121 }
2122
2123 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002124 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002125 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002126 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002127 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002128 int v;
2129
2130#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002131 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002132 * would reduce the sum of this filter, so that we can do the
2133 * early exit comparison without scaling the sum each time.
2134 */
2135 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2136 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002137 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002138 png_uint_32 lmhi, lmlo;
2139 lmlo = lmins & PNG_LOMASK;
2140 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2141
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002142 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002143 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002144 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002146 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002147 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002148 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002149 PNG_WEIGHT_SHIFT;
2150 }
2151 }
2152
2153 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2154 PNG_COST_SHIFT;
2155 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2156 PNG_COST_SHIFT;
2157
2158 if (lmhi > PNG_HIMASK)
2159 lmins = PNG_MAXSUM;
2160 else
2161 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2162 }
2163#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002164
Guy Schalnate5a37791996-06-05 15:50:50 -05002165 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2166 i++, rp++, dp++)
2167 {
2168 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002169
Guy Schalnate5a37791996-06-05 15:50:50 -05002170 sum += (v < 128) ? v : 256 - v;
2171 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002172 for (lp = row_buf + 1; i < row_info->rowbytes;
2173 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002174 {
2175 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002176
Guy Schalnate5a37791996-06-05 15:50:50 -05002177 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002178
2179 if (sum > lmins) /* We are already worse, don't continue. */
2180 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002181 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182
2183#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2184 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2185 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002186 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002187 png_uint_32 sumhi, sumlo;
2188 sumlo = sum & PNG_LOMASK;
2189 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2190
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002191 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002192 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002193 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002197 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198 PNG_WEIGHT_SHIFT;
2199 }
2200 }
2201
2202 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2203 PNG_COST_SHIFT;
2204 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2205 PNG_COST_SHIFT;
2206
2207 if (sumhi > PNG_HIMASK)
2208 sum = PNG_MAXSUM;
2209 else
2210 sum = (sumhi << PNG_HISHIFT) + sumlo;
2211 }
2212#endif
2213
Guy Schalnate5a37791996-06-05 15:50:50 -05002214 if (sum < mins)
2215 {
2216 mins = sum;
2217 best_row = png_ptr->sub_row;
2218 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002219 }
2220
Guy Schalnate5a37791996-06-05 15:50:50 -05002221 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002222 if (filter_to_do == PNG_FILTER_UP)
2223 {
2224 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002225 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002226
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002227 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002228 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002229 i++, rp++, pp++, dp++)
2230 {
2231 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2232 }
2233 best_row = png_ptr->up_row;
2234 }
2235
2236 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002237 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002238 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002239 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002240 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002241 int v;
2242
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002243
Andreas Dilger47a0c421997-05-16 02:46:07 -05002244#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2245 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2246 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002247 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248 png_uint_32 lmhi, lmlo;
2249 lmlo = lmins & PNG_LOMASK;
2250 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2251
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002252 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002254 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002256 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002258 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002259 PNG_WEIGHT_SHIFT;
2260 }
2261 }
2262
2263 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2264 PNG_COST_SHIFT;
2265 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2266 PNG_COST_SHIFT;
2267
2268 if (lmhi > PNG_HIMASK)
2269 lmins = PNG_MAXSUM;
2270 else
2271 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2272 }
2273#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002274
2275 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002276 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002277 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002278 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002279
2280 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002281
2282 if (sum > lmins) /* We are already worse, don't continue. */
2283 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002284 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002285
2286#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2287 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2288 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002289 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002290 png_uint_32 sumhi, sumlo;
2291 sumlo = sum & PNG_LOMASK;
2292 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2293
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002294 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002296 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002297 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002298 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002299 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002300 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002301 PNG_WEIGHT_SHIFT;
2302 }
2303 }
2304
2305 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2306 PNG_COST_SHIFT;
2307 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2308 PNG_COST_SHIFT;
2309
2310 if (sumhi > PNG_HIMASK)
2311 sum = PNG_MAXSUM;
2312 else
2313 sum = (sumhi << PNG_HISHIFT) + sumlo;
2314 }
2315#endif
2316
Guy Schalnate5a37791996-06-05 15:50:50 -05002317 if (sum < mins)
2318 {
2319 mins = sum;
2320 best_row = png_ptr->up_row;
2321 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002322 }
2323
Guy Schalnate5a37791996-06-05 15:50:50 -05002324 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002325 if (filter_to_do == PNG_FILTER_AVG)
2326 {
2327 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002328 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002329 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002330 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002331 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002332 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002333 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002334 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002335 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002336 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2337 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002338 }
2339 best_row = png_ptr->avg_row;
2340 }
2341
2342 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002343 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002344 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002345 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002346 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002347 int v;
2348
2349#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2350 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2351 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002352 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002353 png_uint_32 lmhi, lmlo;
2354 lmlo = lmins & PNG_LOMASK;
2355 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2356
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002357 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002359 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002361 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002362 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002363 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002364 PNG_WEIGHT_SHIFT;
2365 }
2366 }
2367
2368 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2369 PNG_COST_SHIFT;
2370 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2371 PNG_COST_SHIFT;
2372
2373 if (lmhi > PNG_HIMASK)
2374 lmins = PNG_MAXSUM;
2375 else
2376 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2377 }
2378#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002379
2380 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002381 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002382 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002383 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002384
2385 sum += (v < 128) ? v : 256 - v;
2386 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002387 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002388 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002389 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002390 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002391
2392 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002393
2394 if (sum > lmins) /* We are already worse, don't continue. */
2395 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002396 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002397
2398#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2399 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2400 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002401 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002402 png_uint_32 sumhi, sumlo;
2403 sumlo = sum & PNG_LOMASK;
2404 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2405
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002406 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002407 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002412 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002413 PNG_WEIGHT_SHIFT;
2414 }
2415 }
2416
2417 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2418 PNG_COST_SHIFT;
2419 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2420 PNG_COST_SHIFT;
2421
2422 if (sumhi > PNG_HIMASK)
2423 sum = PNG_MAXSUM;
2424 else
2425 sum = (sumhi << PNG_HISHIFT) + sumlo;
2426 }
2427#endif
2428
Guy Schalnate5a37791996-06-05 15:50:50 -05002429 if (sum < mins)
2430 {
2431 mins = sum;
2432 best_row = png_ptr->avg_row;
2433 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002434 }
2435
Andreas Dilger47a0c421997-05-16 02:46:07 -05002436 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002437 if (filter_to_do == PNG_FILTER_PAETH)
2438 {
2439 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002440 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002441 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002442 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002443 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002444 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002445 }
2446
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002448 {
2449 int a, b, c, pa, pb, pc, p;
2450
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002451 b = *pp++;
2452 c = *cp++;
2453 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002454
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002455 p = b - c;
2456 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002457
2458#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 pa = abs(p);
2460 pb = abs(pc);
2461 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002462#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002463 pa = p < 0 ? -p : p;
2464 pb = pc < 0 ? -pc : pc;
2465 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002466#endif
2467
2468 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2469
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002470 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002471 }
2472 best_row = png_ptr->paeth_row;
2473 }
2474
2475 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002476 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002477 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002478 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002479 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002480 int v;
2481
2482#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2483 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2484 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002485 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002486 png_uint_32 lmhi, lmlo;
2487 lmlo = lmins & PNG_LOMASK;
2488 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2489
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002490 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002491 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002492 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002493 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002494 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002495 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002496 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002497 PNG_WEIGHT_SHIFT;
2498 }
2499 }
2500
2501 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2502 PNG_COST_SHIFT;
2503 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2504 PNG_COST_SHIFT;
2505
2506 if (lmhi > PNG_HIMASK)
2507 lmins = PNG_MAXSUM;
2508 else
2509 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2510 }
2511#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002512
2513 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002514 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002515 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002517
2518 sum += (v < 128) ? v : 256 - v;
2519 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002520
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002521 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002522 {
2523 int a, b, c, pa, pb, pc, p;
2524
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002525 b = *pp++;
2526 c = *cp++;
2527 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002528
2529#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002530 p = b - c;
2531 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002532#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002533 pa = abs(p);
2534 pb = abs(pc);
2535 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002536#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002537 pa = p < 0 ? -p : p;
2538 pb = pc < 0 ? -pc : pc;
2539 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002540#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002541 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2542#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002543 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002544 pa = abs(p - a);
2545 pb = abs(p - b);
2546 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002547 if (pa <= pb && pa <= pc)
2548 p = a;
2549 else if (pb <= pc)
2550 p = b;
2551 else
2552 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002553#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002554
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002555 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002556
2557 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002558
2559 if (sum > lmins) /* We are already worse, don't continue. */
2560 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002561 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002562
2563#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2564 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2565 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002566 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002567 png_uint_32 sumhi, sumlo;
2568 sumlo = sum & PNG_LOMASK;
2569 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2570
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002571 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002572 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002573 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002574 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002575 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002576 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002577 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002578 PNG_WEIGHT_SHIFT;
2579 }
2580 }
2581
2582 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2583 PNG_COST_SHIFT;
2584 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2585 PNG_COST_SHIFT;
2586
2587 if (sumhi > PNG_HIMASK)
2588 sum = PNG_MAXSUM;
2589 else
2590 sum = (sumhi << PNG_HISHIFT) + sumlo;
2591 }
2592#endif
2593
Guy Schalnate5a37791996-06-05 15:50:50 -05002594 if (sum < mins)
2595 {
2596 best_row = png_ptr->paeth_row;
2597 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002598 }
2599
Andreas Dilger47a0c421997-05-16 02:46:07 -05002600 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002601
Guy Schalnate5a37791996-06-05 15:50:50 -05002602 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002603
2604#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2605 /* Save the type of filter we picked this time for future calculations */
2606 if (png_ptr->num_prev_filters > 0)
2607 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002608 int j;
2609 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002610 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002611 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002612 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002613 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002614 }
2615#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002616}
2617
2618
Andreas Dilger47a0c421997-05-16 02:46:07 -05002619/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002620void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002621png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2622{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002623 png_debug(1, "in png_write_filtered_row\n");
2624 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002625 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002626
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002627 png_ptr->zstream.next_in = filtered_row;
2628 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002629 /* repeat until we have compressed all the data */
2630 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002631 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002632 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002633
Guy Schalnate5a37791996-06-05 15:50:50 -05002634 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002635 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002636 /* check for compression errors */
2637 if (ret != Z_OK)
2638 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002639 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002640 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002641 else
2642 png_error(png_ptr, "zlib error");
2643 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002644
Guy Schalnate5a37791996-06-05 15:50:50 -05002645 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002646 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002647 {
2648 /* write the IDAT and reset the zlib output buffer */
2649 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002650 png_ptr->zstream.next_out = png_ptr->zbuf;
2651 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002652 }
2653 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002654 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002655
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002656 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002657 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002658 {
2659 png_bytep tptr;
2660
2661 tptr = png_ptr->prev_row;
2662 png_ptr->prev_row = png_ptr->row_buf;
2663 png_ptr->row_buf = tptr;
2664 }
2665
Guy Schalnate5a37791996-06-05 15:50:50 -05002666 /* finish row - updates counters and flushes zlib if last row */
2667 png_write_finish_row(png_ptr);
2668
2669#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2670 png_ptr->flush_rows++;
2671
2672 if (png_ptr->flush_dist > 0 &&
2673 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002674 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002675 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002676 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002677#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002678}
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06002679#endif /* PNG_WRITE_SUPPORTED */