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) && \
42
(png_ptr->mng_features_permitted))
44
png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
45
png_ptr->mng_features_permitted = 0;
48
/* Write IHDR information. */
49
png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
50
info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
51
info_ptr->filter_type,
52
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
53
info_ptr->interlace_type);
57
/* The rest of these check to see if the valid field has the appropriate
58
* flag set, and if it does, writes the chunk.
60
#ifdef PNG_WRITE_gAMA_SUPPORTED
61
if (info_ptr->valid & PNG_INFO_gAMA)
63
# ifdef PNG_FLOATING_POINT_SUPPORTED
64
png_write_gAMA(png_ptr, info_ptr->gamma);
66
#ifdef PNG_FIXED_POINT_SUPPORTED
67
png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
72
#ifdef PNG_WRITE_sRGB_SUPPORTED
73
if (info_ptr->valid & PNG_INFO_sRGB)
74
png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
76
#ifdef PNG_WRITE_iCCP_SUPPORTED
77
if (info_ptr->valid & PNG_INFO_iCCP)
78
png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
79
info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
81
#ifdef PNG_WRITE_sBIT_SUPPORTED
82
if (info_ptr->valid & PNG_INFO_sBIT)
83
png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
85
#ifdef PNG_WRITE_cHRM_SUPPORTED
86
if (info_ptr->valid & PNG_INFO_cHRM)
88
#ifdef PNG_FLOATING_POINT_SUPPORTED
89
png_write_cHRM(png_ptr,
90
info_ptr->x_white, info_ptr->y_white,
91
info_ptr->x_red, info_ptr->y_red,
92
info_ptr->x_green, info_ptr->y_green,
93
info_ptr->x_blue, info_ptr->y_blue);
95
# ifdef PNG_FIXED_POINT_SUPPORTED
96
png_write_cHRM_fixed(png_ptr,
97
info_ptr->int_x_white, info_ptr->int_y_white,
98
info_ptr->int_x_red, info_ptr->int_y_red,
99
info_ptr->int_x_green, info_ptr->int_y_green,
100
info_ptr->int_x_blue, info_ptr->int_y_blue);
105
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
106
if (info_ptr->unknown_chunks_num)
108
png_unknown_chunk *up;
110
png_debug(5, "writing extra chunks");
112
for (up = info_ptr->unknown_chunks;
113
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
116
int keep = png_handle_as_unknown(png_ptr, up->name);
117
if (keep != PNG_HANDLE_CHUNK_NEVER &&
118
up->location && !(up->location & PNG_HAVE_PLTE) &&
119
!(up->location & PNG_HAVE_IDAT) &&
120
((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
121
(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
124
png_warning(png_ptr, "Writing zero-length unknown chunk");
125
png_write_chunk(png_ptr, up->name, up->data, up->size);
130
png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
135
png_write_info(png_structp png_ptr, png_infop info_ptr)
137
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
141
png_debug(1, "in png_write_info");
143
if (png_ptr == NULL || info_ptr == NULL)
146
png_write_info_before_PLTE(png_ptr, info_ptr);
148
if (info_ptr->valid & PNG_INFO_PLTE)
149
png_write_PLTE(png_ptr, info_ptr->palette,
150
(png_uint_32)info_ptr->num_palette);
151
else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
152
png_error(png_ptr, "Valid palette required for paletted images");
154
#ifdef PNG_WRITE_tRNS_SUPPORTED
155
if (info_ptr->valid & PNG_INFO_tRNS)
157
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
158
/* Invert the alpha channel (in tRNS) */
159
if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
160
info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
163
for (j = 0; j<(int)info_ptr->num_trans; j++)
164
info_ptr->trans_alpha[j] = (png_byte)(255 - info_ptr->trans_alpha[j]);
167
png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
168
info_ptr->num_trans, info_ptr->color_type);
171
#ifdef PNG_WRITE_bKGD_SUPPORTED
172
if (info_ptr->valid & PNG_INFO_bKGD)
173
png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
175
#ifdef PNG_WRITE_hIST_SUPPORTED
176
if (info_ptr->valid & PNG_INFO_hIST)
177
png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
179
#ifdef PNG_WRITE_oFFs_SUPPORTED
180
if (info_ptr->valid & PNG_INFO_oFFs)
181
png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
182
info_ptr->offset_unit_type);
184
#ifdef PNG_WRITE_pCAL_SUPPORTED
185
if (info_ptr->valid & PNG_INFO_pCAL)
186
png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
187
info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
188
info_ptr->pcal_units, info_ptr->pcal_params);
191
#ifdef PNG_sCAL_SUPPORTED
192
if (info_ptr->valid & PNG_INFO_sCAL)
193
#ifdef PNG_WRITE_sCAL_SUPPORTED
194
#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
195
png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
196
info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
197
#else /* !FLOATING_POINT */
198
#ifdef PNG_FIXED_POINT_SUPPORTED
199
png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
200
info_ptr->scal_s_width, info_ptr->scal_s_height);
201
#endif /* FIXED_POINT */
202
#endif /* FLOATING_POINT */
203
#else /* !WRITE_sCAL */
205
"png_write_sCAL not supported; sCAL chunk not written");
206
#endif /* WRITE_sCAL */
209
#ifdef PNG_WRITE_pHYs_SUPPORTED
210
if (info_ptr->valid & PNG_INFO_pHYs)
211
png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
212
info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
215
#ifdef PNG_WRITE_tIME_SUPPORTED
216
if (info_ptr->valid & PNG_INFO_tIME)
218
png_write_tIME(png_ptr, &(info_ptr->mod_time));
219
png_ptr->mode |= PNG_WROTE_tIME;
223
#ifdef PNG_WRITE_sPLT_SUPPORTED
224
if (info_ptr->valid & PNG_INFO_sPLT)
225
for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
226
png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
229
#ifdef PNG_WRITE_TEXT_SUPPORTED
230
/* Check to see if we need to write text chunks */
231
for (i = 0; i < info_ptr->num_text; i++)
233
png_debug2(2, "Writing header text chunk %d, type %d", i,
234
info_ptr->text[i].compression);
235
/* An internationalized chunk? */
236
if (info_ptr->text[i].compression > 0)
238
#ifdef PNG_WRITE_iTXt_SUPPORTED
239
/* Write international chunk */
240
png_write_iTXt(png_ptr,
241
info_ptr->text[i].compression,
242
info_ptr->text[i].key,
243
info_ptr->text[i].lang,
244
info_ptr->text[i].lang_key,
245
info_ptr->text[i].text);
247
png_warning(png_ptr, "Unable to write international text");
249
/* Mark this chunk as written */
250
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
252
/* If we want a compressed text chunk */
253
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
255
#ifdef PNG_WRITE_zTXt_SUPPORTED
256
/* Write compressed chunk */
257
png_write_zTXt(png_ptr, info_ptr->text[i].key,
258
info_ptr->text[i].text, 0,
259
info_ptr->text[i].compression);
261
png_warning(png_ptr, "Unable to write compressed text");
263
/* Mark this chunk as written */
264
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
266
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
268
#ifdef PNG_WRITE_tEXt_SUPPORTED
269
/* Write uncompressed chunk */
270
png_write_tEXt(png_ptr, info_ptr->text[i].key,
271
info_ptr->text[i].text,
273
/* Mark this chunk as written */
274
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
277
png_warning(png_ptr, "Unable to write uncompressed text");
283
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
284
if (info_ptr->unknown_chunks_num)
286
png_unknown_chunk *up;
288
png_debug(5, "writing extra chunks");
290
for (up = info_ptr->unknown_chunks;
291
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
294
int keep = png_handle_as_unknown(png_ptr, up->name);
295
if (keep != PNG_HANDLE_CHUNK_NEVER &&
296
up->location && (up->location & PNG_HAVE_PLTE) &&
297
!(up->location & PNG_HAVE_IDAT) &&
298
((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
299
(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
301
png_write_chunk(png_ptr, up->name, up->data, up->size);
308
/* Writes the end of the PNG file. If you don't want to write comments or
309
* time information, you can pass NULL for info. If you already wrote these
310
* in png_write_info(), do not write them again here. If you have long
311
* comments, I suggest writing them here, and compressing them.
314
png_write_end(png_structp png_ptr, png_infop info_ptr)
316
png_debug(1, "in png_write_end");
320
if (!(png_ptr->mode & PNG_HAVE_IDAT))
321
png_error(png_ptr, "No IDATs written into file");
323
/* See if user wants us to write information chunks */
324
if (info_ptr != NULL)
326
#ifdef PNG_WRITE_TEXT_SUPPORTED
327
int i; /* local index variable */
329
#ifdef PNG_WRITE_tIME_SUPPORTED
330
/* Check to see if user has supplied a time chunk */
331
if ((info_ptr->valid & PNG_INFO_tIME) &&
332
!(png_ptr->mode & PNG_WROTE_tIME))
333
png_write_tIME(png_ptr, &(info_ptr->mod_time));
335
#ifdef PNG_WRITE_TEXT_SUPPORTED
336
/* Loop through comment chunks */
337
for (i = 0; i < info_ptr->num_text; i++)
339
png_debug2(2, "Writing trailer text chunk %d, type %d", i,
340
info_ptr->text[i].compression);
341
/* An internationalized chunk? */
342
if (info_ptr->text[i].compression > 0)
344
#ifdef PNG_WRITE_iTXt_SUPPORTED
345
/* Write international chunk */
346
png_write_iTXt(png_ptr,
347
info_ptr->text[i].compression,
348
info_ptr->text[i].key,
349
info_ptr->text[i].lang,
350
info_ptr->text[i].lang_key,
351
info_ptr->text[i].text);
353
png_warning(png_ptr, "Unable to write international text");
355
/* Mark this chunk as written */
356
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
358
else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
360
#ifdef PNG_WRITE_zTXt_SUPPORTED
361
/* Write compressed chunk */
362
png_write_zTXt(png_ptr, info_ptr->text[i].key,
363
info_ptr->text[i].text, 0,
364
info_ptr->text[i].compression);
366
png_warning(png_ptr, "Unable to write compressed text");
368
/* Mark this chunk as written */
369
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
371
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
373
#ifdef PNG_WRITE_tEXt_SUPPORTED
374
/* Write uncompressed chunk */
375
png_write_tEXt(png_ptr, info_ptr->text[i].key,
376
info_ptr->text[i].text, 0);
378
png_warning(png_ptr, "Unable to write uncompressed text");
381
/* Mark this chunk as written */
382
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
386
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
387
if (info_ptr->unknown_chunks_num)
389
png_unknown_chunk *up;
391
png_debug(5, "writing extra chunks");
393
for (up = info_ptr->unknown_chunks;
394
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
397
int keep = png_handle_as_unknown(png_ptr, up->name);
398
if (keep != PNG_HANDLE_CHUNK_NEVER &&
399
up->location && (up->location & PNG_AFTER_IDAT) &&
400
((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
401
(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
403
png_write_chunk(png_ptr, up->name, up->data, up->size);
410
png_ptr->mode |= PNG_AFTER_IDAT;
412
/* Write end of PNG file */
413
png_write_IEND(png_ptr);
414
/* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
415
* and restored again in libpng-1.2.30, may cause some applications that
416
* do not set png_ptr->output_flush_fn to crash. If your application
417
* experiences a problem, please try building libpng with
418
* PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
419
* png-mng-implement at lists.sf.net .
421
#ifdef PNG_WRITE_FLUSH_SUPPORTED
422
# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
428
#ifdef PNG_CONVERT_tIME_SUPPORTED
429
/* "tm" structure is not supported on WindowsCE */
431
png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
433
png_debug(1, "in png_convert_from_struct_tm");
435
ptime->year = (png_uint_16)(1900 + ttime->tm_year);
436
ptime->month = (png_byte)(ttime->tm_mon + 1);
437
ptime->day = (png_byte)ttime->tm_mday;
438
ptime->hour = (png_byte)ttime->tm_hour;
439
ptime->minute = (png_byte)ttime->tm_min;
440
ptime->second = (png_byte)ttime->tm_sec;
444
png_convert_from_time_t(png_timep ptime, time_t ttime)
448
png_debug(1, "in png_convert_from_time_t");
450
tbuf = gmtime(&ttime);
451
png_convert_from_struct_tm(ptime, tbuf);
455
/* Initialize png_ptr structure, and allocate any memory needed */
457
png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
458
png_error_ptr error_fn, png_error_ptr warn_fn)
460
#ifdef PNG_USER_MEM_SUPPORTED
461
return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
462
warn_fn, NULL, NULL, NULL));
465
/* Alternate initialize png_ptr structure, and allocate any memory needed */
467
png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
468
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
469
png_malloc_ptr malloc_fn, png_free_ptr free_fn)
471
#endif /* PNG_USER_MEM_SUPPORTED */
472
volatile int png_cleanup_needed = 0;
473
#ifdef PNG_SETJMP_SUPPORTED
477
#ifdef PNG_SETJMP_SUPPORTED
478
#ifdef USE_FAR_KEYWORD
484
png_debug(1, "in png_create_write_struct");
486
#ifdef PNG_USER_MEM_SUPPORTED
487
png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
488
(png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
490
png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
491
#endif /* PNG_USER_MEM_SUPPORTED */
495
/* Added at libpng-1.2.6 */
496
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
497
png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
498
png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
501
#ifdef PNG_SETJMP_SUPPORTED
502
/* Applications that neglect to set up their own setjmp() and then
503
encounter a png_error() will longjmp here. Since the jmpbuf is
504
then meaningless we abort instead of returning. */
505
#ifdef USE_FAR_KEYWORD
508
if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
510
#ifdef USE_FAR_KEYWORD
511
png_memcpy(png_jmpbuf(png_ptr), jmpbuf, png_sizeof(jmp_buf));
516
#ifdef PNG_USER_MEM_SUPPORTED
517
png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
518
#endif /* PNG_USER_MEM_SUPPORTED */
519
png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
526
if (user_png_ver[i] != png_libpng_ver[i])
527
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
528
} while (png_libpng_ver[i++]);
531
if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
533
/* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
534
* we must recompile any applications that use any older library version.
535
* For versions after libpng 1.0, we will be compatible, so we need
536
* only check the first digit.
538
if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
539
(user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
540
(user_png_ver[0] == '0' && user_png_ver[2] < '9'))
542
#ifdef PNG_STDIO_SUPPORTED
546
png_snprintf(msg, 80,
547
"Application was compiled with png.h from libpng-%.20s",
549
png_warning(png_ptr, msg);
551
png_snprintf(msg, 80,
552
"Application is running with png.c from libpng-%.20s",
554
png_warning(png_ptr, msg);
556
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
560
"Incompatible libpng version in application and library");
561
png_cleanup_needed = 1;
565
/* Initialize zbuf - compression buffer */
566
png_ptr->zbuf_size = PNG_ZBUF_SIZE;
567
if (!png_cleanup_needed)
569
png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
571
if (png_ptr->zbuf == NULL)
572
png_cleanup_needed = 1;
574
if (png_cleanup_needed)
576
/* Clean up PNG structure and deallocate any memory. */
577
png_free(png_ptr, png_ptr->zbuf);
578
png_ptr->zbuf = NULL;
579
#ifdef PNG_USER_MEM_SUPPORTED
580
png_destroy_struct_2((png_voidp)png_ptr,
581
(png_free_ptr)free_fn, (png_voidp)mem_ptr);
583
png_destroy_struct((png_voidp)png_ptr);
588
png_set_write_fn(png_ptr, NULL, NULL, NULL);
590
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
591
png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
599
/* Write a few rows of image data. If the image is interlaced,
600
* either you will have to write the 7 sub images, or, if you
601
* have called png_set_interlace_handling(), you will have to
602
* "write" the image seven times.
605
png_write_rows(png_structp png_ptr, png_bytepp row,
606
png_uint_32 num_rows)
608
png_uint_32 i; /* row counter */
609
png_bytepp rp; /* row pointer */
611
png_debug(1, "in png_write_rows");
616
/* Loop through the rows */
617
for (i = 0, rp = row; i < num_rows; i++, rp++)
619
png_write_row(png_ptr, *rp);
623
/* Write the image. You only need to call this function once, even
624
* if you are writing an interlaced image.
627
png_write_image(png_structp png_ptr, png_bytepp image)
629
png_uint_32 i; /* row index */
630
int pass, num_pass; /* pass variables */
631
png_bytepp rp; /* points to current row */
636
png_debug(1, "in png_write_image");
638
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
639
/* Initialize interlace handling. If image is not interlaced,
640
* this will set pass to 1
642
num_pass = png_set_interlace_handling(png_ptr);
646
/* Loop through passes */
647
for (pass = 0; pass < num_pass; pass++)
649
/* Loop through image */
650
for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
652
png_write_row(png_ptr, *rp);
657
/* Called by user to write a row of image data */
659
png_write_row(png_structp png_ptr, png_bytep row)
664
png_debug2(1, "in png_write_row (row %ld, pass %d)",
665
png_ptr->row_number, png_ptr->pass);
667
/* Initialize transformations and other stuff if first time */
668
if (png_ptr->row_number == 0 && png_ptr->pass == 0)
670
/* Make sure we wrote the header info */
671
if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
673
"png_write_info was never called before png_write_row");
675
/* Check for transforms that have been set but were defined out */
676
#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
677
if (png_ptr->transformations & PNG_INVERT_MONO)
678
png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
680
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
681
if (png_ptr->transformations & PNG_FILLER)
682
png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
684
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
685
defined(PNG_READ_PACKSWAP_SUPPORTED)
686
if (png_ptr->transformations & PNG_PACKSWAP)
688
"PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
690
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
691
if (png_ptr->transformations & PNG_PACK)
692
png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
694
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
695
if (png_ptr->transformations & PNG_SHIFT)
696
png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
698
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
699
if (png_ptr->transformations & PNG_BGR)
700
png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
702
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
703
if (png_ptr->transformations & PNG_SWAP_BYTES)
704
png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
707
png_write_start_row(png_ptr);
710
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
711
/* If interlaced and not interested in row, return */
712
if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
714
switch (png_ptr->pass)
717
if (png_ptr->row_number & 0x07)
719
png_write_finish_row(png_ptr);
724
if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
726
png_write_finish_row(png_ptr);
731
if ((png_ptr->row_number & 0x07) != 4)
733
png_write_finish_row(png_ptr);
738
if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
740
png_write_finish_row(png_ptr);
745
if ((png_ptr->row_number & 0x03) != 2)
747
png_write_finish_row(png_ptr);
752
if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
754
png_write_finish_row(png_ptr);
759
if (!(png_ptr->row_number & 0x01))
761
png_write_finish_row(png_ptr);
769
/* Set up row info for transformations */
770
png_ptr->row_info.color_type = png_ptr->color_type;
771
png_ptr->row_info.width = png_ptr->usr_width;
772
png_ptr->row_info.channels = png_ptr->usr_channels;
773
png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
774
png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
775
png_ptr->row_info.channels);
777
png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
778
png_ptr->row_info.width);
780
png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
781
png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);
782
png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
783
png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
784
png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
785
png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);
787
/* Copy user's row into buffer, leaving room for filter byte. */
788
png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
790
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
791
/* Handle interlacing */
792
if (png_ptr->interlaced && png_ptr->pass < 6 &&
793
(png_ptr->transformations & PNG_INTERLACE))
795
png_do_write_interlace(&(png_ptr->row_info),
796
png_ptr->row_buf + 1, png_ptr->pass);
797
/* This should always get caught above, but still ... */
798
if (!(png_ptr->row_info.width))
800
png_write_finish_row(png_ptr);
806
/* Handle other transformations */
807
if (png_ptr->transformations)
808
png_do_write_transformations(png_ptr);
810
#ifdef PNG_MNG_FEATURES_SUPPORTED
811
/* Write filter_method 64 (intrapixel differencing) only if
812
* 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
813
* 2. Libpng did not write a PNG signature (this filter_method is only
814
* used in PNG datastreams that are embedded in MNG datastreams) and
815
* 3. The application called png_permit_mng_features with a mask that
816
* included PNG_FLAG_MNG_FILTER_64 and
817
* 4. The filter_method is 64 and
818
* 5. The color_type is RGB or RGBA
820
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
821
(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
823
/* Intrapixel differencing */
824
png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
828
/* Find a filter if necessary, filter the row and write it out. */
829
png_write_find_filter(png_ptr, &(png_ptr->row_info));
831
if (png_ptr->write_row_fn != NULL)
832
(*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
835
#ifdef PNG_WRITE_FLUSH_SUPPORTED
836
/* Set the automatic flush interval or 0 to turn flushing off */
838
png_set_flush(png_structp png_ptr, int nrows)
840
png_debug(1, "in png_set_flush");
844
png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
847
/* Flush the current output buffers now */
849
png_write_flush(png_structp png_ptr)
853
png_debug(1, "in png_write_flush");
857
/* We have already written out all of the data */
858
if (png_ptr->row_number >= png_ptr->num_rows)
865
/* Compress the data */
866
ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
869
/* Check for compression errors */
872
if (png_ptr->zstream.msg != NULL)
873
png_error(png_ptr, png_ptr->zstream.msg);
875
png_error(png_ptr, "zlib error");
878
if (!(png_ptr->zstream.avail_out))
880
/* Write the IDAT and reset the zlib output buffer */
881
png_write_IDAT(png_ptr, png_ptr->zbuf,
883
png_ptr->zstream.next_out = png_ptr->zbuf;
884
png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
887
} while(wrote_IDAT == 1);
889
/* If there is any data left to be output, write it into a new IDAT */
890
if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
892
/* Write the IDAT and reset the zlib output buffer */
893
png_write_IDAT(png_ptr, png_ptr->zbuf,
894
png_ptr->zbuf_size - png_ptr->zstream.avail_out);
895
png_ptr->zstream.next_out = png_ptr->zbuf;
896
png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
898
png_ptr->flush_rows = 0;
901
#endif /* PNG_WRITE_FLUSH_SUPPORTED */
903
/* Free all memory used by the write */
905
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
907
png_structp png_ptr = NULL;
908
png_infop info_ptr = NULL;
909
#ifdef PNG_USER_MEM_SUPPORTED
910
png_free_ptr free_fn = NULL;
911
png_voidp mem_ptr = NULL;
914
png_debug(1, "in png_destroy_write_struct");
916
if (png_ptr_ptr != NULL)
918
png_ptr = *png_ptr_ptr;
919
#ifdef PNG_USER_MEM_SUPPORTED
920
free_fn = png_ptr->free_fn;
921
mem_ptr = png_ptr->mem_ptr;
925
#ifdef PNG_USER_MEM_SUPPORTED
928
free_fn = png_ptr->free_fn;
929
mem_ptr = png_ptr->mem_ptr;
933
if (info_ptr_ptr != NULL)
934
info_ptr = *info_ptr_ptr;
936
if (info_ptr != NULL)
940
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
942
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
943
if (png_ptr->num_chunk_list)
945
png_free(png_ptr, png_ptr->chunk_list);
946
png_ptr->num_chunk_list = 0;
951
#ifdef PNG_USER_MEM_SUPPORTED
952
png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
955
png_destroy_struct((png_voidp)info_ptr);
957
*info_ptr_ptr = NULL;
962
png_write_destroy(png_ptr);
963
#ifdef PNG_USER_MEM_SUPPORTED
964
png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
967
png_destroy_struct((png_voidp)png_ptr);
974
/* Free any memory used in png_ptr struct (old method) */
976
png_write_destroy(png_structp png_ptr)
978
#ifdef PNG_SETJMP_SUPPORTED
979
jmp_buf tmp_jmp; /* Save jump buffer */
981
png_error_ptr error_fn;
982
png_error_ptr warning_fn;
984
#ifdef PNG_USER_MEM_SUPPORTED
985
png_free_ptr free_fn;
988
png_debug(1, "in png_write_destroy");
990
/* Free any memory zlib uses */
991
deflateEnd(&png_ptr->zstream);
993
/* Free our memory. png_free checks NULL for us. */
994
png_free(png_ptr, png_ptr->zbuf);
995
png_free(png_ptr, png_ptr->row_buf);
996
#ifdef PNG_WRITE_FILTER_SUPPORTED
997
png_free(png_ptr, png_ptr->prev_row);
998
png_free(png_ptr, png_ptr->sub_row);
999
png_free(png_ptr, png_ptr->up_row);
1000
png_free(png_ptr, png_ptr->avg_row);
1001
png_free(png_ptr, png_ptr->paeth_row);
1004
#ifdef PNG_TIME_RFC1123_SUPPORTED
1005
png_free(png_ptr, png_ptr->time_buffer);
1008
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
1009
png_free(png_ptr, png_ptr->prev_filters);
1010
png_free(png_ptr, png_ptr->filter_weights);
1011
png_free(png_ptr, png_ptr->inv_filter_weights);
1012
png_free(png_ptr, png_ptr->filter_costs);
1013
png_free(png_ptr, png_ptr->inv_filter_costs);
1016
#ifdef PNG_SETJMP_SUPPORTED
1017
/* Reset structure */
1018
png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1021
error_fn = png_ptr->error_fn;
1022
warning_fn = png_ptr->warning_fn;
1023
error_ptr = png_ptr->error_ptr;
1024
#ifdef PNG_USER_MEM_SUPPORTED
1025
free_fn = png_ptr->free_fn;
1028
png_memset(png_ptr, 0, png_sizeof(png_struct));
1030
png_ptr->error_fn = error_fn;
1031
png_ptr->warning_fn = warning_fn;
1032
png_ptr->error_ptr = error_ptr;
1033
#ifdef PNG_USER_MEM_SUPPORTED
1034
png_ptr->free_fn = free_fn;
1037
#ifdef PNG_SETJMP_SUPPORTED
1038
png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1042
/* Allow the application to select one or more row filters to use. */
1044
png_set_filter(png_structp png_ptr, int method, int filters)
1046
png_debug(1, "in png_set_filter");
1048
if (png_ptr == NULL)
1050
#ifdef PNG_MNG_FEATURES_SUPPORTED
1051
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1052
(method == PNG_INTRAPIXEL_DIFFERENCING))
1053
method = PNG_FILTER_TYPE_BASE;
1055
if (method == PNG_FILTER_TYPE_BASE)
1057
switch (filters & (PNG_ALL_FILTERS | 0x07))
1059
#ifdef PNG_WRITE_FILTER_SUPPORTED
1062
case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1063
#endif /* PNG_WRITE_FILTER_SUPPORTED */
1064
case PNG_FILTER_VALUE_NONE:
1065
png_ptr->do_filter = PNG_FILTER_NONE; break;
1066
#ifdef PNG_WRITE_FILTER_SUPPORTED
1067
case PNG_FILTER_VALUE_SUB:
1068
png_ptr->do_filter = PNG_FILTER_SUB; break;
1069
case PNG_FILTER_VALUE_UP:
1070
png_ptr->do_filter = PNG_FILTER_UP; break;
1071
case PNG_FILTER_VALUE_AVG:
1072
png_ptr->do_filter = PNG_FILTER_AVG; break;
1073
case PNG_FILTER_VALUE_PAETH:
1074
png_ptr->do_filter = PNG_FILTER_PAETH; break;
1075
default: png_ptr->do_filter = (png_byte)filters; break;
1077
default: png_warning(png_ptr, "Unknown row filter for method 0");
1078
#endif /* PNG_WRITE_FILTER_SUPPORTED */
1081
/* If we have allocated the row_buf, this means we have already started
1082
* with the image and we should have allocated all of the filter buffers
1083
* that have been selected. If prev_row isn't already allocated, then
1084
* it is too late to start using the filters that need it, since we
1085
* will be missing the data in the previous row. If an application
1086
* wants to start and stop using particular filters during compression,
1087
* it should start out with all of the filters, and then add and
1088
* remove them after the start of compression.
1090
if (png_ptr->row_buf != NULL)
1092
#ifdef PNG_WRITE_FILTER_SUPPORTED
1093
if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1095
png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1096
(png_ptr->rowbytes + 1));
1097
png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1100
if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1102
if (png_ptr->prev_row == NULL)
1104
png_warning(png_ptr, "Can't add Up filter after starting");
1105
png_ptr->do_filter &= ~PNG_FILTER_UP;
1109
png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1110
(png_ptr->rowbytes + 1));
1111
png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1115
if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1117
if (png_ptr->prev_row == NULL)
1119
png_warning(png_ptr, "Can't add Average filter after starting");
1120
png_ptr->do_filter &= ~PNG_FILTER_AVG;
1124
png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1125
(png_ptr->rowbytes + 1));
1126
png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1130
if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1131
png_ptr->paeth_row == NULL)
1133
if (png_ptr->prev_row == NULL)
1135
png_warning(png_ptr, "Can't add Paeth filter after starting");
1136
png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1140
png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1141
(png_ptr->rowbytes + 1));
1142
png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1146
if (png_ptr->do_filter == PNG_NO_FILTERS)
1147
#endif /* PNG_WRITE_FILTER_SUPPORTED */
1148
png_ptr->do_filter = PNG_FILTER_NONE;
1152
png_error(png_ptr, "Unknown custom filter method");
1155
/* This allows us to influence the way in which libpng chooses the "best"
1156
* filter for the current scanline. While the "minimum-sum-of-absolute-
1157
* differences metric is relatively fast and effective, there is some
1158
* question as to whether it can be improved upon by trying to keep the
1159
* filtered data going to zlib more consistent, hopefully resulting in
1160
* better compression.
1162
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
1164
png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1165
int num_weights, png_doublep filter_weights,
1166
png_doublep filter_costs)
1170
png_debug(1, "in png_set_filter_heuristics");
1172
if (png_ptr == NULL)
1174
if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1176
png_warning(png_ptr, "Unknown filter heuristic method");
1180
if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1182
heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1185
if (num_weights < 0 || filter_weights == NULL ||
1186
heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1191
png_ptr->num_prev_filters = (png_byte)num_weights;
1192
png_ptr->heuristic_method = (png_byte)heuristic_method;
1194
if (num_weights > 0)
1196
if (png_ptr->prev_filters == NULL)
1198
png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1199
(png_uint_32)(png_sizeof(png_byte) * num_weights));
1201
/* To make sure that the weighting starts out fairly */
1202
for (i = 0; i < num_weights; i++)
1204
png_ptr->prev_filters[i] = 255;
1208
if (png_ptr->filter_weights == NULL)
1210
png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1211
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1213
png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1214
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1215
for (i = 0; i < num_weights; i++)
1217
png_ptr->inv_filter_weights[i] =
1218
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1222
for (i = 0; i < num_weights; i++)
1224
if (filter_weights[i] < 0.0)
1226
png_ptr->inv_filter_weights[i] =
1227
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1231
png_ptr->inv_filter_weights[i] =
1232
(png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1233
png_ptr->filter_weights[i] =
1234
(png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1239
/* If, in the future, there are other filter methods, this would
1240
* need to be based on png_ptr->filter.
1242
if (png_ptr->filter_costs == NULL)
1244
png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1245
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1247
png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1248
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1250
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1252
png_ptr->inv_filter_costs[i] =
1253
png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1257
/* Here is where we set the relative costs of the different filters. We
1258
* should take the desired compression level into account when setting
1259
* the costs, so that Paeth, for instance, has a high relative cost at low
1260
* compression levels, while it has a lower relative cost at higher
1261
* compression settings. The filter types are in order of increasing
1262
* relative cost, so it would be possible to do this with an algorithm.
1264
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1266
if (filter_costs == NULL || filter_costs[i] < 0.0)
1268
png_ptr->inv_filter_costs[i] =
1269
png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1271
else if (filter_costs[i] >= 1.0)
1273
png_ptr->inv_filter_costs[i] =
1274
(png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1275
png_ptr->filter_costs[i] =
1276
(png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1280
#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1283
png_set_compression_level(png_structp png_ptr, int level)
1285
png_debug(1, "in png_set_compression_level");
1287
if (png_ptr == NULL)
1289
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1290
png_ptr->zlib_level = level;
1294
png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1296
png_debug(1, "in png_set_compression_mem_level");
1298
if (png_ptr == NULL)
1300
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1301
png_ptr->zlib_mem_level = mem_level;
1305
png_set_compression_strategy(png_structp png_ptr, int strategy)
1307
png_debug(1, "in png_set_compression_strategy");
1309
if (png_ptr == NULL)
1311
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1312
png_ptr->zlib_strategy = strategy;
1316
png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1318
if (png_ptr == NULL)
1320
if (window_bits > 15)
1321
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1322
else if (window_bits < 8)
1323
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1325
/* Avoid libpng bug with 256-byte windows */
1326
if (window_bits == 8)
1328
png_warning(png_ptr, "Compression window is being reset to 512");
1332
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1333
png_ptr->zlib_window_bits = window_bits;
1337
png_set_compression_method(png_structp png_ptr, int method)
1339
png_debug(1, "in png_set_compression_method");
1341
if (png_ptr == NULL)
1344
png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1345
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1346
png_ptr->zlib_method = method;
1350
png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1352
if (png_ptr == NULL)
1354
png_ptr->write_row_fn = write_row_fn;
1357
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1359
png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1360
write_user_transform_fn)
1362
png_debug(1, "in png_set_write_user_transform_fn");
1364
if (png_ptr == NULL)
1366
png_ptr->transformations |= PNG_USER_TRANSFORM;
1367
png_ptr->write_user_transform_fn = write_user_transform_fn;
1372
#ifdef PNG_INFO_IMAGE_SUPPORTED
1374
png_write_png(png_structp png_ptr, png_infop info_ptr,
1375
int transforms, voidp params)
1377
if (png_ptr == NULL || info_ptr == NULL)
1380
/* Write the file header information. */
1381
png_write_info(png_ptr, info_ptr);
1383
/* ------ these transformations don't touch the info structure ------- */
1385
#ifdef PNG_WRITE_INVERT_SUPPORTED
1386
/* Invert monochrome pixels */
1387
if (transforms & PNG_TRANSFORM_INVERT_MONO)
1388
png_set_invert_mono(png_ptr);
1391
#ifdef PNG_WRITE_SHIFT_SUPPORTED
1392
/* Shift the pixels up to a legal bit depth and fill in
1393
* as appropriate to correctly scale the image.
1395
if ((transforms & PNG_TRANSFORM_SHIFT)
1396
&& (info_ptr->valid & PNG_INFO_sBIT))
1397
png_set_shift(png_ptr, &info_ptr->sig_bit);
1400
#ifdef PNG_WRITE_PACK_SUPPORTED
1401
/* Pack pixels into bytes */
1402
if (transforms & PNG_TRANSFORM_PACKING)
1403
png_set_packing(png_ptr);
1406
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1407
/* Swap location of alpha bytes from ARGB to RGBA */
1408
if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1409
png_set_swap_alpha(png_ptr);
1412
#ifdef PNG_WRITE_FILLER_SUPPORTED
1413
/* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
1414
if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
1415
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
1416
else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
1417
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1420
#ifdef PNG_WRITE_BGR_SUPPORTED
1421
/* Flip BGR pixels to RGB */
1422
if (transforms & PNG_TRANSFORM_BGR)
1423
png_set_bgr(png_ptr);
1426
#ifdef PNG_WRITE_SWAP_SUPPORTED
1427
/* Swap bytes of 16-bit files to most significant byte first */
1428
if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1429
png_set_swap(png_ptr);
1432
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1433
/* Swap bits of 1, 2, 4 bit packed pixel formats */
1434
if (transforms & PNG_TRANSFORM_PACKSWAP)
1435
png_set_packswap(png_ptr);
1438
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1439
/* Invert the alpha channel from opacity to transparency */
1440
if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1441
png_set_invert_alpha(png_ptr);
1444
/* ----------------------- end of transformations ------------------- */
1446
/* Write the bits */
1447
if (info_ptr->valid & PNG_INFO_IDAT)
1448
png_write_image(png_ptr, info_ptr->row_pointers);
1450
/* It is REQUIRED to call this to finish writing the rest of the file */
1451
png_write_end(png_ptr, info_ptr);
1453
transforms = transforms; /* Quiet compiler warnings */
1457
#endif /* PNG_WRITE_SUPPORTED */