blob: b80a834ba5fbdcf1f62258d2a192b88be6413f2f [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-Pehrson632a84e2010-03-09 22:28:33 -06004 * Last changed in libpng 1.5.0 [March 10, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 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-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_WRITE_SUPPORTED
17
Andreas Dilger47a0c421997-05-16 02:46:07 -050018/* Writes all the PNG information. This is the suggested way to use the
19 * library. If you have a new chunk to add, make a function to write it,
20 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050021 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050022 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
23 * the chunk, as that will keep the code from breaking if you want to just
24 * write a plain PNG file. If you have long comments, I suggest writing
25 * them in png_write_end(), and compressing them.
26 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050027void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060028png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050029{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050030 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050031
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060032 if (png_ptr == NULL || info_ptr == NULL)
33 return;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060034 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
35 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050036 /* Write PNG signature */
37 png_write_sig(png_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050038#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060039 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060040 (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-Pehrson614b91d2009-10-17 19:00:18 -050043 png_ptr->mng_features_permitted = 0;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060044 }
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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060048 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
49 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050050#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060051 info_ptr->interlace_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060052#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060053 0);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060054#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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060095 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-Pehrson614b91d2009-10-17 19:00:18 -0500114 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-Pehrson45624d62010-03-03 11:40:43 -0600116 up->location && !(up->location & PNG_HAVE_PLTE) &&
117 !(up->location & PNG_HAVE_IDAT) &&
118 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
119 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600120 {
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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600148 (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) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600158 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500159 {
160 int j;
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500161 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),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600180 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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600185 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
186 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187#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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600210 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)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600223 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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600232 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-Pehrson45624d62010-03-03 11:40:43 -0600237 /* Write international chunk */
238 png_write_iTXt(png_ptr,
239 info_ptr->text[i].compression,
240 info_ptr->text[i].key,
241 info_ptr->text[i].lang,
242 info_ptr->text[i].lang_key,
243 info_ptr->text[i].text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600244#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-Pehrson45624d62010-03-03 11:40:43 -0600256 info_ptr->text[i].text, 0,
257 info_ptr->text[i].compression);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258#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-Pehrson45624d62010-03-03 11:40:43 -0600269 info_ptr->text[i].text,
270 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-Pehrson614b91d2009-10-17 19:00:18 -0500292 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-Pehrson45624d62010-03-03 11:40:43 -0600294 up->location && (up->location & PNG_HAVE_PLTE) &&
295 !(up->location & PNG_HAVE_IDAT) &&
296 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
297 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600298 {
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-Pehrson45624d62010-03-03 11:40:43 -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,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600345 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-Pehrson45624d62010-03-03 11:40:43 -0600361 info_ptr->text[i].text, 0,
362 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-Pehrson45624d62010-03-03 11:40:43 -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-Pehrson614b91d2009-10-17 19:00:18 -0500395 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-Pehrson45624d62010-03-03 11:40:43 -0600397 up->location && (up->location & PNG_AFTER_IDAT) &&
398 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
399 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600400 {
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-Pehrsonedcd6e12009-11-20 20:28:29 -0600415 * experiences a problem, please try building libpng with
416 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500417 * png-mng-implement at lists.sf.net .
418 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500419#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600420# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500421 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600422# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500423#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500424}
425
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600426#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600427/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500428void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600429png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500430{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500431 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500432
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600433 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
434 ptime->month = (png_byte)(ttime->tm_mon + 1);
435 ptime->day = (png_byte)ttime->tm_mday;
436 ptime->hour = (png_byte)ttime->tm_hour;
437 ptime->minute = (png_byte)ttime->tm_min;
438 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500439}
440
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500441void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600442png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500443{
444 struct tm *tbuf;
445
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500446 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500447
Guy Schalnat0d580581995-07-20 02:43:20 -0500448 tbuf = gmtime(&ttime);
449 png_convert_from_struct_tm(ptime, tbuf);
450}
Guy Schalnat6d764711995-12-19 03:22:19 -0600451#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500452
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500454png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500455png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600456 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500457{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500458#ifdef PNG_USER_MEM_SUPPORTED
459 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600460 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500461}
462
463/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500464png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500465png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600466 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
467 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500468{
469#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -0600470 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600471#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500472 volatile
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600473#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500474 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600475#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600476#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600477 jmp_buf png_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600478#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600479#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500480 int i;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500481
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500482 png_debug(1, "in png_create_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500483
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500484#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600485 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600486 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500487#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600488 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500489#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600490 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500491 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600492
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500493 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500494#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500495 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
496 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500497#endif
498
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500499#ifdef PNG_SETJMP_SUPPORTED
500/* Applications that neglect to set up their own setjmp() and then
501 encounter a png_error() will longjmp here. Since the jmpbuf is
502 then meaningless we abort instead of returning. */
503#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600504 if (setjmp(png_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500505#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600506 if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
507#endif
508#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600509 png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500510#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500511 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500512#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600513
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500514#ifdef PNG_USER_MEM_SUPPORTED
515 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
516#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600517 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500518
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500519 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500520 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500521 i = 0;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500522 do
523 {
524 if (user_png_ver[i] != png_libpng_ver[i])
525 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
526 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500527 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500528
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500529 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500530 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500531 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
532 * we must recompile any applications that use any older library version.
533 * For versions after libpng 1.0, we will be compatible, so we need
534 * only check the first digit.
535 */
536 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500537 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500538 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
539 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600540#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500541 char msg[80];
542 if (user_png_ver)
543 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500544 png_snprintf(msg, 80,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600545 "Application was compiled with png.h from libpng-%.20s",
546 user_png_ver);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500547 png_warning(png_ptr, msg);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500548 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500549 png_snprintf(msg, 80,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600550 "Application is running with png.c from libpng-%.20s",
551 png_libpng_ver);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500552 png_warning(png_ptr, msg);
553#endif
554#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500555 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500556#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500557 png_warning(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600558 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500559 png_cleanup_needed = 1;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500560 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500561 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500562
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500563 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500564 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500565 if (!png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500566 {
567 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600568 png_ptr->zbuf_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500569 if (png_ptr->zbuf == NULL)
570 png_cleanup_needed = 1;
571 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500572 if (png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500573 {
574 /* Clean up PNG structure and deallocate any memory. */
575 png_free(png_ptr, png_ptr->zbuf);
576 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500577#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500578 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600579 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500580#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500581 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500582#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500583 return (NULL);
584 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500585
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500586 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500587
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500588#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600589 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600590 1, NULL, NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600591#endif
592
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500593 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500594}
595
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500596
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600597/* Write a few rows of image data. If the image is interlaced,
598 * either you will have to write the 7 sub images, or, if you
599 * have called png_set_interlace_handling(), you will have to
600 * "write" the image seven times.
601 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500602void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600603png_write_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600604 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500605{
606 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600607 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500608
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500609 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600610
611 if (png_ptr == NULL)
612 return;
613
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500614 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500615 for (i = 0, rp = row; i < num_rows; i++, rp++)
616 {
617 png_write_row(png_ptr, *rp);
618 }
619}
620
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600621/* Write the image. You only need to call this function once, even
622 * if you are writing an interlaced image.
623 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500624void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600625png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500626{
627 png_uint_32 i; /* row index */
628 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600629 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500630
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600631 if (png_ptr == NULL)
632 return;
633
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500634 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500635
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500636#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500637 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500638 * this will set pass to 1
639 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500640 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600641#else
642 num_pass = 1;
643#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500644 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500645 for (pass = 0; pass < num_pass; pass++)
646 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500647 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500648 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
649 {
650 png_write_row(png_ptr, *rp);
651 }
652 }
653}
654
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500655/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500656void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600657png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500658{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600659 if (png_ptr == NULL)
660 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500661
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600662 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600663 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600664
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500665 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600666 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500667 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500668 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500669 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
670 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600671 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600672
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500673 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500674#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500675 if (png_ptr->transformations & PNG_INVERT_MONO)
676 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500677#endif
678#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500679 if (png_ptr->transformations & PNG_FILLER)
680 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500681#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600682#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
683 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500684 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600685 png_warning(png_ptr,
686 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500687#endif
688#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500689 if (png_ptr->transformations & PNG_PACK)
690 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500691#endif
692#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500693 if (png_ptr->transformations & PNG_SHIFT)
694 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500695#endif
696#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500697 if (png_ptr->transformations & PNG_BGR)
698 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500699#endif
700#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500701 if (png_ptr->transformations & PNG_SWAP_BYTES)
702 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500703#endif
704
Guy Schalnat0d580581995-07-20 02:43:20 -0500705 png_write_start_row(png_ptr);
706 }
707
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500708#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500709 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500710 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
711 {
712 switch (png_ptr->pass)
713 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600714 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600715 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500716 {
717 png_write_finish_row(png_ptr);
718 return;
719 }
720 break;
721 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600722 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500723 {
724 png_write_finish_row(png_ptr);
725 return;
726 }
727 break;
728 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600729 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500730 {
731 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600732 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500733 }
734 break;
735 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600736 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500737 {
738 png_write_finish_row(png_ptr);
739 return;
740 }
741 break;
742 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600743 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500744 {
745 png_write_finish_row(png_ptr);
746 return;
747 }
748 break;
749 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600750 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500751 {
752 png_write_finish_row(png_ptr);
753 return;
754 }
755 break;
756 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600757 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500758 {
759 png_write_finish_row(png_ptr);
760 return;
761 }
762 break;
763 }
764 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600765#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500766
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500767 /* Set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600768 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500769 png_ptr->row_info.width = png_ptr->usr_width;
770 png_ptr->row_info.channels = png_ptr->usr_channels;
771 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600772 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
773 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600774
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500775 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
776 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500777
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500778 png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600779 png_debug1(3, "row_info->width = %u", png_ptr->row_info.width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500780 png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
781 png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
782 png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600783 png_debug1(3, "row_info->rowbytes = %u", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784
785 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500786 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500787
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500788#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500789 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500790 if (png_ptr->interlaced && png_ptr->pass < 6 &&
791 (png_ptr->transformations & PNG_INTERLACE))
792 {
793 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600794 png_ptr->row_buf + 1, png_ptr->pass);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500795 /* This should always get caught above, but still ... */
Guy Schalnat0d580581995-07-20 02:43:20 -0500796 if (!(png_ptr->row_info.width))
797 {
798 png_write_finish_row(png_ptr);
799 return;
800 }
801 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600802#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500803
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500804 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500805 if (png_ptr->transformations)
806 png_do_write_transformations(png_ptr);
807
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500808#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600809 /* Write filter_method 64 (intrapixel differencing) only if
810 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
811 * 2. Libpng did not write a PNG signature (this filter_method is only
812 * used in PNG datastreams that are embedded in MNG datastreams) and
813 * 3. The application called png_permit_mng_features with a mask that
814 * included PNG_FLAG_MNG_FILTER_64 and
815 * 4. The filter_method is 64 and
816 * 5. The color_type is RGB or RGBA
817 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500818 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600819 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
820 {
821 /* Intrapixel differencing */
822 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
823 }
824#endif
825
Andreas Dilger47a0c421997-05-16 02:46:07 -0500826 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500827 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600828
829 if (png_ptr->write_row_fn != NULL)
830 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500831}
832
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500833#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600834/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500835void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600836png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600837{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500838 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500839
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600840 if (png_ptr == NULL)
841 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600842 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600843}
844
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500845/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500846void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600847png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600848{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600849 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600850
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500851 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500852
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600853 if (png_ptr == NULL)
854 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500855 /* We have already written out all of the data */
856 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500857 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600858
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600859 do
860 {
861 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600862
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500863 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600864 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600865 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600866
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500867 /* Check for compression errors */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600868 if (ret != Z_OK)
869 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500870 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600871 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600872 else
873 png_error(png_ptr, "zlib error");
874 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600875
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600877 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500878 /* Write the IDAT and reset the zlib output buffer */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600879 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600880 png_ptr->zstream.next_out = png_ptr->zbuf;
881 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600882 wrote_IDAT = 1;
883 }
884 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600885
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600886 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600887 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600888 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500889 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600890 png_write_IDAT(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600891 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600892 png_ptr->zstream.next_out = png_ptr->zbuf;
893 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600894 }
895 png_ptr->flush_rows = 0;
896 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600897}
898#endif /* PNG_WRITE_FLUSH_SUPPORTED */
899
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500900/* Free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500901void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600902png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500903{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600904 png_structp png_ptr = NULL;
905 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500906#ifdef PNG_USER_MEM_SUPPORTED
907 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500908 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500909#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600910
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500911 png_debug(1, "in png_destroy_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500912
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500914 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600915 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500916#ifdef PNG_USER_MEM_SUPPORTED
917 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500918 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500919#endif
920 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600921
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500922#ifdef PNG_USER_MEM_SUPPORTED
923 if (png_ptr != NULL)
924 {
925 free_fn = png_ptr->free_fn;
926 mem_ptr = png_ptr->mem_ptr;
927 }
928#endif
929
Andreas Dilger47a0c421997-05-16 02:46:07 -0500930 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600931 info_ptr = *info_ptr_ptr;
932
Andreas Dilger47a0c421997-05-16 02:46:07 -0500933 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500934 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500935 if (png_ptr != NULL)
936 {
937 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600938
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600939#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500940 if (png_ptr->num_chunk_list)
941 {
942 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500943 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500944 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500945#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500946 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600947
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500948#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500949 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
950 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500951#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600952 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500953#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500954 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500955 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600956
Andreas Dilger47a0c421997-05-16 02:46:07 -0500957 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500958 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600959 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500960#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500961 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600962 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500963#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600964 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500965#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500966 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500967 }
968}
969
970
Andreas Dilger47a0c421997-05-16 02:46:07 -0500971/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -0500972void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600973png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500974{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600975#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500976 jmp_buf tmp_jmp; /* Save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600977#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500978 png_error_ptr error_fn;
979 png_error_ptr warning_fn;
980 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500981#ifdef PNG_USER_MEM_SUPPORTED
982 png_free_ptr free_fn;
983#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500984
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500985 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500986
987 /* Free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600988 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500989
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500990 /* Free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600991 png_free(png_ptr, png_ptr->zbuf);
992 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500993#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600994 png_free(png_ptr, png_ptr->prev_row);
995 png_free(png_ptr, png_ptr->sub_row);
996 png_free(png_ptr, png_ptr->up_row);
997 png_free(png_ptr, png_ptr->avg_row);
998 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500999#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001000
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001001#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001002 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003#endif
1004
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001005#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006 png_free(png_ptr, png_ptr->prev_filters);
1007 png_free(png_ptr, png_ptr->filter_weights);
1008 png_free(png_ptr, png_ptr->inv_filter_weights);
1009 png_free(png_ptr, png_ptr->filter_costs);
1010 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001012
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001014 /* Reset structure */
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001015 png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001016#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001017
1018 error_fn = png_ptr->error_fn;
1019 warning_fn = png_ptr->warning_fn;
1020 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001021#ifdef PNG_USER_MEM_SUPPORTED
1022 free_fn = png_ptr->free_fn;
1023#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001024
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001025 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001026
1027 png_ptr->error_fn = error_fn;
1028 png_ptr->warning_fn = warning_fn;
1029 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001030#ifdef PNG_USER_MEM_SUPPORTED
1031 png_ptr->free_fn = free_fn;
1032#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001033
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001034#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001035 png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001036#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001037}
Guy Schalnate5a37791996-06-05 15:50:50 -05001038
Andreas Dilger47a0c421997-05-16 02:46:07 -05001039/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001040void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001041png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001042{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001043 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001044
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001045 if (png_ptr == NULL)
1046 return;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001047#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001048 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001049 (method == PNG_INTRAPIXEL_DIFFERENCING))
1050 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001051#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001052 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001053 {
1054 switch (filters & (PNG_ALL_FILTERS | 0x07))
1055 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001056#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001057 case 5:
1058 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001059 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001060#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001061 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001062 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001063#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001064 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001065 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001066 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001067 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001068 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001069 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001070 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001071 png_ptr->do_filter = PNG_FILTER_PAETH; break;
1072 default:
1073 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001074#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001075 default:
1076 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001077#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001078 }
1079
Andreas Dilger47a0c421997-05-16 02:46:07 -05001080 /* If we have allocated the row_buf, this means we have already started
1081 * with the image and we should have allocated all of the filter buffers
1082 * that have been selected. If prev_row isn't already allocated, then
1083 * it is too late to start using the filters that need it, since we
1084 * will be missing the data in the previous row. If an application
1085 * wants to start and stop using particular filters during compression,
1086 * it should start out with all of the filters, and then add and
1087 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001088 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001089 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001090 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001091#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001092 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001093 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001095 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 }
1098
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001099 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001100 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001101 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001102 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001104 png_ptr->do_filter &= ~PNG_FILTER_UP;
1105 }
1106 else
1107 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001109 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001110 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001111 }
1112 }
1113
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001114 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001115 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001116 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001117 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001118 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001119 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1120 }
1121 else
1122 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001124 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001126 }
1127 }
1128
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001129 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001131 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001133 {
1134 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001135 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001136 }
1137 else
1138 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001139 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001140 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001142 }
1143 }
1144
1145 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001146#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001147 png_ptr->do_filter = PNG_FILTER_NONE;
1148 }
1149 }
1150 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001151 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001152}
1153
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154/* This allows us to influence the way in which libpng chooses the "best"
1155 * filter for the current scanline. While the "minimum-sum-of-absolute-
1156 * differences metric is relatively fast and effective, there is some
1157 * question as to whether it can be improved upon by trying to keep the
1158 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001159 * better compression.
1160 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001161#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001162void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001163png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1164 int num_weights, png_doublep filter_weights,
1165 png_doublep filter_costs)
1166{
1167 int i;
1168
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001169 png_debug(1, "in png_set_filter_heuristics");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001170
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001171 if (png_ptr == NULL)
1172 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1174 {
1175 png_warning(png_ptr, "Unknown filter heuristic method");
1176 return;
1177 }
1178
1179 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1180 {
1181 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1182 }
1183
1184 if (num_weights < 0 || filter_weights == NULL ||
1185 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1186 {
1187 num_weights = 0;
1188 }
1189
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001190 png_ptr->num_prev_filters = (png_byte)num_weights;
1191 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001192
1193 if (num_weights > 0)
1194 {
1195 if (png_ptr->prev_filters == NULL)
1196 {
1197 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001198 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001200 /* To make sure that the weighting starts out fairly */
1201 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001203 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 }
1205 }
1206
1207 if (png_ptr->filter_weights == NULL)
1208 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001209 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001210 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001212 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001213 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001214 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001215 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001216 png_ptr->inv_filter_weights[i] =
1217 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001218 }
1219 }
1220
1221 for (i = 0; i < num_weights; i++)
1222 {
1223 if (filter_weights[i] < 0.0)
1224 {
1225 png_ptr->inv_filter_weights[i] =
1226 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1227 }
1228 else
1229 {
1230 png_ptr->inv_filter_weights[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001231 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 png_ptr->filter_weights[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001233 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 }
1235 }
1236 }
1237
1238 /* If, in the future, there are other filter methods, this would
1239 * need to be based on png_ptr->filter.
1240 */
1241 if (png_ptr->filter_costs == NULL)
1242 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001243 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001244 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001245
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001246 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001247 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001248
1249 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1250 {
1251 png_ptr->inv_filter_costs[i] =
1252 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1253 }
1254 }
1255
1256 /* Here is where we set the relative costs of the different filters. We
1257 * should take the desired compression level into account when setting
1258 * the costs, so that Paeth, for instance, has a high relative cost at low
1259 * compression levels, while it has a lower relative cost at higher
1260 * compression settings. The filter types are in order of increasing
1261 * relative cost, so it would be possible to do this with an algorithm.
1262 */
1263 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1264 {
1265 if (filter_costs == NULL || filter_costs[i] < 0.0)
1266 {
1267 png_ptr->inv_filter_costs[i] =
1268 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1269 }
1270 else if (filter_costs[i] >= 1.0)
1271 {
1272 png_ptr->inv_filter_costs[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001273 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001274 png_ptr->filter_costs[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001275 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276 }
1277 }
1278}
1279#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1280
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001281void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001282png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001283{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001284 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001285
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001286 if (png_ptr == NULL)
1287 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001288 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001289 png_ptr->zlib_level = level;
1290}
1291
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001292void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001293png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001294{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001295 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001296
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001297 if (png_ptr == NULL)
1298 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001299 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001300 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001301}
1302
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001303void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001304png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001305{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001306 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001307
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001308 if (png_ptr == NULL)
1309 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001310 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001311 png_ptr->zlib_strategy = strategy;
1312}
1313
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001314void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001315png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001316{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001317 if (png_ptr == NULL)
1318 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001319 if (window_bits > 15)
1320 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001321 else if (window_bits < 8)
1322 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001323#ifndef WBITS_8_OK
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001324 /* Avoid libpng bug with 256-byte windows */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001325 if (window_bits == 8)
1326 {
1327 png_warning(png_ptr, "Compression window is being reset to 512");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -05001328 window_bits = 9;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001329 }
1330#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001331 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001332 png_ptr->zlib_window_bits = window_bits;
1333}
1334
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001335void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001336png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001337{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001338 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001339
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001340 if (png_ptr == NULL)
1341 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001342 if (method != 8)
1343 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1344 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001345 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001346}
1347
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001348void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001349png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1350{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001351 if (png_ptr == NULL)
1352 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001353 png_ptr->write_row_fn = write_row_fn;
1354}
1355
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001356#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001357void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001358png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001359 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001360{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001361 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001362
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001363 if (png_ptr == NULL)
1364 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001365 png_ptr->transformations |= PNG_USER_TRANSFORM;
1366 png_ptr->write_user_transform_fn = write_user_transform_fn;
1367}
1368#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001369
1370
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001371#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001372void PNGAPI
1373png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001374 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001375{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001376 if (png_ptr == NULL || info_ptr == NULL)
1377 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001378
1379 /* Write the file header information. */
1380 png_write_info(png_ptr, info_ptr);
1381
1382 /* ------ these transformations don't touch the info structure ------- */
1383
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001384#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001385 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001386 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001387 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001388#endif
1389
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001390#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001391 /* Shift the pixels up to a legal bit depth and fill in
1392 * as appropriate to correctly scale the image.
1393 */
1394 if ((transforms & PNG_TRANSFORM_SHIFT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001395 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001396 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001397#endif
1398
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001399#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001400 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001401 if (transforms & PNG_TRANSFORM_PACKING)
1402 png_set_packing(png_ptr);
1403#endif
1404
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001405#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001406 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001407 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001408 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001409#endif
1410
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001411#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson4a82d692008-12-15 16:25:05 -06001412 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001413 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001414 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001415 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001416 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001417#endif
1418
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001419#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001420 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001421 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001422 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001423#endif
1424
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001425#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001426 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001427 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001428 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001429#endif
1430
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001431#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001432 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001433 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001434 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001435#endif
1436
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001437#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001438 /* Invert the alpha channel from opacity to transparency */
1439 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1440 png_set_invert_alpha(png_ptr);
1441#endif
1442
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001443 /* ----------------------- end of transformations ------------------- */
1444
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001445 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001446 if (info_ptr->valid & PNG_INFO_IDAT)
1447 png_write_image(png_ptr, info_ptr->row_pointers);
1448
1449 /* It is REQUIRED to call this to finish writing the rest of the file */
1450 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001451
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001452 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001453 params = params;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001454}
1455#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001456#endif /* PNG_WRITE_SUPPORTED */