本文整理汇总了C++中png_set_sig_bytes函数的典型用法代码示例。如果您正苦于以下问题:C++ png_set_sig_bytes函数的具体用法?C++ png_set_sig_bytes怎么用?C++ png_set_sig_bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_set_sig_bytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LoadPNG
void LoadPNG( const char *name, byte **pic, int *width, int *height,
int *numLayers, int *numMips, int *bits, byte alphaByte )
{
int bit_depth;
int color_type;
png_uint_32 w;
png_uint_32 h;
unsigned int row;
// size_t rowbytes;
png_infop info;
png_structp png;
png_bytep *row_pointers;
byte *data;
byte *out;
// int size;
// load png
ri.FS_ReadFile( name, ( void ** ) &data );
if ( !data )
{
return;
}
//png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png = png_create_read_struct( PNG_LIBPNG_VER_STRING, ( png_voidp ) NULL, png_user_error_fn, png_user_warning_fn );
if ( !png )
{
ri.Printf( PRINT_WARNING, "LoadPNG: png_create_write_struct() failed for (%s)\n", name );
ri.FS_FreeFile( data );
return;
}
// allocate/initialize the memory for image information. REQUIRED
info = png_create_info_struct( png );
if ( !info )
{
ri.Printf( PRINT_WARNING, "LoadPNG: png_create_info_struct() failed for (%s)\n", name );
ri.FS_FreeFile( data );
png_destroy_read_struct( &png, ( png_infopp ) NULL, ( png_infopp ) NULL );
return;
}
/*
* Set error handling if you are using the setjmp/longjmp method (this is
* the common 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 ) ) )
{
// if we get here, we had a problem reading the file
ri.Printf( PRINT_WARNING, "LoadPNG: first exception handler called for (%s)\n", name );
ri.FS_FreeFile( data );
png_destroy_read_struct( &png, ( png_infopp ) & info, ( png_infopp ) NULL );
return;
}
//png_set_write_fn(png, buffer, png_write_data, png_flush_data);
png_set_read_fn( png, data, png_read_data );
png_set_sig_bytes( png, 0 );
// 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, info );
// get picture info
png_get_IHDR( png, info, ( png_uint_32 * ) &w, ( png_uint_32 * ) &h, &bit_depth, &color_type, NULL, NULL, NULL );
// tell libpng to strip 16 bit/color files down to 8 bits/color
png_set_strip_16( png );
// expand paletted images to RGB triplets
if ( color_type & PNG_COLOR_MASK_PALETTE )
{
png_set_expand( png );
}
// expand gray-scaled images to RGB triplets
if ( !( color_type & PNG_COLOR_MASK_COLOR ) )
{
png_set_gray_to_rgb( png );
}
// 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);
// 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, info, PNG_INFO_tRNS ) )
{
png_set_tRNS_to_alpha( png );
}
// if there is no alpha information, fill with alphaByte
if ( !( color_type & PNG_COLOR_MASK_ALPHA ) )
{
//.........这里部分代码省略.........
示例2: memcpy
bool ImageLoaderPNG::ReadData() {
const int kNumSigBytesToRead = 8;
uint8 pngSigBuf[kNumSigBytesToRead];
memcpy(pngSigBuf, m_RawData, kNumSigBytesToRead);
const int numSigNoMatch = png_sig_cmp(pngSigBuf, 0, kNumSigBytesToRead);
if(numSigNoMatch) {
ReportError("Incorrect PNG signature");
return false;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr) {
ReportError("Could not create read struct");
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
ReportError("Could not create info struct");
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
// Read from our buffer, not a file pointer...
png_set_read_fn(png_ptr, this, PNGStreamReader::ReadDataFromStream);
// Make sure to tell libpng how many bytes we've read...
png_set_sig_bytes(png_ptr, kNumSigBytesToRead);
png_read_info(png_ptr, info_ptr);
int bitDepth = 0;
int colorType = -1;
if( 1 != png_get_IHDR(png_ptr, info_ptr,
(png_uint_32 *)(&m_Width), (png_uint_32 *)(&m_Height),
&bitDepth, &colorType,
NULL, NULL, NULL)
) {
ReportError("Could not read PNG header");
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
if(bitDepth != 8) {
ReportError("Only 8-bit images currently supported.");
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
const int numPixels = m_Width * m_Height;
png_size_t bpr = png_get_rowbytes(png_ptr, info_ptr);
png_bytep rowData = new png_byte[bpr];
switch(colorType) {
default:
ReportError("PNG color type unsupported");
png_destroy_read_struct(&png_ptr, NULL, NULL);
delete [] rowData;
return false;
case PNG_COLOR_TYPE_PALETTE:
{
m_RedChannelPrecision = bitDepth;
m_RedData = new unsigned char[numPixels];
m_GreenChannelPrecision = bitDepth;
m_GreenData = new unsigned char[numPixels];
m_BlueChannelPrecision = bitDepth;
m_BlueData = new unsigned char[numPixels];
png_colorp palette;
int nPaletteEntries;
png_uint_32 ret = png_get_PLTE(png_ptr, info_ptr, &palette, &nPaletteEntries);
if(ret != PNG_INFO_PLTE) {
memset(m_BlueData, 0, numPixels);
memset(m_RedData, 0, numPixels);
memset(m_GreenData, 0, numPixels);
assert(!"Couldn't find PLTE chunk");
break;
}
for(uint32 i = 0; i < m_Height; i++) {
png_read_row(png_ptr, rowData, NULL);
unsigned int rowOffset = i * m_Width;
for(uint32 j = 0; j < m_Width; j++) {
assert(rowData[j] < nPaletteEntries);
const png_color &c = palette[::std::min<unsigned char>(rowData[j], nPaletteEntries - 1)];
m_RedData[rowOffset + j] = c.red;
m_GreenData[rowOffset + j] = c.green;
m_BlueData[rowOffset + j] = c.blue;
}
}
}
break;
case PNG_COLOR_TYPE_GRAY: {
m_RedChannelPrecision = bitDepth;
m_RedData = new unsigned char[numPixels];
//.........这里部分代码省略.........
示例3: BGD_DECLARE
/* This routine is based in part on the Chapter 13 demo code in "PNG: The
* Definitive Guide" (http://www.cdrom.com/pub/png/pngbook.html).
*/
BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
{
png_byte sig[8];
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height, rowbytes;
int bit_depth, color_type, interlace_type;
int num_palette, num_trans;
png_colorp palette;
png_color_16p trans_gray_rgb;
png_color_16p trans_color_rgb;
png_bytep trans;
png_bytep image_data = NULL;
png_bytepp row_pointers = NULL;
gdImagePtr im = NULL;
int i, j, *open = NULL;
volatile int transparent = -1;
volatile int palette_allocated = FALSE;
/* Make sure the signature can't match by dumb luck -- TBB */
/* GRR: isn't sizeof(infile) equal to the size of the pointer? */
memset (infile, 0, sizeof (infile));
/* first do a quick check that the file really is a PNG image; could
* have used slightly more general png_sig_cmp() function instead */
gdGetBuf (sig, 8, infile);
if (!png_check_sig (sig, 8))
return NULL; /* bad signature */
#ifndef PNG_SETJMP_NOT_SUPPORTED
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, &gdPngJmpbufStruct,
gdPngErrorHandler, NULL);
#else
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
#endif
if (png_ptr == NULL)
{
fprintf (stderr, "gd-png error: cannot allocate libpng main struct\n");
return NULL;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL)
{
fprintf (stderr, "gd-png error: cannot allocate libpng info struct\n");
png_destroy_read_struct (&png_ptr, NULL, NULL);
return NULL;
}
/* 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 non-callback function that calls a
* PNG-reading libpng function */
#ifndef PNG_SETJMP_NOT_SUPPORTED
if (setjmp (gdPngJmpbufStruct.jmpbuf))
{
fprintf (stderr, "gd-png error: setjmp returns error condition\n");
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
#endif
png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
png_set_read_fn (png_ptr, (void *) infile, gdPngReadData);
png_read_info (png_ptr, info_ptr); /* read all PNG info up to image data */
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
if ((color_type == PNG_COLOR_TYPE_RGB) ||
(color_type == PNG_COLOR_TYPE_RGB_ALPHA))
{
im = gdImageCreateTrueColor ((int) width, (int) height);
}
else
{
im = gdImageCreate ((int) width, (int) height);
}
if (im == NULL)
{
fprintf (stderr, "gd-png error: cannot allocate gdImage struct\n");
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
gdFree (image_data);
gdFree (row_pointers);
return NULL;
}
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
else if (bit_depth < 8)
png_set_packing (png_ptr); /* expand to 1 byte per pixel */
switch (color_type)
{
case PNG_COLOR_TYPE_PALETTE:
png_get_PLTE (png_ptr, info_ptr, &palette, &num_palette);
//.........这里部分代码省略.........
示例4: 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];
if (gr_get_rotation() % 180 == 0)
snprintf(resPath, sizeof(resPath)-1, "/res/portrait/%s.png", name);
else
snprintf(resPath, sizeof(resPath)-1, "/res/landscape/%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)))) {
result = -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 y;
if (channels < 4) {
for (y = 0; y < (int) height; ++y) {
unsigned char* pRow = pData + y * stride;
png_read_row(png_ptr, pRow, NULL);
int x;
for(x = width - 1; x >= 0; x--) {
//.........这里部分代码省略.........
示例5: png_create_read_struct
bool ImageLoader::ReadPNG(ImageLoaderParams &ImgInfo)
{
// Set up libpng reading.
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, PNGErrorFn, PNGWarnFn);
if (!png_ptr)
return false;
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 false;
}
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 false;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return false;
}
// We read using a FILE*; this could be changed.
File::IOFile file;
if (!file.Open(ImgInfo.Path, "rb"))
{
return false;
}
png_init_io(png_ptr, file.GetHandle());
png_set_sig_bytes(png_ptr, 0);
// Process PNG header, etc.
png_read_info(png_ptr, info_ptr);
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, compression_type, filter_method;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method);
// Force RGB (8 or 16-bit).
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
// Force 8-bit RGB.
if (bit_depth == 16)
png_set_strip_16(png_ptr);
// Force alpha channel (combined with the above, 8-bit RGBA).
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
else if ((color_type & PNG_COLOR_MASK_ALPHA) == 0)
png_set_add_alpha(png_ptr, 0xFF, PNG_FILLER_AFTER);
png_read_update_info(png_ptr, info_ptr);
ImgInfo.Width = width;
ImgInfo.Height = height;
ImgInfo.data_size = width * height * 4;
ImgInfo.dst = ImgInfo.request_buffer_delegate(ImgInfo.data_size, false);
std::vector<u8*> row_pointers(height);
u8* row_pointer = ImgInfo.dst;
for (unsigned i = 0; i < height; ++i)
{
row_pointers[i] = row_pointer;
row_pointer += width * 4;
}
png_read_image(png_ptr, row_pointers.data());
png_read_end(png_ptr, end_info);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return true;
}
示例6: fopen
RawImage* ImageFactory::loadPNG(const char *filename)
{
int outWidth, outHeight;
bool outHasAlpha;
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
int color_type, interlace_type;
FILE* fp = fopen(filename, "rb");
if (fp == NULL)
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,
NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return NULL;
}
/* 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, NULL, NULL);
return NULL;
}
/* 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, NULL);
fclose(fp);
/* If we get here, we had a
* problem reading the file */
return NULL;
}
/* Set up the output control if
* you are using standard C streams */
png_init_io(png_ptr, fp);
/* If we have already
* read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
/*
* 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_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_uint_32 width, height;
int bit_depth;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
outWidth = width;
outHeight = height;
switch (color_type) {
case PNG_COLOR_TYPE_RGBA:
outHasAlpha = true;
break;
case PNG_COLOR_TYPE_RGB:
outHasAlpha = false;
//.........这里部分代码省略.........
示例7: ReadPNGInfo
BMGError ReadPNGInfo( const char *filename,
struct BMGImageStruct * volatile img )
{
jmp_buf err_jmp;
int error;
FILE * volatile file = NULL;
int BitDepth;
int ColorType;
int InterlaceType;
unsigned char signature[8];
png_structp volatile png_ptr = NULL;
png_infop volatile info_ptr = NULL;
png_infop volatile end_info = NULL;
png_uint_32 Width, Height;
/* error handler */
error = setjmp( err_jmp );
if (error != 0)
{
if (end_info != NULL)
png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
else if (info_ptr != NULL)
png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, NULL);
else if (png_ptr != NULL)
png_destroy_read_struct((png_structp *) &png_ptr, NULL, NULL);
if (img)
FreeBMGImage(img);
if (file)
fclose(file);
SetLastBMGError((BMGError) error);
return (BMGError) error;
}
if ( img == NULL )
longjmp ( err_jmp, (int)errInvalidBMGImage );
file = fopen( filename, "rb" );
if ( !file || fread( signature, 1, 8, file ) != 8)
longjmp ( err_jmp, (int)errFileOpen );
/* check the signature */
if ( png_sig_cmp( signature, 0, 8 ) != 0 )
longjmp( err_jmp, (int)errUnsupportedFileFormat );
/* create a pointer to the png read structure */
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
if ( !png_ptr )
longjmp( err_jmp, (int)errMemoryAllocation );
/* create a pointer to the png info structure */
info_ptr = png_create_info_struct( png_ptr );
if ( !info_ptr )
longjmp( err_jmp, (int)errMemoryAllocation );
/* create a pointer to the png end-info structure */
end_info = png_create_info_struct(png_ptr);
if (!end_info)
longjmp( err_jmp, (int)errMemoryAllocation );
/* bamboozle the PNG longjmp buffer */
/*generic PNG error handler*/
/* error will always == 1 which == errLib */
// error = png_setjmp(png_ptr);
error = setjmp( png_jmpbuf( png_ptr ) );
if ( error > 0 )
longjmp( err_jmp, error );
/* set function pointers in the PNG library, for read callbacks */
png_set_read_fn(png_ptr, (png_voidp) file, user_read_data);
/*let the read functions know that we have already read the 1st 8 bytes */
png_set_sig_bytes( png_ptr, 8 );
/* read all PNG data up to the image data */
png_read_info( png_ptr, info_ptr );
/* extract the data we need to form the HBITMAP from the PNG header */
png_get_IHDR( png_ptr, info_ptr, &Width, &Height, &BitDepth, &ColorType,
&InterlaceType, NULL, NULL);
img->width = (unsigned int) Width;
img->height = (unsigned int) Height;
img->bits_per_pixel = (unsigned char)32;
img->scan_width = Width * 4;
img->palette_size = (unsigned short)0;
img->bytes_per_palette_entry = 4U;
img->bits = NULL;
png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
fclose( file );
return BMG_OK;
}
示例8: read_png_file
int read_png_file(uint8_t *data[ 4 ], int linesize[ 4 ],
int *width, int *height, enum AVPixelFormat *pix_fmt,
const char *filename, void *log_ctx)
{
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep *row_pointers;
png_bytep image_data;
png_byte color_type;
png_byte bit_depth;
char header[ 8 ];
int rowbytes;
int res = 0;
FILE *fp = fopen(filename,"rb");
if (! fp) return -1;
while (true)
{
fread(header,1,8,fp);
if (png_sig_cmp(header,0,8))
{
res = -1;
break;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (! png_ptr)
{
res = -1;
break;
}
info_ptr = png_create_info_struct(png_ptr);
if (! info_ptr)
{
res = -1;
break;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
res = -1;
break;
}
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);
*pix_fmt = AV_PIX_FMT_RGBA;
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);
if (setjmp(png_jmpbuf(png_ptr)))
{
res = -1;
break;
}
rowbytes = png_get_rowbytes(png_ptr,info_ptr);
image_data = (png_bytep) malloc(rowbytes * *height);
row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * *height);
int y;
for (y = 0; y < *height; y++)
{
row_pointers[ y ] = image_data + (y * rowbytes);
}
png_read_image(png_ptr,row_pointers);
data[ 0 ] = image_data;
data[ 1 ] = 0;
data[ 2 ] = 0;
data[ 3 ] = 0;
linesize[ 0 ] = rowbytes;
linesize[ 1 ] = 0;
linesize[ 2 ] = 0;
linesize[ 3 ] = 0;
break;
}
fclose(fp);
return res;
//.........这里部分代码省略.........
示例9: open_png
image_t *load_png(char *png) {
int width = 0;
int height = 0;
FILE *f = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_infop end_info = NULL;
png_uint_32 rowbytes = 0;
image_t *image = NULL;
f = open_png(png);
if (f == NULL)
goto err;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
(png_voidp) NULL, NULL, NULL);
if (!png_ptr)
goto err;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
goto err;
end_info = png_create_info_struct(png_ptr);
if (!end_info)
goto err;
if (setjmp(png_jmpbuf(png_ptr))) {
printf("[read_png_file] Error during read_image");
goto err;
}
png_init_io(png_ptr, f);
png_set_sig_bytes(png_ptr, HEADER_SIZE);
png_read_info(png_ptr, info_ptr);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGBA) {
printf("Error: PNG format is not RGBA\n");
goto err;
}
if (setjmp(png_jmpbuf(png_ptr))) {
printf("[read_png_file] Error during read_image");
goto err;
}
image = make_image(width, height, rowbytes);
if (image == NULL)
goto err;
png_read_image(png_ptr, image->rows);
png_read_end(png_ptr, end_info);
done: if (f != NULL)
fclose(f);
if (png_ptr != NULL)
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return image;
err: free_image(image);
image = NULL;
goto done;
}
示例10: FinfoNew
/* create a new finfo record, open FITS file */
static Finfo FinfoNew(char *fname)
{
int len;
char *e=NULL;
char *f=NULL;
char *s=NULL;
#if FITS2PNG
int i;
unsigned char header[8];
/* use volatile to make gcc [-Wclobbered] happy (because of setjmp below) */
volatile Finfo finfo;
#else
Finfo finfo;
#endif
/* sanity check */
if( !fname ) return NULL;
/* return existing finfo, if possible */
if( (finfo=FinfoLookup(fname)) ) return finfo;
/* allocate record */
if( !(finfo = (Finfo)xcalloc(sizeof(FinfoRec), 1)) ){
fprintf(stderr, "ERROR: can't allocate rec for image\n");
return NULL;
}
/* save file name */
finfo->fname = xstrdup(fname);
/* check for file type */
if( (s = strrchr(fname, '.')) && !strcasecmp(s, ".png") ){
/* its a PNG */
finfo->ftype = FTYPE_PNG;
} else {
/* assume FITS type */
finfo->ftype = FTYPE_FITS;
}
/* open file */
switch(finfo->ftype){
case FTYPE_PNG:
#if FITS2PNG
/* code taken from "PNG: The Definitive Guide" by Greg Roelofs,
Chapter 13 "Reading PNG Images" */
/* set data path */
datapath = getenv("JS9_DATAPATH");
/* look for path of the PNG file */
s = Find(fname, "r", NULL, datapath);
if( s && *s ){
if( !(finfo->fp = fopen(s, "rb")) ){
fprintf(stderr, "ERROR: can't open PNG file '%s'\n", fname);
goto error;
}
fread(header, 1, 8, finfo->fp);
if( png_sig_cmp(header, 0, 8) ){
fprintf(stderr, "ERROR: not recognized as a PNG file '%s'\n", fname);
goto error;
}
/* initialize stuff */
finfo->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if( !finfo->png_ptr ){
fprintf(stderr, "ERROR: png_create_read_struct failed '%s'\n", fname);
goto error;
}
finfo->info_ptr = png_create_info_struct(finfo->png_ptr);
if( !finfo->info_ptr ){
fprintf(stderr, "ERROR: png_create_info_struct failed '%s'\n", fname);
goto error;
}
if( setjmp(png_jmpbuf(finfo->png_ptr)) ){
fprintf(stderr, "ERROR: during png init_io '%s'\n", fname);
goto error;
}
png_init_io(finfo->png_ptr, finfo->fp);
png_set_sig_bytes(finfo->png_ptr, 8);
png_read_info(finfo->png_ptr, finfo->info_ptr);
/* get the text chunks that come before the image */
if( png_get_text(finfo->png_ptr, finfo->info_ptr,
&(finfo->text_ptr), &(finfo->num_text)) > 0 ){
/* process all known PNG keywords */
for(i=0; i<finfo->num_text; i++){
if( !strcmp(finfo->text_ptr[i].key, FITSFILE) ){
finfo->fitsfile = xstrdup(finfo->text_ptr[i].text);
/* remove the extension that was used to generate png */
s = strchr(finfo->fitsfile, '[');
if( s ){
*s = '\0';
}
}
}
}
} else {
fprintf(stderr, "ERROR: can't find PNG file '%s' [data path: %s]\n",
fname, datapath?datapath:"none");
goto error;
}
#else
fprintf(stderr,
"ERROR: for fits2png support, build JS9 using --with-png\n");
goto error;
#endif
//.........这里部分代码省略.........
示例11: png_create_read_struct
bool CGLCG::loadPngImage(const TCHAR *name, int &outWidth, int &outHeight, bool &outHasAlpha, GLubyte **outData) {
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
int color_type, interlace_type;
FILE *fp;
if ((fp = _tfopen(name, TEXT("rb"))) == NULL)
return false;
/* 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) {
fclose(fp);
return false;
}
/* 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 false;
}
/* 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 false;
}
/* Set up the output control if
* you are using standard C streams */
png_init_io(png_ptr, fp);
/* If we have already
* read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
/*
* 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_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, (png_voidp)NULL);
outWidth = png_get_image_width(png_ptr, info_ptr);
outHeight = png_get_image_height(png_ptr, info_ptr);
switch (png_get_color_type(png_ptr, info_ptr)) {
case PNG_COLOR_TYPE_RGBA:
outHasAlpha = true;
break;
case PNG_COLOR_TYPE_RGB:
outHasAlpha = false;
break;
default:
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return false;
}
//.........这里部分代码省略.........
示例12: fread
void Texture::InitWithFile(string& filepath) {
FILE* fd = FileIO::OpenFileDescriptor(filepath);
png_byte header[8];
fread(header, 1, 8, fd);
int is_png = !png_sig_cmp(header, 0, 8);
if (!is_png) {
printf("ERROR: %s is not a valid png file\n", filepath.c_str());
fclose(fd);
return;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(fd);
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);
fclose(fd);
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);
fclose(fd);
return;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fd);
return;
}
png_init_io(png_ptr, fd);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
GLint bit_depth, color_type;
png_get_IHDR(png_ptr, info_ptr, &m_width, &m_height, &bit_depth, &color_type, NULL, NULL, NULL);
png_read_update_info(png_ptr, info_ptr);
int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
png_byte *image_data = new png_byte[rowbytes * m_height];
if (!image_data) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fd);
return;
}
png_bytep *row_pointers = new png_bytep[m_height];
if (!row_pointers) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
delete[] image_data;
fclose(fd);
return;
}
for (GLuint i = 0; i < m_height; ++i) {
row_pointers[i] = image_data + i * rowbytes;
}
png_read_image(png_ptr, row_pointers);
glGenTextures(1, &m_glTextureID);
m_dataType = GL_UNSIGNED_BYTE;
m_pixelFormat = GL_RGBA;
m_targetType = GL_TEXTURE_2D;
glBindTexture(m_targetType, m_glTextureID);
glTexImage2D(m_targetType,0, m_pixelFormat, m_width, m_height, 0, m_pixelFormat, m_dataType, (GLvoid*) image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
delete[] image_data;
delete[] row_pointers;
fclose(fd);
}
示例13: fopen
GLuint TextureManager::addFromPNG(const std::string& id, const std::string& texturePath)
{
FILE *fp = fopen(texturePath.c_str(), "rb");
if(!fp)
return GL_INVALID_VALUE;
png_byte header[8];
fread(header, 1, 8, fp);
if(png_sig_cmp(header, 0, 8))
{
fclose(fp);
return GL_INVALID_VALUE;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr)
return GL_INVALID_VALUE;
png_infop info_ptr = png_create_info_struct(png_ptr);
png_infop end_info = png_create_info_struct(png_ptr);
if(!info_ptr || !end_info || setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return GL_INVALID_VALUE;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
int bit_depth, color_type;
png_uint_32 twidth, theight;
png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, NULL, NULL, NULL);
int width = twidth;
int height = theight;
png_read_update_info(png_ptr, info_ptr);
int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
png_byte *image_data = new png_byte[rowbytes * height];
png_bytepp row_pointers = new png_bytep[height];
if(!image_data || !row_pointers) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
delete[] image_data;
fclose(fp);
return GL_INVALID_VALUE;
}
for(int i = 0; i < height; ++i)
row_pointers[height - 1 - i] = image_data + i * rowbytes;
png_read_image(png_ptr, row_pointers);
bool hasAlpha;
/* switch (info_ptr->color_type) {
case PNG_COLOR_TYPE_RGBA:
hasAlpha = true;
break;
case PNG_COLOR_TYPE_RGB:
hasAlpha = false;
break;
default:
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return GL_INVALID_VALUE;
}*/
glGenTextures(1, &textures[id]);
glBindTexture(GL_TEXTURE_2D, textures[id]);
glTexImage2D(GL_TEXTURE_2D, 0, hasAlpha ? GL_RGBA : GL_RGB, width, height, 0, hasAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete[] image_data;
delete[] row_pointers;
fclose(fp);
return true;
}
示例14: load_inp_png_file
short load_inp_png_file(ImageData& img, const std::string& fname_inp, ProgramOptions& opts)
{
FILE* pngfile = fopen(fname_inp.c_str(),"rb");
if (pngfile == NULL) {
perror(fname_inp.c_str());
return ERR_CANT_OPEN;
}
png_byte header[8];
if (fread(header,8,1,pngfile) != 1) {
perror(fname_inp.c_str());
fclose(pngfile);
return ERR_FILE_READ;
}
if (png_sig_cmp(header,0,8)) {
LogErr("%s: Not a PNG file",fname_inp.c_str());
fclose(pngfile);
return ERR_BAD_FILE;
}
img.png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!img.png_ptr)
{
LogErr("%s: png_create_read_struct error",fname_inp.c_str());
fclose(pngfile);
return ERR_BAD_FILE;
}
img.info_ptr=png_create_info_struct(img.png_ptr);
if (!img.info_ptr)
{
png_destroy_read_struct(&img.png_ptr, (png_infopp)NULL, (png_infopp)NULL);
LogErr("%s: png_create_info_struct error",fname_inp.c_str());
fclose(pngfile);
return ERR_BAD_FILE;
}
img.end_info=png_create_info_struct(img.png_ptr);
if (!img.end_info)
{
png_destroy_read_struct(&img.png_ptr, &img.info_ptr, (png_infopp)NULL);
LogErr("%s: png_create_info_struct error",fname_inp.c_str());
fclose(pngfile);
return ERR_BAD_FILE;
}
if (setjmp(png_jmpbuf(img.png_ptr)))
{
png_destroy_read_struct(&img.png_ptr, &img.info_ptr, &img.end_info);
LogErr("%s: PNG error",fname_inp.c_str());
fclose(pngfile);
exit(1);
}
png_init_io(img.png_ptr, pngfile);
png_set_sig_bytes(img.png_ptr,8);
int trafo=PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16|PNG_TRANSFORM_EXPAND;
png_read_png(img.png_ptr, img.info_ptr, trafo , NULL);
int bit_depth, interlace_type, compression_type, filter_method;
png_get_IHDR(img.png_ptr, img.info_ptr, &img.width, &img.height, &bit_depth, &img.color_type,
&interlace_type, &compression_type, &filter_method);
if ((img.color_type & PNG_COLOR_MASK_COLOR)==0)
{
LogErr("%s: Grayscale image not supported",fname_inp.c_str());
fclose(pngfile);
return ERR_BAD_FILE;
}
fclose(pngfile);
if (img.color_type==PNG_COLOR_TYPE_PALETTE)
{
LogErr("Invalid format. This shouldn't happen. PNG_TRANSFORM_EXPAND transforms image to RGB.");
return ERR_BAD_FILE;
}
if (img.color_type & PNG_COLOR_MASK_ALPHA) {
img.col_bits = 32;
} else {
img.col_bits = 24;
}
return ERR_OK;
}
示例15: fopen_unsafe
void *png_read_file(const char *name, png_uint_32 *_w, png_uint_32 *_h,
check_w_h_constraint_t constraint, rgba_surface_allocator_t allocator)
{
png_uint_32 i, w, h, stride;
png_structp png_ptr = NULL;
png_bytep *row_ptrs = NULL;
png_infop info_ptr = NULL;
void *pixels, *s = NULL;
png_byte header[8];
int type, bpp;
FILE *f;
f = fopen_unsafe(name, "rb");
if(!f)
goto exit_out;
if(fread(header, 1, 8, f) < 8)
goto exit_close;
if(png_sig_cmp(header, 0, 8))
goto exit_close;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr)
goto exit_close;
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
goto exit_free_close;
if(setjmp(png_ptr->jmpbuf))
goto exit_free_close;
png_init_io(png_ptr, f);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bpp, &type, NULL, NULL, NULL);
if(!constraint(w, h))
{
warn("Requested image '%s' failed dimension checks.\n", name);
goto exit_free_close;
}
if(type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
else if(type == PNG_COLOR_TYPE_GRAY_ALPHA || !(type & PNG_COLOR_MASK_COLOR))
png_set_gray_to_rgb(png_ptr);
else if(!(type & PNG_COLOR_MASK_COLOR) && bpp < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if(bpp == 16)
png_set_strip_16(png_ptr);
else if(bpp < 8)
png_set_packing(png_ptr);
if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
else if(!(type & PNG_COLOR_MASK_ALPHA))
png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
// FIXME: Are these necessary?
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bpp, &type, NULL, NULL, NULL);
row_ptrs = cmalloc(sizeof(png_bytep) * h);
if(!row_ptrs)
goto exit_free_close;
s = allocator(w, h, &stride, &pixels);
if(!s)
goto exit_free_close;
for(i = 0; i < h; i++)
row_ptrs[i] = (png_bytep)(unsigned char *)pixels + i * stride;
png_read_image(png_ptr, row_ptrs);
if(_w)
*_w = w;
if(_h)
*_h = h;
exit_free_close:
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
free(row_ptrs);
exit_close:
fclose(f);
exit_out:
return s;
}