本文整理汇总了C++中png_init_io函数的典型用法代码示例。如果您正苦于以下问题:C++ png_init_io函数的具体用法?C++ png_init_io怎么用?C++ png_init_io使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_init_io函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: pgeTextureSave
void pgeTextureSave(pgeTexture *texture, const char* filename)
{
if(texture == 0)
return;
if(texture->data == 0 || texture->width == 0 || texture->height == 0)
return;
int swizzled = texture->swizzled;
if(swizzled)
pgeTextureUnswizzle(texture);
png_structp png_ptr;
png_infop info_ptr;
FILE* fp;
unsigned char* line;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr)
return;
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return;
}
unsigned int col_type = PNG_COLOR_TYPE_RGBA;
fp = fopen(filename, "wb");
if (!fp) return;
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, texture->width, texture->height, 8, col_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
line = (unsigned char*) pgeMalloc(texture->width * 4);
if(line == 0)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return;
}
unsigned char* src = (unsigned char*)texture->data;
unsigned char* src8 = (unsigned char*)src;
unsigned short* src16 = (unsigned short*)src;
unsigned int* src32 = (unsigned int*)src;
unsigned int i, x, y;
for(y = 0; y < texture->height; y++)
{
unsigned int swap = 0;
src8 = (unsigned char*)src;
src16 = (unsigned short*)src;
src32 = (unsigned int*)src;
for(i = 0, x = 0; x < texture->width; x++)
{
unsigned int r = 0, g = 0, b = 0, a = 0;
unsigned short col16;
unsigned int col32;
unsigned char col8;
switch(texture->format)
{
case PGE_PIXEL_FORMAT_5650:
col16 = *src16++;
RGBA5650(col16,r,g,b,a);
break;
case PGE_PIXEL_FORMAT_5551:
col16 = *src16++;
RGBA5551(col16,r,g,b,a);
break;
case PGE_PIXEL_FORMAT_4444:
col16 = *src16++;
RGBA4444(col16,r,g,b,a);
break;
case PGE_PIXEL_FORMAT_8888:
col32 = *src32++;
RGBA8888(col32,r,g,b,a);
break;
case PGE_PIXEL_FORMAT_T4:
col8 = *src8;
if(swap == 0)
col8 &= 0xF;
else
//.........这里部分代码省略.........
示例2: read_png_file
int read_png_file(const char *file, int32 *pwidth, int32 *pheight, uint8 **image_data_ptr)
{
FILE *infile; /* PNG file pointer */
png_structp png_ptr; /* internally used by libpng */
png_infop info_ptr; /* user requested transforms */
uint8 *image_data; /* raw png image data */
char sig[8]; /* PNG signature array */
/*char **row_pointers; */
int bit_depth;
int color_type;
png_uint_32 width; /* PNG image width in pixels */
png_uint_32 height; /* PNG image height in pixels */
unsigned int rowbytes; /* raw bytes at row n in image */
image_data = NULL;
int i;
png_bytepp row_pointers = NULL;
/* Open the file. */
infile = fopen(file, "rb");
if (!infile) {
return 0;
}
/*
* 13.3 readpng_init()
*/
/* Check for the 8-byte signature */
fread(sig, 1, 8, infile);
if (png_sig_cmp((unsigned char *) sig, 0, 8) != 0) {
fclose(infile);
return 0;
}
/*
* Set up the PNG structs
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(infile);
return 4; /* out of memory */
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
fclose(infile);
return 4; /* out of memory */
}
/*
* block to handle libpng errors,
* then check whether the PNG file had a bKGD chunk
*/
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(infile);
return 0;
}
/*
* takes our file stream pointer (infile) and
* stores it in the png_ptr struct for later use.
*/
/* png_ptr->io_ptr = (png_voidp)infile;*/
png_init_io(png_ptr, infile);
/*
* lets libpng know that we already checked the 8
* signature bytes, so it should not expect to find
* them at the current file pointer location
*/
png_set_sig_bytes(png_ptr, 8);
/* Read the image info.*/
/*
* reads and processes not only the PNG file's IHDR chunk
* but also any other chunks up to the first IDAT
* (i.e., everything before the image data).
*/
/* read all the info up to the image data */
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
*pwidth = width;
*pheight = height;
if (bit_depth > 8) {
//.........这里部分代码省略.........
示例3: read_png
/* Read a PNG file. You may want to return an error code if the read
* fails (depending upon the failure). There are two "prototypes" given
* here - one where we are given the filename, and we need to open the
* file, and the other where we are given an open file (possibly with
* some or all of the magic bytes read - see comments above).
*/
#ifdef open_file /* prototype 1 */
void read_png(char *file_name) /* We need to open the file */
{
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
FILE *fp;
if ((fp = fopen(file_name, "rb")) == NULL)
return (ERROR);
#else no_open_file /* prototype 2 */
void read_png(FILE *fp, unsigned int sig_read) /* File is already open */
{
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
#endif no_open_file /* Only use one prototype! */
/* 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 supply the
* the compiler header file version, so that we know if the application
* was compiled with a compatible version of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
png_voidp user_error_ptr, user_error_fn, user_warning_fn);
if (png_ptr == NULL)
{
fclose(fp);
return (ERROR);
}
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return (ERROR);
}
/* Set error handling if you are using the setjmp/longjmp method (this is
* the normal method of doing things with libpng). REQUIRED unless you
* set up your own error handlers in the png_create_read_struct() earlier.
*/
if (setjmp(png_jmpbuf(png_ptr)))
{
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
/* If we get here, we had a problem reading the file */
return (ERROR);
}
/* One of the following I/O initialization methods is REQUIRED */
#ifdef streams /* PNG file I/O method 1 */
/* Set up the input control if you are using standard C streams */
png_init_io(png_ptr, fp);
#else no_streams /* PNG file I/O method 2 */
/* If you are using replacement read functions, instead of calling
* png_init_io() here you would call:
*/
png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
/* where user_io_ptr is a structure you want available to the callbacks */
#endif no_streams /* Use only one I/O method! */
/* If we have already read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
#ifdef hilevel
/*
* If you have enough memory to read in the entire image at once,
* and you need to specify only transforms that can be controlled
* with one of the PNG_TRANSFORM_* bits (this presently excludes
* dithering, filling, setting background, and doing gamma
* adjustment), then you can read the entire image (including
* pixels) into the info structure with this call:
*/
png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
#else
/* OK, you're doing it the hard way, with the lower-level functions */
/* The call to png_read_info() gives us all of the information from the
* PNG file before the first IDAT (image data chunk). REQUIRED
*/
png_read_info(png_ptr, info_ptr);
//.........这里部分代码省略.........
示例4: loadpng
pngdata_t * loadpng(const char * path, int cutoff) {
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;
int width, height, rowbytes;
int x, y;
unsigned char header[8]; // 8 is the maximum size that can be checked
png_byte* ptr;
FILE *fp;
int type = 0;
int lb;
uint8_t * bitmap;
/* open file and test for it being a png */
fp = fopen(path, "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", path);
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
abort_("[read_png_file] File %s is not recognized as a PNG file", path);
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
abort_("[read_png_file] png_create_read_struct failed");
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
abort_("[read_png_file] png_create_info_struct failed");
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during init_io");
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
if (png_get_channels(png_ptr, info_ptr) == 1
&& png_get_bit_depth(png_ptr, info_ptr) == 1) {
type = 1;
} else if (png_get_channels(png_ptr, info_ptr) == 4
&& png_get_bit_depth(png_ptr, info_ptr) == 8) {
type = 2;
}
if (type == 0) {
fprintf(stderr, "Invalid PNG! Only mono or 4x8-bit RGBA PNG files are allowed\n");
exit(1);
}
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
png_read_update_info(png_ptr, info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during read_image");
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
rowbytes = type == 1 ? (width / 8) + (width % 8 > 0) : width << 2;
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(rowbytes);
png_read_image(png_ptr, row_pointers);
fclose(fp);
lb = (height / 8) + (height % 8 > 0);
bitmap = malloc(width*lb);
memset(bitmap, 0, width*lb);
#define heat_on(x, y) bitmap[(x*lb)+(y/8)] |= 1 << (7-(y%8))
for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
if (type == 1) {
ptr = &(row[x/8]);
if ((ptr[0] & (1 << (7-(x%8)))) == 0) {
heat_on(x, y);
}
} else {
// Color 0 (red?)
ptr = &(row[(x<<2)]);
if (ptr[0] < cutoff) { // Adjust the cutoff (0 to 255). Any pixel darker (lower) than this will be black. Higher will be white. This can be used to turn anti-aliased pngs into monochrome with some desirable cutoff for black and white.
heat_on(x, y);
}
// Color 1 (green?)
ptr = &(row[(x<<2)+1]);
if (ptr[0] < cutoff) {
heat_on(x, y);
}
// Color 2 (blue?)
ptr = &(row[(x<<2)+2]);
//.........这里部分代码省略.........
示例5: fopen
void CachedTexture::reload() {
//..... load file ......
GLenum internal_format = GL_RGB;
GLenum data_format = GL_RGB;
unsigned char *data = NULL;
if (filename.size() >= 4 && filename.substr(filename.size()-4,4) == ".png") {
//Load a png file, as per the libpng docs:
FILE *fp = fopen(filename.c_str(), "rb");
if (!fp) {
cerr << " cannot open file." << endl;
return;
}
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, (png_error_ptr)NULL, (png_error_ptr)NULL);
if (!png) {
cerr << " cannot alloc read struct." << endl;
fclose(fp);
return;
}
png_infop info = png_create_info_struct(png);
if (!info) {
cerr << " cannot alloc info struct." << endl;
png_destroy_read_struct(&png, (png_infopp)NULL, (png_infopp)NULL);
fclose(fp);
return;
}
png_bytep *row_pointers = NULL;
if (setjmp(png_jmpbuf(png))) {
cerr << " png interal error." << endl;
png_destroy_read_struct(&png, &info, (png_infopp)NULL);
if (data != NULL) delete[] data;
if (row_pointers != NULL) delete[] row_pointers;
fclose(fp);
return;
}
png_init_io(png, fp);
png_read_info(png, info);
w = png_get_image_width(png, info);
h = png_get_image_height(png, info);
{ //find power-of-two dimensions...
tw = 1;
while (tw < w) tw *= 2;
th = 1;
while (th < h) th *= 2;
}
if (!pad && (tw != w || th != h)) {
if (!have_ARB_texture_non_power_of_two()) {
cerr << "WARNING: trying to load texture of size " << w << " x " << h << ", but non-power-of-two textures not supported." << endl;
}
tw = w;
th = h;
}
if (png_get_color_type(png, info) == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png);
if (png_get_bit_depth(png, info) < 8)
png_set_packing(png);
if (png_get_bit_depth(png,info) == 16)
png_set_strip_16(png);
png_read_update_info(png, info);
unsigned int rowbytes = png_get_rowbytes(png, info);
//Make sure it's the format we think it is...
bool problem = false;
if (png_get_color_type(png, info) == PNG_COLOR_TYPE_GRAY) {
data_format = internal_format = GL_LUMINANCE;
if (rowbytes != w*1) problem = true;
} else if (png_get_color_type(png, info) == PNG_COLOR_TYPE_GRAY_ALPHA) {
data_format = internal_format = GL_LUMINANCE_ALPHA;
if (rowbytes != w*2) problem = true;
} else if (png_get_color_type(png, info) == PNG_COLOR_TYPE_PALETTE || png_get_color_type(png, info) == PNG_COLOR_TYPE_RGB) {
data_format = internal_format = GL_RGB;
if (rowbytes != w*3) problem = true;
} else if (png_get_color_type(png, info) == PNG_COLOR_TYPE_RGB_ALPHA) {
data_format = internal_format = GL_RGBA;
if (rowbytes != w*4) problem = true;
} else {
cerr << " unknown color format." << endl;
problem = true;
}
if (problem) {
cerr << " color format problem. (rowbytes: " << rowbytes << " w:" << w << ")" << endl;
png_destroy_read_struct(&png, &info, NULL);
fclose(fp);
return;
}
unsigned int pixelbytes = rowbytes / w;
assert(rowbytes == pixelbytes * w); //sanity check, should have bailed earlier if this was the case.
data = new uint8_t[th*tw*pixelbytes];
row_pointers = new png_bytep[th];
if (flip) {
//texture origin goes top-left, as in most images:
for (unsigned int r = 0; r < th; ++r) {
row_pointers[r] = (png_bytep)(data + r*pixelbytes*tw);
}
} else {
//texture origin goes bottom-left, as GL says:
for (unsigned int r = 0; r < th; ++r) {
row_pointers[th-1-r] = (png_bytep)(data + r*tw*pixelbytes);
}
}
png_read_image(png, row_pointers);
//.........这里部分代码省略.........
示例6: savePNG
void savePNG(const char* file_name)
{
png_structp png_ptr;
png_infop info_ptr;
/* create file */
FILE *fp = fopen(file_name, "wb");
if (!fp)
abort_("[write_png_file] File %s could not be opened for writing", file_name);
/* initialize stuff */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
abort_("[write_png_file] png_create_write_struct failed");
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
abort_("[write_png_file] png_create_info_struct failed");
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[write_png_file] Error during init_io");
png_init_io(png_ptr, fp);
/* write header */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[write_png_file] Error during writing header");
png_set_IHDR(png_ptr, info_ptr, pictureWidth, pictureHeight,
8, PNG_COLOR_TYPE_RGBA, 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)))
abort_("[write_png_file] Error during writing bytes");
png_bytep * row_pointers;
unsigned char *array = new unsigned char[pictureWidth * pictureHeight * 4];
for (int i = pictureWidth * pictureHeight; i >= 0; --i)
{
array[i*4+0] = ((unsigned char*)pictureWriteOut)[i*4+0];
array[i*4+1] = ((unsigned char*)pictureWriteOut)[i*4+1];
array[i*4+2] = ((unsigned char*)pictureWriteOut)[i*4+2];
array[i*4+3] = ((unsigned char*)pictureWriteOut)[i*4+3];
}
// Allocate pointers...
row_pointers = new png_bytep[pictureHeight];
for (int i = 0; i < pictureHeight; i ++)
row_pointers[i] = (png_bytep)(array + (i) * pictureWidth * 4); // we flip it
png_write_image(png_ptr, row_pointers);
/* end write */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[write_png_file] Error during end of write");
png_write_end(png_ptr, NULL);
/* cleanup heap allocation */
free(row_pointers);
fclose(fp);
}
示例7: readpng_init
int readpng_init(FILE *infile, png_store *pngdata)
{
unsigned char sig[8];
long width,height;
int bit_depth, color_type;
/* first do a quick check that the file really is a PNG image; could
* have used slightly more general png_sig_cmp() function instead */
size_t fr = fread(sig, 1, 8, infile);
if(fr != 1*8)
return 1; /* bad signature */
if (!png_check_sig(sig, 8))
return 1; /* bad signature */
/* could pass pointers to user-defined error handlers instead of NULLs: */
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
return 4; /* out of memory */
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
return 4; /* out of memory */
}
/* we could create a second info struct here (end_info), but it's only
* useful if we want to keep pre- and post-IDAT chunk info separated
* (mainly for PNG-aware image editors and converters) */
/* setjmp() must be called in every function that calls a PNG-reading
* libpng function */
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return 2;
}
png_init_io(png_ptr, infile);
png_set_sig_bytes(png_ptr, 8); /* we already read the 8 signature bytes */
png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */
/* alternatively, could make separate calls to png_get_image_width(),
* etc., but want bit_depth and color_type for later [don't care about
* compression_type and filter_type => NULLs] */
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
pngdata->width = width;
pngdata->height = height;
pngdata->bitdepth = bit_depth;
pngdata->colortype = color_type;
pngdata->png_ptr = png_ptr;
pngdata->info_ptr = info_ptr;
/* OK, that's all we need for now; return happy */
return 0;
}
示例8: load_png_graphics
int
load_png_graphics (char *txt_filename, char *png_filename)
{
png_uint_32 row;
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
FILE *fp, *txt_fp;
png_bytep *row_pointers;
if ((fp = fopen(png_filename, "rb")) == NULL)
return (ERROR);
if ((txt_fp = fopen(txt_filename, "r")) == NULL)
return (ERROR);
/* 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 supply the
* the compiler header file version, so that we know if the application
* was compiled with a compatible version of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
(png_voidp) user_error_ptr, user_error_fn, user_warning_fn);
if (png_ptr == NULL) {
fclose(fp);
return (ERROR);
}
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
#if defined (commentout)
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
#endif
png_destroy_read_struct(&png_ptr, NULL, NULL);
return (ERROR);
}
/* Set error handling if you are using the setjmp/longjmp method (this is
* the normal method of doing things with libpng). REQUIRED unless you
* set up your own error handlers in the png_create_read_struct() earlier.
*/
if (setjmp(png_jmpbuf(png_ptr))) {
/* Free all of the memory associated with the png_ptr and info_ptr */
#if defined (commentout)
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
#endif
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
/* If we get here, we had a problem reading the file */
return (ERROR);
}
/* Set up the input control if you are using standard C streams */
png_init_io(png_ptr, fp);
/* If we have already read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
/* OK, you're doing it the hard way, with the lower-level functions */
/* The call to png_read_info() gives us all of the information from the
* PNG file before the first IDAT (image data chunk). REQUIRED
*/
png_read_info(png_ptr, info_ptr);
#if defined (commentout)
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, int_p_NULL, int_p_NULL);
#endif
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
printf ("PNG Header: %d x %d, bd=%d, ct=%d\n", height, width,
bit_depth, color_type);
/* Set up the data transformations you want. Note that these are all
* optional. Only call them if you want/need them. Many of the
* transformations only work on specific types of images, and many
* are mutually exclusive.
*/
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
// png_set_strip_16(png_ptr);
/* Strip alpha bytes from the input data without combining with the
* background (not recommended).
*/
// png_set_strip_alpha(png_ptr);
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
* byte into separate bytes (useful for paletted and grayscale images).
*/
// png_set_packing(png_ptr);
/* Change the order of packed pixels to least significant bit first
//.........这里部分代码省略.........
示例9: fopen
unsigned char *_loadImageRGBApng(char *fileName, int *width, int *height) {
// open the file
FILE *fp = fopen(fileName, "rb");
if (!fp)
return _loadImgError(width, height);
// read the header
const int HEADER_LENGTH = 8;
png_byte header[HEADER_LENGTH];
fread(header, 1, HEADER_LENGTH, fp);
if (png_sig_cmp(header, 0, HEADER_LENGTH))
return _loadImgError(width, height);
// try to create the loading structures
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (!png_ptr) {
fclose(fp);
return _loadImgError(width, height);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp) 0, (png_infopp) 0);
fclose(fp);
return _loadImgError(width, height);
}
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0);
fclose(fp);
return _loadImgError(width, height);
}
if (setjmp(png_ptr->jmpbuf)) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return _loadImgError(width, height);
}
// start the io
png_init_io(png_ptr, fp);
// indicate that we have already read some of the hearder
png_set_sig_bytes(png_ptr, HEADER_LENGTH);
// read the image info, get some info
png_read_info(png_ptr, info_ptr);
*width = png_get_image_width(png_ptr, info_ptr);
*height = png_get_image_height(png_ptr, info_ptr);
int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
// force the image into RGBA, 8 bits per channel
if (color_type != PNG_COLOR_TYPE_RGBA)
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
else if (bit_depth == 16)
png_set_strip_16(png_ptr);
if (color_type != PNG_COLOR_TYPE_RGBA)
png_set_filler(png_ptr, 255, PNG_FILLER_AFTER);
png_read_update_info(png_ptr, info_ptr);
// make sure we're actually in rgba mode
if (png_get_rowbytes(png_ptr, info_ptr) != ((*width) * 4)) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return _loadImgError(width, height);
}
// finally, read the file
unsigned char *buffer = (unsigned char *) malloc((*width) * (*height) * 4);
png_bytep row_pointers[*height];
for (int y = 0 ; y < (*height) ; y++)
row_pointers[y] = (png_byte *) (buffer + ((*height) - 1 - y) * (*width) * 4);
png_read_rows(png_ptr, row_pointers, 0, (long unsigned int) (*height));
// deallocate memory and return
fclose(fp);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return buffer;
}
示例10: res_create_surface
int res_create_surface(const char* name, gr_surface* pSurface) {
char resPath[256];
GGLSurface* surface = NULL;
int result = 0;
unsigned char header[8];
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
*pSurface = NULL;
snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
resPath[sizeof(resPath)-1] = '\0';
FILE* fp = fopen(resPath, "rb");
if (fp == NULL) {
result = -1;
goto exit;
}
size_t bytesRead = fread(header, 1, sizeof(header), fp);
if (bytesRead != sizeof(header)) {
result = -2;
goto exit;
}
if (png_sig_cmp(header, 0, sizeof(header))) {
result = -3;
goto exit;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
result = -4;
goto exit;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
result = -5;
goto exit;
}
if (setjmp(png_jmpbuf(png_ptr))) {
result = -6;
goto exit;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sizeof(header));
png_read_info(png_ptr, info_ptr);
int color_type = info_ptr->color_type;
int bit_depth = info_ptr->bit_depth;
int channels = info_ptr->channels;
if (!(bit_depth == 8 &&
((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
(channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
(channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE ||
color_type == PNG_COLOR_TYPE_GRAY))))) {
return -7;
goto exit;
}
size_t width = info_ptr->width;
size_t height = info_ptr->height;
size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
size_t pixelSize = stride * height;
surface = malloc(sizeof(GGLSurface) + pixelSize);
if (surface == NULL) {
result = -8;
goto exit;
}
unsigned char* pData = (unsigned char*) (surface + 1);
surface->version = sizeof(GGLSurface);
surface->width = width;
surface->height = height;
surface->stride = width; /* Yes, pixels, not bytes */
surface->data = pData;
surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 :
((color_type == PNG_COLOR_TYPE_PALETTE ? GGL_PIXEL_FORMAT_RGBA_8888 : GGL_PIXEL_FORMAT_L_8));
int alpha = 0;
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
alpha = 1;
}
if (color_type == PNG_COLOR_TYPE_GRAY) {
alpha = 1;
}
unsigned int y;
if (channels == 3 || (channels == 1 && !alpha)) {
for (y = 0; y < height; ++y) {
unsigned char* pRow = pData + y * stride;
png_read_row(png_ptr, pRow, NULL);
int x;
//.........这里部分代码省略.........
示例11: res_create_localized_surface
int res_create_localized_surface(const char* name, gr_surface* pSurface) {
char resPath[256];
GGLSurface* surface = NULL;
int result = 0;
unsigned char header[8];
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
*pSurface = NULL;
snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
resPath[sizeof(resPath)-1] = '\0';
FILE* fp = fopen(resPath, "rb");
if (fp == NULL) {
result = -1;
goto exit;
}
size_t bytesRead = fread(header, 1, sizeof(header), fp);
if (bytesRead != sizeof(header)) {
result = -2;
goto exit;
}
if (png_sig_cmp(header, 0, sizeof(header))) {
result = -3;
goto exit;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
result = -4;
goto exit;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
result = -5;
goto exit;
}
if (setjmp(png_jmpbuf(png_ptr))) {
result = -6;
goto exit;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sizeof(header));
png_read_info(png_ptr, info_ptr);
size_t width = info_ptr->width;
size_t height = info_ptr->height;
size_t stride = 4 * width;
int color_type = info_ptr->color_type;
int bit_depth = info_ptr->bit_depth;
int channels = info_ptr->channels;
if (!(bit_depth == 8 &&
(channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
return -7;
goto exit;
}
unsigned char* row = malloc(width);
int y;
for (y = 0; y < height; ++y) {
png_read_row(png_ptr, row, NULL);
int w = (row[1] << 8) | row[0];
int h = (row[3] << 8) | row[2];
int len = row[4];
char* loc = row+5;
if (y+1+h >= height || matches_locale(loc)) {
printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
surface = malloc(sizeof(GGLSurface));
if (surface == NULL) {
result = -8;
goto exit;
}
unsigned char* pData = malloc(w*h);
surface->version = sizeof(GGLSurface);
surface->width = w;
surface->height = h;
surface->stride = w; /* Yes, pixels, not bytes */
surface->data = pData;
surface->format = GGL_PIXEL_FORMAT_A_8;
int i;
for (i = 0; i < h; ++i, ++y) {
png_read_row(png_ptr, row, NULL);
memcpy(pData + i*w, row, w);
}
*pSurface = (gr_surface) surface;
break;
} else {
int i;
//.........这里部分代码省略.........
示例12: pgeTextureScreenshot
void pgeTextureScreenshot(const char* filename)
{
png_structp png_ptr;
png_infop info_ptr;
FILE* fp;
unsigned char* line;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr)
return;
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return;
}
unsigned int col_type = PNG_COLOR_TYPE_RGBA;
fp = fopen(filename, "wb");
if (!fp) return;
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, 480, 272, 8, col_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
line = (unsigned char*) pgeMalloc(480 * 4);
if(line == 0)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return;
}
unsigned char* src = (unsigned char*)pgeGfxGetFramebuffer();
unsigned char* src8 = (unsigned char*)src;
unsigned short* src16 = (unsigned short*)src;
unsigned int* src32 = (unsigned int*)src;
int x, y, i;
for(y = 0; y < 272; y++)
{
src8 = (unsigned char*)src;
src16 = (unsigned short*)src;
src32 = (unsigned int*)src;
for (i = 0, x = 0; x < 480; x++)
{
unsigned int r = 0, g = 0, b = 0, a = 0;
unsigned int col32;
col32 = *src32++;
RGBA8888(col32, r, g, b, a);
line[i++] = r;
line[i++] = g;
line[i++] = b;
line[i++] = 255;
}
png_write_row(png_ptr, line);
src += ((512*32) >> 3);
}
pgeFree(line);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
}
示例13: IGN_Create_PNG_JPR
bool IGN_Create_PNG_JPR(LPCSTR mapname, long minx, long miny, long maxx, long maxy)
{
char tstring[256];
long bitmap_width = TILE_WIDTH*(maxx-minx)/1000;
long bitmap_size = bitmap_width*TILE_WIDTH;
unsigned char * bitmap_memory = (unsigned char *)malloc(bitmap_size);
CIGNTileDatabase db;
if (bitmap_memory == NULL) {
printf("Couldn't allocate %d bytes of memory\n");
return false;
}
// Create PNG header and write out palette
sprintf(tstring, "%s.png", mapname);
FILE * fp_png = fopen(tstring, "wb");
if (fp_png == NULL) {
printf("Couldn't open %s\n", tstring);
return false;
}
// Create and initialize the png_struct with the desired error handler functions.
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp_png);
return false;
}
/* Allocate/initialize the image information data. REQUIRED */
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp_png);
return false;
}
/* Set error handling. REQUIRED */
if (setjmp(png_jmpbuf(png_ptr))) {
/* If we get here, we had a problem reading the file */
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp_png);
return false;
}
/* set up the output control if you are using standard C streams */
png_init_io(png_ptr, fp_png);
/* 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
*/
png_set_IHDR(png_ptr, info_ptr, TILE_WIDTH*(maxx-minx)/1000, TILE_WIDTH*((maxy-miny)/1000), 8, PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
/* set the palette if there is one. REQUIRED for indexed-color images */
png_colorp png_palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof (png_color));
int i;
BYTE * pal_ptr = (BYTE *)png_palette;
BYTE * palette = ign_palette;
for (i=0; i<256; i++) {
*pal_ptr++ = palette[i*4 + 2];
*pal_ptr++ = palette[i*4 + 1];
*pal_ptr++ = palette[i*4 + 0];
}
/* ... set palette colors ... */
png_set_PLTE(png_ptr, info_ptr, png_palette, PNG_MAX_PALETTE_LENGTH);
/* You must not free palette here, because png_set_PLTE only makes a link to
* the palette that you malloced. Wait until you are about to destroy
*the png structure. */
/* Write the file header information. REQUIRED */
png_write_info(png_ptr, info_ptr);
CProgressWindow wndProgress;
wndProgress.Initialize();
wndProgress.ResetProgressBar("Tile:", (maxy-miny)/500*(maxx-minx)/500);
for (int y=maxy-1000; y>=miny; y-=1000) {
memset(bitmap_memory, 0x00, bitmap_size);
for (int x=minx; x<maxx; x+=1000) {
IGN_read_bmp(y, x, &db, bitmap_memory+(x-minx)/1000*TILE_WIDTH, bitmap_width);
}
// write row of PNG to file
for (int x=0; x<TILE_WIDTH; x++) {
png_write_row(png_ptr, bitmap_memory + x*bitmap_width);
if (!wndProgress.ProgressBar()) return false;
//.........这里部分代码省略.........
示例14: S9xDoScreenshot
bool8 S9xDoScreenshot (int width, int height)
{
Settings.TakeScreenshot = FALSE;
#ifdef HAVE_LIBPNG
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_color_8 sig_bit;
int imgwidth, imgheight;
const char *fname;
fname = S9xGetFilenameInc(".png", SCREENSHOT_DIR);
fp = fopen(fname, "wb");
if (!fp)
{
S9xMessage(S9X_ERROR, 0, "Failed to take screenshot.");
return (FALSE);
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fclose(fp);
remove(fname);
S9xMessage(S9X_ERROR, 0, "Failed to take screenshot.");
return (FALSE);
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
fclose(fp);
remove(fname);
S9xMessage(S9X_ERROR, 0, "Failed to take screenshot.");
return (FALSE);
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
remove(fname);
S9xMessage(S9X_ERROR, 0, "Failed to take screenshot.");
return (FALSE);
}
imgwidth = width;
imgheight = height;
if (Settings.StretchScreenshots == 1)
{
if (width > SNES_WIDTH && height <= SNES_HEIGHT_EXTENDED)
imgheight = height << 1;
}
else
if (Settings.StretchScreenshots == 2)
{
if (width <= SNES_WIDTH)
imgwidth = width << 1;
if (height <= SNES_HEIGHT_EXTENDED)
imgheight = height << 1;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, imgwidth, imgheight, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
sig_bit.red = 5;
sig_bit.green = 5;
sig_bit.blue = 5;
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
png_set_shift(png_ptr, &sig_bit);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
png_byte *row_pointer = new png_byte[png_get_rowbytes(png_ptr, info_ptr)];
uint16 *screen = GFX.Screen;
for (int y = 0; y < height; y++, screen += GFX.RealPPL)
{
png_byte *rowpix = row_pointer;
for (int x = 0; x < width; x++)
{
uint32 r, g, b;
DECOMPOSE_PIXEL(screen[x], r, g, b);
*(rowpix++) = r;
*(rowpix++) = g;
*(rowpix++) = b;
if (imgwidth != width)
{
*(rowpix++) = r;
//.........这里部分代码省略.........
示例15: LoadPNG
/**
The constructor loads the named PNG image from the given png filename.
<P>The destructor free all memory and server resources that are used by
the image.
*/
void LoadPNG(const char *png) // I - File to read
{
int i; // Looping var
FILE *fp; // File pointer
int channels; // Number of color channels
png_structp pp; // PNG read pointer
png_infop info; // PNG info pointers
png_bytep *rows; // PNG row pointers
// Open the PNG file...
if ((fp = fopen(png, "rb")) == NULL) return;
// Setup the PNG data structures...
pp = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info = png_create_info_struct(pp);
if (setjmp(pp->jmpbuf))
{
int debug = 1;
return;
}
// Initialize the PNG read "engine"...
png_init_io(pp, fp);
// Get the image dimensions and convert to grayscale or RGB...
png_read_info(pp, info);
if (info->color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(pp);
if (info->color_type & PNG_COLOR_MASK_COLOR)
channels = 3;
else
channels = 1;
if ((info->color_type & PNG_COLOR_MASK_ALPHA) || info->num_trans)
channels ++;
int w = (int)(info->width);
int h = (int)(info->height);
int d = channels;
pictureWidth = w;
pictureHeight = h;
if (info->bit_depth < 8)
{
png_set_packing(pp);
png_set_expand(pp);
}
else if (info->bit_depth == 16)
png_set_strip_16(pp);
# if defined(HAVE_PNG_GET_VALID) && defined(HAVE_PNG_SET_TRNS_TO_ALPHA)
// Handle transparency...
if (png_get_valid(pp, info, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(pp);
# endif // HAVE_PNG_GET_VALID && HAVE_PNG_SET_TRNS_TO_ALPHA
unsigned char *array = (unsigned char *)pictureS;
// Allocate pointers...
rows = new png_bytep[h];
for (i = 0; i < h; i ++)
rows[i] = (png_bytep)(array + i * w * d); // we flip it
// Read the image, handling interlacing as needed...
for (i = png_set_interlace_handling(pp); i > 0; i --)
png_read_rows(pp, rows, NULL, h);
#ifdef WIN32
// Some Windows graphics drivers don't honor transparency when RGB == white
if (channels == 4)
{
// Convert RGB to 0 when alpha == 0...
unsigned char *ptr = (unsigned char *)array;
for (i = w * h; i > 0; i --, ptr += 4)
if (!ptr[3]) ptr[0] = ptr[1] = ptr[2] = 0;
}
#endif // WIN32
if (channels == 3)
{
unsigned char *array2 = new unsigned char[pictureWidth * pictureHeight * 4];
for (int i = w * h - 1; i >= 0; --i)
{
array2[i*4+0] = array[i*3+0];
array2[i*4+1] = array[i*3+1];
array2[i*4+2] = array[i*3+2];
array2[i*4+3] = 255;
}
memcpy(array, array2, w * h * 4);
delete[] array2;
//.........这里部分代码省略.........