本文整理汇总了C++中png_set_palette_to_rgb函数的典型用法代码示例。如果您正苦于以下问题:C++ png_set_palette_to_rgb函数的具体用法?C++ png_set_palette_to_rgb怎么用?C++ png_set_palette_to_rgb使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_set_palette_to_rgb函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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_expand_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;
}
示例2: png_create_read_struct_2
Error ImageLoaderPNG::_load_image(void *rf_up, png_rw_ptr p_func, Ref<Image> p_image) {
png_structp png;
png_infop info;
//png = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL);
png = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, _png_error_function, _png_warn_function, (png_voidp)NULL,
_png_malloc_fn, _png_free_fn);
ERR_FAIL_COND_V(!png, ERR_OUT_OF_MEMORY);
info = png_create_info_struct(png);
if (!info) {
png_destroy_read_struct(&png, NULL, NULL);
ERR_PRINT("Out of Memory");
return ERR_OUT_OF_MEMORY;
}
if (setjmp(png_jmpbuf(png))) {
png_destroy_read_struct(&png, NULL, NULL);
ERR_PRINT("PNG Corrupted");
return ERR_FILE_CORRUPT;
}
png_set_read_fn(png, (void *)rf_up, p_func);
png_uint_32 width, height;
int depth, color;
png_read_info(png, info);
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
//https://svn.gov.pt/projects/ccidadao/repository/middleware-offline/trunk/_src/eidmw/FreeImagePTEiD/Source/FreeImage/PluginPNG.cpp
//png_get_text(png,info,)
/*
printf("Image width:%i\n", width);
printf("Image Height:%i\n", height);
printf("Bit depth:%i\n", depth);
printf("Color type:%i\n", color);
*/
bool update_info = false;
if (depth < 8) { //only bit dept 8 per channel is handled
png_set_packing(png);
update_info = true;
};
if (png_get_color_type(png, info) == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png);
update_info = true;
}
if (depth > 8) {
png_set_strip_16(png);
update_info = true;
}
if (png_get_valid(png, info, PNG_INFO_tRNS)) {
//png_set_expand_gray_1_2_4_to_8(png);
png_set_tRNS_to_alpha(png);
update_info = true;
}
if (update_info) {
png_read_update_info(png, info);
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
}
int components = 0;
Image::Format fmt;
switch (color) {
case PNG_COLOR_TYPE_GRAY: {
fmt = Image::FORMAT_L8;
components = 1;
} break;
case PNG_COLOR_TYPE_GRAY_ALPHA: {
fmt = Image::FORMAT_LA8;
components = 2;
} break;
case PNG_COLOR_TYPE_RGB: {
fmt = Image::FORMAT_RGB8;
components = 3;
} break;
case PNG_COLOR_TYPE_RGB_ALPHA: {
fmt = Image::FORMAT_RGBA8;
components = 4;
} break;
default: {
ERR_PRINT("INVALID PNG TYPE");
//.........这里部分代码省略.........
示例3: image_png_load
/*
=================
image_png_load
=================
*/
GNUC_NONNULL static erbool image_png_load (const char *name, image_t *im)
{
fs_file_t f;
int size, r, width, height, inc;
png_byte *image = NULL, *p = NULL;
png_structp pngst;
png_infop info = NULL;
png_byte depth, color_type;
if (NULL == (f = fs_open(name, FS_RDONLY, &size, false)))
return false;
if (NULL == (pngst = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
&epng_error,
&epng_warn)))
{
sys_printf("failed to create png read struct\n");
fs_close(f);
return false;
}
if (NULL == (info = png_create_info_struct(pngst)))
{
sys_printf("failed to create png info struct\n");
goto error;
}
if (setjmp(png_jmpbuf(pngst)))
goto error;
png_set_user_limits(pngst, IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT);
png_set_read_fn(pngst, f, &epng_read);
png_read_info(pngst, info);
width = png_get_image_width(pngst, info);
height = png_get_image_height(pngst, info);
depth = png_get_bit_depth(pngst, info);
if (16 == depth)
{
/* 16 -> 8 */
png_set_strip_16(pngst);
depth = 8;
}
color_type = png_get_color_type(pngst, info);
/* 1/2/4 gray -> 8 gray */
if (PNG_COLOR_TYPE_GRAY == color_type && depth < 8)
png_set_expand_gray_1_2_4_to_8(pngst);
/* gray/palette -> rgb */
if (PNG_COLOR_TYPE_GRAY == color_type || PNG_COLOR_TYPE_GRAY_ALPHA == color_type)
png_set_gray_to_rgb(pngst);
else if (PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(pngst);
/* transparency -> alpha */
if (png_get_valid(pngst, info, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(pngst);
}
else
{
/* to rgba */
if (PNG_COLOR_TYPE_RGB_ALPHA != color_type &&
PNG_COLOR_TYPE_GRAY_ALPHA != color_type)
png_set_add_alpha(pngst, 0xff, PNG_FILLER_AFTER);
}
/* deinterlace */
png_set_interlace_handling(pngst);
/* read */
inc = width * 4;
p = image = mem_alloc(image_mempool, height * inc);
for (r = 0; r < height ;r++, p += inc)
png_read_row(pngst, p, NULL);
png_read_end(pngst, NULL);
png_destroy_read_struct(&pngst, &info, NULL);
fs_close(f);
im->width = width;
im->height = height;
im->data = image;
return true;
error:
if (NULL != f)
//.........这里部分代码省略.........
示例4: nonzero
//.........这里部分代码省略.........
/* Set up the input control if you are using standard C streams */
png_init_io(png_ptr, fp);
#else no_streams /* PNG file I/O method 2 */
/* If you are using replacement read functions, instead of calling
* png_init_io() here you would call:
*/
png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
/* where user_io_ptr is a structure you want available to the callbacks */
#endif no_streams /* Use only one I/O method! */
/* If we have already read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
#ifdef hilevel
/*
* If you have enough memory to read in the entire image at once,
* and you need to specify only transforms that can be controlled
* with one of the PNG_TRANSFORM_* bits (this presently excludes
* dithering, filling, setting background, and doing gamma
* adjustment), then you can read the entire image (including
* pixels) into the info structure with this call:
*/
png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
#else
/* OK, you're doing it the hard way, with the lower-level functions */
/* The call to png_read_info() gives us all of the information from the
* PNG file before the first IDAT (image data chunk). REQUIRED
*/
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, int_p_NULL, int_p_NULL);
/* Set up the data transformations you want. Note that these are all
* optional. Only call them if you want/need them. Many of the
* transformations only work on specific types of images, and many
* are mutually exclusive.
*/
/* Tell libpng to strip 16 bit/color files down to 8 bits/color */
png_set_strip_16(png_ptr);
/* Strip alpha bytes from the input data without combining with the
* 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_palette_to_rgb(png_ptr);
/* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
/* Expand paletted or RGB images with transparency to full alpha channels
* so the data will be available as RGBA quartets.
*/
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
/* Set the background color to draw transparent and alpha images over.
* It is possible to set the red, green, and blue components directly
* for paletted images instead of supplying a palette index. Note that
* even if the PNG file supplies a background, you are not required to
* use it - you should use the (solid) application background if it has one.
*/
png_color_16 my_background, *image_background;
if (png_get_bKGD(png_ptr, info_ptr, &image_background))
png_set_background(png_ptr, image_background,
PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
else
png_set_background(png_ptr, &my_background,
PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
/* Some suggestions as to how to get a screen gamma value
*
* Note that screen gamma is the display_exponent, which includes
* the CRT_exponent and any correction for viewing conditions
*/
if (/* We have a user-defined screen gamma value */)
{
screen_gamma = user-defined screen_gamma;
}
/* This is one way that applications share the same screen gamma value */
else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL)
示例5: readPNG
Image *
readPNG(const char *filename)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
Image *image;
fp = fopen(filename, "rb");
if (!fp)
goto no_fp;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
goto no_png;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
goto no_png;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
goto no_png;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
goto no_png;
}
png_init_io(png_ptr, fp);
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);
image = new Image(width, height);
if (!image)
goto no_image;
/* Convert to RGBA8 */
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 (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);
for (unsigned y = 0; y < height; ++y) {
png_bytep row = (png_bytep)(image->pixels + y*width*4);
png_read_row(png_ptr, row, NULL);
}
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return image;
no_image:
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
no_png:
fclose(fp);
no_fp:
return NULL;
}
示例6: var_Create
/*****************************************************************************
* LoadPNG: loads the PNG logo into memory
*****************************************************************************/
static picture_t *LoadPNG( vlc_object_t *p_this )
{
picture_t *p_pic;
char *psz_filename;
vlc_value_t val;
FILE *file;
int i, j, i_trans;
vlc_bool_t b_alpha = VLC_TRUE;
png_uint_32 i_width, i_height;
int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
int i_bit_depth;
png_bytep *p_row_pointers;
png_structp p_png;
png_infop p_info, p_end_info;
var_Create( p_this, "logo-file", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
var_Get( p_this, "logo-file", &val );
psz_filename = val.psz_string;
if( !psz_filename || !*psz_filename )
{
msg_Err( p_this, "logo file not specified" );
return 0;
}
if( !(file = fopen( psz_filename , "rb" )) )
{
msg_Err( p_this, "logo file (%s) not found", psz_filename );
free( psz_filename );
return 0;
}
free( psz_filename );
p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
p_info = png_create_info_struct( p_png );
p_end_info = png_create_info_struct( p_png );
png_init_io( p_png, file );
png_read_info( p_png, p_info );
png_get_IHDR( p_png, p_info, &i_width, &i_height,
&i_bit_depth, &i_color_type, &i_interlace_type,
&i_compression_type, &i_filter_type);
if( i_color_type == PNG_COLOR_TYPE_PALETTE )
png_set_palette_to_rgb( p_png );
if( i_color_type == PNG_COLOR_TYPE_GRAY ||
i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
png_set_gray_to_rgb( p_png );
if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
{
png_set_tRNS_to_alpha( p_png );
}
else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
{
b_alpha = VLC_FALSE;
}
p_row_pointers = malloc( sizeof(png_bytep) * i_height );
for( i = 0; i < (int)i_height; i++ )
p_row_pointers[i] = malloc( 4 * ( i_bit_depth + 7 ) / 8 * i_width );
png_read_image( p_png, p_row_pointers );
png_read_end( p_png, p_end_info );
fclose( file );
png_destroy_read_struct( &p_png, &p_info, &p_end_info );
/* Convert to YUVA */
p_pic = malloc( sizeof(picture_t) );
if( vout_AllocatePicture( p_this, p_pic, VLC_FOURCC('Y','U','V','A'),
i_width, i_height, VOUT_ASPECT_FACTOR ) !=
VLC_SUCCESS )
{
for( i = 0; i < (int)i_height; i++ ) free( p_row_pointers[i] );
free( p_row_pointers );
return 0;
}
var_Create(p_this, "logo-transparency", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
var_Get( p_this, "logo-transparency", &val );
i_trans = __MAX( __MIN( val.i_int, 255 ), 0 );
for( j = 0; j < (int)i_height ; j++ )
{
uint8_t *p = (uint8_t *)p_row_pointers[j];
for( i = 0; i < (int)i_width ; i++ )
{
int i_offset = i + j * p_pic->p[Y_PLANE].i_pitch;
p_pic->p[Y_PLANE].p_pixels[i_offset] =
(p[0] * 257L + p[1] * 504 + p[2] * 98)/1000 + 16;
p_pic->p[U_PLANE].p_pixels[i_offset] =
(p[2] * 439L - p[0] * 148 - p[1] * 291)/1000 + 128;
p_pic->p[V_PLANE].p_pixels[i_offset] =
(p[0] * 439L - p[1] * 368 - p[2] * 71)/1000 + 128;
//.........这里部分代码省略.........
示例7: fopen
ImageReaderPNG::ImageType ImageReaderPNG::readData(const std::string& filePath, PNGInfra& infra, bool wantAlpha)
{
if (filePath.empty())
return eInvalid;
infra.pFile = fopen(filePath.c_str(), "rb");
if (!infra.pFile)
{
fprintf(stderr, "Error opening file: %s\n", filePath.c_str());
return eInvalid;
}
unsigned char sig[8];
// check the signature
fread(sig, 1, 8, infra.pFile);
if (!png_check_sig(sig, 8))
{
fprintf(stderr, "Cannot open file: %s - not a valid PNG file.\n", filePath.c_str());
fclose(infra.pFile);
return eInvalid;
}
infra.pPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!infra.pPNG)
{
fclose(infra.pFile);
return eInvalid;
}
infra.pInfo = png_create_info_struct(infra.pPNG);
if (!infra.pInfo)
{
png_destroy_read_struct(&infra.pPNG, NULL, NULL);
fclose(infra.pFile);
return eInvalid;
}
if (setjmp(png_jmpbuf(infra.pPNG)))
{
png_destroy_read_struct(&infra.pPNG, &infra.pInfo, NULL);
fclose(infra.pFile);
return eInvalid;
}
png_init_io(infra.pPNG, infra.pFile);
png_set_sig_bytes(infra.pPNG, 8);
png_read_info(infra.pPNG, infra.pInfo);
int colorType;
int bitDepth, interlaceType, compressionType;
png_get_IHDR(infra.pPNG, infra.pInfo, &infra.width, &infra.height, &bitDepth, &colorType, &interlaceType, &compressionType, NULL);
if (colorType == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(infra.pPNG);
if (png_get_valid(infra.pPNG, infra.pInfo, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(infra.pPNG);
}
if (bitDepth == 16)
png_set_strip_16(infra.pPNG);
ImageType type = eInvalid;
if (!wantAlpha)
{
// add black Alpha
if ((colorType & PNG_COLOR_MASK_ALPHA) == 0)
png_set_add_alpha(infra.pPNG, 0xFF, PNG_FILLER_AFTER);
if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(infra.pPNG);
else if (colorType != PNG_COLOR_TYPE_RGB && colorType != PNG_COLOR_TYPE_RGB_ALPHA)
return eInvalid;
type = eRGBA;
}
else
{
if (colorType == PNG_COLOR_TYPE_RGB_ALPHA)
type = eRGBA;
else if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
type = eA;
else
return eInvalid;
}
png_read_update_info(infra.pPNG, infra.pInfo);
infra.pRows = new png_bytep[infra.height * png_sizeof(png_bytep)];
png_set_rows(infra.pPNG, infra.pInfo, infra.pRows);
for (unsigned int i = 0; i < infra.height; i++)
{
infra.pRows[i] = new png_byte[png_get_rowbytes(infra.pPNG, infra.pInfo)];
}
//.........这里部分代码省略.........
示例8: png_create_read_struct
plMipmap* plPNG::IRead(hsStream* inStream)
{
plMipmap* newMipmap = NULL;
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
try {
// Check PNG Signature
png_byte sig[PNGSIGSIZE];
inStream->Read8Bytes((char*) sig);
if (!png_sig_cmp(sig, 0, PNGSIGSIZE)) {
// Allocate required structs
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
throw false;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
throw false;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
throw false;
}
// Assign delegate function for reading from hsStream
png_set_read_fn(png_ptr, (png_voidp)inStream, pngReadDelegate);
// Get PNG Header information
png_set_sig_bytes(png_ptr, PNGSIGSIZE);
png_read_info(png_ptr, info_ptr);
png_uint_32 imgWidth = png_get_image_width(png_ptr, info_ptr);
png_uint_32 imgHeight = png_get_image_height(png_ptr, info_ptr);
png_uint_32 bitdepth = png_get_bit_depth(png_ptr, info_ptr);
png_uint_32 channels = png_get_channels(png_ptr, info_ptr);
png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr);
// Convert images to RGB color space
switch (color_type) {
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(png_ptr);
channels = 3;
break;
case PNG_COLOR_TYPE_GRAY:
if (bitdepth < 8) {
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
bitdepth = 8;
break;
}
// Convert transparency (if needed) to a full alpha channel
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
channels += 1;
} else if (channels == 3) {
// Add an opaque alpha channel if still none exists
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
channels = 4;
}
// Invert color byte-order as used by plMipmap for DirectX
png_set_bgr(png_ptr);
/// Construct a new mipmap to hold everything
newMipmap = new plMipmap(imgWidth, imgHeight, plMipmap::kARGB32Config, 1, plMipmap::kUncompressed);
char* destp = (char*)newMipmap->GetImage();
png_bytep* row_ptrs = new png_bytep[imgHeight];
const unsigned int stride = imgWidth * bitdepth * channels / 8;
// Assign row pointers to the appropriate locations in the newly-created Mipmap
for (size_t i = 0; i < imgHeight; i++) {
row_ptrs[i] = (png_bytep)destp + (i * stride);
}
png_read_image(png_ptr, row_ptrs);
png_read_end(png_ptr, end_info);
// Clean up allocated structs
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
delete [] row_ptrs;
}
} catch (...) {
delete newMipmap;
newMipmap = nullptr;
}
return newMipmap;
}
示例9: ReadPNG
ImageData ReadPNG(char* fileName)
{
ImageData meta;
meta.valid = false;
// Step 0: Make sure we can open the input file
FILE* inFile = fopen(fileName, "rb");
if (!inFile) {
abort_("ReadPNG: Can't open %s for reading\n", fileName);
}
// Step 1: initialize the reader
png_structp png = png_create_read_struct(
PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png) {
abort_("ReadPNG: png_create_read_struct() failed\n");
}
png_infop info = png_create_info_struct(png);
if (!info) {
abort_("ReadPNG: png_create_info_struct() failed\n");
}
if (setjmp(png_jmpbuf(png))) {
abort_("ReadPNG: Error during initialization.\n");
}
png_init_io(png, inFile);
png_read_info(png, info);
// Step 2: Collect image statistics
int width = png_get_image_width(png, info);
int height = png_get_image_height(png, info);
png_byte color_type = png_get_color_type(png, info);
png_byte bit_depth = png_get_bit_depth(png, info);
// Make some adjustments
if (bit_depth == 16) {
png_set_strip_16(png);
}
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png);
}
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
png_set_expand_gray_1_2_4_to_8(png);
}
if(png_get_valid(png, info, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png);
// These color_type don't have an alpha channel then fill it with 0xff.
if(color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
}
if(color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(png);
}
png_read_update_info(png, info);
// Step 3: Read the data
if (setjmp(png_jmpbuf(png))) {
abort_("ReadPNG: Error during image data reading.\n");
}
png_bytep row_pointers[height];
// This method gives a discontiguous array of rows
// Each row is later copied to the destination image, RGB only.
// There should be a more streamlined method, but this method works for lena.png
for (int y = 0; y < height; y++) {
row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
if (!row_pointers[y]) {
abort_("ReadPNG: Couldn't allocate row %d\n", y);
}
}
// Now we can read the image
int row_bytes = png_get_rowbytes(png, info);
png_read_image(png, row_pointers);
// Convert the image from discontiguous rows to contiguous rows, discarding alpha
Pixel* image = (Pixel*)malloc(height * width * 3);
if (!image) {
abort_("ReadPNG: Couldn't allocate image (%d, %d)\n", height, row_bytes);
}
// Brute force copy, skip over ALPHA channel
for (int row = 0; row < height; row++) {
for (int pixel = 0; pixel < width; pixel++)
{
image[(row * width + pixel)] =
*(Pixel*)(row_pointers[row] + pixel * 4);
}
// As we use them, delete the pointers to the image rows.
free(row_pointers[row]);
//.........这里部分代码省略.........
示例10: PNGLock
void FPngImageWrapper::UncompressPNGData( const ERGBFormat::Type InFormat, const int32 InBitDepth )
{
// thread safety
FScopeLock PNGLock(&GPNGSection);
check( CompressedData.Num() );
check( Width > 0 );
check( Height > 0 );
// Note that PNGs on PC tend to be BGR
check( InFormat == ERGBFormat::BGRA || InFormat == ERGBFormat::RGBA || InFormat == ERGBFormat::Gray ) // Other formats unsupported at present
check( InBitDepth == 8 || InBitDepth == 16 ) // Other formats unsupported at present
// Reset to the beginning of file so we can use png_read_png(), which expects to start at the beginning.
ReadOffset = 0;
png_structp png_ptr = png_create_read_struct_2( PNG_LIBPNG_VER_STRING, this, FPngImageWrapper::user_error_fn, FPngImageWrapper::user_warning_fn, NULL, FPngImageWrapper::user_malloc, FPngImageWrapper::user_free);
check( png_ptr );
png_infop info_ptr = png_create_info_struct( png_ptr );
check( info_ptr );
PNGReadGuard PNGGuard( &png_ptr, &info_ptr );
{
if (ColorType == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
}
if ((ColorType & PNG_COLOR_MASK_COLOR) == 0 && BitDepth < 8)
{
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
// Insert alpha channel with full opacity for RGB images without alpha
if ((ColorType & PNG_COLOR_MASK_ALPHA) == 0 && (InFormat == ERGBFormat::BGRA || InFormat == ERGBFormat::RGBA))
{
// png images don't set PNG_COLOR_MASK_ALPHA if they have alpha from a tRNS chunk, but png_set_add_alpha seems to be safe regardless
if ((ColorType & PNG_COLOR_MASK_COLOR) == 0)
{
png_set_tRNS_to_alpha(png_ptr);
}
else if (ColorType == PNG_COLOR_TYPE_PALETTE)
{
png_set_tRNS_to_alpha(png_ptr);
}
if (InBitDepth == 8)
{
png_set_add_alpha(png_ptr, 0xff , PNG_FILLER_AFTER);
}
else if (InBitDepth == 16)
{
png_set_add_alpha(png_ptr, 0xffff , PNG_FILLER_AFTER);
}
}
// Calculate Pixel Depth
const uint32 PixelChannels = (InFormat == ERGBFormat::Gray) ? 1 : 4;
const uint32 BytesPerPixel = (InBitDepth * PixelChannels) / 8;
const uint32 BytesPerRow = BytesPerPixel * Width;
RawData.Empty(Height * BytesPerRow);
RawData.AddUninitialized(Height * BytesPerRow);
png_set_read_fn( png_ptr, this, FPngImageWrapper::user_read_compressed );
png_bytep* row_pointers = (png_bytep*) png_malloc( png_ptr, Height*sizeof(png_bytep) );
PNGGuard.SetRowPointers(&row_pointers);
for (int32 i = 0; i < Height; i++)
{
row_pointers[i]= &RawData[i * BytesPerRow];
}
png_set_rows(png_ptr, info_ptr, row_pointers);
uint32 Transform = (InFormat == ERGBFormat::BGRA) ? PNG_TRANSFORM_BGR : PNG_TRANSFORM_IDENTITY;
// PNG files store 16-bit pixels in network byte order (big-endian, ie. most significant bits first).
#if PLATFORM_LITTLE_ENDIAN
// We're little endian so we need to swap
if (BitDepth == 16)
{
Transform |= PNG_TRANSFORM_SWAP_ENDIAN;
}
#endif
// Convert grayscale png to RGB if requested
if ((ColorType & PNG_COLOR_MASK_COLOR) == 0 &&
(InFormat == ERGBFormat::RGBA || InFormat == ERGBFormat::BGRA))
{
Transform |= PNG_TRANSFORM_GRAY_TO_RGB;
}
// Convert RGB png to grayscale if requested
if ((ColorType & PNG_COLOR_MASK_COLOR) != 0 && InFormat == ERGBFormat::Gray)
{
png_set_rgb_to_gray_fixed(png_ptr, 2 /* warn if image is in color */, -1, -1);
}
// Strip alpha channel if requested output is grayscale
if (InFormat == ERGBFormat::Gray)
{
// this is not necessarily the best option, instead perhaps:
//.........这里部分代码省略.........
示例11: _buffer
bool PngDecoder::readData( Mat& img )
{
bool result = false;
AutoBuffer<uchar*> _buffer(m_height);
uchar** buffer = _buffer;
int color = img.channels() > 1;
uchar* data = img.data;
int step = (int)img.step;
if( m_png_ptr && m_info_ptr && m_end_info && m_width && m_height )
{
png_structp png_ptr = (png_structp)m_png_ptr;
png_infop info_ptr = (png_infop)m_info_ptr;
png_infop end_info = (png_infop)m_end_info;
if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
{
int y;
if( img.depth() == CV_8U && m_bit_depth == 16 )
png_set_strip_16( png_ptr );
else if( !isBigEndian() )
png_set_swap( png_ptr );
if(img.channels() < 4)
{
/* observation: png_read_image() writes 400 bytes beyond
* end of data when reading a 400x118 color png
* "mpplus_sand.png". OpenCV crashes even with demo
* programs. Looking at the loaded image I'd say we get 4
* bytes per pixel instead of 3 bytes per pixel. Test
* indicate that it is a good idea to always ask for
* stripping alpha.. 18.11.2004 Axel Walthelm
*/
png_set_strip_alpha( png_ptr );
}
if( m_color_type == PNG_COLOR_TYPE_PALETTE )
png_set_palette_to_rgb( png_ptr );
if( m_color_type == PNG_COLOR_TYPE_GRAY && m_bit_depth < 8 )
#if (PNG_LIBPNG_VER_MAJOR*10000 + PNG_LIBPNG_VER_MINOR*100 + PNG_LIBPNG_VER_RELEASE >= 10209) || \
(PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR == 0 && PNG_LIBPNG_VER_RELEASE >= 18)
png_set_expand_gray_1_2_4_to_8( png_ptr );
#else
png_set_gray_1_2_4_to_8( png_ptr );
#endif
if( CV_MAT_CN(m_type) > 1 && color )
png_set_bgr( png_ptr ); // convert RGB to BGR
else if( color )
png_set_gray_to_rgb( png_ptr ); // Gray->RGB
else
png_set_rgb_to_gray( png_ptr, 1, 0.299, 0.587 ); // RGB->Gray
png_read_update_info( png_ptr, info_ptr );
for( y = 0; y < m_height; y++ )
buffer[y] = data + y*step;
png_read_image( png_ptr, buffer );
png_read_end( png_ptr, end_info );
result = true;
}
}
close();
return result;
}
示例12: load_png
int load_png( char *file_name, int stype )
{
char buf[PNG_BYTES_TO_CHECK], *mess;
unsigned char *rgb, *rgb2, *rgb3;
int i, row, do_prog, bit_depth, color_type, interlace_type, width, height;
unsigned int sig_read = 0;
FILE *fp;
png_bytep *row_pointers, trans;
png_color_16p trans_rgb;
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 pwidth, pheight;
png_colorp png_palette;
if ((fp = fopen(file_name, "rb")) == NULL) return -1;
i = fread(buf, 1, PNG_BYTES_TO_CHECK, fp);
if ( i != PNG_BYTES_TO_CHECK ) goto fail;
i = !png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK);
if ( i<=0 ) goto fail;
rewind( fp );
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) goto fail;
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
goto fail;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
fclose(fp);
return -1;
}
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, &pwidth, &pheight, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
width = (int) pwidth;
height = (int) pheight;
if ( width > MAX_WIDTH || height > MAX_HEIGHT )
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return TOO_BIG;
}
row_pointers = malloc( sizeof(png_bytep) * height );
if (setjmp(png_jmpbuf(png_ptr))) // If libpng generates an error now, clean up
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
free(row_pointers);
return FILE_LIB_ERROR;
}
rgb = NULL;
mess = NULL;
if ( width*height > FILE_PROGRESS ) do_prog = 1;
else do_prog = 0;
if ( stype == 0 )
{
mess = _("Loading PNG image");
rgb = mem_image;
}
if ( stype == 1 )
{
mess = _("Loading clipboard image");
rgb = mem_clipboard;
if ( rgb != NULL ) free( rgb ); // Lose old clipboard
mem_clip_mask_clear(); // Lose old clipboard mask
}
if ( color_type != PNG_COLOR_TYPE_PALETTE || bit_depth>8 ) // RGB PNG file
{
png_set_strip_16(png_ptr);
png_set_gray_1_2_4_to_8(png_ptr);
png_set_palette_to_rgb(png_ptr);
png_set_gray_to_rgb(png_ptr);
if ( stype == 0 )
{
mem_pal_copy( mem_pal, mem_pal_def );
mem_cols = 256;
if ( mem_new( width, height, 3 ) != 0 ) goto file_too_huge;
rgb = mem_image;
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
//.........这里部分代码省略.........
示例13: loadPNG
GLuint loadPNG(const char * file_name, GLuint tex)
{
png_byte header[8];
FILE *fp = fopen(file_name, "rb");
if (fp == 0)
{
perror(file_name);
return 0;
}
// read the header
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
{
fprintf(stderr, "error: %s is not a PNG.\n", file_name);
fclose(fp);
return 0;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fprintf(stderr, "error: png_create_read_struct returned 0.\n");
fclose(fp);
return 0;
}
// create png info struct
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fprintf(stderr, "error: png_create_info_struct returned 0.\n");
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fp);
return 0;
}
// create png info struct
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
fprintf(stderr, "error: png_create_info_struct returned 0.\n");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
fclose(fp);
return 0;
}
// the code in this if statement gets called if libpng encounters an error
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "error from libpng\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return 0;
}
// init png reading
png_init_io(png_ptr, fp);
// let libpng know you already read the first 8 bytes
png_set_sig_bytes(png_ptr, 8);
// read all the info up to the image data
png_read_info(png_ptr, info_ptr);
// variables to pass to get info
int bit_depth, color_type;
png_uint_32 temp_width, temp_height;
// get info about png
png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type,
NULL, NULL, NULL);
GLint format;
switch(color_type)
{
case PNG_COLOR_TYPE_RGB:
format = GL_RGB;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
format = GL_RGBA;
break;
case PNG_COLOR_TYPE_GRAY:
//cout<<"color Type Grey"<<endl;
format = GL_RGBA;
png_set_gray_to_rgb(png_ptr);
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
//cout<<"color Type Grey Alpha"<<endl;
format = GL_RGBA;
png_set_gray_to_rgb(png_ptr);
break;
case PNG_COLOR_TYPE_PALETTE:
//cout<<"color Type Palette"<<endl;
format = GL_RGBA;
png_set_palette_to_rgb(png_ptr);
//.........这里部分代码省略.........
示例14: check_png
static const char * VS_CC
check_png(img_hnd_t *ih, int n, FILE *fp, vs_args_t *va)
{
uint8_t signature[PNG_SIG_LENGTH];
if (fread(signature, 1, PNG_SIG_LENGTH, fp) != PNG_SIG_LENGTH ||
png_sig_cmp(signature, 0, PNG_SIG_LENGTH)) {
return "unsupported format";
}
png_structp p_str = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
NULL, NULL);
if (!p_str) {
return "failed to create png_read_struct";
}
png_infop p_info = png_create_info_struct(p_str);
if (!p_info) {
png_destroy_read_struct(&p_str, NULL, NULL);
return "failed to create png_info_struct";
}
png_init_io(p_str, fp);
png_set_sig_bytes(p_str, PNG_SIG_LENGTH);
png_read_info(p_str, p_info);
png_uint_32 width, height;
int color_type, bit_depth;
png_get_IHDR(p_str, p_info, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
if (color_type & PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(p_str);
}
if (bit_depth < 8) {
png_set_packing(p_str);
}
if (ih->enable_alpha == 0) {
if (color_type & PNG_COLOR_MASK_ALPHA) {
png_set_strip_alpha(p_str);
}
} else if ((color_type & PNG_COLOR_MASK_ALPHA) == 0) {
png_set_add_alpha(p_str, 0x00, PNG_FILLER_AFTER);
}
png_read_update_info(p_str, p_info);
png_get_IHDR(p_str, p_info, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
uint32_t row_size = png_get_rowbytes(p_str, p_info);
png_destroy_read_struct(&p_str, &p_info, NULL);
ih->src[n].width = width;
ih->src[n].height = height;
VSPresetFormat pf = get_dst_format(color_type, bit_depth);
if (pf == pfNone) {
return "unsupported png color type";
}
ih->src[n].format = va->vsapi->getFormatPreset(pf, va->core);
ih->src[n].read = read_png;
ih->src[n].flip = 0;
if (row_size > va->max_row_size) {
va->max_row_size = row_size;
}
return NULL;
}
示例15: png_create_read_struct
void *readpng(const unsigned char *base, size_t size, unsigned *_width, unsigned *_height)
{
PngReader reader;
unsigned char *data = 0;
unsigned char **rowptrs = 0;
png_structp p = 0;
png_infop pi = 0;
png_uint_32 width, height;
int bitdepth, colortype, imethod, cmethod, fmethod, i;
p = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if(p == 0) {
LOG("%s: failed to allocate png read struct\n", fn);
return 0;
}
pi = png_create_info_struct(p);
if(pi == 0) {
LOG("%s: failed to allocate png info struct\n", fn);
goto oops;
}
reader.base = base;
reader.end = base + size;
reader.cursor = base;
if(size < 8 || png_sig_cmp((unsigned char*)base, 0, 8)) {
LOG("%s: header is not a PNG header\n", fn);
goto oops;
}
reader.cursor += 8;
if(setjmp(png_jmpbuf(p))) {
LOG("%s: png library error\n", fn);
oops:
png_destroy_read_struct(&p, &pi, 0);
if(data != 0) free(data);
if(rowptrs != 0) free(rowptrs);
return 0;
}
png_set_read_fn (p, &reader, png_reader_read_data);
png_set_sig_bytes(p, 8);
png_read_info(p, pi);
png_get_IHDR(p, pi, &width, &height, &bitdepth, &colortype,
&imethod, &cmethod, &fmethod);
// printf("PNG: %d x %d (d=%d, c=%d)\n",
// width, height, bitdepth, colortype);
switch(colortype){
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(p);
break;
case PNG_COLOR_TYPE_RGB:
if(png_get_valid(p, pi, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(p);
} else {
png_set_filler(p, 0xff, PNG_FILLER_AFTER);
}
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
break;
case PNG_COLOR_TYPE_GRAY:
if(bitdepth < 8) {
png_set_gray_1_2_4_to_8(p);
}
default:
LOG("%s: unsupported (grayscale?) color type\n");
goto oops;
}
if(bitdepth == 16) {
png_set_strip_16(p);
}
data = (unsigned char*) malloc((width * 4) * height);
rowptrs = (unsigned char **) malloc(sizeof(unsigned char*) * height);
if((data == 0) || (rowptrs == 0)){
LOG("could not allocate data buffer\n");
goto oops;
}
for(i = 0; i < height; i++) {
rowptrs[i] = data + ((width * 4) * i);
}
png_read_image(p, rowptrs);
png_destroy_read_struct(&p, &pi, 0);
if(rowptrs != 0) free(rowptrs);
//.........这里部分代码省略.........