michael@0: michael@0: /* pngwrite.c - general routines to write a PNG file michael@0: * michael@0: * Last changed in libpng 1.6.10 [March 6, 2014] michael@0: * Copyright (c) 1998-2014 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: #if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) michael@0: # include michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_SUPPORTED michael@0: michael@0: #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED michael@0: /* Write out all the unknown chunks for the current given location */ michael@0: static void michael@0: write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr, michael@0: unsigned int where) michael@0: { michael@0: if (info_ptr->unknown_chunks_num) michael@0: { michael@0: png_const_unknown_chunkp up; michael@0: michael@0: png_debug(5, "writing extra chunks"); michael@0: michael@0: for (up = info_ptr->unknown_chunks; michael@0: up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; michael@0: ++up) michael@0: if (up->location & where) michael@0: { michael@0: /* If per-chunk unknown chunk handling is enabled use it, otherwise michael@0: * just write the chunks the application has set. michael@0: */ michael@0: #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED michael@0: int keep = png_handle_as_unknown(png_ptr, up->name); michael@0: michael@0: /* NOTE: this code is radically different from the read side in the michael@0: * matter of handling an ancillary unknown chunk. In the read side michael@0: * the default behavior is to discard it, in the code below the default michael@0: * behavior is to write it. Critical chunks are, however, only michael@0: * written if explicitly listed or if the default is set to write all michael@0: * unknown chunks. michael@0: * michael@0: * The default handling is also slightly weird - it is not possible to michael@0: * stop the writing of all unsafe-to-copy chunks! michael@0: * michael@0: * TODO: REVIEW: this would seem to be a bug. michael@0: */ michael@0: if (keep != PNG_HANDLE_CHUNK_NEVER && michael@0: ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ || michael@0: keep == PNG_HANDLE_CHUNK_ALWAYS || michael@0: (keep == PNG_HANDLE_CHUNK_AS_DEFAULT && michael@0: png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS))) michael@0: #endif michael@0: { michael@0: /* TODO: review, what is wrong with a zero length unknown chunk? */ michael@0: if (up->size == 0) michael@0: png_warning(png_ptr, "Writing zero-length unknown chunk"); michael@0: michael@0: png_write_chunk(png_ptr, up->name, up->data, up->size); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif /* PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED */ michael@0: michael@0: /* Writes all the PNG information. This is the suggested way to use the michael@0: * library. If you have a new chunk to add, make a function to write it, michael@0: * and put it in the correct location here. If you want the chunk written michael@0: * after the image data, put it in png_write_end(). I strongly encourage michael@0: * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing michael@0: * the chunk, as that will keep the code from breaking if you want to just michael@0: * write a plain PNG file. If you have long comments, I suggest writing michael@0: * them in png_write_end(), and compressing them. michael@0: */ michael@0: void PNGAPI michael@0: png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr) michael@0: { michael@0: png_debug(1, "in png_write_info_before_PLTE"); michael@0: michael@0: if (png_ptr == NULL || info_ptr == NULL) michael@0: return; michael@0: michael@0: if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) michael@0: { michael@0: /* Write PNG signature */ michael@0: png_write_sig(png_ptr); michael@0: michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \ michael@0: (png_ptr->mng_features_permitted)) michael@0: { michael@0: png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); michael@0: png_ptr->mng_features_permitted = 0; michael@0: } michael@0: #endif michael@0: michael@0: /* Write IHDR information. */ michael@0: png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, michael@0: info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, michael@0: info_ptr->filter_type, michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: info_ptr->interlace_type michael@0: #else michael@0: 0 michael@0: #endif michael@0: ); michael@0: michael@0: /* The rest of these check to see if the valid field has the appropriate michael@0: * flag set, and if it does, writes the chunk. michael@0: * michael@0: * 1.6.0: COLORSPACE support controls the writing of these chunks too, and michael@0: * the chunks will be written if the WRITE routine is there and information michael@0: * is available in the COLORSPACE. (See png_colorspace_sync_info in png.c michael@0: * for where the valid flags get set.) michael@0: * michael@0: * Under certain circumstances the colorspace can be invalidated without michael@0: * syncing the info_struct 'valid' flags; this happens if libpng detects and michael@0: * error and calls png_error while the color space is being set, yet the michael@0: * application continues writing the PNG. So check the 'invalid' flag here michael@0: * too. michael@0: */ michael@0: #ifdef PNG_WRITE_APNG_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_acTL) michael@0: png_write_acTL(png_ptr, info_ptr->num_frames, info_ptr->num_plays); michael@0: #endif michael@0: #ifdef PNG_GAMMA_SUPPORTED michael@0: # ifdef PNG_WRITE_gAMA_SUPPORTED michael@0: if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) && michael@0: (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) && michael@0: (info_ptr->valid & PNG_INFO_gAMA)) michael@0: png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma); michael@0: # endif michael@0: #endif michael@0: michael@0: #ifdef PNG_COLORSPACE_SUPPORTED michael@0: /* Write only one of sRGB or an ICC profile. If a profile was supplied michael@0: * and it matches one of the known sRGB ones issue a warning. michael@0: */ michael@0: # ifdef PNG_WRITE_iCCP_SUPPORTED michael@0: if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) && michael@0: (info_ptr->valid & PNG_INFO_iCCP)) michael@0: { michael@0: # ifdef PNG_WRITE_sRGB_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_sRGB) michael@0: png_app_warning(png_ptr, michael@0: "profile matches sRGB but writing iCCP instead"); michael@0: # endif michael@0: michael@0: png_write_iCCP(png_ptr, info_ptr->iccp_name, michael@0: info_ptr->iccp_profile); michael@0: } michael@0: # ifdef PNG_WRITE_sRGB_SUPPORTED michael@0: else michael@0: # endif michael@0: # endif michael@0: michael@0: # ifdef PNG_WRITE_sRGB_SUPPORTED michael@0: if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) && michael@0: (info_ptr->valid & PNG_INFO_sRGB)) michael@0: png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent); michael@0: # endif /* WRITE_sRGB */ michael@0: #endif /* COLORSPACE */ michael@0: michael@0: #ifdef PNG_WRITE_sBIT_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_sBIT) michael@0: png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); michael@0: #endif michael@0: michael@0: #ifdef PNG_COLORSPACE_SUPPORTED michael@0: # ifdef PNG_WRITE_cHRM_SUPPORTED michael@0: if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) && michael@0: (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) && michael@0: (info_ptr->valid & PNG_INFO_cHRM)) michael@0: png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy); michael@0: # endif michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED michael@0: write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR); michael@0: #endif michael@0: michael@0: png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; michael@0: } michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_write_info(png_structrp png_ptr, png_const_inforp info_ptr) michael@0: { michael@0: #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) michael@0: int i; michael@0: #endif michael@0: michael@0: png_debug(1, "in png_write_info"); michael@0: michael@0: if (png_ptr == NULL || info_ptr == NULL) michael@0: return; michael@0: michael@0: png_write_info_before_PLTE(png_ptr, info_ptr); michael@0: michael@0: if (info_ptr->valid & PNG_INFO_PLTE) michael@0: png_write_PLTE(png_ptr, info_ptr->palette, michael@0: (png_uint_32)info_ptr->num_palette); michael@0: michael@0: else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: png_error(png_ptr, "Valid palette required for paletted images"); michael@0: michael@0: #ifdef PNG_WRITE_tRNS_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_tRNS) michael@0: { michael@0: #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED michael@0: /* Invert the alpha channel (in tRNS) */ michael@0: if ((png_ptr->transformations & PNG_INVERT_ALPHA) && michael@0: info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: int j; michael@0: for (j = 0; j<(int)info_ptr->num_trans; j++) michael@0: info_ptr->trans_alpha[j] = michael@0: (png_byte)(255 - info_ptr->trans_alpha[j]); michael@0: } michael@0: #endif michael@0: png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color), michael@0: info_ptr->num_trans, info_ptr->color_type); michael@0: } michael@0: #endif michael@0: #ifdef PNG_WRITE_bKGD_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_bKGD) michael@0: png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_hIST_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_hIST) michael@0: png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_oFFs_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_oFFs) michael@0: png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, michael@0: info_ptr->offset_unit_type); michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_pCAL_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_pCAL) michael@0: png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, michael@0: info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, michael@0: info_ptr->pcal_units, info_ptr->pcal_params); michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_sCAL_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_sCAL) michael@0: png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, michael@0: info_ptr->scal_s_width, info_ptr->scal_s_height); michael@0: #endif /* sCAL */ michael@0: michael@0: #ifdef PNG_WRITE_pHYs_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_pHYs) michael@0: png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, michael@0: info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); michael@0: #endif /* pHYs */ michael@0: michael@0: #ifdef PNG_WRITE_tIME_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_tIME) michael@0: { michael@0: png_write_tIME(png_ptr, &(info_ptr->mod_time)); michael@0: png_ptr->mode |= PNG_WROTE_tIME; michael@0: } michael@0: #endif /* tIME */ michael@0: michael@0: #ifdef PNG_WRITE_sPLT_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_sPLT) michael@0: for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) michael@0: png_write_sPLT(png_ptr, info_ptr->splt_palettes + i); michael@0: #endif /* sPLT */ michael@0: michael@0: #ifdef PNG_WRITE_TEXT_SUPPORTED michael@0: /* Check to see if we need to write text chunks */ michael@0: for (i = 0; i < info_ptr->num_text; i++) michael@0: { michael@0: png_debug2(2, "Writing header text chunk %d, type %d", i, michael@0: info_ptr->text[i].compression); michael@0: /* An internationalized chunk? */ michael@0: if (info_ptr->text[i].compression > 0) michael@0: { michael@0: #ifdef PNG_WRITE_iTXt_SUPPORTED michael@0: /* Write international chunk */ michael@0: png_write_iTXt(png_ptr, michael@0: info_ptr->text[i].compression, michael@0: info_ptr->text[i].key, michael@0: info_ptr->text[i].lang, michael@0: info_ptr->text[i].lang_key, michael@0: info_ptr->text[i].text); michael@0: #else michael@0: png_warning(png_ptr, "Unable to write international text"); michael@0: #endif michael@0: /* Mark this chunk as written */ michael@0: info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; michael@0: } michael@0: michael@0: /* If we want a compressed text chunk */ michael@0: else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt) michael@0: { michael@0: #ifdef PNG_WRITE_zTXt_SUPPORTED michael@0: /* Write compressed chunk */ michael@0: png_write_zTXt(png_ptr, info_ptr->text[i].key, michael@0: info_ptr->text[i].text, 0, michael@0: info_ptr->text[i].compression); michael@0: #else michael@0: png_warning(png_ptr, "Unable to write compressed text"); michael@0: #endif michael@0: /* Mark this chunk as written */ michael@0: info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; michael@0: } michael@0: michael@0: else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) michael@0: { michael@0: #ifdef PNG_WRITE_tEXt_SUPPORTED michael@0: /* Write uncompressed chunk */ michael@0: png_write_tEXt(png_ptr, info_ptr->text[i].key, michael@0: info_ptr->text[i].text, michael@0: 0); michael@0: /* Mark this chunk as written */ michael@0: info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; michael@0: #else michael@0: /* Can't get here */ michael@0: png_warning(png_ptr, "Unable to write uncompressed text"); michael@0: #endif michael@0: } michael@0: } michael@0: #endif /* tEXt */ michael@0: michael@0: #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED michael@0: write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE); michael@0: #endif michael@0: } michael@0: michael@0: /* Writes the end of the PNG file. If you don't want to write comments or michael@0: * time information, you can pass NULL for info. If you already wrote these michael@0: * in png_write_info(), do not write them again here. If you have long michael@0: * comments, I suggest writing them here, and compressing them. michael@0: */ michael@0: void PNGAPI michael@0: png_write_end(png_structrp png_ptr, png_inforp info_ptr) michael@0: { michael@0: png_debug(1, "in png_write_end"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: if (!(png_ptr->mode & PNG_HAVE_IDAT)) michael@0: png_error(png_ptr, "No IDATs written into file"); michael@0: michael@0: #ifdef PNG_WRITE_APNG_SUPPORTED michael@0: if (png_ptr->num_frames_written != png_ptr->num_frames_to_write) michael@0: png_error(png_ptr, "Not enough frames written"); michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED michael@0: if (png_ptr->num_palette_max > png_ptr->num_palette) michael@0: png_benign_error(png_ptr, "Wrote palette index exceeding num_palette"); michael@0: #endif michael@0: michael@0: /* See if user wants us to write information chunks */ michael@0: if (info_ptr != NULL) michael@0: { michael@0: #ifdef PNG_WRITE_TEXT_SUPPORTED michael@0: int i; /* local index variable */ michael@0: #endif michael@0: #ifdef PNG_WRITE_tIME_SUPPORTED michael@0: /* Check to see if user has supplied a time chunk */ michael@0: if ((info_ptr->valid & PNG_INFO_tIME) && michael@0: !(png_ptr->mode & PNG_WROTE_tIME)) michael@0: png_write_tIME(png_ptr, &(info_ptr->mod_time)); michael@0: michael@0: #endif michael@0: #ifdef PNG_WRITE_TEXT_SUPPORTED michael@0: /* Loop through comment chunks */ michael@0: for (i = 0; i < info_ptr->num_text; i++) michael@0: { michael@0: png_debug2(2, "Writing trailer text chunk %d, type %d", i, michael@0: info_ptr->text[i].compression); michael@0: /* An internationalized chunk? */ michael@0: if (info_ptr->text[i].compression > 0) michael@0: { michael@0: #ifdef PNG_WRITE_iTXt_SUPPORTED michael@0: /* Write international chunk */ michael@0: png_write_iTXt(png_ptr, michael@0: info_ptr->text[i].compression, michael@0: info_ptr->text[i].key, michael@0: info_ptr->text[i].lang, michael@0: info_ptr->text[i].lang_key, michael@0: info_ptr->text[i].text); michael@0: #else michael@0: png_warning(png_ptr, "Unable to write international text"); michael@0: #endif michael@0: /* Mark this chunk as written */ michael@0: info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; michael@0: } michael@0: michael@0: else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) michael@0: { michael@0: #ifdef PNG_WRITE_zTXt_SUPPORTED michael@0: /* Write compressed chunk */ michael@0: png_write_zTXt(png_ptr, info_ptr->text[i].key, michael@0: info_ptr->text[i].text, 0, michael@0: info_ptr->text[i].compression); michael@0: #else michael@0: png_warning(png_ptr, "Unable to write compressed text"); michael@0: #endif michael@0: /* Mark this chunk as written */ michael@0: info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; michael@0: } michael@0: michael@0: else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) michael@0: { michael@0: #ifdef PNG_WRITE_tEXt_SUPPORTED michael@0: /* Write uncompressed chunk */ michael@0: png_write_tEXt(png_ptr, info_ptr->text[i].key, michael@0: info_ptr->text[i].text, 0); michael@0: #else michael@0: png_warning(png_ptr, "Unable to write uncompressed text"); michael@0: #endif michael@0: michael@0: /* Mark this chunk as written */ michael@0: info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED michael@0: write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT); michael@0: #endif michael@0: } michael@0: michael@0: png_ptr->mode |= PNG_AFTER_IDAT; michael@0: michael@0: /* Write end of PNG file */ michael@0: png_write_IEND(png_ptr); michael@0: /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03, michael@0: * and restored again in libpng-1.2.30, may cause some applications that michael@0: * do not set png_ptr->output_flush_fn to crash. If your application michael@0: * experiences a problem, please try building libpng with michael@0: * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to michael@0: * png-mng-implement at lists.sf.net . michael@0: */ michael@0: #ifdef PNG_WRITE_FLUSH_SUPPORTED michael@0: # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED michael@0: png_flush(png_ptr); michael@0: # endif michael@0: #endif michael@0: } michael@0: michael@0: #ifdef PNG_CONVERT_tIME_SUPPORTED michael@0: void PNGAPI michael@0: png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime) michael@0: { michael@0: png_debug(1, "in png_convert_from_struct_tm"); michael@0: michael@0: ptime->year = (png_uint_16)(1900 + ttime->tm_year); michael@0: ptime->month = (png_byte)(ttime->tm_mon + 1); michael@0: ptime->day = (png_byte)ttime->tm_mday; michael@0: ptime->hour = (png_byte)ttime->tm_hour; michael@0: ptime->minute = (png_byte)ttime->tm_min; michael@0: ptime->second = (png_byte)ttime->tm_sec; michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_convert_from_time_t(png_timep ptime, time_t ttime) michael@0: { michael@0: struct tm *tbuf; michael@0: michael@0: png_debug(1, "in png_convert_from_time_t"); michael@0: michael@0: tbuf = gmtime(&ttime); michael@0: png_convert_from_struct_tm(ptime, tbuf); michael@0: } michael@0: #endif michael@0: michael@0: /* Initialize png_ptr structure, and allocate any memory needed */ michael@0: PNG_FUNCTION(png_structp,PNGAPI michael@0: png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr, michael@0: png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) michael@0: { michael@0: #ifndef PNG_USER_MEM_SUPPORTED michael@0: png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, michael@0: error_fn, warn_fn, NULL, NULL, NULL); michael@0: #else michael@0: return png_create_write_struct_2(user_png_ver, error_ptr, error_fn, michael@0: warn_fn, NULL, NULL, NULL); michael@0: } michael@0: michael@0: /* Alternate initialize png_ptr structure, and allocate any memory needed */ michael@0: PNG_FUNCTION(png_structp,PNGAPI michael@0: png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, michael@0: png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, michael@0: png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) michael@0: { michael@0: png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, michael@0: error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); michael@0: #endif /* PNG_USER_MEM_SUPPORTED */ michael@0: if (png_ptr != NULL) michael@0: { michael@0: /* Set the zlib control values to defaults; they can be overridden by the michael@0: * application after the struct has been created. michael@0: */ michael@0: png_ptr->zbuffer_size = PNG_ZBUF_SIZE; michael@0: michael@0: /* The 'zlib_strategy' setting is irrelevant because png_default_claim in michael@0: * pngwutil.c defaults it according to whether or not filters will be michael@0: * used, and ignores this setting. michael@0: */ michael@0: png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY; michael@0: png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION; michael@0: png_ptr->zlib_mem_level = 8; michael@0: png_ptr->zlib_window_bits = 15; michael@0: png_ptr->zlib_method = 8; michael@0: michael@0: #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED michael@0: png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY; michael@0: png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION; michael@0: png_ptr->zlib_text_mem_level = 8; michael@0: png_ptr->zlib_text_window_bits = 15; michael@0: png_ptr->zlib_text_method = 8; michael@0: #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ michael@0: michael@0: /* This is a highly dubious configuration option; by default it is off, michael@0: * but it may be appropriate for private builds that are testing michael@0: * extensions not conformant to the current specification, or of michael@0: * applications that must not fail to write at all costs! michael@0: */ michael@0: #ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED michael@0: png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; michael@0: /* In stable builds only warn if an application error can be completely michael@0: * handled. michael@0: */ michael@0: #endif michael@0: michael@0: /* App warnings are warnings in release (or release candidate) builds but michael@0: * are errors during development. michael@0: */ michael@0: #if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC michael@0: png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; michael@0: #endif michael@0: michael@0: /* TODO: delay this, it can be done in png_init_io() (if the app doesn't michael@0: * do it itself) avoiding setting the default function if it is not michael@0: * required. michael@0: */ michael@0: png_set_write_fn(png_ptr, NULL, NULL, NULL); michael@0: } michael@0: michael@0: return png_ptr; michael@0: } michael@0: michael@0: michael@0: /* Write a few rows of image data. If the image is interlaced, michael@0: * either you will have to write the 7 sub images, or, if you michael@0: * have called png_set_interlace_handling(), you will have to michael@0: * "write" the image seven times. michael@0: */ michael@0: void PNGAPI michael@0: png_write_rows(png_structrp png_ptr, png_bytepp row, michael@0: png_uint_32 num_rows) michael@0: { michael@0: png_uint_32 i; /* row counter */ michael@0: png_bytepp rp; /* row pointer */ michael@0: michael@0: png_debug(1, "in png_write_rows"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* Loop through the rows */ michael@0: for (i = 0, rp = row; i < num_rows; i++, rp++) michael@0: { michael@0: png_write_row(png_ptr, *rp); michael@0: } michael@0: } michael@0: michael@0: /* Write the image. You only need to call this function once, even michael@0: * if you are writing an interlaced image. michael@0: */ michael@0: void PNGAPI michael@0: png_write_image(png_structrp png_ptr, png_bytepp image) michael@0: { michael@0: png_uint_32 i; /* row index */ michael@0: int pass, num_pass; /* pass variables */ michael@0: png_bytepp rp; /* points to current row */ michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_debug(1, "in png_write_image"); michael@0: michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* Initialize interlace handling. If image is not interlaced, michael@0: * this will set pass to 1 michael@0: */ michael@0: num_pass = png_set_interlace_handling(png_ptr); michael@0: #else michael@0: num_pass = 1; michael@0: #endif michael@0: /* Loop through passes */ michael@0: for (pass = 0; pass < num_pass; pass++) michael@0: { michael@0: /* Loop through image */ michael@0: for (i = 0, rp = image; i < png_ptr->height; i++, rp++) michael@0: { michael@0: png_write_row(png_ptr, *rp); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: /* Performs intrapixel differencing */ michael@0: static void michael@0: png_do_write_intrapixel(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_debug(1, "in png_do_write_intrapixel"); michael@0: michael@0: if ((row_info->color_type & PNG_COLOR_MASK_COLOR)) michael@0: { michael@0: int bytes_per_pixel; michael@0: png_uint_32 row_width = row_info->width; michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: png_bytep rp; michael@0: png_uint_32 i; michael@0: michael@0: if (row_info->color_type == PNG_COLOR_TYPE_RGB) michael@0: bytes_per_pixel = 3; michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: bytes_per_pixel = 4; michael@0: michael@0: else michael@0: return; michael@0: michael@0: for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) michael@0: { michael@0: *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff); michael@0: *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff); michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_16BIT_SUPPORTED michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: png_bytep rp; michael@0: png_uint_32 i; michael@0: michael@0: if (row_info->color_type == PNG_COLOR_TYPE_RGB) michael@0: bytes_per_pixel = 6; michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: bytes_per_pixel = 8; michael@0: michael@0: else michael@0: return; michael@0: michael@0: for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) michael@0: { michael@0: png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); michael@0: png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); michael@0: png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); michael@0: png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); michael@0: png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); michael@0: *(rp ) = (png_byte)((red >> 8) & 0xff); michael@0: *(rp + 1) = (png_byte)(red & 0xff); michael@0: *(rp + 4) = (png_byte)((blue >> 8) & 0xff); michael@0: *(rp + 5) = (png_byte)(blue & 0xff); michael@0: } michael@0: } michael@0: #endif /* PNG_WRITE_16BIT_SUPPORTED */ michael@0: } michael@0: } michael@0: #endif /* PNG_MNG_FEATURES_SUPPORTED */ michael@0: michael@0: /* Called by user to write a row of image data */ michael@0: void PNGAPI michael@0: png_write_row(png_structrp png_ptr, png_const_bytep row) michael@0: { michael@0: /* 1.5.6: moved from png_struct to be a local structure: */ michael@0: png_row_info row_info; michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_debug2(1, "in png_write_row (row %u, pass %d)", michael@0: png_ptr->row_number, png_ptr->pass); michael@0: michael@0: /* Initialize transformations and other stuff if first time */ michael@0: if (png_ptr->row_number == 0 && png_ptr->pass == 0) michael@0: { michael@0: /* Make sure we wrote the header info */ michael@0: if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) michael@0: png_error(png_ptr, michael@0: "png_write_info was never called before png_write_row"); michael@0: michael@0: /* Check for transforms that have been set but were defined out */ michael@0: #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_INVERT_MONO) michael@0: png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_FILLER) michael@0: png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined"); michael@0: #endif michael@0: #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ michael@0: defined(PNG_READ_PACKSWAP_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_PACKSWAP) michael@0: png_warning(png_ptr, michael@0: "PNG_WRITE_PACKSWAP_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_PACK) michael@0: png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_SHIFT) michael@0: png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_BGR) michael@0: png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_SWAP_BYTES) michael@0: png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined"); michael@0: #endif michael@0: michael@0: png_write_start_row(png_ptr); michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* If interlaced and not interested in row, return */ michael@0: if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) michael@0: { michael@0: switch (png_ptr->pass) michael@0: { michael@0: case 0: michael@0: if (png_ptr->row_number & 0x07) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 1: michael@0: if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 2: michael@0: if ((png_ptr->row_number & 0x07) != 4) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 3: michael@0: if ((png_ptr->row_number & 0x03) || png_ptr->width < 3) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 4: michael@0: if ((png_ptr->row_number & 0x03) != 2) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 5: michael@0: if ((png_ptr->row_number & 0x01) || png_ptr->width < 2) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: case 6: michael@0: if (!(png_ptr->row_number & 0x01)) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: break; michael@0: michael@0: default: /* error: ignore it */ michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* Set up row info for transformations */ michael@0: row_info.color_type = png_ptr->color_type; michael@0: row_info.width = png_ptr->usr_width; michael@0: row_info.channels = png_ptr->usr_channels; michael@0: row_info.bit_depth = png_ptr->usr_bit_depth; michael@0: row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels); michael@0: row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); michael@0: michael@0: png_debug1(3, "row_info->color_type = %d", row_info.color_type); michael@0: png_debug1(3, "row_info->width = %u", row_info.width); michael@0: png_debug1(3, "row_info->channels = %d", row_info.channels); michael@0: png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth); michael@0: png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth); michael@0: png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes); michael@0: michael@0: /* Copy user's row into buffer, leaving room for filter byte. */ michael@0: memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes); michael@0: michael@0: #ifdef PNG_WRITE_INTERLACING_SUPPORTED michael@0: /* Handle interlacing */ michael@0: if (png_ptr->interlaced && png_ptr->pass < 6 && michael@0: (png_ptr->transformations & PNG_INTERLACE)) michael@0: { michael@0: png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass); michael@0: /* This should always get caught above, but still ... */ michael@0: if (!(row_info.width)) michael@0: { michael@0: png_write_finish_row(png_ptr); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED michael@0: /* Handle other transformations */ michael@0: if (png_ptr->transformations) michael@0: png_do_write_transformations(png_ptr, &row_info); michael@0: #endif michael@0: michael@0: /* At this point the row_info pixel depth must match the 'transformed' depth, michael@0: * which is also the output depth. michael@0: */ michael@0: if (row_info.pixel_depth != png_ptr->pixel_depth || michael@0: row_info.pixel_depth != png_ptr->transformed_pixel_depth) michael@0: png_error(png_ptr, "internal write transform logic error"); michael@0: michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: /* Write filter_method 64 (intrapixel differencing) only if michael@0: * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and michael@0: * 2. Libpng did not write a PNG signature (this filter_method is only michael@0: * used in PNG datastreams that are embedded in MNG datastreams) and michael@0: * 3. The application called png_permit_mng_features with a mask that michael@0: * included PNG_FLAG_MNG_FILTER_64 and michael@0: * 4. The filter_method is 64 and michael@0: * 5. The color_type is RGB or RGBA michael@0: */ michael@0: if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && michael@0: (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) michael@0: { michael@0: /* Intrapixel differencing */ michael@0: png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1); michael@0: } michael@0: #endif michael@0: michael@0: /* Added at libpng-1.5.10 */ michael@0: #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED michael@0: /* Check for out-of-range palette index */ michael@0: if (row_info.color_type == PNG_COLOR_TYPE_PALETTE && michael@0: png_ptr->num_palette_max >= 0) michael@0: png_do_check_palette_indexes(png_ptr, &row_info); michael@0: #endif michael@0: michael@0: /* Find a filter if necessary, filter the row and write it out. */ michael@0: png_write_find_filter(png_ptr, &row_info); michael@0: michael@0: if (png_ptr->write_row_fn != NULL) michael@0: (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_FLUSH_SUPPORTED michael@0: /* Set the automatic flush interval or 0 to turn flushing off */ michael@0: void PNGAPI michael@0: png_set_flush(png_structrp png_ptr, int nrows) michael@0: { michael@0: png_debug(1, "in png_set_flush"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->flush_dist = (nrows < 0 ? 0 : nrows); michael@0: } michael@0: michael@0: /* Flush the current output buffers now */ michael@0: void PNGAPI michael@0: png_write_flush(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_write_flush"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* We have already written out all of the data */ michael@0: if (png_ptr->row_number >= png_ptr->num_rows) michael@0: return; michael@0: michael@0: png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH); michael@0: png_ptr->flush_rows = 0; michael@0: png_flush(png_ptr); michael@0: } michael@0: #endif /* PNG_WRITE_FLUSH_SUPPORTED */ michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: static void png_reset_filter_heuristics(png_structrp png_ptr);/* forward decl */ michael@0: #endif michael@0: michael@0: /* Free any memory used in png_ptr struct without freeing the struct itself. */ michael@0: static void michael@0: png_write_destroy(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_write_destroy"); michael@0: michael@0: /* Free any memory zlib uses */ michael@0: if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) michael@0: deflateEnd(&png_ptr->zstream); michael@0: michael@0: /* Free our memory. png_free checks NULL for us. */ michael@0: png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list); michael@0: png_free(png_ptr, png_ptr->row_buf); michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: png_free(png_ptr, png_ptr->prev_row); michael@0: png_free(png_ptr, png_ptr->sub_row); michael@0: png_free(png_ptr, png_ptr->up_row); michael@0: png_free(png_ptr, png_ptr->avg_row); michael@0: png_free(png_ptr, png_ptr->paeth_row); michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED michael@0: /* Use this to save a little code space, it doesn't free the filter_costs */ michael@0: png_reset_filter_heuristics(png_ptr); michael@0: png_free(png_ptr, png_ptr->filter_costs); michael@0: png_free(png_ptr, png_ptr->inv_filter_costs); michael@0: #endif michael@0: michael@0: #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED michael@0: png_free(png_ptr, png_ptr->chunk_list); michael@0: #endif michael@0: michael@0: /* The error handling and memory handling information is left intact at this michael@0: * point: the jmp_buf may still have to be freed. See png_destroy_png_struct michael@0: * for how this happens. michael@0: */ michael@0: } michael@0: michael@0: /* Free all memory used by the write. michael@0: * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for michael@0: * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free michael@0: * the passed in info_structs but it would quietly fail to free any of the data michael@0: * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it michael@0: * has no png_ptr.) michael@0: */ michael@0: void PNGAPI michael@0: png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) michael@0: { michael@0: png_debug(1, "in png_destroy_write_struct"); michael@0: michael@0: if (png_ptr_ptr != NULL) michael@0: { michael@0: png_structrp png_ptr = *png_ptr_ptr; michael@0: michael@0: if (png_ptr != NULL) /* added in libpng 1.6.0 */ michael@0: { michael@0: png_destroy_info_struct(png_ptr, info_ptr_ptr); michael@0: michael@0: *png_ptr_ptr = NULL; michael@0: png_write_destroy(png_ptr); michael@0: png_destroy_png_struct(png_ptr); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Allow the application to select one or more row filters to use. */ michael@0: void PNGAPI michael@0: png_set_filter(png_structrp png_ptr, int method, int filters) michael@0: { michael@0: png_debug(1, "in png_set_filter"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: #ifdef PNG_MNG_FEATURES_SUPPORTED michael@0: if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && michael@0: (method == PNG_INTRAPIXEL_DIFFERENCING)) michael@0: method = PNG_FILTER_TYPE_BASE; michael@0: michael@0: #endif michael@0: if (method == PNG_FILTER_TYPE_BASE) michael@0: { michael@0: switch (filters & (PNG_ALL_FILTERS | 0x07)) michael@0: { michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: case 5: michael@0: case 6: michael@0: case 7: png_app_error(png_ptr, "Unknown row filter for method 0"); michael@0: /* FALL THROUGH */ michael@0: #endif /* PNG_WRITE_FILTER_SUPPORTED */ michael@0: case PNG_FILTER_VALUE_NONE: michael@0: png_ptr->do_filter = PNG_FILTER_NONE; break; michael@0: michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: case PNG_FILTER_VALUE_SUB: michael@0: png_ptr->do_filter = PNG_FILTER_SUB; break; michael@0: michael@0: case PNG_FILTER_VALUE_UP: michael@0: png_ptr->do_filter = PNG_FILTER_UP; break; michael@0: michael@0: case PNG_FILTER_VALUE_AVG: michael@0: png_ptr->do_filter = PNG_FILTER_AVG; break; michael@0: michael@0: case PNG_FILTER_VALUE_PAETH: michael@0: png_ptr->do_filter = PNG_FILTER_PAETH; break; michael@0: michael@0: default: michael@0: png_ptr->do_filter = (png_byte)filters; break; michael@0: #else michael@0: default: michael@0: png_app_error(png_ptr, "Unknown row filter for method 0"); michael@0: #endif /* PNG_WRITE_FILTER_SUPPORTED */ michael@0: } michael@0: michael@0: /* If we have allocated the row_buf, this means we have already started michael@0: * with the image and we should have allocated all of the filter buffers michael@0: * that have been selected. If prev_row isn't already allocated, then michael@0: * it is too late to start using the filters that need it, since we michael@0: * will be missing the data in the previous row. If an application michael@0: * wants to start and stop using particular filters during compression, michael@0: * it should start out with all of the filters, and then add and michael@0: * remove them after the start of compression. michael@0: */ michael@0: if (png_ptr->row_buf != NULL) michael@0: { michael@0: #ifdef PNG_WRITE_FILTER_SUPPORTED michael@0: if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL) michael@0: { michael@0: png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, michael@0: (png_ptr->rowbytes + 1)); michael@0: png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; michael@0: } michael@0: michael@0: if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL) michael@0: { michael@0: if (png_ptr->prev_row == NULL) michael@0: { michael@0: png_warning(png_ptr, "Can't add Up filter after starting"); michael@0: png_ptr->do_filter = (png_byte)(png_ptr->do_filter & michael@0: ~PNG_FILTER_UP); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->up_row = (png_bytep)png_malloc(png_ptr, michael@0: (png_ptr->rowbytes + 1)); michael@0: png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; michael@0: } michael@0: } michael@0: michael@0: if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL) michael@0: { michael@0: if (png_ptr->prev_row == NULL) michael@0: { michael@0: png_warning(png_ptr, "Can't add Average filter after starting"); michael@0: png_ptr->do_filter = (png_byte)(png_ptr->do_filter & michael@0: ~PNG_FILTER_AVG); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, michael@0: (png_ptr->rowbytes + 1)); michael@0: png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; michael@0: } michael@0: } michael@0: michael@0: if ((png_ptr->do_filter & PNG_FILTER_PAETH) && michael@0: png_ptr->paeth_row == NULL) michael@0: { michael@0: if (png_ptr->prev_row == NULL) michael@0: { michael@0: png_warning(png_ptr, "Can't add Paeth filter after starting"); michael@0: png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, michael@0: (png_ptr->rowbytes + 1)); michael@0: png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; michael@0: } michael@0: } michael@0: michael@0: if (png_ptr->do_filter == PNG_NO_FILTERS) michael@0: #endif /* PNG_WRITE_FILTER_SUPPORTED */ michael@0: png_ptr->do_filter = PNG_FILTER_NONE; michael@0: } michael@0: } michael@0: else michael@0: png_error(png_ptr, "Unknown custom filter method"); michael@0: } michael@0: michael@0: /* This allows us to influence the way in which libpng chooses the "best" michael@0: * filter for the current scanline. While the "minimum-sum-of-absolute- michael@0: * differences metric is relatively fast and effective, there is some michael@0: * question as to whether it can be improved upon by trying to keep the michael@0: * filtered data going to zlib more consistent, hopefully resulting in michael@0: * better compression. michael@0: */ michael@0: #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */ michael@0: /* Convenience reset API. */ michael@0: static void michael@0: png_reset_filter_heuristics(png_structrp png_ptr) michael@0: { michael@0: /* Clear out any old values in the 'weights' - this must be done because if michael@0: * the app calls set_filter_heuristics multiple times with different michael@0: * 'num_weights' values we would otherwise potentially have wrong sized michael@0: * arrays. michael@0: */ michael@0: png_ptr->num_prev_filters = 0; michael@0: png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED; michael@0: if (png_ptr->prev_filters != NULL) michael@0: { michael@0: png_bytep old = png_ptr->prev_filters; michael@0: png_ptr->prev_filters = NULL; michael@0: png_free(png_ptr, old); michael@0: } michael@0: if (png_ptr->filter_weights != NULL) michael@0: { michael@0: png_uint_16p old = png_ptr->filter_weights; michael@0: png_ptr->filter_weights = NULL; michael@0: png_free(png_ptr, old); michael@0: } michael@0: michael@0: if (png_ptr->inv_filter_weights != NULL) michael@0: { michael@0: png_uint_16p old = png_ptr->inv_filter_weights; michael@0: png_ptr->inv_filter_weights = NULL; michael@0: png_free(png_ptr, old); michael@0: } michael@0: michael@0: /* Leave the filter_costs - this array is fixed size. */ michael@0: } michael@0: michael@0: static int michael@0: png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method, michael@0: int num_weights) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return 0; michael@0: michael@0: /* Clear out the arrays */ michael@0: png_reset_filter_heuristics(png_ptr); michael@0: michael@0: /* Check arguments; the 'reset' function makes the correct settings for the michael@0: * unweighted case, but we must handle the weight case by initializing the michael@0: * arrays for the caller. michael@0: */ michael@0: if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int i; michael@0: michael@0: if (num_weights > 0) michael@0: { michael@0: png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr, michael@0: (png_uint_32)((sizeof (png_byte)) * num_weights)); michael@0: michael@0: /* To make sure that the weighting starts out fairly */ michael@0: for (i = 0; i < num_weights; i++) michael@0: { michael@0: png_ptr->prev_filters[i] = 255; michael@0: } michael@0: michael@0: png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr, michael@0: (png_uint_32)((sizeof (png_uint_16)) * num_weights)); michael@0: michael@0: png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr, michael@0: (png_uint_32)((sizeof (png_uint_16)) * num_weights)); michael@0: michael@0: for (i = 0; i < num_weights; i++) michael@0: { michael@0: png_ptr->inv_filter_weights[i] = michael@0: png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; michael@0: } michael@0: michael@0: /* Safe to set this now */ michael@0: png_ptr->num_prev_filters = (png_byte)num_weights; michael@0: } michael@0: michael@0: /* If, in the future, there are other filter methods, this would michael@0: * need to be based on png_ptr->filter. michael@0: */ michael@0: if (png_ptr->filter_costs == NULL) michael@0: { michael@0: png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr, michael@0: (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST)); michael@0: michael@0: png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr, michael@0: (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST)); michael@0: } michael@0: michael@0: for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) michael@0: { michael@0: png_ptr->inv_filter_costs[i] = michael@0: png_ptr->filter_costs[i] = PNG_COST_FACTOR; michael@0: } michael@0: michael@0: /* All the arrays are inited, safe to set this: */ michael@0: png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED; michael@0: michael@0: /* Return the 'ok' code. */ michael@0: return 1; michael@0: } michael@0: else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT || michael@0: heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED) michael@0: { michael@0: return 1; michael@0: } michael@0: else michael@0: { michael@0: png_warning(png_ptr, "Unknown filter heuristic method"); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /* Provide floating and fixed point APIs */ michael@0: #ifdef PNG_FLOATING_POINT_SUPPORTED michael@0: void PNGAPI michael@0: png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method, michael@0: int num_weights, png_const_doublep filter_weights, michael@0: png_const_doublep filter_costs) michael@0: { michael@0: png_debug(1, "in png_set_filter_heuristics"); michael@0: michael@0: /* The internal API allocates all the arrays and ensures that the elements of michael@0: * those arrays are set to the default value. michael@0: */ michael@0: if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights)) michael@0: return; michael@0: michael@0: /* If using the weighted method copy in the weights. */ michael@0: if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int i; michael@0: for (i = 0; i < num_weights; i++) michael@0: { michael@0: if (filter_weights[i] <= 0.0) michael@0: { michael@0: png_ptr->inv_filter_weights[i] = michael@0: png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->inv_filter_weights[i] = michael@0: (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5); michael@0: michael@0: png_ptr->filter_weights[i] = michael@0: (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5); michael@0: } michael@0: } michael@0: michael@0: /* Here is where we set the relative costs of the different filters. We michael@0: * should take the desired compression level into account when setting michael@0: * the costs, so that Paeth, for instance, has a high relative cost at low michael@0: * compression levels, while it has a lower relative cost at higher michael@0: * compression settings. The filter types are in order of increasing michael@0: * relative cost, so it would be possible to do this with an algorithm. michael@0: */ michael@0: for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0) michael@0: { michael@0: png_ptr->inv_filter_costs[i] = michael@0: (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5); michael@0: michael@0: png_ptr->filter_costs[i] = michael@0: (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5); michael@0: } michael@0: } michael@0: } michael@0: #endif /* FLOATING_POINT */ michael@0: michael@0: #ifdef PNG_FIXED_POINT_SUPPORTED michael@0: void PNGAPI michael@0: png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method, michael@0: int num_weights, png_const_fixed_point_p filter_weights, michael@0: png_const_fixed_point_p filter_costs) michael@0: { michael@0: png_debug(1, "in png_set_filter_heuristics_fixed"); michael@0: michael@0: /* The internal API allocates all the arrays and ensures that the elements of michael@0: * those arrays are set to the default value. michael@0: */ michael@0: if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights)) michael@0: return; michael@0: michael@0: /* If using the weighted method copy in the weights. */ michael@0: if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) michael@0: { michael@0: int i; michael@0: for (i = 0; i < num_weights; i++) michael@0: { michael@0: if (filter_weights[i] <= 0) michael@0: { michael@0: png_ptr->inv_filter_weights[i] = michael@0: png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_ptr->inv_filter_weights[i] = (png_uint_16) michael@0: ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1); michael@0: michael@0: png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR* michael@0: PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]); michael@0: } michael@0: } michael@0: michael@0: /* Here is where we set the relative costs of the different filters. We michael@0: * should take the desired compression level into account when setting michael@0: * the costs, so that Paeth, for instance, has a high relative cost at low michael@0: * compression levels, while it has a lower relative cost at higher michael@0: * compression settings. The filter types are in order of increasing michael@0: * relative cost, so it would be possible to do this with an algorithm. michael@0: */ michael@0: for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) michael@0: if (filter_costs[i] >= PNG_FP_1) michael@0: { michael@0: png_uint_32 tmp; michael@0: michael@0: /* Use a 32 bit unsigned temporary here because otherwise the michael@0: * intermediate value will be a 32 bit *signed* integer (ANSI rules) michael@0: * and this will get the wrong answer on division. michael@0: */ michael@0: tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2); michael@0: tmp /= filter_costs[i]; michael@0: michael@0: png_ptr->inv_filter_costs[i] = (png_uint_16)tmp; michael@0: michael@0: tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF; michael@0: tmp /= PNG_FP_1; michael@0: michael@0: png_ptr->filter_costs[i] = (png_uint_16)tmp; michael@0: } michael@0: } michael@0: } michael@0: #endif /* FIXED_POINT */ michael@0: #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ michael@0: michael@0: void PNGAPI michael@0: png_set_compression_level(png_structrp png_ptr, int level) michael@0: { michael@0: png_debug(1, "in png_set_compression_level"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->zlib_level = level; michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_set_compression_mem_level(png_structrp png_ptr, int mem_level) michael@0: { michael@0: png_debug(1, "in png_set_compression_mem_level"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->zlib_mem_level = mem_level; michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_set_compression_strategy(png_structrp png_ptr, int strategy) michael@0: { michael@0: png_debug(1, "in png_set_compression_strategy"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* The flag setting here prevents the libpng dynamic selection of strategy. michael@0: */ michael@0: png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; michael@0: png_ptr->zlib_strategy = strategy; michael@0: } michael@0: michael@0: /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a michael@0: * smaller value of window_bits if it can do so safely. michael@0: */ michael@0: void PNGAPI michael@0: png_set_compression_window_bits(png_structrp png_ptr, int window_bits) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* Prior to 1.6.0 this would warn but then set the window_bits value, this michael@0: * meant that negative window bits values could be selected which would cause michael@0: * libpng to write a non-standard PNG file with raw deflate or gzip michael@0: * compressed IDAT or ancillary chunks. Such files can be read and there is michael@0: * no warning on read, so this seems like a very bad idea. michael@0: */ michael@0: if (window_bits > 15) michael@0: { michael@0: png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); michael@0: window_bits = 15; michael@0: } michael@0: michael@0: else if (window_bits < 8) michael@0: { michael@0: png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); michael@0: window_bits = 8; michael@0: } michael@0: michael@0: png_ptr->zlib_window_bits = window_bits; michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_set_compression_method(png_structrp png_ptr, int method) michael@0: { michael@0: png_debug(1, "in png_set_compression_method"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* This would produce an invalid PNG file if it worked, but it doesn't and michael@0: * deflate will fault it, so it is harmless to just warn here. michael@0: */ michael@0: if (method != 8) michael@0: png_warning(png_ptr, "Only compression method 8 is supported by PNG"); michael@0: michael@0: png_ptr->zlib_method = method; michael@0: } michael@0: michael@0: /* The following were added to libpng-1.5.4 */ michael@0: #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED michael@0: void PNGAPI michael@0: png_set_text_compression_level(png_structrp png_ptr, int level) michael@0: { michael@0: png_debug(1, "in png_set_text_compression_level"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->zlib_text_level = level; michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level) michael@0: { michael@0: png_debug(1, "in png_set_text_compression_mem_level"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->zlib_text_mem_level = mem_level; michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_set_text_compression_strategy(png_structrp png_ptr, int strategy) michael@0: { michael@0: png_debug(1, "in png_set_text_compression_strategy"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->zlib_text_strategy = strategy; michael@0: } michael@0: michael@0: /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a michael@0: * smaller value of window_bits if it can do so safely. michael@0: */ michael@0: void PNGAPI michael@0: png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: if (window_bits > 15) michael@0: { michael@0: png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); michael@0: window_bits = 15; michael@0: } michael@0: michael@0: else if (window_bits < 8) michael@0: { michael@0: png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); michael@0: window_bits = 8; michael@0: } michael@0: michael@0: png_ptr->zlib_text_window_bits = window_bits; michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_set_text_compression_method(png_structrp png_ptr, int method) michael@0: { michael@0: png_debug(1, "in png_set_text_compression_method"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: if (method != 8) michael@0: png_warning(png_ptr, "Only compression method 8 is supported by PNG"); michael@0: michael@0: png_ptr->zlib_text_method = method; michael@0: } michael@0: #endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */ michael@0: /* end of API added to libpng-1.5.4 */ michael@0: michael@0: void PNGAPI michael@0: png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->write_row_fn = write_row_fn; michael@0: } michael@0: michael@0: #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED michael@0: void PNGAPI michael@0: png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr michael@0: write_user_transform_fn) michael@0: { michael@0: png_debug(1, "in png_set_write_user_transform_fn"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->transformations |= PNG_USER_TRANSFORM; michael@0: png_ptr->write_user_transform_fn = write_user_transform_fn; michael@0: } michael@0: #endif michael@0: michael@0: michael@0: #ifdef PNG_INFO_IMAGE_SUPPORTED michael@0: void PNGAPI michael@0: png_write_png(png_structrp png_ptr, png_inforp info_ptr, michael@0: int transforms, voidp params) michael@0: { michael@0: if (png_ptr == NULL || info_ptr == NULL) michael@0: return; michael@0: michael@0: if ((info_ptr->valid & PNG_INFO_IDAT) == 0) michael@0: { michael@0: png_app_error(png_ptr, "no rows for png_write_image to write"); michael@0: return; michael@0: } michael@0: michael@0: /* Write the file header information. */ michael@0: png_write_info(png_ptr, info_ptr); michael@0: michael@0: /* ------ these transformations don't touch the info structure ------- */ michael@0: michael@0: /* Invert monochrome pixels */ michael@0: if (transforms & PNG_TRANSFORM_INVERT_MONO) michael@0: #ifdef PNG_WRITE_INVERT_SUPPORTED michael@0: png_set_invert_mono(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); michael@0: #endif michael@0: michael@0: /* Shift the pixels up to a legal bit depth and fill in michael@0: * as appropriate to correctly scale the image. michael@0: */ michael@0: if (transforms & PNG_TRANSFORM_SHIFT) michael@0: #ifdef PNG_WRITE_SHIFT_SUPPORTED michael@0: if (info_ptr->valid & PNG_INFO_sBIT) michael@0: png_set_shift(png_ptr, &info_ptr->sig_bit); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); michael@0: #endif michael@0: michael@0: /* Pack pixels into bytes */ michael@0: if (transforms & PNG_TRANSFORM_PACKING) michael@0: #ifdef PNG_WRITE_PACK_SUPPORTED michael@0: png_set_packing(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); michael@0: #endif michael@0: michael@0: /* Swap location of alpha bytes from ARGB to RGBA */ michael@0: if (transforms & PNG_TRANSFORM_SWAP_ALPHA) michael@0: #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED michael@0: png_set_swap_alpha(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); michael@0: #endif michael@0: michael@0: /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into michael@0: * RGB, note that the code expects the input color type to be G or RGB; no michael@0: * alpha channel. michael@0: */ michael@0: if (transforms & michael@0: (PNG_TRANSFORM_STRIP_FILLER_AFTER|PNG_TRANSFORM_STRIP_FILLER_BEFORE)) michael@0: { michael@0: #ifdef PNG_WRITE_FILLER_SUPPORTED michael@0: if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) michael@0: { michael@0: if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) michael@0: png_app_error(png_ptr, michael@0: "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported"); michael@0: michael@0: /* Continue if ignored - this is the pre-1.6.10 behavior */ michael@0: png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); michael@0: } michael@0: michael@0: else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) michael@0: png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported"); michael@0: #endif michael@0: } michael@0: michael@0: /* Flip BGR pixels to RGB */ michael@0: if (transforms & PNG_TRANSFORM_BGR) michael@0: #ifdef PNG_WRITE_BGR_SUPPORTED michael@0: png_set_bgr(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); michael@0: #endif michael@0: michael@0: /* Swap bytes of 16-bit files to most significant byte first */ michael@0: if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) michael@0: #ifdef PNG_WRITE_SWAP_SUPPORTED michael@0: png_set_swap(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); michael@0: #endif michael@0: michael@0: /* Swap bits of 1, 2, 4 bit packed pixel formats */ michael@0: if (transforms & PNG_TRANSFORM_PACKSWAP) michael@0: #ifdef PNG_WRITE_PACKSWAP_SUPPORTED michael@0: png_set_packswap(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); michael@0: #endif michael@0: michael@0: /* Invert the alpha channel from opacity to transparency */ michael@0: if (transforms & PNG_TRANSFORM_INVERT_ALPHA) michael@0: #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED michael@0: png_set_invert_alpha(png_ptr); michael@0: #else michael@0: png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); michael@0: #endif michael@0: michael@0: /* ----------------------- end of transformations ------------------- */ michael@0: michael@0: /* Write the bits */ michael@0: png_write_image(png_ptr, info_ptr->row_pointers); michael@0: michael@0: /* It is REQUIRED to call this to finish writing the rest of the file */ michael@0: png_write_end(png_ptr, info_ptr); michael@0: michael@0: PNG_UNUSED(params) michael@0: } michael@0: #endif michael@0: michael@0: michael@0: #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED michael@0: #ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */ michael@0: /* Initialize the write structure - general purpose utility. */ michael@0: static int michael@0: png_image_write_init(png_imagep image) michael@0: { michael@0: png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image, michael@0: png_safe_error, png_safe_warning); michael@0: michael@0: if (png_ptr != NULL) michael@0: { michael@0: png_infop info_ptr = png_create_info_struct(png_ptr); michael@0: michael@0: if (info_ptr != NULL) michael@0: { michael@0: png_controlp control = png_voidcast(png_controlp, michael@0: png_malloc_warn(png_ptr, (sizeof *control))); michael@0: michael@0: if (control != NULL) michael@0: { michael@0: memset(control, 0, (sizeof *control)); michael@0: michael@0: control->png_ptr = png_ptr; michael@0: control->info_ptr = info_ptr; michael@0: control->for_write = 1; michael@0: michael@0: image->opaque = control; michael@0: return 1; michael@0: } michael@0: michael@0: /* Error clean up */ michael@0: png_destroy_info_struct(png_ptr, &info_ptr); michael@0: } michael@0: michael@0: png_destroy_write_struct(&png_ptr, NULL); michael@0: } michael@0: michael@0: return png_image_error(image, "png_image_write_: out of memory"); michael@0: } michael@0: michael@0: /* Arguments to png_image_write_main: */ michael@0: typedef struct michael@0: { michael@0: /* Arguments: */ michael@0: png_imagep image; michael@0: png_const_voidp buffer; michael@0: png_int_32 row_stride; michael@0: png_const_voidp colormap; michael@0: int convert_to_8bit; michael@0: /* Local variables: */ michael@0: png_const_voidp first_row; michael@0: ptrdiff_t row_bytes; michael@0: png_voidp local_row; michael@0: } png_image_write_control; michael@0: michael@0: /* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to michael@0: * do any necessary byte swapping. The component order is defined by the michael@0: * png_image format value. michael@0: */ michael@0: static int michael@0: png_write_image_16bit(png_voidp argument) michael@0: { michael@0: png_image_write_control *display = png_voidcast(png_image_write_control*, michael@0: argument); michael@0: png_imagep image = display->image; michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: michael@0: png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, michael@0: display->first_row); michael@0: png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row); michael@0: png_uint_16p row_end; michael@0: const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1; michael@0: int aindex = 0; michael@0: png_uint_32 y = image->height; michael@0: michael@0: if (image->format & PNG_FORMAT_FLAG_ALPHA) michael@0: { michael@0: # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED michael@0: if (image->format & PNG_FORMAT_FLAG_AFIRST) michael@0: { michael@0: aindex = -1; michael@0: ++input_row; /* To point to the first component */ michael@0: ++output_row; michael@0: } michael@0: michael@0: else michael@0: # endif michael@0: aindex = channels; michael@0: } michael@0: michael@0: else michael@0: png_error(png_ptr, "png_write_image: internal call error"); michael@0: michael@0: /* Work out the output row end and count over this, note that the increment michael@0: * above to 'row' means that row_end can actually be beyond the end of the michael@0: * row; this is correct. michael@0: */ michael@0: row_end = output_row + image->width * (channels+1); michael@0: michael@0: while (y-- > 0) michael@0: { michael@0: png_const_uint_16p in_ptr = input_row; michael@0: png_uint_16p out_ptr = output_row; michael@0: michael@0: while (out_ptr < row_end) michael@0: { michael@0: const png_uint_16 alpha = in_ptr[aindex]; michael@0: png_uint_32 reciprocal = 0; michael@0: int c; michael@0: michael@0: out_ptr[aindex] = alpha; michael@0: michael@0: /* Calculate a reciprocal. The correct calculation is simply michael@0: * component/alpha*65535 << 15. (I.e. 15 bits of precision); this michael@0: * allows correct rounding by adding .5 before the shift. 'reciprocal' michael@0: * is only initialized when required. michael@0: */ michael@0: if (alpha > 0 && alpha < 65535) michael@0: reciprocal = ((0xffff<<15)+(alpha>>1))/alpha; michael@0: michael@0: c = channels; michael@0: do /* always at least one channel */ michael@0: { michael@0: png_uint_16 component = *in_ptr++; michael@0: michael@0: /* The following gives 65535 for an alpha of 0, which is fine, michael@0: * otherwise if 0/0 is represented as some other value there is more michael@0: * likely to be a discontinuity which will probably damage michael@0: * compression when moving from a fully transparent area to a michael@0: * nearly transparent one. (The assumption here is that opaque michael@0: * areas tend not to be 0 intensity.) michael@0: */ michael@0: if (component >= alpha) michael@0: component = 65535; michael@0: michael@0: /* component 0 && alpha < 65535) michael@0: { michael@0: png_uint_32 calc = component * reciprocal; michael@0: calc += 16384; /* round to nearest */ michael@0: component = (png_uint_16)(calc >> 15); michael@0: } michael@0: michael@0: *out_ptr++ = component; michael@0: } michael@0: while (--c > 0); michael@0: michael@0: /* Skip to next component (skip the intervening alpha channel) */ michael@0: ++in_ptr; michael@0: ++out_ptr; michael@0: } michael@0: michael@0: png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row)); michael@0: input_row += display->row_bytes/(sizeof (png_uint_16)); michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: /* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel michael@0: * is present it must be removed from the components, the components are then michael@0: * written in sRGB encoding. No components are added or removed. michael@0: * michael@0: * Calculate an alpha reciprocal to reverse pre-multiplication. As above the michael@0: * calculation can be done to 15 bits of accuracy; however, the output needs to michael@0: * be scaled in the range 0..255*65535, so include that scaling here. michael@0: */ michael@0: #define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha) michael@0: michael@0: static png_byte michael@0: png_unpremultiply(png_uint_32 component, png_uint_32 alpha, michael@0: png_uint_32 reciprocal/*from the above macro*/) michael@0: { michael@0: /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0 michael@0: * is represented as some other value there is more likely to be a michael@0: * discontinuity which will probably damage compression when moving from a michael@0: * fully transparent area to a nearly transparent one. (The assumption here michael@0: * is that opaque areas tend not to be 0 intensity.) michael@0: * michael@0: * There is a rounding problem here; if alpha is less than 128 it will end up michael@0: * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the michael@0: * output change for this too. michael@0: */ michael@0: if (component >= alpha || alpha < 128) michael@0: return 255; michael@0: michael@0: /* component 0) michael@0: { michael@0: /* The test is that alpha/257 (rounded) is less than 255, the first value michael@0: * that becomes 255 is 65407. michael@0: * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore, michael@0: * be exact!) [Could also test reciprocal != 0] michael@0: */ michael@0: if (alpha < 65407) michael@0: { michael@0: component *= reciprocal; michael@0: component += 64; /* round to nearest */ michael@0: component >>= 7; michael@0: } michael@0: michael@0: else michael@0: component *= 255; michael@0: michael@0: /* Convert the component to sRGB. */ michael@0: return (png_byte)PNG_sRGB_FROM_LINEAR(component); michael@0: } michael@0: michael@0: else michael@0: return 0; michael@0: } michael@0: michael@0: static int michael@0: png_write_image_8bit(png_voidp argument) michael@0: { michael@0: png_image_write_control *display = png_voidcast(png_image_write_control*, michael@0: argument); michael@0: png_imagep image = display->image; michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: michael@0: png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, michael@0: display->first_row); michael@0: png_bytep output_row = png_voidcast(png_bytep, display->local_row); michael@0: png_uint_32 y = image->height; michael@0: const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1; michael@0: michael@0: if (image->format & PNG_FORMAT_FLAG_ALPHA) michael@0: { michael@0: png_bytep row_end; michael@0: int aindex; michael@0: michael@0: # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED michael@0: if (image->format & PNG_FORMAT_FLAG_AFIRST) michael@0: { michael@0: aindex = -1; michael@0: ++input_row; /* To point to the first component */ michael@0: ++output_row; michael@0: } michael@0: michael@0: else michael@0: # endif michael@0: aindex = channels; michael@0: michael@0: /* Use row_end in place of a loop counter: */ michael@0: row_end = output_row + image->width * (channels+1); michael@0: michael@0: while (y-- > 0) michael@0: { michael@0: png_const_uint_16p in_ptr = input_row; michael@0: png_bytep out_ptr = output_row; michael@0: michael@0: while (out_ptr < row_end) michael@0: { michael@0: png_uint_16 alpha = in_ptr[aindex]; michael@0: png_byte alphabyte = (png_byte)PNG_DIV257(alpha); michael@0: png_uint_32 reciprocal = 0; michael@0: int c; michael@0: michael@0: /* Scale and write the alpha channel. */ michael@0: out_ptr[aindex] = alphabyte; michael@0: michael@0: if (alphabyte > 0 && alphabyte < 255) michael@0: reciprocal = UNP_RECIPROCAL(alpha); michael@0: michael@0: c = channels; michael@0: do /* always at least one channel */ michael@0: *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal); michael@0: while (--c > 0); michael@0: michael@0: /* Skip to next component (skip the intervening alpha channel) */ michael@0: ++in_ptr; michael@0: ++out_ptr; michael@0: } /* while out_ptr < row_end */ michael@0: michael@0: png_write_row(png_ptr, png_voidcast(png_const_bytep, michael@0: display->local_row)); michael@0: input_row += display->row_bytes/(sizeof (png_uint_16)); michael@0: } /* while y */ michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* No alpha channel, so the row_end really is the end of the row and it michael@0: * is sufficient to loop over the components one by one. michael@0: */ michael@0: png_bytep row_end = output_row + image->width * channels; michael@0: michael@0: while (y-- > 0) michael@0: { michael@0: png_const_uint_16p in_ptr = input_row; michael@0: png_bytep out_ptr = output_row; michael@0: michael@0: while (out_ptr < row_end) michael@0: { michael@0: png_uint_32 component = *in_ptr++; michael@0: michael@0: component *= 255; michael@0: *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component); michael@0: } michael@0: michael@0: png_write_row(png_ptr, output_row); michael@0: input_row += display->row_bytes/(sizeof (png_uint_16)); michael@0: } michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: static void michael@0: png_image_set_PLTE(png_image_write_control *display) michael@0: { michael@0: const png_imagep image = display->image; michael@0: const void *cmap = display->colormap; michael@0: const int entries = image->colormap_entries > 256 ? 256 : michael@0: (int)image->colormap_entries; michael@0: michael@0: /* NOTE: the caller must check for cmap != NULL and entries != 0 */ michael@0: const png_uint_32 format = image->format; michael@0: const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format); michael@0: michael@0: # if defined(PNG_FORMAT_BGR_SUPPORTED) &&\ michael@0: defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED) michael@0: const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 && michael@0: (format & PNG_FORMAT_FLAG_ALPHA) != 0; michael@0: # else michael@0: # define afirst 0 michael@0: # endif michael@0: michael@0: # ifdef PNG_FORMAT_BGR_SUPPORTED michael@0: const int bgr = (format & PNG_FORMAT_FLAG_BGR) ? 2 : 0; michael@0: # else michael@0: # define bgr 0 michael@0: # endif michael@0: michael@0: int i, num_trans; michael@0: png_color palette[256]; michael@0: png_byte tRNS[256]; michael@0: michael@0: memset(tRNS, 255, (sizeof tRNS)); michael@0: memset(palette, 0, (sizeof palette)); michael@0: michael@0: for (i=num_trans=0; i= 3) /* RGB */ michael@0: { michael@0: palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 * michael@0: entry[(2 ^ bgr)]); michael@0: palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 * michael@0: entry[1]); michael@0: palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 * michael@0: entry[bgr]); michael@0: } michael@0: michael@0: else /* Gray */ michael@0: palette[i].blue = palette[i].red = palette[i].green = michael@0: (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry); michael@0: } michael@0: michael@0: else /* alpha */ michael@0: { michael@0: png_uint_16 alpha = entry[afirst ? 0 : channels-1]; michael@0: png_byte alphabyte = (png_byte)PNG_DIV257(alpha); michael@0: png_uint_32 reciprocal = 0; michael@0: michael@0: /* Calculate a reciprocal, as in the png_write_image_8bit code above michael@0: * this is designed to produce a value scaled to 255*65535 when michael@0: * divided by 128 (i.e. asr 7). michael@0: */ michael@0: if (alphabyte > 0 && alphabyte < 255) michael@0: reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha; michael@0: michael@0: tRNS[i] = alphabyte; michael@0: if (alphabyte < 255) michael@0: num_trans = i+1; michael@0: michael@0: if (channels >= 3) /* RGB */ michael@0: { michael@0: palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)], michael@0: alpha, reciprocal); michael@0: palette[i].green = png_unpremultiply(entry[afirst + 1], alpha, michael@0: reciprocal); michael@0: palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha, michael@0: reciprocal); michael@0: } michael@0: michael@0: else /* gray */ michael@0: palette[i].blue = palette[i].red = palette[i].green = michael@0: png_unpremultiply(entry[afirst], alpha, reciprocal); michael@0: } michael@0: } michael@0: michael@0: else /* Color-map has sRGB values */ michael@0: { michael@0: png_const_bytep entry = png_voidcast(png_const_bytep, cmap); michael@0: michael@0: entry += i * channels; michael@0: michael@0: switch (channels) michael@0: { michael@0: case 4: michael@0: tRNS[i] = entry[afirst ? 0 : 3]; michael@0: if (tRNS[i] < 255) michael@0: num_trans = i+1; michael@0: /* FALL THROUGH */ michael@0: case 3: michael@0: palette[i].blue = entry[afirst + (2 ^ bgr)]; michael@0: palette[i].green = entry[afirst + 1]; michael@0: palette[i].red = entry[afirst + bgr]; michael@0: break; michael@0: michael@0: case 2: michael@0: tRNS[i] = entry[1 ^ afirst]; michael@0: if (tRNS[i] < 255) michael@0: num_trans = i+1; michael@0: /* FALL THROUGH */ michael@0: case 1: michael@0: palette[i].blue = palette[i].red = palette[i].green = michael@0: entry[afirst]; michael@0: break; michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: # ifdef afirst michael@0: # undef afirst michael@0: # endif michael@0: # ifdef bgr michael@0: # undef bgr michael@0: # endif michael@0: michael@0: png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette, michael@0: entries); michael@0: michael@0: if (num_trans > 0) michael@0: png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS, michael@0: num_trans, NULL); michael@0: michael@0: image->colormap_entries = entries; michael@0: } michael@0: michael@0: static int michael@0: png_image_write_main(png_voidp argument) michael@0: { michael@0: png_image_write_control *display = png_voidcast(png_image_write_control*, michael@0: argument); michael@0: png_imagep image = display->image; michael@0: png_structrp png_ptr = image->opaque->png_ptr; michael@0: png_inforp info_ptr = image->opaque->info_ptr; michael@0: png_uint_32 format = image->format; michael@0: michael@0: int colormap = (format & PNG_FORMAT_FLAG_COLORMAP) != 0; michael@0: int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR) != 0; /* input */ michael@0: int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0; michael@0: int write_16bit = linear && !colormap && !display->convert_to_8bit; michael@0: michael@0: # ifdef PNG_BENIGN_ERRORS_SUPPORTED michael@0: /* Make sure we error out on any bad situation */ michael@0: png_set_benign_errors(png_ptr, 0/*error*/); michael@0: # endif michael@0: michael@0: /* Default the 'row_stride' parameter if required. */ michael@0: if (display->row_stride == 0) michael@0: display->row_stride = PNG_IMAGE_ROW_STRIDE(*image); michael@0: michael@0: /* Set the required transforms then write the rows in the correct order. */ michael@0: if (format & PNG_FORMAT_FLAG_COLORMAP) michael@0: { michael@0: if (display->colormap != NULL && image->colormap_entries > 0) michael@0: { michael@0: png_uint_32 entries = image->colormap_entries; michael@0: michael@0: png_set_IHDR(png_ptr, info_ptr, image->width, image->height, michael@0: entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)), michael@0: PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, michael@0: PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); michael@0: michael@0: png_image_set_PLTE(display); michael@0: } michael@0: michael@0: else michael@0: png_error(image->opaque->png_ptr, michael@0: "no color-map for color-mapped image"); michael@0: } michael@0: michael@0: else michael@0: png_set_IHDR(png_ptr, info_ptr, image->width, image->height, michael@0: write_16bit ? 16 : 8, michael@0: ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) + michael@0: ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0), michael@0: PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); michael@0: michael@0: /* Counter-intuitively the data transformations must be called *after* michael@0: * png_write_info, not before as in the read code, but the 'set' functions michael@0: * must still be called before. Just set the color space information, never michael@0: * write an interlaced image. michael@0: */ michael@0: michael@0: if (write_16bit) michael@0: { michael@0: /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */ michael@0: png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR); michael@0: michael@0: if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB)) michael@0: png_set_cHRM_fixed(png_ptr, info_ptr, michael@0: /* color x y */ michael@0: /* white */ 31270, 32900, michael@0: /* red */ 64000, 33000, michael@0: /* green */ 30000, 60000, michael@0: /* blue */ 15000, 6000 michael@0: ); michael@0: } michael@0: michael@0: else if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB)) michael@0: png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL); michael@0: michael@0: /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit michael@0: * space must still be gamma encoded. michael@0: */ michael@0: else michael@0: png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE); michael@0: michael@0: /* Write the file header. */ michael@0: png_write_info(png_ptr, info_ptr); michael@0: michael@0: /* Now set up the data transformations (*after* the header is written), michael@0: * remove the handled transformations from the 'format' flags for checking. michael@0: * michael@0: * First check for a little endian system if writing 16 bit files. michael@0: */ michael@0: if (write_16bit) michael@0: { michael@0: PNG_CONST png_uint_16 le = 0x0001; michael@0: michael@0: if (*(png_const_bytep)&le) michael@0: png_set_swap(png_ptr); michael@0: } michael@0: michael@0: # ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED michael@0: if (format & PNG_FORMAT_FLAG_BGR) michael@0: { michael@0: if (!colormap && (format & PNG_FORMAT_FLAG_COLOR) != 0) michael@0: png_set_bgr(png_ptr); michael@0: format &= ~PNG_FORMAT_FLAG_BGR; michael@0: } michael@0: # endif michael@0: michael@0: # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED michael@0: if (format & PNG_FORMAT_FLAG_AFIRST) michael@0: { michael@0: if (!colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0) michael@0: png_set_swap_alpha(png_ptr); michael@0: format &= ~PNG_FORMAT_FLAG_AFIRST; michael@0: } michael@0: # endif michael@0: michael@0: /* If there are 16 or fewer color-map entries we wrote a lower bit depth michael@0: * above, but the application data is still byte packed. michael@0: */ michael@0: if (colormap && image->colormap_entries <= 16) michael@0: png_set_packing(png_ptr); michael@0: michael@0: /* That should have handled all (both) the transforms. */ michael@0: if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR | michael@0: PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0) michael@0: png_error(png_ptr, "png_write_image: unsupported transformation"); michael@0: michael@0: { michael@0: png_const_bytep row = png_voidcast(png_const_bytep, display->buffer); michael@0: ptrdiff_t row_bytes = display->row_stride; michael@0: michael@0: if (linear) michael@0: row_bytes *= (sizeof (png_uint_16)); michael@0: michael@0: if (row_bytes < 0) michael@0: row += (image->height-1) * (-row_bytes); michael@0: michael@0: display->first_row = row; michael@0: display->row_bytes = row_bytes; michael@0: } michael@0: michael@0: /* Apply 'fast' options if the flag is set. */ michael@0: if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0) michael@0: { michael@0: png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS); michael@0: /* NOTE: determined by experiment using pngstest, this reflects some michael@0: * balance between the time to write the image once and the time to read michael@0: * it about 50 times. The speed-up in pngstest was about 10-20% of the michael@0: * total (user) time on a heavily loaded system. michael@0: */ michael@0: png_set_compression_level(png_ptr, 3); michael@0: } michael@0: michael@0: /* Check for the cases that currently require a pre-transform on the row michael@0: * before it is written. This only applies when the input is 16-bit and michael@0: * either there is an alpha channel or it is converted to 8-bit. michael@0: */ michael@0: if ((linear && alpha) || (!colormap && display->convert_to_8bit)) michael@0: { michael@0: png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, michael@0: png_get_rowbytes(png_ptr, info_ptr))); michael@0: int result; michael@0: michael@0: display->local_row = row; michael@0: if (write_16bit) michael@0: result = png_safe_execute(image, png_write_image_16bit, display); michael@0: else michael@0: result = png_safe_execute(image, png_write_image_8bit, display); michael@0: display->local_row = NULL; michael@0: michael@0: png_free(png_ptr, row); michael@0: michael@0: /* Skip the 'write_end' on error: */ michael@0: if (!result) michael@0: return 0; michael@0: } michael@0: michael@0: /* Otherwise this is the case where the input is in a format currently michael@0: * supported by the rest of the libpng write code; call it directly. michael@0: */ michael@0: else michael@0: { michael@0: png_const_bytep row = png_voidcast(png_const_bytep, display->first_row); michael@0: ptrdiff_t row_bytes = display->row_bytes; michael@0: png_uint_32 y = image->height; michael@0: michael@0: while (y-- > 0) michael@0: { michael@0: png_write_row(png_ptr, row); michael@0: row += row_bytes; michael@0: } michael@0: } michael@0: michael@0: png_write_end(png_ptr, info_ptr); michael@0: return 1; michael@0: } michael@0: michael@0: int PNGAPI michael@0: png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit, michael@0: const void *buffer, png_int_32 row_stride, const void *colormap) michael@0: { michael@0: /* Write the image to the given (FILE*). */ michael@0: if (image != NULL && image->version == PNG_IMAGE_VERSION) michael@0: { michael@0: if (file != NULL) michael@0: { michael@0: if (png_image_write_init(image)) michael@0: { michael@0: png_image_write_control display; michael@0: int result; michael@0: michael@0: /* This is slightly evil, but png_init_io doesn't do anything other michael@0: * than this and we haven't changed the standard IO functions so michael@0: * this saves a 'safe' function. michael@0: */ michael@0: image->opaque->png_ptr->io_ptr = file; michael@0: michael@0: memset(&display, 0, (sizeof display)); michael@0: display.image = image; michael@0: display.buffer = buffer; michael@0: display.row_stride = row_stride; michael@0: display.colormap = colormap; michael@0: display.convert_to_8bit = convert_to_8bit; michael@0: michael@0: result = png_safe_execute(image, png_image_write_main, &display); michael@0: png_image_free(image); michael@0: return result; michael@0: } michael@0: michael@0: else michael@0: return 0; michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, michael@0: "png_image_write_to_stdio: invalid argument"); michael@0: } michael@0: michael@0: else if (image != NULL) michael@0: return png_image_error(image, michael@0: "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION"); michael@0: michael@0: else michael@0: return 0; michael@0: } michael@0: michael@0: int PNGAPI michael@0: png_image_write_to_file(png_imagep image, const char *file_name, michael@0: int convert_to_8bit, const void *buffer, png_int_32 row_stride, michael@0: const void *colormap) michael@0: { michael@0: /* Write the image to the named file. */ michael@0: if (image != NULL && image->version == PNG_IMAGE_VERSION) michael@0: { michael@0: if (file_name != NULL) michael@0: { michael@0: FILE *fp = fopen(file_name, "wb"); michael@0: michael@0: if (fp != NULL) michael@0: { michael@0: if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer, michael@0: row_stride, colormap)) michael@0: { michael@0: int error; /* from fflush/fclose */ michael@0: michael@0: /* Make sure the file is flushed correctly. */ michael@0: if (fflush(fp) == 0 && ferror(fp) == 0) michael@0: { michael@0: if (fclose(fp) == 0) michael@0: return 1; michael@0: michael@0: error = errno; /* from fclose */ michael@0: } michael@0: michael@0: else michael@0: { michael@0: error = errno; /* from fflush or ferror */ michael@0: (void)fclose(fp); michael@0: } michael@0: michael@0: (void)remove(file_name); michael@0: /* The image has already been cleaned up; this is just used to michael@0: * set the error (because the original write succeeded). michael@0: */ michael@0: return png_image_error(image, strerror(error)); michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* Clean up: just the opened file. */ michael@0: (void)fclose(fp); michael@0: (void)remove(file_name); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, strerror(errno)); michael@0: } michael@0: michael@0: else michael@0: return png_image_error(image, michael@0: "png_image_write_to_file: invalid argument"); michael@0: } michael@0: michael@0: else if (image != NULL) michael@0: return png_image_error(image, michael@0: "png_image_write_to_file: incorrect PNG_IMAGE_VERSION"); michael@0: michael@0: else michael@0: return 0; michael@0: } michael@0: #endif /* PNG_STDIO_SUPPORTED */ michael@0: #endif /* SIMPLIFIED_WRITE */ michael@0: michael@0: #ifdef PNG_WRITE_APNG_SUPPORTED michael@0: void PNGAPI michael@0: png_write_frame_head(png_structp png_ptr, png_infop info_ptr, michael@0: png_bytepp row_pointers, png_uint_32 width, png_uint_32 height, michael@0: png_uint_32 x_offset, png_uint_32 y_offset, michael@0: png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op, michael@0: png_byte blend_op) michael@0: { michael@0: png_debug(1, "in png_write_frame_head"); michael@0: michael@0: /* there is a chance this has been set after png_write_info was called, michael@0: * so it would be set but not written. is there a way to be sure? */ michael@0: if (!(info_ptr->valid & PNG_INFO_acTL)) michael@0: png_error(png_ptr, "png_write_frame_head(): acTL not set"); michael@0: michael@0: png_write_reset(png_ptr); michael@0: michael@0: png_write_reinit(png_ptr, info_ptr, width, height); michael@0: michael@0: if ( !(png_ptr->num_frames_written == 0 && michael@0: (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) ) ) michael@0: png_write_fcTL(png_ptr, width, height, x_offset, y_offset, michael@0: delay_num, delay_den, dispose_op, blend_op); michael@0: michael@0: PNG_UNUSED(row_pointers) michael@0: } michael@0: michael@0: void PNGAPI michael@0: png_write_frame_tail(png_structp png_ptr, png_infop info_ptr) michael@0: { michael@0: png_debug(1, "in png_write_frame_tail"); michael@0: michael@0: png_ptr->num_frames_written++; michael@0: michael@0: PNG_UNUSED(info_ptr) michael@0: } michael@0: #endif /* PNG_WRITE_APNG_SUPPORTED */ michael@0: #endif /* PNG_WRITE_SUPPORTED */