]> git.saurik.com Git - wxWidgets.git/blame - src/png/pngwrite.c
make wxSelectDispatcher ctor public, it can be useful to create it in places other...
[wxWidgets.git] / src / png / pngwrite.c
CommitLineData
0272a10d
VZ
1
2/* pngwrite.c - general routines to write a PNG file
3 *
4 * Last changed in libpng 1.2.15 January 5, 2007
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2007 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 */
10
11/* get internal access to png.h */
12#define PNG_INTERNAL
13#include "png.h"
14#ifdef PNG_WRITE_SUPPORTED
15
16/* Writes all the PNG information. This is the suggested way to use the
17 * library. If you have a new chunk to add, make a function to write it,
18 * and put it in the correct location here. If you want the chunk written
19 * after the image data, put it in png_write_end(). I strongly encourage
20 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
21 * the chunk, as that will keep the code from breaking if you want to just
22 * write a plain PNG file. If you have long comments, I suggest writing
23 * them in png_write_end(), and compressing them.
24 */
25void PNGAPI
26png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
27{
28 png_debug(1, "in png_write_info_before_PLTE\n");
29 if (png_ptr == NULL || info_ptr == NULL)
30 return;
31 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
32 {
33 png_write_sig(png_ptr); /* write PNG signature */
34#if defined(PNG_MNG_FEATURES_SUPPORTED)
35 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
36 {
37 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
38 png_ptr->mng_features_permitted=0;
39 }
40#endif
41 /* write IHDR information. */
42 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
43 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
44 info_ptr->filter_type,
45#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
46 info_ptr->interlace_type);
47#else
48 0);
49#endif
50 /* the rest of these check to see if the valid field has the appropriate
51 flag set, and if it does, writes the chunk. */
52#if defined(PNG_WRITE_gAMA_SUPPORTED)
53 if (info_ptr->valid & PNG_INFO_gAMA)
54 {
55# ifdef PNG_FLOATING_POINT_SUPPORTED
56 png_write_gAMA(png_ptr, info_ptr->gamma);
57#else
58#ifdef PNG_FIXED_POINT_SUPPORTED
59 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
60# endif
61#endif
62 }
63#endif
64#if defined(PNG_WRITE_sRGB_SUPPORTED)
65 if (info_ptr->valid & PNG_INFO_sRGB)
66 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
67#endif
68#if defined(PNG_WRITE_iCCP_SUPPORTED)
69 if (info_ptr->valid & PNG_INFO_iCCP)
70 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
71 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
72#endif
73#if defined(PNG_WRITE_sBIT_SUPPORTED)
74 if (info_ptr->valid & PNG_INFO_sBIT)
75 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
76#endif
77#if defined(PNG_WRITE_cHRM_SUPPORTED)
78 if (info_ptr->valid & PNG_INFO_cHRM)
79 {
80#ifdef PNG_FLOATING_POINT_SUPPORTED
81 png_write_cHRM(png_ptr,
82 info_ptr->x_white, info_ptr->y_white,
83 info_ptr->x_red, info_ptr->y_red,
84 info_ptr->x_green, info_ptr->y_green,
85 info_ptr->x_blue, info_ptr->y_blue);
86#else
87# ifdef PNG_FIXED_POINT_SUPPORTED
88 png_write_cHRM_fixed(png_ptr,
89 info_ptr->int_x_white, info_ptr->int_y_white,
90 info_ptr->int_x_red, info_ptr->int_y_red,
91 info_ptr->int_x_green, info_ptr->int_y_green,
92 info_ptr->int_x_blue, info_ptr->int_y_blue);
93# endif
94#endif
95 }
96#endif
97#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
98 if (info_ptr->unknown_chunks_num)
99 {
100 png_unknown_chunk *up;
101
102 png_debug(5, "writing extra chunks\n");
103
104 for (up = info_ptr->unknown_chunks;
105 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
106 up++)
107 {
108 int keep=png_handle_as_unknown(png_ptr, up->name);
109 if (keep != PNG_HANDLE_CHUNK_NEVER &&
110 up->location && !(up->location & PNG_HAVE_PLTE) &&
111 !(up->location & PNG_HAVE_IDAT) &&
112 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
113 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
114 {
115 png_write_chunk(png_ptr, up->name, up->data, up->size);
116 }
117 }
118 }
119#endif
120 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
121 }
122}
123
124void PNGAPI
125png_write_info(png_structp png_ptr, png_infop info_ptr)
126{
127#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
128 int i;
129#endif
130
131 png_debug(1, "in png_write_info\n");
132
133 if (png_ptr == NULL || info_ptr == NULL)
134 return;
135
136 png_write_info_before_PLTE(png_ptr, info_ptr);
137
138 if (info_ptr->valid & PNG_INFO_PLTE)
139 png_write_PLTE(png_ptr, info_ptr->palette,
140 (png_uint_32)info_ptr->num_palette);
141 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
142 png_error(png_ptr, "Valid palette required for paletted images");
143
144#if defined(PNG_WRITE_tRNS_SUPPORTED)
145 if (info_ptr->valid & PNG_INFO_tRNS)
146 {
147#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
148 /* invert the alpha channel (in tRNS) */
149 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
150 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
151 {
152 int j;
153 for (j=0; j<(int)info_ptr->num_trans; j++)
154 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
155 }
156#endif
157 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
158 info_ptr->num_trans, info_ptr->color_type);
159 }
160#endif
161#if defined(PNG_WRITE_bKGD_SUPPORTED)
162 if (info_ptr->valid & PNG_INFO_bKGD)
163 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
164#endif
165#if defined(PNG_WRITE_hIST_SUPPORTED)
166 if (info_ptr->valid & PNG_INFO_hIST)
167 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
168#endif
169#if defined(PNG_WRITE_oFFs_SUPPORTED)
170 if (info_ptr->valid & PNG_INFO_oFFs)
171 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
172 info_ptr->offset_unit_type);
173#endif
174#if defined(PNG_WRITE_pCAL_SUPPORTED)
175 if (info_ptr->valid & PNG_INFO_pCAL)
176 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
177 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
178 info_ptr->pcal_units, info_ptr->pcal_params);
179#endif
180#if defined(PNG_WRITE_sCAL_SUPPORTED)
181 if (info_ptr->valid & PNG_INFO_sCAL)
182#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
183 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
184 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
185#else
186#ifdef PNG_FIXED_POINT_SUPPORTED
187 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
188 info_ptr->scal_s_width, info_ptr->scal_s_height);
189#else
190 png_warning(png_ptr,
191 "png_write_sCAL not supported; sCAL chunk not written.");
192#endif
193#endif
194#endif
195#if defined(PNG_WRITE_pHYs_SUPPORTED)
196 if (info_ptr->valid & PNG_INFO_pHYs)
197 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
198 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
199#endif
200#if defined(PNG_WRITE_tIME_SUPPORTED)
201 if (info_ptr->valid & PNG_INFO_tIME)
202 {
203 png_write_tIME(png_ptr, &(info_ptr->mod_time));
204 png_ptr->mode |= PNG_WROTE_tIME;
205 }
206#endif
207#if defined(PNG_WRITE_sPLT_SUPPORTED)
208 if (info_ptr->valid & PNG_INFO_sPLT)
209 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
210 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
211#endif
212#if defined(PNG_WRITE_TEXT_SUPPORTED)
213 /* Check to see if we need to write text chunks */
214 for (i = 0; i < info_ptr->num_text; i++)
215 {
216 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
217 info_ptr->text[i].compression);
218 /* an internationalized chunk? */
219 if (info_ptr->text[i].compression > 0)
220 {
221#if defined(PNG_WRITE_iTXt_SUPPORTED)
222 /* write international chunk */
223 png_write_iTXt(png_ptr,
224 info_ptr->text[i].compression,
225 info_ptr->text[i].key,
226 info_ptr->text[i].lang,
227 info_ptr->text[i].lang_key,
228 info_ptr->text[i].text);
229#else
230 png_warning(png_ptr, "Unable to write international text");
231#endif
232 /* Mark this chunk as written */
233 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
234 }
235 /* If we want a compressed text chunk */
236 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
237 {
238#if defined(PNG_WRITE_zTXt_SUPPORTED)
239 /* write compressed chunk */
240 png_write_zTXt(png_ptr, info_ptr->text[i].key,
241 info_ptr->text[i].text, 0,
242 info_ptr->text[i].compression);
243#else
244 png_warning(png_ptr, "Unable to write compressed text");
245#endif
246 /* Mark this chunk as written */
247 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
248 }
249 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
250 {
251#if defined(PNG_WRITE_tEXt_SUPPORTED)
252 /* write uncompressed chunk */
253 png_write_tEXt(png_ptr, info_ptr->text[i].key,
254 info_ptr->text[i].text,
255 0);
256#else
257 png_warning(png_ptr, "Unable to write uncompressed text");
258#endif
259 /* Mark this chunk as written */
260 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
261 }
262 }
263#endif
264#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
265 if (info_ptr->unknown_chunks_num)
266 {
267 png_unknown_chunk *up;
268
269 png_debug(5, "writing extra chunks\n");
270
271 for (up = info_ptr->unknown_chunks;
272 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
273 up++)
274 {
275 int keep=png_handle_as_unknown(png_ptr, up->name);
276 if (keep != PNG_HANDLE_CHUNK_NEVER &&
277 up->location && (up->location & PNG_HAVE_PLTE) &&
278 !(up->location & PNG_HAVE_IDAT) &&
279 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
280 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
281 {
282 png_write_chunk(png_ptr, up->name, up->data, up->size);
283 }
284 }
285 }
286#endif
287}
288
289/* Writes the end of the PNG file. If you don't want to write comments or
290 * time information, you can pass NULL for info. If you already wrote these
291 * in png_write_info(), do not write them again here. If you have long
292 * comments, I suggest writing them here, and compressing them.
293 */
294void PNGAPI
295png_write_end(png_structp png_ptr, png_infop info_ptr)
296{
297 png_debug(1, "in png_write_end\n");
298 if (png_ptr == NULL)
299 return;
300 if (!(png_ptr->mode & PNG_HAVE_IDAT))
301 png_error(png_ptr, "No IDATs written into file");
302
303 /* see if user wants us to write information chunks */
304 if (info_ptr != NULL)
305 {
306#if defined(PNG_WRITE_TEXT_SUPPORTED)
307 int i; /* local index variable */
308#endif
309#if defined(PNG_WRITE_tIME_SUPPORTED)
310 /* check to see if user has supplied a time chunk */
311 if ((info_ptr->valid & PNG_INFO_tIME) &&
312 !(png_ptr->mode & PNG_WROTE_tIME))
313 png_write_tIME(png_ptr, &(info_ptr->mod_time));
314#endif
315#if defined(PNG_WRITE_TEXT_SUPPORTED)
316 /* loop through comment chunks */
317 for (i = 0; i < info_ptr->num_text; i++)
318 {
319 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
320 info_ptr->text[i].compression);
321 /* an internationalized chunk? */
322 if (info_ptr->text[i].compression > 0)
323 {
324#if defined(PNG_WRITE_iTXt_SUPPORTED)
325 /* write international chunk */
326 png_write_iTXt(png_ptr,
327 info_ptr->text[i].compression,
328 info_ptr->text[i].key,
329 info_ptr->text[i].lang,
330 info_ptr->text[i].lang_key,
331 info_ptr->text[i].text);
332#else
333 png_warning(png_ptr, "Unable to write international text");
334#endif
335 /* Mark this chunk as written */
336 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
337 }
338 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
339 {
340#if defined(PNG_WRITE_zTXt_SUPPORTED)
341 /* write compressed chunk */
342 png_write_zTXt(png_ptr, info_ptr->text[i].key,
343 info_ptr->text[i].text, 0,
344 info_ptr->text[i].compression);
345#else
346 png_warning(png_ptr, "Unable to write compressed text");
347#endif
348 /* Mark this chunk as written */
349 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
350 }
351 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
352 {
353#if defined(PNG_WRITE_tEXt_SUPPORTED)
354 /* write uncompressed chunk */
355 png_write_tEXt(png_ptr, info_ptr->text[i].key,
356 info_ptr->text[i].text, 0);
357#else
358 png_warning(png_ptr, "Unable to write uncompressed text");
359#endif
360
361 /* Mark this chunk as written */
362 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
363 }
364 }
365#endif
366#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
367 if (info_ptr->unknown_chunks_num)
368 {
369 png_unknown_chunk *up;
370
371 png_debug(5, "writing extra chunks\n");
372
373 for (up = info_ptr->unknown_chunks;
374 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
375 up++)
376 {
377 int keep=png_handle_as_unknown(png_ptr, up->name);
378 if (keep != PNG_HANDLE_CHUNK_NEVER &&
379 up->location && (up->location & PNG_AFTER_IDAT) &&
380 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
381 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
382 {
383 png_write_chunk(png_ptr, up->name, up->data, up->size);
384 }
385 }
386 }
387#endif
388 }
389
390 png_ptr->mode |= PNG_AFTER_IDAT;
391
392 /* write end of PNG file */
393 png_write_IEND(png_ptr);
394}
395
396#if defined(PNG_WRITE_tIME_SUPPORTED)
397#if !defined(_WIN32_WCE)
398/* "time.h" functions are not supported on WindowsCE */
399void PNGAPI
400png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
401{
402 png_debug(1, "in png_convert_from_struct_tm\n");
403 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
404 ptime->month = (png_byte)(ttime->tm_mon + 1);
405 ptime->day = (png_byte)ttime->tm_mday;
406 ptime->hour = (png_byte)ttime->tm_hour;
407 ptime->minute = (png_byte)ttime->tm_min;
408 ptime->second = (png_byte)ttime->tm_sec;
409}
410
411void PNGAPI
412png_convert_from_time_t(png_timep ptime, time_t ttime)
413{
414 struct tm *tbuf;
415
416 png_debug(1, "in png_convert_from_time_t\n");
417 tbuf = gmtime(&ttime);
418 png_convert_from_struct_tm(ptime, tbuf);
419}
420#endif
421#endif
422
423/* Initialize png_ptr structure, and allocate any memory needed */
424png_structp PNGAPI
425png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
426 png_error_ptr error_fn, png_error_ptr warn_fn)
427{
428#ifdef PNG_USER_MEM_SUPPORTED
429 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
430 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
431}
432
433/* Alternate initialize png_ptr structure, and allocate any memory needed */
434png_structp PNGAPI
435png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
436 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
437 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
438{
439#endif /* PNG_USER_MEM_SUPPORTED */
440 png_structp png_ptr;
441#ifdef PNG_SETJMP_SUPPORTED
442#ifdef USE_FAR_KEYWORD
443 jmp_buf jmpbuf;
444#endif
445#endif
446 int i;
447 png_debug(1, "in png_create_write_struct\n");
448#ifdef PNG_USER_MEM_SUPPORTED
449 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
450 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
451#else
452 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
453#endif /* PNG_USER_MEM_SUPPORTED */
454 if (png_ptr == NULL)
455 return (NULL);
456
457 /* added at libpng-1.2.6 */
458#ifdef PNG_SET_USER_LIMITS_SUPPORTED
459 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
460 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
461#endif
462
463#ifdef PNG_SETJMP_SUPPORTED
464#ifdef USE_FAR_KEYWORD
465 if (setjmp(jmpbuf))
466#else
467 if (setjmp(png_ptr->jmpbuf))
468#endif
469 {
470 png_free(png_ptr, png_ptr->zbuf);
471 png_ptr->zbuf=NULL;
472 png_destroy_struct(png_ptr);
473 return (NULL);
474 }
475#ifdef USE_FAR_KEYWORD
476 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
477#endif
478#endif
479
480#ifdef PNG_USER_MEM_SUPPORTED
481 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
482#endif /* PNG_USER_MEM_SUPPORTED */
483 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
484
485 i=0;
486 do
487 {
488 if(user_png_ver[i] != png_libpng_ver[i])
489 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
490 } while (png_libpng_ver[i++]);
491
492 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
493 {
494 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
495 * we must recompile any applications that use any older library version.
496 * For versions after libpng 1.0, we will be compatible, so we need
497 * only check the first digit.
498 */
499 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
500 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
501 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
502 {
503#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
504 char msg[80];
505 if (user_png_ver)
506 {
507 png_snprintf(msg, 80,
508 "Application was compiled with png.h from libpng-%.20s",
509 user_png_ver);
510 png_warning(png_ptr, msg);
511 }
512 png_snprintf(msg, 80,
513 "Application is running with png.c from libpng-%.20s",
514 png_libpng_ver);
515 png_warning(png_ptr, msg);
516#endif
517#ifdef PNG_ERROR_NUMBERS_SUPPORTED
518 png_ptr->flags=0;
519#endif
520 png_error(png_ptr,
521 "Incompatible libpng version in application and library");
522 }
523 }
524
525 /* initialize zbuf - compression buffer */
526 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
527 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
528 (png_uint_32)png_ptr->zbuf_size);
529
530 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
531 png_flush_ptr_NULL);
532
533#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
534 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
535 1, png_doublep_NULL, png_doublep_NULL);
536#endif
537
538#ifdef PNG_SETJMP_SUPPORTED
539/* Applications that neglect to set up their own setjmp() and then encounter
540 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
541 abort instead of returning. */
542#ifdef USE_FAR_KEYWORD
543 if (setjmp(jmpbuf))
544 PNG_ABORT();
545 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
546#else
547 if (setjmp(png_ptr->jmpbuf))
548 PNG_ABORT();
549#endif
550#endif
551 return (png_ptr);
552}
553
554/* Initialize png_ptr structure, and allocate any memory needed */
555#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
556/* Deprecated. */
557#undef png_write_init
558void PNGAPI
559png_write_init(png_structp png_ptr)
560{
561 /* We only come here via pre-1.0.7-compiled applications */
562 png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
563}
564
565void PNGAPI
566png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
567 png_size_t png_struct_size, png_size_t png_info_size)
568{
569 /* We only come here via pre-1.0.12-compiled applications */
570 if(png_ptr == NULL) return;
571#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
572 if(png_sizeof(png_struct) > png_struct_size ||
573 png_sizeof(png_info) > png_info_size)
574 {
575 char msg[80];
576 png_ptr->warning_fn=NULL;
577 if (user_png_ver)
578 {
579 png_snprintf(msg, 80,
580 "Application was compiled with png.h from libpng-%.20s",
581 user_png_ver);
582 png_warning(png_ptr, msg);
583 }
584 png_snprintf(msg, 80,
585 "Application is running with png.c from libpng-%.20s",
586 png_libpng_ver);
587 png_warning(png_ptr, msg);
588 }
589#endif
590 if(png_sizeof(png_struct) > png_struct_size)
591 {
592 png_ptr->error_fn=NULL;
593#ifdef PNG_ERROR_NUMBERS_SUPPORTED
594 png_ptr->flags=0;
595#endif
596 png_error(png_ptr,
597 "The png struct allocated by the application for writing is too small.");
598 }
599 if(png_sizeof(png_info) > png_info_size)
600 {
601 png_ptr->error_fn=NULL;
602#ifdef PNG_ERROR_NUMBERS_SUPPORTED
603 png_ptr->flags=0;
604#endif
605 png_error(png_ptr,
606 "The info struct allocated by the application for writing is too small.");
607 }
608 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
609}
610#endif /* PNG_1_0_X || PNG_1_2_X */
611
612
613void PNGAPI
614png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
615 png_size_t png_struct_size)
616{
617 png_structp png_ptr=*ptr_ptr;
618#ifdef PNG_SETJMP_SUPPORTED
619 jmp_buf tmp_jmp; /* to save current jump buffer */
620#endif
621
622 int i = 0;
623
624 if (png_ptr == NULL)
625 return;
626
627 do
628 {
629 if (user_png_ver[i] != png_libpng_ver[i])
630 {
631#ifdef PNG_LEGACY_SUPPORTED
632 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
633#else
634 png_ptr->warning_fn=NULL;
635 png_warning(png_ptr,
636 "Application uses deprecated png_write_init() and should be recompiled.");
637 break;
638#endif
639 }
640 } while (png_libpng_ver[i++]);
641
642 png_debug(1, "in png_write_init_3\n");
643
644#ifdef PNG_SETJMP_SUPPORTED
645 /* save jump buffer and error functions */
646 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
647#endif
648
649 if (png_sizeof(png_struct) > png_struct_size)
650 {
651 png_destroy_struct(png_ptr);
652 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
653 *ptr_ptr = png_ptr;
654 }
655
656 /* reset all variables to 0 */
657 png_memset(png_ptr, 0, png_sizeof (png_struct));
658
659 /* added at libpng-1.2.6 */
660#ifdef PNG_SET_USER_LIMITS_SUPPORTED
661 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
662 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
663#endif
664
665#ifdef PNG_SETJMP_SUPPORTED
666 /* restore jump buffer */
667 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
668#endif
669
670 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
671 png_flush_ptr_NULL);
672
673 /* initialize zbuf - compression buffer */
674 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
675 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
676 (png_uint_32)png_ptr->zbuf_size);
677
678#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
679 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
680 1, png_doublep_NULL, png_doublep_NULL);
681#endif
682}
683
684/* Write a few rows of image data. If the image is interlaced,
685 * either you will have to write the 7 sub images, or, if you
686 * have called png_set_interlace_handling(), you will have to
687 * "write" the image seven times.
688 */
689void PNGAPI
690png_write_rows(png_structp png_ptr, png_bytepp row,
691 png_uint_32 num_rows)
692{
693 png_uint_32 i; /* row counter */
694 png_bytepp rp; /* row pointer */
695
696 png_debug(1, "in png_write_rows\n");
697
698 if (png_ptr == NULL)
699 return;
700
701 /* loop through the rows */
702 for (i = 0, rp = row; i < num_rows; i++, rp++)
703 {
704 png_write_row(png_ptr, *rp);
705 }
706}
707
708/* Write the image. You only need to call this function once, even
709 * if you are writing an interlaced image.
710 */
711void PNGAPI
712png_write_image(png_structp png_ptr, png_bytepp image)
713{
714 png_uint_32 i; /* row index */
715 int pass, num_pass; /* pass variables */
716 png_bytepp rp; /* points to current row */
717
718 if (png_ptr == NULL)
719 return;
720
721 png_debug(1, "in png_write_image\n");
722#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
723 /* intialize interlace handling. If image is not interlaced,
724 this will set pass to 1 */
725 num_pass = png_set_interlace_handling(png_ptr);
726#else
727 num_pass = 1;
728#endif
729 /* loop through passes */
730 for (pass = 0; pass < num_pass; pass++)
731 {
732 /* loop through image */
733 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
734 {
735 png_write_row(png_ptr, *rp);
736 }
737 }
738}
739
740/* called by user to write a row of image data */
741void PNGAPI
742png_write_row(png_structp png_ptr, png_bytep row)
743{
744 if (png_ptr == NULL)
745 return;
746 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
747 png_ptr->row_number, png_ptr->pass);
748
749 /* initialize transformations and other stuff if first time */
750 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
751 {
752 /* make sure we wrote the header info */
753 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
754 png_error(png_ptr,
755 "png_write_info was never called before png_write_row.");
756
757 /* check for transforms that have been set but were defined out */
758#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
759 if (png_ptr->transformations & PNG_INVERT_MONO)
760 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
761#endif
762#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
763 if (png_ptr->transformations & PNG_FILLER)
764 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
765#endif
766#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
767 if (png_ptr->transformations & PNG_PACKSWAP)
768 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
769#endif
770#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
771 if (png_ptr->transformations & PNG_PACK)
772 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
773#endif
774#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
775 if (png_ptr->transformations & PNG_SHIFT)
776 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
777#endif
778#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
779 if (png_ptr->transformations & PNG_BGR)
780 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
781#endif
782#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
783 if (png_ptr->transformations & PNG_SWAP_BYTES)
784 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
785#endif
786
787 png_write_start_row(png_ptr);
788 }
789
790#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
791 /* if interlaced and not interested in row, return */
792 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
793 {
794 switch (png_ptr->pass)
795 {
796 case 0:
797 if (png_ptr->row_number & 0x07)
798 {
799 png_write_finish_row(png_ptr);
800 return;
801 }
802 break;
803 case 1:
804 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
805 {
806 png_write_finish_row(png_ptr);
807 return;
808 }
809 break;
810 case 2:
811 if ((png_ptr->row_number & 0x07) != 4)
812 {
813 png_write_finish_row(png_ptr);
814 return;
815 }
816 break;
817 case 3:
818 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
819 {
820 png_write_finish_row(png_ptr);
821 return;
822 }
823 break;
824 case 4:
825 if ((png_ptr->row_number & 0x03) != 2)
826 {
827 png_write_finish_row(png_ptr);
828 return;
829 }
830 break;
831 case 5:
832 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
833 {
834 png_write_finish_row(png_ptr);
835 return;
836 }
837 break;
838 case 6:
839 if (!(png_ptr->row_number & 0x01))
840 {
841 png_write_finish_row(png_ptr);
842 return;
843 }
844 break;
845 }
846 }
847#endif
848
849 /* set up row info for transformations */
850 png_ptr->row_info.color_type = png_ptr->color_type;
851 png_ptr->row_info.width = png_ptr->usr_width;
852 png_ptr->row_info.channels = png_ptr->usr_channels;
853 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
854 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
855 png_ptr->row_info.channels);
856
857 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
858 png_ptr->row_info.width);
859
860 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
861 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
862 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
863 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
864 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
865 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
866
867 /* Copy user's row into buffer, leaving room for filter byte. */
868 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
869 png_ptr->row_info.rowbytes);
870
871#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
872 /* handle interlacing */
873 if (png_ptr->interlaced && png_ptr->pass < 6 &&
874 (png_ptr->transformations & PNG_INTERLACE))
875 {
876 png_do_write_interlace(&(png_ptr->row_info),
877 png_ptr->row_buf + 1, png_ptr->pass);
878 /* this should always get caught above, but still ... */
879 if (!(png_ptr->row_info.width))
880 {
881 png_write_finish_row(png_ptr);
882 return;
883 }
884 }
885#endif
886
887 /* handle other transformations */
888 if (png_ptr->transformations)
889 png_do_write_transformations(png_ptr);
890
891#if defined(PNG_MNG_FEATURES_SUPPORTED)
892 /* Write filter_method 64 (intrapixel differencing) only if
893 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
894 * 2. Libpng did not write a PNG signature (this filter_method is only
895 * used in PNG datastreams that are embedded in MNG datastreams) and
896 * 3. The application called png_permit_mng_features with a mask that
897 * included PNG_FLAG_MNG_FILTER_64 and
898 * 4. The filter_method is 64 and
899 * 5. The color_type is RGB or RGBA
900 */
901 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
902 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
903 {
904 /* Intrapixel differencing */
905 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
906 }
907#endif
908
909 /* Find a filter if necessary, filter the row and write it out. */
910 png_write_find_filter(png_ptr, &(png_ptr->row_info));
911
912 if (png_ptr->write_row_fn != NULL)
913 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
914}
915
916#if defined(PNG_WRITE_FLUSH_SUPPORTED)
917/* Set the automatic flush interval or 0 to turn flushing off */
918void PNGAPI
919png_set_flush(png_structp png_ptr, int nrows)
920{
921 png_debug(1, "in png_set_flush\n");
922 if (png_ptr == NULL)
923 return;
924 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
925}
926
927/* flush the current output buffers now */
928void PNGAPI
929png_write_flush(png_structp png_ptr)
930{
931 int wrote_IDAT;
932
933 png_debug(1, "in png_write_flush\n");
934 if (png_ptr == NULL)
935 return;
936 /* We have already written out all of the data */
937 if (png_ptr->row_number >= png_ptr->num_rows)
938 return;
939
940 do
941 {
942 int ret;
943
944 /* compress the data */
945 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
946 wrote_IDAT = 0;
947
948 /* check for compression errors */
949 if (ret != Z_OK)
950 {
951 if (png_ptr->zstream.msg != NULL)
952 png_error(png_ptr, png_ptr->zstream.msg);
953 else
954 png_error(png_ptr, "zlib error");
955 }
956
957 if (!(png_ptr->zstream.avail_out))
958 {
959 /* write the IDAT and reset the zlib output buffer */
960 png_write_IDAT(png_ptr, png_ptr->zbuf,
961 png_ptr->zbuf_size);
962 png_ptr->zstream.next_out = png_ptr->zbuf;
963 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
964 wrote_IDAT = 1;
965 }
966 } while(wrote_IDAT == 1);
967
968 /* If there is any data left to be output, write it into a new IDAT */
969 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
970 {
971 /* write the IDAT and reset the zlib output buffer */
972 png_write_IDAT(png_ptr, png_ptr->zbuf,
973 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
974 png_ptr->zstream.next_out = png_ptr->zbuf;
975 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
976 }
977 png_ptr->flush_rows = 0;
978 png_flush(png_ptr);
979}
980#endif /* PNG_WRITE_FLUSH_SUPPORTED */
981
982/* free all memory used by the write */
983void PNGAPI
984png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
985{
986 png_structp png_ptr = NULL;
987 png_infop info_ptr = NULL;
988#ifdef PNG_USER_MEM_SUPPORTED
989 png_free_ptr free_fn = NULL;
990 png_voidp mem_ptr = NULL;
991#endif
992
993 png_debug(1, "in png_destroy_write_struct\n");
994 if (png_ptr_ptr != NULL)
995 {
996 png_ptr = *png_ptr_ptr;
997#ifdef PNG_USER_MEM_SUPPORTED
998 free_fn = png_ptr->free_fn;
999 mem_ptr = png_ptr->mem_ptr;
1000#endif
1001 }
1002
1003 if (info_ptr_ptr != NULL)
1004 info_ptr = *info_ptr_ptr;
1005
1006 if (info_ptr != NULL)
1007 {
1008 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
1009
1010#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1011 if (png_ptr->num_chunk_list)
1012 {
1013 png_free(png_ptr, png_ptr->chunk_list);
1014 png_ptr->chunk_list=NULL;
1015 png_ptr->num_chunk_list=0;
1016 }
1017#endif
1018
1019#ifdef PNG_USER_MEM_SUPPORTED
1020 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1021 (png_voidp)mem_ptr);
1022#else
1023 png_destroy_struct((png_voidp)info_ptr);
1024#endif
1025 *info_ptr_ptr = NULL;
1026 }
1027
1028 if (png_ptr != NULL)
1029 {
1030 png_write_destroy(png_ptr);
1031#ifdef PNG_USER_MEM_SUPPORTED
1032 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1033 (png_voidp)mem_ptr);
1034#else
1035 png_destroy_struct((png_voidp)png_ptr);
1036#endif
1037 *png_ptr_ptr = NULL;
1038 }
1039}
1040
1041
1042/* Free any memory used in png_ptr struct (old method) */
1043void /* PRIVATE */
1044png_write_destroy(png_structp png_ptr)
1045{
1046#ifdef PNG_SETJMP_SUPPORTED
1047 jmp_buf tmp_jmp; /* save jump buffer */
1048#endif
1049 png_error_ptr error_fn;
1050 png_error_ptr warning_fn;
1051 png_voidp error_ptr;
1052#ifdef PNG_USER_MEM_SUPPORTED
1053 png_free_ptr free_fn;
1054#endif
1055
1056 png_debug(1, "in png_write_destroy\n");
1057 /* free any memory zlib uses */
1058 deflateEnd(&png_ptr->zstream);
1059
1060 /* free our memory. png_free checks NULL for us. */
1061 png_free(png_ptr, png_ptr->zbuf);
1062 png_free(png_ptr, png_ptr->row_buf);
1063 png_free(png_ptr, png_ptr->prev_row);
1064 png_free(png_ptr, png_ptr->sub_row);
1065 png_free(png_ptr, png_ptr->up_row);
1066 png_free(png_ptr, png_ptr->avg_row);
1067 png_free(png_ptr, png_ptr->paeth_row);
1068
1069#if defined(PNG_TIME_RFC1123_SUPPORTED)
1070 png_free(png_ptr, png_ptr->time_buffer);
1071#endif
1072
1073#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1074 png_free(png_ptr, png_ptr->prev_filters);
1075 png_free(png_ptr, png_ptr->filter_weights);
1076 png_free(png_ptr, png_ptr->inv_filter_weights);
1077 png_free(png_ptr, png_ptr->filter_costs);
1078 png_free(png_ptr, png_ptr->inv_filter_costs);
1079#endif
1080
1081#ifdef PNG_SETJMP_SUPPORTED
1082 /* reset structure */
1083 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
1084#endif
1085
1086 error_fn = png_ptr->error_fn;
1087 warning_fn = png_ptr->warning_fn;
1088 error_ptr = png_ptr->error_ptr;
1089#ifdef PNG_USER_MEM_SUPPORTED
1090 free_fn = png_ptr->free_fn;
1091#endif
1092
1093 png_memset(png_ptr, 0, png_sizeof (png_struct));
1094
1095 png_ptr->error_fn = error_fn;
1096 png_ptr->warning_fn = warning_fn;
1097 png_ptr->error_ptr = error_ptr;
1098#ifdef PNG_USER_MEM_SUPPORTED
1099 png_ptr->free_fn = free_fn;
1100#endif
1101
1102#ifdef PNG_SETJMP_SUPPORTED
1103 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
1104#endif
1105}
1106
1107/* Allow the application to select one or more row filters to use. */
1108void PNGAPI
1109png_set_filter(png_structp png_ptr, int method, int filters)
1110{
1111 png_debug(1, "in png_set_filter\n");
1112 if (png_ptr == NULL)
1113 return;
1114#if defined(PNG_MNG_FEATURES_SUPPORTED)
1115 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1116 (method == PNG_INTRAPIXEL_DIFFERENCING))
1117 method = PNG_FILTER_TYPE_BASE;
1118#endif
1119 if (method == PNG_FILTER_TYPE_BASE)
1120 {
1121 switch (filters & (PNG_ALL_FILTERS | 0x07))
1122 {
1123#ifndef PNG_NO_WRITE_FILTER
1124 case 5:
1125 case 6:
1126 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1127#endif /* PNG_NO_WRITE_FILTER */
1128 case PNG_FILTER_VALUE_NONE:
1129 png_ptr->do_filter=PNG_FILTER_NONE; break;
1130#ifndef PNG_NO_WRITE_FILTER
1131 case PNG_FILTER_VALUE_SUB:
1132 png_ptr->do_filter=PNG_FILTER_SUB; break;
1133 case PNG_FILTER_VALUE_UP:
1134 png_ptr->do_filter=PNG_FILTER_UP; break;
1135 case PNG_FILTER_VALUE_AVG:
1136 png_ptr->do_filter=PNG_FILTER_AVG; break;
1137 case PNG_FILTER_VALUE_PAETH:
1138 png_ptr->do_filter=PNG_FILTER_PAETH; break;
1139 default: png_ptr->do_filter = (png_byte)filters; break;
1140#else
1141 default: png_warning(png_ptr, "Unknown row filter for method 0");
1142#endif /* PNG_NO_WRITE_FILTER */
1143 }
1144
1145 /* If we have allocated the row_buf, this means we have already started
1146 * with the image and we should have allocated all of the filter buffers
1147 * that have been selected. If prev_row isn't already allocated, then
1148 * it is too late to start using the filters that need it, since we
1149 * will be missing the data in the previous row. If an application
1150 * wants to start and stop using particular filters during compression,
1151 * it should start out with all of the filters, and then add and
1152 * remove them after the start of compression.
1153 */
1154 if (png_ptr->row_buf != NULL)
1155 {
1156#ifndef PNG_NO_WRITE_FILTER
1157 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1158 {
1159 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1160 (png_ptr->rowbytes + 1));
1161 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1162 }
1163
1164 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1165 {
1166 if (png_ptr->prev_row == NULL)
1167 {
1168 png_warning(png_ptr, "Can't add Up filter after starting");
1169 png_ptr->do_filter &= ~PNG_FILTER_UP;
1170 }
1171 else
1172 {
1173 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1174 (png_ptr->rowbytes + 1));
1175 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1176 }
1177 }
1178
1179 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1180 {
1181 if (png_ptr->prev_row == NULL)
1182 {
1183 png_warning(png_ptr, "Can't add Average filter after starting");
1184 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1185 }
1186 else
1187 {
1188 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1189 (png_ptr->rowbytes + 1));
1190 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1191 }
1192 }
1193
1194 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1195 png_ptr->paeth_row == NULL)
1196 {
1197 if (png_ptr->prev_row == NULL)
1198 {
1199 png_warning(png_ptr, "Can't add Paeth filter after starting");
1200 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1201 }
1202 else
1203 {
1204 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1205 (png_ptr->rowbytes + 1));
1206 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1207 }
1208 }
1209
1210 if (png_ptr->do_filter == PNG_NO_FILTERS)
1211#endif /* PNG_NO_WRITE_FILTER */
1212 png_ptr->do_filter = PNG_FILTER_NONE;
1213 }
1214 }
1215 else
1216 png_error(png_ptr, "Unknown custom filter method");
1217}
1218
1219/* This allows us to influence the way in which libpng chooses the "best"
1220 * filter for the current scanline. While the "minimum-sum-of-absolute-
1221 * differences metric is relatively fast and effective, there is some
1222 * question as to whether it can be improved upon by trying to keep the
1223 * filtered data going to zlib more consistent, hopefully resulting in
1224 * better compression.
1225 */
1226#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1227void PNGAPI
1228png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1229 int num_weights, png_doublep filter_weights,
1230 png_doublep filter_costs)
1231{
1232 int i;
1233
1234 png_debug(1, "in png_set_filter_heuristics\n");
1235 if (png_ptr == NULL)
1236 return;
1237 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1238 {
1239 png_warning(png_ptr, "Unknown filter heuristic method");
1240 return;
1241 }
1242
1243 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1244 {
1245 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1246 }
1247
1248 if (num_weights < 0 || filter_weights == NULL ||
1249 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1250 {
1251 num_weights = 0;
1252 }
1253
1254 png_ptr->num_prev_filters = (png_byte)num_weights;
1255 png_ptr->heuristic_method = (png_byte)heuristic_method;
1256
1257 if (num_weights > 0)
1258 {
1259 if (png_ptr->prev_filters == NULL)
1260 {
1261 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1262 (png_uint_32)(png_sizeof(png_byte) * num_weights));
1263
1264 /* To make sure that the weighting starts out fairly */
1265 for (i = 0; i < num_weights; i++)
1266 {
1267 png_ptr->prev_filters[i] = 255;
1268 }
1269 }
1270
1271 if (png_ptr->filter_weights == NULL)
1272 {
1273 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1274 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1275
1276 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1277 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1278 for (i = 0; i < num_weights; i++)
1279 {
1280 png_ptr->inv_filter_weights[i] =
1281 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1282 }
1283 }
1284
1285 for (i = 0; i < num_weights; i++)
1286 {
1287 if (filter_weights[i] < 0.0)
1288 {
1289 png_ptr->inv_filter_weights[i] =
1290 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1291 }
1292 else
1293 {
1294 png_ptr->inv_filter_weights[i] =
1295 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1296 png_ptr->filter_weights[i] =
1297 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1298 }
1299 }
1300 }
1301
1302 /* If, in the future, there are other filter methods, this would
1303 * need to be based on png_ptr->filter.
1304 */
1305 if (png_ptr->filter_costs == NULL)
1306 {
1307 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1308 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1309
1310 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1311 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1312
1313 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1314 {
1315 png_ptr->inv_filter_costs[i] =
1316 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1317 }
1318 }
1319
1320 /* Here is where we set the relative costs of the different filters. We
1321 * should take the desired compression level into account when setting
1322 * the costs, so that Paeth, for instance, has a high relative cost at low
1323 * compression levels, while it has a lower relative cost at higher
1324 * compression settings. The filter types are in order of increasing
1325 * relative cost, so it would be possible to do this with an algorithm.
1326 */
1327 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1328 {
1329 if (filter_costs == NULL || filter_costs[i] < 0.0)
1330 {
1331 png_ptr->inv_filter_costs[i] =
1332 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1333 }
1334 else if (filter_costs[i] >= 1.0)
1335 {
1336 png_ptr->inv_filter_costs[i] =
1337 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1338 png_ptr->filter_costs[i] =
1339 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1340 }
1341 }
1342}
1343#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1344
1345void PNGAPI
1346png_set_compression_level(png_structp png_ptr, int level)
1347{
1348 png_debug(1, "in png_set_compression_level\n");
1349 if (png_ptr == NULL)
1350 return;
1351 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1352 png_ptr->zlib_level = level;
1353}
1354
1355void PNGAPI
1356png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1357{
1358 png_debug(1, "in png_set_compression_mem_level\n");
1359 if (png_ptr == NULL)
1360 return;
1361 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1362 png_ptr->zlib_mem_level = mem_level;
1363}
1364
1365void PNGAPI
1366png_set_compression_strategy(png_structp png_ptr, int strategy)
1367{
1368 png_debug(1, "in png_set_compression_strategy\n");
1369 if (png_ptr == NULL)
1370 return;
1371 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1372 png_ptr->zlib_strategy = strategy;
1373}
1374
1375void PNGAPI
1376png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1377{
1378 if (png_ptr == NULL)
1379 return;
1380 if (window_bits > 15)
1381 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1382 else if (window_bits < 8)
1383 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1384#ifndef WBITS_8_OK
1385 /* avoid libpng bug with 256-byte windows */
1386 if (window_bits == 8)
1387 {
1388 png_warning(png_ptr, "Compression window is being reset to 512");
1389 window_bits=9;
1390 }
1391#endif
1392 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1393 png_ptr->zlib_window_bits = window_bits;
1394}
1395
1396void PNGAPI
1397png_set_compression_method(png_structp png_ptr, int method)
1398{
1399 png_debug(1, "in png_set_compression_method\n");
1400 if (png_ptr == NULL)
1401 return;
1402 if (method != 8)
1403 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1404 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1405 png_ptr->zlib_method = method;
1406}
1407
1408void PNGAPI
1409png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1410{
1411 if (png_ptr == NULL)
1412 return;
1413 png_ptr->write_row_fn = write_row_fn;
1414}
1415
1416#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1417void PNGAPI
1418png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1419 write_user_transform_fn)
1420{
1421 png_debug(1, "in png_set_write_user_transform_fn\n");
1422 if (png_ptr == NULL)
1423 return;
1424 png_ptr->transformations |= PNG_USER_TRANSFORM;
1425 png_ptr->write_user_transform_fn = write_user_transform_fn;
1426}
1427#endif
1428
1429
1430#if defined(PNG_INFO_IMAGE_SUPPORTED)
1431void PNGAPI
1432png_write_png(png_structp png_ptr, png_infop info_ptr,
1433 int transforms, voidp params)
1434{
1435 if (png_ptr == NULL || info_ptr == NULL)
1436 return;
1437#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1438 /* invert the alpha channel from opacity to transparency */
1439 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1440 png_set_invert_alpha(png_ptr);
1441#endif
1442
1443 /* Write the file header information. */
1444 png_write_info(png_ptr, info_ptr);
1445
1446 /* ------ these transformations don't touch the info structure ------- */
1447
1448#if defined(PNG_WRITE_INVERT_SUPPORTED)
1449 /* invert monochrome pixels */
1450 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1451 png_set_invert_mono(png_ptr);
1452#endif
1453
1454#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1455 /* Shift the pixels up to a legal bit depth and fill in
1456 * as appropriate to correctly scale the image.
1457 */
1458 if ((transforms & PNG_TRANSFORM_SHIFT)
1459 && (info_ptr->valid & PNG_INFO_sBIT))
1460 png_set_shift(png_ptr, &info_ptr->sig_bit);
1461#endif
1462
1463#if defined(PNG_WRITE_PACK_SUPPORTED)
1464 /* pack pixels into bytes */
1465 if (transforms & PNG_TRANSFORM_PACKING)
1466 png_set_packing(png_ptr);
1467#endif
1468
1469#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1470 /* swap location of alpha bytes from ARGB to RGBA */
1471 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1472 png_set_swap_alpha(png_ptr);
1473#endif
1474
1475#if defined(PNG_WRITE_FILLER_SUPPORTED)
1476 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1477 * RGB (4 channels -> 3 channels). The second parameter is not used.
1478 */
1479 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1480 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1481#endif
1482
1483#if defined(PNG_WRITE_BGR_SUPPORTED)
1484 /* flip BGR pixels to RGB */
1485 if (transforms & PNG_TRANSFORM_BGR)
1486 png_set_bgr(png_ptr);
1487#endif
1488
1489#if defined(PNG_WRITE_SWAP_SUPPORTED)
1490 /* swap bytes of 16-bit files to most significant byte first */
1491 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1492 png_set_swap(png_ptr);
1493#endif
1494
1495#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1496 /* swap bits of 1, 2, 4 bit packed pixel formats */
1497 if (transforms & PNG_TRANSFORM_PACKSWAP)
1498 png_set_packswap(png_ptr);
1499#endif
1500
1501 /* ----------------------- end of transformations ------------------- */
1502
1503 /* write the bits */
1504 if (info_ptr->valid & PNG_INFO_IDAT)
1505 png_write_image(png_ptr, info_ptr->row_pointers);
1506
1507 /* It is REQUIRED to call this to finish writing the rest of the file */
1508 png_write_end(png_ptr, info_ptr);
1509
1510 transforms = transforms; /* quiet compiler warnings */
1511 params = params;
1512}
1513#endif
1514#endif /* PNG_WRITE_SUPPORTED */