jpg压缩原理可以参考这篇文章http://hi.baidu.com/tiandsp/item/f5a2dcde6ef1405bd73aae41,我很早以前转的一篇文章。

  没有使用libjpeg的压缩代码可以看看这篇文章http://hi.baidu.com/tiandsp/item/9b5843c58a3b4474cfd4f841,也是我很早以前转的。

  这次使用libjpeg库压缩和上一篇的解压正好对应起来,有好多函数名称我都是对称的起的,所以结合起来看效果更好。

  和上一篇一样,只能处理24位和8位的图像。

  代码如下:

#include <iostream>
#include <stdio.h>
extern "C"{
#include "jpeglib.h"
};
#pragma comment(lib,"jpeg.lib")
using namespace std;#pragma pack(2)        //两字节对齐,否则bmp_fileheader会占16Byte
struct bmp_fileheader
{unsigned short    bfType;        //若不对齐,这个会占4Byteunsigned long    bfSize;unsigned short    bfReverved1;unsigned short    bfReverved2;unsigned long    bfOffBits;
};struct bmp_infoheader
{unsigned long    biSize;unsigned long    biWidth;unsigned long    biHeight;unsigned short    biPlanes;unsigned short    biBitCount;unsigned long    biCompression;unsigned long    biSizeImage;unsigned long    biXPelsPerMeter;unsigned long    biYpelsPerMeter;unsigned long    biClrUsed;unsigned long    biClrImportant;
};FILE *input_file;
FILE *output_file;struct bmp_fileheader bfh;
struct bmp_infoheader bih;unsigned char *src_buffer;
unsigned char *dst_buffer;void read_bmp_header()
{    fread(&bfh,sizeof(struct bmp_fileheader),1,input_file);fread(&bih,sizeof(struct bmp_infoheader),1,input_file);
}void read_bmp_data()
{fseek(input_file,bfh.bfOffBits,SEEK_SET);src_buffer=new unsigned char[bih.biWidth*bih.biHeight*bih.biBitCount/8];fread(src_buffer,sizeof(unsigned char)*bih.biWidth*bih.biHeight*bih.biBitCount/8,1,input_file);unsigned long width=bih.biWidth;unsigned long height=bih.biHeight;unsigned short depth=unsigned short(bih.biBitCount/8);unsigned char *src_point;unsigned char *dst_point;dst_buffer=new unsigned char[width*height*depth];    src_point=src_buffer+width*depth*(height-1);dst_point=dst_buffer+width*depth*(height-1);for (unsigned long i=0;i<height;i++){for (unsigned long j=0;j<width*depth;j+=depth){if (depth==1)        //处理灰度图
            {dst_point[j]=src_point[j];}if (depth==3)        //处理彩色图
            {dst_point[j+2]=src_point[j+0];dst_point[j+1]=src_point[j+1];dst_point[j+0]=src_point[j+2];}}dst_point-=width*depth;src_point-=width*depth;}
}void synthese_jpeg()
{struct jpeg_compress_struct cinfo;struct jpeg_error_mgr jerr;JSAMPARRAY buffer;unsigned long width=bih.biWidth;unsigned long height=bih.biHeight;unsigned short depth=unsigned short(bih.biBitCount/8);unsigned char *point;cinfo.err=jpeg_std_error(&jerr);        //libjpeg各种配置jpeg_create_compress(&cinfo);jpeg_stdio_dest(&cinfo,output_file);cinfo.image_width=width;cinfo.image_height=height;cinfo.input_components=depth;if (depth==1)cinfo.in_color_space=JCS_GRAYSCALE;elsecinfo.in_color_space=JCS_RGB;jpeg_set_defaults(&cinfo);jpeg_set_quality(&cinfo,20,TRUE);    //中间的值为压缩质量,越大质量越好jpeg_start_compress(&cinfo,TRUE);buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1);point=dst_buffer+width*depth*(height-1);while (cinfo.next_scanline<height){memcpy(*buffer,point,width*depth);jpeg_write_scanlines(&cinfo,buffer,1);point-=width*depth;}jpeg_finish_compress(&cinfo);jpeg_destroy_compress(&cinfo);
}int main()
{input_file=fopen("lena_gray.bmp","rb");output_file=fopen("lena.jpg","wb");read_bmp_header();read_bmp_data();synthese_jpeg();fclose(input_file);fclose(output_file);delete[] src_buffer;delete[] dst_buffer;cout<<"good job."<<endl;cin.get();return 0;
}

转载于:https://www.cnblogs.com/tiandsp/archive/2012/12/03/2799561.html

