blob: c7611ee9b39f11f6e05028f7ae4e847a316c2761 [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-Pehrson6b3d50b2011-03-31 20:14:29 -05004 * Last changed in libpng 1.5.3 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_WRITE_SUPPORTED
17
Andreas Dilger47a0c421997-05-16 02:46:07 -050018/* Writes all the PNG information. This is the suggested way to use the
19 * library. If you have a new chunk to add, make a function to write it,
20 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050021 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050022 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
23 * the chunk, as that will keep the code from breaking if you want to just
24 * write a plain PNG file. If you have long comments, I suggest writing
25 * them in png_write_end(), and compressing them.
26 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050027void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060028png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050029{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050030 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050031
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060032 if (png_ptr == NULL || info_ptr == NULL)
33 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050034
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060035 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
36 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050037 /* Write PNG signature */
38 png_write_sig(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050039
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050040#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060041 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060042 (png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060043 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050044 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050045 png_ptr->mng_features_permitted = 0;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060046 }
47#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050048
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-Pehrson31aee0d2010-07-29 17:39:14 -050063 png_write_gAMA_fixed(png_ptr, info_ptr->gamma);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050064#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050065#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060066 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060067 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060068#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050069
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050070#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060071 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060072 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050073 (png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060074#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050075#ifdef PNG_WRITE_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076 if (info_ptr->valid & PNG_INFO_sBIT)
77 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050078#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050079#ifdef PNG_WRITE_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060080 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050081 png_write_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060082 info_ptr->x_white, info_ptr->y_white,
83 info_ptr->x_red, info_ptr->y_red,
84 info_ptr->x_green, info_ptr->y_green,
85 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060086#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050087
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050088#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060089 if (info_ptr->unknown_chunks_num)
90 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050091 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060092
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050093 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060094
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050095 for (up = info_ptr->unknown_chunks;
96 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
97 up++)
98 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050099 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500100
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500101 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600102 up->location && !(up->location & PNG_HAVE_PLTE) &&
103 !(up->location & PNG_HAVE_IDAT) &&
104 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
105 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600106 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500107 if (up->size == 0)
108 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500109
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600110 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600111 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500112 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600113 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500114#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600115 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
116 }
117}
118
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500119void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600120png_write_info(png_structp png_ptr, png_infop info_ptr)
121{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600122#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600123 int i;
124#endif
125
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500126 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600127
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600128 if (png_ptr == NULL || info_ptr == NULL)
129 return;
130
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_write_info_before_PLTE(png_ptr, info_ptr);
132
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600133 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 png_write_PLTE(png_ptr, info_ptr->palette,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600135 (png_uint_32)info_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500136
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600137 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600138 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600139
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500140#ifdef PNG_WRITE_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600141 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500142 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500143#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500144 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600146 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500147 {
148 int j;
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500149 for (j = 0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500150 info_ptr->trans_alpha[j] =
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500151 (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500152 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600153#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500154 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600155 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500156 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500157#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500158#ifdef PNG_WRITE_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600159 if (info_ptr->valid & PNG_INFO_bKGD)
160 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500161#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500162
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500163#ifdef PNG_WRITE_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600164 if (info_ptr->valid & PNG_INFO_hIST)
165 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500166#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500167
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500168#ifdef PNG_WRITE_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600169 if (info_ptr->valid & PNG_INFO_oFFs)
170 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600171 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500172#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500173
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500174#ifdef PNG_WRITE_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175 if (info_ptr->valid & PNG_INFO_pCAL)
176 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600177 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
178 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500179#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500180
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500181#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500182 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600183 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600184 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500185#endif /* sCAL */
186
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500187#ifdef PNG_WRITE_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188 if (info_ptr->valid & PNG_INFO_pHYs)
189 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600190 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500191#endif /* pHYs */
192
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500193#ifdef PNG_WRITE_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600194 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500195 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600197 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500198 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500199#endif /* tIME */
200
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500201#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600202 if (info_ptr->valid & PNG_INFO_sPLT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600203 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
204 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500205#endif /* sPLT */
206
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500207#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500208 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500209 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500210 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500211 png_debug2(2, "Writing header text chunk %d, type %d", i,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600212 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500213 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600214 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600215 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500216#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600217 /* Write international chunk */
218 png_write_iTXt(png_ptr,
219 info_ptr->text[i].compression,
220 info_ptr->text[i].key,
221 info_ptr->text[i].lang,
222 info_ptr->text[i].lang_key,
223 info_ptr->text[i].text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600224#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600225 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600226#endif
227 /* Mark this chunk as written */
228 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
229 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500230
Andreas Dilger47a0c421997-05-16 02:46:07 -0500231 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600232 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500234#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500235 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600237 info_ptr->text[i].text, 0,
238 info_ptr->text[i].compression);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600240 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241#endif
242 /* Mark this chunk as written */
243 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
244 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500245
Andreas Dilger47a0c421997-05-16 02:46:07 -0500246 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
247 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500248#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500249 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600251 info_ptr->text[i].text,
252 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500253 /* Mark this chunk as written */
254 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500255#else
256 /* Can't get here */
257 png_warning(png_ptr, "Unable to write uncompressed text");
258#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500259 }
260 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500261#endif /* tEXt */
262
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500263#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600264 if (info_ptr->unknown_chunks_num)
265 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500266 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600267
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500268 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600269
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500270 for (up = info_ptr->unknown_chunks;
271 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
272 up++)
273 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500274 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500275 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600276 up->location && (up->location & PNG_HAVE_PLTE) &&
277 !(up->location & PNG_HAVE_IDAT) &&
278 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
279 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600280 {
281 png_write_chunk(png_ptr, up->name, up->data, up->size);
282 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500283 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600284 }
285#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500286}
Guy Schalnat0d580581995-07-20 02:43:20 -0500287
Andreas Dilger47a0c421997-05-16 02:46:07 -0500288/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600289 * time information, you can pass NULL for info. If you already wrote these
290 * in png_write_info(), do not write them again here. If you have long
291 * comments, I suggest writing them here, and compressing them.
292 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500293void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500294png_write_end(png_structp png_ptr, png_infop info_ptr)
295{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500296 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500297
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600298 if (png_ptr == NULL)
299 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500300
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 if (!(png_ptr->mode & PNG_HAVE_IDAT))
302 png_error(png_ptr, "No IDATs written into file");
303
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500304 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 if (info_ptr != NULL)
306 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500307#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500308 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600309#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500310#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500311 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600312 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600313 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500315
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500317#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500318 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600319 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500320 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500321 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500323 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600324 if (info_ptr->text[i].compression > 0)
325 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500326#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500327 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500328 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600329 info_ptr->text[i].compression,
330 info_ptr->text[i].key,
331 info_ptr->text[i].lang,
332 info_ptr->text[i].lang_key,
333 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600334#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500335 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500337 /* Mark this chunk as written */
338 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600339 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500340
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600341 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500342 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500343#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500344 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600345 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600346 info_ptr->text[i].text, 0,
347 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500348#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600349 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500350#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500351 /* Mark this chunk as written */
352 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500353 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500354
Andreas Dilger47a0c421997-05-16 02:46:07 -0500355 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500356 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500357#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500358 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600359 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600360 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500361#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600362 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500363#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500364
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 /* Mark this chunk as written */
366 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500367 }
368 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600369#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500370#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600371 if (info_ptr->unknown_chunks_num)
372 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500373 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500375 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600376
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500377 for (up = info_ptr->unknown_chunks;
378 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
379 up++)
380 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500381 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500382 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600383 up->location && (up->location & PNG_AFTER_IDAT) &&
384 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
385 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600386 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600387 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600388 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500389 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390 }
391#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500392 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500393
394 png_ptr->mode |= PNG_AFTER_IDAT;
395
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500396 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500397 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500398 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
399 * and restored again in libpng-1.2.30, may cause some applications that
400 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600401 * experiences a problem, please try building libpng with
402 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500403 * png-mng-implement at lists.sf.net .
404 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500405#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600406# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500407 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600408# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500409#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500410}
411
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600412#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600413/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500414void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500415png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500416{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500417 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500418
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600419 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
420 ptime->month = (png_byte)(ttime->tm_mon + 1);
421 ptime->day = (png_byte)ttime->tm_mday;
422 ptime->hour = (png_byte)ttime->tm_hour;
423 ptime->minute = (png_byte)ttime->tm_min;
424 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500425}
426
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500427void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600428png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500429{
430 struct tm *tbuf;
431
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500432 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500433
Guy Schalnat0d580581995-07-20 02:43:20 -0500434 tbuf = gmtime(&ttime);
435 png_convert_from_struct_tm(ptime, tbuf);
436}
Guy Schalnat6d764711995-12-19 03:22:19 -0600437#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500438
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500440PNG_FUNCTION(png_structp,PNGAPI
441png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
442 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500443{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500444#ifdef PNG_USER_MEM_SUPPORTED
445 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600446 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500447}
448
449/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500450static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
451
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500452PNG_FUNCTION(png_structp,PNGAPI
453png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600454 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500455 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500456{
457#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -0600458 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600459#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500460 volatile
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600461#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500462 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600463#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600464#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600465 jmp_buf png_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600466#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600467#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500468 int i;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500469
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500470 png_debug(1, "in png_create_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500471
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500472#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600473 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600474 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500475#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600476 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500477#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600478 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500479 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600480
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500481 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500482#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500483 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
484 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500485#endif
486
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500487#ifdef PNG_SETJMP_SUPPORTED
488/* Applications that neglect to set up their own setjmp() and then
489 encounter a png_error() will longjmp here. Since the jmpbuf is
490 then meaningless we abort instead of returning. */
491#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600492 if (setjmp(png_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500493#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600494 if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
495#endif
496#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -0600497 png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500498#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500499 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500500#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600501
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500502#ifdef PNG_USER_MEM_SUPPORTED
503 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
504#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600505 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500506
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500507 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500509 i = 0;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500510 do
511 {
512 if (user_png_ver[i] != png_libpng_ver[i])
513 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
514 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500515 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500516
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500517 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500518 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500519 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
520 * we must recompile any applications that use any older library version.
521 * For versions after libpng 1.0, we will be compatible, so we need
522 * only check the first digit.
523 */
524 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500525 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500526 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
527 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600528#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500529 char msg[80];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500530
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500531 if (user_png_ver)
532 {
Glenn Randers-Pehrson92a3ef42010-03-31 21:50:21 -0500533 png_snprintf2(msg, 80,
534 "Application built with libpng-%.20s"
535 " but running with %.20s",
536 user_png_ver,
537 png_libpng_ver);
538 png_warning(png_ptr, msg);
539 }
540#else
541 png_warning(png_ptr,
542 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500543#endif
544#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500545 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500546#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500547 png_cleanup_needed = 1;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500548 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500549 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500550
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500551 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500552 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500553
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500554 if (!png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500555 {
556 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600557 png_ptr->zbuf_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500558 if (png_ptr->zbuf == NULL)
559 png_cleanup_needed = 1;
560 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500561
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500562 if (png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500563 {
564 /* Clean up PNG structure and deallocate any memory. */
565 png_free(png_ptr, png_ptr->zbuf);
566 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500567#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500568 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600569 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500570#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500571 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500572#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500573 return (NULL);
574 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500575
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500576 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500577
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500578#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500579 png_reset_filter_heuristics(png_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600580#endif
581
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500582 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500583}
584
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500585
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600586/* Write a few rows of image data. If the image is interlaced,
587 * either you will have to write the 7 sub images, or, if you
588 * have called png_set_interlace_handling(), you will have to
589 * "write" the image seven times.
590 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500591void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600592png_write_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600593 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500594{
595 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600596 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500597
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500598 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600599
600 if (png_ptr == NULL)
601 return;
602
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500603 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500604 for (i = 0, rp = row; i < num_rows; i++, rp++)
605 {
606 png_write_row(png_ptr, *rp);
607 }
608}
609
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600610/* Write the image. You only need to call this function once, even
611 * if you are writing an interlaced image.
612 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500613void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600614png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500615{
616 png_uint_32 i; /* row index */
617 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600618 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500619
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600620 if (png_ptr == NULL)
621 return;
622
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500623 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500624
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500625#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500626 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500627 * this will set pass to 1
628 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500629 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600630#else
631 num_pass = 1;
632#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500633 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500634 for (pass = 0; pass < num_pass; pass++)
635 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500636 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500637 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
638 {
639 png_write_row(png_ptr, *rp);
640 }
641 }
642}
643
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500644/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500645void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500646png_write_row(png_structp png_ptr, png_const_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500647{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600648 if (png_ptr == NULL)
649 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500650
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600651 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600652 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600653
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500654 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600655 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500656 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500657 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500658 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
659 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600660 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600661
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500662 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500663#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500664 if (png_ptr->transformations & PNG_INVERT_MONO)
665 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500666#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500667
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500668#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500669 if (png_ptr->transformations & PNG_FILLER)
670 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500671#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600672#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
673 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500674 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600675 png_warning(png_ptr,
676 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500677#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500678
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500679#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500680 if (png_ptr->transformations & PNG_PACK)
681 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500682#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500683
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500684#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500685 if (png_ptr->transformations & PNG_SHIFT)
686 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500687#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500688
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500689#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500690 if (png_ptr->transformations & PNG_BGR)
691 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500692#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500693
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500694#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500695 if (png_ptr->transformations & PNG_SWAP_BYTES)
696 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500697#endif
698
Guy Schalnat0d580581995-07-20 02:43:20 -0500699 png_write_start_row(png_ptr);
700 }
701
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500702#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500703 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500704 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
705 {
706 switch (png_ptr->pass)
707 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600708 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600709 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500710 {
711 png_write_finish_row(png_ptr);
712 return;
713 }
714 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500715
Guy Schalnat0d580581995-07-20 02:43:20 -0500716 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600717 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500718 {
719 png_write_finish_row(png_ptr);
720 return;
721 }
722 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500723
Guy Schalnat0d580581995-07-20 02:43:20 -0500724 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600725 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500726 {
727 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600728 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 }
730 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500731
Guy Schalnat0d580581995-07-20 02:43:20 -0500732 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600733 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500734 {
735 png_write_finish_row(png_ptr);
736 return;
737 }
738 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500739
Guy Schalnat0d580581995-07-20 02:43:20 -0500740 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600741 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500742 {
743 png_write_finish_row(png_ptr);
744 return;
745 }
746 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500747
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600749 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500750 {
751 png_write_finish_row(png_ptr);
752 return;
753 }
754 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500755
Guy Schalnat0d580581995-07-20 02:43:20 -0500756 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600757 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500758 {
759 png_write_finish_row(png_ptr);
760 return;
761 }
762 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500763
764 default: /* error: ignore it */
765 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500766 }
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);
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600782 png_debug1(3, "row_info->width = %u", png_ptr->row_info.width);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500783 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);
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600786 png_debug1(3, "row_info->rowbytes = %lu",
787 (unsigned long)png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500788
789 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500790 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500791
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500792#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500793 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500794 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500795 (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnat0d580581995-07-20 02:43:20 -0500796 {
797 png_do_write_interlace(&(png_ptr->row_info),
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500798 png_ptr->row_buf + 1, png_ptr->pass);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500799 /* This should always get caught above, but still ... */
Guy Schalnat0d580581995-07-20 02:43:20 -0500800 if (!(png_ptr->row_info.width))
801 {
802 png_write_finish_row(png_ptr);
803 return;
804 }
805 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600806#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500807
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500808 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500809 if (png_ptr->transformations)
810 png_do_write_transformations(png_ptr);
811
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500812#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600813 /* Write filter_method 64 (intrapixel differencing) only if
814 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
815 * 2. Libpng did not write a PNG signature (this filter_method is only
816 * used in PNG datastreams that are embedded in MNG datastreams) and
817 * 3. The application called png_permit_mng_features with a mask that
818 * included PNG_FLAG_MNG_FILTER_64 and
819 * 4. The filter_method is 64 and
820 * 5. The color_type is RGB or RGBA
821 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500822 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500823 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600824 {
825 /* Intrapixel differencing */
826 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
827 }
828#endif
829
Andreas Dilger47a0c421997-05-16 02:46:07 -0500830 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500831 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600832
833 if (png_ptr->write_row_fn != NULL)
834 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500835}
836
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500837#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600838/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500839void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600840png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600841{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500842 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500843
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600844 if (png_ptr == NULL)
845 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500846
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600847 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600848}
849
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500850/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500851void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600852png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600853{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600854 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600855
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500856 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500857
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600858 if (png_ptr == NULL)
859 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500860
Guy Schalnate5a37791996-06-05 15:50:50 -0500861 /* We have already written out all of the data */
862 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500863 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600864
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600865 do
866 {
867 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600868
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500869 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600870 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600871 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600872
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500873 /* Check for compression errors */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600874 if (ret != Z_OK)
875 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600877 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500878
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600879 else
880 png_error(png_ptr, "zlib error");
881 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600882
Andreas Dilger47a0c421997-05-16 02:46:07 -0500883 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600884 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500885 /* Write the IDAT and reset the zlib output buffer */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600886 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600887 png_ptr->zstream.next_out = png_ptr->zbuf;
888 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600889 wrote_IDAT = 1;
890 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500891 } while (wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600892
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600893 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600894 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600895 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500896 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600897 png_write_IDAT(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600898 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600899 png_ptr->zstream.next_out = png_ptr->zbuf;
900 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600901 }
902 png_ptr->flush_rows = 0;
903 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600904}
905#endif /* PNG_WRITE_FLUSH_SUPPORTED */
906
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500907/* Free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500908void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600909png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500910{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600911 png_structp png_ptr = NULL;
912 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500913#ifdef PNG_USER_MEM_SUPPORTED
914 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500915 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500916#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600917
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500918 png_debug(1, "in png_destroy_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500919
Andreas Dilger47a0c421997-05-16 02:46:07 -0500920 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500921 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600922 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500923#ifdef PNG_USER_MEM_SUPPORTED
924 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500925 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500926#endif
927 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600928
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500929#ifdef PNG_USER_MEM_SUPPORTED
930 if (png_ptr != NULL)
931 {
932 free_fn = png_ptr->free_fn;
933 mem_ptr = png_ptr->mem_ptr;
934 }
935#endif
936
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600938 info_ptr = *info_ptr_ptr;
939
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500941 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500942 if (png_ptr != NULL)
943 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500944 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600945
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600946#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500947 if (png_ptr->num_chunk_list)
948 {
949 png_free(png_ptr, png_ptr->chunk_list);
950 png_ptr->num_chunk_list = 0;
951 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500952#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500953 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600954
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500955#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500956 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500957 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500958#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600959 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500960#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500961 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500962 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600963
Andreas Dilger47a0c421997-05-16 02:46:07 -0500964 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500965 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600966 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500967#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500968 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600969 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500970#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600971 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500972#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500973 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500974 }
975}
976
977
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -0500979void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600980png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500981{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600982#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500983 jmp_buf tmp_jmp; /* Save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600984#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500985 png_error_ptr error_fn;
986 png_error_ptr warning_fn;
987 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500988#ifdef PNG_USER_MEM_SUPPORTED
989 png_free_ptr free_fn;
990#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500991
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500992 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500993
994 /* Free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600995 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500996
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500997 /* Free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600998 png_free(png_ptr, png_ptr->zbuf);
999 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001000#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001001 png_free(png_ptr, png_ptr->prev_row);
1002 png_free(png_ptr, png_ptr->sub_row);
1003 png_free(png_ptr, png_ptr->up_row);
1004 png_free(png_ptr, png_ptr->avg_row);
1005 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001006#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001007
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001008#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001009 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001010#endif
1011
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001012#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001013 /* Use this to save a little code space, it doesn't free the filter_costs */
1014 png_reset_filter_heuristics(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001015 png_free(png_ptr, png_ptr->filter_costs);
1016 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001017#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001018
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001019#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001020 /* Reset structure */
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001021 png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001022#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001023
1024 error_fn = png_ptr->error_fn;
1025 warning_fn = png_ptr->warning_fn;
1026 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001027#ifdef PNG_USER_MEM_SUPPORTED
1028 free_fn = png_ptr->free_fn;
1029#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001030
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001031 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001032
1033 png_ptr->error_fn = error_fn;
1034 png_ptr->warning_fn = warning_fn;
1035 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001036#ifdef PNG_USER_MEM_SUPPORTED
1037 png_ptr->free_fn = free_fn;
1038#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001039
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001040#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001041 png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001042#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001043}
Guy Schalnate5a37791996-06-05 15:50:50 -05001044
Andreas Dilger47a0c421997-05-16 02:46:07 -05001045/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001046void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001047png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001048{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001049 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001050
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001051 if (png_ptr == NULL)
1052 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001053
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001054#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001055 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001056 (method == PNG_INTRAPIXEL_DIFFERENCING))
1057 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001058
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001059#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001061 {
1062 switch (filters & (PNG_ALL_FILTERS | 0x07))
1063 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001064#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001065 case 5:
1066 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001068#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001069 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001070 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001071
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001072#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001073 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001074 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001075
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001076 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001077 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001078
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001079 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001080 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001081
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001082 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001083 png_ptr->do_filter = PNG_FILTER_PAETH; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001084
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001085 default:
1086 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001087#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001088 default:
1089 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001090#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001091 }
1092
Andreas Dilger47a0c421997-05-16 02:46:07 -05001093 /* If we have allocated the row_buf, this means we have already started
1094 * with the image and we should have allocated all of the filter buffers
1095 * that have been selected. If prev_row isn't already allocated, then
1096 * it is too late to start using the filters that need it, since we
1097 * will be missing the data in the previous row. If an application
1098 * wants to start and stop using particular filters during compression,
1099 * it should start out with all of the filters, and then add and
1100 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001101 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001103 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001104#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001105 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001107 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001108 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001110 }
1111
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001112 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001113 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001115 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001116 png_warning(png_ptr, "Can't add Up filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001117 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1118 ~PNG_FILTER_UP);
Guy Schalnate5a37791996-06-05 15:50:50 -05001119 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001120
Guy Schalnate5a37791996-06-05 15:50:50 -05001121 else
1122 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001124 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001126 }
1127 }
1128
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001129 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001130 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001131 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001132 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133 png_warning(png_ptr, "Can't add Average filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001134 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1135 ~PNG_FILTER_AVG);
Guy Schalnate5a37791996-06-05 15:50:50 -05001136 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001137
Guy Schalnate5a37791996-06-05 15:50:50 -05001138 else
1139 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001140 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001141 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001143 }
1144 }
1145
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001146 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001148 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001150 {
1151 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001152 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001154
Guy Schalnate5a37791996-06-05 15:50:50 -05001155 else
1156 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001157 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001158 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001160 }
1161 }
1162
1163 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001164#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001165 png_ptr->do_filter = PNG_FILTER_NONE;
1166 }
1167 }
1168 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001170}
1171
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172/* This allows us to influence the way in which libpng chooses the "best"
1173 * filter for the current scanline. While the "minimum-sum-of-absolute-
1174 * differences metric is relatively fast and effective, there is some
1175 * question as to whether it can be improved upon by trying to keep the
1176 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001177 * better compression.
1178 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001179#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson47dc5f72011-03-30 09:59:02 -05001180/* Convenience reset API. */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001181static void
1182png_reset_filter_heuristics(png_structp png_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001183{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001184 /* Clear out any old values in the 'weights' - this must be done because if
1185 * the app calls set_filter_heuristics multiple times with different
1186 * 'num_weights' values we would otherwise potentially have wrong sized
1187 * arrays.
1188 */
1189 png_ptr->num_prev_filters = 0;
1190 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1191 if (png_ptr->prev_filters != NULL)
1192 {
1193 png_bytep old = png_ptr->prev_filters;
1194 png_ptr->prev_filters = NULL;
1195 png_free(png_ptr, old);
1196 }
1197 if (png_ptr->filter_weights != NULL)
1198 {
1199 png_uint_16p old = png_ptr->filter_weights;
1200 png_ptr->filter_weights = NULL;
1201 png_free(png_ptr, old);
1202 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001204 if (png_ptr->inv_filter_weights != NULL)
1205 {
1206 png_uint_16p old = png_ptr->inv_filter_weights;
1207 png_ptr->inv_filter_weights = NULL;
1208 png_free(png_ptr, old);
1209 }
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001210
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001211 /* Leave the filter_costs - this array is fixed size. */
1212}
1213
1214static int
1215png_init_filter_heuristics(png_structp png_ptr, int heuristic_method,
1216 int num_weights)
1217{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001218 if (png_ptr == NULL)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001219 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001220
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001221 /* Clear out the arrays */
1222 png_reset_filter_heuristics(png_ptr);
1223
1224 /* Check arguments; the 'reset' function makes the correct settings for the
1225 * unweighted case, but we must handle the weight case by initializing the
1226 * arrays for the caller.
1227 */
1228 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001230 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001231
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001232 if (num_weights > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001233 {
1234 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001235 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001236
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001237 /* To make sure that the weighting starts out fairly */
1238 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001239 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001240 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001242
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001243 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1244 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001245
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001246 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1247 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001248
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001249 for (i = 0; i < num_weights; i++)
1250 {
1251 png_ptr->inv_filter_weights[i] =
1252 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1253 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001254
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001255 /* Safe to set this now */
1256 png_ptr->num_prev_filters = (png_byte)num_weights;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001257 }
1258
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001259 /* If, in the future, there are other filter methods, this would
1260 * need to be based on png_ptr->filter.
1261 */
1262 if (png_ptr->filter_costs == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001264 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1265 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001267 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1268 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001269 }
1270
Andreas Dilger47a0c421997-05-16 02:46:07 -05001271 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1272 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001273 png_ptr->inv_filter_costs[i] =
1274 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001275 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001276
1277 /* All the arrays are inited, safe to set this: */
1278 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
1279
1280 /* Return the 'ok' code. */
1281 return 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001282 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001283 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
1284 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001285 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001286 return 1;
1287 }
1288 else
1289 {
1290 png_warning(png_ptr, "Unknown filter heuristic method");
1291 return 0;
1292 }
1293}
1294
1295/* Provide floating and fixed point APIs */
1296#ifdef PNG_FLOATING_POINT_SUPPORTED
1297void PNGAPI
1298png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001299 int num_weights, png_const_doublep filter_weights,
1300 png_const_doublep filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001301{
1302 png_debug(1, "in png_set_filter_heuristics");
1303
1304 /* The internal API allocates all the arrays and ensures that the elements of
1305 * those arrays are set to the default value.
1306 */
1307 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1308 return;
1309
1310 /* If using the weighted method copy in the weights. */
1311 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1312 {
1313 int i;
1314 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001316 if (filter_weights[i] <= 0.0)
1317 {
1318 png_ptr->inv_filter_weights[i] =
1319 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1320 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001321
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001322 else
1323 {
1324 png_ptr->inv_filter_weights[i] =
1325 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001326
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001327 png_ptr->filter_weights[i] =
1328 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
1329 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001330 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001331
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001332 /* Here is where we set the relative costs of the different filters. We
1333 * should take the desired compression level into account when setting
1334 * the costs, so that Paeth, for instance, has a high relative cost at low
1335 * compression levels, while it has a lower relative cost at higher
1336 * compression settings. The filter types are in order of increasing
1337 * relative cost, so it would be possible to do this with an algorithm.
1338 */
1339 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001340 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001341 png_ptr->inv_filter_costs[i] =
1342 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001343
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001344 png_ptr->filter_costs[i] =
1345 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001346 }
1347 }
1348}
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001349#endif /* FLOATING_POINT */
1350
1351#ifdef PNG_FIXED_POINT_SUPPORTED
1352void PNGAPI
1353png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001354 int num_weights, png_const_fixed_point_p filter_weights,
1355 png_const_fixed_point_p filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001356{
1357 png_debug(1, "in png_set_filter_heuristics_fixed");
1358
1359 /* The internal API allocates all the arrays and ensures that the elements of
1360 * those arrays are set to the default value.
1361 */
1362 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1363 return;
1364
1365 /* If using the weighted method copy in the weights. */
1366 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1367 {
1368 int i;
1369 for (i = 0; i < num_weights; i++)
1370 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001371 if (filter_weights[i] <= 0)
1372 {
1373 png_ptr->inv_filter_weights[i] =
1374 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1375 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001376
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001377 else
1378 {
1379 png_ptr->inv_filter_weights[i] = (png_uint_16)
1380 ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001381
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001382 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
1383 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
1384 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001385 }
1386
1387 /* Here is where we set the relative costs of the different filters. We
1388 * should take the desired compression level into account when setting
1389 * the costs, so that Paeth, for instance, has a high relative cost at low
1390 * compression levels, while it has a lower relative cost at higher
1391 * compression settings. The filter types are in order of increasing
1392 * relative cost, so it would be possible to do this with an algorithm.
1393 */
1394 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1395 if (filter_costs[i] >= PNG_FP_1)
1396 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001397 png_uint_32 tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001398
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001399 /* Use a 32 bit unsigned temporary here because otherwise the
1400 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1401 * and this will get the wrong answer on division.
1402 */
1403 tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
1404 tmp /= filter_costs[i];
1405
1406 png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
1407
1408 tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
1409 tmp /= PNG_FP_1;
1410
1411 png_ptr->filter_costs[i] = (png_uint_16)tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001412 }
1413 }
1414}
1415#endif /* FIXED_POINT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001416#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1417
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001418void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001419png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001420{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001421 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001422
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001423 if (png_ptr == NULL)
1424 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001425
Guy Schalnate5a37791996-06-05 15:50:50 -05001426 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001427 png_ptr->zlib_level = level;
1428}
1429
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001430void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001431png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001432{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001433 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001434
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001435 if (png_ptr == NULL)
1436 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001437
Guy Schalnate5a37791996-06-05 15:50:50 -05001438 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001439 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001440}
1441
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001442void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001443png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001444{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001445 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001446
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001447 if (png_ptr == NULL)
1448 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001449
Guy Schalnate5a37791996-06-05 15:50:50 -05001450 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001451 png_ptr->zlib_strategy = strategy;
1452}
1453
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001454void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001455png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001456{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001457 if (png_ptr == NULL)
1458 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001459
Guy Schalnate5a37791996-06-05 15:50:50 -05001460 if (window_bits > 15)
1461 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001462
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001463 else if (window_bits < 8)
1464 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001465
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001466#ifndef WBITS_8_OK
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001467 /* Avoid libpng bug with 256-byte windows */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001468 if (window_bits == 8)
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001469 {
1470 png_warning(png_ptr, "Compression window is being reset to 512");
1471 window_bits = 9;
1472 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001473
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001474#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001475 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001476 png_ptr->zlib_window_bits = window_bits;
1477}
1478
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001479void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001480png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001481{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001482 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001483
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001484 if (png_ptr == NULL)
1485 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001486
Guy Schalnate5a37791996-06-05 15:50:50 -05001487 if (method != 8)
1488 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001489
Guy Schalnate5a37791996-06-05 15:50:50 -05001490 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001491 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001492}
1493
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001494/* The following were added to libpng-1.5.2 */
1495#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION
1496void PNGAPI
1497png_set_text_compression_level(png_structp png_ptr, int level)
1498{
1499 png_debug(1, "in png_set_text_compression_level");
1500
1501 if (png_ptr == NULL)
1502 return;
1503
1504 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_LEVEL;
1505 png_ptr->zlib_text_level = level;
1506}
1507
1508void PNGAPI
1509png_set_text_compression_mem_level(png_structp png_ptr, int mem_level)
1510{
1511 png_debug(1, "in png_set_text_compression_mem_level");
1512
1513 if (png_ptr == NULL)
1514 return;
1515
1516 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL;
1517 png_ptr->zlib_text_mem_level = mem_level;
1518}
1519
1520void PNGAPI
1521png_set_text_compression_strategy(png_structp png_ptr, int strategy)
1522{
1523 png_debug(1, "in png_set_text_compression_strategy");
1524
1525 if (png_ptr == NULL)
1526 return;
1527
1528 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_STRATEGY;
1529 png_ptr->zlib_text_strategy = strategy;
1530}
1531
1532void PNGAPI
1533png_set_text_compression_window_bits(png_structp png_ptr, int window_bits)
1534{
1535 if (png_ptr == NULL)
1536 return;
1537
1538 if (window_bits > 15)
1539 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1540
1541 else if (window_bits < 8)
1542 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1543
1544#ifndef WBITS_8_OK
1545 /* Avoid libpng bug with 256-byte windows */
1546 if (window_bits == 8)
1547 {
1548 png_warning(png_ptr, "Text compression window is being reset to 512");
1549 window_bits = 9;
1550 }
1551
1552#endif
1553 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS;
1554 png_ptr->zlib_text_window_bits = window_bits;
1555}
1556
1557void PNGAPI
1558png_set_text_compression_method(png_structp png_ptr, int method)
1559{
1560 png_debug(1, "in png_set_text_compression_method");
1561
1562 if (png_ptr == NULL)
1563 return;
1564
1565 if (method != 8)
1566 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1567
1568 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_METHOD;
1569 png_ptr->zlib_text_method = method;
1570}
1571#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
1572/* end of API added to libpng-1.5.2 */
1573
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001574void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001575png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1576{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001577 if (png_ptr == NULL)
1578 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001579
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001580 png_ptr->write_row_fn = write_row_fn;
1581}
1582
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001583#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001584void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001585png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001586 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001587{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001588 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001589
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001590 if (png_ptr == NULL)
1591 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001592
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001593 png_ptr->transformations |= PNG_USER_TRANSFORM;
1594 png_ptr->write_user_transform_fn = write_user_transform_fn;
1595}
1596#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001597
1598
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001599#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001600void PNGAPI
1601png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001602 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001603{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001604 if (png_ptr == NULL || info_ptr == NULL)
1605 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001606
1607 /* Write the file header information. */
1608 png_write_info(png_ptr, info_ptr);
1609
1610 /* ------ these transformations don't touch the info structure ------- */
1611
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001612#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001613 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001614 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001615 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001616#endif
1617
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001618#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001619 /* Shift the pixels up to a legal bit depth and fill in
1620 * as appropriate to correctly scale the image.
1621 */
1622 if ((transforms & PNG_TRANSFORM_SHIFT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001623 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001624 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001625#endif
1626
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001627#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001628 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001629 if (transforms & PNG_TRANSFORM_PACKING)
1630 png_set_packing(png_ptr);
1631#endif
1632
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001633#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001634 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001635 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001636 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001637#endif
1638
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001639#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson4a82d692008-12-15 16:25:05 -06001640 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001641 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001642 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001643
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001644 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001645 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001646#endif
1647
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001648#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001649 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001650 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001651 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001652#endif
1653
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001654#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001655 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001656 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001657 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001658#endif
1659
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001660#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001661 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001662 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001663 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001664#endif
1665
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001666#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001667 /* Invert the alpha channel from opacity to transparency */
1668 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1669 png_set_invert_alpha(png_ptr);
1670#endif
1671
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001672 /* ----------------------- end of transformations ------------------- */
1673
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001674 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001675 if (info_ptr->valid & PNG_INFO_IDAT)
1676 png_write_image(png_ptr, info_ptr->row_pointers);
1677
1678 /* It is REQUIRED to call this to finish writing the rest of the file */
1679 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001680
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001681 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1682 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001683}
1684#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001685#endif /* PNG_WRITE_SUPPORTED */