本文整理汇总了C++中png_destroy_read_struct函数的典型用法代码示例。如果您正苦于以下问题:C++ png_destroy_read_struct函数的具体用法?C++ png_destroy_read_struct怎么用?C++ png_destroy_read_struct使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_destroy_read_struct函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ReadPNG
int R2Image::
ReadPNG(const char *filename)
{
// Open file
FILE *fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Unable to open PNG file %s\n", filename);
return 0;
}
// Create and initialize the png_struct
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return 0;
}
// Allocate/initialize the memory for image information.
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return 0;
}
// Set up the input control if you are using standard C streams
png_init_io(png_ptr, fp);
// Read the png info
png_read_info(png_ptr, info_ptr);
// Extract image info
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
// Set nchannels
nchannels = 0;
if (color_type == PNG_COLOR_TYPE_GRAY) nchannels = 1;
else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) nchannels = 2;
else if (color_type == PNG_COLOR_TYPE_RGB) nchannels = 3;
else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) nchannels = 4;
else {
fclose(fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return 0;
}
// Allocate the pixels and row pointers
int rowsize = png_get_rowbytes(png_ptr, info_ptr);
assert(rowsize >= width * nchannels);
png_bytep buffer = new unsigned char [ height * rowsize * png_sizeof(png_bytep) ];
png_bytep *row_pointers = (png_bytep *) png_malloc(png_ptr, height * png_sizeof(png_bytep));
for (int i = 0; i < height; i++) row_pointers[i] = &buffer[ (height - i - 1) * rowsize ];
// Read the pixels
png_read_image(png_ptr, row_pointers);
// Finish reading
png_read_end(png_ptr, info_ptr);
// Copy pixels from temporary buffer
pixels = new double [ height * width * nchannels ];
for (int j = 0; j < height; j++) {
for (int i = 0; i < width * nchannels; i++) {
pixels[j*width*nchannels + i] = buffer[j*width*nchannels + i] / 255.0;
}
}
// Free the row pointers
png_free(png_ptr, row_pointers);
// Clean up after the read, and free any memory allocated
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
// Close the file
fclose(fp);
// Delete temporary buffer
delete [] buffer;
// Return success
return 1;
}
示例2: png_destroy_read_struct
~PNGAutoClean() {
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
}
示例3: fopen
struct gl_texture_t *ReadPNGFromFile (const char *filename)
{
FILE *fp;
png_byte magic[8];
/* Open image file */
fp = fopen (filename, "rb");
if (!fp) {
fprintf (stderr, "error: couldn't open \"%s\"!\n", filename);
return NULL;
}
/* Read magic number */
fread (magic, 1, sizeof (magic), fp);
/* Check for valid magic number */
if (!png_check_sig (magic, sizeof (magic))) {
fprintf (stderr, "error: \"%s\" is not a valid PNG image!\n",filename);
fclose (fp);
return NULL;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose (fp);
return NULL;
}
/* Create a png info struct */
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
fclose (fp);
png_destroy_read_struct (&png_ptr, NULL, NULL);
return NULL;
}
/* Create our OpenGL texture object */
struct gl_texture_t *texinfo = (struct gl_texture_t *) malloc (sizeof (struct gl_texture_t));
/* Initialize the setjmp for returning properly after a libpng
error occured */
if (setjmp (png_jmpbuf(png_ptr))) {
fclose (fp);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
if (texinfo) {
if (texinfo->texels)
free (texinfo->texels);
free (texinfo);
}
return NULL;
}
/* Setup libpng for using standard C fread() function
with our FILE pointer */
png_init_io (png_ptr, fp);
/* Tell libpng that we have already read the magic number */
png_set_sig_bytes (png_ptr, sizeof (magic));
/* Read png info */
png_read_info (png_ptr, info_ptr);
int bit_depth, color_type;
/* Get some usefull information from header */
bit_depth = png_get_bit_depth (png_ptr, info_ptr);
color_type = png_get_color_type (png_ptr, info_ptr);
/* Convert index color images to RGB images */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb (png_ptr);
/* Convert 1-2-4 bits grayscale images to 8 bits
grayscale. */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_gray_1_2_4_to_8 (png_ptr);
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha (png_ptr);
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
else if (bit_depth < 8)
png_set_packing (png_ptr);
/* Update info structure to apply transformations */
png_read_update_info (png_ptr, info_ptr);
/* Retrieve updated information */
png_uint_32 w, h;
png_get_IHDR (png_ptr, info_ptr, &w, &h, &bit_depth,&color_type, NULL, NULL, NULL);
texinfo->width = w;
texinfo->height = h;
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
texinfo->format = GL_LUMINANCE;
texinfo->internalFormat = 1;
break;
//.........这里部分代码省略.........
示例4: 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);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
//.........这里部分代码省略.........
示例5: fopen
void t_RenderClass::loadTexture(int target, const char *filename)
{
FILE *theFile = fopen(filename,"rb");
if (!theFile)
{
LOGI("Fopen failed");
exit(1);
}
char header[8];
fread(header,1,8,theFile);
if (ferror(theFile))
{
LOGI("Fread failed");
exit(1);
}
if (png_sig_cmp((png_byte *) header,0,8))
{
LOGI("The file is not a png\n");
exit(1);
}
png_structp png_ptr = png_create_read_struct(
PNG_LIBPNG_VER_STRING,
NULL,NULL,NULL
);
if (!png_ptr)
{
printf("Failed initialization of png_ptr\n");
exit(1);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr,
(png_infopp)NULL, (png_infopp)NULL);
printf("Info ptr creation failed\n");
exit(1);
}
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
png_destroy_read_struct(&png_ptr, &info_ptr,
(png_infopp)NULL);
printf("Info ptr creation failed\n");
exit(1);
}
png_init_io(png_ptr,theFile);
png_set_sig_bytes(png_ptr,8);
png_read_info(png_ptr,info_ptr);
int width = png_get_image_width(png_ptr,info_ptr);
int height = png_get_image_width(png_ptr,info_ptr);
int depth = png_get_bit_depth(png_ptr,info_ptr);
int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
png_byte* image_data = new png_byte[rowbytes * height];
png_bytep *row_pointers = new png_bytep [height];
for (int i = 0; i < height; ++i)
{
row_pointers[i] = image_data + i * rowbytes;
}
LOGI("I have read the image with width %d, height %d, depth %d, rowbytes %d\n",width,height,depth,rowbytes);
png_read_image(png_ptr,row_pointers);
png_read_end(png_ptr,NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,image_data);
checkGlError("glClear");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
checkGlError("glClear");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
checkGlError("glClear");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
checkGlError("glClear");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
checkGlError("glClear");
}
示例6: fopen
unsigned char *read_png_file(const char* file_name, int *width, int *height,
int *stride) {
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep* row_pointers;
int y;
unsigned char header[8]; // 8 is the maximum size that can be checked
unsigned char *buf;
FILE *fp;
/* open file and test for it being a png */
fp = fopen(file_name, "rb");
if (!fp) return NULL;
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8)) {
roadmap_log (ROADMAP_ERROR,
"[read_png_file] File %s is not recognized as a PNG file",
file_name);
fclose(fp);
return NULL;
}
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
roadmap_log (ROADMAP_ERROR,
"[read_png_file] png_create_read_struct failed");
fclose(fp);
return NULL;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
roadmap_log (ROADMAP_ERROR,
"[read_png_file] png_create_info_struct failed");
fclose(fp);
return NULL;
}
if (setjmp(png_jmpbuf(png_ptr))) {
roadmap_log (ROADMAP_ERROR, "[read_png_file] Error during init_io");
fclose(fp);
return NULL;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
*width = info_ptr->width;
*height = info_ptr->height;
*stride = info_ptr->rowbytes;
color_type = info_ptr->color_type;
bit_depth = info_ptr->bit_depth;
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr))) {
roadmap_log (ROADMAP_ERROR, "[read_png_file] Error during read_image");
fclose(fp);
return NULL;
}
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * *height);
buf = malloc (*height * info_ptr->rowbytes);
for (y=0; y<*height; y++) {
row_pointers[y] = (png_byte*) (buf + y * info_ptr->rowbytes);
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, NULL);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
free (row_pointers);
fclose(fp);
return buf;
}
示例7: png_create_info_struct
uint8_t* ImageDecoder::decodePNGImpl(png_structp pngPtr, uint32_t* width, uint32_t* height)
{
png_bytep* rowPtrs = NULL;
uint8_t* outData = NULL;
png_infop infoPtr = png_create_info_struct(pngPtr);
if (!infoPtr)
{
LOG(LOG_ERROR,"Couldn't initialize png info struct");
png_destroy_read_struct(&pngPtr, (png_infopp)0, (png_infopp)0);
return NULL;
}
if (setjmp(png_jmpbuf(pngPtr)))
{
png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0);
if (rowPtrs != NULL) delete [] rowPtrs;
if (outData != NULL) delete [] outData;
LOG(LOG_ERROR,"error during reading of the png file");
return NULL;
}
png_read_info(pngPtr, infoPtr);
*width = png_get_image_width(pngPtr, infoPtr);
*height = png_get_image_height(pngPtr, infoPtr);
//bits per CHANNEL! note: not per pixel!
png_uint_32 bitdepth = png_get_bit_depth(pngPtr, infoPtr);
//Number of channels
png_uint_32 channels = png_get_channels(pngPtr, infoPtr);
//Color type. (RGB, RGBA, Luminance, luminance alpha... palette... etc)
png_uint_32 color_type = png_get_color_type(pngPtr, infoPtr);
// Transform everything into 24 bit RGB
switch (color_type)
{
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(pngPtr);
// png_set_palette_to_rgb wil convert into 32
// bit, but we don't want the alpha
png_set_strip_alpha(pngPtr);
break;
case PNG_COLOR_TYPE_GRAY:
if (bitdepth < 8)
png_set_gray_to_rgb(pngPtr);
break;
}
if (bitdepth == 16)
{
png_set_strip_16(pngPtr);
}
if (channels > 3)
{
LOG(LOG_NOT_IMPLEMENTED, "Alpha channel not supported in PNG");
png_set_strip_alpha(pngPtr);
}
// Update the infoPtr to reflect the transformations set
// above. Read new values by calling png_get_* again.
png_read_update_info(pngPtr, infoPtr);
//bitdepth = png_get_bit_depth(pngPtr, infoPtr);
//color_type = png_get_color_type(pngPtr, infoPtr);
channels = png_get_channels(pngPtr, infoPtr);
if (channels != 3)
{
// Should never get here because of the
// transformations
LOG(LOG_NOT_IMPLEMENTED, "Unexpected number of channels in PNG!");
png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0);
return NULL;
}
const unsigned int stride = png_get_rowbytes(pngPtr, infoPtr);
outData = new uint8_t[(*height) * stride];
rowPtrs = new png_bytep[(*height)];
for (size_t i = 0; i < (*height); i++)
{
rowPtrs[i] = (png_bytep)outData + i* stride;
}
png_read_image(pngPtr, rowPtrs);
png_read_end(pngPtr, NULL);
png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0);
delete[] (png_bytep)rowPtrs;
return outData;
}
示例8: load_png_image
int load_png_image(const char *filepath,
struct pngload_attribute *png_attr)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_bytep* row_pointers;
char buf[PNG_BYTES_TO_CHECK];
int w, h, x, y, temp, color_type;
fp = fopen( filepath, "rb" );
if( fp == NULL ) {
return -1;
}
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
info_ptr = png_create_info_struct( png_ptr );
setjmp( png_jmpbuf(png_ptr) );
/* 读取PNG_BYTES_TO_CHECK个字节的数据 */
temp = fread( buf, 1, PNG_BYTES_TO_CHECK, fp );
/* 若读到的数据并没有PNG_BYTES_TO_CHECK个字节 */
if( temp < PNG_BYTES_TO_CHECK ) {
fclose(fp);
png_destroy_read_struct( &png_ptr, &info_ptr, 0);
return -3;
}
/* 检测数据是否为PNG的签名 */
temp = png_sig_cmp( (png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK );
/* 如果不是PNG的签名,则说明该文件不是PNG文件 */
if( temp != 0 ) {
fclose(fp);
png_destroy_read_struct( &png_ptr, &info_ptr, 0);
return -4;
}
/* 复位文件指针 */
rewind( fp );
/* 开始读文件 */
png_init_io( png_ptr, fp );
/* 读取PNG图片信息 */
png_read_png( png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0 );
/* 获取图像的色彩类型 */
color_type = png_get_color_type( png_ptr, info_ptr );
/* 获取图像的宽高 */
png_attr->width = w = png_get_image_width( png_ptr, info_ptr );
png_attr->height = h = png_get_image_height( png_ptr, info_ptr );
printf("w=%d,h=%d\n", w, h);
/* 获取图像的所有行像素数据,row_pointers里边就是rgba数据 */
row_pointers = png_get_rows( png_ptr, info_ptr );
unsigned char *data;
int index = 0;
/* 根据不同的色彩类型进行相应处理 */
png_attr->color_type = color_type;
switch( color_type ) {
case PNG_COLOR_TYPE_RGB_ALPHA:
printf("rgba\n");
data = malloc(w*h*4);
png_attr->buf = data;
for( y=0; y<h; ++y ) {
for( x=0; x<w*4; ) {
/* 以下是RGBA数据,需要自己补充代码,保存RGBA数据 */
data[index++] = row_pointers[y][x++]; // red
data[index++] = row_pointers[y][x++]; // green
data[index++] = row_pointers[y][x++]; // blue
data[index++] = row_pointers[y][x++]; // alpha
}
}
break;
case PNG_COLOR_TYPE_RGB:
printf("rgb\n");
data = malloc(w*h*3);
png_attr->buf = data;
for( y=0; y<h; ++y ) {
for( x=0; x<w*3; ) {
data[index++] = row_pointers[y][x++]; // red
data[index++] = row_pointers[y][x++]; // green
data[index++] = row_pointers[y][x++]; // blue
}
}
break;
/* 其它色彩类型的图像就不读了 */
default:
fclose(fp);
png_destroy_read_struct( &png_ptr, &info_ptr, 0);
return -2;
}
png_destroy_read_struct( &png_ptr, &info_ptr, 0);
return 0;
}
示例9: png_read_bitmap
// Loads a PNG image
//
// filename - name of the targa file to load
// image_data - allocated storage for the bitmap
//
// returns - true if succesful, false otherwise
//
int png_read_bitmap(char *real_filename, ubyte *image_data, ubyte *bpp, int dest_size, int cf_type)
{
char filename[MAX_FILENAME_LEN];
png_infop info_ptr;
png_structp png_ptr;
png_bytepp row_pointers;
unsigned int i, len;
png_file = NULL;
strcpy_s( filename, real_filename );
char *p = strchr( filename, '.' );
if ( p ) *p = 0;
strcat_s( filename, ".png" );
png_file = cfopen(filename, "rb", CFILE_NORMAL, cf_type);
if (png_file == NULL)
return PNG_ERROR_READING;
/* 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, NULL, NULL, NULL);
if (png_ptr == NULL)
{
mprintf(("png_read_bitmap: png_ptr went wrong\n"));
cfclose(png_file);
return PNG_ERROR_READING;
}
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
mprintf(("png_read_bitmap: info_ptr went wrong\n"));
cfclose(png_file);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return PNG_ERROR_READING;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
mprintf(("png_read_bitmap: something went wrong\n"));
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
cfclose(png_file);
/* If we get here, we had a problem reading the file */
return PNG_ERROR_READING;
}
png_set_read_fn(png_ptr, &png_file, png_scp_read_data);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16, NULL);
len = png_get_rowbytes(png_ptr, info_ptr);
row_pointers = png_get_rows(png_ptr, info_ptr);
if(bpp)
*bpp = (ubyte)(len / png_get_image_width(png_ptr, info_ptr)) << 3;
//copy row data to image
unsigned int height = png_get_image_height(png_ptr, info_ptr);
for (i = 0; i < height; i++) {
memcpy(&image_data[i * len], row_pointers[i], len);
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
cfclose(png_file);
return PNG_ERROR_NONE;
}
示例10: png_destroy_read_struct
unsigned char *readpng_get_image( png_store* pngdata)
{
double gamma;
png_uint_32 i, rowbytes;
png_bytepp row_pointers = NULL;
png_structp png_ptr = pngdata->png_ptr;
png_infop info_ptr = pngdata->info_ptr;
int color_type = pngdata->colortype;
int bit_depth = pngdata->bitdepth;
int height = pngdata->height;
double display_exponent = DISPLAY_EXPONENT;
int *pChannels = &(pngdata->channels);
unsigned long *pRowbytes = &(pngdata->rowbytes);
unsigned char * image_data;
/* 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 NULL;
}
/* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
* transparency chunks to full alpha channel; strip 16-bit-per-sample
* images to 8 bits per sample; and convert grayscale to RGB[A] */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
if (bit_depth == 16)
png_set_strip_16(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
/* unlike the example in the libpng documentation, we have *no* idea where
* this file may have come from--so if it doesn't have a file gamma, don't
* do any correction ("do no harm") */
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
png_set_gamma(png_ptr, display_exponent, gamma);
/* all transformations have been registered; now update info_ptr data,
* get rowbytes and channels, and allocate image memory */
png_read_update_info(png_ptr, info_ptr);
*pRowbytes = rowbytes = png_get_rowbytes(png_ptr, info_ptr);
*pChannels = (int)png_get_channels(png_ptr, info_ptr);
if ((image_data = (unsigned char *)malloc(rowbytes*height)) == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return NULL;
}
if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
free(image_data);
image_data = NULL;
return NULL;
}
/* set the individual row_pointers to point at the correct offsets */
for (i = 0; i < height; ++i)
row_pointers[i] = image_data + i*rowbytes;
/* now we can go ahead and just read the whole image */
png_read_image(png_ptr, row_pointers);
/* and we're done! (png_read_end() can be omitted if no processing of
* post-IDAT text/time/etc. is desired) */
// free(row_pointers);
// row_pointers = NULL;
png_read_end(png_ptr, NULL);
pngdata->image_data = image_data;
pngdata->row_pointers = row_pointers;
return image_data;
}
示例11: 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;
}
示例12: LoadPNG
//.........这里部分代码省略.........
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;
}
// Free memory and return...
delete[] rows;
png_read_end(pp, info);
png_destroy_read_struct(&pp, &info, NULL);
fclose(fp);
}
示例13: rtgui_image_png_load
static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
{
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int color_type;
double gamma;
struct rtgui_image_png *png;
png = (struct rtgui_image_png *) rtgui_malloc(sizeof(struct rtgui_image_png));
png->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png->png_ptr == RT_NULL)
{
rtgui_free(png);
return RT_FALSE;
}
png->info_ptr = png_create_info_struct(png->png_ptr);
if (png->info_ptr == RT_NULL)
{
png_destroy_read_struct(&png->png_ptr, NULL, NULL);
rtgui_free(png);
return RT_FALSE;
}
png->filerw = file;
png_set_read_fn(png->png_ptr, png->filerw, rtgui_image_png_read_data);
png_read_info(png->png_ptr, png->info_ptr);
png_get_IHDR(png->png_ptr, png->info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
/* set image information */
image->w = width;
image->h = height;
image->engine = &rtgui_image_png_engine;
image->data = png;
if (bit_depth == 16)
png_set_strip_16(png->png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png->png_ptr);
if (bit_depth < 8)
png_set_expand(png->png_ptr);
if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS))
png_set_expand(png->png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png->png_ptr);
/* Ignore background color */
/* set gamma conversion */
if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma))
png_set_gamma(png->png_ptr, (double)2.2, gamma);
png_read_update_info(png->png_ptr, png->info_ptr);
if (load == RT_TRUE)
{
/* load all pixels */
png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t));
if (png->pixels == RT_NULL)
{
png_read_end(png->png_ptr, RT_NULL);
/* destroy png struct */
png_destroy_info_struct(png->png_ptr, &png->info_ptr);
png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL);
/* release data */
rtgui_free(png);
return RT_FALSE;
}
rtgui_image_png_process(png->png_ptr, png->info_ptr, png);
}
else
{
png->pixels = RT_NULL;
}
return RT_TRUE;
}
示例14: png_destroy_read_struct
~png_struct_guard()
{
png_destroy_read_struct(p_,i_,0);
}
示例15: fopen
//.........这里部分代码省略.........
"2nd png_create_info_struct() returned NULL");
goto cleanup;
}
if (setjmp(png_jmpbuf(png_ptr))) {
snprintf(whynot, whynotlen, "libpng encounted an error");
goto cleanup;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
/*
* PNG_TRANSFORM_STRIP_16 |
* PNG_TRANSFORM_PACKING forces 8 bit
* PNG_TRANSFORM_EXPAND forces to expand a palette into RGB
*/
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
png_get_IHDR(png_ptr, info_ptr, &tw, &th, &bit_depth, &color_type, NULL, NULL, NULL);
if (bit_depth != 8) {
snprintf(whynot, whynotlen, "load_png_texture only supports 8-bit image channel depth");
goto cleanup;
}
if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
snprintf(whynot, whynotlen, "load_png_texture only supports RGB and RGBA");
goto cleanup;
}
if (w)
*w = tw;
if (h)
*h = th;
int has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
if (hasAlpha)
*hasAlpha = has_alpha;
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
image_data_row_bytes = row_bytes;
/* align to 4 byte boundary */
if (image_data_row_bytes & 0x03)
image_data_row_bytes += 4 - (image_data_row_bytes & 0x03);
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
image_data = malloc(image_data_row_bytes * th * sizeof(png_byte) + 15);
if (!image_data) {
snprintf(whynot, whynotlen, "malloc failed in load_png_texture");
goto cleanup;
}
int bytes_per_pixel = (color_type == PNG_COLOR_TYPE_RGB_ALPHA ? 4 : 3);
for (i = 0; i < th; i++) {
png_byte *src_row;
png_byte *dest_row = image_data + i * image_data_row_bytes;
if (flipVertical)
src_row = row_pointers[th - i - 1];
else
src_row = row_pointers[i];
if (flipHorizontal) {
for (j = 0; j < tw; j++) {
png_byte *src = src_row + bytes_per_pixel * j;
png_byte *dest = dest_row + bytes_per_pixel * (tw - j - 1);
memcpy(dest, src, bytes_per_pixel);
}
} else {
memcpy(dest_row, src_row, row_bytes);
}
if (has_alpha && pre_multiply_alpha) {
for (j = 0; j < tw; j++) {
png_byte *pixel = dest_row + bytes_per_pixel * j;
float alpha = pixel[3] / 255.0;
pixel[0] = pixel[0] * alpha;
pixel[1] = pixel[1] * alpha;
pixel[2] = pixel[2] * alpha;
}
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return (char *)image_data;
cleanup:
if (image_data)
free(image_data);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
#else
snprintf(whynot, whynotlen, "load_png_texture: compiled without opengl support.");
#endif
return 0;
}