开源JPEG图像(解)压缩库libjpeg的编译与使用示例(以VS2010为例)

首先解释以下两个名词(仅供参考,可跳过)

JPEG=Joint Photographic Experts Group(联合图像专家小组),是第一个国际图像压缩标准。JPEG图像压缩算法能够在提供良好的压缩性能的同时,具有比较好的重建质量,被广泛应用于图像、视频处理领域。人们日常碰到的.jpeg,.jpg等指代的是图像数据经压缩编码后在媒体上的封存形式,不能与JPEG压缩标准混为一谈。(百度百科

IJG is an informal group that writes and distributes a widely used free library for JPEG image compression. The first version was released on 7-Oct-1991. (http://www.ijg.org/)


本文将关注IJG提供的JPEG图像压缩/解压缩库的编译与初步使用。

首先在IJG网站http://www.ijg.org/下载源代码,此文编辑时已发布的版本为version 9a


编译

我们将要在Visual Studio 2010环境下编译,编译目标为libjpeg.lib


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用libjpeg将YUV420sp图像压缩JPEG的C++代码示例: ```cpp #include <stdio.h> #include <jpeglib.h> // YUV420sp图像数据 // yData:Y分量数据 // uvData:U和V分量交替存储的数据 // width:图像宽度 // height:图像高度 void compressYUV420spToJPEG(unsigned char* yData, unsigned char* uvData, int width, int height, const char* filename) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; // 创建JPEG压缩对象并设置错误处理 cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); // 打开输出文件 FILE* outfile; if ((outfile = fopen(filename, "wb")) == NULL) { fprintf(stderr, "Can't open output file.\n"); return; } jpeg_stdio_dest(&cinfo, outfile); // 设置图像参数 cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_YCbCr; jpeg_set_defaults(&cinfo); // 设置压缩参数 jpeg_set_quality(&cinfo, 90, TRUE); jpeg_set_colorspace(&cinfo, JCS_YCbCr); jpeg_default_colorspace(&cinfo); // 开始压缩 jpeg_start_compress(&cinfo, TRUE); // 每次处理一行数据 JSAMPROW row[1]; int row_stride = width * 3; // 一行数据的字节数 row[0] = new JSAMPLE[row_stride]; while (cinfo.next_scanline < cinfo.image_height) { // 将Y分量数据复制到输出行 for (int i = 0; i < width; i++) { row[0][i * 3] = yData[cinfo.next_scanline * width + i]; } // 将UV分量数据复制到输出行 for (int i = 0; i < width; i += 2) { row[0][i * 3 + 1] = uvData[(cinfo.next_scanline / 2) * width + i]; row[0][i * 3 + 2] = uvData[(cinfo.next_scanline / 2) * width + i + 1]; } // 压缩当前行数据 jpeg_write_scanlines(&cinfo, row, 1); } // 完成压缩 jpeg_finish_compress(&cinfo); // 释放资源 jpeg_destroy_compress(&cinfo); fclose(outfile); delete[] row[0]; } int main() { // 假设已有YUV420sp图像数据,存储在yData和uvData中 unsigned char* yData; unsigned char* uvData; int width; int height; // 调用函数进行压缩 compressYUV420spToJPEG(yData, uvData, width, height, "output.jpg"); return 0; } ``` 请注意,此代码中假设已经拥有YUV420sp图像数据,存储在yData和uvData中。你需要替换为实际的图像数据。此外,你需要安装libjpeg并在编译时链接该。具体的编译命令可以根据你的开发环境进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值