source: trunk/src/3rdparty/libpng/pngwrite.c@ 890

Last change on this file since 890 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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