C++ 使用libjpeg读取.jpeg文件时出错
当file对象成功读取输入图像文件时,我在以下代码中的函数C++ 使用libjpeg读取.jpeg文件时出错,c++,c,image,jpeg,libjpeg,C++,C,Image,Jpeg,Libjpeg,当file对象成功读取输入图像文件时,我在以下代码中的函数jpeg\u read\u header()中遇到空指针异常 #include "jpeglib.h" #include <iostream> using namespace std; void decode_frame(char *filename) { unsigned char* raw_image; JSAMPROW row_pointer[1]; unsigned long locat
jpeg\u read\u header()
中遇到空指针异常
#include "jpeglib.h"
#include <iostream>
using namespace std;
void decode_frame(char *filename)
{
unsigned char* raw_image;
JSAMPROW row_pointer[1];
unsigned long location = 0;
struct jpeg_error_mgr jerr;
struct jpeg_decompress_struct cinfo ;
FILE *infile = fopen(filename, "rb" );
if (infile == NULL )
{
printf("Error opening jpeg file %s\n!", filename );
return -1;
}
cinfo.err = jpeg_std_error(&jerr);
/* create decompressor */
jpeg_create_decompress(&cinfo);
/* this makes the library read from infile */
jpeg_stdio_src(&cinfo, infile );
/* read jpeg header */
jpeg_read_header(&cinfo, TRUE);
/* decompress */
jpeg_start_decompress(&cinfo);
/*allocate memory */
raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
/* now actually read the jpeg into the raw buffer */
row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
/* read scanlines */
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for( int i=0; i < cinfo.image_width*cinfo.num_components;i++)
raw_image[location++] = row_pointer[0][i];
}
/* clean up */
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose( infile );
free( row_pointer[0] );
}
int main(){
char *f = "Example.jpg";
decode_frame(f);
return 0;
}
我哪里做错了?这是我第一次使用图像库。全局状态
200
表示(根据libjpeg源代码):
调用jpeg\u stdio\u src
仅对准备解压器结构有效,即分配内部cinfo->src
缓冲区并初始化其他解压器状态
换句话说,JPEG文件解析还没有开始:所以这里没有问题
您至少需要执行jpeg\u read\u header
,以确保cinfo
结构中填充了所有元数据信息(image\u width
,image\u height
,jpeg\u color\u space
等)。如果在该步骤中出现问题,这可能与您的JPEG文件有关,该文件可能已损坏(?)
您看到stderr上打印的类似于
而不是JPEG文件
的内容吗?我在JPEG中得到空指针异常(仅限JPEG\u read_header()fn)。。。关于jpeg文件被破坏,我尝试了几种jpeg,但结果是一样的。你的平台是什么?通过一些修改(将您的示例构建为纯C代码),它可以在Mac OS X上运行。您看到了吗?您可以建议在VS2005上构建jpeg.lib的步骤吗?对于VS2010,我们可以使用以下方法:nmake/f makefile.vc setup-v10生成.sln文件,但我需要在VS2005上构建它,这是一个旧项目。我没有在VS2005上使用libjpeg的经验。无论如何,你应该打开一个新的问题(或者找到它是否已经存在),因为这是一个不同的主题。
cinfo{err=0x0020f8fc mem=0x021cb320 progress=0x00000000 ...}
progress 0x00000000 {progress_monitor=??? pass_counter=??? pass_limit=??? ...}
client_data 0xcccccccc void *
is_decompressor 1
global_state 200
src 0x021c4480 {next_input_byte=0x00000000 <Bad Ptr> bytes_in_buffer=0 init_source=0x686ccb50 ...}
image_width 0
image_height 0
num_components 0
jpeg_color_space JCS_UNKNOWN
out_color_space JCS_UNKNOWN
scale_num 0
scale_denom 0
output_gamma 0.00000000000000000
buffered_image 0
raw_data_out 0
dct_method JDCT_ISLOW
#define DSTATE_START 200 /* after create_decompress */