bmp转jpg(使用libjpeg)相关推荐

  1. libjpeg在windows下的编译

    以前搞图片在LCD屏上显示的时候,曾经在Linux下编译过libjpeg,现在再次编译,不过换了平台而已.本文使用的编译器为VS2003,编译的libjpeg为最新版本,即jpeg-8c. 编译步骤: ...

  2. jpg转bmp(使用libjpeg)

    原文链接:http://www.cnblogs.com/tiandsp/archive/2012/11/30/2796758.html 还是关于图像格式上的东西.使用了libjpeg库将jpeg图像转 ...

  3. jpg转bmp(使用libjpeg库)

    写得很好:http://bbs.witech.com.cn/forum.php?mod=viewthread&tid=8131&extra=page%3D1&page=1 另外 ...

  4. 利用libjpeg库实现jpg与bmp图片互转指南

    jpg与bmp图片互转指南 一.原理: ​ jpg与bmp图片格式都是以rgb像素为基础的,但是jpg在bmp的rgb的基础上进行了压缩.而且存储的方式是bgr,因此,在二者转换过程中需要转换对应的格 ...

  5. BMP图片读写接口函数

    我很早就学习了BMP位图.印象中,那时应该是在研究AVI视频文件格式时顺便研究的,或者是研究YUV转RGB时顺便研究的.但未写文章出来,我一直以为我的学习只有在发表了文章才算是完结,否则不能算是我做过 ...

  6. libjpeg学习1:简单使用示例

    libjpeg这个库主要用于处理jpeg数据,比如将RGB压缩成JPEG,或者将JPEG解压为RGB.其实早在4年前已经接触过,但一直没写过这方面的文章.后来想想还是有必要写出来,至少可以证明自己搞过 ...

  7. 对bmp文件内存压缩 与 解压缩

    对24位bmp位图文件进行jpg的压缩 传出的参数:outdata 压缩后数据内存指针 nSize 压缩后数据的大小 传入参数:bmp文件的内存数据 以及文件信息 需要 jconfig.h jmore ...

  8. c语言libjpeg处理图像,解决使用 libjpeg 保存图片时因磁盘写入失败导致程序退出的有关问题...

    0. libjpeg 介绍 libjpeg 是一个完全用C语言编写的库,包含了被广泛使用的JPEG解码.JPEG编码和其他的JPEG功能的实现.这个库由独立JPEG工作组维护. 参考:http://z ...

  9. 【正点原子I.MX6U-MINI应用篇】5、嵌入式Linux在LCD上显示BMP、JPG、PNG图片

    一.BMP图像介绍与显示 我们常用的图片格式有很多,一般最常用的有三种:JPEG(或 JPG).PNG.BMP和GIF.其中 JPEG(或JPG).PNG以及 BMP 都是静态图片,而 GIF 则可以 ...

最新文章

  1. 将功能绑定到Twitter Bootstrap Modal关闭
  2. 2006关注IT技术
  3. 【java的socket编程】结合多线程Thread实现通信(使用线程池和非线程池对比)、java开发UDP/IP网络程序
  4. [Linux] linux下安装配置 zookeeper/redis/solr/tomcat/IK分词器 详细实例.
  5. (2021) 18 [代码讲解] 可执行文件
  6. Magento重建所有索引方法
  7. Intel汇编语言程序设计学习笔记1
  8. python面试自我介绍_如何拿到半数面试公司Offer——我的Python求职之路
  9. tsp遗传算法 c语言,遗传算法解决TSP问题
  10. 计算机硬盘容量的最小单位,计算机中存储数据的最小单位和存储容量的基本单位各是什么?...
  11. 韩家炜课题组重磅发文:文本分类只需标签名称,不需要任何标注数据!
  12. html代码实现全国地图分布,echarts基于canvas中国地图省市地区介绍代码
  13. 使用Java写入Excel下拉选择框选项过多不显示问题
  14. Android 本地化翻译插件,解放你的双手! AndroidLocalizePlugin
  15. 容器化部署openvpn,访问策略配置
  16. win10亮度怎么调_笔记本屏幕亮度怎么调
  17. 059.迪杰斯特拉(Dijkstra)算法的原理以及解决最短路径问题
  18. 阿里云云计算:1. 云计算的概念
  19. elk之拼音插件可选参数
  20. 算法题(六十二)头条2017年笔试题——头条校招

热门文章

  1. 【Lucene4.8教程之中的一个】使用Lucene4.8进行索引及搜索的基本操作
  2. centos7 docker安装和使用_入门教程
  3. (五)Redis在项目中应用
  4. 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求
  5. 【转】VS2013中如何解决error C4996: 'fopen'问题
  6. Go学习笔记—标准库Context
  7. linux查看fcsan设备,fc-san存储
  8. 全国计算机等级考试题库二级C操作题100套(第27套)
  9. python socketio_flask-socketio实现WebSocket的方法
  10. 2014 计算机网络 考研真题,2014计算机网络考研试题过关必练