jpg转bmp使用libjpeg bmp jpeg jpg gif

写得很好:http://bbs.witech.com.cn/forum.php?mod=viewthread&tid=8131&extra=page=1&page=1
另外在libjpegb库中有现成的模型,在example.c中,有jpeg图片的压缩和解压代码。解压出来后是bmp格式的图片,填充好图片文件头,再把图像数据写入就oK,在下面的例子程序中体现。
相关理论:http://www.cnblogs.com/leaven/archive/2010/04/06/1705846.html

libjpeg库的交叉编译:http://blog.csdn.net/npy_lp/article/details/6991664/,详细可以参考下载的库中的README文件

转载: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
#include
extern "C"{
#include "jpeglib.h"
};
#pragma comment(lib,"jpeg.lib")
using namespace std;


#pragmapack(2)//两字节对齐,否则bmp_fileheader会占16Byte
struct bmp_fileheader
{
unsignedshortbfType;//若不对齐,这个会占4Byte
unsignedlongbfSize;
unsignedshortbfReverved1;
jpg转bmp(使用libjpeg) bmp jpeg jpg gif
unsignedshortbfReverved2;
unsignedlongbfOffBits;
};


struct bmp_infoheader
{
unsignedlongbiSize;
unsignedlongbiWidth;
unsignedlongbiHeight;
unsignedshortbiPlanes;
unsignedshortbiBitCount;
unsignedlongbiCompression;
unsignedlongbiSizeImage;
unsignedlongbiXPelsPerMeter;
unsignedlongbiYpelsPerMeter;
unsignedlongbiClrUsed;
unsignedlongbiClrImportant;
};


FILE *input_file;
FILE *output_file;


void write_bmp_header(j_decompress_ptr cinfo)
{
structbmp_fileheader bfh;
structbmp_infoheader bih;


unsignedlong width;
unsignedlong height;
unsignedshort depth;
unsignedlong headersize;
unsignedlong 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)
{
unsignedchar *dst_width_buff;
unsignedchar *point;


unsignedlong width;
unsignedlong height;
unsignedshort 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
{
for (unsigned long j=0;j
{
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(unsignedchar)*width*depth,1,output_file);//一次写一行
}

delete[]dst_width_buff;
}


void analyse_jpeg()
{
structjpeg_decompress_struct cinfo;
structjpeg_error_mgr jerr;
JSAMPARRAYbuffer;
unsignedchar *src_buff;
unsignedchar *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);


unsignedlong width=cinfo.output_width;
unsignedlong height=cinfo.output_height;
unsignedshort depth=cinfo.output_components;


src_buff=newunsigned 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
{
jpeg_read_scanlines(&cinfo,buffer,1);//读取一行jpg图像数据到buffer
memcpy(point,*buffer,width*depth);//将buffer中的数据逐行给src_buff
point+=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();
return0;
}

  

爱华网本文地址 » http://www.aihuau.com/a/25101016/317289.html

更多阅读

JPG格式怎样转换成BMP格式 jpg转换成bmp格式

JPG格式怎样转换成BMP格式——简介工作时常需要转换各图片类型,今天说说JPG转换成BMP。JPG格式怎样转换成BMP格式——工具/原料画图工具JPG格式怎样转换成BMP格式——方法/步骤JPG格式怎样转换成BMP格式 1、首先保存所需转换的图片

怎么把cr2图片转化为jpg格式 如何批量把cr2变成jpg

怎么把cr2图片转化为jpg格式——简介电脑上没有特殊的cr2读取软件的话是不支持打开cr2格式的图片的,但是很多时候我们需要把相机的图片导入到电脑中,并利用电脑分享给我们的好友,所以需要在分享之前,转换下照片的格式,可以让好友们都能看

pdf怎么转成jpg 精 怎么把jpg转换成pdf

对于PDF文档,许多用户都不满足于只能浏览,都希望能把里面的内容保存出来以供编辑。PDF转WORD,EXCEL已不是什么新鲜事,下面就讲讲怎么把pdf转成jpg图片的形式吧,总的来说是一件较为简单的事情pdf怎么转成jpg 精——工具Adobe Reader (以9

jpg图片转256色失真的解决方法 jpg缩小不失真

昨天在做系统盘时无意中解决了一个困扰我很长时间的问题:jpg图片转256色时失真的问题。由于制作启动盘所需的背景图片必须是640*480的256色位图,我一般是用jpg的转换,可颜色失真的问题一直困扰着我,第一次做试用背景时我选的是1024*768

声明:《jpg转bmp使用libjpeg bmp jpeg jpg gif》为网友替沵攩偑分享!如侵犯到您的合法权益请联系我们删除