blob: 0016d2c22d277f9cec5121db589811dae2775fe8 [file] [log] [blame]
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwrite.c - general routines to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson5ade7ed2009-09-30 15:11:49 -05004 * Last changed in libpng 1.4.0 [September 30, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050014/* Get internal access to png.h */
Guy Schalnat0d580581995-07-20 02:43:20 -050015#include "png.h"
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -060016#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050017#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Andreas Dilger47a0c421997-05-16 02:46:07 -050019/* Writes all the PNG information. This is the suggested way to use the
20 * library. If you have a new chunk to add, make a function to write it,
21 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050022 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050023 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
24 * the chunk, as that will keep the code from breaking if you want to just
25 * write a plain PNG file. If you have long comments, I suggest writing
26 * them in png_write_end(), and compressing them.
27 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050028void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060029png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050030{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050031 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050032
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060033 if (png_ptr == NULL || info_ptr == NULL)
34 return;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060035 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
36 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050037 /* Write PNG signature */
38 png_write_sig(png_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050039#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050040 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060041 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050042 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060043 png_ptr->mng_features_permitted=0;
44 }
45#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050046 /* Write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060047 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
48 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060049 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050050#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060051 info_ptr->interlace_type);
52#else
53 0);
54#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050055 /* The rest of these check to see if the valid field has the appropriate
56 * flag set, and if it does, writes the chunk.
57 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050058#ifdef PNG_WRITE_gAMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060059 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060060 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060061# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060062 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060063#else
64#ifdef PNG_FIXED_POINT_SUPPORTED
65 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060066# endif
67#endif
68 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050069#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050070#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060071 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060072 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050074#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060075 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060076 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060077 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
78#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050079#ifdef PNG_WRITE_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060080 if (info_ptr->valid & PNG_INFO_sBIT)
81 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050082#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050083#ifdef PNG_WRITE_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060084 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060085 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060086#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050087 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060088 info_ptr->x_white, info_ptr->y_white,
89 info_ptr->x_red, info_ptr->y_red,
90 info_ptr->x_green, info_ptr->y_green,
91 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060092#else
93# ifdef PNG_FIXED_POINT_SUPPORTED
94 png_write_cHRM_fixed(png_ptr,
95 info_ptr->int_x_white, info_ptr->int_y_white,
96 info_ptr->int_x_red, info_ptr->int_y_red,
97 info_ptr->int_x_green, info_ptr->int_y_green,
98 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060099# endif
100#endif
101 }
102#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500103#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600104 if (info_ptr->unknown_chunks_num)
105 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500106 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600107
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500108 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600109
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500110 for (up = info_ptr->unknown_chunks;
111 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
112 up++)
113 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600114 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500115 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500116 up->location && !(up->location & PNG_HAVE_PLTE) &&
117 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500118 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600119 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
120 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500121 if (up->size == 0)
122 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600123 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600124 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500125 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600126 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500127#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600128 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
129 }
130}
131
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500132void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600133png_write_info(png_structp png_ptr, png_infop info_ptr)
134{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600135#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600136 int i;
137#endif
138
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500139 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600140
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600141 if (png_ptr == NULL || info_ptr == NULL)
142 return;
143
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600144 png_write_info_before_PLTE(png_ptr, info_ptr);
145
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600146 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500147 png_write_PLTE(png_ptr, info_ptr->palette,
148 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600149 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600150 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600151
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500152#ifdef PNG_WRITE_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600153 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500154 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500155#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500156 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500157 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
158 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
159 {
160 int j;
161 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500162 info_ptr->trans_alpha[j] = (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500163 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600164#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500165 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600166 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500167 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500168#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500169#ifdef PNG_WRITE_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600170 if (info_ptr->valid & PNG_INFO_bKGD)
171 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500172#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500173#ifdef PNG_WRITE_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174 if (info_ptr->valid & PNG_INFO_hIST)
175 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500176#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500177#ifdef PNG_WRITE_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600178 if (info_ptr->valid & PNG_INFO_oFFs)
179 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
180 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500181#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500182#ifdef PNG_WRITE_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 if (info_ptr->valid & PNG_INFO_pCAL)
184 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
185 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
186 info_ptr->pcal_units, info_ptr->pcal_params);
187#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500188
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500189#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600190 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500191#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500192#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600193 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
194 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500195#else /* !FLOATING_POINT */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600196#ifdef PNG_FIXED_POINT_SUPPORTED
197 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600198 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500199#endif /* FIXED_POINT */
200#endif /* FLOATING_POINT */
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500201#else /* !WRITE_sCAL */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600202 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500203 "png_write_sCAL not supported; sCAL chunk not written");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500204#endif /* WRITE_sCAL */
205#endif /* sCAL */
206
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500207#ifdef PNG_WRITE_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500208 if (info_ptr->valid & PNG_INFO_pHYs)
209 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
210 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500211#endif /* pHYs */
212
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500213#ifdef PNG_WRITE_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600214 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500215 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600216 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600217 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500218 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500219#endif /* tIME */
220
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500221#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600222 if (info_ptr->valid & PNG_INFO_sPLT)
223 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
224 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500225#endif /* sPLT */
226
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500227#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500228 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500230 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500231 png_debug2(2, "Writing header text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500232 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500233 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600234 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600235 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500236#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500237 /* Write international chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600238 png_write_iTXt(png_ptr,
239 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600240 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600241 info_ptr->text[i].lang,
242 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600243 info_ptr->text[i].text);
244#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600245 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600246#endif
247 /* Mark this chunk as written */
248 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
249 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600251 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500253#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500254 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600256 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500257 info_ptr->text[i].compression);
258#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600259 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260#endif
261 /* Mark this chunk as written */
262 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
263 }
264 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
265 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500266#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500267 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600269 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600270 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271 /* Mark this chunk as written */
272 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500273#else
274 /* Can't get here */
275 png_warning(png_ptr, "Unable to write uncompressed text");
276#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 }
278 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500279#endif /* tEXt */
280
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500281#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600282 if (info_ptr->unknown_chunks_num)
283 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500284 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600285
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500286 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600287
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500288 for (up = info_ptr->unknown_chunks;
289 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
290 up++)
291 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600292 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500293 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600294 up->location && (up->location & PNG_HAVE_PLTE) &&
295 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500296 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600297 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
298 {
299 png_write_chunk(png_ptr, up->name, up->data, up->size);
300 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500301 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600302 }
303#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304}
Guy Schalnat0d580581995-07-20 02:43:20 -0500305
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600307 * time information, you can pass NULL for info. If you already wrote these
308 * in png_write_info(), do not write them again here. If you have long
309 * comments, I suggest writing them here, and compressing them.
310 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500311void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312png_write_end(png_structp png_ptr, png_infop info_ptr)
313{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500314 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500315
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600316 if (png_ptr == NULL)
317 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 if (!(png_ptr->mode & PNG_HAVE_IDAT))
319 png_error(png_ptr, "No IDATs written into file");
320
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500321 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 if (info_ptr != NULL)
323 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500324#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600326#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500327#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500328 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600329 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600330 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331 png_write_tIME(png_ptr, &(info_ptr->mod_time));
332#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500333#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500334 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600335 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500336 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500337 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500339 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600340 if (info_ptr->text[i].compression > 0)
341 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500342#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500343 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500344 png_write_iTXt(png_ptr,
345 info_ptr->text[i].compression,
346 info_ptr->text[i].key,
347 info_ptr->text[i].lang,
348 info_ptr->text[i].lang_key,
349 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600350#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500351 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600352#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500353 /* Mark this chunk as written */
354 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600355 }
356 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500357 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500358#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500359 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600360 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600361 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600362 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500363#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600364 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500365#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 /* Mark this chunk as written */
367 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500370 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500371#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500372 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600373 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600374 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500375#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600376 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500377#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500378
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 /* Mark this chunk as written */
380 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500381 }
382 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600383#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500384#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600385 if (info_ptr->unknown_chunks_num)
386 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500387 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500389 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500391 for (up = info_ptr->unknown_chunks;
392 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
393 up++)
394 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600395 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500396 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600397 up->location && (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500398 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600399 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
400 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600401 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600402 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500403 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404 }
405#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500406 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500407
408 png_ptr->mode |= PNG_AFTER_IDAT;
409
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500410 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500411 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500412 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
413 * and restored again in libpng-1.2.30, may cause some applications that
414 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600415 * experiences a problem, please report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500416 * png-mng-implement at lists.sf.net .
417 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500418#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500419 png_flush(png_ptr);
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500420#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500421}
422
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500423#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500424/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500425void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600426png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500427{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500428 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500429
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600430 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
431 ptime->month = (png_byte)(ttime->tm_mon + 1);
432 ptime->day = (png_byte)ttime->tm_mday;
433 ptime->hour = (png_byte)ttime->tm_hour;
434 ptime->minute = (png_byte)ttime->tm_min;
435 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500436}
437
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500438void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600439png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500440{
441 struct tm *tbuf;
442
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500443 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500444
Guy Schalnat0d580581995-07-20 02:43:20 -0500445 tbuf = gmtime(&ttime);
446 png_convert_from_struct_tm(ptime, tbuf);
447}
Guy Schalnat6d764711995-12-19 03:22:19 -0600448#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500449
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500450
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500452png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500453png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600454 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500455{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500456#ifdef PNG_USER_MEM_SUPPORTED
457 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500458 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500459}
460
461/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500462png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500463png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
464 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
465 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
466{
467#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500468 int png_cleanup_needed = 0;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600469#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500470 volatile
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600471#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500472 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600473#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600474#ifdef USE_FAR_KEYWORD
475 jmp_buf jmpbuf;
476#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600477#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500478 int i;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500479
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500480 png_debug(1, "in png_create_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500481
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500482#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600483 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500484 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500485#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600486 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500487#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600488 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500489 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600490
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500491 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500492#ifdef PNG_SET_USER_LIMITS_SUPPORTED
493 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
494 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
495#endif
496
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500497#ifdef PNG_SETJMP_SUPPORTED
498/* Applications that neglect to set up their own setjmp() and then
499 encounter a png_error() will longjmp here. Since the jmpbuf is
500 then meaningless we abort instead of returning. */
501#ifdef USE_FAR_KEYWORD
502 if (setjmp(jmpbuf))
503#else
504 if (setjmp(png_ptr->jmpbuf))
505#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500506 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500507#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600508
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500509#ifdef PNG_USER_MEM_SUPPORTED
510 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
511#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600512 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500513
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500514 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500516 i=0;
517 do
518 {
519 if (user_png_ver[i] != png_libpng_ver[i])
520 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
521 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500522 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500523
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500524 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500525 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500526 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
527 * we must recompile any applications that use any older library version.
528 * For versions after libpng 1.0, we will be compatible, so we need
529 * only check the first digit.
530 */
531 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500532 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500533 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
534 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500535#if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500536 char msg[80];
537 if (user_png_ver)
538 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500539 png_snprintf(msg, 80,
540 "Application was compiled with png.h from libpng-%.20s",
541 user_png_ver);
542 png_warning(png_ptr, msg);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500543 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500544 png_snprintf(msg, 80,
545 "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500546 png_libpng_ver);
547 png_warning(png_ptr, msg);
548#endif
549#ifdef PNG_ERROR_NUMBERS_SUPPORTED
550 png_ptr->flags=0;
551#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500552 png_warning(png_ptr,
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500553 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500554 png_cleanup_needed = 1;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500555 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500556 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500557
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500558 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500559 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500560 if (!png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500561 {
562 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
563 png_ptr->zbuf_size);
564 if (png_ptr->zbuf == NULL)
565 png_cleanup_needed = 1;
566 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500567 if (png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500568 {
569 /* Clean up PNG structure and deallocate any memory. */
570 png_free(png_ptr, png_ptr->zbuf);
571 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500572#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500573 png_destroy_struct_2((png_voidp)png_ptr,
574 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500575#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500576 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500577#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500578 return (NULL);
579 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500580
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500581 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500582
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500583#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600584 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500585 1, NULL, NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600586#endif
587
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500588 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500589}
590
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500591
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600592/* Write a few rows of image data. If the image is interlaced,
593 * either you will have to write the 7 sub images, or, if you
594 * have called png_set_interlace_handling(), you will have to
595 * "write" the image seven times.
596 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500597void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600598png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500599 png_uint_32 num_rows)
600{
601 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600602 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500603
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500604 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600605
606 if (png_ptr == NULL)
607 return;
608
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500609 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500610 for (i = 0, rp = row; i < num_rows; i++, rp++)
611 {
612 png_write_row(png_ptr, *rp);
613 }
614}
615
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600616/* Write the image. You only need to call this function once, even
617 * if you are writing an interlaced image.
618 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500619void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600620png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500621{
622 png_uint_32 i; /* row index */
623 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600624 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500625
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600626 if (png_ptr == NULL)
627 return;
628
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500629 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500630
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500631#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500632 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500633 * this will set pass to 1
634 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500635 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600636#else
637 num_pass = 1;
638#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500639 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500640 for (pass = 0; pass < num_pass; pass++)
641 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500642 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500643 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
644 {
645 png_write_row(png_ptr, *rp);
646 }
647 }
648}
649
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500650/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500651void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600652png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500653{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600654 if (png_ptr == NULL)
655 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500656
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500657 png_debug2(1, "in png_write_row (row %ld, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600658 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600659
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500660 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600661 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500662 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500663 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500664 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
665 png_error(png_ptr,
666 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600667
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500668 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500669#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500670 if (png_ptr->transformations & PNG_INVERT_MONO)
671 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500672#endif
673#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500674 if (png_ptr->transformations & PNG_FILLER)
675 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500676#endif
677#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500678 if (png_ptr->transformations & PNG_PACKSWAP)
679 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500680#endif
681#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500682 if (png_ptr->transformations & PNG_PACK)
683 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500684#endif
685#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500686 if (png_ptr->transformations & PNG_SHIFT)
687 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500688#endif
689#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500690 if (png_ptr->transformations & PNG_BGR)
691 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500692#endif
693#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500694 if (png_ptr->transformations & PNG_SWAP_BYTES)
695 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500696#endif
697
Guy Schalnat0d580581995-07-20 02:43:20 -0500698 png_write_start_row(png_ptr);
699 }
700
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500701#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500702 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500703 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
704 {
705 switch (png_ptr->pass)
706 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600707 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600708 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500709 {
710 png_write_finish_row(png_ptr);
711 return;
712 }
713 break;
714 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600715 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500716 {
717 png_write_finish_row(png_ptr);
718 return;
719 }
720 break;
721 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600722 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500723 {
724 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600725 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500726 }
727 break;
728 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600729 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500730 {
731 png_write_finish_row(png_ptr);
732 return;
733 }
734 break;
735 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600736 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500737 {
738 png_write_finish_row(png_ptr);
739 return;
740 }
741 break;
742 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600743 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500744 {
745 png_write_finish_row(png_ptr);
746 return;
747 }
748 break;
749 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600750 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500751 {
752 png_write_finish_row(png_ptr);
753 return;
754 }
755 break;
756 }
757 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600758#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500759
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500760 /* Set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600761 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500762 png_ptr->row_info.width = png_ptr->usr_width;
763 png_ptr->row_info.channels = png_ptr->usr_channels;
764 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600765 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
766 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600767
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500768 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
769 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500770
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500771 png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
772 png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);
773 png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
774 png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
775 png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
776 png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500777
778 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500779 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500780
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500781#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500782 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500783 if (png_ptr->interlaced && png_ptr->pass < 6 &&
784 (png_ptr->transformations & PNG_INTERLACE))
785 {
786 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600787 png_ptr->row_buf + 1, png_ptr->pass);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500788 /* This should always get caught above, but still ... */
Guy Schalnat0d580581995-07-20 02:43:20 -0500789 if (!(png_ptr->row_info.width))
790 {
791 png_write_finish_row(png_ptr);
792 return;
793 }
794 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600795#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500796
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500797 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500798 if (png_ptr->transformations)
799 png_do_write_transformations(png_ptr);
800
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500801#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600802 /* Write filter_method 64 (intrapixel differencing) only if
803 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
804 * 2. Libpng did not write a PNG signature (this filter_method is only
805 * used in PNG datastreams that are embedded in MNG datastreams) and
806 * 3. The application called png_permit_mng_features with a mask that
807 * included PNG_FLAG_MNG_FILTER_64 and
808 * 4. The filter_method is 64 and
809 * 5. The color_type is RGB or RGBA
810 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500811 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600812 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
813 {
814 /* Intrapixel differencing */
815 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
816 }
817#endif
818
Andreas Dilger47a0c421997-05-16 02:46:07 -0500819 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500820 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600821
822 if (png_ptr->write_row_fn != NULL)
823 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500824}
825
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500826#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600827/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500828void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600829png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600830{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500831 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500832
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600833 if (png_ptr == NULL)
834 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600835 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600836}
837
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500838/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500839void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600840png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600841{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600842 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600843
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500844 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500845
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600846 if (png_ptr == NULL)
847 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500848 /* We have already written out all of the data */
849 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500850 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600851
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600852 do
853 {
854 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600855
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500856 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600857 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600858 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600859
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500860 /* Check for compression errors */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600861 if (ret != Z_OK)
862 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600864 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600865 else
866 png_error(png_ptr, "zlib error");
867 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600868
Andreas Dilger47a0c421997-05-16 02:46:07 -0500869 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600870 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500871 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600872 png_write_IDAT(png_ptr, png_ptr->zbuf,
873 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600874 png_ptr->zstream.next_out = png_ptr->zbuf;
875 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600876 wrote_IDAT = 1;
877 }
878 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600879
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600880 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600881 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600882 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500883 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600884 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
886 png_ptr->zstream.next_out = png_ptr->zbuf;
887 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600888 }
889 png_ptr->flush_rows = 0;
890 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600891}
892#endif /* PNG_WRITE_FLUSH_SUPPORTED */
893
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500894/* Free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500895void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600896png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500897{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600898 png_structp png_ptr = NULL;
899 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500900#ifdef PNG_USER_MEM_SUPPORTED
901 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500902 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500903#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600904
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500905 png_debug(1, "in png_destroy_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500906
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500908 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600909 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500910#ifdef PNG_USER_MEM_SUPPORTED
911 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500912 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500913#endif
914 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600915
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500916#ifdef PNG_USER_MEM_SUPPORTED
917 if (png_ptr != NULL)
918 {
919 free_fn = png_ptr->free_fn;
920 mem_ptr = png_ptr->mem_ptr;
921 }
922#endif
923
Andreas Dilger47a0c421997-05-16 02:46:07 -0500924 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600925 info_ptr = *info_ptr_ptr;
926
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500928 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500929 if (png_ptr != NULL)
930 {
931 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600932
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500933#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500934 if (png_ptr->num_chunk_list)
935 {
936 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500937 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500938 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500939#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500940 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600941
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500942#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500943 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
944 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500945#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600946 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500947#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500948 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500949 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600950
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500952 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600953 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500954#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500955 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
956 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500957#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600958 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500959#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500960 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500961 }
962}
963
964
Andreas Dilger47a0c421997-05-16 02:46:07 -0500965/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -0500966void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600967png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500968{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600969#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500970 jmp_buf tmp_jmp; /* Save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600971#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500972 png_error_ptr error_fn;
973 png_error_ptr warning_fn;
974 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500975#ifdef PNG_USER_MEM_SUPPORTED
976 png_free_ptr free_fn;
977#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500978
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500979 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500980
981 /* Free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600982 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500983
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500984 /* Free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600985 png_free(png_ptr, png_ptr->zbuf);
986 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500987#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600988 png_free(png_ptr, png_ptr->prev_row);
989 png_free(png_ptr, png_ptr->sub_row);
990 png_free(png_ptr, png_ptr->up_row);
991 png_free(png_ptr, png_ptr->avg_row);
992 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500993#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600994
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500995#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600996 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600997#endif
998
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500999#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001000 png_free(png_ptr, png_ptr->prev_filters);
1001 png_free(png_ptr, png_ptr->filter_weights);
1002 png_free(png_ptr, png_ptr->inv_filter_weights);
1003 png_free(png_ptr, png_ptr->filter_costs);
1004 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001006
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001007#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001008 /* Reset structure */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001009 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001010#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001011
1012 error_fn = png_ptr->error_fn;
1013 warning_fn = png_ptr->warning_fn;
1014 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001015#ifdef PNG_USER_MEM_SUPPORTED
1016 free_fn = png_ptr->free_fn;
1017#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001018
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001019 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001020
1021 png_ptr->error_fn = error_fn;
1022 png_ptr->warning_fn = warning_fn;
1023 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001024#ifdef PNG_USER_MEM_SUPPORTED
1025 png_ptr->free_fn = free_fn;
1026#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001027
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001028#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001029 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001030#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001031}
Guy Schalnate5a37791996-06-05 15:50:50 -05001032
Andreas Dilger47a0c421997-05-16 02:46:07 -05001033/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001034void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001035png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001036{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001037 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001038
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001039 if (png_ptr == NULL)
1040 return;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001041#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001042 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001043 (method == PNG_INTRAPIXEL_DIFFERENCING))
1044 method = PNG_FILTER_TYPE_BASE;
1045#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001046 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001047 {
1048 switch (filters & (PNG_ALL_FILTERS | 0x07))
1049 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001050#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001051 case 5:
1052 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001053 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001054#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001055 case PNG_FILTER_VALUE_NONE:
1056 png_ptr->do_filter=PNG_FILTER_NONE; break;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001057#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001058 case PNG_FILTER_VALUE_SUB:
1059 png_ptr->do_filter=PNG_FILTER_SUB; break;
1060 case PNG_FILTER_VALUE_UP:
1061 png_ptr->do_filter=PNG_FILTER_UP; break;
1062 case PNG_FILTER_VALUE_AVG:
1063 png_ptr->do_filter=PNG_FILTER_AVG; break;
1064 case PNG_FILTER_VALUE_PAETH:
1065 png_ptr->do_filter=PNG_FILTER_PAETH; break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001066 default: png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001067#else
1068 default: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001069#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001070 }
1071
Andreas Dilger47a0c421997-05-16 02:46:07 -05001072 /* If we have allocated the row_buf, this means we have already started
1073 * with the image and we should have allocated all of the filter buffers
1074 * that have been selected. If prev_row isn't already allocated, then
1075 * it is too late to start using the filters that need it, since we
1076 * will be missing the data in the previous row. If an application
1077 * wants to start and stop using particular filters during compression,
1078 * it should start out with all of the filters, and then add and
1079 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001080 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001082 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001083#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001084 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001085 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001086 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001087 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001089 }
1090
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001091 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001092 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001093 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001094 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001095 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001096 png_ptr->do_filter &= ~PNG_FILTER_UP;
1097 }
1098 else
1099 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001101 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001103 }
1104 }
1105
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001106 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001107 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001109 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001110 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001111 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1112 }
1113 else
1114 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001116 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001117 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001118 }
1119 }
1120
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001121 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001123 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001125 {
1126 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001127 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001128 }
1129 else
1130 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001131 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001132 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001134 }
1135 }
1136
1137 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001138#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001139 png_ptr->do_filter = PNG_FILTER_NONE;
1140 }
1141 }
1142 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001144}
1145
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146/* This allows us to influence the way in which libpng chooses the "best"
1147 * filter for the current scanline. While the "minimum-sum-of-absolute-
1148 * differences metric is relatively fast and effective, there is some
1149 * question as to whether it can be improved upon by trying to keep the
1150 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001151 * better compression.
1152 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001153#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001154void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1156 int num_weights, png_doublep filter_weights,
1157 png_doublep filter_costs)
1158{
1159 int i;
1160
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001161 png_debug(1, "in png_set_filter_heuristics");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001162
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001163 if (png_ptr == NULL)
1164 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1166 {
1167 png_warning(png_ptr, "Unknown filter heuristic method");
1168 return;
1169 }
1170
1171 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1172 {
1173 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1174 }
1175
1176 if (num_weights < 0 || filter_weights == NULL ||
1177 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1178 {
1179 num_weights = 0;
1180 }
1181
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001182 png_ptr->num_prev_filters = (png_byte)num_weights;
1183 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001184
1185 if (num_weights > 0)
1186 {
1187 if (png_ptr->prev_filters == NULL)
1188 {
1189 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001190 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001191
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001192 /* To make sure that the weighting starts out fairly */
1193 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001195 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 }
1197 }
1198
1199 if (png_ptr->filter_weights == NULL)
1200 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001201 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001202 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001204 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001205 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001206 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001207 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001208 png_ptr->inv_filter_weights[i] =
1209 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001210 }
1211 }
1212
1213 for (i = 0; i < num_weights; i++)
1214 {
1215 if (filter_weights[i] < 0.0)
1216 {
1217 png_ptr->inv_filter_weights[i] =
1218 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1219 }
1220 else
1221 {
1222 png_ptr->inv_filter_weights[i] =
1223 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1224 png_ptr->filter_weights[i] =
1225 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1226 }
1227 }
1228 }
1229
1230 /* If, in the future, there are other filter methods, this would
1231 * need to be based on png_ptr->filter.
1232 */
1233 if (png_ptr->filter_costs == NULL)
1234 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001235 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001236 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001238 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001239 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001240
1241 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1242 {
1243 png_ptr->inv_filter_costs[i] =
1244 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1245 }
1246 }
1247
1248 /* Here is where we set the relative costs of the different filters. We
1249 * should take the desired compression level into account when setting
1250 * the costs, so that Paeth, for instance, has a high relative cost at low
1251 * compression levels, while it has a lower relative cost at higher
1252 * compression settings. The filter types are in order of increasing
1253 * relative cost, so it would be possible to do this with an algorithm.
1254 */
1255 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1256 {
1257 if (filter_costs == NULL || filter_costs[i] < 0.0)
1258 {
1259 png_ptr->inv_filter_costs[i] =
1260 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1261 }
1262 else if (filter_costs[i] >= 1.0)
1263 {
1264 png_ptr->inv_filter_costs[i] =
1265 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1266 png_ptr->filter_costs[i] =
1267 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1268 }
1269 }
1270}
1271#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1272
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001273void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001274png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001275{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001276 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001277
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001278 if (png_ptr == NULL)
1279 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001280 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001281 png_ptr->zlib_level = level;
1282}
1283
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001284void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001285png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001286{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001287 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001288
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001289 if (png_ptr == NULL)
1290 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001291 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001292 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001293}
1294
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001295void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001296png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001297{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001298 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001299
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001300 if (png_ptr == NULL)
1301 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001302 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001303 png_ptr->zlib_strategy = strategy;
1304}
1305
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001306void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001307png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001308{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001309 if (png_ptr == NULL)
1310 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001311 if (window_bits > 15)
1312 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001313 else if (window_bits < 8)
1314 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001315#ifndef WBITS_8_OK
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001316 /* Avoid libpng bug with 256-byte windows */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001317 if (window_bits == 8)
1318 {
1319 png_warning(png_ptr, "Compression window is being reset to 512");
1320 window_bits=9;
1321 }
1322#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001323 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001324 png_ptr->zlib_window_bits = window_bits;
1325}
1326
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001327void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001328png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001329{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001330 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001331
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001332 if (png_ptr == NULL)
1333 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001334 if (method != 8)
1335 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1336 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001337 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001338}
1339
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001340void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001341png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1342{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001343 if (png_ptr == NULL)
1344 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001345 png_ptr->write_row_fn = write_row_fn;
1346}
1347
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001348#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001349void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001350png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1351 write_user_transform_fn)
1352{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001353 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001354
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001355 if (png_ptr == NULL)
1356 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001357 png_ptr->transformations |= PNG_USER_TRANSFORM;
1358 png_ptr->write_user_transform_fn = write_user_transform_fn;
1359}
1360#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001361
1362
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001363#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001364void PNGAPI
1365png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001366 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001367{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001368 if (png_ptr == NULL || info_ptr == NULL)
1369 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001370
1371 /* Write the file header information. */
1372 png_write_info(png_ptr, info_ptr);
1373
1374 /* ------ these transformations don't touch the info structure ------- */
1375
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001376#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001377 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001378 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001379 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001380#endif
1381
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001382#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001383 /* Shift the pixels up to a legal bit depth and fill in
1384 * as appropriate to correctly scale the image.
1385 */
1386 if ((transforms & PNG_TRANSFORM_SHIFT)
1387 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001388 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001389#endif
1390
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001391#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001392 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001393 if (transforms & PNG_TRANSFORM_PACKING)
1394 png_set_packing(png_ptr);
1395#endif
1396
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001397#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001398 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001399 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001400 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001401#endif
1402
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001403#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson4a82d692008-12-15 16:25:05 -06001404 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001405 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001406 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001407 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001408 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001409#endif
1410
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001411#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001412 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001413 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001414 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001415#endif
1416
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001417#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001418 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001419 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001420 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001421#endif
1422
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001423#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001424 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001425 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001426 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001427#endif
1428
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001429#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001430 /* Invert the alpha channel from opacity to transparency */
1431 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1432 png_set_invert_alpha(png_ptr);
1433#endif
1434
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001435#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001436 /* Invert the alpha channel from opacity to transparency */
1437 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1438 png_set_invert_alpha(png_ptr);
1439#endif
1440
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001441 /* ----------------------- end of transformations ------------------- */
1442
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001443 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001444 if (info_ptr->valid & PNG_INFO_IDAT)
1445 png_write_image(png_ptr, info_ptr->row_pointers);
1446
1447 /* It is REQUIRED to call this to finish writing the rest of the file */
1448 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001449
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001450 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001451 params = params;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001452}
1453#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001454#endif /* PNG_WRITE_SUPPORTED */