【发布时间】:2012-04-18 05:00:25
【问题描述】:
我是 libpng 的新手,文档让我很困惑。 以下是我的代码不起作用,我看不出原因。 有人可以指出我正确的方向吗?或建议不同的(“更简单”)库?
我是如何理解 libpng 的:
在
rb模式下用fopen打开文件用
png_create_read_struct创建png_structp用
png_create_info_struct创建png_infop分配空间
-
读取数据
#include <stdio.h> #include <png.h> int main( int argc, char **argv ) { int x, y; int height, width; png_structp png_ptr; png_infop info_ptr; png_bytep *row_pointers; FILE *fp = fopen( "test.png", "rb"); { if (!fp) printf("File could not be opened for reading"); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); info_ptr = png_create_info_struct(png_ptr); 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); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height); for (y=0; y<height; y++) row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png_ptr,info_ptr)); png_read_image(png_ptr, row_pointers); fclose(fp); } for (y=0; y<height; y++) { png_byte *row = row_pointers[y]; for (x=0; x<width; x++) { png_byte* ptr = &(row[x*4]); printf("Pixel at position [ %d - %d ] has RGBA values: %d - %d - %d - %d\n", x, y, ptr[0], ptr[1], ptr[2], ptr[3]); } } }
【问题讨论】:
-
png_create_read_struct失败。编译器不写任何错误它什么都不做 -
怎么样?你必须更具体。
-
我用
if (!png_ptr) printf("png_create_read_struct failed");看看有什么问题。 -
显然无法分配结构。您使用的是什么编译器/等?
-
VS 2008 express,会不会是错误的include libpng文件到编译器造成的?我遇到了一点麻烦。