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 <jpg_file>\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;
    }
使用LIBJPEG库在STM32上进行JPEG图像的解码和编码是比较常见的应用。下面是一个简单的例程,展示如何在STM32上使用LIBJPEG库进行JPEG图像的解码和编码。 首先,需要将LIBJPEG库添加到STM32项目中。可以从LIBJPEG官网下载最新版本的库文件,并将其添加到STM32项目中。在添加库文件时,需要注意将库文件的头文件和源文件都添加到项目中。 接下来,需要编写代码来初始化JPEG解码器和编码器。例如,以下代码初始化了JPEG解码器: ``` /* JPEG decoding object */ jpeg_decompress_struct cinfo; /* Error handler object */ jpeg_error_mgr jerr; /* Initialize the JPEG decompression object with default error handling */ cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); ``` 然后,需要读取JPEG图像文件并将其解码。以下代码展示了如何读取并解码JPEG图像: ``` /* Open the input file */ FILE *infile = fopen("input.jpg", "rb"); if (infile == NULL) { printf("Error opening input file"); return; } /* Specify the input source */ jpeg_stdio_src(&cinfo, infile); /* Read the JPEG header */ jpeg_read_header(&cinfo, TRUE); /* Start the decompressor */ jpeg_start_decompress(&cinfo); /* Allocate memory for the image buffer */ unsigned char *buffer = (unsigned char*)malloc(cinfo.output_width * cinfo.output_height * cinfo.output_components); /* Read the image data */ unsigned char *row_pointer; while (cinfo.output_scanline < cinfo.output_height) { row_pointer = buffer + cinfo.output_scanline * cinfo.output_width * cinfo.output_components; jpeg_read_scanlines(&cinfo, &row_pointer, 1); } /* Finish decompression and release resources */ jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(infile); ``` 最后,可以使用LIBJPEG库来编码JPEG图像。以下代码展示了如何使用LIBJPEG库来编码JPEG图像: ``` /* JPEG encoding object */ jpeg_compress_struct cinfo; /* Error handler object */ jpeg_error_mgr jerr; /* Initialize the JPEG compression object with default error handling */ cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); /* Specify the output file */ FILE *outfile = fopen("output.jpg", "wb"); if (outfile == NULL) { printf("Error opening output file"); return; } jpeg_stdio_dest(&cinfo, outfile); /* Set the image parameters */ cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; /* Set the compression parameters */ jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE); /* Start the compressor */ jpeg_start_compress(&cinfo, TRUE); /* Write the image data */ unsigned char *row_pointer; while (cinfo.next_scanline < cinfo.image_height) { row_pointer = &image[cinfo.next_scanline * width * 3]; jpeg_write_scanlines(&cinfo, &row_pointer, 1); } /* Finish compression and release resources */ jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); fclose(outfile); ``` 注意,以上代码仅供参考,具体实现可能需要根据具体的应用场景进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值