annotate libpng/pngwrite.c @ 47:45308962220f libavformat

added jpeg image encoder and decoder (new YUV handling routines and mjpeg codec fixes are necessary to go further)
author bellard
date Sun, 02 Feb 2003 19:18:09 +0000
parents cfc262e9955c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
1
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
2 /* pngwrite.c - general routines to write a PNG file
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
3 *
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
4 * libpng 1.0.15 - October 3, 2002
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
5 * For conditions of distribution and use, see copyright notice in png.h
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
6 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
9 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
10
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
11 /* get internal access to png.h */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
12 #define PNG_INTERNAL
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
13 #include "png.h"
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
14 #ifdef PNG_WRITE_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
15
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
16 void png_write_init(png_struct *png_ptr)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
17 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
18 png_debug(1, "in png_write_init_3\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
19
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
20 /* reset all variables to 0 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
21 png_memset(png_ptr, 0, sizeof (png_struct));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
22
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
23 /* initialize zbuf - compression buffer */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
24 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
25 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
26 (png_uint_32)png_ptr->zbuf_size);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
27
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
28 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
29 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
30 1, png_doublep_NULL, png_doublep_NULL);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
31 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
32 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
33
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
34 /* Writes all the PNG information. This is the suggested way to use the
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
35 * library. If you have a new chunk to add, make a function to write it,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
36 * and put it in the correct location here. If you want the chunk written
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
37 * after the image data, put it in png_write_end(). I strongly encourage
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
38 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
39 * the chunk, as that will keep the code from breaking if you want to just
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
40 * write a plain PNG file. If you have long comments, I suggest writing
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
41 * them in png_write_end(), and compressing them.
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
42 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
43 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
44 png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
45 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
46 png_debug(1, "in png_write_info_before_PLTE\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
47 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
48 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
49 png_write_sig(png_ptr); /* write PNG signature */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
50 #if defined(PNG_MNG_FEATURES_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
51 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
52 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
53 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
54 png_ptr->mng_features_permitted=0;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
55 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
56 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
57 /* write IHDR information. */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
58 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
59 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
60 info_ptr->filter_type,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
61 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
62 info_ptr->interlace_type);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
63 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
64 0);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
65 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
66 /* the rest of these check to see if the valid field has the appropriate
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
67 flag set, and if it does, writes the chunk. */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
68 #if defined(PNG_WRITE_gAMA_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
69 if (info_ptr->valid & PNG_INFO_gAMA)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
70 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
71 # ifdef PNG_FLOATING_POINT_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
72 png_write_gAMA(png_ptr, info_ptr->gamma);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
73 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
74 #ifdef PNG_FIXED_POINT_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
75 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
76 # endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
77 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
78 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
79 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
80 #if defined(PNG_WRITE_sRGB_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
81 if (info_ptr->valid & PNG_INFO_sRGB)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
82 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
83 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
84 #if defined(PNG_WRITE_iCCP_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
85 if (info_ptr->valid & PNG_INFO_iCCP)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
86 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
87 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
88 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
89 #if defined(PNG_WRITE_sBIT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
90 if (info_ptr->valid & PNG_INFO_sBIT)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
91 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
92 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
93 #if defined(PNG_WRITE_cHRM_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
94 if (info_ptr->valid & PNG_INFO_cHRM)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
95 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
96 #ifdef PNG_FLOATING_POINT_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
97 png_write_cHRM(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
98 info_ptr->x_white, info_ptr->y_white,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
99 info_ptr->x_red, info_ptr->y_red,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
100 info_ptr->x_green, info_ptr->y_green,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
101 info_ptr->x_blue, info_ptr->y_blue);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
102 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
103 # ifdef PNG_FIXED_POINT_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
104 png_write_cHRM_fixed(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
105 info_ptr->int_x_white, info_ptr->int_y_white,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
106 info_ptr->int_x_red, info_ptr->int_y_red,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
107 info_ptr->int_x_green, info_ptr->int_y_green,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
108 info_ptr->int_x_blue, info_ptr->int_y_blue);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
109 # endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
110 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
111 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
112 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
113 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
114 if (info_ptr->unknown_chunks_num)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
115 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
116 png_unknown_chunk *up;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
117
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
118 png_debug(5, "writing extra chunks\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
119
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
120 for (up = info_ptr->unknown_chunks;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
121 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
122 up++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
123 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
124 int keep=png_handle_as_unknown(png_ptr, up->name);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
125 if (keep != HANDLE_CHUNK_NEVER &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
126 up->location && (!(up->location & PNG_HAVE_PLTE)) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
127 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
128 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
129 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
130 png_write_chunk(png_ptr, up->name, up->data, up->size);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
131 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
132 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
133 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
134 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
135 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
136 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
137 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
138
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
139 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
140 png_write_info(png_structp png_ptr, png_infop info_ptr)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
141 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
142 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
143 int i;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
144 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
145
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
146 png_debug(1, "in png_write_info\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
147
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
148 png_write_info_before_PLTE(png_ptr, info_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
149
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
150 if (info_ptr->valid & PNG_INFO_PLTE)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
151 png_write_PLTE(png_ptr, info_ptr->palette,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
152 (png_uint_32)info_ptr->num_palette);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
153 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
154 png_error(png_ptr, "Valid palette required for paletted images\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
155
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
156 #if defined(PNG_WRITE_tRNS_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
157 if (info_ptr->valid & PNG_INFO_tRNS)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
158 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
159 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
160 /* invert the alpha channel (in tRNS) */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
161 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
162 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
163 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
164 int j;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
165 for (j=0; j<(int)info_ptr->num_trans; j++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
166 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
167 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
168 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
169 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
170 info_ptr->num_trans, info_ptr->color_type);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
171 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
172 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
173 #if defined(PNG_WRITE_bKGD_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
174 if (info_ptr->valid & PNG_INFO_bKGD)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
175 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
176 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
177 #if defined(PNG_WRITE_hIST_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
178 if (info_ptr->valid & PNG_INFO_hIST)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
179 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
180 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
181 #if defined(PNG_WRITE_oFFs_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
182 if (info_ptr->valid & PNG_INFO_oFFs)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
183 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
184 info_ptr->offset_unit_type);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
185 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
186 #if defined(PNG_WRITE_pCAL_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
187 if (info_ptr->valid & PNG_INFO_pCAL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
188 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
189 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
190 info_ptr->pcal_units, info_ptr->pcal_params);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
191 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
192 #if defined(PNG_WRITE_sCAL_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
193 if (info_ptr->valid & PNG_INFO_sCAL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
194 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
195 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
196 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
197 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
198 #ifdef PNG_FIXED_POINT_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
199 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
200 info_ptr->scal_s_width, info_ptr->scal_s_height);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
201 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
202 png_warning(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
203 "png_write_sCAL not supported; sCAL chunk not written.\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
204 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
205 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
206 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
207 #if defined(PNG_WRITE_pHYs_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
208 if (info_ptr->valid & PNG_INFO_pHYs)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
209 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
210 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
211 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
212 #if defined(PNG_WRITE_tIME_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
213 if (info_ptr->valid & PNG_INFO_tIME)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
214 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
215 png_write_tIME(png_ptr, &(info_ptr->mod_time));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
216 png_ptr->mode |= PNG_WROTE_tIME;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
217 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
218 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
219 #if defined(PNG_WRITE_sPLT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
220 if (info_ptr->valid & PNG_INFO_sPLT)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
221 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
222 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
223 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
224 #if defined(PNG_WRITE_TEXT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
225 /* Check to see if we need to write text chunks */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
226 for (i = 0; i < info_ptr->num_text; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
227 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
228 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
229 info_ptr->text[i].compression);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
230 /* an internationalized chunk? */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
231 if (info_ptr->text[i].compression > 0)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
232 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
233 #if defined(PNG_WRITE_iTXt_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
234 /* write international chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
235 png_write_iTXt(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
236 info_ptr->text[i].compression,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
237 info_ptr->text[i].key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
238 info_ptr->text[i].lang,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
239 info_ptr->text[i].lang_key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
240 info_ptr->text[i].text);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
241 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
242 png_warning(png_ptr, "Unable to write international text\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
243 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
244 /* Mark this chunk as written */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
245 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
246 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
247 /* If we want a compressed text chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
248 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
249 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
250 #if defined(PNG_WRITE_zTXt_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
251 /* write compressed chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
252 png_write_zTXt(png_ptr, info_ptr->text[i].key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
253 info_ptr->text[i].text, 0,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
254 info_ptr->text[i].compression);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
255 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
256 png_warning(png_ptr, "Unable to write compressed text\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
257 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
258 /* Mark this chunk as written */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
259 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
260 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
261 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
262 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
263 #if defined(PNG_WRITE_tEXt_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
264 /* write uncompressed chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
265 png_write_tEXt(png_ptr, info_ptr->text[i].key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
266 info_ptr->text[i].text,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
267 0);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
268 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
269 png_warning(png_ptr, "Unable to write uncompressed text\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
270 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
271 /* Mark this chunk as written */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
272 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
273 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
274 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
275 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
276 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
277 if (info_ptr->unknown_chunks_num)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
278 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
279 png_unknown_chunk *up;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
280
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
281 png_debug(5, "writing extra chunks\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
282
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
283 for (up = info_ptr->unknown_chunks;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
284 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
285 up++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
286 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
287 int keep=png_handle_as_unknown(png_ptr, up->name);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
288 if (keep != HANDLE_CHUNK_NEVER &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
289 up->location && (up->location & PNG_HAVE_PLTE) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
290 !(up->location & PNG_HAVE_IDAT) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
291 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
292 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
293 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
294 png_write_chunk(png_ptr, up->name, up->data, up->size);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
295 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
296 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
297 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
298 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
299 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
300
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
301 /* Writes the end of the PNG file. If you don't want to write comments or
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
302 * time information, you can pass NULL for info. If you already wrote these
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
303 * in png_write_info(), do not write them again here. If you have long
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
304 * comments, I suggest writing them here, and compressing them.
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
305 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
306 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
307 png_write_end(png_structp png_ptr, png_infop info_ptr)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
308 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
309 png_debug(1, "in png_write_end\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
310 if (!(png_ptr->mode & PNG_HAVE_IDAT))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
311 png_error(png_ptr, "No IDATs written into file");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
312
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
313 /* see if user wants us to write information chunks */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
314 if (info_ptr != NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
315 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
316 #if defined(PNG_WRITE_TEXT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
317 int i; /* local index variable */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
318 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
319 #if defined(PNG_WRITE_tIME_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
320 /* check to see if user has supplied a time chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
321 if ((info_ptr->valid & PNG_INFO_tIME) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
322 !(png_ptr->mode & PNG_WROTE_tIME))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
323 png_write_tIME(png_ptr, &(info_ptr->mod_time));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
324 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
325 #if defined(PNG_WRITE_TEXT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
326 /* loop through comment chunks */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
327 for (i = 0; i < info_ptr->num_text; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
328 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
329 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
330 info_ptr->text[i].compression);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
331 /* an internationalized chunk? */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
332 if (info_ptr->text[i].compression > 0)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
333 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
334 #if defined(PNG_WRITE_iTXt_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
335 /* write international chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
336 png_write_iTXt(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
337 info_ptr->text[i].compression,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
338 info_ptr->text[i].key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
339 info_ptr->text[i].lang,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
340 info_ptr->text[i].lang_key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
341 info_ptr->text[i].text);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
342 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
343 png_warning(png_ptr, "Unable to write international text\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
344 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
345 /* Mark this chunk as written */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
346 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
347 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
348 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
349 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
350 #if defined(PNG_WRITE_zTXt_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
351 /* write compressed chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
352 png_write_zTXt(png_ptr, info_ptr->text[i].key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
353 info_ptr->text[i].text, 0,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
354 info_ptr->text[i].compression);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
355 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
356 png_warning(png_ptr, "Unable to write compressed text\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
357 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
358 /* Mark this chunk as written */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
359 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
360 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
361 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
362 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
363 #if defined(PNG_WRITE_tEXt_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
364 /* write uncompressed chunk */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
365 png_write_tEXt(png_ptr, info_ptr->text[i].key,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
366 info_ptr->text[i].text, 0);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
367 #else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
368 png_warning(png_ptr, "Unable to write uncompressed text\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
369 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
370
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
371 /* Mark this chunk as written */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
372 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
373 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
374 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
375 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
376 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
377 if (info_ptr->unknown_chunks_num)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
378 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
379 png_unknown_chunk *up;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
380
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
381 png_debug(5, "writing extra chunks\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
382
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
383 for (up = info_ptr->unknown_chunks;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
384 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
385 up++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
386 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
387 int keep=png_handle_as_unknown(png_ptr, up->name);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
388 if (keep != HANDLE_CHUNK_NEVER &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
389 up->location && (up->location & PNG_AFTER_IDAT) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
390 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
391 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
392 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
393 png_write_chunk(png_ptr, up->name, up->data, up->size);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
394 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
395 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
396 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
397 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
398 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
399
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
400 png_ptr->mode |= PNG_AFTER_IDAT;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
401
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
402 /* write end of PNG file */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
403 png_write_IEND(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
404 #if 0
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
405 /* This flush, added in libpng-1.0.8, causes some applications to crash
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
406 because they do not set png_ptr->output_flush_fn */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
407 png_flush(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
408 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
409 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
410
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
411 #if defined(PNG_WRITE_tIME_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
412 #if !defined(_WIN32_WCE)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
413 /* "time.h" functions are not supported on WindowsCE */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
414 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
415 png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
416 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
417 png_debug(1, "in png_convert_from_struct_tm\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
418 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
419 ptime->month = (png_byte)(ttime->tm_mon + 1);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
420 ptime->day = (png_byte)ttime->tm_mday;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
421 ptime->hour = (png_byte)ttime->tm_hour;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
422 ptime->minute = (png_byte)ttime->tm_min;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
423 ptime->second = (png_byte)ttime->tm_sec;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
424 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
425
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
426 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
427 png_convert_from_time_t(png_timep ptime, time_t ttime)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
428 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
429 struct tm *tbuf;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
430
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
431 png_debug(1, "in png_convert_from_time_t\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
432 tbuf = gmtime(&ttime);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
433 png_convert_from_struct_tm(ptime, tbuf);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
434 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
435 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
436 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
437
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
438 /* called by user to write a row of image data */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
439 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
440 png_write_row(png_structp png_ptr, png_bytep row)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
441 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
442 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
443 png_ptr->row_number, png_ptr->pass);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
444 /* initialize transformations and other stuff if first time */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
445 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
446 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
447 /* make sure we wrote the header info */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
448 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
449 png_error(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
450 "png_write_info was never called before png_write_row.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
451
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
452 /* check for transforms that have been set but were defined out */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
453 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
454 if (png_ptr->transformations & PNG_INVERT_MONO)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
455 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
456 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
457 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
458 if (png_ptr->transformations & PNG_FILLER)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
459 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
460 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
461 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
462 if (png_ptr->transformations & PNG_PACKSWAP)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
463 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
464 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
465 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
466 if (png_ptr->transformations & PNG_PACK)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
467 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
468 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
469 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
470 if (png_ptr->transformations & PNG_SHIFT)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
471 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
472 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
473 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
474 if (png_ptr->transformations & PNG_BGR)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
475 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
476 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
477 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
478 if (png_ptr->transformations & PNG_SWAP_BYTES)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
479 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
480 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
481
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
482 png_write_start_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
483 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
484
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
485 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
486 /* if interlaced and not interested in row, return */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
487 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
488 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
489 switch (png_ptr->pass)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
490 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
491 case 0:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
492 if (png_ptr->row_number & 0x07)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
493 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
494 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
495 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
496 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
497 break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
498 case 1:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
499 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
500 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
501 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
502 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
503 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
504 break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
505 case 2:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
506 if ((png_ptr->row_number & 0x07) != 4)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
507 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
508 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
509 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
510 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
511 break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
512 case 3:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
513 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
514 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
515 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
516 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
517 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
518 break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
519 case 4:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
520 if ((png_ptr->row_number & 0x03) != 2)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
521 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
522 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
523 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
524 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
525 break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
526 case 5:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
527 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
528 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
529 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
530 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
531 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
532 break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
533 case 6:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
534 if (!(png_ptr->row_number & 0x01))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
535 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
536 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
537 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
538 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
539 break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
540 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
541 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
542 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
543
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
544 /* set up row info for transformations */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
545 png_ptr->row_info.color_type = png_ptr->color_type;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
546 png_ptr->row_info.width = png_ptr->usr_width;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
547 png_ptr->row_info.channels = png_ptr->usr_channels;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
548 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
549 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
550 png_ptr->row_info.channels);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
551
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
552 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
553 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
554
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
555 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
556 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
557 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
558 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
559 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
560 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
561
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
562 /* Copy user's row into buffer, leaving room for filter byte. */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
563 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
564 png_ptr->row_info.rowbytes);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
565
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
566 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
567 /* handle interlacing */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
568 if (png_ptr->interlaced && png_ptr->pass < 6 &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
569 (png_ptr->transformations & PNG_INTERLACE))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
570 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
571 png_do_write_interlace(&(png_ptr->row_info),
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
572 png_ptr->row_buf + 1, png_ptr->pass);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
573 /* this should always get caught above, but still ... */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
574 if (!(png_ptr->row_info.width))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
575 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
576 png_write_finish_row(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
577 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
578 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
579 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
580 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
581
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
582 #if 0
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
583 /* handle other transformations */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
584 if (png_ptr->transformations)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
585 png_do_write_transformations(png_ptr);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
586 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
587
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
588 #if defined(PNG_MNG_FEATURES_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
589 /* Write filter_method 64 (intrapixel differencing) only if
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
590 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
591 * 2. Libpng did not write a PNG signature (this filter_method is only
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
592 * used in PNG datastreams that are embedded in MNG datastreams) and
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
593 * 3. The application called png_permit_mng_features with a mask that
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
594 * included PNG_FLAG_MNG_FILTER_64 and
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
595 * 4. The filter_method is 64 and
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
596 * 5. The color_type is RGB or RGBA
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
597 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
598 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
599 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
600 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
601 /* Intrapixel differencing */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
602 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
603 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
604 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
605
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
606 /* Find a filter if necessary, filter the row and write it out. */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
607 png_write_find_filter(png_ptr, &(png_ptr->row_info));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
608
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
609 if (png_ptr->write_row_fn != NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
610 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
611 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
612
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
613 /* Free any memory used in png_ptr struct (old method) */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
614 void /* PRIVATE */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
615 png_write_destroy(png_structp png_ptr)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
616 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
617 #ifdef PNG_SETJMP_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
618 jmp_buf tmp_jmp; /* save jump buffer */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
619 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
620 #ifdef PNG_USER_MEM_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
621 png_free_ptr free_fn;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
622 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
623
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
624 png_debug(1, "in png_write_destroy\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
625 /* free any memory zlib uses */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
626 deflateEnd(&png_ptr->zstream);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
627
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
628 /* free our memory. png_free checks NULL for us. */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
629 png_free(png_ptr, png_ptr->zbuf);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
630 png_free(png_ptr, png_ptr->row_buf);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
631 png_free(png_ptr, png_ptr->prev_row);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
632 png_free(png_ptr, png_ptr->sub_row);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
633 png_free(png_ptr, png_ptr->up_row);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
634 png_free(png_ptr, png_ptr->avg_row);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
635 png_free(png_ptr, png_ptr->paeth_row);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
636
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
637 #if defined(PNG_TIME_RFC1123_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
638 png_free(png_ptr, png_ptr->time_buffer);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
639 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
640
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
641 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
642 png_free(png_ptr, png_ptr->prev_filters);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
643 png_free(png_ptr, png_ptr->filter_weights);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
644 png_free(png_ptr, png_ptr->inv_filter_weights);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
645 png_free(png_ptr, png_ptr->filter_costs);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
646 png_free(png_ptr, png_ptr->inv_filter_costs);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
647 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
648
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
649 #ifdef PNG_SETJMP_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
650 /* reset structure */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
651 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
652 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
653
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
654 #ifdef PNG_USER_MEM_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
655 free_fn = png_ptr->free_fn;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
656 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
657
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
658 png_memset(png_ptr, 0, sizeof (png_struct));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
659
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
660 #ifdef PNG_USER_MEM_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
661 png_ptr->free_fn = free_fn;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
662 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
663
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
664 #ifdef PNG_SETJMP_SUPPORTED
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
665 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
666 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
667 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
668
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
669 /* Allow the application to select one or more row filters to use. */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
670 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
671 png_set_filter(png_structp png_ptr, int method, int filters)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
672 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
673 png_debug(1, "in png_set_filter\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
674 #if defined(PNG_MNG_FEATURES_SUPPORTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
675 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
676 (method == PNG_INTRAPIXEL_DIFFERENCING))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
677 method = PNG_FILTER_TYPE_BASE;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
678 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
679 if (method == PNG_FILTER_TYPE_BASE)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
680 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
681 switch (filters & (PNG_ALL_FILTERS | 0x07))
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
682 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
683 case 5:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
684 case 6:
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
685 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
686 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
687 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
688 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
689 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
690 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
691 default: png_ptr->do_filter = (png_byte)filters; break;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
692 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
693
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
694 /* If we have allocated the row_buf, this means we have already started
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
695 * with the image and we should have allocated all of the filter buffers
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
696 * that have been selected. If prev_row isn't already allocated, then
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
697 * it is too late to start using the filters that need it, since we
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
698 * will be missing the data in the previous row. If an application
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
699 * wants to start and stop using particular filters during compression,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
700 * it should start out with all of the filters, and then add and
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
701 * remove them after the start of compression.
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
702 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
703 if (png_ptr->row_buf != NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
704 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
705 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
706 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
707 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
708 (png_ptr->rowbytes + 1));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
709 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
710 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
711
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
712 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
713 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
714 if (png_ptr->prev_row == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
715 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
716 png_warning(png_ptr, "Can't add Up filter after starting");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
717 png_ptr->do_filter &= ~PNG_FILTER_UP;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
718 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
719 else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
720 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
721 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
722 (png_ptr->rowbytes + 1));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
723 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
724 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
725 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
726
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
727 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
728 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
729 if (png_ptr->prev_row == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
730 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
731 png_warning(png_ptr, "Can't add Average filter after starting");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
732 png_ptr->do_filter &= ~PNG_FILTER_AVG;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
733 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
734 else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
735 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
736 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
737 (png_ptr->rowbytes + 1));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
738 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
739 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
740 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
741
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
742 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
743 png_ptr->paeth_row == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
744 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
745 if (png_ptr->prev_row == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
746 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
747 png_warning(png_ptr, "Can't add Paeth filter after starting");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
748 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
749 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
750 else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
751 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
752 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
753 (png_ptr->rowbytes + 1));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
754 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
755 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
756 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
757
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
758 if (png_ptr->do_filter == PNG_NO_FILTERS)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
759 png_ptr->do_filter = PNG_FILTER_NONE;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
760 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
761 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
762 else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
763 png_error(png_ptr, "Unknown custom filter method");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
764 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
765
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
766 /* This allows us to influence the way in which libpng chooses the "best"
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
767 * filter for the current scanline. While the "minimum-sum-of-absolute-
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
768 * differences metric is relatively fast and effective, there is some
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
769 * question as to whether it can be improved upon by trying to keep the
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
770 * filtered data going to zlib more consistent, hopefully resulting in
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
771 * better compression.
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
772 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
773 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
774 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
775 png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
776 int num_weights, png_doublep filter_weights,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
777 png_doublep filter_costs)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
778 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
779 int i;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
780
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
781 png_debug(1, "in png_set_filter_heuristics\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
782 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
783 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
784 png_warning(png_ptr, "Unknown filter heuristic method");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
785 return;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
786 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
787
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
788 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
789 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
790 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
791 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
792
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
793 if (num_weights < 0 || filter_weights == NULL ||
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
794 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
795 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
796 num_weights = 0;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
797 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
798
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
799 png_ptr->num_prev_filters = (png_byte)num_weights;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
800 png_ptr->heuristic_method = (png_byte)heuristic_method;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
801
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
802 if (num_weights > 0)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
803 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
804 if (png_ptr->prev_filters == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
805 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
806 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
807 (png_uint_32)(sizeof(png_byte) * num_weights));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
808
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
809 /* To make sure that the weighting starts out fairly */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
810 for (i = 0; i < num_weights; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
811 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
812 png_ptr->prev_filters[i] = 255;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
813 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
814 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
815
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
816 if (png_ptr->filter_weights == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
817 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
818 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
819 (png_uint_32)(sizeof(png_uint_16) * num_weights));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
820
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
821 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
822 (png_uint_32)(sizeof(png_uint_16) * num_weights));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
823 for (i = 0; i < num_weights; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
824 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
825 png_ptr->inv_filter_weights[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
826 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
827 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
828 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
829
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
830 for (i = 0; i < num_weights; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
831 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
832 if (filter_weights[i] < 0.0)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
833 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
834 png_ptr->inv_filter_weights[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
835 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
836 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
837 else
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
838 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
839 png_ptr->inv_filter_weights[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
840 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
841 png_ptr->filter_weights[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
842 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
843 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
844 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
845 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
846
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
847 /* If, in the future, there are other filter methods, this would
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
848 * need to be based on png_ptr->filter.
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
849 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
850 if (png_ptr->filter_costs == NULL)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
851 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
852 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
853 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
854
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
855 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
856 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
857
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
858 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
859 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
860 png_ptr->inv_filter_costs[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
861 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
862 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
863 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
864
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
865 /* Here is where we set the relative costs of the different filters. We
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
866 * should take the desired compression level into account when setting
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
867 * the costs, so that Paeth, for instance, has a high relative cost at low
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
868 * compression levels, while it has a lower relative cost at higher
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
869 * compression settings. The filter types are in order of increasing
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
870 * relative cost, so it would be possible to do this with an algorithm.
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
871 */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
872 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
873 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
874 if (filter_costs == NULL || filter_costs[i] < 0.0)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
875 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
876 png_ptr->inv_filter_costs[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
877 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
878 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
879 else if (filter_costs[i] >= 1.0)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
880 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
881 png_ptr->inv_filter_costs[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
882 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
883 png_ptr->filter_costs[i] =
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
884 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
885 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
886 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
887 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
888 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
889
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
890 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
891 png_set_compression_level(png_structp png_ptr, int level)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
892 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
893 png_debug(1, "in png_set_compression_level\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
894 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
895 png_ptr->zlib_level = level;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
896 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
897
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
898 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
899 png_set_compression_mem_level(png_structp png_ptr, int mem_level)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
900 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
901 png_debug(1, "in png_set_compression_mem_level\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
902 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
903 png_ptr->zlib_mem_level = mem_level;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
904 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
905
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
906 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
907 png_set_compression_strategy(png_structp png_ptr, int strategy)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
908 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
909 png_debug(1, "in png_set_compression_strategy\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
910 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
911 png_ptr->zlib_strategy = strategy;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
912 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
913
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
914 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
915 png_set_compression_window_bits(png_structp png_ptr, int window_bits)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
916 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
917 if (window_bits > 15)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
918 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
919 else if (window_bits < 8)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
920 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
921 #ifndef WBITS_8_OK
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
922 /* avoid libpng bug with 256-byte windows */
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
923 if (window_bits == 8)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
924 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
925 png_warning(png_ptr, "Compression window is being reset to 512");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
926 window_bits=9;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
927 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
928 #endif
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
929 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
930 png_ptr->zlib_window_bits = window_bits;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
931 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
932
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
933 void PNGAPI
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
934 png_set_compression_method(png_structp png_ptr, int method)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
935 {
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
936 png_debug(1, "in png_set_compression_method\n");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
937 if (method != 8)
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
938 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
939 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
940 png_ptr->zlib_method = method;
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
941 }
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
942
cfc262e9955c added stripped down libpng
bellard
parents:
diff changeset
943 #endif /* PNG_WRITE_SUPPORTED */