source: liacs/MIR2010/SourceCode/cximage/png/pngwrite.c@ 405

Last change on this file since 405 was 95, checked in by Rick van der Zwet, 15 years ago

Bad boy, improper move of directory

File size: 49.2 KB
Line 
1
2/* pngwrite.c - general routines to write a PNG file
3 *
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 */
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 */
25void PNGAPI
26png_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
124void PNGAPI
125png_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 */
294void PNGAPI
295png_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 */
404void PNGAPI
405png_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
416void PNGAPI
417png_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 */
429png_structp PNGAPI
430png_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 */
439png_structp PNGAPI
440png_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#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 */
469
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
475
476#ifdef PNG_SETJMP_SUPPORTED
477#ifdef USE_FAR_KEYWORD
478 if (setjmp(jmpbuf))
479#else
480 if (setjmp(png_ptr->jmpbuf))
481#endif
482 {
483 png_free(png_ptr, png_ptr->zbuf);
484 png_ptr->zbuf=NULL;
485 png_destroy_struct(png_ptr);
486 return (NULL);
487 }
488#ifdef USE_FAR_KEYWORD
489 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
490#endif
491#endif
492
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);
497
498 i=0;
499 do
500 {
501 if(user_png_ver[i] != png_libpng_ver[i])
502 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
503 } while (png_libpng_ver[i++]);
504
505 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
506 {
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.
511 */
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'))
515 {
516#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
517 char msg[80];
518 if (user_png_ver)
519 {
520 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
521 user_png_ver);
522 png_warning(png_ptr, msg);
523 }
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");
533 }
534 }
535
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);
540
541 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
542 png_flush_ptr_NULL);
543
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
548
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);
563}
564
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
569void PNGAPI
570png_write_init(png_structp png_ptr)
571{
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);
574}
575
576void PNGAPI
577png_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)
579{
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)
585 {
586 char msg[80];
587 png_ptr->warning_fn=NULL;
588 if (user_png_ver)
589 {
590 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
591 user_png_ver);
592 png_warning(png_ptr, msg);
593 }
594 sprintf(msg, "Application is running with png.c from libpng-%.20s",
595 png_libpng_ver);
596 png_warning(png_ptr, msg);
597 }
598#endif
599 if(png_sizeof(png_struct) > png_struct_size)
600 {
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.");
607 }
608 if(png_sizeof(png_info) > png_info_size)
609 {
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.");
616 }
617 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
618}
619#endif /* PNG_1_0_X || PNG_1_2_X */
620
621
622void PNGAPI
623png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
624 png_size_t png_struct_size)
625{
626 png_structp png_ptr=*ptr_ptr;
627#ifdef PNG_SETJMP_SUPPORTED
628 jmp_buf tmp_jmp; /* to save current jump buffer */
629#endif
630
631 int i = 0;
632
633 if (png_ptr == NULL)
634 return;
635
636 do
637 {
638 if (user_png_ver[i] != png_libpng_ver[i])
639 {
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
648 }
649 } while (png_libpng_ver[i++]);
650
651 png_debug(1, "in png_write_init_3\n");
652
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
657
658 if (png_sizeof(png_struct) > png_struct_size)
659 {
660 png_destroy_struct(png_ptr);
661 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
662 *ptr_ptr = png_ptr;
663 }
664
665 /* reset all variables to 0 */
666 png_memset(png_ptr, 0, png_sizeof (png_struct));
667
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
673
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 */
681
682#ifdef PNG_SETJMP_SUPPORTED
683 /* restore jump buffer */
684 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
685#endif
686
687 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
688 png_flush_ptr_NULL);
689
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);
694
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
699}
700
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.
705 */
706void PNGAPI
707png_write_rows(png_structp png_ptr, png_bytepp row,
708 png_uint_32 num_rows)
709{
710 png_uint_32 i; /* row counter */
711 png_bytepp rp; /* row pointer */
712
713 png_debug(1, "in png_write_rows\n");
714
715 if (png_ptr == NULL)
716 return;
717
718 /* loop through the rows */
719 for (i = 0, rp = row; i < num_rows; i++, rp++)
720 {
721 png_write_row(png_ptr, *rp);
722 }
723}
724
725/* Write the image. You only need to call this function once, even
726 * if you are writing an interlaced image.
727 */
728void PNGAPI
729png_write_image(png_structp png_ptr, png_bytepp image)
730{
731 png_uint_32 i; /* row index */
732 int pass, num_pass; /* pass variables */
733 png_bytepp rp; /* points to current row */
734
735 if (png_ptr == NULL)
736 return;
737
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++)
748 {
749 /* loop through image */
750 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
751 {
752 png_write_row(png_ptr, *rp);
753 }
754 }
755}
756
757/* called by user to write a row of image data */
758void PNGAPI
759png_write_row(png_structp png_ptr, png_bytep row)
760{
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);
765
766 /* initialize transformations and other stuff if first time */
767 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
768 {
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.");
773
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
803
804 png_write_start_row(png_ptr);
805 }
806
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))
810 {
811 switch (png_ptr->pass)
812 {
813 case 0:
814 if (png_ptr->row_number & 0x07)
815 {
816 png_write_finish_row(png_ptr);
817 return;
818 }
819 break;
820 case 1:
821 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
822 {
823 png_write_finish_row(png_ptr);
824 return;
825 }
826 break;
827 case 2:
828 if ((png_ptr->row_number & 0x07) != 4)
829 {
830 png_write_finish_row(png_ptr);
831 return;
832 }
833 break;
834 case 3:
835 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
836 {
837 png_write_finish_row(png_ptr);
838 return;
839 }
840 break;
841 case 4:
842 if ((png_ptr->row_number & 0x03) != 2)
843 {
844 png_write_finish_row(png_ptr);
845 return;
846 }
847 break;
848 case 5:
849 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
850 {
851 png_write_finish_row(png_ptr);
852 return;
853 }
854 break;
855 case 6:
856 if (!(png_ptr->row_number & 0x01))
857 {
858 png_write_finish_row(png_ptr);
859 return;
860 }
861 break;
862 }
863 }
864#endif
865
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);
873
874 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
875 png_ptr->row_info.width);
876
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);
883
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);
887
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))
892 {
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))
897 {
898 png_write_finish_row(png_ptr);
899 return;
900 }
901 }
902#endif
903
904 /* handle other transformations */
905 if (png_ptr->transformations)
906 png_do_write_transformations(png_ptr);
907
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
917 */
918 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
919 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
920 {
921 /* Intrapixel differencing */
922 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
923 }
924#endif
925
926 /* Find a filter if necessary, filter the row and write it out. */
927 png_write_find_filter(png_ptr, &(png_ptr->row_info));
928
929 if (png_ptr->write_row_fn != NULL)
930 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
931}
932
933#if defined(PNG_WRITE_FLUSH_SUPPORTED)
934/* Set the automatic flush interval or 0 to turn flushing off */
935void PNGAPI
936png_set_flush(png_structp png_ptr, int nrows)
937{
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);
942}
943
944/* flush the current output buffers now */
945void PNGAPI
946png_write_flush(png_structp png_ptr)
947{
948 int wrote_IDAT;
949
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;
956
957 do
958 {
959 int ret;
960
961 /* compress the data */
962 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
963 wrote_IDAT = 0;
964
965 /* check for compression errors */
966 if (ret != Z_OK)
967 {
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");
972 }
973
974 if (!(png_ptr->zstream.avail_out))
975 {
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;
982 }
983 } while(wrote_IDAT == 1);
984
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)
987 {
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;
993 }
994 png_ptr->flush_rows = 0;
995 png_flush(png_ptr);
996}
997#endif /* PNG_WRITE_FLUSH_SUPPORTED */
998
999/* free all memory used by the write */
1000void PNGAPI
1001png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
1002{
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
1009
1010 png_debug(1, "in png_destroy_write_struct\n");
1011 if (png_ptr_ptr != NULL)
1012 {
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
1018 }
1019
1020 if (info_ptr_ptr != NULL)
1021 info_ptr = *info_ptr_ptr;
1022
1023 if (info_ptr != NULL)
1024 {
1025 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
1026
1027#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1028 if (png_ptr->num_chunk_list)
1029 {
1030 png_free(png_ptr, png_ptr->chunk_list);
1031 png_ptr->chunk_list=NULL;
1032 png_ptr->num_chunk_list=0;
1033 }
1034#endif
1035
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;
1043 }
1044
1045 if (png_ptr != NULL)
1046 {
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;
1055 }
1056}
1057
1058
1059/* Free any memory used in png_ptr struct (old method) */
1060void /* PRIVATE */
1061png_write_destroy(png_structp png_ptr)
1062{
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
1072
1073 png_debug(1, "in png_write_destroy\n");
1074 /* free any memory zlib uses */
1075 deflateEnd(&png_ptr->zstream);
1076
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);
1085
1086#if defined(PNG_TIME_RFC1123_SUPPORTED)
1087 png_free(png_ptr, png_ptr->time_buffer);
1088#endif
1089
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
1097
1098#ifdef PNG_SETJMP_SUPPORTED
1099 /* reset structure */
1100 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
1101#endif
1102
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
1109
1110 png_memset(png_ptr, 0, png_sizeof (png_struct));
1111
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
1118
1119#ifdef PNG_SETJMP_SUPPORTED
1120 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
1121#endif
1122}
1123
1124/* Allow the application to select one or more row filters to use. */
1125void PNGAPI
1126png_set_filter(png_structp png_ptr, int method, int filters)
1127{
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)
1137 {
1138 switch (filters & (PNG_ALL_FILTERS | 0x07))
1139 {
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;
1149 }
1150
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.
1159 */
1160 if (png_ptr->row_buf != NULL)
1161 {
1162 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1163 {
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;
1167 }
1168
1169 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1170 {
1171 if (png_ptr->prev_row == NULL)
1172 {
1173 png_warning(png_ptr, "Can't add Up filter after starting");
1174 png_ptr->do_filter &= ~PNG_FILTER_UP;
1175 }
1176 else
1177 {
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;
1181 }
1182 }
1183
1184 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1185 {
1186 if (png_ptr->prev_row == NULL)
1187 {
1188 png_warning(png_ptr, "Can't add Average filter after starting");
1189 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1190 }
1191 else
1192 {
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;
1196 }
1197 }
1198
1199 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1200 png_ptr->paeth_row == NULL)
1201 {
1202 if (png_ptr->prev_row == NULL)
1203 {
1204 png_warning(png_ptr, "Can't add Paeth filter after starting");
1205 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1206 }
1207 else
1208 {
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;
1212 }
1213 }
1214
1215 if (png_ptr->do_filter == PNG_NO_FILTERS)
1216 png_ptr->do_filter = PNG_FILTER_NONE;
1217 }
1218 }
1219 else
1220 png_error(png_ptr, "Unknown custom filter method");
1221}
1222
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.
1229 */
1230#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1231void PNGAPI
1232png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1233 int num_weights, png_doublep filter_weights,
1234 png_doublep filter_costs)
1235{
1236 int i;
1237
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)
1242 {
1243 png_warning(png_ptr, "Unknown filter heuristic method");
1244 return;
1245 }
1246
1247 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1248 {
1249 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1250 }
1251
1252 if (num_weights < 0 || filter_weights == NULL ||
1253 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1254 {
1255 num_weights = 0;
1256 }
1257
1258 png_ptr->num_prev_filters = (png_byte)num_weights;
1259 png_ptr->heuristic_method = (png_byte)heuristic_method;
1260
1261 if (num_weights > 0)
1262 {
1263 if (png_ptr->prev_filters == NULL)
1264 {
1265 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1266 (png_uint_32)(png_sizeof(png_byte) * num_weights));
1267
1268 /* To make sure that the weighting starts out fairly */
1269 for (i = 0; i < num_weights; i++)
1270 {
1271 png_ptr->prev_filters[i] = 255;
1272 }
1273 }
1274
1275 if (png_ptr->filter_weights == NULL)
1276 {
1277 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1278 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1279
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++)
1283 {
1284 png_ptr->inv_filter_weights[i] =
1285 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1286 }
1287 }
1288
1289 for (i = 0; i < num_weights; i++)
1290 {
1291 if (filter_weights[i] < 0.0)
1292 {
1293 png_ptr->inv_filter_weights[i] =
1294 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1295 }
1296 else
1297 {
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);
1302 }
1303 }
1304 }
1305
1306 /* If, in the future, there are other filter methods, this would
1307 * need to be based on png_ptr->filter.
1308 */
1309 if (png_ptr->filter_costs == NULL)
1310 {
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));
1313
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));
1316
1317 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1318 {
1319 png_ptr->inv_filter_costs[i] =
1320 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1321 }
1322 }
1323
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.
1330 */
1331 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1332 {
1333 if (filter_costs == NULL || filter_costs[i] < 0.0)
1334 {
1335 png_ptr->inv_filter_costs[i] =
1336 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1337 }
1338 else if (filter_costs[i] >= 1.0)
1339 {
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);
1344 }
1345 }
1346}
1347#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1348
1349void PNGAPI
1350png_set_compression_level(png_structp png_ptr, int level)
1351{
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;
1357}
1358
1359void PNGAPI
1360png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1361{
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;
1367}
1368
1369void PNGAPI
1370png_set_compression_strategy(png_structp png_ptr, int strategy)
1371{
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;
1377}
1378
1379void PNGAPI
1380png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1381{
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)
1391 {
1392 png_warning(png_ptr, "Compression window is being reset to 512");
1393 window_bits=9;
1394 }
1395#endif
1396 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1397 png_ptr->zlib_window_bits = window_bits;
1398}
1399
1400void PNGAPI
1401png_set_compression_method(png_structp png_ptr, int method)
1402{
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;
1410}
1411
1412void PNGAPI
1413png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1414{
1415 if (png_ptr == NULL)
1416 return;
1417 png_ptr->write_row_fn = write_row_fn;
1418}
1419
1420#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1421void PNGAPI
1422png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1423 write_user_transform_fn)
1424{
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;
1430}
1431#endif
1432
1433
1434#if defined(PNG_INFO_IMAGE_SUPPORTED)
1435void PNGAPI
1436png_write_png(png_structp png_ptr, png_infop info_ptr,
1437 int transforms, voidp params)
1438{
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
1446
1447 /* Write the file header information. */
1448 png_write_info(png_ptr, info_ptr);
1449
1450 /* ------ these transformations don't touch the info structure ------- */
1451
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
1457
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.
1461 */
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
1466
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
1472
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
1478
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.
1482 */
1483 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1484 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1485#endif
1486
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
1492
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
1498
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
1504
1505 /* ----------------------- end of transformations ------------------- */
1506
1507 /* write the bits */
1508 if (info_ptr->valid & PNG_INFO_IDAT)
1509 png_write_image(png_ptr, info_ptr->row_pointers);
1510
1511 /* It is REQUIRED to call this to finish writing the rest of the file */
1512 png_write_end(png_ptr, info_ptr);
1513
1514 if(transforms == 0 || params == NULL)
1515 /* quiet compiler warnings */ return;
1516}
1517#endif
1518#endif /* PNG_WRITE_SUPPORTED */
Note: See TracBrowser for help on using the repository browser.