本文整理汇总了C++中png_read_image函数的典型用法代码示例。如果您正苦于以下问题:C++ png_read_image函数的具体用法?C++ png_read_image怎么用?C++ png_read_image使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_read_image函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ReadFilePNG
//.........这里部分代码省略.........
// get rid of lower-order byte in 16-bit images
// TODO: could allow this and read in IntImage in this case...
if (bits == 16)
png_set_strip_16(png_ptr);
// change palette color into RGB
if (colorType == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
// want at least 8 bits
if (bits < 8)
png_set_expand(png_ptr);
// if there is a transparent palette entry, create alpha channel
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
// make gray images with alpha channel into RGBA -- TODO: or just ignore alpha?
if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
// colorType == PNG_COLOR_TYPE_GRAY // but leave gray images alone
png_set_gray_to_rgb(png_ptr);
// set the background color to draw transparent and alpha images over.
// only needed for gray images with alpha
if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA ||
colorType == PNG_COLOR_TYPE_GRAY) {
png_color_16 *pBackground;
if (png_get_bKGD(png_ptr, info_ptr, &pBackground))
png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
}
// if required set gamma conversion
// this seems to cause problems, so let's just leave gamma alone.
//double gamma;
//if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
// //fprintf(stderr, "\n reading gamma %lf\n", gamma);
//png_set_gamma(png_ptr, 1.0, gamma);
//}
// we need colors in BGR order, not RGB
png_set_bgr(png_ptr);
// always convert 3-band to 4-band images (add alpha):
if (colorType == PNG_COLOR_TYPE_RGB)
png_set_add_alpha(png_ptr, 255, PNG_FILLER_AFTER);
// after the transformations have been registered update info_ptr data
png_read_update_info(png_ptr, info_ptr);
// get again width, height and the new bit-depth and color-type
png_get_IHDR(png_ptr, info_ptr,
&pwidth, &pheight,
//(png_uint_32 *)&width, (png_uint_32 *)&height,
&bits, &colorType, NULL, NULL, NULL);
width = pwidth;
height = pheight;
nBands = (int)png_get_channels(png_ptr, info_ptr);
if (DEBUG_ImageIOpng)
fprintf(stderr, " -> w=%d, h=%d, %2d bits, %s, nB=%d\n",
width, height, bits,
colorType == PNG_COLOR_TYPE_GRAY ? "gray" :
colorType == PNG_COLOR_TYPE_PALETTE ? "plt " :
colorType == PNG_COLOR_TYPE_RGB ? "rgb " :
colorType == PNG_COLOR_TYPE_RGB_ALPHA ? "rgba" :
colorType == PNG_COLOR_TYPE_GRAY_ALPHA ? "gr-a" : "??? ",
nBands);
if (! (nBands==1 || nBands==3 || nBands==4)) {
fclose(stream);
throw CError("ReadFilePNG: Can't handle nBands=%d", nBands);
}
// Set the image shape
CShape sh(width, height, nBands);
// Allocate the image if necessary
img.ReAllocate(sh);
// allocate a vector of row pointers
std::vector<uchar *> rowPtrs;
rowPtrs.resize(height);
for (int y = 0; y<height; y++)
rowPtrs[y] = &img.Pixel(0, y, 0);
// read the whole image
png_read_image(png_ptr, &rowPtrs[0]);
// read the additional chunks in the PNG file (not really needed)
png_read_end(png_ptr, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(stream);
}
示例2: read_png
static void read_png(unsigned char** block, unsigned* width, unsigned* height, FILE* file, const int base_img_size)
{
png_structp png_ptr;
png_infop info_ptr;
png_bytep* row_pointers;
unsigned row, x, y;
int rowbytes;
char* dst;
//png_uint_32 is 64 bit on some architectures!
png_uint_32 widthpu32,heightpu32;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr == NULL) {
printf("read_png: Could not create read struct.\n");
exit(1);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
printf("read_png: Could not create info struct.\n");
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
exit(1);
}
if (setjmp(png_ptr->jmpbuf)) {
printf("read_png: fatal error.\n");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_info**)0);
/* free pointers before returning, if necessary */
free(png_ptr);
free(info_ptr);
exit(1);
}
/* Set up the input control if you are using standard C streams */
png_init_io(png_ptr, file);
/* 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,
&widthpu32, &heightpu32, &bit_depth, &color_type,
&interlace_type, NULL, NULL
);
*width = widthpu32;
*height = heightpu32;
if (*height % base_img_size != 0 || *width % base_img_size != 0) {
printf("read_png: Invalid image size.\n");
exit(1);
}
// printf("read_png: width=%d, height=%d, bit_depth=%d\n", width, height, bit_depth);
// printf("read_png: color_type=%d, interlace_type=%d\n", color_type, interlace_type);
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
png_set_strip_16(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);
/* Expand paletted colors into true RGB triplets */
png_set_expand(png_ptr);
png_start_read_image(png_ptr);
/* The easiest way to read the image: */
rowbytes = png_get_rowbytes(png_ptr, info_ptr) * 3;
row_pointers = malloc(*height * sizeof(*row_pointers));
row_pointers[0] = malloc(rowbytes * *height * 2);
for (row = 1; row < *height; row++) {
row_pointers[row] = row_pointers[row - 1] + rowbytes * 2;
}
/* Read the entire image in one go */
png_read_image(png_ptr, row_pointers);
// we use fixed height here because block is of limited, fixed size
// not fixed any more
*block = realloc(*block, *height * *width * 6);
// *block = malloc(*height * *width * 6);
dst = *block;
for (y = 0; y < *height; y++) {
for (x = 0; x < *width * 3; x++) {
*dst++ = row_pointers[y][x];
//.........这里部分代码省略.........
示例3: LoadPNG
//.........这里部分代码省略.........
/*
* 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 ) nullptr );
return;
}
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, nullptr, nullptr, nullptr );
// 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
// 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 ) )
{
png_set_filler( png, alphaByte, PNG_FILLER_AFTER );
}
// expand pictures with less than 8bpp to 8bpp
if ( bit_depth < 8 )
{
png_set_packing( png );
}
png_set_interlace_handling( png );
// update structure with the above settings
png_read_update_info( png, info );
// allocate the memory to hold the image
*width = w;
*height = h;
*pic = out = ( byte * ) ri.Z_Malloc( w * h * 4 );
row_pointers = ( png_bytep * ) ri.Hunk_AllocateTempMemory( sizeof( png_bytep ) * h );
// set a new exception handler
if ( setjmp( png_jmpbuf( png ) ) )
{
ri.Printf( PRINT_WARNING, "LoadPNG: second exception handler called for (%s)\n", name );
ri.Hunk_FreeTempMemory( row_pointers );
ri.FS_FreeFile( data );
png_destroy_read_struct( &png, ( png_infopp ) & info, ( png_infopp ) nullptr );
return;
}
for ( row = 0; row < h; row++ )
{
row_pointers[ row ] = ( png_bytep )( out + ( row * 4 * w ) );
}
// read image data
png_read_image( png, row_pointers );
// read rest of file, and get additional chunks in info
png_read_end( png, info );
// clean up after the read, and free any memory allocated
png_destroy_read_struct( &png, &info, ( png_infopp ) nullptr );
ri.Hunk_FreeTempMemory( row_pointers );
ri.FS_FreeFile( data );
}
示例4: WXUNUSED
bool
wxPNGHandler::LoadFile(wxImage *image,
wxInputStream& stream,
bool verbose,
int WXUNUSED(index))
{
// VZ: as this function uses setjmp() the only fool-proof error handling
// method is to use goto (setjmp is not really C++ dtors friendly...)
unsigned char **lines = NULL;
png_infop info_ptr = (png_infop) NULL;
wxPNGInfoStruct wxinfo;
png_uint_32 i, width, height = 0;
int bit_depth, color_type, interlace_type;
wxinfo.verbose = verbose;
wxinfo.stream.in = &stream;
image->Destroy();
png_structp png_ptr = png_create_read_struct
(
PNG_LIBPNG_VER_STRING,
NULL,
wx_PNG_error,
wx_PNG_warning
);
if (!png_ptr)
goto error;
// NB: please see the comment near wxPNGInfoStruct declaration for
// explanation why this line is mandatory
png_set_read_fn( png_ptr, &wxinfo, wx_PNG_stream_reader);
info_ptr = png_create_info_struct( png_ptr );
if (!info_ptr)
goto error;
if (setjmp(wxinfo.jmpbuf))
goto error;
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_expand( png_ptr );
// Fix for Bug [ 439207 ] Monochrome PNG images come up black
if (bit_depth < 8)
png_set_expand( png_ptr );
png_set_strip_16( png_ptr );
png_set_packing( png_ptr );
if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand( png_ptr );
png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
image->Create((int)width, (int)height, (bool) false /* no need to init pixels */);
if (!image->IsOk())
goto error;
// initialize all line pointers to NULL to ensure that they can be safely
// free()d if an error occurs before all of them could be allocated
lines = (unsigned char **)calloc(height, sizeof(unsigned char *));
if ( !lines )
goto error;
for (i = 0; i < height; i++)
{
if ((lines[i] = (unsigned char *)malloc( (size_t)(width * 4))) == NULL)
goto error;
}
png_read_image( png_ptr, lines );
png_read_end( png_ptr, info_ptr );
#if wxUSE_PALETTE
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
png_colorp palette = NULL;
int numPalette = 0;
(void) png_get_PLTE(png_ptr, info_ptr, &palette, &numPalette);
unsigned char* r = new unsigned char[numPalette];
unsigned char* g = new unsigned char[numPalette];
unsigned char* b = new unsigned char[numPalette];
for (int j = 0; j < numPalette; j++)
{
r[j] = palette[j].red;
g[j] = palette[j].green;
b[j] = palette[j].blue;
}
image->SetPalette(wxPalette(numPalette, r, g, b));
delete[] r;
delete[] g;
//.........这里部分代码省略.........
示例5: png_read_png
//.........这里部分代码省略.........
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)
png_set_swap(png_ptr);
#endif
/* We don't handle adding filler bytes */
/* Optional call to gamma correct and add the background to the palette
* and update info structure. REQUIRED if you are expecting libpng to
* update the palette for you (i.e., you selected such a transform above).
*/
png_read_update_info(png_ptr, info_ptr);
/* -------------- image transformations end here ------------------- */
#ifdef PNG_FREE_ME_SUPPORTED
png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
#endif
if(info_ptr->row_pointers == NULL)
{
info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
info_ptr->height * png_sizeof(png_bytep));
#ifdef PNG_FREE_ME_SUPPORTED
info_ptr->free_me |= PNG_FREE_ROWS;
#endif
for (row = 0; row < (int)info_ptr->height; row++)
{
info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
png_get_rowbytes(png_ptr, info_ptr));
}
}
png_read_image(png_ptr, info_ptr->row_pointers);
info_ptr->valid |= PNG_INFO_IDAT;
/* read rest of file, and get additional chunks in info_ptr - REQUIRED */
png_read_end(png_ptr, info_ptr);
if(transforms == 0 || params == NULL)
/* quiet compiler warnings */ return;
}
示例6: PngReadFileToPixmap
//.........这里部分代码省略.........
png_row_bytes = png_width * 3;
png_channels = 3;
}
rwidth = png_width;
rheight = png_height;
components = png_channels;
/* possible integer overflow? */
assert ((int) png_row_bytes > 0);
assert ((int) png_height > 0);
assert (((int)png_row_bytes) * ((int)png_height) * sizeof(png_byte) > 0);
buf = rxvt_malloc(png_row_bytes * png_height * sizeof(png_byte));
if (buf == NULL){
rxvt_msg (DBG_ERROR, DBG_IMAGES, "png read error: out of memory..\n");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(ifile);
return -1;
}
/* possible integer overflow? */
assert ((int) png_height > 0);
assert (sizeof(png_bytep) * ((int)png_height) > 0);
png_row_ptrs = rxvt_malloc (sizeof(png_bytep)*png_height);
if (png_row_ptrs == NULL){
rxvt_msg (DBG_ERROR, DBG_IMAGES, "png read error: out of memory..\n");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(ifile);
return -1;
}
for(i = 0; i < (int)png_height; i++){
png_row_ptrs[i] = (png_byte*)(buf + i * png_row_bytes);
}
png_read_image(png_ptr, png_row_ptrs);
png_read_end(png_ptr,NULL);
rxvt_free(png_row_ptrs);
vwidth = *w;
vheight = *h;
stretched =0;
if (*w == 0 || *h == 0){
*w = rwidth;
*h = rheight;
}else{
if ((long)((double)rwidth * vheight/vwidth) < rheight){
*w = (long)((double)vheight * rwidth/rheight);
}else{
*h = (long)((double)vwidth * rheight/rwidth);
}
stretched = 1;
}
vwidth = *w;
vheight = *h;
image = 0;
visual = XDefaultVisual(display,XDefaultScreen(display));
if (display_depth >16){
image = XCreateImage(display,visual, display_depth,
ZPixmap,0,0,vwidth,vheight,32,0);
}else
if (display_depth >8){
image = XCreateImage(display,visual, display_depth,
ZPixmap,0,0,vwidth,vheight,16,0);
}else{
示例7: ReadPNG
unsigned char * ReadPNG(FILE * fp, unsigned int & sizeX, unsigned int &sizeY, int &img_depth, int &img_color_type, unsigned char *** row_pointer_ptr)
{
png_structp png_ptr;
png_bytepp row_pointers;
png_infop info_ptr;
int interlace_type;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
if (png_ptr == NULL)
{
exit(1);
return NULL;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fprintf(stderr,"VSImage ERROR : PNG info_ptr == NULL !!!\n");
exit(1);
return NULL;
}
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);
/* If we get here, we had a problem reading the file */
exit(1);
return NULL;
}
png_init_io(png_ptr, fp);
//png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */
png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)&sizeX, (png_uint_32 *)&sizeY, &img_depth, &img_color_type, &interlace_type, NULL, NULL);
# if __BYTE_ORDER != __BIG_ENDIAN
if (img_depth==16)
png_set_swap (png_ptr);
#endif
if (img_depth==16)//for now
png_set_strip_16(png_ptr);
if (img_color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (img_color_type == PNG_COLOR_TYPE_GRAY && img_depth < 8)
png_set_gray_1_2_4_to_8(png_ptr);
png_set_expand (png_ptr);
png_read_update_info (png_ptr,info_ptr);
png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)&sizeX, (png_uint_32 *)&sizeY, &img_depth, &img_color_type, &interlace_type, NULL, NULL);
row_pointers = (unsigned char **)malloc (sizeof (unsigned char *) *sizeY);
int numchan=1;
if (img_color_type&PNG_COLOR_MASK_COLOR)
numchan =3;
if (img_color_type &PNG_COLOR_MASK_PALETTE)
numchan =1;
if (img_color_type&PNG_COLOR_MASK_ALPHA)
numchan++;
unsigned long stride = numchan*sizeof (unsigned char)*img_depth/8;
unsigned char * image = (unsigned char *) malloc (stride*sizeX*sizeY);
for (unsigned int i=0;i<sizeY;i++)
{
row_pointers[i] = &image[i*stride*sizeX];
}
png_read_image (png_ptr,row_pointers);
unsigned char * result;
result = image;
//free (row_pointers);
*row_pointer_ptr=row_pointers;
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return result;
}
示例8: PyObject_AsFileDescriptor
//.........这里部分代码省略.........
png_set_swap(png_ptr);
}
// Convert palletes to full RGB
if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
}
// If there's an alpha channel convert gray to RGB
if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(png_ptr);
}
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr)))
{
throw Py::RuntimeError(
"_image_module::readpng: error during read_image");
}
png_bytep *row_pointers = new png_bytep[height];
png_uint_32 row;
for (row = 0; row < height; row++)
{
row_pointers[row] = new png_byte[png_get_rowbytes(png_ptr,info_ptr)];
}
png_read_image(png_ptr, row_pointers);
npy_intp dimensions[3];
dimensions[0] = height; //numrows
dimensions[1] = width; //numcols
if (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA)
{
dimensions[2] = 4; //RGBA images
}
else if (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_COLOR)
{
dimensions[2] = 3; //RGB images
}
else
{
dimensions[2] = 1; //Greyscale images
}
//For gray, return an x by y array, not an x by y by 1
int num_dims = (png_get_color_type(png_ptr, info_ptr)
& PNG_COLOR_MASK_COLOR) ? 3 : 2;
PyArrayObject *A = NULL;
if (float_result) {
double max_value = (1 << bit_depth) - 1;
PyArrayObject *A = (PyArrayObject *) PyArray_SimpleNew(
num_dims, dimensions, PyArray_FLOAT);
if (A == NULL)
{
throw Py::MemoryError("Could not allocate image array");
}
for (png_uint_32 y = 0; y < height; y++)
示例9: gst_gl_differencematte_loader
static gboolean
gst_gl_differencematte_loader (GstGLFilter * filter)
{
GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
png_structp png_ptr;
png_infop info_ptr;
guint sig_read = 0;
png_uint_32 width = 0;
png_uint_32 height = 0;
gint bit_depth = 0;
gint color_type = 0;
gint interlace_type = 0;
png_FILE_p fp = NULL;
guint y = 0;
guchar **rows = NULL;
gint filler;
if (!GST_GL_BASE_FILTER (filter)->context)
return TRUE;
if ((fp = fopen (differencematte->location, "rb")) == NULL)
LOAD_ERROR ("file not found");
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose (fp);
LOAD_ERROR ("failed to initialize the png_struct");
}
png_set_error_fn (png_ptr, NULL, NULL, user_warning_fn);
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);
LOAD_ERROR ("failed to initialize the memory for image information");
}
png_init_io (png_ptr, fp);
png_set_sig_bytes (png_ptr, sig_read);
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);
if (color_type == PNG_COLOR_TYPE_RGB) {
filler = 0xff;
png_set_filler (png_ptr, filler, PNG_FILLER_AFTER);
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
}
if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
fclose (fp);
png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
LOAD_ERROR ("color type is not rgb");
}
differencematte->pbuf_width = width;
differencematte->pbuf_height = height;
differencematte->pixbuf =
(guchar *) malloc (sizeof (guchar) * width * height * 4);
rows = (guchar **) malloc (sizeof (guchar *) * height);
for (y = 0; y < height; ++y)
rows[y] = (guchar *) (differencematte->pixbuf + y * width * 4);
png_read_image (png_ptr, rows);
free (rows);
png_read_end (png_ptr, info_ptr);
png_destroy_read_struct (&png_ptr, &info_ptr, png_infopp_NULL);
fclose (fp);
return TRUE;
}
示例10: fopen
//.........这里部分代码省略.........
/* Initialize the setjmp for returning properly after a libpng
error occured */
if (setjmp (png_jmpbuf(png_ptr))) {
fclose (fp);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
if (texinfo) {
if (texinfo->texels)
free (texinfo->texels);
free (texinfo);
}
return NULL;
}
/* Setup libpng for using standard C fread() function
with our FILE pointer */
png_init_io (png_ptr, fp);
/* Tell libpng that we have already read the magic number */
png_set_sig_bytes (png_ptr, sizeof (magic));
/* Read png info */
png_read_info (png_ptr, info_ptr);
int bit_depth, color_type;
/* Get some usefull information from header */
bit_depth = png_get_bit_depth (png_ptr, info_ptr);
color_type = png_get_color_type (png_ptr, info_ptr);
/* Convert index color images to RGB images */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb (png_ptr);
/* Convert 1-2-4 bits grayscale images to 8 bits
grayscale. */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_gray_1_2_4_to_8 (png_ptr);
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha (png_ptr);
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
else if (bit_depth < 8)
png_set_packing (png_ptr);
/* Update info structure to apply transformations */
png_read_update_info (png_ptr, info_ptr);
/* Retrieve updated information */
png_uint_32 w, h;
png_get_IHDR (png_ptr, info_ptr, &w, &h, &bit_depth,&color_type, NULL, NULL, NULL);
texinfo->width = w;
texinfo->height = h;
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
texinfo->format = GL_LUMINANCE;
texinfo->internalFormat = 1;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
texinfo->format = GL_LUMINANCE_ALPHA;
texinfo->internalFormat = 2;
break;
case PNG_COLOR_TYPE_RGB:
texinfo->format = GL_RGB;
texinfo->internalFormat = 3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
texinfo->format = GL_RGBA;
texinfo->internalFormat = 4;
break;
default:
/* Badness */
break;
}
/* We can now allocate memory for storing pixel data */
texinfo->texels = (GLubyte *)malloc (sizeof (GLubyte)*texinfo->width*texinfo->height*texinfo->internalFormat);
png_bytep *row_pointers;
/* Setup a pointer array. Each one points at the begening of a row. */
row_pointers = (png_bytep *)malloc (sizeof (png_bytep) * texinfo->height);
for (int i = 0; i < texinfo->height; ++i)
row_pointers[i] = (png_bytep)(texinfo->texels + ((texinfo->height - (i + 1)) * texinfo->width * texinfo->internalFormat));
/* Read pixel data using row pointers */
png_read_image(png_ptr, row_pointers);
/* We don't need row pointers anymore */
free (row_pointers);
return(texinfo);
}
示例11: glmReadPNG
//.........这里部分代码省略.........
/* 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)
png_set_expand(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_expand(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_expand(png_ptr);
/* Add filler (or alpha) byte (before/after each RGB triplet) */
/* png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); */
png_read_update_info(png_ptr, info_ptr);
channels = png_get_channels(png_ptr, info_ptr);
/* allocate the memory to hold the image using the fields of info_ptr. */
bytes_per_row = png_get_rowbytes(png_ptr, info_ptr);
buffer = (unsigned char*) malloc(bytes_per_row*height);
format = channels;
row_pointers = (png_bytepp) malloc(height*sizeof(png_bytep));
for (y = 0; y < height; y++) {
row_pointers[height-y-1] = buffer + y*bytes_per_row;
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, info_ptr);
free(row_pointers);
/* clean up after the read, and free any memory allocated - REQUIRED */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
/* close the file */
fclose(fp);
/* that's it */
if (buffer) {
*width_ret = width;
*height_ret = height;
switch(format) {
case 1:
*type_ret = GL_LUMINANCE;
break;
case 2:
*type_ret = GL_LUMINANCE_ALPHA;
break;
case 3:
*type_ret = GL_RGB;
break;
case 4:
*type_ret = GL_RGBA;
break;
}
pngerror = ERR_NO_ERROR;
}
else {
pngerror = ERR_MEM;
}
return buffer;
}
示例12: loadPNG
//.........这里部分代码省略.........
printf("png ptr not created\n");
return 0;
}
if (setjmp(png_jmpbuf(png_ptr))) {
printf("set jmp failed\n");
return 0;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
printf("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);
printf("%s broken?\n", filename);
return 0;
}
GLubyte *pixels =
(GLubyte *) malloc(sizeof(GLubyte) * (width * height * components));
row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * height);
for (int 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);
//printf("%s has %i colour components\n",filename,components);
//glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, glcolours, GL_UNSIGNED_BYTE, pixels);
glTexImage2D(GL_TEXTURE_2D, 0, glcolours, width, height, 0, glcolours,
GL_UNSIGNED_BYTE, pixels);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(pngFile);
free(row_pointers);
free(pixels);
return texture;
}
示例13: SplashDecodePng
int
SplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr)
{
int stride;
ImageFormat srcFormat;
png_uint_32 i, rowbytes;
png_bytepp row_pointers = NULL;
png_bytep image_data = NULL;
int success = 0;
double gamma;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_uint_32 width, height;
int bit_depth, color_type;
ImageRect srcRect, dstRect;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
goto done;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
goto done;
}
#ifdef __APPLE__
/* use setjmp/longjmp versions that do not save/restore the signal mask */
if (_setjmp(png_set_longjmp_fn(png_ptr, _longjmp, sizeof(jmp_buf)))) {
#else
if (setjmp(png_jmpbuf(png_ptr))) {
#endif
goto done;
}
png_set_read_fn(png_ptr, io_ptr, read_func);
png_set_sig_bytes(png_ptr, SIG_BYTES); /* we already read the 8 signature bytes */
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,
NULL, NULL, NULL);
/* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
* transparency chunks to full alpha channel; strip 16-bit-per-sample
* images to 8 bits per sample; and convert grayscale to RGB[A]
* this may be sub-optimal but this simplifies implementation */
png_set_expand(png_ptr);
png_set_tRNS_to_alpha(png_ptr);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
png_set_strip_16(png_ptr);
png_set_gray_to_rgb(png_ptr);
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
png_set_gamma(png_ptr, 2.2, gamma);
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if (!SAFE_TO_ALLOC(rowbytes, height)) {
goto done;
}
if ((image_data = (unsigned char *) malloc(rowbytes * height)) == NULL) {
goto done;
}
if (!SAFE_TO_ALLOC(height, sizeof(png_bytep))) {
goto done;
}
if ((row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep)))
== NULL) {
goto done;
}
for (i = 0; i < height; ++i)
row_pointers[i] = image_data + i * rowbytes;
png_read_image(png_ptr, row_pointers);
SplashCleanup(splash);
splash->width = width;
splash->height = height;
if (!SAFE_TO_ALLOC(splash->width, splash->imageFormat.depthBytes)) {
goto done;
}
stride = splash->width * splash->imageFormat.depthBytes;
if (!SAFE_TO_ALLOC(splash->height, stride)) {
goto done;
}
//.........这里部分代码省略.........
示例14: read_png_file
//.........这里部分代码省略.........
// 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
width = twidth;
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) {
//clean up memory and close stuff
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return;
}
//row_pointers is for pointing to image_data for reading the png with libpng
png_bytep *row_pointers = new png_bytep[height];
if (!row_pointers) {
//clean up memory and close stuff
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
delete[] image_data;
fclose(fp);
return ;
}
// set the individual row_pointers to point at the correct offsets of image_data
if(!flipY){
for (int i = 0; i < height; ++i)
row_pointers[i] = image_data + i * rowbytes;
}
else
{
for (int i = 0; i < height; ++i)
row_pointers[height - 1 - i] = image_data + i * rowbytes;
}
//read the png into image_data through row_pointers
png_read_image(png_ptr, row_pointers);
/*
// set the individual row_pointers to point at the correct offsets of image_data
for (int i = 0; i < height; ++i)
row_pointers[height - 1 - i] = (png_bytep)(image_data + i * rowbytes);
///////////////////////////////////////////////////////////////
*/
//png_read_image(png_ptr, row_pointers);
//png_set_rows(png_ptr,info_ptr,row_pointers);
//png_read_png(png_ptr,info_ptr,/*PNG_TRANSFORM_BGR |*/ PNG_TRANSFORM_INVERT_ALPHA,NULL);
//png_destroy_read_struct(&png_ptr, &info_ptr,NULL);
unsigned int* pixels = (unsigned int*)malloc(rowbytes * height);
memcpy(pixels,image_data,rowbytes * height);
tex.mPixelData = pixels;
tex.mWidth = width;
tex.mHeight = height;
if(color_type == PNG_COLOR_TYPE_RGB_ALPHA)
{
tex.mNumberOfColors = 4;
}
else if(color_type == PNG_COLOR_TYPE_RGB)
{
tex.mNumberOfColors = 3;
}
else
{
tex.mNumberOfColors = 3;
}
tex.mHasAlpha = tex.mNumberOfColors==4?true:false;
tex.mBitsPerPixel = tex.mNumberOfColors * bit_depth;
if(tex.mNumberOfColors == 4)
{
tex.mPixelFormat = GL_RGBA;
}
else
{
tex.mPixelFormat = GL_RGB;
}
//clean up memory and close stuff
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
delete[] image_data;
delete[] row_pointers;
fclose(fp);
}
示例15: NATIVECALL
static NATIVECALL(png_new){
Link * args = array_getArray(Arg);
unsigned char pngheader[HEADERSIZE];
png_structp png;
png_infop info;
png_bytep * row_pointers;
png_byte* row ;
FILE *fp;
int y, width, height;
/* open the file args[0] is the filename */
string_t filename_string = object_getString(args[0]);
fp = fopen(filename_string->data , "rb");
if (! fp){
return exception("CouldNotOpenFile", NULL, NULL);
}
/* read the png header and ensure it is a png */
fread(pngheader, 1, HEADERSIZE, fp);
if (png_sig_cmp(pngheader, 0, HEADERSIZE)){
return exception("NotPNG", NULL, NULL);
}
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_set_sig_bytes(png, HEADERSIZE);
info = png_create_info_struct(png);
png_init_io(png, fp);
png_read_info(png, info);
Link obj = object_create(image_type);
Image self = obj->value.vptr;
self->width = info->width;
self->height = info->height;
width = info->width;
height = info->height;
//info->bit_depth;
//info->channels;
//info->color_type;
//printf(" bit depth:%i channels:%i color type:%i rowbytes:%i\n", info->bit_depth, info->channels, info->color_type , info->rowbytes);
/* read png information */
png_read_update_info(png, info);
/* allocate memory for rows */
row_pointers = malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++) row_pointers[y] = malloc(info->rowbytes);
/* read image into allocated memory */
png_read_image(png, row_pointers);
fclose(fp);
png_byte * pdata;
self->format = info->channels;
self->data = pdata = malloc(self->width * self->height * self->format);
for (y=0; y<height; y++) {
row = row_pointers[height-y-1];
memcpy(pdata,row, self->width * self->format);
pdata+=self->width * self->format;
free(row);
}
free(row_pointers);
png_destroy_read_struct(&png, &info, NULL);
return obj;
}