本文整理汇总了C++中png_write_image函数的典型用法代码示例。如果您正苦于以下问题:C++ png_write_image函数的具体用法?C++ png_write_image怎么用?C++ png_write_image使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_write_image函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: OSG_CHECK_ARG
//.........这里部分代码省略.........
#if defined(GL_BGRA) || defined(GL_BGRA_EXT)
case Image::OSG_BGRA_PF:
#endif
case Image::OSG_RGBA_PF:
ctype = PNG_COLOR_TYPE_RGB_ALPHA;
break;
default:
FWARNING(("PNGImageFileType::write: unknown pixel format %d!\n",
pImage->getPixelFormat()));
png_destroy_write_struct(&png_ptr, NULL);
return 0;
}
Int32 bit_depth;
switch (pImage->getDataType())
{
case Image::OSG_UINT8_IMAGEDATA:
bit_depth = 8;
break;
case Image::OSG_UINT16_IMAGEDATA:
bit_depth = 16;
break;
default:
FWARNING (("Invalid pixeldepth, cannot store data\n"));
return 0;
};
png_set_IHDR(png_ptr,
info_ptr,
pImage->getWidth(),
pImage->getHeight(),
bit_depth,
ctype,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
/* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
/* note that if sRGB is present the gAMA and cHRM chunks must be ignored
* on read and must be written in accordance with the sRGB profile */
/* Write the file header information. REQUIRED */
png_write_info(png_ptr, info_ptr);
#if BYTE_ORDER == LITTLE_ENDIAN
if (bit_depth == 16)
png_set_swap(png_ptr);
#endif
if(pImage->getPixelFormat() == Image::OSG_BGR_PF ||
pImage->getPixelFormat() == Image::OSG_BGRA_PF)
{
/* flip BGR pixels to RGB */
png_set_bgr(png_ptr);
/* swap location of alpha bytes from ARGB to RGBA */
png_set_swap_alpha(png_ptr);
}
/* The easiest way to write the image (you may have a different memory
* layout, however, so choose what fits your needs best). You need to
* use the first method if you aren't handling interlacing yourself.
*/
png_bytep *row_pointers = new png_bytep [pImage->getHeight()];
for(Int32 k = 0; k < pImage->getHeight(); k++)
{
row_pointers[k] =
(const_cast<UInt8 *>(pImage->getData())) +
(pImage->getHeight() - 1 - k) *
pImage->getWidth() * pImage->getBpp();
}
/* write out the entire image data in one call */
png_write_image(png_ptr, row_pointers);
/* It is REQUIRED to call this to finish writing the rest of the file */
png_write_end(png_ptr, info_ptr);
/* clean up after the write, and free any memory allocated */
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
/* that's it */
return bufferInfo.length;
#else
SWARNING << getMimeType()
<< " storeData is not compiled into the current binary "
<< std::endl;
return 0;
#endif
}
示例2: SavePNG
//--------------------------------------------------------------------------------
bool SavePNG(const Path& in_file, BitmapData* in_pData)
{
PROFILE_BLOCK;
if (in_pData == nullptr)
return false;
FILE *fp = nullptr;
FOPEN(&fp, in_file.GetData(), L("wb"));
if (!fp)
{
AssertMsg(false, L("File %s could not be opened for writing"), in_file.GetData());
return false;
}
png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr)
{
AssertMsg(false, L("png_create_write_struct failed"));
return false;
}
png_info* info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
AssertMsg(false, L("png_create_info_struct failed"));
return false;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
AssertMsg(false, L("Error during init_io"));
return false;
}
png_init_io(png_ptr, fp);
if (setjmp(png_jmpbuf(png_ptr)))
{
AssertMsg(false, L("Error during writing header"));
return false;
}
s32 bit_depth = 0;
s32 color_type = 0;
switch (in_pData->GetFormat())
{
case BufferFormat::A_U1:
color_type = ePNG_GrayScale; bit_depth = 1; break;
case BufferFormat::A_U8:
color_type = ePNG_GrayScale; bit_depth = 8; break;
case BufferFormat::BGR_U24:
color_type = ePNG_TrueColor; bit_depth = 8; break;
case BufferFormat::ABGR_U32:
color_type = ePNG_TrueColor | ePNG_Alpha; bit_depth = 8; break;
default:
return false;
}
png_set_IHDR(png_ptr, info_ptr, in_pData->GetWidth(), in_pData->GetHeight(), bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
if (setjmp(png_jmpbuf(png_ptr)))
AssertMsg(false, L("Error during writing bytes"));
u8* pBuffer = (u8*)in_pData->GetBuffer();
u8** row_pointers = new u8*[in_pData->GetHeight()];
for (u32 i = 0; i < in_pData->GetHeight(); i++)
{
row_pointers[i] = pBuffer;
pBuffer += in_pData->GetBufferPitch();
}
png_write_image(png_ptr, row_pointers);
if (setjmp(png_jmpbuf(png_ptr)))
{
delete[] row_pointers;
AssertMsg(false, L("Error during end of write"));
return false;
}
png_write_end(png_ptr, nullptr);
delete[] row_pointers;
fclose(fp);
return true;
}
示例3: _lossless_pack_png
static PoolVector<uint8_t> _lossless_pack_png(const Ref<Image> &p_image) {
Ref<Image> img = p_image->duplicate();
if (img->is_compressed())
img->decompress();
ERR_FAIL_COND_V(img->is_compressed(), PoolVector<uint8_t>());
png_structp png_ptr;
png_infop info_ptr;
png_bytep *row_pointers;
/* initialize stuff */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
ERR_FAIL_COND_V(!png_ptr, PoolVector<uint8_t>());
info_ptr = png_create_info_struct(png_ptr);
ERR_FAIL_COND_V(!info_ptr, PoolVector<uint8_t>());
if (setjmp(png_jmpbuf(png_ptr))) {
ERR_FAIL_V(PoolVector<uint8_t>());
}
PoolVector<uint8_t> ret;
ret.push_back('P');
ret.push_back('N');
ret.push_back('G');
ret.push_back(' ');
png_set_write_fn(png_ptr, &ret, _write_png_data, NULL);
/* write header */
if (setjmp(png_jmpbuf(png_ptr))) {
ERR_FAIL_V(PoolVector<uint8_t>());
}
int pngf = 0;
int cs = 0;
switch (img->get_format()) {
case Image::FORMAT_L8: {
pngf = PNG_COLOR_TYPE_GRAY;
cs = 1;
} break;
case Image::FORMAT_LA8: {
pngf = PNG_COLOR_TYPE_GRAY_ALPHA;
cs = 2;
} break;
case Image::FORMAT_RGB8: {
pngf = PNG_COLOR_TYPE_RGB;
cs = 3;
} break;
case Image::FORMAT_RGBA8: {
pngf = PNG_COLOR_TYPE_RGB_ALPHA;
cs = 4;
} break;
default: {
if (img->detect_alpha()) {
img->convert(Image::FORMAT_RGBA8);
pngf = PNG_COLOR_TYPE_RGB_ALPHA;
cs = 4;
} else {
img->convert(Image::FORMAT_RGB8);
pngf = PNG_COLOR_TYPE_RGB;
cs = 3;
}
}
}
int w = img->get_width();
int h = img->get_height();
png_set_IHDR(png_ptr, info_ptr, w, h,
8, pngf, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
/* write bytes */
if (setjmp(png_jmpbuf(png_ptr))) {
ERR_FAIL_V(PoolVector<uint8_t>());
}
PoolVector<uint8_t>::Read r = img->get_data().read();
row_pointers = (png_bytep *)memalloc(sizeof(png_bytep) * h);
for (int i = 0; i < h; i++) {
row_pointers[i] = (png_bytep)&r[i * w * cs];
}
png_write_image(png_ptr, row_pointers);
//.........这里部分代码省略.........
示例4: fopen
bool ImageWriter::writePNG(SDL_Surface *surface, const std::string &filename)
{
// TODO Maybe someone can make this look nice?
FILE *fp = fopen(filename.c_str(), "wb");
if (!fp)
{
logger->log("could not open file %s for writing", filename.c_str());
return false;
}
png_structp png_ptr;
png_infop info_ptr;
png_bytep *row_pointers;
int colortype;
if (SDL_MUSTLOCK(surface)) {
SDL_LockSurface(surface);
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (!png_ptr)
{
logger->log("Had trouble creating png_structp");
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
logger->log("Could not create png_info");
return false;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
logger->log("problem writing to %s", filename.c_str());
return false;
}
png_init_io(png_ptr, fp);
colortype = (surface->format->BitsPerPixel == 24) ?
PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8, colortype,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
row_pointers = new png_bytep[surface->h];
if (!row_pointers)
{
logger->log("Had trouble converting surface to row pointers");
return false;
}
for (int i = 0; i < surface->h; i++)
{
row_pointers[i] = (png_bytep)(Uint8 *)surface->pixels + i * surface->pitch;
}
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
fclose(fp);
delete [] row_pointers;
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
if (SDL_MUSTLOCK(surface)) {
SDL_UnlockSurface(surface);
}
return true;
}
示例5: PyErr_Clear
//.........这里部分代码省略.........
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_file,
&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 (close_dup_file)
{
if (npy_PyFile_DupClose(py_file, fp)) {
throw Py::RuntimeError("Error closing dupe file handle");
}
}
if (close_file)
{
npy_PyFile_CloseFile(py_file);
Py_DECREF(py_file);
}
/* 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 (close_dup_file)
{
if (npy_PyFile_DupClose(py_file, fp)) {
throw Py::RuntimeError("Error closing dupe file handle");
}
}
if (close_file)
{
npy_PyFile_CloseFile(py_file);
Py_DECREF(py_file);
}
if (PyErr_Occurred()) {
throw Py::Exception();
} else {
return Py::Object();
}
}
示例6: IMG_SavePNG_RW
//.........这里部分代码省略.........
// if (!palette_alpha) {
// SDL_SetError("Couldn't create memory for palette transparency");
// goto savedone;
// }
// /* FIXME: memset? */
// for (i=0;i<(fmt->colorkey+1);i++) {
// palette_alpha[i]=255;
// }
// palette_alpha[fmt->colorkey]=0;
// png_set_tRNS(png_ptr,info_ptr,palette_alpha,fmt->colorkey+1,NULL);
// }
}else{ /* Truecolor */
if (fmt->Amask) {
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);
} else {
png_set_IHDR(png_ptr,info_ptr,
surf->w,surf->h,8,PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
}
}
png_write_info(png_ptr, info_ptr);
if (fmt->BitsPerPixel==8) { /* Paletted */
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);
}
}else{ /* Truecolor */
if(fmt->BytesPerPixel==3){
if(fmt->Amask){ /* check for 24 bit with alpha */
funky_format=1;
}else{
/* Check for RGB/BGR/GBR/RBG/etc surfaces.*/
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if(fmt->Rmask!=0xFF0000
|| fmt->Gmask!=0x00FF00
|| fmt->Bmask!=0x0000FF){
#else
if(fmt->Rmask!=0x0000FF
|| fmt->Gmask!=0x00FF00
|| fmt->Bmask!=0xFF0000){
#endif
funky_format=1;
}
}
}else if (fmt->BytesPerPixel==4){
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
示例7: IF_PRINT_WARNING
bool ImageMemory::SaveImage(const std::string &filename)
{
if(pixels == NULL) {
IF_PRINT_WARNING(VIDEO_DEBUG) << "pixels member was NULL upon function invocation for file: " << filename << std::endl;
return false;
}
// open up the file for writing
FILE *fp = fopen(filename.c_str(), "wb");
if(fp == NULL) {
IF_PRINT_WARNING(VIDEO_DEBUG) << "could not open file: " << filename << std::endl;
return false;
}
// grab a write structure
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL);
if(!png_ptr) {
IF_PRINT_WARNING(VIDEO_DEBUG) << "png_create_write_struct() failed for file: " << filename << std::endl;
fclose(fp);
return false;
}
// and a place to store the metadata
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
IF_PRINT_WARNING(VIDEO_DEBUG) << "png_create_info_struct() failed for file: " << filename << std::endl;
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return false;
}
// prepare for error handling!
if(setjmp(png_jmpbuf(png_ptr))) {
IF_PRINT_WARNING(VIDEO_DEBUG) << "setjmp returned non-zero for file: " << filename << std::endl;
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return false;
}
// tell it where to look
png_init_io(png_ptr, fp);
// write the header
int32 color_type = rgb_format ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA;
png_set_IHDR(png_ptr, info_ptr, width, height, 8, color_type,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
// get the row array from our data
png_bytep *row_pointers = new png_bytep[height];
if(!row_pointers) {
IF_PRINT_WARNING(VIDEO_DEBUG) << "Couldn't allocate png row_pointers for: " << filename << std::endl;
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return false;
}
int32 bytes_per_row = rgb_format ? width * 3 : width * 4;
for(uint32 i = 0; i < height; ++i) {
row_pointers[i] = (png_bytep)pixels + bytes_per_row * i;
}
// tell it what the rows are
png_set_rows(png_ptr, info_ptr, row_pointers);
// and write the PNG
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
png_write_image(png_ptr, row_pointers);
// clean up
png_write_end(png_ptr, info_ptr);
fclose(fp);
// free the memory
delete[] row_pointers;
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return true;
} // bool ImageMemory::SaveImage(const std::string& filename)
示例8: StreamFile
//.........这里部分代码省略.........
png_set_write_fn(png_ptr_write,framer,EncoderWriteCallback, NULL);
info_ptr_write = png_create_info_struct(png_ptr_write);
if(info_ptr_write == NULL) {
alert("unable to create write struct");
return;
}
if(setjmp(png_ptr_write->jmpbuf)) {
alert("something didn't work, jump 2");
return;
}
png_read_frame_head(png_ptr_read, info_ptr_read);
if(png_get_valid(png_ptr_read, info_ptr_read, PNG_INFO_fcTL))
{
png_get_next_frame_fcTL(png_ptr_read, info_ptr_read,
&next_frame_width, &next_frame_height,
&next_frame_x_offset, &next_frame_y_offset,
&next_frame_delay_num, &next_frame_delay_den,
&next_frame_dispose_op, &next_frame_blend_op);
}
else
{
/* the first frame doesn't have an fcTL so it's expected to be hidden,
* but we'll extract it anyway next_frame_x_offset , next_frame_y_offset */
next_frame_width = png_get_image_width(png_ptr_read, info_ptr_read);
next_frame_height = png_get_image_height(png_ptr_read, info_ptr_read);
}
QRect C_frame_rect(0,0,next_frame_width,next_frame_height);
AFRAMES FrameInfo = OneFrameReader(png_ptr_read, info_ptr_read, png_ptr_write, info_ptr_write,
next_frame_width, next_frame_height);
png_write_info(png_ptr_write, info_ptr_write);
png_read_image(png_ptr_read, row_pointers);
png_write_image(png_ptr_write, row_pointers);
png_write_end(png_ptr_write, NULL);
float Fraction = (float)next_frame_delay_num / (float)next_frame_delay_den + 0.00;
////////qDebug() << "### Fraction " << Fraction;
int PlayGo;
if (Fraction < 0.001 ) {
PlayGo = 100;
} else if (Fraction < 1.010 && Fraction > 0.9) {
PlayGo = 1000;
} else {
PlayGo = Fraction * 1000;
}
/* extract frames */
if ( framer->isValid() && Frect.contains(C_frame_rect) ) {
validloop++;
int Coalpha = 255;
/* prepare image if no background find grab a pixel color! */
QImage tmpgd(C_frame_rect.width(),C_frame_rect.height(),QImage::Format_ARGB32);
/* compose it files */
APNGFRAME one;
one.dimg = framer->stream();
one.maxframe = Frect;
one.pos = validloop;
one.point = QPoint(next_frame_x_offset,next_frame_y_offset);
one.play = PlayGo;
if (!(png_get_valid(png_ptr_read, info_ptr_read, PNG_INFO_bKGD))) {
tmpgd.loadFromData(one.dimg);
QRgb GrepColor = tmpgd.pixel(QPoint(2,2));
one.bg = QColor(GrepColor);
one.bg.setAlpha(qAlpha(GrepColor));
Coalpha = qAlpha(GrepColor);
示例9: save_png
/* save rgb888 to png format in fp */
int save_png(const char* path, const char* data, int width, int height)
{
FILE *fp;
png_byte **volatile rows;
png_struct *png;
png_info *info;
fp = fopen(path, "w");
if (!fp) {
int errsv = errno;
E("Cannot open file %s for writing.\n", path);
return errsv;
}
rows = malloc(height * sizeof rows[0]);
if (!rows) goto oops;
int i;
for (i = 0; i < height; i++)
rows[i] = (png_byte *) data + i * width * 3 /*fb.stride*/;
png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL,
png_simple_error_callback,
png_simple_warning_callback);
if (!png) {
E("png_create_write_struct failed\n");
goto oops;
}
info = png_create_info_struct (png);
if (!info) {
E("png_create_info_struct failed\n");
png_destroy_write_struct (&png, NULL);
goto oops;
}
png_set_write_fn (png, fp, stdio_write_func, png_simple_output_flush_fn);
png_set_IHDR (png, info,
width,
height,
#define DEPTH 8
DEPTH,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_color_16 white;
white.gray = (1 << DEPTH) - 1;
white.red = white.blue = white.green = white.gray;
png_set_bKGD (png, info, &white);
png_write_info (png, info);
png_write_image (png, rows);
png_write_end (png, info);
png_destroy_write_struct (&png, &info);
fclose(fp);
free (rows);
return 0;
oops:
fclose(fp);
free (rows);
return -1;
}
示例10: write_png
int write_png(struct image image, char *filename) {
int rc = 0, i = 0, x = 0, y = 0, width = 0, height = 0;
unsigned char *data = NULL;
FILE *file = NULL;
png_byte color_type, bit_depth;
png_structp png_ptr;
png_infop info_ptr;
png_bytep *row_pointers;
file = fopen(filename, "wb");
if (!file) {
printf("Could not open %s\n", filename);
rc = 1;
goto cleanup;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
printf("png_create_write_struct failed\n");
rc = 1;
goto cleanup;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
printf("png_create_info_struct failed\n");
rc = 1;
goto cleanup;
}
if (setjmp(png_jmpbuf(png_ptr))) {
printf("Error initializing write\n");
rc = 1;
goto cleanup;
}
png_init_io(png_ptr, file);
if (setjmp(png_jmpbuf(png_ptr))) {
printf("Error writing header\n");
rc = 1;
goto cleanup;
}
data = image.data;
width = image.width;
height = image.height;
color_type = 2;
bit_depth = 8;
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height + 1);
for (y = 0; y < height; y++) {
row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png_ptr, info_ptr) + 1);
}
for (y = 0, i = 0; y < height; y++) {
png_byte *row = row_pointers[y];
for (x = 0; x < width; x++) {
png_byte *rgb = &(row[x * 3]);
rgb[0] = data[i++];
rgb[1] = data[i++];
rgb[2] = data[i++];
}
}
png_write_image(png_ptr, row_pointers);
if (setjmp(png_jmpbuf(png_ptr))) {
printf("Error ending write\n");
rc = 1;
goto cleanup;
}
png_write_end(png_ptr, NULL);
cleanup:
if (file) {
fclose(file);
}
return rc;
}
示例11: fopen
ImageIO::errorType ImageIO::savePNG(const char * filename)
{
#ifdef ENABLE_PNG
FILE *file = fopen(filename, "wb");
if (!file)
{
printf("Error in savePNG: Cannot open file %s.\n", filename);
return IO_ERROR;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
printf("Error in savePNG: Creating the internal structure failed.\n");
fclose(file);
return (IO_ERROR);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
printf("Error in savePNG: Creating the information structure failed.\n");
fclose(file);
return (IO_ERROR);
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
printf("Error in savePNG: cannot setup the error handling.\n");
return (IO_ERROR);
}
// setup the output
png_init_io(png_ptr, file);
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
printf("Error in savePNG: cannot write the png header.\n");
return (IO_ERROR);
}
int bit_depth = BITS_PER_CHANNEL_8; // currently we only support 8 bits per channel
int color_type;
switch(bytesPerPixel)
{
case IMAGE_IO_RGB:
color_type = PNG_COLOR_TYPE_RGB;
break;
case IMAGE_IO_RGB_ALPHA:
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
break;
default:
png_destroy_write_struct(&png_ptr, &info_ptr);
printf("Error in savePNG: cannot handle bytesPerPixel that is not 3 or 4.\n");
return OTHER_ERROR;
break;
}
png_set_IHDR(png_ptr, info_ptr, (png_uint_32)width, (png_uint_32)height,
bit_depth, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
printf("Error in savePNG: cannot write the png file.\n");
return (IO_ERROR);
}
unsigned int bytesPerRow = bytesPerPixel * width;
png_bytep * row_pointers = (png_bytep*) malloc (sizeof(png_bytep) * height);
for(unsigned int row = 0; row < height; row++)
row_pointers[row] = (png_byte*)(&pixels[(height - row - 1) * bytesPerRow]);
png_write_image(png_ptr, row_pointers);
free(row_pointers);
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
printf("Error in savePNG: unknown error occurred during end of file.\n");
return (IO_ERROR);
}
png_write_end(png_ptr, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
//.........这里部分代码省略.........
示例12: writePng
static Bool
writePng (unsigned char *buffer,
png_rw_ptr writeFunc,
void *closure,
int width,
int height,
int stride)
{
png_struct *png;
png_info *info;
png_byte **rows;
png_color_16 white;
int i;
rows = malloc (height * sizeof (png_byte *));
if (!rows)
return FALSE;
for (i = 0; i < height; i++)
rows[height - i - 1] = buffer + i * stride;
png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png)
{
free (rows);
return FALSE;
}
info = png_create_info_struct (png);
if (!info)
{
png_destroy_read_struct (&png, NULL, NULL);
free (rows);
return FALSE;
}
if (setjmp (png_jmpbuf (png)))
{
png_destroy_read_struct (&png, NULL, NULL);
free (rows);
return FALSE;
}
png_set_write_fn (png, closure, writeFunc, NULL);
png_set_IHDR (png, info,
width, height, 8,
PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
white.red = 0xff;
white.blue = 0xff;
white.green = 0xff;
png_set_bKGD (png, info, &white);
png_write_info (png, info);
png_write_image (png, rows);
png_write_end (png, info);
png_destroy_write_struct (&png, &info);
free (rows);
return TRUE;
}
示例13: return
//.........这里部分代码省略.........
*obyte = (unsigned char)l32->a; obyte++;
}
}
}
free(line32);
}
break;
case PNG_COLOR_TYPE_GRAY:
{
unsigned short *line = (unsigned short *) calloc(info->width * info->channels, sizeof(unsigned short));
for (png_uint_32 iy = 0; iy < info->height; ++iy) {
if (map->Get16Gray(0, iy, info->width, line) != 1) {
for (png_uint_32 i = 0; i < info->height; i++)
if (row_pointers[i]) free(row_pointers[i]);
if (row_pointers) {
free(row_pointers);
row_pointers = NULL;
}
fclose(ostream);
_tremove(filename);
free(line);
png_destroy_write_struct (&png, &info);
return BMMRES_IOERROR;
}
unsigned short *l=line;
unsigned char *obyte = (unsigned char *)row_pointers[iy];
for (png_uint_32 ix = 0; ix < info->width; ix++) {
*obyte++ = (unsigned char)(*l >> 8); l++;
}
}
free(line);
}
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
{
BMM_Color_64 *line64 = (BMM_Color_64 *) calloc(info->width,sizeof(BMM_Color_64));
unsigned short *line = (unsigned short *) calloc(info->width, sizeof(unsigned short));
for (png_uint_32 iy = 0; iy < info->height; ++iy) {
if (GetOutputPixels(0, iy, info->width, line64) != 1 ||
map->Get16Gray(0, iy, info->width, line) != 1) {
for (png_uint_32 i = 0; i < info->height; i++)
if (row_pointers[i]) free(row_pointers[i]);
if (row_pointers) {
free(row_pointers);
row_pointers = NULL;
}
fclose(ostream);
_tremove(filename);
free(line);
free(line64);
png_destroy_write_struct (&png, &info);
return BMMRES_IOERROR;
}
unsigned short *l=line;
BMM_Color_64 *l64 = line64;
unsigned char *obyte = (unsigned char *)row_pointers[iy];
for (png_uint_32 ix = 0; ix < info->width; ix++, l64++) {
*obyte++ = (unsigned char)(*l >> 8); l++;
*obyte++ = (unsigned char)(l64->a >> 8);
}
}
free(line);
free(line64);
}
break;
}
break;
#ifdef OUTPUT_1_2_4
case 4: { // Paletted only
}
break;
case 2: { // Paletted only
}
break;
case 1: { // Paletted only
}
#endif
break;
}
png_write_info(png, info);
png_set_swap(png);
png_write_image(png, row_pointers);
png_write_end(png, info);
fclose(ostream);
for (i = 0; i < info->height; i++)
free(row_pointers[i]);
free(row_pointers);
png_destroy_write_struct (&png, &info);
return BMMRES_SUCCESS;
}
示例14: write_RGBA_to_png
int write_RGBA_to_png(uint16_t width, uint16_t height, uint8_t * array, const char * filename)
{
int x, y;
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;
FILE * fp = fopen(filename, "wb");
if(fp == NULL)
return 0;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
PNGSETJMP
png_init_io(png_ptr, fp);
PNGSETJMP
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);
png_write_info(png_ptr, info_ptr);
/* write bytes */
PNGSETJMP
/* convert uint8_t[] to pngbyte[][] */
row_pointers = malloc(sizeof(*row_pointers) * height);
for(y = 0;y < height;y++)
{
row_pointers[y] = malloc(png_get_rowbytes(png_ptr, info_ptr));
for(x = 0;x < width * 4;x += 4)
{
row_pointers[y][x] = array[y * width * 4 + x];
row_pointers[y][x+1] = array[y * width * 4 + x+1];
row_pointers[y][x+2] = array[y * width * 4 + x+2];
row_pointers[y][x+3] = array[y * width * 4 + x+3];
}
}
png_write_image(png_ptr, row_pointers);
/* end write */
PNGSETJMP
png_write_end(png_ptr, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
for(y = 0;y < height;y++)
{
free(row_pointers[y]);
}
free(row_pointers);
fclose(fp);
return 1;
}
示例15: 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 (setjmp(png_jmpbuf(png_ptr)))
{
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
break;
}
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];
}
}
//.........这里部分代码省略.........