[英]Error reading .jpeg file using libjpeg
當FILE對象成功讀取輸入圖像文件時,在以下代碼中的jpeg_read_header()
函數中jpeg_read_header()
NULL指針異常。
#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;
}
cinfo alfter jpeg_stdio_src(&cinfo, infile )
函數調用的值是:
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
我哪里出錯了? 這是我第一次使用圖像庫。
全局狀態200
表示(根據libjpeg源代碼):
#define DSTATE_START 200 /* after create_decompress */
調用jpeg_stdio_src
僅對准備解壓縮器結構有效,即,它分配內部cinfo->src
緩沖區並初始化其他解壓縮器狀態。
換句話說,JPEG文件解析尚未開始:因此您在這里沒有問題。
你至少需要執行jpeg_read_header
以確保cinfo
結構充滿了所有的元數據信息( image_width
, image_height
, jpeg_color_space
等)。 如果在該步驟出現問題,則可能與您的JPEG文件有關,該文件可能已損壞(?)。
您是否看到類似在Stderr上Not a JPEG file
打印Not a JPEG file
內容?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.