15
#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
19
#ifdef PNG_WRITE_SUPPORTED
21
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
24
write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
27
if (info_ptr->unknown_chunks_num != 0)
29
png_const_unknown_chunkp up;
31
png_debug(5, "writing extra chunks");
33
for (up = info_ptr->unknown_chunks;
34
up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
36
if ((up->location & where) != 0)
41
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
42
int keep = png_handle_as_unknown(png_ptr, up->name);
56
if (keep != PNG_HANDLE_CHUNK_NEVER &&
57
((up->name[3] & 0x20) ||
58
keep == PNG_HANDLE_CHUNK_ALWAYS ||
59
(keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
60
png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
65
png_warning(png_ptr, "Writing zero-length unknown chunk");
67
png_write_chunk(png_ptr, up->name, up->data, up->size);
84
png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
86
png_debug(1, "in png_write_info_before_PLTE");
88
if (png_ptr == NULL || info_ptr == NULL)
91
if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
94
png_write_sig(png_ptr);
96
#ifdef PNG_MNG_FEATURES_SUPPORTED
97
if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \
98
png_ptr->mng_features_permitted != 0)
101
"MNG features are not allowed in a PNG datastream");
102
png_ptr->mng_features_permitted = 0;
107
png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
108
info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
109
info_ptr->filter_type,
110
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
111
info_ptr->interlace_type
131
#ifdef PNG_GAMMA_SUPPORTED
132
# ifdef PNG_WRITE_gAMA_SUPPORTED
133
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
134
(info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 &&
135
(info_ptr->valid & PNG_INFO_gAMA) != 0)
136
png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
140
#ifdef PNG_COLORSPACE_SUPPORTED
144
# ifdef PNG_WRITE_iCCP_SUPPORTED
145
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
146
(info_ptr->valid & PNG_INFO_iCCP) != 0)
148
# ifdef PNG_WRITE_sRGB_SUPPORTED
149
if ((info_ptr->valid & PNG_INFO_sRGB) != 0)
150
png_app_warning(png_ptr,
151
"profile matches sRGB but writing iCCP instead");
154
png_write_iCCP(png_ptr, info_ptr->iccp_name,
155
info_ptr->iccp_profile);
157
# ifdef PNG_WRITE_sRGB_SUPPORTED
162
# ifdef PNG_WRITE_sRGB_SUPPORTED
163
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
164
(info_ptr->valid & PNG_INFO_sRGB) != 0)
165
png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
169
#ifdef PNG_WRITE_sBIT_SUPPORTED
170
if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
171
png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
174
#ifdef PNG_COLORSPACE_SUPPORTED
175
# ifdef PNG_WRITE_cHRM_SUPPORTED
176
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
177
(info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 &&
178
(info_ptr->valid & PNG_INFO_cHRM) != 0)
179
png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
183
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
184
write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
187
png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
192
png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
194
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
198
png_debug(1, "in png_write_info");
200
if (png_ptr == NULL || info_ptr == NULL)
203
png_write_info_before_PLTE(png_ptr, info_ptr);
205
if ((info_ptr->valid & PNG_INFO_PLTE) != 0)
206
png_write_PLTE(png_ptr, info_ptr->palette,
207
(png_uint_32)info_ptr->num_palette);
209
else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
210
png_error(png_ptr, "Valid palette required for paletted images");
212
#ifdef PNG_WRITE_tRNS_SUPPORTED
213
if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
215
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
217
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
218
info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
222
jend = info_ptr->num_trans;
223
if (jend > PNG_MAX_PALETTE_LENGTH)
224
jend = PNG_MAX_PALETTE_LENGTH;
226
for (j = 0; j<jend; ++j)
227
info_ptr->trans_alpha[j] =
228
(png_byte)(255 - info_ptr->trans_alpha[j]);
231
png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
232
info_ptr->num_trans, info_ptr->color_type);
235
#ifdef PNG_WRITE_bKGD_SUPPORTED
236
if ((info_ptr->valid & PNG_INFO_bKGD) != 0)
237
png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
240
#ifdef PNG_WRITE_eXIf_SUPPORTED
241
if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
243
png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
244
png_ptr->mode |= PNG_WROTE_eXIf;
248
#ifdef PNG_WRITE_hIST_SUPPORTED
249
if ((info_ptr->valid & PNG_INFO_hIST) != 0)
250
png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
253
#ifdef PNG_WRITE_oFFs_SUPPORTED
254
if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
255
png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
256
info_ptr->offset_unit_type);
259
#ifdef PNG_WRITE_pCAL_SUPPORTED
260
if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
261
png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
262
info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
263
info_ptr->pcal_units, info_ptr->pcal_params);
266
#ifdef PNG_WRITE_sCAL_SUPPORTED
267
if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
268
png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
269
info_ptr->scal_s_width, info_ptr->scal_s_height);
272
#ifdef PNG_WRITE_pHYs_SUPPORTED
273
if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
274
png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
275
info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
278
#ifdef PNG_WRITE_tIME_SUPPORTED
279
if ((info_ptr->valid & PNG_INFO_tIME) != 0)
281
png_write_tIME(png_ptr, &(info_ptr->mod_time));
282
png_ptr->mode |= PNG_WROTE_tIME;
286
#ifdef PNG_WRITE_sPLT_SUPPORTED
287
if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
288
for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
289
png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
292
#ifdef PNG_WRITE_TEXT_SUPPORTED
294
for (i = 0; i < info_ptr->num_text; i++)
296
png_debug2(2, "Writing header text chunk %d, type %d", i,
297
info_ptr->text[i].compression);
299
if (info_ptr->text[i].compression > 0)
301
#ifdef PNG_WRITE_iTXt_SUPPORTED
303
png_write_iTXt(png_ptr,
304
info_ptr->text[i].compression,
305
info_ptr->text[i].key,
306
info_ptr->text[i].lang,
307
info_ptr->text[i].lang_key,
308
info_ptr->text[i].text);
310
if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
311
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
313
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
315
png_warning(png_ptr, "Unable to write international text");
320
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
322
#ifdef PNG_WRITE_zTXt_SUPPORTED
324
png_write_zTXt(png_ptr, info_ptr->text[i].key,
325
info_ptr->text[i].text, info_ptr->text[i].compression);
327
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
329
png_warning(png_ptr, "Unable to write compressed text");
333
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
335
#ifdef PNG_WRITE_tEXt_SUPPORTED
337
png_write_tEXt(png_ptr, info_ptr->text[i].key,
338
info_ptr->text[i].text,
341
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
344
png_warning(png_ptr, "Unable to write uncompressed text");
350
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
351
write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
361
png_write_end(png_structrp png_ptr, png_inforp info_ptr)
363
png_debug(1, "in png_write_end");
368
if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
369
png_error(png_ptr, "No IDATs written into file");
371
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
372
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
373
png_ptr->num_palette_max >= png_ptr->num_palette)
374
png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
378
if (info_ptr != NULL)
380
#ifdef PNG_WRITE_TEXT_SUPPORTED
383
#ifdef PNG_WRITE_tIME_SUPPORTED
385
if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
386
(png_ptr->mode & PNG_WROTE_tIME) == 0)
387
png_write_tIME(png_ptr, &(info_ptr->mod_time));
390
#ifdef PNG_WRITE_TEXT_SUPPORTED
392
for (i = 0; i < info_ptr->num_text; i++)
394
png_debug2(2, "Writing trailer text chunk %d, type %d", i,
395
info_ptr->text[i].compression);
397
if (info_ptr->text[i].compression > 0)
399
#ifdef PNG_WRITE_iTXt_SUPPORTED
401
png_write_iTXt(png_ptr,
402
info_ptr->text[i].compression,
403
info_ptr->text[i].key,
404
info_ptr->text[i].lang,
405
info_ptr->text[i].lang_key,
406
info_ptr->text[i].text);
408
if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
409
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
411
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
413
png_warning(png_ptr, "Unable to write international text");
417
else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
419
#ifdef PNG_WRITE_zTXt_SUPPORTED
421
png_write_zTXt(png_ptr, info_ptr->text[i].key,
422
info_ptr->text[i].text, info_ptr->text[i].compression);
424
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
426
png_warning(png_ptr, "Unable to write compressed text");
430
else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
432
#ifdef PNG_WRITE_tEXt_SUPPORTED
434
png_write_tEXt(png_ptr, info_ptr->text[i].key,
435
info_ptr->text[i].text, 0);
437
info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
439
png_warning(png_ptr, "Unable to write uncompressed text");
445
#ifdef PNG_WRITE_eXIf_SUPPORTED
446
if ((info_ptr->valid & PNG_INFO_eXIf) != 0 &&
447
(png_ptr->mode & PNG_WROTE_eXIf) == 0)
448
png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
451
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
452
write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
456
png_ptr->mode |= PNG_AFTER_IDAT;
459
png_write_IEND(png_ptr);
468
#ifdef PNG_WRITE_FLUSH_SUPPORTED
469
# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
475
#ifdef PNG_CONVERT_tIME_SUPPORTED
477
png_convert_from_struct_tm(png_timep ptime, const struct tm * ttime)
479
png_debug(1, "in png_convert_from_struct_tm");
481
ptime->year = (png_uint_16)(1900 + ttime->tm_year);
482
ptime->month = (png_byte)(ttime->tm_mon + 1);
483
ptime->day = (png_byte)ttime->tm_mday;
484
ptime->hour = (png_byte)ttime->tm_hour;
485
ptime->minute = (png_byte)ttime->tm_min;
486
ptime->second = (png_byte)ttime->tm_sec;
490
png_convert_from_time_t(png_timep ptime, time_t ttime)
494
png_debug(1, "in png_convert_from_time_t");
496
tbuf = gmtime(&ttime);
503
memset(ptime, 0, sizeof(*ptime));
507
png_convert_from_struct_tm(ptime, tbuf);
512
PNG_FUNCTION(png_structp,PNGAPI
513
png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
514
png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
516
#ifndef PNG_USER_MEM_SUPPORTED
517
png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
518
error_fn, warn_fn, NULL, NULL, NULL);
520
return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
521
warn_fn, NULL, NULL, NULL);
525
PNG_FUNCTION(png_structp,PNGAPI
526
png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
527
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
528
png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
530
png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
531
error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
538
png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
544
png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
545
png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
546
png_ptr->zlib_mem_level = 8;
547
png_ptr->zlib_window_bits = 15;
548
png_ptr->zlib_method = 8;
550
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
551
png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
552
png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
553
png_ptr->zlib_text_mem_level = 8;
554
png_ptr->zlib_text_window_bits = 15;
555
png_ptr->zlib_text_method = 8;
563
#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
567
png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
574
png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
581
png_set_write_fn(png_ptr, NULL, NULL, NULL);
594
png_write_rows(png_structrp png_ptr, png_bytepp row,
595
png_uint_32 num_rows)
600
png_debug(1, "in png_write_rows");
606
for (i = 0, rp = row; i < num_rows; i++, rp++)
608
png_write_row(png_ptr, *rp);
616
png_write_image(png_structrp png_ptr, png_bytepp image)
625
png_debug(1, "in png_write_image");
627
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
631
num_pass = png_set_interlace_handling(png_ptr);
636
for (pass = 0; pass < num_pass; pass++)
639
for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
641
png_write_row(png_ptr, *rp);
646
#ifdef PNG_MNG_FEATURES_SUPPORTED
649
png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
651
png_debug(1, "in png_do_write_intrapixel");
653
if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
656
png_uint_32 row_width = row_info->width;
657
if (row_info->bit_depth == 8)
662
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
665
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
671
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
673
*(rp) = (png_byte)(*rp - *(rp + 1));
674
*(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1));
678
#ifdef PNG_WRITE_16BIT_SUPPORTED
679
else if (row_info->bit_depth == 16)
684
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
687
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
693
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
695
png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
696
png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
697
png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
698
png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
699
png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
700
*(rp ) = (png_byte)(red >> 8);
701
*(rp + 1) = (png_byte)red;
702
*(rp + 4) = (png_byte)(blue >> 8);
703
*(rp + 5) = (png_byte)blue;
713
png_write_row(png_structrp png_ptr, png_const_bytep row)
716
png_row_info row_info;
718
png_debug2(1, "in png_write_row (row %u, pass %d)",
719
png_ptr->row_number, png_ptr->pass);
725
if (png_ptr->row_number == 0 && png_ptr->pass == 0)
728
if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
730
"png_write_info was never called before png_write_row");
733
#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
734
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
735
png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
738
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
739
if ((png_ptr->transformations & PNG_FILLER) != 0)
740
png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
742
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
743
defined(PNG_READ_PACKSWAP_SUPPORTED)
744
if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
746
"PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
749
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
750
if ((png_ptr->transformations & PNG_PACK) != 0)
751
png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
754
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
755
if ((png_ptr->transformations & PNG_SHIFT) != 0)
756
png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
759
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
760
if ((png_ptr->transformations & PNG_BGR) != 0)
761
png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
764
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
765
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
766
png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
769
png_write_start_row(png_ptr);
772
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
774
if (png_ptr->interlaced != 0 &&
775
(png_ptr->transformations & PNG_INTERLACE) != 0)
777
switch (png_ptr->pass)
780
if ((png_ptr->row_number & 0x07) != 0)
782
png_write_finish_row(png_ptr);
788
if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
790
png_write_finish_row(png_ptr);
796
if ((png_ptr->row_number & 0x07) != 4)
798
png_write_finish_row(png_ptr);
804
if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
806
png_write_finish_row(png_ptr);
812
if ((png_ptr->row_number & 0x03) != 2)
814
png_write_finish_row(png_ptr);
820
if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
822
png_write_finish_row(png_ptr);
828
if ((png_ptr->row_number & 0x01) == 0)
830
png_write_finish_row(png_ptr);
842
row_info.color_type = png_ptr->color_type;
843
row_info.width = png_ptr->usr_width;
844
row_info.channels = png_ptr->usr_channels;
845
row_info.bit_depth = png_ptr->usr_bit_depth;
846
row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
847
row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
849
png_debug1(3, "row_info->color_type = %d", row_info.color_type);
850
png_debug1(3, "row_info->width = %u", row_info.width);
851
png_debug1(3, "row_info->channels = %d", row_info.channels);
852
png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
853
png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
854
png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
857
memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
859
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
861
if (png_ptr->interlaced && png_ptr->pass < 6 &&
862
(png_ptr->transformations & PNG_INTERLACE) != 0)
864
png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
866
if (row_info.width == 0)
868
png_write_finish_row(png_ptr);
874
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
876
if (png_ptr->transformations != 0)
877
png_do_write_transformations(png_ptr, &row_info);
883
if (row_info.pixel_depth != png_ptr->pixel_depth ||
884
row_info.pixel_depth != png_ptr->transformed_pixel_depth)
885
png_error(png_ptr, "internal write transform logic error");
887
#ifdef PNG_MNG_FEATURES_SUPPORTED
897
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
898
(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
901
png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
906
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
908
if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
909
png_ptr->num_palette_max >= 0)
910
png_do_check_palette_indexes(png_ptr, &row_info);
914
png_write_find_filter(png_ptr, &row_info);
916
if (png_ptr->write_row_fn != NULL)
917
(*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
920
#ifdef PNG_WRITE_FLUSH_SUPPORTED
923
png_set_flush(png_structrp png_ptr, int nrows)
925
png_debug(1, "in png_set_flush");
930
png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows);
935
png_write_flush(png_structrp png_ptr)
937
png_debug(1, "in png_write_flush");
943
if (png_ptr->row_number >= png_ptr->num_rows)
946
png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
947
png_ptr->flush_rows = 0;
954
png_write_destroy(png_structrp png_ptr)
956
png_debug(1, "in png_write_destroy");
959
if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
960
deflateEnd(&png_ptr->zstream);
963
png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
964
png_free(png_ptr, png_ptr->row_buf);
965
png_ptr->row_buf = NULL;
966
#ifdef PNG_WRITE_FILTER_SUPPORTED
967
png_free(png_ptr, png_ptr->prev_row);
968
png_free(png_ptr, png_ptr->try_row);
969
png_free(png_ptr, png_ptr->tst_row);
970
png_ptr->prev_row = NULL;
971
png_ptr->try_row = NULL;
972
png_ptr->tst_row = NULL;
975
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
976
png_free(png_ptr, png_ptr->chunk_list);
977
png_ptr->chunk_list = NULL;
994
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
996
png_debug(1, "in png_destroy_write_struct");
998
if (png_ptr_ptr != NULL)
1000
png_structrp png_ptr = *png_ptr_ptr;
1002
if (png_ptr != NULL)
1004
png_destroy_info_struct(png_ptr, info_ptr_ptr);
1006
*png_ptr_ptr = NULL;
1007
png_write_destroy(png_ptr);
1008
png_destroy_png_struct(png_ptr);
1015
png_set_filter(png_structrp png_ptr, int method, int filters)
1017
png_debug(1, "in png_set_filter");
1019
if (png_ptr == NULL)
1022
#ifdef PNG_MNG_FEATURES_SUPPORTED
1023
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
1024
(method == PNG_INTRAPIXEL_DIFFERENCING))
1025
method = PNG_FILTER_TYPE_BASE;
1028
if (method == PNG_FILTER_TYPE_BASE)
1030
switch (filters & (PNG_ALL_FILTERS | 0x07))
1032
#ifdef PNG_WRITE_FILTER_SUPPORTED
1035
case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
1038
case PNG_FILTER_VALUE_NONE:
1039
png_ptr->do_filter = PNG_FILTER_NONE; break;
1041
#ifdef PNG_WRITE_FILTER_SUPPORTED
1042
case PNG_FILTER_VALUE_SUB:
1043
png_ptr->do_filter = PNG_FILTER_SUB; break;
1045
case PNG_FILTER_VALUE_UP:
1046
png_ptr->do_filter = PNG_FILTER_UP; break;
1048
case PNG_FILTER_VALUE_AVG:
1049
png_ptr->do_filter = PNG_FILTER_AVG; break;
1051
case PNG_FILTER_VALUE_PAETH:
1052
png_ptr->do_filter = PNG_FILTER_PAETH; break;
1055
png_ptr->do_filter = (png_byte)filters; break;
1058
png_app_error(png_ptr, "Unknown row filter for method 0");
1062
#ifdef PNG_WRITE_FILTER_SUPPORTED
1076
if (png_ptr->row_buf != NULL)
1079
png_alloc_size_t buf_size;
1085
if (png_ptr->height == 1)
1086
filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1088
if (png_ptr->width == 1)
1089
filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1091
if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0
1092
&& png_ptr->prev_row == NULL)
1097
png_app_warning(png_ptr,
1098
"png_set_filter: UP/AVG/PAETH cannot be added after start");
1099
filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1104
if (filters & PNG_FILTER_SUB)
1107
if (filters & PNG_FILTER_UP)
1110
if (filters & PNG_FILTER_AVG)
1113
if (filters & PNG_FILTER_PAETH)
1119
buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,
1120
png_ptr->width) + 1;
1122
if (png_ptr->try_row == NULL)
1123
png_ptr->try_row = png_voidcast(png_bytep,
1124
png_malloc(png_ptr, buf_size));
1126
if (num_filters > 1)
1128
if (png_ptr->tst_row == NULL)
1129
png_ptr->tst_row = png_voidcast(png_bytep,
1130
png_malloc(png_ptr, buf_size));
1133
png_ptr->do_filter = (png_byte)filters;
1137
png_error(png_ptr, "Unknown custom filter method");
1140
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
1142
#ifdef PNG_FLOATING_POINT_SUPPORTED
1144
png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
1145
int num_weights, png_const_doublep filter_weights,
1146
png_const_doublep filter_costs)
1149
PNG_UNUSED(heuristic_method)
1150
PNG_UNUSED(num_weights)
1151
PNG_UNUSED(filter_weights)
1152
PNG_UNUSED(filter_costs)
1156
#ifdef PNG_FIXED_POINT_SUPPORTED
1158
png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
1159
int num_weights, png_const_fixed_point_p filter_weights,
1160
png_const_fixed_point_p filter_costs)
1163
PNG_UNUSED(heuristic_method)
1164
PNG_UNUSED(num_weights)
1165
PNG_UNUSED(filter_weights)
1166
PNG_UNUSED(filter_costs)
1171
#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
1173
png_set_compression_level(png_structrp png_ptr, int level)
1175
png_debug(1, "in png_set_compression_level");
1177
if (png_ptr == NULL)
1180
png_ptr->zlib_level = level;
1184
png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
1186
png_debug(1, "in png_set_compression_mem_level");
1188
if (png_ptr == NULL)
1191
png_ptr->zlib_mem_level = mem_level;
1195
png_set_compression_strategy(png_structrp png_ptr, int strategy)
1197
png_debug(1, "in png_set_compression_strategy");
1199
if (png_ptr == NULL)
1204
png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1205
png_ptr->zlib_strategy = strategy;
1212
png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
1214
png_debug(1, "in png_set_compression_window_bits");
1216
if (png_ptr == NULL)
1225
if (window_bits > 15)
1227
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1231
else if (window_bits < 8)
1233
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1237
png_ptr->zlib_window_bits = window_bits;
1241
png_set_compression_method(png_structrp png_ptr, int method)
1243
png_debug(1, "in png_set_compression_method");
1245
if (png_ptr == NULL)
1252
png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1254
png_ptr->zlib_method = method;
1259
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1261
png_set_text_compression_level(png_structrp png_ptr, int level)
1263
png_debug(1, "in png_set_text_compression_level");
1265
if (png_ptr == NULL)
1268
png_ptr->zlib_text_level = level;
1272
png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
1274
png_debug(1, "in png_set_text_compression_mem_level");
1276
if (png_ptr == NULL)
1279
png_ptr->zlib_text_mem_level = mem_level;
1283
png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
1285
png_debug(1, "in png_set_text_compression_strategy");
1287
if (png_ptr == NULL)
1290
png_ptr->zlib_text_strategy = strategy;
1297
png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
1299
png_debug(1, "in png_set_text_compression_window_bits");
1301
if (png_ptr == NULL)
1304
if (window_bits > 15)
1306
png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1310
else if (window_bits < 8)
1312
png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1316
png_ptr->zlib_text_window_bits = window_bits;
1320
png_set_text_compression_method(png_structrp png_ptr, int method)
1322
png_debug(1, "in png_set_text_compression_method");
1324
if (png_ptr == NULL)
1328
png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1330
png_ptr->zlib_text_method = method;
1336
png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
1338
png_debug(1, "in png_set_write_status_fn");
1340
if (png_ptr == NULL)
1343
png_ptr->write_row_fn = write_row_fn;
1346
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1348
png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
1349
write_user_transform_fn)
1351
png_debug(1, "in png_set_write_user_transform_fn");
1353
if (png_ptr == NULL)
1356
png_ptr->transformations |= PNG_USER_TRANSFORM;
1357
png_ptr->write_user_transform_fn = write_user_transform_fn;
1362
#ifdef PNG_INFO_IMAGE_SUPPORTED
1364
png_write_png(png_structrp png_ptr, png_inforp info_ptr,
1365
int transforms, voidp params)
1367
png_debug(1, "in png_write_png");
1369
if (png_ptr == NULL || info_ptr == NULL)
1372
if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
1374
png_app_error(png_ptr, "no rows for png_write_image to write");
1379
png_write_info(png_ptr, info_ptr);
1384
if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
1385
#ifdef PNG_WRITE_INVERT_SUPPORTED
1386
png_set_invert_mono(png_ptr);
1388
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
1394
if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
1395
#ifdef PNG_WRITE_SHIFT_SUPPORTED
1396
if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
1397
png_set_shift(png_ptr, &info_ptr->sig_bit);
1399
png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
1403
if ((transforms & PNG_TRANSFORM_PACKING) != 0)
1404
#ifdef PNG_WRITE_PACK_SUPPORTED
1405
png_set_packing(png_ptr);
1407
png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
1411
if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
1412
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1413
png_set_swap_alpha(png_ptr);
1415
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1422
if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
1423
PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
1425
#ifdef PNG_WRITE_FILLER_SUPPORTED
1426
if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
1428
if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
1429
png_app_error(png_ptr,
1430
"PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
1433
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
1436
else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
1437
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1439
png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
1444
if ((transforms & PNG_TRANSFORM_BGR) != 0)
1445
#ifdef PNG_WRITE_BGR_SUPPORTED
1446
png_set_bgr(png_ptr);
1448
png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
1452
if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
1453
#ifdef PNG_WRITE_SWAP_SUPPORTED
1454
png_set_swap(png_ptr);
1456
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1460
if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
1461
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1462
png_set_packswap(png_ptr);
1464
png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
1468
if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
1469
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1470
png_set_invert_alpha(png_ptr);
1472
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1478
png_write_image(png_ptr, info_ptr->row_pointers);
1481
png_write_end(png_ptr, info_ptr);
1488
#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1491
png_image_write_init(png_imagep image)
1493
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
1494
png_safe_error, png_safe_warning);
1496
if (png_ptr != NULL)
1498
png_infop info_ptr = png_create_info_struct(png_ptr);
1500
if (info_ptr != NULL)
1502
png_controlp control = png_voidcast(png_controlp,
1503
png_malloc_warn(png_ptr, (sizeof *control)));
1505
if (control != NULL)
1507
memset(control, 0, (sizeof *control));
1509
control->png_ptr = png_ptr;
1510
control->info_ptr = info_ptr;
1511
control->for_write = 1;
1513
image->opaque = control;
1518
png_destroy_info_struct(png_ptr, &info_ptr);
1521
png_destroy_write_struct(&png_ptr, NULL);
1524
return png_image_error(image, "png_image_write_: out of memory");
1532
png_const_voidp buffer;
1533
png_int_32 row_stride;
1534
png_const_voidp colormap;
1535
int convert_to_8bit;
1537
png_const_voidp first_row;
1538
ptrdiff_t row_bytes;
1539
png_voidp local_row;
1542
png_alloc_size_t memory_bytes;
1543
png_alloc_size_t output_bytes;
1544
} png_image_write_control;
1551
png_write_image_16bit(png_voidp argument)
1553
png_image_write_control *display = png_voidcast(png_image_write_control*,
1555
png_imagep image = display->image;
1556
png_structrp png_ptr = image->opaque->png_ptr;
1558
png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1559
display->first_row);
1560
png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
1561
png_uint_16p row_end;
1562
unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
1565
png_uint_32 y = image->height;
1567
if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
1569
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1570
if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1577
aindex = (int)channels;
1579
aindex = (int)channels;
1584
png_error(png_ptr, "png_write_image: internal call error");
1590
row_end = output_row + image->width * (channels+1);
1594
png_const_uint_16p in_ptr = input_row;
1595
png_uint_16p out_ptr = output_row;
1597
while (out_ptr < row_end)
1599
png_uint_16 alpha = in_ptr[aindex];
1600
png_uint_32 reciprocal = 0;
1603
out_ptr[aindex] = alpha;
1610
if (alpha > 0 && alpha < 65535)
1611
reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
1616
png_uint_16 component = *in_ptr++;
1625
if (component >= alpha)
1631
else if (component > 0 && alpha < 65535)
1633
png_uint_32 calc = component * reciprocal;
1635
component = (png_uint_16)(calc >> 15);
1638
*out_ptr++ = component;
1647
png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
1648
input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
1662
# define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+((alpha)>>1))/(alpha))
1665
png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
1666
png_uint_32 reciprocal)
1678
if (component >= alpha || alpha < 128)
1684
else if (component > 0)
1693
component *= reciprocal;
1702
return (png_byte)PNG_sRGB_FROM_LINEAR(component);
1710
png_write_image_8bit(png_voidp argument)
1712
png_image_write_control *display = png_voidcast(png_image_write_control*,
1714
png_imagep image = display->image;
1715
png_structrp png_ptr = image->opaque->png_ptr;
1717
png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1718
display->first_row);
1719
png_bytep output_row = png_voidcast(png_bytep, display->local_row);
1720
png_uint_32 y = image->height;
1721
unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
1724
if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
1729
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1730
if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1739
aindex = (int)channels;
1742
row_end = output_row + image->width * (channels+1);
1746
png_const_uint_16p in_ptr = input_row;
1747
png_bytep out_ptr = output_row;
1749
while (out_ptr < row_end)
1751
png_uint_16 alpha = in_ptr[aindex];
1752
png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1753
png_uint_32 reciprocal = 0;
1757
out_ptr[aindex] = alphabyte;
1759
if (alphabyte > 0 && alphabyte < 255)
1760
reciprocal = UNP_RECIPROCAL(alpha);
1764
*out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
1772
png_write_row(png_ptr, png_voidcast(png_const_bytep,
1773
display->local_row));
1774
input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
1783
png_bytep row_end = output_row + image->width * channels;
1787
png_const_uint_16p in_ptr = input_row;
1788
png_bytep out_ptr = output_row;
1790
while (out_ptr < row_end)
1792
png_uint_32 component = *in_ptr++;
1795
*out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1798
png_write_row(png_ptr, output_row);
1799
input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
1807
png_image_set_PLTE(png_image_write_control *display)
1809
png_imagep image = display->image;
1810
const void *cmap = display->colormap;
1811
int entries = image->colormap_entries > 256 ? 256 :
1812
(int)image->colormap_entries;
1815
png_uint_32 format = image->format;
1816
unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
1818
# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
1819
defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
1820
int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1821
(format & PNG_FORMAT_FLAG_ALPHA) != 0;
1826
# ifdef PNG_FORMAT_BGR_SUPPORTED
1827
int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1833
png_color palette[256];
1836
memset(tRNS, 255, (sizeof tRNS));
1837
memset(palette, 0, (sizeof palette));
1839
for (i=num_trans=0; i<entries; ++i)
1844
if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
1846
png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
1848
entry += (unsigned int)i * channels;
1850
if ((channels & 1) != 0)
1854
palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1856
palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1858
palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1863
palette[i].blue = palette[i].red = palette[i].green =
1864
(png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
1869
png_uint_16 alpha = entry[afirst ? 0 : channels-1];
1870
png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1871
png_uint_32 reciprocal = 0;
1877
if (alphabyte > 0 && alphabyte < 255)
1878
reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
1880
tRNS[i] = alphabyte;
1881
if (alphabyte < 255)
1886
palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
1888
palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
1890
palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
1895
palette[i].blue = palette[i].red = palette[i].green =
1896
png_unpremultiply(entry[afirst], alpha, reciprocal);
1902
png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
1904
entry += (unsigned int)i * channels;
1909
tRNS[i] = entry[afirst ? 0 : 3];
1914
palette[i].blue = entry[afirst + (2 ^ bgr)];
1915
palette[i].green = entry[afirst + 1];
1916
palette[i].red = entry[afirst + bgr];
1920
tRNS[i] = entry[1 ^ afirst];
1925
palette[i].blue = palette[i].red = palette[i].green =
1942
png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
1946
png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
1949
image->colormap_entries = (png_uint_32)entries;
1953
png_image_write_main(png_voidp argument)
1955
png_image_write_control *display = png_voidcast(png_image_write_control*,
1957
png_imagep image = display->image;
1958
png_structrp png_ptr = image->opaque->png_ptr;
1959
png_inforp info_ptr = image->opaque->info_ptr;
1960
png_uint_32 format = image->format;
1963
int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
1964
int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR);
1965
int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
1966
int write_16bit = linear && (display->convert_to_8bit == 0);
1968
# ifdef PNG_BENIGN_ERRORS_SUPPORTED
1970
png_set_benign_errors(png_ptr, 0);
1977
unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
1979
if (image->width <= 0x7fffffffU/channels)
1982
png_uint_32 png_row_stride = image->width * channels;
1984
if (display->row_stride == 0)
1985
display->row_stride = (png_int_32)png_row_stride;
1987
if (display->row_stride < 0)
1988
check = (png_uint_32)(-display->row_stride);
1991
check = (png_uint_32)display->row_stride;
1993
if (check >= png_row_stride)
1999
if (image->height > 0xffffffffU/png_row_stride)
2000
png_error(image->opaque->png_ptr, "memory image too large");
2004
png_error(image->opaque->png_ptr, "supplied row stride too small");
2008
png_error(image->opaque->png_ptr, "image row stride too large");
2012
if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
2014
if (display->colormap != NULL && image->colormap_entries > 0)
2016
png_uint_32 entries = image->colormap_entries;
2018
png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2019
entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
2020
PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
2021
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2023
png_image_set_PLTE(display);
2027
png_error(image->opaque->png_ptr,
2028
"no color-map for color-mapped image");
2032
png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2033
write_16bit ? 16 : 8,
2034
((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
2035
((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
2036
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2044
if (write_16bit != 0)
2047
png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
2049
if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
2050
png_set_cHRM_fixed(png_ptr, info_ptr,
2059
else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
2060
png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
2066
png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
2069
png_write_info(png_ptr, info_ptr);
2076
if (write_16bit != 0)
2078
png_uint_16 le = 0x0001;
2080
if ((*(png_const_bytep) & le) != 0)
2081
png_set_swap(png_ptr);
2084
# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2085
if ((format & PNG_FORMAT_FLAG_BGR) != 0)
2087
if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
2088
png_set_bgr(png_ptr);
2089
format &= ~PNG_FORMAT_FLAG_BGR;
2093
# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2094
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
2096
if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
2097
png_set_swap_alpha(png_ptr);
2098
format &= ~PNG_FORMAT_FLAG_AFIRST;
2105
if (colormap != 0 && image->colormap_entries <= 16)
2106
png_set_packing(png_ptr);
2109
if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
2110
PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
2111
png_error(png_ptr, "png_write_image: unsupported transformation");
2114
png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
2115
ptrdiff_t row_bytes = display->row_stride;
2118
row_bytes *= (sizeof (png_uint_16));
2121
row += (image->height-1) * (-row_bytes);
2123
display->first_row = row;
2124
display->row_bytes = row_bytes;
2128
if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
2130
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
2136
# ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
2137
png_set_compression_level(png_ptr, 3);
2145
if ((linear != 0 && alpha != 0 ) ||
2146
(colormap == 0 && display->convert_to_8bit != 0))
2148
png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2149
png_get_rowbytes(png_ptr, info_ptr)));
2152
display->local_row = row;
2153
if (write_16bit != 0)
2154
result = png_safe_execute(image, png_write_image_16bit, display);
2156
result = png_safe_execute(image, png_write_image_8bit, display);
2157
display->local_row = NULL;
2159
png_free(png_ptr, row);
2171
png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
2172
ptrdiff_t row_bytes = display->row_bytes;
2173
png_uint_32 y = image->height;
2177
png_write_row(png_ptr, row);
2182
png_write_end(png_ptr, info_ptr);
2187
static void (PNGCBAPI
2188
image_memory_write)(png_structp png_ptr, png_bytep data, size_t size)
2190
png_image_write_control *display = png_voidcast(png_image_write_control*,
2192
png_alloc_size_t ob = display->output_bytes;
2195
if (size <= ((png_alloc_size_t)-1) - ob)
2200
if (display->memory_bytes >= ob+size)
2201
memcpy(display->memory+ob, data, size);
2204
display->output_bytes = ob+size;
2209
png_error(png_ptr, "png_image_write_to_memory: PNG too big");
2212
static void (PNGCBAPI
2213
image_memory_flush)(png_structp png_ptr)
2219
png_image_write_memory(png_voidp argument)
2221
png_image_write_control *display = png_voidcast(png_image_write_control*,
2228
png_set_write_fn(display->image->opaque->png_ptr, display,
2229
image_memory_write, image_memory_flush);
2231
return png_image_write_main(display);
2235
png_image_write_to_memory(png_imagep image, void *memory,
2236
png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit,
2237
const void *buffer, png_int_32 row_stride, const void *colormap)
2240
if (image != NULL && image->version == PNG_IMAGE_VERSION)
2242
if (memory_bytes != NULL && buffer != NULL)
2250
if (png_image_write_init(image) != 0)
2252
png_image_write_control display;
2255
memset(&display, 0, (sizeof display));
2256
display.image = image;
2257
display.buffer = buffer;
2258
display.row_stride = row_stride;
2259
display.colormap = colormap;
2260
display.convert_to_8bit = convert_to_8bit;
2261
display.memory = png_voidcast(png_bytep, memory);
2262
display.memory_bytes = *memory_bytes;
2263
display.output_bytes = 0;
2265
result = png_safe_execute(image, png_image_write_memory, &display);
2266
png_image_free(image);
2274
if (memory != NULL && display.output_bytes > *memory_bytes)
2277
*memory_bytes = display.output_bytes;
2288
return png_image_error(image,
2289
"png_image_write_to_memory: invalid argument");
2292
else if (image != NULL)
2293
return png_image_error(image,
2294
"png_image_write_to_memory: incorrect PNG_IMAGE_VERSION");
2300
#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
2302
png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
2303
const void *buffer, png_int_32 row_stride, const void *colormap)
2306
if (image != NULL && image->version == PNG_IMAGE_VERSION)
2308
if (file != NULL && buffer != NULL)
2310
if (png_image_write_init(image) != 0)
2312
png_image_write_control display;
2319
image->opaque->png_ptr->io_ptr = file;
2321
memset(&display, 0, (sizeof display));
2322
display.image = image;
2323
display.buffer = buffer;
2324
display.row_stride = row_stride;
2325
display.colormap = colormap;
2326
display.convert_to_8bit = convert_to_8bit;
2328
result = png_safe_execute(image, png_image_write_main, &display);
2329
png_image_free(image);
2338
return png_image_error(image,
2339
"png_image_write_to_stdio: invalid argument");
2342
else if (image != NULL)
2343
return png_image_error(image,
2344
"png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
2351
png_image_write_to_file(png_imagep image, const char *file_name,
2352
int convert_to_8bit, const void *buffer, png_int_32 row_stride,
2353
const void *colormap)
2356
if (image != NULL && image->version == PNG_IMAGE_VERSION)
2358
if (file_name != NULL && buffer != NULL)
2360
FILE *fp = fopen(file_name, "wb");
2364
if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
2365
row_stride, colormap) != 0)
2370
if (fflush(fp) == 0 && ferror(fp) == 0)
2372
if (fclose(fp) == 0)
2384
(void)remove(file_name);
2388
return png_image_error(image, strerror(error));
2395
(void)remove(file_name);
2401
return png_image_error(image, strerror(errno));
2405
return png_image_error(image,
2406
"png_image_write_to_file: invalid argument");
2409
else if (image != NULL)
2410
return png_image_error(image,
2411
"png_image_write_to_file: incorrect PNG_IMAGE_VERSION");