2 /* pngwrite.c - general routines to write a PNG file
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.)
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
14 /* Get internal access to png.h */
15 #define PNG_NO_PEDANTIC_WARNINGS
17 #ifdef PNG_WRITE_SUPPORTED
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.
30 png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
32 png_debug(1, "in png_write_info_before_PLTE");
34 if (png_ptr == NULL || info_ptr == NULL)
36 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
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))
43 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
44 png_ptr->mng_features_permitted = 0;
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);
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.
59 #ifdef PNG_WRITE_gAMA_SUPPORTED
60 if (info_ptr->valid & PNG_INFO_gAMA)
62 # ifdef PNG_FLOATING_POINT_SUPPORTED
63 png_write_gAMA(png_ptr, info_ptr->gamma);
65 #ifdef PNG_FIXED_POINT_SUPPORTED
66 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
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);
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);
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);
84 #ifdef PNG_WRITE_cHRM_SUPPORTED
85 if (info_ptr->valid & PNG_INFO_cHRM)
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);
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);
104 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
105 if (info_ptr->unknown_chunks_num)
107 png_unknown_chunk *up;
109 png_debug(5, "writing extra chunks");
111 for (up = info_ptr->unknown_chunks;
112 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
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)))
123 png_warning(png_ptr, "Writing zero-length unknown chunk");
124 png_write_chunk(png_ptr, up->name, up->data, up->size);
129 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
134 png_write_info(png_structp png_ptr, png_infop info_ptr)
136 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
140 png_debug(1, "in png_write_info");
142 if (png_ptr == NULL || info_ptr == NULL)
145 png_write_info_before_PLTE(png_ptr, info_ptr);
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");
153 #ifdef PNG_WRITE_tRNS_SUPPORTED
154 if (info_ptr->valid & PNG_INFO_tRNS)
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)
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]);
166 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
167 info_ptr->num_trans, info_ptr->color_type);
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);
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);
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);
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);
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 */
204 "png_write_sCAL not supported; sCAL chunk not written");
205 #endif /* WRITE_sCAL */
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);
214 #ifdef PNG_WRITE_tIME_SUPPORTED
215 if (info_ptr->valid & PNG_INFO_tIME)
217 png_write_tIME(png_ptr, &(info_ptr->mod_time));
218 png_ptr->mode |= PNG_WROTE_tIME;
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);
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++)
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)
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);
246 png_warning(png_ptr, "Unable to write international text");
248 /* Mark this chunk as written */
249 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
251 /* If we want a compressed text chunk */
252 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
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);
260 png_warning(png_ptr, "Unable to write compressed text");
262 /* Mark this chunk as written */
263 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
265 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
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,
272 /* Mark this chunk as written */
273 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
276 png_warning(png_ptr, "Unable to write uncompressed text");
282 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
283 if (info_ptr->unknown_chunks_num)
285 png_unknown_chunk *up;
287 png_debug(5, "writing extra chunks");
289 for (up = info_ptr->unknown_chunks;
290 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
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)))
300 png_write_chunk(png_ptr, up->name, up->data, up->size);
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.
313 png_write_end(png_structp png_ptr, png_infop info_ptr)
315 png_debug(1, "in png_write_end");
319 if (!(png_ptr->mode & PNG_HAVE_IDAT))
320 png_error(png_ptr, "No IDATs written into file");
322 /* See if user wants us to write information chunks */
323 if (info_ptr != NULL)
325 #ifdef PNG_WRITE_TEXT_SUPPORTED
326 int i; /* local index variable */
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));
334 #ifdef PNG_WRITE_TEXT_SUPPORTED
335 /* Loop through comment chunks */
336 for (i = 0; i < info_ptr->num_text; i++)
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)
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);
352 png_warning(png_ptr, "Unable to write international text");
354 /* Mark this chunk as written */
355 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
357 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
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);
365 png_warning(png_ptr, "Unable to write compressed text");
367 /* Mark this chunk as written */
368 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
370 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
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);
377 png_warning(png_ptr, "Unable to write uncompressed text");
380 /* Mark this chunk as written */
381 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
385 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
386 if (info_ptr->unknown_chunks_num)
388 png_unknown_chunk *up;
390 png_debug(5, "writing extra chunks");
392 for (up = info_ptr->unknown_chunks;
393 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
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)))
402 png_write_chunk(png_ptr, up->name, up->data, up->size);
409 png_ptr->mode |= PNG_AFTER_IDAT;
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 .
420 #ifdef PNG_WRITE_FLUSH_SUPPORTED
421 # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
427 #ifdef PNG_CONVERT_tIME_SUPPORTED
428 /* "tm" structure is not supported on WindowsCE */
430 png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
432 png_debug(1, "in png_convert_from_struct_tm");
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;
443 png_convert_from_time_t(png_timep ptime, time_t ttime)
447 png_debug(1, "in png_convert_from_time_t");
449 tbuf = gmtime(&ttime);
450 png_convert_from_struct_tm(ptime, tbuf);
454 /* Initialize png_ptr structure, and allocate any memory needed */
456 png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
457 png_error_ptr error_fn, png_error_ptr warn_fn)
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));
464 /* Alternate initialize png_ptr structure, and allocate any memory needed */
466 png_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)
470 #endif /* PNG_USER_MEM_SUPPORTED */
471 volatile int png_cleanup_needed = 0;
472 #ifdef PNG_SETJMP_SUPPORTED
476 #ifdef PNG_SETJMP_SUPPORTED
477 #ifdef USE_FAR_KEYWORD
483 png_debug(1, "in png_create_write_struct");
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);
489 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
490 #endif /* PNG_USER_MEM_SUPPORTED */
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;
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
507 if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
509 #ifdef USE_FAR_KEYWORD
510 png_memcpy(png_jmpbuf(png_ptr), jmpbuf, png_sizeof(jmp_buf));
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);
525 if (user_png_ver[i] != png_libpng_ver[i])
526 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
527 } while (png_libpng_ver[i++]);
530 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
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.
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'))
541 #ifdef PNG_STDIO_SUPPORTED
545 png_snprintf(msg, 80,
546 "Application was compiled with png.h from libpng-%.20s",
548 png_warning(png_ptr, msg);
550 png_snprintf(msg, 80,
551 "Application is running with png.c from libpng-%.20s",
553 png_warning(png_ptr, msg);
555 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
559 "Incompatible libpng version in application and library");
560 png_cleanup_needed = 1;
564 /* Initialize zbuf - compression buffer */
565 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
566 if (!png_cleanup_needed)
568 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
570 if (png_ptr->zbuf == NULL)
571 png_cleanup_needed = 1;
573 if (png_cleanup_needed)
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);
582 png_destroy_struct((png_voidp)png_ptr);
587 png_set_write_fn(png_ptr, NULL, NULL, NULL);
589 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
590 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
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.
604 png_write_rows(png_structp png_ptr, png_bytepp row,
605 png_uint_32 num_rows)
607 png_uint_32 i; /* row counter */
608 png_bytepp rp; /* row pointer */
610 png_debug(1, "in png_write_rows");
615 /* Loop through the rows */
616 for (i = 0, rp = row; i < num_rows; i++, rp++)
618 png_write_row(png_ptr, *rp);
622 /* Write the image. You only need to call this function once, even
623 * if you are writing an interlaced image.
626 png_write_image(png_structp png_ptr, png_bytepp image)
628 png_uint_32 i; /* row index */
629 int pass, num_pass; /* pass variables */
630 png_bytepp rp; /* points to current row */
635 png_debug(1, "in png_write_image");
637 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
638 /* Initialize interlace handling. If image is not interlaced,
639 * this will set pass to 1
641 num_pass = png_set_interlace_handling(png_ptr);
645 /* Loop through passes */
646 for (pass = 0; pass < num_pass; pass++)
648 /* Loop through image */
649 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
651 png_write_row(png_ptr, *rp);
656 /* Called by user to write a row of image data */
658 png_write_row(png_structp png_ptr, png_bytep row)
663 png_debug2(1, "in png_write_row (row %ld, pass %d)",
664 png_ptr->row_number, png_ptr->pass);
666 /* Initialize transformations and other stuff if first time */
667 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
669 /* Make sure we wrote the header info */
670 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
672 "png_write_info was never called before png_write_row");
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");
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");
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");
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");
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");
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");
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");
704 png_write_start_row(png_ptr);
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))
711 switch (png_ptr->pass)
714 if (png_ptr->row_number & 0x07)
716 png_write_finish_row(png_ptr);
721 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
723 png_write_finish_row(png_ptr);
728 if ((png_ptr->row_number & 0x07) != 4)
730 png_write_finish_row(png_ptr);
735 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
737 png_write_finish_row(png_ptr);
742 if ((png_ptr->row_number & 0x03) != 2)
744 png_write_finish_row(png_ptr);
749 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
751 png_write_finish_row(png_ptr);
756 if (!(png_ptr->row_number & 0x01))
758 png_write_finish_row(png_ptr);
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);
774 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
775 png_ptr->row_info.width);
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);
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);
787 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
788 /* Handle interlacing */
789 if (png_ptr->interlaced && png_ptr->pass < 6 &&
790 (png_ptr->transformations & PNG_INTERLACE))
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))
797 png_write_finish_row(png_ptr);
803 /* Handle other transformations */
804 if (png_ptr->transformations)
805 png_do_write_transformations(png_ptr);
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
817 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
818 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
820 /* Intrapixel differencing */
821 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
825 /* Find a filter if necessary, filter the row and write it out. */
826 png_write_find_filter(png_ptr, &(png_ptr->row_info));
828 if (png_ptr->write_row_fn != NULL)
829 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
832 #ifdef PNG_WRITE_FLUSH_SUPPORTED
833 /* Set the automatic flush interval or 0 to turn flushing off */
835 png_set_flush(png_structp png_ptr, int nrows)
837 png_debug(1, "in png_set_flush");
841 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
844 /* Flush the current output buffers now */
846 png_write_flush(png_structp png_ptr)
850 png_debug(1, "in png_write_flush");
854 /* We have already written out all of the data */
855 if (png_ptr->row_number >= png_ptr->num_rows)
862 /* Compress the data */
863 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
866 /* Check for compression errors */
869 if (png_ptr->zstream.msg != NULL)
870 png_error(png_ptr, png_ptr->zstream.msg);
872 png_error(png_ptr, "zlib error");
875 if (!(png_ptr->zstream.avail_out))
877 /* Write the IDAT and reset the zlib output buffer */
878 png_write_IDAT(png_ptr, png_ptr->zbuf,
880 png_ptr->zstream.next_out = png_ptr->zbuf;
881 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
884 } while(wrote_IDAT == 1);
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)
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;
895 png_ptr->flush_rows = 0;
898 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
900 /* Free all memory used by the write */
902 png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
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;
911 png_debug(1, "in png_destroy_write_struct");
913 if (png_ptr_ptr != NULL)
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;
922 #ifdef PNG_USER_MEM_SUPPORTED
925 free_fn = png_ptr->free_fn;
926 mem_ptr = png_ptr->mem_ptr;
930 if (info_ptr_ptr != NULL)
931 info_ptr = *info_ptr_ptr;
933 if (info_ptr != NULL)
937 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
939 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
940 if (png_ptr->num_chunk_list)
942 png_free(png_ptr, png_ptr->chunk_list);
943 png_ptr->num_chunk_list = 0;
948 #ifdef PNG_USER_MEM_SUPPORTED
949 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
952 png_destroy_struct((png_voidp)info_ptr);
954 *info_ptr_ptr = NULL;
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,
964 png_destroy_struct((png_voidp)png_ptr);
971 /* Free any memory used in png_ptr struct (old method) */
973 png_write_destroy(png_structp png_ptr)
975 #ifdef PNG_SETJMP_SUPPORTED
976 jmp_buf tmp_jmp; /* Save jump buffer */
978 png_error_ptr error_fn;
979 png_error_ptr warning_fn;
981 #ifdef PNG_USER_MEM_SUPPORTED
982 png_free_ptr free_fn;
985 png_debug(1, "in png_write_destroy");
987 /* Free any memory zlib uses */
988 deflateEnd(&png_ptr->zstream);
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);
1001 #ifdef PNG_TIME_RFC1123_SUPPORTED
1002 png_free(png_ptr, png_ptr->time_buffer);
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);
1013 #ifdef PNG_SETJMP_SUPPORTED
1014 /* Reset structure */
1015 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
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;
1025 png_memset(png_ptr, 0, png_sizeof(png_struct));
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;
1034 #ifdef PNG_SETJMP_SUPPORTED
1035 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1039 /* Allow the application to select one or more row filters to use. */
1041 png_set_filter(png_structp png_ptr, int method, int filters)
1043 png_debug(1, "in png_set_filter");
1045 if (png_ptr == NULL)
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;
1052 if (method == PNG_FILTER_TYPE_BASE)
1054 switch (filters & (PNG_ALL_FILTERS | 0x07))
1056 #ifdef PNG_WRITE_FILTER_SUPPORTED
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;
1074 default: png_warning(png_ptr, "Unknown row filter for method 0");
1075 #endif /* PNG_WRITE_FILTER_SUPPORTED */
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.
1087 if (png_ptr->row_buf != NULL)
1089 #ifdef PNG_WRITE_FILTER_SUPPORTED
1090 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
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;
1097 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1099 if (png_ptr->prev_row == NULL)
1101 png_warning(png_ptr, "Can't add Up filter after starting");
1102 png_ptr->do_filter &= ~PNG_FILTER_UP;
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;
1112 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1114 if (png_ptr->prev_row == NULL)
1116 png_warning(png_ptr, "Can't add Average filter after starting");
1117 png_ptr->do_filter &= ~PNG_FILTER_AVG;
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;
1127 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1128 png_ptr->paeth_row == NULL)
1130 if (png_ptr->prev_row == NULL)
1132 png_warning(png_ptr, "Can't add Paeth filter after starting");
1133 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
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;
1143 if (png_ptr->do_filter == PNG_NO_FILTERS)
1144 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1145 png_ptr->do_filter = PNG_FILTER_NONE;
1149 png_error(png_ptr, "Unknown custom filter method");
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.
1159 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
1161 png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1162 int num_weights, png_doublep filter_weights,
1163 png_doublep filter_costs)
1167 png_debug(1, "in png_set_filter_heuristics");
1169 if (png_ptr == NULL)
1171 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1173 png_warning(png_ptr, "Unknown filter heuristic method");
1177 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1179 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1182 if (num_weights < 0 || filter_weights == NULL ||
1183 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1188 png_ptr->num_prev_filters = (png_byte)num_weights;
1189 png_ptr->heuristic_method = (png_byte)heuristic_method;
1191 if (num_weights > 0)
1193 if (png_ptr->prev_filters == NULL)
1195 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1196 (png_uint_32)(png_sizeof(png_byte) * num_weights));
1198 /* To make sure that the weighting starts out fairly */
1199 for (i = 0; i < num_weights; i++)
1201 png_ptr->prev_filters[i] = 255;
1205 if (png_ptr->filter_weights == NULL)
1207 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1208 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
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++)
1214 png_ptr->inv_filter_weights[i] =
1215 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1219 for (i = 0; i < num_weights; i++)
1221 if (filter_weights[i] < 0.0)
1223 png_ptr->inv_filter_weights[i] =
1224 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
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);
1236 /* If, in the future, there are other filter methods, this would
1237 * need to be based on png_ptr->filter.
1239 if (png_ptr->filter_costs == NULL)
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));
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));
1247 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1249 png_ptr->inv_filter_costs[i] =
1250 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
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.
1261 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1263 if (filter_costs == NULL || filter_costs[i] < 0.0)
1265 png_ptr->inv_filter_costs[i] =
1266 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1268 else if (filter_costs[i] >= 1.0)
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);
1277 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1280 png_set_compression_level(png_structp png_ptr, int level)
1282 png_debug(1, "in png_set_compression_level");
1284 if (png_ptr == NULL)
1286 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1287 png_ptr->zlib_level = level;
1291 png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1293 png_debug(1, "in png_set_compression_mem_level");
1295 if (png_ptr == NULL)
1297 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1298 png_ptr->zlib_mem_level = mem_level;
1302 png_set_compression_strategy(png_structp png_ptr, int strategy)
1304 png_debug(1, "in png_set_compression_strategy");
1306 if (png_ptr == NULL)
1308 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1309 png_ptr->zlib_strategy = strategy;
1313 png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1315 if (png_ptr == NULL)
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");
1322 /* Avoid libpng bug with 256-byte windows */
1323 if (window_bits == 8)
1325 png_warning(png_ptr, "Compression window is being reset to 512");
1329 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1330 png_ptr->zlib_window_bits = window_bits;
1334 png_set_compression_method(png_structp png_ptr, int method)
1336 png_debug(1, "in png_set_compression_method");
1338 if (png_ptr == NULL)
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;
1347 png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1349 if (png_ptr == NULL)
1351 png_ptr->write_row_fn = write_row_fn;
1354 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1356 png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1357 write_user_transform_fn)
1359 png_debug(1, "in png_set_write_user_transform_fn");
1361 if (png_ptr == NULL)
1363 png_ptr->transformations |= PNG_USER_TRANSFORM;
1364 png_ptr->write_user_transform_fn = write_user_transform_fn;
1369 #ifdef PNG_INFO_IMAGE_SUPPORTED
1371 png_write_png(png_structp png_ptr, png_infop info_ptr,
1372 int transforms, voidp params)
1374 if (png_ptr == NULL || info_ptr == NULL)
1377 /* Write the file header information. */
1378 png_write_info(png_ptr, info_ptr);
1380 /* ------ these transformations don't touch the info structure ------- */
1382 #ifdef PNG_WRITE_INVERT_SUPPORTED
1383 /* Invert monochrome pixels */
1384 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1385 png_set_invert_mono(png_ptr);
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.
1392 if ((transforms & PNG_TRANSFORM_SHIFT)
1393 && (info_ptr->valid & PNG_INFO_sBIT))
1394 png_set_shift(png_ptr, &info_ptr->sig_bit);
1397 #ifdef PNG_WRITE_PACK_SUPPORTED
1398 /* Pack pixels into bytes */
1399 if (transforms & PNG_TRANSFORM_PACKING)
1400 png_set_packing(png_ptr);
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);
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);
1417 #ifdef PNG_WRITE_BGR_SUPPORTED
1418 /* Flip BGR pixels to RGB */
1419 if (transforms & PNG_TRANSFORM_BGR)
1420 png_set_bgr(png_ptr);
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);
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);
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);
1441 /* ----------------------- end of transformations ------------------- */
1443 /* Write the bits */
1444 if (info_ptr->valid & PNG_INFO_IDAT)
1445 png_write_image(png_ptr, info_ptr->row_pointers);
1447 /* It is REQUIRED to call this to finish writing the rest of the file */
1448 png_write_end(png_ptr, info_ptr);
1450 transforms = transforms; /* Quiet compiler warnings */
1454 #endif /* PNG_WRITE_SUPPORTED */