本文整理汇总了C++中png_get_color_type函数的典型用法代码示例。如果您正苦于以下问题:C++ png_get_color_type函数的具体用法?C++ png_get_color_type怎么用?C++ png_get_color_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_get_color_type函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: GP_ASSERT
Image* Image::create(const char* path)
{
GP_ASSERT(path);
// Open the file.
std::unique_ptr<Stream> stream(FileSystem::open(path));
if (stream.get() == NULL || !stream->canRead())
{
GP_ERROR("Failed to open image file '%s' - %d.", path, errno);
return NULL;
}
// Verify PNG signature.
unsigned char sig[8];
if (stream->read(sig, 1, 8) != 8 || png_sig_cmp(sig, 0, 8) != 0)
{
GP_ERROR("Failed to load file '%s'; not a valid PNG.", path);
return NULL;
}
// Initialize png read struct (last three parameters use stderr+longjump if NULL).
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL)
{
GP_ERROR("Failed to create PNG structure for reading PNG file '%s'.", path);
return NULL;
}
// Initialize info struct.
png_infop info = png_create_info_struct(png);
if (info == NULL)
{
GP_ERROR("Failed to create PNG info structure for PNG file '%s'.", path);
png_destroy_read_struct(&png, NULL, NULL);
return NULL;
}
// Set up error handling (required without using custom error handlers above).
if (setjmp(png_jmpbuf(png)))
{
GP_ERROR("Failed to set up error handling for reading PNG file '%s'.", path);
png_destroy_read_struct(&png, &info, NULL);
return NULL;
}
// Initialize file io.
png_set_read_fn(png, stream.get(), readStream);
// Indicate that we already read the first 8 bytes (signature).
png_set_sig_bytes(png, 8);
// Read the entire image into memory.
png_read_png(png, info, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_GRAY_TO_RGB, NULL);
Image* image = new Image();
image->_width = png_get_image_width(png, info);
image->_height = png_get_image_height(png, info);
png_byte colorType = png_get_color_type(png, info);
switch (colorType)
{
case PNG_COLOR_TYPE_RGBA:
image->_format = Image::RGBA;
break;
case PNG_COLOR_TYPE_RGB:
image->_format = Image::RGB;
break;
default:
GP_ERROR("Unsupported PNG color type (%d) for image file '%s'.", (int)colorType, path);
png_destroy_read_struct(&png, &info, NULL);
return NULL;
}
size_t stride = png_get_rowbytes(png, info);
// Allocate image data.
image->_data = new unsigned char[stride * image->_height];
//GP_INFO("File[%s] ColorType : %d height : %d width : %d size : %d", path, image->_format, image->_height, image->_width, stride * image->_height);
// Read rows into image data.
png_bytepp rows = png_get_rows(png, info);
for (unsigned int i = 0; i < image->_height; ++i)
{
memcpy(image->_data+(stride * (image->_height-1-i)), rows[i], stride);
}
//if (0 == strcmp(path, "res/hello_jpg.png"))
//{
// std::string strTmp;
// for (int i = 0; i < stride * image->_height; i++)
// {
// char szTmp[10] = { 0 };
// sprintf(szTmp, "%02X", image->_data[i]);
// strTmp.append(szTmp);
// if (i % 20 == 0)
// {
// GP_INFO("DATA : %s", strTmp.c_str());
//.........这里部分代码省略.........
开发者ID:terry8yu,项目名称:gameplay,代码行数:101,代码来源:Image.cpp
示例2: decodeWithWIC
bool Image::initWithPngData(const unsigned char * data, ssize_t dataLen)
{
#if CC_USE_WIC
return decodeWithWIC(data, dataLen);
#else
// length of bytes to check if it is a valid png file
#define PNGSIGSIZE 8
bool ret = false;
png_byte header[PNGSIGSIZE] = { 0 };
png_structp png_ptr = 0;
png_infop info_ptr = 0;
do
{
// png header len is 8 bytes
CC_BREAK_IF(dataLen < PNGSIGSIZE);
// check the data is png or not
memcpy(header, data, PNGSIGSIZE);
CC_BREAK_IF(png_sig_cmp(header, 0, PNGSIGSIZE));
// init png_struct
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
CC_BREAK_IF(!png_ptr);
// init png_info
info_ptr = png_create_info_struct(png_ptr);
CC_BREAK_IF(!info_ptr);
#if (PARA_TARGET_PLATFORM != PARA_PLATFORM_BADA && PARA_TARGET_PLATFORM != PARA_PLATFORM_NACL && PARA_TARGET_PLATFORM != PARA_PLATFORM_TIZEN)
CC_BREAK_IF(setjmp(png_jmpbuf(png_ptr)));
#endif
// set the read call back function
tImageSource imageSource;
imageSource.data = (unsigned char*)data;
imageSource.size = dataLen;
imageSource.offset = 0;
png_set_read_fn(png_ptr, &imageSource, pngReadCallback);
// read png header info
// read png file info
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);
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr);
//OUTPUT_LOG("color type %u", color_type);
// force palette images to be expanded to 24-bit RGB
// it may include alpha channel
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
}
// low-bit-depth grayscale images are to be expanded to 8 bits
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
{
bit_depth = 8;
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
// expand any tRNS chunk data into a full alpha channel
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
}
// reduce images with 16-bit samples to 8 bits
if (bit_depth == 16)
{
png_set_strip_16(png_ptr);
}
// Expanded earlier for grayscale, now take care of palette and rgb
if (bit_depth < 8)
{
png_set_packing(png_ptr);
}
// update info
png_read_update_info(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
switch (color_type)
{
case PNG_COLOR_TYPE_GRAY:
_renderFormat = Texture2D::PixelFormat::I8;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
_renderFormat = Texture2D::PixelFormat::AI88;
break;
case PNG_COLOR_TYPE_RGB:
_renderFormat = Texture2D::PixelFormat::RGB888;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
_renderFormat = Texture2D::PixelFormat::RGBA8888;
break;
default:
//.........这里部分代码省略.........
开发者ID:imfoollink,项目名称:NPLRuntime,代码行数:101,代码来源:CCImage.cpp
示例3: loadTextureFromPNG
/*
* Magical PNG loader. You probably shouldn't edit this.
*/
int loadTextureFromPNG(char* filename , TextureInfo* t){
//Required structs and file pointers.
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
FILE *fp;
//Again, this should return an error code instead of exiting,
//but for this assignment, it should work anyway.
if ((fp = fopen(filename, "rb")) == NULL){
printf("Error opening %s\n",filename);
exit(1);
}
//Magic, do not touch.
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
printf("LibPNG did something terrible!\n");
fclose(fp);
exit(1);
}
//Allocate the struct we're going to read the data into.
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
printf("LibPNG did something terrible!\n");
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
exit(1);
}
//Error handling magic. Do not touch.
if (setjmp(png_jmpbuf(png_ptr))) {
printf("LibPNG did something terrible!\n");
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
exit(1);
}
//Point libPNG to the right place
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sig_read);
//Yet more magic you shouldn't touch. We -probably- have enough memory to read it all in one go.
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, png_voidp_NULL);
//Start setting values in the textureinfo struct
//Note that PNGs -can- have alpha.
t->width = png_get_image_width(png_ptr,info_ptr);
t->height = png_get_image_height(png_ptr,info_ptr);
switch (png_get_color_type(png_ptr,info_ptr)) {
case PNG_COLOR_TYPE_RGBA:
t->hasAlpha = true;
break;
case PNG_COLOR_TYPE_RGB:
t->hasAlpha = false;
break;
default:
printf("Invalid PNG.\n");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
exit(1);
}
//How long is a row?
unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
//Allocate some space!
t->textureData = (unsigned char*) malloc(row_bytes * t->height);
//Read the image data into the texture array, in reverse row order. Because GL.
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
for (int i = 0; i < t->height; i++) {
memcpy(t->textureData+(row_bytes * (t->height-1-i)), row_pointers[i], row_bytes);
}
//Clean up
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
return 0;
}
开发者ID:le4dface,项目名称:texture-lighting,代码行数:85,代码来源:G308_ImageLoader.cpp
示例4: load_png_data
unsigned char* load_png_data(char *filename, int *w, int *h) {
FILE *fp = fopen(filename, "rb");
if(!fp) {
return NULL;
}
png_byte header[8];
fread(header, 1, 8, fp);
if(png_sig_cmp(header, 0, 8)) {
return NULL;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr) {
return NULL;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
fclose(fp);
return NULL;
}
png_infop end_ptr = png_create_info_struct(png_ptr);
if(!end_ptr) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return NULL;
}
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_ptr);
fclose(fp);
return NULL;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
switch(png_get_color_type(png_ptr, info_ptr)) {
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(png_ptr);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
break;
case PNG_COLOR_TYPE_GRAY:
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
case PNG_COLOR_TYPE_GRAY_ALPHA:
if(bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
if(bit_depth==16) png_set_strip_16(png_ptr);
png_set_gray_to_rgb(png_ptr);
break;
case PNG_COLOR_TYPE_RGB:
if(bit_depth < 8 ) png_set_packing(png_ptr);
if(bit_depth == 16) png_set_strip_16(png_ptr);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
break;
default:
break;
}
png_read_update_info(png_ptr, info_ptr);
*w = png_get_image_width(png_ptr, info_ptr);
*h = png_get_image_height(png_ptr, info_ptr);
int rowbytes = (int)png_get_rowbytes(png_ptr, info_ptr);
png_bytep data = malloc((*h)*rowbytes*sizeof(*data));
png_bytep row_pointers[*h];
int i;
for(i=0; i<(*h); ++i) {
row_pointers[i] = &data[i*rowbytes];
}
png_read_image(png_ptr, row_pointers);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_ptr);
fclose(fp);
return data;
}
开发者ID:xymostech,项目名称:FPSGame,代码行数:87,代码来源:texture.c
示例5: ReadPNG
//-----------------------------------------------------------------
Image::Ptr ReadPNG(File* file)
{
assert(file);
if (!file) {
return 0;
}
// initialize the necessary libpng data structures
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (!png_ptr) {
return 0;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, 0, 0);
return 0;
}
// check if the file is actually a PNG file
u8 sig_buf[8];
if (file->read(sig_buf, 8) != 8 || png_sig_cmp(sig_buf, 0, 8) != 0) {
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return 0;
}
// libpng uses SJLJ for error handling, so we need to define
// any automatic variables before the call to setjmp()
Image::Ptr image;
ArrayAutoPtr<png_bytep> rows;
// establish a return point
if (setjmp(png_jmpbuf(png_ptr)) != 0) {
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return 0;
}
// tell libpng that we are already 8 bytes into the image file
png_set_sig_bytes(png_ptr, 8);
// tell libpng that we are going to use our own io functions
png_set_read_fn(png_ptr, file, read_callback);
// read the png header
png_read_info(png_ptr, info_ptr);
// get the image attributes
int img_width = png_get_image_width(png_ptr, info_ptr);
int img_height = png_get_image_height(png_ptr, info_ptr);
int img_bit_depth = png_get_bit_depth(png_ptr, info_ptr);
int img_color_type = png_get_color_type(png_ptr, info_ptr);
// if the color channel bit depth is 16 bit, strip it to 8 bit
if (img_bit_depth == 16) {
png_set_strip_16(png_ptr);
}
// Note: Not sure if we need this, or if it's correct what we are doing here.
// if the image has a tRNS chunk, use it for the alpha channel
//if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
// png_set_tRNS_to_alpha(png_ptr);
//}
switch (img_color_type)
{
case PNG_COLOR_TYPE_PALETTE:
{
image = new ImageImpl(img_width, img_height, PixelFormat::RGB_P8);
// get palette
png_colorp palette = 0;
int num_palette = 0;
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
// copy palette over to image
std::memcpy(image->getPalette(), palette, num_palette * sizeof(RGB));
break;
}
case PNG_COLOR_TYPE_GRAY:
{
png_set_gray_to_rgb(png_ptr);
image = new ImageImpl(img_width, img_height, PixelFormat::RGB);
break;
}
case PNG_COLOR_TYPE_GRAY_ALPHA:
{
png_set_gray_to_rgb(png_ptr);
image = new ImageImpl(img_width, img_height, PixelFormat::RGBA);
break;
}
case PNG_COLOR_TYPE_RGB:
{
image = new ImageImpl(img_width, img_height, PixelFormat::RGB);
break;
}
case PNG_COLOR_TYPE_RGB_ALPHA:
{
image = new ImageImpl(img_width, img_height, PixelFormat::RGBA);
//.........这里部分代码省略.........
开发者ID:kyuu,项目名称:azura,代码行数:101,代码来源:png.cpp
示例6: loadPng
static bool loadPng(const uint8_t *inputData, size_t size,
Bytes &outputData, Color &color, Alpha &alpha, uint32_t &width, uint32_t &height,
uint32_t &stride, const Bitmap::StrideFn &strideFn) {
auto png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
log::text("libpng", "fail to create read struct");
return false;
}
auto info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
log::text("libpng", "fail to create info struct");
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
if (setjmp(png_jmpbuf(png_ptr))) {
log::text("libpng", "error in processing (setjmp return)");
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return false;
}
Bitmap_readState state;
state.data = inputData;
state.offset = 0;
png_set_read_fn(png_ptr,(png_voidp)&state, Bitmap_readDynamicData);
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);
png_byte bitdepth = png_get_bit_depth(png_ptr, info_ptr);
png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
}
if (color_type == PNG_COLOR_TYPE_GRAY && bitdepth < 8) {
bitdepth = 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 (bitdepth == 16) {
png_set_strip_16(png_ptr);
}
if (bitdepth < 8) {
png_set_packing(png_ptr);
}
png_read_update_info(png_ptr, info_ptr);
bitdepth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
auto rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY) {
color = (color == Color::A8?Color::A8:Color::I8);
} else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
color = Color::IA88;
} else if (color_type == PNG_COLOR_TYPE_RGB) {
color = Color::RGB888;
} else if (color_type == PNG_COLOR_TYPE_RGBA) {
color = Color::RGBA8888;
} else {
width = 0;
height = 0;
stride = 0;
outputData.clear();
log::format("Bitmap", "unsupported color type: %u", (unsigned int)color_type);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return false;
}
if (strideFn) {
stride = max((uint32_t)strideFn(color, width), (uint32_t)rowbytes);
} else {
stride = (uint32_t)rowbytes;
}
if (color == Color::I8 || color == Color::RGB888) {
alpha = Alpha::Opaque;
} else {
alpha = Alpha::Unpremultiplied;
}
// read png data
png_bytep* row_pointers = new png_bytep[height];
auto dataLen = stride * height;
outputData.resize(dataLen);
for (unsigned short i = 0; i < height; ++i) {
row_pointers[i] = outputData.data() + i*stride;
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, nullptr);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
//.........这里部分代码省略.........
开发者ID:SBKarr,项目名称:stappler,代码行数:101,代码来源:SPBitmap.cpp
示例7: strcpy
int ShapeWipeMain::read_pattern_image(int new_frame_width, int new_frame_height)
{
png_byte header[8];
int is_png;
int row;
int col;
int scaled_row;
int scaled_col;
int pixel_width;
unsigned char value;
png_uint_32 width;
png_uint_32 height;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
png_bytep *image;
frame_width = new_frame_width;
frame_height = new_frame_height;
// Convert name to filename
for(int i = 0; i < shape_paths.size(); i++)
{
if(!strcmp(shape_titles.get(i), shape_name))
{
strcpy(filename, shape_paths.get(i));
break;
}
}
FILE *fp = fopen(filename, "rb");
if (!fp)
{
return 1;
}
fread(header, 1, 8, fp);
is_png = !png_sig_cmp(header, 0, 8);
if (!is_png)
{
return 1;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr)
{
return 1;
}
/* Tell libpng we already checked the first 8 bytes */
png_set_sig_bytes(png_ptr, 8);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
return 1;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return 1;
}
png_init_io(png_ptr, fp);
png_read_info(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
width = png_get_image_width (png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
/* Skip the alpha channel if present
* stripping alpha currently doesn't work in conjunction with
* converting to grayscale in libpng */
if (color_type & PNG_COLOR_MASK_ALPHA)
pixel_width = 2;
else
pixel_width = 1;
/* Convert 16 bit data to 8 bit */
if (bit_depth == 16) png_set_strip_16(png_ptr);
/* Expand to 1 pixel per byte if necessary */
if (bit_depth < 8) png_set_packing(png_ptr);
/* Convert to grayscale */
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_rgb_to_gray_fixed(png_ptr, 1, -1, -1);
/* Allocate memory to hold the original png image */
image = (png_bytep*)malloc(sizeof(png_bytep)*height);
for (row = 0; row < height; row++)
//.........这里部分代码省略.........
开发者ID:petterreinholdtsen,项目名称:cinelerra-hv,代码行数:101,代码来源:shapewipe.C
示例8: gst_pngdec_caps_create_and_set
static GstFlowReturn
gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
{
GstFlowReturn ret = GST_FLOW_OK;
gint bpc = 0, color_type;
png_uint_32 width, height;
GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);
/* Get bits per channel */
bpc = png_get_bit_depth (pngdec->png, pngdec->info);
/* Get Color type */
color_type = png_get_color_type (pngdec->png, pngdec->info);
/* Add alpha channel if 16-bit depth, but not for GRAY images */
if ((bpc > 8) && (color_type != PNG_COLOR_TYPE_GRAY)) {
png_set_add_alpha (pngdec->png, 0xffff, PNG_FILLER_BEFORE);
png_set_swap (pngdec->png);
}
#if 0
/* We used to have this HACK to reverse the outgoing bytes, but the problem
* that originally required the hack seems to have been in videoconvert's
* RGBA descriptions. It doesn't seem needed now that's fixed, but might
* still be needed on big-endian systems, I'm not sure. J.S. 6/7/2007 */
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_bgr (pngdec->png);
#endif
/* Gray scale with alpha channel converted to RGB */
if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
GST_LOG_OBJECT (pngdec,
"converting grayscale png with alpha channel to RGB");
png_set_gray_to_rgb (pngdec->png);
}
/* Gray scale converted to upscaled to 8 bits */
if ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
(color_type == PNG_COLOR_TYPE_GRAY)) {
if (bpc < 8) { /* Convert to 8 bits */
GST_LOG_OBJECT (pngdec, "converting grayscale image to 8 bits");
#if PNG_LIBPNG_VER < 10400
png_set_gray_1_2_4_to_8 (pngdec->png);
#else
png_set_expand_gray_1_2_4_to_8 (pngdec->png);
#endif
}
}
/* Palette converted to RGB */
if (color_type == PNG_COLOR_TYPE_PALETTE) {
GST_LOG_OBJECT (pngdec, "converting palette png to RGB");
png_set_palette_to_rgb (pngdec->png);
}
png_set_interlace_handling (pngdec->png);
/* Update the info structure */
png_read_update_info (pngdec->png, pngdec->info);
/* Get IHDR header again after transformation settings */
png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
&bpc, &pngdec->color_type, NULL, NULL, NULL);
GST_LOG_OBJECT (pngdec, "this is a %dx%d PNG image", (gint) width,
(gint) height);
switch (pngdec->color_type) {
case PNG_COLOR_TYPE_RGB:
GST_LOG_OBJECT (pngdec, "we have no alpha channel, depth is 24 bits");
if (bpc == 8)
format = GST_VIDEO_FORMAT_RGB;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
GST_LOG_OBJECT (pngdec,
"we have an alpha channel, depth is 32 or 64 bits");
if (bpc == 8)
format = GST_VIDEO_FORMAT_RGBA;
else if (bpc == 16)
format = GST_VIDEO_FORMAT_ARGB64;
break;
case PNG_COLOR_TYPE_GRAY:
GST_LOG_OBJECT (pngdec,
"We have an gray image, depth is 8 or 16 (be) bits");
if (bpc == 8)
format = GST_VIDEO_FORMAT_GRAY8;
else if (bpc == 16)
format = GST_VIDEO_FORMAT_GRAY16_BE;
break;
default:
break;
}
if (format == GST_VIDEO_FORMAT_UNKNOWN) {
GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
("pngdec does not support this color type"));
ret = GST_FLOW_NOT_SUPPORTED;
goto beach;
}
//.........这里部分代码省略.........
开发者ID:Lachann,项目名称:gst-plugins-good,代码行数:101,代码来源:gstpngdec.c
示例9: PPGame_LoadPNG
unsigned char* PPGame_LoadPNG(const char* fileName,unsigned long* imageWidth,unsigned long* imageHeight,unsigned long* bytesPerRow)
{
FILE *PNG_file = fopen(fileName, "rb");
// fprintf(stderr, "open PNG file %s\n", fileName);
if (PNG_file == NULL)
{
fprintf(stderr, "Can't open PNG file %s\n", fileName);
return NULL;
}
unsigned char PNG_header[PNG_HEADER_SIZE];
fread(PNG_header, 1, PNG_HEADER_SIZE, PNG_file);
if (png_sig_cmp(PNG_header, 0, PNG_HEADER_SIZE) != 0)
{
fprintf(stderr, "%s is not a PNG file\n", fileName);
fclose(PNG_file);
return NULL;
}
/*
{
for (int i=0;i<8;i++) {
printf("%02X,",PNG_header[i]);
}
printf("\n");
}
*/
png_structp PNG_reader = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (PNG_reader == NULL)
{
fprintf(stderr, "Can't start reading PNG file %s\n", fileName);
fclose(PNG_file);
return NULL;
}
png_infop PNG_info = png_create_info_struct(PNG_reader);
if (PNG_info == NULL)
{
fprintf(stderr, "Can't get info for PNG file %s\n", fileName);
png_destroy_read_struct(&PNG_reader, NULL, NULL);
fclose(PNG_file);
return NULL;
}
png_infop PNG_end_info = png_create_info_struct(PNG_reader);
if (PNG_end_info == NULL)
{
fprintf(stderr, "Can't get end info for PNG file %s\n", fileName);
png_destroy_read_struct(&PNG_reader, &PNG_info, NULL);
fclose(PNG_file);
return NULL;
}
if (setjmp(png_jmpbuf(PNG_reader)))
{
fprintf(stderr, "Can't load PNG file %s\n", fileName);
png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info);
fclose(PNG_file);
return NULL;
}
png_init_io(PNG_reader, PNG_file);
png_set_sig_bytes(PNG_reader, PNG_HEADER_SIZE);
png_read_info(PNG_reader, PNG_info);
if (PNG_reader == NULL) {
fclose(PNG_file);
return NULL;
}
png_uint_32 width, height;
width = png_get_image_width(PNG_reader, PNG_info);
height = png_get_image_height(PNG_reader, PNG_info);
//printf("width %d\n",width);
//printf("height %d\n",height);
png_uint_32 bit_depth, color_type;
bit_depth = png_get_bit_depth(PNG_reader, PNG_info);
color_type = png_get_color_type(PNG_reader, PNG_info);
//printf("bit_depth %d\n",bit_depth);
//printf("rowbytes %d\n",png_get_rowbytes(PNG_reader, PNG_info));
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
//png_set_palette_to_rgb(PNG_reader);
png_set_palette_to_rgb(PNG_reader);
}
/*
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
{
png_set_gray_1_2_4_to_8(PNG_reader);
}
*/
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
//.........这里部分代码省略.........
开发者ID:yamagame,项目名称:PicoPicoGameEngine,代码行数:101,代码来源:PPGameUtilLinux.cpp
示例10: read_png
gavl_video_frame_t * read_png(const char * filename,
gavl_video_format_t * format,
gavl_pixelformat_t pixelformat)
{
int i;
unsigned char ** rows;
gavl_video_converter_t * cnv;
gavl_video_options_t * opt;
gavl_video_format_t format_1;
gavl_video_frame_t * frame, * frame_1;
int bit_depth;
int color_type;
int has_alpha = 0;
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
FILE * file;
file = fopen(filename, "rb");
if(!file)
{
fprintf(stderr, "Cannot open file %s\n", filename);
return NULL;
}
png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING, NULL,
NULL, NULL);
setjmp(png_jmpbuf(png_ptr));
info_ptr = png_create_info_struct(png_ptr);
end_info = png_create_info_struct(png_ptr);
png_init_io(png_ptr, file);
png_read_info(png_ptr, info_ptr);
format->frame_width = png_get_image_width(png_ptr, info_ptr);
format->frame_height = png_get_image_height(png_ptr, info_ptr);
format->image_width = format->frame_width;
format->image_height = format->frame_height;
format->pixel_width = 1;
format->pixel_height = 1;
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
switch(color_type)
{
case PNG_COLOR_TYPE_GRAY: /* (bit depths 1, 2, 4, 8, 16) */
if(bit_depth < 8)
#if GAVL_MAKE_BUILD(PNG_LIBPNG_VER_MAJOR, PNG_LIBPNG_VER_MINOR, PNG_LIBPNG_VER_RELEASE) < GAVL_MAKE_BUILD(1,2,9)
png_set_gray_1_2_4_to_8(png_ptr);
#else
png_set_expand_gray_1_2_4_to_8(png_ptr);
#endif
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
has_alpha = 1;
}
png_set_gray_to_rgb(png_ptr);
break;
case PNG_COLOR_TYPE_GRAY_ALPHA: /* (bit depths 8, 16) */
if(bit_depth == 16)
png_set_strip_16(png_ptr);
png_set_gray_to_rgb(png_ptr);
break;
case PNG_COLOR_TYPE_PALETTE: /* (bit depths 1, 2, 4, 8) */
png_set_palette_to_rgb(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
has_alpha = 1;
}
break;
case PNG_COLOR_TYPE_RGB: /* (bit_depths 8, 16) */
if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
has_alpha = 1;
}
if(bit_depth == 16)
png_set_strip_16(png_ptr);
break;
case PNG_COLOR_TYPE_RGB_ALPHA: /* (bit_depths 8, 16) */
if(bit_depth == 16)
png_set_strip_16(png_ptr);
has_alpha = 1;
break;
}
if(has_alpha)
format->pixelformat = GAVL_RGBA_32;
//.........这里部分代码省略.........
开发者ID:Distrotech,项目名称:gmerlin,代码行数:101,代码来源:pngutil.c
示例11: readPngFile
void readPngFile(char* file_name)
{
//printf("%s\n", file_name);
char header[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp){
printf("Unable to open file '%s'\n", file_name);
exit(-1);
//abort_("[read_png_file] File %s could not be opened for reading", file_name);
}
fread(header, 1, 8, fp);
// if (png_sig_cmp(header, 0, 8))
// abort_("[read_png_file] File %s is not recognized as a PNG file", file_name);
//
/* initialize stuff */
pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!pngPtr)
abort_("[read_png_file] png_create_read_struct failed");
infoPtr = png_create_info_struct(pngPtr);
if (!infoPtr)
abort_("[read_png_file] png_create_info_struct failed");
if (setjmp(png_jmpbuf(pngPtr)))
abort_("[read_png_file] Error during init_io");
png_init_io(pngPtr, fp);
png_set_sig_bytes(pngPtr, 8);
png_read_info(pngPtr, infoPtr);
imageWidth = png_get_image_width(pngPtr, infoPtr);
imageLength = png_get_image_height(pngPtr, infoPtr);
config = png_get_color_type(pngPtr, infoPtr);
bitsPerSample = png_get_bit_depth(pngPtr, infoPtr); // = 8 bits
numberOfPasses = png_set_interlace_handling(pngPtr);
png_read_update_info(pngPtr, infoPtr);
imageWidth = png_get_image_width(pngPtr, infoPtr);
imageLength = png_get_image_height(pngPtr, infoPtr);
samplesPerPixel = png_get_channels(pngPtr, infoPtr); // = 4 bytes
bitsPerPixel = samplesPerPixel*bitsPerSample;
linebytes = samplesPerPixel * imageWidth; // = 640
//linebytes = png_get_rowbytes(pngPtr, infoPtr); = 640
imageBitSize = (sizeof(uint8) * imageWidth * imageLength * samplesPerPixel);
imageSize = imageWidth * imageLength * samplesPerPixel;
//printf("linebytes = %i, expected %i\n",linebytes,png_get_rowbytes(pngPtr, infoPtr));
//printf("Image Height is %d", sizeof(png_bytep) * imageLength);
/* read file */
if (setjmp(png_jmpbuf(pngPtr)))
abort_("[read_png_file] Error during read_image");
rowPointers = (png_bytep*) malloc(sizeof(png_bytep) * imageLength);
for (y=0; y<imageLength; y++)
rowPointers[y] = (png_byte*) malloc(png_get_rowbytes(pngPtr,infoPtr));
png_read_image(pngPtr, rowPointers);
fclose(fp);
}
开发者ID:BeauJoh,项目名称:linear3DSobelPowerFilter,代码行数:69,代码来源:RGBAUtilities.c
示例12: read_image_from_png_file
// ref: http://zarb.org/~gc/html/libpng.html
bool read_image_from_png_file(const char *filepath, image_t &image) {
FILE *fp = fopen(filepath, "rb");
if (!fp) {
log("[read_png_file] File %s could not be opened for reading", filepath);
return false;
}
unsigned char header[8];
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8)) {
log("[read_png_file] File %s is not recognized as a PNG file", filepath);
return false;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
log("[read_png_file] png_create_read_struct failed");
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
log("[read_png_file] png_create_info_struct failed");
return false;
}
if (setjmp(png_jmpbuf(png_ptr))) {
log("[read_png_file] Error during init_io");
return false;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
int width = png_get_image_width(png_ptr, info_ptr);
int height = png_get_image_height(png_ptr, info_ptr);
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
if (setjmp(png_jmpbuf(png_ptr))) {
log("[read_png_file] Error during read_image");
return false;
}
png_byte byte_depth = png_get_channels(png_ptr, info_ptr);
if (bit_depth == 16)
byte_depth *= 2;
png_byte *buf = new png_byte[width * height * byte_depth];
int offset = 0;
for (int y = 0; y < height; y++) {
png_read_row(png_ptr, buf + offset, NULL);
offset += width * byte_depth;
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
image.width = width;
image.height = height;
image.byte_depth = byte_depth;
image.data = (unsigned char *)buf;
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
image.format = GL_LUMINANCE;
break;
case PNG_COLOR_TYPE_RGB:
image.format = GL_RGB;
break;
case PNG_COLOR_TYPE_RGBA:
image.format = GL_RGBA;
break;
case PNG_COLOR_TYPE_GA:
image.format = GL_LUMINANCE_ALPHA;
break;
default:
log("Non-support color type");
return false;
}
return true;
}
开发者ID:ishikawash,项目名称:glfw-example,代码行数:91,代码来源:reflection_demo.cpp
示例13: libaroma_png_ex
/*
* Function : libaroma_png_ex
* Return Value: LIBAROMA_CANVASP
* Descriptions: read png - extended
*/
LIBAROMA_CANVASP libaroma_png_ex(
LIBAROMA_STREAMP stream,
byte freeStream,
byte hicolor) {
if (!stream) {
return NULL;
}
/* init adapter */
_LIBAROMA_PNG_ADAPTERP adapter = (_LIBAROMA_PNG_ADAPTERP)
malloc(sizeof(_LIBAROMA_PNG_ADAPTER));
if (!adapter) {
if (freeStream){
libaroma_stream_close(stream);
}
ALOGW("libaroma_png_ex cannot allocating adapter");
return NULL;
}
adapter->data = stream->data;
adapter->n = stream->size;
adapter->p = 0;
/* png structures */
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
volatile LIBAROMA_CANVASP cv = NULL;
byte header[8];
/* headers */
memcpy(header, adapter->data, sizeof(header));
adapter->p += sizeof(header);
/* compare signature */
if (png_sig_cmp(header, 0, sizeof(header))) {
ALOGW("libaroma_png_ex signature error");
goto exit;
}
/* read structure */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
ALOGW("libaroma_png_ex png_ptr is invalid");
goto exit;
}
/* info structure */
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
ALOGW("libaroma_png_ex info_ptr is invalid");
goto exit;
}
/* jmp */
if (setjmp(png_jmpbuf(png_ptr))) {
ALOGW("libaroma_png_ex jmp error");
goto exit;
}
/* callback */
png_set_read_fn(png_ptr, adapter, _libaroma_png_reader);
png_set_sig_bytes(png_ptr, sizeof(header));
png_read_info(png_ptr, info_ptr);
/* check color */
int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
int color_type = png_get_color_type(png_ptr, info_ptr);
int channels = png_get_channels(png_ptr, info_ptr);
if ((color_type == PNG_COLOR_TYPE_GRAY)||
(color_type == PNG_COLOR_TYPE_GRAY_ALPHA)){
png_set_gray_to_rgb(png_ptr);
png_read_update_info(png_ptr, info_ptr);
}
else if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
png_read_update_info(png_ptr, info_ptr);
}
else 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)
)) {
ALOGW("libaroma_png_ex mode not supported (c:%i, t:%i, d:%i)",
channels, color_type, bit_depth);
goto exit;
}
/* init main info */
int pcv_w = png_get_image_width(png_ptr, info_ptr);
int pcv_h = png_get_image_height(png_ptr, info_ptr);
int pcv_c = png_get_channels(png_ptr, info_ptr);
/* verbose */
//.........这里部分代码省略.........
开发者ID:fzkangga,项目名称:libaroma,代码行数:101,代码来源:png.c
示例14: imagelib_load_png_from_mem
image_t* imagelib_load_png_from_mem( char* buffer, size_t size, const imageloadersettings_t settings )
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if ( !png_ptr ) {
return NULL;
}
png_infop info = png_create_info_struct(png_ptr);
if ( !info ) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
return NULL;
}
if ( setjmp(png_jmpbuf(png_ptr)) ) {
png_destroy_read_struct(&png_ptr, &info, NULL);
return NULL;
}
png_memory_buffer membuffer = {
.data = buffer,
.size = size,
.offset = 0
};
//png_init_io(png_ptr,fp);
png_set_read_fn(png_ptr,&membuffer,png_read_data);
png_set_sig_bytes(png_ptr,0);
int png_transforms = PNG_TRANSFORM_STRIP_16|PNG_TRANSFORM_SWAP_ENDIAN|PNG_TRANSFORM_PACKING|PNG_TRANSFORM_EXPAND|(settings.swaprb?PNG_TRANSFORM_BGR:0);
png_read_png(png_ptr, info, png_transforms, NULL);
int rowbytes = png_get_rowbytes(png_ptr, info);
int width = png_get_image_width(png_ptr, info);
int height = png_get_image_height(png_ptr, info);
int bit_depth = png_get_bit_depth(png_ptr, info);
int color_type = png_get_color_type(png_ptr, info);
// convert bit_depth + color_type to image format
int bytepp = 0;
if ( bit_depth==8 )
{
switch( color_type )
{
case PNG_COLOR_TYPE_GRAY: bytepp = 1; break;
case PNG_COLOR_TYPE_GRAY_ALPHA: bytepp = 2; break;
case PNG_COLOR_TYPE_RGB: bytepp = 3; break;
case PNG_COLOR_TYPE_RGBA: bytepp = 4; break;
}
}
if ( bytepp==0 ) {
png_destroy_read_struct(&png_ptr, &info, NULL);
return NULL;
}
int forcergba = settings.forcergba && color_type!=PNG_COLOR_TYPE_RGBA;
image_t* image = malloc(sizeof(image_t));
image->data = malloc(sizeof(unsigned char)*width*height*(forcergba?4:bytepp));
image->width = width;
image->height = height;
image->bpp = bytepp;
int stride = width*(forcergba?4:bytepp);
png_bytepp row_pointers = png_get_rows(png_ptr, info);
int x,y;
if (!forcergba) {
if (settings.swapy) {
for ( y=0; y<image->height; y++ )
memcpy(image->data+stride*(image->height-1-y), row_pointers[y], rowbytes);
}
else {
for ( y=0; y<image->height; y++ )
memcpy(image->data+stride*y, row_pointers[y], rowbytes);
}
} else {
if (color_type==PNG_COLOR_TYPE_RGB) {
for ( y=0;y<image->height;y++ ) {
unsigned char* src = row_pointers[y];
unsigned char* trg = image->data+stride*y;
for ( x=0; x<image->width; x++ ) {
int tx = x*4;
int sx = x*bytepp;
trg[tx+0] = src[sx+0];
trg[tx+1] = src[sx+1];
trg[tx+2] = src[sx+2];
trg[tx+3] = settings.alpha;
}
}
}
else
//.........这里部分代码省略.........
开发者ID:Santanote,项目名称:apkenv,代码行数:101,代码来源:loadpng.c
示例15: Load
//.........这里部分代码省略.........
png_set_gray_to_rgb(png_ptr);
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
// flip the RGBA pixels to BGRA
png_set_bgr(png_ptr);
#endif
pixel_depth = 32;
break;
default:
throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
}
// unlike the example in the libpng documentation, we have *no* idea where
// this file may have come from--so if it doesn't have a file gamma, don't
// do any correction ("do no harm")
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
double gamma = 0;
double screen_gamma = 2.2;
if (png_get_gAMA(png_ptr, info_ptr, &gamma) && ( flags & PNG_IGNOREGAMMA ) != PNG_IGNOREGAMMA) {
png_set_gamma(png_ptr, screen_gamma, gamma);
}
}
// all transformations have been registered; now update info_ptr data
png_read_update_info(png_ptr, info_ptr);
// color type may have changed, due to our transformations
color_type = png_get_color_type(png_ptr,info_ptr);
// create a DIB and write the bitmap header
// set up the DIB palette, if needed
switch (color_type) {
case PNG_COLOR_TYPE_RGB:
png_set_invert_alpha(png_ptr);
if(image_type == FIT_BITMAP) {
dib = FreeImage_AllocateHeader(header_only, width, height, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth);
}
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
if(image_type == FIT_BITMAP) {
dib = FreeImage_AllocateHeader(header_only, width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth);
}
break;
case PNG_COLOR_TYPE_PALETTE:
dib = FreeImage_AllocateHeader(header_only, width, height, pixel_depth);
png_get_PLTE(
|
请发表评论