使用libpng时出现的错误
执行到此处时调试报错:ca4.exe 已触发了一个断点。点继续提示:0x0FD72793 (appcrt140d.dll) (ca4.exe 中)处有未经处理的异常: 将一个无效参数传递给了将无效参数视为严重错误的函数。
调试输出如下:
Debug Assertion Failed!
Program: ...\test\Documents\Visual Studio 2015\Projects\ca4\Debug\ca4.exe
File: f:\dd\vctools\crt\core_crt\src\appcrt\lowio\read.cpp
Line: 498
Expression: _osfile(fh) & FOPEN
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
Second Chance Assertion Failed: File f:\dd\vctools\crt\core_crt\src\appcrt\internal\winapi_nonmsdk.cpp, Line 165
ca4.exe 已触发了一个断点。
0x0FD72793 (appcrt140d.dll) (ca4.exe 中)处有未经处理的异常: 将一个无效参数传递给了将无效参数视为严重错误的函数。
程序片段如下:
bool PngData::ReadDataFromFile()
{
char png_header[8];
png_structp png_ptr;
png_infop info_ptr;
int width, height, rowBytes;
png_byte color_type;
png_byte bit_depth;
png_colorp palette;
FILE *file;
fopen_s(&file, m_filename.data(), "rb");
if (file == nullptr)
{
globalvars::gamelog.LogErr("Load picture failed: Cannot open picture file");
return false;
}
fread_s(png_header, sizeof(png_header), 8, 1, file);
if (png_sig_cmp((png_bytep)png_header, 0, 8))
{
globalvars::gamelog.LogErr("Load picture failed: Not a png file");
fclose(file);
return false;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
globalvars::gamelog.LogErr("Load picture failed: Set jmp error");
fclose(file);
return false;
}
//I/O initialisation methods
png_init_io(png_ptr, file);
png_set_sig_bytes(png_ptr, 8); //Required!!!
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, NULL); //运行至此时出错
width = info_ptr->width;
height = info_ptr->height;
unsigned char* rgba = new unsigned char[width * height * 4];
png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr);
int pos = (width * height * 4) - (4 * width);
for (int row = 0; row < height; row++)
{
for (int col = 0; col < (4 * width); col += 4)
{
rgba[pos++] = row_pointers[row][col]; // red
rgba[pos++] = row_pointers[row][col + 1]; // green
rgba[pos++] = row_pointers[row][col + 2]; // blue
rgba[pos++] = row_pointers[row][col + 3]; // alpha
}
pos = (pos - (width * 4) * 2); //move the pointer back two rows
}
m_PictureSize.ImageWidth = width;
m_PictureSize.ImageHeight = height;
m_PixelData = rgba;
return true;
}