blob: f7e8535ae2e903168734c900062f4b3f54d8ab26 [file] [log] [blame]
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwrite.c - general routines to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06004 * libpng 1.0.5c - November 27, 1999
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * 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
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06008 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Guy Schalnat0d580581995-07-20 02:43:20 -050010
11/* get internal access to png.h */
12#define PNG_INTERNAL
13#include "png.h"
14
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Writes all the PNG information. This is the suggested way to use the
16 * library. If you have a new chunk to add, make a function to write it,
17 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050018 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050019 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
20 * the chunk, as that will keep the code from breaking if you want to just
21 * write a plain PNG file. If you have long comments, I suggest writing
22 * them in png_write_end(), and compressing them.
23 */
Guy Schalnat0d580581995-07-20 02:43:20 -050024void
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060025png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050026{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060027 png_debug(1, "in png_write_info_before_PLTE\n");
28 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
29 {
Guy Schalnat0d580581995-07-20 02:43:20 -050030 png_write_sig(png_ptr); /* write PNG signature */
31 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060032 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
33 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060034 info_ptr->filter_type,
35#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
36 info_ptr->interlace_type);
37#else
38 0);
39#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050040 /* the rest of these check to see if the valid field has the appropriate
41 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050042#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060043 if (info_ptr->valid & PNG_INFO_gAMA)
44 png_write_gAMA(png_ptr, info_ptr->gamma);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050045#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060046#if defined(PNG_WRITE_sRGB_SUPPORTED)
47 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060048 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060049#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050050#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060051 if (info_ptr->valid & PNG_INFO_sBIT)
52 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050053#endif
54#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060055 if (info_ptr->valid & PNG_INFO_cHRM)
Guy Schalnat0d580581995-07-20 02:43:20 -050056 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060057 info_ptr->x_white, info_ptr->y_white,
58 info_ptr->x_red, info_ptr->y_red,
59 info_ptr->x_green, info_ptr->y_green,
60 info_ptr->x_blue, info_ptr->y_blue);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050061#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060062 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
63 }
64}
65
66void
67png_write_info(png_structp png_ptr, png_infop info_ptr)
68{
69#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
70 int i;
71#endif
72
73 png_debug(1, "in png_write_info\n");
74
75 png_write_info_before_PLTE(png_ptr, info_ptr);
76
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -050078 png_write_PLTE(png_ptr, info_ptr->palette,
79 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060080 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -050081 png_error(png_ptr, "Valid palette required for paletted images\n");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060082
Guy Schalnat51f0eb41995-09-26 05:22:39 -050083#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060084 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060085 {
86#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
87 /* invert the alpha channel (in tRNS) */
88 if (png_ptr->transformations & PNG_INVERT_ALPHA &&
89 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
90 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060091 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060092 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -050093 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060094 }
95#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060096 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
97 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060098 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050099#endif
100#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101 if (info_ptr->valid & PNG_INFO_bKGD)
102 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500103#endif
104#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600105 if (info_ptr->valid & PNG_INFO_hIST)
106 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500107#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500108#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600109 if (info_ptr->valid & PNG_INFO_oFFs)
110 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
111 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500112#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113#if defined(PNG_WRITE_pCAL_SUPPORTED)
114 if (info_ptr->valid & PNG_INFO_pCAL)
115 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
116 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
117 info_ptr->pcal_units, info_ptr->pcal_params);
118#endif
119#if defined(PNG_WRITE_pHYs_SUPPORTED)
120 if (info_ptr->valid & PNG_INFO_pHYs)
121 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
122 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
123#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500124#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600125 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500126 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600127 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600128 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500129 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500130#endif
131#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500132 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500134 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
136 info_ptr->text[i].compression);
137 /* If we want a compressed text chunk */
138 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
139 {
140#if defined(PNG_WRITE_zTXt_SUPPORTED)
141 /* write compressed chunk */
142 png_write_zTXt(png_ptr, info_ptr->text[i].key,
143 info_ptr->text[i].text, info_ptr->text[i].text_length,
144 info_ptr->text[i].compression);
145#else
146 png_warning(png_ptr, "Unable to write compressed text\n");
147#endif
148 /* Mark this chunk as written */
149 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
150 }
151 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
152 {
153#if defined(PNG_WRITE_tEXt_SUPPORTED)
154 /* write uncompressed chunk */
155 png_write_tEXt(png_ptr, info_ptr->text[i].key,
156 info_ptr->text[i].text, info_ptr->text[i].text_length);
157#else
158 png_warning(png_ptr, "Unable to write uncompressed text\n");
159#endif
160 /* Mark this chunk as written */
161 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
162 }
163 }
164#endif
165}
Guy Schalnat0d580581995-07-20 02:43:20 -0500166
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600168 * time information, you can pass NULL for info. If you already wrote these
169 * in png_write_info(), do not write them again here. If you have long
170 * comments, I suggest writing them here, and compressing them.
171 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172void
173png_write_end(png_structp png_ptr, png_infop info_ptr)
174{
175 png_debug(1, "in png_write_end\n");
176 if (!(png_ptr->mode & PNG_HAVE_IDAT))
177 png_error(png_ptr, "No IDATs written into file");
178
179 /* see if user wants us to write information chunks */
180 if (info_ptr != NULL)
181 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600182#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600184#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500185#if defined(PNG_WRITE_tIME_SUPPORTED)
186 /* check to see if user has supplied a time chunk */
187 if (info_ptr->valid & PNG_INFO_tIME &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600188 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189 png_write_tIME(png_ptr, &(info_ptr->mod_time));
190#endif
191#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
192 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600193 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500194 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
196 info_ptr->text[i].compression);
197 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500198 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500199#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600200 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600201 png_write_zTXt(png_ptr, info_ptr->text[i].key,
202 info_ptr->text[i].text, info_ptr->text[i].text_length,
203 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500204#else
205 png_warning(png_ptr, "Unable to write compressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500206#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 /* Mark this chunk as written */
208 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500209 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500211 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500212#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500213 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600214 png_write_tEXt(png_ptr, info_ptr->text[i].key,
215 info_ptr->text[i].text, info_ptr->text[i].text_length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500216#else
217 png_warning(png_ptr, "Unable to write uncompressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500218#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500219
Andreas Dilger47a0c421997-05-16 02:46:07 -0500220 /* Mark this chunk as written */
221 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500222 }
223 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600224#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500225 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500226
227 png_ptr->mode |= PNG_AFTER_IDAT;
228
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500230 png_write_IEND(png_ptr);
231}
232
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500233#if defined(PNG_WRITE_tIME_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500234void
Guy Schalnat6d764711995-12-19 03:22:19 -0600235png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500236{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600238 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
239 ptime->month = (png_byte)(ttime->tm_mon + 1);
240 ptime->day = (png_byte)ttime->tm_mday;
241 ptime->hour = (png_byte)ttime->tm_hour;
242 ptime->minute = (png_byte)ttime->tm_min;
243 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500244}
245
246void
Guy Schalnat6d764711995-12-19 03:22:19 -0600247png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500248{
249 struct tm *tbuf;
250
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500252 tbuf = gmtime(&ttime);
253 png_convert_from_struct_tm(ptime, tbuf);
254}
Guy Schalnat6d764711995-12-19 03:22:19 -0600255#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500256
Andreas Dilger47a0c421997-05-16 02:46:07 -0500257/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500258png_structp
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500259png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600260 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500261{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500262#ifdef PNG_USER_MEM_SUPPORTED
263 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
264 warn_fn, NULL, NULL, NULL));
265}
266
267/* Alternate initialize png_ptr structure, and allocate any memory needed */
268png_structp
269png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
270 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
271 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
272{
273#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500274 png_structp png_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600275#ifdef USE_FAR_KEYWORD
276 jmp_buf jmpbuf;
277#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 png_debug(1, "in png_create_write_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500279#ifdef PNG_USER_MEM_SUPPORTED
280 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
281 (png_malloc_ptr)malloc_fn)) == NULL)
282#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500283 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500284#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500285 {
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600286 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500287 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600288#ifdef USE_FAR_KEYWORD
289 if (setjmp(jmpbuf))
290#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500291 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600292#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500293 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600294 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -0500295 png_destroy_struct(png_ptr);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600296 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500297 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600298#ifdef USE_FAR_KEYWORD
299 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
300#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500301#ifdef PNG_USER_MEM_SUPPORTED
302 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
303#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600304 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500305
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
307 * we must recompile any applications that use any older library version.
308 * For versions after libpng 1.0, we will be compatible, so we need
309 * only check the first digit.
310 */
311 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
312 (png_libpng_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -0500313 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314 png_error(png_ptr,
315 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -0500316 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500317
318 /* initialize zbuf - compression buffer */
319 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600320 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
321 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500322
323 png_set_write_fn(png_ptr, NULL, NULL, NULL);
324
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600325#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
326 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
327 1, NULL, NULL);
328#endif
329
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600330 return ((png_structp)png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500331}
332
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500334void
335png_write_init(png_structp png_ptr)
336{
337 jmp_buf tmp_jmp; /* to save current jump buffer */
338
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 png_debug(1, "in png_write_init\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500340 /* save jump buffer and error functions */
341 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
342
343 /* reset all variables to 0 */
344 png_memset(png_ptr, 0, sizeof (png_struct));
345
346 /* restore jump buffer */
347 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
348
349 /* initialize zbuf - compression buffer */
350 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600351 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
352 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500353 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354
355#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
356 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
357 1, NULL, NULL);
358#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500359}
360
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600361/* Write a few rows of image data. If the image is interlaced,
362 * either you will have to write the 7 sub images, or, if you
363 * have called png_set_interlace_handling(), you will have to
364 * "write" the image seven times.
365 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500366void
Guy Schalnat6d764711995-12-19 03:22:19 -0600367png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 png_uint_32 num_rows)
369{
370 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600371 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500372
Andreas Dilger47a0c421997-05-16 02:46:07 -0500373 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 /* loop through the rows */
375 for (i = 0, rp = row; i < num_rows; i++, rp++)
376 {
377 png_write_row(png_ptr, *rp);
378 }
379}
380
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600381/* Write the image. You only need to call this function once, even
382 * if you are writing an interlaced image.
383 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500384void
Guy Schalnat6d764711995-12-19 03:22:19 -0600385png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500386{
387 png_uint_32 i; /* row index */
388 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600389 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500390
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600392#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500393 /* intialize interlace handling. If image is not interlaced,
394 this will set pass to 1 */
395 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600396#else
397 num_pass = 1;
398#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500399 /* loop through passes */
400 for (pass = 0; pass < num_pass; pass++)
401 {
402 /* loop through image */
403 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
404 {
405 png_write_row(png_ptr, *rp);
406 }
407 }
408}
409
Guy Schalnate5a37791996-06-05 15:50:50 -0500410/* called by user to write a row of image data */
Guy Schalnat0d580581995-07-20 02:43:20 -0500411void
Guy Schalnat6d764711995-12-19 03:22:19 -0600412png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500413{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600414 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
415 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500416 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600417 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500418 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500419 /* check for transforms that have been set but were defined out */
420#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
421 if (png_ptr->transformations & PNG_INVERT_MONO)
422 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
423#endif
424#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
425 if (png_ptr->transformations & PNG_FILLER)
426 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
427#endif
428#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
429 if (png_ptr->transformations & PNG_PACKSWAP)
430 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
431#endif
432#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
433 if (png_ptr->transformations & PNG_PACK)
434 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
435#endif
436#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
437 if (png_ptr->transformations & PNG_SHIFT)
438 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
439#endif
440#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
441 if (png_ptr->transformations & PNG_BGR)
442 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
443#endif
444#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
445 if (png_ptr->transformations & PNG_SWAP_BYTES)
446 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
447#endif
448
Guy Schalnat0d580581995-07-20 02:43:20 -0500449 png_write_start_row(png_ptr);
450 }
451
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500452#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500453 /* if interlaced and not interested in row, return */
454 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
455 {
456 switch (png_ptr->pass)
457 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600458 case 0:
Guy Schalnat0d580581995-07-20 02:43:20 -0500459 if (png_ptr->row_number & 7)
460 {
461 png_write_finish_row(png_ptr);
462 return;
463 }
464 break;
465 case 1:
466 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
467 {
468 png_write_finish_row(png_ptr);
469 return;
470 }
471 break;
472 case 2:
473 if ((png_ptr->row_number & 7) != 4)
474 {
475 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600476 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500477 }
478 break;
479 case 3:
480 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
481 {
482 png_write_finish_row(png_ptr);
483 return;
484 }
485 break;
486 case 4:
487 if ((png_ptr->row_number & 3) != 2)
488 {
489 png_write_finish_row(png_ptr);
490 return;
491 }
492 break;
493 case 5:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600494 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 {
496 png_write_finish_row(png_ptr);
497 return;
498 }
499 break;
500 case 6:
501 if (!(png_ptr->row_number & 1))
502 {
503 png_write_finish_row(png_ptr);
504 return;
505 }
506 break;
507 }
508 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600509#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500510
511 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600512 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500513 png_ptr->row_info.width = png_ptr->usr_width;
514 png_ptr->row_info.channels = png_ptr->usr_channels;
515 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600516 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
517 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600518
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
520 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
521
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600522 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
523 png_debug1(3, "row_info->width = %d\n", png_ptr->row_info.width);
524 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
525 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
526 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
527 png_debug1(3, "row_info->rowbytes = %d\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528
529 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600530 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600531 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500532
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500533#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500534 /* handle interlacing */
535 if (png_ptr->interlaced && png_ptr->pass < 6 &&
536 (png_ptr->transformations & PNG_INTERLACE))
537 {
538 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600539 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500540 /* this should always get caught above, but still ... */
541 if (!(png_ptr->row_info.width))
542 {
543 png_write_finish_row(png_ptr);
544 return;
545 }
546 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600547#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500548
549 /* handle other transformations */
550 if (png_ptr->transformations)
551 png_do_write_transformations(png_ptr);
552
Andreas Dilger47a0c421997-05-16 02:46:07 -0500553 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500554 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600555
556 if (png_ptr->write_row_fn != NULL)
557 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500558}
559
Guy Schalnat0f716451995-11-28 11:22:13 -0600560#if defined(PNG_WRITE_FLUSH_SUPPORTED)
561/* Set the automatic flush interval or 0 to turn flushing off */
562void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600563png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600564{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600566 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600567}
568
569/* flush the current output buffers now */
570void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600571png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600572{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600573 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600574
Andreas Dilger47a0c421997-05-16 02:46:07 -0500575 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500576 /* We have already written out all of the data */
577 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600578 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600579
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600580 do
581 {
582 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600583
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600584 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600585 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600586 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600587
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600588 /* check for compression errors */
589 if (ret != Z_OK)
590 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600592 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600593 else
594 png_error(png_ptr, "zlib error");
595 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600596
Andreas Dilger47a0c421997-05-16 02:46:07 -0500597 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600598 {
599 /* write the IDAT and reset the zlib output buffer */
600 png_write_IDAT(png_ptr, png_ptr->zbuf,
601 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600602 png_ptr->zstream.next_out = png_ptr->zbuf;
603 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600604 wrote_IDAT = 1;
605 }
606 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600607
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600608 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600609 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600610 {
611 /* write the IDAT and reset the zlib output buffer */
612 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
614 png_ptr->zstream.next_out = png_ptr->zbuf;
615 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600616 }
617 png_ptr->flush_rows = 0;
618 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600619}
620#endif /* PNG_WRITE_FLUSH_SUPPORTED */
621
Guy Schalnate5a37791996-06-05 15:50:50 -0500622/* free all memory used by the write */
623void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600624png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500625{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600626 png_structp png_ptr = NULL;
627 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500628#ifdef PNG_USER_MEM_SUPPORTED
629 png_free_ptr free_fn = NULL;
630#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600631
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632 png_debug(1, "in png_destroy_write_struct\n");
633 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500634 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600635 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500636#ifdef PNG_USER_MEM_SUPPORTED
637 free_fn = png_ptr->free_fn;
638#endif
639 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600640
Andreas Dilger47a0c421997-05-16 02:46:07 -0500641 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600642 info_ptr = *info_ptr_ptr;
643
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500645 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500646#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
647 png_debug(1, "in png_info_destroy\n");
648 if (info_ptr->text != NULL)
649 {
650 int i;
651 for (i = 0; i < info_ptr->num_text; i++)
652 {
653 if(info_ptr->text[i].key != NULL)
654 {
655 png_free(png_ptr, info_ptr->text[i].key);
656 info_ptr->text[i].key = NULL;
657 }
658 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600659 png_free(png_ptr, info_ptr->text);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500660 info_ptr->text = NULL;
661 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600662#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600663#if defined(PNG_READ_pCAL_SUPPORTED)
664 png_free(png_ptr, info_ptr->pcal_purpose);
665 png_free(png_ptr, info_ptr->pcal_units);
666 if (info_ptr->pcal_params != NULL)
667 {
668 int i;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600669 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600670 {
671 png_free(png_ptr, info_ptr->pcal_params[i]);
672 }
673 png_free(png_ptr, info_ptr->pcal_params);
674 }
675#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500676#ifdef PNG_USER_MEM_SUPPORTED
677 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
678#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600679 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500680#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600681 *info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500682 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600683
Andreas Dilger47a0c421997-05-16 02:46:07 -0500684 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500685 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600686 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500687#ifdef PNG_USER_MEM_SUPPORTED
688 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
689#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600690 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500691#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600692 *png_ptr_ptr = (png_structp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500693 }
694}
695
696
Andreas Dilger47a0c421997-05-16 02:46:07 -0500697/* Free any memory used in png_ptr struct (old method) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500698void
Guy Schalnat6d764711995-12-19 03:22:19 -0600699png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500700{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600701 jmp_buf tmp_jmp; /* save jump buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -0500702 png_error_ptr error_fn;
703 png_error_ptr warning_fn;
704 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500705#ifdef PNG_USER_MEM_SUPPORTED
706 png_free_ptr free_fn;
707#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500708
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500710 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600711 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500712
Guy Schalnat0d580581995-07-20 02:43:20 -0500713 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600714 png_free(png_ptr, png_ptr->zbuf);
715 png_free(png_ptr, png_ptr->row_buf);
716 png_free(png_ptr, png_ptr->prev_row);
717 png_free(png_ptr, png_ptr->sub_row);
718 png_free(png_ptr, png_ptr->up_row);
719 png_free(png_ptr, png_ptr->avg_row);
720 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600721#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600722 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600723#endif /* PNG_TIME_RFC1123_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
725 png_free(png_ptr, png_ptr->prev_filters);
726 png_free(png_ptr, png_ptr->filter_weights);
727 png_free(png_ptr, png_ptr->inv_filter_weights);
728 png_free(png_ptr, png_ptr->filter_costs);
729 png_free(png_ptr, png_ptr->inv_filter_costs);
730#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500731
Guy Schalnat0d580581995-07-20 02:43:20 -0500732 /* reset structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500733 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Guy Schalnate5a37791996-06-05 15:50:50 -0500734
735 error_fn = png_ptr->error_fn;
736 warning_fn = png_ptr->warning_fn;
737 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500738#ifdef PNG_USER_MEM_SUPPORTED
739 free_fn = png_ptr->free_fn;
740#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500741
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500742 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500743
744 png_ptr->error_fn = error_fn;
745 png_ptr->warning_fn = warning_fn;
746 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500747#ifdef PNG_USER_MEM_SUPPORTED
748 png_ptr->free_fn = free_fn;
749#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500750
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500751 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
752}
Guy Schalnate5a37791996-06-05 15:50:50 -0500753
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754/* Allow the application to select one or more row filters to use. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500755void
Guy Schalnate5a37791996-06-05 15:50:50 -0500756png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500757{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500758 png_debug(1, "in png_set_filter\n");
759 /* We allow 'method' only for future expansion of the base filter method. */
760 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500761 {
762 switch (filters & (PNG_ALL_FILTERS | 0x07))
763 {
764 case 5:
765 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500766 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
767 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
768 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
769 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
770 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
771 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -0500772 default: png_ptr->do_filter = (png_byte)filters; break;
773 }
774
Andreas Dilger47a0c421997-05-16 02:46:07 -0500775 /* If we have allocated the row_buf, this means we have already started
776 * with the image and we should have allocated all of the filter buffers
777 * that have been selected. If prev_row isn't already allocated, then
778 * it is too late to start using the filters that need it, since we
779 * will be missing the data in the previous row. If an application
780 * wants to start and stop using particular filters during compression,
781 * it should start out with all of the filters, and then add and
782 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500783 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500785 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500786 if (png_ptr->do_filter & PNG_FILTER_SUB && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500787 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500788 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600789 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500790 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500791 }
792
Andreas Dilger47a0c421997-05-16 02:46:07 -0500793 if (png_ptr->do_filter & PNG_FILTER_UP && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500794 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500795 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500796 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500798 png_ptr->do_filter &= ~PNG_FILTER_UP;
799 }
800 else
801 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500802 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600803 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500804 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -0500805 }
806 }
807
Andreas Dilger47a0c421997-05-16 02:46:07 -0500808 if (png_ptr->do_filter & PNG_FILTER_AVG && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500809 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500810 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500811 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500812 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500813 png_ptr->do_filter &= ~PNG_FILTER_AVG;
814 }
815 else
816 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500817 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600818 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500819 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -0500820 }
821 }
822
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 if (png_ptr->do_filter & PNG_FILTER_PAETH &&
824 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500825 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500826 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500827 {
828 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500829 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -0500830 }
831 else
832 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600833 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600834 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -0500836 }
837 }
838
839 if (png_ptr->do_filter == PNG_NO_FILTERS)
840 png_ptr->do_filter = PNG_FILTER_NONE;
841 }
842 }
843 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500844 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500845}
846
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847/* This allows us to influence the way in which libpng chooses the "best"
848 * filter for the current scanline. While the "minimum-sum-of-absolute-
849 * differences metric is relatively fast and effective, there is some
850 * question as to whether it can be improved upon by trying to keep the
851 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600852 * better compression.
853 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500854#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
855void
856png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
857 int num_weights, png_doublep filter_weights,
858 png_doublep filter_costs)
859{
860 int i;
861
862 png_debug(1, "in png_set_filter_heuristics\n");
863 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
864 {
865 png_warning(png_ptr, "Unknown filter heuristic method");
866 return;
867 }
868
869 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
870 {
871 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
872 }
873
874 if (num_weights < 0 || filter_weights == NULL ||
875 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
876 {
877 num_weights = 0;
878 }
879
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500880 png_ptr->num_prev_filters = (png_byte)num_weights;
881 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500882
883 if (num_weights > 0)
884 {
885 if (png_ptr->prev_filters == NULL)
886 {
887 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600888 (png_uint_32)(sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500889
890 /* To make sure that the weighting starts out fairly */
891 for (i = 0; i < num_weights; i++)
892 {
893 png_ptr->prev_filters[i] = 255;
894 }
895 }
896
897 if (png_ptr->filter_weights == NULL)
898 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -0600899 png_ptr->filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600900 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500901
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -0600902 png_ptr->inv_filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600903 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904
905 for (i = 0; i < num_weights; i++)
906 {
907 png_ptr->inv_filter_weights[i] =
908 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
909 }
910 }
911
912 for (i = 0; i < num_weights; i++)
913 {
914 if (filter_weights[i] < 0.0)
915 {
916 png_ptr->inv_filter_weights[i] =
917 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
918 }
919 else
920 {
921 png_ptr->inv_filter_weights[i] =
922 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
923 png_ptr->filter_weights[i] =
924 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
925 }
926 }
927 }
928
929 /* If, in the future, there are other filter methods, this would
930 * need to be based on png_ptr->filter.
931 */
932 if (png_ptr->filter_costs == NULL)
933 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -0600934 png_ptr->filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600935 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500936
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -0600937 png_ptr->inv_filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600938 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939
940 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
941 {
942 png_ptr->inv_filter_costs[i] =
943 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
944 }
945 }
946
947 /* Here is where we set the relative costs of the different filters. We
948 * should take the desired compression level into account when setting
949 * the costs, so that Paeth, for instance, has a high relative cost at low
950 * compression levels, while it has a lower relative cost at higher
951 * compression settings. The filter types are in order of increasing
952 * relative cost, so it would be possible to do this with an algorithm.
953 */
954 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
955 {
956 if (filter_costs == NULL || filter_costs[i] < 0.0)
957 {
958 png_ptr->inv_filter_costs[i] =
959 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
960 }
961 else if (filter_costs[i] >= 1.0)
962 {
963 png_ptr->inv_filter_costs[i] =
964 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
965 png_ptr->filter_costs[i] =
966 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
967 }
968 }
969}
970#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
971
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500972void
Guy Schalnat6d764711995-12-19 03:22:19 -0600973png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500974{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500976 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500977 png_ptr->zlib_level = level;
978}
979
980void
Guy Schalnat6d764711995-12-19 03:22:19 -0600981png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500982{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500983 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500984 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600985 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500986}
987
988void
Guy Schalnat6d764711995-12-19 03:22:19 -0600989png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500990{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500992 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500993 png_ptr->zlib_strategy = strategy;
994}
995
996void
Guy Schalnat6d764711995-12-19 03:22:19 -0600997png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500998{
Guy Schalnate5a37791996-06-05 15:50:50 -0500999 if (window_bits > 15)
1000 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001001 else if (window_bits < 8)
1002 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Guy Schalnate5a37791996-06-05 15:50:50 -05001003 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001004 png_ptr->zlib_window_bits = window_bits;
1005}
1006
1007void
Guy Schalnat6d764711995-12-19 03:22:19 -06001008png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001009{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001010 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001011 if (method != 8)
1012 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1013 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001014 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001015}
1016
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001017void
1018png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1019{
1020 png_ptr->write_row_fn = write_row_fn;
1021}
1022
1023#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1024void
1025png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1026 write_user_transform_fn)
1027{
1028 png_debug(1, "in png_set_write_user_transform_fn\n");
1029 png_ptr->transformations |= PNG_USER_TRANSFORM;
1030 png_ptr->write_user_transform_fn = write_user_transform_fn;
1031}
1032#endif