2013-05-26 73 views
0

我一直在努力讓libpng工作在我的opengl C++程序上。我正在嘗試加載png作爲紋理。我已經下載了libpng16源代碼並使用Visual Studio 2010構建它。我已正確鏈接了lib文件幷包含了png.h文件。libpng錯誤:讀取錯誤(Visual Studio 2010)

當我建立我的項目,libpng打印「libpng錯誤:讀取錯誤」到我的控制檯,沒有別的。我已經嘗試了我在互聯網上找到的所有解決方案,包括更改libpng項目上的運行時配置以匹配我即將使用它的項目。

錯誤發生在png_read_png功能:

FILE * file = fopen(filename,"r"); 
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING , NULL ,NULL , NULL); 
if (png_ptr == NULL) 
{ 
printf ("Could not initialize libPNG ’s read struct.\n") ; 
exit(-1); 
} 
png_infop png_info_ptr = png_create_info_struct(png_ptr) ; 
if (png_info_ptr == NULL) 
{ 
printf ("Could not initialize libPNG ’s info pointer.\n"); 
exit (-1) ; 
} 
if (setjmp(png_jmpbuf(png_ptr))) 
{ 

    printf ("LibPNG encountered an error.\n") ; 
png_destroy_read_struct(&png_ptr, &png_info_ptr ,NULL); 
    exit(-1); 
} 

png_init_io (png_ptr , file); 
png_read_png (png_ptr , png_info_ptr , 0 , NULL) ; 
png_uint_32 png_width = 0; 
png_uint_32 png_height = 0; 
int bits = 0; 
int colour_type = 0; 
png_get_IHDR (png_ptr , png_info_ptr , & png_width , & png_height ,& bits , & colour_type ,NULL , NULL , NULL); 
const unsigned BITS_PER_BYTE = 8; 
unsigned bytes_per_colour = (unsigned)bits/ BITS_PER_BYTE ; 
unsigned colours_per_pixel; 

if (colour_type == PNG_COLOR_TYPE_RGB) 
{ 
    colours_per_pixel = 3; 
} 
else 
{ 
printf (" Colour types other than RGB are not supported.") ; 
exit (-1) ; 
} 
printf ("png_width = %d, png_height = %d , bits = %d, colour type = %d. \n" , png_width , png_height , bits , colour_type); 
unsigned char * data = new unsigned char [ png_width * png_height * colours_per_pixel * bytes_per_colour]; 
png_bytepp row_pointers = png_get_rows (png_ptr , png_info_ptr) ; 
unsigned index = 0; 
for (unsigned y = 0; y < png_height ; y ++) 
{ 
    unsigned x = 0; 
    while (x < png_width * colours_per_pixel * bytes_per_colour) { 
data [index++] = row_pointers [y][x++]; 
data [index++] = row_pointers [y][x++]; 
data [index++] = row_pointers [y][x++]; 
    } 
} 

我已經確定了正確的文件名是過去了,我已經嘗試過多種不同的PNG的

與此的任何援助都應該理解

謝謝

回答

1

在Windows上,您必須打開二進制文件中的圖像文件模式,否則任何發生的字節序列,可能被解釋爲將被轉換爲單個。現在,您正在以文本模式的標準模式打開文件。您可以通過在模式字符串中添加'b'來打開二進制模式,即

FILE * file = fopen(filename,"rb"); 
+0

謝謝sooo much!我不敢相信我找不到解決方案。你爲我節省了很多時間。 –