jpg转bmp(使用libjpeg)
原文链接:http://www.cnblogs.com/tiandsp/archive/2012/11/30/2796758.html
还是关于图像格式上的东西。使用了libjpeg库将jpeg图像转换到bmp格式。解压原理还是相对复杂的,将来有机会说不定会详细介绍。这里只是库的使用而已。
首先需要下载libjpeg库,网址在这里:http://www.ijg.org/
然后需要配置环境,我是在windows下用vs2010搞的,编译库可以参考这篇文章。编译出jpeg.lib就可以了。当然实际编程还需要相应的头文件,头文件在下载的文件中。
如果不想编译就在这下载吧:http://vdisk.weibo.com/s/jpiMs
下面是相应的例程,只能将24位彩色图和8位深度图的jpg转换到bmp。
#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;void write_bmp_header(j_decompress_ptr cinfo)
{struct bmp_fileheader bfh;struct bmp_infoheader bih;unsigned long width;unsigned long height;unsigned short depth;unsigned long headersize;unsigned long filesize;width=cinfo->output_width;height=cinfo->output_height;depth=cinfo->output_components;if (depth==1){headersize=14+40+256*4;filesize=headersize+width*height;}if (depth==3){headersize=14+40;filesize=headersize+width*height*depth;}memset(&bfh,0,sizeof(struct bmp_fileheader));memset(&bih,0,sizeof(struct bmp_infoheader));//写入比较关键的几个bmp头参数bfh.bfType=0x4D42;bfh.bfSize=filesize;bfh.bfOffBits=headersize;bih.biSize=40;bih.biWidth=width;bih.biHeight=height;bih.biPlanes=1;bih.biBitCount=(unsigned short)depth*8;bih.biSizeImage=width*height*depth;fwrite(&bfh,sizeof(struct bmp_fileheader),1,output_file);fwrite(&bih,sizeof(struct bmp_infoheader),1,output_file);if (depth==1) //灰度图像要添加调色板{unsigned char *platte;platte=new unsigned char[256*4];unsigned char j=0;for (int i=0;i<1024;i+=4){platte[i]=j;platte[i+1]=j;platte[i+2]=j;platte[i+3]=0;j++;}fwrite(platte,sizeof(unsigned char)*1024,1,output_file);delete[] platte;}
}void write_bmp_data(j_decompress_ptr cinfo,unsigned char *src_buff)
{unsigned char *dst_width_buff;unsigned char *point;unsigned long width;unsigned long height;unsigned short depth;width=cinfo->output_width;height=cinfo->output_height;depth=cinfo->output_components;dst_width_buff=new unsigned char[width*depth];memset(dst_width_buff,0,sizeof(unsigned char)*width*depth);point=src_buff+width*depth*(height-1); //倒着写数据,bmp格式是倒的,jpg是正的for (unsigned long i=0;i<height;i++){for (unsigned long j=0;j<width*depth;j+=depth){if (depth==1) //处理灰度图{dst_width_buff[j]=point[j];}if (depth==3) //处理彩色图{dst_width_buff[j+2]=point[j+0];dst_width_buff[j+1]=point[j+1];dst_width_buff[j+0]=point[j+2];}}point-=width*depth;fwrite(dst_width_buff,sizeof(unsigned char)*width*depth,1,output_file); //一次写一行}
}void analyse_jpeg()
{struct jpeg_decompress_struct cinfo;struct jpeg_error_mgr jerr;JSAMPARRAY buffer;unsigned char *src_buff;unsigned char *point;cinfo.err=jpeg_std_error(&jerr); //一下为libjpeg函数,具体参看相关文档jpeg_create_decompress(&cinfo);jpeg_stdio_src(&cinfo,input_file);jpeg_read_header(&cinfo,TRUE);jpeg_start_decompress(&cinfo);unsigned long width=cinfo.output_width;unsigned long height=cinfo.output_height;unsigned short depth=cinfo.output_components;src_buff=new unsigned char[width*height*depth];memset(src_buff,0,sizeof(unsigned char)*width*height*depth);buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1);point=src_buff;while (cinfo.output_scanline<height){jpeg_read_scanlines(&cinfo,buffer,1); //读取一行jpg图像数据到buffermemcpy(point,*buffer,width*depth); //将buffer中的数据逐行给src_buffpoint+=width*depth; //一次改变一行}write_bmp_header(&cinfo); //写bmp文件头write_bmp_data(&cinfo,src_buff); //写bmp像素数据jpeg_finish_decompress(&cinfo);jpeg_destroy_decompress(&cinfo);delete[] src_buff;
}int main()
{input_file=fopen("lena.jpg","rb");output_file=fopen("lena.bmp","wb");analyse_jpeg();fclose(input_file);fclose(output_file);cout<<"good job."<<endl;cin.get();return 0;
}
注:write_bmp_data()有内存泄露,不想在源代码上编辑了。
jpg转bmp(使用libjpeg)相关推荐
- jpg转bmp(使用libjpeg库)
写得很好:http://bbs.witech.com.cn/forum.php?mod=viewthread&tid=8131&extra=page%3D1&page=1 另外 ...
- bmp转jpg(使用libjpeg)
jpg压缩原理可以参考这篇文章http://hi.baidu.com/tiandsp/item/f5a2dcde6ef1405bd73aae41,我很早以前转的一篇文章. 没有使用libjpeg的压缩 ...
- 利用libjpeg库实现jpg与bmp图片互转指南
jpg与bmp图片互转指南 一.原理: jpg与bmp图片格式都是以rgb像素为基础的,但是jpg在bmp的rgb的基础上进行了压缩.而且存储的方式是bgr,因此,在二者转换过程中需要转换对应的格 ...
- BMP图片读写接口函数
我很早就学习了BMP位图.印象中,那时应该是在研究AVI视频文件格式时顺便研究的,或者是研究YUV转RGB时顺便研究的.但未写文章出来,我一直以为我的学习只有在发表了文章才算是完结,否则不能算是我做过 ...
- libjpeg学习1:简单使用示例
libjpeg这个库主要用于处理jpeg数据,比如将RGB压缩成JPEG,或者将JPEG解压为RGB.其实早在4年前已经接触过,但一直没写过这方面的文章.后来想想还是有必要写出来,至少可以证明自己搞过 ...
- libjpeg在windows下的编译
以前搞图片在LCD屏上显示的时候,曾经在Linux下编译过libjpeg,现在再次编译,不过换了平台而已.本文使用的编译器为VS2003,编译的libjpeg为最新版本,即jpeg-8c. 编译步骤: ...
- 对bmp文件内存压缩 与 解压缩
对24位bmp位图文件进行jpg的压缩 传出的参数:outdata 压缩后数据内存指针 nSize 压缩后数据的大小 传入参数:bmp文件的内存数据 以及文件信息 需要 jconfig.h jmore ...
- c语言libjpeg处理图像,解决使用 libjpeg 保存图片时因磁盘写入失败导致程序退出的有关问题...
0. libjpeg 介绍 libjpeg 是一个完全用C语言编写的库,包含了被广泛使用的JPEG解码.JPEG编码和其他的JPEG功能的实现.这个库由独立JPEG工作组维护. 参考:http://z ...
- 【正点原子I.MX6U-MINI应用篇】5、嵌入式Linux在LCD上显示BMP、JPG、PNG图片
一.BMP图像介绍与显示 我们常用的图片格式有很多,一般最常用的有三种:JPEG(或 JPG).PNG.BMP和GIF.其中 JPEG(或JPG).PNG以及 BMP 都是静态图片,而 GIF 则可以 ...
最新文章
- php 图片 cdn,PHP文件生成的图片无法使用CDN缓存的解决方法
- 关于linux内核模块的装载过程
- 使用JXL.jar实现JAVA对EXCEL的读写操作
- java asm tree_使用ASM 4处理Java类文件–第二部分:Tree API
- 基于windows使用fabric将gitlab的文件远程同步到服务器(本地)
- https证书相关概念
- 学JAVA第十三天,方法、方法重载及构造函数
- KM算法 详解+模板
- 009 HDFS的shell命令(里面有一个安全模式)
- Wireshark系列之5 显示过滤器
- 计算机快速扫描技术的优点是,与传统扫描仪比较起来高拍仪的优点有哪些?
- STM32 HAL us delay(微秒延时)的指令延时实现方式及优化
- Python 北京房价预测实验报告 深度学习 tensorflow keras
- Linux下使用zlib实现文件压缩解压
- 深度学习之美 第五章 学习笔记
- fld to xml and xml to fld
- python中quad_python scipy integrate.quad用法及代码示例
- 亚马逊、速卖通、wish、Lazada、shoppe、ebay、煤炉测评跟淘宝shua单区别在哪?
- 亚马逊云科技联合学而思网校,共同开发AI启蒙课程
- Mysql数据库每天定时备份
热门文章
- FLANN C++ 使用
- 睡眠是锁定计算机怎么设置密码,笔记本电脑如何设置睡眠唤醒密码?
- Redis---客户端和服务端
- origin python控制台怎么用_如何在标准python控制台中访问BPY?BPY是python的混合器...
- 【内网安全】——数据库提权姿势
- 【Java并发编程】并发编程大合集
- 重庆大学计算机信息管理基础2013,重庆大学计算机信息管理基础课程考试试卷B.doc...
- Win自动配置VS Code的C++开发环境
- 计算机最快接口速度,实测:USB3.1究竟比USB3.0接口快多少?
- 数据库系统期末总结(一)(往届试卷2018A卷、C卷、E卷选择题)