本文整理汇总了C++中png_write_end函数的典型用法代码示例。如果您正苦于以下问题:C++ png_write_end函数的具体用法?C++ png_write_end怎么用?C++ png_write_end使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_write_end函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: png_create_info_struct
size_t ImageEncoderPNG::encode( const void * _buffer, size_t _size, const CodecDataInfo* _bufferDataInfo )
{
(void)_size;
const ImageCodecDataInfo* dataInfo = static_cast<const ImageCodecDataInfo*>( _bufferDataInfo );
// allocate/initialize the image information data.
png_infop info_ptr = png_create_info_struct( m_png_ptr );
if( info_ptr == nullptr )
{
LOGGER_ERROR(m_serviceProvider)("PNG encoder error: Can't create info structure"
);
return 0;
}
int color_type;
if( dataInfo->channels == 1 )
{
color_type = PNG_COLOR_TYPE_GRAY;
}
else if( dataInfo->channels == 3 )
{
color_type = PNG_COLOR_TYPE_RGB;
}
else if( dataInfo->channels == 4 )
{
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
}
else
{
LOGGER_ERROR(m_serviceProvider)("PNG codec error: unsupported image format channels %d"
, dataInfo->channels
);
png_destroy_info_struct( m_png_ptr, &info_ptr );
return 0;
}
png_uint_32 width = (png_uint_32)dataInfo->width;
png_uint_32 height = (png_uint_32)dataInfo->height;
int pixel_depth = 8;
png_set_IHDR( m_png_ptr, info_ptr, width, height, pixel_depth, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
png_set_bgr( m_png_ptr );
// Write the file header information.
png_write_info( m_png_ptr, info_ptr );
size_t pitch = m_options.pitch;
png_bytep png_buffer = (png_bytep)_buffer;
for( png_uint_32 k = 0; k < height; ++k)
{
png_write_row( m_png_ptr, png_buffer );
png_buffer += pitch;
}
//}
// It is REQUIRED to call this to finish writing the rest of the file
// Bug with png_flush
png_write_end( m_png_ptr, info_ptr );
png_destroy_info_struct( m_png_ptr, &info_ptr );
size_t writeBytes = pitch * height;
return writeBytes;
}
示例2: export_png
//.........这里部分代码省略.........
gint compression,
gint bit_depth)
{
gint i, src_x, src_y;
png_uint_32 width, height;
guchar *pixels;
png_color_16 white;
int png_color_type;
gchar format_string[16];
const Babl *format;
src_x = result->x;
src_y = result->y;
width = result->width;
height = result->height;
{
const Babl *babl = gegl_buffer_get_format (input);
if (bit_depth != 16)
bit_depth = 8;
if (babl_format_has_alpha (babl))
if (babl_format_get_n_components (babl) != 2)
{
png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
strcpy (format_string, "R'G'B'A ");
}
else
{
png_color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
strcpy (format_string, "Y'A ");
}
else
if (babl_format_get_n_components (babl) != 1)
{
png_color_type = PNG_COLOR_TYPE_RGB;
strcpy (format_string, "R'G'B' ");
}
else
{
png_color_type = PNG_COLOR_TYPE_GRAY;
strcpy (format_string, "Y' ");
}
}
if (bit_depth == 16)
strcat (format_string, "u16");
else
strcat (format_string, "u8");
if (setjmp (png_jmpbuf (png)))
return -1;
png_set_compression_level (png, compression);
png_set_IHDR (png, info,
width, height, bit_depth, png_color_type,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
if (png_color_type == PNG_COLOR_TYPE_RGB || png_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
{
white.red = 0xff;
white.blue = 0xff;
white.green = 0xff;
}
else
white.gray = 0xff;
png_set_bKGD (png, info, &white);
png_write_info (png, info);
#if BYTE_ORDER == LITTLE_ENDIAN
if (bit_depth > 8)
png_set_swap (png);
#endif
format = babl_format (format_string);
pixels = g_malloc0 (width * babl_format_get_bytes_per_pixel (format));
for (i=0; i< height; i++)
{
GeglRectangle rect;
rect.x = src_x;
rect.y = src_y+i;
rect.width = width;
rect.height = 1;
gegl_buffer_get (input, &rect, 1.0, babl_format (format_string), pixels, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
png_write_rows (png, &pixels, 1);
}
png_write_end (png, info);
g_free (pixels);
return 0;
}
示例3: SavePNG
void SavePNG( const char *name, const byte *pic, int width, int height, int numBytes, qboolean flip )
{
png_structp png;
png_infop info;
int i;
int row_stride;
byte *buffer;
byte *row;
png_bytep *row_pointers;
png = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
if ( !png )
{
return;
}
// Allocate/initialize the image information data
info = png_create_info_struct( png );
if ( !info )
{
png_destroy_write_struct( &png, ( png_infopp ) NULL );
return;
}
png_compressed_size = 0;
buffer = ri.Hunk_AllocateTempMemory( width * height * numBytes );
// set error handling
if ( setjmp( png_jmpbuf( png ) ) )
{
ri.Hunk_FreeTempMemory( buffer );
png_destroy_write_struct( &png, &info );
return;
}
png_set_write_fn( png, buffer, png_write_data, png_flush_data );
switch ( numBytes )
{
default:
png_set_IHDR( png, info, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
break;
case 3:
png_set_IHDR( png, info, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
break;
case 2:
png_set_IHDR( png, info, width, height, 8, PNG_COLOR_TYPE_GA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
break;
case 1:
png_set_IHDR( png, info, width, height, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
break;
}
// write the file header information
png_write_info( png, info );
row_pointers = ri.Hunk_AllocateTempMemory( height * sizeof( png_bytep ) );
if ( setjmp( png_jmpbuf( png ) ) )
{
ri.Hunk_FreeTempMemory( row_pointers );
ri.Hunk_FreeTempMemory( buffer );
png_destroy_write_struct( &png, &info );
return;
}
row_stride = width * numBytes;
row = ( byte * ) pic + ( height - 1 ) * row_stride;
if ( flip )
{
for ( i = height - 1; i >= 0; i-- )
{
row_pointers[ i ] = row;
row -= row_stride;
}
}
else
{
for ( i = 0; i < height; i++ )
{
row_pointers[ i ] = row;
row -= row_stride;
}
}
png_write_image( png, row_pointers );
png_write_end( png, info );
// clean up after the write, and free any memory allocated
png_destroy_write_struct( &png, &info );
ri.Hunk_FreeTempMemory( row_pointers );
//.........这里部分代码省略.........
示例4: savePNGto
static int savePNGto(FILE *fp, gPixmap *pixmap)
{
gUnmanagedSurface *surface = pixmap->surface;
if (!surface)
return -2;
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (!png_ptr)
{
eDebug("[ePNG] couldn't allocate write struct");
return -2;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
eDebug("[ePNG] failed to allocate info struct");
png_destroy_write_struct(&png_ptr, 0);
return -3;
}
png_set_IHDR(png_ptr, info_ptr, surface->x, surface->y, surface->bpp/surface->bypp,
PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
if (setjmp(png_jmpbuf(png_ptr)))
{
eDebug("[ePNG] png setjump failed or activated");
png_destroy_write_struct(&png_ptr, &info_ptr);
return -4;
}
png_init_io(png_ptr, fp);
png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
png_byte *row_pointer;
png_byte *cr = new png_byte[surface->y * surface->stride];
if (cr == NULL)
{
eDebug("[ePNG] failed to allocate memory image");
return -5;
}
for (int i = 0; i < surface->y; ++i)
{
row_pointer = ((png_byte*)surface->data) + i * surface->stride;
if (surface->bypp == 4)
{
memcpy(cr, row_pointer, surface->stride);
for (int j = 0; j < surface->stride; j += 4)
{
unsigned char tmp = cr[j];
cr[j] = cr[j+2];
cr[j+2] = tmp;
}
png_write_row(png_ptr, cr);
}
else
png_write_row(png_ptr, row_pointer);
}
delete [] cr;
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
return 0;
}
示例5: write_png_rgb_to_buffer
CAMLprim value write_png_rgb_to_buffer(value buffer, value width, value height,
value with_alpha) {
CAMLparam4(buffer, width, height, with_alpha);
CAMLlocal1(vres);
png_structp png_ptr;
png_infop info_ptr;
/* static */
struct mem_buffer state;
int w, h, a;
/* initialise - put this before png_write_png() call */
state.buffer = NULL;
state.size = 0;
w = Int_val(width);
h = Int_val(height);
a = Bool_val(with_alpha);
if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL)) == NULL )
failwith("png_create_write_struct");
if((info_ptr = png_create_info_struct(png_ptr)) == NULL ) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
failwith("png_create_info_struct");
}
/* error handling */
if (setjmp(png_jmpbuf(png_ptr))) {
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_write_struct(&png_ptr, &info_ptr);
failwith("png write error");
}
/* the final arg is NULL because we dont need in flush() */
png_set_write_fn(png_ptr, &state, png_write_data_to_buffer, NULL);
/* we use system default compression */
/* png_set_filter(png_ptr, 0, PNG_FILTER_NONE |
PNG_FILTER_SUB | PNG_FILTER_PAETH ); */
/* png_set_compression...() */
png_set_IHDR(png_ptr, info_ptr, w, h,
8 /* fixed */,
a ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB, /* fixed */
PNG_INTERLACE_ADAM7,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
/* infos... */
png_write_info(png_ptr, info_ptr);
{
int rowbytes, i;
png_bytep *row_pointers;
char *buf = String_val(buffer);
row_pointers = (png_bytep*)stat_alloc(sizeof(png_bytep) * h);
rowbytes= png_get_rowbytes(png_ptr, info_ptr);
for(i=0; i< h; i++) {
row_pointers[i] = (png_bytep)(buf + rowbytes * i);
}
png_write_image(png_ptr, row_pointers);
stat_free((void*)row_pointers);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
vres = caml_alloc_string(state.size);
memcpy(String_val(vres), state.buffer, state.size);
free(state.buffer);
CAMLreturn(vres);
}
示例6: PyObject_AsFileDescriptor
//.........这里部分代码省略.........
try
{
struct png_color_8_struct sig_bit;
png_uint_32 row;
row_pointers = new png_bytep[height];
for (row = 0; row < (png_uint_32)height; ++row)
{
row_pointers[row] = pixBuffer + row * width * 4;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
throw Py::RuntimeError("Could not create write struct");
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
throw Py::RuntimeError("Could not create info struct");
}
if (setjmp(png_jmpbuf(png_ptr)))
{
throw Py::RuntimeError("Error building image");
}
if (fp)
{
png_init_io(png_ptr, fp);
}
else
{
png_set_write_fn(png_ptr, (void*)py_fileobj.ptr(),
&write_png_data, &flush_png_data);
}
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);
// Save the dpi of the image in the file
if (args.size() == 5)
{
double dpi = Py::Float(args[4]);
size_t dots_per_meter = (size_t)(dpi / (2.54 / 100.0));
png_set_pHYs(png_ptr, info_ptr, dots_per_meter, dots_per_meter, PNG_RESOLUTION_METER);
}
// this a a color image!
sig_bit.gray = 0;
sig_bit.red = 8;
sig_bit.green = 8;
sig_bit.blue = 8;
/* if the image has an alpha channel then */
sig_bit.alpha = 8;
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
png_write_info(png_ptr, info_ptr);
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
}
catch (...)
{
if (png_ptr && info_ptr)
{
png_destroy_write_struct(&png_ptr, &info_ptr);
}
delete [] row_pointers;
if (fp && close_file)
{
fclose(fp);
}
/* Changed calls to png_destroy_write_struct to follow
http://www.libpng.org/pub/png/libpng-manual.txt.
This ensures the info_ptr memory is released.
*/
throw;
}
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
#if PY3K
if (fp)
{
fflush(fp);
}
#endif
if (fp && close_file)
{
fclose(fp);
}
if (PyErr_Occurred()) {
throw Py::Exception();
} else {
return Py::Object();
}
}
示例7: main
//.........这里部分代码省略.........
break;
}
if (lr) {
xisflipped = !xisflipped;
}
if (tb) {
yisflipped = !yisflipped;
}
row = calloc(sizeof(u_char), (xisy) ? height : width);
if (!row) {
die("Memory allocation error");
}
png_ptr = png_create_write_struct(
PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (!png_ptr) {
die("Cannot allocate png_structp");
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
die("Cannot allocate png_infop");
}
if (setjmp(png_jmpbuf(png_ptr))) {
die("Error on png write");
}
if (pngFile) {
out = fopen(pngFile, "wb");
if (!out) {
die("Cannot create output file");
}
} else {
out = stdout;
SET_BINARY(STDOUT_FILENO);
}
png_init_io(png_ptr, out);
/* Turning off filtering yields a large speed improvement at a
modest price in file size */
png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
if (nwidth == -1) {
nwidth = width;
}
png_set_IHDR(png_ptr, info_ptr, ((xisy) ? height : width) * denom / num,
((xisy) ? width : height) * denom / num,
8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
for (y = 0; (y < info_ptr->height); y++) {
int x;
u_char *p;
row_pointer = row;
p = row;
for (x = 0; (x < info_ptr->width); x++) {
int tx, ty;
int accum = 0, total = 0;
int ty1 = (xisy ? x : y) * num / denom;
int tx1 = (xisy ? y : x) * num / denom;
int tx2, ty2;
int xsteps, ysteps;
if (!antialias) {
tx2 = tx1 + 1;
ty2 = ty1 + 1;
} else {
ty2 = (xisy ? (x + 1) : (y + 1)) * num / denom;
tx2 = (xisy ? (y + 1) : (x + 1)) * num / denom;
}
ysteps = abs(ty2 - ty1);
xsteps = abs(tx2 - tx1);
if (xisflipped) {
tx1 = width - 1 - tx1;
tx2 = width - 1 - tx2;
}
if (yisflipped) {
ty1 = height - 1 - ty1;
ty2 = height - 1 - ty2;
}
ty = ty1;
while (ty != ty2) {
tx = tx1;
while (tx != tx2) {
accum += GetBWPixel(raster, ty, tx);
total++;
tx += sign(tx2 - tx1);
}
ty += sign(ty2 - ty1);
}
if (total > 0) {
*p = accum / total;
}
p++;
}
png_write_row(png_ptr, row_pointer);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
if (out != stdout) {
fclose(out);
}
return 0;
}
示例8: Save
//.........这里部分代码省略.........
case FIC_CMYK:
break;
}
// write possible ICC profile
FIICCPROFILE *iccProfile = FreeImage_GetICCProfile(dib);
if (iccProfile->size && iccProfile->data) {
png_set_iCCP(png_ptr, info_ptr, "Embedded Profile", 0, (png_charp)iccProfile->data, iccProfile->size);
}
// write metadata
WriteMetadata(png_ptr, info_ptr, dib);
// Optional gamma chunk is strongly suggested if you have any guess
// as to the correct gamma of the image.
// png_set_gAMA(png_ptr, info_ptr, gamma);
// set the transparency table
if ((pixel_depth == 8) && (FreeImage_IsTransparent(dib)) && (FreeImage_GetTransparencyCount(dib) > 0)) {
png_set_tRNS(png_ptr, info_ptr, FreeImage_GetTransparencyTable(dib), FreeImage_GetTransparencyCount(dib), NULL);
}
// set the background color
if(FreeImage_HasBackgroundColor(dib)) {
png_color_16 image_background;
RGBQUAD rgbBkColor;
FreeImage_GetBackgroundColor(dib, &rgbBkColor);
memset(&image_background, 0, sizeof(png_color_16));
image_background.blue = rgbBkColor.rgbBlue;
image_background.green = rgbBkColor.rgbGreen;
image_background.red = rgbBkColor.rgbRed;
image_background.index = rgbBkColor.rgbReserved;
png_set_bKGD(png_ptr, info_ptr, &image_background);
}
// Write the file header information.
png_write_info(png_ptr, info_ptr);
// write out the image data
#ifndef FREEIMAGE_BIGENDIAN
if (bit_depth == 16) {
// turn on 16 bit byte swapping
png_set_swap(png_ptr);
}
#endif
int number_passes = 1;
if (bInterlaced) {
number_passes = png_set_interlace_handling(png_ptr);
}
if ((pixel_depth == 32) && (!has_alpha_channel)) {
BYTE *buffer = (BYTE *)malloc(width * 3);
// transparent conversion to 24-bit
// the number of passes is either 1 for non-interlaced images, or 7 for interlaced images
for (int pass = 0; pass < number_passes; pass++) {
for (png_uint_32 k = 0; k < height; k++) {
FreeImage_ConvertLine32To24(buffer, FreeImage_GetScanLine(dib, height - k - 1), width);
png_write_row(png_ptr, buffer);
}
}
free(buffer);
} else {
// the number of passes is either 1 for non-interlaced images, or 7 for interlaced images
for (int pass = 0; pass < number_passes; pass++) {
for (png_uint_32 k = 0; k < height; k++) {
png_write_row(png_ptr, FreeImage_GetScanLine(dib, height - k - 1));
}
}
}
// It is REQUIRED to call this to finish writing the rest of the file
// Bug with png_flush
png_write_end(png_ptr, info_ptr);
// clean up after the write, and free any memory allocated
if (palette) {
png_free(png_ptr, palette);
}
png_destroy_write_struct(&png_ptr, &info_ptr);
return TRUE;
} catch (const char *text) {
FreeImage_OutputMessageProc(s_format_id, text);
}
}
return FALSE;
}
示例9: IMG_SavePNG_RW
//.........这里部分代码省略.........
if (!fmt->Amask) { /* check for 32bit but no alpha */
funky_format=1;
}else{
/* Check for ARGB/ABGR/GBAR/RABG/etc surfaces.*/
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if(fmt->Rmask!=0xFF000000
|| fmt->Gmask!=0x00FF0000
|| fmt->Bmask!=0x0000FF00
|| fmt->Amask!=0x000000FF){
#else
if(fmt->Rmask!=0x000000FF
|| fmt->Gmask!=0x0000FF00
|| fmt->Bmask!=0x00FF0000
|| fmt->Amask!=0xFF000000){
#endif
funky_format=1;
}
}
}else{ /* 555 or 565 16 bit color */
funky_format=1;
}
if (funky_format) {
/* Allocate non-funky format, and copy pixeldata in*/
if(fmt->Amask){
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif
}else{
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0xff0000, 0x00ff00, 0x0000ff, 0x00000000);
#else
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000);
#endif
}
if(!tempsurf){
SDL_SetError("Couldn't allocate temp surface");
goto savedone;
}
if(surf->flags&SDL_SRCALPHA){
temp_alpha=fmt->alpha;
used_alpha=1;
SDL_SetAlpha(surf,0,255); /* Set for an opaque blit */
}else{
used_alpha=0;
}
if(SDL_BlitSurface(surf,NULL,tempsurf,NULL)!=0){
SDL_SetError("Couldn't blit surface to temp surface");
SDL_FreeSurface(tempsurf);
goto savedone;
}
if (used_alpha) {
SDL_SetAlpha(surf,SDL_SRCALPHA,(Uint8)temp_alpha); /* Restore alpha settings*/
}
for(i=0;i<tempsurf->h;i++){
row_pointers[i]= ((png_byte*)tempsurf->pixels) + i*tempsurf->pitch;
}
if(SDL_MUSTLOCK(tempsurf)){
SDL_LockSurface(tempsurf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(tempsurf)){
SDL_UnlockSurface(tempsurf);
}
SDL_FreeSurface(tempsurf);
} else {
for(i=0;i<surf->h;i++){
row_pointers[i]= ((png_byte*)surf->pixels) + i*surf->pitch;
}
if(SDL_MUSTLOCK(surf)){
SDL_LockSurface(surf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(surf)){
SDL_UnlockSurface(surf);
}
}
}
png_write_end(png_ptr, NULL);
ret=0; /* got here, so nothing went wrong. YAY! */
savedone: /* clean up and return */
png_destroy_write_struct(&png_ptr,&info_ptr);
if (palette) {
free(palette);
}
if (palette_alpha) {
free(palette_alpha);
}
if (row_pointers) {
free(row_pointers);
}
return ret;
}
示例10: pngconv_lossless2png
//.........这里部分代码省略.........
PNG_FILTER_TYPE_DEFAULT);
if (format == 3) {
register int i;
if (index_data_count == 0) {
fprintf(stderr, "jpegconv_lossless2png: index_data_count == 0 at line(%d)\n", __LINE__);
png_destroy_write_struct((png_structpp) &png_ptr,
(png_infopp) &png_info_ptr);
return NULL;
}
png_palette = (png_colorp) malloc(sizeof(png_color)*index_data_count);
png_set_packing(png_ptr);
if (tag_no == 20) {
swf_rgb_t *rgb_list = index_data;
for (i=0 ; i < index_data_count ; i++) {
png_palette[i].red = rgb_list[i].red;
png_palette[i].green = rgb_list[i].green;
png_palette[i].blue = rgb_list[i].blue;
}
} else {
swf_rgba_t *rgba_list = index_data;
png_bytep trans = NULL;
int num_trans = 0;
png_color_16p trans_values = NULL;
for (i=0 ; i < index_data_count ; i++) {
png_palette[i].red = rgba_list[i].red;
png_palette[i].green = rgba_list[i].green;
png_palette[i].blue = rgba_list[i].blue;
}
// scanning to end of transparent pixel
for (i = index_data_count - 1 ; 0 <= i ; i--) {
if (rgba_list[i].alpha < 254) { // 254 XXX
break;
}
}
num_trans = i + 1;
if (num_trans > 0) {
trans = malloc(num_trans);
for (i = 0 ; i < num_trans ; i++) {
trans[i] = rgba_list[i].alpha;
}
png_set_tRNS(png_ptr, png_info_ptr, trans, num_trans,
trans_values);
free(trans);
}
}
png_set_PLTE( png_ptr, png_info_ptr, png_palette, index_data_count);
free(png_palette);
}
png_image_data = (png_bytepp) malloc(png_height * sizeof(png_bytep));
if (color_type == PNG_COLOR_TYPE_PALETTE) {
for (y=0 ; y < png_height ; y++) {
png_image_data[y] = (png_bytep) malloc(png_get_rowbytes(png_ptr, png_info_ptr));
for (x=0 ; x < png_width ; x++) {
unsigned char *data = image_data;
png_image_data[y][x] = data[x + y*((png_width +3) & -4)];
}
}
} else if (color_type == PNG_COLOR_TYPE_RGB) {
swf_xrgb_t *xrgb_list = image_data;
for (y=0 ; y < png_height ; y++) {
png_image_data[y] = (png_bytep) malloc(png_get_rowbytes(png_ptr, png_info_ptr));
for (x=0 ; x < png_width ; x++) {
png_image_data[y][3*x] = xrgb_list[x+y*png_width].red;
png_image_data[y][3*x+1] = xrgb_list[x+y*png_width].green;
png_image_data[y][3*x+2] = xrgb_list[x+y*png_width].blue;
}
}
} else {
swf_argb_t *argb_list = image_data;
for (y=0 ; y < png_height ; y++) {
png_image_data[y] = (png_bytep) malloc(png_get_rowbytes(png_ptr, png_info_ptr));
for (x=0 ; x < png_width ; x++) {
png_image_data[y][4*x] = argb_list[x+y*png_width].red;
png_image_data[y][4*x+1] = argb_list[x+y*png_width].green;
png_image_data[y][4*x+2] = argb_list[x+y*png_width].blue;
png_image_data[y][4*x+3] = argb_list[x+y*png_width].alpha;
}
}
}
png_buff.data = NULL;
png_buff.data_len = 0;
png_buff.data_offset = 0;
png_data_write((png_structp) png_ptr, (my_png_buffer*) &png_buff);
png_write_info(png_ptr, png_info_ptr);
png_write_image(png_ptr, png_image_data);
png_write_end(png_ptr, png_info_ptr);
//
for (y=0 ; y < png_height ; y++) {
free(png_image_data[y]);
}
free(png_image_data);
png_destroy_write_struct((png_structpp) &png_ptr,
(png_infopp) &png_info_ptr);
*length = png_buff.data_offset;
return png_buff.data;
}
示例11: save_image
//saves the given SDL structure into a given filename.
void save_image(surface surf, const std::string &filename)
{
//opens the actual file
const util::scoped_FILE file(fopen(filename.c_str(),"wb"));
//initializes PNG write structures
//TODO: review whether providing NULL error handlers is something
//sensible
png_struct* png_ptr = png_create_write_struct
(PNG_LIBPNG_VER_STRING, reinterpret_cast<png_voidp>(png_voidp_NULL),
png_error_ptr_NULL, png_error_ptr_NULL);
if(!png_ptr)
throw exploder_failure("Unable to initialize the png write structure");
png_info* info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
png_destroy_write_struct(&png_ptr,
static_cast<png_infopp>(NULL));
throw exploder_failure("Unable to initialize the png info structure");
}
//instructs the PNG library to use the open file
png_init_io(png_ptr, file);
//sets compression level to the maximum
png_set_compression_level(png_ptr,
Z_BEST_COMPRESSION);
//configures the header
png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h,
8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
//puts the actual image data in the row_pointers array
png_byte **row_pointers = new png_byte *[surf->h];
surface_lock lock(surf);
//converts the data to the RGBA format. We cannot pass SDL data
//directly to the png lib, even if we know its pixel format, because of
//endianness problems.
util::scoped_array<rgba> rgba_data(new rgba[surf->w * surf->h]);
Uint32 *surf_data = lock.pixels();
int pos = 0;
for(int y = 0; y < surf->h; ++y) {
row_pointers[y] = reinterpret_cast<png_byte*>(rgba_data + pos);
for(int x = 0; x < surf->w; ++x) {
Uint8 red, green, blue, alpha;
SDL_GetRGBA(*surf_data, surf->format, &red, &green, &blue, &alpha);
rgba_data[pos].r = red;
rgba_data[pos].g = green;
rgba_data[pos].b = blue;
rgba_data[pos].a = alpha;
pos++;
surf_data++;
}
}
png_set_rows(png_ptr, info_ptr, row_pointers);
//writes the actual image data
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
//cleans everything
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
}
示例12: image_png_compress
int image_png_compress(MediaScanImage *i, MediaScanThumbSpec *spec) {
int j, x, y;
int color_space = PNG_COLOR_TYPE_RGB_ALPHA;
volatile unsigned char *ptr = NULL;
png_structp png_ptr;
png_infop info_ptr;
Buffer *buf;
if (!i->_pixbuf_size) {
LOG_WARN("PNG compression requires pixbuf data (%s)\n", i->path);
return 0;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
FATAL("Could not initialize libpng\n");
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, NULL);
FATAL("Could not initialize libpng\n");
}
// Initialize buffer for compressed data
buf = (Buffer *)malloc(sizeof(Buffer));
buffer_init(buf, BUF_SIZE);
i->_dbuf = (void *)buf;
png_set_write_fn(png_ptr, buf, image_png_write_buf, image_png_flush_buf);
if (setjmp(png_jmpbuf(png_ptr))) {
if (ptr != NULL)
free((void *)ptr);
return 0;
}
// Match output color space with input file
switch (i->channels) {
case 4:
case 3:
LOG_DEBUG("PNG output color space set to RGBA\n");
color_space = PNG_COLOR_TYPE_RGB_ALPHA;
break;
case 2:
case 1:
LOG_DEBUG("PNG output color space set to gray alpha\n");
color_space = PNG_COLOR_TYPE_GRAY_ALPHA;
break;
}
png_set_IHDR(png_ptr, info_ptr, spec->width, spec->height, 8, color_space,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
ptr = (unsigned char *)malloc(png_get_rowbytes(png_ptr, info_ptr));
j = 0;
if (color_space == PNG_COLOR_TYPE_GRAY_ALPHA) {
for (y = 0; y < spec->height; y++) {
for (x = 0; x < spec->width; x++) {
ptr[x * 2] = COL_BLUE(i->_pixbuf[j]);
ptr[x * 2 + 1] = COL_ALPHA(i->_pixbuf[j]);
j++;
}
png_write_row(png_ptr, (png_bytep) ptr);
}
}
else { // RGB
for (y = 0; y < spec->height; y++) {
for (x = 0; x < spec->width; x++) {
ptr[x * 4] = COL_RED(i->_pixbuf[j]);
ptr[x * 4 + 1] = COL_GREEN(i->_pixbuf[j]);
ptr[x * 4 + 2] = COL_BLUE(i->_pixbuf[j]);
ptr[x * 4 + 3] = COL_ALPHA(i->_pixbuf[j]);
j++;
}
png_write_row(png_ptr, (png_bytep) ptr);
}
}
free((void *)ptr);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
return 1;
}
示例13: write_png_index_to_buffer
CAMLprim value write_png_index_to_buffer(value buffer, value cmap, value
width, value height) {
CAMLparam4(buffer, cmap, width, height);
CAMLlocal1(vres);
png_structp png_ptr;
png_infop info_ptr;
/* static */
struct mem_buffer state;
int w, h;
/* initialise - put this before png_write_png() call */
state.buffer = NULL;
state.size = 0;
w = Int_val(width);
h = Int_val(height);
if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL)) == NULL ) {
failwith("png_create_write_struct");
}
if((info_ptr = png_create_info_struct(png_ptr)) == NULL ) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
failwith("png_create_info_struct");
}
/* error handling */
if (setjmp(png_jmpbuf(png_ptr))) {
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_write_struct(&png_ptr, &info_ptr);
/* If we get here, we had a problem writing the file */
failwith("png write error");
}
/* the final arg is NULL because we dont need in flush() */
png_set_write_fn(png_ptr, &state, png_write_data_to_buffer, NULL);
/* we use system default compression */
/* png_set_filter(png_ptr, 0, PNG_FILTER_NONE |
PNG_FILTER_SUB | PNG_FILTER_PAETH ); */
/* png_set_compression...() */
png_set_IHDR(png_ptr, info_ptr, w, h,
8 /* fixed */,
PNG_COLOR_TYPE_PALETTE, /* fixed */
PNG_INTERLACE_ADAM7,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
{
png_colorp palette;
int num_palette;
PngPalette_val(cmap, &palette, &num_palette );
if(num_palette <= 0 ) {
png_destroy_write_struct(&png_ptr, &info_ptr);
failwith("png write error (null colormap)");
}
png_set_PLTE(png_ptr, info_ptr, palette, num_palette );
}
/* infos... */
png_write_info(png_ptr, info_ptr);
{
int rowbytes, i;
png_bytep *row_pointers;
char *buf = String_val(buffer);
row_pointers = (png_bytep*)stat_alloc(sizeof(png_bytep) * h);
rowbytes= png_get_rowbytes(png_ptr, info_ptr);
#if 0
printf("rowbytes= %d width=%d\n", rowbytes, w);
#endif
if(rowbytes != w && rowbytes != w * 2) {
png_destroy_write_struct(&png_ptr, &info_ptr);
failwith("png write error (illegal byte/pixel)");
}
for(i=0; i< h; i++) {
row_pointers[i] = (png_bytep)(buf + rowbytes * i);
}
png_write_image(png_ptr, row_pointers);
stat_free((void*)row_pointers);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
vres = caml_alloc_string(state.size);
memcpy(String_val(vres), state.buffer, state.size);
free(state.buffer);
CAMLreturn(vres);
//.........这里部分代码省略.........
示例14: switch
// Method to write raw image into PNG at dest. The raw scanline begins
// at the bottom of the image per SecondLife conventions.
BOOL LLPngWrapper::writePng(const LLImageRaw* rawImage, U8* dest)
{
try
{
S8 numComponents = rawImage->getComponents();
switch (numComponents)
{
case 1:
mColorType = PNG_COLOR_TYPE_GRAY;
break;
case 2:
mColorType = PNG_COLOR_TYPE_GRAY_ALPHA;
break;
case 3:
mColorType = PNG_COLOR_TYPE_RGB;
break;
case 4:
mColorType = PNG_COLOR_TYPE_RGB_ALPHA;
break;
default:
mColorType = -1;
}
if (mColorType == -1)
{
throw "Unsupported image: unexpected number of channels";
}
mWritePngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, &errorHandler, NULL);
if (!mWritePngPtr)
{
throw "Problem creating png write structure";
}
mWriteInfoPtr = png_create_info_struct(mWritePngPtr);
// Setup write function
PngDataInfo dataPtr;
dataPtr.mData = dest;
dataPtr.mOffset = 0;
png_set_write_fn(mWritePngPtr, &dataPtr, &writeDataCallback, &writeFlush);
// Setup image params
mWidth = rawImage->getWidth();
mHeight = rawImage->getHeight();
mBitDepth = 8; // Fixed to 8-bpp in SL
mChannels = numComponents;
mInterlaceType = PNG_INTERLACE_NONE;
mCompressionType = PNG_COMPRESSION_TYPE_DEFAULT;
mFilterMethod = PNG_FILTER_TYPE_DEFAULT;
// Write header
png_set_IHDR(mWritePngPtr, mWriteInfoPtr, mWidth, mHeight,
mBitDepth, mColorType, mInterlaceType,
mCompressionType, mFilterMethod);
// Get data and compute row size
const U8* data = rawImage->getData();
int offset = mWidth * mChannels;
// Ready to write, start with the header
png_write_info(mWritePngPtr, mWriteInfoPtr);
// Write image (sorry, must const-cast for libpng)
const U8 * rowPointer;
for (U32 i=0; i < mHeight; i++)
{
rowPointer = &data[(mHeight-1-i)*offset];
png_write_row(mWritePngPtr, const_cast<png_bytep>(rowPointer));
}
// Finish up
png_write_end(mWritePngPtr, mWriteInfoPtr);
mFinalSize = dataPtr.mOffset;
}
catch (png_const_charp msg)
{
mErrorMessage = msg;
releaseResources();
return (FALSE);
}
releaseResources();
return TRUE;
}
示例15: write_png_file_index
CAMLprim value write_png_file_index(value fd, value buffer, value cmap,
value width, value height) {
CAMLparam5(fd, buffer, cmap, width, height);
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
int w, h;
w = Int_val(width);
h = Int_val(height);
if ((fp = fdopen(Int_val(fd), "wb")) == NULL ) {
failwith("png file open failed");
}
if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL)) == NULL ) {
fclose(fp);
failwith("png_create_write_struct");
}
if((info_ptr = png_create_info_struct(png_ptr)) == NULL ) {
fclose(fp);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
failwith("png_create_info_struct");
}
/* error handling */
if (setjmp(png_jmpbuf(png_ptr))) {
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
/* If we get here, we had a problem writing the file */
failwith("png write error");
}
/* use standard C stream */
png_init_io(png_ptr, fp);
/* we use system default compression */
/* png_set_filter(png_ptr, 0, PNG_FILTER_NONE |
PNG_FILTER_SUB | PNG_FILTER_PAETH ); */
/* png_set_compression...() */
png_set_IHDR(png_ptr, info_ptr, w, h,
8 /* fixed */,
PNG_COLOR_TYPE_PALETTE, /* fixed */
PNG_INTERLACE_ADAM7,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
{
png_colorp palette;
int num_palette;
PngPalette_val(cmap, &palette, &num_palette );
if(num_palette <= 0 ) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
/* If we get here, we had a problem writing the file */
failwith("png write error (null colormap)");
}
png_set_PLTE(png_ptr, info_ptr, palette, num_palette );
}
/* infos... */
png_write_info(png_ptr, info_ptr);
{
int rowbytes, i;
png_bytep *row_pointers;
char *buf = String_val(buffer);
row_pointers = (png_bytep*)stat_alloc(sizeof(png_bytep) * h);
rowbytes= png_get_rowbytes(png_ptr, info_ptr);
#if 0
printf("rowbytes= %d width=%d\n", rowbytes, w);
#endif
if(rowbytes != w && rowbytes != w * 2) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
/* If we get here, we had a problem writing the file */
failwith("png write error (illegal byte/pixel)");
}
for(i=0; i< h; i++) {
row_pointers[i] = (png_bytep)(buf + rowbytes * i);
}
png_write_image(png_ptr, row_pointers);
stat_free((void*)row_pointers);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
//.........这里部分代码省略.........