简体   繁体   English

libpng错误:读取错误(Visual Studio 2010)

[英]libpng error: read error (Visual Studio 2010)

i've been struggling to get libpng to work on my opengl c++ program. 我一直在努力让libpng在我的opengl c ++程序上工作。 I am trying to load png's as textures. 我正在尝试将png加载为纹理。 I have downloaded the libpng16 source code and built it using Visual Studio 2010. I have correctly linked the lib files and included the png.h file. 我已经下载了libpng16源代码,并使用Visual Studio 2010对其进行了构建。我已正确链接了lib文件,并包含了png.h文件。

When I build my project, libpng prints "libpng error: read error" to my console and nothing else. 当我构建项目时,libpng会向控制台输出“ libpng错误:读取错误”,而没有其他输出。 I have tried all the solutions i've found on the internet including changing my runtime configurations on the libpng project to match my project that im using it on. 我已经尝试了在互联网上找到的所有解决方案,包括更改libpng项目上的运行时配置以匹配我使用它的项目。

The error occurs at the png_read_png function: 错误发生在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++];
  }
}

I have made sure that the correct filename is passed and I have tried multiple different PNG's 我确保传递了正确的文件名,并尝试了多个不同的PNG

Any assistance with this will be appreciated 任何帮助,将不胜感激

Thanks 谢谢

On Windows you must open image files in binary mode, otherwise any occurance of a sequence of bytes, that could be interpreted as will be converted into a single . 在Windows上,您必须以二进制模式打开图像文件,否则任何字节序列的出现(可以解释为将被转换为单个)。 Right now you're opening the file in standard mode, which is text mode. 现在,您将以标准模式(即文本模式)打开文件。 You can open in binary mode, by adding a 'b' to the mode string, ie 您可以在二进制模式下打开,方法是在模式字符串中添加“ b”,即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM