博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bmp转jpg(使用libjpeg)
阅读量:7043 次
发布时间:2019-06-28

本文共 1987 字,大约阅读时间需要 6 分钟。

  jpg压缩原理可以参考这篇文章,我很早以前转的一篇文章。

  没有使用libjpeg的压缩代码可以看看这篇文章,也是我很早以前转的。

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

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

  代码如下:

#include 
#include
extern "C"{#include "jpeglib.h"};#pragma comment(lib,"jpeg.lib")using namespace std;#pragma pack(2) //两字节对齐,否则bmp_fileheader会占16Bytestruct bmp_fileheader{ unsigned short bfType; //若不对齐,这个会占4Byte unsigned 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
alloc_sarray) ((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1); point=dst_buffer+width*depth*(height-1); while (cinfo.next_scanline

转载地址:http://dwhal.baihongyu.com/

你可能感兴趣的文章
SQL Server-流程控制 6,WaitFor 语句
查看>>
Linux 内核通知链随笔【中】【转】
查看>>
阻塞通信之Socket编程
查看>>
iOS:UIView的CALayer基本演练
查看>>
Moo.fx 超级轻量级的 javascript 特效库
查看>>
代码阅读总结之Fitch and Mather 7.0(自定义字符串缓存页)
查看>>
Ajax发送Post请求
查看>>
android文件导出错误—— failed to pull a selection
查看>>
ECHO.js 纯javascript轻量级延迟加载
查看>>
第 3 章 Networking
查看>>
.NET设计模式实例之单例模式( Singleton Pattern)
查看>>
Innodb:RR隔离级别下insert...select 对select表加锁模型和死锁案列
查看>>
Python函数简单示例
查看>>
SAP MM Planning File 作用及其生成方式
查看>>
随手画个圆,你是怎么画的?我们分析了10万个圆,得到了这样的结论
查看>>
【C++】This指针和复制构造函数
查看>>
js中substring和substr的用法比较
查看>>
RESTful架构详解(转)
查看>>
工业物联网趋势已经形成
查看>>
一首诗的代码
查看>>