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