2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
5506
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
2
|
23 |
*/
|
|
24 |
|
|
25 |
/* pngwrite.c - general routines to write a PNG file
|
|
26 |
*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file and, per its terms, should not be removed:
|
|
31 |
*
|
10576
|
32 |
* Last changed in libpng 1.5.4 [July 7, 2011]
|
|
33 |
* Copyright (c) 1998-2011 Glenn Randers-Pehrson
|
2
|
34 |
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
|
35 |
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
10576
|
36 |
*
|
|
37 |
* This code is released under the libpng license.
|
|
38 |
* For conditions of distribution and use, see the disclaimer
|
|
39 |
* and license in png.h
|
2
|
40 |
*/
|
|
41 |
|
10576
|
42 |
#include "pngpriv.h"
|
|
43 |
|
2
|
44 |
#ifdef PNG_WRITE_SUPPORTED
|
|
45 |
|
|
46 |
/* Writes all the PNG information. This is the suggested way to use the
|
|
47 |
* library. If you have a new chunk to add, make a function to write it,
|
|
48 |
* and put it in the correct location here. If you want the chunk written
|
|
49 |
* after the image data, put it in png_write_end(). I strongly encourage
|
|
50 |
* you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
|
|
51 |
* the chunk, as that will keep the code from breaking if you want to just
|
|
52 |
* write a plain PNG file. If you have long comments, I suggest writing
|
|
53 |
* them in png_write_end(), and compressing them.
|
|
54 |
*/
|
|
55 |
void PNGAPI
|
|
56 |
png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
|
|
57 |
{
|
10576
|
58 |
png_debug(1, "in png_write_info_before_PLTE");
|
|
59 |
|
2
|
60 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
61 |
return;
|
10576
|
62 |
|
2
|
63 |
if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
|
|
64 |
{
|
10576
|
65 |
/* Write PNG signature */
|
|
66 |
png_write_sig(png_ptr);
|
|
67 |
|
|
68 |
#ifdef PNG_MNG_FEATURES_SUPPORTED
|
|
69 |
if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
|
|
70 |
(png_ptr->mng_features_permitted))
|
2
|
71 |
{
|
10576
|
72 |
png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
|
|
73 |
png_ptr->mng_features_permitted = 0;
|
2
|
74 |
}
|
|
75 |
#endif
|
10576
|
76 |
|
|
77 |
/* Write IHDR information. */
|
2
|
78 |
png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
|
10576
|
79 |
info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
|
|
80 |
info_ptr->filter_type,
|
|
81 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
|
82 |
info_ptr->interlace_type);
|
2
|
83 |
#else
|
10576
|
84 |
0);
|
2
|
85 |
#endif
|
10576
|
86 |
/* The rest of these check to see if the valid field has the appropriate
|
|
87 |
* flag set, and if it does, writes the chunk.
|
|
88 |
*/
|
|
89 |
#ifdef PNG_WRITE_gAMA_SUPPORTED
|
2
|
90 |
if (info_ptr->valid & PNG_INFO_gAMA)
|
10576
|
91 |
png_write_gAMA_fixed(png_ptr, info_ptr->gamma);
|
2
|
92 |
#endif
|
10576
|
93 |
#ifdef PNG_WRITE_sRGB_SUPPORTED
|
2
|
94 |
if (info_ptr->valid & PNG_INFO_sRGB)
|
|
95 |
png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
|
|
96 |
#endif
|
10576
|
97 |
|
|
98 |
#ifdef PNG_WRITE_iCCP_SUPPORTED
|
2
|
99 |
if (info_ptr->valid & PNG_INFO_iCCP)
|
|
100 |
png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
|
10576
|
101 |
(png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
|
2
|
102 |
#endif
|
10576
|
103 |
#ifdef PNG_WRITE_sBIT_SUPPORTED
|
2
|
104 |
if (info_ptr->valid & PNG_INFO_sBIT)
|
|
105 |
png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
|
|
106 |
#endif
|
10576
|
107 |
#ifdef PNG_WRITE_cHRM_SUPPORTED
|
2
|
108 |
if (info_ptr->valid & PNG_INFO_cHRM)
|
|
109 |
png_write_cHRM_fixed(png_ptr,
|
10576
|
110 |
info_ptr->x_white, info_ptr->y_white,
|
|
111 |
info_ptr->x_red, info_ptr->y_red,
|
|
112 |
info_ptr->x_green, info_ptr->y_green,
|
|
113 |
info_ptr->x_blue, info_ptr->y_blue);
|
2
|
114 |
#endif
|
10576
|
115 |
|
|
116 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
|
2
|
117 |
if (info_ptr->unknown_chunks_num)
|
|
118 |
{
|
10576
|
119 |
png_unknown_chunk *up;
|
|
120 |
|
|
121 |
png_debug(5, "writing extra chunks");
|
2
|
122 |
|
10576
|
123 |
for (up = info_ptr->unknown_chunks;
|
|
124 |
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
|
|
125 |
up++)
|
|
126 |
{
|
|
127 |
int keep = png_handle_as_unknown(png_ptr, up->name);
|
2
|
128 |
|
|
129 |
if (keep != PNG_HANDLE_CHUNK_NEVER &&
|
10576
|
130 |
up->location &&
|
|
131 |
!(up->location & PNG_HAVE_PLTE) &&
|
|
132 |
!(up->location & PNG_HAVE_IDAT) &&
|
|
133 |
!(up->location & PNG_AFTER_IDAT) &&
|
|
134 |
((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
|
|
135 |
(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
|
2
|
136 |
{
|
10576
|
137 |
if (up->size == 0)
|
|
138 |
png_warning(png_ptr, "Writing zero-length unknown chunk");
|
|
139 |
|
2
|
140 |
png_write_chunk(png_ptr, up->name, up->data, up->size);
|
|
141 |
}
|
10576
|
142 |
}
|
2
|
143 |
}
|
|
144 |
#endif
|
|
145 |
png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
void PNGAPI
|
|
150 |
png_write_info(png_structp png_ptr, png_infop info_ptr)
|
|
151 |
{
|
|
152 |
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
|
|
153 |
int i;
|
|
154 |
#endif
|
|
155 |
|
10576
|
156 |
png_debug(1, "in png_write_info");
|
2
|
157 |
|
|
158 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
159 |
return;
|
|
160 |
|
|
161 |
png_write_info_before_PLTE(png_ptr, info_ptr);
|
|
162 |
|
|
163 |
if (info_ptr->valid & PNG_INFO_PLTE)
|
|
164 |
png_write_PLTE(png_ptr, info_ptr->palette,
|
10576
|
165 |
(png_uint_32)info_ptr->num_palette);
|
|
166 |
|
2
|
167 |
else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
|
168 |
png_error(png_ptr, "Valid palette required for paletted images");
|
|
169 |
|
10576
|
170 |
#ifdef PNG_WRITE_tRNS_SUPPORTED
|
2
|
171 |
if (info_ptr->valid & PNG_INFO_tRNS)
|
10576
|
172 |
{
|
|
173 |
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
|
|
174 |
/* Invert the alpha channel (in tRNS) */
|
|
175 |
if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
|
|
176 |
info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
2
|
177 |
{
|
10576
|
178 |
int j;
|
|
179 |
for (j = 0; j<(int)info_ptr->num_trans; j++)
|
|
180 |
info_ptr->trans_alpha[j] =
|
|
181 |
(png_byte)(255 - info_ptr->trans_alpha[j]);
|
2
|
182 |
}
|
|
183 |
#endif
|
10576
|
184 |
png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
|
|
185 |
info_ptr->num_trans, info_ptr->color_type);
|
|
186 |
}
|
|
187 |
#endif
|
|
188 |
#ifdef PNG_WRITE_bKGD_SUPPORTED
|
2
|
189 |
if (info_ptr->valid & PNG_INFO_bKGD)
|
|
190 |
png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
|
|
191 |
#endif
|
10576
|
192 |
|
|
193 |
#ifdef PNG_WRITE_hIST_SUPPORTED
|
2
|
194 |
if (info_ptr->valid & PNG_INFO_hIST)
|
|
195 |
png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
|
|
196 |
#endif
|
10576
|
197 |
|
|
198 |
#ifdef PNG_WRITE_oFFs_SUPPORTED
|
2
|
199 |
if (info_ptr->valid & PNG_INFO_oFFs)
|
|
200 |
png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
|
10576
|
201 |
info_ptr->offset_unit_type);
|
2
|
202 |
#endif
|
10576
|
203 |
|
|
204 |
#ifdef PNG_WRITE_pCAL_SUPPORTED
|
2
|
205 |
if (info_ptr->valid & PNG_INFO_pCAL)
|
|
206 |
png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
|
10576
|
207 |
info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
|
|
208 |
info_ptr->pcal_units, info_ptr->pcal_params);
|
2
|
209 |
#endif
|
10576
|
210 |
|
|
211 |
#ifdef PNG_WRITE_sCAL_SUPPORTED
|
2
|
212 |
if (info_ptr->valid & PNG_INFO_sCAL)
|
|
213 |
png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
|
|
214 |
info_ptr->scal_s_width, info_ptr->scal_s_height);
|
10576
|
215 |
#endif /* sCAL */
|
|
216 |
|
|
217 |
#ifdef PNG_WRITE_pHYs_SUPPORTED
|
2
|
218 |
if (info_ptr->valid & PNG_INFO_pHYs)
|
|
219 |
png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
|
10576
|
220 |
info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
|
|
221 |
#endif /* pHYs */
|
|
222 |
|
|
223 |
#ifdef PNG_WRITE_tIME_SUPPORTED
|
2
|
224 |
if (info_ptr->valid & PNG_INFO_tIME)
|
|
225 |
{
|
|
226 |
png_write_tIME(png_ptr, &(info_ptr->mod_time));
|
|
227 |
png_ptr->mode |= PNG_WROTE_tIME;
|
|
228 |
}
|
10576
|
229 |
#endif /* tIME */
|
|
230 |
|
|
231 |
#ifdef PNG_WRITE_sPLT_SUPPORTED
|
2
|
232 |
if (info_ptr->valid & PNG_INFO_sPLT)
|
10576
|
233 |
for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
|
|
234 |
png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
|
|
235 |
#endif /* sPLT */
|
|
236 |
|
|
237 |
#ifdef PNG_WRITE_TEXT_SUPPORTED
|
2
|
238 |
/* Check to see if we need to write text chunks */
|
|
239 |
for (i = 0; i < info_ptr->num_text; i++)
|
|
240 |
{
|
10576
|
241 |
png_debug2(2, "Writing header text chunk %d, type %d", i,
|
|
242 |
info_ptr->text[i].compression);
|
|
243 |
/* An internationalized chunk? */
|
2
|
244 |
if (info_ptr->text[i].compression > 0)
|
|
245 |
{
|
10576
|
246 |
#ifdef PNG_WRITE_iTXt_SUPPORTED
|
|
247 |
/* Write international chunk */
|
|
248 |
png_write_iTXt(png_ptr,
|
|
249 |
info_ptr->text[i].compression,
|
|
250 |
info_ptr->text[i].key,
|
|
251 |
info_ptr->text[i].lang,
|
|
252 |
info_ptr->text[i].lang_key,
|
|
253 |
info_ptr->text[i].text);
|
2
|
254 |
#else
|
|
255 |
png_warning(png_ptr, "Unable to write international text");
|
|
256 |
#endif
|
|
257 |
/* Mark this chunk as written */
|
|
258 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
|
259 |
}
|
10576
|
260 |
|
2
|
261 |
/* If we want a compressed text chunk */
|
|
262 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
|
|
263 |
{
|
10576
|
264 |
#ifdef PNG_WRITE_zTXt_SUPPORTED
|
|
265 |
/* Write compressed chunk */
|
2
|
266 |
png_write_zTXt(png_ptr, info_ptr->text[i].key,
|
10576
|
267 |
info_ptr->text[i].text, 0,
|
|
268 |
info_ptr->text[i].compression);
|
2
|
269 |
#else
|
|
270 |
png_warning(png_ptr, "Unable to write compressed text");
|
|
271 |
#endif
|
|
272 |
/* Mark this chunk as written */
|
|
273 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
|
|
274 |
}
|
10576
|
275 |
|
2
|
276 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
|
|
277 |
{
|
10576
|
278 |
#ifdef PNG_WRITE_tEXt_SUPPORTED
|
|
279 |
/* Write uncompressed chunk */
|
2
|
280 |
png_write_tEXt(png_ptr, info_ptr->text[i].key,
|
10576
|
281 |
info_ptr->text[i].text,
|
|
282 |
0);
|
|
283 |
/* Mark this chunk as written */
|
|
284 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
2
|
285 |
#else
|
10576
|
286 |
/* Can't get here */
|
2
|
287 |
png_warning(png_ptr, "Unable to write uncompressed text");
|
|
288 |
#endif
|
|
289 |
}
|
|
290 |
}
|
10576
|
291 |
#endif /* tEXt */
|
|
292 |
|
|
293 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
|
2
|
294 |
if (info_ptr->unknown_chunks_num)
|
|
295 |
{
|
10576
|
296 |
png_unknown_chunk *up;
|
2
|
297 |
|
10576
|
298 |
png_debug(5, "writing extra chunks");
|
2
|
299 |
|
10576
|
300 |
for (up = info_ptr->unknown_chunks;
|
|
301 |
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
|
|
302 |
up++)
|
|
303 |
{
|
|
304 |
int keep = png_handle_as_unknown(png_ptr, up->name);
|
2
|
305 |
if (keep != PNG_HANDLE_CHUNK_NEVER &&
|
10576
|
306 |
up->location &&
|
|
307 |
(up->location & PNG_HAVE_PLTE) &&
|
|
308 |
!(up->location & PNG_HAVE_IDAT) &&
|
|
309 |
!(up->location & PNG_AFTER_IDAT) &&
|
|
310 |
((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
|
|
311 |
(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
|
2
|
312 |
{
|
|
313 |
png_write_chunk(png_ptr, up->name, up->data, up->size);
|
|
314 |
}
|
10576
|
315 |
}
|
2
|
316 |
}
|
|
317 |
#endif
|
|
318 |
}
|
|
319 |
|
|
320 |
/* Writes the end of the PNG file. If you don't want to write comments or
|
|
321 |
* time information, you can pass NULL for info. If you already wrote these
|
|
322 |
* in png_write_info(), do not write them again here. If you have long
|
|
323 |
* comments, I suggest writing them here, and compressing them.
|
|
324 |
*/
|
|
325 |
void PNGAPI
|
|
326 |
png_write_end(png_structp png_ptr, png_infop info_ptr)
|
|
327 |
{
|
10576
|
328 |
png_debug(1, "in png_write_end");
|
|
329 |
|
2
|
330 |
if (png_ptr == NULL)
|
|
331 |
return;
|
10576
|
332 |
|
2
|
333 |
if (!(png_ptr->mode & PNG_HAVE_IDAT))
|
|
334 |
png_error(png_ptr, "No IDATs written into file");
|
|
335 |
|
10576
|
336 |
/* See if user wants us to write information chunks */
|
2
|
337 |
if (info_ptr != NULL)
|
|
338 |
{
|
10576
|
339 |
#ifdef PNG_WRITE_TEXT_SUPPORTED
|
2
|
340 |
int i; /* local index variable */
|
|
341 |
#endif
|
10576
|
342 |
#ifdef PNG_WRITE_tIME_SUPPORTED
|
|
343 |
/* Check to see if user has supplied a time chunk */
|
2
|
344 |
if ((info_ptr->valid & PNG_INFO_tIME) &&
|
10576
|
345 |
!(png_ptr->mode & PNG_WROTE_tIME))
|
2
|
346 |
png_write_tIME(png_ptr, &(info_ptr->mod_time));
|
10576
|
347 |
|
2
|
348 |
#endif
|
10576
|
349 |
#ifdef PNG_WRITE_TEXT_SUPPORTED
|
|
350 |
/* Loop through comment chunks */
|
2
|
351 |
for (i = 0; i < info_ptr->num_text; i++)
|
|
352 |
{
|
10576
|
353 |
png_debug2(2, "Writing trailer text chunk %d, type %d", i,
|
2
|
354 |
info_ptr->text[i].compression);
|
10576
|
355 |
/* An internationalized chunk? */
|
2
|
356 |
if (info_ptr->text[i].compression > 0)
|
|
357 |
{
|
10576
|
358 |
#ifdef PNG_WRITE_iTXt_SUPPORTED
|
|
359 |
/* Write international chunk */
|
|
360 |
png_write_iTXt(png_ptr,
|
|
361 |
info_ptr->text[i].compression,
|
|
362 |
info_ptr->text[i].key,
|
|
363 |
info_ptr->text[i].lang,
|
|
364 |
info_ptr->text[i].lang_key,
|
|
365 |
info_ptr->text[i].text);
|
2
|
366 |
#else
|
10576
|
367 |
png_warning(png_ptr, "Unable to write international text");
|
2
|
368 |
#endif
|
10576
|
369 |
/* Mark this chunk as written */
|
|
370 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
2
|
371 |
}
|
10576
|
372 |
|
2
|
373 |
else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
|
|
374 |
{
|
10576
|
375 |
#ifdef PNG_WRITE_zTXt_SUPPORTED
|
|
376 |
/* Write compressed chunk */
|
2
|
377 |
png_write_zTXt(png_ptr, info_ptr->text[i].key,
|
10576
|
378 |
info_ptr->text[i].text, 0,
|
|
379 |
info_ptr->text[i].compression);
|
2
|
380 |
#else
|
|
381 |
png_warning(png_ptr, "Unable to write compressed text");
|
|
382 |
#endif
|
|
383 |
/* Mark this chunk as written */
|
|
384 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
|
|
385 |
}
|
10576
|
386 |
|
2
|
387 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
|
|
388 |
{
|
10576
|
389 |
#ifdef PNG_WRITE_tEXt_SUPPORTED
|
|
390 |
/* Write uncompressed chunk */
|
2
|
391 |
png_write_tEXt(png_ptr, info_ptr->text[i].key,
|
10576
|
392 |
info_ptr->text[i].text, 0);
|
2
|
393 |
#else
|
|
394 |
png_warning(png_ptr, "Unable to write uncompressed text");
|
|
395 |
#endif
|
|
396 |
|
|
397 |
/* Mark this chunk as written */
|
|
398 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
|
399 |
}
|
|
400 |
}
|
|
401 |
#endif
|
10576
|
402 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
|
2
|
403 |
if (info_ptr->unknown_chunks_num)
|
|
404 |
{
|
10576
|
405 |
png_unknown_chunk *up;
|
2
|
406 |
|
10576
|
407 |
png_debug(5, "writing extra chunks");
|
2
|
408 |
|
10576
|
409 |
for (up = info_ptr->unknown_chunks;
|
|
410 |
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
|
|
411 |
up++)
|
|
412 |
{
|
|
413 |
int keep = png_handle_as_unknown(png_ptr, up->name);
|
2
|
414 |
if (keep != PNG_HANDLE_CHUNK_NEVER &&
|
10576
|
415 |
up->location &&
|
|
416 |
(up->location & PNG_AFTER_IDAT) &&
|
|
417 |
((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
|
|
418 |
(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
|
2
|
419 |
{
|
|
420 |
png_write_chunk(png_ptr, up->name, up->data, up->size);
|
|
421 |
}
|
10576
|
422 |
}
|
2
|
423 |
}
|
|
424 |
#endif
|
|
425 |
}
|
|
426 |
|
|
427 |
png_ptr->mode |= PNG_AFTER_IDAT;
|
|
428 |
|
10576
|
429 |
/* Write end of PNG file */
|
2
|
430 |
png_write_IEND(png_ptr);
|
10576
|
431 |
/* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
|
|
432 |
* and restored again in libpng-1.2.30, may cause some applications that
|
|
433 |
* do not set png_ptr->output_flush_fn to crash. If your application
|
|
434 |
* experiences a problem, please try building libpng with
|
|
435 |
* PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
|
|
436 |
* png-mng-implement at lists.sf.net .
|
|
437 |
*/
|
|
438 |
#ifdef PNG_WRITE_FLUSH_SUPPORTED
|
|
439 |
# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
|
2
|
440 |
png_flush(png_ptr);
|
10576
|
441 |
# endif
|
2
|
442 |
#endif
|
|
443 |
}
|
|
444 |
|
10576
|
445 |
#ifdef PNG_CONVERT_tIME_SUPPORTED
|
|
446 |
/* "tm" structure is not supported on WindowsCE */
|
2
|
447 |
void PNGAPI
|
10576
|
448 |
png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime)
|
2
|
449 |
{
|
10576
|
450 |
png_debug(1, "in png_convert_from_struct_tm");
|
|
451 |
|
2
|
452 |
ptime->year = (png_uint_16)(1900 + ttime->tm_year);
|
|
453 |
ptime->month = (png_byte)(ttime->tm_mon + 1);
|
|
454 |
ptime->day = (png_byte)ttime->tm_mday;
|
|
455 |
ptime->hour = (png_byte)ttime->tm_hour;
|
|
456 |
ptime->minute = (png_byte)ttime->tm_min;
|
|
457 |
ptime->second = (png_byte)ttime->tm_sec;
|
|
458 |
}
|
|
459 |
|
|
460 |
void PNGAPI
|
|
461 |
png_convert_from_time_t(png_timep ptime, time_t ttime)
|
|
462 |
{
|
|
463 |
struct tm *tbuf;
|
|
464 |
|
10576
|
465 |
png_debug(1, "in png_convert_from_time_t");
|
|
466 |
|
2
|
467 |
tbuf = gmtime(&ttime);
|
|
468 |
png_convert_from_struct_tm(ptime, tbuf);
|
|
469 |
}
|
|
470 |
#endif
|
|
471 |
|
|
472 |
/* Initialize png_ptr structure, and allocate any memory needed */
|
10576
|
473 |
PNG_FUNCTION(png_structp,PNGAPI
|
|
474 |
png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
|
|
475 |
png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
|
2
|
476 |
{
|
|
477 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
478 |
return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
|
10576
|
479 |
warn_fn, NULL, NULL, NULL));
|
2
|
480 |
}
|
|
481 |
|
|
482 |
/* Alternate initialize png_ptr structure, and allocate any memory needed */
|
10576
|
483 |
static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
|
|
484 |
|
|
485 |
PNG_FUNCTION(png_structp,PNGAPI
|
|
486 |
png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
|
|
487 |
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
|
|
488 |
png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
|
2
|
489 |
{
|
|
490 |
#endif /* PNG_USER_MEM_SUPPORTED */
|
10576
|
491 |
volatile int png_cleanup_needed = 0;
|
|
492 |
#ifdef PNG_SETJMP_SUPPORTED
|
|
493 |
volatile
|
|
494 |
#endif
|
2
|
495 |
png_structp png_ptr;
|
|
496 |
#ifdef PNG_SETJMP_SUPPORTED
|
|
497 |
#ifdef USE_FAR_KEYWORD
|
10576
|
498 |
jmp_buf tmp_jmpbuf;
|
2
|
499 |
#endif
|
|
500 |
#endif
|
10576
|
501 |
|
|
502 |
png_debug(1, "in png_create_write_struct");
|
|
503 |
|
2
|
504 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
505 |
png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
|
10576
|
506 |
(png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
|
2
|
507 |
#else
|
|
508 |
png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
|
|
509 |
#endif /* PNG_USER_MEM_SUPPORTED */
|
|
510 |
if (png_ptr == NULL)
|
|
511 |
return (NULL);
|
|
512 |
|
10576
|
513 |
/* Added at libpng-1.2.6 */
|
2
|
514 |
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
|
10576
|
515 |
png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
|
|
516 |
png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
|
2
|
517 |
#endif
|
|
518 |
|
|
519 |
#ifdef PNG_SETJMP_SUPPORTED
|
10576
|
520 |
/* Applications that neglect to set up their own setjmp() and then
|
|
521 |
encounter a png_error() will longjmp here. Since the jmpbuf is
|
|
522 |
then meaningless we abort instead of returning. */
|
2
|
523 |
#ifdef USE_FAR_KEYWORD
|
10576
|
524 |
if (setjmp(tmp_jmpbuf))
|
2
|
525 |
#else
|
10576
|
526 |
if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
|
2
|
527 |
#endif
|
|
528 |
#ifdef USE_FAR_KEYWORD
|
10576
|
529 |
png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
|
2
|
530 |
#endif
|
10576
|
531 |
PNG_ABORT();
|
2
|
532 |
#endif
|
|
533 |
|
|
534 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
535 |
png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
|
|
536 |
#endif /* PNG_USER_MEM_SUPPORTED */
|
|
537 |
png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
|
|
538 |
|
10576
|
539 |
if (!png_user_version_check(png_ptr, user_png_ver))
|
|
540 |
png_cleanup_needed = 1;
|
2
|
541 |
|
10576
|
542 |
/* Initialize zbuf - compression buffer */
|
|
543 |
png_ptr->zbuf_size = PNG_ZBUF_SIZE;
|
|
544 |
|
|
545 |
if (!png_cleanup_needed)
|
2
|
546 |
{
|
10576
|
547 |
png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
|
|
548 |
png_ptr->zbuf_size);
|
|
549 |
if (png_ptr->zbuf == NULL)
|
|
550 |
png_cleanup_needed = 1;
|
2
|
551 |
}
|
|
552 |
|
10576
|
553 |
if (png_cleanup_needed)
|
|
554 |
{
|
|
555 |
/* Clean up PNG structure and deallocate any memory. */
|
|
556 |
png_free(png_ptr, png_ptr->zbuf);
|
|
557 |
png_ptr->zbuf = NULL;
|
|
558 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
559 |
png_destroy_struct_2((png_voidp)png_ptr,
|
|
560 |
(png_free_ptr)free_fn, (png_voidp)mem_ptr);
|
|
561 |
#else
|
|
562 |
png_destroy_struct((png_voidp)png_ptr);
|
|
563 |
#endif
|
|
564 |
return (NULL);
|
|
565 |
}
|
2
|
566 |
|
10576
|
567 |
png_set_write_fn(png_ptr, NULL, NULL, NULL);
|
2
|
568 |
|
10576
|
569 |
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
|
|
570 |
png_reset_filter_heuristics(png_ptr);
|
2
|
571 |
#endif
|
|
572 |
|
|
573 |
return (png_ptr);
|
|
574 |
}
|
|
575 |
|
|
576 |
|
|
577 |
/* Write a few rows of image data. If the image is interlaced,
|
|
578 |
* either you will have to write the 7 sub images, or, if you
|
|
579 |
* have called png_set_interlace_handling(), you will have to
|
|
580 |
* "write" the image seven times.
|
|
581 |
*/
|
|
582 |
void PNGAPI
|
|
583 |
png_write_rows(png_structp png_ptr, png_bytepp row,
|
10576
|
584 |
png_uint_32 num_rows)
|
2
|
585 |
{
|
|
586 |
png_uint_32 i; /* row counter */
|
|
587 |
png_bytepp rp; /* row pointer */
|
|
588 |
|
10576
|
589 |
png_debug(1, "in png_write_rows");
|
2
|
590 |
|
|
591 |
if (png_ptr == NULL)
|
|
592 |
return;
|
|
593 |
|
10576
|
594 |
/* Loop through the rows */
|
2
|
595 |
for (i = 0, rp = row; i < num_rows; i++, rp++)
|
|
596 |
{
|
|
597 |
png_write_row(png_ptr, *rp);
|
|
598 |
}
|
|
599 |
}
|
|
600 |
|
|
601 |
/* Write the image. You only need to call this function once, even
|
|
602 |
* if you are writing an interlaced image.
|
|
603 |
*/
|
|
604 |
void PNGAPI
|
|
605 |
png_write_image(png_structp png_ptr, png_bytepp image)
|
|
606 |
{
|
|
607 |
png_uint_32 i; /* row index */
|
|
608 |
int pass, num_pass; /* pass variables */
|
|
609 |
png_bytepp rp; /* points to current row */
|
|
610 |
|
|
611 |
if (png_ptr == NULL)
|
|
612 |
return;
|
|
613 |
|
10576
|
614 |
png_debug(1, "in png_write_image");
|
|
615 |
|
|
616 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
|
617 |
/* Initialize interlace handling. If image is not interlaced,
|
|
618 |
* this will set pass to 1
|
|
619 |
*/
|
2
|
620 |
num_pass = png_set_interlace_handling(png_ptr);
|
|
621 |
#else
|
|
622 |
num_pass = 1;
|
|
623 |
#endif
|
10576
|
624 |
/* Loop through passes */
|
2
|
625 |
for (pass = 0; pass < num_pass; pass++)
|
|
626 |
{
|
10576
|
627 |
/* Loop through image */
|
2
|
628 |
for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
|
|
629 |
{
|
|
630 |
png_write_row(png_ptr, *rp);
|
|
631 |
}
|
|
632 |
}
|
|
633 |
}
|
|
634 |
|
10576
|
635 |
/* Called by user to write a row of image data */
|
2
|
636 |
void PNGAPI
|
10576
|
637 |
png_write_row(png_structp png_ptr, png_const_bytep row)
|
2
|
638 |
{
|
|
639 |
if (png_ptr == NULL)
|
|
640 |
return;
|
10576
|
641 |
|
|
642 |
png_debug2(1, "in png_write_row (row %u, pass %d)",
|
2
|
643 |
png_ptr->row_number, png_ptr->pass);
|
|
644 |
|
10576
|
645 |
/* Initialize transformations and other stuff if first time */
|
2
|
646 |
if (png_ptr->row_number == 0 && png_ptr->pass == 0)
|
|
647 |
{
|
10576
|
648 |
/* Make sure we wrote the header info */
|
|
649 |
if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
|
|
650 |
png_error(png_ptr,
|
|
651 |
"png_write_info was never called before png_write_row");
|
2
|
652 |
|
10576
|
653 |
/* Check for transforms that have been set but were defined out */
|
2
|
654 |
#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
|
10576
|
655 |
if (png_ptr->transformations & PNG_INVERT_MONO)
|
|
656 |
png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
|
2
|
657 |
#endif
|
10576
|
658 |
|
2
|
659 |
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
|
10576
|
660 |
if (png_ptr->transformations & PNG_FILLER)
|
|
661 |
png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
|
2
|
662 |
#endif
|
10576
|
663 |
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
|
|
664 |
defined(PNG_READ_PACKSWAP_SUPPORTED)
|
|
665 |
if (png_ptr->transformations & PNG_PACKSWAP)
|
|
666 |
png_warning(png_ptr,
|
|
667 |
"PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
|
2
|
668 |
#endif
|
10576
|
669 |
|
2
|
670 |
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
|
10576
|
671 |
if (png_ptr->transformations & PNG_PACK)
|
|
672 |
png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
|
2
|
673 |
#endif
|
10576
|
674 |
|
2
|
675 |
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
|
10576
|
676 |
if (png_ptr->transformations & PNG_SHIFT)
|
|
677 |
png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
|
2
|
678 |
#endif
|
10576
|
679 |
|
2
|
680 |
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
|
10576
|
681 |
if (png_ptr->transformations & PNG_BGR)
|
|
682 |
png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
|
2
|
683 |
#endif
|
10576
|
684 |
|
2
|
685 |
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
|
10576
|
686 |
if (png_ptr->transformations & PNG_SWAP_BYTES)
|
|
687 |
png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
|
2
|
688 |
#endif
|
|
689 |
|
|
690 |
png_write_start_row(png_ptr);
|
|
691 |
}
|
|
692 |
|
10576
|
693 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
|
694 |
/* If interlaced and not interested in row, return */
|
2
|
695 |
if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
|
|
696 |
{
|
|
697 |
switch (png_ptr->pass)
|
|
698 |
{
|
|
699 |
case 0:
|
|
700 |
if (png_ptr->row_number & 0x07)
|
|
701 |
{
|
|
702 |
png_write_finish_row(png_ptr);
|
|
703 |
return;
|
|
704 |
}
|
|
705 |
break;
|
10576
|
706 |
|
2
|
707 |
case 1:
|
|
708 |
if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
|
|
709 |
{
|
|
710 |
png_write_finish_row(png_ptr);
|
|
711 |
return;
|
|
712 |
}
|
|
713 |
break;
|
10576
|
714 |
|
2
|
715 |
case 2:
|
|
716 |
if ((png_ptr->row_number & 0x07) != 4)
|
|
717 |
{
|
|
718 |
png_write_finish_row(png_ptr);
|
|
719 |
return;
|
|
720 |
}
|
|
721 |
break;
|
10576
|
722 |
|
2
|
723 |
case 3:
|
|
724 |
if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
|
|
725 |
{
|
|
726 |
png_write_finish_row(png_ptr);
|
|
727 |
return;
|
|
728 |
}
|
|
729 |
break;
|
10576
|
730 |
|
2
|
731 |
case 4:
|
|
732 |
if ((png_ptr->row_number & 0x03) != 2)
|
|
733 |
{
|
|
734 |
png_write_finish_row(png_ptr);
|
|
735 |
return;
|
|
736 |
}
|
|
737 |
break;
|
10576
|
738 |
|
2
|
739 |
case 5:
|
|
740 |
if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
|
|
741 |
{
|
|
742 |
png_write_finish_row(png_ptr);
|
|
743 |
return;
|
|
744 |
}
|
|
745 |
break;
|
10576
|
746 |
|
2
|
747 |
case 6:
|
|
748 |
if (!(png_ptr->row_number & 0x01))
|
|
749 |
{
|
|
750 |
png_write_finish_row(png_ptr);
|
|
751 |
return;
|
|
752 |
}
|
|
753 |
break;
|
10576
|
754 |
|
|
755 |
default: /* error: ignore it */
|
|
756 |
break;
|
2
|
757 |
}
|
|
758 |
}
|
|
759 |
#endif
|
|
760 |
|
10576
|
761 |
/* Set up row info for transformations */
|
2
|
762 |
png_ptr->row_info.color_type = png_ptr->color_type;
|
|
763 |
png_ptr->row_info.width = png_ptr->usr_width;
|
|
764 |
png_ptr->row_info.channels = png_ptr->usr_channels;
|
|
765 |
png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
|
|
766 |
png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
|
|
767 |
png_ptr->row_info.channels);
|
|
768 |
|
|
769 |
png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
|
|
770 |
png_ptr->row_info.width);
|
|
771 |
|
10576
|
772 |
png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
|
|
773 |
png_debug1(3, "row_info->width = %u", png_ptr->row_info.width);
|
|
774 |
png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
|
|
775 |
png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
|
|
776 |
png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
|
|
777 |
png_debug1(3, "row_info->rowbytes = %lu",
|
|
778 |
(unsigned long)png_ptr->row_info.rowbytes);
|
2
|
779 |
|
|
780 |
/* Copy user's row into buffer, leaving room for filter byte. */
|
10576
|
781 |
png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
|
2
|
782 |
|
10576
|
783 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
|
784 |
/* Handle interlacing */
|
2
|
785 |
if (png_ptr->interlaced && png_ptr->pass < 6 &&
|
10576
|
786 |
(png_ptr->transformations & PNG_INTERLACE))
|
2
|
787 |
{
|
|
788 |
png_do_write_interlace(&(png_ptr->row_info),
|
10576
|
789 |
png_ptr->row_buf + 1, png_ptr->pass);
|
|
790 |
/* This should always get caught above, but still ... */
|
2
|
791 |
if (!(png_ptr->row_info.width))
|
|
792 |
{
|
|
793 |
png_write_finish_row(png_ptr);
|
|
794 |
return;
|
|
795 |
}
|
|
796 |
}
|
|
797 |
#endif
|
|
798 |
|
10576
|
799 |
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
|
|
800 |
/* Handle other transformations */
|
2
|
801 |
if (png_ptr->transformations)
|
|
802 |
png_do_write_transformations(png_ptr);
|
10576
|
803 |
#endif
|
2
|
804 |
|
10576
|
805 |
#ifdef PNG_MNG_FEATURES_SUPPORTED
|
2
|
806 |
/* Write filter_method 64 (intrapixel differencing) only if
|
|
807 |
* 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
|
|
808 |
* 2. Libpng did not write a PNG signature (this filter_method is only
|
|
809 |
* used in PNG datastreams that are embedded in MNG datastreams) and
|
|
810 |
* 3. The application called png_permit_mng_features with a mask that
|
|
811 |
* included PNG_FLAG_MNG_FILTER_64 and
|
|
812 |
* 4. The filter_method is 64 and
|
|
813 |
* 5. The color_type is RGB or RGBA
|
|
814 |
*/
|
10576
|
815 |
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
|
|
816 |
(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
|
2
|
817 |
{
|
|
818 |
/* Intrapixel differencing */
|
|
819 |
png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
|
|
820 |
}
|
|
821 |
#endif
|
|
822 |
|
|
823 |
/* Find a filter if necessary, filter the row and write it out. */
|
|
824 |
png_write_find_filter(png_ptr, &(png_ptr->row_info));
|
|
825 |
|
|
826 |
if (png_ptr->write_row_fn != NULL)
|
|
827 |
(*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
|
|
828 |
}
|
|
829 |
|
10576
|
830 |
#ifdef PNG_WRITE_FLUSH_SUPPORTED
|
2
|
831 |
/* Set the automatic flush interval or 0 to turn flushing off */
|
|
832 |
void PNGAPI
|
|
833 |
png_set_flush(png_structp png_ptr, int nrows)
|
|
834 |
{
|
10576
|
835 |
png_debug(1, "in png_set_flush");
|
|
836 |
|
2
|
837 |
if (png_ptr == NULL)
|
|
838 |
return;
|
10576
|
839 |
|
2
|
840 |
png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
|
|
841 |
}
|
|
842 |
|
10576
|
843 |
/* Flush the current output buffers now */
|
2
|
844 |
void PNGAPI
|
|
845 |
png_write_flush(png_structp png_ptr)
|
|
846 |
{
|
|
847 |
int wrote_IDAT;
|
|
848 |
|
10576
|
849 |
png_debug(1, "in png_write_flush");
|
|
850 |
|
2
|
851 |
if (png_ptr == NULL)
|
|
852 |
return;
|
10576
|
853 |
|
2
|
854 |
/* We have already written out all of the data */
|
|
855 |
if (png_ptr->row_number >= png_ptr->num_rows)
|
10576
|
856 |
return;
|
2
|
857 |
|
|
858 |
do
|
|
859 |
{
|
|
860 |
int ret;
|
|
861 |
|
10576
|
862 |
/* Compress the data */
|
2
|
863 |
ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
|
|
864 |
wrote_IDAT = 0;
|
|
865 |
|
10576
|
866 |
/* Check for compression errors */
|
2
|
867 |
if (ret != Z_OK)
|
|
868 |
{
|
|
869 |
if (png_ptr->zstream.msg != NULL)
|
|
870 |
png_error(png_ptr, png_ptr->zstream.msg);
|
10576
|
871 |
|
2
|
872 |
else
|
|
873 |
png_error(png_ptr, "zlib error");
|
|
874 |
}
|
|
875 |
|
|
876 |
if (!(png_ptr->zstream.avail_out))
|
|
877 |
{
|
10576
|
878 |
/* Write the IDAT and reset the zlib output buffer */
|
|
879 |
png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
|
2
|
880 |
wrote_IDAT = 1;
|
|
881 |
}
|
10576
|
882 |
} while (wrote_IDAT == 1);
|
2
|
883 |
|
|
884 |
/* If there is any data left to be output, write it into a new IDAT */
|
|
885 |
if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
|
|
886 |
{
|
10576
|
887 |
/* Write the IDAT and reset the zlib output buffer */
|
2
|
888 |
png_write_IDAT(png_ptr, png_ptr->zbuf,
|
10576
|
889 |
png_ptr->zbuf_size - png_ptr->zstream.avail_out);
|
2
|
890 |
}
|
|
891 |
png_ptr->flush_rows = 0;
|
|
892 |
png_flush(png_ptr);
|
|
893 |
}
|
|
894 |
#endif /* PNG_WRITE_FLUSH_SUPPORTED */
|
|
895 |
|
10576
|
896 |
/* Free all memory used by the write */
|
2
|
897 |
void PNGAPI
|
|
898 |
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
|
|
899 |
{
|
|
900 |
png_structp png_ptr = NULL;
|
|
901 |
png_infop info_ptr = NULL;
|
|
902 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
903 |
png_free_ptr free_fn = NULL;
|
|
904 |
png_voidp mem_ptr = NULL;
|
|
905 |
#endif
|
|
906 |
|
10576
|
907 |
png_debug(1, "in png_destroy_write_struct");
|
|
908 |
|
2
|
909 |
if (png_ptr_ptr != NULL)
|
|
910 |
{
|
|
911 |
png_ptr = *png_ptr_ptr;
|
|
912 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
913 |
free_fn = png_ptr->free_fn;
|
|
914 |
mem_ptr = png_ptr->mem_ptr;
|
|
915 |
#endif
|
|
916 |
}
|
|
917 |
|
10576
|
918 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
919 |
if (png_ptr != NULL)
|
|
920 |
{
|
|
921 |
free_fn = png_ptr->free_fn;
|
|
922 |
mem_ptr = png_ptr->mem_ptr;
|
|
923 |
}
|
|
924 |
#endif
|
|
925 |
|
2
|
926 |
if (info_ptr_ptr != NULL)
|
|
927 |
info_ptr = *info_ptr_ptr;
|
|
928 |
|
|
929 |
if (info_ptr != NULL)
|
|
930 |
{
|
10576
|
931 |
if (png_ptr != NULL)
|
2
|
932 |
{
|
10576
|
933 |
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
|
934 |
|
|
935 |
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
|
|
936 |
if (png_ptr->num_chunk_list)
|
|
937 |
{
|
|
938 |
png_free(png_ptr, png_ptr->chunk_list);
|
|
939 |
png_ptr->num_chunk_list = 0;
|
|
940 |
}
|
|
941 |
#endif
|
2
|
942 |
}
|
|
943 |
|
|
944 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
945 |
png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
|
10576
|
946 |
(png_voidp)mem_ptr);
|
2
|
947 |
#else
|
|
948 |
png_destroy_struct((png_voidp)info_ptr);
|
|
949 |
#endif
|
|
950 |
*info_ptr_ptr = NULL;
|
|
951 |
}
|
|
952 |
|
|
953 |
if (png_ptr != NULL)
|
|
954 |
{
|
|
955 |
png_write_destroy(png_ptr);
|
|
956 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
957 |
png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
|
10576
|
958 |
(png_voidp)mem_ptr);
|
2
|
959 |
#else
|
|
960 |
png_destroy_struct((png_voidp)png_ptr);
|
|
961 |
#endif
|
|
962 |
*png_ptr_ptr = NULL;
|
|
963 |
}
|
|
964 |
}
|
|
965 |
|
|
966 |
|
|
967 |
/* Free any memory used in png_ptr struct (old method) */
|
|
968 |
void /* PRIVATE */
|
|
969 |
png_write_destroy(png_structp png_ptr)
|
|
970 |
{
|
|
971 |
#ifdef PNG_SETJMP_SUPPORTED
|
10576
|
972 |
jmp_buf tmp_jmp; /* Save jump buffer */
|
2
|
973 |
#endif
|
|
974 |
png_error_ptr error_fn;
|
10576
|
975 |
#ifdef PNG_WARNINGS_SUPPORTED
|
2
|
976 |
png_error_ptr warning_fn;
|
10576
|
977 |
#endif
|
2
|
978 |
png_voidp error_ptr;
|
|
979 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
980 |
png_free_ptr free_fn;
|
|
981 |
#endif
|
|
982 |
|
10576
|
983 |
png_debug(1, "in png_write_destroy");
|
2
|
984 |
|
10576
|
985 |
/* Free any memory zlib uses */
|
|
986 |
if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
|
|
987 |
deflateEnd(&png_ptr->zstream);
|
|
988 |
|
|
989 |
/* Free our memory. png_free checks NULL for us. */
|
2
|
990 |
png_free(png_ptr, png_ptr->zbuf);
|
|
991 |
png_free(png_ptr, png_ptr->row_buf);
|
10576
|
992 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
2
|
993 |
png_free(png_ptr, png_ptr->prev_row);
|
|
994 |
png_free(png_ptr, png_ptr->sub_row);
|
|
995 |
png_free(png_ptr, png_ptr->up_row);
|
|
996 |
png_free(png_ptr, png_ptr->avg_row);
|
|
997 |
png_free(png_ptr, png_ptr->paeth_row);
|
|
998 |
#endif
|
|
999 |
|
10576
|
1000 |
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
|
|
1001 |
/* Use this to save a little code space, it doesn't free the filter_costs */
|
|
1002 |
png_reset_filter_heuristics(png_ptr);
|
2
|
1003 |
png_free(png_ptr, png_ptr->filter_costs);
|
|
1004 |
png_free(png_ptr, png_ptr->inv_filter_costs);
|
|
1005 |
#endif
|
|
1006 |
|
|
1007 |
#ifdef PNG_SETJMP_SUPPORTED
|
10576
|
1008 |
/* Reset structure */
|
|
1009 |
png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
|
2
|
1010 |
#endif
|
|
1011 |
|
|
1012 |
error_fn = png_ptr->error_fn;
|
10576
|
1013 |
#ifdef PNG_WARNINGS_SUPPORTED
|
2
|
1014 |
warning_fn = png_ptr->warning_fn;
|
10576
|
1015 |
#endif
|
2
|
1016 |
error_ptr = png_ptr->error_ptr;
|
|
1017 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
1018 |
free_fn = png_ptr->free_fn;
|
|
1019 |
#endif
|
|
1020 |
|
10576
|
1021 |
png_memset(png_ptr, 0, png_sizeof(png_struct));
|
2
|
1022 |
|
|
1023 |
png_ptr->error_fn = error_fn;
|
10576
|
1024 |
#ifdef PNG_WARNINGS_SUPPORTED
|
2
|
1025 |
png_ptr->warning_fn = warning_fn;
|
10576
|
1026 |
#endif
|
2
|
1027 |
png_ptr->error_ptr = error_ptr;
|
|
1028 |
#ifdef PNG_USER_MEM_SUPPORTED
|
|
1029 |
png_ptr->free_fn = free_fn;
|
|
1030 |
#endif
|
|
1031 |
|
|
1032 |
#ifdef PNG_SETJMP_SUPPORTED
|
10576
|
1033 |
png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
|
2
|
1034 |
#endif
|
|
1035 |
}
|
|
1036 |
|
|
1037 |
/* Allow the application to select one or more row filters to use. */
|
|
1038 |
void PNGAPI
|
|
1039 |
png_set_filter(png_structp png_ptr, int method, int filters)
|
|
1040 |
{
|
10576
|
1041 |
png_debug(1, "in png_set_filter");
|
|
1042 |
|
2
|
1043 |
if (png_ptr == NULL)
|
|
1044 |
return;
|
10576
|
1045 |
|
|
1046 |
#ifdef PNG_MNG_FEATURES_SUPPORTED
|
|
1047 |
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
|
|
1048 |
(method == PNG_INTRAPIXEL_DIFFERENCING))
|
|
1049 |
method = PNG_FILTER_TYPE_BASE;
|
|
1050 |
|
2
|
1051 |
#endif
|
|
1052 |
if (method == PNG_FILTER_TYPE_BASE)
|
|
1053 |
{
|
|
1054 |
switch (filters & (PNG_ALL_FILTERS | 0x07))
|
|
1055 |
{
|
10576
|
1056 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
2
|
1057 |
case 5:
|
|
1058 |
case 6:
|
|
1059 |
case 7: png_warning(png_ptr, "Unknown row filter for method 0");
|
10576
|
1060 |
#endif /* PNG_WRITE_FILTER_SUPPORTED */
|
|
1061 |
case PNG_FILTER_VALUE_NONE:
|
|
1062 |
png_ptr->do_filter = PNG_FILTER_NONE; break;
|
|
1063 |
|
|
1064 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
|
1065 |
case PNG_FILTER_VALUE_SUB:
|
|
1066 |
png_ptr->do_filter = PNG_FILTER_SUB; break;
|
|
1067 |
|
|
1068 |
case PNG_FILTER_VALUE_UP:
|
|
1069 |
png_ptr->do_filter = PNG_FILTER_UP; break;
|
|
1070 |
|
|
1071 |
case PNG_FILTER_VALUE_AVG:
|
|
1072 |
png_ptr->do_filter = PNG_FILTER_AVG; break;
|
|
1073 |
|
|
1074 |
case PNG_FILTER_VALUE_PAETH:
|
|
1075 |
png_ptr->do_filter = PNG_FILTER_PAETH; break;
|
|
1076 |
|
|
1077 |
default:
|
|
1078 |
png_ptr->do_filter = (png_byte)filters; break;
|
|
1079 |
#else
|
|
1080 |
default:
|
|
1081 |
png_warning(png_ptr, "Unknown row filter for method 0");
|
|
1082 |
#endif /* PNG_WRITE_FILTER_SUPPORTED */
|
2
|
1083 |
}
|
|
1084 |
|
|
1085 |
/* If we have allocated the row_buf, this means we have already started
|
|
1086 |
* with the image and we should have allocated all of the filter buffers
|
|
1087 |
* that have been selected. If prev_row isn't already allocated, then
|
|
1088 |
* it is too late to start using the filters that need it, since we
|
|
1089 |
* will be missing the data in the previous row. If an application
|
|
1090 |
* wants to start and stop using particular filters during compression,
|
|
1091 |
* it should start out with all of the filters, and then add and
|
|
1092 |
* remove them after the start of compression.
|
|
1093 |
*/
|
|
1094 |
if (png_ptr->row_buf != NULL)
|
|
1095 |
{
|
10576
|
1096 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
2
|
1097 |
if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
|
|
1098 |
{
|
|
1099 |
png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1100 |
(png_ptr->rowbytes + 1));
|
2
|
1101 |
png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
|
|
1102 |
}
|
|
1103 |
|
|
1104 |
if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
|
|
1105 |
{
|
|
1106 |
if (png_ptr->prev_row == NULL)
|
|
1107 |
{
|
|
1108 |
png_warning(png_ptr, "Can't add Up filter after starting");
|
10576
|
1109 |
png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
|
|
1110 |
~PNG_FILTER_UP);
|
2
|
1111 |
}
|
10576
|
1112 |
|
2
|
1113 |
else
|
|
1114 |
{
|
|
1115 |
png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1116 |
(png_ptr->rowbytes + 1));
|
2
|
1117 |
png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
|
|
1118 |
}
|
|
1119 |
}
|
|
1120 |
|
|
1121 |
if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
|
|
1122 |
{
|
|
1123 |
if (png_ptr->prev_row == NULL)
|
|
1124 |
{
|
|
1125 |
png_warning(png_ptr, "Can't add Average filter after starting");
|
10576
|
1126 |
png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
|
|
1127 |
~PNG_FILTER_AVG);
|
2
|
1128 |
}
|
10576
|
1129 |
|
2
|
1130 |
else
|
|
1131 |
{
|
|
1132 |
png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1133 |
(png_ptr->rowbytes + 1));
|
2
|
1134 |
png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
|
|
1135 |
}
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
|
|
1139 |
png_ptr->paeth_row == NULL)
|
|
1140 |
{
|
|
1141 |
if (png_ptr->prev_row == NULL)
|
|
1142 |
{
|
|
1143 |
png_warning(png_ptr, "Can't add Paeth filter after starting");
|
|
1144 |
png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
|
|
1145 |
}
|
10576
|
1146 |
|
2
|
1147 |
else
|
|
1148 |
{
|
|
1149 |
png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1150 |
(png_ptr->rowbytes + 1));
|
2
|
1151 |
png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
|
|
1152 |
}
|
|
1153 |
}
|
|
1154 |
|
|
1155 |
if (png_ptr->do_filter == PNG_NO_FILTERS)
|
10576
|
1156 |
#endif /* PNG_WRITE_FILTER_SUPPORTED */
|
2
|
1157 |
png_ptr->do_filter = PNG_FILTER_NONE;
|
|
1158 |
}
|
|
1159 |
}
|
|
1160 |
else
|
|
1161 |
png_error(png_ptr, "Unknown custom filter method");
|
|
1162 |
}
|
|
1163 |
|
|
1164 |
/* This allows us to influence the way in which libpng chooses the "best"
|
|
1165 |
* filter for the current scanline. While the "minimum-sum-of-absolute-
|
|
1166 |
* differences metric is relatively fast and effective, there is some
|
|
1167 |
* question as to whether it can be improved upon by trying to keep the
|
|
1168 |
* filtered data going to zlib more consistent, hopefully resulting in
|
|
1169 |
* better compression.
|
|
1170 |
*/
|
10576
|
1171 |
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
|
|
1172 |
/* Convenience reset API. */
|
|
1173 |
static void
|
|
1174 |
png_reset_filter_heuristics(png_structp png_ptr)
|
2
|
1175 |
{
|
10576
|
1176 |
/* Clear out any old values in the 'weights' - this must be done because if
|
|
1177 |
* the app calls set_filter_heuristics multiple times with different
|
|
1178 |
* 'num_weights' values we would otherwise potentially have wrong sized
|
|
1179 |
* arrays.
|
|
1180 |
*/
|
|
1181 |
png_ptr->num_prev_filters = 0;
|
|
1182 |
png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
|
|
1183 |
if (png_ptr->prev_filters != NULL)
|
2
|
1184 |
{
|
10576
|
1185 |
png_bytep old = png_ptr->prev_filters;
|
|
1186 |
png_ptr->prev_filters = NULL;
|
|
1187 |
png_free(png_ptr, old);
|
|
1188 |
}
|
|
1189 |
if (png_ptr->filter_weights != NULL)
|
|
1190 |
{
|
|
1191 |
png_uint_16p old = png_ptr->filter_weights;
|
|
1192 |
png_ptr->filter_weights = NULL;
|
|
1193 |
png_free(png_ptr, old);
|
2
|
1194 |
}
|
|
1195 |
|
10576
|
1196 |
if (png_ptr->inv_filter_weights != NULL)
|
2
|
1197 |
{
|
10576
|
1198 |
png_uint_16p old = png_ptr->inv_filter_weights;
|
|
1199 |
png_ptr->inv_filter_weights = NULL;
|
|
1200 |
png_free(png_ptr, old);
|
2
|
1201 |
}
|
|
1202 |
|
10576
|
1203 |
/* Leave the filter_costs - this array is fixed size. */
|
|
1204 |
}
|
|
1205 |
|
|
1206 |
static int
|
|
1207 |
png_init_filter_heuristics(png_structp png_ptr, int heuristic_method,
|
|
1208 |
int num_weights)
|
|
1209 |
{
|
|
1210 |
if (png_ptr == NULL)
|
|
1211 |
return 0;
|
2
|
1212 |
|
10576
|
1213 |
/* Clear out the arrays */
|
|
1214 |
png_reset_filter_heuristics(png_ptr);
|
2
|
1215 |
|
10576
|
1216 |
/* Check arguments; the 'reset' function makes the correct settings for the
|
|
1217 |
* unweighted case, but we must handle the weight case by initializing the
|
|
1218 |
* arrays for the caller.
|
|
1219 |
*/
|
|
1220 |
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
2
|
1221 |
{
|
10576
|
1222 |
int i;
|
|
1223 |
|
|
1224 |
if (num_weights > 0)
|
2
|
1225 |
{
|
|
1226 |
png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
|
10576
|
1227 |
(png_uint_32)(png_sizeof(png_byte) * num_weights));
|
2
|
1228 |
|
|
1229 |
/* To make sure that the weighting starts out fairly */
|
|
1230 |
for (i = 0; i < num_weights; i++)
|
|
1231 |
{
|
|
1232 |
png_ptr->prev_filters[i] = 255;
|
|
1233 |
}
|
|
1234 |
|
|
1235 |
png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
10576
|
1236 |
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
|
2
|
1237 |
|
|
1238 |
png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
10576
|
1239 |
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
|
|
1240 |
|
2
|
1241 |
for (i = 0; i < num_weights; i++)
|
|
1242 |
{
|
|
1243 |
png_ptr->inv_filter_weights[i] =
|
|
1244 |
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
|
1245 |
}
|
10576
|
1246 |
|
|
1247 |
/* Safe to set this now */
|
|
1248 |
png_ptr->num_prev_filters = (png_byte)num_weights;
|
2
|
1249 |
}
|
|
1250 |
|
10576
|
1251 |
/* If, in the future, there are other filter methods, this would
|
|
1252 |
* need to be based on png_ptr->filter.
|
|
1253 |
*/
|
|
1254 |
if (png_ptr->filter_costs == NULL)
|
2
|
1255 |
{
|
10576
|
1256 |
png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
|
1257 |
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
|
|
1258 |
|
|
1259 |
png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
|
1260 |
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
|
2
|
1261 |
}
|
|
1262 |
|
|
1263 |
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
|
|
1264 |
{
|
|
1265 |
png_ptr->inv_filter_costs[i] =
|
|
1266 |
png_ptr->filter_costs[i] = PNG_COST_FACTOR;
|
|
1267 |
}
|
10576
|
1268 |
|
|
1269 |
/* All the arrays are inited, safe to set this: */
|
|
1270 |
png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
|
|
1271 |
|
|
1272 |
/* Return the 'ok' code. */
|
|
1273 |
return 1;
|
2
|
1274 |
}
|
10576
|
1275 |
else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
|
|
1276 |
heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
|
|
1277 |
{
|
|
1278 |
return 1;
|
|
1279 |
}
|
|
1280 |
else
|
|
1281 |
{
|
|
1282 |
png_warning(png_ptr, "Unknown filter heuristic method");
|
|
1283 |
return 0;
|
|
1284 |
}
|
|
1285 |
}
|
|
1286 |
|
|
1287 |
/* Provide floating and fixed point APIs */
|
|
1288 |
#ifdef PNG_FLOATING_POINT_SUPPORTED
|
|
1289 |
void PNGAPI
|
|
1290 |
png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
|
|
1291 |
int num_weights, png_const_doublep filter_weights,
|
|
1292 |
png_const_doublep filter_costs)
|
|
1293 |
{
|
|
1294 |
png_debug(1, "in png_set_filter_heuristics");
|
2
|
1295 |
|
10576
|
1296 |
/* The internal API allocates all the arrays and ensures that the elements of
|
|
1297 |
* those arrays are set to the default value.
|
2
|
1298 |
*/
|
10576
|
1299 |
if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
|
|
1300 |
return;
|
|
1301 |
|
|
1302 |
/* If using the weighted method copy in the weights. */
|
|
1303 |
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
2
|
1304 |
{
|
10576
|
1305 |
int i;
|
|
1306 |
for (i = 0; i < num_weights; i++)
|
|
1307 |
{
|
|
1308 |
if (filter_weights[i] <= 0.0)
|
|
1309 |
{
|
|
1310 |
png_ptr->inv_filter_weights[i] =
|
|
1311 |
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
|
1312 |
}
|
|
1313 |
|
|
1314 |
else
|
|
1315 |
{
|
|
1316 |
png_ptr->inv_filter_weights[i] =
|
|
1317 |
(png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
|
|
1318 |
|
|
1319 |
png_ptr->filter_weights[i] =
|
|
1320 |
(png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
|
|
1321 |
}
|
|
1322 |
}
|
|
1323 |
|
|
1324 |
/* Here is where we set the relative costs of the different filters. We
|
|
1325 |
* should take the desired compression level into account when setting
|
|
1326 |
* the costs, so that Paeth, for instance, has a high relative cost at low
|
|
1327 |
* compression levels, while it has a lower relative cost at higher
|
|
1328 |
* compression settings. The filter types are in order of increasing
|
|
1329 |
* relative cost, so it would be possible to do this with an algorithm.
|
|
1330 |
*/
|
|
1331 |
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
|
2
|
1332 |
{
|
|
1333 |
png_ptr->inv_filter_costs[i] =
|
10576
|
1334 |
(png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
|
|
1335 |
|
2
|
1336 |
png_ptr->filter_costs[i] =
|
10576
|
1337 |
(png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
|
2
|
1338 |
}
|
|
1339 |
}
|
|
1340 |
}
|
10576
|
1341 |
#endif /* FLOATING_POINT */
|
|
1342 |
|
|
1343 |
#ifdef PNG_FIXED_POINT_SUPPORTED
|
|
1344 |
void PNGAPI
|
|
1345 |
png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method,
|
|
1346 |
int num_weights, png_const_fixed_point_p filter_weights,
|
|
1347 |
png_const_fixed_point_p filter_costs)
|
|
1348 |
{
|
|
1349 |
png_debug(1, "in png_set_filter_heuristics_fixed");
|
|
1350 |
|
|
1351 |
/* The internal API allocates all the arrays and ensures that the elements of
|
|
1352 |
* those arrays are set to the default value.
|
|
1353 |
*/
|
|
1354 |
if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
|
|
1355 |
return;
|
|
1356 |
|
|
1357 |
/* If using the weighted method copy in the weights. */
|
|
1358 |
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
|
1359 |
{
|
|
1360 |
int i;
|
|
1361 |
for (i = 0; i < num_weights; i++)
|
|
1362 |
{
|
|
1363 |
if (filter_weights[i] <= 0)
|
|
1364 |
{
|
|
1365 |
png_ptr->inv_filter_weights[i] =
|
|
1366 |
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
|
1367 |
}
|
|
1368 |
|
|
1369 |
else
|
|
1370 |
{
|
|
1371 |
png_ptr->inv_filter_weights[i] = (png_uint_16)
|
|
1372 |
((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
|
|
1373 |
|
|
1374 |
png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
|
|
1375 |
PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
|
|
1376 |
}
|
|
1377 |
}
|
|
1378 |
|
|
1379 |
/* Here is where we set the relative costs of the different filters. We
|
|
1380 |
* should take the desired compression level into account when setting
|
|
1381 |
* the costs, so that Paeth, for instance, has a high relative cost at low
|
|
1382 |
* compression levels, while it has a lower relative cost at higher
|
|
1383 |
* compression settings. The filter types are in order of increasing
|
|
1384 |
* relative cost, so it would be possible to do this with an algorithm.
|
|
1385 |
*/
|
|
1386 |
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
|
|
1387 |
if (filter_costs[i] >= PNG_FP_1)
|
|
1388 |
{
|
|
1389 |
png_uint_32 tmp;
|
|
1390 |
|
|
1391 |
/* Use a 32 bit unsigned temporary here because otherwise the
|
|
1392 |
* intermediate value will be a 32 bit *signed* integer (ANSI rules)
|
|
1393 |
* and this will get the wrong answer on division.
|
|
1394 |
*/
|
|
1395 |
tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
|
|
1396 |
tmp /= filter_costs[i];
|
|
1397 |
|
|
1398 |
png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
|
|
1399 |
|
|
1400 |
tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
|
|
1401 |
tmp /= PNG_FP_1;
|
|
1402 |
|
|
1403 |
png_ptr->filter_costs[i] = (png_uint_16)tmp;
|
|
1404 |
}
|
|
1405 |
}
|
|
1406 |
}
|
|
1407 |
#endif /* FIXED_POINT */
|
2
|
1408 |
#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
|
|
1409 |
|
|
1410 |
void PNGAPI
|
|
1411 |
png_set_compression_level(png_structp png_ptr, int level)
|
|
1412 |
{
|
10576
|
1413 |
png_debug(1, "in png_set_compression_level");
|
|
1414 |
|
2
|
1415 |
if (png_ptr == NULL)
|
|
1416 |
return;
|
10576
|
1417 |
|
2
|
1418 |
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
|
|
1419 |
png_ptr->zlib_level = level;
|
|
1420 |
}
|
|
1421 |
|
|
1422 |
void PNGAPI
|
|
1423 |
png_set_compression_mem_level(png_structp png_ptr, int mem_level)
|
|
1424 |
{
|
10576
|
1425 |
png_debug(1, "in png_set_compression_mem_level");
|
|
1426 |
|
2
|
1427 |
if (png_ptr == NULL)
|
|
1428 |
return;
|
10576
|
1429 |
|
2
|
1430 |
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
|
|
1431 |
png_ptr->zlib_mem_level = mem_level;
|
|
1432 |
}
|
|
1433 |
|
|
1434 |
void PNGAPI
|
|
1435 |
png_set_compression_strategy(png_structp png_ptr, int strategy)
|
|
1436 |
{
|
10576
|
1437 |
png_debug(1, "in png_set_compression_strategy");
|
|
1438 |
|
2
|
1439 |
if (png_ptr == NULL)
|
|
1440 |
return;
|
10576
|
1441 |
|
2
|
1442 |
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
|
|
1443 |
png_ptr->zlib_strategy = strategy;
|
|
1444 |
}
|
|
1445 |
|
10576
|
1446 |
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
|
|
1447 |
* smaller value of window_bits if it can do so safely.
|
|
1448 |
*/
|
2
|
1449 |
void PNGAPI
|
|
1450 |
png_set_compression_window_bits(png_structp png_ptr, int window_bits)
|
|
1451 |
{
|
|
1452 |
if (png_ptr == NULL)
|
|
1453 |
return;
|
10576
|
1454 |
|
2
|
1455 |
if (window_bits > 15)
|
|
1456 |
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
|
10576
|
1457 |
|
2
|
1458 |
else if (window_bits < 8)
|
|
1459 |
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
|
10576
|
1460 |
|
2
|
1461 |
#ifndef WBITS_8_OK
|
10576
|
1462 |
/* Avoid libpng bug with 256-byte windows */
|
2
|
1463 |
if (window_bits == 8)
|
10576
|
1464 |
{
|
|
1465 |
png_warning(png_ptr, "Compression window is being reset to 512");
|
|
1466 |
window_bits = 9;
|
|
1467 |
}
|
|
1468 |
|
2
|
1469 |
#endif
|
|
1470 |
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
|
|
1471 |
png_ptr->zlib_window_bits = window_bits;
|
|
1472 |
}
|
|
1473 |
|
|
1474 |
void PNGAPI
|
|
1475 |
png_set_compression_method(png_structp png_ptr, int method)
|
|
1476 |
{
|
10576
|
1477 |
png_debug(1, "in png_set_compression_method");
|
|
1478 |
|
2
|
1479 |
if (png_ptr == NULL)
|
|
1480 |
return;
|
10576
|
1481 |
|
2
|
1482 |
if (method != 8)
|
|
1483 |
png_warning(png_ptr, "Only compression method 8 is supported by PNG");
|
10576
|
1484 |
|
2
|
1485 |
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
|
|
1486 |
png_ptr->zlib_method = method;
|
|
1487 |
}
|
|
1488 |
|
10576
|
1489 |
/* The following were added to libpng-1.5.4 */
|
|
1490 |
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
|
|
1491 |
void PNGAPI
|
|
1492 |
png_set_text_compression_level(png_structp png_ptr, int level)
|
|
1493 |
{
|
|
1494 |
png_debug(1, "in png_set_text_compression_level");
|
|
1495 |
|
|
1496 |
if (png_ptr == NULL)
|
|
1497 |
return;
|
|
1498 |
|
|
1499 |
png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_LEVEL;
|
|
1500 |
png_ptr->zlib_text_level = level;
|
|
1501 |
}
|
|
1502 |
|
|
1503 |
void PNGAPI
|
|
1504 |
png_set_text_compression_mem_level(png_structp png_ptr, int mem_level)
|
|
1505 |
{
|
|
1506 |
png_debug(1, "in png_set_text_compression_mem_level");
|
|
1507 |
|
|
1508 |
if (png_ptr == NULL)
|
|
1509 |
return;
|
|
1510 |
|
|
1511 |
png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL;
|
|
1512 |
png_ptr->zlib_text_mem_level = mem_level;
|
|
1513 |
}
|
|
1514 |
|
|
1515 |
void PNGAPI
|
|
1516 |
png_set_text_compression_strategy(png_structp png_ptr, int strategy)
|
|
1517 |
{
|
|
1518 |
png_debug(1, "in png_set_text_compression_strategy");
|
|
1519 |
|
|
1520 |
if (png_ptr == NULL)
|
|
1521 |
return;
|
|
1522 |
|
|
1523 |
png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_STRATEGY;
|
|
1524 |
png_ptr->zlib_text_strategy = strategy;
|
|
1525 |
}
|
|
1526 |
|
|
1527 |
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
|
|
1528 |
* smaller value of window_bits if it can do so safely.
|
|
1529 |
*/
|
|
1530 |
void PNGAPI
|
|
1531 |
png_set_text_compression_window_bits(png_structp png_ptr, int window_bits)
|
|
1532 |
{
|
|
1533 |
if (png_ptr == NULL)
|
|
1534 |
return;
|
|
1535 |
|
|
1536 |
if (window_bits > 15)
|
|
1537 |
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
|
|
1538 |
|
|
1539 |
else if (window_bits < 8)
|
|
1540 |
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
|
|
1541 |
|
|
1542 |
#ifndef WBITS_8_OK
|
|
1543 |
/* Avoid libpng bug with 256-byte windows */
|
|
1544 |
if (window_bits == 8)
|
|
1545 |
{
|
|
1546 |
png_warning(png_ptr, "Text compression window is being reset to 512");
|
|
1547 |
window_bits = 9;
|
|
1548 |
}
|
|
1549 |
|
|
1550 |
#endif
|
|
1551 |
png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS;
|
|
1552 |
png_ptr->zlib_text_window_bits = window_bits;
|
|
1553 |
}
|
|
1554 |
|
|
1555 |
void PNGAPI
|
|
1556 |
png_set_text_compression_method(png_structp png_ptr, int method)
|
|
1557 |
{
|
|
1558 |
png_debug(1, "in png_set_text_compression_method");
|
|
1559 |
|
|
1560 |
if (png_ptr == NULL)
|
|
1561 |
return;
|
|
1562 |
|
|
1563 |
if (method != 8)
|
|
1564 |
png_warning(png_ptr, "Only compression method 8 is supported by PNG");
|
|
1565 |
|
|
1566 |
png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_METHOD;
|
|
1567 |
png_ptr->zlib_text_method = method;
|
|
1568 |
}
|
|
1569 |
#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
|
|
1570 |
/* end of API added to libpng-1.5.4 */
|
|
1571 |
|
2
|
1572 |
void PNGAPI
|
|
1573 |
png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
|
|
1574 |
{
|
|
1575 |
if (png_ptr == NULL)
|
|
1576 |
return;
|
10576
|
1577 |
|
2
|
1578 |
png_ptr->write_row_fn = write_row_fn;
|
|
1579 |
}
|
|
1580 |
|
10576
|
1581 |
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
|
2
|
1582 |
void PNGAPI
|
|
1583 |
png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
|
10576
|
1584 |
write_user_transform_fn)
|
2
|
1585 |
{
|
10576
|
1586 |
png_debug(1, "in png_set_write_user_transform_fn");
|
|
1587 |
|
2
|
1588 |
if (png_ptr == NULL)
|
|
1589 |
return;
|
10576
|
1590 |
|
2
|
1591 |
png_ptr->transformations |= PNG_USER_TRANSFORM;
|
|
1592 |
png_ptr->write_user_transform_fn = write_user_transform_fn;
|
|
1593 |
}
|
|
1594 |
#endif
|
|
1595 |
|
|
1596 |
|
10576
|
1597 |
#ifdef PNG_INFO_IMAGE_SUPPORTED
|
2
|
1598 |
void PNGAPI
|
|
1599 |
png_write_png(png_structp png_ptr, png_infop info_ptr,
|
10576
|
1600 |
int transforms, voidp params)
|
2
|
1601 |
{
|
|
1602 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
1603 |
return;
|
|
1604 |
|
|
1605 |
/* Write the file header information. */
|
|
1606 |
png_write_info(png_ptr, info_ptr);
|
|
1607 |
|
|
1608 |
/* ------ these transformations don't touch the info structure ------- */
|
|
1609 |
|
10576
|
1610 |
#ifdef PNG_WRITE_INVERT_SUPPORTED
|
|
1611 |
/* Invert monochrome pixels */
|
2
|
1612 |
if (transforms & PNG_TRANSFORM_INVERT_MONO)
|
10576
|
1613 |
png_set_invert_mono(png_ptr);
|
2
|
1614 |
#endif
|
|
1615 |
|
10576
|
1616 |
#ifdef PNG_WRITE_SHIFT_SUPPORTED
|
2
|
1617 |
/* Shift the pixels up to a legal bit depth and fill in
|
|
1618 |
* as appropriate to correctly scale the image.
|
|
1619 |
*/
|
|
1620 |
if ((transforms & PNG_TRANSFORM_SHIFT)
|
10576
|
1621 |
&& (info_ptr->valid & PNG_INFO_sBIT))
|
|
1622 |
png_set_shift(png_ptr, &info_ptr->sig_bit);
|
2
|
1623 |
#endif
|
|
1624 |
|
10576
|
1625 |
#ifdef PNG_WRITE_PACK_SUPPORTED
|
|
1626 |
/* Pack pixels into bytes */
|
2
|
1627 |
if (transforms & PNG_TRANSFORM_PACKING)
|
|
1628 |
png_set_packing(png_ptr);
|
|
1629 |
#endif
|
|
1630 |
|
10576
|
1631 |
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
|
|
1632 |
/* Swap location of alpha bytes from ARGB to RGBA */
|
2
|
1633 |
if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
|
10576
|
1634 |
png_set_swap_alpha(png_ptr);
|
2
|
1635 |
#endif
|
|
1636 |
|
10576
|
1637 |
#ifdef PNG_WRITE_FILLER_SUPPORTED
|
|
1638 |
/* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */
|
|
1639 |
if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
|
|
1640 |
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
|
|
1641 |
|
|
1642 |
else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
|
|
1643 |
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
|
2
|
1644 |
#endif
|
|
1645 |
|
10576
|
1646 |
#ifdef PNG_WRITE_BGR_SUPPORTED
|
|
1647 |
/* Flip BGR pixels to RGB */
|
2
|
1648 |
if (transforms & PNG_TRANSFORM_BGR)
|
10576
|
1649 |
png_set_bgr(png_ptr);
|
2
|
1650 |
#endif
|
|
1651 |
|
10576
|
1652 |
#ifdef PNG_WRITE_SWAP_SUPPORTED
|
|
1653 |
/* Swap bytes of 16-bit files to most significant byte first */
|
2
|
1654 |
if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
|
10576
|
1655 |
png_set_swap(png_ptr);
|
2
|
1656 |
#endif
|
|
1657 |
|
10576
|
1658 |
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
|
|
1659 |
/* Swap bits of 1, 2, 4 bit packed pixel formats */
|
2
|
1660 |
if (transforms & PNG_TRANSFORM_PACKSWAP)
|
10576
|
1661 |
png_set_packswap(png_ptr);
|
|
1662 |
#endif
|
|
1663 |
|
|
1664 |
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
|
|
1665 |
/* Invert the alpha channel from opacity to transparency */
|
|
1666 |
if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
|
|
1667 |
png_set_invert_alpha(png_ptr);
|
2
|
1668 |
#endif
|
|
1669 |
|
|
1670 |
/* ----------------------- end of transformations ------------------- */
|
|
1671 |
|
10576
|
1672 |
/* Write the bits */
|
2
|
1673 |
if (info_ptr->valid & PNG_INFO_IDAT)
|
|
1674 |
png_write_image(png_ptr, info_ptr->row_pointers);
|
|
1675 |
|
|
1676 |
/* It is REQUIRED to call this to finish writing the rest of the file */
|
|
1677 |
png_write_end(png_ptr, info_ptr);
|
|
1678 |
|
10576
|
1679 |
PNG_UNUSED(transforms) /* Quiet compiler warnings */
|
|
1680 |
PNG_UNUSED(params)
|
2
|
1681 |
}
|
|
1682 |
#endif
|
|
1683 |
#endif /* PNG_WRITE_SUPPORTED */
|