【问题标题】:Error reading .jpeg file using libjpeg使用 libjpeg 读取 .jpeg 文件时出错
【发布时间】:2013-11-25 16:19:12
【问题描述】:

当输入图像文件被 FILE 对象成功读取时,我在以下代码中的函数 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(&amp;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     

我哪里出错了?这是我第一次使用图片库。

【问题讨论】:

    标签: c++ c image jpeg libjpeg


    【解决方案1】:

    全局状态200表示(根据libjpeg源码):

    #define DSTATE_START 200 /* after create_decompress */
    

    调用jpeg_stdio_src只是为了准备解压器结构,即分配内部cinfo-&gt;src缓冲区并初始化其他解压器状态。

    也就是说 JPEG 文件解析还没有开始:所以这里没有问题。

    您至少需要执行jpeg_read_header 以确保cinfo 结构填充了所有元数据信息(image_widthimage_heightjpeg_color_space 等)。如果该步骤出现问题,这可能与您的 JPEG 文件损坏有关 (?)。

    你看到像Not a JPEG file这样的东西印在stderr上了吗?

    【讨论】:

    • 我只在 jpeg_read_header() fn 中得到 NULL 指针异常...关于 jpeg 文件被破坏,我尝试了几个 jpeg 但结果是一样的。
    • 你的平台是什么?经过一些修改(将您的示例构建为纯 C 代码),它可以在 Mac OS X 上运行。您见过 this 吗?
    • 您能否建议在 VS2005 上构建 jpeg.lib 的步骤,对于 VS2010,我们可以使用:nmake /f makefile.vc setup-v10 生成 .sln 文件,但我需要构建它在 VS2005 上,它是一个旧项目。
    • 我没有在 VS2005 上使用 libjpeg 的经验。无论如何,您绝对应该打开一个新问题(或查找它是否不存在),因为这是一个不同的主题。
    猜你喜欢
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多