core: add support for the autoverbose feature
[fbsplash.git] / core / libs / libpng-1.2.18 / pngwrite.c
blobdf7ade3349abddde00989e28a019ae3f1acce67a
2 /* pngwrite.c - general routines to write a PNG file
4 * Last changed in libpng 1.2.15 January 5, 2007
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2007 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 */
11 /* get internal access to png.h */
12 #define PNG_INTERNAL
13 #include "png.h"
14 #ifdef PNG_WRITE_SUPPORTED
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.
25 void PNGAPI
26 png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
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))
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))
37 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
38 png_ptr->mng_features_permitted=0;
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)
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
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)
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
96 #endif
97 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
98 if (info_ptr->unknown_chunks_num)
100 png_unknown_chunk *up;
102 png_debug(5, "writing extra chunks\n");
104 for (up = info_ptr->unknown_chunks;
105 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
106 up++)
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)))
115 png_write_chunk(png_ptr, up->name, up->data, up->size);
119 #endif
120 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
124 void PNGAPI
125 png_write_info(png_structp png_ptr, png_infop info_ptr)
127 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
128 int i;
129 #endif
131 png_debug(1, "in png_write_info\n");
133 if (png_ptr == NULL || info_ptr == NULL)
134 return;
136 png_write_info_before_PLTE(png_ptr, info_ptr);
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");
144 #if defined(PNG_WRITE_tRNS_SUPPORTED)
145 if (info_ptr->valid & PNG_INFO_tRNS)
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)
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]);
156 #endif
157 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
158 info_ptr->num_trans, info_ptr->color_type);
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)
203 png_write_tIME(png_ptr, &(info_ptr->mod_time));
204 png_ptr->mode |= PNG_WROTE_tIME;
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++)
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)
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;
235 /* If we want a compressed text chunk */
236 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
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;
249 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
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,
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;
263 #endif
264 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
265 if (info_ptr->unknown_chunks_num)
267 png_unknown_chunk *up;
269 png_debug(5, "writing extra chunks\n");
271 for (up = info_ptr->unknown_chunks;
272 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
273 up++)
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)))
282 png_write_chunk(png_ptr, up->name, up->data, up->size);
286 #endif
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.
294 void PNGAPI
295 png_write_end(png_structp png_ptr, png_infop info_ptr)
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");
303 /* see if user wants us to write information chunks */
304 if (info_ptr != NULL)
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++)
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)
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;
338 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
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;
351 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
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
361 /* Mark this chunk as written */
362 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
365 #endif
366 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
367 if (info_ptr->unknown_chunks_num)
369 png_unknown_chunk *up;
371 png_debug(5, "writing extra chunks\n");
373 for (up = info_ptr->unknown_chunks;
374 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
375 up++)
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)))
383 png_write_chunk(png_ptr, up->name, up->data, up->size);
387 #endif
390 png_ptr->mode |= PNG_AFTER_IDAT;
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
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)
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;
416 void PNGAPI
417 png_convert_from_time_t(png_timep ptime, time_t ttime)
419 struct tm *tbuf;
421 png_debug(1, "in png_convert_from_time_t\n");
422 tbuf = gmtime(&ttime);
423 png_convert_from_struct_tm(ptime, tbuf);
425 #endif
426 #endif
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)
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));
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)
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);
462 #if !defined(PNG_1_0_X)
463 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
464 #ifdef PNG_MMX_CODE_SUPPORTED
465 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
466 #endif
467 #endif
468 #endif /* PNG_1_0_X */
470 /* added at libpng-1.2.6 */
471 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
472 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
473 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
474 #endif
476 #ifdef PNG_SETJMP_SUPPORTED
477 #ifdef USE_FAR_KEYWORD
478 if (setjmp(jmpbuf))
479 #else
480 if (setjmp(png_ptr->jmpbuf))
481 #endif
483 png_free(png_ptr, png_ptr->zbuf);
484 png_ptr->zbuf=NULL;
485 png_destroy_struct(png_ptr);
486 return (NULL);
488 #ifdef USE_FAR_KEYWORD
489 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
490 #endif
491 #endif
493 #ifdef PNG_USER_MEM_SUPPORTED
494 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
495 #endif /* PNG_USER_MEM_SUPPORTED */
496 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
498 i=0;
501 if(user_png_ver[i] != png_libpng_ver[i])
502 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
503 } while (png_libpng_ver[i++]);
505 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
507 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
508 * we must recompile any applications that use any older library version.
509 * For versions after libpng 1.0, we will be compatible, so we need
510 * only check the first digit.
512 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
513 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
514 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
516 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
517 char msg[80];
518 if (user_png_ver)
520 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
521 user_png_ver);
522 png_warning(png_ptr, msg);
524 sprintf(msg, "Application is running with png.c from libpng-%.20s",
525 png_libpng_ver);
526 png_warning(png_ptr, msg);
527 #endif
528 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
529 png_ptr->flags=0;
530 #endif
531 png_error(png_ptr,
532 "Incompatible libpng version in application and library");
536 /* initialize zbuf - compression buffer */
537 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
538 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
539 (png_uint_32)png_ptr->zbuf_size);
541 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
542 png_flush_ptr_NULL);
544 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
545 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
546 1, png_doublep_NULL, png_doublep_NULL);
547 #endif
549 #ifdef PNG_SETJMP_SUPPORTED
550 /* Applications that neglect to set up their own setjmp() and then encounter
551 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
552 abort instead of returning. */
553 #ifdef USE_FAR_KEYWORD
554 if (setjmp(jmpbuf))
555 PNG_ABORT();
556 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
557 #else
558 if (setjmp(png_ptr->jmpbuf))
559 PNG_ABORT();
560 #endif
561 #endif
562 return (png_ptr);
565 /* Initialize png_ptr structure, and allocate any memory needed */
566 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
567 /* Deprecated. */
568 #undef png_write_init
569 void PNGAPI
570 png_write_init(png_structp png_ptr)
572 /* We only come here via pre-1.0.7-compiled applications */
573 png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
576 void PNGAPI
577 png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
578 png_size_t png_struct_size, png_size_t png_info_size)
580 /* We only come here via pre-1.0.12-compiled applications */
581 if(png_ptr == NULL) return;
582 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
583 if(png_sizeof(png_struct) > png_struct_size ||
584 png_sizeof(png_info) > png_info_size)
586 char msg[80];
587 png_ptr->warning_fn=NULL;
588 if (user_png_ver)
590 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
591 user_png_ver);
592 png_warning(png_ptr, msg);
594 sprintf(msg, "Application is running with png.c from libpng-%.20s",
595 png_libpng_ver);
596 png_warning(png_ptr, msg);
598 #endif
599 if(png_sizeof(png_struct) > png_struct_size)
601 png_ptr->error_fn=NULL;
602 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
603 png_ptr->flags=0;
604 #endif
605 png_error(png_ptr,
606 "The png struct allocated by the application for writing is too small.");
608 if(png_sizeof(png_info) > png_info_size)
610 png_ptr->error_fn=NULL;
611 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
612 png_ptr->flags=0;
613 #endif
614 png_error(png_ptr,
615 "The info struct allocated by the application for writing is too small.");
617 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
619 #endif /* PNG_1_0_X || PNG_1_2_X */
622 void PNGAPI
623 png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
624 png_size_t png_struct_size)
626 png_structp png_ptr=*ptr_ptr;
627 #ifdef PNG_SETJMP_SUPPORTED
628 jmp_buf tmp_jmp; /* to save current jump buffer */
629 #endif
631 int i = 0;
633 if (png_ptr == NULL)
634 return;
638 if (user_png_ver[i] != png_libpng_ver[i])
640 #ifdef PNG_LEGACY_SUPPORTED
641 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
642 #else
643 png_ptr->warning_fn=NULL;
644 png_warning(png_ptr,
645 "Application uses deprecated png_write_init() and should be recompiled.");
646 break;
647 #endif
649 } while (png_libpng_ver[i++]);
651 png_debug(1, "in png_write_init_3\n");
653 #ifdef PNG_SETJMP_SUPPORTED
654 /* save jump buffer and error functions */
655 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
656 #endif
658 if (png_sizeof(png_struct) > png_struct_size)
660 png_destroy_struct(png_ptr);
661 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
662 *ptr_ptr = png_ptr;
665 /* reset all variables to 0 */
666 png_memset(png_ptr, 0, png_sizeof (png_struct));
668 /* added at libpng-1.2.6 */
669 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
670 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
671 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
672 #endif
674 #if !defined(PNG_1_0_X)
675 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
676 #ifdef PNG_MMX_CODE_SUPPORTED
677 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
678 #endif
679 #endif
680 #endif /* PNG_1_0_X */
682 #ifdef PNG_SETJMP_SUPPORTED
683 /* restore jump buffer */
684 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
685 #endif
687 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
688 png_flush_ptr_NULL);
690 /* initialize zbuf - compression buffer */
691 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
692 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
693 (png_uint_32)png_ptr->zbuf_size);
695 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
696 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
697 1, png_doublep_NULL, png_doublep_NULL);
698 #endif
701 /* Write a few rows of image data. If the image is interlaced,
702 * either you will have to write the 7 sub images, or, if you
703 * have called png_set_interlace_handling(), you will have to
704 * "write" the image seven times.
706 void PNGAPI
707 png_write_rows(png_structp png_ptr, png_bytepp row,
708 png_uint_32 num_rows)
710 png_uint_32 i; /* row counter */
711 png_bytepp rp; /* row pointer */
713 png_debug(1, "in png_write_rows\n");
715 if (png_ptr == NULL)
716 return;
718 /* loop through the rows */
719 for (i = 0, rp = row; i < num_rows; i++, rp++)
721 png_write_row(png_ptr, *rp);
725 /* Write the image. You only need to call this function once, even
726 * if you are writing an interlaced image.
728 void PNGAPI
729 png_write_image(png_structp png_ptr, png_bytepp image)
731 png_uint_32 i; /* row index */
732 int pass, num_pass; /* pass variables */
733 png_bytepp rp; /* points to current row */
735 if (png_ptr == NULL)
736 return;
738 png_debug(1, "in png_write_image\n");
739 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
740 /* intialize interlace handling. If image is not interlaced,
741 this will set pass to 1 */
742 num_pass = png_set_interlace_handling(png_ptr);
743 #else
744 num_pass = 1;
745 #endif
746 /* loop through passes */
747 for (pass = 0; pass < num_pass; pass++)
749 /* loop through image */
750 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
752 png_write_row(png_ptr, *rp);
757 /* called by user to write a row of image data */
758 void PNGAPI
759 png_write_row(png_structp png_ptr, png_bytep row)
761 if (png_ptr == NULL)
762 return;
763 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
764 png_ptr->row_number, png_ptr->pass);
766 /* initialize transformations and other stuff if first time */
767 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
769 /* make sure we wrote the header info */
770 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
771 png_error(png_ptr,
772 "png_write_info was never called before png_write_row.");
774 /* check for transforms that have been set but were defined out */
775 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
776 if (png_ptr->transformations & PNG_INVERT_MONO)
777 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
778 #endif
779 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
780 if (png_ptr->transformations & PNG_FILLER)
781 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
782 #endif
783 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
784 if (png_ptr->transformations & PNG_PACKSWAP)
785 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
786 #endif
787 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
788 if (png_ptr->transformations & PNG_PACK)
789 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
790 #endif
791 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
792 if (png_ptr->transformations & PNG_SHIFT)
793 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
794 #endif
795 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
796 if (png_ptr->transformations & PNG_BGR)
797 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
798 #endif
799 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
800 if (png_ptr->transformations & PNG_SWAP_BYTES)
801 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
802 #endif
804 png_write_start_row(png_ptr);
807 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
808 /* if interlaced and not interested in row, return */
809 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
811 switch (png_ptr->pass)
813 case 0:
814 if (png_ptr->row_number & 0x07)
816 png_write_finish_row(png_ptr);
817 return;
819 break;
820 case 1:
821 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
823 png_write_finish_row(png_ptr);
824 return;
826 break;
827 case 2:
828 if ((png_ptr->row_number & 0x07) != 4)
830 png_write_finish_row(png_ptr);
831 return;
833 break;
834 case 3:
835 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
837 png_write_finish_row(png_ptr);
838 return;
840 break;
841 case 4:
842 if ((png_ptr->row_number & 0x03) != 2)
844 png_write_finish_row(png_ptr);
845 return;
847 break;
848 case 5:
849 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
851 png_write_finish_row(png_ptr);
852 return;
854 break;
855 case 6:
856 if (!(png_ptr->row_number & 0x01))
858 png_write_finish_row(png_ptr);
859 return;
861 break;
864 #endif
866 /* set up row info for transformations */
867 png_ptr->row_info.color_type = png_ptr->color_type;
868 png_ptr->row_info.width = png_ptr->usr_width;
869 png_ptr->row_info.channels = png_ptr->usr_channels;
870 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
871 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
872 png_ptr->row_info.channels);
874 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
875 png_ptr->row_info.width);
877 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
878 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
879 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
880 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
881 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
882 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
884 /* Copy user's row into buffer, leaving room for filter byte. */
885 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
886 png_ptr->row_info.rowbytes);
888 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
889 /* handle interlacing */
890 if (png_ptr->interlaced && png_ptr->pass < 6 &&
891 (png_ptr->transformations & PNG_INTERLACE))
893 png_do_write_interlace(&(png_ptr->row_info),
894 png_ptr->row_buf + 1, png_ptr->pass);
895 /* this should always get caught above, but still ... */
896 if (!(png_ptr->row_info.width))
898 png_write_finish_row(png_ptr);
899 return;
902 #endif
904 /* handle other transformations */
905 if (png_ptr->transformations)
906 png_do_write_transformations(png_ptr);
908 #if defined(PNG_MNG_FEATURES_SUPPORTED)
909 /* Write filter_method 64 (intrapixel differencing) only if
910 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
911 * 2. Libpng did not write a PNG signature (this filter_method is only
912 * used in PNG datastreams that are embedded in MNG datastreams) and
913 * 3. The application called png_permit_mng_features with a mask that
914 * included PNG_FLAG_MNG_FILTER_64 and
915 * 4. The filter_method is 64 and
916 * 5. The color_type is RGB or RGBA
918 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
919 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
921 /* Intrapixel differencing */
922 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
924 #endif
926 /* Find a filter if necessary, filter the row and write it out. */
927 png_write_find_filter(png_ptr, &(png_ptr->row_info));
929 if (png_ptr->write_row_fn != NULL)
930 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
933 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
934 /* Set the automatic flush interval or 0 to turn flushing off */
935 void PNGAPI
936 png_set_flush(png_structp png_ptr, int nrows)
938 png_debug(1, "in png_set_flush\n");
939 if (png_ptr == NULL)
940 return;
941 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
944 /* flush the current output buffers now */
945 void PNGAPI
946 png_write_flush(png_structp png_ptr)
948 int wrote_IDAT;
950 png_debug(1, "in png_write_flush\n");
951 if (png_ptr == NULL)
952 return;
953 /* We have already written out all of the data */
954 if (png_ptr->row_number >= png_ptr->num_rows)
955 return;
959 int ret;
961 /* compress the data */
962 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
963 wrote_IDAT = 0;
965 /* check for compression errors */
966 if (ret != Z_OK)
968 if (png_ptr->zstream.msg != NULL)
969 png_error(png_ptr, png_ptr->zstream.msg);
970 else
971 png_error(png_ptr, "zlib error");
974 if (!(png_ptr->zstream.avail_out))
976 /* write the IDAT and reset the zlib output buffer */
977 png_write_IDAT(png_ptr, png_ptr->zbuf,
978 png_ptr->zbuf_size);
979 png_ptr->zstream.next_out = png_ptr->zbuf;
980 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
981 wrote_IDAT = 1;
983 } while(wrote_IDAT == 1);
985 /* If there is any data left to be output, write it into a new IDAT */
986 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
988 /* write the IDAT and reset the zlib output buffer */
989 png_write_IDAT(png_ptr, png_ptr->zbuf,
990 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
991 png_ptr->zstream.next_out = png_ptr->zbuf;
992 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
994 png_ptr->flush_rows = 0;
995 png_flush(png_ptr);
997 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
999 /* free all memory used by the write */
1000 void PNGAPI
1001 png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
1003 png_structp png_ptr = NULL;
1004 png_infop info_ptr = NULL;
1005 #ifdef PNG_USER_MEM_SUPPORTED
1006 png_free_ptr free_fn = NULL;
1007 png_voidp mem_ptr = NULL;
1008 #endif
1010 png_debug(1, "in png_destroy_write_struct\n");
1011 if (png_ptr_ptr != NULL)
1013 png_ptr = *png_ptr_ptr;
1014 #ifdef PNG_USER_MEM_SUPPORTED
1015 free_fn = png_ptr->free_fn;
1016 mem_ptr = png_ptr->mem_ptr;
1017 #endif
1020 if (info_ptr_ptr != NULL)
1021 info_ptr = *info_ptr_ptr;
1023 if (info_ptr != NULL)
1025 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
1027 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1028 if (png_ptr->num_chunk_list)
1030 png_free(png_ptr, png_ptr->chunk_list);
1031 png_ptr->chunk_list=NULL;
1032 png_ptr->num_chunk_list=0;
1034 #endif
1036 #ifdef PNG_USER_MEM_SUPPORTED
1037 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1038 (png_voidp)mem_ptr);
1039 #else
1040 png_destroy_struct((png_voidp)info_ptr);
1041 #endif
1042 *info_ptr_ptr = NULL;
1045 if (png_ptr != NULL)
1047 png_write_destroy(png_ptr);
1048 #ifdef PNG_USER_MEM_SUPPORTED
1049 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1050 (png_voidp)mem_ptr);
1051 #else
1052 png_destroy_struct((png_voidp)png_ptr);
1053 #endif
1054 *png_ptr_ptr = NULL;
1059 /* Free any memory used in png_ptr struct (old method) */
1060 void /* PRIVATE */
1061 png_write_destroy(png_structp png_ptr)
1063 #ifdef PNG_SETJMP_SUPPORTED
1064 jmp_buf tmp_jmp; /* save jump buffer */
1065 #endif
1066 png_error_ptr error_fn;
1067 png_error_ptr warning_fn;
1068 png_voidp error_ptr;
1069 #ifdef PNG_USER_MEM_SUPPORTED
1070 png_free_ptr free_fn;
1071 #endif
1073 png_debug(1, "in png_write_destroy\n");
1074 /* free any memory zlib uses */
1075 deflateEnd(&png_ptr->zstream);
1077 /* free our memory. png_free checks NULL for us. */
1078 png_free(png_ptr, png_ptr->zbuf);
1079 png_free(png_ptr, png_ptr->row_buf);
1080 png_free(png_ptr, png_ptr->prev_row);
1081 png_free(png_ptr, png_ptr->sub_row);
1082 png_free(png_ptr, png_ptr->up_row);
1083 png_free(png_ptr, png_ptr->avg_row);
1084 png_free(png_ptr, png_ptr->paeth_row);
1086 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1087 png_free(png_ptr, png_ptr->time_buffer);
1088 #endif
1090 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1091 png_free(png_ptr, png_ptr->prev_filters);
1092 png_free(png_ptr, png_ptr->filter_weights);
1093 png_free(png_ptr, png_ptr->inv_filter_weights);
1094 png_free(png_ptr, png_ptr->filter_costs);
1095 png_free(png_ptr, png_ptr->inv_filter_costs);
1096 #endif
1098 #ifdef PNG_SETJMP_SUPPORTED
1099 /* reset structure */
1100 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
1101 #endif
1103 error_fn = png_ptr->error_fn;
1104 warning_fn = png_ptr->warning_fn;
1105 error_ptr = png_ptr->error_ptr;
1106 #ifdef PNG_USER_MEM_SUPPORTED
1107 free_fn = png_ptr->free_fn;
1108 #endif
1110 png_memset(png_ptr, 0, png_sizeof (png_struct));
1112 png_ptr->error_fn = error_fn;
1113 png_ptr->warning_fn = warning_fn;
1114 png_ptr->error_ptr = error_ptr;
1115 #ifdef PNG_USER_MEM_SUPPORTED
1116 png_ptr->free_fn = free_fn;
1117 #endif
1119 #ifdef PNG_SETJMP_SUPPORTED
1120 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
1121 #endif
1124 /* Allow the application to select one or more row filters to use. */
1125 void PNGAPI
1126 png_set_filter(png_structp png_ptr, int method, int filters)
1128 png_debug(1, "in png_set_filter\n");
1129 if (png_ptr == NULL)
1130 return;
1131 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1132 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1133 (method == PNG_INTRAPIXEL_DIFFERENCING))
1134 method = PNG_FILTER_TYPE_BASE;
1135 #endif
1136 if (method == PNG_FILTER_TYPE_BASE)
1138 switch (filters & (PNG_ALL_FILTERS | 0x07))
1140 case 5:
1141 case 6:
1142 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1143 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
1144 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
1145 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
1146 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
1147 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
1148 default: png_ptr->do_filter = (png_byte)filters; break;
1151 /* If we have allocated the row_buf, this means we have already started
1152 * with the image and we should have allocated all of the filter buffers
1153 * that have been selected. If prev_row isn't already allocated, then
1154 * it is too late to start using the filters that need it, since we
1155 * will be missing the data in the previous row. If an application
1156 * wants to start and stop using particular filters during compression,
1157 * it should start out with all of the filters, and then add and
1158 * remove them after the start of compression.
1160 if (png_ptr->row_buf != NULL)
1162 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1164 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1165 (png_ptr->rowbytes + 1));
1166 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1169 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1171 if (png_ptr->prev_row == NULL)
1173 png_warning(png_ptr, "Can't add Up filter after starting");
1174 png_ptr->do_filter &= ~PNG_FILTER_UP;
1176 else
1178 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1179 (png_ptr->rowbytes + 1));
1180 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1184 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1186 if (png_ptr->prev_row == NULL)
1188 png_warning(png_ptr, "Can't add Average filter after starting");
1189 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1191 else
1193 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1194 (png_ptr->rowbytes + 1));
1195 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1199 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1200 png_ptr->paeth_row == NULL)
1202 if (png_ptr->prev_row == NULL)
1204 png_warning(png_ptr, "Can't add Paeth filter after starting");
1205 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1207 else
1209 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1210 (png_ptr->rowbytes + 1));
1211 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1215 if (png_ptr->do_filter == PNG_NO_FILTERS)
1216 png_ptr->do_filter = PNG_FILTER_NONE;
1219 else
1220 png_error(png_ptr, "Unknown custom filter method");
1223 /* This allows us to influence the way in which libpng chooses the "best"
1224 * filter for the current scanline. While the "minimum-sum-of-absolute-
1225 * differences metric is relatively fast and effective, there is some
1226 * question as to whether it can be improved upon by trying to keep the
1227 * filtered data going to zlib more consistent, hopefully resulting in
1228 * better compression.
1230 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1231 void PNGAPI
1232 png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1233 int num_weights, png_doublep filter_weights,
1234 png_doublep filter_costs)
1236 int i;
1238 png_debug(1, "in png_set_filter_heuristics\n");
1239 if (png_ptr == NULL)
1240 return;
1241 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1243 png_warning(png_ptr, "Unknown filter heuristic method");
1244 return;
1247 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1249 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1252 if (num_weights < 0 || filter_weights == NULL ||
1253 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1255 num_weights = 0;
1258 png_ptr->num_prev_filters = (png_byte)num_weights;
1259 png_ptr->heuristic_method = (png_byte)heuristic_method;
1261 if (num_weights > 0)
1263 if (png_ptr->prev_filters == NULL)
1265 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1266 (png_uint_32)(png_sizeof(png_byte) * num_weights));
1268 /* To make sure that the weighting starts out fairly */
1269 for (i = 0; i < num_weights; i++)
1271 png_ptr->prev_filters[i] = 255;
1275 if (png_ptr->filter_weights == NULL)
1277 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1278 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1280 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1281 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1282 for (i = 0; i < num_weights; i++)
1284 png_ptr->inv_filter_weights[i] =
1285 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1289 for (i = 0; i < num_weights; i++)
1291 if (filter_weights[i] < 0.0)
1293 png_ptr->inv_filter_weights[i] =
1294 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1296 else
1298 png_ptr->inv_filter_weights[i] =
1299 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1300 png_ptr->filter_weights[i] =
1301 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1306 /* If, in the future, there are other filter methods, this would
1307 * need to be based on png_ptr->filter.
1309 if (png_ptr->filter_costs == NULL)
1311 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1312 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1314 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1315 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1317 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1319 png_ptr->inv_filter_costs[i] =
1320 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1324 /* Here is where we set the relative costs of the different filters. We
1325 * should take the desired compression level into account when setting
1326 * the costs, so that Paeth, for instance, has a high relative cost at low
1327 * compression levels, while it has a lower relative cost at higher
1328 * compression settings. The filter types are in order of increasing
1329 * relative cost, so it would be possible to do this with an algorithm.
1331 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1333 if (filter_costs == NULL || filter_costs[i] < 0.0)
1335 png_ptr->inv_filter_costs[i] =
1336 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1338 else if (filter_costs[i] >= 1.0)
1340 png_ptr->inv_filter_costs[i] =
1341 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1342 png_ptr->filter_costs[i] =
1343 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1347 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1349 void PNGAPI
1350 png_set_compression_level(png_structp png_ptr, int level)
1352 png_debug(1, "in png_set_compression_level\n");
1353 if (png_ptr == NULL)
1354 return;
1355 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1356 png_ptr->zlib_level = level;
1359 void PNGAPI
1360 png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1362 png_debug(1, "in png_set_compression_mem_level\n");
1363 if (png_ptr == NULL)
1364 return;
1365 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1366 png_ptr->zlib_mem_level = mem_level;
1369 void PNGAPI
1370 png_set_compression_strategy(png_structp png_ptr, int strategy)
1372 png_debug(1, "in png_set_compression_strategy\n");
1373 if (png_ptr == NULL)
1374 return;
1375 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1376 png_ptr->zlib_strategy = strategy;
1379 void PNGAPI
1380 png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1382 if (png_ptr == NULL)
1383 return;
1384 if (window_bits > 15)
1385 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1386 else if (window_bits < 8)
1387 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1388 #ifndef WBITS_8_OK
1389 /* avoid libpng bug with 256-byte windows */
1390 if (window_bits == 8)
1392 png_warning(png_ptr, "Compression window is being reset to 512");
1393 window_bits=9;
1395 #endif
1396 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1397 png_ptr->zlib_window_bits = window_bits;
1400 void PNGAPI
1401 png_set_compression_method(png_structp png_ptr, int method)
1403 png_debug(1, "in png_set_compression_method\n");
1404 if (png_ptr == NULL)
1405 return;
1406 if (method != 8)
1407 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1408 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1409 png_ptr->zlib_method = method;
1412 void PNGAPI
1413 png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1415 if (png_ptr == NULL)
1416 return;
1417 png_ptr->write_row_fn = write_row_fn;
1420 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1421 void PNGAPI
1422 png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1423 write_user_transform_fn)
1425 png_debug(1, "in png_set_write_user_transform_fn\n");
1426 if (png_ptr == NULL)
1427 return;
1428 png_ptr->transformations |= PNG_USER_TRANSFORM;
1429 png_ptr->write_user_transform_fn = write_user_transform_fn;
1431 #endif
1434 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1435 void PNGAPI
1436 png_write_png(png_structp png_ptr, png_infop info_ptr,
1437 int transforms, voidp params)
1439 if (png_ptr == NULL || info_ptr == NULL)
1440 return;
1441 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1442 /* invert the alpha channel from opacity to transparency */
1443 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1444 png_set_invert_alpha(png_ptr);
1445 #endif
1447 /* Write the file header information. */
1448 png_write_info(png_ptr, info_ptr);
1450 /* ------ these transformations don't touch the info structure ------- */
1452 #if defined(PNG_WRITE_INVERT_SUPPORTED)
1453 /* invert monochrome pixels */
1454 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1455 png_set_invert_mono(png_ptr);
1456 #endif
1458 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
1459 /* Shift the pixels up to a legal bit depth and fill in
1460 * as appropriate to correctly scale the image.
1462 if ((transforms & PNG_TRANSFORM_SHIFT)
1463 && (info_ptr->valid & PNG_INFO_sBIT))
1464 png_set_shift(png_ptr, &info_ptr->sig_bit);
1465 #endif
1467 #if defined(PNG_WRITE_PACK_SUPPORTED)
1468 /* pack pixels into bytes */
1469 if (transforms & PNG_TRANSFORM_PACKING)
1470 png_set_packing(png_ptr);
1471 #endif
1473 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1474 /* swap location of alpha bytes from ARGB to RGBA */
1475 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1476 png_set_swap_alpha(png_ptr);
1477 #endif
1479 #if defined(PNG_WRITE_FILLER_SUPPORTED)
1480 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1481 * RGB (4 channels -> 3 channels). The second parameter is not used.
1483 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1484 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1485 #endif
1487 #if defined(PNG_WRITE_BGR_SUPPORTED)
1488 /* flip BGR pixels to RGB */
1489 if (transforms & PNG_TRANSFORM_BGR)
1490 png_set_bgr(png_ptr);
1491 #endif
1493 #if defined(PNG_WRITE_SWAP_SUPPORTED)
1494 /* swap bytes of 16-bit files to most significant byte first */
1495 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1496 png_set_swap(png_ptr);
1497 #endif
1499 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1500 /* swap bits of 1, 2, 4 bit packed pixel formats */
1501 if (transforms & PNG_TRANSFORM_PACKSWAP)
1502 png_set_packswap(png_ptr);
1503 #endif
1505 /* ----------------------- end of transformations ------------------- */
1507 /* write the bits */
1508 if (info_ptr->valid & PNG_INFO_IDAT)
1509 png_write_image(png_ptr, info_ptr->row_pointers);
1511 /* It is REQUIRED to call this to finish writing the rest of the file */
1512 png_write_end(png_ptr, info_ptr);
1514 if(transforms == 0 || params == NULL)
1515 /* quiet compiler warnings */ return;
1517 #endif
1518 #endif /* PNG_WRITE_SUPPORTED */