C jpeg\u读取\u扫描线中的libjpeg崩溃
当我运行这段代码加载一个jpeg文件时,我在jpeg\u read\u扫描线中得到一个崩溃 我正在使用64位windows 7和VC++2010 我正在加载的图像是100x75 jpg图像 如果你需要更多的细节,尽管问 崩溃消息是: LibTest.exe中0x012db29e处的未处理异常:0xC0000005:访问冲突写入位置0xCDCDC jpeg\u读取\u扫描线中的libjpeg崩溃,c,jpeg,loader,C,Jpeg,Loader,当我运行这段代码加载一个jpeg文件时,我在jpeg\u read\u扫描线中得到一个崩溃 我正在使用64位windows 7和VC++2010 我正在加载的图像是100x75 jpg图像 如果你需要更多的细节,尽管问 崩溃消息是: LibTest.exe中0x012db29e处的未处理异常:0xC0000005:访问冲突写入位置0xCDCD void JPG_Load (const char *path, image_t *img) { struct jpeg_decompress_stru
void JPG_Load (const char *path, image_t *img)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int infile;
JSAMPARRAY buffer;
int row_stride;
unsigned char *out;
infile = fopen(path,"rb");
if (infile == 0) {
memset (img, 0, sizeof(image_t));
return;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, (FILE *)infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
out = malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components);
img->pixels = out;
img->width = cinfo.output_width;
img->height = cinfo.output_height;
img->bytesPerPixel = cinfo.out_color_components;
while (cinfo.output_scanline < cinfo.output_height) {
buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline);
jpeg_read_scanlines(&cinfo, buffer, 1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
}
不要这样做
buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline); // WRONG
您正在强制转换为JSAMPARRAY
,这基本上是void**
。结果是垃圾,因为这不是您拥有的数据类型:您拥有一个字节数组
查看文档时,jpeg\u read\u scanlines
函数不带指向缓冲区的指针。它接受一个指向扫描线数组的指针,每个扫描线都是指向行数据的指针
while (cinfo.output_scanline < cinfo.output_height) {
unsigned char *rowp[1];
rowp[0] = (unsigned char *) out + row_stride * cinfo.output_scanline;
jpeg_read_scanlines(&cinfo, rowp, 1);
}
while(cinfo.output\u扫描线
建议:只有在您知道强制转换是正确的情况下,添加强制转换以修复编译器错误才有效。除非知道类型是什么,否则不要强制转换为任何类型。不要这样做
buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline); // WRONG
您正在强制转换为JSAMPARRAY
,这基本上是void**
。结果是垃圾,因为这不是您拥有的数据类型:您拥有一个字节数组
查看文档时,jpeg\u read\u scanlines
函数不带指向缓冲区的指针。它接受一个指向扫描线数组的指针,每个扫描线都是指向行数据的指针
while (cinfo.output_scanline < cinfo.output_height) {
unsigned char *rowp[1];
rowp[0] = (unsigned char *) out + row_stride * cinfo.output_scanline;
jpeg_read_scanlines(&cinfo, rowp, 1);
}
while(cinfo.output\u扫描线
建议:只有在您知道强制转换是正确的情况下,添加强制转换以修复编译器错误才有效。除非你知道类型是什么,否则不要强制转换为任何类型