libjpeg的简单使用

解压步骤如下:
/*
Allocate and initialize a JPEG decompression object // 分配和初始化一个decompression结构体
Specify the source of the compressed data (eg, a file) // 指定源文件
Call jpeg_read_header() to obtain image info // 用jpeg_read_header获得jpg信息
Set parameters for decompression // 设置解压参数,比如放大、缩小
jpeg_start_decompress(…); // 启动解压:jpeg_start_decompress
while (scan lines remain to be read)
jpeg_read_scanlines(…); // 循环调用jpeg_read_scanlines
jpeg_finish_decompress(…); // jpeg_finish_decompress
Release the JPEG decompression object // 释放decompression结构体
*/
代码:

int main(int argc, char **argv)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE * infile;
    int row_stride;
    unsigned char *buffer;

    if (argc != 2)
    {
        printf("Usage: \n");
        printf("%s \n", argv[0]);
        return -1;
    }

    if (FBDeviceInit())
    {
        return -1;
    }

    FBCleanScreen(0);

    // 分配和初始化一个decompression结构体
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    // 指定源文件
    if ((infile = fopen(argv[1], "rb")) == NULL) {
        fprintf(stderr, "can't open %s\n", argv[1]);
        return -1;
    }
    jpeg_stdio_src(&cinfo, infile);

    // 用jpeg_read_header获得jpg信息
    jpeg_read_header(&cinfo, TRUE);
    /* 源信息 */
    printf("image_width = %d\n", cinfo.image_width);
    printf("image_height = %d\n", cinfo.image_height);
    printf("num_components = %d\n", cinfo.num_components);

    // 设置解压参数,比如放大、缩小
    printf("enter scale M/N:\n");
    scanf("%d/%d", &cinfo.scale_num, &cinfo.scale_denom);
    printf("scale to : %d/%d\n", cinfo.scale_num, cinfo.scale_denom);

    // 启动解压:jpeg_start_decompress   
    jpeg_start_decompress(&cinfo);

    /* 输出的图象的信息 */
    printf("output_width = %d\n", cinfo.output_width);
    printf("output_height = %d\n", cinfo.output_height);
    printf("output_components = %d\n", cinfo.output_components);//解压的是rgb,故为3元素

    // 一行的数据长度
    row_stride = cinfo.output_width * cinfo.output_components;
    buffer = malloc(row_stride);//分配空间用来存储一行数据

    // 循环调用jpeg_read_scanlines来一行一行地获得解压的数据
    while (cinfo.output_scanline < cinfo.output_height) 
    {
        (void) jpeg_read_scanlines(&cinfo, &buffer, 1);

        // 写到LCD去
        FBShowLine(0, cinfo.output_width, cinfo.output_scanline, buffer);//一行行的将数据写到LCD上去
    }

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

    return 0;
}

rgb压缩为jpeg代码:

int write_jpeg_file(const char* jpeg_file, unsigned char* rgb_buffer, int width, int height, int quality)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    int row_stride = 0;
    FILE* fp = NULL;
    JSAMPROW row_pointer[1];

    cinfo.err = jpeg_std_error(&jerr);

    jpeg_create_compress(&cinfo);
    fp = fopen(jpeg_file, "wb");
    if (fp == NULL)
    {
        printf("open file %s failed.\n", jpeg_file);
        return -1;
    }
    jpeg_stdio_dest(&cinfo, fp);
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;//设置输入格式

    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, 1);  // todo 1 == true
    jpeg_start_compress(&cinfo, TRUE);
    row_stride = width * cinfo.input_components;

    while (cinfo.next_scanline < cinfo.image_height)
    {
        row_pointer[0] = &rgb_buffer[cinfo.next_scanline * row_stride];
        jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
    fclose(fp);

    return 0;
    }

你可能感兴趣的:(linux)