decoder jpeg to rgb 24, using libjpeg

#include <stdio.h>
#include <setjmp.h>
 
#include "jpeglib.h"
#include "jerror.h"
 
struct my_error_mgr  
{
    struct jpeg_error_mgr pub;    /* "public" fields */
    
    jmp_buf setjmp_buffer;    /* for return to caller */
};
 
typedef struct my_error_mgr * my_error_ptr;
 
 
void my_error_exit (j_common_ptr cinfo)
{
    my_error_ptr myerr = (my_error_ptr) cinfo->err;
    
    (*cinfo->err->output_message) (cinfo);
    
    longjmp(myerr->setjmp_buffer, 1);
}
 
 
int read_JPEG_file (char * filename, char *outfilename)
{
    struct jpeg_decompress_struct cinfo;
    struct my_error_mgr jerr;
    FILE * infile;        /* source file */
    FILE * outfile;
    JSAMPARRAY buffer;        /* Output row buffer */
    int row_stride;        /* physical row width in output buffer */
 
    if ((infile = fopen(filename, "rb")) == NULL)
    {
        fprintf(stderr, "can't open %s/n", filename);
        return 0;
    }
    if ((outfile = fopen(outfilename, "wb")) == NULL)
    {
        fprintf(stderr, "can't open %s/n", outfilename);
        return 0;
    }
 
    cinfo.err = jpeg_std_error(&jerr.pub);
    jerr.pub.error_exit = my_error_exit;
 
    if (setjmp(jerr.setjmp_buffer))  
    {
        printf("Eror/n");
        jpeg_destroy_decompress(&cinfo);
        fclose(infile);
        return 0;
    }
 
    jpeg_create_decompress(&cinfo);
 
    jpeg_stdio_src(&cinfo, infile);
 
    jpeg_read_header(&cinfo, TRUE);
 
    printf("image width is %d/n", cinfo.image_width);    /* nominal image width (from SOF marker) */
    printf("image height is %d/n", cinfo.image_height);    /* nominal image width (from SOF marker) */
    printf("numcom is %d/n", cinfo.num_components);    /* nominal image width (from SOF marker) */
    
    
    jpeg_start_decompress(&cinfo);
    printf("image width is %d/n", cinfo.output_width);    /* nominal image width (from SOF marker) */
    printf("image height is %d/n", cinfo.output_height);    /* nominal image width (from SOF marker) */
    printf("numcom is %d/n", cinfo.output_components);    /* nominal image width (from SOF marker) */
 
    row_stride = cinfo.output_width * cinfo.output_components;
    buffer = (*cinfo.mem->alloc_sarray)    ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
 
    while (cinfo.output_scanline < cinfo.output_height)  
    {
        jpeg_read_scanlines(&cinfo, buffer, 1);
        //put_scanline_someplace(buffer[0], row_stride);
        fwrite(buffer[0], 1, row_stride, outfile);
 
    }
    jpeg_finish_decompress(&cinfo);
 
    jpeg_destroy_decompress(&cinfo);
 
    fclose(infile);
    fclose(outfile);
 
    return 1;
}
 
int main(int argc, char **argv)
{
    if(argc<3)
    {
        printf("Input Error/n");
        return 0;
    }
    read_JPEG_file(argv[1], argv[2]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值