gpt4 book ai didi

c++ - libpng 在 png_read_info() 上崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:07 24 4
gpt4 key购买 nike

我正在尝试在 vs2013 中使用 libpng 1.2.10 读取一个 png 文件。我下载了最新的 zlib 并编译了 pnglib,效果很好。现在我正在尝试加载一个文件:

int *w = &width;
int *h = &height;
const char* name = file.c_str();
FILE *png_file = fopen(name, "rb");
if (!png_file)
{
std::cerr << "Could not open " + file << std::endl;
return;
}

unsigned char header[PNG_SIG_BYTES];

fread(header, 1, PNG_SIG_BYTES, png_file);
if (png_sig_cmp(header, 0, PNG_SIG_BYTES))
{
std::cerr << "PNG signature fail " + file << std::endl;
return;
}

png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(png_ptr == NULL)
{
std::cerr << "PNG read fail " + file << std::endl;
return;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
std::cerr << "PNG info fail " + file << std::endl;
return;
}
png_infop end_info = png_create_info_struct(png_ptr);
if(!end_info)
{
std::cerr << "PNG info end fail " + file << std::endl;
return;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
std::cerr << "PNG setjmp fail " + file << std::endl;
return;
}
png_init_io(png_ptr, png_file);
png_set_sig_bytes(png_ptr, PNG_SIG_BYTES);
png_read_info(png_ptr, info_ptr);

*w = png_get_image_width(png_ptr, info_ptr);
*h = png_get_image_height(png_ptr, info_ptr);

png_uint_32 bit_depth, color_type;
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);

if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
{
std::cerr << "Grayscale PNG not supported " + file << std::endl;
return;
}

if (bit_depth == 16)
png_set_strip_16(png_ptr);

if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
else if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(png_ptr);
}

if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
else
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);

png_read_update_info(png_ptr, info_ptr);

png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
png_uint_32 numbytes = rowbytes*(height);
png_byte* pixels = (png_byte*)malloc(numbytes);
png_byte** row_ptrs = (png_byte**)malloc((height)* sizeof(png_byte*));

int i;
for (i = 0; i<height; i++)
row_ptrs[i] = pixels + (height - 1 - i)*rowbytes;

png_read_image(png_ptr, row_ptrs);

free(row_ptrs);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(png_file);

//return (char *)pixels;

Create(*w, *h, 4, pixels, GL_UNSIGNED_BYTE);

不幸的是我得到了

Unhandled exception at 0x77D78E19 (ntdll.dll) in SimpleShader.exe: 0xC0000005: Access violation writing location 0x00000014.

在线

    png_read_info(png_ptr, info_ptr);

具体错误发生在这里:

#ifdef PNG_STDIO_SUPPORTED
/* This is the function that does the actual reading of data. If you are
* not reading from a standard C stream, you should create a replacement
* read_data function and use it at run time with png_set_read_fn(), rather
* than changing the library.
*/
void PNGCBAPI
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
png_size_t check;

if (png_ptr == NULL)
return;

/* fread() returns 0 on error, so it is OK to store this in a png_size_t
* instead of an int, which is what fread() actually returns.
*/
check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr)); // <---------- ERROR HERE

if (check != length)
png_error(png_ptr, "Read Error");
}
#endif

可能是什么问题?

编辑:好的,当我在 Release模式下编译 libpng 和我的项目时它不会崩溃。我需要在 Debug模式下运行我的项目...

最佳答案

这是由于 libpng 和您的项目之间的编译器设置不匹配造成的。特别是 Runtime Library 设置,在 libpng 和您的项目中都必须是 Multi-threaded DLL (/MD)。自述文件指出:

If you don't use the Visual Studio defaults your application must still be built with the default runtime option (/MD). If, for some reason, it is not then your application will crash inside libpng16.dll as soon as libpng tries to read from a file handle you pass in.

避免崩溃和保留调试可能性的最简单方法是复制项目中的“发布”配置并更改一些属性,以便您可以在 Visual Studio 中轻松调试。方法如下:

  1. 单击项目上的 RMB -> 属性 -> 配置管理器 -> 展开事件解决方案配置并单击 。从 Release 中选择一个新名称并复制设置。
  2. 现在在项目设置中,选择新创建的配置并更改以下属性:
    • C/C++ -> 常规 -> 调试信息格式:用于编辑并继续的程序数据库 (/ZI)
    • C/C++ -> Optimization -> Optimization: Disabled (/Od), Whole Program Optimization: No
    • 这就是所有必要的。您还可以禁用链接器优化(引用和 COMDAT 折叠)、启用最小重建等。

就是这样,祝你好运!

关于c++ - libpng 在 png_read_info() 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com