使用Jpeglib静态库,用vs2022编译生成release版本的jpeg.lib,调用程序如果是debug编译的,jpeg_read_scanlines(&cinfo, buffer, 1)是正常的,如果是release版本,用jpeg_read_scanlines(&cinfo, buffer, 1);读取jpeg图像,第一次调用buffer里面都是0值,并且如果读出图像中所有行,退出会报错,图像的最后一行不读,则没有问题。
void LoadJpgFile(char* filename, unsigned char* ImgData, int& iWidth, int& iHeight)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE* infile;
JSAMPARRAY buffer;
int row_stride;
if (0 != fopen_s(&infile, filename, "rb"))
{
fprintf(stderr, "can't open %s\n", filename);
return;
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error.
* We need to clean up the JPEG object, close the input file, and return.
*/
jpeg_destroy_decompress(&cinfo);
if(infile) fclose(infile);
return;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
iWidth = cinfo.output_width;
iHeight = cinfo.output_height;
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1);
int row_to_go, next_row_out;
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(ImgData + cinfo.output_scanline * row_stride, buffer[0], row_stride);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
//jpeg_destroy((j_common_ptr)&cinfo);
_fcloseall();
return;
引用 皆我百晓生 小程序回复内容作答:
在使用jpeglib读取jpeg图像时,在release版本下出现异常的问题可能涉及到编译器优化、内存管理以及线程安全等因素。以下是一些可能的解决方案:
确保使用的是与编译器和操作系统相匹配的jpeglib版本,以避免兼容性问题。
检查编译器的优化选项,尝试禁用某些优化选项或更改优化级别,看是否能解决问题。
检查内存分配相关代码,确保正确地分配和释放内存,并遵循jpeglib的内存管理规则。
检查并确保在读取图像时的线程安全性,例如使用适当的锁或同步机制。
将代码与其他可靠的jpeg图像处理库进行对比,看是否存在差异或问题。
如果问题仍然存在,建议查阅jpeglib的文档、论坛或社区,寻求其他开发者的帮助和建议,以获取更具体和深入的解决方案。