2 /* pngwrite.c - general routines to write a PNG file
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, Glenn Randers-Pehrson
12 /* get internal access to png.h */
14 #include "../png/png.h"
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 encurage
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(png_structp png_ptr
, png_infop info_ptr
)
28 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
32 png_debug(1, "in png_write_info\n");
33 png_write_sig(png_ptr
); /* write PNG signature */
34 /* write IHDR information. */
35 png_write_IHDR(png_ptr
, info_ptr
->width
, info_ptr
->height
,
36 info_ptr
->bit_depth
, info_ptr
->color_type
, info_ptr
->compression_type
,
37 info_ptr
->filter_type
,
38 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
39 info_ptr
->interlace_type
);
43 /* the rest of these check to see if the valid field has the appropriate
44 flag set, and if it does, writes the chunk. */
45 #if defined(PNG_WRITE_gAMA_SUPPORTED)
46 if (info_ptr
->valid
& PNG_INFO_gAMA
)
47 png_write_gAMA(png_ptr
, info_ptr
->gamma
);
49 #if defined(PNG_WRITE_sRGB_SUPPORTED)
50 if (info_ptr
->valid
& PNG_INFO_sRGB
)
51 png_write_sRGB(png_ptr
, (int)info_ptr
->srgb_intent
);
53 #if defined(PNG_WRITE_sBIT_SUPPORTED)
54 if (info_ptr
->valid
& PNG_INFO_sBIT
)
55 png_write_sBIT(png_ptr
, &(info_ptr
->sig_bit
), info_ptr
->color_type
);
57 #if defined(PNG_WRITE_cHRM_SUPPORTED)
58 if (info_ptr
->valid
& PNG_INFO_cHRM
)
59 png_write_cHRM(png_ptr
,
60 info_ptr
->x_white
, info_ptr
->y_white
,
61 info_ptr
->x_red
, info_ptr
->y_red
,
62 info_ptr
->x_green
, info_ptr
->y_green
,
63 info_ptr
->x_blue
, info_ptr
->y_blue
);
65 if (info_ptr
->valid
& PNG_INFO_PLTE
)
66 png_write_PLTE(png_ptr
, info_ptr
->palette
,
67 (png_uint_32
)info_ptr
->num_palette
);
68 else if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
69 png_error(png_ptr
, "Valid palette required for paletted images\n");
71 #if defined(PNG_WRITE_tRNS_SUPPORTED)
72 if (info_ptr
->valid
& PNG_INFO_tRNS
)
74 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
75 /* invert the alpha channel (in tRNS) */
76 if (png_ptr
->transformations
& PNG_INVERT_ALPHA
&&
77 info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
80 for (j
=0; j
<(int)info_ptr
->num_trans
; j
++)
81 info_ptr
->trans
[j
] = 255 - info_ptr
->trans
[j
];
84 png_write_tRNS(png_ptr
, info_ptr
->trans
, &(info_ptr
->trans_values
),
85 info_ptr
->num_trans
, info_ptr
->color_type
);
88 #if defined(PNG_WRITE_bKGD_SUPPORTED)
89 if (info_ptr
->valid
& PNG_INFO_bKGD
)
90 png_write_bKGD(png_ptr
, &(info_ptr
->background
), info_ptr
->color_type
);
92 #if defined(PNG_WRITE_hIST_SUPPORTED)
93 if (info_ptr
->valid
& PNG_INFO_hIST
)
94 png_write_hIST(png_ptr
, info_ptr
->hist
, info_ptr
->num_palette
);
96 #if defined(PNG_WRITE_oFFs_SUPPORTED)
97 if (info_ptr
->valid
& PNG_INFO_oFFs
)
98 png_write_oFFs(png_ptr
, info_ptr
->x_offset
, info_ptr
->y_offset
,
99 info_ptr
->offset_unit_type
);
101 #if defined(PNG_WRITE_pCAL_SUPPORTED)
102 if (info_ptr
->valid
& PNG_INFO_pCAL
)
103 png_write_pCAL(png_ptr
, info_ptr
->pcal_purpose
, info_ptr
->pcal_X0
,
104 info_ptr
->pcal_X1
, info_ptr
->pcal_type
, info_ptr
->pcal_nparams
,
105 info_ptr
->pcal_units
, info_ptr
->pcal_params
);
107 #if defined(PNG_WRITE_pHYs_SUPPORTED)
108 if (info_ptr
->valid
& PNG_INFO_pHYs
)
109 png_write_pHYs(png_ptr
, info_ptr
->x_pixels_per_unit
,
110 info_ptr
->y_pixels_per_unit
, info_ptr
->phys_unit_type
);
112 #if defined(PNG_WRITE_tIME_SUPPORTED)
113 if (info_ptr
->valid
& PNG_INFO_tIME
)
115 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
116 png_ptr
->flags
|= PNG_FLAG_WROTE_tIME
;
119 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
120 /* Check to see if we need to write text chunks */
121 for (i
= 0; i
< info_ptr
->num_text
; i
++)
123 png_debug2(2, "Writing header text chunk %d, type %d\n", i
,
124 info_ptr
->text
[i
].compression
);
125 /* If we want a compressed text chunk */
126 if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
128 #if defined(PNG_WRITE_zTXt_SUPPORTED)
129 /* write compressed chunk */
130 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
131 info_ptr
->text
[i
].text
, info_ptr
->text
[i
].text_length
,
132 info_ptr
->text
[i
].compression
);
134 png_warning(png_ptr
, "Unable to write compressed text\n");
136 /* Mark this chunk as written */
137 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
139 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
141 #if defined(PNG_WRITE_tEXt_SUPPORTED)
142 /* write uncompressed chunk */
143 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
144 info_ptr
->text
[i
].text
, info_ptr
->text
[i
].text_length
);
146 png_warning(png_ptr
, "Unable to write uncompressed text\n");
148 /* Mark this chunk as written */
149 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
155 /* Writes the end of the PNG file. If you don't want to write comments or
156 * time information, you can pass NULL for info. If you already wrote these
157 * in png_write_info(), do not write them again here. If you have long
158 * comments, I suggest writing them here, and compressing them.
161 png_write_end(png_structp png_ptr
, png_infop info_ptr
)
163 png_debug(1, "in png_write_end\n");
164 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
165 png_error(png_ptr
, "No IDATs written into file");
167 /* see if user wants us to write information chunks */
168 if (info_ptr
!= NULL
)
170 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
171 int i
; /* local index variable */
173 #if defined(PNG_WRITE_tIME_SUPPORTED)
174 /* check to see if user has supplied a time chunk */
175 if (info_ptr
->valid
& PNG_INFO_tIME
&&
176 !(png_ptr
->flags
& PNG_FLAG_WROTE_tIME
))
177 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
179 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
180 /* loop through comment chunks */
181 for (i
= 0; i
< info_ptr
->num_text
; i
++)
183 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i
,
184 info_ptr
->text
[i
].compression
);
185 if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
187 #if defined(PNG_WRITE_zTXt_SUPPORTED)
188 /* write compressed chunk */
189 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
190 info_ptr
->text
[i
].text
, info_ptr
->text
[i
].text_length
,
191 info_ptr
->text
[i
].compression
);
193 png_warning(png_ptr
, "Unable to write compressed text\n");
195 /* Mark this chunk as written */
196 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
198 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
200 #if defined(PNG_WRITE_tEXt_SUPPORTED)
201 /* write uncompressed chunk */
202 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
203 info_ptr
->text
[i
].text
, info_ptr
->text
[i
].text_length
);
205 png_warning(png_ptr
, "Unable to write uncompressed text\n");
208 /* Mark this chunk as written */
209 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
215 png_ptr
->mode
|= PNG_AFTER_IDAT
;
217 /* write end of PNG file */
218 png_write_IEND(png_ptr
);
221 #if defined(PNG_TIME_RFC1123_SUPPORTED)
222 /* Convert the supplied time into an RFC 1123 string suitable for use in
223 * a "Creation Time" or other text-based time string.
226 png_convert_to_rfc1123(png_structp png_ptr
, png_timep ptime
)
228 static PNG_CONST
char short_months
[12][4] =
229 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
230 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
232 if (png_ptr
->time_buffer
== NULL
)
234 png_ptr
->time_buffer
= (png_charp
)png_malloc(png_ptr
, (png_uint_32
)(29*
238 #ifdef USE_FAR_KEYWORD
240 char near_time_buf
[29];
241 sprintf(near_time_buf
, "%d %s %d %02d:%02d:%02d +0000",
242 ptime
->day
% 31, short_months
[ptime
->month
- 1],
243 ptime
->year
, ptime
->hour
% 24, ptime
->minute
% 60,
245 png_memcpy(png_ptr
->time_buffer
, near_time_buf
,
249 sprintf(png_ptr
->time_buffer
, "%d %s %d %02d:%02d:%02d +0000",
250 ptime
->day
% 31, short_months
[ptime
->month
- 1],
251 ptime
->year
, ptime
->hour
% 24, ptime
->minute
% 60,
254 return ((png_charp
)png_ptr
->time_buffer
);
256 #endif /* PNG_TIME_RFC1123_SUPPORTED */
258 #if defined(PNG_WRITE_tIME_SUPPORTED)
260 png_convert_from_struct_tm(png_timep ptime
, struct tm FAR
* ttime
)
262 png_debug(1, "in png_convert_from_struct_tm\n");
263 ptime
->year
= (png_uint_16
)(1900 + ttime
->tm_year
);
264 ptime
->month
= (png_byte
)(ttime
->tm_mon
+ 1);
265 ptime
->day
= (png_byte
)ttime
->tm_mday
;
266 ptime
->hour
= (png_byte
)ttime
->tm_hour
;
267 ptime
->minute
= (png_byte
)ttime
->tm_min
;
268 ptime
->second
= (png_byte
)ttime
->tm_sec
;
272 png_convert_from_time_t(png_timep ptime
, time_t ttime
)
276 png_debug(1, "in png_convert_from_time_t\n");
277 tbuf
= gmtime(&ttime
);
278 png_convert_from_struct_tm(ptime
, tbuf
);
282 /* Initialize png_ptr structure, and allocate any memory needed */
284 png_create_write_struct(png_const_charp user_png_ver
, voidp error_ptr
,
285 png_error_ptr error_fn
, png_error_ptr warn_fn
)
288 #ifdef USE_FAR_KEYWORD
291 png_debug(1, "in png_create_write_struct\n");
292 if ((png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
)) == NULL
)
294 return ((png_structp
)NULL
);
296 #ifdef USE_FAR_KEYWORD
299 if (setjmp(png_ptr
->jmpbuf
))
302 png_free(png_ptr
, png_ptr
->zbuf
);
303 png_destroy_struct(png_ptr
);
304 return ((png_structp
)NULL
);
306 #ifdef USE_FAR_KEYWORD
307 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,sizeof(jmp_buf));
309 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
311 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
312 * we must recompile any applications that use any older library version.
313 * For versions after libpng 1.0, we will be compatible, so we need
314 * only check the first digit.
316 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
317 (png_libpng_ver
[0] == '0' && user_png_ver
[2] < '9'))
320 "Incompatible libpng version in application and library");
323 /* initialize zbuf - compression buffer */
324 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
325 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
326 (png_uint_32
)png_ptr
->zbuf_size
);
328 png_set_write_fn(png_ptr
, NULL
, NULL
, NULL
);
330 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
331 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
335 return ((png_structp
)png_ptr
);
339 /* Initialize png_ptr structure, and allocate any memory needed */
341 png_write_init(png_structp png_ptr
)
343 jmp_buf tmp_jmp
; /* to save current jump buffer */
345 png_debug(1, "in png_write_init\n");
346 /* save jump buffer and error functions */
347 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
349 /* reset all variables to 0 */
350 png_memset(png_ptr
, 0, sizeof (png_struct
));
352 /* restore jump buffer */
353 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
355 /* initialize zbuf - compression buffer */
356 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
357 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
358 (png_uint_32
)png_ptr
->zbuf_size
);
359 png_set_write_fn(png_ptr
, NULL
, NULL
, NULL
);
361 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
362 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
367 /* Write a few rows of image data. If the image is interlaced,
368 * either you will have to write the 7 sub images, or, if you
369 * have called png_set_interlace_handling(), you will have to
370 * "write" the image seven times.
373 png_write_rows(png_structp png_ptr
, png_bytepp row
,
374 png_uint_32 num_rows
)
376 png_uint_32 i
; /* row counter */
377 png_bytepp rp
; /* row pointer */
379 png_debug(1, "in png_write_rows\n");
380 /* loop through the rows */
381 for (i
= 0, rp
= row
; i
< num_rows
; i
++, rp
++)
383 png_write_row(png_ptr
, *rp
);
387 /* Write the image. You only need to call this function once, even
388 * if you are writing an interlaced image.
391 png_write_image(png_structp png_ptr
, png_bytepp image
)
393 png_uint_32 i
; /* row index */
394 int pass
, num_pass
; /* pass variables */
395 png_bytepp rp
; /* points to current row */
397 png_debug(1, "in png_write_image\n");
398 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
399 /* intialize interlace handling. If image is not interlaced,
400 this will set pass to 1 */
401 num_pass
= png_set_interlace_handling(png_ptr
);
405 /* loop through passes */
406 for (pass
= 0; pass
< num_pass
; pass
++)
408 /* loop through image */
409 for (i
= 0, rp
= image
; i
< png_ptr
->height
; i
++, rp
++)
411 png_write_row(png_ptr
, *rp
);
416 /* called by user to write a row of image data */
418 png_write_row(png_structp png_ptr
, png_bytep row
)
420 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
421 png_ptr
->row_number
, png_ptr
->pass
);
422 /* initialize transformations and other stuff if first time */
423 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
425 png_write_start_row(png_ptr
);
428 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
429 /* if interlaced and not interested in row, return */
430 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
432 switch (png_ptr
->pass
)
435 if (png_ptr
->row_number
& 7)
437 png_write_finish_row(png_ptr
);
442 if ((png_ptr
->row_number
& 7) || png_ptr
->width
< 5)
444 png_write_finish_row(png_ptr
);
449 if ((png_ptr
->row_number
& 7) != 4)
451 png_write_finish_row(png_ptr
);
456 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
458 png_write_finish_row(png_ptr
);
463 if ((png_ptr
->row_number
& 3) != 2)
465 png_write_finish_row(png_ptr
);
470 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
472 png_write_finish_row(png_ptr
);
477 if (!(png_ptr
->row_number
& 1))
479 png_write_finish_row(png_ptr
);
487 /* set up row info for transformations */
488 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
489 png_ptr
->row_info
.width
= png_ptr
->usr_width
;
490 png_ptr
->row_info
.channels
= png_ptr
->usr_channels
;
491 png_ptr
->row_info
.bit_depth
= png_ptr
->usr_bit_depth
;
492 png_ptr
->row_info
.pixel_depth
= (png_byte
)(png_ptr
->row_info
.bit_depth
*
493 png_ptr
->row_info
.channels
);
495 png_ptr
->row_info
.rowbytes
= ((png_ptr
->row_info
.width
*
496 (png_uint_32
)png_ptr
->row_info
.pixel_depth
+ 7) >> 3);
498 png_debug1(3, "row_info->color_type = %d\n", png_ptr
->row_info
.color_type
);
499 png_debug1(3, "row_info->width = %d\n", png_ptr
->row_info
.width
);
500 png_debug1(3, "row_info->channels = %d\n", png_ptr
->row_info
.channels
);
501 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr
->row_info
.bit_depth
);
502 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr
->row_info
.pixel_depth
);
503 png_debug1(3, "row_info->rowbytes = %d\n", png_ptr
->row_info
.rowbytes
);
505 /* Copy user's row into buffer, leaving room for filter byte. */
506 png_memcpy_check(png_ptr
, png_ptr
->row_buf
+ 1, row
,
507 png_ptr
->row_info
.rowbytes
);
509 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
510 /* handle interlacing */
511 if (png_ptr
->interlaced
&& png_ptr
->pass
< 6 &&
512 (png_ptr
->transformations
& PNG_INTERLACE
))
514 png_do_write_interlace(&(png_ptr
->row_info
),
515 png_ptr
->row_buf
+ 1, png_ptr
->pass
);
516 /* this should always get caught above, but still ... */
517 if (!(png_ptr
->row_info
.width
))
519 png_write_finish_row(png_ptr
);
525 /* handle other transformations */
526 if (png_ptr
->transformations
)
527 png_do_write_transformations(png_ptr
);
529 /* Find a filter if necessary, filter the row and write it out. */
530 png_write_find_filter(png_ptr
, &(png_ptr
->row_info
));
532 if (png_ptr
->write_row_fn
!= NULL
)
533 (*(png_ptr
->write_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
536 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
537 /* Set the automatic flush interval or 0 to turn flushing off */
539 png_set_flush(png_structp png_ptr
, int nrows
)
541 png_debug(1, "in png_set_flush\n");
542 png_ptr
->flush_dist
= (nrows
< 0 ? 0 : nrows
);
545 /* flush the current output buffers now */
547 png_write_flush(png_structp png_ptr
)
551 png_debug(1, "in png_write_flush\n");
552 /* We have already written out all of the data */
553 if (png_ptr
->row_number
>= png_ptr
->num_rows
)
560 /* compress the data */
561 ret
= deflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
564 /* check for compression errors */
567 if (png_ptr
->zstream
.msg
!= NULL
)
568 png_error(png_ptr
, png_ptr
->zstream
.msg
);
570 png_error(png_ptr
, "zlib error");
573 if (!(png_ptr
->zstream
.avail_out
))
575 /* write the IDAT and reset the zlib output buffer */
576 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
578 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
579 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
582 } while(wrote_IDAT
== 1);
584 /* If there is any data left to be output, write it into a new IDAT */
585 if (png_ptr
->zbuf_size
!= png_ptr
->zstream
.avail_out
)
587 /* write the IDAT and reset the zlib output buffer */
588 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
589 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
590 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
591 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
593 png_ptr
->flush_rows
= 0;
596 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
598 /* free all memory used by the write */
600 png_destroy_write_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
)
602 png_structp png_ptr
= NULL
;
603 png_infop info_ptr
= NULL
;
605 png_debug(1, "in png_destroy_write_struct\n");
606 if (png_ptr_ptr
!= NULL
)
607 png_ptr
= *png_ptr_ptr
;
609 if (info_ptr_ptr
!= NULL
)
610 info_ptr
= *info_ptr_ptr
;
612 if (info_ptr
!= NULL
)
614 #ifdef PNG_WRITE_tEXt_SUPPORTED
615 png_free(png_ptr
, info_ptr
->text
);
617 #if defined(PNG_READ_pCAL_SUPPORTED)
618 png_free(png_ptr
, info_ptr
->pcal_purpose
);
619 png_free(png_ptr
, info_ptr
->pcal_units
);
620 if (info_ptr
->pcal_params
!= NULL
)
623 for (i
= 0; i
< (int)info_ptr
->pcal_nparams
; i
++)
625 png_free(png_ptr
, info_ptr
->pcal_params
[i
]);
627 png_free(png_ptr
, info_ptr
->pcal_params
);
630 png_destroy_struct((png_voidp
)info_ptr
);
631 *info_ptr_ptr
= (png_infop
)NULL
;
636 png_write_destroy(png_ptr
);
637 png_destroy_struct((png_voidp
)png_ptr
);
638 *png_ptr_ptr
= (png_structp
)NULL
;
643 /* Free any memory used in png_ptr struct (old method) */
645 png_write_destroy(png_structp png_ptr
)
647 jmp_buf tmp_jmp
; /* save jump buffer */
648 png_error_ptr error_fn
;
649 png_error_ptr warning_fn
;
652 png_debug(1, "in png_write_destroy\n");
653 /* free any memory zlib uses */
654 deflateEnd(&png_ptr
->zstream
);
656 /* free our memory. png_free checks NULL for us. */
657 png_free(png_ptr
, png_ptr
->zbuf
);
658 png_free(png_ptr
, png_ptr
->row_buf
);
659 png_free(png_ptr
, png_ptr
->prev_row
);
660 png_free(png_ptr
, png_ptr
->sub_row
);
661 png_free(png_ptr
, png_ptr
->up_row
);
662 png_free(png_ptr
, png_ptr
->avg_row
);
663 png_free(png_ptr
, png_ptr
->paeth_row
);
664 #if defined(PNG_TIME_RFC1123_SUPPORTED)
665 png_free(png_ptr
, png_ptr
->time_buffer
);
666 #endif /* PNG_TIME_RFC1123_SUPPORTED */
667 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
668 png_free(png_ptr
, png_ptr
->prev_filters
);
669 png_free(png_ptr
, png_ptr
->filter_weights
);
670 png_free(png_ptr
, png_ptr
->inv_filter_weights
);
671 png_free(png_ptr
, png_ptr
->filter_costs
);
672 png_free(png_ptr
, png_ptr
->inv_filter_costs
);
673 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
675 /* reset structure */
676 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
678 error_fn
= png_ptr
->error_fn
;
679 warning_fn
= png_ptr
->warning_fn
;
680 error_ptr
= png_ptr
->error_ptr
;
682 png_memset(png_ptr
, 0, sizeof (png_struct
));
684 png_ptr
->error_fn
= error_fn
;
685 png_ptr
->warning_fn
= warning_fn
;
686 png_ptr
->error_ptr
= error_ptr
;
688 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
691 /* Allow the application to select one or more row filters to use. */
693 png_set_filter(png_structp png_ptr
, int method
, int filters
)
695 png_debug(1, "in png_set_filter\n");
696 /* We allow 'method' only for future expansion of the base filter method. */
697 if (method
== PNG_FILTER_TYPE_BASE
)
699 switch (filters
& (PNG_ALL_FILTERS
| 0x07))
703 case 7: png_warning(png_ptr
, "Unknown row filter for method 0");
704 case PNG_FILTER_VALUE_NONE
: png_ptr
->do_filter
=PNG_FILTER_NONE
; break;
705 case PNG_FILTER_VALUE_SUB
: png_ptr
->do_filter
=PNG_FILTER_SUB
; break;
706 case PNG_FILTER_VALUE_UP
: png_ptr
->do_filter
=PNG_FILTER_UP
; break;
707 case PNG_FILTER_VALUE_AVG
: png_ptr
->do_filter
=PNG_FILTER_AVG
; break;
708 case PNG_FILTER_VALUE_PAETH
: png_ptr
->do_filter
=PNG_FILTER_PAETH
;break;
709 default: png_ptr
->do_filter
= (png_byte
)filters
; break;
712 /* If we have allocated the row_buf, this means we have already started
713 * with the image and we should have allocated all of the filter buffers
714 * that have been selected. If prev_row isn't already allocated, then
715 * it is too late to start using the filters that need it, since we
716 * will be missing the data in the previous row. If an application
717 * wants to start and stop using particular filters during compression,
718 * it should start out with all of the filters, and then add and
719 * remove them after the start of compression.
721 if (png_ptr
->row_buf
!= NULL
)
723 if (png_ptr
->do_filter
& PNG_FILTER_SUB
&& png_ptr
->sub_row
== NULL
)
725 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
726 (png_ptr
->rowbytes
+ 1));
727 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
730 if (png_ptr
->do_filter
& PNG_FILTER_UP
&& png_ptr
->up_row
== NULL
)
732 if (png_ptr
->prev_row
== NULL
)
734 png_warning(png_ptr
, "Can't add Up filter after starting");
735 png_ptr
->do_filter
&= ~PNG_FILTER_UP
;
739 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
740 (png_ptr
->rowbytes
+ 1));
741 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
745 if (png_ptr
->do_filter
& PNG_FILTER_AVG
&& png_ptr
->avg_row
== NULL
)
747 if (png_ptr
->prev_row
== NULL
)
749 png_warning(png_ptr
, "Can't add Average filter after starting");
750 png_ptr
->do_filter
&= ~PNG_FILTER_AVG
;
754 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
755 (png_ptr
->rowbytes
+ 1));
756 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
760 if (png_ptr
->do_filter
& PNG_FILTER_PAETH
&&
761 png_ptr
->paeth_row
== NULL
)
763 if (png_ptr
->prev_row
== NULL
)
765 png_warning(png_ptr
, "Can't add Paeth filter after starting");
766 png_ptr
->do_filter
&= ~PNG_FILTER_PAETH
;
770 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
771 (png_ptr
->rowbytes
+ 1));
772 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
776 if (png_ptr
->do_filter
== PNG_NO_FILTERS
)
777 png_ptr
->do_filter
= PNG_FILTER_NONE
;
781 png_error(png_ptr
, "Unknown custom filter method");
784 /* This allows us to influence the way in which libpng chooses the "best"
785 * filter for the current scanline. While the "minimum-sum-of-absolute-
786 * differences metric is relatively fast and effective, there is some
787 * question as to whether it can be improved upon by trying to keep the
788 * filtered data going to zlib more consistent, hopefully resulting in
789 * better compression.
791 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
793 png_set_filter_heuristics(png_structp png_ptr
, int heuristic_method
,
794 int num_weights
, png_doublep filter_weights
,
795 png_doublep filter_costs
)
797 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
801 png_debug(1, "in png_set_filter_heuristics\n");
802 if (heuristic_method
>= PNG_FILTER_HEURISTIC_LAST
)
804 png_warning(png_ptr
, "Unknown filter heuristic method");
808 if (heuristic_method
== PNG_FILTER_HEURISTIC_DEFAULT
)
810 heuristic_method
= PNG_FILTER_HEURISTIC_UNWEIGHTED
;
813 if (num_weights
< 0 || filter_weights
== NULL
||
814 heuristic_method
== PNG_FILTER_HEURISTIC_UNWEIGHTED
)
819 png_ptr
->num_prev_filters
= num_weights
;
820 png_ptr
->heuristic_method
= heuristic_method
;
824 if (png_ptr
->prev_filters
== NULL
)
826 png_ptr
->prev_filters
= (png_bytep
)png_malloc(png_ptr
,
827 (png_uint_32
)(sizeof(png_byte
) * num_weights
));
829 /* To make sure that the weighting starts out fairly */
830 for (i
= 0; i
< num_weights
; i
++)
832 png_ptr
->prev_filters
[i
] = 255;
836 if (png_ptr
->filter_weights
== NULL
)
838 png_ptr
->filter_weights
= (png_uint_16p
) png_malloc(png_ptr
,
839 (png_uint_32
)(sizeof(png_uint_16
) * num_weights
));
841 png_ptr
->inv_filter_weights
= (png_uint_16p
) png_malloc(png_ptr
,
842 (png_uint_32
)(sizeof(png_uint_16
) * num_weights
));
844 for (i
= 0; i
< num_weights
; i
++)
846 png_ptr
->inv_filter_weights
[i
] =
847 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
851 for (i
= 0; i
< num_weights
; i
++)
853 if (filter_weights
[i
] < 0.0)
855 png_ptr
->inv_filter_weights
[i
] =
856 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
860 png_ptr
->inv_filter_weights
[i
] =
861 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
*filter_weights
[i
]+0.5);
862 png_ptr
->filter_weights
[i
] =
863 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
/filter_weights
[i
]+0.5);
868 /* If, in the future, there are other filter methods, this would
869 * need to be based on png_ptr->filter.
871 if (png_ptr
->filter_costs
== NULL
)
873 png_ptr
->filter_costs
= (png_uint_16p
) png_malloc(png_ptr
,
874 (png_uint_32
)(sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
876 png_ptr
->inv_filter_costs
= (png_uint_16p
) png_malloc(png_ptr
,
877 (png_uint_32
)(sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
879 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
881 png_ptr
->inv_filter_costs
[i
] =
882 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
886 /* Here is where we set the relative costs of the different filters. We
887 * should take the desired compression level into account when setting
888 * the costs, so that Paeth, for instance, has a high relative cost at low
889 * compression levels, while it has a lower relative cost at higher
890 * compression settings. The filter types are in order of increasing
891 * relative cost, so it would be possible to do this with an algorithm.
893 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
895 if (filter_costs
== NULL
|| filter_costs
[i
] < 0.0)
897 png_ptr
->inv_filter_costs
[i
] =
898 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
900 else if (filter_costs
[i
] >= 1.0)
902 png_ptr
->inv_filter_costs
[i
] =
903 (png_uint_16
)((double)PNG_COST_FACTOR
/ filter_costs
[i
] + 0.5);
904 png_ptr
->filter_costs
[i
] =
905 (png_uint_16
)((double)PNG_COST_FACTOR
* filter_costs
[i
] + 0.5);
909 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
912 png_set_compression_level(png_structp png_ptr
, int level
)
914 png_debug(1, "in png_set_compression_level\n");
915 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_LEVEL
;
916 png_ptr
->zlib_level
= level
;
920 png_set_compression_mem_level(png_structp png_ptr
, int mem_level
)
922 png_debug(1, "in png_set_compression_mem_level\n");
923 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
;
924 png_ptr
->zlib_mem_level
= mem_level
;
928 png_set_compression_strategy(png_structp png_ptr
, int strategy
)
930 png_debug(1, "in png_set_compression_strategy\n");
931 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_STRATEGY
;
932 png_ptr
->zlib_strategy
= strategy
;
936 png_set_compression_window_bits(png_structp png_ptr
, int window_bits
)
938 if (window_bits
> 15)
939 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
940 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
;
941 png_ptr
->zlib_window_bits
= window_bits
;
945 png_set_compression_method(png_structp png_ptr
, int method
)
947 png_debug(1, "in png_set_compression_method\n");
949 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
950 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_METHOD
;
951 png_ptr
->zlib_method
= method
;
955 png_set_write_status_fn(png_structp png_ptr
, png_write_status_ptr write_row_fn
)
957 png_ptr
->write_row_fn
= write_row_fn
;
960 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
962 png_set_write_user_transform_fn(png_structp png_ptr
, png_user_transform_ptr
963 write_user_transform_fn
)
965 png_debug(1, "in png_set_write_user_transform_fn\n");
966 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
967 png_ptr
->write_user_transform_fn
= write_user_transform_fn
;