1 | |
---|
2 | /* pngwrite.c - general routines to write a PNG file |
---|
3 | * |
---|
4 | * Last changed in libpng 1.5.11 [June 14, 2012] |
---|
5 | * Copyright (c) 1998-2012 Glenn Randers-Pehrson |
---|
6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
---|
7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
---|
8 | * |
---|
9 | * This code is released under the libpng license. |
---|
10 | * For conditions of distribution and use, see the disclaimer |
---|
11 | * and license in png.h |
---|
12 | */ |
---|
13 | |
---|
14 | #include "pngpriv.h" |
---|
15 | |
---|
16 | #ifdef PNG_WRITE_SUPPORTED |
---|
17 | |
---|
18 | /* Writes all the PNG information. This is the suggested way to use the |
---|
19 | * library. If you have a new chunk to add, make a function to write it, |
---|
20 | * and put it in the correct location here. If you want the chunk written |
---|
21 | * after the image data, put it in png_write_end(). I strongly encourage |
---|
22 | * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing |
---|
23 | * the chunk, as that will keep the code from breaking if you want to just |
---|
24 | * write a plain PNG file. If you have long comments, I suggest writing |
---|
25 | * them in png_write_end(), and compressing them. |
---|
26 | */ |
---|
27 | void PNGAPI |
---|
28 | png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr) |
---|
29 | { |
---|
30 | png_debug(1, "in png_write_info_before_PLTE"); |
---|
31 | |
---|
32 | if (png_ptr == NULL || info_ptr == NULL) |
---|
33 | return; |
---|
34 | |
---|
35 | if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) |
---|
36 | { |
---|
37 | /* Write PNG signature */ |
---|
38 | png_write_sig(png_ptr); |
---|
39 | |
---|
40 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
---|
41 | if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \ |
---|
42 | (png_ptr->mng_features_permitted)) |
---|
43 | { |
---|
44 | png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); |
---|
45 | png_ptr->mng_features_permitted = 0; |
---|
46 | } |
---|
47 | #endif |
---|
48 | |
---|
49 | /* Write IHDR information. */ |
---|
50 | png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, |
---|
51 | info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, |
---|
52 | info_ptr->filter_type, |
---|
53 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
---|
54 | info_ptr->interlace_type); |
---|
55 | #else |
---|
56 | 0); |
---|
57 | #endif |
---|
58 | /* The rest of these check to see if the valid field has the appropriate |
---|
59 | * flag set, and if it does, writes the chunk. |
---|
60 | */ |
---|
61 | #ifdef PNG_WRITE_gAMA_SUPPORTED |
---|
62 | if (info_ptr->valid & PNG_INFO_gAMA) |
---|
63 | png_write_gAMA_fixed(png_ptr, info_ptr->gamma); |
---|
64 | #endif |
---|
65 | #ifdef PNG_WRITE_sRGB_SUPPORTED |
---|
66 | if (info_ptr->valid & PNG_INFO_sRGB) |
---|
67 | png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent); |
---|
68 | #endif |
---|
69 | |
---|
70 | #ifdef PNG_WRITE_iCCP_SUPPORTED |
---|
71 | if (info_ptr->valid & PNG_INFO_iCCP) |
---|
72 | png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE, |
---|
73 | (png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen); |
---|
74 | #endif |
---|
75 | #ifdef PNG_WRITE_sBIT_SUPPORTED |
---|
76 | if (info_ptr->valid & PNG_INFO_sBIT) |
---|
77 | png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); |
---|
78 | #endif |
---|
79 | #ifdef PNG_WRITE_cHRM_SUPPORTED |
---|
80 | if (info_ptr->valid & PNG_INFO_cHRM) |
---|
81 | png_write_cHRM_fixed(png_ptr, |
---|
82 | info_ptr->x_white, info_ptr->y_white, |
---|
83 | info_ptr->x_red, info_ptr->y_red, |
---|
84 | info_ptr->x_green, info_ptr->y_green, |
---|
85 | info_ptr->x_blue, info_ptr->y_blue); |
---|
86 | #endif |
---|
87 | |
---|
88 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
---|
89 | if (info_ptr->unknown_chunks_num) |
---|
90 | { |
---|
91 | png_unknown_chunk *up; |
---|
92 | |
---|
93 | png_debug(5, "writing extra chunks"); |
---|
94 | |
---|
95 | for (up = info_ptr->unknown_chunks; |
---|
96 | up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; |
---|
97 | up++) |
---|
98 | { |
---|
99 | int keep = png_handle_as_unknown(png_ptr, up->name); |
---|
100 | |
---|
101 | if (keep != PNG_HANDLE_CHUNK_NEVER && |
---|
102 | up->location && |
---|
103 | !(up->location & PNG_HAVE_PLTE) && |
---|
104 | !(up->location & PNG_HAVE_IDAT) && |
---|
105 | !(up->location & PNG_AFTER_IDAT) && |
---|
106 | ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || |
---|
107 | (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) |
---|
108 | { |
---|
109 | if (up->size == 0) |
---|
110 | png_warning(png_ptr, "Writing zero-length unknown chunk"); |
---|
111 | |
---|
112 | png_write_chunk(png_ptr, up->name, up->data, up->size); |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | #endif |
---|
117 | png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | void PNGAPI |
---|
122 | png_write_info(png_structp png_ptr, png_infop info_ptr) |
---|
123 | { |
---|
124 | #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) |
---|
125 | int i; |
---|
126 | #endif |
---|
127 | |
---|
128 | png_debug(1, "in png_write_info"); |
---|
129 | |
---|
130 | if (png_ptr == NULL || info_ptr == NULL) |
---|
131 | return; |
---|
132 | |
---|
133 | png_write_info_before_PLTE(png_ptr, info_ptr); |
---|
134 | |
---|
135 | if (info_ptr->valid & PNG_INFO_PLTE) |
---|
136 | png_write_PLTE(png_ptr, info_ptr->palette, |
---|
137 | (png_uint_32)info_ptr->num_palette); |
---|
138 | |
---|
139 | else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
---|
140 | png_error(png_ptr, "Valid palette required for paletted images"); |
---|
141 | |
---|
142 | #ifdef PNG_WRITE_tRNS_SUPPORTED |
---|
143 | if (info_ptr->valid & PNG_INFO_tRNS) |
---|
144 | { |
---|
145 | #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
---|
146 | /* Invert the alpha channel (in tRNS) */ |
---|
147 | if ((png_ptr->transformations & PNG_INVERT_ALPHA) && |
---|
148 | info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
---|
149 | { |
---|
150 | int j; |
---|
151 | for (j = 0; j<(int)info_ptr->num_trans; j++) |
---|
152 | info_ptr->trans_alpha[j] = |
---|
153 | (png_byte)(255 - info_ptr->trans_alpha[j]); |
---|
154 | } |
---|
155 | #endif |
---|
156 | png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color), |
---|
157 | info_ptr->num_trans, info_ptr->color_type); |
---|
158 | } |
---|
159 | #endif |
---|
160 | #ifdef PNG_WRITE_bKGD_SUPPORTED |
---|
161 | if (info_ptr->valid & PNG_INFO_bKGD) |
---|
162 | png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); |
---|
163 | #endif |
---|
164 | |
---|
165 | #ifdef PNG_WRITE_hIST_SUPPORTED |
---|
166 | if (info_ptr->valid & PNG_INFO_hIST) |
---|
167 | png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); |
---|
168 | #endif |
---|
169 | |
---|
170 | #ifdef PNG_WRITE_oFFs_SUPPORTED |
---|
171 | if (info_ptr->valid & PNG_INFO_oFFs) |
---|
172 | png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, |
---|
173 | info_ptr->offset_unit_type); |
---|
174 | #endif |
---|
175 | |
---|
176 | #ifdef PNG_WRITE_pCAL_SUPPORTED |
---|
177 | if (info_ptr->valid & PNG_INFO_pCAL) |
---|
178 | png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, |
---|
179 | info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, |
---|
180 | info_ptr->pcal_units, info_ptr->pcal_params); |
---|
181 | #endif |
---|
182 | |
---|
183 | #ifdef PNG_WRITE_sCAL_SUPPORTED |
---|
184 | if (info_ptr->valid & PNG_INFO_sCAL) |
---|
185 | png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, |
---|
186 | info_ptr->scal_s_width, info_ptr->scal_s_height); |
---|
187 | #endif /* sCAL */ |
---|
188 | |
---|
189 | #ifdef PNG_WRITE_pHYs_SUPPORTED |
---|
190 | if (info_ptr->valid & PNG_INFO_pHYs) |
---|
191 | png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, |
---|
192 | info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); |
---|
193 | #endif /* pHYs */ |
---|
194 | |
---|
195 | #ifdef PNG_WRITE_tIME_SUPPORTED |
---|
196 | if (info_ptr->valid & PNG_INFO_tIME) |
---|
197 | { |
---|
198 | png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
---|
199 | png_ptr->mode |= PNG_WROTE_tIME; |
---|
200 | } |
---|
201 | #endif /* tIME */ |
---|
202 | |
---|
203 | #ifdef PNG_WRITE_sPLT_SUPPORTED |
---|
204 | if (info_ptr->valid & PNG_INFO_sPLT) |
---|
205 | for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) |
---|
206 | png_write_sPLT(png_ptr, info_ptr->splt_palettes + i); |
---|
207 | #endif /* sPLT */ |
---|
208 | |
---|
209 | #ifdef PNG_WRITE_TEXT_SUPPORTED |
---|
210 | /* Check to see if we need to write text chunks */ |
---|
211 | for (i = 0; i < info_ptr->num_text; i++) |
---|
212 | { |
---|
213 | png_debug2(2, "Writing header text chunk %d, type %d", i, |
---|
214 | info_ptr->text[i].compression); |
---|
215 | /* An internationalized chunk? */ |
---|
216 | if (info_ptr->text[i].compression > 0) |
---|
217 | { |
---|
218 | #ifdef PNG_WRITE_iTXt_SUPPORTED |
---|
219 | /* Write international chunk */ |
---|
220 | png_write_iTXt(png_ptr, |
---|
221 | info_ptr->text[i].compression, |
---|
222 | info_ptr->text[i].key, |
---|
223 | info_ptr->text[i].lang, |
---|
224 | info_ptr->text[i].lang_key, |
---|
225 | info_ptr->text[i].text); |
---|
226 | #else |
---|
227 | png_warning(png_ptr, "Unable to write international text"); |
---|
228 | #endif |
---|
229 | /* Mark this chunk as written */ |
---|
230 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
---|
231 | } |
---|
232 | |
---|
233 | /* If we want a compressed text chunk */ |
---|
234 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt) |
---|
235 | { |
---|
236 | #ifdef PNG_WRITE_zTXt_SUPPORTED |
---|
237 | /* Write compressed chunk */ |
---|
238 | png_write_zTXt(png_ptr, info_ptr->text[i].key, |
---|
239 | info_ptr->text[i].text, 0, |
---|
240 | info_ptr->text[i].compression); |
---|
241 | #else |
---|
242 | png_warning(png_ptr, "Unable to write compressed text"); |
---|
243 | #endif |
---|
244 | /* Mark this chunk as written */ |
---|
245 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
---|
246 | } |
---|
247 | |
---|
248 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
---|
249 | { |
---|
250 | #ifdef PNG_WRITE_tEXt_SUPPORTED |
---|
251 | /* Write uncompressed chunk */ |
---|
252 | png_write_tEXt(png_ptr, info_ptr->text[i].key, |
---|
253 | info_ptr->text[i].text, |
---|
254 | 0); |
---|
255 | /* Mark this chunk as written */ |
---|
256 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
---|
257 | #else |
---|
258 | /* Can't get here */ |
---|
259 | png_warning(png_ptr, "Unable to write uncompressed text"); |
---|
260 | #endif |
---|
261 | } |
---|
262 | } |
---|
263 | #endif /* tEXt */ |
---|
264 | |
---|
265 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
---|
266 | if (info_ptr->unknown_chunks_num) |
---|
267 | { |
---|
268 | png_unknown_chunk *up; |
---|
269 | |
---|
270 | png_debug(5, "writing extra chunks"); |
---|
271 | |
---|
272 | for (up = info_ptr->unknown_chunks; |
---|
273 | up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; |
---|
274 | up++) |
---|
275 | { |
---|
276 | int keep = png_handle_as_unknown(png_ptr, up->name); |
---|
277 | if (keep != PNG_HANDLE_CHUNK_NEVER && |
---|
278 | up->location && |
---|
279 | (up->location & PNG_HAVE_PLTE) && |
---|
280 | !(up->location & PNG_HAVE_IDAT) && |
---|
281 | !(up->location & PNG_AFTER_IDAT) && |
---|
282 | ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || |
---|
283 | (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) |
---|
284 | { |
---|
285 | png_write_chunk(png_ptr, up->name, up->data, up->size); |
---|
286 | } |
---|
287 | } |
---|
288 | } |
---|
289 | #endif |
---|
290 | } |
---|
291 | |
---|
292 | /* Writes the end of the PNG file. If you don't want to write comments or |
---|
293 | * time information, you can pass NULL for info. If you already wrote these |
---|
294 | * in png_write_info(), do not write them again here. If you have long |
---|
295 | * comments, I suggest writing them here, and compressing them. |
---|
296 | */ |
---|
297 | void PNGAPI |
---|
298 | png_write_end(png_structp png_ptr, png_infop info_ptr) |
---|
299 | { |
---|
300 | png_debug(1, "in png_write_end"); |
---|
301 | |
---|
302 | if (png_ptr == NULL) |
---|
303 | return; |
---|
304 | |
---|
305 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
---|
306 | png_error(png_ptr, "No IDATs written into file"); |
---|
307 | |
---|
308 | #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED |
---|
309 | if (png_ptr->num_palette_max > png_ptr->num_palette) |
---|
310 | png_benign_error(png_ptr, "Wrote palette index exceeding num_palette"); |
---|
311 | #endif |
---|
312 | |
---|
313 | /* See if user wants us to write information chunks */ |
---|
314 | if (info_ptr != NULL) |
---|
315 | { |
---|
316 | #ifdef PNG_WRITE_TEXT_SUPPORTED |
---|
317 | int i; /* local index variable */ |
---|
318 | #endif |
---|
319 | #ifdef PNG_WRITE_tIME_SUPPORTED |
---|
320 | /* Check to see if user has supplied a time chunk */ |
---|
321 | if ((info_ptr->valid & PNG_INFO_tIME) && |
---|
322 | !(png_ptr->mode & PNG_WROTE_tIME)) |
---|
323 | png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
---|
324 | |
---|
325 | #endif |
---|
326 | #ifdef PNG_WRITE_TEXT_SUPPORTED |
---|
327 | /* Loop through comment chunks */ |
---|
328 | for (i = 0; i < info_ptr->num_text; i++) |
---|
329 | { |
---|
330 | png_debug2(2, "Writing trailer text chunk %d, type %d", i, |
---|
331 | info_ptr->text[i].compression); |
---|
332 | /* An internationalized chunk? */ |
---|
333 | if (info_ptr->text[i].compression > 0) |
---|
334 | { |
---|
335 | #ifdef PNG_WRITE_iTXt_SUPPORTED |
---|
336 | /* Write international chunk */ |
---|
337 | png_write_iTXt(png_ptr, |
---|
338 | info_ptr->text[i].compression, |
---|
339 | info_ptr->text[i].key, |
---|
340 | info_ptr->text[i].lang, |
---|
341 | info_ptr->text[i].lang_key, |
---|
342 | info_ptr->text[i].text); |
---|
343 | #else |
---|
344 | png_warning(png_ptr, "Unable to write international text"); |
---|
345 | #endif |
---|
346 | /* Mark this chunk as written */ |
---|
347 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
---|
348 | } |
---|
349 | |
---|
350 | else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) |
---|
351 | { |
---|
352 | #ifdef PNG_WRITE_zTXt_SUPPORTED |
---|
353 | /* Write compressed chunk */ |
---|
354 | png_write_zTXt(png_ptr, info_ptr->text[i].key, |
---|
355 | info_ptr->text[i].text, 0, |
---|
356 | info_ptr->text[i].compression); |
---|
357 | #else |
---|
358 | png_warning(png_ptr, "Unable to write compressed text"); |
---|
359 | #endif |
---|
360 | /* Mark this chunk as written */ |
---|
361 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
---|
362 | } |
---|
363 | |
---|
364 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
---|
365 | { |
---|
366 | #ifdef PNG_WRITE_tEXt_SUPPORTED |
---|
367 | /* Write uncompressed chunk */ |
---|
368 | png_write_tEXt(png_ptr, info_ptr->text[i].key, |
---|
369 | info_ptr->text[i].text, 0); |
---|
370 | #else |
---|
371 | png_warning(png_ptr, "Unable to write uncompressed text"); |
---|
372 | #endif |
---|
373 | |
---|
374 | /* Mark this chunk as written */ |
---|
375 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
---|
376 | } |
---|
377 | } |
---|
378 | #endif |
---|
379 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
---|
380 | if (info_ptr->unknown_chunks_num) |
---|
381 | { |
---|
382 | png_unknown_chunk *up; |
---|
383 | |
---|
384 | png_debug(5, "writing extra chunks"); |
---|
385 | |
---|
386 | for (up = info_ptr->unknown_chunks; |
---|
387 | up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; |
---|
388 | up++) |
---|
389 | { |
---|
390 | int keep = png_handle_as_unknown(png_ptr, up->name); |
---|
391 | if (keep != PNG_HANDLE_CHUNK_NEVER && |
---|
392 | up->location && |
---|
393 | (up->location & PNG_AFTER_IDAT) && |
---|
394 | ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || |
---|
395 | (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) |
---|
396 | { |
---|
397 | png_write_chunk(png_ptr, up->name, up->data, up->size); |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | #endif |
---|
402 | } |
---|
403 | |
---|
404 | png_ptr->mode |= PNG_AFTER_IDAT; |
---|
405 | |
---|
406 | /* Write end of PNG file */ |
---|
407 | png_write_IEND(png_ptr); |
---|
408 | /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03, |
---|
409 | * and restored again in libpng-1.2.30, may cause some applications that |
---|
410 | * do not set png_ptr->output_flush_fn to crash. If your application |
---|
411 | * experiences a problem, please try building libpng with |
---|
412 | * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to |
---|
413 | * png-mng-implement at lists.sf.net . |
---|
414 | */ |
---|
415 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
---|
416 | # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED |
---|
417 | png_flush(png_ptr); |
---|
418 | # endif |
---|
419 | #endif |
---|
420 | } |
---|
421 | |
---|
422 | #ifdef PNG_CONVERT_tIME_SUPPORTED |
---|
423 | /* "tm" structure is not supported on WindowsCE */ |
---|
424 | void PNGAPI |
---|
425 | png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime) |
---|
426 | { |
---|
427 | png_debug(1, "in png_convert_from_struct_tm"); |
---|
428 | |
---|
429 | ptime->year = (png_uint_16)(1900 + ttime->tm_year); |
---|
430 | ptime->month = (png_byte)(ttime->tm_mon + 1); |
---|
431 | ptime->day = (png_byte)ttime->tm_mday; |
---|
432 | ptime->hour = (png_byte)ttime->tm_hour; |
---|
433 | ptime->minute = (png_byte)ttime->tm_min; |
---|
434 | ptime->second = (png_byte)ttime->tm_sec; |
---|
435 | } |
---|
436 | |
---|
437 | void PNGAPI |
---|
438 | png_convert_from_time_t(png_timep ptime, time_t ttime) |
---|
439 | { |
---|
440 | struct tm *tbuf; |
---|
441 | |
---|
442 | png_debug(1, "in png_convert_from_time_t"); |
---|
443 | |
---|
444 | tbuf = gmtime(&ttime); |
---|
445 | png_convert_from_struct_tm(ptime, tbuf); |
---|
446 | } |
---|
447 | #endif |
---|
448 | |
---|
449 | /* Initialize png_ptr structure, and allocate any memory needed */ |
---|
450 | PNG_FUNCTION(png_structp,PNGAPI |
---|
451 | png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr, |
---|
452 | png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) |
---|
453 | { |
---|
454 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
455 | return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn, |
---|
456 | warn_fn, NULL, NULL, NULL)); |
---|
457 | } |
---|
458 | |
---|
459 | /* Alternate initialize png_ptr structure, and allocate any memory needed */ |
---|
460 | static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */ |
---|
461 | |
---|
462 | PNG_FUNCTION(png_structp,PNGAPI |
---|
463 | png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, |
---|
464 | png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, |
---|
465 | png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) |
---|
466 | { |
---|
467 | #endif /* PNG_USER_MEM_SUPPORTED */ |
---|
468 | volatile int png_cleanup_needed = 0; |
---|
469 | #ifdef PNG_SETJMP_SUPPORTED |
---|
470 | volatile |
---|
471 | #endif |
---|
472 | png_structp png_ptr; |
---|
473 | #ifdef PNG_SETJMP_SUPPORTED |
---|
474 | #ifdef USE_FAR_KEYWORD |
---|
475 | jmp_buf tmp_jmpbuf; |
---|
476 | #endif |
---|
477 | #endif |
---|
478 | |
---|
479 | png_debug(1, "in png_create_write_struct"); |
---|
480 | |
---|
481 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
482 | png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, |
---|
483 | (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr); |
---|
484 | #else |
---|
485 | png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); |
---|
486 | #endif /* PNG_USER_MEM_SUPPORTED */ |
---|
487 | if (png_ptr == NULL) |
---|
488 | return (NULL); |
---|
489 | |
---|
490 | /* Added at libpng-1.2.6 */ |
---|
491 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
---|
492 | png_ptr->user_width_max = PNG_USER_WIDTH_MAX; |
---|
493 | png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; |
---|
494 | #endif |
---|
495 | |
---|
496 | #ifdef PNG_SETJMP_SUPPORTED |
---|
497 | /* Applications that neglect to set up their own setjmp() and then |
---|
498 | * encounter a png_error() will longjmp here. Since the jmpbuf is |
---|
499 | * then meaningless we abort instead of returning. |
---|
500 | */ |
---|
501 | #ifdef USE_FAR_KEYWORD |
---|
502 | if (setjmp(tmp_jmpbuf)) |
---|
503 | #else |
---|
504 | if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */ |
---|
505 | #endif |
---|
506 | #ifdef USE_FAR_KEYWORD |
---|
507 | png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf)); |
---|
508 | #endif |
---|
509 | PNG_ABORT(); |
---|
510 | #endif |
---|
511 | |
---|
512 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
513 | png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); |
---|
514 | #endif /* PNG_USER_MEM_SUPPORTED */ |
---|
515 | png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); |
---|
516 | |
---|
517 | if (!png_user_version_check(png_ptr, user_png_ver)) |
---|
518 | png_cleanup_needed = 1; |
---|
519 | |
---|
520 | /* Initialize zbuf - compression buffer */ |
---|
521 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
---|
522 | |
---|
523 | if (!png_cleanup_needed) |
---|
524 | { |
---|
525 | png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, |
---|
526 | png_ptr->zbuf_size); |
---|
527 | if (png_ptr->zbuf == NULL) |
---|
528 | png_cleanup_needed = 1; |
---|
529 | } |
---|
530 | |
---|
531 | if (png_cleanup_needed) |
---|
532 | { |
---|
533 | /* Clean up PNG structure and deallocate any memory. */ |
---|
534 | png_free(png_ptr, png_ptr->zbuf); |
---|
535 | png_ptr->zbuf = NULL; |
---|
536 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
537 | png_destroy_struct_2((png_voidp)png_ptr, |
---|
538 | (png_free_ptr)free_fn, (png_voidp)mem_ptr); |
---|
539 | #else |
---|
540 | png_destroy_struct((png_voidp)png_ptr); |
---|
541 | #endif |
---|
542 | return (NULL); |
---|
543 | } |
---|
544 | |
---|
545 | png_set_write_fn(png_ptr, NULL, NULL, NULL); |
---|
546 | |
---|
547 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
---|
548 | png_reset_filter_heuristics(png_ptr); |
---|
549 | #endif |
---|
550 | |
---|
551 | return (png_ptr); |
---|
552 | } |
---|
553 | |
---|
554 | |
---|
555 | /* Write a few rows of image data. If the image is interlaced, |
---|
556 | * either you will have to write the 7 sub images, or, if you |
---|
557 | * have called png_set_interlace_handling(), you will have to |
---|
558 | * "write" the image seven times. |
---|
559 | */ |
---|
560 | void PNGAPI |
---|
561 | png_write_rows(png_structp png_ptr, png_bytepp row, |
---|
562 | png_uint_32 num_rows) |
---|
563 | { |
---|
564 | png_uint_32 i; /* row counter */ |
---|
565 | png_bytepp rp; /* row pointer */ |
---|
566 | |
---|
567 | png_debug(1, "in png_write_rows"); |
---|
568 | |
---|
569 | if (png_ptr == NULL) |
---|
570 | return; |
---|
571 | |
---|
572 | /* Loop through the rows */ |
---|
573 | for (i = 0, rp = row; i < num_rows; i++, rp++) |
---|
574 | { |
---|
575 | png_write_row(png_ptr, *rp); |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | /* Write the image. You only need to call this function once, even |
---|
580 | * if you are writing an interlaced image. |
---|
581 | */ |
---|
582 | void PNGAPI |
---|
583 | png_write_image(png_structp png_ptr, png_bytepp image) |
---|
584 | { |
---|
585 | png_uint_32 i; /* row index */ |
---|
586 | int pass, num_pass; /* pass variables */ |
---|
587 | png_bytepp rp; /* points to current row */ |
---|
588 | |
---|
589 | if (png_ptr == NULL) |
---|
590 | return; |
---|
591 | |
---|
592 | png_debug(1, "in png_write_image"); |
---|
593 | |
---|
594 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
---|
595 | /* Initialize interlace handling. If image is not interlaced, |
---|
596 | * this will set pass to 1 |
---|
597 | */ |
---|
598 | num_pass = png_set_interlace_handling(png_ptr); |
---|
599 | #else |
---|
600 | num_pass = 1; |
---|
601 | #endif |
---|
602 | /* Loop through passes */ |
---|
603 | for (pass = 0; pass < num_pass; pass++) |
---|
604 | { |
---|
605 | /* Loop through image */ |
---|
606 | for (i = 0, rp = image; i < png_ptr->height; i++, rp++) |
---|
607 | { |
---|
608 | png_write_row(png_ptr, *rp); |
---|
609 | } |
---|
610 | } |
---|
611 | } |
---|
612 | |
---|
613 | /* Called by user to write a row of image data */ |
---|
614 | void PNGAPI |
---|
615 | png_write_row(png_structp png_ptr, png_const_bytep row) |
---|
616 | { |
---|
617 | /* 1.5.6: moved from png_struct to be a local structure: */ |
---|
618 | png_row_info row_info; |
---|
619 | |
---|
620 | if (png_ptr == NULL) |
---|
621 | return; |
---|
622 | |
---|
623 | png_debug2(1, "in png_write_row (row %u, pass %d)", |
---|
624 | png_ptr->row_number, png_ptr->pass); |
---|
625 | |
---|
626 | /* Initialize transformations and other stuff if first time */ |
---|
627 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
---|
628 | { |
---|
629 | /* Make sure we wrote the header info */ |
---|
630 | if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) |
---|
631 | png_error(png_ptr, |
---|
632 | "png_write_info was never called before png_write_row"); |
---|
633 | |
---|
634 | /* Check for transforms that have been set but were defined out */ |
---|
635 | #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED) |
---|
636 | if (png_ptr->transformations & PNG_INVERT_MONO) |
---|
637 | png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined"); |
---|
638 | #endif |
---|
639 | |
---|
640 | #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED) |
---|
641 | if (png_ptr->transformations & PNG_FILLER) |
---|
642 | png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined"); |
---|
643 | #endif |
---|
644 | #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
---|
645 | defined(PNG_READ_PACKSWAP_SUPPORTED) |
---|
646 | if (png_ptr->transformations & PNG_PACKSWAP) |
---|
647 | png_warning(png_ptr, |
---|
648 | "PNG_WRITE_PACKSWAP_SUPPORTED is not defined"); |
---|
649 | #endif |
---|
650 | |
---|
651 | #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED) |
---|
652 | if (png_ptr->transformations & PNG_PACK) |
---|
653 | png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined"); |
---|
654 | #endif |
---|
655 | |
---|
656 | #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED) |
---|
657 | if (png_ptr->transformations & PNG_SHIFT) |
---|
658 | png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined"); |
---|
659 | #endif |
---|
660 | |
---|
661 | #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED) |
---|
662 | if (png_ptr->transformations & PNG_BGR) |
---|
663 | png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined"); |
---|
664 | #endif |
---|
665 | |
---|
666 | #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED) |
---|
667 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
---|
668 | png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined"); |
---|
669 | #endif |
---|
670 | |
---|
671 | png_write_start_row(png_ptr); |
---|
672 | } |
---|
673 | |
---|
674 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
---|
675 | /* If interlaced and not interested in row, return */ |
---|
676 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
---|
677 | { |
---|
678 | switch (png_ptr->pass) |
---|
679 | { |
---|
680 | case 0: |
---|
681 | if (png_ptr->row_number & 0x07) |
---|
682 | { |
---|
683 | png_write_finish_row(png_ptr); |
---|
684 | return; |
---|
685 | } |
---|
686 | break; |
---|
687 | |
---|
688 | case 1: |
---|
689 | if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
---|
690 | { |
---|
691 | png_write_finish_row(png_ptr); |
---|
692 | return; |
---|
693 | } |
---|
694 | break; |
---|
695 | |
---|
696 | case 2: |
---|
697 | if ((png_ptr->row_number & 0x07) != 4) |
---|
698 | { |
---|
699 | png_write_finish_row(png_ptr); |
---|
700 | return; |
---|
701 | } |
---|
702 | break; |
---|
703 | |
---|
704 | case 3: |
---|
705 | if ((png_ptr->row_number & 0x03) || png_ptr->width < 3) |
---|
706 | { |
---|
707 | png_write_finish_row(png_ptr); |
---|
708 | return; |
---|
709 | } |
---|
710 | break; |
---|
711 | |
---|
712 | case 4: |
---|
713 | if ((png_ptr->row_number & 0x03) != 2) |
---|
714 | { |
---|
715 | png_write_finish_row(png_ptr); |
---|
716 | return; |
---|
717 | } |
---|
718 | break; |
---|
719 | |
---|
720 | case 5: |
---|
721 | if ((png_ptr->row_number & 0x01) || png_ptr->width < 2) |
---|
722 | { |
---|
723 | png_write_finish_row(png_ptr); |
---|
724 | return; |
---|
725 | } |
---|
726 | break; |
---|
727 | |
---|
728 | case 6: |
---|
729 | if (!(png_ptr->row_number & 0x01)) |
---|
730 | { |
---|
731 | png_write_finish_row(png_ptr); |
---|
732 | return; |
---|
733 | } |
---|
734 | break; |
---|
735 | |
---|
736 | default: /* error: ignore it */ |
---|
737 | break; |
---|
738 | } |
---|
739 | } |
---|
740 | #endif |
---|
741 | |
---|
742 | /* Set up row info for transformations */ |
---|
743 | row_info.color_type = png_ptr->color_type; |
---|
744 | row_info.width = png_ptr->usr_width; |
---|
745 | row_info.channels = png_ptr->usr_channels; |
---|
746 | row_info.bit_depth = png_ptr->usr_bit_depth; |
---|
747 | row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels); |
---|
748 | row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); |
---|
749 | |
---|
750 | png_debug1(3, "row_info->color_type = %d", row_info.color_type); |
---|
751 | png_debug1(3, "row_info->width = %u", row_info.width); |
---|
752 | png_debug1(3, "row_info->channels = %d", row_info.channels); |
---|
753 | png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth); |
---|
754 | png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth); |
---|
755 | png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes); |
---|
756 | |
---|
757 | /* Copy user's row into buffer, leaving room for filter byte. */ |
---|
758 | png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes); |
---|
759 | |
---|
760 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
---|
761 | /* Handle interlacing */ |
---|
762 | if (png_ptr->interlaced && png_ptr->pass < 6 && |
---|
763 | (png_ptr->transformations & PNG_INTERLACE)) |
---|
764 | { |
---|
765 | png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass); |
---|
766 | /* This should always get caught above, but still ... */ |
---|
767 | if (!(row_info.width)) |
---|
768 | { |
---|
769 | png_write_finish_row(png_ptr); |
---|
770 | return; |
---|
771 | } |
---|
772 | } |
---|
773 | #endif |
---|
774 | |
---|
775 | #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED |
---|
776 | /* Handle other transformations */ |
---|
777 | if (png_ptr->transformations) |
---|
778 | png_do_write_transformations(png_ptr, &row_info); |
---|
779 | #endif |
---|
780 | |
---|
781 | /* At this point the row_info pixel depth must match the 'transformed' depth, |
---|
782 | * which is also the output depth. |
---|
783 | */ |
---|
784 | if (row_info.pixel_depth != png_ptr->pixel_depth || |
---|
785 | row_info.pixel_depth != png_ptr->transformed_pixel_depth) |
---|
786 | png_error(png_ptr, "internal write transform logic error"); |
---|
787 | |
---|
788 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
---|
789 | /* Write filter_method 64 (intrapixel differencing) only if |
---|
790 | * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and |
---|
791 | * 2. Libpng did not write a PNG signature (this filter_method is only |
---|
792 | * used in PNG datastreams that are embedded in MNG datastreams) and |
---|
793 | * 3. The application called png_permit_mng_features with a mask that |
---|
794 | * included PNG_FLAG_MNG_FILTER_64 and |
---|
795 | * 4. The filter_method is 64 and |
---|
796 | * 5. The color_type is RGB or RGBA |
---|
797 | */ |
---|
798 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
---|
799 | (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
---|
800 | { |
---|
801 | /* Intrapixel differencing */ |
---|
802 | png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1); |
---|
803 | } |
---|
804 | #endif |
---|
805 | |
---|
806 | /* Added at libpng-1.5.10 */ |
---|
807 | #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED |
---|
808 | /* Check for out-of-range palette index */ |
---|
809 | if (row_info.color_type == PNG_COLOR_TYPE_PALETTE && |
---|
810 | png_ptr->num_palette_max >= 0) |
---|
811 | png_do_check_palette_indexes(png_ptr, &row_info); |
---|
812 | #endif |
---|
813 | |
---|
814 | /* Find a filter if necessary, filter the row and write it out. */ |
---|
815 | png_write_find_filter(png_ptr, &row_info); |
---|
816 | |
---|
817 | if (png_ptr->write_row_fn != NULL) |
---|
818 | (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
---|
819 | } |
---|
820 | |
---|
821 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
---|
822 | /* Set the automatic flush interval or 0 to turn flushing off */ |
---|
823 | void PNGAPI |
---|
824 | png_set_flush(png_structp png_ptr, int nrows) |
---|
825 | { |
---|
826 | png_debug(1, "in png_set_flush"); |
---|
827 | |
---|
828 | if (png_ptr == NULL) |
---|
829 | return; |
---|
830 | |
---|
831 | png_ptr->flush_dist = (nrows < 0 ? 0 : nrows); |
---|
832 | } |
---|
833 | |
---|
834 | /* Flush the current output buffers now */ |
---|
835 | void PNGAPI |
---|
836 | png_write_flush(png_structp png_ptr) |
---|
837 | { |
---|
838 | int wrote_IDAT; |
---|
839 | |
---|
840 | png_debug(1, "in png_write_flush"); |
---|
841 | |
---|
842 | if (png_ptr == NULL) |
---|
843 | return; |
---|
844 | |
---|
845 | /* We have already written out all of the data */ |
---|
846 | if (png_ptr->row_number >= png_ptr->num_rows) |
---|
847 | return; |
---|
848 | |
---|
849 | do |
---|
850 | { |
---|
851 | int ret; |
---|
852 | |
---|
853 | /* Compress the data */ |
---|
854 | ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH); |
---|
855 | wrote_IDAT = 0; |
---|
856 | |
---|
857 | /* Check for compression errors */ |
---|
858 | if (ret != Z_OK) |
---|
859 | { |
---|
860 | if (png_ptr->zstream.msg != NULL) |
---|
861 | png_error(png_ptr, png_ptr->zstream.msg); |
---|
862 | |
---|
863 | else |
---|
864 | png_error(png_ptr, "zlib error"); |
---|
865 | } |
---|
866 | |
---|
867 | if (!(png_ptr->zstream.avail_out)) |
---|
868 | { |
---|
869 | /* Write the IDAT and reset the zlib output buffer */ |
---|
870 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
---|
871 | wrote_IDAT = 1; |
---|
872 | } |
---|
873 | } while (wrote_IDAT == 1); |
---|
874 | |
---|
875 | /* If there is any data left to be output, write it into a new IDAT */ |
---|
876 | if (png_ptr->zbuf_size != png_ptr->zstream.avail_out) |
---|
877 | { |
---|
878 | /* Write the IDAT and reset the zlib output buffer */ |
---|
879 | png_write_IDAT(png_ptr, png_ptr->zbuf, |
---|
880 | png_ptr->zbuf_size - png_ptr->zstream.avail_out); |
---|
881 | } |
---|
882 | png_ptr->flush_rows = 0; |
---|
883 | png_flush(png_ptr); |
---|
884 | } |
---|
885 | #endif /* PNG_WRITE_FLUSH_SUPPORTED */ |
---|
886 | |
---|
887 | /* Free all memory used by the write */ |
---|
888 | void PNGAPI |
---|
889 | png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) |
---|
890 | { |
---|
891 | png_structp png_ptr = NULL; |
---|
892 | png_infop info_ptr = NULL; |
---|
893 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
894 | png_free_ptr free_fn = NULL; |
---|
895 | png_voidp mem_ptr = NULL; |
---|
896 | #endif |
---|
897 | |
---|
898 | png_debug(1, "in png_destroy_write_struct"); |
---|
899 | |
---|
900 | if (png_ptr_ptr != NULL) |
---|
901 | png_ptr = *png_ptr_ptr; |
---|
902 | |
---|
903 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
904 | if (png_ptr != NULL) |
---|
905 | { |
---|
906 | free_fn = png_ptr->free_fn; |
---|
907 | mem_ptr = png_ptr->mem_ptr; |
---|
908 | } |
---|
909 | #endif |
---|
910 | |
---|
911 | if (info_ptr_ptr != NULL) |
---|
912 | info_ptr = *info_ptr_ptr; |
---|
913 | |
---|
914 | if (info_ptr != NULL) |
---|
915 | { |
---|
916 | if (png_ptr != NULL) |
---|
917 | { |
---|
918 | png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); |
---|
919 | |
---|
920 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
---|
921 | if (png_ptr->num_chunk_list) |
---|
922 | { |
---|
923 | png_free(png_ptr, png_ptr->chunk_list); |
---|
924 | png_ptr->num_chunk_list = 0; |
---|
925 | } |
---|
926 | #endif |
---|
927 | } |
---|
928 | |
---|
929 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
930 | png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, |
---|
931 | (png_voidp)mem_ptr); |
---|
932 | #else |
---|
933 | png_destroy_struct((png_voidp)info_ptr); |
---|
934 | #endif |
---|
935 | *info_ptr_ptr = NULL; |
---|
936 | } |
---|
937 | |
---|
938 | if (png_ptr != NULL) |
---|
939 | { |
---|
940 | png_write_destroy(png_ptr); |
---|
941 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
942 | png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, |
---|
943 | (png_voidp)mem_ptr); |
---|
944 | #else |
---|
945 | png_destroy_struct((png_voidp)png_ptr); |
---|
946 | #endif |
---|
947 | *png_ptr_ptr = NULL; |
---|
948 | } |
---|
949 | } |
---|
950 | |
---|
951 | |
---|
952 | /* Free any memory used in png_ptr struct (old method) */ |
---|
953 | void /* PRIVATE */ |
---|
954 | png_write_destroy(png_structp png_ptr) |
---|
955 | { |
---|
956 | #ifdef PNG_SETJMP_SUPPORTED |
---|
957 | jmp_buf tmp_jmp; /* Save jump buffer */ |
---|
958 | #endif |
---|
959 | png_error_ptr error_fn; |
---|
960 | #ifdef PNG_WARNINGS_SUPPORTED |
---|
961 | png_error_ptr warning_fn; |
---|
962 | #endif |
---|
963 | png_voidp error_ptr; |
---|
964 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
965 | png_free_ptr free_fn; |
---|
966 | #endif |
---|
967 | |
---|
968 | png_debug(1, "in png_write_destroy"); |
---|
969 | |
---|
970 | /* Free any memory zlib uses */ |
---|
971 | if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED) |
---|
972 | deflateEnd(&png_ptr->zstream); |
---|
973 | |
---|
974 | /* Free our memory. png_free checks NULL for us. */ |
---|
975 | png_free(png_ptr, png_ptr->zbuf); |
---|
976 | png_free(png_ptr, png_ptr->row_buf); |
---|
977 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
---|
978 | png_free(png_ptr, png_ptr->prev_row); |
---|
979 | png_free(png_ptr, png_ptr->sub_row); |
---|
980 | png_free(png_ptr, png_ptr->up_row); |
---|
981 | png_free(png_ptr, png_ptr->avg_row); |
---|
982 | png_free(png_ptr, png_ptr->paeth_row); |
---|
983 | #endif |
---|
984 | |
---|
985 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
---|
986 | /* Use this to save a little code space, it doesn't free the filter_costs */ |
---|
987 | png_reset_filter_heuristics(png_ptr); |
---|
988 | png_free(png_ptr, png_ptr->filter_costs); |
---|
989 | png_free(png_ptr, png_ptr->inv_filter_costs); |
---|
990 | #endif |
---|
991 | |
---|
992 | #ifdef PNG_SETJMP_SUPPORTED |
---|
993 | /* Reset structure */ |
---|
994 | png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf)); |
---|
995 | #endif |
---|
996 | |
---|
997 | error_fn = png_ptr->error_fn; |
---|
998 | #ifdef PNG_WARNINGS_SUPPORTED |
---|
999 | warning_fn = png_ptr->warning_fn; |
---|
1000 | #endif |
---|
1001 | error_ptr = png_ptr->error_ptr; |
---|
1002 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
1003 | free_fn = png_ptr->free_fn; |
---|
1004 | #endif |
---|
1005 | |
---|
1006 | png_memset(png_ptr, 0, png_sizeof(png_struct)); |
---|
1007 | |
---|
1008 | png_ptr->error_fn = error_fn; |
---|
1009 | #ifdef PNG_WARNINGS_SUPPORTED |
---|
1010 | png_ptr->warning_fn = warning_fn; |
---|
1011 | #endif |
---|
1012 | png_ptr->error_ptr = error_ptr; |
---|
1013 | #ifdef PNG_USER_MEM_SUPPORTED |
---|
1014 | png_ptr->free_fn = free_fn; |
---|
1015 | #endif |
---|
1016 | |
---|
1017 | #ifdef PNG_SETJMP_SUPPORTED |
---|
1018 | png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf)); |
---|
1019 | #endif |
---|
1020 | } |
---|
1021 | |
---|
1022 | /* Allow the application to select one or more row filters to use. */ |
---|
1023 | void PNGAPI |
---|
1024 | png_set_filter(png_structp png_ptr, int method, int filters) |
---|
1025 | { |
---|
1026 | png_debug(1, "in png_set_filter"); |
---|
1027 | |
---|
1028 | if (png_ptr == NULL) |
---|
1029 | return; |
---|
1030 | |
---|
1031 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
---|
1032 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
---|
1033 | (method == PNG_INTRAPIXEL_DIFFERENCING)) |
---|
1034 | method = PNG_FILTER_TYPE_BASE; |
---|
1035 | |
---|
1036 | #endif |
---|
1037 | if (method == PNG_FILTER_TYPE_BASE) |
---|
1038 | { |
---|
1039 | switch (filters & (PNG_ALL_FILTERS | 0x07)) |
---|
1040 | { |
---|
1041 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
---|
1042 | case 5: |
---|
1043 | case 6: |
---|
1044 | case 7: png_warning(png_ptr, "Unknown row filter for method 0"); |
---|
1045 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
---|
1046 | case PNG_FILTER_VALUE_NONE: |
---|
1047 | png_ptr->do_filter = PNG_FILTER_NONE; break; |
---|
1048 | |
---|
1049 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
---|
1050 | case PNG_FILTER_VALUE_SUB: |
---|
1051 | png_ptr->do_filter = PNG_FILTER_SUB; break; |
---|
1052 | |
---|
1053 | case PNG_FILTER_VALUE_UP: |
---|
1054 | png_ptr->do_filter = PNG_FILTER_UP; break; |
---|
1055 | |
---|
1056 | case PNG_FILTER_VALUE_AVG: |
---|
1057 | png_ptr->do_filter = PNG_FILTER_AVG; break; |
---|
1058 | |
---|
1059 | case PNG_FILTER_VALUE_PAETH: |
---|
1060 | png_ptr->do_filter = PNG_FILTER_PAETH; break; |
---|
1061 | |
---|
1062 | default: |
---|
1063 | png_ptr->do_filter = (png_byte)filters; break; |
---|
1064 | #else |
---|
1065 | default: |
---|
1066 | png_warning(png_ptr, "Unknown row filter for method 0"); |
---|
1067 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
---|
1068 | } |
---|
1069 | |
---|
1070 | /* If we have allocated the row_buf, this means we have already started |
---|
1071 | * with the image and we should have allocated all of the filter buffers |
---|
1072 | * that have been selected. If prev_row isn't already allocated, then |
---|
1073 | * it is too late to start using the filters that need it, since we |
---|
1074 | * will be missing the data in the previous row. If an application |
---|
1075 | * wants to start and stop using particular filters during compression, |
---|
1076 | * it should start out with all of the filters, and then add and |
---|
1077 | * remove them after the start of compression. |
---|
1078 | */ |
---|
1079 | if (png_ptr->row_buf != NULL) |
---|
1080 | { |
---|
1081 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
---|
1082 | if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL) |
---|
1083 | { |
---|
1084 | png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, |
---|
1085 | (png_ptr->rowbytes + 1)); |
---|
1086 | png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; |
---|
1087 | } |
---|
1088 | |
---|
1089 | if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL) |
---|
1090 | { |
---|
1091 | if (png_ptr->prev_row == NULL) |
---|
1092 | { |
---|
1093 | png_warning(png_ptr, "Can't add Up filter after starting"); |
---|
1094 | png_ptr->do_filter = (png_byte)(png_ptr->do_filter & |
---|
1095 | ~PNG_FILTER_UP); |
---|
1096 | } |
---|
1097 | |
---|
1098 | else |
---|
1099 | { |
---|
1100 | png_ptr->up_row = (png_bytep)png_malloc(png_ptr, |
---|
1101 | (png_ptr->rowbytes + 1)); |
---|
1102 | png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; |
---|
1103 | } |
---|
1104 | } |
---|
1105 | |
---|
1106 | if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL) |
---|
1107 | { |
---|
1108 | if (png_ptr->prev_row == NULL) |
---|
1109 | { |
---|
1110 | png_warning(png_ptr, "Can't add Average filter after starting"); |
---|
1111 | png_ptr->do_filter = (png_byte)(png_ptr->do_filter & |
---|
1112 | ~PNG_FILTER_AVG); |
---|
1113 | } |
---|
1114 | |
---|
1115 | else |
---|
1116 | { |
---|
1117 | png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, |
---|
1118 | (png_ptr->rowbytes + 1)); |
---|
1119 | png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; |
---|
1120 | } |
---|
1121 | } |
---|
1122 | |
---|
1123 | if ((png_ptr->do_filter & PNG_FILTER_PAETH) && |
---|
1124 | png_ptr->paeth_row == NULL) |
---|
1125 | { |
---|
1126 | if (png_ptr->prev_row == NULL) |
---|
1127 | { |
---|
1128 | png_warning(png_ptr, "Can't add Paeth filter after starting"); |
---|
1129 | png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH); |
---|
1130 | } |
---|
1131 | |
---|
1132 | else |
---|
1133 | { |
---|
1134 | png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, |
---|
1135 | (png_ptr->rowbytes + 1)); |
---|
1136 | png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; |
---|
1137 | } |
---|
1138 | } |
---|
1139 | |
---|
1140 | if (png_ptr->do_filter == PNG_NO_FILTERS) |
---|
1141 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
---|
1142 | png_ptr->do_filter = PNG_FILTER_NONE; |
---|
1143 | } |
---|
1144 | } |
---|
1145 | else |
---|
1146 | png_error(png_ptr, "Unknown custom filter method"); |
---|
1147 | } |
---|
1148 | |
---|
1149 | /* This allows us to influence the way in which libpng chooses the "best" |
---|
1150 | * filter for the current scanline. While the "minimum-sum-of-absolute- |
---|
1151 | * differences metric is relatively fast and effective, there is some |
---|
1152 | * question as to whether it can be improved upon by trying to keep the |
---|
1153 | * filtered data going to zlib more consistent, hopefully resulting in |
---|
1154 | * better compression. |
---|
1155 | */ |
---|
1156 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */ |
---|
1157 | /* Convenience reset API. */ |
---|
1158 | static void |
---|
1159 | png_reset_filter_heuristics(png_structp png_ptr) |
---|
1160 | { |
---|
1161 | /* Clear out any old values in the 'weights' - this must be done because if |
---|
1162 | * the app calls set_filter_heuristics multiple times with different |
---|
1163 | * 'num_weights' values we would otherwise potentially have wrong sized |
---|
1164 | * arrays. |
---|
1165 | */ |
---|
1166 | png_ptr->num_prev_filters = 0; |
---|
1167 | png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED; |
---|
1168 | if (png_ptr->prev_filters != NULL) |
---|
1169 | { |
---|
1170 | png_bytep old = png_ptr->prev_filters; |
---|
1171 | png_ptr->prev_filters = NULL; |
---|
1172 | png_free(png_ptr, old); |
---|
1173 | } |
---|
1174 | if (png_ptr->filter_weights != NULL) |
---|
1175 | { |
---|
1176 | png_uint_16p old = png_ptr->filter_weights; |
---|
1177 | png_ptr->filter_weights = NULL; |
---|
1178 | png_free(png_ptr, old); |
---|
1179 | } |
---|
1180 | |
---|
1181 | if (png_ptr->inv_filter_weights != NULL) |
---|
1182 | { |
---|
1183 | png_uint_16p old = png_ptr->inv_filter_weights; |
---|
1184 | png_ptr->inv_filter_weights = NULL; |
---|
1185 | png_free(png_ptr, old); |
---|
1186 | } |
---|
1187 | |
---|
1188 | /* Leave the filter_costs - this array is fixed size. */ |
---|
1189 | } |
---|
1190 | |
---|
1191 | static int |
---|
1192 | png_init_filter_heuristics(png_structp png_ptr, int heuristic_method, |
---|
1193 | int num_weights) |
---|
1194 | { |
---|
1195 | if (png_ptr == NULL) |
---|
1196 | return 0; |
---|
1197 | |
---|
1198 | /* Clear out the arrays */ |
---|
1199 | png_reset_filter_heuristics(png_ptr); |
---|
1200 | |
---|
1201 | /* Check arguments; the 'reset' function makes the correct settings for the |
---|
1202 | * unweighted case, but we must handle the weight case by initializing the |
---|
1203 | * arrays for the caller. |
---|
1204 | */ |
---|
1205 | if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
---|
1206 | { |
---|
1207 | int i; |
---|
1208 | |
---|
1209 | if (num_weights > 0) |
---|
1210 | { |
---|
1211 | png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr, |
---|
1212 | (png_uint_32)(png_sizeof(png_byte) * num_weights)); |
---|
1213 | |
---|
1214 | /* To make sure that the weighting starts out fairly */ |
---|
1215 | for (i = 0; i < num_weights; i++) |
---|
1216 | { |
---|
1217 | png_ptr->prev_filters[i] = 255; |
---|
1218 | } |
---|
1219 | |
---|
1220 | png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr, |
---|
1221 | (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); |
---|
1222 | |
---|
1223 | png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr, |
---|
1224 | (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); |
---|
1225 | |
---|
1226 | for (i = 0; i < num_weights; i++) |
---|
1227 | { |
---|
1228 | png_ptr->inv_filter_weights[i] = |
---|
1229 | png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; |
---|
1230 | } |
---|
1231 | |
---|
1232 | /* Safe to set this now */ |
---|
1233 | png_ptr->num_prev_filters = (png_byte)num_weights; |
---|
1234 | } |
---|
1235 | |
---|
1236 | /* If, in the future, there are other filter methods, this would |
---|
1237 | * need to be based on png_ptr->filter. |
---|
1238 | */ |
---|
1239 | if (png_ptr->filter_costs == NULL) |
---|
1240 | { |
---|
1241 | png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr, |
---|
1242 | (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); |
---|
1243 | |
---|
1244 | png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr, |
---|
1245 | (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); |
---|
1246 | } |
---|
1247 | |
---|
1248 | for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) |
---|
1249 | { |
---|
1250 | png_ptr->inv_filter_costs[i] = |
---|
1251 | png_ptr->filter_costs[i] = PNG_COST_FACTOR; |
---|
1252 | } |
---|
1253 | |
---|
1254 | /* All the arrays are inited, safe to set this: */ |
---|
1255 | png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED; |
---|
1256 | |
---|
1257 | /* Return the 'ok' code. */ |
---|
1258 | return 1; |
---|
1259 | } |
---|
1260 | else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT || |
---|
1261 | heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED) |
---|
1262 | { |
---|
1263 | return 1; |
---|
1264 | } |
---|
1265 | else |
---|
1266 | { |
---|
1267 | png_warning(png_ptr, "Unknown filter heuristic method"); |
---|
1268 | return 0; |
---|
1269 | } |
---|
1270 | } |
---|
1271 | |
---|
1272 | /* Provide floating and fixed point APIs */ |
---|
1273 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
---|
1274 | void PNGAPI |
---|
1275 | png_set_filter_heuristics(png_structp png_ptr, int heuristic_method, |
---|
1276 | int num_weights, png_const_doublep filter_weights, |
---|
1277 | png_const_doublep filter_costs) |
---|
1278 | { |
---|
1279 | png_debug(1, "in png_set_filter_heuristics"); |
---|
1280 | |
---|
1281 | /* The internal API allocates all the arrays and ensures that the elements of |
---|
1282 | * those arrays are set to the default value. |
---|
1283 | */ |
---|
1284 | if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights)) |
---|
1285 | return; |
---|
1286 | |
---|
1287 | /* If using the weighted method copy in the weights. */ |
---|
1288 | if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
---|
1289 | { |
---|
1290 | int i; |
---|
1291 | for (i = 0; i < num_weights; i++) |
---|
1292 | { |
---|
1293 | if (filter_weights[i] <= 0.0) |
---|
1294 | { |
---|
1295 | png_ptr->inv_filter_weights[i] = |
---|
1296 | png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; |
---|
1297 | } |
---|
1298 | |
---|
1299 | else |
---|
1300 | { |
---|
1301 | png_ptr->inv_filter_weights[i] = |
---|
1302 | (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5); |
---|
1303 | |
---|
1304 | png_ptr->filter_weights[i] = |
---|
1305 | (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5); |
---|
1306 | } |
---|
1307 | } |
---|
1308 | |
---|
1309 | /* Here is where we set the relative costs of the different filters. We |
---|
1310 | * should take the desired compression level into account when setting |
---|
1311 | * the costs, so that Paeth, for instance, has a high relative cost at low |
---|
1312 | * compression levels, while it has a lower relative cost at higher |
---|
1313 | * compression settings. The filter types are in order of increasing |
---|
1314 | * relative cost, so it would be possible to do this with an algorithm. |
---|
1315 | */ |
---|
1316 | for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0) |
---|
1317 | { |
---|
1318 | png_ptr->inv_filter_costs[i] = |
---|
1319 | (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5); |
---|
1320 | |
---|
1321 | png_ptr->filter_costs[i] = |
---|
1322 | (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5); |
---|
1323 | } |
---|
1324 | } |
---|
1325 | } |
---|
1326 | #endif /* FLOATING_POINT */ |
---|
1327 | |
---|
1328 | #ifdef PNG_FIXED_POINT_SUPPORTED |
---|
1329 | void PNGAPI |
---|
1330 | png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method, |
---|
1331 | int num_weights, png_const_fixed_point_p filter_weights, |
---|
1332 | png_const_fixed_point_p filter_costs) |
---|
1333 | { |
---|
1334 | png_debug(1, "in png_set_filter_heuristics_fixed"); |
---|
1335 | |
---|
1336 | /* The internal API allocates all the arrays and ensures that the elements of |
---|
1337 | * those arrays are set to the default value. |
---|
1338 | */ |
---|
1339 | if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights)) |
---|
1340 | return; |
---|
1341 | |
---|
1342 | /* If using the weighted method copy in the weights. */ |
---|
1343 | if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
---|
1344 | { |
---|
1345 | int i; |
---|
1346 | for (i = 0; i < num_weights; i++) |
---|
1347 | { |
---|
1348 | if (filter_weights[i] <= 0) |
---|
1349 | { |
---|
1350 | png_ptr->inv_filter_weights[i] = |
---|
1351 | png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; |
---|
1352 | } |
---|
1353 | |
---|
1354 | else |
---|
1355 | { |
---|
1356 | png_ptr->inv_filter_weights[i] = (png_uint_16) |
---|
1357 | ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1); |
---|
1358 | |
---|
1359 | png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR* |
---|
1360 | PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]); |
---|
1361 | } |
---|
1362 | } |
---|
1363 | |
---|
1364 | /* Here is where we set the relative costs of the different filters. We |
---|
1365 | * should take the desired compression level into account when setting |
---|
1366 | * the costs, so that Paeth, for instance, has a high relative cost at low |
---|
1367 | * compression levels, while it has a lower relative cost at higher |
---|
1368 | * compression settings. The filter types are in order of increasing |
---|
1369 | * relative cost, so it would be possible to do this with an algorithm. |
---|
1370 | */ |
---|
1371 | for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) |
---|
1372 | if (filter_costs[i] >= PNG_FP_1) |
---|
1373 | { |
---|
1374 | png_uint_32 tmp; |
---|
1375 | |
---|
1376 | /* Use a 32 bit unsigned temporary here because otherwise the |
---|
1377 | * intermediate value will be a 32 bit *signed* integer (ANSI rules) |
---|
1378 | * and this will get the wrong answer on division. |
---|
1379 | */ |
---|
1380 | tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2); |
---|
1381 | tmp /= filter_costs[i]; |
---|
1382 | |
---|
1383 | png_ptr->inv_filter_costs[i] = (png_uint_16)tmp; |
---|
1384 | |
---|
1385 | tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF; |
---|
1386 | tmp /= PNG_FP_1; |
---|
1387 | |
---|
1388 | png_ptr->filter_costs[i] = (png_uint_16)tmp; |
---|
1389 | } |
---|
1390 | } |
---|
1391 | } |
---|
1392 | #endif /* FIXED_POINT */ |
---|
1393 | #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ |
---|
1394 | |
---|
1395 | void PNGAPI |
---|
1396 | png_set_compression_level(png_structp png_ptr, int level) |
---|
1397 | { |
---|
1398 | png_debug(1, "in png_set_compression_level"); |
---|
1399 | |
---|
1400 | if (png_ptr == NULL) |
---|
1401 | return; |
---|
1402 | |
---|
1403 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL; |
---|
1404 | png_ptr->zlib_level = level; |
---|
1405 | } |
---|
1406 | |
---|
1407 | void PNGAPI |
---|
1408 | png_set_compression_mem_level(png_structp png_ptr, int mem_level) |
---|
1409 | { |
---|
1410 | png_debug(1, "in png_set_compression_mem_level"); |
---|
1411 | |
---|
1412 | if (png_ptr == NULL) |
---|
1413 | return; |
---|
1414 | |
---|
1415 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL; |
---|
1416 | png_ptr->zlib_mem_level = mem_level; |
---|
1417 | } |
---|
1418 | |
---|
1419 | void PNGAPI |
---|
1420 | png_set_compression_strategy(png_structp png_ptr, int strategy) |
---|
1421 | { |
---|
1422 | png_debug(1, "in png_set_compression_strategy"); |
---|
1423 | |
---|
1424 | if (png_ptr == NULL) |
---|
1425 | return; |
---|
1426 | |
---|
1427 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; |
---|
1428 | png_ptr->zlib_strategy = strategy; |
---|
1429 | } |
---|
1430 | |
---|
1431 | /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a |
---|
1432 | * smaller value of window_bits if it can do so safely. |
---|
1433 | */ |
---|
1434 | void PNGAPI |
---|
1435 | png_set_compression_window_bits(png_structp png_ptr, int window_bits) |
---|
1436 | { |
---|
1437 | if (png_ptr == NULL) |
---|
1438 | return; |
---|
1439 | |
---|
1440 | if (window_bits > 15) |
---|
1441 | png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); |
---|
1442 | |
---|
1443 | else if (window_bits < 8) |
---|
1444 | png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); |
---|
1445 | |
---|
1446 | #ifndef WBITS_8_OK |
---|
1447 | /* Avoid libpng bug with 256-byte windows */ |
---|
1448 | if (window_bits == 8) |
---|
1449 | { |
---|
1450 | png_warning(png_ptr, "Compression window is being reset to 512"); |
---|
1451 | window_bits = 9; |
---|
1452 | } |
---|
1453 | |
---|
1454 | #endif |
---|
1455 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS; |
---|
1456 | png_ptr->zlib_window_bits = window_bits; |
---|
1457 | } |
---|
1458 | |
---|
1459 | void PNGAPI |
---|
1460 | png_set_compression_method(png_structp png_ptr, int method) |
---|
1461 | { |
---|
1462 | png_debug(1, "in png_set_compression_method"); |
---|
1463 | |
---|
1464 | if (png_ptr == NULL) |
---|
1465 | return; |
---|
1466 | |
---|
1467 | if (method != 8) |
---|
1468 | png_warning(png_ptr, "Only compression method 8 is supported by PNG"); |
---|
1469 | |
---|
1470 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD; |
---|
1471 | png_ptr->zlib_method = method; |
---|
1472 | } |
---|
1473 | |
---|
1474 | /* The following were added to libpng-1.5.4 */ |
---|
1475 | #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED |
---|
1476 | void PNGAPI |
---|
1477 | png_set_text_compression_level(png_structp png_ptr, int level) |
---|
1478 | { |
---|
1479 | png_debug(1, "in png_set_text_compression_level"); |
---|
1480 | |
---|
1481 | if (png_ptr == NULL) |
---|
1482 | return; |
---|
1483 | |
---|
1484 | png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_LEVEL; |
---|
1485 | png_ptr->zlib_text_level = level; |
---|
1486 | } |
---|
1487 | |
---|
1488 | void PNGAPI |
---|
1489 | png_set_text_compression_mem_level(png_structp png_ptr, int mem_level) |
---|
1490 | { |
---|
1491 | png_debug(1, "in png_set_text_compression_mem_level"); |
---|
1492 | |
---|
1493 | if (png_ptr == NULL) |
---|
1494 | return; |
---|
1495 | |
---|
1496 | png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL; |
---|
1497 | png_ptr->zlib_text_mem_level = mem_level; |
---|
1498 | } |
---|
1499 | |
---|
1500 | void PNGAPI |
---|
1501 | png_set_text_compression_strategy(png_structp png_ptr, int strategy) |
---|
1502 | { |
---|
1503 | png_debug(1, "in png_set_text_compression_strategy"); |
---|
1504 | |
---|
1505 | if (png_ptr == NULL) |
---|
1506 | return; |
---|
1507 | |
---|
1508 | png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_STRATEGY; |
---|
1509 | png_ptr->zlib_text_strategy = strategy; |
---|
1510 | } |
---|
1511 | |
---|
1512 | /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a |
---|
1513 | * smaller value of window_bits if it can do so safely. |
---|
1514 | */ |
---|
1515 | void PNGAPI |
---|
1516 | png_set_text_compression_window_bits(png_structp png_ptr, int window_bits) |
---|
1517 | { |
---|
1518 | if (png_ptr == NULL) |
---|
1519 | return; |
---|
1520 | |
---|
1521 | if (window_bits > 15) |
---|
1522 | png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); |
---|
1523 | |
---|
1524 | else if (window_bits < 8) |
---|
1525 | png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); |
---|
1526 | |
---|
1527 | #ifndef WBITS_8_OK |
---|
1528 | /* Avoid libpng bug with 256-byte windows */ |
---|
1529 | if (window_bits == 8) |
---|
1530 | { |
---|
1531 | png_warning(png_ptr, "Text compression window is being reset to 512"); |
---|
1532 | window_bits = 9; |
---|
1533 | } |
---|
1534 | |
---|
1535 | #endif |
---|
1536 | png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS; |
---|
1537 | png_ptr->zlib_text_window_bits = window_bits; |
---|
1538 | } |
---|
1539 | |
---|
1540 | void PNGAPI |
---|
1541 | png_set_text_compression_method(png_structp png_ptr, int method) |
---|
1542 | { |
---|
1543 | png_debug(1, "in png_set_text_compression_method"); |
---|
1544 | |
---|
1545 | if (png_ptr == NULL) |
---|
1546 | return; |
---|
1547 | |
---|
1548 | if (method != 8) |
---|
1549 | png_warning(png_ptr, "Only compression method 8 is supported by PNG"); |
---|
1550 | |
---|
1551 | png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_METHOD; |
---|
1552 | png_ptr->zlib_text_method = method; |
---|
1553 | } |
---|
1554 | #endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */ |
---|
1555 | /* end of API added to libpng-1.5.4 */ |
---|
1556 | |
---|
1557 | void PNGAPI |
---|
1558 | png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn) |
---|
1559 | { |
---|
1560 | if (png_ptr == NULL) |
---|
1561 | return; |
---|
1562 | |
---|
1563 | png_ptr->write_row_fn = write_row_fn; |
---|
1564 | } |
---|
1565 | |
---|
1566 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
---|
1567 | void PNGAPI |
---|
1568 | png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr |
---|
1569 | write_user_transform_fn) |
---|
1570 | { |
---|
1571 | png_debug(1, "in png_set_write_user_transform_fn"); |
---|
1572 | |
---|
1573 | if (png_ptr == NULL) |
---|
1574 | return; |
---|
1575 | |
---|
1576 | png_ptr->transformations |= PNG_USER_TRANSFORM; |
---|
1577 | png_ptr->write_user_transform_fn = write_user_transform_fn; |
---|
1578 | } |
---|
1579 | #endif |
---|
1580 | |
---|
1581 | |
---|
1582 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
---|
1583 | void PNGAPI |
---|
1584 | png_write_png(png_structp png_ptr, png_infop info_ptr, |
---|
1585 | int transforms, voidp params) |
---|
1586 | { |
---|
1587 | if (png_ptr == NULL || info_ptr == NULL) |
---|
1588 | return; |
---|
1589 | |
---|
1590 | /* Write the file header information. */ |
---|
1591 | png_write_info(png_ptr, info_ptr); |
---|
1592 | |
---|
1593 | /* ------ these transformations don't touch the info structure ------- */ |
---|
1594 | |
---|
1595 | #ifdef PNG_WRITE_INVERT_SUPPORTED |
---|
1596 | /* Invert monochrome pixels */ |
---|
1597 | if (transforms & PNG_TRANSFORM_INVERT_MONO) |
---|
1598 | png_set_invert_mono(png_ptr); |
---|
1599 | #endif |
---|
1600 | |
---|
1601 | #ifdef PNG_WRITE_SHIFT_SUPPORTED |
---|
1602 | /* Shift the pixels up to a legal bit depth and fill in |
---|
1603 | * as appropriate to correctly scale the image. |
---|
1604 | */ |
---|
1605 | if ((transforms & PNG_TRANSFORM_SHIFT) |
---|
1606 | && (info_ptr->valid & PNG_INFO_sBIT)) |
---|
1607 | png_set_shift(png_ptr, &info_ptr->sig_bit); |
---|
1608 | #endif |
---|
1609 | |
---|
1610 | #ifdef PNG_WRITE_PACK_SUPPORTED |
---|
1611 | /* Pack pixels into bytes */ |
---|
1612 | if (transforms & PNG_TRANSFORM_PACKING) |
---|
1613 | png_set_packing(png_ptr); |
---|
1614 | #endif |
---|
1615 | |
---|
1616 | #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
---|
1617 | /* Swap location of alpha bytes from ARGB to RGBA */ |
---|
1618 | if (transforms & PNG_TRANSFORM_SWAP_ALPHA) |
---|
1619 | png_set_swap_alpha(png_ptr); |
---|
1620 | #endif |
---|
1621 | |
---|
1622 | #ifdef PNG_WRITE_FILLER_SUPPORTED |
---|
1623 | /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */ |
---|
1624 | if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) |
---|
1625 | png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); |
---|
1626 | |
---|
1627 | else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) |
---|
1628 | png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); |
---|
1629 | #endif |
---|
1630 | |
---|
1631 | #ifdef PNG_WRITE_BGR_SUPPORTED |
---|
1632 | /* Flip BGR pixels to RGB */ |
---|
1633 | if (transforms & PNG_TRANSFORM_BGR) |
---|
1634 | png_set_bgr(png_ptr); |
---|
1635 | #endif |
---|
1636 | |
---|
1637 | #ifdef PNG_WRITE_SWAP_SUPPORTED |
---|
1638 | /* Swap bytes of 16-bit files to most significant byte first */ |
---|
1639 | if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) |
---|
1640 | png_set_swap(png_ptr); |
---|
1641 | #endif |
---|
1642 | |
---|
1643 | #ifdef PNG_WRITE_PACKSWAP_SUPPORTED |
---|
1644 | /* Swap bits of 1, 2, 4 bit packed pixel formats */ |
---|
1645 | if (transforms & PNG_TRANSFORM_PACKSWAP) |
---|
1646 | png_set_packswap(png_ptr); |
---|
1647 | #endif |
---|
1648 | |
---|
1649 | #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
---|
1650 | /* Invert the alpha channel from opacity to transparency */ |
---|
1651 | if (transforms & PNG_TRANSFORM_INVERT_ALPHA) |
---|
1652 | png_set_invert_alpha(png_ptr); |
---|
1653 | #endif |
---|
1654 | |
---|
1655 | /* ----------------------- end of transformations ------------------- */ |
---|
1656 | |
---|
1657 | /* Write the bits */ |
---|
1658 | if (info_ptr->valid & PNG_INFO_IDAT) |
---|
1659 | png_write_image(png_ptr, info_ptr->row_pointers); |
---|
1660 | |
---|
1661 | /* It is REQUIRED to call this to finish writing the rest of the file */ |
---|
1662 | png_write_end(png_ptr, info_ptr); |
---|
1663 | |
---|
1664 | PNG_UNUSED(transforms) /* Quiet compiler warnings */ |
---|
1665 | PNG_UNUSED(params) |
---|
1666 | } |
---|
1667 | #endif |
---|
1668 | #endif /* PNG_WRITE_SUPPORTED */ |
---|