本文整理汇总了C++中png_sig_cmp函数的典型用法代码示例。如果您正苦于以下问题:C++ png_sig_cmp函数的具体用法?C++ png_sig_cmp怎么用?C++ png_sig_cmp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_sig_cmp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: png_load_from_memory
void png_load_from_memory(aasset_buffer_t& buf, raw_bitmap_t* out_bitmap)
{
//png header - 8 bytes
if (png_sig_cmp(buf.data(), 0, 8))
{
my_assert(false && "Couldn't load texture. It isn't png file.");
return;
}
auto png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
my_assert(png_ptr && "Couldn't load texture. Png load error.");
MAKE_GUARD(png_ptr, [](decltype(png_ptr) ptr)
{ png_destroy_read_struct(&ptr, (png_infopp)NULL, (png_infopp)NULL);});
auto info_ptr = png_create_info_struct(png_ptr);
my_assert(info_ptr && "Couldn't load texture. Png load error.");
auto info_deleter = [png_ptr](decltype(info_ptr) ptr)
{ png_destroy_info_struct(png_ptr, &ptr);};
MAKE_GUARD(info_ptr, info_deleter);
// the code in this if statement gets called if libpng encounters an error
if (setjmp((long*) png_jmpbuf(png_ptr)))
{
my_assert(false && "error from libpng");
return;
}
png_set_read_fn(png_ptr, &buf, read_from_aasset_buffer);
// tell libpng we already read the signature
png_set_sig_bytes(png_ptr, 8);
buf.seek(8);
png_read_info(png_ptr, info_ptr);
png_uint_32 width = 0;
png_uint_32 height = 0;
int bitDepth = 0;
int colorType = -1;
auto res = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, NULL, NULL, NULL);
my_assert(res == 1);
bool transparency = false;
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
transparency = true;
}
// Expands PNG with less than 8bits per channel to 8bits.
if (bitDepth < 8)
{
png_set_packing(png_ptr);
// Shrinks PNG with 16bits per color channel down to 8bits.
}
else if (bitDepth == 16)
{
png_set_strip_16(png_ptr);
}
uint32_t format = 0;
switch (colorType)
{
case PNG_COLOR_TYPE_RGB:
format = transparency ? GL_RGBA : GL_RGB;
break;
case PNG_COLOR_TYPE_RGBA:
format = GL_RGBA;
break;
case PNG_COLOR_TYPE_PALETTE:
{
png_set_palette_to_rgb(png_ptr);
format = transparency ? GL_RGBA : GL_RGB;
break;
}
default:
my_assert(false && "Png read error. Uknown color type");
return;
}
//apply transformations
png_read_update_info(png_ptr, info_ptr);
out_bitmap->init(width, height, format);
auto row_size = png_get_rowbytes(png_ptr, info_ptr);
my_assert(row_size > 0);
my_assert(row_size == width * out_bitmap->bytes_per_pixel());
auto row_ptrs = new png_bytep[height];
MAKE_GUARD(row_ptrs, [](decltype(row_ptrs) ptr)
{ delete[] ptr;});
for (int32_t i = 0; i < height; ++i)
{
row_ptrs[height - (i + 1)] = out_bitmap->get_row(i);
}
png_read_image(png_ptr, row_ptrs);
}
示例2: LoadPng
TypedImage LoadPng(const std::string& filename)
{
#ifdef HAVE_PNG
FILE *in = fopen(filename.c_str(), "rb");
if( in ) {
//check the header
const size_t nBytes = 8;
png_byte header[nBytes];
size_t nread = fread(header, 1, nBytes, in);
int nIsPNG = png_sig_cmp(header, 0, nread);
if ( nIsPNG != 0 ) {
throw std::runtime_error( filename + " is not a PNG file" );
}
//set up initial png structs
png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, &PngWarningsCallback);
if (!png_ptr) {
throw std::runtime_error( "PNG Init error 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);
throw std::runtime_error( "PNG Init error 2" );
}
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
throw std::runtime_error( "PNG Init error 3" );
}
png_init_io(png_ptr, in);
png_set_sig_bytes(png_ptr, nBytes);
//read the file
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
if( png_get_bit_depth(png_ptr, info_ptr) == 1) {
//Unpack bools to bytes to ease loading.
png_set_packing(png_ptr);
} else if( png_get_bit_depth(png_ptr, info_ptr) < 8) {
//Expand nonbool colour depths up to 8bpp
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
//Get rid of palette, by transforming it to RGB
if(png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
}
if( png_get_interlace_type(png_ptr,info_ptr) != PNG_INTERLACE_NONE) {
throw std::runtime_error( "Interlace not yet supported" );
}
const size_t w = png_get_image_width(png_ptr,info_ptr);
const size_t h = png_get_image_height(png_ptr,info_ptr);
const size_t pitch = png_get_rowbytes(png_ptr, info_ptr);
TypedImage img;
img.Alloc(w, h, PngFormat(png_ptr, info_ptr), pitch);
png_bytepp rows = png_get_rows(png_ptr, info_ptr);
for( unsigned int r = 0; r < h; r++) {
memcpy( img.ptr + pitch*r, rows[r], pitch );
}
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(in);
return img;
}
throw std::runtime_error("Unable to load PNG file, '" + filename + "'");
#else
throw std::runtime_error("PNG Support not enabled. Please rebuild Pangolin.");
#endif
}
示例3: fopen
void png::load(const std::string& file_name)
{
// unfortunately, we need to break down to the C-code level here, since
// libpng is written in C itself
// we need to open the file in binary mode
FILE* fp = fopen(file_name.c_str(), "rb");
if (!fp)
{
throw std::runtime_error{"failed to open " + file_name};
}
// read in the header (max size of 8), use it to validate this as a png file
png_byte header[8];
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
{
fclose(fp);
throw std::runtime_error{file_name + " is not a valid png file"};
}
// set up libpng structs for reading info
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
nullptr, nullptr);
if (!png_ptr)
{
fclose(fp);
throw std::runtime_error{"Failed to create libpng read struct"};
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
fclose(fp);
throw std::runtime_error{"Failed to create libpng info struct"};
}
// set error handling to not abort the entire program
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
fclose(fp);
throw std::runtime_error{"Error reading png metadata"};
}
// initialize png reading
png_init_io(png_ptr, fp);
// let it know we've already read the first 8 bytes
png_set_sig_bytes(png_ptr, 8);
// read in the basic image info
png_read_info(png_ptr, info_ptr);
// convert to 8 bits
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
if (bit_depth == 16)
png_set_strip_16(png_ptr);
// verify this is in RGBA format, and if not, convert it to RGBA
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
if (color_type != PNG_COLOR_TYPE_RGBA && color_type != PNG_COLOR_TYPE_RGB)
{
if (color_type == PNG_COLOR_TYPE_GRAY || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA)
{
if (bit_depth < 8)
png_set_expand(png_ptr);
png_set_gray_to_rgb(png_ptr);
}
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
}
// convert tRNS to alpha channel
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
size_t width = png_get_image_width(png_ptr, info_ptr);
size_t height = png_get_image_height(png_ptr, info_ptr);
rgba_pixel* newpix = nullptr;
png_byte* row = nullptr;
png_read_update_info(png_ptr, info_ptr);
// begin reading in the image
if (setjmp(png_jmpbuf(png_ptr)))
{
delete[] newpix;
delete[] row;
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
fclose(fp);
throw std::runtime_error{"Error reading image with libpng"};
}
int bpr = png_get_rowbytes(png_ptr, info_ptr); // number of bytes in a row
// initialize our image storage
newpix = new rgba_pixel[height * width];
row = new png_byte[bpr];
//.........这里部分代码省略.........
示例4: load_png
uint32_t * load_png(struct load_image *args)
{
png_structp png;
png_infop info;
int color_type;
void *rgb_data;
png_bytep *pointers;
int i;
png_uint_32 w,h;
int depth;
struct png_context context;
png_uint_32 stride;
if(png_sig_cmp(args->data, 0, 8)) {
DEBUG_PUTS("not png");
return 0;
}
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
info = png_create_info_struct(png);
context.data = args->data;
context.len = args->length;
context.pos = 8;
png_set_read_fn(png, &context, &read_callback);
png_set_sig_bytes(png, 8);
png_read_info(png, info);
png_get_IHDR(png, info, &w, &h, &depth, &color_type, 0, 0, 0);
DEBUG_PRINTF("w: %d\n", w);
DEBUG_PRINTF("h: %d\n", h);
switch (color_type) {
case PNG_COLOR_TYPE_PALETTE:
DEBUG_PUTS("pallete");
png_set_palette_to_rgb(png);
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
DEBUG_PUTS("rgba");
break;
default:
rgb_data = 0;
goto out;
};
png_set_filler(png, 0xff, PNG_FILLER_AFTER);
pointers = (png_bytep *) malloc(h * sizeof(*pointers));
if (!pointers) {
rgb_data = 0;
goto out;
}
stride = png_get_rowbytes(png, info);
rgb_data = malloc(stride * 4 * h);
if (!rgb_data) {
rgb_data = 0;
goto out;
}
for (i = 0; i < h; i++)
pointers[i] = rgb_data + w * 4 * i;
png_read_image(png, pointers);
*args->w = w;
*args->h = h;
out:
png_destroy_read_struct(&png, &info, 0);
return rgb_data;
}
示例5: read_image_file_png
/* return -2 if file cannot be opened
* return -1 if file data is corrupted
* return 0 if file format is not valid
* return 1 if file successfully loaded
*/
int read_image_file_png (const char* filename, ImageData *imagedata)
{
int i, width, height, BPP;
unsigned char* pixelArray = NULL;
png_bytep* row_pointers;
png_byte color_type;
png_infop info_ptr;
png_structp png_ptr;
png_byte header[8];
FILE *fp;
/* open file for reading-bytes */
fp = fopen (filename, "rb");
if(!fp) ABORT("Read Error: file '%s' could not be opened for reading.", filename, -2)
/* Test if file type is png */
/* 8 bytes is the maximum size that can be checked */
if(fread(header, 1, 8, fp) != 8
|| png_sig_cmp(header, 0, 8))
ABORT("Read Error: file '%s' is not recognized as a PNG file.", filename, 0)
/* initialise - prepare for parsing */
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr)
ABORT("Read Error: file '%s' is corrupted. png_create_read_struct failed.", filename, -1)
info_ptr = png_create_info_struct (png_ptr);
if(!info_ptr)
ABORT("Read Error: file '%s' is corrupted. png_create_info_struct failed.", filename, -1)
if(setjmp(png_jmpbuf(png_ptr)))
ABORT("Read Error: file '%s' is corrupted. setjmp(png_jmpbuf(png_ptr)) failed.", filename, -1)
png_init_io (png_ptr, fp);
png_set_sig_bytes (png_ptr, 8);
png_read_info (png_ptr, info_ptr);
/* ensure 3-byte colour depth */
color_type = png_get_color_type (png_ptr, info_ptr);
if(color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb (png_ptr);
/* ensure Alpha channel present */
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
/*png_get_bit_depth(png_ptr, info_ptr) = 8... why so? */
height = png_get_image_height(png_ptr, info_ptr);
width = png_get_image_width (png_ptr, info_ptr);
BPP = 4;
/* ensure BGRA and not ARGB */
png_set_bgr(png_ptr);
/* make libpng to handle expansion of the interlaced image */
png_set_interlace_handling (png_ptr);
/* reflect requested transformations */
png_read_update_info (png_ptr, info_ptr);
/* prepare to read file */
if(setjmp(png_jmpbuf(png_ptr)))
ABORT("Read Error: file '%s' is corrupted. setjmp(png_jmpbuf(png_ptr)) failed.", filename, -1)
/* allocate memory needed to hold the image */
pixelArray = (unsigned char*) malloc (height*width*BPP);
row_pointers = (png_bytep*) malloc (height * sizeof(*row_pointers));
for(i = 0; i < height; i++)
row_pointers[i] = (png_bytep) (pixelArray + i*width*BPP);
/* finally... read image file... */
png_read_image(png_ptr, row_pointers);
/* free resources */
free(row_pointers);
fclose(fp);
/* success */
imagedata->pixelArray = pixelArray;
imagedata->height = height;
imagedata->width = width;
imagedata->bpp = BPP*8;
return 1;
}
示例6: res_create_surface_png
int res_create_surface_png(const char* name, gr_surface* pSurface) {
GGLSurface* surface = NULL;
int result = 0;
unsigned char header[8];
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
FILE* fp = fopen(name, "rb");
if (fp == NULL) {
char resPath[256];
snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
resPath[sizeof(resPath)-1] = '\0';
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_set_packing(png_ptr);
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;
size_t pixelSize = stride * height;
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)))) {
return -7;
goto exit;
}
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 : GGL_PIXEL_FORMAT_RGBA_8888;
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
}
int x;
size_t y;
if (channels == 3) {
for (y = 0; y < height; ++y) {
unsigned char* pRow = pData + y * stride;
png_read_row(png_ptr, pRow, NULL);
for(x = width - 1; x >= 0; x--) {
int sx = x * 3;
int dx = x * 4;
unsigned char r = pRow[sx];
//.........这里部分代码省略.........
示例7: replaceBootImage
void* replaceBootImage(AbstractFile* imageWrapper, const unsigned int* key, const unsigned int* iv, AbstractFile* png, size_t *fileSize) {
AbstractFile* imageFile;
unsigned char header[8];
InfoIBootIM* info;
png_uint_32 i;
png_bytepp row_pointers;
uint8_t* imageBuffer;
void* buffer;
png->read(png, header, 8);
if(png_sig_cmp(header, 0, 8) != 0) {
XLOG(0, "error: not a valid png file\n");
return NULL;
}
png->seek(png, 0);
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, pngError, pngWarn);
if (!png_ptr) {
return NULL;
}
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 NULL;
}
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 NULL;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
XLOG(0, "error reading png\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
free(buffer);
return NULL;
}
png_set_read_fn(png_ptr, png, pngRead);
png_read_info(png_ptr, info_ptr);
if(info_ptr->bit_depth > 8) {
XLOG(0, "warning: bit depth per channel is greater than 8 (%d). Attempting to strip, but image quality will be degraded.\n", info_ptr->bit_depth);
}
if(info_ptr->color_type == PNG_COLOR_TYPE_GRAY || info_ptr->color_type == PNG_COLOR_TYPE_RGB) {
XLOG(0, "notice: attempting to add dummy transparency channel\n");
}
if(info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
XLOG(0, "notice: attempting to expand palette into full rgb\n");
}
png_set_expand(png_ptr);
png_set_strip_16(png_ptr);
png_set_bgr(png_ptr);
png_set_add_alpha(png_ptr, 0x0, PNG_FILLER_AFTER);
png_set_invert_alpha(png_ptr);
png_read_update_info(png_ptr, info_ptr);
if(info_ptr->width > 320 || info_ptr->height > 480) {
XLOG(0, "error: dimensions out of range, must be within 320x480, not %lux%lu\n", info_ptr->width, info_ptr->height);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return NULL;
}
if(info_ptr->bit_depth != 8) {
XLOG(0, "error: bit depth per channel must be 8 not %d!\n", info_ptr->bit_depth);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return NULL;
}
if(info_ptr->color_type != PNG_COLOR_TYPE_GRAY_ALPHA && info_ptr->color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
XLOG(0, "error: incorrect color type, must be greyscale with alpha, or rgb with alpha\n");
if(info_ptr->color_type == PNG_COLOR_TYPE_GRAY || info_ptr->color_type == PNG_COLOR_TYPE_RGB) {
XLOG(0, "It appears you're missing an alpha channel. Add transparency to your image\n");
}
if(info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
XLOG(0, "This PNG is saved with the palette color type rather than ARGB.\n");
}
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return NULL;
}
row_pointers = (png_bytepp) malloc(sizeof(png_bytep) * info_ptr->height);
imageBuffer = malloc(info_ptr->height * info_ptr->rowbytes);
for(i = 0; i < info_ptr->height; i++) {
row_pointers[i] = imageBuffer + (info_ptr->rowbytes * i);
}
//.........这里部分代码省略.........
示例8: loadImage
// 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)
#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MINOR > 3)
png_set_expand_gray_1_2_4_to_8(png_ptr);
#else
png_set_gray_1_2_4_to_8(png_ptr);
#endif
else
png_set_packing(png_ptr);
}
示例9: png_texture_load
GLuint png_texture_load(const char * file_name, int width, int height)
{
png_uint_32 temp_width, temp_height;
int bit_depth, color_type, i;
png_byte header[8];
GLuint texture;
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_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
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
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
// get info about png
png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type, NULL, NULL, NULL);
#if 0
if (width ) {
width = temp_width;
}
if (height ) {
height = temp_height;
}
#endif
// Update the png info struct.
png_read_update_info(png_ptr, info_ptr);
// Row size in bytes.
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
// glTexImage2d requires rows to be 4-byte aligned
rowbytes += 3 - ((rowbytes-1) % 4);
// Allocate the image_data as a big block, to be given to opengl
image_data = (png_byte*)malloc(rowbytes * temp_height * sizeof(png_byte)+15);
if (image_data == NULL)
{
fprintf(stderr, "error: could not allocate memory for PNG image data\n");
//.........这里部分代码省略.........
示例10: read_png_file
GLuint read_png_file(char* file_name)
{
unsigned char header[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", file_name);
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", file_name);
/* 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");
end_info = png_create_info_struct(png_ptr);
if (!end_info)
abort_("[read_png_file] png_create_end_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);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
// Row size in bytes.
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
// glTexImage2d requires rows to be 4-byte aligned
rowbytes += 3 - ((rowbytes-1) % 4);
// Allocate the image_data as a big block, to be given to opengl
image_data = (png_byte*)malloc(rowbytes * height * sizeof(png_byte)+15);
if (image_data == NULL)
abort_("[read_png_file] png_create_end_info_struct failed");
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during read_image");
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
if (bit_depth == 16)
rowbytes = width*8;
else
rowbytes = width*4;
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(rowbytes);
png_read_image(png_ptr, row_pointers);
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image_data );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// clean up
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
free(image_data);
free(row_pointers);
fclose(fp);
return Texture;
}
示例11: Assert
ImageNode *LoadPNGImage(const char *fileName)
{
static ImageNode *result;
static FILE *fd;
static unsigned char **rows;
static png_structp pngData;
static png_infop pngInfo;
static png_infop pngEndInfo;
unsigned char header[8];
unsigned long rowBytes;
int bitDepth, colorType;
unsigned int x, y;
png_uint_32 width;
png_uint_32 height;
Assert(fileName);
result = NULL;
fd = NULL;
rows = NULL;
pngData = NULL;
pngInfo = NULL;
pngEndInfo = NULL;
fd = fopen(fileName, "rb");
if(!fd) {
return NULL;
}
x = fread(header, 1, sizeof(header), fd);
if(x != sizeof(header) || png_sig_cmp(header, 0, sizeof(header))) {
fclose(fd);
return NULL;
}
pngData = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(JUNLIKELY(!pngData)) {
fclose(fd);
Warning(_("could not create read struct for PNG image: %s"), fileName);
return NULL;
}
if(JUNLIKELY(setjmp(png_jmpbuf(pngData)))) {
png_destroy_read_struct(&pngData, &pngInfo, &pngEndInfo);
if(fd) {
fclose(fd);
}
if(rows) {
ReleaseStack(rows);
}
DestroyImage(result);
Warning(_("error reading PNG image: %s"), fileName);
return NULL;
}
pngInfo = png_create_info_struct(pngData);
if(JUNLIKELY(!pngInfo)) {
png_destroy_read_struct(&pngData, NULL, NULL);
fclose(fd);
Warning(_("could not create info struct for PNG image: %s"), fileName);
return NULL;
}
pngEndInfo = png_create_info_struct(pngData);
if(JUNLIKELY(!pngEndInfo)) {
png_destroy_read_struct(&pngData, &pngInfo, NULL);
fclose(fd);
Warning("could not create end info struct for PNG image: %s", fileName);
return NULL;
}
png_init_io(pngData, fd);
png_set_sig_bytes(pngData, sizeof(header));
png_read_info(pngData, pngInfo);
png_get_IHDR(pngData, pngInfo, &width, &height,
&bitDepth, &colorType, NULL, NULL, NULL);
result = CreateImage(width, height, 0);
png_set_expand(pngData);
if(bitDepth == 16) {
png_set_strip_16(pngData);
} else if(bitDepth < 8) {
png_set_packing(pngData);
}
png_set_swap_alpha(pngData);
png_set_filler(pngData, 0xFF, PNG_FILLER_BEFORE);
if(colorType == PNG_COLOR_TYPE_GRAY
|| colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(pngData);
}
png_read_update_info(pngData, pngInfo);
//.........这里部分代码省略.........
示例12: read_png_file_low
static void read_png_file_low(const char* file_name, png_image_data_t *png_image_data)
{
png_structp png_ptr;
int number_of_passes;
char header[8]; // 8 is the maximum size that can be checked
int y;
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", file_name);
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", file_name);
/* 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");
png_image_data->info_ptr = png_create_info_struct(png_ptr);
if (!png_image_data->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); /* 8 == sig_read ? */
png_read_info(png_ptr, png_image_data->info_ptr);
/* set transformations */
/*
if ((png_image_data->info_ptr->color_type & PNG_COLOR_TYPE_RGB) == 0)
transforms |= PNG_TRANSFORM_BGR;
*/
if (png_get_valid(png_ptr, png_image_data->info_ptr, PNG_INFO_sBIT))
{
png_color_8p sig_bit;
png_get_sBIT(png_ptr, png_image_data->info_ptr, &sig_bit);
printf("sig_bit: %d\n", sig_bit);
png_set_shift(png_ptr, sig_bit);
}
/* TODO DFG: is this needed? */
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, png_image_data->info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during read_image");
#if 1
png_image_data->row_pointers = (png_bytep*) qemu_malloc(sizeof(png_bytep) * png_image_data->info_ptr->height);
for (y=0; y < png_image_data->info_ptr->height; y++)
png_image_data->row_pointers[y] = (png_byte*) qemu_malloc(png_image_data->info_ptr->rowbytes);
png_read_image(png_ptr, png_image_data->row_pointers);
#endif
/* DFG TODO: Cleanup */
/* png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); */
fclose(fp);
}
示例13: read_png_file
static void read_png_file(const char* file_name, png_image_data_t *png_image_data, int max_width, int max_height)
{
png_structp png_ptr;
png_infop info_ptr;
png_bytep *row_pointers;
char header[8]; // 8 is the maximum size that can be checked
int y;
char* dest;
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", file_name);
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", file_name);
/* 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");
if (max_width > 0 && max_height > 0)
png_set_user_limits(png_ptr, max_width, max_height);
info_ptr = png_create_info_struct(png_ptr);
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during init_io");
png_init_io(png_ptr, fp);
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during init_io");
png_set_sig_bytes(png_ptr, sizeof(header));
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during init_io");
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_ALPHA, png_voidp_NULL);
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[read_png_file] Error during init_io");
row_pointers = png_get_rows(png_ptr, info_ptr);
png_image_data->height = png_get_image_height(png_ptr, info_ptr);
png_image_data->width = png_get_image_width(png_ptr, info_ptr);
png_image_data->image4c = (void*)qemu_malloc(png_image_data->width * png_image_data->height * 4);
dest = (char*)png_image_data->image4c;
/* transform this from 3 channels to a (fake for now) 4 channels */
for (y=0; y < png_image_data->height; y++) {
char* src = row_pointers[y];
int x;
for (x = 0; x < png_image_data->width; x++) {
*dest = *src; dest++, src++;
*dest = *src; dest++, src++;
*dest = *src; dest++, src++;
*dest = 0; dest++; /* alpha channel ignored */
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
}
示例14: png_read_info
/* Read the information before the actual image data. This has been
* changed in v0.90 to allow reading a file that already has the magic
* bytes read from the stream. You can tell libpng how many bytes have
* been read from the beginning of the stream (up to the maximum of 8)
* via png_set_sig_bytes(), and we will only check the remaining bytes
* here. The application can then have access to the signature bytes we
* read if it is determined that this isn't a valid PNG file.
*/
void PNGAPI
png_read_info(png_structp png_ptr, png_infop info_ptr)
{
png_debug(1, "in png_read_info\n");
/* save jump buffer and error functions */
/* If we haven't checked all of the PNG signature bytes, do so now. */
if (png_ptr->sig_bytes < 8)
{
png_size_t num_checked = png_ptr->sig_bytes,
num_to_check = 8 - num_checked;
png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
png_ptr->sig_bytes = 8;
if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
{
if (num_checked < 4 &&
png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
png_error(png_ptr, "Not a PNG file");
else
png_error(png_ptr, "PNG file corrupted by ASCII conversion");
}
if (num_checked < 3)
png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
}
for(;;)
{
#ifdef PNG_USE_LOCAL_ARRAYS
PNG_IHDR;
PNG_IDAT;
PNG_IEND;
PNG_PLTE;
#if defined(PNG_READ_bKGD_SUPPORTED)
PNG_bKGD;
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
PNG_cHRM;
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
PNG_gAMA;
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
PNG_hIST;
#endif
#if defined(PNG_READ_iCCP_SUPPORTED)
PNG_iCCP;
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
PNG_iTXt;
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
PNG_oFFs;
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
PNG_pCAL;
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
PNG_pHYs;
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
PNG_sBIT;
#endif
#if defined(PNG_READ_sCAL_SUPPORTED)
PNG_sCAL;
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
PNG_sPLT;
#endif
#if defined(PNG_READ_sRGB_SUPPORTED)
PNG_sRGB;
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
PNG_tEXt;
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
PNG_tIME;
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
PNG_tRNS;
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
PNG_zTXt;
#endif
#endif /* PNG_GLOBAL_ARRAYS */
png_byte chunk_length[4];
png_uint_32 length;
png_read_data(png_ptr, chunk_length, 4);
length = png_get_uint_32(chunk_length);
png_reset_crc(png_ptr);
//.........这里部分代码省略.........
示例15: lload
static int
lload(lua_State *L) {
int top = lua_gettop(L);
FILE *fp;
struct png_source source;
if (top == 1) {
const char *filename = luaL_checkstring(L,1);
fp = fopen(filename, "rb");
if (fp == NULL) {
return luaL_error(L, strerror(errno));
}
unsigned char header[PNGSIGSIZE];
if (fread(header, 1, PNGSIGSIZE, fp) != PNGSIGSIZE) {
return luaL_error(L, "png invalid");
}
if (png_sig_cmp(header, 0, PNGSIGSIZE)) {
return luaL_error(L, "png sig invalid");
}
fseek(fp, 0, SEEK_SET);
} else if (top == 2) {
luaL_checktype(L,1,LUA_TLIGHTUSERDATA);
void *data = lua_touserdata(L,1);
size_t size = luaL_checkinteger(L,2);
if (size < PNGSIGSIZE) {
return luaL_error(L, "png invalid");
}
if (png_sig_cmp(data, 0, PNGSIGSIZE)) {
return luaL_error(L, "png sig invalid");
}
source.data = data;
source.size = size;
source.offset = 0;
} else {
return luaL_error(L, "invalid argument number");
}
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
int step;//, type;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
return 0;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
return 0;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return 0;
}
if (top == 1)
png_init_io(png_ptr, fp);
else
png_set_read_fn(png_ptr, (void *)&source, png_read_cb);
//png_set_sig_bytes(png_ptr, PNGSIGSIZE);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
bit_depth = 8;
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) != 0)
png_set_tRNS_to_alpha(png_ptr);
if (bit_depth == 16)
png_set_strip_16(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
png_read_update_info(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
// type = TEXTURE_DEPTH;
step = 1;
break;
case PNG_COLOR_TYPE_RGB:
//type = TEXTURE_RGB;
step = 3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
//.........这里部分代码省略.........