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