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的
与此的任何援助都应该理解
谢谢
谢谢sooo much!我不敢相信我找不到解决方案。你为我节省了很多时间。 –