标签:pre ade 数据量 pbm mamicode 特定 src yuv 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;
}
}
标签:pre ade 数据量 pbm mamicode 特定 src yuv bmp
原文地址:https://www.cnblogs.com/serendipity-my/p/13974055.html