blob: 4bcadc2a13f61818ae63bcb19ddaf40e9a9c28d4 [file] [log] [blame]
Andreas Dilger47a0c421997-05-16 02:46:07 -05001
2/* pngwrite.c - general routines to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06004 * libpng 0.98
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-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06009 * January 16, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060010 */
Guy Schalnat0d580581995-07-20 02:43:20 -050011
12/* get internal access to png.h */
13#define PNG_INTERNAL
14#include "png.h"
15
Andreas Dilger47a0c421997-05-16 02:46:07 -050016/* 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.
24 */
Guy Schalnat0d580581995-07-20 02:43:20 -050025void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060026png_write_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050027{
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060028#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -050029 int i;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060030#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050031
32 png_debug(1, "in png_write_info\n");
Guy Schalnat0d580581995-07-20 02:43:20 -050033 png_write_sig(png_ptr); /* write PNG signature */
34 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060035 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, info_ptr->interlace_type);
Guy Schalnat0d580581995-07-20 02:43:20 -050038 /* the rest of these check to see if the valid field has the appropriate
39 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050040#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060041 if (info_ptr->valid & PNG_INFO_gAMA)
42 png_write_gAMA(png_ptr, info_ptr->gamma);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050043#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060044#if defined(PNG_WRITE_sRGB_SUPPORTED)
45 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060046 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050048#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060049 if (info_ptr->valid & PNG_INFO_sBIT)
50 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050051#endif
52#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060053 if (info_ptr->valid & PNG_INFO_cHRM)
Guy Schalnat0d580581995-07-20 02:43:20 -050054 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060055 info_ptr->x_white, info_ptr->y_white,
56 info_ptr->x_red, info_ptr->y_red,
57 info_ptr->x_green, info_ptr->y_green,
58 info_ptr->x_blue, info_ptr->y_blue);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050059#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060060 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -050061 png_write_PLTE(png_ptr, info_ptr->palette,
62 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -050064 png_error(png_ptr, "Valid palette required for paletted images\n");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060065
Guy Schalnat51f0eb41995-09-26 05:22:39 -050066#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060068 {
69#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
70 /* invert the alpha channel (in tRNS) */
71 if (png_ptr->transformations & PNG_INVERT_ALPHA &&
72 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
73 {
74 int i;
75 for (i=0; i<info_ptr->num_trans; i++)
76 info_ptr->trans[i] = 255 - info_ptr->trans[i];
77 }
78#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060079 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
80 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060081 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050082#endif
83#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060084 if (info_ptr->valid & PNG_INFO_bKGD)
85 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050086#endif
87#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060088 if (info_ptr->valid & PNG_INFO_hIST)
89 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050090#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050091#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060092 if (info_ptr->valid & PNG_INFO_oFFs)
93 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
94 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050095#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050096#if defined(PNG_WRITE_pCAL_SUPPORTED)
97 if (info_ptr->valid & PNG_INFO_pCAL)
98 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
99 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
100 info_ptr->pcal_units, info_ptr->pcal_params);
101#endif
102#if defined(PNG_WRITE_pHYs_SUPPORTED)
103 if (info_ptr->valid & PNG_INFO_pHYs)
104 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
105 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
106#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500107#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600108 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500109 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600110 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Guy Schalnate5a37791996-06-05 15:50:50 -0500111 png_ptr->flags |= PNG_FLAG_WROTE_tIME;
112 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500113#endif
114#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500115 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500117 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
119 info_ptr->text[i].compression);
120 /* If we want a compressed text chunk */
121 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
122 {
123#if defined(PNG_WRITE_zTXt_SUPPORTED)
124 /* write compressed chunk */
125 png_write_zTXt(png_ptr, info_ptr->text[i].key,
126 info_ptr->text[i].text, info_ptr->text[i].text_length,
127 info_ptr->text[i].compression);
128#else
129 png_warning(png_ptr, "Unable to write compressed text\n");
130#endif
131 /* Mark this chunk as written */
132 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
133 }
134 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
135 {
136#if defined(PNG_WRITE_tEXt_SUPPORTED)
137 /* write uncompressed chunk */
138 png_write_tEXt(png_ptr, info_ptr->text[i].key,
139 info_ptr->text[i].text, info_ptr->text[i].text_length);
140#else
141 png_warning(png_ptr, "Unable to write uncompressed text\n");
142#endif
143 /* Mark this chunk as written */
144 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
145 }
146 }
147#endif
148}
Guy Schalnat0d580581995-07-20 02:43:20 -0500149
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600151 * time information, you can pass NULL for info. If you already wrote these
152 * in png_write_info(), do not write them again here. If you have long
153 * comments, I suggest writing them here, and compressing them.
154 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155void
156png_write_end(png_structp png_ptr, png_infop info_ptr)
157{
158 png_debug(1, "in png_write_end\n");
159 if (!(png_ptr->mode & PNG_HAVE_IDAT))
160 png_error(png_ptr, "No IDATs written into file");
161
162 /* see if user wants us to write information chunks */
163 if (info_ptr != NULL)
164 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600165#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600167#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168#if defined(PNG_WRITE_tIME_SUPPORTED)
169 /* check to see if user has supplied a time chunk */
170 if (info_ptr->valid & PNG_INFO_tIME &&
171 !(png_ptr->flags & PNG_FLAG_WROTE_tIME))
172 png_write_tIME(png_ptr, &(info_ptr->mod_time));
173#endif
174#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
175 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600176 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500177 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
179 info_ptr->text[i].compression);
180 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500181 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500182#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600183 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600184 png_write_zTXt(png_ptr, info_ptr->text[i].key,
185 info_ptr->text[i].text, info_ptr->text[i].text_length,
186 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500187#else
188 png_warning(png_ptr, "Unable to write compressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500189#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190 /* Mark this chunk as written */
191 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500192 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500194 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500195#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500196 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600197 png_write_tEXt(png_ptr, info_ptr->text[i].key,
198 info_ptr->text[i].text, info_ptr->text[i].text_length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500199#else
200 png_warning(png_ptr, "Unable to write uncompressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500201#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500202
Andreas Dilger47a0c421997-05-16 02:46:07 -0500203 /* Mark this chunk as written */
204 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500205 }
206 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600207#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500208 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500209
210 png_ptr->mode |= PNG_AFTER_IDAT;
211
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500213 png_write_IEND(png_ptr);
214}
215
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600216#if defined(PNG_TIME_RFC1123_SUPPORTED)
217/* Convert the supplied time into an RFC 1123 string suitable for use in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600218 * a "Creation Time" or other text-based time string.
219 */
220png_charp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600221png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600222{
223 const char *short_months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
224 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
225
226 if (png_ptr->time_buffer == NULL)
227 {
228 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, 29*sizeof(char));
229 }
230
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600231#ifdef USE_FAR_KEYWORD
232 {
233 char near_time_buf[29];
234 sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000",
235 ptime->day % 31, short_months[ptime->month],
236 ptime->year, ptime->hour % 24, ptime->minute % 60,
237 ptime->second % 61);
238 png_memcpy(png_ptr->time_buffer, near_time_buf,
239 29*sizeof(char));
240 }
241#else
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600242 sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600243 ptime->day % 31, short_months[ptime->month],
244 ptime->year, ptime->hour % 24, ptime->minute % 60,
245 ptime->second % 61);
246#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600247 return png_ptr->time_buffer;
248}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600249#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600250
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500251#if defined(PNG_WRITE_tIME_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500252void
Guy Schalnat6d764711995-12-19 03:22:19 -0600253png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500254{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600256 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
257 ptime->month = (png_byte)(ttime->tm_mon + 1);
258 ptime->day = (png_byte)ttime->tm_mday;
259 ptime->hour = (png_byte)ttime->tm_hour;
260 ptime->minute = (png_byte)ttime->tm_min;
261 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500262}
263
264void
Guy Schalnat6d764711995-12-19 03:22:19 -0600265png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500266{
267 struct tm *tbuf;
268
Andreas Dilger47a0c421997-05-16 02:46:07 -0500269 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500270 tbuf = gmtime(&ttime);
271 png_convert_from_struct_tm(ptime, tbuf);
272}
Guy Schalnat6d764711995-12-19 03:22:19 -0600273#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500274
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500276png_structp
277png_create_write_struct(png_const_charp user_png_ver, voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600278 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500279{
Guy Schalnate5a37791996-06-05 15:50:50 -0500280 png_structp png_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600281#ifdef USE_FAR_KEYWORD
282 jmp_buf jmpbuf;
283#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500284 png_debug(1, "in png_create_write_struct\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500285 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
286 {
287 return (png_structp)NULL;
288 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600289#ifdef USE_FAR_KEYWORD
290 if (setjmp(jmpbuf))
291#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500292 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600293#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500294 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600295 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -0500296 png_destroy_struct(png_ptr);
297 return (png_structp)NULL;
298 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600299#ifdef USE_FAR_KEYWORD
300 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
301#endif
302 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500303
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
305 * we must recompile any applications that use any older library version.
306 * For versions after libpng 1.0, we will be compatible, so we need
307 * only check the first digit.
308 */
309 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
310 (png_libpng_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -0500311 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 png_error(png_ptr,
313 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -0500314 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500315
316 /* initialize zbuf - compression buffer */
317 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600318 png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500319
320 png_set_write_fn(png_ptr, NULL, NULL, NULL);
321
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600322#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
323 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
324 1, NULL, NULL);
325#endif
326
Guy Schalnate5a37791996-06-05 15:50:50 -0500327 return (png_ptr);
328}
329
330
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500332void
333png_write_init(png_structp png_ptr)
334{
335 jmp_buf tmp_jmp; /* to save current jump buffer */
336
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 png_debug(1, "in png_write_init\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500338 /* save jump buffer and error functions */
339 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
340
341 /* reset all variables to 0 */
342 png_memset(png_ptr, 0, sizeof (png_struct));
343
344 /* restore jump buffer */
345 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
346
347 /* initialize zbuf - compression buffer */
348 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600349 png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500350 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500351
352#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
353 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
354 1, NULL, NULL);
355#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500356}
357
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600358/* Write a few rows of image data. If the image is interlaced,
359 * either you will have to write the 7 sub images, or, if you
360 * have called png_set_interlace_handling(), you will have to
361 * "write" the image seven times.
362 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500363void
Guy Schalnat6d764711995-12-19 03:22:19 -0600364png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500365 png_uint_32 num_rows)
366{
367 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600368 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500369
Andreas Dilger47a0c421997-05-16 02:46:07 -0500370 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500371 /* loop through the rows */
372 for (i = 0, rp = row; i < num_rows; i++, rp++)
373 {
374 png_write_row(png_ptr, *rp);
375 }
376}
377
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600378/* Write the image. You only need to call this function once, even
379 * if you are writing an interlaced image.
380 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500381void
Guy Schalnat6d764711995-12-19 03:22:19 -0600382png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500383{
384 png_uint_32 i; /* row index */
385 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600386 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500387
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 png_debug(1, "in png_write_image\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500389 /* intialize interlace handling. If image is not interlaced,
390 this will set pass to 1 */
391 num_pass = png_set_interlace_handling(png_ptr);
392 /* loop through passes */
393 for (pass = 0; pass < num_pass; pass++)
394 {
395 /* loop through image */
396 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
397 {
398 png_write_row(png_ptr, *rp);
399 }
400 }
401}
402
Guy Schalnate5a37791996-06-05 15:50:50 -0500403/* called by user to write a row of image data */
Guy Schalnat0d580581995-07-20 02:43:20 -0500404void
Guy Schalnat6d764711995-12-19 03:22:19 -0600405png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500406{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600407 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
408 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500409 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600410 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500411 {
412 png_write_start_row(png_ptr);
413 }
414
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500415#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500416 /* if interlaced and not interested in row, return */
417 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
418 {
419 switch (png_ptr->pass)
420 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600421 case 0:
Guy Schalnat0d580581995-07-20 02:43:20 -0500422 if (png_ptr->row_number & 7)
423 {
424 png_write_finish_row(png_ptr);
425 return;
426 }
427 break;
428 case 1:
429 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
430 {
431 png_write_finish_row(png_ptr);
432 return;
433 }
434 break;
435 case 2:
436 if ((png_ptr->row_number & 7) != 4)
437 {
438 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600439 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500440 }
441 break;
442 case 3:
443 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
444 {
445 png_write_finish_row(png_ptr);
446 return;
447 }
448 break;
449 case 4:
450 if ((png_ptr->row_number & 3) != 2)
451 {
452 png_write_finish_row(png_ptr);
453 return;
454 }
455 break;
456 case 5:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600457 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500458 {
459 png_write_finish_row(png_ptr);
460 return;
461 }
462 break;
463 case 6:
464 if (!(png_ptr->row_number & 1))
465 {
466 png_write_finish_row(png_ptr);
467 return;
468 }
469 break;
470 }
471 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600472#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500473
474 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600475 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500476 png_ptr->row_info.width = png_ptr->usr_width;
477 png_ptr->row_info.channels = png_ptr->usr_channels;
478 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600479 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
480 png_ptr->row_info.channels);
Guy Schalnat0d580581995-07-20 02:43:20 -0500481 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
482 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
483
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600484 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
485 png_debug1(3, "row_info->width = %d\n", png_ptr->row_info.width);
486 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
487 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
488 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
489 png_debug1(3, "row_info->rowbytes = %d\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490
491 /* Copy user's row into buffer, leaving room for filter byte. */
492 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500493
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500494#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 /* handle interlacing */
496 if (png_ptr->interlaced && png_ptr->pass < 6 &&
497 (png_ptr->transformations & PNG_INTERLACE))
498 {
499 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600500 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 /* this should always get caught above, but still ... */
502 if (!(png_ptr->row_info.width))
503 {
504 png_write_finish_row(png_ptr);
505 return;
506 }
507 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600508#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500509
510 /* handle other transformations */
511 if (png_ptr->transformations)
512 png_do_write_transformations(png_ptr);
513
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Guy Schalnat0d580581995-07-20 02:43:20 -0500516}
517
Guy Schalnat0f716451995-11-28 11:22:13 -0600518#if defined(PNG_WRITE_FLUSH_SUPPORTED)
519/* Set the automatic flush interval or 0 to turn flushing off */
520void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600521png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600522{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500523 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600524 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600525}
526
527/* flush the current output buffers now */
528void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600529png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600530{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600531 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600532
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500534 /* We have already written out all of the data */
535 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600536 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600537
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600538 do
539 {
540 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600541
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600542 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600543 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600544 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600545
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600546 /* check for compression errors */
547 if (ret != Z_OK)
548 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600550 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600551 else
552 png_error(png_ptr, "zlib error");
553 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600554
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600556 {
557 /* write the IDAT and reset the zlib output buffer */
558 png_write_IDAT(png_ptr, png_ptr->zbuf,
559 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600560 png_ptr->zstream.next_out = png_ptr->zbuf;
561 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600562 wrote_IDAT = 1;
563 }
564 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600565
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600566 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600568 {
569 /* write the IDAT and reset the zlib output buffer */
570 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600571 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
572 png_ptr->zstream.next_out = png_ptr->zbuf;
573 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600574 }
575 png_ptr->flush_rows = 0;
576 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600577}
578#endif /* PNG_WRITE_FLUSH_SUPPORTED */
579
Guy Schalnate5a37791996-06-05 15:50:50 -0500580/* free all memory used by the write */
581void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600582png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500583{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600584 png_structp png_ptr = NULL;
585 png_infop info_ptr = NULL;
586
Andreas Dilger47a0c421997-05-16 02:46:07 -0500587 png_debug(1, "in png_destroy_write_struct\n");
588 if (png_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600589 png_ptr = *png_ptr_ptr;
590
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600592 info_ptr = *info_ptr_ptr;
593
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500595 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600596#ifdef PNG_WRITE_tEXt_SUPPORTED
597 png_free(png_ptr, info_ptr->text);
598#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600599#if defined(PNG_READ_pCAL_SUPPORTED)
600 png_free(png_ptr, info_ptr->pcal_purpose);
601 png_free(png_ptr, info_ptr->pcal_units);
602 if (info_ptr->pcal_params != NULL)
603 {
604 int i;
605 for (i = 0; i < info_ptr->pcal_nparams; i++)
606 {
607 png_free(png_ptr, info_ptr->pcal_params[i]);
608 }
609 png_free(png_ptr, info_ptr->pcal_params);
610 }
611#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600612 png_destroy_struct((png_voidp)info_ptr);
613 *info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500614 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600615
Andreas Dilger47a0c421997-05-16 02:46:07 -0500616 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500617 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600618 png_write_destroy(png_ptr);
619 png_destroy_struct((png_voidp)png_ptr);
620 *png_ptr_ptr = (png_structp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500621 }
622}
623
624
Andreas Dilger47a0c421997-05-16 02:46:07 -0500625/* Free any memory used in png_ptr struct (old method) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500626void
Guy Schalnat6d764711995-12-19 03:22:19 -0600627png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500628{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600629 jmp_buf tmp_jmp; /* save jump buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -0500630 png_error_ptr error_fn;
631 png_error_ptr warning_fn;
632 png_voidp error_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500633
Andreas Dilger47a0c421997-05-16 02:46:07 -0500634 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500635 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600636 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500637
Guy Schalnat0d580581995-07-20 02:43:20 -0500638 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600639 png_free(png_ptr, png_ptr->zbuf);
640 png_free(png_ptr, png_ptr->row_buf);
641 png_free(png_ptr, png_ptr->prev_row);
642 png_free(png_ptr, png_ptr->sub_row);
643 png_free(png_ptr, png_ptr->up_row);
644 png_free(png_ptr, png_ptr->avg_row);
645 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600646#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600647 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600648#endif /* PNG_TIME_RFC1123_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500649#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
650 png_free(png_ptr, png_ptr->prev_filters);
651 png_free(png_ptr, png_ptr->filter_weights);
652 png_free(png_ptr, png_ptr->inv_filter_weights);
653 png_free(png_ptr, png_ptr->filter_costs);
654 png_free(png_ptr, png_ptr->inv_filter_costs);
655#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500656
Guy Schalnat0d580581995-07-20 02:43:20 -0500657 /* reset structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500658 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Guy Schalnate5a37791996-06-05 15:50:50 -0500659
660 error_fn = png_ptr->error_fn;
661 warning_fn = png_ptr->warning_fn;
662 error_ptr = png_ptr->error_ptr;
663
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500664 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500665
666 png_ptr->error_fn = error_fn;
667 png_ptr->warning_fn = warning_fn;
668 png_ptr->error_ptr = error_ptr;
669
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500670 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
671}
Guy Schalnate5a37791996-06-05 15:50:50 -0500672
Andreas Dilger47a0c421997-05-16 02:46:07 -0500673/* Allow the application to select one or more row filters to use. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500674void
Guy Schalnate5a37791996-06-05 15:50:50 -0500675png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500676{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500677 png_debug(1, "in png_set_filter\n");
678 /* We allow 'method' only for future expansion of the base filter method. */
679 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500680 {
681 switch (filters & (PNG_ALL_FILTERS | 0x07))
682 {
683 case 5:
684 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500685 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
686 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
687 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
688 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
689 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
690 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -0500691 default: png_ptr->do_filter = (png_byte)filters; break;
692 }
693
Andreas Dilger47a0c421997-05-16 02:46:07 -0500694 /* If we have allocated the row_buf, this means we have already started
695 * with the image and we should have allocated all of the filter buffers
696 * that have been selected. If prev_row isn't already allocated, then
697 * it is too late to start using the filters that need it, since we
698 * will be missing the data in the previous row. If an application
699 * wants to start and stop using particular filters during compression,
700 * it should start out with all of the filters, and then add and
701 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500702 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500704 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500705 if (png_ptr->do_filter & PNG_FILTER_SUB && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500706 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500707 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -0500708 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500710 }
711
Andreas Dilger47a0c421997-05-16 02:46:07 -0500712 if (png_ptr->do_filter & PNG_FILTER_UP && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500713 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500714 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500715 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500717 png_ptr->do_filter &= ~PNG_FILTER_UP;
718 }
719 else
720 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -0500722 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -0500724 }
725 }
726
Andreas Dilger47a0c421997-05-16 02:46:07 -0500727 if (png_ptr->do_filter & PNG_FILTER_AVG && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500728 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500729 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500730 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500732 png_ptr->do_filter &= ~PNG_FILTER_AVG;
733 }
734 else
735 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500736 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -0500737 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500738 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -0500739 }
740 }
741
Andreas Dilger47a0c421997-05-16 02:46:07 -0500742 if (png_ptr->do_filter & PNG_FILTER_PAETH &&
743 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500744 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500745 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500746 {
747 png_warning(png_ptr, "Can't add Paeth filter after starting");
748 png_ptr->do_filter &= ~PNG_FILTER_PAETH;
749 }
750 else
751 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600752 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -0500753 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -0500755 }
756 }
757
758 if (png_ptr->do_filter == PNG_NO_FILTERS)
759 png_ptr->do_filter = PNG_FILTER_NONE;
760 }
761 }
762 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500763 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500764}
765
Andreas Dilger47a0c421997-05-16 02:46:07 -0500766/* This allows us to influence the way in which libpng chooses the "best"
767 * filter for the current scanline. While the "minimum-sum-of-absolute-
768 * differences metric is relatively fast and effective, there is some
769 * question as to whether it can be improved upon by trying to keep the
770 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600771 * better compression.
772 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500773#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
774void
775png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
776 int num_weights, png_doublep filter_weights,
777 png_doublep filter_costs)
778{
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600779#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500780 int i;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600781#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500782
783 png_debug(1, "in png_set_filter_heuristics\n");
784 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
785 {
786 png_warning(png_ptr, "Unknown filter heuristic method");
787 return;
788 }
789
790 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
791 {
792 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
793 }
794
795 if (num_weights < 0 || filter_weights == NULL ||
796 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
797 {
798 num_weights = 0;
799 }
800
801 png_ptr->num_prev_filters = num_weights;
802 png_ptr->heuristic_method = heuristic_method;
803
804 if (num_weights > 0)
805 {
806 if (png_ptr->prev_filters == NULL)
807 {
808 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
809 sizeof(png_byte) * num_weights);
810
811 /* To make sure that the weighting starts out fairly */
812 for (i = 0; i < num_weights; i++)
813 {
814 png_ptr->prev_filters[i] = 255;
815 }
816 }
817
818 if (png_ptr->filter_weights == NULL)
819 {
820 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
821 sizeof(png_uint_16) * num_weights);
822
823 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
824 sizeof(png_uint_16) * num_weights);
825
826 for (i = 0; i < num_weights; i++)
827 {
828 png_ptr->inv_filter_weights[i] =
829 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
830 }
831 }
832
833 for (i = 0; i < num_weights; i++)
834 {
835 if (filter_weights[i] < 0.0)
836 {
837 png_ptr->inv_filter_weights[i] =
838 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
839 }
840 else
841 {
842 png_ptr->inv_filter_weights[i] =
843 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
844 png_ptr->filter_weights[i] =
845 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
846 }
847 }
848 }
849
850 /* If, in the future, there are other filter methods, this would
851 * need to be based on png_ptr->filter.
852 */
853 if (png_ptr->filter_costs == NULL)
854 {
855 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
856 sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST);
857
858 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
859 sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST);
860
861 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
862 {
863 png_ptr->inv_filter_costs[i] =
864 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
865 }
866 }
867
868 /* Here is where we set the relative costs of the different filters. We
869 * should take the desired compression level into account when setting
870 * the costs, so that Paeth, for instance, has a high relative cost at low
871 * compression levels, while it has a lower relative cost at higher
872 * compression settings. The filter types are in order of increasing
873 * relative cost, so it would be possible to do this with an algorithm.
874 */
875 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
876 {
877 if (filter_costs == NULL || filter_costs[i] < 0.0)
878 {
879 png_ptr->inv_filter_costs[i] =
880 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
881 }
882 else if (filter_costs[i] >= 1.0)
883 {
884 png_ptr->inv_filter_costs[i] =
885 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
886 png_ptr->filter_costs[i] =
887 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
888 }
889 }
890}
891#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
892
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500893void
Guy Schalnat6d764711995-12-19 03:22:19 -0600894png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500895{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500897 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500898 png_ptr->zlib_level = level;
899}
900
901void
Guy Schalnat6d764711995-12-19 03:22:19 -0600902png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500903{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500905 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600906 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500907}
908
909void
Guy Schalnat6d764711995-12-19 03:22:19 -0600910png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500911{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500912 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500913 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500914 png_ptr->zlib_strategy = strategy;
915}
916
917void
Guy Schalnat6d764711995-12-19 03:22:19 -0600918png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500919{
Guy Schalnate5a37791996-06-05 15:50:50 -0500920 if (window_bits > 15)
921 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
922 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500923 png_ptr->zlib_window_bits = window_bits;
924}
925
926void
Guy Schalnat6d764711995-12-19 03:22:19 -0600927png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500928{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500929 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500930 if (method != 8)
931 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
932 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500933 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -0500934}
935