rev |
line source |
nuclear@2
|
1
|
nuclear@2
|
2 /* pngwutil.c - utilities to write a PNG file
|
nuclear@2
|
3 *
|
nuclear@2
|
4 * Last changed in libpng 1.2.30 [August 15, 2008]
|
nuclear@2
|
5 * For conditions of distribution and use, see copyright notice in png.h
|
nuclear@2
|
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
|
nuclear@2
|
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
nuclear@2
|
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
nuclear@2
|
9 */
|
nuclear@2
|
10
|
nuclear@2
|
11 #define PNG_INTERNAL
|
nuclear@2
|
12 #include "png.h"
|
nuclear@2
|
13 #ifdef PNG_WRITE_SUPPORTED
|
nuclear@2
|
14
|
nuclear@2
|
15 /* Place a 32-bit number into a buffer in PNG byte order. We work
|
nuclear@2
|
16 * with unsigned numbers for convenience, although one supported
|
nuclear@2
|
17 * ancillary chunk uses signed (two's complement) numbers.
|
nuclear@2
|
18 */
|
nuclear@2
|
19 void PNGAPI
|
nuclear@2
|
20 png_save_uint_32(png_bytep buf, png_uint_32 i)
|
nuclear@2
|
21 {
|
nuclear@2
|
22 buf[0] = (png_byte)((i >> 24) & 0xff);
|
nuclear@2
|
23 buf[1] = (png_byte)((i >> 16) & 0xff);
|
nuclear@2
|
24 buf[2] = (png_byte)((i >> 8) & 0xff);
|
nuclear@2
|
25 buf[3] = (png_byte)(i & 0xff);
|
nuclear@2
|
26 }
|
nuclear@2
|
27
|
nuclear@2
|
28 /* The png_save_int_32 function assumes integers are stored in two's
|
nuclear@2
|
29 * complement format. If this isn't the case, then this routine needs to
|
nuclear@2
|
30 * be modified to write data in two's complement format.
|
nuclear@2
|
31 */
|
nuclear@2
|
32 void PNGAPI
|
nuclear@2
|
33 png_save_int_32(png_bytep buf, png_int_32 i)
|
nuclear@2
|
34 {
|
nuclear@2
|
35 buf[0] = (png_byte)((i >> 24) & 0xff);
|
nuclear@2
|
36 buf[1] = (png_byte)((i >> 16) & 0xff);
|
nuclear@2
|
37 buf[2] = (png_byte)((i >> 8) & 0xff);
|
nuclear@2
|
38 buf[3] = (png_byte)(i & 0xff);
|
nuclear@2
|
39 }
|
nuclear@2
|
40
|
nuclear@2
|
41 /* Place a 16-bit number into a buffer in PNG byte order.
|
nuclear@2
|
42 * The parameter is declared unsigned int, not png_uint_16,
|
nuclear@2
|
43 * just to avoid potential problems on pre-ANSI C compilers.
|
nuclear@2
|
44 */
|
nuclear@2
|
45 void PNGAPI
|
nuclear@2
|
46 png_save_uint_16(png_bytep buf, unsigned int i)
|
nuclear@2
|
47 {
|
nuclear@2
|
48 buf[0] = (png_byte)((i >> 8) & 0xff);
|
nuclear@2
|
49 buf[1] = (png_byte)(i & 0xff);
|
nuclear@2
|
50 }
|
nuclear@2
|
51
|
nuclear@2
|
52 /* Simple function to write the signature. If we have already written
|
nuclear@2
|
53 * the magic bytes of the signature, or more likely, the PNG stream is
|
nuclear@2
|
54 * being embedded into another stream and doesn't need its own signature,
|
nuclear@2
|
55 * we should call png_set_sig_bytes() to tell libpng how many of the
|
nuclear@2
|
56 * bytes have already been written.
|
nuclear@2
|
57 */
|
nuclear@2
|
58 void /* PRIVATE */
|
nuclear@2
|
59 png_write_sig(png_structp png_ptr)
|
nuclear@2
|
60 {
|
nuclear@2
|
61 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
|
nuclear@2
|
62
|
nuclear@2
|
63 /* write the rest of the 8 byte signature */
|
nuclear@2
|
64 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
|
nuclear@2
|
65 (png_size_t)(8 - png_ptr->sig_bytes));
|
nuclear@2
|
66 if (png_ptr->sig_bytes < 3)
|
nuclear@2
|
67 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
|
nuclear@2
|
68 }
|
nuclear@2
|
69
|
nuclear@2
|
70 /* Write a PNG chunk all at once. The type is an array of ASCII characters
|
nuclear@2
|
71 * representing the chunk name. The array must be at least 4 bytes in
|
nuclear@2
|
72 * length, and does not need to be null terminated. To be safe, pass the
|
nuclear@2
|
73 * pre-defined chunk names here, and if you need a new one, define it
|
nuclear@2
|
74 * where the others are defined. The length is the length of the data.
|
nuclear@2
|
75 * All the data must be present. If that is not possible, use the
|
nuclear@2
|
76 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
|
nuclear@2
|
77 * functions instead.
|
nuclear@2
|
78 */
|
nuclear@2
|
79 void PNGAPI
|
nuclear@2
|
80 png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
|
nuclear@2
|
81 png_bytep data, png_size_t length)
|
nuclear@2
|
82 {
|
nuclear@2
|
83 if (png_ptr == NULL) return;
|
nuclear@2
|
84 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
|
nuclear@2
|
85 png_write_chunk_data(png_ptr, data, (png_size_t)length);
|
nuclear@2
|
86 png_write_chunk_end(png_ptr);
|
nuclear@2
|
87 }
|
nuclear@2
|
88
|
nuclear@2
|
89 /* Write the start of a PNG chunk. The type is the chunk type.
|
nuclear@2
|
90 * The total_length is the sum of the lengths of all the data you will be
|
nuclear@2
|
91 * passing in png_write_chunk_data().
|
nuclear@2
|
92 */
|
nuclear@2
|
93 void PNGAPI
|
nuclear@2
|
94 png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
|
nuclear@2
|
95 png_uint_32 length)
|
nuclear@2
|
96 {
|
nuclear@2
|
97 png_byte buf[8];
|
nuclear@2
|
98
|
nuclear@2
|
99 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
|
nuclear@2
|
100 (unsigned long)length);
|
nuclear@2
|
101 if (png_ptr == NULL) return;
|
nuclear@2
|
102
|
nuclear@2
|
103 /* write the length and the chunk name */
|
nuclear@2
|
104 png_save_uint_32(buf, length);
|
nuclear@2
|
105 png_memcpy(buf + 4, chunk_name, 4);
|
nuclear@2
|
106 png_write_data(png_ptr, buf, (png_size_t)8);
|
nuclear@2
|
107 /* put the chunk name into png_ptr->chunk_name */
|
nuclear@2
|
108 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
|
nuclear@2
|
109 /* reset the crc and run it over the chunk name */
|
nuclear@2
|
110 png_reset_crc(png_ptr);
|
nuclear@2
|
111 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
|
nuclear@2
|
112 }
|
nuclear@2
|
113
|
nuclear@2
|
114 /* Write the data of a PNG chunk started with png_write_chunk_start().
|
nuclear@2
|
115 * Note that multiple calls to this function are allowed, and that the
|
nuclear@2
|
116 * sum of the lengths from these calls *must* add up to the total_length
|
nuclear@2
|
117 * given to png_write_chunk_start().
|
nuclear@2
|
118 */
|
nuclear@2
|
119 void PNGAPI
|
nuclear@2
|
120 png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
nuclear@2
|
121 {
|
nuclear@2
|
122 /* write the data, and run the CRC over it */
|
nuclear@2
|
123 if (png_ptr == NULL) return;
|
nuclear@2
|
124 if (data != NULL && length > 0)
|
nuclear@2
|
125 {
|
nuclear@2
|
126 png_write_data(png_ptr, data, length);
|
nuclear@2
|
127 /* update the CRC after writing the data,
|
nuclear@2
|
128 * in case that the user I/O routine alters it.
|
nuclear@2
|
129 */
|
nuclear@2
|
130 png_calculate_crc(png_ptr, data, length);
|
nuclear@2
|
131 }
|
nuclear@2
|
132 }
|
nuclear@2
|
133
|
nuclear@2
|
134 /* Finish a chunk started with png_write_chunk_start(). */
|
nuclear@2
|
135 void PNGAPI
|
nuclear@2
|
136 png_write_chunk_end(png_structp png_ptr)
|
nuclear@2
|
137 {
|
nuclear@2
|
138 png_byte buf[4];
|
nuclear@2
|
139
|
nuclear@2
|
140 if (png_ptr == NULL) return;
|
nuclear@2
|
141
|
nuclear@2
|
142 /* write the crc in a single operation */
|
nuclear@2
|
143 png_save_uint_32(buf, png_ptr->crc);
|
nuclear@2
|
144
|
nuclear@2
|
145 png_write_data(png_ptr, buf, (png_size_t)4);
|
nuclear@2
|
146 }
|
nuclear@2
|
147
|
nuclear@2
|
148 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
|
nuclear@2
|
149 /*
|
nuclear@2
|
150 * This pair of functions encapsulates the operation of (a) compressing a
|
nuclear@2
|
151 * text string, and (b) issuing it later as a series of chunk data writes.
|
nuclear@2
|
152 * The compression_state structure is shared context for these functions
|
nuclear@2
|
153 * set up by the caller in order to make the whole mess thread-safe.
|
nuclear@2
|
154 */
|
nuclear@2
|
155
|
nuclear@2
|
156 typedef struct
|
nuclear@2
|
157 {
|
nuclear@2
|
158 char *input; /* the uncompressed input data */
|
nuclear@2
|
159 int input_len; /* its length */
|
nuclear@2
|
160 int num_output_ptr; /* number of output pointers used */
|
nuclear@2
|
161 int max_output_ptr; /* size of output_ptr */
|
nuclear@2
|
162 png_charpp output_ptr; /* array of pointers to output */
|
nuclear@2
|
163 } compression_state;
|
nuclear@2
|
164
|
nuclear@2
|
165 /* compress given text into storage in the png_ptr structure */
|
nuclear@2
|
166 static int /* PRIVATE */
|
nuclear@2
|
167 png_text_compress(png_structp png_ptr,
|
nuclear@2
|
168 png_charp text, png_size_t text_len, int compression,
|
nuclear@2
|
169 compression_state *comp)
|
nuclear@2
|
170 {
|
nuclear@2
|
171 int ret;
|
nuclear@2
|
172
|
nuclear@2
|
173 comp->num_output_ptr = 0;
|
nuclear@2
|
174 comp->max_output_ptr = 0;
|
nuclear@2
|
175 comp->output_ptr = NULL;
|
nuclear@2
|
176 comp->input = NULL;
|
nuclear@2
|
177 comp->input_len = 0;
|
nuclear@2
|
178
|
nuclear@2
|
179 /* we may just want to pass the text right through */
|
nuclear@2
|
180 if (compression == PNG_TEXT_COMPRESSION_NONE)
|
nuclear@2
|
181 {
|
nuclear@2
|
182 comp->input = text;
|
nuclear@2
|
183 comp->input_len = text_len;
|
nuclear@2
|
184 return((int)text_len);
|
nuclear@2
|
185 }
|
nuclear@2
|
186
|
nuclear@2
|
187 if (compression >= PNG_TEXT_COMPRESSION_LAST)
|
nuclear@2
|
188 {
|
nuclear@2
|
189 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
|
nuclear@2
|
190 char msg[50];
|
nuclear@2
|
191 png_snprintf(msg, 50, "Unknown compression type %d", compression);
|
nuclear@2
|
192 png_warning(png_ptr, msg);
|
nuclear@2
|
193 #else
|
nuclear@2
|
194 png_warning(png_ptr, "Unknown compression type");
|
nuclear@2
|
195 #endif
|
nuclear@2
|
196 }
|
nuclear@2
|
197
|
nuclear@2
|
198 /* We can't write the chunk until we find out how much data we have,
|
nuclear@2
|
199 * which means we need to run the compressor first and save the
|
nuclear@2
|
200 * output. This shouldn't be a problem, as the vast majority of
|
nuclear@2
|
201 * comments should be reasonable, but we will set up an array of
|
nuclear@2
|
202 * malloc'd pointers to be sure.
|
nuclear@2
|
203 *
|
nuclear@2
|
204 * If we knew the application was well behaved, we could simplify this
|
nuclear@2
|
205 * greatly by assuming we can always malloc an output buffer large
|
nuclear@2
|
206 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
|
nuclear@2
|
207 * and malloc this directly. The only time this would be a bad idea is
|
nuclear@2
|
208 * if we can't malloc more than 64K and we have 64K of random input
|
nuclear@2
|
209 * data, or if the input string is incredibly large (although this
|
nuclear@2
|
210 * wouldn't cause a failure, just a slowdown due to swapping).
|
nuclear@2
|
211 */
|
nuclear@2
|
212
|
nuclear@2
|
213 /* set up the compression buffers */
|
nuclear@2
|
214 png_ptr->zstream.avail_in = (uInt)text_len;
|
nuclear@2
|
215 png_ptr->zstream.next_in = (Bytef *)text;
|
nuclear@2
|
216 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
nuclear@2
|
217 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
|
nuclear@2
|
218
|
nuclear@2
|
219 /* this is the same compression loop as in png_write_row() */
|
nuclear@2
|
220 do
|
nuclear@2
|
221 {
|
nuclear@2
|
222 /* compress the data */
|
nuclear@2
|
223 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
|
nuclear@2
|
224 if (ret != Z_OK)
|
nuclear@2
|
225 {
|
nuclear@2
|
226 /* error */
|
nuclear@2
|
227 if (png_ptr->zstream.msg != NULL)
|
nuclear@2
|
228 png_error(png_ptr, png_ptr->zstream.msg);
|
nuclear@2
|
229 else
|
nuclear@2
|
230 png_error(png_ptr, "zlib error");
|
nuclear@2
|
231 }
|
nuclear@2
|
232 /* check to see if we need more room */
|
nuclear@2
|
233 if (!(png_ptr->zstream.avail_out))
|
nuclear@2
|
234 {
|
nuclear@2
|
235 /* make sure the output array has room */
|
nuclear@2
|
236 if (comp->num_output_ptr >= comp->max_output_ptr)
|
nuclear@2
|
237 {
|
nuclear@2
|
238 int old_max;
|
nuclear@2
|
239
|
nuclear@2
|
240 old_max = comp->max_output_ptr;
|
nuclear@2
|
241 comp->max_output_ptr = comp->num_output_ptr + 4;
|
nuclear@2
|
242 if (comp->output_ptr != NULL)
|
nuclear@2
|
243 {
|
nuclear@2
|
244 png_charpp old_ptr;
|
nuclear@2
|
245
|
nuclear@2
|
246 old_ptr = comp->output_ptr;
|
nuclear@2
|
247 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
|
nuclear@2
|
248 (png_uint_32)
|
nuclear@2
|
249 (comp->max_output_ptr * png_sizeof(png_charpp)));
|
nuclear@2
|
250 png_memcpy(comp->output_ptr, old_ptr, old_max
|
nuclear@2
|
251 * png_sizeof(png_charp));
|
nuclear@2
|
252 png_free(png_ptr, old_ptr);
|
nuclear@2
|
253 }
|
nuclear@2
|
254 else
|
nuclear@2
|
255 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
|
nuclear@2
|
256 (png_uint_32)
|
nuclear@2
|
257 (comp->max_output_ptr * png_sizeof(png_charp)));
|
nuclear@2
|
258 }
|
nuclear@2
|
259
|
nuclear@2
|
260 /* save the data */
|
nuclear@2
|
261 comp->output_ptr[comp->num_output_ptr] =
|
nuclear@2
|
262 (png_charp)png_malloc(png_ptr,
|
nuclear@2
|
263 (png_uint_32)png_ptr->zbuf_size);
|
nuclear@2
|
264 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
|
nuclear@2
|
265 png_ptr->zbuf_size);
|
nuclear@2
|
266 comp->num_output_ptr++;
|
nuclear@2
|
267
|
nuclear@2
|
268 /* and reset the buffer */
|
nuclear@2
|
269 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
nuclear@2
|
270 png_ptr->zstream.next_out = png_ptr->zbuf;
|
nuclear@2
|
271 }
|
nuclear@2
|
272 /* continue until we don't have any more to compress */
|
nuclear@2
|
273 } while (png_ptr->zstream.avail_in);
|
nuclear@2
|
274
|
nuclear@2
|
275 /* finish the compression */
|
nuclear@2
|
276 do
|
nuclear@2
|
277 {
|
nuclear@2
|
278 /* tell zlib we are finished */
|
nuclear@2
|
279 ret = deflate(&png_ptr->zstream, Z_FINISH);
|
nuclear@2
|
280
|
nuclear@2
|
281 if (ret == Z_OK)
|
nuclear@2
|
282 {
|
nuclear@2
|
283 /* check to see if we need more room */
|
nuclear@2
|
284 if (!(png_ptr->zstream.avail_out))
|
nuclear@2
|
285 {
|
nuclear@2
|
286 /* check to make sure our output array has room */
|
nuclear@2
|
287 if (comp->num_output_ptr >= comp->max_output_ptr)
|
nuclear@2
|
288 {
|
nuclear@2
|
289 int old_max;
|
nuclear@2
|
290
|
nuclear@2
|
291 old_max = comp->max_output_ptr;
|
nuclear@2
|
292 comp->max_output_ptr = comp->num_output_ptr + 4;
|
nuclear@2
|
293 if (comp->output_ptr != NULL)
|
nuclear@2
|
294 {
|
nuclear@2
|
295 png_charpp old_ptr;
|
nuclear@2
|
296
|
nuclear@2
|
297 old_ptr = comp->output_ptr;
|
nuclear@2
|
298 /* This could be optimized to realloc() */
|
nuclear@2
|
299 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
|
nuclear@2
|
300 (png_uint_32)(comp->max_output_ptr *
|
nuclear@2
|
301 png_sizeof(png_charp)));
|
nuclear@2
|
302 png_memcpy(comp->output_ptr, old_ptr,
|
nuclear@2
|
303 old_max * png_sizeof(png_charp));
|
nuclear@2
|
304 png_free(png_ptr, old_ptr);
|
nuclear@2
|
305 }
|
nuclear@2
|
306 else
|
nuclear@2
|
307 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
|
nuclear@2
|
308 (png_uint_32)(comp->max_output_ptr *
|
nuclear@2
|
309 png_sizeof(png_charp)));
|
nuclear@2
|
310 }
|
nuclear@2
|
311
|
nuclear@2
|
312 /* save off the data */
|
nuclear@2
|
313 comp->output_ptr[comp->num_output_ptr] =
|
nuclear@2
|
314 (png_charp)png_malloc(png_ptr,
|
nuclear@2
|
315 (png_uint_32)png_ptr->zbuf_size);
|
nuclear@2
|
316 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
|
nuclear@2
|
317 png_ptr->zbuf_size);
|
nuclear@2
|
318 comp->num_output_ptr++;
|
nuclear@2
|
319
|
nuclear@2
|
320 /* and reset the buffer pointers */
|
nuclear@2
|
321 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
nuclear@2
|
322 png_ptr->zstream.next_out = png_ptr->zbuf;
|
nuclear@2
|
323 }
|
nuclear@2
|
324 }
|
nuclear@2
|
325 else if (ret != Z_STREAM_END)
|
nuclear@2
|
326 {
|
nuclear@2
|
327 /* we got an error */
|
nuclear@2
|
328 if (png_ptr->zstream.msg != NULL)
|
nuclear@2
|
329 png_error(png_ptr, png_ptr->zstream.msg);
|
nuclear@2
|
330 else
|
nuclear@2
|
331 png_error(png_ptr, "zlib error");
|
nuclear@2
|
332 }
|
nuclear@2
|
333 } while (ret != Z_STREAM_END);
|
nuclear@2
|
334
|
nuclear@2
|
335 /* text length is number of buffers plus last buffer */
|
nuclear@2
|
336 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
|
nuclear@2
|
337 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
|
nuclear@2
|
338 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
|
nuclear@2
|
339
|
nuclear@2
|
340 return((int)text_len);
|
nuclear@2
|
341 }
|
nuclear@2
|
342
|
nuclear@2
|
343 /* ship the compressed text out via chunk writes */
|
nuclear@2
|
344 static void /* PRIVATE */
|
nuclear@2
|
345 png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
|
nuclear@2
|
346 {
|
nuclear@2
|
347 int i;
|
nuclear@2
|
348
|
nuclear@2
|
349 /* handle the no-compression case */
|
nuclear@2
|
350 if (comp->input)
|
nuclear@2
|
351 {
|
nuclear@2
|
352 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
|
nuclear@2
|
353 (png_size_t)comp->input_len);
|
nuclear@2
|
354 return;
|
nuclear@2
|
355 }
|
nuclear@2
|
356
|
nuclear@2
|
357 /* write saved output buffers, if any */
|
nuclear@2
|
358 for (i = 0; i < comp->num_output_ptr; i++)
|
nuclear@2
|
359 {
|
nuclear@2
|
360 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
|
nuclear@2
|
361 (png_size_t)png_ptr->zbuf_size);
|
nuclear@2
|
362 png_free(png_ptr, comp->output_ptr[i]);
|
nuclear@2
|
363 comp->output_ptr[i]=NULL;
|
nuclear@2
|
364 }
|
nuclear@2
|
365 if (comp->max_output_ptr != 0)
|
nuclear@2
|
366 png_free(png_ptr, comp->output_ptr);
|
nuclear@2
|
367 comp->output_ptr=NULL;
|
nuclear@2
|
368 /* write anything left in zbuf */
|
nuclear@2
|
369 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
|
nuclear@2
|
370 png_write_chunk_data(png_ptr, png_ptr->zbuf,
|
nuclear@2
|
371 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
|
nuclear@2
|
372
|
nuclear@2
|
373 /* reset zlib for another zTXt/iTXt or image data */
|
nuclear@2
|
374 deflateReset(&png_ptr->zstream);
|
nuclear@2
|
375 png_ptr->zstream.data_type = Z_BINARY;
|
nuclear@2
|
376 }
|
nuclear@2
|
377 #endif
|
nuclear@2
|
378
|
nuclear@2
|
379 /* Write the IHDR chunk, and update the png_struct with the necessary
|
nuclear@2
|
380 * information. Note that the rest of this code depends upon this
|
nuclear@2
|
381 * information being correct.
|
nuclear@2
|
382 */
|
nuclear@2
|
383 void /* PRIVATE */
|
nuclear@2
|
384 png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
|
nuclear@2
|
385 int bit_depth, int color_type, int compression_type, int filter_type,
|
nuclear@2
|
386 int interlace_type)
|
nuclear@2
|
387 {
|
nuclear@2
|
388 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
389 PNG_IHDR;
|
nuclear@2
|
390 #endif
|
nuclear@2
|
391 int ret;
|
nuclear@2
|
392
|
nuclear@2
|
393 png_byte buf[13]; /* buffer to store the IHDR info */
|
nuclear@2
|
394
|
nuclear@2
|
395 png_debug(1, "in png_write_IHDR\n");
|
nuclear@2
|
396 /* Check that we have valid input data from the application info */
|
nuclear@2
|
397 switch (color_type)
|
nuclear@2
|
398 {
|
nuclear@2
|
399 case PNG_COLOR_TYPE_GRAY:
|
nuclear@2
|
400 switch (bit_depth)
|
nuclear@2
|
401 {
|
nuclear@2
|
402 case 1:
|
nuclear@2
|
403 case 2:
|
nuclear@2
|
404 case 4:
|
nuclear@2
|
405 case 8:
|
nuclear@2
|
406 case 16: png_ptr->channels = 1; break;
|
nuclear@2
|
407 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
|
nuclear@2
|
408 }
|
nuclear@2
|
409 break;
|
nuclear@2
|
410 case PNG_COLOR_TYPE_RGB:
|
nuclear@2
|
411 if (bit_depth != 8 && bit_depth != 16)
|
nuclear@2
|
412 png_error(png_ptr, "Invalid bit depth for RGB image");
|
nuclear@2
|
413 png_ptr->channels = 3;
|
nuclear@2
|
414 break;
|
nuclear@2
|
415 case PNG_COLOR_TYPE_PALETTE:
|
nuclear@2
|
416 switch (bit_depth)
|
nuclear@2
|
417 {
|
nuclear@2
|
418 case 1:
|
nuclear@2
|
419 case 2:
|
nuclear@2
|
420 case 4:
|
nuclear@2
|
421 case 8: png_ptr->channels = 1; break;
|
nuclear@2
|
422 default: png_error(png_ptr, "Invalid bit depth for paletted image");
|
nuclear@2
|
423 }
|
nuclear@2
|
424 break;
|
nuclear@2
|
425 case PNG_COLOR_TYPE_GRAY_ALPHA:
|
nuclear@2
|
426 if (bit_depth != 8 && bit_depth != 16)
|
nuclear@2
|
427 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
|
nuclear@2
|
428 png_ptr->channels = 2;
|
nuclear@2
|
429 break;
|
nuclear@2
|
430 case PNG_COLOR_TYPE_RGB_ALPHA:
|
nuclear@2
|
431 if (bit_depth != 8 && bit_depth != 16)
|
nuclear@2
|
432 png_error(png_ptr, "Invalid bit depth for RGBA image");
|
nuclear@2
|
433 png_ptr->channels = 4;
|
nuclear@2
|
434 break;
|
nuclear@2
|
435 default:
|
nuclear@2
|
436 png_error(png_ptr, "Invalid image color type specified");
|
nuclear@2
|
437 }
|
nuclear@2
|
438
|
nuclear@2
|
439 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
|
nuclear@2
|
440 {
|
nuclear@2
|
441 png_warning(png_ptr, "Invalid compression type specified");
|
nuclear@2
|
442 compression_type = PNG_COMPRESSION_TYPE_BASE;
|
nuclear@2
|
443 }
|
nuclear@2
|
444
|
nuclear@2
|
445 /* Write filter_method 64 (intrapixel differencing) only if
|
nuclear@2
|
446 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
|
nuclear@2
|
447 * 2. Libpng did not write a PNG signature (this filter_method is only
|
nuclear@2
|
448 * used in PNG datastreams that are embedded in MNG datastreams) and
|
nuclear@2
|
449 * 3. The application called png_permit_mng_features with a mask that
|
nuclear@2
|
450 * included PNG_FLAG_MNG_FILTER_64 and
|
nuclear@2
|
451 * 4. The filter_method is 64 and
|
nuclear@2
|
452 * 5. The color_type is RGB or RGBA
|
nuclear@2
|
453 */
|
nuclear@2
|
454 if (
|
nuclear@2
|
455 #if defined(PNG_MNG_FEATURES_SUPPORTED)
|
nuclear@2
|
456 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
|
nuclear@2
|
457 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
|
nuclear@2
|
458 (color_type == PNG_COLOR_TYPE_RGB ||
|
nuclear@2
|
459 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
|
nuclear@2
|
460 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
|
nuclear@2
|
461 #endif
|
nuclear@2
|
462 filter_type != PNG_FILTER_TYPE_BASE)
|
nuclear@2
|
463 {
|
nuclear@2
|
464 png_warning(png_ptr, "Invalid filter type specified");
|
nuclear@2
|
465 filter_type = PNG_FILTER_TYPE_BASE;
|
nuclear@2
|
466 }
|
nuclear@2
|
467
|
nuclear@2
|
468 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
nuclear@2
|
469 if (interlace_type != PNG_INTERLACE_NONE &&
|
nuclear@2
|
470 interlace_type != PNG_INTERLACE_ADAM7)
|
nuclear@2
|
471 {
|
nuclear@2
|
472 png_warning(png_ptr, "Invalid interlace type specified");
|
nuclear@2
|
473 interlace_type = PNG_INTERLACE_ADAM7;
|
nuclear@2
|
474 }
|
nuclear@2
|
475 #else
|
nuclear@2
|
476 interlace_type=PNG_INTERLACE_NONE;
|
nuclear@2
|
477 #endif
|
nuclear@2
|
478
|
nuclear@2
|
479 /* save off the relevent information */
|
nuclear@2
|
480 png_ptr->bit_depth = (png_byte)bit_depth;
|
nuclear@2
|
481 png_ptr->color_type = (png_byte)color_type;
|
nuclear@2
|
482 png_ptr->interlaced = (png_byte)interlace_type;
|
nuclear@2
|
483 #if defined(PNG_MNG_FEATURES_SUPPORTED)
|
nuclear@2
|
484 png_ptr->filter_type = (png_byte)filter_type;
|
nuclear@2
|
485 #endif
|
nuclear@2
|
486 png_ptr->compression_type = (png_byte)compression_type;
|
nuclear@2
|
487 png_ptr->width = width;
|
nuclear@2
|
488 png_ptr->height = height;
|
nuclear@2
|
489
|
nuclear@2
|
490 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
|
nuclear@2
|
491 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
|
nuclear@2
|
492 /* set the usr info, so any transformations can modify it */
|
nuclear@2
|
493 png_ptr->usr_width = png_ptr->width;
|
nuclear@2
|
494 png_ptr->usr_bit_depth = png_ptr->bit_depth;
|
nuclear@2
|
495 png_ptr->usr_channels = png_ptr->channels;
|
nuclear@2
|
496
|
nuclear@2
|
497 /* pack the header information into the buffer */
|
nuclear@2
|
498 png_save_uint_32(buf, width);
|
nuclear@2
|
499 png_save_uint_32(buf + 4, height);
|
nuclear@2
|
500 buf[8] = (png_byte)bit_depth;
|
nuclear@2
|
501 buf[9] = (png_byte)color_type;
|
nuclear@2
|
502 buf[10] = (png_byte)compression_type;
|
nuclear@2
|
503 buf[11] = (png_byte)filter_type;
|
nuclear@2
|
504 buf[12] = (png_byte)interlace_type;
|
nuclear@2
|
505
|
nuclear@2
|
506 /* write the chunk */
|
nuclear@2
|
507 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
|
nuclear@2
|
508
|
nuclear@2
|
509 /* initialize zlib with PNG info */
|
nuclear@2
|
510 png_ptr->zstream.zalloc = png_zalloc;
|
nuclear@2
|
511 png_ptr->zstream.zfree = png_zfree;
|
nuclear@2
|
512 png_ptr->zstream.opaque = (voidpf)png_ptr;
|
nuclear@2
|
513 if (!(png_ptr->do_filter))
|
nuclear@2
|
514 {
|
nuclear@2
|
515 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
|
nuclear@2
|
516 png_ptr->bit_depth < 8)
|
nuclear@2
|
517 png_ptr->do_filter = PNG_FILTER_NONE;
|
nuclear@2
|
518 else
|
nuclear@2
|
519 png_ptr->do_filter = PNG_ALL_FILTERS;
|
nuclear@2
|
520 }
|
nuclear@2
|
521 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
|
nuclear@2
|
522 {
|
nuclear@2
|
523 if (png_ptr->do_filter != PNG_FILTER_NONE)
|
nuclear@2
|
524 png_ptr->zlib_strategy = Z_FILTERED;
|
nuclear@2
|
525 else
|
nuclear@2
|
526 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
|
nuclear@2
|
527 }
|
nuclear@2
|
528 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
|
nuclear@2
|
529 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
|
nuclear@2
|
530 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
|
nuclear@2
|
531 png_ptr->zlib_mem_level = 8;
|
nuclear@2
|
532 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
|
nuclear@2
|
533 png_ptr->zlib_window_bits = 15;
|
nuclear@2
|
534 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
|
nuclear@2
|
535 png_ptr->zlib_method = 8;
|
nuclear@2
|
536 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
|
nuclear@2
|
537 png_ptr->zlib_method, png_ptr->zlib_window_bits,
|
nuclear@2
|
538 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
|
nuclear@2
|
539 if (ret != Z_OK)
|
nuclear@2
|
540 {
|
nuclear@2
|
541 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
|
nuclear@2
|
542 "zlib failed to initialize compressor -- version error");
|
nuclear@2
|
543 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
|
nuclear@2
|
544 "zlib failed to initialize compressor -- stream error");
|
nuclear@2
|
545 if (ret == Z_MEM_ERROR) png_error(png_ptr,
|
nuclear@2
|
546 "zlib failed to initialize compressor -- mem error");
|
nuclear@2
|
547 png_error(png_ptr, "zlib failed to initialize compressor");
|
nuclear@2
|
548 }
|
nuclear@2
|
549 png_ptr->zstream.next_out = png_ptr->zbuf;
|
nuclear@2
|
550 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
nuclear@2
|
551 /* libpng is not interested in zstream.data_type */
|
nuclear@2
|
552 /* set it to a predefined value, to avoid its evaluation inside zlib */
|
nuclear@2
|
553 png_ptr->zstream.data_type = Z_BINARY;
|
nuclear@2
|
554
|
nuclear@2
|
555 png_ptr->mode = PNG_HAVE_IHDR;
|
nuclear@2
|
556 }
|
nuclear@2
|
557
|
nuclear@2
|
558 /* write the palette. We are careful not to trust png_color to be in the
|
nuclear@2
|
559 * correct order for PNG, so people can redefine it to any convenient
|
nuclear@2
|
560 * structure.
|
nuclear@2
|
561 */
|
nuclear@2
|
562 void /* PRIVATE */
|
nuclear@2
|
563 png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
|
nuclear@2
|
564 {
|
nuclear@2
|
565 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
566 PNG_PLTE;
|
nuclear@2
|
567 #endif
|
nuclear@2
|
568 png_uint_32 i;
|
nuclear@2
|
569 png_colorp pal_ptr;
|
nuclear@2
|
570 png_byte buf[3];
|
nuclear@2
|
571
|
nuclear@2
|
572 png_debug(1, "in png_write_PLTE\n");
|
nuclear@2
|
573 if ((
|
nuclear@2
|
574 #if defined(PNG_MNG_FEATURES_SUPPORTED)
|
nuclear@2
|
575 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
|
nuclear@2
|
576 #endif
|
nuclear@2
|
577 num_pal == 0) || num_pal > 256)
|
nuclear@2
|
578 {
|
nuclear@2
|
579 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
nuclear@2
|
580 {
|
nuclear@2
|
581 png_error(png_ptr, "Invalid number of colors in palette");
|
nuclear@2
|
582 }
|
nuclear@2
|
583 else
|
nuclear@2
|
584 {
|
nuclear@2
|
585 png_warning(png_ptr, "Invalid number of colors in palette");
|
nuclear@2
|
586 return;
|
nuclear@2
|
587 }
|
nuclear@2
|
588 }
|
nuclear@2
|
589
|
nuclear@2
|
590 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
|
nuclear@2
|
591 {
|
nuclear@2
|
592 png_warning(png_ptr,
|
nuclear@2
|
593 "Ignoring request to write a PLTE chunk in grayscale PNG");
|
nuclear@2
|
594 return;
|
nuclear@2
|
595 }
|
nuclear@2
|
596
|
nuclear@2
|
597 png_ptr->num_palette = (png_uint_16)num_pal;
|
nuclear@2
|
598 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
|
nuclear@2
|
599
|
nuclear@2
|
600 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
|
nuclear@2
|
601 (png_uint_32)(num_pal * 3));
|
nuclear@2
|
602 #ifndef PNG_NO_POINTER_INDEXING
|
nuclear@2
|
603 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
|
nuclear@2
|
604 {
|
nuclear@2
|
605 buf[0] = pal_ptr->red;
|
nuclear@2
|
606 buf[1] = pal_ptr->green;
|
nuclear@2
|
607 buf[2] = pal_ptr->blue;
|
nuclear@2
|
608 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
|
nuclear@2
|
609 }
|
nuclear@2
|
610 #else
|
nuclear@2
|
611 /* This is a little slower but some buggy compilers need to do this instead */
|
nuclear@2
|
612 pal_ptr=palette;
|
nuclear@2
|
613 for (i = 0; i < num_pal; i++)
|
nuclear@2
|
614 {
|
nuclear@2
|
615 buf[0] = pal_ptr[i].red;
|
nuclear@2
|
616 buf[1] = pal_ptr[i].green;
|
nuclear@2
|
617 buf[2] = pal_ptr[i].blue;
|
nuclear@2
|
618 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
|
nuclear@2
|
619 }
|
nuclear@2
|
620 #endif
|
nuclear@2
|
621 png_write_chunk_end(png_ptr);
|
nuclear@2
|
622 png_ptr->mode |= PNG_HAVE_PLTE;
|
nuclear@2
|
623 }
|
nuclear@2
|
624
|
nuclear@2
|
625 /* write an IDAT chunk */
|
nuclear@2
|
626 void /* PRIVATE */
|
nuclear@2
|
627 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
|
nuclear@2
|
628 {
|
nuclear@2
|
629 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
630 PNG_IDAT;
|
nuclear@2
|
631 #endif
|
nuclear@2
|
632 png_debug(1, "in png_write_IDAT\n");
|
nuclear@2
|
633
|
nuclear@2
|
634 /* Optimize the CMF field in the zlib stream. */
|
nuclear@2
|
635 /* This hack of the zlib stream is compliant to the stream specification. */
|
nuclear@2
|
636 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
|
nuclear@2
|
637 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
|
nuclear@2
|
638 {
|
nuclear@2
|
639 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
|
nuclear@2
|
640 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
|
nuclear@2
|
641 {
|
nuclear@2
|
642 /* Avoid memory underflows and multiplication overflows. */
|
nuclear@2
|
643 /* The conditions below are practically always satisfied;
|
nuclear@2
|
644 however, they still must be checked. */
|
nuclear@2
|
645 if (length >= 2 &&
|
nuclear@2
|
646 png_ptr->height < 16384 && png_ptr->width < 16384)
|
nuclear@2
|
647 {
|
nuclear@2
|
648 png_uint_32 uncompressed_idat_size = png_ptr->height *
|
nuclear@2
|
649 ((png_ptr->width *
|
nuclear@2
|
650 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
|
nuclear@2
|
651 unsigned int z_cinfo = z_cmf >> 4;
|
nuclear@2
|
652 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
|
nuclear@2
|
653 while (uncompressed_idat_size <= half_z_window_size &&
|
nuclear@2
|
654 half_z_window_size >= 256)
|
nuclear@2
|
655 {
|
nuclear@2
|
656 z_cinfo--;
|
nuclear@2
|
657 half_z_window_size >>= 1;
|
nuclear@2
|
658 }
|
nuclear@2
|
659 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
|
nuclear@2
|
660 if (data[0] != (png_byte)z_cmf)
|
nuclear@2
|
661 {
|
nuclear@2
|
662 data[0] = (png_byte)z_cmf;
|
nuclear@2
|
663 data[1] &= 0xe0;
|
nuclear@2
|
664 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
|
nuclear@2
|
665 }
|
nuclear@2
|
666 }
|
nuclear@2
|
667 }
|
nuclear@2
|
668 else
|
nuclear@2
|
669 png_error(png_ptr,
|
nuclear@2
|
670 "Invalid zlib compression method or flags in IDAT");
|
nuclear@2
|
671 }
|
nuclear@2
|
672
|
nuclear@2
|
673 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
|
nuclear@2
|
674 png_ptr->mode |= PNG_HAVE_IDAT;
|
nuclear@2
|
675 }
|
nuclear@2
|
676
|
nuclear@2
|
677 /* write an IEND chunk */
|
nuclear@2
|
678 void /* PRIVATE */
|
nuclear@2
|
679 png_write_IEND(png_structp png_ptr)
|
nuclear@2
|
680 {
|
nuclear@2
|
681 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
682 PNG_IEND;
|
nuclear@2
|
683 #endif
|
nuclear@2
|
684 png_debug(1, "in png_write_IEND\n");
|
nuclear@2
|
685 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
|
nuclear@2
|
686 (png_size_t)0);
|
nuclear@2
|
687 png_ptr->mode |= PNG_HAVE_IEND;
|
nuclear@2
|
688 }
|
nuclear@2
|
689
|
nuclear@2
|
690 #if defined(PNG_WRITE_gAMA_SUPPORTED)
|
nuclear@2
|
691 /* write a gAMA chunk */
|
nuclear@2
|
692 #ifdef PNG_FLOATING_POINT_SUPPORTED
|
nuclear@2
|
693 void /* PRIVATE */
|
nuclear@2
|
694 png_write_gAMA(png_structp png_ptr, double file_gamma)
|
nuclear@2
|
695 {
|
nuclear@2
|
696 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
697 PNG_gAMA;
|
nuclear@2
|
698 #endif
|
nuclear@2
|
699 png_uint_32 igamma;
|
nuclear@2
|
700 png_byte buf[4];
|
nuclear@2
|
701
|
nuclear@2
|
702 png_debug(1, "in png_write_gAMA\n");
|
nuclear@2
|
703 /* file_gamma is saved in 1/100,000ths */
|
nuclear@2
|
704 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
|
nuclear@2
|
705 png_save_uint_32(buf, igamma);
|
nuclear@2
|
706 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
|
nuclear@2
|
707 }
|
nuclear@2
|
708 #endif
|
nuclear@2
|
709 #ifdef PNG_FIXED_POINT_SUPPORTED
|
nuclear@2
|
710 void /* PRIVATE */
|
nuclear@2
|
711 png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
|
nuclear@2
|
712 {
|
nuclear@2
|
713 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
714 PNG_gAMA;
|
nuclear@2
|
715 #endif
|
nuclear@2
|
716 png_byte buf[4];
|
nuclear@2
|
717
|
nuclear@2
|
718 png_debug(1, "in png_write_gAMA\n");
|
nuclear@2
|
719 /* file_gamma is saved in 1/100,000ths */
|
nuclear@2
|
720 png_save_uint_32(buf, (png_uint_32)file_gamma);
|
nuclear@2
|
721 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
|
nuclear@2
|
722 }
|
nuclear@2
|
723 #endif
|
nuclear@2
|
724 #endif
|
nuclear@2
|
725
|
nuclear@2
|
726 #if defined(PNG_WRITE_sRGB_SUPPORTED)
|
nuclear@2
|
727 /* write a sRGB chunk */
|
nuclear@2
|
728 void /* PRIVATE */
|
nuclear@2
|
729 png_write_sRGB(png_structp png_ptr, int srgb_intent)
|
nuclear@2
|
730 {
|
nuclear@2
|
731 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
732 PNG_sRGB;
|
nuclear@2
|
733 #endif
|
nuclear@2
|
734 png_byte buf[1];
|
nuclear@2
|
735
|
nuclear@2
|
736 png_debug(1, "in png_write_sRGB\n");
|
nuclear@2
|
737 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
|
nuclear@2
|
738 png_warning(png_ptr,
|
nuclear@2
|
739 "Invalid sRGB rendering intent specified");
|
nuclear@2
|
740 buf[0]=(png_byte)srgb_intent;
|
nuclear@2
|
741 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
|
nuclear@2
|
742 }
|
nuclear@2
|
743 #endif
|
nuclear@2
|
744
|
nuclear@2
|
745 #if defined(PNG_WRITE_iCCP_SUPPORTED)
|
nuclear@2
|
746 /* write an iCCP chunk */
|
nuclear@2
|
747 void /* PRIVATE */
|
nuclear@2
|
748 png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
|
nuclear@2
|
749 png_charp profile, int profile_len)
|
nuclear@2
|
750 {
|
nuclear@2
|
751 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
752 PNG_iCCP;
|
nuclear@2
|
753 #endif
|
nuclear@2
|
754 png_size_t name_len;
|
nuclear@2
|
755 png_charp new_name;
|
nuclear@2
|
756 compression_state comp;
|
nuclear@2
|
757 int embedded_profile_len = 0;
|
nuclear@2
|
758
|
nuclear@2
|
759 png_debug(1, "in png_write_iCCP\n");
|
nuclear@2
|
760
|
nuclear@2
|
761 comp.num_output_ptr = 0;
|
nuclear@2
|
762 comp.max_output_ptr = 0;
|
nuclear@2
|
763 comp.output_ptr = NULL;
|
nuclear@2
|
764 comp.input = NULL;
|
nuclear@2
|
765 comp.input_len = 0;
|
nuclear@2
|
766
|
nuclear@2
|
767 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
|
nuclear@2
|
768 &new_name)) == 0)
|
nuclear@2
|
769 {
|
nuclear@2
|
770 png_warning(png_ptr, "Empty keyword in iCCP chunk");
|
nuclear@2
|
771 return;
|
nuclear@2
|
772 }
|
nuclear@2
|
773
|
nuclear@2
|
774 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
|
nuclear@2
|
775 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
|
nuclear@2
|
776
|
nuclear@2
|
777 if (profile == NULL)
|
nuclear@2
|
778 profile_len = 0;
|
nuclear@2
|
779
|
nuclear@2
|
780 if (profile_len > 3)
|
nuclear@2
|
781 embedded_profile_len =
|
nuclear@2
|
782 ((*( (png_bytep)profile ))<<24) |
|
nuclear@2
|
783 ((*( (png_bytep)profile + 1))<<16) |
|
nuclear@2
|
784 ((*( (png_bytep)profile + 2))<< 8) |
|
nuclear@2
|
785 ((*( (png_bytep)profile + 3)) );
|
nuclear@2
|
786
|
nuclear@2
|
787 if (profile_len < embedded_profile_len)
|
nuclear@2
|
788 {
|
nuclear@2
|
789 png_warning(png_ptr,
|
nuclear@2
|
790 "Embedded profile length too large in iCCP chunk");
|
nuclear@2
|
791 return;
|
nuclear@2
|
792 }
|
nuclear@2
|
793
|
nuclear@2
|
794 if (profile_len > embedded_profile_len)
|
nuclear@2
|
795 {
|
nuclear@2
|
796 png_warning(png_ptr,
|
nuclear@2
|
797 "Truncating profile to actual length in iCCP chunk");
|
nuclear@2
|
798 profile_len = embedded_profile_len;
|
nuclear@2
|
799 }
|
nuclear@2
|
800
|
nuclear@2
|
801 if (profile_len)
|
nuclear@2
|
802 profile_len = png_text_compress(png_ptr, profile,
|
nuclear@2
|
803 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
|
nuclear@2
|
804
|
nuclear@2
|
805 /* make sure we include the NULL after the name and the compression type */
|
nuclear@2
|
806 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
|
nuclear@2
|
807 (png_uint_32)(name_len + profile_len + 2));
|
nuclear@2
|
808 new_name[name_len + 1] = 0x00;
|
nuclear@2
|
809 png_write_chunk_data(png_ptr, (png_bytep)new_name,
|
nuclear@2
|
810 (png_size_t)(name_len + 2));
|
nuclear@2
|
811
|
nuclear@2
|
812 if (profile_len)
|
nuclear@2
|
813 png_write_compressed_data_out(png_ptr, &comp);
|
nuclear@2
|
814
|
nuclear@2
|
815 png_write_chunk_end(png_ptr);
|
nuclear@2
|
816 png_free(png_ptr, new_name);
|
nuclear@2
|
817 }
|
nuclear@2
|
818 #endif
|
nuclear@2
|
819
|
nuclear@2
|
820 #if defined(PNG_WRITE_sPLT_SUPPORTED)
|
nuclear@2
|
821 /* write a sPLT chunk */
|
nuclear@2
|
822 void /* PRIVATE */
|
nuclear@2
|
823 png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
|
nuclear@2
|
824 {
|
nuclear@2
|
825 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
826 PNG_sPLT;
|
nuclear@2
|
827 #endif
|
nuclear@2
|
828 png_size_t name_len;
|
nuclear@2
|
829 png_charp new_name;
|
nuclear@2
|
830 png_byte entrybuf[10];
|
nuclear@2
|
831 int entry_size = (spalette->depth == 8 ? 6 : 10);
|
nuclear@2
|
832 int palette_size = entry_size * spalette->nentries;
|
nuclear@2
|
833 png_sPLT_entryp ep;
|
nuclear@2
|
834 #ifdef PNG_NO_POINTER_INDEXING
|
nuclear@2
|
835 int i;
|
nuclear@2
|
836 #endif
|
nuclear@2
|
837
|
nuclear@2
|
838 png_debug(1, "in png_write_sPLT\n");
|
nuclear@2
|
839 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
|
nuclear@2
|
840 spalette->name, &new_name))==0)
|
nuclear@2
|
841 {
|
nuclear@2
|
842 png_warning(png_ptr, "Empty keyword in sPLT chunk");
|
nuclear@2
|
843 return;
|
nuclear@2
|
844 }
|
nuclear@2
|
845
|
nuclear@2
|
846 /* make sure we include the NULL after the name */
|
nuclear@2
|
847 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
|
nuclear@2
|
848 (png_uint_32)(name_len + 2 + palette_size));
|
nuclear@2
|
849 png_write_chunk_data(png_ptr, (png_bytep)new_name,
|
nuclear@2
|
850 (png_size_t)(name_len + 1));
|
nuclear@2
|
851 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
|
nuclear@2
|
852
|
nuclear@2
|
853 /* loop through each palette entry, writing appropriately */
|
nuclear@2
|
854 #ifndef PNG_NO_POINTER_INDEXING
|
nuclear@2
|
855 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
|
nuclear@2
|
856 {
|
nuclear@2
|
857 if (spalette->depth == 8)
|
nuclear@2
|
858 {
|
nuclear@2
|
859 entrybuf[0] = (png_byte)ep->red;
|
nuclear@2
|
860 entrybuf[1] = (png_byte)ep->green;
|
nuclear@2
|
861 entrybuf[2] = (png_byte)ep->blue;
|
nuclear@2
|
862 entrybuf[3] = (png_byte)ep->alpha;
|
nuclear@2
|
863 png_save_uint_16(entrybuf + 4, ep->frequency);
|
nuclear@2
|
864 }
|
nuclear@2
|
865 else
|
nuclear@2
|
866 {
|
nuclear@2
|
867 png_save_uint_16(entrybuf + 0, ep->red);
|
nuclear@2
|
868 png_save_uint_16(entrybuf + 2, ep->green);
|
nuclear@2
|
869 png_save_uint_16(entrybuf + 4, ep->blue);
|
nuclear@2
|
870 png_save_uint_16(entrybuf + 6, ep->alpha);
|
nuclear@2
|
871 png_save_uint_16(entrybuf + 8, ep->frequency);
|
nuclear@2
|
872 }
|
nuclear@2
|
873 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
|
nuclear@2
|
874 }
|
nuclear@2
|
875 #else
|
nuclear@2
|
876 ep=spalette->entries;
|
nuclear@2
|
877 for (i=0; i>spalette->nentries; i++)
|
nuclear@2
|
878 {
|
nuclear@2
|
879 if (spalette->depth == 8)
|
nuclear@2
|
880 {
|
nuclear@2
|
881 entrybuf[0] = (png_byte)ep[i].red;
|
nuclear@2
|
882 entrybuf[1] = (png_byte)ep[i].green;
|
nuclear@2
|
883 entrybuf[2] = (png_byte)ep[i].blue;
|
nuclear@2
|
884 entrybuf[3] = (png_byte)ep[i].alpha;
|
nuclear@2
|
885 png_save_uint_16(entrybuf + 4, ep[i].frequency);
|
nuclear@2
|
886 }
|
nuclear@2
|
887 else
|
nuclear@2
|
888 {
|
nuclear@2
|
889 png_save_uint_16(entrybuf + 0, ep[i].red);
|
nuclear@2
|
890 png_save_uint_16(entrybuf + 2, ep[i].green);
|
nuclear@2
|
891 png_save_uint_16(entrybuf + 4, ep[i].blue);
|
nuclear@2
|
892 png_save_uint_16(entrybuf + 6, ep[i].alpha);
|
nuclear@2
|
893 png_save_uint_16(entrybuf + 8, ep[i].frequency);
|
nuclear@2
|
894 }
|
nuclear@2
|
895 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
|
nuclear@2
|
896 }
|
nuclear@2
|
897 #endif
|
nuclear@2
|
898
|
nuclear@2
|
899 png_write_chunk_end(png_ptr);
|
nuclear@2
|
900 png_free(png_ptr, new_name);
|
nuclear@2
|
901 }
|
nuclear@2
|
902 #endif
|
nuclear@2
|
903
|
nuclear@2
|
904 #if defined(PNG_WRITE_sBIT_SUPPORTED)
|
nuclear@2
|
905 /* write the sBIT chunk */
|
nuclear@2
|
906 void /* PRIVATE */
|
nuclear@2
|
907 png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
|
nuclear@2
|
908 {
|
nuclear@2
|
909 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
910 PNG_sBIT;
|
nuclear@2
|
911 #endif
|
nuclear@2
|
912 png_byte buf[4];
|
nuclear@2
|
913 png_size_t size;
|
nuclear@2
|
914
|
nuclear@2
|
915 png_debug(1, "in png_write_sBIT\n");
|
nuclear@2
|
916 /* make sure we don't depend upon the order of PNG_COLOR_8 */
|
nuclear@2
|
917 if (color_type & PNG_COLOR_MASK_COLOR)
|
nuclear@2
|
918 {
|
nuclear@2
|
919 png_byte maxbits;
|
nuclear@2
|
920
|
nuclear@2
|
921 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
|
nuclear@2
|
922 png_ptr->usr_bit_depth);
|
nuclear@2
|
923 if (sbit->red == 0 || sbit->red > maxbits ||
|
nuclear@2
|
924 sbit->green == 0 || sbit->green > maxbits ||
|
nuclear@2
|
925 sbit->blue == 0 || sbit->blue > maxbits)
|
nuclear@2
|
926 {
|
nuclear@2
|
927 png_warning(png_ptr, "Invalid sBIT depth specified");
|
nuclear@2
|
928 return;
|
nuclear@2
|
929 }
|
nuclear@2
|
930 buf[0] = sbit->red;
|
nuclear@2
|
931 buf[1] = sbit->green;
|
nuclear@2
|
932 buf[2] = sbit->blue;
|
nuclear@2
|
933 size = 3;
|
nuclear@2
|
934 }
|
nuclear@2
|
935 else
|
nuclear@2
|
936 {
|
nuclear@2
|
937 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
|
nuclear@2
|
938 {
|
nuclear@2
|
939 png_warning(png_ptr, "Invalid sBIT depth specified");
|
nuclear@2
|
940 return;
|
nuclear@2
|
941 }
|
nuclear@2
|
942 buf[0] = sbit->gray;
|
nuclear@2
|
943 size = 1;
|
nuclear@2
|
944 }
|
nuclear@2
|
945
|
nuclear@2
|
946 if (color_type & PNG_COLOR_MASK_ALPHA)
|
nuclear@2
|
947 {
|
nuclear@2
|
948 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
|
nuclear@2
|
949 {
|
nuclear@2
|
950 png_warning(png_ptr, "Invalid sBIT depth specified");
|
nuclear@2
|
951 return;
|
nuclear@2
|
952 }
|
nuclear@2
|
953 buf[size++] = sbit->alpha;
|
nuclear@2
|
954 }
|
nuclear@2
|
955
|
nuclear@2
|
956 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
|
nuclear@2
|
957 }
|
nuclear@2
|
958 #endif
|
nuclear@2
|
959
|
nuclear@2
|
960 #if defined(PNG_WRITE_cHRM_SUPPORTED)
|
nuclear@2
|
961 /* write the cHRM chunk */
|
nuclear@2
|
962 #ifdef PNG_FLOATING_POINT_SUPPORTED
|
nuclear@2
|
963 void /* PRIVATE */
|
nuclear@2
|
964 png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
|
nuclear@2
|
965 double red_x, double red_y, double green_x, double green_y,
|
nuclear@2
|
966 double blue_x, double blue_y)
|
nuclear@2
|
967 {
|
nuclear@2
|
968 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
969 PNG_cHRM;
|
nuclear@2
|
970 #endif
|
nuclear@2
|
971 png_byte buf[32];
|
nuclear@2
|
972 png_uint_32 itemp;
|
nuclear@2
|
973
|
nuclear@2
|
974 png_debug(1, "in png_write_cHRM\n");
|
nuclear@2
|
975 /* each value is saved in 1/100,000ths */
|
nuclear@2
|
976 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
|
nuclear@2
|
977 white_x + white_y > 1.0)
|
nuclear@2
|
978 {
|
nuclear@2
|
979 png_warning(png_ptr, "Invalid cHRM white point specified");
|
nuclear@2
|
980 #if !defined(PNG_NO_CONSOLE_IO)
|
nuclear@2
|
981 fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y);
|
nuclear@2
|
982 #endif
|
nuclear@2
|
983 return;
|
nuclear@2
|
984 }
|
nuclear@2
|
985 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
|
nuclear@2
|
986 png_save_uint_32(buf, itemp);
|
nuclear@2
|
987 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
|
nuclear@2
|
988 png_save_uint_32(buf + 4, itemp);
|
nuclear@2
|
989
|
nuclear@2
|
990 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
|
nuclear@2
|
991 {
|
nuclear@2
|
992 png_warning(png_ptr, "Invalid cHRM red point specified");
|
nuclear@2
|
993 return;
|
nuclear@2
|
994 }
|
nuclear@2
|
995 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
|
nuclear@2
|
996 png_save_uint_32(buf + 8, itemp);
|
nuclear@2
|
997 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
|
nuclear@2
|
998 png_save_uint_32(buf + 12, itemp);
|
nuclear@2
|
999
|
nuclear@2
|
1000 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
|
nuclear@2
|
1001 {
|
nuclear@2
|
1002 png_warning(png_ptr, "Invalid cHRM green point specified");
|
nuclear@2
|
1003 return;
|
nuclear@2
|
1004 }
|
nuclear@2
|
1005 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
|
nuclear@2
|
1006 png_save_uint_32(buf + 16, itemp);
|
nuclear@2
|
1007 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
|
nuclear@2
|
1008 png_save_uint_32(buf + 20, itemp);
|
nuclear@2
|
1009
|
nuclear@2
|
1010 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
|
nuclear@2
|
1011 {
|
nuclear@2
|
1012 png_warning(png_ptr, "Invalid cHRM blue point specified");
|
nuclear@2
|
1013 return;
|
nuclear@2
|
1014 }
|
nuclear@2
|
1015 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
|
nuclear@2
|
1016 png_save_uint_32(buf + 24, itemp);
|
nuclear@2
|
1017 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
|
nuclear@2
|
1018 png_save_uint_32(buf + 28, itemp);
|
nuclear@2
|
1019
|
nuclear@2
|
1020 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
|
nuclear@2
|
1021 }
|
nuclear@2
|
1022 #endif
|
nuclear@2
|
1023 #ifdef PNG_FIXED_POINT_SUPPORTED
|
nuclear@2
|
1024 void /* PRIVATE */
|
nuclear@2
|
1025 png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
|
nuclear@2
|
1026 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
|
nuclear@2
|
1027 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
|
nuclear@2
|
1028 png_fixed_point blue_y)
|
nuclear@2
|
1029 {
|
nuclear@2
|
1030 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1031 PNG_cHRM;
|
nuclear@2
|
1032 #endif
|
nuclear@2
|
1033 png_byte buf[32];
|
nuclear@2
|
1034
|
nuclear@2
|
1035 png_debug(1, "in png_write_cHRM\n");
|
nuclear@2
|
1036 /* each value is saved in 1/100,000ths */
|
nuclear@2
|
1037 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
|
nuclear@2
|
1038 {
|
nuclear@2
|
1039 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
|
nuclear@2
|
1040 #if !defined(PNG_NO_CONSOLE_IO)
|
nuclear@2
|
1041 fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x,
|
nuclear@2
|
1042 (unsigned long)white_y);
|
nuclear@2
|
1043 #endif
|
nuclear@2
|
1044 return;
|
nuclear@2
|
1045 }
|
nuclear@2
|
1046 png_save_uint_32(buf, (png_uint_32)white_x);
|
nuclear@2
|
1047 png_save_uint_32(buf + 4, (png_uint_32)white_y);
|
nuclear@2
|
1048
|
nuclear@2
|
1049 if (red_x + red_y > 100000L)
|
nuclear@2
|
1050 {
|
nuclear@2
|
1051 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
|
nuclear@2
|
1052 return;
|
nuclear@2
|
1053 }
|
nuclear@2
|
1054 png_save_uint_32(buf + 8, (png_uint_32)red_x);
|
nuclear@2
|
1055 png_save_uint_32(buf + 12, (png_uint_32)red_y);
|
nuclear@2
|
1056
|
nuclear@2
|
1057 if (green_x + green_y > 100000L)
|
nuclear@2
|
1058 {
|
nuclear@2
|
1059 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
|
nuclear@2
|
1060 return;
|
nuclear@2
|
1061 }
|
nuclear@2
|
1062 png_save_uint_32(buf + 16, (png_uint_32)green_x);
|
nuclear@2
|
1063 png_save_uint_32(buf + 20, (png_uint_32)green_y);
|
nuclear@2
|
1064
|
nuclear@2
|
1065 if (blue_x + blue_y > 100000L)
|
nuclear@2
|
1066 {
|
nuclear@2
|
1067 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
|
nuclear@2
|
1068 return;
|
nuclear@2
|
1069 }
|
nuclear@2
|
1070 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
|
nuclear@2
|
1071 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
|
nuclear@2
|
1072
|
nuclear@2
|
1073 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
|
nuclear@2
|
1074 }
|
nuclear@2
|
1075 #endif
|
nuclear@2
|
1076 #endif
|
nuclear@2
|
1077
|
nuclear@2
|
1078 #if defined(PNG_WRITE_tRNS_SUPPORTED)
|
nuclear@2
|
1079 /* write the tRNS chunk */
|
nuclear@2
|
1080 void /* PRIVATE */
|
nuclear@2
|
1081 png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
|
nuclear@2
|
1082 int num_trans, int color_type)
|
nuclear@2
|
1083 {
|
nuclear@2
|
1084 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1085 PNG_tRNS;
|
nuclear@2
|
1086 #endif
|
nuclear@2
|
1087 png_byte buf[6];
|
nuclear@2
|
1088
|
nuclear@2
|
1089 png_debug(1, "in png_write_tRNS\n");
|
nuclear@2
|
1090 if (color_type == PNG_COLOR_TYPE_PALETTE)
|
nuclear@2
|
1091 {
|
nuclear@2
|
1092 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
|
nuclear@2
|
1093 {
|
nuclear@2
|
1094 png_warning(png_ptr, "Invalid number of transparent colors specified");
|
nuclear@2
|
1095 return;
|
nuclear@2
|
1096 }
|
nuclear@2
|
1097 /* write the chunk out as it is */
|
nuclear@2
|
1098 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
|
nuclear@2
|
1099 (png_size_t)num_trans);
|
nuclear@2
|
1100 }
|
nuclear@2
|
1101 else if (color_type == PNG_COLOR_TYPE_GRAY)
|
nuclear@2
|
1102 {
|
nuclear@2
|
1103 /* one 16 bit value */
|
nuclear@2
|
1104 if (tran->gray >= (1 << png_ptr->bit_depth))
|
nuclear@2
|
1105 {
|
nuclear@2
|
1106 png_warning(png_ptr,
|
nuclear@2
|
1107 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
|
nuclear@2
|
1108 return;
|
nuclear@2
|
1109 }
|
nuclear@2
|
1110 png_save_uint_16(buf, tran->gray);
|
nuclear@2
|
1111 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
|
nuclear@2
|
1112 }
|
nuclear@2
|
1113 else if (color_type == PNG_COLOR_TYPE_RGB)
|
nuclear@2
|
1114 {
|
nuclear@2
|
1115 /* three 16 bit values */
|
nuclear@2
|
1116 png_save_uint_16(buf, tran->red);
|
nuclear@2
|
1117 png_save_uint_16(buf + 2, tran->green);
|
nuclear@2
|
1118 png_save_uint_16(buf + 4, tran->blue);
|
nuclear@2
|
1119 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
|
nuclear@2
|
1120 {
|
nuclear@2
|
1121 png_warning(png_ptr,
|
nuclear@2
|
1122 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
|
nuclear@2
|
1123 return;
|
nuclear@2
|
1124 }
|
nuclear@2
|
1125 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
|
nuclear@2
|
1126 }
|
nuclear@2
|
1127 else
|
nuclear@2
|
1128 {
|
nuclear@2
|
1129 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
|
nuclear@2
|
1130 }
|
nuclear@2
|
1131 }
|
nuclear@2
|
1132 #endif
|
nuclear@2
|
1133
|
nuclear@2
|
1134 #if defined(PNG_WRITE_bKGD_SUPPORTED)
|
nuclear@2
|
1135 /* write the background chunk */
|
nuclear@2
|
1136 void /* PRIVATE */
|
nuclear@2
|
1137 png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
|
nuclear@2
|
1138 {
|
nuclear@2
|
1139 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1140 PNG_bKGD;
|
nuclear@2
|
1141 #endif
|
nuclear@2
|
1142 png_byte buf[6];
|
nuclear@2
|
1143
|
nuclear@2
|
1144 png_debug(1, "in png_write_bKGD\n");
|
nuclear@2
|
1145 if (color_type == PNG_COLOR_TYPE_PALETTE)
|
nuclear@2
|
1146 {
|
nuclear@2
|
1147 if (
|
nuclear@2
|
1148 #if defined(PNG_MNG_FEATURES_SUPPORTED)
|
nuclear@2
|
1149 (png_ptr->num_palette ||
|
nuclear@2
|
1150 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
|
nuclear@2
|
1151 #endif
|
nuclear@2
|
1152 back->index > png_ptr->num_palette)
|
nuclear@2
|
1153 {
|
nuclear@2
|
1154 png_warning(png_ptr, "Invalid background palette index");
|
nuclear@2
|
1155 return;
|
nuclear@2
|
1156 }
|
nuclear@2
|
1157 buf[0] = back->index;
|
nuclear@2
|
1158 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
|
nuclear@2
|
1159 }
|
nuclear@2
|
1160 else if (color_type & PNG_COLOR_MASK_COLOR)
|
nuclear@2
|
1161 {
|
nuclear@2
|
1162 png_save_uint_16(buf, back->red);
|
nuclear@2
|
1163 png_save_uint_16(buf + 2, back->green);
|
nuclear@2
|
1164 png_save_uint_16(buf + 4, back->blue);
|
nuclear@2
|
1165 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
|
nuclear@2
|
1166 {
|
nuclear@2
|
1167 png_warning(png_ptr,
|
nuclear@2
|
1168 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
|
nuclear@2
|
1169 return;
|
nuclear@2
|
1170 }
|
nuclear@2
|
1171 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
|
nuclear@2
|
1172 }
|
nuclear@2
|
1173 else
|
nuclear@2
|
1174 {
|
nuclear@2
|
1175 if (back->gray >= (1 << png_ptr->bit_depth))
|
nuclear@2
|
1176 {
|
nuclear@2
|
1177 png_warning(png_ptr,
|
nuclear@2
|
1178 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
|
nuclear@2
|
1179 return;
|
nuclear@2
|
1180 }
|
nuclear@2
|
1181 png_save_uint_16(buf, back->gray);
|
nuclear@2
|
1182 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
|
nuclear@2
|
1183 }
|
nuclear@2
|
1184 }
|
nuclear@2
|
1185 #endif
|
nuclear@2
|
1186
|
nuclear@2
|
1187 #if defined(PNG_WRITE_hIST_SUPPORTED)
|
nuclear@2
|
1188 /* write the histogram */
|
nuclear@2
|
1189 void /* PRIVATE */
|
nuclear@2
|
1190 png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
|
nuclear@2
|
1191 {
|
nuclear@2
|
1192 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1193 PNG_hIST;
|
nuclear@2
|
1194 #endif
|
nuclear@2
|
1195 int i;
|
nuclear@2
|
1196 png_byte buf[3];
|
nuclear@2
|
1197
|
nuclear@2
|
1198 png_debug(1, "in png_write_hIST\n");
|
nuclear@2
|
1199 if (num_hist > (int)png_ptr->num_palette)
|
nuclear@2
|
1200 {
|
nuclear@2
|
1201 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
|
nuclear@2
|
1202 png_ptr->num_palette);
|
nuclear@2
|
1203 png_warning(png_ptr, "Invalid number of histogram entries specified");
|
nuclear@2
|
1204 return;
|
nuclear@2
|
1205 }
|
nuclear@2
|
1206
|
nuclear@2
|
1207 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
|
nuclear@2
|
1208 (png_uint_32)(num_hist * 2));
|
nuclear@2
|
1209 for (i = 0; i < num_hist; i++)
|
nuclear@2
|
1210 {
|
nuclear@2
|
1211 png_save_uint_16(buf, hist[i]);
|
nuclear@2
|
1212 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
|
nuclear@2
|
1213 }
|
nuclear@2
|
1214 png_write_chunk_end(png_ptr);
|
nuclear@2
|
1215 }
|
nuclear@2
|
1216 #endif
|
nuclear@2
|
1217
|
nuclear@2
|
1218 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
|
nuclear@2
|
1219 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
|
nuclear@2
|
1220 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
|
nuclear@2
|
1221 * and if invalid, correct the keyword rather than discarding the entire
|
nuclear@2
|
1222 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
|
nuclear@2
|
1223 * length, forbids leading or trailing whitespace, multiple internal spaces,
|
nuclear@2
|
1224 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
|
nuclear@2
|
1225 *
|
nuclear@2
|
1226 * The new_key is allocated to hold the corrected keyword and must be freed
|
nuclear@2
|
1227 * by the calling routine. This avoids problems with trying to write to
|
nuclear@2
|
1228 * static keywords without having to have duplicate copies of the strings.
|
nuclear@2
|
1229 */
|
nuclear@2
|
1230 png_size_t /* PRIVATE */
|
nuclear@2
|
1231 png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
|
nuclear@2
|
1232 {
|
nuclear@2
|
1233 png_size_t key_len;
|
nuclear@2
|
1234 png_charp kp, dp;
|
nuclear@2
|
1235 int kflag;
|
nuclear@2
|
1236 int kwarn=0;
|
nuclear@2
|
1237
|
nuclear@2
|
1238 png_debug(1, "in png_check_keyword\n");
|
nuclear@2
|
1239 *new_key = NULL;
|
nuclear@2
|
1240
|
nuclear@2
|
1241 if (key == NULL || (key_len = png_strlen(key)) == 0)
|
nuclear@2
|
1242 {
|
nuclear@2
|
1243 png_warning(png_ptr, "zero length keyword");
|
nuclear@2
|
1244 return ((png_size_t)0);
|
nuclear@2
|
1245 }
|
nuclear@2
|
1246
|
nuclear@2
|
1247 png_debug1(2, "Keyword to be checked is '%s'\n", key);
|
nuclear@2
|
1248
|
nuclear@2
|
1249 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
|
nuclear@2
|
1250 if (*new_key == NULL)
|
nuclear@2
|
1251 {
|
nuclear@2
|
1252 png_warning(png_ptr, "Out of memory while procesing keyword");
|
nuclear@2
|
1253 return ((png_size_t)0);
|
nuclear@2
|
1254 }
|
nuclear@2
|
1255
|
nuclear@2
|
1256 /* Replace non-printing characters with a blank and print a warning */
|
nuclear@2
|
1257 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
|
nuclear@2
|
1258 {
|
nuclear@2
|
1259 if ((png_byte)*kp < 0x20 ||
|
nuclear@2
|
1260 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
|
nuclear@2
|
1261 {
|
nuclear@2
|
1262 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
|
nuclear@2
|
1263 char msg[40];
|
nuclear@2
|
1264
|
nuclear@2
|
1265 png_snprintf(msg, 40,
|
nuclear@2
|
1266 "invalid keyword character 0x%02X", (png_byte)*kp);
|
nuclear@2
|
1267 png_warning(png_ptr, msg);
|
nuclear@2
|
1268 #else
|
nuclear@2
|
1269 png_warning(png_ptr, "invalid character in keyword");
|
nuclear@2
|
1270 #endif
|
nuclear@2
|
1271 *dp = ' ';
|
nuclear@2
|
1272 }
|
nuclear@2
|
1273 else
|
nuclear@2
|
1274 {
|
nuclear@2
|
1275 *dp = *kp;
|
nuclear@2
|
1276 }
|
nuclear@2
|
1277 }
|
nuclear@2
|
1278 *dp = '\0';
|
nuclear@2
|
1279
|
nuclear@2
|
1280 /* Remove any trailing white space. */
|
nuclear@2
|
1281 kp = *new_key + key_len - 1;
|
nuclear@2
|
1282 if (*kp == ' ')
|
nuclear@2
|
1283 {
|
nuclear@2
|
1284 png_warning(png_ptr, "trailing spaces removed from keyword");
|
nuclear@2
|
1285
|
nuclear@2
|
1286 while (*kp == ' ')
|
nuclear@2
|
1287 {
|
nuclear@2
|
1288 *(kp--) = '\0';
|
nuclear@2
|
1289 key_len--;
|
nuclear@2
|
1290 }
|
nuclear@2
|
1291 }
|
nuclear@2
|
1292
|
nuclear@2
|
1293 /* Remove any leading white space. */
|
nuclear@2
|
1294 kp = *new_key;
|
nuclear@2
|
1295 if (*kp == ' ')
|
nuclear@2
|
1296 {
|
nuclear@2
|
1297 png_warning(png_ptr, "leading spaces removed from keyword");
|
nuclear@2
|
1298
|
nuclear@2
|
1299 while (*kp == ' ')
|
nuclear@2
|
1300 {
|
nuclear@2
|
1301 kp++;
|
nuclear@2
|
1302 key_len--;
|
nuclear@2
|
1303 }
|
nuclear@2
|
1304 }
|
nuclear@2
|
1305
|
nuclear@2
|
1306 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
|
nuclear@2
|
1307
|
nuclear@2
|
1308 /* Remove multiple internal spaces. */
|
nuclear@2
|
1309 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
|
nuclear@2
|
1310 {
|
nuclear@2
|
1311 if (*kp == ' ' && kflag == 0)
|
nuclear@2
|
1312 {
|
nuclear@2
|
1313 *(dp++) = *kp;
|
nuclear@2
|
1314 kflag = 1;
|
nuclear@2
|
1315 }
|
nuclear@2
|
1316 else if (*kp == ' ')
|
nuclear@2
|
1317 {
|
nuclear@2
|
1318 key_len--;
|
nuclear@2
|
1319 kwarn=1;
|
nuclear@2
|
1320 }
|
nuclear@2
|
1321 else
|
nuclear@2
|
1322 {
|
nuclear@2
|
1323 *(dp++) = *kp;
|
nuclear@2
|
1324 kflag = 0;
|
nuclear@2
|
1325 }
|
nuclear@2
|
1326 }
|
nuclear@2
|
1327 *dp = '\0';
|
nuclear@2
|
1328 if (kwarn)
|
nuclear@2
|
1329 png_warning(png_ptr, "extra interior spaces removed from keyword");
|
nuclear@2
|
1330
|
nuclear@2
|
1331 if (key_len == 0)
|
nuclear@2
|
1332 {
|
nuclear@2
|
1333 png_free(png_ptr, *new_key);
|
nuclear@2
|
1334 *new_key=NULL;
|
nuclear@2
|
1335 png_warning(png_ptr, "Zero length keyword");
|
nuclear@2
|
1336 }
|
nuclear@2
|
1337
|
nuclear@2
|
1338 if (key_len > 79)
|
nuclear@2
|
1339 {
|
nuclear@2
|
1340 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
|
nuclear@2
|
1341 new_key[79] = '\0';
|
nuclear@2
|
1342 key_len = 79;
|
nuclear@2
|
1343 }
|
nuclear@2
|
1344
|
nuclear@2
|
1345 return (key_len);
|
nuclear@2
|
1346 }
|
nuclear@2
|
1347 #endif
|
nuclear@2
|
1348
|
nuclear@2
|
1349 #if defined(PNG_WRITE_tEXt_SUPPORTED)
|
nuclear@2
|
1350 /* write a tEXt chunk */
|
nuclear@2
|
1351 void /* PRIVATE */
|
nuclear@2
|
1352 png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
|
nuclear@2
|
1353 png_size_t text_len)
|
nuclear@2
|
1354 {
|
nuclear@2
|
1355 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1356 PNG_tEXt;
|
nuclear@2
|
1357 #endif
|
nuclear@2
|
1358 png_size_t key_len;
|
nuclear@2
|
1359 png_charp new_key;
|
nuclear@2
|
1360
|
nuclear@2
|
1361 png_debug(1, "in png_write_tEXt\n");
|
nuclear@2
|
1362 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
|
nuclear@2
|
1363 {
|
nuclear@2
|
1364 png_warning(png_ptr, "Empty keyword in tEXt chunk");
|
nuclear@2
|
1365 return;
|
nuclear@2
|
1366 }
|
nuclear@2
|
1367
|
nuclear@2
|
1368 if (text == NULL || *text == '\0')
|
nuclear@2
|
1369 text_len = 0;
|
nuclear@2
|
1370 else
|
nuclear@2
|
1371 text_len = png_strlen(text);
|
nuclear@2
|
1372
|
nuclear@2
|
1373 /* make sure we include the 0 after the key */
|
nuclear@2
|
1374 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
|
nuclear@2
|
1375 (png_uint_32)(key_len + text_len + 1));
|
nuclear@2
|
1376 /*
|
nuclear@2
|
1377 * We leave it to the application to meet PNG-1.0 requirements on the
|
nuclear@2
|
1378 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
|
nuclear@2
|
1379 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
|
nuclear@2
|
1380 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
|
nuclear@2
|
1381 */
|
nuclear@2
|
1382 png_write_chunk_data(png_ptr, (png_bytep)new_key,
|
nuclear@2
|
1383 (png_size_t)(key_len + 1));
|
nuclear@2
|
1384 if (text_len)
|
nuclear@2
|
1385 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
|
nuclear@2
|
1386
|
nuclear@2
|
1387 png_write_chunk_end(png_ptr);
|
nuclear@2
|
1388 png_free(png_ptr, new_key);
|
nuclear@2
|
1389 }
|
nuclear@2
|
1390 #endif
|
nuclear@2
|
1391
|
nuclear@2
|
1392 #if defined(PNG_WRITE_zTXt_SUPPORTED)
|
nuclear@2
|
1393 /* write a compressed text chunk */
|
nuclear@2
|
1394 void /* PRIVATE */
|
nuclear@2
|
1395 png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
|
nuclear@2
|
1396 png_size_t text_len, int compression)
|
nuclear@2
|
1397 {
|
nuclear@2
|
1398 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1399 PNG_zTXt;
|
nuclear@2
|
1400 #endif
|
nuclear@2
|
1401 png_size_t key_len;
|
nuclear@2
|
1402 char buf[1];
|
nuclear@2
|
1403 png_charp new_key;
|
nuclear@2
|
1404 compression_state comp;
|
nuclear@2
|
1405
|
nuclear@2
|
1406 png_debug(1, "in png_write_zTXt\n");
|
nuclear@2
|
1407
|
nuclear@2
|
1408 comp.num_output_ptr = 0;
|
nuclear@2
|
1409 comp.max_output_ptr = 0;
|
nuclear@2
|
1410 comp.output_ptr = NULL;
|
nuclear@2
|
1411 comp.input = NULL;
|
nuclear@2
|
1412 comp.input_len = 0;
|
nuclear@2
|
1413
|
nuclear@2
|
1414 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
|
nuclear@2
|
1415 {
|
nuclear@2
|
1416 png_warning(png_ptr, "Empty keyword in zTXt chunk");
|
nuclear@2
|
1417 png_free(png_ptr, new_key);
|
nuclear@2
|
1418 return;
|
nuclear@2
|
1419 }
|
nuclear@2
|
1420
|
nuclear@2
|
1421 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
|
nuclear@2
|
1422 {
|
nuclear@2
|
1423 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
|
nuclear@2
|
1424 png_free(png_ptr, new_key);
|
nuclear@2
|
1425 return;
|
nuclear@2
|
1426 }
|
nuclear@2
|
1427
|
nuclear@2
|
1428 text_len = png_strlen(text);
|
nuclear@2
|
1429
|
nuclear@2
|
1430 /* compute the compressed data; do it now for the length */
|
nuclear@2
|
1431 text_len = png_text_compress(png_ptr, text, text_len, compression,
|
nuclear@2
|
1432 &comp);
|
nuclear@2
|
1433
|
nuclear@2
|
1434 /* write start of chunk */
|
nuclear@2
|
1435 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
|
nuclear@2
|
1436 (png_uint_32)(key_len+text_len + 2));
|
nuclear@2
|
1437 /* write key */
|
nuclear@2
|
1438 png_write_chunk_data(png_ptr, (png_bytep)new_key,
|
nuclear@2
|
1439 (png_size_t)(key_len + 1));
|
nuclear@2
|
1440 png_free(png_ptr, new_key);
|
nuclear@2
|
1441
|
nuclear@2
|
1442 buf[0] = (png_byte)compression;
|
nuclear@2
|
1443 /* write compression */
|
nuclear@2
|
1444 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
|
nuclear@2
|
1445 /* write the compressed data */
|
nuclear@2
|
1446 png_write_compressed_data_out(png_ptr, &comp);
|
nuclear@2
|
1447
|
nuclear@2
|
1448 /* close the chunk */
|
nuclear@2
|
1449 png_write_chunk_end(png_ptr);
|
nuclear@2
|
1450 }
|
nuclear@2
|
1451 #endif
|
nuclear@2
|
1452
|
nuclear@2
|
1453 #if defined(PNG_WRITE_iTXt_SUPPORTED)
|
nuclear@2
|
1454 /* write an iTXt chunk */
|
nuclear@2
|
1455 void /* PRIVATE */
|
nuclear@2
|
1456 png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
|
nuclear@2
|
1457 png_charp lang, png_charp lang_key, png_charp text)
|
nuclear@2
|
1458 {
|
nuclear@2
|
1459 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1460 PNG_iTXt;
|
nuclear@2
|
1461 #endif
|
nuclear@2
|
1462 png_size_t lang_len, key_len, lang_key_len, text_len;
|
nuclear@2
|
1463 png_charp new_lang, new_key;
|
nuclear@2
|
1464 png_byte cbuf[2];
|
nuclear@2
|
1465 compression_state comp;
|
nuclear@2
|
1466
|
nuclear@2
|
1467 png_debug(1, "in png_write_iTXt\n");
|
nuclear@2
|
1468
|
nuclear@2
|
1469 comp.num_output_ptr = 0;
|
nuclear@2
|
1470 comp.max_output_ptr = 0;
|
nuclear@2
|
1471 comp.output_ptr = NULL;
|
nuclear@2
|
1472 comp.input = NULL;
|
nuclear@2
|
1473
|
nuclear@2
|
1474 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
|
nuclear@2
|
1475 {
|
nuclear@2
|
1476 png_warning(png_ptr, "Empty keyword in iTXt chunk");
|
nuclear@2
|
1477 return;
|
nuclear@2
|
1478 }
|
nuclear@2
|
1479 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
|
nuclear@2
|
1480 {
|
nuclear@2
|
1481 png_warning(png_ptr, "Empty language field in iTXt chunk");
|
nuclear@2
|
1482 new_lang = NULL;
|
nuclear@2
|
1483 lang_len = 0;
|
nuclear@2
|
1484 }
|
nuclear@2
|
1485
|
nuclear@2
|
1486 if (lang_key == NULL)
|
nuclear@2
|
1487 lang_key_len = 0;
|
nuclear@2
|
1488 else
|
nuclear@2
|
1489 lang_key_len = png_strlen(lang_key);
|
nuclear@2
|
1490
|
nuclear@2
|
1491 if (text == NULL)
|
nuclear@2
|
1492 text_len = 0;
|
nuclear@2
|
1493 else
|
nuclear@2
|
1494 text_len = png_strlen(text);
|
nuclear@2
|
1495
|
nuclear@2
|
1496 /* compute the compressed data; do it now for the length */
|
nuclear@2
|
1497 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
|
nuclear@2
|
1498 &comp);
|
nuclear@2
|
1499
|
nuclear@2
|
1500
|
nuclear@2
|
1501 /* make sure we include the compression flag, the compression byte,
|
nuclear@2
|
1502 * and the NULs after the key, lang, and lang_key parts */
|
nuclear@2
|
1503
|
nuclear@2
|
1504 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
|
nuclear@2
|
1505 (png_uint_32)(
|
nuclear@2
|
1506 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
|
nuclear@2
|
1507 + key_len
|
nuclear@2
|
1508 + lang_len
|
nuclear@2
|
1509 + lang_key_len
|
nuclear@2
|
1510 + text_len));
|
nuclear@2
|
1511
|
nuclear@2
|
1512 /*
|
nuclear@2
|
1513 * We leave it to the application to meet PNG-1.0 requirements on the
|
nuclear@2
|
1514 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
|
nuclear@2
|
1515 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
|
nuclear@2
|
1516 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
|
nuclear@2
|
1517 */
|
nuclear@2
|
1518 png_write_chunk_data(png_ptr, (png_bytep)new_key,
|
nuclear@2
|
1519 (png_size_t)(key_len + 1));
|
nuclear@2
|
1520
|
nuclear@2
|
1521 /* set the compression flag */
|
nuclear@2
|
1522 if (compression == PNG_ITXT_COMPRESSION_NONE || \
|
nuclear@2
|
1523 compression == PNG_TEXT_COMPRESSION_NONE)
|
nuclear@2
|
1524 cbuf[0] = 0;
|
nuclear@2
|
1525 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
|
nuclear@2
|
1526 cbuf[0] = 1;
|
nuclear@2
|
1527 /* set the compression method */
|
nuclear@2
|
1528 cbuf[1] = 0;
|
nuclear@2
|
1529 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
|
nuclear@2
|
1530
|
nuclear@2
|
1531 cbuf[0] = 0;
|
nuclear@2
|
1532 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
|
nuclear@2
|
1533 (png_size_t)(lang_len + 1));
|
nuclear@2
|
1534 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
|
nuclear@2
|
1535 (png_size_t)(lang_key_len + 1));
|
nuclear@2
|
1536 png_write_compressed_data_out(png_ptr, &comp);
|
nuclear@2
|
1537
|
nuclear@2
|
1538 png_write_chunk_end(png_ptr);
|
nuclear@2
|
1539 png_free(png_ptr, new_key);
|
nuclear@2
|
1540 png_free(png_ptr, new_lang);
|
nuclear@2
|
1541 }
|
nuclear@2
|
1542 #endif
|
nuclear@2
|
1543
|
nuclear@2
|
1544 #if defined(PNG_WRITE_oFFs_SUPPORTED)
|
nuclear@2
|
1545 /* write the oFFs chunk */
|
nuclear@2
|
1546 void /* PRIVATE */
|
nuclear@2
|
1547 png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
|
nuclear@2
|
1548 int unit_type)
|
nuclear@2
|
1549 {
|
nuclear@2
|
1550 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1551 PNG_oFFs;
|
nuclear@2
|
1552 #endif
|
nuclear@2
|
1553 png_byte buf[9];
|
nuclear@2
|
1554
|
nuclear@2
|
1555 png_debug(1, "in png_write_oFFs\n");
|
nuclear@2
|
1556 if (unit_type >= PNG_OFFSET_LAST)
|
nuclear@2
|
1557 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
|
nuclear@2
|
1558
|
nuclear@2
|
1559 png_save_int_32(buf, x_offset);
|
nuclear@2
|
1560 png_save_int_32(buf + 4, y_offset);
|
nuclear@2
|
1561 buf[8] = (png_byte)unit_type;
|
nuclear@2
|
1562
|
nuclear@2
|
1563 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
|
nuclear@2
|
1564 }
|
nuclear@2
|
1565 #endif
|
nuclear@2
|
1566 #if defined(PNG_WRITE_pCAL_SUPPORTED)
|
nuclear@2
|
1567 /* write the pCAL chunk (described in the PNG extensions document) */
|
nuclear@2
|
1568 void /* PRIVATE */
|
nuclear@2
|
1569 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
|
nuclear@2
|
1570 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
|
nuclear@2
|
1571 {
|
nuclear@2
|
1572 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1573 PNG_pCAL;
|
nuclear@2
|
1574 #endif
|
nuclear@2
|
1575 png_size_t purpose_len, units_len, total_len;
|
nuclear@2
|
1576 png_uint_32p params_len;
|
nuclear@2
|
1577 png_byte buf[10];
|
nuclear@2
|
1578 png_charp new_purpose;
|
nuclear@2
|
1579 int i;
|
nuclear@2
|
1580
|
nuclear@2
|
1581 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
|
nuclear@2
|
1582 if (type >= PNG_EQUATION_LAST)
|
nuclear@2
|
1583 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
|
nuclear@2
|
1584
|
nuclear@2
|
1585 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
|
nuclear@2
|
1586 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
|
nuclear@2
|
1587 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
|
nuclear@2
|
1588 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
|
nuclear@2
|
1589 total_len = purpose_len + units_len + 10;
|
nuclear@2
|
1590
|
nuclear@2
|
1591 params_len = (png_uint_32p)png_malloc(png_ptr,
|
nuclear@2
|
1592 (png_uint_32)(nparams * png_sizeof(png_uint_32)));
|
nuclear@2
|
1593
|
nuclear@2
|
1594 /* Find the length of each parameter, making sure we don't count the
|
nuclear@2
|
1595 null terminator for the last parameter. */
|
nuclear@2
|
1596 for (i = 0; i < nparams; i++)
|
nuclear@2
|
1597 {
|
nuclear@2
|
1598 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
|
nuclear@2
|
1599 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
|
nuclear@2
|
1600 (unsigned long) params_len[i]);
|
nuclear@2
|
1601 total_len += (png_size_t)params_len[i];
|
nuclear@2
|
1602 }
|
nuclear@2
|
1603
|
nuclear@2
|
1604 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
|
nuclear@2
|
1605 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
|
nuclear@2
|
1606 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
|
nuclear@2
|
1607 (png_size_t)purpose_len);
|
nuclear@2
|
1608 png_save_int_32(buf, X0);
|
nuclear@2
|
1609 png_save_int_32(buf + 4, X1);
|
nuclear@2
|
1610 buf[8] = (png_byte)type;
|
nuclear@2
|
1611 buf[9] = (png_byte)nparams;
|
nuclear@2
|
1612 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
|
nuclear@2
|
1613 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
|
nuclear@2
|
1614
|
nuclear@2
|
1615 png_free(png_ptr, new_purpose);
|
nuclear@2
|
1616
|
nuclear@2
|
1617 for (i = 0; i < nparams; i++)
|
nuclear@2
|
1618 {
|
nuclear@2
|
1619 png_write_chunk_data(png_ptr, (png_bytep)params[i],
|
nuclear@2
|
1620 (png_size_t)params_len[i]);
|
nuclear@2
|
1621 }
|
nuclear@2
|
1622
|
nuclear@2
|
1623 png_free(png_ptr, params_len);
|
nuclear@2
|
1624 png_write_chunk_end(png_ptr);
|
nuclear@2
|
1625 }
|
nuclear@2
|
1626 #endif
|
nuclear@2
|
1627
|
nuclear@2
|
1628 #if defined(PNG_WRITE_sCAL_SUPPORTED)
|
nuclear@2
|
1629 /* write the sCAL chunk */
|
nuclear@2
|
1630 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
|
nuclear@2
|
1631 void /* PRIVATE */
|
nuclear@2
|
1632 png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
|
nuclear@2
|
1633 {
|
nuclear@2
|
1634 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1635 PNG_sCAL;
|
nuclear@2
|
1636 #endif
|
nuclear@2
|
1637 char buf[64];
|
nuclear@2
|
1638 png_size_t total_len;
|
nuclear@2
|
1639
|
nuclear@2
|
1640 png_debug(1, "in png_write_sCAL\n");
|
nuclear@2
|
1641
|
nuclear@2
|
1642 buf[0] = (char)unit;
|
nuclear@2
|
1643 #if defined(_WIN32_WCE)
|
nuclear@2
|
1644 /* sprintf() function is not supported on WindowsCE */
|
nuclear@2
|
1645 {
|
nuclear@2
|
1646 wchar_t wc_buf[32];
|
nuclear@2
|
1647 size_t wc_len;
|
nuclear@2
|
1648 swprintf(wc_buf, TEXT("%12.12e"), width);
|
nuclear@2
|
1649 wc_len = wcslen(wc_buf);
|
nuclear@2
|
1650 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
|
nuclear@2
|
1651 total_len = wc_len + 2;
|
nuclear@2
|
1652 swprintf(wc_buf, TEXT("%12.12e"), height);
|
nuclear@2
|
1653 wc_len = wcslen(wc_buf);
|
nuclear@2
|
1654 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
|
nuclear@2
|
1655 NULL, NULL);
|
nuclear@2
|
1656 total_len += wc_len;
|
nuclear@2
|
1657 }
|
nuclear@2
|
1658 #else
|
nuclear@2
|
1659 png_snprintf(buf + 1, 63, "%12.12e", width);
|
nuclear@2
|
1660 total_len = 1 + png_strlen(buf + 1) + 1;
|
nuclear@2
|
1661 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
|
nuclear@2
|
1662 total_len += png_strlen(buf + total_len);
|
nuclear@2
|
1663 #endif
|
nuclear@2
|
1664
|
nuclear@2
|
1665 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
|
nuclear@2
|
1666 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
|
nuclear@2
|
1667 }
|
nuclear@2
|
1668 #else
|
nuclear@2
|
1669 #ifdef PNG_FIXED_POINT_SUPPORTED
|
nuclear@2
|
1670 void /* PRIVATE */
|
nuclear@2
|
1671 png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
|
nuclear@2
|
1672 png_charp height)
|
nuclear@2
|
1673 {
|
nuclear@2
|
1674 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1675 PNG_sCAL;
|
nuclear@2
|
1676 #endif
|
nuclear@2
|
1677 png_byte buf[64];
|
nuclear@2
|
1678 png_size_t wlen, hlen, total_len;
|
nuclear@2
|
1679
|
nuclear@2
|
1680 png_debug(1, "in png_write_sCAL_s\n");
|
nuclear@2
|
1681
|
nuclear@2
|
1682 wlen = png_strlen(width);
|
nuclear@2
|
1683 hlen = png_strlen(height);
|
nuclear@2
|
1684 total_len = wlen + hlen + 2;
|
nuclear@2
|
1685 if (total_len > 64)
|
nuclear@2
|
1686 {
|
nuclear@2
|
1687 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
|
nuclear@2
|
1688 return;
|
nuclear@2
|
1689 }
|
nuclear@2
|
1690
|
nuclear@2
|
1691 buf[0] = (png_byte)unit;
|
nuclear@2
|
1692 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
|
nuclear@2
|
1693 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
|
nuclear@2
|
1694
|
nuclear@2
|
1695 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
|
nuclear@2
|
1696 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
|
nuclear@2
|
1697 }
|
nuclear@2
|
1698 #endif
|
nuclear@2
|
1699 #endif
|
nuclear@2
|
1700 #endif
|
nuclear@2
|
1701
|
nuclear@2
|
1702 #if defined(PNG_WRITE_pHYs_SUPPORTED)
|
nuclear@2
|
1703 /* write the pHYs chunk */
|
nuclear@2
|
1704 void /* PRIVATE */
|
nuclear@2
|
1705 png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
|
nuclear@2
|
1706 png_uint_32 y_pixels_per_unit,
|
nuclear@2
|
1707 int unit_type)
|
nuclear@2
|
1708 {
|
nuclear@2
|
1709 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1710 PNG_pHYs;
|
nuclear@2
|
1711 #endif
|
nuclear@2
|
1712 png_byte buf[9];
|
nuclear@2
|
1713
|
nuclear@2
|
1714 png_debug(1, "in png_write_pHYs\n");
|
nuclear@2
|
1715 if (unit_type >= PNG_RESOLUTION_LAST)
|
nuclear@2
|
1716 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
|
nuclear@2
|
1717
|
nuclear@2
|
1718 png_save_uint_32(buf, x_pixels_per_unit);
|
nuclear@2
|
1719 png_save_uint_32(buf + 4, y_pixels_per_unit);
|
nuclear@2
|
1720 buf[8] = (png_byte)unit_type;
|
nuclear@2
|
1721
|
nuclear@2
|
1722 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
|
nuclear@2
|
1723 }
|
nuclear@2
|
1724 #endif
|
nuclear@2
|
1725
|
nuclear@2
|
1726 #if defined(PNG_WRITE_tIME_SUPPORTED)
|
nuclear@2
|
1727 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
|
nuclear@2
|
1728 * or png_convert_from_time_t(), or fill in the structure yourself.
|
nuclear@2
|
1729 */
|
nuclear@2
|
1730 void /* PRIVATE */
|
nuclear@2
|
1731 png_write_tIME(png_structp png_ptr, png_timep mod_time)
|
nuclear@2
|
1732 {
|
nuclear@2
|
1733 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1734 PNG_tIME;
|
nuclear@2
|
1735 #endif
|
nuclear@2
|
1736 png_byte buf[7];
|
nuclear@2
|
1737
|
nuclear@2
|
1738 png_debug(1, "in png_write_tIME\n");
|
nuclear@2
|
1739 if (mod_time->month > 12 || mod_time->month < 1 ||
|
nuclear@2
|
1740 mod_time->day > 31 || mod_time->day < 1 ||
|
nuclear@2
|
1741 mod_time->hour > 23 || mod_time->second > 60)
|
nuclear@2
|
1742 {
|
nuclear@2
|
1743 png_warning(png_ptr, "Invalid time specified for tIME chunk");
|
nuclear@2
|
1744 return;
|
nuclear@2
|
1745 }
|
nuclear@2
|
1746
|
nuclear@2
|
1747 png_save_uint_16(buf, mod_time->year);
|
nuclear@2
|
1748 buf[2] = mod_time->month;
|
nuclear@2
|
1749 buf[3] = mod_time->day;
|
nuclear@2
|
1750 buf[4] = mod_time->hour;
|
nuclear@2
|
1751 buf[5] = mod_time->minute;
|
nuclear@2
|
1752 buf[6] = mod_time->second;
|
nuclear@2
|
1753
|
nuclear@2
|
1754 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
|
nuclear@2
|
1755 }
|
nuclear@2
|
1756 #endif
|
nuclear@2
|
1757
|
nuclear@2
|
1758 /* initializes the row writing capability of libpng */
|
nuclear@2
|
1759 void /* PRIVATE */
|
nuclear@2
|
1760 png_write_start_row(png_structp png_ptr)
|
nuclear@2
|
1761 {
|
nuclear@2
|
1762 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
nuclear@2
|
1763 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1764 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
|
nuclear@2
|
1765
|
nuclear@2
|
1766 /* start of interlace block */
|
nuclear@2
|
1767 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
|
nuclear@2
|
1768
|
nuclear@2
|
1769 /* offset to next interlace block */
|
nuclear@2
|
1770 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
|
nuclear@2
|
1771
|
nuclear@2
|
1772 /* start of interlace block in the y direction */
|
nuclear@2
|
1773 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
|
nuclear@2
|
1774
|
nuclear@2
|
1775 /* offset to next interlace block in the y direction */
|
nuclear@2
|
1776 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
|
nuclear@2
|
1777 #endif
|
nuclear@2
|
1778 #endif
|
nuclear@2
|
1779
|
nuclear@2
|
1780 png_size_t buf_size;
|
nuclear@2
|
1781
|
nuclear@2
|
1782 png_debug(1, "in png_write_start_row\n");
|
nuclear@2
|
1783 buf_size = (png_size_t)(PNG_ROWBYTES(
|
nuclear@2
|
1784 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
|
nuclear@2
|
1785
|
nuclear@2
|
1786 /* set up row buffer */
|
nuclear@2
|
1787 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
|
nuclear@2
|
1788 (png_uint_32)buf_size);
|
nuclear@2
|
1789 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
|
nuclear@2
|
1790
|
nuclear@2
|
1791 #ifndef PNG_NO_WRITE_FILTER
|
nuclear@2
|
1792 /* set up filtering buffer, if using this filter */
|
nuclear@2
|
1793 if (png_ptr->do_filter & PNG_FILTER_SUB)
|
nuclear@2
|
1794 {
|
nuclear@2
|
1795 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
|
nuclear@2
|
1796 (png_uint_32)(png_ptr->rowbytes + 1));
|
nuclear@2
|
1797 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
|
nuclear@2
|
1798 }
|
nuclear@2
|
1799
|
nuclear@2
|
1800 /* We only need to keep the previous row if we are using one of these. */
|
nuclear@2
|
1801 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
|
nuclear@2
|
1802 {
|
nuclear@2
|
1803 /* set up previous row buffer */
|
nuclear@2
|
1804 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
|
nuclear@2
|
1805 (png_uint_32)buf_size);
|
nuclear@2
|
1806 png_memset(png_ptr->prev_row, 0, buf_size);
|
nuclear@2
|
1807
|
nuclear@2
|
1808 if (png_ptr->do_filter & PNG_FILTER_UP)
|
nuclear@2
|
1809 {
|
nuclear@2
|
1810 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
|
nuclear@2
|
1811 (png_uint_32)(png_ptr->rowbytes + 1));
|
nuclear@2
|
1812 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
|
nuclear@2
|
1813 }
|
nuclear@2
|
1814
|
nuclear@2
|
1815 if (png_ptr->do_filter & PNG_FILTER_AVG)
|
nuclear@2
|
1816 {
|
nuclear@2
|
1817 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
|
nuclear@2
|
1818 (png_uint_32)(png_ptr->rowbytes + 1));
|
nuclear@2
|
1819 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
|
nuclear@2
|
1820 }
|
nuclear@2
|
1821
|
nuclear@2
|
1822 if (png_ptr->do_filter & PNG_FILTER_PAETH)
|
nuclear@2
|
1823 {
|
nuclear@2
|
1824 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
|
nuclear@2
|
1825 (png_uint_32)(png_ptr->rowbytes + 1));
|
nuclear@2
|
1826 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
|
nuclear@2
|
1827 }
|
nuclear@2
|
1828 }
|
nuclear@2
|
1829 #endif /* PNG_NO_WRITE_FILTER */
|
nuclear@2
|
1830
|
nuclear@2
|
1831 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
nuclear@2
|
1832 /* if interlaced, we need to set up width and height of pass */
|
nuclear@2
|
1833 if (png_ptr->interlaced)
|
nuclear@2
|
1834 {
|
nuclear@2
|
1835 if (!(png_ptr->transformations & PNG_INTERLACE))
|
nuclear@2
|
1836 {
|
nuclear@2
|
1837 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
|
nuclear@2
|
1838 png_pass_ystart[0]) / png_pass_yinc[0];
|
nuclear@2
|
1839 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
|
nuclear@2
|
1840 png_pass_start[0]) / png_pass_inc[0];
|
nuclear@2
|
1841 }
|
nuclear@2
|
1842 else
|
nuclear@2
|
1843 {
|
nuclear@2
|
1844 png_ptr->num_rows = png_ptr->height;
|
nuclear@2
|
1845 png_ptr->usr_width = png_ptr->width;
|
nuclear@2
|
1846 }
|
nuclear@2
|
1847 }
|
nuclear@2
|
1848 else
|
nuclear@2
|
1849 #endif
|
nuclear@2
|
1850 {
|
nuclear@2
|
1851 png_ptr->num_rows = png_ptr->height;
|
nuclear@2
|
1852 png_ptr->usr_width = png_ptr->width;
|
nuclear@2
|
1853 }
|
nuclear@2
|
1854 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
nuclear@2
|
1855 png_ptr->zstream.next_out = png_ptr->zbuf;
|
nuclear@2
|
1856 }
|
nuclear@2
|
1857
|
nuclear@2
|
1858 /* Internal use only. Called when finished processing a row of data. */
|
nuclear@2
|
1859 void /* PRIVATE */
|
nuclear@2
|
1860 png_write_finish_row(png_structp png_ptr)
|
nuclear@2
|
1861 {
|
nuclear@2
|
1862 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
nuclear@2
|
1863 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1864 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
|
nuclear@2
|
1865
|
nuclear@2
|
1866 /* start of interlace block */
|
nuclear@2
|
1867 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
|
nuclear@2
|
1868
|
nuclear@2
|
1869 /* offset to next interlace block */
|
nuclear@2
|
1870 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
|
nuclear@2
|
1871
|
nuclear@2
|
1872 /* start of interlace block in the y direction */
|
nuclear@2
|
1873 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
|
nuclear@2
|
1874
|
nuclear@2
|
1875 /* offset to next interlace block in the y direction */
|
nuclear@2
|
1876 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
|
nuclear@2
|
1877 #endif
|
nuclear@2
|
1878 #endif
|
nuclear@2
|
1879
|
nuclear@2
|
1880 int ret;
|
nuclear@2
|
1881
|
nuclear@2
|
1882 png_debug(1, "in png_write_finish_row\n");
|
nuclear@2
|
1883 /* next row */
|
nuclear@2
|
1884 png_ptr->row_number++;
|
nuclear@2
|
1885
|
nuclear@2
|
1886 /* see if we are done */
|
nuclear@2
|
1887 if (png_ptr->row_number < png_ptr->num_rows)
|
nuclear@2
|
1888 return;
|
nuclear@2
|
1889
|
nuclear@2
|
1890 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
nuclear@2
|
1891 /* if interlaced, go to next pass */
|
nuclear@2
|
1892 if (png_ptr->interlaced)
|
nuclear@2
|
1893 {
|
nuclear@2
|
1894 png_ptr->row_number = 0;
|
nuclear@2
|
1895 if (png_ptr->transformations & PNG_INTERLACE)
|
nuclear@2
|
1896 {
|
nuclear@2
|
1897 png_ptr->pass++;
|
nuclear@2
|
1898 }
|
nuclear@2
|
1899 else
|
nuclear@2
|
1900 {
|
nuclear@2
|
1901 /* loop until we find a non-zero width or height pass */
|
nuclear@2
|
1902 do
|
nuclear@2
|
1903 {
|
nuclear@2
|
1904 png_ptr->pass++;
|
nuclear@2
|
1905 if (png_ptr->pass >= 7)
|
nuclear@2
|
1906 break;
|
nuclear@2
|
1907 png_ptr->usr_width = (png_ptr->width +
|
nuclear@2
|
1908 png_pass_inc[png_ptr->pass] - 1 -
|
nuclear@2
|
1909 png_pass_start[png_ptr->pass]) /
|
nuclear@2
|
1910 png_pass_inc[png_ptr->pass];
|
nuclear@2
|
1911 png_ptr->num_rows = (png_ptr->height +
|
nuclear@2
|
1912 png_pass_yinc[png_ptr->pass] - 1 -
|
nuclear@2
|
1913 png_pass_ystart[png_ptr->pass]) /
|
nuclear@2
|
1914 png_pass_yinc[png_ptr->pass];
|
nuclear@2
|
1915 if (png_ptr->transformations & PNG_INTERLACE)
|
nuclear@2
|
1916 break;
|
nuclear@2
|
1917 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
|
nuclear@2
|
1918
|
nuclear@2
|
1919 }
|
nuclear@2
|
1920
|
nuclear@2
|
1921 /* reset the row above the image for the next pass */
|
nuclear@2
|
1922 if (png_ptr->pass < 7)
|
nuclear@2
|
1923 {
|
nuclear@2
|
1924 if (png_ptr->prev_row != NULL)
|
nuclear@2
|
1925 png_memset(png_ptr->prev_row, 0,
|
nuclear@2
|
1926 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
|
nuclear@2
|
1927 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
|
nuclear@2
|
1928 return;
|
nuclear@2
|
1929 }
|
nuclear@2
|
1930 }
|
nuclear@2
|
1931 #endif
|
nuclear@2
|
1932
|
nuclear@2
|
1933 /* if we get here, we've just written the last row, so we need
|
nuclear@2
|
1934 to flush the compressor */
|
nuclear@2
|
1935 do
|
nuclear@2
|
1936 {
|
nuclear@2
|
1937 /* tell the compressor we are done */
|
nuclear@2
|
1938 ret = deflate(&png_ptr->zstream, Z_FINISH);
|
nuclear@2
|
1939 /* check for an error */
|
nuclear@2
|
1940 if (ret == Z_OK)
|
nuclear@2
|
1941 {
|
nuclear@2
|
1942 /* check to see if we need more room */
|
nuclear@2
|
1943 if (!(png_ptr->zstream.avail_out))
|
nuclear@2
|
1944 {
|
nuclear@2
|
1945 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
|
nuclear@2
|
1946 png_ptr->zstream.next_out = png_ptr->zbuf;
|
nuclear@2
|
1947 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
nuclear@2
|
1948 }
|
nuclear@2
|
1949 }
|
nuclear@2
|
1950 else if (ret != Z_STREAM_END)
|
nuclear@2
|
1951 {
|
nuclear@2
|
1952 if (png_ptr->zstream.msg != NULL)
|
nuclear@2
|
1953 png_error(png_ptr, png_ptr->zstream.msg);
|
nuclear@2
|
1954 else
|
nuclear@2
|
1955 png_error(png_ptr, "zlib error");
|
nuclear@2
|
1956 }
|
nuclear@2
|
1957 } while (ret != Z_STREAM_END);
|
nuclear@2
|
1958
|
nuclear@2
|
1959 /* write any extra space */
|
nuclear@2
|
1960 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
|
nuclear@2
|
1961 {
|
nuclear@2
|
1962 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
|
nuclear@2
|
1963 png_ptr->zstream.avail_out);
|
nuclear@2
|
1964 }
|
nuclear@2
|
1965
|
nuclear@2
|
1966 deflateReset(&png_ptr->zstream);
|
nuclear@2
|
1967 png_ptr->zstream.data_type = Z_BINARY;
|
nuclear@2
|
1968 }
|
nuclear@2
|
1969
|
nuclear@2
|
1970 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
|
nuclear@2
|
1971 /* Pick out the correct pixels for the interlace pass.
|
nuclear@2
|
1972 * The basic idea here is to go through the row with a source
|
nuclear@2
|
1973 * pointer and a destination pointer (sp and dp), and copy the
|
nuclear@2
|
1974 * correct pixels for the pass. As the row gets compacted,
|
nuclear@2
|
1975 * sp will always be >= dp, so we should never overwrite anything.
|
nuclear@2
|
1976 * See the default: case for the easiest code to understand.
|
nuclear@2
|
1977 */
|
nuclear@2
|
1978 void /* PRIVATE */
|
nuclear@2
|
1979 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
|
nuclear@2
|
1980 {
|
nuclear@2
|
1981 #ifdef PNG_USE_LOCAL_ARRAYS
|
nuclear@2
|
1982 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
|
nuclear@2
|
1983
|
nuclear@2
|
1984 /* start of interlace block */
|
nuclear@2
|
1985 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
|
nuclear@2
|
1986
|
nuclear@2
|
1987 /* offset to next interlace block */
|
nuclear@2
|
1988 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
|
nuclear@2
|
1989 #endif
|
nuclear@2
|
1990
|
nuclear@2
|
1991 png_debug(1, "in png_do_write_interlace\n");
|
nuclear@2
|
1992 /* we don't have to do anything on the last pass (6) */
|
nuclear@2
|
1993 #if defined(PNG_USELESS_TESTS_SUPPORTED)
|
nuclear@2
|
1994 if (row != NULL && row_info != NULL && pass < 6)
|
nuclear@2
|
1995 #else
|
nuclear@2
|
1996 if (pass < 6)
|
nuclear@2
|
1997 #endif
|
nuclear@2
|
1998 {
|
nuclear@2
|
1999 /* each pixel depth is handled separately */
|
nuclear@2
|
2000 switch (row_info->pixel_depth)
|
nuclear@2
|
2001 {
|
nuclear@2
|
2002 case 1:
|
nuclear@2
|
2003 {
|
nuclear@2
|
2004 png_bytep sp;
|
nuclear@2
|
2005 png_bytep dp;
|
nuclear@2
|
2006 int shift;
|
nuclear@2
|
2007 int d;
|
nuclear@2
|
2008 int value;
|
nuclear@2
|
2009 png_uint_32 i;
|
nuclear@2
|
2010 png_uint_32 row_width = row_info->width;
|
nuclear@2
|
2011
|
nuclear@2
|
2012 dp = row;
|
nuclear@2
|
2013 d = 0;
|
nuclear@2
|
2014 shift = 7;
|
nuclear@2
|
2015 for (i = png_pass_start[pass]; i < row_width;
|
nuclear@2
|
2016 i += png_pass_inc[pass])
|
nuclear@2
|
2017 {
|
nuclear@2
|
2018 sp = row + (png_size_t)(i >> 3);
|
nuclear@2
|
2019 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
|
nuclear@2
|
2020 d |= (value << shift);
|
nuclear@2
|
2021
|
nuclear@2
|
2022 if (shift == 0)
|
nuclear@2
|
2023 {
|
nuclear@2
|
2024 shift = 7;
|
nuclear@2
|
2025 *dp++ = (png_byte)d;
|
nuclear@2
|
2026 d = 0;
|
nuclear@2
|
2027 }
|
nuclear@2
|
2028 else
|
nuclear@2
|
2029 shift--;
|
nuclear@2
|
2030
|
nuclear@2
|
2031 }
|
nuclear@2
|
2032 if (shift != 7)
|
nuclear@2
|
2033 *dp = (png_byte)d;
|
nuclear@2
|
2034 break;
|
nuclear@2
|
2035 }
|
nuclear@2
|
2036 case 2:
|
nuclear@2
|
2037 {
|
nuclear@2
|
2038 png_bytep sp;
|
nuclear@2
|
2039 png_bytep dp;
|
nuclear@2
|
2040 int shift;
|
nuclear@2
|
2041 int d;
|
nuclear@2
|
2042 int value;
|
nuclear@2
|
2043 png_uint_32 i;
|
nuclear@2
|
2044 png_uint_32 row_width = row_info->width;
|
nuclear@2
|
2045
|
nuclear@2
|
2046 dp = row;
|
nuclear@2
|
2047 shift = 6;
|
nuclear@2
|
2048 d = 0;
|
nuclear@2
|
2049 for (i = png_pass_start[pass]; i < row_width;
|
nuclear@2
|
2050 i += png_pass_inc[pass])
|
nuclear@2
|
2051 {
|
nuclear@2
|
2052 sp = row + (png_size_t)(i >> 2);
|
nuclear@2
|
2053 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
|
nuclear@2
|
2054 d |= (value << shift);
|
nuclear@2
|
2055
|
nuclear@2
|
2056 if (shift == 0)
|
nuclear@2
|
2057 {
|
nuclear@2
|
2058 shift = 6;
|
nuclear@2
|
2059 *dp++ = (png_byte)d;
|
nuclear@2
|
2060 d = 0;
|
nuclear@2
|
2061 }
|
nuclear@2
|
2062 else
|
nuclear@2
|
2063 shift -= 2;
|
nuclear@2
|
2064 }
|
nuclear@2
|
2065 if (shift != 6)
|
nuclear@2
|
2066 *dp = (png_byte)d;
|
nuclear@2
|
2067 break;
|
nuclear@2
|
2068 }
|
nuclear@2
|
2069 case 4:
|
nuclear@2
|
2070 {
|
nuclear@2
|
2071 png_bytep sp;
|
nuclear@2
|
2072 png_bytep dp;
|
nuclear@2
|
2073 int shift;
|
nuclear@2
|
2074 int d;
|
nuclear@2
|
2075 int value;
|
nuclear@2
|
2076 png_uint_32 i;
|
nuclear@2
|
2077 png_uint_32 row_width = row_info->width;
|
nuclear@2
|
2078
|
nuclear@2
|
2079 dp = row;
|
nuclear@2
|
2080 shift = 4;
|
nuclear@2
|
2081 d = 0;
|
nuclear@2
|
2082 for (i = png_pass_start[pass]; i < row_width;
|
nuclear@2
|
2083 i += png_pass_inc[pass])
|
nuclear@2
|
2084 {
|
nuclear@2
|
2085 sp = row + (png_size_t)(i >> 1);
|
nuclear@2
|
2086 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
|
nuclear@2
|
2087 d |= (value << shift);
|
nuclear@2
|
2088
|
nuclear@2
|
2089 if (shift == 0)
|
nuclear@2
|
2090 {
|
nuclear@2
|
2091 shift = 4;
|
nuclear@2
|
2092 *dp++ = (png_byte)d;
|
nuclear@2
|
2093 d = 0;
|
nuclear@2
|
2094 }
|
nuclear@2
|
2095 else
|
nuclear@2
|
2096 shift -= 4;
|
nuclear@2
|
2097 }
|
nuclear@2
|
2098 if (shift != 4)
|
nuclear@2
|
2099 *dp = (png_byte)d;
|
nuclear@2
|
2100 break;
|
nuclear@2
|
2101 }
|
nuclear@2
|
2102 default:
|
nuclear@2
|
2103 {
|
nuclear@2
|
2104 png_bytep sp;
|
nuclear@2
|
2105 png_bytep dp;
|
nuclear@2
|
2106 png_uint_32 i;
|
nuclear@2
|
2107 png_uint_32 row_width = row_info->width;
|
nuclear@2
|
2108 png_size_t pixel_bytes;
|
nuclear@2
|
2109
|
nuclear@2
|
2110 /* start at the beginning */
|
nuclear@2
|
2111 dp = row;
|
nuclear@2
|
2112 /* find out how many bytes each pixel takes up */
|
nuclear@2
|
2113 pixel_bytes = (row_info->pixel_depth >> 3);
|
nuclear@2
|
2114 /* loop through the row, only looking at the pixels that
|
nuclear@2
|
2115 matter */
|
nuclear@2
|
2116 for (i = png_pass_start[pass]; i < row_width;
|
nuclear@2
|
2117 i += png_pass_inc[pass])
|
nuclear@2
|
2118 {
|
nuclear@2
|
2119 /* find out where the original pixel is */
|
nuclear@2
|
2120 sp = row + (png_size_t)i * pixel_bytes;
|
nuclear@2
|
2121 /* move the pixel */
|
nuclear@2
|
2122 if (dp != sp)
|
nuclear@2
|
2123 png_memcpy(dp, sp, pixel_bytes);
|
nuclear@2
|
2124 /* next pixel */
|
nuclear@2
|
2125 dp += pixel_bytes;
|
nuclear@2
|
2126 }
|
nuclear@2
|
2127 break;
|
nuclear@2
|
2128 }
|
nuclear@2
|
2129 }
|
nuclear@2
|
2130 /* set new row width */
|
nuclear@2
|
2131 row_info->width = (row_info->width +
|
nuclear@2
|
2132 png_pass_inc[pass] - 1 -
|
nuclear@2
|
2133 png_pass_start[pass]) /
|
nuclear@2
|
2134 png_pass_inc[pass];
|
nuclear@2
|
2135 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
|
nuclear@2
|
2136 row_info->width);
|
nuclear@2
|
2137 }
|
nuclear@2
|
2138 }
|
nuclear@2
|
2139 #endif
|
nuclear@2
|
2140
|
nuclear@2
|
2141 /* This filters the row, chooses which filter to use, if it has not already
|
nuclear@2
|
2142 * been specified by the application, and then writes the row out with the
|
nuclear@2
|
2143 * chosen filter.
|
nuclear@2
|
2144 */
|
nuclear@2
|
2145 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
|
nuclear@2
|
2146 #define PNG_HISHIFT 10
|
nuclear@2
|
2147 #define PNG_LOMASK ((png_uint_32)0xffffL)
|
nuclear@2
|
2148 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
|
nuclear@2
|
2149 void /* PRIVATE */
|
nuclear@2
|
2150 png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
|
nuclear@2
|
2151 {
|
nuclear@2
|
2152 png_bytep best_row;
|
nuclear@2
|
2153 #ifndef PNG_NO_WRITE_FILTER
|
nuclear@2
|
2154 png_bytep prev_row, row_buf;
|
nuclear@2
|
2155 png_uint_32 mins, bpp;
|
nuclear@2
|
2156 png_byte filter_to_do = png_ptr->do_filter;
|
nuclear@2
|
2157 png_uint_32 row_bytes = row_info->rowbytes;
|
nuclear@2
|
2158 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2159 int num_p_filters = (int)png_ptr->num_prev_filters;
|
nuclear@2
|
2160 #endif
|
nuclear@2
|
2161
|
nuclear@2
|
2162 png_debug(1, "in png_write_find_filter\n");
|
nuclear@2
|
2163 /* find out how many bytes offset each pixel is */
|
nuclear@2
|
2164 bpp = (row_info->pixel_depth + 7) >> 3;
|
nuclear@2
|
2165
|
nuclear@2
|
2166 prev_row = png_ptr->prev_row;
|
nuclear@2
|
2167 #endif
|
nuclear@2
|
2168 best_row = png_ptr->row_buf;
|
nuclear@2
|
2169 #ifndef PNG_NO_WRITE_FILTER
|
nuclear@2
|
2170 row_buf = best_row;
|
nuclear@2
|
2171 mins = PNG_MAXSUM;
|
nuclear@2
|
2172
|
nuclear@2
|
2173 /* The prediction method we use is to find which method provides the
|
nuclear@2
|
2174 * smallest value when summing the absolute values of the distances
|
nuclear@2
|
2175 * from zero, using anything >= 128 as negative numbers. This is known
|
nuclear@2
|
2176 * as the "minimum sum of absolute differences" heuristic. Other
|
nuclear@2
|
2177 * heuristics are the "weighted minimum sum of absolute differences"
|
nuclear@2
|
2178 * (experimental and can in theory improve compression), and the "zlib
|
nuclear@2
|
2179 * predictive" method (not implemented yet), which does test compressions
|
nuclear@2
|
2180 * of lines using different filter methods, and then chooses the
|
nuclear@2
|
2181 * (series of) filter(s) that give minimum compressed data size (VERY
|
nuclear@2
|
2182 * computationally expensive).
|
nuclear@2
|
2183 *
|
nuclear@2
|
2184 * GRR 980525: consider also
|
nuclear@2
|
2185 * (1) minimum sum of absolute differences from running average (i.e.,
|
nuclear@2
|
2186 * keep running sum of non-absolute differences & count of bytes)
|
nuclear@2
|
2187 * [track dispersion, too? restart average if dispersion too large?]
|
nuclear@2
|
2188 * (1b) minimum sum of absolute differences from sliding average, probably
|
nuclear@2
|
2189 * with window size <= deflate window (usually 32K)
|
nuclear@2
|
2190 * (2) minimum sum of squared differences from zero or running average
|
nuclear@2
|
2191 * (i.e., ~ root-mean-square approach)
|
nuclear@2
|
2192 */
|
nuclear@2
|
2193
|
nuclear@2
|
2194
|
nuclear@2
|
2195 /* We don't need to test the 'no filter' case if this is the only filter
|
nuclear@2
|
2196 * that has been chosen, as it doesn't actually do anything to the data.
|
nuclear@2
|
2197 */
|
nuclear@2
|
2198 if ((filter_to_do & PNG_FILTER_NONE) &&
|
nuclear@2
|
2199 filter_to_do != PNG_FILTER_NONE)
|
nuclear@2
|
2200 {
|
nuclear@2
|
2201 png_bytep rp;
|
nuclear@2
|
2202 png_uint_32 sum = 0;
|
nuclear@2
|
2203 png_uint_32 i;
|
nuclear@2
|
2204 int v;
|
nuclear@2
|
2205
|
nuclear@2
|
2206 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
|
nuclear@2
|
2207 {
|
nuclear@2
|
2208 v = *rp;
|
nuclear@2
|
2209 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2210 }
|
nuclear@2
|
2211
|
nuclear@2
|
2212 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2213 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2214 {
|
nuclear@2
|
2215 png_uint_32 sumhi, sumlo;
|
nuclear@2
|
2216 int j;
|
nuclear@2
|
2217 sumlo = sum & PNG_LOMASK;
|
nuclear@2
|
2218 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
|
nuclear@2
|
2219
|
nuclear@2
|
2220 /* Reduce the sum if we match any of the previous rows */
|
nuclear@2
|
2221 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2222 {
|
nuclear@2
|
2223 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
|
nuclear@2
|
2224 {
|
nuclear@2
|
2225 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2226 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2227 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2228 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2229 }
|
nuclear@2
|
2230 }
|
nuclear@2
|
2231
|
nuclear@2
|
2232 /* Factor in the cost of this filter (this is here for completeness,
|
nuclear@2
|
2233 * but it makes no sense to have a "cost" for the NONE filter, as
|
nuclear@2
|
2234 * it has the minimum possible computational cost - none).
|
nuclear@2
|
2235 */
|
nuclear@2
|
2236 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
|
nuclear@2
|
2237 PNG_COST_SHIFT;
|
nuclear@2
|
2238 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
|
nuclear@2
|
2239 PNG_COST_SHIFT;
|
nuclear@2
|
2240
|
nuclear@2
|
2241 if (sumhi > PNG_HIMASK)
|
nuclear@2
|
2242 sum = PNG_MAXSUM;
|
nuclear@2
|
2243 else
|
nuclear@2
|
2244 sum = (sumhi << PNG_HISHIFT) + sumlo;
|
nuclear@2
|
2245 }
|
nuclear@2
|
2246 #endif
|
nuclear@2
|
2247 mins = sum;
|
nuclear@2
|
2248 }
|
nuclear@2
|
2249
|
nuclear@2
|
2250 /* sub filter */
|
nuclear@2
|
2251 if (filter_to_do == PNG_FILTER_SUB)
|
nuclear@2
|
2252 /* it's the only filter so no testing is needed */
|
nuclear@2
|
2253 {
|
nuclear@2
|
2254 png_bytep rp, lp, dp;
|
nuclear@2
|
2255 png_uint_32 i;
|
nuclear@2
|
2256 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
|
nuclear@2
|
2257 i++, rp++, dp++)
|
nuclear@2
|
2258 {
|
nuclear@2
|
2259 *dp = *rp;
|
nuclear@2
|
2260 }
|
nuclear@2
|
2261 for (lp = row_buf + 1; i < row_bytes;
|
nuclear@2
|
2262 i++, rp++, lp++, dp++)
|
nuclear@2
|
2263 {
|
nuclear@2
|
2264 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
|
nuclear@2
|
2265 }
|
nuclear@2
|
2266 best_row = png_ptr->sub_row;
|
nuclear@2
|
2267 }
|
nuclear@2
|
2268
|
nuclear@2
|
2269 else if (filter_to_do & PNG_FILTER_SUB)
|
nuclear@2
|
2270 {
|
nuclear@2
|
2271 png_bytep rp, dp, lp;
|
nuclear@2
|
2272 png_uint_32 sum = 0, lmins = mins;
|
nuclear@2
|
2273 png_uint_32 i;
|
nuclear@2
|
2274 int v;
|
nuclear@2
|
2275
|
nuclear@2
|
2276 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2277 /* We temporarily increase the "minimum sum" by the factor we
|
nuclear@2
|
2278 * would reduce the sum of this filter, so that we can do the
|
nuclear@2
|
2279 * early exit comparison without scaling the sum each time.
|
nuclear@2
|
2280 */
|
nuclear@2
|
2281 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2282 {
|
nuclear@2
|
2283 int j;
|
nuclear@2
|
2284 png_uint_32 lmhi, lmlo;
|
nuclear@2
|
2285 lmlo = lmins & PNG_LOMASK;
|
nuclear@2
|
2286 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2287
|
nuclear@2
|
2288 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2289 {
|
nuclear@2
|
2290 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
|
nuclear@2
|
2291 {
|
nuclear@2
|
2292 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2293 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2294 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2295 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2296 }
|
nuclear@2
|
2297 }
|
nuclear@2
|
2298
|
nuclear@2
|
2299 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
|
nuclear@2
|
2300 PNG_COST_SHIFT;
|
nuclear@2
|
2301 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
|
nuclear@2
|
2302 PNG_COST_SHIFT;
|
nuclear@2
|
2303
|
nuclear@2
|
2304 if (lmhi > PNG_HIMASK)
|
nuclear@2
|
2305 lmins = PNG_MAXSUM;
|
nuclear@2
|
2306 else
|
nuclear@2
|
2307 lmins = (lmhi << PNG_HISHIFT) + lmlo;
|
nuclear@2
|
2308 }
|
nuclear@2
|
2309 #endif
|
nuclear@2
|
2310
|
nuclear@2
|
2311 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
|
nuclear@2
|
2312 i++, rp++, dp++)
|
nuclear@2
|
2313 {
|
nuclear@2
|
2314 v = *dp = *rp;
|
nuclear@2
|
2315
|
nuclear@2
|
2316 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2317 }
|
nuclear@2
|
2318 for (lp = row_buf + 1; i < row_bytes;
|
nuclear@2
|
2319 i++, rp++, lp++, dp++)
|
nuclear@2
|
2320 {
|
nuclear@2
|
2321 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
|
nuclear@2
|
2322
|
nuclear@2
|
2323 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2324
|
nuclear@2
|
2325 if (sum > lmins) /* We are already worse, don't continue. */
|
nuclear@2
|
2326 break;
|
nuclear@2
|
2327 }
|
nuclear@2
|
2328
|
nuclear@2
|
2329 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2330 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2331 {
|
nuclear@2
|
2332 int j;
|
nuclear@2
|
2333 png_uint_32 sumhi, sumlo;
|
nuclear@2
|
2334 sumlo = sum & PNG_LOMASK;
|
nuclear@2
|
2335 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2336
|
nuclear@2
|
2337 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2338 {
|
nuclear@2
|
2339 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
|
nuclear@2
|
2340 {
|
nuclear@2
|
2341 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2342 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2343 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2344 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2345 }
|
nuclear@2
|
2346 }
|
nuclear@2
|
2347
|
nuclear@2
|
2348 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
|
nuclear@2
|
2349 PNG_COST_SHIFT;
|
nuclear@2
|
2350 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
|
nuclear@2
|
2351 PNG_COST_SHIFT;
|
nuclear@2
|
2352
|
nuclear@2
|
2353 if (sumhi > PNG_HIMASK)
|
nuclear@2
|
2354 sum = PNG_MAXSUM;
|
nuclear@2
|
2355 else
|
nuclear@2
|
2356 sum = (sumhi << PNG_HISHIFT) + sumlo;
|
nuclear@2
|
2357 }
|
nuclear@2
|
2358 #endif
|
nuclear@2
|
2359
|
nuclear@2
|
2360 if (sum < mins)
|
nuclear@2
|
2361 {
|
nuclear@2
|
2362 mins = sum;
|
nuclear@2
|
2363 best_row = png_ptr->sub_row;
|
nuclear@2
|
2364 }
|
nuclear@2
|
2365 }
|
nuclear@2
|
2366
|
nuclear@2
|
2367 /* up filter */
|
nuclear@2
|
2368 if (filter_to_do == PNG_FILTER_UP)
|
nuclear@2
|
2369 {
|
nuclear@2
|
2370 png_bytep rp, dp, pp;
|
nuclear@2
|
2371 png_uint_32 i;
|
nuclear@2
|
2372
|
nuclear@2
|
2373 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
|
nuclear@2
|
2374 pp = prev_row + 1; i < row_bytes;
|
nuclear@2
|
2375 i++, rp++, pp++, dp++)
|
nuclear@2
|
2376 {
|
nuclear@2
|
2377 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
|
nuclear@2
|
2378 }
|
nuclear@2
|
2379 best_row = png_ptr->up_row;
|
nuclear@2
|
2380 }
|
nuclear@2
|
2381
|
nuclear@2
|
2382 else if (filter_to_do & PNG_FILTER_UP)
|
nuclear@2
|
2383 {
|
nuclear@2
|
2384 png_bytep rp, dp, pp;
|
nuclear@2
|
2385 png_uint_32 sum = 0, lmins = mins;
|
nuclear@2
|
2386 png_uint_32 i;
|
nuclear@2
|
2387 int v;
|
nuclear@2
|
2388
|
nuclear@2
|
2389
|
nuclear@2
|
2390 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2391 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2392 {
|
nuclear@2
|
2393 int j;
|
nuclear@2
|
2394 png_uint_32 lmhi, lmlo;
|
nuclear@2
|
2395 lmlo = lmins & PNG_LOMASK;
|
nuclear@2
|
2396 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2397
|
nuclear@2
|
2398 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2399 {
|
nuclear@2
|
2400 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
|
nuclear@2
|
2401 {
|
nuclear@2
|
2402 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2403 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2404 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2405 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2406 }
|
nuclear@2
|
2407 }
|
nuclear@2
|
2408
|
nuclear@2
|
2409 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
|
nuclear@2
|
2410 PNG_COST_SHIFT;
|
nuclear@2
|
2411 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
|
nuclear@2
|
2412 PNG_COST_SHIFT;
|
nuclear@2
|
2413
|
nuclear@2
|
2414 if (lmhi > PNG_HIMASK)
|
nuclear@2
|
2415 lmins = PNG_MAXSUM;
|
nuclear@2
|
2416 else
|
nuclear@2
|
2417 lmins = (lmhi << PNG_HISHIFT) + lmlo;
|
nuclear@2
|
2418 }
|
nuclear@2
|
2419 #endif
|
nuclear@2
|
2420
|
nuclear@2
|
2421 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
|
nuclear@2
|
2422 pp = prev_row + 1; i < row_bytes; i++)
|
nuclear@2
|
2423 {
|
nuclear@2
|
2424 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
|
nuclear@2
|
2425
|
nuclear@2
|
2426 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2427
|
nuclear@2
|
2428 if (sum > lmins) /* We are already worse, don't continue. */
|
nuclear@2
|
2429 break;
|
nuclear@2
|
2430 }
|
nuclear@2
|
2431
|
nuclear@2
|
2432 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2433 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2434 {
|
nuclear@2
|
2435 int j;
|
nuclear@2
|
2436 png_uint_32 sumhi, sumlo;
|
nuclear@2
|
2437 sumlo = sum & PNG_LOMASK;
|
nuclear@2
|
2438 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2439
|
nuclear@2
|
2440 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2441 {
|
nuclear@2
|
2442 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
|
nuclear@2
|
2443 {
|
nuclear@2
|
2444 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2445 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2446 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2447 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2448 }
|
nuclear@2
|
2449 }
|
nuclear@2
|
2450
|
nuclear@2
|
2451 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
|
nuclear@2
|
2452 PNG_COST_SHIFT;
|
nuclear@2
|
2453 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
|
nuclear@2
|
2454 PNG_COST_SHIFT;
|
nuclear@2
|
2455
|
nuclear@2
|
2456 if (sumhi > PNG_HIMASK)
|
nuclear@2
|
2457 sum = PNG_MAXSUM;
|
nuclear@2
|
2458 else
|
nuclear@2
|
2459 sum = (sumhi << PNG_HISHIFT) + sumlo;
|
nuclear@2
|
2460 }
|
nuclear@2
|
2461 #endif
|
nuclear@2
|
2462
|
nuclear@2
|
2463 if (sum < mins)
|
nuclear@2
|
2464 {
|
nuclear@2
|
2465 mins = sum;
|
nuclear@2
|
2466 best_row = png_ptr->up_row;
|
nuclear@2
|
2467 }
|
nuclear@2
|
2468 }
|
nuclear@2
|
2469
|
nuclear@2
|
2470 /* avg filter */
|
nuclear@2
|
2471 if (filter_to_do == PNG_FILTER_AVG)
|
nuclear@2
|
2472 {
|
nuclear@2
|
2473 png_bytep rp, dp, pp, lp;
|
nuclear@2
|
2474 png_uint_32 i;
|
nuclear@2
|
2475 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
|
nuclear@2
|
2476 pp = prev_row + 1; i < bpp; i++)
|
nuclear@2
|
2477 {
|
nuclear@2
|
2478 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
|
nuclear@2
|
2479 }
|
nuclear@2
|
2480 for (lp = row_buf + 1; i < row_bytes; i++)
|
nuclear@2
|
2481 {
|
nuclear@2
|
2482 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
|
nuclear@2
|
2483 & 0xff);
|
nuclear@2
|
2484 }
|
nuclear@2
|
2485 best_row = png_ptr->avg_row;
|
nuclear@2
|
2486 }
|
nuclear@2
|
2487
|
nuclear@2
|
2488 else if (filter_to_do & PNG_FILTER_AVG)
|
nuclear@2
|
2489 {
|
nuclear@2
|
2490 png_bytep rp, dp, pp, lp;
|
nuclear@2
|
2491 png_uint_32 sum = 0, lmins = mins;
|
nuclear@2
|
2492 png_uint_32 i;
|
nuclear@2
|
2493 int v;
|
nuclear@2
|
2494
|
nuclear@2
|
2495 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2496 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2497 {
|
nuclear@2
|
2498 int j;
|
nuclear@2
|
2499 png_uint_32 lmhi, lmlo;
|
nuclear@2
|
2500 lmlo = lmins & PNG_LOMASK;
|
nuclear@2
|
2501 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2502
|
nuclear@2
|
2503 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2504 {
|
nuclear@2
|
2505 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
|
nuclear@2
|
2506 {
|
nuclear@2
|
2507 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2508 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2509 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2510 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2511 }
|
nuclear@2
|
2512 }
|
nuclear@2
|
2513
|
nuclear@2
|
2514 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
|
nuclear@2
|
2515 PNG_COST_SHIFT;
|
nuclear@2
|
2516 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
|
nuclear@2
|
2517 PNG_COST_SHIFT;
|
nuclear@2
|
2518
|
nuclear@2
|
2519 if (lmhi > PNG_HIMASK)
|
nuclear@2
|
2520 lmins = PNG_MAXSUM;
|
nuclear@2
|
2521 else
|
nuclear@2
|
2522 lmins = (lmhi << PNG_HISHIFT) + lmlo;
|
nuclear@2
|
2523 }
|
nuclear@2
|
2524 #endif
|
nuclear@2
|
2525
|
nuclear@2
|
2526 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
|
nuclear@2
|
2527 pp = prev_row + 1; i < bpp; i++)
|
nuclear@2
|
2528 {
|
nuclear@2
|
2529 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
|
nuclear@2
|
2530
|
nuclear@2
|
2531 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2532 }
|
nuclear@2
|
2533 for (lp = row_buf + 1; i < row_bytes; i++)
|
nuclear@2
|
2534 {
|
nuclear@2
|
2535 v = *dp++ =
|
nuclear@2
|
2536 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
|
nuclear@2
|
2537
|
nuclear@2
|
2538 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2539
|
nuclear@2
|
2540 if (sum > lmins) /* We are already worse, don't continue. */
|
nuclear@2
|
2541 break;
|
nuclear@2
|
2542 }
|
nuclear@2
|
2543
|
nuclear@2
|
2544 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2545 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2546 {
|
nuclear@2
|
2547 int j;
|
nuclear@2
|
2548 png_uint_32 sumhi, sumlo;
|
nuclear@2
|
2549 sumlo = sum & PNG_LOMASK;
|
nuclear@2
|
2550 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2551
|
nuclear@2
|
2552 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2553 {
|
nuclear@2
|
2554 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
|
nuclear@2
|
2555 {
|
nuclear@2
|
2556 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2557 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2558 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2559 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2560 }
|
nuclear@2
|
2561 }
|
nuclear@2
|
2562
|
nuclear@2
|
2563 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
|
nuclear@2
|
2564 PNG_COST_SHIFT;
|
nuclear@2
|
2565 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
|
nuclear@2
|
2566 PNG_COST_SHIFT;
|
nuclear@2
|
2567
|
nuclear@2
|
2568 if (sumhi > PNG_HIMASK)
|
nuclear@2
|
2569 sum = PNG_MAXSUM;
|
nuclear@2
|
2570 else
|
nuclear@2
|
2571 sum = (sumhi << PNG_HISHIFT) + sumlo;
|
nuclear@2
|
2572 }
|
nuclear@2
|
2573 #endif
|
nuclear@2
|
2574
|
nuclear@2
|
2575 if (sum < mins)
|
nuclear@2
|
2576 {
|
nuclear@2
|
2577 mins = sum;
|
nuclear@2
|
2578 best_row = png_ptr->avg_row;
|
nuclear@2
|
2579 }
|
nuclear@2
|
2580 }
|
nuclear@2
|
2581
|
nuclear@2
|
2582 /* Paeth filter */
|
nuclear@2
|
2583 if (filter_to_do == PNG_FILTER_PAETH)
|
nuclear@2
|
2584 {
|
nuclear@2
|
2585 png_bytep rp, dp, pp, cp, lp;
|
nuclear@2
|
2586 png_uint_32 i;
|
nuclear@2
|
2587 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
|
nuclear@2
|
2588 pp = prev_row + 1; i < bpp; i++)
|
nuclear@2
|
2589 {
|
nuclear@2
|
2590 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
|
nuclear@2
|
2591 }
|
nuclear@2
|
2592
|
nuclear@2
|
2593 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
|
nuclear@2
|
2594 {
|
nuclear@2
|
2595 int a, b, c, pa, pb, pc, p;
|
nuclear@2
|
2596
|
nuclear@2
|
2597 b = *pp++;
|
nuclear@2
|
2598 c = *cp++;
|
nuclear@2
|
2599 a = *lp++;
|
nuclear@2
|
2600
|
nuclear@2
|
2601 p = b - c;
|
nuclear@2
|
2602 pc = a - c;
|
nuclear@2
|
2603
|
nuclear@2
|
2604 #ifdef PNG_USE_ABS
|
nuclear@2
|
2605 pa = abs(p);
|
nuclear@2
|
2606 pb = abs(pc);
|
nuclear@2
|
2607 pc = abs(p + pc);
|
nuclear@2
|
2608 #else
|
nuclear@2
|
2609 pa = p < 0 ? -p : p;
|
nuclear@2
|
2610 pb = pc < 0 ? -pc : pc;
|
nuclear@2
|
2611 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
|
nuclear@2
|
2612 #endif
|
nuclear@2
|
2613
|
nuclear@2
|
2614 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
|
nuclear@2
|
2615
|
nuclear@2
|
2616 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
|
nuclear@2
|
2617 }
|
nuclear@2
|
2618 best_row = png_ptr->paeth_row;
|
nuclear@2
|
2619 }
|
nuclear@2
|
2620
|
nuclear@2
|
2621 else if (filter_to_do & PNG_FILTER_PAETH)
|
nuclear@2
|
2622 {
|
nuclear@2
|
2623 png_bytep rp, dp, pp, cp, lp;
|
nuclear@2
|
2624 png_uint_32 sum = 0, lmins = mins;
|
nuclear@2
|
2625 png_uint_32 i;
|
nuclear@2
|
2626 int v;
|
nuclear@2
|
2627
|
nuclear@2
|
2628 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2629 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2630 {
|
nuclear@2
|
2631 int j;
|
nuclear@2
|
2632 png_uint_32 lmhi, lmlo;
|
nuclear@2
|
2633 lmlo = lmins & PNG_LOMASK;
|
nuclear@2
|
2634 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2635
|
nuclear@2
|
2636 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2637 {
|
nuclear@2
|
2638 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
|
nuclear@2
|
2639 {
|
nuclear@2
|
2640 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2641 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2642 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
|
nuclear@2
|
2643 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2644 }
|
nuclear@2
|
2645 }
|
nuclear@2
|
2646
|
nuclear@2
|
2647 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
|
nuclear@2
|
2648 PNG_COST_SHIFT;
|
nuclear@2
|
2649 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
|
nuclear@2
|
2650 PNG_COST_SHIFT;
|
nuclear@2
|
2651
|
nuclear@2
|
2652 if (lmhi > PNG_HIMASK)
|
nuclear@2
|
2653 lmins = PNG_MAXSUM;
|
nuclear@2
|
2654 else
|
nuclear@2
|
2655 lmins = (lmhi << PNG_HISHIFT) + lmlo;
|
nuclear@2
|
2656 }
|
nuclear@2
|
2657 #endif
|
nuclear@2
|
2658
|
nuclear@2
|
2659 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
|
nuclear@2
|
2660 pp = prev_row + 1; i < bpp; i++)
|
nuclear@2
|
2661 {
|
nuclear@2
|
2662 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
|
nuclear@2
|
2663
|
nuclear@2
|
2664 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2665 }
|
nuclear@2
|
2666
|
nuclear@2
|
2667 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
|
nuclear@2
|
2668 {
|
nuclear@2
|
2669 int a, b, c, pa, pb, pc, p;
|
nuclear@2
|
2670
|
nuclear@2
|
2671 b = *pp++;
|
nuclear@2
|
2672 c = *cp++;
|
nuclear@2
|
2673 a = *lp++;
|
nuclear@2
|
2674
|
nuclear@2
|
2675 #ifndef PNG_SLOW_PAETH
|
nuclear@2
|
2676 p = b - c;
|
nuclear@2
|
2677 pc = a - c;
|
nuclear@2
|
2678 #ifdef PNG_USE_ABS
|
nuclear@2
|
2679 pa = abs(p);
|
nuclear@2
|
2680 pb = abs(pc);
|
nuclear@2
|
2681 pc = abs(p + pc);
|
nuclear@2
|
2682 #else
|
nuclear@2
|
2683 pa = p < 0 ? -p : p;
|
nuclear@2
|
2684 pb = pc < 0 ? -pc : pc;
|
nuclear@2
|
2685 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
|
nuclear@2
|
2686 #endif
|
nuclear@2
|
2687 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
|
nuclear@2
|
2688 #else /* PNG_SLOW_PAETH */
|
nuclear@2
|
2689 p = a + b - c;
|
nuclear@2
|
2690 pa = abs(p - a);
|
nuclear@2
|
2691 pb = abs(p - b);
|
nuclear@2
|
2692 pc = abs(p - c);
|
nuclear@2
|
2693 if (pa <= pb && pa <= pc)
|
nuclear@2
|
2694 p = a;
|
nuclear@2
|
2695 else if (pb <= pc)
|
nuclear@2
|
2696 p = b;
|
nuclear@2
|
2697 else
|
nuclear@2
|
2698 p = c;
|
nuclear@2
|
2699 #endif /* PNG_SLOW_PAETH */
|
nuclear@2
|
2700
|
nuclear@2
|
2701 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
|
nuclear@2
|
2702
|
nuclear@2
|
2703 sum += (v < 128) ? v : 256 - v;
|
nuclear@2
|
2704
|
nuclear@2
|
2705 if (sum > lmins) /* We are already worse, don't continue. */
|
nuclear@2
|
2706 break;
|
nuclear@2
|
2707 }
|
nuclear@2
|
2708
|
nuclear@2
|
2709 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2710 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
nuclear@2
|
2711 {
|
nuclear@2
|
2712 int j;
|
nuclear@2
|
2713 png_uint_32 sumhi, sumlo;
|
nuclear@2
|
2714 sumlo = sum & PNG_LOMASK;
|
nuclear@2
|
2715 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
|
nuclear@2
|
2716
|
nuclear@2
|
2717 for (j = 0; j < num_p_filters; j++)
|
nuclear@2
|
2718 {
|
nuclear@2
|
2719 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
|
nuclear@2
|
2720 {
|
nuclear@2
|
2721 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2722 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2723 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
|
nuclear@2
|
2724 PNG_WEIGHT_SHIFT;
|
nuclear@2
|
2725 }
|
nuclear@2
|
2726 }
|
nuclear@2
|
2727
|
nuclear@2
|
2728 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
|
nuclear@2
|
2729 PNG_COST_SHIFT;
|
nuclear@2
|
2730 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
|
nuclear@2
|
2731 PNG_COST_SHIFT;
|
nuclear@2
|
2732
|
nuclear@2
|
2733 if (sumhi > PNG_HIMASK)
|
nuclear@2
|
2734 sum = PNG_MAXSUM;
|
nuclear@2
|
2735 else
|
nuclear@2
|
2736 sum = (sumhi << PNG_HISHIFT) + sumlo;
|
nuclear@2
|
2737 }
|
nuclear@2
|
2738 #endif
|
nuclear@2
|
2739
|
nuclear@2
|
2740 if (sum < mins)
|
nuclear@2
|
2741 {
|
nuclear@2
|
2742 best_row = png_ptr->paeth_row;
|
nuclear@2
|
2743 }
|
nuclear@2
|
2744 }
|
nuclear@2
|
2745 #endif /* PNG_NO_WRITE_FILTER */
|
nuclear@2
|
2746 /* Do the actual writing of the filtered row data from the chosen filter. */
|
nuclear@2
|
2747
|
nuclear@2
|
2748 png_write_filtered_row(png_ptr, best_row);
|
nuclear@2
|
2749
|
nuclear@2
|
2750 #ifndef PNG_NO_WRITE_FILTER
|
nuclear@2
|
2751 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
|
nuclear@2
|
2752 /* Save the type of filter we picked this time for future calculations */
|
nuclear@2
|
2753 if (png_ptr->num_prev_filters > 0)
|
nuclear@2
|
2754 {
|
nuclear@2
|
2755 int j;
|
nuclear@2
|
2756 for (j = 1; j < num_p_filters; j++)
|
nuclear@2
|
2757 {
|
nuclear@2
|
2758 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
|
nuclear@2
|
2759 }
|
nuclear@2
|
2760 png_ptr->prev_filters[j] = best_row[0];
|
nuclear@2
|
2761 }
|
nuclear@2
|
2762 #endif
|
nuclear@2
|
2763 #endif /* PNG_NO_WRITE_FILTER */
|
nuclear@2
|
2764 }
|
nuclear@2
|
2765
|
nuclear@2
|
2766
|
nuclear@2
|
2767 /* Do the actual writing of a previously filtered row. */
|
nuclear@2
|
2768 void /* PRIVATE */
|
nuclear@2
|
2769 png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
|
nuclear@2
|
2770 {
|
nuclear@2
|
2771 png_debug(1, "in png_write_filtered_row\n");
|
nuclear@2
|
2772 png_debug1(2, "filter = %d\n", filtered_row[0]);
|
nuclear@2
|
2773 /* set up the zlib input buffer */
|
nuclear@2
|
2774
|
nuclear@2
|
2775 png_ptr->zstream.next_in = filtered_row;
|
nuclear@2
|
2776 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
|
nuclear@2
|
2777 /* repeat until we have compressed all the data */
|
nuclear@2
|
2778 do
|
nuclear@2
|
2779 {
|
nuclear@2
|
2780 int ret; /* return of zlib */
|
nuclear@2
|
2781
|
nuclear@2
|
2782 /* compress the data */
|
nuclear@2
|
2783 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
|
nuclear@2
|
2784 /* check for compression errors */
|
nuclear@2
|
2785 if (ret != Z_OK)
|
nuclear@2
|
2786 {
|
nuclear@2
|
2787 if (png_ptr->zstream.msg != NULL)
|
nuclear@2
|
2788 png_error(png_ptr, png_ptr->zstream.msg);
|
nuclear@2
|
2789 else
|
nuclear@2
|
2790 png_error(png_ptr, "zlib error");
|
nuclear@2
|
2791 }
|
nuclear@2
|
2792
|
nuclear@2
|
2793 /* see if it is time to write another IDAT */
|
nuclear@2
|
2794 if (!(png_ptr->zstream.avail_out))
|
nuclear@2
|
2795 {
|
nuclear@2
|
2796 /* write the IDAT and reset the zlib output buffer */
|
nuclear@2
|
2797 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
|
nuclear@2
|
2798 png_ptr->zstream.next_out = png_ptr->zbuf;
|
nuclear@2
|
2799 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
|
nuclear@2
|
2800 }
|
nuclear@2
|
2801 /* repeat until all data has been compressed */
|
nuclear@2
|
2802 } while (png_ptr->zstream.avail_in);
|
nuclear@2
|
2803
|
nuclear@2
|
2804 /* swap the current and previous rows */
|
nuclear@2
|
2805 if (png_ptr->prev_row != NULL)
|
nuclear@2
|
2806 {
|
nuclear@2
|
2807 png_bytep tptr;
|
nuclear@2
|
2808
|
nuclear@2
|
2809 tptr = png_ptr->prev_row;
|
nuclear@2
|
2810 png_ptr->prev_row = png_ptr->row_buf;
|
nuclear@2
|
2811 png_ptr->row_buf = tptr;
|
nuclear@2
|
2812 }
|
nuclear@2
|
2813
|
nuclear@2
|
2814 /* finish row - updates counters and flushes zlib if last row */
|
nuclear@2
|
2815 png_write_finish_row(png_ptr);
|
nuclear@2
|
2816
|
nuclear@2
|
2817 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
|
nuclear@2
|
2818 png_ptr->flush_rows++;
|
nuclear@2
|
2819
|
nuclear@2
|
2820 if (png_ptr->flush_dist > 0 &&
|
nuclear@2
|
2821 png_ptr->flush_rows >= png_ptr->flush_dist)
|
nuclear@2
|
2822 {
|
nuclear@2
|
2823 png_write_flush(png_ptr);
|
nuclear@2
|
2824 }
|
nuclear@2
|
2825 #endif
|
nuclear@2
|
2826 }
|
nuclear@2
|
2827 #endif /* PNG_WRITE_SUPPORTED */
|