本文整理汇总了C++中png_destroy_write_struct函数的典型用法代码示例。如果您正苦于以下问题:C++ png_destroy_write_struct函数的具体用法?C++ png_destroy_write_struct怎么用?C++ png_destroy_write_struct使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_destroy_write_struct函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: png_create
/**
* Write PNG image data
* @param buffer Image data
* @param filename A filename of the PNG image to save
* @param width Image width in pixels
* @param height Image height in pixels
* @param depth The bit depth of the image.
* Valid values shall include 1, 2, 4, 8, 16.
* @return TRUE if it completed successfully or FALSE otherwise
*/
static bool
png_create (char *buffer, const char *filename, Uint32 width, Uint32 height,
Sint32 depth)
{
Uint32 i;
FILE *out;
LOG_DBG ("Write %s", filename);
if (buffer == NULL)
{
LOG_ERR ("image_to_buffer() failed! (filename = %s)", filename);
return FALSE;
}
out = fopen_data (filename, "w");
if (out == NULL)
{
LOG_ERR ("can't open \"%s\"", filename);
return FALSE;
}
/* allocate and initialize png_ptr struct for writing */
png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
/* error_ptr */
(png_voidp) NULL,
/* error_fn */
NULL,
/* warn_fn */
NULL);
if (png_ptr == NULL)
{
LOG_ERR ("png_create_write_struct() failed");
return FALSE;
}
/* allocate and initialize the info structure */
png_infop info_ptr = png_create_info_struct (png_ptr);
if (png_ptr == NULL)
{
LOG_ERR ("png_create_info_struct() failed");
return FALSE;
}
/* initialize input/output for PNG file to the default functions */
png_init_io (png_ptr, out);
/* set library compression level */
png_set_IHDR (png_ptr, info_ptr,
/* width */
width,
/* height */
height,
/* bit depth */
depth,
PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_compression_level (png_ptr, Z_BEST_COMPRESSION);
png_set_text (png_ptr, info_ptr, text, sizeof text / sizeof text[0]);
png_color_16 background;
background.red = 0;
background.green = 0;
background.blue = 0;
png_set_bKGD (png_ptr, info_ptr, &background);
png_write_info (png_ptr, info_ptr);
for (i = 0; i < height; i++)
{
{
png_write_row (png_ptr, (png_byte *) & buffer[i * width * 4]);
}
}
png_write_end (png_ptr, info_ptr);
png_destroy_write_struct (&png_ptr, &info_ptr);
fclose (out);
return TRUE;
}
示例2: pixWriteStreamPng
/*!
* pixWriteStreamPng()
*
* Input: stream
* pix
* gamma (use 0.0 if gamma is not defined)
* Return: 0 if OK; 1 on error
*
* Notes:
* (1) If called from pixWriteStream(), the stream is positioned
* at the beginning of the file.
* (2) To do sequential writes of png format images to a stream,
* use pixWriteStreamPng() directly.
* (3) gamma is an optional png chunk. If no gamma value is to be
* placed into the file, use gamma = 0.0. Otherwise, if
* gamma > 0.0, its value is written into the header.
* (4) The use of gamma in png is highly problematic. For an illuminating
* discussion, see: http://hsivonen.iki.fi/png-gamma/
* (5) What is the effect/meaning of gamma in the png file? This
* gamma, which we can call the 'source' gamma, is the
* inverse of the gamma that was used in enhance.c to brighten
* or darken images. The 'source' gamma is supposed to indicate
* the intensity mapping that was done at the time the
* image was captured. Display programs typically apply a
* 'display' gamma of 2.2 to the output, which is intended
* to linearize the intensity based on the response of
* thermionic tubes (CRTs). Flat panel LCDs have typically
* been designed to give a similar response as CRTs (call it
* "backward compatibility"). The 'display' gamma is
* in some sense the inverse of the 'source' gamma.
* jpeg encoders attached to scanners and cameras will lighten
* the pixels, applying a gamma corresponding to approximately
* a square-root relation of output vs input:
* output = input^(gamma)
* where gamma is often set near 0.4545 (1/gamma is 2.2).
* This is stored in the image file. Then if the display
* program reads the gamma, it will apply a display gamma,
* typically about 2.2; the product is 1.0, and the
* display program produces a linear output. This works because
* the dark colors were appropriately boosted by the scanner,
* as described by the 'source' gamma, so they should not
* be further boosted by the display program.
* (6) As an example, with xv and display, if no gamma is stored,
* the program acts as if gamma were 0.4545, multiplies this by 2.2,
* and does a linear rendering. Taking this as a baseline
* brightness, if the stored gamma is:
* > 0.4545, the image is rendered lighter than baseline
* < 0.4545, the image is rendered darker than baseline
* In contrast, gqview seems to ignore the gamma chunk in png.
* (7) The only valid pixel depths in leptonica are 1, 2, 4, 8, 16
* and 32. However, it is possible, and in some cases desirable,
* to write out a png file using an rgb pix that has 24 bpp.
* For example, the open source xpdf SplashBitmap class generates
* 24 bpp rgb images. Consequently, we anble writing 24 bpp pix.
* To generate such a pix, you can make a 24 bpp pix without data
* and assign the data array to the pix; e.g.,
* pix = pixCreateHeader(w, h, 24);
* pixSetData(pix, rgbdata);
* See pixConvert32To24() for an example, where we get rgbdata
* from the 32 bpp pix. Caution: do not call pixSetPadBits(),
* because the alignment is wrong and you may erase part of the
* last pixel on each line.
*/
l_int32
pixWriteStreamPng(FILE *fp,
PIX *pix,
l_float32 gamma)
{
char commentstring[] = "Comment";
l_int32 i, j, k;
l_int32 wpl, d, cmflag;
l_int32 ncolors;
l_int32 *rmap, *gmap, *bmap;
l_uint32 *data, *ppixel;
png_byte bit_depth, color_type;
png_uint_32 w, h;
png_uint_32 xres, yres;
png_bytep *row_pointers;
png_bytep rowbuffer;
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette;
PIX *pixt;
PIXCMAP *cmap;
char *text;
PROCNAME("pixWriteStreamPng");
if (!fp)
return ERROR_INT("stream not open", procName, 1);
if (!pix)
return ERROR_INT("pix not defined", procName, 1);
/* Allocate the 2 data structures */
if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
(png_voidp)NULL, NULL, NULL)) == NULL)
return ERROR_INT("png_ptr not made", procName, 1);
if ((info_ptr = png_create_info_struct(png_ptr)) == NULL) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
//.........这里部分代码省略.........
示例3: onion_png_response
/**
* @short Writes an image as png to the response object
*
* @param image flat buffer with all pixels
* @param Bpp Bytes per pixel: 1 grayscale, 2 grayscale with alpha, 3 RGB, 4 RGB with alpha. Negative if in BGR format (cairo)
* @param width The width of the image
* @param height The height of the image
* @param res where to write the image, it sets the necessary structs
*/
int onion_png_response(unsigned char *image, int Bpp, int width, int height, onion_response *res){
// Many copied from example.c from libpng source code.
png_structp png_ptr;
png_infop info_ptr;
/* Create and initialize the png_struct with the desired error handler
* functions. If you want to use the default stderr and longjump method,
* you can supply NULL for the last three parameters. We also check that
* the library version is compatible with the one used at compile time,
* in case we are using dynamically linked libraries. REQUIRED.
*/
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, error, warning);
if (png_ptr == NULL)
{
return OCS_INTERNAL_ERROR;
}
/* Allocate/initialize the image information data. REQUIRED */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_write_struct(&png_ptr, NULL);
return OCS_INTERNAL_ERROR;
}
onion_png_data opd;
opd.res=res;
opd.sent=0;
png_set_write_fn(png_ptr, (void *)&opd, onion_png_write, onion_png_flush);
/* where user_io_ptr is a structure you want available to the callbacks */
onion_response_set_header(res, "Content-Type", "image/png");
if (onion_response_write_headers(res)==OR_SKIP_CONTENT) // Maybe it was HEAD.
return OCS_PROCESSED;
/* Set the image information here. Width and height are up to 2^31,
* bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
* the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
* PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
* or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
* PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
* currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
*/
if (Bpp<0){
png_set_bgr(png_ptr);
Bpp=-Bpp;
}
switch(Bpp){
case 1:
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
break;
case 2:
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY_ALPHA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
break;
case 3:
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
break;
case 4:
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
break;
default:
png_error(png_ptr, "Wrong bytes per pixel");
break;
}
png_uint_32 k;
png_bytep row_pointers[height];
if (height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
png_error (png_ptr, "Image is too tall to process in memory");
for (k = 0; k < height; k++)
row_pointers[k] = (png_byte *) (image + k*width*Bpp);
// Sets the rows to save at the image
png_set_rows(png_ptr, info_ptr, row_pointers);
// If header already sent, then there was an error.
if (opd.sent){
return OCS_PROCESSED;
}
opd.sent=1;
// Finally WRITE the image. Uses the onion_response_write via onion_png_write helper.
png_write_png(png_ptr, info_ptr, 0, NULL);
//.........这里部分代码省略.........
示例4: RE_SavePNG
int RE_SavePNG( const char *filename, byte *buf, size_t width, size_t height, int byteDepth ) {
fileHandle_t fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
unsigned int x, y;
png_byte ** row_pointers = NULL;
/* "status" contains the return value of this function. At first
it is set to a value which means 'failure'. When the routine
has finished its work, it is set to a value which means
'success'. */
int status = -1;
/* The following number is set by trial and error only. I cannot
see where it it is documented in the libpng manual.
*/
int depth = 8;
fp = ri->FS_FOpenFileWrite( filename, qtrue );
if ( !fp ) {
goto fopen_failed;
}
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
goto png_create_write_struct_failed;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
goto png_create_info_struct_failed;
}
/* Set up error handling. */
if (setjmp (png_jmpbuf (png_ptr))) {
goto png_failure;
}
/* Set image attributes. */
png_set_IHDR (png_ptr,
info_ptr,
width,
height,
depth,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
/* Initialize rows of PNG. */
row_pointers = (png_byte **)png_malloc (png_ptr, height * sizeof (png_byte *));
for ( y=0; y<height; ++y ) {
png_byte *row = (png_byte *)png_malloc (png_ptr, sizeof (uint8_t) * width * byteDepth);
row_pointers[height-y-1] = row;
for (x = 0; x < width; ++x) {
byte *px = buf + (width * y + x)*3;
*row++ = px[0];
*row++ = px[1];
*row++ = px[2];
}
}
/* Write the image data to "fp". */
// png_init_io (png_ptr, fp);
png_set_write_fn( png_ptr, (png_voidp)&fp, user_write_data, user_flush_data );
png_set_rows (png_ptr, info_ptr, row_pointers);
png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
/* The routine has successfully written the file, so we set
"status" to a value which indicates success. */
status = 0;
for (y = 0; y < height; y++) {
png_free (png_ptr, row_pointers[y]);
}
png_free (png_ptr, row_pointers);
png_failure:
png_create_info_struct_failed:
png_destroy_write_struct (&png_ptr, &info_ptr);
png_create_write_struct_failed:
ri->FS_FCloseFile( fp );
fopen_failed:
return status;
}
示例5: pixbuf2png
static int pixbuf2png(GdkPixbuf *pb, const char *output_png)
{
int i;
int width = gdk_pixbuf_get_width(pb);
int height = gdk_pixbuf_get_height(pb);
int n_channels = gdk_pixbuf_get_n_channels(pb);
int rowstride = gdk_pixbuf_get_rowstride(pb);
guchar *pixels = gdk_pixbuf_get_pixels(pb);
guchar *pixels_out = MALLOC(sizeof(guchar)*width*height*4);
//printf("pixbuf2png> opening: %s\n", output_png);
FILE *fout = FOPEN(output_png, "wb");
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr) {
asfPrintWarning("Couldn't open png file: %s\n", output_png);
return FALSE;
}
//printf("pixbuf2png> png_create_info_struct\n");
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fout);
asfPrintWarning("Couldn't open png info for %s\n", output_png);
return FALSE;
}
//printf("pixbuf2png> setjmp\n");
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fout);
asfPrintWarning("Error writing the png: %s\n", output_png);
return FALSE;
}
//printf("pixbuf2png> png_init_io\n");
png_init_io(png_ptr, fout);
//printf("pixbuf2png> png_set_IHDR\n");
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
//printf("pixbuf2png> png_write_info\n");
png_write_info(png_ptr, info_ptr);
// add a transparency byte to each pixel in the pixels_out buffer
for (i=0; i<height; ++i) {
int j;
for (j=0; j<width; ++j) {
// output: red=k, green=k+1, blue=k+2, alpha=k+3
int out_k = 4*(j + i*width);
// input: red=k, green=k+1, blue=k+2
int in_k = j*n_channels + i*rowstride;
// make it transparent, if the pixel is black
// (i.e., all channels are 0)
int trans = pixels[in_k] == 0 &&
pixels[in_k+1] == 0 && pixels[in_k+2] == 0;
pixels_out[out_k] = pixels[in_k];
pixels_out[out_k+1] = pixels[in_k+1];
pixels_out[out_k+2] = pixels[in_k+2];
pixels_out[out_k+3] = trans ? 0 : 255;
}
}
//printf("pixbuf2png> row_pointers\n");
png_bytep *row_pointers = MALLOC(sizeof(png_bytep)*height);
for (i=0; i<height; ++i)
row_pointers[i] = pixels_out + i*width*4;
//printf("pixbuf2png> png_write_image\n");
png_write_image(png_ptr, row_pointers);
//printf("pixbuf2png> png_write_end\n");
png_write_end(png_ptr, NULL);
//printf("pixbuf2png> png_destroy_write_struct\n");
png_destroy_write_struct(&png_ptr, &info_ptr);
//printf("pixbuf2png> fclose\n");
fclose(fout);
//printf("pixbuf2png> freeing row pointers\n");
FREE(row_pointers);
FREE(pixels_out);
return TRUE;
}
示例6: main
int
main(int argc, char **argv)
{
unsigned int i;
png_byte **row_pointers;
unsigned int filesize;
png_infop info_ptr;
png_structp png_ptr;
if(argc!=3)
usage(argv[0]);
rawfile = fopen(argv[1], "rb");
if(!rawfile)
usage(argv[0]);
filesize = file_get_size(rawfile);
side_size = raw_get_side(filesize);
if(side_size == 0)
usage(argv[0]);
raw_buffer = malloc(filesize);
if( !raw_buffer ) {
fprintf(stderr, "Not enough memory\n");
fclose(rawfile);
exit(EXIT_FAILURE);
}
if(fread(raw_buffer, sizeof(char), filesize, rawfile) != filesize) {
fprintf(stderr, "Raw file reading error\n");
fclose(rawfile);
free(raw_buffer);
exit(EXIT_FAILURE);
}
fclose(rawfile);
/*Create png file*/
pngfile = fopen(argv[2], "wb");
if(!pngfile) {
fprintf(stderr, "Can't open %s for writting\n", argv[2]);
free(raw_buffer);
exit(EXIT_FAILURE);
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fprintf(stderr, "Can't create png file\n");
free(raw_buffer);
fclose(pngfile);
exit(EXIT_FAILURE);
}
info_ptr = png_create_info_struct(png_ptr);
if (!png_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fprintf(stderr, "Can't create png file\n");
free(raw_buffer);
fclose(pngfile);
exit(EXIT_FAILURE);
}
png_init_io(png_ptr, pngfile);
png_set_IHDR(png_ptr, info_ptr, side_size, side_size, 8, PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
row_pointers = malloc(sizeof(png_byte *)*side_size);
if( !row_pointers ) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fprintf(stderr, "Not enough memory\n");
free(raw_buffer);
fclose(pngfile);
exit(EXIT_FAILURE);
}
png_write_info (png_ptr, info_ptr);
for(i = 0; i < side_size; i++)
row_pointers[i] = raw_buffer+i*side_size;
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(pngfile);
exit(EXIT_SUCCESS);
}
示例7: end_png
void end_png()
{
png_destroy_write_struct(&png_ptr, &info_ptr);
}
示例8: fopen
bool KPWriteImage::write2PNG(const QString& destPath)
{
FILE* file = fopen(QFile::encodeName(destPath), "wb");
if (!file)
{
kDebug( 51000 ) << "Failed to open PNG file for writing" << endl;
return false;
}
uchar *data = 0;
int bitsDepth = d->sixteenBit ? 16 : 8;
png_color_8 sig_bit;
png_bytep row_ptr;
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
png_init_io(png_ptr, file);
if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) // Intel
png_set_bgr(png_ptr);
else // PPC
png_set_swap_alpha(png_ptr);
if (d->hasAlpha)
{
png_set_IHDR(png_ptr, info_ptr, d->width, d->height, bitsDepth,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
if (d->sixteenBit)
data = new uchar[d->width * 8 * sizeof(uchar)];
else
data = new uchar[d->width * 4 * sizeof(uchar)];
}
else
{
png_set_IHDR(png_ptr, info_ptr, d->width, d->height, bitsDepth,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
if (d->sixteenBit)
data = new uchar[d->width * 6 * sizeof(uchar)];
else
data = new uchar[d->width * 3 * sizeof(uchar)];
}
sig_bit.red = bitsDepth;
sig_bit.green = bitsDepth;
sig_bit.blue = bitsDepth;
sig_bit.alpha = bitsDepth;
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
png_set_compression_level(png_ptr, 9);
// Write ICC profil.
if (!d->iccProfile.isEmpty())
{
png_set_iCCP(png_ptr, info_ptr, (png_charp)"icc", PNG_COMPRESSION_TYPE_BASE,
d->iccProfile.data(), d->iccProfile.size());
}
// Write Software info.
QString libpngver(PNG_HEADER_VERSION_STRING);
libpngver.replace('\n', ' ');
QString soft = d->kipipluginsVer;
soft.append(QString(" (%1)").arg(libpngver));
png_text text;
text.key = (png_charp)"Software";
text.text = soft.toAscii().data();
text.compression = PNG_TEXT_COMPRESSION_zTXt;
png_set_text(png_ptr, info_ptr, &(text), 1);
// Store Exif data.
QByteArray ba = d->metadata.getExif(true);
writeRawProfile(png_ptr, info_ptr, (png_charp)"exif", ba.data(), (png_uint_32) ba.size());
// Store Iptc data.
QByteArray ba2 = d->metadata.getIptc();
writeRawProfile(png_ptr, info_ptr, (png_charp)"iptc", ba2.data(), (png_uint_32) ba2.size());
// Store Xmp data.
QByteArray ba3 = d->metadata.getXmp();
writeRawProfile(png_ptr, info_ptr, (png_charp)("xmp"), ba3.data(), (png_uint_32) ba3.size());
png_write_info(png_ptr, info_ptr);
png_set_shift(png_ptr, &sig_bit);
png_set_packing(png_ptr);
uchar* ptr = (uchar*)d->data.data();
uint x, y, j;
for (y = 0; y < d->height; y++)
{
if (cancel())
{
delete [] data;
fclose(file);
png_destroy_write_struct(&png_ptr, (png_infopp) & info_ptr);
png_destroy_info_struct(png_ptr, (png_infopp) & info_ptr);
return false;
}
//.........这里部分代码省略.........
示例9: png_create_write_struct
bool PngEncoder::write( const Mat& img, const std::vector<int>& params )
{
png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
png_infop info_ptr = 0;
FILE * volatile f = 0;
int y, width = img.cols, height = img.rows;
int depth = img.depth(), channels = img.channels();
volatile bool result = false;
AutoBuffer<uchar*> buffer;
if( depth != CV_8U && depth != CV_16U )
return false;
if( png_ptr )
{
info_ptr = png_create_info_struct( png_ptr );
if( info_ptr )
{
if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
{
if( m_buf )
{
png_set_write_fn(png_ptr, this,
(png_rw_ptr)writeDataToBuf, (png_flush_ptr)flushBuf);
}
else
{
f = fopen( m_filename.c_str(), "wb" );
if( f )
png_init_io( png_ptr, (png_FILE_p)f );
}
int compression_level = -1; // Invalid value to allow setting 0-9 as valid
int compression_strategy = IMWRITE_PNG_STRATEGY_RLE; // Default strategy
bool isBilevel = false;
for( size_t i = 0; i < params.size(); i += 2 )
{
if( params[i] == IMWRITE_PNG_COMPRESSION )
{
compression_strategy = IMWRITE_PNG_STRATEGY_DEFAULT; // Default strategy
compression_level = params[i+1];
compression_level = MIN(MAX(compression_level, 0), Z_BEST_COMPRESSION);
}
if( params[i] == IMWRITE_PNG_STRATEGY )
{
compression_strategy = params[i+1];
compression_strategy = MIN(MAX(compression_strategy, 0), Z_FIXED);
}
if( params[i] == IMWRITE_PNG_BILEVEL )
{
isBilevel = params[i+1] != 0;
}
}
if( m_buf || f )
{
if( compression_level >= 0 )
{
png_set_compression_level( png_ptr, compression_level );
}
else
{
// tune parameters for speed
// (see http://wiki.linuxquestions.org/wiki/Libpng)
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
png_set_compression_level(png_ptr, Z_BEST_SPEED);
}
png_set_compression_strategy(png_ptr, compression_strategy);
png_set_IHDR( png_ptr, info_ptr, width, height, depth == CV_8U ? isBilevel?1:8 : 16,
channels == 1 ? PNG_COLOR_TYPE_GRAY :
channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
png_write_info( png_ptr, info_ptr );
if (isBilevel)
png_set_packing(png_ptr);
png_set_bgr( png_ptr );
if( !isBigEndian() )
png_set_swap( png_ptr );
buffer.allocate(height);
for( y = 0; y < height; y++ )
buffer[y] = img.data + y*img.step;
png_write_image( png_ptr, buffer );
png_write_end( png_ptr, info_ptr );
result = true;
}
}
}
}
png_destroy_write_struct( &png_ptr, &info_ptr );
//.........这里部分代码省略.........
示例10: CC_BREAK_IF
bool CCImage::_saveImageToPNG(const char * pszFilePath, bool bIsToRGB)
{
bool bRet = false;
do
{
CC_BREAK_IF(NULL == pszFilePath);
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette;
png_bytep *row_pointers;
fp = fopen(pszFilePath, "wb");
CC_BREAK_IF(NULL == fp);
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (NULL == png_ptr)
{
fclose(fp);
break;
}
info_ptr = png_create_info_struct(png_ptr);
if (NULL == info_ptr)
{
fclose(fp);
png_destroy_write_struct(&png_ptr, NULL);
break;
}
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
if (setjmp(png_jmpbuf(png_ptr)))
{
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
break;
}
#endif
png_init_io(png_ptr, fp);
if (!bIsToRGB && m_bHasAlpha)
{
png_set_IHDR(png_ptr, info_ptr, m_nWidth, m_nHeight, 8, PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
}
else
{
png_set_IHDR(png_ptr, info_ptr, m_nWidth, m_nHeight, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
}
palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof (png_color));
png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
row_pointers = (png_bytep *)malloc(m_nHeight * sizeof(png_bytep));
if(row_pointers == NULL)
{
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
break;
}
if (!m_bHasAlpha)
{
for (int i = 0; i < (int)m_nHeight; i++)
{
row_pointers[i] = (png_bytep)m_pData + i * m_nWidth * 3;
}
png_write_image(png_ptr, row_pointers);
free(row_pointers);
row_pointers = NULL;
}
else
{
if (bIsToRGB)
{
unsigned char *pTempData = new unsigned char[m_nWidth * m_nHeight * 3];
if (NULL == pTempData)
{
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
break;
}
for (int i = 0; i < m_nHeight; ++i)
{
for (int j = 0; j < m_nWidth; ++j)
{
pTempData[(i * m_nWidth + j) * 3] = m_pData[(i * m_nWidth + j) * 4];
pTempData[(i * m_nWidth + j) * 3 + 1] = m_pData[(i * m_nWidth + j) * 4 + 1];
pTempData[(i * m_nWidth + j) * 3 + 2] = m_pData[(i * m_nWidth + j) * 4 + 2];
}
}
//.........这里部分代码省略.........
示例11: MIN
bool PngEncoder::write( const Mat& img, const Vector<int>& params )
{
int compression_level = 0;
for( size_t i = 0; i < params.size(); i += 2 )
{
if( params[i] == CV_IMWRITE_PNG_COMPRESSION )
{
compression_level = params[i+1];
compression_level = MIN(MAX(compression_level, 0), MAX_MEM_LEVEL);
}
}
png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
png_infop info_ptr = 0;
FILE* f = 0;
int y, width = img.cols, height = img.rows;
int depth = img.depth(), channels = img.channels();
bool result = false;
AutoBuffer<uchar*> buffer;
if( depth != CV_8U && depth != CV_16U )
return false;
if( png_ptr )
{
info_ptr = png_create_info_struct( png_ptr );
if( info_ptr )
{
if( setjmp( png_ptr->jmpbuf ) == 0 )
{
if( m_buf )
{
png_set_write_fn(png_ptr, this,
(png_rw_ptr)writeDataToBuf, (png_flush_ptr)flushBuf);
}
else
{
f = fopen( m_filename.c_str(), "wb" );
if( f )
png_init_io( png_ptr, f );
}
if( m_buf || f )
{
if( compression_level > 0 )
{
png_set_compression_mem_level( png_ptr, compression_level );
}
else
{
// tune parameters for speed
// (see http://wiki.linuxquestions.org/wiki/Libpng)
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
png_set_compression_level(png_ptr, Z_BEST_SPEED);
}
png_set_compression_strategy(png_ptr, Z_HUFFMAN_ONLY);
png_set_IHDR( png_ptr, info_ptr, width, height, depth == CV_8U ? 8 : 16,
channels == 1 ? PNG_COLOR_TYPE_GRAY :
channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
png_write_info( png_ptr, info_ptr );
png_set_bgr( png_ptr );
if( !isBigEndian() )
png_set_swap( png_ptr );
buffer.allocate(height);
for( y = 0; y < height; y++ )
buffer[y] = img.data + y*img.step;
png_write_image( png_ptr, buffer );
png_write_end( png_ptr, info_ptr );
result = true;
}
}
}
}
png_destroy_write_struct( &png_ptr, &info_ptr );
if(f) fclose( f );
return result;
}
示例12: fopen
void GBTileView::savePNG(const char* name)
{
uint8_t writeBuffer[1024 * 3];
FILE* fp = fopen(name, "wb");
if (!fp) {
systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name);
return;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if (!png_ptr) {
fclose(fp);
return;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return;
}
if (setjmp(png_ptr->jmpbuf)) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr,
info_ptr,
w,
h,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
uint8_t* b = writeBuffer;
int sizeX = w;
int sizeY = h;
uint8_t* pixU8 = (uint8_t*)data;
for (int y = 0; y < sizeY; y++) {
for (int x = 0; x < sizeX; x++) {
int blue = *pixU8++;
int green = *pixU8++;
int red = *pixU8++;
*b++ = red;
*b++ = green;
*b++ = blue;
}
png_write_row(png_ptr, writeBuffer);
b = writeBuffer;
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
}
示例13: main
//.........这里部分代码省略.........
int channels = png_get_channels(png_ptr, info_ptr);
if (channels != 4)
{
fprintf(stderr, "channels must be 4.\n");
fclose(fpin);
return 1;
}
fclose(fpin);
/* now write jpeg */
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW jrow_pointer[1];
FILE *outfp;
char filename[strlen(outfile) + 10];
strcpy(filename, outfile);
strcat(filename, ".rgb.jpg");
outfp = fopen(filename, "wb");
if (!outfp)
{
perror(filename);
return 1;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfp);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, jpeg_quality, 1);
jpeg_start_compress(&cinfo, 1);
unsigned char *row = malloc(width * 3);
while (cinfo.next_scanline < cinfo.image_height)
{
int x;
jrow_pointer[0] = row;
unsigned char *source = row_pointers[cinfo.next_scanline];
for (x = 0; x < width; ++x)
{
row[x * 3 + 0] = source[0];
row[x * 3 + 1] = source[1];
row[x * 3 + 2] = source[2];
source += 4;
}
jpeg_write_scanlines(&cinfo, jrow_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(outfp);
jpeg_destroy_compress(&cinfo);
/* and write png */
strcpy(filename, outfile);
strcat(filename, ".a.png");
outfp = fopen(filename, "wb");
if (!outfp)
{
perror(filename);
return 1;
}
png_structp png_ptr_w = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
png_infop info_ptr_w = png_create_info_struct(png_ptr_w);
if (setjmp(png_jmpbuf(png_ptr_w)))
{
png_destroy_write_struct(&png_ptr_w, &info_ptr_w);
fclose(outfp);
return 1;
}
png_init_io(png_ptr_w, outfp);
png_set_IHDR(png_ptr_w, info_ptr_w, width, height, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
/* turn RGBA into A, in-place */
int x, y;
for (y=0; y < height; ++y)
{
unsigned char *source = row_pointers[y];
unsigned char *dst = source;
for (x=0; x < width; ++x)
{
*dst++ = source[3];
source += 4;
}
}
png_set_rows(png_ptr_w, info_ptr_w, row_pointers);
png_write_png(png_ptr_w, info_ptr_w, PNG_TRANSFORM_IDENTITY, 0);
png_write_end(png_ptr_w, info_ptr_w);
png_destroy_write_struct(&png_ptr_w, &info_ptr_w);
fclose(outfp);
return 0;
}
示例14: PNGWriteClose
void PNGWriteClose(png_structp png, png_infop info) {
if (!setjmp(png_jmpbuf(png))) {
png_write_end(png, info);
}
png_destroy_write_struct(&png, &info);
}
示例15: png_destroy_read_struct
bool PNGImage::CleanMemoryUsage()
{
if(mRead == true && mWrite == true)
return false;
if(mRead == true)
{
if(mPng != NULL && mInfo != NULL)
{
png_destroy_read_struct(&mPng, &mInfo, NULL);
mPng = NULL;
mInfo = NULL;
}
else
{
if(mPng != NULL)
{
png_destroy_read_struct(&mPng, NULL, NULL);
mPng = NULL;
}
else if(mInfo != NULL)
{
png_read_info(mPng, mInfo);
mInfo = NULL;
}
}
for (unsigned int y = 0; y < mHeight; y++)
dFree(mRowPointers[y]);
dFree(mRowPointers);
mRowPointers= NULL;
}
else if(mWrite == true)
{
if(mPng != NULL && mInfo != NULL)
{
png_destroy_write_struct(&mPng, &mInfo);
mPng = NULL;
mInfo = NULL;
}
else
{
if(mPng != NULL)
{
png_destroy_write_struct(&mPng, NULL);
mPng = NULL;
}
if(mInfo != NULL)
{
png_read_info(mPng, mInfo);
mInfo = NULL;
}
}
for (unsigned int y = 0; y < mHeight; y++)
dFree(mRowPointers[y]);
dFree(mRowPointers);
mRowPointers = NULL;
}
return true;
}