我是libpng的新手,文檔對我來說真的很讓人困惑。 下面是我的代碼不工作,我不明白爲什麼。 有人可以指向我正確的方向嗎?或建議不同的(「更容易」)圖書館?使用libpng無法讀取png文件
如何我理解的libpng:
在
rb
模式打開與fopen
文件創建
png_structp
與png_create_read_struct
創建
png_infop
與png_create_info_struct
分配空間
讀取數據
#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]); } } }
什麼不工作? – user7116 2012-04-04 13:42:07
'png_create_read_struct'失敗。 compilator不寫任何錯誤,它只是無所事事 – user1281071 2012-04-04 13:47:54
如何如此?你必須更具體。 – user7116 2012-04-04 13:48:21