blob: afb4b43be8fa1b1cc65306b37ea8bd44352afc12 [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-Pehrson08a33431998-03-07 06:06:55 -06004 * libpng 1.00
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-Pehrson08a33431998-03-07 06:06:55 -06009 * March 7, 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,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060037 info_ptr->filter_type,
38#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
39 info_ptr->interlace_type);
40#else
41 0);
42#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050043 /* the rest of these check to see if the valid field has the appropriate
44 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050045#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060046 if (info_ptr->valid & PNG_INFO_gAMA)
47 png_write_gAMA(png_ptr, info_ptr->gamma);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050048#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060049#if defined(PNG_WRITE_sRGB_SUPPORTED)
50 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060051 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060052#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050053#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060054 if (info_ptr->valid & PNG_INFO_sBIT)
55 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050056#endif
57#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060058 if (info_ptr->valid & PNG_INFO_cHRM)
Guy Schalnat0d580581995-07-20 02:43:20 -050059 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060060 info_ptr->x_white, info_ptr->y_white,
61 info_ptr->x_red, info_ptr->y_red,
62 info_ptr->x_green, info_ptr->y_green,
63 info_ptr->x_blue, info_ptr->y_blue);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050064#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060065 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_PLTE(png_ptr, info_ptr->palette,
67 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -050069 png_error(png_ptr, "Valid palette required for paletted images\n");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060070
Guy Schalnat51f0eb41995-09-26 05:22:39 -050071#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060072 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060073 {
74#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
75 /* invert the alpha channel (in tRNS) */
76 if (png_ptr->transformations & PNG_INVERT_ALPHA &&
77 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
78 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060079 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060080 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060081 info_ptr->trans[j] = 255 - info_ptr->trans[j];
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060082 }
83#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060084 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
85 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060086 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050087#endif
88#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060089 if (info_ptr->valid & PNG_INFO_bKGD)
90 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050091#endif
92#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060093 if (info_ptr->valid & PNG_INFO_hIST)
94 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050095#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050096#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060097 if (info_ptr->valid & PNG_INFO_oFFs)
98 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
99 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500100#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101#if defined(PNG_WRITE_pCAL_SUPPORTED)
102 if (info_ptr->valid & PNG_INFO_pCAL)
103 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
104 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
105 info_ptr->pcal_units, info_ptr->pcal_params);
106#endif
107#if defined(PNG_WRITE_pHYs_SUPPORTED)
108 if (info_ptr->valid & PNG_INFO_pHYs)
109 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
110 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
111#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500112#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600113 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500114 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600115 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Guy Schalnate5a37791996-06-05 15:50:50 -0500116 png_ptr->flags |= PNG_FLAG_WROTE_tIME;
117 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500118#endif
119#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500120 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500121 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500122 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500123 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
124 info_ptr->text[i].compression);
125 /* If we want a compressed text chunk */
126 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
127 {
128#if defined(PNG_WRITE_zTXt_SUPPORTED)
129 /* write compressed chunk */
130 png_write_zTXt(png_ptr, info_ptr->text[i].key,
131 info_ptr->text[i].text, info_ptr->text[i].text_length,
132 info_ptr->text[i].compression);
133#else
134 png_warning(png_ptr, "Unable to write compressed text\n");
135#endif
136 /* Mark this chunk as written */
137 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
138 }
139 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
140 {
141#if defined(PNG_WRITE_tEXt_SUPPORTED)
142 /* write uncompressed chunk */
143 png_write_tEXt(png_ptr, info_ptr->text[i].key,
144 info_ptr->text[i].text, info_ptr->text[i].text_length);
145#else
146 png_warning(png_ptr, "Unable to write uncompressed text\n");
147#endif
148 /* Mark this chunk as written */
149 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
150 }
151 }
152#endif
153}
Guy Schalnat0d580581995-07-20 02:43:20 -0500154
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600156 * time information, you can pass NULL for info. If you already wrote these
157 * in png_write_info(), do not write them again here. If you have long
158 * comments, I suggest writing them here, and compressing them.
159 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500160void
161png_write_end(png_structp png_ptr, png_infop info_ptr)
162{
163 png_debug(1, "in png_write_end\n");
164 if (!(png_ptr->mode & PNG_HAVE_IDAT))
165 png_error(png_ptr, "No IDATs written into file");
166
167 /* see if user wants us to write information chunks */
168 if (info_ptr != NULL)
169 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600170#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600172#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173#if defined(PNG_WRITE_tIME_SUPPORTED)
174 /* check to see if user has supplied a time chunk */
175 if (info_ptr->valid & PNG_INFO_tIME &&
176 !(png_ptr->flags & PNG_FLAG_WROTE_tIME))
177 png_write_tIME(png_ptr, &(info_ptr->mod_time));
178#endif
179#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
180 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600181 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500182 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
184 info_ptr->text[i].compression);
185 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500186 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500187#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600189 png_write_zTXt(png_ptr, info_ptr->text[i].key,
190 info_ptr->text[i].text, info_ptr->text[i].text_length,
191 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500192#else
193 png_warning(png_ptr, "Unable to write compressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500194#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 /* Mark this chunk as written */
196 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500197 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500198 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500199 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500200#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500201 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600202 png_write_tEXt(png_ptr, info_ptr->text[i].key,
203 info_ptr->text[i].text, info_ptr->text[i].text_length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500204#else
205 png_warning(png_ptr, "Unable to write uncompressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500206#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500207
Andreas Dilger47a0c421997-05-16 02:46:07 -0500208 /* Mark this chunk as written */
209 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500210 }
211 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600212#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500213 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500214
215 png_ptr->mode |= PNG_AFTER_IDAT;
216
Andreas Dilger47a0c421997-05-16 02:46:07 -0500217 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500218 png_write_IEND(png_ptr);
219}
220
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600221#if defined(PNG_TIME_RFC1123_SUPPORTED)
222/* Convert the supplied time into an RFC 1123 string suitable for use in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600223 * a "Creation Time" or other text-based time string.
224 */
225png_charp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600226png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600227{
228 const char *short_months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
229 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
230
231 if (png_ptr->time_buffer == NULL)
232 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600233 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
234 sizeof(char)));
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600235 }
236
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600237#ifdef USE_FAR_KEYWORD
238 {
239 char near_time_buf[29];
240 sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000",
241 ptime->day % 31, short_months[ptime->month],
242 ptime->year, ptime->hour % 24, ptime->minute % 60,
243 ptime->second % 61);
244 png_memcpy(png_ptr->time_buffer, near_time_buf,
245 29*sizeof(char));
246 }
247#else
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600248 sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600249 ptime->day % 31, short_months[ptime->month],
250 ptime->year, ptime->hour % 24, ptime->minute % 60,
251 ptime->second % 61);
252#endif
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600253 return ((png_charp)png_ptr->time_buffer);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600254}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600255#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600256
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500257#if defined(PNG_WRITE_tIME_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500258void
Guy Schalnat6d764711995-12-19 03:22:19 -0600259png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500260{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600262 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
263 ptime->month = (png_byte)(ttime->tm_mon + 1);
264 ptime->day = (png_byte)ttime->tm_mday;
265 ptime->hour = (png_byte)ttime->tm_hour;
266 ptime->minute = (png_byte)ttime->tm_min;
267 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500268}
269
270void
Guy Schalnat6d764711995-12-19 03:22:19 -0600271png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500272{
273 struct tm *tbuf;
274
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500276 tbuf = gmtime(&ttime);
277 png_convert_from_struct_tm(ptime, tbuf);
278}
Guy Schalnat6d764711995-12-19 03:22:19 -0600279#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500280
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500282png_structp
283png_create_write_struct(png_const_charp user_png_ver, voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600284 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500285{
Guy Schalnate5a37791996-06-05 15:50:50 -0500286 png_structp png_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600287#ifdef USE_FAR_KEYWORD
288 jmp_buf jmpbuf;
289#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290 png_debug(1, "in png_create_write_struct\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500291 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
292 {
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600293 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500294 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600295#ifdef USE_FAR_KEYWORD
296 if (setjmp(jmpbuf))
297#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500298 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600299#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500300 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600301 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -0500302 png_destroy_struct(png_ptr);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600303 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500304 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600305#ifdef USE_FAR_KEYWORD
306 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
307#endif
308 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500309
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
311 * we must recompile any applications that use any older library version.
312 * For versions after libpng 1.0, we will be compatible, so we need
313 * only check the first digit.
314 */
315 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
316 (png_libpng_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -0500317 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 png_error(png_ptr,
319 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -0500320 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500321
322 /* initialize zbuf - compression buffer */
323 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600324 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
325 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500326
327 png_set_write_fn(png_ptr, NULL, NULL, NULL);
328
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600329#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
330 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
331 1, NULL, NULL);
332#endif
333
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600334 return ((png_structp)png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500335}
336
337
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500339void
340png_write_init(png_structp png_ptr)
341{
342 jmp_buf tmp_jmp; /* to save current jump buffer */
343
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 png_debug(1, "in png_write_init\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500345 /* save jump buffer and error functions */
346 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
347
348 /* reset all variables to 0 */
349 png_memset(png_ptr, 0, sizeof (png_struct));
350
351 /* restore jump buffer */
352 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
353
354 /* initialize zbuf - compression buffer */
355 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600356 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
357 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500358 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500359
360#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
361 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
362 1, NULL, NULL);
363#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500364}
365
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600366/* Write a few rows of image data. If the image is interlaced,
367 * either you will have to write the 7 sub images, or, if you
368 * have called png_set_interlace_handling(), you will have to
369 * "write" the image seven times.
370 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500371void
Guy Schalnat6d764711995-12-19 03:22:19 -0600372png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500373 png_uint_32 num_rows)
374{
375 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600376 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500377
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500379 /* loop through the rows */
380 for (i = 0, rp = row; i < num_rows; i++, rp++)
381 {
382 png_write_row(png_ptr, *rp);
383 }
384}
385
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600386/* Write the image. You only need to call this function once, even
387 * if you are writing an interlaced image.
388 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500389void
Guy Schalnat6d764711995-12-19 03:22:19 -0600390png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500391{
392 png_uint_32 i; /* row index */
393 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600394 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500395
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600397#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500398 /* intialize interlace handling. If image is not interlaced,
399 this will set pass to 1 */
400 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600401#else
402 num_pass = 1;
403#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500404 /* loop through passes */
405 for (pass = 0; pass < num_pass; pass++)
406 {
407 /* loop through image */
408 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
409 {
410 png_write_row(png_ptr, *rp);
411 }
412 }
413}
414
Guy Schalnate5a37791996-06-05 15:50:50 -0500415/* called by user to write a row of image data */
Guy Schalnat0d580581995-07-20 02:43:20 -0500416void
Guy Schalnat6d764711995-12-19 03:22:19 -0600417png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500418{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600419 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
420 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500421 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600422 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500423 {
424 png_write_start_row(png_ptr);
425 }
426
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500427#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500428 /* if interlaced and not interested in row, return */
429 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
430 {
431 switch (png_ptr->pass)
432 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600433 case 0:
Guy Schalnat0d580581995-07-20 02:43:20 -0500434 if (png_ptr->row_number & 7)
435 {
436 png_write_finish_row(png_ptr);
437 return;
438 }
439 break;
440 case 1:
441 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
442 {
443 png_write_finish_row(png_ptr);
444 return;
445 }
446 break;
447 case 2:
448 if ((png_ptr->row_number & 7) != 4)
449 {
450 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600451 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500452 }
453 break;
454 case 3:
455 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
456 {
457 png_write_finish_row(png_ptr);
458 return;
459 }
460 break;
461 case 4:
462 if ((png_ptr->row_number & 3) != 2)
463 {
464 png_write_finish_row(png_ptr);
465 return;
466 }
467 break;
468 case 5:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600469 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500470 {
471 png_write_finish_row(png_ptr);
472 return;
473 }
474 break;
475 case 6:
476 if (!(png_ptr->row_number & 1))
477 {
478 png_write_finish_row(png_ptr);
479 return;
480 }
481 break;
482 }
483 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600484#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500485
486 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600487 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500488 png_ptr->row_info.width = png_ptr->usr_width;
489 png_ptr->row_info.channels = png_ptr->usr_channels;
490 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600491 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
492 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600493
Guy Schalnat0d580581995-07-20 02:43:20 -0500494 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
495 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
496
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600497 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
498 png_debug1(3, "row_info->width = %d\n", png_ptr->row_info.width);
499 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
500 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
501 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
502 png_debug1(3, "row_info->rowbytes = %d\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503
504 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600505 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600506 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500507
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500508#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500509 /* handle interlacing */
510 if (png_ptr->interlaced && png_ptr->pass < 6 &&
511 (png_ptr->transformations & PNG_INTERLACE))
512 {
513 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600514 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500515 /* this should always get caught above, but still ... */
516 if (!(png_ptr->row_info.width))
517 {
518 png_write_finish_row(png_ptr);
519 return;
520 }
521 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600522#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500523
524 /* handle other transformations */
525 if (png_ptr->transformations)
526 png_do_write_transformations(png_ptr);
527
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500529 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600530
531 if (png_ptr->write_row_fn != NULL)
532 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500533}
534
Guy Schalnat0f716451995-11-28 11:22:13 -0600535#if defined(PNG_WRITE_FLUSH_SUPPORTED)
536/* Set the automatic flush interval or 0 to turn flushing off */
537void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600538png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600539{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600541 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600542}
543
544/* flush the current output buffers now */
545void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600546png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600547{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600548 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600549
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500551 /* We have already written out all of the data */
552 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600553 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600554
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600555 do
556 {
557 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600558
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600559 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600560 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600561 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600562
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600563 /* check for compression errors */
564 if (ret != Z_OK)
565 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500566 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600568 else
569 png_error(png_ptr, "zlib error");
570 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600571
Andreas Dilger47a0c421997-05-16 02:46:07 -0500572 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600573 {
574 /* write the IDAT and reset the zlib output buffer */
575 png_write_IDAT(png_ptr, png_ptr->zbuf,
576 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600577 png_ptr->zstream.next_out = png_ptr->zbuf;
578 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600579 wrote_IDAT = 1;
580 }
581 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600582
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600583 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600584 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600585 {
586 /* write the IDAT and reset the zlib output buffer */
587 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600588 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
589 png_ptr->zstream.next_out = png_ptr->zbuf;
590 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600591 }
592 png_ptr->flush_rows = 0;
593 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600594}
595#endif /* PNG_WRITE_FLUSH_SUPPORTED */
596
Guy Schalnate5a37791996-06-05 15:50:50 -0500597/* free all memory used by the write */
598void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600599png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500600{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601 png_structp png_ptr = NULL;
602 png_infop info_ptr = NULL;
603
Andreas Dilger47a0c421997-05-16 02:46:07 -0500604 png_debug(1, "in png_destroy_write_struct\n");
605 if (png_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600606 png_ptr = *png_ptr_ptr;
607
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600609 info_ptr = *info_ptr_ptr;
610
Andreas Dilger47a0c421997-05-16 02:46:07 -0500611 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500612 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600613#ifdef PNG_WRITE_tEXt_SUPPORTED
614 png_free(png_ptr, info_ptr->text);
615#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600616#if defined(PNG_READ_pCAL_SUPPORTED)
617 png_free(png_ptr, info_ptr->pcal_purpose);
618 png_free(png_ptr, info_ptr->pcal_units);
619 if (info_ptr->pcal_params != NULL)
620 {
621 int i;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600622 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600623 {
624 png_free(png_ptr, info_ptr->pcal_params[i]);
625 }
626 png_free(png_ptr, info_ptr->pcal_params);
627 }
628#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600629 png_destroy_struct((png_voidp)info_ptr);
630 *info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500631 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600632
Andreas Dilger47a0c421997-05-16 02:46:07 -0500633 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500634 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600635 png_write_destroy(png_ptr);
636 png_destroy_struct((png_voidp)png_ptr);
637 *png_ptr_ptr = (png_structp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500638 }
639}
640
641
Andreas Dilger47a0c421997-05-16 02:46:07 -0500642/* Free any memory used in png_ptr struct (old method) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500643void
Guy Schalnat6d764711995-12-19 03:22:19 -0600644png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500645{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600646 jmp_buf tmp_jmp; /* save jump buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -0500647 png_error_ptr error_fn;
648 png_error_ptr warning_fn;
649 png_voidp error_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500650
Andreas Dilger47a0c421997-05-16 02:46:07 -0500651 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500652 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600653 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500654
Guy Schalnat0d580581995-07-20 02:43:20 -0500655 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600656 png_free(png_ptr, png_ptr->zbuf);
657 png_free(png_ptr, png_ptr->row_buf);
658 png_free(png_ptr, png_ptr->prev_row);
659 png_free(png_ptr, png_ptr->sub_row);
660 png_free(png_ptr, png_ptr->up_row);
661 png_free(png_ptr, png_ptr->avg_row);
662 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600663#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600664 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600665#endif /* PNG_TIME_RFC1123_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500666#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
667 png_free(png_ptr, png_ptr->prev_filters);
668 png_free(png_ptr, png_ptr->filter_weights);
669 png_free(png_ptr, png_ptr->inv_filter_weights);
670 png_free(png_ptr, png_ptr->filter_costs);
671 png_free(png_ptr, png_ptr->inv_filter_costs);
672#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500673
Guy Schalnat0d580581995-07-20 02:43:20 -0500674 /* reset structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500675 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Guy Schalnate5a37791996-06-05 15:50:50 -0500676
677 error_fn = png_ptr->error_fn;
678 warning_fn = png_ptr->warning_fn;
679 error_ptr = png_ptr->error_ptr;
680
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500681 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500682
683 png_ptr->error_fn = error_fn;
684 png_ptr->warning_fn = warning_fn;
685 png_ptr->error_ptr = error_ptr;
686
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500687 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
688}
Guy Schalnate5a37791996-06-05 15:50:50 -0500689
Andreas Dilger47a0c421997-05-16 02:46:07 -0500690/* Allow the application to select one or more row filters to use. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500691void
Guy Schalnate5a37791996-06-05 15:50:50 -0500692png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500693{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500694 png_debug(1, "in png_set_filter\n");
695 /* We allow 'method' only for future expansion of the base filter method. */
696 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500697 {
698 switch (filters & (PNG_ALL_FILTERS | 0x07))
699 {
700 case 5:
701 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500702 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
703 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
704 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
705 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
706 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
707 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -0500708 default: png_ptr->do_filter = (png_byte)filters; break;
709 }
710
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 /* If we have allocated the row_buf, this means we have already started
712 * with the image and we should have allocated all of the filter buffers
713 * that have been selected. If prev_row isn't already allocated, then
714 * it is too late to start using the filters that need it, since we
715 * will be missing the data in the previous row. If an application
716 * wants to start and stop using particular filters during compression,
717 * it should start out with all of the filters, and then add and
718 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500719 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500721 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722 if (png_ptr->do_filter & PNG_FILTER_SUB && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500723 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600725 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500726 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500727 }
728
Andreas Dilger47a0c421997-05-16 02:46:07 -0500729 if (png_ptr->do_filter & PNG_FILTER_UP && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500730 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500732 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500733 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500734 png_ptr->do_filter &= ~PNG_FILTER_UP;
735 }
736 else
737 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500738 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600739 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500740 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -0500741 }
742 }
743
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744 if (png_ptr->do_filter & PNG_FILTER_AVG && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500745 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500747 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500748 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500749 png_ptr->do_filter &= ~PNG_FILTER_AVG;
750 }
751 else
752 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500753 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600754 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500755 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -0500756 }
757 }
758
Andreas Dilger47a0c421997-05-16 02:46:07 -0500759 if (png_ptr->do_filter & PNG_FILTER_PAETH &&
760 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500761 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500763 {
764 png_warning(png_ptr, "Can't add Paeth filter after starting");
765 png_ptr->do_filter &= ~PNG_FILTER_PAETH;
766 }
767 else
768 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600769 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600770 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500771 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -0500772 }
773 }
774
775 if (png_ptr->do_filter == PNG_NO_FILTERS)
776 png_ptr->do_filter = PNG_FILTER_NONE;
777 }
778 }
779 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500780 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500781}
782
Andreas Dilger47a0c421997-05-16 02:46:07 -0500783/* This allows us to influence the way in which libpng chooses the "best"
784 * filter for the current scanline. While the "minimum-sum-of-absolute-
785 * differences metric is relatively fast and effective, there is some
786 * question as to whether it can be improved upon by trying to keep the
787 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600788 * better compression.
789 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500790#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
791void
792png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
793 int num_weights, png_doublep filter_weights,
794 png_doublep filter_costs)
795{
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600796#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 int i;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600798#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500799
800 png_debug(1, "in png_set_filter_heuristics\n");
801 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
802 {
803 png_warning(png_ptr, "Unknown filter heuristic method");
804 return;
805 }
806
807 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
808 {
809 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
810 }
811
812 if (num_weights < 0 || filter_weights == NULL ||
813 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
814 {
815 num_weights = 0;
816 }
817
818 png_ptr->num_prev_filters = num_weights;
819 png_ptr->heuristic_method = heuristic_method;
820
821 if (num_weights > 0)
822 {
823 if (png_ptr->prev_filters == NULL)
824 {
825 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600826 (png_uint_32)(sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827
828 /* To make sure that the weighting starts out fairly */
829 for (i = 0; i < num_weights; i++)
830 {
831 png_ptr->prev_filters[i] = 255;
832 }
833 }
834
835 if (png_ptr->filter_weights == NULL)
836 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600837 png_ptr->filter_weights = png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600838 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500839
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600840 png_ptr->inv_filter_weights = png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600841 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500842
843 for (i = 0; i < num_weights; i++)
844 {
845 png_ptr->inv_filter_weights[i] =
846 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
847 }
848 }
849
850 for (i = 0; i < num_weights; i++)
851 {
852 if (filter_weights[i] < 0.0)
853 {
854 png_ptr->inv_filter_weights[i] =
855 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
856 }
857 else
858 {
859 png_ptr->inv_filter_weights[i] =
860 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
861 png_ptr->filter_weights[i] =
862 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
863 }
864 }
865 }
866
867 /* If, in the future, there are other filter methods, this would
868 * need to be based on png_ptr->filter.
869 */
870 if (png_ptr->filter_costs == NULL)
871 {
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600872 png_ptr->filter_costs = png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600873 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500874
875 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600876 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500877
878 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
879 {
880 png_ptr->inv_filter_costs[i] =
881 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
882 }
883 }
884
885 /* Here is where we set the relative costs of the different filters. We
886 * should take the desired compression level into account when setting
887 * the costs, so that Paeth, for instance, has a high relative cost at low
888 * compression levels, while it has a lower relative cost at higher
889 * compression settings. The filter types are in order of increasing
890 * relative cost, so it would be possible to do this with an algorithm.
891 */
892 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
893 {
894 if (filter_costs == NULL || filter_costs[i] < 0.0)
895 {
896 png_ptr->inv_filter_costs[i] =
897 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
898 }
899 else if (filter_costs[i] >= 1.0)
900 {
901 png_ptr->inv_filter_costs[i] =
902 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
903 png_ptr->filter_costs[i] =
904 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
905 }
906 }
907}
908#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
909
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500910void
Guy Schalnat6d764711995-12-19 03:22:19 -0600911png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500912{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500914 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500915 png_ptr->zlib_level = level;
916}
917
918void
Guy Schalnat6d764711995-12-19 03:22:19 -0600919png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500920{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500921 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500922 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600923 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500924}
925
926void
Guy Schalnat6d764711995-12-19 03:22:19 -0600927png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500928{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500929 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500930 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500931 png_ptr->zlib_strategy = strategy;
932}
933
934void
Guy Schalnat6d764711995-12-19 03:22:19 -0600935png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500936{
Guy Schalnate5a37791996-06-05 15:50:50 -0500937 if (window_bits > 15)
938 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
939 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500940 png_ptr->zlib_window_bits = window_bits;
941}
942
943void
Guy Schalnat6d764711995-12-19 03:22:19 -0600944png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500945{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500946 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500947 if (method != 8)
948 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
949 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500950 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -0500951}
952
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600953void
954png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
955{
956 png_ptr->write_row_fn = write_row_fn;
957}
958
959#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
960void
961png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
962 write_user_transform_fn)
963{
964 png_debug(1, "in png_set_write_user_transform_fn\n");
965 png_ptr->transformations |= PNG_USER_TRANSFORM;
966 png_ptr->write_user_transform_fn = write_user_transform_fn;
967}
968#endif
969