Debian Codegraph @

PNG/libpng/pngwrite.c

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