本文整理汇总了C++中png_read_info函数的典型用法代码示例。如果您正苦于以下问题:C++ png_read_info函数的具体用法?C++ png_read_info怎么用?C++ png_read_info使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_read_info函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: png_create_read_struct
void TextureLoader::loadPNGTexture(std::ifstream& textureFile, unsigned int& width, unsigned int& height, unsigned char*& data) {
// Read and validate header
png_byte header[8];
textureFile.read((char*) header, 8);
if (png_sig_cmp(header, 0, 8)) {
return;
}
// Create PNG structs
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
return;
}
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);
return;
}
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
return;
}
// PNG error handling
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return;
}
// Setup stream read function
png_set_read_fn(png_ptr, (png_voidp)& textureFile, readPNGData);
// Read PNG info
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
png_set_strip_16(png_ptr);
png_set_expand_gray_1_2_4_to_8(png_ptr);
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);
}
png_read_update_info(png_ptr, info_ptr);
int channels = png_get_channels(png_ptr, info_ptr);
if(channels != 4) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return;
}
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
// Allocate memory for data
int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
data = new unsigned char[rowbytes * height];
png_bytep *row_pointers = new png_bytep[height];
if (!data || !row_pointers) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return;
}
// Set up row_pointers to point at the correct offsets of data[]
for(unsigned int i = 0; i < height; ++i) {
//row_pointers[height - 1 - i] = data + i * rowbytes; // same thing
row_pointers[i] = data + (height - 1 - i) * rowbytes;
}
// Read the data
png_read_image(png_ptr, row_pointers);
}
示例2: png2pnm
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
volatile BOOL raw, BOOL alpha)
{
png_struct *png_ptr = NULL;
png_info *info_ptr = NULL;
png_byte buf[8];
png_byte *png_pixels = NULL;
png_byte **row_pointers = NULL;
png_byte *pix_ptr = NULL;
png_uint_32 row_bytes;
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int channels;
int color_type;
int alpha_present;
int row, col;
int ret;
int i;
long dep_16;
/* read and check signature in PNG file */
ret = fread (buf, 1, 8, png_file);
if (ret != 8)
return FALSE;
ret = png_sig_cmp (buf, 0, 8);
if (ret)
return FALSE;
/* create png and info structures */
png_ptr = png_create_read_struct (png_get_libpng_ver(NULL),
NULL, NULL, NULL);
if (!png_ptr)
return FALSE; /* out of memory */
info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr)
{
png_destroy_read_struct (&png_ptr, NULL, NULL);
return FALSE; /* out of memory */
}
if (setjmp (png_jmpbuf(png_ptr)))
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return FALSE;
}
/* set up the input control for C streams */
png_init_io (png_ptr, png_file);
png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
/* read the file information */
png_read_info (png_ptr, info_ptr);
/* get size and bit-depth of the PNG-image */
png_get_IHDR (png_ptr, info_ptr,
&width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
/* set-up the transformations */
/* transform paletted images into full-color rgb */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand (png_ptr);
/* expand images to bit-depth 8 (only applicable for grayscale images) */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand (png_ptr);
/* transform transparency maps into full alpha-channel */
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand (png_ptr);
#ifdef NJET
/* downgrade 16-bit images to 8-bit */
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
/* transform grayscale images into full-color */
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb (png_ptr);
/* only if file has a file gamma, we do a correction */
if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
png_set_gamma (png_ptr, (double) 2.2, file_gamma);
#endif
/* 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);
/* get the new color-type and bit-depth (after expansion/stripping) */
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
/* check for 16-bit files */
if (bit_depth == 16)
{
//.........这里部分代码省略.........
示例3: png_create_read_struct
// load in the image data
IImage* CImageLoaderPng::loadImage(io::IReadFile* file) const
{
#ifdef _IRR_COMPILE_WITH_LIBPNG_
if (!file)
return 0;
video::IImage* image = 0;
//Used to point to image rows
u8** RowPointers = 0;
png_byte buffer[8];
// Read the first few bytes of the PNG file
if( file->read(buffer, 8) != 8 )
{
os::Printer::log("LOAD PNG: can't read file\n", file->getFileName(), ELL_ERROR);
return 0;
}
// Check if it really is a PNG file
if( png_sig_cmp(buffer, 0, 8) )
{
os::Printer::log("LOAD PNG: not really a png\n", file->getFileName(), ELL_ERROR);
return 0;
}
// Allocate the png read struct
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, (png_error_ptr)png_cpexcept_error, NULL);
if (!png_ptr)
{
os::Printer::log("LOAD PNG: Internal PNG create read struct failure\n", file->getFileName(), ELL_ERROR);
return 0;
}
// Allocate the png info struct
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
os::Printer::log("LOAD PNG: Internal PNG create info struct failure\n", file->getFileName(), ELL_ERROR);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return 0;
}
// for proper error handling
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (RowPointers)
delete [] RowPointers;
return 0;
}
// changed by zola so we don't need to have public FILE pointers
png_set_read_fn(png_ptr, file, user_read_data_fcn);
png_set_sig_bytes(png_ptr, 8); // Tell png that we read the signature
png_read_info(png_ptr, info_ptr); // Read the info section of the png file
u32 Width;
u32 Height;
s32 BitDepth;
s32 ColorType;
{
// Use temporary variables to avoid passing casted pointers
png_uint_32 w,h;
// Extract info
png_get_IHDR(png_ptr, info_ptr,
&w, &h,
&BitDepth, &ColorType, NULL, NULL, NULL);
Width=w;
Height=h;
}
// Convert palette color to true color
if (ColorType==PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
// Convert low bit colors to 8 bit colors
if (BitDepth < 8)
{
if (ColorType==PNG_COLOR_TYPE_GRAY || ColorType==PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_1_2_4_to_8(png_ptr);
else
png_set_packing(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
// Convert high bit colors to 8 bit colors
if (BitDepth == 16)
png_set_strip_16(png_ptr);
// Convert gray color to true color
if (ColorType==PNG_COLOR_TYPE_GRAY || ColorType==PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
// Update the changes
//.........这里部分代码省略.........
示例4: png_texture_load
GLuint png_texture_load(const char * file_name, int * width, int * height)
{
png_byte header[8];
FILE *fp = fopen(file_name, "rb");
if (fp == 0)
{
perror(file_name);
return 0;
}
// read the header
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
{
fprintf(stderr, "error: %s is not a PNG.\n", file_name);
fclose(fp);
return 0;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fprintf(stderr, "error: png_create_read_struct returned 0.\n");
fclose(fp);
return 0;
}
// create png info struct
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fprintf(stderr, "error: png_create_info_struct returned 0.\n");
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fp);
return 0;
}
// create png info struct
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
fprintf(stderr, "error: png_create_info_struct returned 0.\n");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
fclose(fp);
return 0;
}
// the code in this if statement gets called if libpng encounters an error
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "error from libpng\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return 0;
}
// init png reading
png_init_io(png_ptr, fp);
// let libpng know you already read the first 8 bytes
png_set_sig_bytes(png_ptr, 8);
// read all the info up to the image data
png_read_info(png_ptr, info_ptr);
// variables to pass to get info
int bit_depth, color_type;
png_uint_32 temp_width, temp_height;
// get info about png
png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type,
NULL, NULL, NULL);
if (width){ *width = temp_width; }
if (height){ *height = temp_height; }
//printf("%s: %lux%lu %d\n", file_name, temp_width, temp_height, color_type);
if (bit_depth != 8)
{
fprintf(stderr, "%s: Unsupported bit depth %d. Must be 8.\n", file_name, bit_depth);
return 0;
}
GLint format;
switch(color_type)
{
case PNG_COLOR_TYPE_RGB:
format = GL_RGB;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
format = GL_RGBA;
break;
default:
fprintf(stderr, "%s: Unknown libpng color type %d.\n", file_name, color_type);
return 0;
}
// Update the png info struct.
//.........这里部分代码省略.........
示例5: pngLoadRawF
int APIENTRY pngLoadRawF(FILE *fp, pngRawInfo *pinfo) {
unsigned char header[8];
png_structp png;
png_infop info;
png_infop endinfo;
png_bytep data;
png_bytep *row_p;
double fileGamma;
png_uint_32 width, height;
int depth, color;
png_uint_32 i;
if (pinfo == NULL) return 0;
fread(header, 1, 8, fp);
if (!png_check_sig(header, 8)) return 0;
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info = png_create_info_struct(png);
endinfo = png_create_info_struct(png);
// DH: added following lines
if (setjmp(png->jmpbuf))
{
png_destroy_read_struct(&png, &info, &endinfo);
return 0;
}
// ~DH
png_init_io(png, fp);
png_set_sig_bytes(png, 8);
png_read_info(png, info);
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
pinfo->Width = width;
pinfo->Height = height;
pinfo->Depth = depth;
/*--GAMMA--*/
checkForGammaEnv();
if (png_get_gAMA(png, info, &fileGamma))
png_set_gamma(png, screenGamma, fileGamma);
else
png_set_gamma(png, screenGamma, 1.0/2.2);
png_read_update_info(png, info);
data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
for (i = 0; i < height; i++) {
if (StandardOrientation)
row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
else
row_p[i] = &data[png_get_rowbytes(png, info)*i];
}
png_read_image(png, row_p);
free(row_p);
if (color == PNG_COLOR_TYPE_PALETTE) {
int cols;
png_get_PLTE(png, info, (png_colorp *) &pinfo->Palette, &cols);
}
else {
pinfo->Palette = NULL;
}
if (color&PNG_COLOR_MASK_ALPHA) {
if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA)
pinfo->Components = 2;
else
pinfo->Components = 4;
pinfo->Alpha = 8;
}
else {
if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY)
pinfo->Components = 1;
else
pinfo->Components = 3;
pinfo->Alpha = 0;
}
pinfo->Data = data;
png_read_end(png, endinfo);
png_destroy_read_struct(&png, &info, &endinfo);
return 1;
}
示例6: fopen
/**
* loadTexture takes a filename and returns a Texture pointer to the texture place in memory
*/
Texture *Graphics::loadTexture(const char *filename)
{
// log it out
Log::info("Loading texture \"%s\"...", filename);
// header for testing if it is a png
png_byte header[8];
// open file as binary
FILE *fp = fopen(filename, "rb");
if (!fp)
{
Log::error("File failed to load!");
return NULL;
}
// read the header
fread(header, 1, 8, fp);
// test if png
int is_png = !png_sig_cmp(header, 0, 8);
if (!is_png)
{
Log::error("Image is not a PNG!");
fclose(fp);
return NULL;
}
// create png struct
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
Log::error("png_ptr failed to create read struct!");
fclose(fp);
return NULL;
}
// create png info struct
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
Log::error("info_ptr failed to create struct!");
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
fclose(fp);
return NULL;
}
// create png info struct
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
Log::error("end_info failed to create struct!");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
fclose(fp);
return NULL;
}
// png error stuff, not sure libpng man suggests this.
if (setjmp(png_jmpbuf(png_ptr)))
{
Log::error("png_jumpbuf overflow!");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return NULL;
}
// init png reading
png_init_io(png_ptr, fp);
//let libpng know you already read the first 8 bytes
png_set_sig_bytes(png_ptr, 8);
// read all the info up to the image data
png_read_info(png_ptr, info_ptr);
//variables to pass to get info
int bit_depth, color_type;
png_uint_32 twidth, theight;
// get info about png
png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, NULL, NULL, NULL);
// update width and height based on png info
int width = twidth;
int height = theight;
// update the png info struct.
png_read_update_info(png_ptr, info_ptr);
// row size in bytes.
int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
// allocate the image_data as a big block, to be given to opengl
png_byte *image_data = new png_byte[rowbytes * height];
if (!image_data)
{
Log::error("image_data failed to create a new png_byte!");
//.........这里部分代码省略.........
示例7: glmReadPNG
GLubyte*
glmReadPNG(const char* filename, GLboolean alpha, int* width_ret, int* height_ret, int* type_ret)
{
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
FILE *fp;
unsigned char *buffer;
int y, bytes_per_row;
int channels;
int format;
png_bytepp row_pointers;
if ((fp = fopen(filename, "rb")) == NULL) {
pngerror = ERR_OPEN;
return NULL;
}
/* 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,
(void *)user_error_ptr, user_error_fn, user_warning_fn);*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, err_callback, warn_callback);
if (png_ptr == NULL) {
pngerror = ERR_MEM;
fclose(fp);
return 0;
}
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
pngerror = ERR_MEM;
fclose(fp);
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return 0;
}
/* 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.
*/
buffer = NULL;
if (setjmp(setjmp_buffer)) {
pngerror = ERR_PNGLIB;
/* 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 */
if (buffer) free(buffer);
return NULL;
}
/* Set up the input control if you are using standard C streams */
png_init_io(png_ptr, fp);
/* 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,
&interlace_type, NULL, NULL);
/**** 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 th
* 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
* (not useful if you are using png_set_packing). */
/* png_set_packswap(png_ptr); */
/* expand paletted colors into true RGB triplets */
if (color_type == PNG_COLOR_TYPE_PALETTE)
//.........这里部分代码省略.........
示例8: load_png
int load_png(char * file_name, int * w, int * h, int * depth, char ** data)
{
#if COMPILE_PNG_CODE
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type,
compression_type, filter_type;
int rowbytes, channels;
unsigned int i;
char * buff;
char ** row_pointers;
fp = fopen(file_name, "rb");
if (!fp){
error_print("Cannot load required png-file (%s). Terminating!",file_name);
sys_exit(1);
}
png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING, (png_voidp)NULL/*user_error_ptr*/,
NULL/*user_error_fn*/, NULL/*user_warning_fn*/);
if (!png_ptr) {
error_print("Error png-ptr png-file (%s). Terminating!",file_name);
sys_exit(1);
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
error_print("Error create info struct png-file (%s). Terminating!",file_name);
sys_exit(1);
}
png_init_io(png_ptr, fp);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height,
&bit_depth, &color_type, &interlace_type,
&compression_type, &filter_type);
channels = png_get_channels(png_ptr, info_ptr);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
buff = (char *) malloc(height*rowbytes);
row_pointers = (char **) malloc(height*sizeof(char *));
for(i=0;i<height;i++){
row_pointers[i]=&buff[rowbytes*i];
}
png_read_image(png_ptr, (png_bytepp)row_pointers);
//fprintf(stderr,"load_png: rgstereo=%d\n",options_rgstereo_on);
if( options_rgstereo_on ){
//fprintf(stderr,"load_png: graying out texture\n");
for(i=0;i<height;i++){
int j,k,d;
for(j=0;j<rowbytes;j+=channels){
d=0;
for(k=0;k<channels;k++) d+=(unsigned char)row_pointers[i][j+k];
d/=channels;
for(k=0;k<channels;k++) row_pointers[i][j+k]=d;
}
}
}
free(row_pointers);
free(info_ptr);
free(png_ptr);
fclose(fp);
*data = buff;
*w = width;
*h = height;
*depth = channels*bit_depth;
return 1;
#else
*data = 0;
*w = 0;
*h = 0;
*depth = 0;
return 1;
#endif
}
示例9: png_create_read_struct
void PNGCodec::decode(std::vector<uint8_t> &in,
std::vector<uint8_t> *out, unsigned int *width,
unsigned int *height, ColorFormat *format, uint8_t level)
{
if (level != 0)
{
// TODO: Error handling.
return;
}
if (png_check_sig(in.data(), SIGNATURE_LENGTH) == false)
{
// TODO: Error handling.
return;
}
png_structp pngPtr = png_create_read_struct(
PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (pngPtr == nullptr)
{
// TODO: Error handling.
return;
}
png_infop pngInfoPtr = png_create_info_struct(pngPtr);
if (pngInfoPtr == nullptr)
{
// TODO: Error handling.
png_destroy_read_struct(&pngPtr, nullptr, nullptr);
return;
}
PNGVectorStream stream;
stream.vec = ∈
stream.offset = 0;
png_set_read_fn(pngPtr, &stream, PNGReadCallback);
png_read_info(pngPtr, pngInfoPtr);
int bitDepth = 0;
int colorType = -1;
png_uint_32 retval = png_get_IHDR(pngPtr, pngInfoPtr,
width,
height,
&bitDepth,
&colorType,
nullptr, nullptr, nullptr);
if (retval != 1)
{
// TODO: Error handling.
png_destroy_read_struct(&pngPtr, &pngInfoPtr, nullptr);
return;
}
switch (colorType)
{
case PNG_COLOR_TYPE_RGB:
*format = ColorFormat::RGB;
break;
case PNG_COLOR_TYPE_RGBA:
*format = ColorFormat::RGBA;
break;
default:
// TODO: Error handling.
png_destroy_read_struct(&pngPtr, &pngInfoPtr, nullptr);
return;
}
out->resize((*width) * (*height) * static_cast<unsigned int>(*format));
for (unsigned int i = 0; i < *height; ++i)
png_read_row(pngPtr,
&(*out)[i * (*width) * static_cast<unsigned int>(*format)], nullptr);
png_destroy_read_struct(&pngPtr, &pngInfoPtr, nullptr);
}
示例10: loadPNG
int loadPNG(const char *filename) {
GLuint texture;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytep *row_pointers = NULL;
int bitDepth, colourType;
FILE *pngFile = fopen(filename, "rb");
if (!pngFile)
return 0;
png_byte sig[8];
fread(&sig, 8, sizeof(png_byte), pngFile);
rewind(pngFile);
if (!png_check_sig(sig, 8)) {
//SDL_Log("png sig failure\n");
return 0;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
//SDL_Log("png ptr not created\n");
return 0;
}
if (setjmp(png_jmpbuf(png_ptr))) {
//SDL_Log("set jmp failed\n");
return 0;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
//SDL_Log("cant get png info ptr\n");
return 0;
}
png_init_io(png_ptr, pngFile);
png_read_info(png_ptr, info_ptr);
bitDepth = png_get_bit_depth(png_ptr, info_ptr);
colourType = png_get_color_type(png_ptr, info_ptr);
if (colourType == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (colourType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)
//png_set_gray_1_2_4_to_8(png_ptr);
png_set_expand_gray_1_2_4_to_8(png_ptr); // thanks to Jesse Jaara for bug fix for newer libpng...
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
if (bitDepth == 16)
png_set_strip_16(png_ptr);
else if (bitDepth < 8)
png_set_packing(png_ptr);
png_read_update_info(png_ptr, info_ptr);
png_uint_32 width, height;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colourType, NULL, NULL, NULL);
int components; // = GetTextureInfo(colourType);
switch (colourType) {
case PNG_COLOR_TYPE_GRAY:
components = 1;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
components = 2;
break;
case PNG_COLOR_TYPE_RGB:
components = 3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
components = 4;
break;
default:
components = -1;
}
if (components == -1) {
if (png_ptr)
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
//SDL_Log("%s broken?\n", filename);
return 0;
}
GLubyte *pixels =
(GLubyte *) malloc(sizeof(GLubyte) * (width * height * components));
row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * height);
int i = 0;
for (i = 0; i < height; ++i) {
row_pointers[i] = (png_bytep) (pixels + (i * width * components));
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, NULL);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLuint glcolours;
(components == 4) ? (glcolours = GL_RGBA) : (0);
(components == 3) ? (glcolours = GL_RGB) : (0);
(components == 2) ? (glcolours = GL_LUMINANCE_ALPHA) : (0);
(components == 1) ? (glcolours = GL_LUMINANCE) : (0);
//SDL_Log("%s has %i colour components\n",filename,components);
//glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, glcolours, GL_UNSIGNED_BYTE, pixels);
//.........这里部分代码省略.........
示例11: throw
void RawImage::loadPNG(FILE* file) throw(ImageException)
{
png_structp pngData = NULL;
png_infop pngInfo = NULL;
unsigned char** rowPtrs = NULL;
try {
pngData = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, // error_ptr
NULL, // error_fn
NULL // warn_fn
);
pngInfo = png_create_info_struct(pngData);
unsigned char header[8];
if (fread(header, 1, 8, file) < 8)
throw ImageException("Missing PNG header data.");
if (png_sig_cmp(header, 0, 8) != 0)
throw ImageException("Not a PNG file!");
if (setjmp(png_jmpbuf(pngData)))
throw ImageException("PNG library error.");
png_init_io(pngData, file);
png_set_sig_bytes(pngData, 8);
png_read_info(pngData, pngInfo);
png_uint_32 width, height;
int bitDepth, colorType, iMethod, cMethod, fMethod;
png_get_IHDR(pngData, pngInfo, &width, &height, &bitDepth, &colorType,
&iMethod, &cMethod, &fMethod);
int bytesPerChannel = (bitDepth / 8);
if (bitDepth % 8 != 0)
++bytesPerChannel;
_width = width;
_height = height;
switch (colorType) {
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(pngData);
_type = GL_RGB;
_bytesPerPixel = bytesPerChannel * 3;
break;
case PNG_COLOR_TYPE_RGBA:
_type = GL_RGBA;
_bytesPerPixel = bytesPerChannel * 4;
break;
case PNG_COLOR_TYPE_RGB:
_type = GL_RGB;
_bytesPerPixel = bytesPerChannel * 3;
break;
case PNG_COLOR_TYPE_GRAY:
if (bitDepth < 8)
png_set_gray_1_2_4_to_8(pngData);
_type = GL_ALPHA;
_bytesPerPixel = bytesPerChannel;
break;
default:
throw ImageException("Unknown PNG type.");
}
_pixels = new unsigned char[_bytesPerPixel * _width * _height];
rowPtrs = new unsigned char*[_height];
for (size_t i = 0; i < _height; ++i)
rowPtrs[i] = &_pixels[_bytesPerPixel * _width * (_height - i - 1)];
png_read_image(pngData, rowPtrs);
png_destroy_read_struct(&pngData, &pngInfo, NULL);
delete[] rowPtrs;
} catch (ImageException& e) {
png_destroy_read_struct(&pngData, &pngInfo, NULL);
if (rowPtrs != NULL)
delete[] rowPtrs;
throw e;
}
}
示例12: error
bool PNGLoader::doDecodeImage(const byte *fileDataPtr, uint fileSize, byte *&uncompressedDataPtr, int &width, int &height, int &pitch) {
#ifndef USE_INTERNAL_PNG_DECODER
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
int bitDepth;
int colorType;
int interlaceType;
int i;
// Check for valid PNG signature
if (!doIsCorrectImageFormat(fileDataPtr, fileSize)) {
error("png_check_sig failed");
}
// Die beiden PNG Strukturen erstellen
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
error("Could not create libpng read struct.");
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
error("Could not create libpng info struct.");
}
// Alternative Lesefunktion benutzen
const byte **ref = &fileDataPtr;
png_set_read_fn(png_ptr, (void *)ref, png_user_read_data);
// PNG Header einlesen
png_read_info(png_ptr, info_ptr);
// PNG Informationen auslesen
png_uint_32 w, h;
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bitDepth, &colorType, &interlaceType, NULL, NULL);
width = w;
height = h;
// Pitch des Ausgabebildes berechnen
pitch = GraphicEngine::calcPitch(GraphicEngine::CF_ARGB32, width);
// Speicher für die endgültigen Bilddaten reservieren
// Dieses geschieht vor dem reservieren von Speicher für temporäre Bilddaten um die Fragmentierung des Speichers gering zu halten
uncompressedDataPtr = new byte[pitch * height];
if (!uncompressedDataPtr) {
error("Could not allocate memory for output image.");
}
// Bilder jeglicher Farbformate werden zunächst in ARGB Bilder umgewandelt
if (bitDepth == 16)
png_set_strip_16(png_ptr);
if (colorType == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (bitDepth < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
if (colorType == PNG_COLOR_TYPE_GRAY ||
colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
png_set_bgr(png_ptr);
if (colorType != PNG_COLOR_TYPE_RGB_ALPHA)
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
// Nachdem die Transformationen registriert wurden, werden die Bilddaten erneut eingelesen
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bitDepth, &colorType, NULL, NULL, NULL);
width = w;
height = h;
if (interlaceType == PNG_INTERLACE_NONE) {
// PNGs without interlacing can simply be read row by row.
for (i = 0; i < height; i++) {
png_read_row(png_ptr, uncompressedDataPtr + i * pitch, NULL);
}
} else {
// PNGs with interlacing require us to allocate an auxillary
// buffer with pointers to all row starts.
// Allocate row pointer buffer
png_bytep *pRowPtr = new png_bytep[height];
if (!pRowPtr) {
error("Could not allocate memory for row pointers.");
}
// Initialize row pointers
for (i = 0; i < height; i++)
pRowPtr[i] = uncompressedDataPtr + i * pitch;
// Read image data
png_read_image(png_ptr, pRowPtr);
// Free row pointer buffer
delete[] pRowPtr;
}
//.........这里部分代码省略.........
示例13: pngReadImage
/* pngReadImage: Reads an image from a memory buffer.
Returns: non-zero if successful.
*/
int pngReadImage(int w, int h, int d, char* bits, char *data, int nBytes) {
png_bytep *row_pointers = NULL;
pngReadState rs;
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
DBG("pngReadImage: png_sig_cmp");
/* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */
if(png_sig_cmp(data, (png_size_t)0, PNG_BYTES_TO_CHECK)) return 0;
DBG("pngReadImage: png_create_read_struct");
/* Create the png_struct */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) return 0;
DBG("pngReadImage: png_create_info_struct");
/* Allocate/initialize the image information data. REQUIRED */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
return 0;
}
/* Set error handling. REQUIRED if you aren't supplying your own
* error handling functions in the png_create_read_struct() call.
*/
if (setjmp(png_jmpbuf(png_ptr))) {
DBG("pngReadImage: triggered png_error");
/* If we get here, we had a problem reading the file */
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
if(row_pointers) free(row_pointers);
return 0;
}
rs.data = data;
rs.position = 0;
rs.length = nBytes;
png_set_read_fn(png_ptr, (void *)&rs, readBytes);
DBG("pngReadImage: png_read_info");
/* 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,
&interlace_type, int_p_NULL, int_p_NULL);
/* 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);
/* flip the RGB pixels to BGR (or RGBA to BGRA) */
if (color_type & PNG_COLOR_MASK_COLOR) {
DBG("pngReadImage: png_set_bgr");
png_set_bgr(png_ptr);
}
if(d == 32 && color_type == PNG_COLOR_TYPE_RGB) {
/* Add filler (or alpha) byte (before/after each RGB triplet) */
DBG("pngReadImage: png_set_filler");
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
}
if(0) {
/* 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
* (not useful if you are using png_set_packing). */
png_set_packswap(png_ptr);
/* Expand paletted colors into true RGB triplets */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
}
/* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_gray_1_2_4_to_8(png_ptr);
/* Expand paletted or RGB images with transparency to full alpha channels
* so the data will be available as RGBA quartets.
*/
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
if(0) {
//.........这里部分代码省略.........
示例14: naMain
//.........这里部分代码省略.........
PNG_LIBPNG_VER_STRING,
(png_voidp)NULL, (png_error_ptr)NULL, (png_error_ptr)NULL
);
if (png_ptr == NULL) {
fputs(MSG[E_GENERIC], stderr);
fputc('\n', stderr);
fclose(f);
return EXIT_FAILURE;
}
info_ptr = png_create_info_struct(png_ptr);
end_info = png_create_info_struct(png_ptr);
try_png_read(
(info_ptr == NULL) || (end_info == NULL),
&png_ptr, &info_ptr, &end_info, f, (char*)NULL
);
try_png_read(
setjmp(png_jmpbuf(png_ptr)),
&png_ptr, &info_ptr, &end_info, f, MSG[E_READ]
);
png_init_io(png_ptr, f);
png_set_sig_bytes(png_ptr, SIG_BYTES);
/* Bildinfo lesen: Bilddimensionen und ggf. Farbpalette.
* Palette ggf. konvertieren. *********************************************/
long int width, height, pwidth, pheight;
// png_uint_32 width, height, pwidth, pheight;
comp_t *image, **row, *rwp;
png_read_info(png_ptr, info_ptr);
width = info_ptr->width;
height = info_ptr->height;
const long int original_width = info_ptr->width;
const int bit_depth = info_ptr->bit_depth,
color_type = info_ptr->color_type;
const bool image_is_pal = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE);
const bool image_is_gray = (
(color_type == PNG_COLOR_TYPE_GRAY) ||
(color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
);
const bool alpha = (
(color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
(color_type == PNG_COLOR_TYPE_RGB_ALPHA)
);
int num_palette = 0;
png_color *png_pal_got = NULL;
if (image_is_pal)
try_png_read(
!png_get_PLTE(png_ptr, info_ptr, &png_pal_got, &num_palette),
&png_ptr, &info_ptr, &end_info, f, MSG[E_READPAL]
);
png_color png_pal[num_palette];
comp_t comp_pal[num_palette];
if (image_is_pal)
示例15: png_read_png
void PNGAPI
png_read_png(png_structp png_ptr, png_infop info_ptr,
int transforms,
voidp params)
{
int row;
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
/* invert the alpha channel from opacity to transparency */
if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
png_set_invert_alpha(png_ptr);
#endif
/* The call to png_read_info() gives us all of the information from the
* PNG file before the first IDAT (image data chunk).
*/
png_read_info(png_ptr, info_ptr);
/* -------------- image transformations start here ------------------- */
#if defined(PNG_READ_16_TO_8_SUPPORTED)
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
if (transforms & PNG_TRANSFORM_STRIP_16)
png_set_strip_16(png_ptr);
#endif
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
/* Strip alpha bytes from the input data without combining with the
* background (not recommended).
*/
if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
png_set_strip_alpha(png_ptr);
#endif
#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
* byte into separate bytes (useful for paletted and grayscale images).
*/
if (transforms & PNG_TRANSFORM_PACKING)
png_set_packing(png_ptr);
#endif
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
/* Change the order of packed pixels to least significant bit first
* (not useful if you are using png_set_packing). */
if (transforms & PNG_TRANSFORM_PACKSWAP)
png_set_packswap(png_ptr);
#endif
#if defined(PNG_READ_EXPAND_SUPPORTED)
/* Expand paletted colors into true RGB triplets
* Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
* Expand paletted or RGB images with transparency to full alpha
* channels so the data will be available as RGBA quartets.
*/
if (transforms & PNG_TRANSFORM_EXPAND)
if ((png_ptr->bit_depth < 8) ||
(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
png_set_expand(png_ptr);
#endif
/* We don't handle background color or gamma transformation or dithering. */
#if defined(PNG_READ_INVERT_SUPPORTED)
/* invert monochrome files to have 0 as white and 1 as black */
if (transforms & PNG_TRANSFORM_INVERT_MONO)
png_set_invert_mono(png_ptr);
#endif
#if defined(PNG_READ_SHIFT_SUPPORTED)
/* If you want to shift the pixel values from the range [0,255] or
* [0,65535] to the original [0,7] or [0,31], or whatever range the
* colors were originally in:
*/
if ((transforms & PNG_TRANSFORM_SHIFT)
&& png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
{
png_color_8p sig_bit;
png_get_sBIT(png_ptr, info_ptr, &sig_bit);
png_set_shift(png_ptr, sig_bit);
}
#endif
#if defined(PNG_READ_BGR_SUPPORTED)
/* flip the RGB pixels to BGR (or RGBA to BGRA) */
if (transforms & PNG_TRANSFORM_BGR)
png_set_bgr(png_ptr);
#endif
#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
/* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
png_set_swap_alpha(png_ptr);
#endif
#if defined(PNG_READ_SWAP_SUPPORTED)
/* swap bytes of 16 bit files to least significant byte first */
if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
//.........这里部分代码省略.........