blob: 0e255205b1b0f7636bf1c8251df8ebb38166274b [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-Pehrson65a22372010-03-03 05:38:29 -06004 * Last changed in libpng 1.5.0 [March 3, 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-Pehrson3dfe9372009-08-22 08:44:23 -050014/* Get internal access to png.h */
Glenn Randers-Pehrsonf9795312010-02-09 01:16:48 -060015#define PNG_EXPOSE_INTERNAL_STRUCTURES
Glenn Randers-Pehrson03f9b022009-12-04 08:40:41 -060016#define PNG_NO_PEDANTIC_WARNINGS
Guy Schalnat0d580581995-07-20 02:43:20 -050017#include "png.h"
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -060018#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050019#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Writes all the PNG information. This is the suggested way to use the
22 * library. If you have a new chunk to add, make a function to write it,
23 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050024 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050025 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
26 * the chunk, as that will keep the code from breaking if you want to just
27 * write a plain PNG file. If you have long comments, I suggest writing
28 * them in png_write_end(), and compressing them.
29 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050030void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060031png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050032{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050033 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050034
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060035 if (png_ptr == NULL || info_ptr == NULL)
36 return;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060037 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
38 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050039 /* Write PNG signature */
40 png_write_sig(png_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050041#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060042 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060043 (png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060044 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050045 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050046 png_ptr->mng_features_permitted = 0;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060047 }
48#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050049 /* Write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060050 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060051 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
52 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050053#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060054 info_ptr->interlace_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060055#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060056 0);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060057#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050058 /* The rest of these check to see if the valid field has the appropriate
59 * flag set, and if it does, writes the chunk.
60 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050061#ifdef PNG_WRITE_gAMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060062 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060063 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060064# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060065 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060066#else
67#ifdef PNG_FIXED_POINT_SUPPORTED
68 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060069# endif
70#endif
71 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050072#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050073#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060074 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060075 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060076#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050077#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060078 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060079 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060080 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
81#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050082#ifdef PNG_WRITE_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060083 if (info_ptr->valid & PNG_INFO_sBIT)
84 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050085#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050086#ifdef PNG_WRITE_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060088 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060089#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050090 png_write_cHRM(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060091 info_ptr->x_white, info_ptr->y_white,
92 info_ptr->x_red, info_ptr->y_red,
93 info_ptr->x_green, info_ptr->y_green,
94 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060095#else
96# ifdef PNG_FIXED_POINT_SUPPORTED
97 png_write_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060098 info_ptr->int_x_white, info_ptr->int_y_white,
99 info_ptr->int_x_red, info_ptr->int_y_red,
100 info_ptr->int_x_green, info_ptr->int_y_green,
101 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600102# endif
103#endif
104 }
105#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500106#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600107 if (info_ptr->unknown_chunks_num)
108 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500109 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600110
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500111 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600112
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500113 for (up = info_ptr->unknown_chunks;
114 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
115 up++)
116 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500117 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500118 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600119 up->location && !(up->location & PNG_HAVE_PLTE) &&
120 !(up->location & PNG_HAVE_IDAT) &&
121 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
122 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600123 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500124 if (up->size == 0)
125 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600126 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600127 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500128 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600129 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500130#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
132 }
133}
134
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500135void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600136png_write_info(png_structp png_ptr, png_infop info_ptr)
137{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600138#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600139 int i;
140#endif
141
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500142 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600143
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600144 if (png_ptr == NULL || info_ptr == NULL)
145 return;
146
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600147 png_write_info_before_PLTE(png_ptr, info_ptr);
148
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600149 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150 png_write_PLTE(png_ptr, info_ptr->palette,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600151 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600152 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600153 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600154
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500155#ifdef PNG_WRITE_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600156 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500157 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500158#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500159 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500160 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600161 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500162 {
163 int j;
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500164 for (j = 0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500165 info_ptr->trans_alpha[j] = (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500166 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600167#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500168 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600169 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500170 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500171#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500172#ifdef PNG_WRITE_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600173 if (info_ptr->valid & PNG_INFO_bKGD)
174 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500175#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500176#ifdef PNG_WRITE_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600177 if (info_ptr->valid & PNG_INFO_hIST)
178 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500179#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500180#ifdef PNG_WRITE_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600181 if (info_ptr->valid & PNG_INFO_oFFs)
182 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600183 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500184#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500185#ifdef PNG_WRITE_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500186 if (info_ptr->valid & PNG_INFO_pCAL)
187 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600188 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
189 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500191
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500192#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600193 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500194#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500195#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600196 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
197 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500198#else /* !FLOATING_POINT */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600199#ifdef PNG_FIXED_POINT_SUPPORTED
200 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600201 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500202#endif /* FIXED_POINT */
203#endif /* FLOATING_POINT */
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500204#else /* !WRITE_sCAL */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600205 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500206 "png_write_sCAL not supported; sCAL chunk not written");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207#endif /* WRITE_sCAL */
208#endif /* sCAL */
209
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500210#ifdef PNG_WRITE_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211 if (info_ptr->valid & PNG_INFO_pHYs)
212 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600213 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500214#endif /* pHYs */
215
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500216#ifdef PNG_WRITE_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600217 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500218 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600219 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600220 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500221 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500222#endif /* tIME */
223
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500224#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600225 if (info_ptr->valid & PNG_INFO_sPLT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600226 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
227 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500228#endif /* sPLT */
229
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500230#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500231 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500232 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500233 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500234 png_debug2(2, "Writing header text chunk %d, type %d", i,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600235 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500236 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600237 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600238 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500239#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600240 /* Write international chunk */
241 png_write_iTXt(png_ptr,
242 info_ptr->text[i].compression,
243 info_ptr->text[i].key,
244 info_ptr->text[i].lang,
245 info_ptr->text[i].lang_key,
246 info_ptr->text[i].text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600248 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600249#endif
250 /* Mark this chunk as written */
251 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
252 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500253 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600254 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500256#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500257 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600259 info_ptr->text[i].text, 0,
260 info_ptr->text[i].compression);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600262 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500263#endif
264 /* Mark this chunk as written */
265 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
266 }
267 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
268 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500269#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500270 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600272 info_ptr->text[i].text,
273 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274 /* Mark this chunk as written */
275 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500276#else
277 /* Can't get here */
278 png_warning(png_ptr, "Unable to write uncompressed text");
279#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280 }
281 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500282#endif /* tEXt */
283
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500284#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600285 if (info_ptr->unknown_chunks_num)
286 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500287 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600288
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500289 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600290
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500291 for (up = info_ptr->unknown_chunks;
292 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
293 up++)
294 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500295 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500296 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600297 up->location && (up->location & PNG_HAVE_PLTE) &&
298 !(up->location & PNG_HAVE_IDAT) &&
299 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
300 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600301 {
302 png_write_chunk(png_ptr, up->name, up->data, up->size);
303 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500304 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600305 }
306#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307}
Guy Schalnat0d580581995-07-20 02:43:20 -0500308
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600310 * time information, you can pass NULL for info. If you already wrote these
311 * in png_write_info(), do not write them again here. If you have long
312 * comments, I suggest writing them here, and compressing them.
313 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500314void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315png_write_end(png_structp png_ptr, png_infop info_ptr)
316{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500317 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500318
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600319 if (png_ptr == NULL)
320 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 if (!(png_ptr->mode & PNG_HAVE_IDAT))
322 png_error(png_ptr, "No IDATs written into file");
323
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500324 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325 if (info_ptr != NULL)
326 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500327#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600329#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500330#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500331 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600332 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600333 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334 png_write_tIME(png_ptr, &(info_ptr->mod_time));
335#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500336#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500337 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600338 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500339 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500340 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500342 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600343 if (info_ptr->text[i].compression > 0)
344 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500345#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500346 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500347 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600348 info_ptr->text[i].compression,
349 info_ptr->text[i].key,
350 info_ptr->text[i].lang,
351 info_ptr->text[i].lang_key,
352 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600353#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500354 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600355#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500356 /* Mark this chunk as written */
357 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600358 }
359 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500360 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500361#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500362 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600363 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600364 info_ptr->text[i].text, 0,
365 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500366#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600367 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500368#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 /* Mark this chunk as written */
370 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500371 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500373 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500374#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500375 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600376 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600377 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500378#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600379 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500380#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500381
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 /* Mark this chunk as written */
383 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500384 }
385 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600386#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500387#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388 if (info_ptr->unknown_chunks_num)
389 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500390 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600391
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500392 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600393
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500394 for (up = info_ptr->unknown_chunks;
395 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
396 up++)
397 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500398 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500399 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600400 up->location && (up->location & PNG_AFTER_IDAT) &&
401 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
402 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600403 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600405 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500406 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600407 }
408#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500409 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500410
411 png_ptr->mode |= PNG_AFTER_IDAT;
412
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500413 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500414 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500415 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
416 * and restored again in libpng-1.2.30, may cause some applications that
417 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600418 * experiences a problem, please try building libpng with
419 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500420 * png-mng-implement at lists.sf.net .
421 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500422#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600423# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500424 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600425# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500426#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500427}
428
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600429#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600430/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500431void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600432png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500433{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500434 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500435
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600436 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
437 ptime->month = (png_byte)(ttime->tm_mon + 1);
438 ptime->day = (png_byte)ttime->tm_mday;
439 ptime->hour = (png_byte)ttime->tm_hour;
440 ptime->minute = (png_byte)ttime->tm_min;
441 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500442}
443
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500444void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600445png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500446{
447 struct tm *tbuf;
448
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500449 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500450
Guy Schalnat0d580581995-07-20 02:43:20 -0500451 tbuf = gmtime(&ttime);
452 png_convert_from_struct_tm(ptime, tbuf);
453}
Guy Schalnat6d764711995-12-19 03:22:19 -0600454#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500455
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500457png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500458png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600459 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500460{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500461#ifdef PNG_USER_MEM_SUPPORTED
462 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600463 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500464}
465
466/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500467png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500468png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600469 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
470 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500471{
472#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -0600473 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600474#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500475 volatile
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600476#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500477 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600478#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600479#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600480 jmp_buf png_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600481#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600482#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500483 int i;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500484
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500485 png_debug(1, "in png_create_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500486
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500487#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600488 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600489 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500490#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600491 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500492#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600493 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500494 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600495
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500496 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500497#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500498 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
499 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500500#endif
501
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500502#ifdef PNG_SETJMP_SUPPORTED
503/* Applications that neglect to set up their own setjmp() and then
504 encounter a png_error() will longjmp here. Since the jmpbuf is
505 then meaningless we abort instead of returning. */
506#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600507 if (setjmp(png_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500508#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600509 if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
510#endif
511#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600512 png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500513#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500514 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500515#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600516
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500517#ifdef PNG_USER_MEM_SUPPORTED
518 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
519#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600520 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500521
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500522 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500523 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500524 i = 0;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500525 do
526 {
527 if (user_png_ver[i] != png_libpng_ver[i])
528 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
529 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500530 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500531
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500532 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500533 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500534 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
535 * we must recompile any applications that use any older library version.
536 * For versions after libpng 1.0, we will be compatible, so we need
537 * only check the first digit.
538 */
539 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500540 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500541 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
542 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600543#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500544 char msg[80];
545 if (user_png_ver)
546 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500547 png_snprintf(msg, 80,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600548 "Application was compiled with png.h from libpng-%.20s",
549 user_png_ver);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500550 png_warning(png_ptr, msg);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500551 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500552 png_snprintf(msg, 80,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600553 "Application is running with png.c from libpng-%.20s",
554 png_libpng_ver);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500555 png_warning(png_ptr, msg);
556#endif
557#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500558 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500559#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500560 png_warning(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600561 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500562 png_cleanup_needed = 1;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500563 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500564 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500565
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500566 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500567 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500568 if (!png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500569 {
570 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600571 png_ptr->zbuf_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500572 if (png_ptr->zbuf == NULL)
573 png_cleanup_needed = 1;
574 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500575 if (png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500576 {
577 /* Clean up PNG structure and deallocate any memory. */
578 png_free(png_ptr, png_ptr->zbuf);
579 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500580#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500581 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600582 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500583#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500584 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500585#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500586 return (NULL);
587 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500588
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500589 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500590
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500591#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600592 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600593 1, NULL, NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600594#endif
595
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500596 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500597}
598
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500599
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600600/* Write a few rows of image data. If the image is interlaced,
601 * either you will have to write the 7 sub images, or, if you
602 * have called png_set_interlace_handling(), you will have to
603 * "write" the image seven times.
604 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500605void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600606png_write_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600607 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500608{
609 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600610 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500611
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500612 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600613
614 if (png_ptr == NULL)
615 return;
616
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500617 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500618 for (i = 0, rp = row; i < num_rows; i++, rp++)
619 {
620 png_write_row(png_ptr, *rp);
621 }
622}
623
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600624/* Write the image. You only need to call this function once, even
625 * if you are writing an interlaced image.
626 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500627void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600628png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500629{
630 png_uint_32 i; /* row index */
631 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600632 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500633
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600634 if (png_ptr == NULL)
635 return;
636
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500637 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500638
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500639#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500640 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500641 * this will set pass to 1
642 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500643 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600644#else
645 num_pass = 1;
646#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500647 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500648 for (pass = 0; pass < num_pass; pass++)
649 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500650 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500651 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
652 {
653 png_write_row(png_ptr, *rp);
654 }
655 }
656}
657
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500658/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500659void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600660png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500661{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600662 if (png_ptr == NULL)
663 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500664
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500665 png_debug2(1, "in png_write_row (row %ld, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600666 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600667
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500668 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600669 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500670 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500671 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500672 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
673 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600674 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600675
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500676 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500677#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500678 if (png_ptr->transformations & PNG_INVERT_MONO)
679 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500680#endif
681#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500682 if (png_ptr->transformations & PNG_FILLER)
683 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500684#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600685#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
686 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500687 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600688 png_warning(png_ptr,
689 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500690#endif
691#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500692 if (png_ptr->transformations & PNG_PACK)
693 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500694#endif
695#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500696 if (png_ptr->transformations & PNG_SHIFT)
697 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500698#endif
699#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500700 if (png_ptr->transformations & PNG_BGR)
701 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500702#endif
703#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500704 if (png_ptr->transformations & PNG_SWAP_BYTES)
705 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500706#endif
707
Guy Schalnat0d580581995-07-20 02:43:20 -0500708 png_write_start_row(png_ptr);
709 }
710
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500711#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500712 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500713 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
714 {
715 switch (png_ptr->pass)
716 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600717 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600718 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500719 {
720 png_write_finish_row(png_ptr);
721 return;
722 }
723 break;
724 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600725 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500726 {
727 png_write_finish_row(png_ptr);
728 return;
729 }
730 break;
731 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600732 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500733 {
734 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600735 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500736 }
737 break;
738 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600739 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500740 {
741 png_write_finish_row(png_ptr);
742 return;
743 }
744 break;
745 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600746 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500747 {
748 png_write_finish_row(png_ptr);
749 return;
750 }
751 break;
752 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600753 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500754 {
755 png_write_finish_row(png_ptr);
756 return;
757 }
758 break;
759 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600760 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500761 {
762 png_write_finish_row(png_ptr);
763 return;
764 }
765 break;
766 }
767 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600768#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500769
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500770 /* Set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600771 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500772 png_ptr->row_info.width = png_ptr->usr_width;
773 png_ptr->row_info.channels = png_ptr->usr_channels;
774 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600775 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
776 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600777
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500778 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
779 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500780
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500781 png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
782 png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);
783 png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
784 png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
785 png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
786 png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500787
788 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500789 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500790
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500791#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500792 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500793 if (png_ptr->interlaced && png_ptr->pass < 6 &&
794 (png_ptr->transformations & PNG_INTERLACE))
795 {
796 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600797 png_ptr->row_buf + 1, png_ptr->pass);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500798 /* This should always get caught above, but still ... */
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 if (!(png_ptr->row_info.width))
800 {
801 png_write_finish_row(png_ptr);
802 return;
803 }
804 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600805#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500806
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500807 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500808 if (png_ptr->transformations)
809 png_do_write_transformations(png_ptr);
810
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500811#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600812 /* Write filter_method 64 (intrapixel differencing) only if
813 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
814 * 2. Libpng did not write a PNG signature (this filter_method is only
815 * used in PNG datastreams that are embedded in MNG datastreams) and
816 * 3. The application called png_permit_mng_features with a mask that
817 * included PNG_FLAG_MNG_FILTER_64 and
818 * 4. The filter_method is 64 and
819 * 5. The color_type is RGB or RGBA
820 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500821 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600822 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
823 {
824 /* Intrapixel differencing */
825 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
826 }
827#endif
828
Andreas Dilger47a0c421997-05-16 02:46:07 -0500829 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500830 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600831
832 if (png_ptr->write_row_fn != NULL)
833 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500834}
835
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500836#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600837/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500838void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600839png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600840{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500841 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500842
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600843 if (png_ptr == NULL)
844 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600845 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600846}
847
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500848/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500849void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600850png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600851{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600852 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600853
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500854 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500855
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600856 if (png_ptr == NULL)
857 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500858 /* We have already written out all of the data */
859 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500860 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600861
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600862 do
863 {
864 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600865
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500866 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600867 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600868 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600869
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500870 /* Check for compression errors */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600871 if (ret != Z_OK)
872 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500873 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600874 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600875 else
876 png_error(png_ptr, "zlib error");
877 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600878
Andreas Dilger47a0c421997-05-16 02:46:07 -0500879 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600880 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500881 /* Write the IDAT and reset the zlib output buffer */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600882 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600883 png_ptr->zstream.next_out = png_ptr->zbuf;
884 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600885 wrote_IDAT = 1;
886 }
887 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600888
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600889 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600890 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600891 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500892 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600893 png_write_IDAT(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600894 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600895 png_ptr->zstream.next_out = png_ptr->zbuf;
896 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600897 }
898 png_ptr->flush_rows = 0;
899 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600900}
901#endif /* PNG_WRITE_FLUSH_SUPPORTED */
902
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500903/* Free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500904void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600905png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500906{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600907 png_structp png_ptr = NULL;
908 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500909#ifdef PNG_USER_MEM_SUPPORTED
910 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500911 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500912#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600913
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500914 png_debug(1, "in png_destroy_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500915
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500917 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600918 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500919#ifdef PNG_USER_MEM_SUPPORTED
920 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500921 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500922#endif
923 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600924
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500925#ifdef PNG_USER_MEM_SUPPORTED
926 if (png_ptr != NULL)
927 {
928 free_fn = png_ptr->free_fn;
929 mem_ptr = png_ptr->mem_ptr;
930 }
931#endif
932
Andreas Dilger47a0c421997-05-16 02:46:07 -0500933 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600934 info_ptr = *info_ptr_ptr;
935
Andreas Dilger47a0c421997-05-16 02:46:07 -0500936 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500937 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500938 if (png_ptr != NULL)
939 {
940 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600941
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600942#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500943 if (png_ptr->num_chunk_list)
944 {
945 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500946 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500947 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500948#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500949 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600950
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500951#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500952 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
953 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500954#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600955 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500956#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500957 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500958 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600959
Andreas Dilger47a0c421997-05-16 02:46:07 -0500960 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500961 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600962 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500963#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500964 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600965 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500966#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500968#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500969 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500970 }
971}
972
973
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -0500975void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600976png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500977{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600978#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500979 jmp_buf tmp_jmp; /* Save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600980#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500981 png_error_ptr error_fn;
982 png_error_ptr warning_fn;
983 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500984#ifdef PNG_USER_MEM_SUPPORTED
985 png_free_ptr free_fn;
986#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500987
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500988 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500989
990 /* Free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600991 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500992
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500993 /* Free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600994 png_free(png_ptr, png_ptr->zbuf);
995 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500996#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600997 png_free(png_ptr, png_ptr->prev_row);
998 png_free(png_ptr, png_ptr->sub_row);
999 png_free(png_ptr, png_ptr->up_row);
1000 png_free(png_ptr, png_ptr->avg_row);
1001 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001002#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001004#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001005 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006#endif
1007
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001008#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001009 png_free(png_ptr, png_ptr->prev_filters);
1010 png_free(png_ptr, png_ptr->filter_weights);
1011 png_free(png_ptr, png_ptr->inv_filter_weights);
1012 png_free(png_ptr, png_ptr->filter_costs);
1013 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001015
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001016#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001017 /* Reset structure */
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001018 png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001019#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001020
1021 error_fn = png_ptr->error_fn;
1022 warning_fn = png_ptr->warning_fn;
1023 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001024#ifdef PNG_USER_MEM_SUPPORTED
1025 free_fn = png_ptr->free_fn;
1026#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001027
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001028 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001029
1030 png_ptr->error_fn = error_fn;
1031 png_ptr->warning_fn = warning_fn;
1032 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001033#ifdef PNG_USER_MEM_SUPPORTED
1034 png_ptr->free_fn = free_fn;
1035#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001036
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001037#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001038 png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001039#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001040}
Guy Schalnate5a37791996-06-05 15:50:50 -05001041
Andreas Dilger47a0c421997-05-16 02:46:07 -05001042/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001043void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001044png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001045{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001046 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001047
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001048 if (png_ptr == NULL)
1049 return;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001050#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001051 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001052 (method == PNG_INTRAPIXEL_DIFFERENCING))
1053 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001054#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001055 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001056 {
1057 switch (filters & (PNG_ALL_FILTERS | 0x07))
1058 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001059#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001060 case 5:
1061 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001062 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001063#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001064 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001065 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001066#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001067 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001068 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001069 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001070 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001071 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001072 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001073 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001074 png_ptr->do_filter = PNG_FILTER_PAETH; break;
1075 default:
1076 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001077#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001078 default:
1079 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001080#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001081 }
1082
Andreas Dilger47a0c421997-05-16 02:46:07 -05001083 /* If we have allocated the row_buf, this means we have already started
1084 * with the image and we should have allocated all of the filter buffers
1085 * that have been selected. If prev_row isn't already allocated, then
1086 * it is too late to start using the filters that need it, since we
1087 * will be missing the data in the previous row. If an application
1088 * wants to start and stop using particular filters during compression,
1089 * it should start out with all of the filters, and then add and
1090 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001091 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001093 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001094#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001095 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001096 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001098 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001099 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001100 }
1101
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001102 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001103 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001104 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001105 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001107 png_ptr->do_filter &= ~PNG_FILTER_UP;
1108 }
1109 else
1110 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001111 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001112 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001113 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001114 }
1115 }
1116
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001117 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001118 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001121 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001122 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1123 }
1124 else
1125 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001127 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001128 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001129 }
1130 }
1131
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001132 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001134 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001136 {
1137 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001138 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001139 }
1140 else
1141 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001142 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001143 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001145 }
1146 }
1147
1148 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001149#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001150 png_ptr->do_filter = PNG_FILTER_NONE;
1151 }
1152 }
1153 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001155}
1156
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157/* This allows us to influence the way in which libpng chooses the "best"
1158 * filter for the current scanline. While the "minimum-sum-of-absolute-
1159 * differences metric is relatively fast and effective, there is some
1160 * question as to whether it can be improved upon by trying to keep the
1161 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001162 * better compression.
1163 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001164#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001165void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1167 int num_weights, png_doublep filter_weights,
1168 png_doublep filter_costs)
1169{
1170 int i;
1171
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001172 png_debug(1, "in png_set_filter_heuristics");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001173
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001174 if (png_ptr == NULL)
1175 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001176 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1177 {
1178 png_warning(png_ptr, "Unknown filter heuristic method");
1179 return;
1180 }
1181
1182 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1183 {
1184 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1185 }
1186
1187 if (num_weights < 0 || filter_weights == NULL ||
1188 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1189 {
1190 num_weights = 0;
1191 }
1192
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001193 png_ptr->num_prev_filters = (png_byte)num_weights;
1194 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195
1196 if (num_weights > 0)
1197 {
1198 if (png_ptr->prev_filters == NULL)
1199 {
1200 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001201 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001203 /* To make sure that the weighting starts out fairly */
1204 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001206 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001207 }
1208 }
1209
1210 if (png_ptr->filter_weights == NULL)
1211 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001212 png_ptr->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));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001215 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001216 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001217 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001218 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001219 png_ptr->inv_filter_weights[i] =
1220 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 }
1222 }
1223
1224 for (i = 0; i < num_weights; i++)
1225 {
1226 if (filter_weights[i] < 0.0)
1227 {
1228 png_ptr->inv_filter_weights[i] =
1229 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1230 }
1231 else
1232 {
1233 png_ptr->inv_filter_weights[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001234 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 png_ptr->filter_weights[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001236 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 }
1238 }
1239 }
1240
1241 /* If, in the future, there are other filter methods, this would
1242 * need to be based on png_ptr->filter.
1243 */
1244 if (png_ptr->filter_costs == NULL)
1245 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001246 png_ptr->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
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001249 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001250 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251
1252 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1253 {
1254 png_ptr->inv_filter_costs[i] =
1255 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1256 }
1257 }
1258
1259 /* Here is where we set the relative costs of the different filters. We
1260 * should take the desired compression level into account when setting
1261 * the costs, so that Paeth, for instance, has a high relative cost at low
1262 * compression levels, while it has a lower relative cost at higher
1263 * compression settings. The filter types are in order of increasing
1264 * relative cost, so it would be possible to do this with an algorithm.
1265 */
1266 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1267 {
1268 if (filter_costs == NULL || filter_costs[i] < 0.0)
1269 {
1270 png_ptr->inv_filter_costs[i] =
1271 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1272 }
1273 else if (filter_costs[i] >= 1.0)
1274 {
1275 png_ptr->inv_filter_costs[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001276 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001277 png_ptr->filter_costs[i] =
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001278 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001279 }
1280 }
1281}
1282#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1283
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001284void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001285png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001286{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001287 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001288
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001289 if (png_ptr == NULL)
1290 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001291 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001292 png_ptr->zlib_level = level;
1293}
1294
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001295void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001296png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001297{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001298 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001299
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001300 if (png_ptr == NULL)
1301 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001302 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001303 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001304}
1305
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001306void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001307png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001308{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001309 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001310
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001311 if (png_ptr == NULL)
1312 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001313 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001314 png_ptr->zlib_strategy = strategy;
1315}
1316
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001317void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001318png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001319{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001320 if (png_ptr == NULL)
1321 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001322 if (window_bits > 15)
1323 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001324 else if (window_bits < 8)
1325 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001326#ifndef WBITS_8_OK
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001327 /* Avoid libpng bug with 256-byte windows */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001328 if (window_bits == 8)
1329 {
1330 png_warning(png_ptr, "Compression window is being reset to 512");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -05001331 window_bits = 9;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001332 }
1333#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001334 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001335 png_ptr->zlib_window_bits = window_bits;
1336}
1337
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001338void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001339png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001340{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001341 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001342
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001343 if (png_ptr == NULL)
1344 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001345 if (method != 8)
1346 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1347 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001348 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001349}
1350
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001351void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001352png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1353{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001354 if (png_ptr == NULL)
1355 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001356 png_ptr->write_row_fn = write_row_fn;
1357}
1358
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001359#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001360void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001361png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001362 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001363{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001364 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001365
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001366 if (png_ptr == NULL)
1367 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001368 png_ptr->transformations |= PNG_USER_TRANSFORM;
1369 png_ptr->write_user_transform_fn = write_user_transform_fn;
1370}
1371#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001372
1373
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001374#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001375void PNGAPI
1376png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001377 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001378{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001379 if (png_ptr == NULL || info_ptr == NULL)
1380 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001381
1382 /* Write the file header information. */
1383 png_write_info(png_ptr, info_ptr);
1384
1385 /* ------ these transformations don't touch the info structure ------- */
1386
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001387#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001388 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001389 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001390 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001391#endif
1392
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001393#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001394 /* Shift the pixels up to a legal bit depth and fill in
1395 * as appropriate to correctly scale the image.
1396 */
1397 if ((transforms & PNG_TRANSFORM_SHIFT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001398 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001399 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001400#endif
1401
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001402#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001403 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001404 if (transforms & PNG_TRANSFORM_PACKING)
1405 png_set_packing(png_ptr);
1406#endif
1407
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001408#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001409 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001410 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001411 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001412#endif
1413
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001414#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson4a82d692008-12-15 16:25:05 -06001415 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001416 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001417 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001418 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001419 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001420#endif
1421
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001422#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001423 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001424 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001425 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001426#endif
1427
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001428#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001429 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001430 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001431 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001432#endif
1433
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001434#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001435 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001436 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001437 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001438#endif
1439
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001440#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001441 /* Invert the alpha channel from opacity to transparency */
1442 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1443 png_set_invert_alpha(png_ptr);
1444#endif
1445
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001446 /* ----------------------- end of transformations ------------------- */
1447
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001448 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001449 if (info_ptr->valid & PNG_INFO_IDAT)
1450 png_write_image(png_ptr, info_ptr->row_pointers);
1451
1452 /* It is REQUIRED to call this to finish writing the rest of the file */
1453 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001454
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001455 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001456 params = params;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001457}
1458#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001459#endif /* PNG_WRITE_SUPPORTED */