jpeg解码库使用实例

jpeg库下载地址:
    http://www.ijg.org/

交叉编译三部曲:
    A ./configure --host=arm-linux-gcc --prefix=/home/flying/jpeg-install
    B  make
    C  make install
jpeg库是解压jpeg/jpg图片或将图片压缩为jpeg/jpg格式所用到的库,使用起来比较简单!

jpeg/jpg解码过程:

    1. 创建jpeg对象和错误处理对象
        struct jpeg_decompress_struct cinfo;
        struct jpeg_error_mgr         jerr; 

    2. 将错误处理对象绑定到jpeg对象上
        EXTERN(struct jpeg_error_mgr *) jpeg_std_error
        JPP((struct jpeg_error_mgr * err));

    3. 初始化jpeg对象
        #define jpeg_create_compress(cinfo) \
    jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
                        (size_t) sizeof(struct jpeg_compress_struct))

    4. 指定解压数据源(有两种方式)
        a. EXTERN(void) jpeg_mem_src JPP((j_decompress_ptr cinfo, const unsigned char * inbuffer, unsigned long insize));
            参数一: jpeg对象地址
            参数二: 储存jpeg数据源缓冲区
            参数三: 缓冲区数据源大小

        b. EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));
            参数一: jpeg对象地址
            参数二: 要解压的jpeg图片的文件指针

    5. 填充cinfo对象的缺省信息,常见的可用信息包括图像的
       宽:       cinfo.image_width,
       高:       cinfo.image_height,
       色彩空间:  cinfo.jpeg_color_space,
       颜色通道数: cinfo.num_components等。
       EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
                                  boolean require_image));

    6. 为解压设定参数**(依情况而定)**
        比如解压的图片与原图的缩放比例为1/2,这可以设置参数
        cinfo.scale_num   = 1
        cinfo.scale_denom = 2

    7. 开始解压
        EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));

    在完成解压缩操作后,会将解压后的图像信息填充至cinfo结构中。比如,输出图
    像宽度cinfo.output_width,输出图像高度cinfo.output_height,每个像
    素中的颜色通道数cinfo.output_components(比如灰度为1,全彩色为3)
    等。
    一般情况下,这些参数是在jpeg_start_decompress后才被填充到cinfo中
    的,如果希望在调用jpeg_start_decompress之前就获得这些参数,可以通过
    调用jpeg_calc_output_dimensions()的方法来实现。


    8. 取数据
        解压出来的数据是按照行取出的,从左到右,从上到下的顺序,每个像
        素对应的各颜色或灰度通道数据是依次存储,24位RGB图解压后的颜色
        通道排序是R,G,B,R,G,B......

        我们可以使用此函数取出循环取出数据:
        extern JDIMENSION jpeg_read_scanlines(j_decompress_ptr, JSAMPARRAY,
                      JDIMENSION);
            参数二: 一般来说就是unsigned char *,表示保存数据的缓冲区
            参数三: 表示要读取的行数

    9. 解压完毕释放资源
        boolean jpeg_finish_decompress(j_decompress_ptr cinfo);

        后续不需要使用jpeg对象:
        void jpeg_destroy_decompress(j_decompress_ptr cinfo);

        如果在后续还需使用jpeg对象,则使用:
        void jpeg_abort_decompress(j_decompress_ptr cinfo);
实例:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include "jpeglib.h"
#include "my_err.h"



struct image_info
{
    int width;
    int height;
    int pixel_size;
};

