码迷,mamicode.com
首页 > 其他好文 > 详细

bmp2jpeg

时间:2020-11-19 12:45:48      阅读:11      评论:0      收藏:0      [点我收藏+]

标签:pre   ade   数据量   pbm   mamicode   特定   src   yuv   bmp   

bmp2jpg

步骤

  1. 图像分割
  2. 颜色空转换
  3. DCT变换
  4. 数据量化(将频率系数转换为整数)
  5. 哈夫曼编码
  • 直流系数
  • 交流系数(游程)

技术图片
应该需要读取bmp文件里特定位置的数据然后进行分割

读取bmp图片数据
//读取bmp infoHead里的内容
#include<iostream>
#include<algorithm>
#include<cstring>
#include<windows.h>
using namespace std;
char bmpname[100];
char jpegname[100];
int bmpheight,bmpwidth,linebyte;
unsigned char * pBmpBuf;     //存储图像数据
int main()
{
	// 读入图片
	cout<<"输入要转换的bmp文件:";
	cin>>bmpname;
	FILE *fp;
	if( (fp=fopen(bmpname,"rb"))==NULL )
	{
		cout<<"The file can not open"<<endl;
		exit(0);
	}
	if(fseek(fp,sizeof(BITMAPFILEHEADER),0))  //跳过BITMAPFILEHEADE
    {
        cout<<"跳转失败"<<endl;
        return FALSE;
    }
	BITMAPINFOHEADER infoHead;
	fread(&infoHead,sizeof(BITMAPINFOHEADER),1,fp);
	bmpwidth = infoHead.biWidth;
	bmpheight = infoHead.biHeight;
	linebyte = (bmpwidth*24/8+3)/4*4;   //每行的字节数
	pBmpBuf = new unsigned char[linebyte*bmpheight];
	fread(pBmpBuf,sizeof(char),linebyte*bmpheight,fp);
	cout<<bmpwidth<<" "<<bmpheight<<endl;
	fclose(fp);
	return 0;
}

jpeg编码要求缓冲区的高和宽为8或16的倍数
所以需要调整缓冲区的高和宽

#include<iostream>
#include<algorithm>
#include<cstring>
#include<windows.h>
using namespace std;
char bmpname[100];
char jpegname[100];
int bmpheight,bmpwidth,linebyte;
unsigned char * pBmpBuf;     //存储图像数据
int main()
{
	// 读入图片
	cout<<"输入要转换的bmp文件:";
	cin>>bmpname;
	FILE *fp;
	if( (fp=fopen(bmpname,"rb"))==NULL )
	{
		cout<<"The file can not open"<<endl;
		exit(0);
	}
	if(fseek(fp,sizeof(BITMAPFILEHEADER),0))  //跳过BITMAPFILEHEADE
    {
        cout<<"跳转失败"<<endl;
        return FALSE;
    }
	BITMAPINFOHEADER infoHead;
	fread(&infoHead,sizeof(BITMAPINFOHEADER),1,fp);
	cout<<"infoHead.biWidth: "<<infoHead.biWidth<<"infoHead.biHeight: "<<infoHead.biHeight<<endl;
	cout<<"*********"<<endl;
	
	//第一步处理,把需要用到的数据变成8的倍数
	if(infoHead.biWidth % 8 == 0)
	{
		bmpwidth = infoHead.biWidth;
	}
	else
	{
		bmpwidth = infoHead.biWidth + 8 - (infoHead.biWidth % 8);
	}
	if(infoHead.biWidth % 8 == 0)
	{
		bmpheight = infoHead.biHeight;
	}
	else
	{
		bmpheight = infoHead.biHeight + 8 - (infoHead.biHeight % 8);
	}
	cout<<bmpwidth<<" "<<bmpheight<<endl;
	linebyte = (bmpwidth*24/8+3)/4*4;   //每行的字节数
	pBmpBuf = new unsigned char[linebyte*bmpheight];
	fread(pBmpBuf,sizeof(char),linebyte*bmpheight,fp);
	fclose(fp);
	return 0;
}

技术图片

将缓冲区每个像素点进行颜色转换
将转换后的结果存入缓冲区
void RGB2YUV(BYTE* pBuf, BYTE* pYBuff, BYTE* pUBuff, BYTE* pVBuff)
{
	//临时变量
	double tmpY = 0;
	double tmpU = 0;
	double tmpV = 0;
	BYTE tmpB = 0;
	BYTE tmpG = 0;
	BYTE tmpR = 0;
	
	//缓冲长度
	size_t elemNum = _msize(pBuf) / 3;
	
	for(int i=0;i<elemNum;i++)
	{
		//获取缓冲区每个像素点的R G B
		tmpB = pBuf[i*3];
		tmpG = pBuf[i*3+1];
		tmpR = pBuf[i*3+2];
		
		//按公式进行转换
		tmpY = 0.299 * tmpR + 0.587 * tmpG + 0.114 * tmpB;
		tmpU = -0.1687 * tmpR - 0.3313 * tmpG + 0.5 * tmpB + 128;
		tmpV = 0.5 * tmpR - 0.4187 * tmpG - 0.0813 * tmpB + 128;
		   
		//把转换后的YUV放入缓冲区
		  pYBuff[i] = tmpY;
		  pYBuff[i] = tmpU;
		  pYBuff[i] = tmpV;
	}
}
分割为8*8的像素小块

bmp2jpeg

标签:pre   ade   数据量   pbm   mamicode   特定   src   yuv   bmp   

原文地址:https://www.cnblogs.com/serendipity-my/p/13974055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!