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 |
*
|
29913
|
32 |
* Last changed in libpng 1.6.15 [November 20, 2014]
|
|
33 |
* Copyright (c) 1998-2014 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"
|
29913
|
43 |
#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
|
|
44 |
# include <errno.h>
|
|
45 |
#endif
|
10576
|
46 |
|
2
|
47 |
#ifdef PNG_WRITE_SUPPORTED
|
|
48 |
|
29913
|
49 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
|
|
50 |
/* Write out all the unknown chunks for the current given location */
|
|
51 |
static void
|
|
52 |
write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
|
|
53 |
unsigned int where)
|
|
54 |
{
|
|
55 |
if (info_ptr->unknown_chunks_num != 0)
|
|
56 |
{
|
|
57 |
png_const_unknown_chunkp up;
|
|
58 |
|
|
59 |
png_debug(5, "writing extra chunks");
|
|
60 |
|
|
61 |
for (up = info_ptr->unknown_chunks;
|
|
62 |
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
|
|
63 |
++up)
|
|
64 |
if ((up->location & where) != 0)
|
|
65 |
{
|
|
66 |
/* If per-chunk unknown chunk handling is enabled use it, otherwise
|
|
67 |
* just write the chunks the application has set.
|
|
68 |
*/
|
|
69 |
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
|
|
70 |
int keep = png_handle_as_unknown(png_ptr, up->name);
|
|
71 |
|
|
72 |
/* NOTE: this code is radically different from the read side in the
|
|
73 |
* matter of handling an ancillary unknown chunk. In the read side
|
|
74 |
* the default behavior is to discard it, in the code below the default
|
|
75 |
* behavior is to write it. Critical chunks are, however, only
|
|
76 |
* written if explicitly listed or if the default is set to write all
|
|
77 |
* unknown chunks.
|
|
78 |
*
|
|
79 |
* The default handling is also slightly weird - it is not possible to
|
|
80 |
* stop the writing of all unsafe-to-copy chunks!
|
|
81 |
*
|
|
82 |
* TODO: REVIEW: this would seem to be a bug.
|
|
83 |
*/
|
|
84 |
if (keep != PNG_HANDLE_CHUNK_NEVER &&
|
|
85 |
((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
|
|
86 |
keep == PNG_HANDLE_CHUNK_ALWAYS ||
|
|
87 |
(keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
|
|
88 |
png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
|
|
89 |
#endif
|
|
90 |
{
|
|
91 |
/* TODO: review, what is wrong with a zero length unknown chunk? */
|
|
92 |
if (up->size == 0)
|
|
93 |
png_warning(png_ptr, "Writing zero-length unknown chunk");
|
|
94 |
|
|
95 |
png_write_chunk(png_ptr, up->name, up->data, up->size);
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
}
|
|
100 |
#endif /* WRITE_UNKNOWN_CHUNKS */
|
|
101 |
|
2
|
102 |
/* Writes all the PNG information. This is the suggested way to use the
|
|
103 |
* library. If you have a new chunk to add, make a function to write it,
|
|
104 |
* and put it in the correct location here. If you want the chunk written
|
|
105 |
* after the image data, put it in png_write_end(). I strongly encourage
|
|
106 |
* you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
|
|
107 |
* the chunk, as that will keep the code from breaking if you want to just
|
|
108 |
* write a plain PNG file. If you have long comments, I suggest writing
|
|
109 |
* them in png_write_end(), and compressing them.
|
|
110 |
*/
|
|
111 |
void PNGAPI
|
29913
|
112 |
png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
|
2
|
113 |
{
|
10576
|
114 |
png_debug(1, "in png_write_info_before_PLTE");
|
|
115 |
|
2
|
116 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
117 |
return;
|
10576
|
118 |
|
29913
|
119 |
if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
|
2
|
120 |
{
|
10576
|
121 |
/* Write PNG signature */
|
|
122 |
png_write_sig(png_ptr);
|
|
123 |
|
|
124 |
#ifdef PNG_MNG_FEATURES_SUPPORTED
|
29913
|
125 |
if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \
|
|
126 |
png_ptr->mng_features_permitted != 0)
|
2
|
127 |
{
|
10576
|
128 |
png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
|
|
129 |
png_ptr->mng_features_permitted = 0;
|
2
|
130 |
}
|
|
131 |
#endif
|
10576
|
132 |
|
|
133 |
/* Write IHDR information. */
|
2
|
134 |
png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
|
10576
|
135 |
info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
|
|
136 |
info_ptr->filter_type,
|
|
137 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
29913
|
138 |
info_ptr->interlace_type
|
2
|
139 |
#else
|
29913
|
140 |
0
|
2
|
141 |
#endif
|
29913
|
142 |
);
|
|
143 |
|
10576
|
144 |
/* The rest of these check to see if the valid field has the appropriate
|
|
145 |
* flag set, and if it does, writes the chunk.
|
29913
|
146 |
*
|
|
147 |
* 1.6.0: COLORSPACE support controls the writing of these chunks too, and
|
|
148 |
* the chunks will be written if the WRITE routine is there and information
|
|
149 |
* is available in the COLORSPACE. (See png_colorspace_sync_info in png.c
|
|
150 |
* for where the valid flags get set.)
|
|
151 |
*
|
|
152 |
* Under certain circumstances the colorspace can be invalidated without
|
|
153 |
* syncing the info_struct 'valid' flags; this happens if libpng detects and
|
|
154 |
* error and calls png_error while the color space is being set, yet the
|
|
155 |
* application continues writing the PNG. So check the 'invalid' flag here
|
|
156 |
* too.
|
10576
|
157 |
*/
|
29913
|
158 |
#ifdef PNG_GAMMA_SUPPORTED
|
|
159 |
# ifdef PNG_WRITE_gAMA_SUPPORTED
|
|
160 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
|
|
161 |
(info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 &&
|
|
162 |
(info_ptr->valid & PNG_INFO_gAMA) != 0)
|
|
163 |
png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
|
|
164 |
# endif
|
2
|
165 |
#endif
|
10576
|
166 |
|
29913
|
167 |
#ifdef PNG_COLORSPACE_SUPPORTED
|
|
168 |
/* Write only one of sRGB or an ICC profile. If a profile was supplied
|
|
169 |
* and it matches one of the known sRGB ones issue a warning.
|
|
170 |
*/
|
|
171 |
# ifdef PNG_WRITE_iCCP_SUPPORTED
|
|
172 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
|
|
173 |
(info_ptr->valid & PNG_INFO_iCCP) != 0)
|
|
174 |
{
|
|
175 |
# ifdef PNG_WRITE_sRGB_SUPPORTED
|
|
176 |
if ((info_ptr->valid & PNG_INFO_sRGB) != 0)
|
|
177 |
png_app_warning(png_ptr,
|
|
178 |
"profile matches sRGB but writing iCCP instead");
|
|
179 |
# endif
|
|
180 |
|
|
181 |
png_write_iCCP(png_ptr, info_ptr->iccp_name,
|
|
182 |
info_ptr->iccp_profile);
|
|
183 |
}
|
|
184 |
# ifdef PNG_WRITE_sRGB_SUPPORTED
|
|
185 |
else
|
|
186 |
# endif
|
|
187 |
# endif
|
|
188 |
|
|
189 |
# ifdef PNG_WRITE_sRGB_SUPPORTED
|
|
190 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
|
|
191 |
(info_ptr->valid & PNG_INFO_sRGB) != 0)
|
|
192 |
png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
|
|
193 |
# endif /* WRITE_sRGB */
|
|
194 |
#endif /* COLORSPACE */
|
|
195 |
|
10576
|
196 |
#ifdef PNG_WRITE_sBIT_SUPPORTED
|
29913
|
197 |
if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
|
2
|
198 |
png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
|
|
199 |
#endif
|
29913
|
200 |
|
|
201 |
#ifdef PNG_COLORSPACE_SUPPORTED
|
|
202 |
# ifdef PNG_WRITE_cHRM_SUPPORTED
|
|
203 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
|
|
204 |
(info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 &&
|
|
205 |
(info_ptr->valid & PNG_INFO_cHRM) != 0)
|
|
206 |
png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
|
|
207 |
# endif
|
2
|
208 |
#endif
|
10576
|
209 |
|
|
210 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
|
29913
|
211 |
write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
|
|
212 |
#endif
|
2
|
213 |
|
|
214 |
png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
void PNGAPI
|
29913
|
219 |
png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
|
2
|
220 |
{
|
|
221 |
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
|
|
222 |
int i;
|
|
223 |
#endif
|
|
224 |
|
10576
|
225 |
png_debug(1, "in png_write_info");
|
2
|
226 |
|
|
227 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
228 |
return;
|
|
229 |
|
|
230 |
png_write_info_before_PLTE(png_ptr, info_ptr);
|
|
231 |
|
29913
|
232 |
if ((info_ptr->valid & PNG_INFO_PLTE) != 0)
|
2
|
233 |
png_write_PLTE(png_ptr, info_ptr->palette,
|
10576
|
234 |
(png_uint_32)info_ptr->num_palette);
|
|
235 |
|
29913
|
236 |
else if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) !=0)
|
2
|
237 |
png_error(png_ptr, "Valid palette required for paletted images");
|
|
238 |
|
10576
|
239 |
#ifdef PNG_WRITE_tRNS_SUPPORTED
|
29913
|
240 |
if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
|
10576
|
241 |
{
|
|
242 |
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
|
|
243 |
/* Invert the alpha channel (in tRNS) */
|
29913
|
244 |
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
|
10576
|
245 |
info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
|
2
|
246 |
{
|
10576
|
247 |
int j;
|
|
248 |
for (j = 0; j<(int)info_ptr->num_trans; j++)
|
|
249 |
info_ptr->trans_alpha[j] =
|
|
250 |
(png_byte)(255 - info_ptr->trans_alpha[j]);
|
2
|
251 |
}
|
|
252 |
#endif
|
10576
|
253 |
png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
|
|
254 |
info_ptr->num_trans, info_ptr->color_type);
|
|
255 |
}
|
|
256 |
#endif
|
|
257 |
#ifdef PNG_WRITE_bKGD_SUPPORTED
|
29913
|
258 |
if ((info_ptr->valid & PNG_INFO_bKGD) != 0)
|
2
|
259 |
png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
|
|
260 |
#endif
|
10576
|
261 |
|
|
262 |
#ifdef PNG_WRITE_hIST_SUPPORTED
|
29913
|
263 |
if ((info_ptr->valid & PNG_INFO_hIST) != 0)
|
2
|
264 |
png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
|
|
265 |
#endif
|
10576
|
266 |
|
|
267 |
#ifdef PNG_WRITE_oFFs_SUPPORTED
|
29913
|
268 |
if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
|
2
|
269 |
png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
|
10576
|
270 |
info_ptr->offset_unit_type);
|
2
|
271 |
#endif
|
10576
|
272 |
|
|
273 |
#ifdef PNG_WRITE_pCAL_SUPPORTED
|
29913
|
274 |
if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
|
2
|
275 |
png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
|
10576
|
276 |
info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
|
|
277 |
info_ptr->pcal_units, info_ptr->pcal_params);
|
2
|
278 |
#endif
|
10576
|
279 |
|
|
280 |
#ifdef PNG_WRITE_sCAL_SUPPORTED
|
29913
|
281 |
if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
|
2
|
282 |
png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
|
|
283 |
info_ptr->scal_s_width, info_ptr->scal_s_height);
|
10576
|
284 |
#endif /* sCAL */
|
|
285 |
|
|
286 |
#ifdef PNG_WRITE_pHYs_SUPPORTED
|
29913
|
287 |
if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
|
2
|
288 |
png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
|
10576
|
289 |
info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
|
|
290 |
#endif /* pHYs */
|
|
291 |
|
|
292 |
#ifdef PNG_WRITE_tIME_SUPPORTED
|
29913
|
293 |
if ((info_ptr->valid & PNG_INFO_tIME) != 0)
|
2
|
294 |
{
|
|
295 |
png_write_tIME(png_ptr, &(info_ptr->mod_time));
|
|
296 |
png_ptr->mode |= PNG_WROTE_tIME;
|
|
297 |
}
|
10576
|
298 |
#endif /* tIME */
|
|
299 |
|
|
300 |
#ifdef PNG_WRITE_sPLT_SUPPORTED
|
29913
|
301 |
if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
|
10576
|
302 |
for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
|
|
303 |
png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
|
|
304 |
#endif /* sPLT */
|
|
305 |
|
|
306 |
#ifdef PNG_WRITE_TEXT_SUPPORTED
|
2
|
307 |
/* Check to see if we need to write text chunks */
|
|
308 |
for (i = 0; i < info_ptr->num_text; i++)
|
|
309 |
{
|
10576
|
310 |
png_debug2(2, "Writing header text chunk %d, type %d", i,
|
|
311 |
info_ptr->text[i].compression);
|
|
312 |
/* An internationalized chunk? */
|
2
|
313 |
if (info_ptr->text[i].compression > 0)
|
|
314 |
{
|
10576
|
315 |
#ifdef PNG_WRITE_iTXt_SUPPORTED
|
|
316 |
/* Write international chunk */
|
|
317 |
png_write_iTXt(png_ptr,
|
|
318 |
info_ptr->text[i].compression,
|
|
319 |
info_ptr->text[i].key,
|
|
320 |
info_ptr->text[i].lang,
|
|
321 |
info_ptr->text[i].lang_key,
|
|
322 |
info_ptr->text[i].text);
|
29913
|
323 |
/* Mark this chunk as written */
|
|
324 |
if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
|
|
325 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
|
326 |
else
|
|
327 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
|
2
|
328 |
#else
|
29913
|
329 |
png_warning(png_ptr, "Unable to write international text");
|
2
|
330 |
#endif
|
|
331 |
}
|
10576
|
332 |
|
2
|
333 |
/* If we want a compressed text chunk */
|
|
334 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
|
|
335 |
{
|
10576
|
336 |
#ifdef PNG_WRITE_zTXt_SUPPORTED
|
|
337 |
/* Write compressed chunk */
|
2
|
338 |
png_write_zTXt(png_ptr, info_ptr->text[i].key,
|
29913
|
339 |
info_ptr->text[i].text, info_ptr->text[i].compression);
|
|
340 |
/* Mark this chunk as written */
|
|
341 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
|
2
|
342 |
#else
|
|
343 |
png_warning(png_ptr, "Unable to write compressed text");
|
|
344 |
#endif
|
|
345 |
}
|
10576
|
346 |
|
2
|
347 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
|
|
348 |
{
|
10576
|
349 |
#ifdef PNG_WRITE_tEXt_SUPPORTED
|
|
350 |
/* Write uncompressed chunk */
|
2
|
351 |
png_write_tEXt(png_ptr, info_ptr->text[i].key,
|
10576
|
352 |
info_ptr->text[i].text,
|
|
353 |
0);
|
|
354 |
/* Mark this chunk as written */
|
|
355 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
2
|
356 |
#else
|
10576
|
357 |
/* Can't get here */
|
2
|
358 |
png_warning(png_ptr, "Unable to write uncompressed text");
|
|
359 |
#endif
|
|
360 |
}
|
|
361 |
}
|
10576
|
362 |
#endif /* tEXt */
|
|
363 |
|
|
364 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
|
29913
|
365 |
write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
|
2
|
366 |
#endif
|
|
367 |
}
|
|
368 |
|
|
369 |
/* Writes the end of the PNG file. If you don't want to write comments or
|
|
370 |
* time information, you can pass NULL for info. If you already wrote these
|
|
371 |
* in png_write_info(), do not write them again here. If you have long
|
|
372 |
* comments, I suggest writing them here, and compressing them.
|
|
373 |
*/
|
|
374 |
void PNGAPI
|
29913
|
375 |
png_write_end(png_structrp png_ptr, png_inforp info_ptr)
|
2
|
376 |
{
|
10576
|
377 |
png_debug(1, "in png_write_end");
|
|
378 |
|
2
|
379 |
if (png_ptr == NULL)
|
|
380 |
return;
|
10576
|
381 |
|
29913
|
382 |
if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
|
2
|
383 |
png_error(png_ptr, "No IDATs written into file");
|
|
384 |
|
29913
|
385 |
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
|
|
386 |
if (png_ptr->num_palette_max > png_ptr->num_palette)
|
|
387 |
png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
|
|
388 |
#endif
|
|
389 |
|
10576
|
390 |
/* See if user wants us to write information chunks */
|
2
|
391 |
if (info_ptr != NULL)
|
|
392 |
{
|
10576
|
393 |
#ifdef PNG_WRITE_TEXT_SUPPORTED
|
2
|
394 |
int i; /* local index variable */
|
|
395 |
#endif
|
10576
|
396 |
#ifdef PNG_WRITE_tIME_SUPPORTED
|
|
397 |
/* Check to see if user has supplied a time chunk */
|
29913
|
398 |
if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
|
|
399 |
(png_ptr->mode & PNG_WROTE_tIME) == 0)
|
2
|
400 |
png_write_tIME(png_ptr, &(info_ptr->mod_time));
|
10576
|
401 |
|
2
|
402 |
#endif
|
10576
|
403 |
#ifdef PNG_WRITE_TEXT_SUPPORTED
|
|
404 |
/* Loop through comment chunks */
|
2
|
405 |
for (i = 0; i < info_ptr->num_text; i++)
|
|
406 |
{
|
10576
|
407 |
png_debug2(2, "Writing trailer text chunk %d, type %d", i,
|
2
|
408 |
info_ptr->text[i].compression);
|
10576
|
409 |
/* An internationalized chunk? */
|
2
|
410 |
if (info_ptr->text[i].compression > 0)
|
|
411 |
{
|
10576
|
412 |
#ifdef PNG_WRITE_iTXt_SUPPORTED
|
|
413 |
/* Write international chunk */
|
|
414 |
png_write_iTXt(png_ptr,
|
|
415 |
info_ptr->text[i].compression,
|
|
416 |
info_ptr->text[i].key,
|
|
417 |
info_ptr->text[i].lang,
|
|
418 |
info_ptr->text[i].lang_key,
|
|
419 |
info_ptr->text[i].text);
|
29913
|
420 |
/* Mark this chunk as written */
|
|
421 |
if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
|
|
422 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
|
423 |
else
|
|
424 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
|
2
|
425 |
#else
|
10576
|
426 |
png_warning(png_ptr, "Unable to write international text");
|
2
|
427 |
#endif
|
|
428 |
}
|
10576
|
429 |
|
2
|
430 |
else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
|
|
431 |
{
|
10576
|
432 |
#ifdef PNG_WRITE_zTXt_SUPPORTED
|
|
433 |
/* Write compressed chunk */
|
2
|
434 |
png_write_zTXt(png_ptr, info_ptr->text[i].key,
|
29913
|
435 |
info_ptr->text[i].text, info_ptr->text[i].compression);
|
|
436 |
/* Mark this chunk as written */
|
|
437 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
|
2
|
438 |
#else
|
|
439 |
png_warning(png_ptr, "Unable to write compressed text");
|
|
440 |
#endif
|
|
441 |
}
|
10576
|
442 |
|
2
|
443 |
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
|
|
444 |
{
|
10576
|
445 |
#ifdef PNG_WRITE_tEXt_SUPPORTED
|
|
446 |
/* Write uncompressed chunk */
|
2
|
447 |
png_write_tEXt(png_ptr, info_ptr->text[i].key,
|
10576
|
448 |
info_ptr->text[i].text, 0);
|
29913
|
449 |
/* Mark this chunk as written */
|
|
450 |
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
|
2
|
451 |
#else
|
|
452 |
png_warning(png_ptr, "Unable to write uncompressed text");
|
|
453 |
#endif
|
|
454 |
}
|
|
455 |
}
|
|
456 |
#endif
|
10576
|
457 |
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
|
29913
|
458 |
write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
|
2
|
459 |
#endif
|
|
460 |
}
|
|
461 |
|
|
462 |
png_ptr->mode |= PNG_AFTER_IDAT;
|
|
463 |
|
10576
|
464 |
/* Write end of PNG file */
|
2
|
465 |
png_write_IEND(png_ptr);
|
29913
|
466 |
|
10576
|
467 |
/* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
|
|
468 |
* and restored again in libpng-1.2.30, may cause some applications that
|
|
469 |
* do not set png_ptr->output_flush_fn to crash. If your application
|
|
470 |
* experiences a problem, please try building libpng with
|
|
471 |
* PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
|
|
472 |
* png-mng-implement at lists.sf.net .
|
|
473 |
*/
|
|
474 |
#ifdef PNG_WRITE_FLUSH_SUPPORTED
|
|
475 |
# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
|
2
|
476 |
png_flush(png_ptr);
|
10576
|
477 |
# endif
|
2
|
478 |
#endif
|
|
479 |
}
|
|
480 |
|
10576
|
481 |
#ifdef PNG_CONVERT_tIME_SUPPORTED
|
2
|
482 |
void PNGAPI
|
29913
|
483 |
png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
|
2
|
484 |
{
|
10576
|
485 |
png_debug(1, "in png_convert_from_struct_tm");
|
|
486 |
|
2
|
487 |
ptime->year = (png_uint_16)(1900 + ttime->tm_year);
|
|
488 |
ptime->month = (png_byte)(ttime->tm_mon + 1);
|
|
489 |
ptime->day = (png_byte)ttime->tm_mday;
|
|
490 |
ptime->hour = (png_byte)ttime->tm_hour;
|
|
491 |
ptime->minute = (png_byte)ttime->tm_min;
|
|
492 |
ptime->second = (png_byte)ttime->tm_sec;
|
|
493 |
}
|
|
494 |
|
|
495 |
void PNGAPI
|
|
496 |
png_convert_from_time_t(png_timep ptime, time_t ttime)
|
|
497 |
{
|
|
498 |
struct tm *tbuf;
|
|
499 |
|
10576
|
500 |
png_debug(1, "in png_convert_from_time_t");
|
|
501 |
|
2
|
502 |
tbuf = gmtime(&ttime);
|
|
503 |
png_convert_from_struct_tm(ptime, tbuf);
|
|
504 |
}
|
|
505 |
#endif
|
|
506 |
|
|
507 |
/* Initialize png_ptr structure, and allocate any memory needed */
|
10576
|
508 |
PNG_FUNCTION(png_structp,PNGAPI
|
|
509 |
png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
|
|
510 |
png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
|
2
|
511 |
{
|
29913
|
512 |
#ifndef PNG_USER_MEM_SUPPORTED
|
|
513 |
png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
|
|
514 |
error_fn, warn_fn, NULL, NULL, NULL);
|
|
515 |
#else
|
|
516 |
return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
|
|
517 |
warn_fn, NULL, NULL, NULL);
|
2
|
518 |
}
|
|
519 |
|
|
520 |
/* Alternate initialize png_ptr structure, and allocate any memory needed */
|
10576
|
521 |
PNG_FUNCTION(png_structp,PNGAPI
|
|
522 |
png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
|
|
523 |
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
|
|
524 |
png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
|
2
|
525 |
{
|
29913
|
526 |
png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
|
|
527 |
error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
|
|
528 |
#endif /* USER_MEM */
|
|
529 |
if (png_ptr != NULL)
|
|
530 |
{
|
|
531 |
/* Set the zlib control values to defaults; they can be overridden by the
|
|
532 |
* application after the struct has been created.
|
|
533 |
*/
|
|
534 |
png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
|
10576
|
535 |
|
29913
|
536 |
/* The 'zlib_strategy' setting is irrelevant because png_default_claim in
|
|
537 |
* pngwutil.c defaults it according to whether or not filters will be
|
|
538 |
* used, and ignores this setting.
|
|
539 |
*/
|
|
540 |
png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
|
|
541 |
png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
|
|
542 |
png_ptr->zlib_mem_level = 8;
|
|
543 |
png_ptr->zlib_window_bits = 15;
|
|
544 |
png_ptr->zlib_method = 8;
|
2
|
545 |
|
29913
|
546 |
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
|
|
547 |
png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
|
|
548 |
png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
|
|
549 |
png_ptr->zlib_text_mem_level = 8;
|
|
550 |
png_ptr->zlib_text_window_bits = 15;
|
|
551 |
png_ptr->zlib_text_method = 8;
|
|
552 |
#endif /* WRITE_COMPRESSED_TEXT */
|
|
553 |
|
|
554 |
/* This is a highly dubious configuration option; by default it is off,
|
|
555 |
* but it may be appropriate for private builds that are testing
|
|
556 |
* extensions not conformant to the current specification, or of
|
|
557 |
* applications that must not fail to write at all costs!
|
|
558 |
*/
|
|
559 |
#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
|
|
560 |
/* In stable builds only warn if an application error can be completely
|
|
561 |
* handled.
|
|
562 |
*/
|
|
563 |
png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
|
2
|
564 |
#endif
|
|
565 |
|
29913
|
566 |
/* App warnings are warnings in release (or release candidate) builds but
|
|
567 |
* are errors during development.
|
|
568 |
*/
|
|
569 |
#if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
|
|
570 |
png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
|
2
|
571 |
#endif
|
|
572 |
|
29913
|
573 |
/* TODO: delay this, it can be done in png_init_io() (if the app doesn't
|
|
574 |
* do it itself) avoiding setting the default function if it is not
|
|
575 |
* required.
|
|
576 |
*/
|
|
577 |
png_set_write_fn(png_ptr, NULL, NULL, NULL);
|
2
|
578 |
}
|
|
579 |
|
29913
|
580 |
return png_ptr;
|
2
|
581 |
}
|
|
582 |
|
|
583 |
|
|
584 |
/* Write a few rows of image data. If the image is interlaced,
|
|
585 |
* either you will have to write the 7 sub images, or, if you
|
|
586 |
* have called png_set_interlace_handling(), you will have to
|
|
587 |
* "write" the image seven times.
|
|
588 |
*/
|
|
589 |
void PNGAPI
|
29913
|
590 |
png_write_rows(png_structrp png_ptr, png_bytepp row,
|
10576
|
591 |
png_uint_32 num_rows)
|
2
|
592 |
{
|
|
593 |
png_uint_32 i; /* row counter */
|
|
594 |
png_bytepp rp; /* row pointer */
|
|
595 |
|
10576
|
596 |
png_debug(1, "in png_write_rows");
|
2
|
597 |
|
|
598 |
if (png_ptr == NULL)
|
|
599 |
return;
|
|
600 |
|
10576
|
601 |
/* Loop through the rows */
|
2
|
602 |
for (i = 0, rp = row; i < num_rows; i++, rp++)
|
|
603 |
{
|
|
604 |
png_write_row(png_ptr, *rp);
|
|
605 |
}
|
|
606 |
}
|
|
607 |
|
|
608 |
/* Write the image. You only need to call this function once, even
|
|
609 |
* if you are writing an interlaced image.
|
|
610 |
*/
|
|
611 |
void PNGAPI
|
29913
|
612 |
png_write_image(png_structrp png_ptr, png_bytepp image)
|
2
|
613 |
{
|
|
614 |
png_uint_32 i; /* row index */
|
|
615 |
int pass, num_pass; /* pass variables */
|
|
616 |
png_bytepp rp; /* points to current row */
|
|
617 |
|
|
618 |
if (png_ptr == NULL)
|
|
619 |
return;
|
|
620 |
|
10576
|
621 |
png_debug(1, "in png_write_image");
|
|
622 |
|
|
623 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
|
624 |
/* Initialize interlace handling. If image is not interlaced,
|
|
625 |
* this will set pass to 1
|
|
626 |
*/
|
2
|
627 |
num_pass = png_set_interlace_handling(png_ptr);
|
|
628 |
#else
|
|
629 |
num_pass = 1;
|
|
630 |
#endif
|
10576
|
631 |
/* Loop through passes */
|
2
|
632 |
for (pass = 0; pass < num_pass; pass++)
|
|
633 |
{
|
10576
|
634 |
/* Loop through image */
|
2
|
635 |
for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
|
|
636 |
{
|
|
637 |
png_write_row(png_ptr, *rp);
|
|
638 |
}
|
|
639 |
}
|
|
640 |
}
|
|
641 |
|
29913
|
642 |
#ifdef PNG_MNG_FEATURES_SUPPORTED
|
|
643 |
/* Performs intrapixel differencing */
|
|
644 |
static void
|
|
645 |
png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
|
|
646 |
{
|
|
647 |
png_debug(1, "in png_do_write_intrapixel");
|
|
648 |
|
|
649 |
if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
|
|
650 |
{
|
|
651 |
int bytes_per_pixel;
|
|
652 |
png_uint_32 row_width = row_info->width;
|
|
653 |
if (row_info->bit_depth == 8)
|
|
654 |
{
|
|
655 |
png_bytep rp;
|
|
656 |
png_uint_32 i;
|
|
657 |
|
|
658 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
|
|
659 |
bytes_per_pixel = 3;
|
|
660 |
|
|
661 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
|
662 |
bytes_per_pixel = 4;
|
|
663 |
|
|
664 |
else
|
|
665 |
return;
|
|
666 |
|
|
667 |
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
|
|
668 |
{
|
|
669 |
*(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
|
|
670 |
*(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
|
|
671 |
}
|
|
672 |
}
|
|
673 |
|
|
674 |
#ifdef PNG_WRITE_16BIT_SUPPORTED
|
|
675 |
else if (row_info->bit_depth == 16)
|
|
676 |
{
|
|
677 |
png_bytep rp;
|
|
678 |
png_uint_32 i;
|
|
679 |
|
|
680 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
|
|
681 |
bytes_per_pixel = 6;
|
|
682 |
|
|
683 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
|
684 |
bytes_per_pixel = 8;
|
|
685 |
|
|
686 |
else
|
|
687 |
return;
|
|
688 |
|
|
689 |
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
|
|
690 |
{
|
|
691 |
png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
|
|
692 |
png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
|
|
693 |
png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
|
|
694 |
png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
|
|
695 |
png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
|
|
696 |
*(rp ) = (png_byte)((red >> 8) & 0xff);
|
|
697 |
*(rp + 1) = (png_byte)(red & 0xff);
|
|
698 |
*(rp + 4) = (png_byte)((blue >> 8) & 0xff);
|
|
699 |
*(rp + 5) = (png_byte)(blue & 0xff);
|
|
700 |
}
|
|
701 |
}
|
|
702 |
#endif /* WRITE_16BIT */
|
|
703 |
}
|
|
704 |
}
|
|
705 |
#endif /* MNG_FEATURES */
|
|
706 |
|
10576
|
707 |
/* Called by user to write a row of image data */
|
2
|
708 |
void PNGAPI
|
29913
|
709 |
png_write_row(png_structrp png_ptr, png_const_bytep row)
|
2
|
710 |
{
|
29913
|
711 |
/* 1.5.6: moved from png_struct to be a local structure: */
|
|
712 |
png_row_info row_info;
|
|
713 |
|
2
|
714 |
if (png_ptr == NULL)
|
|
715 |
return;
|
10576
|
716 |
|
|
717 |
png_debug2(1, "in png_write_row (row %u, pass %d)",
|
2
|
718 |
png_ptr->row_number, png_ptr->pass);
|
|
719 |
|
10576
|
720 |
/* Initialize transformations and other stuff if first time */
|
2
|
721 |
if (png_ptr->row_number == 0 && png_ptr->pass == 0)
|
|
722 |
{
|
10576
|
723 |
/* Make sure we wrote the header info */
|
29913
|
724 |
if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
|
10576
|
725 |
png_error(png_ptr,
|
|
726 |
"png_write_info was never called before png_write_row");
|
2
|
727 |
|
10576
|
728 |
/* Check for transforms that have been set but were defined out */
|
2
|
729 |
#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
|
29913
|
730 |
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
|
10576
|
731 |
png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
|
2
|
732 |
#endif
|
10576
|
733 |
|
2
|
734 |
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
|
29913
|
735 |
if ((png_ptr->transformations & PNG_FILLER) != 0)
|
10576
|
736 |
png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
|
2
|
737 |
#endif
|
10576
|
738 |
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
|
|
739 |
defined(PNG_READ_PACKSWAP_SUPPORTED)
|
29913
|
740 |
if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
|
10576
|
741 |
png_warning(png_ptr,
|
|
742 |
"PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
|
2
|
743 |
#endif
|
10576
|
744 |
|
2
|
745 |
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
|
29913
|
746 |
if ((png_ptr->transformations & PNG_PACK) != 0)
|
10576
|
747 |
png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
|
2
|
748 |
#endif
|
10576
|
749 |
|
2
|
750 |
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
|
29913
|
751 |
if ((png_ptr->transformations & PNG_SHIFT) != 0)
|
10576
|
752 |
png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
|
2
|
753 |
#endif
|
10576
|
754 |
|
2
|
755 |
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
|
29913
|
756 |
if ((png_ptr->transformations & PNG_BGR) != 0)
|
10576
|
757 |
png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
|
2
|
758 |
#endif
|
10576
|
759 |
|
2
|
760 |
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
|
29913
|
761 |
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
|
10576
|
762 |
png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
|
2
|
763 |
#endif
|
|
764 |
|
|
765 |
png_write_start_row(png_ptr);
|
|
766 |
}
|
|
767 |
|
10576
|
768 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
|
769 |
/* If interlaced and not interested in row, return */
|
29913
|
770 |
if (png_ptr->interlaced != 0 &&
|
|
771 |
(png_ptr->transformations & PNG_INTERLACE) != 0)
|
2
|
772 |
{
|
|
773 |
switch (png_ptr->pass)
|
|
774 |
{
|
|
775 |
case 0:
|
29913
|
776 |
if ((png_ptr->row_number & 0x07) != 0)
|
2
|
777 |
{
|
|
778 |
png_write_finish_row(png_ptr);
|
|
779 |
return;
|
|
780 |
}
|
|
781 |
break;
|
10576
|
782 |
|
2
|
783 |
case 1:
|
29913
|
784 |
if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
|
2
|
785 |
{
|
|
786 |
png_write_finish_row(png_ptr);
|
|
787 |
return;
|
|
788 |
}
|
|
789 |
break;
|
10576
|
790 |
|
2
|
791 |
case 2:
|
|
792 |
if ((png_ptr->row_number & 0x07) != 4)
|
|
793 |
{
|
|
794 |
png_write_finish_row(png_ptr);
|
|
795 |
return;
|
|
796 |
}
|
|
797 |
break;
|
10576
|
798 |
|
2
|
799 |
case 3:
|
29913
|
800 |
if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
|
2
|
801 |
{
|
|
802 |
png_write_finish_row(png_ptr);
|
|
803 |
return;
|
|
804 |
}
|
|
805 |
break;
|
10576
|
806 |
|
2
|
807 |
case 4:
|
|
808 |
if ((png_ptr->row_number & 0x03) != 2)
|
|
809 |
{
|
|
810 |
png_write_finish_row(png_ptr);
|
|
811 |
return;
|
|
812 |
}
|
|
813 |
break;
|
10576
|
814 |
|
2
|
815 |
case 5:
|
29913
|
816 |
if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
|
2
|
817 |
{
|
|
818 |
png_write_finish_row(png_ptr);
|
|
819 |
return;
|
|
820 |
}
|
|
821 |
break;
|
10576
|
822 |
|
2
|
823 |
case 6:
|
29913
|
824 |
if ((png_ptr->row_number & 0x01) == 0)
|
2
|
825 |
{
|
|
826 |
png_write_finish_row(png_ptr);
|
|
827 |
return;
|
|
828 |
}
|
|
829 |
break;
|
10576
|
830 |
|
|
831 |
default: /* error: ignore it */
|
|
832 |
break;
|
2
|
833 |
}
|
|
834 |
}
|
|
835 |
#endif
|
|
836 |
|
10576
|
837 |
/* Set up row info for transformations */
|
29913
|
838 |
row_info.color_type = png_ptr->color_type;
|
|
839 |
row_info.width = png_ptr->usr_width;
|
|
840 |
row_info.channels = png_ptr->usr_channels;
|
|
841 |
row_info.bit_depth = png_ptr->usr_bit_depth;
|
|
842 |
row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
|
|
843 |
row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
|
2
|
844 |
|
29913
|
845 |
png_debug1(3, "row_info->color_type = %d", row_info.color_type);
|
|
846 |
png_debug1(3, "row_info->width = %u", row_info.width);
|
|
847 |
png_debug1(3, "row_info->channels = %d", row_info.channels);
|
|
848 |
png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
|
|
849 |
png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
|
|
850 |
png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
|
2
|
851 |
|
|
852 |
/* Copy user's row into buffer, leaving room for filter byte. */
|
29913
|
853 |
memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
|
2
|
854 |
|
10576
|
855 |
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
|
|
856 |
/* Handle interlacing */
|
2
|
857 |
if (png_ptr->interlaced && png_ptr->pass < 6 &&
|
29913
|
858 |
(png_ptr->transformations & PNG_INTERLACE) != 0)
|
2
|
859 |
{
|
29913
|
860 |
png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
|
10576
|
861 |
/* This should always get caught above, but still ... */
|
29913
|
862 |
if (row_info.width == 0)
|
2
|
863 |
{
|
|
864 |
png_write_finish_row(png_ptr);
|
|
865 |
return;
|
|
866 |
}
|
|
867 |
}
|
|
868 |
#endif
|
|
869 |
|
10576
|
870 |
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
|
|
871 |
/* Handle other transformations */
|
29913
|
872 |
if (png_ptr->transformations != 0)
|
|
873 |
png_do_write_transformations(png_ptr, &row_info);
|
10576
|
874 |
#endif
|
2
|
875 |
|
29913
|
876 |
/* At this point the row_info pixel depth must match the 'transformed' depth,
|
|
877 |
* which is also the output depth.
|
|
878 |
*/
|
|
879 |
if (row_info.pixel_depth != png_ptr->pixel_depth ||
|
|
880 |
row_info.pixel_depth != png_ptr->transformed_pixel_depth)
|
|
881 |
png_error(png_ptr, "internal write transform logic error");
|
|
882 |
|
10576
|
883 |
#ifdef PNG_MNG_FEATURES_SUPPORTED
|
2
|
884 |
/* Write filter_method 64 (intrapixel differencing) only if
|
|
885 |
* 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
|
|
886 |
* 2. Libpng did not write a PNG signature (this filter_method is only
|
|
887 |
* used in PNG datastreams that are embedded in MNG datastreams) and
|
|
888 |
* 3. The application called png_permit_mng_features with a mask that
|
|
889 |
* included PNG_FLAG_MNG_FILTER_64 and
|
|
890 |
* 4. The filter_method is 64 and
|
|
891 |
* 5. The color_type is RGB or RGBA
|
|
892 |
*/
|
29913
|
893 |
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
|
10576
|
894 |
(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
|
2
|
895 |
{
|
|
896 |
/* Intrapixel differencing */
|
29913
|
897 |
png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
|
2
|
898 |
}
|
|
899 |
#endif
|
|
900 |
|
29913
|
901 |
/* Added at libpng-1.5.10 */
|
|
902 |
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
|
|
903 |
/* Check for out-of-range palette index */
|
|
904 |
if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
|
|
905 |
png_ptr->num_palette_max >= 0)
|
|
906 |
png_do_check_palette_indexes(png_ptr, &row_info);
|
|
907 |
#endif
|
|
908 |
|
2
|
909 |
/* Find a filter if necessary, filter the row and write it out. */
|
29913
|
910 |
png_write_find_filter(png_ptr, &row_info);
|
2
|
911 |
|
|
912 |
if (png_ptr->write_row_fn != NULL)
|
|
913 |
(*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
|
|
914 |
}
|
|
915 |
|
10576
|
916 |
#ifdef PNG_WRITE_FLUSH_SUPPORTED
|
2
|
917 |
/* Set the automatic flush interval or 0 to turn flushing off */
|
|
918 |
void PNGAPI
|
29913
|
919 |
png_set_flush(png_structrp png_ptr, int nrows)
|
2
|
920 |
{
|
10576
|
921 |
png_debug(1, "in png_set_flush");
|
|
922 |
|
2
|
923 |
if (png_ptr == NULL)
|
|
924 |
return;
|
10576
|
925 |
|
2
|
926 |
png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
|
|
927 |
}
|
|
928 |
|
10576
|
929 |
/* Flush the current output buffers now */
|
2
|
930 |
void PNGAPI
|
29913
|
931 |
png_write_flush(png_structrp png_ptr)
|
2
|
932 |
{
|
10576
|
933 |
png_debug(1, "in png_write_flush");
|
|
934 |
|
2
|
935 |
if (png_ptr == NULL)
|
|
936 |
return;
|
10576
|
937 |
|
2
|
938 |
/* We have already written out all of the data */
|
|
939 |
if (png_ptr->row_number >= png_ptr->num_rows)
|
10576
|
940 |
return;
|
2
|
941 |
|
29913
|
942 |
png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
|
2
|
943 |
png_ptr->flush_rows = 0;
|
|
944 |
png_flush(png_ptr);
|
|
945 |
}
|
29913
|
946 |
#endif /* WRITE_FLUSH */
|
10576
|
947 |
|
29913
|
948 |
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
|
|
949 |
static void png_reset_filter_heuristics(png_structrp png_ptr);/* forward decl */
|
10576
|
950 |
#endif
|
|
951 |
|
29913
|
952 |
/* Free any memory used in png_ptr struct without freeing the struct itself. */
|
|
953 |
static void
|
|
954 |
png_write_destroy(png_structrp png_ptr)
|
2
|
955 |
{
|
10576
|
956 |
png_debug(1, "in png_write_destroy");
|
2
|
957 |
|
10576
|
958 |
/* Free any memory zlib uses */
|
29913
|
959 |
if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
|
10576
|
960 |
deflateEnd(&png_ptr->zstream);
|
|
961 |
|
|
962 |
/* Free our memory. png_free checks NULL for us. */
|
29913
|
963 |
png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
|
2
|
964 |
png_free(png_ptr, png_ptr->row_buf);
|
29913
|
965 |
png_ptr->row_buf = NULL;
|
10576
|
966 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
2
|
967 |
png_free(png_ptr, png_ptr->prev_row);
|
|
968 |
png_free(png_ptr, png_ptr->sub_row);
|
|
969 |
png_free(png_ptr, png_ptr->up_row);
|
|
970 |
png_free(png_ptr, png_ptr->avg_row);
|
|
971 |
png_free(png_ptr, png_ptr->paeth_row);
|
29913
|
972 |
png_ptr->prev_row = NULL;
|
|
973 |
png_ptr->sub_row = NULL;
|
|
974 |
png_ptr->up_row = NULL;
|
|
975 |
png_ptr->avg_row = NULL;
|
|
976 |
png_ptr->paeth_row = NULL;
|
2
|
977 |
#endif
|
|
978 |
|
10576
|
979 |
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
|
|
980 |
/* Use this to save a little code space, it doesn't free the filter_costs */
|
|
981 |
png_reset_filter_heuristics(png_ptr);
|
2
|
982 |
png_free(png_ptr, png_ptr->filter_costs);
|
|
983 |
png_free(png_ptr, png_ptr->inv_filter_costs);
|
29913
|
984 |
png_ptr->filter_costs = NULL;
|
|
985 |
png_ptr->inv_filter_costs = NULL;
|
2
|
986 |
#endif
|
|
987 |
|
29913
|
988 |
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
|
|
989 |
png_free(png_ptr, png_ptr->chunk_list);
|
|
990 |
png_ptr->chunk_list = NULL;
|
2
|
991 |
#endif
|
|
992 |
|
29913
|
993 |
/* The error handling and memory handling information is left intact at this
|
|
994 |
* point: the jmp_buf may still have to be freed. See png_destroy_png_struct
|
|
995 |
* for how this happens.
|
|
996 |
*/
|
|
997 |
}
|
2
|
998 |
|
29913
|
999 |
/* Free all memory used by the write.
|
|
1000 |
* In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
|
|
1001 |
* *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
|
|
1002 |
* the passed in info_structs but it would quietly fail to free any of the data
|
|
1003 |
* inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
|
|
1004 |
* has no png_ptr.)
|
|
1005 |
*/
|
|
1006 |
void PNGAPI
|
|
1007 |
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
|
|
1008 |
{
|
|
1009 |
png_debug(1, "in png_destroy_write_struct");
|
2
|
1010 |
|
29913
|
1011 |
if (png_ptr_ptr != NULL)
|
|
1012 |
{
|
|
1013 |
png_structrp png_ptr = *png_ptr_ptr;
|
|
1014 |
|
|
1015 |
if (png_ptr != NULL) /* added in libpng 1.6.0 */
|
|
1016 |
{
|
|
1017 |
png_destroy_info_struct(png_ptr, info_ptr_ptr);
|
|
1018 |
|
|
1019 |
*png_ptr_ptr = NULL;
|
|
1020 |
png_write_destroy(png_ptr);
|
|
1021 |
png_destroy_png_struct(png_ptr);
|
|
1022 |
}
|
|
1023 |
}
|
2
|
1024 |
}
|
|
1025 |
|
|
1026 |
/* Allow the application to select one or more row filters to use. */
|
|
1027 |
void PNGAPI
|
29913
|
1028 |
png_set_filter(png_structrp png_ptr, int method, int filters)
|
2
|
1029 |
{
|
10576
|
1030 |
png_debug(1, "in png_set_filter");
|
|
1031 |
|
2
|
1032 |
if (png_ptr == NULL)
|
|
1033 |
return;
|
10576
|
1034 |
|
|
1035 |
#ifdef PNG_MNG_FEATURES_SUPPORTED
|
29913
|
1036 |
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
|
10576
|
1037 |
(method == PNG_INTRAPIXEL_DIFFERENCING))
|
|
1038 |
method = PNG_FILTER_TYPE_BASE;
|
|
1039 |
|
2
|
1040 |
#endif
|
|
1041 |
if (method == PNG_FILTER_TYPE_BASE)
|
|
1042 |
{
|
|
1043 |
switch (filters & (PNG_ALL_FILTERS | 0x07))
|
|
1044 |
{
|
10576
|
1045 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
2
|
1046 |
case 5:
|
|
1047 |
case 6:
|
29913
|
1048 |
case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
|
|
1049 |
/* FALL THROUGH */
|
|
1050 |
#endif /* WRITE_FILTER */
|
10576
|
1051 |
case PNG_FILTER_VALUE_NONE:
|
|
1052 |
png_ptr->do_filter = PNG_FILTER_NONE; break;
|
|
1053 |
|
|
1054 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
|
1055 |
case PNG_FILTER_VALUE_SUB:
|
|
1056 |
png_ptr->do_filter = PNG_FILTER_SUB; break;
|
|
1057 |
|
|
1058 |
case PNG_FILTER_VALUE_UP:
|
|
1059 |
png_ptr->do_filter = PNG_FILTER_UP; break;
|
|
1060 |
|
|
1061 |
case PNG_FILTER_VALUE_AVG:
|
|
1062 |
png_ptr->do_filter = PNG_FILTER_AVG; break;
|
|
1063 |
|
|
1064 |
case PNG_FILTER_VALUE_PAETH:
|
|
1065 |
png_ptr->do_filter = PNG_FILTER_PAETH; break;
|
|
1066 |
|
|
1067 |
default:
|
|
1068 |
png_ptr->do_filter = (png_byte)filters; break;
|
|
1069 |
#else
|
|
1070 |
default:
|
29913
|
1071 |
png_app_error(png_ptr, "Unknown row filter for method 0");
|
|
1072 |
#endif /* WRITE_FILTER */
|
2
|
1073 |
}
|
|
1074 |
|
|
1075 |
/* If we have allocated the row_buf, this means we have already started
|
|
1076 |
* with the image and we should have allocated all of the filter buffers
|
|
1077 |
* that have been selected. If prev_row isn't already allocated, then
|
|
1078 |
* it is too late to start using the filters that need it, since we
|
|
1079 |
* will be missing the data in the previous row. If an application
|
|
1080 |
* wants to start and stop using particular filters during compression,
|
|
1081 |
* it should start out with all of the filters, and then add and
|
|
1082 |
* remove them after the start of compression.
|
|
1083 |
*/
|
|
1084 |
if (png_ptr->row_buf != NULL)
|
|
1085 |
{
|
10576
|
1086 |
#ifdef PNG_WRITE_FILTER_SUPPORTED
|
29913
|
1087 |
if ((png_ptr->do_filter & PNG_FILTER_SUB) != 0 &&
|
|
1088 |
png_ptr->sub_row == NULL)
|
2
|
1089 |
{
|
|
1090 |
png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1091 |
(png_ptr->rowbytes + 1));
|
2
|
1092 |
png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
|
|
1093 |
}
|
|
1094 |
|
29913
|
1095 |
if ((png_ptr->do_filter & PNG_FILTER_UP) != 0 &&
|
|
1096 |
png_ptr->up_row == NULL)
|
2
|
1097 |
{
|
|
1098 |
if (png_ptr->prev_row == NULL)
|
|
1099 |
{
|
|
1100 |
png_warning(png_ptr, "Can't add Up filter after starting");
|
10576
|
1101 |
png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
|
|
1102 |
~PNG_FILTER_UP);
|
2
|
1103 |
}
|
10576
|
1104 |
|
2
|
1105 |
else
|
|
1106 |
{
|
|
1107 |
png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1108 |
(png_ptr->rowbytes + 1));
|
2
|
1109 |
png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
|
|
1110 |
}
|
|
1111 |
}
|
|
1112 |
|
29913
|
1113 |
if ((png_ptr->do_filter & PNG_FILTER_AVG) != 0 &&
|
|
1114 |
png_ptr->avg_row == NULL)
|
2
|
1115 |
{
|
|
1116 |
if (png_ptr->prev_row == NULL)
|
|
1117 |
{
|
|
1118 |
png_warning(png_ptr, "Can't add Average filter after starting");
|
10576
|
1119 |
png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
|
|
1120 |
~PNG_FILTER_AVG);
|
2
|
1121 |
}
|
10576
|
1122 |
|
2
|
1123 |
else
|
|
1124 |
{
|
|
1125 |
png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1126 |
(png_ptr->rowbytes + 1));
|
2
|
1127 |
png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
|
|
1128 |
}
|
|
1129 |
}
|
|
1130 |
|
29913
|
1131 |
if ((png_ptr->do_filter & PNG_FILTER_PAETH) != 0 &&
|
2
|
1132 |
png_ptr->paeth_row == NULL)
|
|
1133 |
{
|
|
1134 |
if (png_ptr->prev_row == NULL)
|
|
1135 |
{
|
|
1136 |
png_warning(png_ptr, "Can't add Paeth filter after starting");
|
|
1137 |
png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
|
|
1138 |
}
|
10576
|
1139 |
|
2
|
1140 |
else
|
|
1141 |
{
|
|
1142 |
png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
|
10576
|
1143 |
(png_ptr->rowbytes + 1));
|
2
|
1144 |
png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
|
|
1145 |
}
|
|
1146 |
}
|
|
1147 |
|
|
1148 |
if (png_ptr->do_filter == PNG_NO_FILTERS)
|
29913
|
1149 |
#endif /* WRITE_FILTER */
|
2
|
1150 |
png_ptr->do_filter = PNG_FILTER_NONE;
|
|
1151 |
}
|
|
1152 |
}
|
|
1153 |
else
|
|
1154 |
png_error(png_ptr, "Unknown custom filter method");
|
|
1155 |
}
|
|
1156 |
|
|
1157 |
/* This allows us to influence the way in which libpng chooses the "best"
|
|
1158 |
* filter for the current scanline. While the "minimum-sum-of-absolute-
|
|
1159 |
* differences metric is relatively fast and effective, there is some
|
|
1160 |
* question as to whether it can be improved upon by trying to keep the
|
|
1161 |
* filtered data going to zlib more consistent, hopefully resulting in
|
|
1162 |
* better compression.
|
|
1163 |
*/
|
10576
|
1164 |
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
|
|
1165 |
/* Convenience reset API. */
|
|
1166 |
static void
|
29913
|
1167 |
png_reset_filter_heuristics(png_structrp png_ptr)
|
2
|
1168 |
{
|
10576
|
1169 |
/* Clear out any old values in the 'weights' - this must be done because if
|
|
1170 |
* the app calls set_filter_heuristics multiple times with different
|
|
1171 |
* 'num_weights' values we would otherwise potentially have wrong sized
|
|
1172 |
* arrays.
|
|
1173 |
*/
|
|
1174 |
png_ptr->num_prev_filters = 0;
|
|
1175 |
png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
|
|
1176 |
if (png_ptr->prev_filters != NULL)
|
2
|
1177 |
{
|
10576
|
1178 |
png_bytep old = png_ptr->prev_filters;
|
|
1179 |
png_ptr->prev_filters = NULL;
|
|
1180 |
png_free(png_ptr, old);
|
|
1181 |
}
|
|
1182 |
if (png_ptr->filter_weights != NULL)
|
|
1183 |
{
|
|
1184 |
png_uint_16p old = png_ptr->filter_weights;
|
|
1185 |
png_ptr->filter_weights = NULL;
|
|
1186 |
png_free(png_ptr, old);
|
2
|
1187 |
}
|
|
1188 |
|
10576
|
1189 |
if (png_ptr->inv_filter_weights != NULL)
|
2
|
1190 |
{
|
10576
|
1191 |
png_uint_16p old = png_ptr->inv_filter_weights;
|
|
1192 |
png_ptr->inv_filter_weights = NULL;
|
|
1193 |
png_free(png_ptr, old);
|
2
|
1194 |
}
|
|
1195 |
|
10576
|
1196 |
/* Leave the filter_costs - this array is fixed size. */
|
|
1197 |
}
|
|
1198 |
|
|
1199 |
static int
|
29913
|
1200 |
png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
|
10576
|
1201 |
int num_weights)
|
|
1202 |
{
|
|
1203 |
if (png_ptr == NULL)
|
|
1204 |
return 0;
|
2
|
1205 |
|
10576
|
1206 |
/* Clear out the arrays */
|
|
1207 |
png_reset_filter_heuristics(png_ptr);
|
2
|
1208 |
|
10576
|
1209 |
/* Check arguments; the 'reset' function makes the correct settings for the
|
|
1210 |
* unweighted case, but we must handle the weight case by initializing the
|
|
1211 |
* arrays for the caller.
|
|
1212 |
*/
|
|
1213 |
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
2
|
1214 |
{
|
10576
|
1215 |
int i;
|
|
1216 |
|
|
1217 |
if (num_weights > 0)
|
2
|
1218 |
{
|
|
1219 |
png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
|
29913
|
1220 |
(png_uint_32)((sizeof (png_byte)) * num_weights));
|
2
|
1221 |
|
|
1222 |
/* To make sure that the weighting starts out fairly */
|
|
1223 |
for (i = 0; i < num_weights; i++)
|
|
1224 |
{
|
|
1225 |
png_ptr->prev_filters[i] = 255;
|
|
1226 |
}
|
|
1227 |
|
|
1228 |
png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
29913
|
1229 |
(png_uint_32)((sizeof (png_uint_16)) * num_weights));
|
2
|
1230 |
|
|
1231 |
png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
29913
|
1232 |
(png_uint_32)((sizeof (png_uint_16)) * num_weights));
|
10576
|
1233 |
|
2
|
1234 |
for (i = 0; i < num_weights; i++)
|
|
1235 |
{
|
|
1236 |
png_ptr->inv_filter_weights[i] =
|
|
1237 |
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
|
1238 |
}
|
10576
|
1239 |
|
|
1240 |
/* Safe to set this now */
|
|
1241 |
png_ptr->num_prev_filters = (png_byte)num_weights;
|
2
|
1242 |
}
|
|
1243 |
|
10576
|
1244 |
/* If, in the future, there are other filter methods, this would
|
|
1245 |
* need to be based on png_ptr->filter.
|
|
1246 |
*/
|
|
1247 |
if (png_ptr->filter_costs == NULL)
|
2
|
1248 |
{
|
10576
|
1249 |
png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
29913
|
1250 |
(png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
|
10576
|
1251 |
|
|
1252 |
png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
29913
|
1253 |
(png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
|
2
|
1254 |
}
|
|
1255 |
|
|
1256 |
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
|
|
1257 |
{
|
|
1258 |
png_ptr->inv_filter_costs[i] =
|
|
1259 |
png_ptr->filter_costs[i] = PNG_COST_FACTOR;
|
|
1260 |
}
|
10576
|
1261 |
|
|
1262 |
/* All the arrays are inited, safe to set this: */
|
|
1263 |
png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
|
|
1264 |
|
|
1265 |
/* Return the 'ok' code. */
|
|
1266 |
return 1;
|
2
|
1267 |
}
|
10576
|
1268 |
else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
|
|
1269 |
heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
|
|
1270 |
{
|
|
1271 |
return 1;
|
|
1272 |
}
|
|
1273 |
else
|
|
1274 |
{
|
|
1275 |
png_warning(png_ptr, "Unknown filter heuristic method");
|
|
1276 |
return 0;
|
|
1277 |
}
|
|
1278 |
}
|
|
1279 |
|
|
1280 |
/* Provide floating and fixed point APIs */
|
|
1281 |
#ifdef PNG_FLOATING_POINT_SUPPORTED
|
|
1282 |
void PNGAPI
|
29913
|
1283 |
png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
|
10576
|
1284 |
int num_weights, png_const_doublep filter_weights,
|
|
1285 |
png_const_doublep filter_costs)
|
|
1286 |
{
|
|
1287 |
png_debug(1, "in png_set_filter_heuristics");
|
2
|
1288 |
|
10576
|
1289 |
/* The internal API allocates all the arrays and ensures that the elements of
|
|
1290 |
* those arrays are set to the default value.
|
2
|
1291 |
*/
|
29913
|
1292 |
if (png_init_filter_heuristics(png_ptr, heuristic_method, num_weights) == 0)
|
10576
|
1293 |
return;
|
|
1294 |
|
|
1295 |
/* If using the weighted method copy in the weights. */
|
|
1296 |
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
2
|
1297 |
{
|
10576
|
1298 |
int i;
|
|
1299 |
for (i = 0; i < num_weights; i++)
|
|
1300 |
{
|
|
1301 |
if (filter_weights[i] <= 0.0)
|
|
1302 |
{
|
|
1303 |
png_ptr->inv_filter_weights[i] =
|
|
1304 |
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
|
1305 |
}
|
|
1306 |
|
|
1307 |
else
|
|
1308 |
{
|
|
1309 |
png_ptr->inv_filter_weights[i] =
|
|
1310 |
(png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
|
|
1311 |
|
|
1312 |
png_ptr->filter_weights[i] =
|
|
1313 |
(png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
|
|
1314 |
}
|
|
1315 |
}
|
|
1316 |
|
|
1317 |
/* Here is where we set the relative costs of the different filters. We
|
|
1318 |
* should take the desired compression level into account when setting
|
|
1319 |
* the costs, so that Paeth, for instance, has a high relative cost at low
|
|
1320 |
* compression levels, while it has a lower relative cost at higher
|
|
1321 |
* compression settings. The filter types are in order of increasing
|
|
1322 |
* relative cost, so it would be possible to do this with an algorithm.
|
|
1323 |
*/
|
|
1324 |
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
|
2
|
1325 |
{
|
|
1326 |
png_ptr->inv_filter_costs[i] =
|
10576
|
1327 |
(png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
|
|
1328 |
|
2
|
1329 |
png_ptr->filter_costs[i] =
|
10576
|
1330 |
(png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
|
2
|
1331 |
}
|
|
1332 |
}
|
|
1333 |
}
|
10576
|
1334 |
#endif /* FLOATING_POINT */
|
|
1335 |
|
|
1336 |
#ifdef PNG_FIXED_POINT_SUPPORTED
|
|
1337 |
void PNGAPI
|
29913
|
1338 |
png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
|
10576
|
1339 |
int num_weights, png_const_fixed_point_p filter_weights,
|
|
1340 |
png_const_fixed_point_p filter_costs)
|
|
1341 |
{
|
|
1342 |
png_debug(1, "in png_set_filter_heuristics_fixed");
|
|
1343 |
|
|
1344 |
/* The internal API allocates all the arrays and ensures that the elements of
|
|
1345 |
* those arrays are set to the default value.
|
|
1346 |
*/
|
29913
|
1347 |
if (png_init_filter_heuristics(png_ptr, heuristic_method, num_weights) == 0)
|
10576
|
1348 |
return;
|
|
1349 |
|
|
1350 |
/* If using the weighted method copy in the weights. */
|
|
1351 |
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
|
1352 |
{
|
|
1353 |
int i;
|
|
1354 |
for (i = 0; i < num_weights; i++)
|
|
1355 |
{
|
|
1356 |
if (filter_weights[i] <= 0)
|
|
1357 |
{
|
|
1358 |
png_ptr->inv_filter_weights[i] =
|
|
1359 |
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
|
1360 |
}
|
|
1361 |
|
|
1362 |
else
|
|
1363 |
{
|
|
1364 |
png_ptr->inv_filter_weights[i] = (png_uint_16)
|
|
1365 |
((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
|
|
1366 |
|
|
1367 |
png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
|
|
1368 |
PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
|
|
1369 |
}
|
|
1370 |
}
|
|
1371 |
|
|
1372 |
/* Here is where we set the relative costs of the different filters. We
|
|
1373 |
* should take the desired compression level into account when setting
|
|
1374 |
* the costs, so that Paeth, for instance, has a high relative cost at low
|
|
1375 |
* compression levels, while it has a lower relative cost at higher
|
|
1376 |
* compression settings. The filter types are in order of increasing
|
|
1377 |
* relative cost, so it would be possible to do this with an algorithm.
|
|
1378 |
*/
|
|
1379 |
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
|
|
1380 |
if (filter_costs[i] >= PNG_FP_1)
|
|
1381 |
{
|
|
1382 |
png_uint_32 tmp;
|
|
1383 |
|
|
1384 |
/* Use a 32 bit unsigned temporary here because otherwise the
|
|
1385 |
* intermediate value will be a 32 bit *signed* integer (ANSI rules)
|
|
1386 |
* and this will get the wrong answer on division.
|
|
1387 |
*/
|
|
1388 |
tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
|
|
1389 |
tmp /= filter_costs[i];
|
|
1390 |
|
|
1391 |
png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
|
|
1392 |
|
|
1393 |
tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
|
|
1394 |
tmp /= PNG_FP_1;
|
|
1395 |
|
|
1396 |
png_ptr->filter_costs[i] = (png_uint_16)tmp;
|
|
1397 |
}
|
|
1398 |
}
|
|
1399 |
}
|
|
1400 |
#endif /* FIXED_POINT */
|
29913
|
1401 |
#endif /* WRITE_WEIGHTED_FILTER */
|
2
|
1402 |
|
|
1403 |
void PNGAPI
|
29913
|
1404 |
png_set_compression_level(png_structrp png_ptr, int level)
|
2
|
1405 |
{
|
10576
|
1406 |
png_debug(1, "in png_set_compression_level");
|
|
1407 |
|
2
|
1408 |
if (png_ptr == NULL)
|
|
1409 |
return;
|
10576
|
1410 |
|
2
|
1411 |
png_ptr->zlib_level = level;
|
|
1412 |
}
|
|
1413 |
|
|
1414 |
void PNGAPI
|
29913
|
1415 |
png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
|
2
|
1416 |
{
|
10576
|
1417 |
png_debug(1, "in png_set_compression_mem_level");
|
|
1418 |
|
2
|
1419 |
if (png_ptr == NULL)
|
|
1420 |
return;
|
10576
|
1421 |
|
2
|
1422 |
png_ptr->zlib_mem_level = mem_level;
|
|
1423 |
}
|
|
1424 |
|
|
1425 |
void PNGAPI
|
29913
|
1426 |
png_set_compression_strategy(png_structrp png_ptr, int strategy)
|
2
|
1427 |
{
|
10576
|
1428 |
png_debug(1, "in png_set_compression_strategy");
|
|
1429 |
|
2
|
1430 |
if (png_ptr == NULL)
|
|
1431 |
return;
|
10576
|
1432 |
|
29913
|
1433 |
/* The flag setting here prevents the libpng dynamic selection of strategy.
|
|
1434 |
*/
|
2
|
1435 |
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
|
|
1436 |
png_ptr->zlib_strategy = strategy;
|
|
1437 |
}
|
|
1438 |
|
10576
|
1439 |
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
|
|
1440 |
* smaller value of window_bits if it can do so safely.
|
|
1441 |
*/
|
2
|
1442 |
void PNGAPI
|
29913
|
1443 |
png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
|
2
|
1444 |
{
|
|
1445 |
if (png_ptr == NULL)
|
|
1446 |
return;
|
10576
|
1447 |
|
29913
|
1448 |
/* Prior to 1.6.0 this would warn but then set the window_bits value, this
|
|
1449 |
* meant that negative window bits values could be selected which would cause
|
|
1450 |
* libpng to write a non-standard PNG file with raw deflate or gzip
|
|
1451 |
* compressed IDAT or ancillary chunks. Such files can be read and there is
|
|
1452 |
* no warning on read, so this seems like a very bad idea.
|
|
1453 |
*/
|
2
|
1454 |
if (window_bits > 15)
|
29913
|
1455 |
{
|
2
|
1456 |
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
|
29913
|
1457 |
window_bits = 15;
|
|
1458 |
}
|
10576
|
1459 |
|
2
|
1460 |
else if (window_bits < 8)
|
29913
|
1461 |
{
|
2
|
1462 |
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
|
29913
|
1463 |
window_bits = 8;
|
|
1464 |
}
|
10576
|
1465 |
|
2
|
1466 |
png_ptr->zlib_window_bits = window_bits;
|
|
1467 |
}
|
|
1468 |
|
|
1469 |
void PNGAPI
|
29913
|
1470 |
png_set_compression_method(png_structrp png_ptr, int method)
|
2
|
1471 |
{
|
10576
|
1472 |
png_debug(1, "in png_set_compression_method");
|
|
1473 |
|
2
|
1474 |
if (png_ptr == NULL)
|
|
1475 |
return;
|
10576
|
1476 |
|
29913
|
1477 |
/* This would produce an invalid PNG file if it worked, but it doesn't and
|
|
1478 |
* deflate will fault it, so it is harmless to just warn here.
|
|
1479 |
*/
|
2
|
1480 |
if (method != 8)
|
|
1481 |
png_warning(png_ptr, "Only compression method 8 is supported by PNG");
|
10576
|
1482 |
|
2
|
1483 |
png_ptr->zlib_method = method;
|
|
1484 |
}
|
|
1485 |
|
10576
|
1486 |
/* The following were added to libpng-1.5.4 */
|
|
1487 |
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
|
|
1488 |
void PNGAPI
|
29913
|
1489 |
png_set_text_compression_level(png_structrp png_ptr, int level)
|
10576
|
1490 |
{
|
|
1491 |
png_debug(1, "in png_set_text_compression_level");
|
|
1492 |
|
|
1493 |
if (png_ptr == NULL)
|
|
1494 |
return;
|
|
1495 |
|
|
1496 |
png_ptr->zlib_text_level = level;
|
|
1497 |
}
|
|
1498 |
|
|
1499 |
void PNGAPI
|
29913
|
1500 |
png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
|
10576
|
1501 |
{
|
|
1502 |
png_debug(1, "in png_set_text_compression_mem_level");
|
|
1503 |
|
|
1504 |
if (png_ptr == NULL)
|
|
1505 |
return;
|
|
1506 |
|
|
1507 |
png_ptr->zlib_text_mem_level = mem_level;
|
|
1508 |
}
|
|
1509 |
|
|
1510 |
void PNGAPI
|
29913
|
1511 |
png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
|
10576
|
1512 |
{
|
|
1513 |
png_debug(1, "in png_set_text_compression_strategy");
|
|
1514 |
|
|
1515 |
if (png_ptr == NULL)
|
|
1516 |
return;
|
|
1517 |
|
|
1518 |
png_ptr->zlib_text_strategy = strategy;
|
|
1519 |
}
|
|
1520 |
|
|
1521 |
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
|
|
1522 |
* smaller value of window_bits if it can do so safely.
|
|
1523 |
*/
|
|
1524 |
void PNGAPI
|
29913
|
1525 |
png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
|
10576
|
1526 |
{
|
|
1527 |
if (png_ptr == NULL)
|
|
1528 |
return;
|
|
1529 |
|
|
1530 |
if (window_bits > 15)
|
29913
|
1531 |
{
|
10576
|
1532 |
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
|
29913
|
1533 |
window_bits = 15;
|
|
1534 |
}
|
10576
|
1535 |
|
|
1536 |
else if (window_bits < 8)
|
29913
|
1537 |
{
|
10576
|
1538 |
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
|
29913
|
1539 |
window_bits = 8;
|
|
1540 |
}
|
10576
|
1541 |
|
|
1542 |
png_ptr->zlib_text_window_bits = window_bits;
|
|
1543 |
}
|
|
1544 |
|
|
1545 |
void PNGAPI
|
29913
|
1546 |
png_set_text_compression_method(png_structrp png_ptr, int method)
|
10576
|
1547 |
{
|
|
1548 |
png_debug(1, "in png_set_text_compression_method");
|
|
1549 |
|
|
1550 |
if (png_ptr == NULL)
|
|
1551 |
return;
|
|
1552 |
|
|
1553 |
if (method != 8)
|
|
1554 |
png_warning(png_ptr, "Only compression method 8 is supported by PNG");
|
|
1555 |
|
|
1556 |
png_ptr->zlib_text_method = method;
|
|
1557 |
}
|
29913
|
1558 |
#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
|
10576
|
1559 |
/* end of API added to libpng-1.5.4 */
|
|
1560 |
|
2
|
1561 |
void PNGAPI
|
29913
|
1562 |
png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
|
2
|
1563 |
{
|
|
1564 |
if (png_ptr == NULL)
|
|
1565 |
return;
|
10576
|
1566 |
|
2
|
1567 |
png_ptr->write_row_fn = write_row_fn;
|
|
1568 |
}
|
|
1569 |
|
10576
|
1570 |
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
|
2
|
1571 |
void PNGAPI
|
29913
|
1572 |
png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
|
10576
|
1573 |
write_user_transform_fn)
|
2
|
1574 |
{
|
10576
|
1575 |
png_debug(1, "in png_set_write_user_transform_fn");
|
|
1576 |
|
2
|
1577 |
if (png_ptr == NULL)
|
|
1578 |
return;
|
10576
|
1579 |
|
2
|
1580 |
png_ptr->transformations |= PNG_USER_TRANSFORM;
|
|
1581 |
png_ptr->write_user_transform_fn = write_user_transform_fn;
|
|
1582 |
}
|
|
1583 |
#endif
|
|
1584 |
|
|
1585 |
|
10576
|
1586 |
#ifdef PNG_INFO_IMAGE_SUPPORTED
|
2
|
1587 |
void PNGAPI
|
29913
|
1588 |
png_write_png(png_structrp png_ptr, png_inforp info_ptr,
|
10576
|
1589 |
int transforms, voidp params)
|
2
|
1590 |
{
|
|
1591 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
1592 |
return;
|
|
1593 |
|
29913
|
1594 |
if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
|
|
1595 |
{
|
|
1596 |
png_app_error(png_ptr, "no rows for png_write_image to write");
|
|
1597 |
return;
|
|
1598 |
}
|
|
1599 |
|
2
|
1600 |
/* Write the file header information. */
|
|
1601 |
png_write_info(png_ptr, info_ptr);
|
|
1602 |
|
|
1603 |
/* ------ these transformations don't touch the info structure ------- */
|
|
1604 |
|
29913
|
1605 |
/* Invert monochrome pixels */
|
|
1606 |
if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
|
10576
|
1607 |
#ifdef PNG_WRITE_INVERT_SUPPORTED
|
|
1608 |
png_set_invert_mono(png_ptr);
|
29913
|
1609 |
#else
|
|
1610 |
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
|
2
|
1611 |
#endif
|
|
1612 |
|
|
1613 |
/* Shift the pixels up to a legal bit depth and fill in
|
|
1614 |
* as appropriate to correctly scale the image.
|
|
1615 |
*/
|
29913
|
1616 |
if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
|
|
1617 |
#ifdef PNG_WRITE_SHIFT_SUPPORTED
|
|
1618 |
if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
|
|
1619 |
png_set_shift(png_ptr, &info_ptr->sig_bit);
|
|
1620 |
#else
|
|
1621 |
png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
|
2
|
1622 |
#endif
|
|
1623 |
|
29913
|
1624 |
/* Pack pixels into bytes */
|
|
1625 |
if ((transforms & PNG_TRANSFORM_PACKING) != 0)
|
10576
|
1626 |
#ifdef PNG_WRITE_PACK_SUPPORTED
|
29913
|
1627 |
png_set_packing(png_ptr);
|
|
1628 |
#else
|
|
1629 |
png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
|
2
|
1630 |
#endif
|
|
1631 |
|
29913
|
1632 |
/* Swap location of alpha bytes from ARGB to RGBA */
|
|
1633 |
if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
|
10576
|
1634 |
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
|
|
1635 |
png_set_swap_alpha(png_ptr);
|
29913
|
1636 |
#else
|
|
1637 |
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
|
2
|
1638 |
#endif
|
|
1639 |
|
29913
|
1640 |
/* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
|
|
1641 |
* RGB, note that the code expects the input color type to be G or RGB; no
|
|
1642 |
* alpha channel.
|
|
1643 |
*/
|
|
1644 |
if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
|
|
1645 |
PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
|
|
1646 |
{
|
10576
|
1647 |
#ifdef PNG_WRITE_FILLER_SUPPORTED
|
29913
|
1648 |
if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
|
|
1649 |
{
|
|
1650 |
if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
|
|
1651 |
png_app_error(png_ptr,
|
|
1652 |
"PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
|
10576
|
1653 |
|
29913
|
1654 |
/* Continue if ignored - this is the pre-1.6.10 behavior */
|
|
1655 |
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
|
|
1656 |
}
|
2
|
1657 |
|
29913
|
1658 |
else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
|
|
1659 |
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
|
|
1660 |
#else
|
|
1661 |
png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
|
|
1662 |
#endif
|
|
1663 |
}
|
|
1664 |
|
10576
|
1665 |
/* Flip BGR pixels to RGB */
|
29913
|
1666 |
if ((transforms & PNG_TRANSFORM_BGR) != 0)
|
|
1667 |
#ifdef PNG_WRITE_BGR_SUPPORTED
|
10576
|
1668 |
png_set_bgr(png_ptr);
|
29913
|
1669 |
#else
|
|
1670 |
png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
|
2
|
1671 |
#endif
|
|
1672 |
|
29913
|
1673 |
/* Swap bytes of 16-bit files to most significant byte first */
|
|
1674 |
if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
|
10576
|
1675 |
#ifdef PNG_WRITE_SWAP_SUPPORTED
|
|
1676 |
png_set_swap(png_ptr);
|
29913
|
1677 |
#else
|
|
1678 |
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
|
2
|
1679 |
#endif
|
|
1680 |
|
29913
|
1681 |
/* Swap bits of 1, 2, 4 bit packed pixel formats */
|
|
1682 |
if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
|
10576
|
1683 |
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
|
|
1684 |
png_set_packswap(png_ptr);
|
29913
|
1685 |
#else
|
|
1686 |
png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
|
10576
|
1687 |
#endif
|
|
1688 |
|
29913
|
1689 |
/* Invert the alpha channel from opacity to transparency */
|
|
1690 |
if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
|
10576
|
1691 |
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
|
|
1692 |
png_set_invert_alpha(png_ptr);
|
29913
|
1693 |
#else
|
|
1694 |
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
|
2
|
1695 |
#endif
|
|
1696 |
|
|
1697 |
/* ----------------------- end of transformations ------------------- */
|
|
1698 |
|
10576
|
1699 |
/* Write the bits */
|
29913
|
1700 |
png_write_image(png_ptr, info_ptr->row_pointers);
|
2
|
1701 |
|
|
1702 |
/* It is REQUIRED to call this to finish writing the rest of the file */
|
|
1703 |
png_write_end(png_ptr, info_ptr);
|
|
1704 |
|
10576
|
1705 |
PNG_UNUSED(params)
|
2
|
1706 |
}
|
|
1707 |
#endif
|
29913
|
1708 |
|
|
1709 |
|
|
1710 |
#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
|
|
1711 |
#ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
|
|
1712 |
/* Initialize the write structure - general purpose utility. */
|
|
1713 |
static int
|
|
1714 |
png_image_write_init(png_imagep image)
|
|
1715 |
{
|
|
1716 |
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
|
|
1717 |
png_safe_error, png_safe_warning);
|
|
1718 |
|
|
1719 |
if (png_ptr != NULL)
|
|
1720 |
{
|
|
1721 |
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
1722 |
|
|
1723 |
if (info_ptr != NULL)
|
|
1724 |
{
|
|
1725 |
png_controlp control = png_voidcast(png_controlp,
|
|
1726 |
png_malloc_warn(png_ptr, (sizeof *control)));
|
|
1727 |
|
|
1728 |
if (control != NULL)
|
|
1729 |
{
|
|
1730 |
memset(control, 0, (sizeof *control));
|
|
1731 |
|
|
1732 |
control->png_ptr = png_ptr;
|
|
1733 |
control->info_ptr = info_ptr;
|
|
1734 |
control->for_write = 1;
|
|
1735 |
|
|
1736 |
image->opaque = control;
|
|
1737 |
return 1;
|
|
1738 |
}
|
|
1739 |
|
|
1740 |
/* Error clean up */
|
|
1741 |
png_destroy_info_struct(png_ptr, &info_ptr);
|
|
1742 |
}
|
|
1743 |
|
|
1744 |
png_destroy_write_struct(&png_ptr, NULL);
|
|
1745 |
}
|
|
1746 |
|
|
1747 |
return png_image_error(image, "png_image_write_: out of memory");
|
|
1748 |
}
|
|
1749 |
|
|
1750 |
/* Arguments to png_image_write_main: */
|
|
1751 |
typedef struct
|
|
1752 |
{
|
|
1753 |
/* Arguments: */
|
|
1754 |
png_imagep image;
|
|
1755 |
png_const_voidp buffer;
|
|
1756 |
png_int_32 row_stride;
|
|
1757 |
png_const_voidp colormap;
|
|
1758 |
int convert_to_8bit;
|
|
1759 |
/* Local variables: */
|
|
1760 |
png_const_voidp first_row;
|
|
1761 |
ptrdiff_t row_bytes;
|
|
1762 |
png_voidp local_row;
|
|
1763 |
} png_image_write_control;
|
|
1764 |
|
|
1765 |
/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
|
|
1766 |
* do any necessary byte swapping. The component order is defined by the
|
|
1767 |
* png_image format value.
|
|
1768 |
*/
|
|
1769 |
static int
|
|
1770 |
png_write_image_16bit(png_voidp argument)
|
|
1771 |
{
|
|
1772 |
png_image_write_control *display = png_voidcast(png_image_write_control*,
|
|
1773 |
argument);
|
|
1774 |
png_imagep image = display->image;
|
|
1775 |
png_structrp png_ptr = image->opaque->png_ptr;
|
|
1776 |
|
|
1777 |
png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
|
|
1778 |
display->first_row);
|
|
1779 |
png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
|
|
1780 |
png_uint_16p row_end;
|
|
1781 |
const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
|
|
1782 |
int aindex = 0;
|
|
1783 |
png_uint_32 y = image->height;
|
|
1784 |
|
|
1785 |
if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
|
|
1786 |
{
|
|
1787 |
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
|
|
1788 |
if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
|
|
1789 |
{
|
|
1790 |
aindex = -1;
|
|
1791 |
++input_row; /* To point to the first component */
|
|
1792 |
++output_row;
|
|
1793 |
}
|
|
1794 |
|
|
1795 |
else
|
|
1796 |
# endif
|
|
1797 |
aindex = channels;
|
|
1798 |
}
|
|
1799 |
|
|
1800 |
else
|
|
1801 |
png_error(png_ptr, "png_write_image: internal call error");
|
|
1802 |
|
|
1803 |
/* Work out the output row end and count over this, note that the increment
|
|
1804 |
* above to 'row' means that row_end can actually be beyond the end of the
|
|
1805 |
* row; this is correct.
|
|
1806 |
*/
|
|
1807 |
row_end = output_row + image->width * (channels+1);
|
|
1808 |
|
|
1809 |
while (y-- > 0)
|
|
1810 |
{
|
|
1811 |
png_const_uint_16p in_ptr = input_row;
|
|
1812 |
png_uint_16p out_ptr = output_row;
|
|
1813 |
|
|
1814 |
while (out_ptr < row_end)
|
|
1815 |
{
|
|
1816 |
const png_uint_16 alpha = in_ptr[aindex];
|
|
1817 |
png_uint_32 reciprocal = 0;
|
|
1818 |
int c;
|
|
1819 |
|
|
1820 |
out_ptr[aindex] = alpha;
|
|
1821 |
|
|
1822 |
/* Calculate a reciprocal. The correct calculation is simply
|
|
1823 |
* component/alpha*65535 << 15. (I.e. 15 bits of precision); this
|
|
1824 |
* allows correct rounding by adding .5 before the shift. 'reciprocal'
|
|
1825 |
* is only initialized when required.
|
|
1826 |
*/
|
|
1827 |
if (alpha > 0 && alpha < 65535)
|
|
1828 |
reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
|
|
1829 |
|
|
1830 |
c = channels;
|
|
1831 |
do /* always at least one channel */
|
|
1832 |
{
|
|
1833 |
png_uint_16 component = *in_ptr++;
|
|
1834 |
|
|
1835 |
/* The following gives 65535 for an alpha of 0, which is fine,
|
|
1836 |
* otherwise if 0/0 is represented as some other value there is more
|
|
1837 |
* likely to be a discontinuity which will probably damage
|
|
1838 |
* compression when moving from a fully transparent area to a
|
|
1839 |
* nearly transparent one. (The assumption here is that opaque
|
|
1840 |
* areas tend not to be 0 intensity.)
|
|
1841 |
*/
|
|
1842 |
if (component >= alpha)
|
|
1843 |
component = 65535;
|
|
1844 |
|
|
1845 |
/* component<alpha, so component/alpha is less than one and
|
|
1846 |
* component*reciprocal is less than 2^31.
|
|
1847 |
*/
|
|
1848 |
else if (component > 0 && alpha < 65535)
|
|
1849 |
{
|
|
1850 |
png_uint_32 calc = component * reciprocal;
|
|
1851 |
calc += 16384; /* round to nearest */
|
|
1852 |
component = (png_uint_16)(calc >> 15);
|
|
1853 |
}
|
|
1854 |
|
|
1855 |
*out_ptr++ = component;
|
|
1856 |
}
|
|
1857 |
while (--c > 0);
|
|
1858 |
|
|
1859 |
/* Skip to next component (skip the intervening alpha channel) */
|
|
1860 |
++in_ptr;
|
|
1861 |
++out_ptr;
|
|
1862 |
}
|
|
1863 |
|
|
1864 |
png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
|
|
1865 |
input_row += display->row_bytes/(sizeof (png_uint_16));
|
|
1866 |
}
|
|
1867 |
|
|
1868 |
return 1;
|
|
1869 |
}
|
|
1870 |
|
|
1871 |
/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
|
|
1872 |
* is present it must be removed from the components, the components are then
|
|
1873 |
* written in sRGB encoding. No components are added or removed.
|
|
1874 |
*
|
|
1875 |
* Calculate an alpha reciprocal to reverse pre-multiplication. As above the
|
|
1876 |
* calculation can be done to 15 bits of accuracy; however, the output needs to
|
|
1877 |
* be scaled in the range 0..255*65535, so include that scaling here.
|
|
1878 |
*/
|
|
1879 |
#define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
|
|
1880 |
|
|
1881 |
static png_byte
|
|
1882 |
png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
|
|
1883 |
png_uint_32 reciprocal/*from the above macro*/)
|
|
1884 |
{
|
|
1885 |
/* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
|
|
1886 |
* is represented as some other value there is more likely to be a
|
|
1887 |
* discontinuity which will probably damage compression when moving from a
|
|
1888 |
* fully transparent area to a nearly transparent one. (The assumption here
|
|
1889 |
* is that opaque areas tend not to be 0 intensity.)
|
|
1890 |
*
|
|
1891 |
* There is a rounding problem here; if alpha is less than 128 it will end up
|
|
1892 |
* as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
|
|
1893 |
* output change for this too.
|
|
1894 |
*/
|
|
1895 |
if (component >= alpha || alpha < 128)
|
|
1896 |
return 255;
|
|
1897 |
|
|
1898 |
/* component<alpha, so component/alpha is less than one and
|
|
1899 |
* component*reciprocal is less than 2^31.
|
|
1900 |
*/
|
|
1901 |
else if (component > 0)
|
|
1902 |
{
|
|
1903 |
/* The test is that alpha/257 (rounded) is less than 255, the first value
|
|
1904 |
* that becomes 255 is 65407.
|
|
1905 |
* NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
|
|
1906 |
* be exact!) [Could also test reciprocal != 0]
|
|
1907 |
*/
|
|
1908 |
if (alpha < 65407)
|
|
1909 |
{
|
|
1910 |
component *= reciprocal;
|
|
1911 |
component += 64; /* round to nearest */
|
|
1912 |
component >>= 7;
|
|
1913 |
}
|
|
1914 |
|
|
1915 |
else
|
|
1916 |
component *= 255;
|
|
1917 |
|
|
1918 |
/* Convert the component to sRGB. */
|
|
1919 |
return (png_byte)PNG_sRGB_FROM_LINEAR(component);
|
|
1920 |
}
|
|
1921 |
|
|
1922 |
else
|
|
1923 |
return 0;
|
|
1924 |
}
|
|
1925 |
|
|
1926 |
static int
|
|
1927 |
png_write_image_8bit(png_voidp argument)
|
|
1928 |
{
|
|
1929 |
png_image_write_control *display = png_voidcast(png_image_write_control*,
|
|
1930 |
argument);
|
|
1931 |
png_imagep image = display->image;
|
|
1932 |
png_structrp png_ptr = image->opaque->png_ptr;
|
|
1933 |
|
|
1934 |
png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
|
|
1935 |
display->first_row);
|
|
1936 |
png_bytep output_row = png_voidcast(png_bytep, display->local_row);
|
|
1937 |
png_uint_32 y = image->height;
|
|
1938 |
const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
|
|
1939 |
|
|
1940 |
if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
|
|
1941 |
{
|
|
1942 |
png_bytep row_end;
|
|
1943 |
int aindex;
|
|
1944 |
|
|
1945 |
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
|
|
1946 |
if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
|
|
1947 |
{
|
|
1948 |
aindex = -1;
|
|
1949 |
++input_row; /* To point to the first component */
|
|
1950 |
++output_row;
|
|
1951 |
}
|
|
1952 |
|
|
1953 |
else
|
|
1954 |
# endif
|
|
1955 |
aindex = channels;
|
|
1956 |
|
|
1957 |
/* Use row_end in place of a loop counter: */
|
|
1958 |
row_end = output_row + image->width * (channels+1);
|
|
1959 |
|
|
1960 |
while (y-- > 0)
|
|
1961 |
{
|
|
1962 |
png_const_uint_16p in_ptr = input_row;
|
|
1963 |
png_bytep out_ptr = output_row;
|
|
1964 |
|
|
1965 |
while (out_ptr < row_end)
|
|
1966 |
{
|
|
1967 |
png_uint_16 alpha = in_ptr[aindex];
|
|
1968 |
png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
|
|
1969 |
png_uint_32 reciprocal = 0;
|
|
1970 |
int c;
|
|
1971 |
|
|
1972 |
/* Scale and write the alpha channel. */
|
|
1973 |
out_ptr[aindex] = alphabyte;
|
|
1974 |
|
|
1975 |
if (alphabyte > 0 && alphabyte < 255)
|
|
1976 |
reciprocal = UNP_RECIPROCAL(alpha);
|
|
1977 |
|
|
1978 |
c = channels;
|
|
1979 |
do /* always at least one channel */
|
|
1980 |
*out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
|
|
1981 |
while (--c > 0);
|
|
1982 |
|
|
1983 |
/* Skip to next component (skip the intervening alpha channel) */
|
|
1984 |
++in_ptr;
|
|
1985 |
++out_ptr;
|
|
1986 |
} /* while out_ptr < row_end */
|
|
1987 |
|
|
1988 |
png_write_row(png_ptr, png_voidcast(png_const_bytep,
|
|
1989 |
display->local_row));
|
|
1990 |
input_row += display->row_bytes/(sizeof (png_uint_16));
|
|
1991 |
} /* while y */
|
|
1992 |
}
|
|
1993 |
|
|
1994 |
else
|
|
1995 |
{
|
|
1996 |
/* No alpha channel, so the row_end really is the end of the row and it
|
|
1997 |
* is sufficient to loop over the components one by one.
|
|
1998 |
*/
|
|
1999 |
png_bytep row_end = output_row + image->width * channels;
|
|
2000 |
|
|
2001 |
while (y-- > 0)
|
|
2002 |
{
|
|
2003 |
png_const_uint_16p in_ptr = input_row;
|
|
2004 |
png_bytep out_ptr = output_row;
|
|
2005 |
|
|
2006 |
while (out_ptr < row_end)
|
|
2007 |
{
|
|
2008 |
png_uint_32 component = *in_ptr++;
|
|
2009 |
|
|
2010 |
component *= 255;
|
|
2011 |
*out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
|
|
2012 |
}
|
|
2013 |
|
|
2014 |
png_write_row(png_ptr, output_row);
|
|
2015 |
input_row += display->row_bytes/(sizeof (png_uint_16));
|
|
2016 |
}
|
|
2017 |
}
|
|
2018 |
|
|
2019 |
return 1;
|
|
2020 |
}
|
|
2021 |
|
|
2022 |
static void
|
|
2023 |
png_image_set_PLTE(png_image_write_control *display)
|
|
2024 |
{
|
|
2025 |
const png_imagep image = display->image;
|
|
2026 |
const void *cmap = display->colormap;
|
|
2027 |
const int entries = image->colormap_entries > 256 ? 256 :
|
|
2028 |
(int)image->colormap_entries;
|
|
2029 |
|
|
2030 |
/* NOTE: the caller must check for cmap != NULL and entries != 0 */
|
|
2031 |
const png_uint_32 format = image->format;
|
|
2032 |
const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
|
|
2033 |
|
|
2034 |
# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
|
|
2035 |
defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
|
|
2036 |
const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
|
|
2037 |
(format & PNG_FORMAT_FLAG_ALPHA) != 0;
|
|
2038 |
# else
|
|
2039 |
# define afirst 0
|
|
2040 |
# endif
|
|
2041 |
|
|
2042 |
# ifdef PNG_FORMAT_BGR_SUPPORTED
|
|
2043 |
const int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
|
|
2044 |
# else
|
|
2045 |
# define bgr 0
|
|
2046 |
# endif
|
|
2047 |
|
|
2048 |
int i, num_trans;
|
|
2049 |
png_color palette[256];
|
|
2050 |
png_byte tRNS[256];
|
|
2051 |
|
|
2052 |
memset(tRNS, 255, (sizeof tRNS));
|
|
2053 |
memset(palette, 0, (sizeof palette));
|
|
2054 |
|
|
2055 |
for (i=num_trans=0; i<entries; ++i)
|
|
2056 |
{
|
|
2057 |
/* This gets automatically converted to sRGB with reversal of the
|
|
2058 |
* pre-multiplication if the color-map has an alpha channel.
|
|
2059 |
*/
|
|
2060 |
if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
|
|
2061 |
{
|
|
2062 |
png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
|
|
2063 |
|
|
2064 |
entry += i * channels;
|
|
2065 |
|
|
2066 |
if ((channels & 1) != 0) /* no alpha */
|
|
2067 |
{
|
|
2068 |
if (channels >= 3) /* RGB */
|
|
2069 |
{
|
|
2070 |
palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
|
|
2071 |
entry[(2 ^ bgr)]);
|
|
2072 |
palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
|
|
2073 |
entry[1]);
|
|
2074 |
palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
|
|
2075 |
entry[bgr]);
|
|
2076 |
}
|
|
2077 |
|
|
2078 |
else /* Gray */
|
|
2079 |
palette[i].blue = palette[i].red = palette[i].green =
|
|
2080 |
(png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
|
|
2081 |
}
|
|
2082 |
|
|
2083 |
else /* alpha */
|
|
2084 |
{
|
|
2085 |
png_uint_16 alpha = entry[afirst ? 0 : channels-1];
|
|
2086 |
png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
|
|
2087 |
png_uint_32 reciprocal = 0;
|
|
2088 |
|
|
2089 |
/* Calculate a reciprocal, as in the png_write_image_8bit code above
|
|
2090 |
* this is designed to produce a value scaled to 255*65535 when
|
|
2091 |
* divided by 128 (i.e. asr 7).
|
|
2092 |
*/
|
|
2093 |
if (alphabyte > 0 && alphabyte < 255)
|
|
2094 |
reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
|
|
2095 |
|
|
2096 |
tRNS[i] = alphabyte;
|
|
2097 |
if (alphabyte < 255)
|
|
2098 |
num_trans = i+1;
|
|
2099 |
|
|
2100 |
if (channels >= 3) /* RGB */
|
|
2101 |
{
|
|
2102 |
palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
|
|
2103 |
alpha, reciprocal);
|
|
2104 |
palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
|
|
2105 |
reciprocal);
|
|
2106 |
palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
|
|
2107 |
reciprocal);
|
|
2108 |
}
|
|
2109 |
|
|
2110 |
else /* gray */
|
|
2111 |
palette[i].blue = palette[i].red = palette[i].green =
|
|
2112 |
png_unpremultiply(entry[afirst], alpha, reciprocal);
|
|
2113 |
}
|
|
2114 |
}
|
|
2115 |
|
|
2116 |
else /* Color-map has sRGB values */
|
|
2117 |
{
|
|
2118 |
png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
|
|
2119 |
|
|
2120 |
entry += i * channels;
|
|
2121 |
|
|
2122 |
switch (channels)
|
|
2123 |
{
|
|
2124 |
case 4:
|
|
2125 |
tRNS[i] = entry[afirst ? 0 : 3];
|
|
2126 |
if (tRNS[i] < 255)
|
|
2127 |
num_trans = i+1;
|
|
2128 |
/* FALL THROUGH */
|
|
2129 |
case 3:
|
|
2130 |
palette[i].blue = entry[afirst + (2 ^ bgr)];
|
|
2131 |
palette[i].green = entry[afirst + 1];
|
|
2132 |
palette[i].red = entry[afirst + bgr];
|
|
2133 |
break;
|
|
2134 |
|
|
2135 |
case 2:
|
|
2136 |
tRNS[i] = entry[1 ^ afirst];
|
|
2137 |
if (tRNS[i] < 255)
|
|
2138 |
num_trans = i+1;
|
|
2139 |
/* FALL THROUGH */
|
|
2140 |
case 1:
|
|
2141 |
palette[i].blue = palette[i].red = palette[i].green =
|
|
2142 |
entry[afirst];
|
|
2143 |
break;
|
|
2144 |
|
|
2145 |
default:
|
|
2146 |
break;
|
|
2147 |
}
|
|
2148 |
}
|
|
2149 |
}
|
|
2150 |
|
|
2151 |
# ifdef afirst
|
|
2152 |
# undef afirst
|
|
2153 |
# endif
|
|
2154 |
# ifdef bgr
|
|
2155 |
# undef bgr
|
|
2156 |
# endif
|
|
2157 |
|
|
2158 |
png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
|
|
2159 |
entries);
|
|
2160 |
|
|
2161 |
if (num_trans > 0)
|
|
2162 |
png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
|
|
2163 |
num_trans, NULL);
|
|
2164 |
|
|
2165 |
image->colormap_entries = entries;
|
|
2166 |
}
|
|
2167 |
|
|
2168 |
static int
|
|
2169 |
png_image_write_main(png_voidp argument)
|
|
2170 |
{
|
|
2171 |
png_image_write_control *display = png_voidcast(png_image_write_control*,
|
|
2172 |
argument);
|
|
2173 |
png_imagep image = display->image;
|
|
2174 |
png_structrp png_ptr = image->opaque->png_ptr;
|
|
2175 |
png_inforp info_ptr = image->opaque->info_ptr;
|
|
2176 |
png_uint_32 format = image->format;
|
|
2177 |
|
|
2178 |
/* The following four ints are actually booleans */
|
|
2179 |
int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
|
|
2180 |
int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */
|
|
2181 |
int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
|
|
2182 |
int write_16bit = linear && !colormap && (display->convert_to_8bit == 0);
|
|
2183 |
|
|
2184 |
# ifdef PNG_BENIGN_ERRORS_SUPPORTED
|
|
2185 |
/* Make sure we error out on any bad situation */
|
|
2186 |
png_set_benign_errors(png_ptr, 0/*error*/);
|
|
2187 |
# endif
|
|
2188 |
|
|
2189 |
/* Default the 'row_stride' parameter if required. */
|
|
2190 |
if (display->row_stride == 0)
|
|
2191 |
display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
|
|
2192 |
|
|
2193 |
/* Set the required transforms then write the rows in the correct order. */
|
|
2194 |
if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
|
|
2195 |
{
|
|
2196 |
if (display->colormap != NULL && image->colormap_entries > 0)
|
|
2197 |
{
|
|
2198 |
png_uint_32 entries = image->colormap_entries;
|
|
2199 |
|
|
2200 |
png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
|
|
2201 |
entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
|
|
2202 |
PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
|
|
2203 |
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
2204 |
|
|
2205 |
png_image_set_PLTE(display);
|
|
2206 |
}
|
|
2207 |
|
|
2208 |
else
|
|
2209 |
png_error(image->opaque->png_ptr,
|
|
2210 |
"no color-map for color-mapped image");
|
|
2211 |
}
|
|
2212 |
|
|
2213 |
else
|
|
2214 |
png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
|
|
2215 |
write_16bit ? 16 : 8,
|
|
2216 |
((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
|
|
2217 |
((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
|
|
2218 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
2219 |
|
|
2220 |
/* Counter-intuitively the data transformations must be called *after*
|
|
2221 |
* png_write_info, not before as in the read code, but the 'set' functions
|
|
2222 |
* must still be called before. Just set the color space information, never
|
|
2223 |
* write an interlaced image.
|
|
2224 |
*/
|
|
2225 |
|
|
2226 |
if (write_16bit != 0)
|
|
2227 |
{
|
|
2228 |
/* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
|
|
2229 |
png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
|
|
2230 |
|
|
2231 |
if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
|
|
2232 |
png_set_cHRM_fixed(png_ptr, info_ptr,
|
|
2233 |
/* color x y */
|
|
2234 |
/* white */ 31270, 32900,
|
|
2235 |
/* red */ 64000, 33000,
|
|
2236 |
/* green */ 30000, 60000,
|
|
2237 |
/* blue */ 15000, 6000
|
|
2238 |
);
|
|
2239 |
}
|
|
2240 |
|
|
2241 |
else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
|
|
2242 |
png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
|
|
2243 |
|
|
2244 |
/* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
|
|
2245 |
* space must still be gamma encoded.
|
|
2246 |
*/
|
|
2247 |
else
|
|
2248 |
png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
|
|
2249 |
|
|
2250 |
/* Write the file header. */
|
|
2251 |
png_write_info(png_ptr, info_ptr);
|
|
2252 |
|
|
2253 |
/* Now set up the data transformations (*after* the header is written),
|
|
2254 |
* remove the handled transformations from the 'format' flags for checking.
|
|
2255 |
*
|
|
2256 |
* First check for a little endian system if writing 16 bit files.
|
|
2257 |
*/
|
|
2258 |
if (write_16bit != 0)
|
|
2259 |
{
|
|
2260 |
PNG_CONST png_uint_16 le = 0x0001;
|
|
2261 |
|
|
2262 |
if ((*(png_const_bytep) & le) != 0)
|
|
2263 |
png_set_swap(png_ptr);
|
|
2264 |
}
|
|
2265 |
|
|
2266 |
# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
|
|
2267 |
if ((format & PNG_FORMAT_FLAG_BGR) != 0)
|
|
2268 |
{
|
|
2269 |
if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
|
|
2270 |
png_set_bgr(png_ptr);
|
|
2271 |
format &= ~PNG_FORMAT_FLAG_BGR;
|
|
2272 |
}
|
|
2273 |
# endif
|
|
2274 |
|
|
2275 |
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
|
|
2276 |
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
|
|
2277 |
{
|
|
2278 |
if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
|
|
2279 |
png_set_swap_alpha(png_ptr);
|
|
2280 |
format &= ~PNG_FORMAT_FLAG_AFIRST;
|
|
2281 |
}
|
|
2282 |
# endif
|
|
2283 |
|
|
2284 |
/* If there are 16 or fewer color-map entries we wrote a lower bit depth
|
|
2285 |
* above, but the application data is still byte packed.
|
|
2286 |
*/
|
|
2287 |
if (colormap != 0 && image->colormap_entries <= 16)
|
|
2288 |
png_set_packing(png_ptr);
|
|
2289 |
|
|
2290 |
/* That should have handled all (both) the transforms. */
|
|
2291 |
if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
|
|
2292 |
PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
|
|
2293 |
png_error(png_ptr, "png_write_image: unsupported transformation");
|
|
2294 |
|
|
2295 |
{
|
|
2296 |
png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
|
|
2297 |
ptrdiff_t row_bytes = display->row_stride;
|
|
2298 |
|
|
2299 |
if (linear != 0)
|
|
2300 |
row_bytes *= (sizeof (png_uint_16));
|
|
2301 |
|
|
2302 |
if (row_bytes < 0)
|
|
2303 |
row += (image->height-1) * (-row_bytes);
|
|
2304 |
|
|
2305 |
display->first_row = row;
|
|
2306 |
display->row_bytes = row_bytes;
|
|
2307 |
}
|
|
2308 |
|
|
2309 |
/* Apply 'fast' options if the flag is set. */
|
|
2310 |
if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
|
|
2311 |
{
|
|
2312 |
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
|
|
2313 |
/* NOTE: determined by experiment using pngstest, this reflects some
|
|
2314 |
* balance between the time to write the image once and the time to read
|
|
2315 |
* it about 50 times. The speed-up in pngstest was about 10-20% of the
|
|
2316 |
* total (user) time on a heavily loaded system.
|
|
2317 |
*/
|
|
2318 |
png_set_compression_level(png_ptr, 3);
|
|
2319 |
}
|
|
2320 |
|
|
2321 |
/* Check for the cases that currently require a pre-transform on the row
|
|
2322 |
* before it is written. This only applies when the input is 16-bit and
|
|
2323 |
* either there is an alpha channel or it is converted to 8-bit.
|
|
2324 |
*/
|
|
2325 |
if ((linear != 0 && alpha != 0 ) ||
|
|
2326 |
(colormap == 0 && display->convert_to_8bit != 0))
|
|
2327 |
{
|
|
2328 |
png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
|
|
2329 |
png_get_rowbytes(png_ptr, info_ptr)));
|
|
2330 |
int result;
|
|
2331 |
|
|
2332 |
display->local_row = row;
|
|
2333 |
if (write_16bit != 0)
|
|
2334 |
result = png_safe_execute(image, png_write_image_16bit, display);
|
|
2335 |
else
|
|
2336 |
result = png_safe_execute(image, png_write_image_8bit, display);
|
|
2337 |
display->local_row = NULL;
|
|
2338 |
|
|
2339 |
png_free(png_ptr, row);
|
|
2340 |
|
|
2341 |
/* Skip the 'write_end' on error: */
|
|
2342 |
if (result == 0)
|
|
2343 |
return 0;
|
|
2344 |
}
|
|
2345 |
|
|
2346 |
/* Otherwise this is the case where the input is in a format currently
|
|
2347 |
* supported by the rest of the libpng write code; call it directly.
|
|
2348 |
*/
|
|
2349 |
else
|
|
2350 |
{
|
|
2351 |
png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
|
|
2352 |
ptrdiff_t row_bytes = display->row_bytes;
|
|
2353 |
png_uint_32 y = image->height;
|
|
2354 |
|
|
2355 |
while (y-- > 0)
|
|
2356 |
{
|
|
2357 |
png_write_row(png_ptr, row);
|
|
2358 |
row += row_bytes;
|
|
2359 |
}
|
|
2360 |
}
|
|
2361 |
|
|
2362 |
png_write_end(png_ptr, info_ptr);
|
|
2363 |
return 1;
|
|
2364 |
}
|
|
2365 |
|
|
2366 |
int PNGAPI
|
|
2367 |
png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
|
|
2368 |
const void *buffer, png_int_32 row_stride, const void *colormap)
|
|
2369 |
{
|
|
2370 |
/* Write the image to the given (FILE*). */
|
|
2371 |
if (image != NULL && image->version == PNG_IMAGE_VERSION)
|
|
2372 |
{
|
|
2373 |
if (file != NULL)
|
|
2374 |
{
|
|
2375 |
if (png_image_write_init(image) != 0)
|
|
2376 |
{
|
|
2377 |
png_image_write_control display;
|
|
2378 |
int result;
|
|
2379 |
|
|
2380 |
/* This is slightly evil, but png_init_io doesn't do anything other
|
|
2381 |
* than this and we haven't changed the standard IO functions so
|
|
2382 |
* this saves a 'safe' function.
|
|
2383 |
*/
|
|
2384 |
image->opaque->png_ptr->io_ptr = file;
|
|
2385 |
|
|
2386 |
memset(&display, 0, (sizeof display));
|
|
2387 |
display.image = image;
|
|
2388 |
display.buffer = buffer;
|
|
2389 |
display.row_stride = row_stride;
|
|
2390 |
display.colormap = colormap;
|
|
2391 |
display.convert_to_8bit = convert_to_8bit;
|
|
2392 |
|
|
2393 |
result = png_safe_execute(image, png_image_write_main, &display);
|
|
2394 |
png_image_free(image);
|
|
2395 |
return result;
|
|
2396 |
}
|
|
2397 |
|
|
2398 |
else
|
|
2399 |
return 0;
|
|
2400 |
}
|
|
2401 |
|
|
2402 |
else
|
|
2403 |
return png_image_error(image,
|
|
2404 |
"png_image_write_to_stdio: invalid argument");
|
|
2405 |
}
|
|
2406 |
|
|
2407 |
else if (image != NULL)
|
|
2408 |
return png_image_error(image,
|
|
2409 |
"png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
|
|
2410 |
|
|
2411 |
else
|
|
2412 |
return 0;
|
|
2413 |
}
|
|
2414 |
|
|
2415 |
int PNGAPI
|
|
2416 |
png_image_write_to_file(png_imagep image, const char *file_name,
|
|
2417 |
int convert_to_8bit, const void *buffer, png_int_32 row_stride,
|
|
2418 |
const void *colormap)
|
|
2419 |
{
|
|
2420 |
/* Write the image to the named file. */
|
|
2421 |
if (image != NULL && image->version == PNG_IMAGE_VERSION)
|
|
2422 |
{
|
|
2423 |
if (file_name != NULL)
|
|
2424 |
{
|
|
2425 |
FILE *fp = fopen(file_name, "wb");
|
|
2426 |
|
|
2427 |
if (fp != NULL)
|
|
2428 |
{
|
|
2429 |
if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
|
|
2430 |
row_stride, colormap) != 0)
|
|
2431 |
{
|
|
2432 |
int error; /* from fflush/fclose */
|
|
2433 |
|
|
2434 |
/* Make sure the file is flushed correctly. */
|
|
2435 |
if (fflush(fp) == 0 && ferror(fp) == 0)
|
|
2436 |
{
|
|
2437 |
if (fclose(fp) == 0)
|
|
2438 |
return 1;
|
|
2439 |
|
|
2440 |
error = errno; /* from fclose */
|
|
2441 |
}
|
|
2442 |
|
|
2443 |
else
|
|
2444 |
{
|
|
2445 |
error = errno; /* from fflush or ferror */
|
|
2446 |
(void)fclose(fp);
|
|
2447 |
}
|
|
2448 |
|
|
2449 |
(void)remove(file_name);
|
|
2450 |
/* The image has already been cleaned up; this is just used to
|
|
2451 |
* set the error (because the original write succeeded).
|
|
2452 |
*/
|
|
2453 |
return png_image_error(image, strerror(error));
|
|
2454 |
}
|
|
2455 |
|
|
2456 |
else
|
|
2457 |
{
|
|
2458 |
/* Clean up: just the opened file. */
|
|
2459 |
(void)fclose(fp);
|
|
2460 |
(void)remove(file_name);
|
|
2461 |
return 0;
|
|
2462 |
}
|
|
2463 |
}
|
|
2464 |
|
|
2465 |
else
|
|
2466 |
return png_image_error(image, strerror(errno));
|
|
2467 |
}
|
|
2468 |
|
|
2469 |
else
|
|
2470 |
return png_image_error(image,
|
|
2471 |
"png_image_write_to_file: invalid argument");
|
|
2472 |
}
|
|
2473 |
|
|
2474 |
else if (image != NULL)
|
|
2475 |
return png_image_error(image,
|
|
2476 |
"png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
|
|
2477 |
|
|
2478 |
else
|
|
2479 |
return 0;
|
|
2480 |
}
|
|
2481 |
#endif /* STDIO */
|
|
2482 |
#endif /* SIMPLIFIED_WRITE */
|
|
2483 |
#endif /* WRITE */
|