//将图片显示到LCD屏幕上,最后两个参数指定显示的原点坐标
void write_jpeg_to_lcd(unsigned char *lcdmem, unsigned char *rgb_buffer
                        , struct fb_var_screeninfo *vinfo, struct image_info *image_info
                        , size_t xoffset, size_t yoffset)
{
    assert(lcdmem);
    assert(rgb_buffer);
    assert(vinfo);
    assert(image_info); 

    int x, y;

    size_t r_offset = vinfo->red.offset / 8;
    size_t g_offset = vinfo->green.offset / 8;
    size_t b_offset = vinfo->blue.offset / 8;

    for (y = 0; (y < vinfo->yres - yoffset) && (y < image_info->height); ++y) {

        for (x = 0; (x < vinfo->xres - xoffset) && (x < image_info->width); ++x) {

            size_t image_offset     =   (x * image_info->pixel_size) + (image_info->width * image_info->pixel_size * y);
            size_t lcd_offset       =   ((x + xoffset) * vinfo->bits_per_pixel / 8) + ((y + yoffset) * vinfo->xres * vinfo->bits_per_pixel /8);

            memcpy(lcdmem + lcd_offset + r_offset, rgb_buffer + image_offset, 1);
            memcpy(lcdmem + lcd_offset + g_offset, rgb_buffer + image_offset + 1, 1);
            memcpy(lcdmem + lcd_offset + b_offset, rgb_buffer + image_offset + 2, 1);
        }
    }
}

void read_image_from_file(int fd, unsigned char *jpeg_buffer, size_t image_size)
{
    assert(jpeg_buffer);

    int nread = 0;

    while (image_size > 0) {
        nread = read(fd, jpeg_buffer, image_size);
        if (nread < 0) {
            if (EINTR == errno) {
                continue;
            } else {
                err_sys("read error");
            }
        }

        image_size -= nread;
        jpeg_buffer += nread;
    }
}


int main(int argc, char **argv)
{
    if (2 != argc) {
        err_quit("Usage: %s <*.jpeg/jpg>\n", argv[1]);
    }

    int image_fd;
    if ((image_fd = open(argv[1], O_RDONLY)) < 0) {
        err_sys("open error");
    }

    struct stat sbuf;
    if (fstat(image_fd, &sbuf) < 0) {
        err_sys("stat error");
    }

    unsigned char *jpeg_buffer = calloc(1, sbuf.st_size);
    read_image_from_file(image_fd, jpeg_buffer, sbuf.st_size);


    struct jpeg_decompress_struct   cinfo;
    struct jpeg_error_mgr           jerr;

    cinfo.err   =   jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    jpeg_mem_src(&cinfo, jpeg_buffer, sbuf.st_size);

    if (JPEG_HEADER_OK != jpeg_read_header(&cinfo, true)) {
        err_sys("read head error");
    }

    jpeg_start_decompress(&cinfo);


    struct image_info *image_info   =   calloc(1, sizeof(struct image_info));
    if (image_info == NULL) {
        err_sys("calloc error for image_info");
    }

    image_info->width       =   cinfo.output_width;
    image_info->height      =   cinfo.output_height;
    image_info->pixel_size  =   cinfo.output_components;

    int row_stride  =   image_info->width * image_info->pixel_size;
    size_t rgb_size =   row_stride * image_info->height;

    unsigned char *rgb_buffer = calloc(1, rgb_size);
    if (rgb_buffer == NULL) {
        err_sys("rgb_buffer error");
    }

    //循环读取解压后的数据,cinfo.output_scanline表示当前读取的行数
    while (cinfo.output_scanline < image_info->height) {
        unsigned char *buffer_array[1];
        buffer_array[0] =   rgb_buffer + cinfo.output_scanline * row_stride;
        jpeg_read_scanlines(&cinfo, buffer_array, 1);
    }


    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    free(jpeg_buffer);


    int lcd_fd;
    if ((lcd_fd = open("/dev/fb0", O_RDWR)) < 0 ) {
        err_sys("open error");
    }   

    struct fb_var_screeninfo vinfo;
    ioctl(lcd_fd, FBIOGET_VSCREENINFO, &vinfo);

    size_t mem_size =   vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
    unsigned char *lcdmem   =   mmap(0, mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, lcd_fd, 0);
    if (lcdmem == MAP_FAILED) {
        err_sys("mmap error");
    }

    write_jpeg_to_lcd(lcdmem, rgb_buffer, &vinfo, image_info, 100, 100);

    free(rgb_buffer);
    munmap(lcdmem, mem_size);
    close(lcd_fd);
    close(image_fd);

    return EXIT_SUCCESS;
}
参考博客:
    http://blog.csdn.net/xipiaoyouzi/article/details/53257720

你可能感兴趣的:(c语言)