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