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