2 /* pngwrite.c - general routines to write a PNG file
4 * libpng 1.2.4 - July 8, 2002
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2002 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.)
11 /* get internal access to png.h */
14 #ifdef PNG_WRITE_SUPPORTED
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.
26 png_write_info_before_PLTE(png_structp png_ptr
, png_infop info_ptr
)
28 png_debug(1, "in png_write_info_before_PLTE\n");
29 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
31 png_write_sig(png_ptr
); /* write PNG signature */
32 #if defined(PNG_MNG_FEATURES_SUPPORTED)
33 if((png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
)&&(png_ptr
->mng_features_permitted
))
35 png_warning(png_ptr
,"MNG features are not allowed in a PNG datastream\n");
36 png_ptr
->mng_features_permitted
=0;
39 /* write IHDR information. */
40 png_write_IHDR(png_ptr
, info_ptr
->width
, info_ptr
->height
,
41 info_ptr
->bit_depth
, info_ptr
->color_type
, info_ptr
->compression_type
,
42 info_ptr
->filter_type
,
43 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
44 info_ptr
->interlace_type
);
48 /* the rest of these check to see if the valid field has the appropriate
49 flag set, and if it does, writes the chunk. */
50 #if defined(PNG_WRITE_gAMA_SUPPORTED)
51 if (info_ptr
->valid
& PNG_INFO_gAMA
)
53 # ifdef PNG_FLOATING_POINT_SUPPORTED
54 png_write_gAMA(png_ptr
, info_ptr
->gamma
);
56 #ifdef PNG_FIXED_POINT_SUPPORTED
57 png_write_gAMA_fixed(png_ptr
, info_ptr
->int_gamma
);
62 #if defined(PNG_WRITE_sRGB_SUPPORTED)
63 if (info_ptr
->valid
& PNG_INFO_sRGB
)
64 png_write_sRGB(png_ptr
, (int)info_ptr
->srgb_intent
);
66 #if defined(PNG_WRITE_iCCP_SUPPORTED)
67 if (info_ptr
->valid
& PNG_INFO_iCCP
)
68 png_write_iCCP(png_ptr
, info_ptr
->iccp_name
, PNG_COMPRESSION_TYPE_BASE
,
69 info_ptr
->iccp_profile
, (int)info_ptr
->iccp_proflen
);
71 #if defined(PNG_WRITE_sBIT_SUPPORTED)
72 if (info_ptr
->valid
& PNG_INFO_sBIT
)
73 png_write_sBIT(png_ptr
, &(info_ptr
->sig_bit
), info_ptr
->color_type
);
75 #if defined(PNG_WRITE_cHRM_SUPPORTED)
76 if (info_ptr
->valid
& PNG_INFO_cHRM
)
78 #ifdef PNG_FLOATING_POINT_SUPPORTED
79 png_write_cHRM(png_ptr
,
80 info_ptr
->x_white
, info_ptr
->y_white
,
81 info_ptr
->x_red
, info_ptr
->y_red
,
82 info_ptr
->x_green
, info_ptr
->y_green
,
83 info_ptr
->x_blue
, info_ptr
->y_blue
);
85 # ifdef PNG_FIXED_POINT_SUPPORTED
86 png_write_cHRM_fixed(png_ptr
,
87 info_ptr
->int_x_white
, info_ptr
->int_y_white
,
88 info_ptr
->int_x_red
, info_ptr
->int_y_red
,
89 info_ptr
->int_x_green
, info_ptr
->int_y_green
,
90 info_ptr
->int_x_blue
, info_ptr
->int_y_blue
);
95 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
96 if (info_ptr
->unknown_chunks_num
)
98 png_unknown_chunk
*up
;
100 png_debug(5, "writing extra chunks\n");
102 for (up
= info_ptr
->unknown_chunks
;
103 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
106 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
107 if (keep
!= HANDLE_CHUNK_NEVER
&&
108 up
->location
&& (!(up
->location
& PNG_HAVE_PLTE
)) &&
109 ((up
->name
[3] & 0x20) || keep
== HANDLE_CHUNK_ALWAYS
||
110 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
112 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
117 png_ptr
->mode
|= PNG_WROTE_INFO_BEFORE_PLTE
;
122 png_write_info(png_structp png_ptr
, png_infop info_ptr
)
124 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
128 png_debug(1, "in png_write_info\n");
130 png_write_info_before_PLTE(png_ptr
, info_ptr
);
132 if (info_ptr
->valid
& PNG_INFO_PLTE
)
133 png_write_PLTE(png_ptr
, info_ptr
->palette
,
134 (png_uint_32
)info_ptr
->num_palette
);
135 else if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
136 png_error(png_ptr
, "Valid palette required for paletted images\n");
138 #if defined(PNG_WRITE_tRNS_SUPPORTED)
139 if (info_ptr
->valid
& PNG_INFO_tRNS
)
141 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
142 /* invert the alpha channel (in tRNS) */
143 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) &&
144 info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
147 for (j
=0; j
<(int)info_ptr
->num_trans
; j
++)
148 info_ptr
->trans
[j
] = (png_byte
)(255 - info_ptr
->trans
[j
]);
151 png_write_tRNS(png_ptr
, info_ptr
->trans
, &(info_ptr
->trans_values
),
152 info_ptr
->num_trans
, info_ptr
->color_type
);
155 #if defined(PNG_WRITE_bKGD_SUPPORTED)
156 if (info_ptr
->valid
& PNG_INFO_bKGD
)
157 png_write_bKGD(png_ptr
, &(info_ptr
->background
), info_ptr
->color_type
);
159 #if defined(PNG_WRITE_hIST_SUPPORTED)
160 if (info_ptr
->valid
& PNG_INFO_hIST
)
161 png_write_hIST(png_ptr
, info_ptr
->hist
, info_ptr
->num_palette
);
163 #if defined(PNG_WRITE_oFFs_SUPPORTED)
164 if (info_ptr
->valid
& PNG_INFO_oFFs
)
165 png_write_oFFs(png_ptr
, info_ptr
->x_offset
, info_ptr
->y_offset
,
166 info_ptr
->offset_unit_type
);
168 #if defined(PNG_WRITE_pCAL_SUPPORTED)
169 if (info_ptr
->valid
& PNG_INFO_pCAL
)
170 png_write_pCAL(png_ptr
, info_ptr
->pcal_purpose
, info_ptr
->pcal_X0
,
171 info_ptr
->pcal_X1
, info_ptr
->pcal_type
, info_ptr
->pcal_nparams
,
172 info_ptr
->pcal_units
, info_ptr
->pcal_params
);
174 #if defined(PNG_WRITE_sCAL_SUPPORTED)
175 if (info_ptr
->valid
& PNG_INFO_sCAL
)
176 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
177 png_write_sCAL(png_ptr
, (int)info_ptr
->scal_unit
,
178 info_ptr
->scal_pixel_width
, info_ptr
->scal_pixel_height
);
180 #ifdef PNG_FIXED_POINT_SUPPORTED
181 png_write_sCAL_s(png_ptr
, (int)info_ptr
->scal_unit
,
182 info_ptr
->scal_s_width
, info_ptr
->scal_s_height
);
185 "png_write_sCAL not supported; sCAL chunk not written.\n");
189 #if defined(PNG_WRITE_pHYs_SUPPORTED)
190 if (info_ptr
->valid
& PNG_INFO_pHYs
)
191 png_write_pHYs(png_ptr
, info_ptr
->x_pixels_per_unit
,
192 info_ptr
->y_pixels_per_unit
, info_ptr
->phys_unit_type
);
194 #if defined(PNG_WRITE_tIME_SUPPORTED)
195 if (info_ptr
->valid
& PNG_INFO_tIME
)
197 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
198 png_ptr
->mode
|= PNG_WROTE_tIME
;
201 #if defined(PNG_WRITE_sPLT_SUPPORTED)
202 if (info_ptr
->valid
& PNG_INFO_sPLT
)
203 for (i
= 0; i
< (int)info_ptr
->splt_palettes_num
; i
++)
204 png_write_sPLT(png_ptr
, info_ptr
->splt_palettes
+ i
);
206 #if defined(PNG_WRITE_TEXT_SUPPORTED)
207 /* Check to see if we need to write text chunks */
208 for (i
= 0; i
< info_ptr
->num_text
; i
++)
210 png_debug2(2, "Writing header text chunk %d, type %d\n", i
,
211 info_ptr
->text
[i
].compression
);
212 /* an internationalized chunk? */
213 if (info_ptr
->text
[i
].compression
> 0)
215 #if defined(PNG_WRITE_iTXt_SUPPORTED)
216 /* write international chunk */
217 png_write_iTXt(png_ptr
,
218 info_ptr
->text
[i
].compression
,
219 info_ptr
->text
[i
].key
,
220 info_ptr
->text
[i
].lang
,
221 info_ptr
->text
[i
].lang_key
,
222 info_ptr
->text
[i
].text
);
224 png_warning(png_ptr
, "Unable to write international text\n");
226 /* Mark this chunk as written */
227 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
229 /* If we want a compressed text chunk */
230 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_zTXt
)
232 #if defined(PNG_WRITE_zTXt_SUPPORTED)
233 /* write compressed chunk */
234 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
235 info_ptr
->text
[i
].text
, 0,
236 info_ptr
->text
[i
].compression
);
238 png_warning(png_ptr
, "Unable to write compressed text\n");
240 /* Mark this chunk as written */
241 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
243 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
245 #if defined(PNG_WRITE_tEXt_SUPPORTED)
246 /* write uncompressed chunk */
247 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
248 info_ptr
->text
[i
].text
,
251 png_warning(png_ptr
, "Unable to write uncompressed text\n");
253 /* Mark this chunk as written */
254 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
258 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
259 if (info_ptr
->unknown_chunks_num
)
261 png_unknown_chunk
*up
;
263 png_debug(5, "writing extra chunks\n");
265 for (up
= info_ptr
->unknown_chunks
;
266 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
269 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
270 if (keep
!= HANDLE_CHUNK_NEVER
&&
271 up
->location
&& (up
->location
& PNG_HAVE_PLTE
) &&
272 !(up
->location
& PNG_HAVE_IDAT
) &&
273 ((up
->name
[3] & 0x20) || keep
== HANDLE_CHUNK_ALWAYS
||
274 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
276 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
283 /* Writes the end of the PNG file. If you don't want to write comments or
284 * time information, you can pass NULL for info. If you already wrote these
285 * in png_write_info(), do not write them again here. If you have long
286 * comments, I suggest writing them here, and compressing them.
289 png_write_end(png_structp png_ptr
, png_infop info_ptr
)
291 png_debug(1, "in png_write_end\n");
292 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
293 png_error(png_ptr
, "No IDATs written into file");
295 /* see if user wants us to write information chunks */
296 if (info_ptr
!= NULL
)
298 #if defined(PNG_WRITE_TEXT_SUPPORTED)
299 int i
; /* local index variable */
301 #if defined(PNG_WRITE_tIME_SUPPORTED)
302 /* check to see if user has supplied a time chunk */
303 if ((info_ptr
->valid
& PNG_INFO_tIME
) &&
304 !(png_ptr
->mode
& PNG_WROTE_tIME
))
305 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
307 #if defined(PNG_WRITE_TEXT_SUPPORTED)
308 /* loop through comment chunks */
309 for (i
= 0; i
< info_ptr
->num_text
; i
++)
311 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i
,
312 info_ptr
->text
[i
].compression
);
313 /* an internationalized chunk? */
314 if (info_ptr
->text
[i
].compression
> 0)
316 #if defined(PNG_WRITE_iTXt_SUPPORTED)
317 /* write international chunk */
318 png_write_iTXt(png_ptr
,
319 info_ptr
->text
[i
].compression
,
320 info_ptr
->text
[i
].key
,
321 info_ptr
->text
[i
].lang
,
322 info_ptr
->text
[i
].lang_key
,
323 info_ptr
->text
[i
].text
);
325 png_warning(png_ptr
, "Unable to write international text\n");
327 /* Mark this chunk as written */
328 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
330 else if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
332 #if defined(PNG_WRITE_zTXt_SUPPORTED)
333 /* write compressed chunk */
334 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
335 info_ptr
->text
[i
].text
, 0,
336 info_ptr
->text
[i
].compression
);
338 png_warning(png_ptr
, "Unable to write compressed text\n");
340 /* Mark this chunk as written */
341 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
343 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
345 #if defined(PNG_WRITE_tEXt_SUPPORTED)
346 /* write uncompressed chunk */
347 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
348 info_ptr
->text
[i
].text
, 0);
350 png_warning(png_ptr
, "Unable to write uncompressed text\n");
353 /* Mark this chunk as written */
354 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
358 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
359 if (info_ptr
->unknown_chunks_num
)
361 png_unknown_chunk
*up
;
363 png_debug(5, "writing extra chunks\n");
365 for (up
= info_ptr
->unknown_chunks
;
366 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
369 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
370 if (keep
!= HANDLE_CHUNK_NEVER
&&
371 up
->location
&& (up
->location
& PNG_AFTER_IDAT
) &&
372 ((up
->name
[3] & 0x20) || keep
== HANDLE_CHUNK_ALWAYS
||
373 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
375 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
382 png_ptr
->mode
|= PNG_AFTER_IDAT
;
384 /* write end of PNG file */
385 png_write_IEND(png_ptr
);
387 /* This flush, added in libpng-1.0.8, causes some applications to crash
388 because they do not set png_ptr->output_flush_fn */
393 #if defined(PNG_WRITE_tIME_SUPPORTED)
394 #if !defined(_WIN32_WCE)
395 /* "time.h" functions are not supported on WindowsCE */
397 png_convert_from_struct_tm(png_timep ptime
, struct tm FAR
* ttime
)
399 png_debug(1, "in png_convert_from_struct_tm\n");
400 ptime
->year
= (png_uint_16
)(1900 + ttime
->tm_year
);
401 ptime
->month
= (png_byte
)(ttime
->tm_mon
+ 1);
402 ptime
->day
= (png_byte
)ttime
->tm_mday
;
403 ptime
->hour
= (png_byte
)ttime
->tm_hour
;
404 ptime
->minute
= (png_byte
)ttime
->tm_min
;
405 ptime
->second
= (png_byte
)ttime
->tm_sec
;
409 png_convert_from_time_t(png_timep ptime
, time_t ttime
)
413 png_debug(1, "in png_convert_from_time_t\n");
414 tbuf
= gmtime(&ttime
);
415 png_convert_from_struct_tm(ptime
, tbuf
);
420 /* Initialize png_ptr structure, and allocate any memory needed */
422 png_create_write_struct(png_const_charp user_png_ver
, png_voidp error_ptr
,
423 png_error_ptr error_fn
, png_error_ptr warn_fn
)
425 #ifdef PNG_USER_MEM_SUPPORTED
426 return (png_create_write_struct_2(user_png_ver
, error_ptr
, error_fn
,
427 warn_fn
, png_voidp_NULL
, png_malloc_ptr_NULL
, png_free_ptr_NULL
));
430 /* Alternate initialize png_ptr structure, and allocate any memory needed */
432 png_create_write_struct_2(png_const_charp user_png_ver
, png_voidp error_ptr
,
433 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
434 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
)
436 #endif /* PNG_USER_MEM_SUPPORTED */
438 #ifdef PNG_SETJMP_SUPPORTED
439 #ifdef USE_FAR_KEYWORD
444 png_debug(1, "in png_create_write_struct\n");
445 #ifdef PNG_USER_MEM_SUPPORTED
446 png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
447 (png_malloc_ptr
)malloc_fn
, (png_voidp
)mem_ptr
);
449 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
450 #endif /* PNG_USER_MEM_SUPPORTED */
454 #if !defined(PNG_1_0_X)
455 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
456 png_init_mmx_flags(png_ptr
); /* 1.2.0 addition */
458 #endif /* PNG_1_0_X */
460 #ifdef PNG_SETJMP_SUPPORTED
461 #ifdef USE_FAR_KEYWORD
464 if (setjmp(png_ptr
->jmpbuf
))
467 png_free(png_ptr
, png_ptr
->zbuf
);
469 png_destroy_struct(png_ptr
);
472 #ifdef USE_FAR_KEYWORD
473 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,sizeof(jmp_buf));
477 #ifdef PNG_USER_MEM_SUPPORTED
478 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
479 #endif /* PNG_USER_MEM_SUPPORTED */
480 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
485 if(user_png_ver
[i
] != png_libpng_ver
[i
])
486 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
487 } while (png_libpng_ver
[i
++]);
489 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
491 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
492 * we must recompile any applications that use any older library version.
493 * For versions after libpng 1.0, we will be compatible, so we need
494 * only check the first digit.
496 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
497 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
498 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
500 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
504 sprintf(msg
, "Application was compiled with png.h from libpng-%.20s",
506 png_warning(png_ptr
, msg
);
508 sprintf(msg
, "Application is running with png.c from libpng-%.20s",
510 png_warning(png_ptr
, msg
);
512 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
516 "Incompatible libpng version in application and library");
520 /* initialize zbuf - compression buffer */
521 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
522 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
523 (png_uint_32
)png_ptr
->zbuf_size
);
525 png_set_write_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
,
528 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
529 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
530 1, png_doublep_NULL
, png_doublep_NULL
);
533 #ifdef PNG_SETJMP_SUPPORTED
534 /* Applications that neglect to set up their own setjmp() and then encounter
535 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
536 abort instead of returning. */
537 #ifdef USE_FAR_KEYWORD
540 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,sizeof(jmp_buf));
542 if (setjmp(png_ptr
->jmpbuf
))
549 /* Initialize png_ptr structure, and allocate any memory needed */
550 #undef png_write_init
552 png_write_init(png_structp png_ptr
)
554 /* We only come here via pre-1.0.7-compiled applications */
555 png_write_init_2(png_ptr
, "1.0.6 or earlier", 0, 0);
559 png_write_init_2(png_structp png_ptr
, png_const_charp user_png_ver
,
560 png_size_t png_struct_size
, png_size_t png_info_size
)
562 /* We only come here via pre-1.0.12-compiled applications */
563 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
564 if(sizeof(png_struct
) > png_struct_size
|| sizeof(png_info
) > png_info_size
)
567 png_ptr
->warning_fn
=NULL
;
570 sprintf(msg
, "Application was compiled with png.h from libpng-%.20s",
572 png_warning(png_ptr
, msg
);
574 sprintf(msg
, "Application is running with png.c from libpng-%.20s",
576 png_warning(png_ptr
, msg
);
579 if(sizeof(png_struct
) > png_struct_size
)
581 png_ptr
->error_fn
=NULL
;
582 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
586 "The png struct allocated by the application for writing is too small.");
588 if(sizeof(png_info
) > png_info_size
)
590 png_ptr
->error_fn
=NULL
;
591 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
595 "The info struct allocated by the application for writing is too small.");
597 png_write_init_3(&png_ptr
, user_png_ver
, png_struct_size
);
602 png_write_init_3(png_structpp ptr_ptr
, png_const_charp user_png_ver
,
603 png_size_t png_struct_size
)
605 png_structp png_ptr
=*ptr_ptr
;
606 #ifdef PNG_SETJMP_SUPPORTED
607 jmp_buf tmp_jmp
; /* to save current jump buffer */
612 if (user_png_ver
[i
] != png_libpng_ver
[i
])
614 #ifdef PNG_LEGACY_SUPPORTED
615 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
617 png_ptr
->warning_fn
=NULL
;
619 "Application uses deprecated png_write_init() and should be recompiled.");
623 } while (png_libpng_ver
[i
++]);
625 png_debug(1, "in png_write_init_3\n");
627 #ifdef PNG_SETJMP_SUPPORTED
628 /* save jump buffer and error functions */
629 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
632 if (sizeof(png_struct
) > png_struct_size
)
634 png_destroy_struct(png_ptr
);
635 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
639 /* reset all variables to 0 */
640 png_memset(png_ptr
, 0, sizeof (png_struct
));
642 #if !defined(PNG_1_0_X)
643 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
644 png_init_mmx_flags(png_ptr
); /* 1.2.0 addition */
646 #endif /* PNG_1_0_X */
648 #ifdef PNG_SETJMP_SUPPORTED
649 /* restore jump buffer */
650 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
653 png_set_write_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
,
656 /* initialize zbuf - compression buffer */
657 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
658 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
659 (png_uint_32
)png_ptr
->zbuf_size
);
661 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
662 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
663 1, png_doublep_NULL
, png_doublep_NULL
);
667 /* Write a few rows of image data. If the image is interlaced,
668 * either you will have to write the 7 sub images, or, if you
669 * have called png_set_interlace_handling(), you will have to
670 * "write" the image seven times.
673 png_write_rows(png_structp png_ptr
, png_bytepp row
,
674 png_uint_32 num_rows
)
676 png_uint_32 i
; /* row counter */
677 png_bytepp rp
; /* row pointer */
679 png_debug(1, "in png_write_rows\n");
680 /* loop through the rows */
681 for (i
= 0, rp
= row
; i
< num_rows
; i
++, rp
++)
683 png_write_row(png_ptr
, *rp
);
687 /* Write the image. You only need to call this function once, even
688 * if you are writing an interlaced image.
691 png_write_image(png_structp png_ptr
, png_bytepp image
)
693 png_uint_32 i
; /* row index */
694 int pass
, num_pass
; /* pass variables */
695 png_bytepp rp
; /* points to current row */
697 png_debug(1, "in png_write_image\n");
698 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
699 /* intialize interlace handling. If image is not interlaced,
700 this will set pass to 1 */
701 num_pass
= png_set_interlace_handling(png_ptr
);
705 /* loop through passes */
706 for (pass
= 0; pass
< num_pass
; pass
++)
708 /* loop through image */
709 for (i
= 0, rp
= image
; i
< png_ptr
->height
; i
++, rp
++)
711 png_write_row(png_ptr
, *rp
);
716 /* called by user to write a row of image data */
718 png_write_row(png_structp png_ptr
, png_bytep row
)
720 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
721 png_ptr
->row_number
, png_ptr
->pass
);
722 /* initialize transformations and other stuff if first time */
723 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
725 /* make sure we wrote the header info */
726 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
728 "png_write_info was never called before png_write_row.");
730 /* check for transforms that have been set but were defined out */
731 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
732 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
733 png_warning(png_ptr
, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
735 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
736 if (png_ptr
->transformations
& PNG_FILLER
)
737 png_warning(png_ptr
, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
739 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
740 if (png_ptr
->transformations
& PNG_PACKSWAP
)
741 png_warning(png_ptr
, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
743 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
744 if (png_ptr
->transformations
& PNG_PACK
)
745 png_warning(png_ptr
, "PNG_WRITE_PACK_SUPPORTED is not defined.");
747 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
748 if (png_ptr
->transformations
& PNG_SHIFT
)
749 png_warning(png_ptr
, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
751 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
752 if (png_ptr
->transformations
& PNG_BGR
)
753 png_warning(png_ptr
, "PNG_WRITE_BGR_SUPPORTED is not defined.");
755 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
756 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
757 png_warning(png_ptr
, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
760 png_write_start_row(png_ptr
);
763 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
764 /* if interlaced and not interested in row, return */
765 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
767 switch (png_ptr
->pass
)
770 if (png_ptr
->row_number
& 0x07)
772 png_write_finish_row(png_ptr
);
777 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
779 png_write_finish_row(png_ptr
);
784 if ((png_ptr
->row_number
& 0x07) != 4)
786 png_write_finish_row(png_ptr
);
791 if ((png_ptr
->row_number
& 0x03) || png_ptr
->width
< 3)
793 png_write_finish_row(png_ptr
);
798 if ((png_ptr
->row_number
& 0x03) != 2)
800 png_write_finish_row(png_ptr
);
805 if ((png_ptr
->row_number
& 0x01) || png_ptr
->width
< 2)
807 png_write_finish_row(png_ptr
);
812 if (!(png_ptr
->row_number
& 0x01))
814 png_write_finish_row(png_ptr
);
822 /* set up row info for transformations */
823 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
824 png_ptr
->row_info
.width
= png_ptr
->usr_width
;
825 png_ptr
->row_info
.channels
= png_ptr
->usr_channels
;
826 png_ptr
->row_info
.bit_depth
= png_ptr
->usr_bit_depth
;
827 png_ptr
->row_info
.pixel_depth
= (png_byte
)(png_ptr
->row_info
.bit_depth
*
828 png_ptr
->row_info
.channels
);
830 png_ptr
->row_info
.rowbytes
= ((png_ptr
->row_info
.width
*
831 (png_uint_32
)png_ptr
->row_info
.pixel_depth
+ 7) >> 3);
833 png_debug1(3, "row_info->color_type = %d\n", png_ptr
->row_info
.color_type
);
834 png_debug1(3, "row_info->width = %lu\n", png_ptr
->row_info
.width
);
835 png_debug1(3, "row_info->channels = %d\n", png_ptr
->row_info
.channels
);
836 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr
->row_info
.bit_depth
);
837 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr
->row_info
.pixel_depth
);
838 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr
->row_info
.rowbytes
);
840 /* Copy user's row into buffer, leaving room for filter byte. */
841 png_memcpy_check(png_ptr
, png_ptr
->row_buf
+ 1, row
,
842 png_ptr
->row_info
.rowbytes
);
844 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
845 /* handle interlacing */
846 if (png_ptr
->interlaced
&& png_ptr
->pass
< 6 &&
847 (png_ptr
->transformations
& PNG_INTERLACE
))
849 png_do_write_interlace(&(png_ptr
->row_info
),
850 png_ptr
->row_buf
+ 1, png_ptr
->pass
);
851 /* this should always get caught above, but still ... */
852 if (!(png_ptr
->row_info
.width
))
854 png_write_finish_row(png_ptr
);
860 /* handle other transformations */
861 if (png_ptr
->transformations
)
862 png_do_write_transformations(png_ptr
);
864 #if defined(PNG_MNG_FEATURES_SUPPORTED)
865 /* Write filter_method 64 (intrapixel differencing) only if
866 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
867 * 2. Libpng did not write a PNG signature (this filter_method is only
868 * used in PNG datastreams that are embedded in MNG datastreams) and
869 * 3. The application called png_permit_mng_features with a mask that
870 * included PNG_FLAG_MNG_FILTER_64 and
871 * 4. The filter_method is 64 and
872 * 5. The color_type is RGB or RGBA
874 if((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
875 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
877 /* Intrapixel differencing */
878 png_do_write_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
882 /* Find a filter if necessary, filter the row and write it out. */
883 png_write_find_filter(png_ptr
, &(png_ptr
->row_info
));
885 if (png_ptr
->write_row_fn
!= NULL
)
886 (*(png_ptr
->write_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
889 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
890 /* Set the automatic flush interval or 0 to turn flushing off */
892 png_set_flush(png_structp png_ptr
, int nrows
)
894 png_debug(1, "in png_set_flush\n");
895 png_ptr
->flush_dist
= (nrows
< 0 ? 0 : nrows
);
898 /* flush the current output buffers now */
900 png_write_flush(png_structp png_ptr
)
904 png_debug(1, "in png_write_flush\n");
905 /* We have already written out all of the data */
906 if (png_ptr
->row_number
>= png_ptr
->num_rows
)
913 /* compress the data */
914 ret
= deflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
917 /* check for compression errors */
920 if (png_ptr
->zstream
.msg
!= NULL
)
921 png_error(png_ptr
, png_ptr
->zstream
.msg
);
923 png_error(png_ptr
, "zlib error");
926 if (!(png_ptr
->zstream
.avail_out
))
928 /* write the IDAT and reset the zlib output buffer */
929 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
931 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
932 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
935 } while(wrote_IDAT
== 1);
937 /* If there is any data left to be output, write it into a new IDAT */
938 if (png_ptr
->zbuf_size
!= png_ptr
->zstream
.avail_out
)
940 /* write the IDAT and reset the zlib output buffer */
941 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
942 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
943 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
944 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
946 png_ptr
->flush_rows
= 0;
949 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
951 /* free all memory used by the write */
953 png_destroy_write_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
)
955 png_structp png_ptr
= NULL
;
956 png_infop info_ptr
= NULL
;
957 #ifdef PNG_USER_MEM_SUPPORTED
958 png_free_ptr free_fn
= NULL
;
959 png_voidp mem_ptr
= NULL
;
962 png_debug(1, "in png_destroy_write_struct\n");
963 if (png_ptr_ptr
!= NULL
)
965 png_ptr
= *png_ptr_ptr
;
966 #ifdef PNG_USER_MEM_SUPPORTED
967 free_fn
= png_ptr
->free_fn
;
968 mem_ptr
= png_ptr
->mem_ptr
;
972 if (info_ptr_ptr
!= NULL
)
973 info_ptr
= *info_ptr_ptr
;
975 if (info_ptr
!= NULL
)
977 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ALL
, -1);
979 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
980 if (png_ptr
->num_chunk_list
)
982 png_free(png_ptr
, png_ptr
->chunk_list
);
983 png_ptr
->chunk_list
=NULL
;
984 png_ptr
->num_chunk_list
=0;
988 #ifdef PNG_USER_MEM_SUPPORTED
989 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
992 png_destroy_struct((png_voidp
)info_ptr
);
994 *info_ptr_ptr
= NULL
;
999 png_write_destroy(png_ptr
);
1000 #ifdef PNG_USER_MEM_SUPPORTED
1001 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1002 (png_voidp
)mem_ptr
);
1004 png_destroy_struct((png_voidp
)png_ptr
);
1006 *png_ptr_ptr
= NULL
;
1011 /* Free any memory used in png_ptr struct (old method) */
1013 png_write_destroy(png_structp png_ptr
)
1015 #ifdef PNG_SETJMP_SUPPORTED
1016 jmp_buf tmp_jmp
; /* save jump buffer */
1018 png_error_ptr error_fn
;
1019 png_error_ptr warning_fn
;
1020 png_voidp error_ptr
;
1021 #ifdef PNG_USER_MEM_SUPPORTED
1022 png_free_ptr free_fn
;
1025 png_debug(1, "in png_write_destroy\n");
1026 /* free any memory zlib uses */
1027 deflateEnd(&png_ptr
->zstream
);
1029 /* free our memory. png_free checks NULL for us. */
1030 png_free(png_ptr
, png_ptr
->zbuf
);
1031 png_free(png_ptr
, png_ptr
->row_buf
);
1032 png_free(png_ptr
, png_ptr
->prev_row
);
1033 png_free(png_ptr
, png_ptr
->sub_row
);
1034 png_free(png_ptr
, png_ptr
->up_row
);
1035 png_free(png_ptr
, png_ptr
->avg_row
);
1036 png_free(png_ptr
, png_ptr
->paeth_row
);
1038 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1039 png_free(png_ptr
, png_ptr
->time_buffer
);
1042 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1043 png_free(png_ptr
, png_ptr
->prev_filters
);
1044 png_free(png_ptr
, png_ptr
->filter_weights
);
1045 png_free(png_ptr
, png_ptr
->inv_filter_weights
);
1046 png_free(png_ptr
, png_ptr
->filter_costs
);
1047 png_free(png_ptr
, png_ptr
->inv_filter_costs
);
1050 #ifdef PNG_SETJMP_SUPPORTED
1051 /* reset structure */
1052 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
1055 error_fn
= png_ptr
->error_fn
;
1056 warning_fn
= png_ptr
->warning_fn
;
1057 error_ptr
= png_ptr
->error_ptr
;
1058 #ifdef PNG_USER_MEM_SUPPORTED
1059 free_fn
= png_ptr
->free_fn
;
1062 png_memset(png_ptr
, 0, sizeof (png_struct
));
1064 png_ptr
->error_fn
= error_fn
;
1065 png_ptr
->warning_fn
= warning_fn
;
1066 png_ptr
->error_ptr
= error_ptr
;
1067 #ifdef PNG_USER_MEM_SUPPORTED
1068 png_ptr
->free_fn
= free_fn
;
1071 #ifdef PNG_SETJMP_SUPPORTED
1072 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
1076 /* Allow the application to select one or more row filters to use. */
1078 png_set_filter(png_structp png_ptr
, int method
, int filters
)
1080 png_debug(1, "in png_set_filter\n");
1081 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1082 if((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
1083 (method
== PNG_INTRAPIXEL_DIFFERENCING
))
1084 method
= PNG_FILTER_TYPE_BASE
;
1086 if (method
== PNG_FILTER_TYPE_BASE
)
1088 switch (filters
& (PNG_ALL_FILTERS
| 0x07))
1092 case 7: png_warning(png_ptr
, "Unknown row filter for method 0");
1093 case PNG_FILTER_VALUE_NONE
: png_ptr
->do_filter
=PNG_FILTER_NONE
; break;
1094 case PNG_FILTER_VALUE_SUB
: png_ptr
->do_filter
=PNG_FILTER_SUB
; break;
1095 case PNG_FILTER_VALUE_UP
: png_ptr
->do_filter
=PNG_FILTER_UP
; break;
1096 case PNG_FILTER_VALUE_AVG
: png_ptr
->do_filter
=PNG_FILTER_AVG
; break;
1097 case PNG_FILTER_VALUE_PAETH
: png_ptr
->do_filter
=PNG_FILTER_PAETH
;break;
1098 default: png_ptr
->do_filter
= (png_byte
)filters
; break;
1101 /* If we have allocated the row_buf, this means we have already started
1102 * with the image and we should have allocated all of the filter buffers
1103 * that have been selected. If prev_row isn't already allocated, then
1104 * it is too late to start using the filters that need it, since we
1105 * will be missing the data in the previous row. If an application
1106 * wants to start and stop using particular filters during compression,
1107 * it should start out with all of the filters, and then add and
1108 * remove them after the start of compression.
1110 if (png_ptr
->row_buf
!= NULL
)
1112 if ((png_ptr
->do_filter
& PNG_FILTER_SUB
) && png_ptr
->sub_row
== NULL
)
1114 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
1115 (png_ptr
->rowbytes
+ 1));
1116 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
1119 if ((png_ptr
->do_filter
& PNG_FILTER_UP
) && png_ptr
->up_row
== NULL
)
1121 if (png_ptr
->prev_row
== NULL
)
1123 png_warning(png_ptr
, "Can't add Up filter after starting");
1124 png_ptr
->do_filter
&= ~PNG_FILTER_UP
;
1128 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
1129 (png_ptr
->rowbytes
+ 1));
1130 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
1134 if ((png_ptr
->do_filter
& PNG_FILTER_AVG
) && png_ptr
->avg_row
== NULL
)
1136 if (png_ptr
->prev_row
== NULL
)
1138 png_warning(png_ptr
, "Can't add Average filter after starting");
1139 png_ptr
->do_filter
&= ~PNG_FILTER_AVG
;
1143 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
1144 (png_ptr
->rowbytes
+ 1));
1145 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
1149 if ((png_ptr
->do_filter
& PNG_FILTER_PAETH
) &&
1150 png_ptr
->paeth_row
== NULL
)
1152 if (png_ptr
->prev_row
== NULL
)
1154 png_warning(png_ptr
, "Can't add Paeth filter after starting");
1155 png_ptr
->do_filter
&= (png_byte
)(~PNG_FILTER_PAETH
);
1159 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
1160 (png_ptr
->rowbytes
+ 1));
1161 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
1165 if (png_ptr
->do_filter
== PNG_NO_FILTERS
)
1166 png_ptr
->do_filter
= PNG_FILTER_NONE
;
1170 png_error(png_ptr
, "Unknown custom filter method");
1173 /* This allows us to influence the way in which libpng chooses the "best"
1174 * filter for the current scanline. While the "minimum-sum-of-absolute-
1175 * differences metric is relatively fast and effective, there is some
1176 * question as to whether it can be improved upon by trying to keep the
1177 * filtered data going to zlib more consistent, hopefully resulting in
1178 * better compression.
1180 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1182 png_set_filter_heuristics(png_structp png_ptr
, int heuristic_method
,
1183 int num_weights
, png_doublep filter_weights
,
1184 png_doublep filter_costs
)
1188 png_debug(1, "in png_set_filter_heuristics\n");
1189 if (heuristic_method
>= PNG_FILTER_HEURISTIC_LAST
)
1191 png_warning(png_ptr
, "Unknown filter heuristic method");
1195 if (heuristic_method
== PNG_FILTER_HEURISTIC_DEFAULT
)
1197 heuristic_method
= PNG_FILTER_HEURISTIC_UNWEIGHTED
;
1200 if (num_weights
< 0 || filter_weights
== NULL
||
1201 heuristic_method
== PNG_FILTER_HEURISTIC_UNWEIGHTED
)
1206 png_ptr
->num_prev_filters
= (png_byte
)num_weights
;
1207 png_ptr
->heuristic_method
= (png_byte
)heuristic_method
;
1209 if (num_weights
> 0)
1211 if (png_ptr
->prev_filters
== NULL
)
1213 png_ptr
->prev_filters
= (png_bytep
)png_malloc(png_ptr
,
1214 (png_uint_32
)(sizeof(png_byte
) * num_weights
));
1216 /* To make sure that the weighting starts out fairly */
1217 for (i
= 0; i
< num_weights
; i
++)
1219 png_ptr
->prev_filters
[i
] = 255;
1223 if (png_ptr
->filter_weights
== NULL
)
1225 png_ptr
->filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1226 (png_uint_32
)(sizeof(png_uint_16
) * num_weights
));
1228 png_ptr
->inv_filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1229 (png_uint_32
)(sizeof(png_uint_16
) * num_weights
));
1230 for (i
= 0; i
< num_weights
; i
++)
1232 png_ptr
->inv_filter_weights
[i
] =
1233 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1237 for (i
= 0; i
< num_weights
; i
++)
1239 if (filter_weights
[i
] < 0.0)
1241 png_ptr
->inv_filter_weights
[i
] =
1242 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1246 png_ptr
->inv_filter_weights
[i
] =
1247 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
*filter_weights
[i
]+0.5);
1248 png_ptr
->filter_weights
[i
] =
1249 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
/filter_weights
[i
]+0.5);
1254 /* If, in the future, there are other filter methods, this would
1255 * need to be based on png_ptr->filter.
1257 if (png_ptr
->filter_costs
== NULL
)
1259 png_ptr
->filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1260 (png_uint_32
)(sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1262 png_ptr
->inv_filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1263 (png_uint_32
)(sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1265 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1267 png_ptr
->inv_filter_costs
[i
] =
1268 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1272 /* Here is where we set the relative costs of the different filters. We
1273 * should take the desired compression level into account when setting
1274 * the costs, so that Paeth, for instance, has a high relative cost at low
1275 * compression levels, while it has a lower relative cost at higher
1276 * compression settings. The filter types are in order of increasing
1277 * relative cost, so it would be possible to do this with an algorithm.
1279 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1281 if (filter_costs
== NULL
|| filter_costs
[i
] < 0.0)
1283 png_ptr
->inv_filter_costs
[i
] =
1284 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1286 else if (filter_costs
[i
] >= 1.0)
1288 png_ptr
->inv_filter_costs
[i
] =
1289 (png_uint_16
)((double)PNG_COST_FACTOR
/ filter_costs
[i
] + 0.5);
1290 png_ptr
->filter_costs
[i
] =
1291 (png_uint_16
)((double)PNG_COST_FACTOR
* filter_costs
[i
] + 0.5);
1295 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1298 png_set_compression_level(png_structp png_ptr
, int level
)
1300 png_debug(1, "in png_set_compression_level\n");
1301 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_LEVEL
;
1302 png_ptr
->zlib_level
= level
;
1306 png_set_compression_mem_level(png_structp png_ptr
, int mem_level
)
1308 png_debug(1, "in png_set_compression_mem_level\n");
1309 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
;
1310 png_ptr
->zlib_mem_level
= mem_level
;
1314 png_set_compression_strategy(png_structp png_ptr
, int strategy
)
1316 png_debug(1, "in png_set_compression_strategy\n");
1317 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_STRATEGY
;
1318 png_ptr
->zlib_strategy
= strategy
;
1322 png_set_compression_window_bits(png_structp png_ptr
, int window_bits
)
1324 if (window_bits
> 15)
1325 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1326 else if (window_bits
< 8)
1327 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1329 /* avoid libpng bug with 256-byte windows */
1330 if (window_bits
== 8)
1332 png_warning(png_ptr
, "Compression window is being reset to 512");
1336 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
;
1337 png_ptr
->zlib_window_bits
= window_bits
;
1341 png_set_compression_method(png_structp png_ptr
, int method
)
1343 png_debug(1, "in png_set_compression_method\n");
1345 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1346 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_METHOD
;
1347 png_ptr
->zlib_method
= method
;
1351 png_set_write_status_fn(png_structp png_ptr
, png_write_status_ptr write_row_fn
)
1353 png_ptr
->write_row_fn
= write_row_fn
;
1356 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1358 png_set_write_user_transform_fn(png_structp png_ptr
, png_user_transform_ptr
1359 write_user_transform_fn
)
1361 png_debug(1, "in png_set_write_user_transform_fn\n");
1362 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
1363 png_ptr
->write_user_transform_fn
= write_user_transform_fn
;
1368 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1370 png_write_png(png_structp png_ptr
, png_infop info_ptr
,
1371 int transforms
, voidp params
)
1373 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1374 /* invert the alpha channel from opacity to transparency */
1375 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1376 png_set_invert_alpha(png_ptr
);
1379 /* Write the file header information. */
1380 png_write_info(png_ptr
, info_ptr
);
1382 /* ------ these transformations don't touch the info structure ------- */
1384 #if defined(PNG_WRITE_INVERT_SUPPORTED)
1385 /* invert monochrome pixels */
1386 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1387 png_set_invert_mono(png_ptr
);
1390 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
1391 /* Shift the pixels up to a legal bit depth and fill in
1392 * as appropriate to correctly scale the image.
1394 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1395 && (info_ptr
->valid
& PNG_INFO_sBIT
))
1396 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1399 #if defined(PNG_WRITE_PACK_SUPPORTED)
1400 /* pack pixels into bytes */
1401 if (transforms
& PNG_TRANSFORM_PACKING
)
1402 png_set_packing(png_ptr
);
1405 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1406 /* swap location of alpha bytes from ARGB to RGBA */
1407 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1408 png_set_swap_alpha(png_ptr
);
1411 #if defined(PNG_WRITE_FILLER_SUPPORTED)
1412 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1413 * RGB (4 channels -> 3 channels). The second parameter is not used.
1415 if (transforms
& PNG_TRANSFORM_STRIP_FILLER
)
1416 png_set_filler(png_ptr
, 0, PNG_FILLER_BEFORE
);
1419 #if defined(PNG_WRITE_BGR_SUPPORTED)
1420 /* flip BGR pixels to RGB */
1421 if (transforms
& PNG_TRANSFORM_BGR
)
1422 png_set_bgr(png_ptr
);
1425 #if defined(PNG_WRITE_SWAP_SUPPORTED)
1426 /* swap bytes of 16-bit files to most significant byte first */
1427 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1428 png_set_swap(png_ptr
);
1431 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1432 /* swap bits of 1, 2, 4 bit packed pixel formats */
1433 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1434 png_set_packswap(png_ptr
);
1437 /* ----------------------- end of transformations ------------------- */
1439 /* write the bits */
1440 if (info_ptr
->valid
& PNG_INFO_IDAT
)
1441 png_write_image(png_ptr
, info_ptr
->row_pointers
);
1443 /* It is REQUIRED to call this to finish writing the rest of the file */
1444 png_write_end(png_ptr
, info_ptr
);
1446 if(transforms
== 0 || params
== NULL
)
1447 /* quiet compiler warnings */ return;
1450 #endif /* PNG_WRITE_SUPPORTED */