本文整理汇总了C++中png_get_rowbytes函数的典型用法代码示例。如果您正苦于以下问题:C++ png_get_rowbytes函数的具体用法?C++ png_get_rowbytes怎么用?C++ png_get_rowbytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_get_rowbytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: load_png
static FtkBitmap* load_png (const char *filename)
{
FtkColor bg = {0};
unsigned int x = 0;
unsigned int y = 0;
png_byte bit_depth = 0;
unsigned int width = 0;
unsigned int height = 0;
int number_of_passes = 0;
png_byte color_type = 0;
png_infop info_ptr = NULL;
png_structp png_ptr = NULL;
png_bytep * row_pointers = NULL;
char header[8]; // 8 is the maximum size that can be checked
int rowbytes = 0;
FtkColor* dst = NULL;
unsigned char* src = NULL;
FtkBitmap* bitmap = NULL;
FILE *fp = fopen(filename, "rb");
return_val_if_fail(fp, NULL);
bg.a = 0xff;
fread(header, 1, 8, fp);
return_val_if_fail(png_sig_cmp(header, 0, 8) == 0, NULL);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
return_val_if_fail(png_ptr, NULL);
info_ptr = png_create_info_struct(png_ptr);
return_val_if_fail(info_ptr, NULL);
return_val_if_fail(setjmp(png_jmpbuf(png_ptr)) == 0, NULL);
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
return_val_if_fail(setjmp(png_jmpbuf(png_ptr)) == 0, NULL);
rowbytes = png_get_rowbytes(png_ptr,info_ptr);
row_pointers = (png_bytep*) FTK_ZALLOC(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
{
row_pointers[y] = (png_byte*) FTK_ZALLOC(rowbytes);
}
png_read_image(png_ptr, row_pointers);
fclose(fp);
bitmap = ftk_bitmap_create(width, height, bg);
dst = ftk_bitmap_lock(bitmap);
if (color_type == PNG_COLOR_TYPE_RGBA)
{
unsigned int w = width;
unsigned int h = height;
for(y = 0; y < h; y++)
{
src = row_pointers[y];
for(x = 0; x < w; x++)
{
if(src[3])
{
dst->r = src[0];
dst->g = src[1];
dst->b = src[2];
dst->a = src[3];
}
else
{
dst->r = 0xff;
dst->g = 0xff;
dst->b = 0xff;
dst->a = 0;
}
src +=4;
dst++;
}
}
}
else if(color_type == PNG_COLOR_TYPE_RGB)
{
unsigned int w = width;
unsigned int h = height;
for(y = 0; y < h; y++)
//.........这里部分代码省略.........
示例2: readpng_get_image
png_bytep readpng_get_image(png_structp* png_ptr, png_infop* info_ptr, png_infop* end_ptr)
{
//test for
if(setjmp(png_jmpbuf(*png_ptr)))
{
png_destroy_read_struct(png_ptr, info_ptr, NULL);
std::cerr << "error during setjmp" << std::endl;
return NULL;
}
png_uint_32 width, height;
int bit_depth, color_type;
png_uint_32 numrowbytes;
png_bytep dataBlock;
// gamma correction start (optional)
double display_exponent = 2.2; //standard in most systems + standard in imageprocessing
int envGamma = 0;
if(envGamma)
display_exponent = (double)envGamma;
double gamma;
if(png_get_gAMA(*png_ptr, *info_ptr, &gamma))
png_set_gamma(*png_ptr, display_exponent, gamma);
// gamma correction end
png_get_IHDR(*png_ptr, *info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
//transform the png to a standard format
if(color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(*png_ptr);
if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand(*png_ptr);
if(png_get_valid(*png_ptr, *info_ptr, PNG_INFO_tRNS))
png_set_expand(*png_ptr);
if(bit_depth == 16)
png_set_strip_16(*png_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);
//end
//proccessing for the dataBlock (pixeldata)
numrowbytes = png_get_rowbytes(*png_ptr, *info_ptr);
png_bytep row_pointers[height];
dataBlock = (png_bytep)malloc(sizeof(png_byte)*numrowbytes*height);
for(png_uint_32 i = 0; i<height; i++)
row_pointers[i] = dataBlock + i*numrowbytes;
png_read_image(*png_ptr, row_pointers);
//end
//optional reading of end in end_ptr and test for consistence
png_read_end(*png_ptr, NULL);
return dataBlock;
}
示例3: spng_read
//.........这里部分代码省略.........
png_set_bgr(png_ptr);
// Convert palette to RGB
if (color_type == PNG_COLOR_TYPE_PALETTE) {
printfdf("(\"%s\"): Converting palette PNG to RGB.", path);
png_set_palette_to_rgb(png_ptr);
color_type = PNG_COLOR_TYPE_RGB;
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
printfdf("(\"%s\"): Converting rDNS to full alpha.", path);
png_set_tRNS_to_alpha(png_ptr);
}
if (color_type == PNG_COLOR_TYPE_GRAY
|| PNG_COLOR_TYPE_GRAY_ALPHA == color_type) {
printfdf("(\"%s\"): Converting gray (+ alpha) PNG to RGB.", path);
png_set_gray_to_rgb(png_ptr);
if (PNG_COLOR_TYPE_GRAY == color_type)
color_type = PNG_COLOR_TYPE_RGB;
else
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
}
/*
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
printfdf("(\"%s\"): Converting 1/2/4 bit gray PNG to 8-bit.", path);
#if PNG_LIBPNG_VER >= 10209
png_set_expand_gray_1_2_4_to_8(png_ptr);
#else
png_set_gray_1_2_4_to_8(png_ptr);
#endif
bit_depth = 8;
}
*/
// Somehow XImage requires 24-bit visual to use 32 bits per pixel
if (color_type == PNG_COLOR_TYPE_RGB) {
printfdf("(\"%s\"): Appending filler alpha values.", path);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
}
// Premultiply alpha
if (PNG_COLOR_TYPE_RGB_ALPHA == color_type) {
#if PNG_LIBPNG_VER >= 10504
png_set_alpha_mode(png_ptr, PNG_ALPHA_STANDARD, 1.0);
#else
need_premultiply = true;
#endif
}
/*
int number_passes = 1;
#ifdef PNG_READ_INTERLACING_SUPPORTED
number_passes = png_set_interlace_handling(png_ptr);
#endif */
if (PNG_INTERLACE_NONE != interlace_type)
png_set_interlace_handling(png_ptr);
}
png_read_update_info(png_ptr, info_ptr);
int depth = 0;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
switch (color_type) {
case PNG_COLOR_TYPE_GRAY: depth = 1 * bit_depth; break;
case PNG_COLOR_TYPE_RGB: depth = 3 * bit_depth; break;
case PNG_COLOR_TYPE_RGB_ALPHA: depth = 4 * bit_depth; break;
default: assert(0); break;
}
// Read data and fill to Picture
{
int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
png_bytep row_pointers[height];
memset(row_pointers, 0, sizeof(row_pointers));
row_pointers[0] = png_malloc(png_ptr, rowbytes * height);
for (int row = 1; row < height; row++)
row_pointers[row] = row_pointers[row - 1] + rowbytes;
png_read_image(png_ptr, row_pointers);
if (need_premultiply)
for (int row = 0; row < height; row++)
simg_data32_premultiply(row_pointers[row], width);
if (unlikely(!(pictw = simg_data_to_pictw(ps, width, height, depth,
row_pointers[0], rowbytes)))) {
printfef("(\"%s\"): Failed to create Picture.", path);
goto spng_read_end;
}
}
spng_read_end:
if (png_ptr)
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if (fp)
fclose(fp);
return pictw;
}
示例4: loadpng
//.........这里部分代码省略.........
}
end_info = png_create_info_struct (png_ptr);
if (!end_info) {
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
fprintf (stderr, "Read error 2\n");
return 1;
}
/* CHARGEMENT DE L'IMAGE */
if (setjmp (png_ptr->jmpbuf)) {
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
fclose (fp);
fprintf (stderr, "Erreur de chargement\n");
return 1;
}
png_init_io (png_ptr, fp);
png_set_read_status_fn (png_ptr, NULL);
png_read_info (png_ptr, info_ptr);
png_get_IHDR (png_ptr, info_ptr, &width, &height,
&bit_depth, &color_type, &interlace_type,
&compression_type, &filter_type);
/*
printf ("taille : %dx%d\n",width,height);
printf ("depth : %d\n",bit_depth);
printf ("color type : ");
switch (color_type) {
case PNG_COLOR_TYPE_GRAY:
printf ("PNG_COLOR_TYPE_GRAY (bit depths 1, 2, 4, 8, 16)\n");
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
printf ("PNG_COLOR_TYPE_GRAY_ALPHA (bit depths 8, 16)\n");
break;
case PNG_COLOR_TYPE_PALETTE:
printf ("PNG_COLOR_TYPE_PALETTE (bit depths 1, 2, 4, 8)\n");
break;
case PNG_COLOR_TYPE_RGB:
printf ("PNG_COLOR_TYPE_RGB (bit_depths 8, 16)\n");
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
printf ("PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16)\n");
break;
}
*/
// printf ("PNG_COLOR_MASK_ALPHA : %x\n", PNG_COLOR_MASK_ALPHA);
// printf ("PNG_COLOR_MASK_COLOR : %x\n", PNG_COLOR_MASK_COLOR);
// printf ("PNG_COLOR_MASK_PALETTE : %x\n", PNG_COLOR_MASK_PALETTE);
if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
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);
else if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb (png_ptr);
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha (png_ptr);
png_read_update_info (png_ptr, info_ptr);
// printf ("channels : %d\n", png_get_channels (png_ptr, info_ptr));
rowbytes = png_get_rowbytes (png_ptr, info_ptr);
// printf ("rowbytes : %d\n", rowbytes);
row_pointers = (unsigned int **) malloc (height * sizeof (unsigned int *));
for (y = 0; y < height; y++)
row_pointers[y] = (unsigned int *) malloc (4 * width);
png_read_image (png_ptr, (png_bytepp) row_pointers);
// for (y=0;y<height;y++) {
// for (x=0;x<width;x++) {
// if (row_pointers[y][x] & 0xf000)
// printf ("%x ",(((unsigned int**)row_pointers)[y][x])&0xf);
// else
// printf (" ");
// }
// printf ("\n");
// }
png_read_end (png_ptr, end_info);
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
(*buf) = (unsigned int **) malloc (height * sizeof (void *));
for (y = 0; y < height; y++) {
(*buf)[y] = (unsigned int *) malloc (width * 4);
for (x = 0; x < width; x++)
(*buf)[y][x] = row_pointers[y][x];
}
*w = width;
*h = height;
return 0;
}
示例5: png_texture_load
//.........这里部分代码省略.........
// 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);
if (width){ *width = temp_width; }
if (height){ *height = temp_height; }
//printf("%s: %lux%lu %d\n", file_name, temp_width, temp_height, color_type);
if (bit_depth != 8)
{
fprintf(stderr, "%s: Unsupported bit depth %d. Must be 8.\n", file_name, bit_depth);
return TextureReturn();
}
GLint format;
switch(color_type)
{
case PNG_COLOR_TYPE_RGB:
format = GL_RGB;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
format = GL_RGBA;
break;
default:
fprintf(stderr, "%s: Unknown libpng color type %d.\n", file_name, color_type);
return TextureReturn();
}
// 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);
// glTexImage2d requires rows to be 4-byte aligned
rowbytes += 3 - ((rowbytes-1) % 4);
// Allocate the image_data as a big block, to be given to opengl
png_byte * image_data = (png_byte *)malloc(rowbytes * temp_height * sizeof(png_byte)+15);
if (image_data == NULL)
{
fprintf(stderr, "error: could not allocate memory for PNG image data\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return TextureReturn();
}
// row_pointers is for pointing to image_data for reading the png with libpng
png_byte ** row_pointers = (png_byte **)malloc(temp_height * sizeof(png_byte *));
if (row_pointers == NULL)
{
fprintf(stderr, "error: could not allocate memory for PNG row pointers\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
free(image_data);
fclose(fp);
return TextureReturn();
}
// set the individual row_pointers to point at the correct offsets of image_data
for (unsigned int i = 0; i < temp_height; i++)
{
row_pointers[temp_height - 1 - i] = image_data + i * rowbytes;
}
// read the png into image_data through row_pointers
png_read_image(png_ptr, row_pointers);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
*width = temp_width;
*height = temp_height;
TextureReturn thing;
thing.format = format;
thing.pixels = image_data;
return thing;/*
// Generate the OpenGL texture object
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, format, temp_width, temp_height, 0, format, GL_UNSIGNED_BYTE, image_data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// clean up
free(row_pointers);
return texture;*/
}
示例6: image_png_load
int image_png_load(MediaScanImage *i) {
int bit_depth, color_type, num_passes, x, y;
int ofs;
volatile unsigned char *ptr = NULL; // volatile = won't be rolled back if longjmp is called
PNGData *p = (PNGData *)i->_png;
if (setjmp(png_jmpbuf(p->png_ptr))) {
if (ptr != NULL)
free((void *)ptr);
image_png_destroy(i);
return 0;
}
// XXX If reusing the object a second time, we need to completely create a new png struct
bit_depth = png_get_bit_depth(p->png_ptr, p->info_ptr);
color_type = png_get_color_type(p->png_ptr, p->info_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_expand(p->png_ptr); // png_set_palette_to_rgb(p->png_ptr);
i->channels = 4;
}
else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand(p->png_ptr); // png_set_expand_gray_1_2_4_to_8(p->png_ptr);
else if (png_get_valid(p->png_ptr, p->info_ptr, PNG_INFO_tRNS))
png_set_expand(p->png_ptr); // png_set_tRNS_to_alpha(p->png_ptr);
if (bit_depth == 16)
png_set_strip_16(p->png_ptr);
else if (bit_depth < 8)
png_set_packing(p->png_ptr);
// Make non-alpha RGB/Palette 32-bit and Gray 16-bit for easier handling
if (!(color_type & PNG_COLOR_MASK_ALPHA)) {
png_set_add_alpha(p->png_ptr, 0xFF, PNG_FILLER_AFTER);
}
num_passes = png_set_interlace_handling(p->png_ptr);
LOG_DEBUG("png bit_depth %d, color_type %d, channels %d, num_passes %d\n",
bit_depth, color_type, i->channels, num_passes);
png_read_update_info(p->png_ptr, p->info_ptr);
image_alloc_pixbuf(i, i->width, i->height);
ptr = (unsigned char *)malloc(png_get_rowbytes(p->png_ptr, p->info_ptr));
ofs = 0;
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { // Grayscale (Alpha)
if (num_passes == 1) { // Non-interlaced
for (y = 0; y < i->height; y++) {
png_read_row(p->png_ptr, (unsigned char *)ptr, NULL);
for (x = 0; x < i->width; x++) {
i->_pixbuf[ofs++] = COL_FULL(ptr[x * 2], ptr[x * 2], ptr[x * 2], ptr[x * 2 + 1]);
}
}
}
else if (num_passes == 7) { // Interlaced
image_png_interlace_pass_gray(i, (unsigned char *)ptr, 0, 8, 0, 8);
image_png_interlace_pass_gray(i, (unsigned char *)ptr, 0, 8, 4, 8);
image_png_interlace_pass_gray(i, (unsigned char *)ptr, 4, 8, 0, 4);
image_png_interlace_pass_gray(i, (unsigned char *)ptr, 0, 4, 2, 4);
image_png_interlace_pass_gray(i, (unsigned char *)ptr, 2, 4, 0, 2);
image_png_interlace_pass_gray(i, (unsigned char *)ptr, 0, 2, 1, 2);
image_png_interlace_pass_gray(i, (unsigned char *)ptr, 1, 2, 0, 1);
}
}
else { // RGB(A)
if (num_passes == 1) { // Non-interlaced
for (y = 0; y < i->height; y++) {
png_read_row(p->png_ptr, (unsigned char *)ptr, NULL);
for (x = 0; x < i->width; x++) {
i->_pixbuf[ofs++] = COL_FULL(ptr[x * 4], ptr[x * 4 + 1], ptr[x * 4 + 2], ptr[x * 4 + 3]);
}
}
}
else if (num_passes == 7) { // Interlaced
// The first pass will return an image 1/8 as wide as the entire image
// (every 8th column starting in column 0)
// and 1/8 as high as the original (every 8th row starting in row 0)
image_png_interlace_pass(i, (unsigned char *)ptr, 0, 8, 0, 8);
// The second will be 1/8 as wide (starting in column 4)
// and 1/8 as high (also starting in row 0)
image_png_interlace_pass(i, (unsigned char *)ptr, 0, 8, 4, 8);
// The third pass will be 1/4 as wide (every 4th pixel starting in column 0)
// and 1/8 as high (every 8th row starting in row 4)
image_png_interlace_pass(i, (unsigned char *)ptr, 4, 8, 0, 4);
// The fourth pass will be 1/4 as wide and 1/4 as high
// (every 4th column starting in column 2, and every 4th row starting in row 0)
image_png_interlace_pass(i, (unsigned char *)ptr, 0, 4, 2, 4);
// The fifth pass will return an image 1/2 as wide,
// and 1/4 as high (starting at column 0 and row 2)
image_png_interlace_pass(i, (unsigned char *)ptr, 2, 4, 0, 2);
//.........这里部分代码省略.........
示例7: pngLoadRawF
int APIENTRY pngLoadRawF(FILE *fp, pngRawInfo *pinfo) {
unsigned char header[8];
png_structp png;
png_infop info;
png_infop endinfo;
png_bytep data;
png_bytep *row_p;
double fileGamma;
png_uint_32 width, height;
int depth, color;
png_uint_32 i;
if (pinfo == NULL) return 0;
fread(header, 1, 8, fp);
if (!png_check_sig(header, 8)) return 0;
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info = png_create_info_struct(png);
endinfo = png_create_info_struct(png);
// DH: added following lines
if (setjmp(png->jmpbuf))
{
png_destroy_read_struct(&png, &info, &endinfo);
return 0;
}
// ~DH
png_init_io(png, fp);
png_set_sig_bytes(png, 8);
png_read_info(png, info);
png_get_IHDR(png, info, &width, &height, &depth, &color, NULL, NULL, NULL);
pinfo->Width = width;
pinfo->Height = height;
pinfo->Depth = depth;
/*--GAMMA--*/
checkForGammaEnv();
if (png_get_gAMA(png, info, &fileGamma))
png_set_gamma(png, screenGamma, fileGamma);
else
png_set_gamma(png, screenGamma, 1.0/2.2);
png_read_update_info(png, info);
data = (png_bytep) malloc(png_get_rowbytes(png, info)*height);
row_p = (png_bytep *) malloc(sizeof(png_bytep)*height);
for (i = 0; i < height; i++) {
if (StandardOrientation)
row_p[height - 1 - i] = &data[png_get_rowbytes(png, info)*i];
else
row_p[i] = &data[png_get_rowbytes(png, info)*i];
}
png_read_image(png, row_p);
free(row_p);
if (color == PNG_COLOR_TYPE_PALETTE) {
int cols;
png_get_PLTE(png, info, (png_colorp *) &pinfo->Palette, &cols);
}
else {
pinfo->Palette = NULL;
}
if (color&PNG_COLOR_MASK_ALPHA) {
if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA)
pinfo->Components = 2;
else
pinfo->Components = 4;
pinfo->Alpha = 8;
}
else {
if (color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY)
pinfo->Components = 1;
else
pinfo->Components = 3;
pinfo->Alpha = 0;
}
pinfo->Data = data;
png_read_end(png, endinfo);
png_destroy_read_struct(&png, &info, &endinfo);
return 1;
}
示例8: npng_pitch
/**
* @brief Gets the pitch of a png.
*/
int npng_pitch( npng_t *npng )
{
/* Make sure loaded info. */
return png_get_rowbytes( npng->png_ptr, npng->info_ptr );
}
示例9: fclose
void PngReader::read(unsigned x0, unsigned y0,ImageData32& image)
{
FILE *fp=fopen(fileName_.c_str(),"r");
if (!fp) throw ImageReaderException("cannot open image file "+fileName_);
png_structp png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING,0,0,0);
if (!png_ptr)
{
fclose(fp);
throw ImageReaderException("failed to allocate png_ptr");
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr,0,0);
fclose(fp);
throw ImageReaderException("failed to create info_ptr");
}
png_init_io(png_ptr, fp);
png_read_info(png_ptr, info_ptr);
if (color_type_ == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
if (bit_depth_ == 16)
png_set_strip_16(png_ptr);
if (color_type_ == PNG_COLOR_TYPE_GRAY ||
color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
// quick hack -- only work in >=libpng 1.2.7
png_set_add_alpha(png_ptr,1,1);
double gamma;
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
png_set_gamma(png_ptr, 2.2, gamma);
png_read_update_info(png_ptr, info_ptr);
//START read image rows
unsigned w=std::min((unsigned)image.width(),width_);
unsigned h=std::min((unsigned)image.height(),height_);
unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
unsigned char* row= new unsigned char[rowbytes];
for (unsigned i=0; i<height_; ++i)
{
png_read_row(png_ptr,row,0);
if (i>=y0 && i<h)
{
image.setRow(i-y0,(unsigned*) &row[x0],w);
}
}
//END
delete [] row;
png_read_end(png_ptr,0);
png_destroy_read_struct(&png_ptr, &info_ptr,0);
fclose(fp);
}
示例10: png_create_info_struct
uint8_t* ImageDecoder::decodePNGImpl(png_structp pngPtr, uint32_t* width, uint32_t* height, bool* hasAlpha)
{
png_bytep* rowPtrs = NULL;
uint8_t* outData = NULL;
png_infop infoPtr = png_create_info_struct(pngPtr);
if (!infoPtr)
{
LOG(LOG_ERROR,"Couldn't initialize png info struct");
png_destroy_read_struct(&pngPtr, (png_infopp)0, (png_infopp)0);
return NULL;
}
if (setjmp(png_jmpbuf(pngPtr)))
{
png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0);
if (rowPtrs != NULL) delete [] rowPtrs;
if (outData != NULL) delete [] outData;
LOG(LOG_ERROR,"error during reading of the png file");
return NULL;
}
png_read_info(pngPtr, infoPtr);
*width = png_get_image_width(pngPtr, infoPtr);
*height = png_get_image_height(pngPtr, infoPtr);
//bits per CHANNEL! note: not per pixel!
png_uint_32 bitdepth = png_get_bit_depth(pngPtr, infoPtr);
//Number of channels
png_uint_32 channels = png_get_channels(pngPtr, infoPtr);
//Color type. (RGB, RGBA, Luminance, luminance alpha... palette... etc)
png_uint_32 color_type = png_get_color_type(pngPtr, infoPtr);
// Transform everything into 24 bit RGB
switch (color_type)
{
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(pngPtr);
break;
case PNG_COLOR_TYPE_GRAY:
if (bitdepth < 8)
png_set_gray_to_rgb(pngPtr);
break;
}
if (bitdepth == 16)
{
png_set_strip_16(pngPtr);
}
*hasAlpha = (channels > 3);
// Update the infoPtr to reflect the transformations set
// above. Read new values by calling png_get_* again.
png_read_update_info(pngPtr, infoPtr);
//bitdepth = png_get_bit_depth(pngPtr, infoPtr);
//color_type = png_get_color_type(pngPtr, infoPtr);
const unsigned int stride = png_get_rowbytes(pngPtr, infoPtr);
outData = new uint8_t[(*height) * stride];
rowPtrs = new png_bytep[(*height)];
for (size_t i = 0; i < (*height); i++)
{
rowPtrs[i] = (png_bytep)outData + i* stride;
}
png_read_image(pngPtr, rowPtrs);
png_read_end(pngPtr, NULL);
png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0);
delete[] (png_bytep)rowPtrs;
return outData;
}
示例11: fopen
char* Model::load_png(std::string fileName, int *width, int *height) {
fileName = "Resources/" + fileName;
FILE *png_file = fopen(fileName.c_str(), "rb");
//assert(png_file.open);
uint8_t header[PNG_SIG_BYTES];
fread(header, 1, PNG_SIG_BYTES, png_file);
assert(!png_sig_cmp(header, 0, PNG_SIG_BYTES));
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
assert(png_ptr);
png_infop info_ptr = png_create_info_struct(png_ptr);
assert(info_ptr);
png_infop end_info = png_create_info_struct(png_ptr);
assert(end_info);
assert(!setjmp(png_jmpbuf(png_ptr)));
png_init_io(png_ptr, png_file);
png_set_sig_bytes(png_ptr, PNG_SIG_BYTES);
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_uint_32 bit_depth, color_type;
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
// if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
// png_set_gray_1_2_4_to_8(png_ptr);
if (bit_depth == 16)
png_set_strip_16(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
else if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
else
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
png_read_update_info(png_ptr, info_ptr);
png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
png_uint_32 numbytes = rowbytes * (*height);
png_byte* pixels = (png_byte*) malloc(numbytes);
png_byte** row_ptrs = (png_byte**) malloc((*height) * sizeof (png_byte*));
int i;
for (i = 0; i < (*height); i++)
row_ptrs[i] = pixels + ((*height) - 1 - i) * rowbytes;
png_read_image(png_ptr, row_ptrs);
free(row_ptrs);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(png_file);
return (char *) pixels;
}
示例12: LoadPNGJPGFromMemory
unsigned char* LoadPNGJPGFromMemory(const unsigned char* buffer, int len, int* width, int* height)
{
const int number=8;
// проверяем сигнатуру файла (первые number байт)
if ( !png_check_sig((png_bytep)buffer, number) )
{
// неизвестный формат
return LoadJPGWithAlphaFromMemory(buffer, len, width, height);
}
// создаем внутреннюю структуру png для работы с файлом
// последние параметры - структура, для функции обработки ошибок и варнинга (последн. 2 параметра)
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
// создаем структуру с информацией о файле
png_infop info_ptr = png_create_info_struct(png_ptr);
PNGBuffer pngBuffer;
pngBuffer.data = (png_bytep)buffer;
pngBuffer.position = 8;
png_set_read_fn(png_ptr, (void*)&pngBuffer, PNGRead);
// говорим библиотеке, что мы уже прочли number байт, когда проверяли сигнатуру
png_set_sig_bytes(png_ptr, number);
// читаем всю информацию о файле
png_read_info(png_ptr, info_ptr);
// Эта функция возвращает инфу из info_ptr
png_uint_32 w = 0, h = 0; // размер картинки в пикселях
int bit_depth = 0; // глубина цвета (одного из каналов, может быть 1, 2, 4, 8, 16)
int color_type = 0; // описывает какие каналы присутствуют:
// PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE,
// PNG_COLOR_TYPE_RGB, PNG_COLOR_TYPE_RGB_ALPHA...
// последние 3 параметра могут быть нулями и обозначают: тип фильтра, тип компрессии и тип смещения
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
// png формат может содержать 16 бит на канал, но нам нужно только 8, поэтому сужаем канал
if (bit_depth == 16) png_set_strip_16(png_ptr);
// преобразуем файл если он содержит палитру в нормальный RGB
if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8) png_set_palette_to_rgb(png_ptr);
// если в грэйскейле меньше бит на канал чем 8, то конвертим к нормальному 8-битному
//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 (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
double gamma = 0.0f;
// если есть информация о гамме в файле, то устанавливаем на 2.2
if ( png_get_gAMA(png_ptr, info_ptr, &gamma) ) png_set_gamma(png_ptr, 2.2, gamma);
// иначе ставим дефолтную гамму для файла в 0.45455 (good guess for GIF images on PCs)
else png_set_gamma(png_ptr, 2.2, 0.45455);
// после всех трансформаций, апдейтим информацию в библиотеке
png_read_update_info(png_ptr, info_ptr);
// опять получаем все размеры и параметры обновленной картинки
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
// определяем кол-во байт нужных для того чтобы вместить строку
png_uint_32 row_bytes = png_get_rowbytes(png_ptr, info_ptr);
// теперь, мы можем выделить память чтобы вместить картинку
png_byte* data = new png_byte[row_bytes * h];
// выделяем память, для указателей на каждую строку
png_byte **row_pointers = new png_byte * [h];
// сопоставляем массив указателей на строчки, с выделенными в памяти (res)
// т.к. изображение перевернутое, то указатели идут снизу вверх
for (unsigned int i = 0; i < h; i++)
row_pointers[i] = data + i * row_bytes;
// все, читаем картинку
png_read_image(png_ptr, row_pointers);
// освобождаем память от указателей на строки
delete []row_pointers;
// освобождаем память выделенную для библиотеки libpng
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
*width=w;
*height=h;
return data;
}
示例13: ReadPNG
//.........这里部分代码省略.........
/* read all PNG data up to the image data */
png_read_info( png_ptr, info_ptr );
/* extract the data we need to form the HBITMAP from the PNG header */
png_get_IHDR( png_ptr, info_ptr, &Width, &Height, &BitDepth, &ColorType,
&InterlaceType, NULL, NULL);
img->width = (unsigned int) Width;
img->height = (unsigned int) Height;
img->bits_per_pixel = (unsigned char)32;
img->scan_width = Width * 4;
/* convert 16-bit images to 8-bit images */
if (BitDepth == 16)
png_set_strip_16(png_ptr);
/* These are not really required per Rice format spec,
* but is done just in case someone uses them.
*/
/* convert palette color to rgb color */
if (ColorType == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
ColorType = PNG_COLOR_TYPE_RGB;
}
/* expand 1,2,4 bit gray scale to 8 bit gray scale */
if (ColorType == PNG_COLOR_TYPE_GRAY && BitDepth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
/* convert gray scale or gray scale + alpha to rgb color */
if (ColorType == PNG_COLOR_TYPE_GRAY ||
ColorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(png_ptr);
ColorType = PNG_COLOR_TYPE_RGB;
}
/* add alpha channel if any */
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
ColorType = PNG_COLOR_TYPE_RGB_ALPHA;
}
/* convert rgb to rgba */
if (ColorType == PNG_COLOR_TYPE_RGB) {
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
ColorType = PNG_COLOR_TYPE_RGB_ALPHA;
}
png_set_bgr(png_ptr);
/* set the background color if one is found */
if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD) )
png_get_bKGD(png_ptr, info_ptr, &ImageBackground);
/* get the transparent color if one is there */
if ( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) )
png_get_tRNS( png_ptr, info_ptr, &trns, &NumTrans, &TransColors );
img->palette_size = (unsigned short)0;
img->bytes_per_palette_entry = 4U;
tmp = AllocateBMGImage( img );
if ( tmp != BMG_OK )
longjmp( err_jmp, (int)tmp );
png_read_update_info( png_ptr, info_ptr );
/* create buffer to read data to */
rows = (unsigned char **)malloc(Height*sizeof(unsigned char *));
if ( !rows )
longjmp( err_jmp, (int)errMemoryAllocation );
k = png_get_rowbytes( png_ptr, info_ptr );
rows[0] = (unsigned char *)malloc( Height*k*sizeof(char));
if ( !rows[0] )
longjmp( err_jmp, (int)errMemoryAllocation );
for ( i = 1; i < (int)Height; i++ )
rows[i] = rows[i-1] + k;
/* read the entire image into rows */
png_read_image( png_ptr, rows );
bits = img->bits + (Height - 1) * img->scan_width;
for ( i = 0; i < (int)Height; i++ )
{
memcpy(bits, rows[i], 4*Width);
bits -= img->scan_width;
}
free( rows[0] );
free( rows );
png_read_end( png_ptr, info_ptr );
png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
fclose( file );
return BMG_OK;
}
示例14: png_create_read_struct
int PngDecoder::validate()
{
if (png_sig_cmp((png_bytep)buf->data(), 0, PNG_HEADER_SIZE)) {
std::cout << "[PngDecoder] error: %s is not a PNG." << std::endl;
return -1;
}
buf->drain(PNG_HEADER_SIZE);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
std::cout << "[PngDecoder] error: png_create_read_struct returned 0." << std::endl;
return -1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
std::cout << "[PngDecoder] error: png_create_info_struct returned 0." << std::endl;
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return -1;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
std::cout << "[PngDecoder] error: png_create_info_struct returned 0." << std::endl;
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
return -1;
}
if (setjmp(png_jmpbuf(png_ptr))) {
std::cout << "[PngDecoder] error from libpng" << std::endl;
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return -1;
}
png_set_read_fn(png_ptr, buf, pngDecoderReadFunction);
png_set_sig_bytes(png_ptr, PNG_HEADER_SIZE);
png_read_info(png_ptr, info_ptr);
png_set_expand(png_ptr);
png_set_strip_16(png_ptr);
png_set_gray_to_rgb(png_ptr);
png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
imgInfo.width = png_get_image_width(png_ptr, info_ptr);
imgInfo.height = png_get_image_height(png_ptr, info_ptr);
imgInfo.color_type = png_get_color_type(png_ptr, info_ptr);
imgInfo.channels = png_get_channels(png_ptr, info_ptr);
imgInfo.bit_depth = png_get_bit_depth(png_ptr, info_ptr);
imgInfo.interlance_type = png_get_interlace_type(png_ptr, info_ptr);
imgInfo.rowbytes = png_get_rowbytes(png_ptr, info_ptr);
#ifdef _PNGDECODER_DEBUG
std::cout << "[PngDecoder] Extracted w=" << imgInfo.width
<< " h=" << imgInfo.height
<< " color_type=" << imgInfo.color_type
<< " bit_depth="<< imgInfo.bit_depth
<< " rowbytes="<< imgInfo.rowbytes
<< " channels="<< imgInfo.channels
<< " interlance_type="<< imgInfo.interlance_type
<< std::endl;
#endif
if (imgInfo.bit_depth != 8) {
std::cout << "[PngDecoder] error: Unsupported bit depth=" << imgInfo.bit_depth <<". Must be 8." << std::endl;
return -1;
}
switch(imgInfo.color_type) {
case PNG_COLOR_TYPE_RGB:
imgInfo.format = GL_RGB;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
imgInfo.format = GL_RGBA;
break;
case PNG_COLOR_TYPE_PALETTE:
if (imgInfo.color_type & PNG_COLOR_MASK_ALPHA) {
imgInfo.format = GL_RGBA;
} else if (imgInfo.color_type & PNG_COLOR_MASK_COLOR) {
imgInfo.format = GL_RGB;
}
break;
default:
std::cout << "[PngDecoder] error: unknown libpng color type=" << imgInfo.color_type << " rgb=" << PNG_COLOR_TYPE_RGB << " rgba=" << PNG_COLOR_TYPE_RGBA << " palette=" << PNG_COLOR_TYPE_PALETTE << std::endl;
return -1;
}
return 0;
}
示例15: fh_png_load
int fh_png_load(const char *name,unsigned char **buffer,int* /*xp*/,int* /*yp*/)
{
static const png_color_16 my_background = {0, 0, 0, 0, 0};
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
unsigned int i;
int bit_depth, color_type, interlace_type;
int number_passes,pass;
png_byte * fbptr;
FILE * fh;
if(!(fh=fopen(name,"rb"))) return(FH_ERROR_FILE);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if(png_ptr == NULL) {
fclose(fh);
return(FH_ERROR_FORMAT);
}
info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
if(setjmp(png_ptr->jmpbuf))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
png_init_io(png_ptr,fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,&interlace_type, NULL, NULL);
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
png_set_background(png_ptr, (png_color_16*)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
/* other possibility for png_set_background: use png_get_bKGD */
}
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(png_ptr);
png_set_background(png_ptr, (png_color_16*)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
}
/* this test does not trigger for 8bit-paletted PNGs with newer libpng (1.2.36 at least),
but the data delivered is with alpha channel anyway, so always strip alpha for now
*/
#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR <= 2 && PNG_LIBPNG_VER_RELEASE < 36
if (color_type & PNG_COLOR_MASK_ALPHA)
#endif
png_set_strip_alpha(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
if (bit_depth == 16)
png_set_strip_16(png_ptr);
/* on Intel PC ?:
if (bit_depth == 16)
png_set_swap(png_ptr);
*/
number_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr,info_ptr);
if (width * 3 != png_get_rowbytes(png_ptr, info_ptr))
{
printf("[png.cpp]: Error processing %s - please report (including image).\n", name);
printf(" width: %lu rowbytes: %lu\n", width, png_get_rowbytes(png_ptr, info_ptr));
fclose(fh);
return(FH_ERROR_FORMAT);
}
for(pass = 0; pass < number_passes; pass++)
{
fbptr = (png_byte *)(*buffer);
for (i = 0; i < height; i++, fbptr += width * 3)
{
png_read_row(png_ptr, fbptr, NULL);
}
}
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_OK);
}