第四次作业——BMP2YUV

一、BMP文件的组成结构

BMP(全称Bitmap)是Windows操作系统中的标准图像文件格式,可以分成两类:设备相关位图(DDB)和设备无关位图(DIB),使用广泛。它采用位映射存储格式,除了图像深度可选以外,在绝大多数应用中不采用其他任何压缩,因此,BMP文件所占用的空间很大。BMP文件的图像深度可选lbit、4bit、8bit、16bit及24bit。图像的扫描方式是按从左到右、从下到上的顺序。由于BMP文件格式是Windows环境中交换与图有关的数据的一种标准,因此在Windows环境中运行的图形图像软件都支持BMP图像格式。
 

典型的BMP图像文件由四部分组成:

(1)位图头文件数据结构,它包含 BMP 图像文件的类型、显示内容等信息;

//定义bmp文件结构体
typedef struct tagBITMAPFILEHEADER {
    WORD bfType; //说明文件的类型
    DWORD bfSize; // 说明文件的大小,用字节为单位 
    WORD bfReserved1; //保留,设置为0 
    WORD bfReserved2; // 保留,设置为0 
    DWORD bfOffBits; //说明从BITMAPFILEHEADER结构开始到实际的图像数据之间的字节偏移量 
 
} BITMAPFILEHEADER;

(2)位图信息数据结构,它包含有 BMP 图像的宽、高、压缩方法,以及定义颜色等信息:

typedef struct tagBITMAPINFOHEADER { 
        DWORD    biSize;       /* 说明结构体所需字节数 */
        LONG        biWidth;   /* 以像素为单位说明图像的宽度 */
        LONG        biHeight;  /* 以像素为单位说明图像的高速 */
        WORD       biPlanes;   /* 说明位面数,必须为1 */
        WORD       biBitCount;  /* 说明位数/像素,1、2、4、8、24 */
        DWORD    biCompression;  /* 说明图像是否压缩及压缩类型 				BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS */
        DWORD    biSizeImage;    /*  以字节为单位说明图像大小 ,必须是4         的整数倍*/
        LONG        biXPelsPerMeter;    /*  目标设备的水平分辨率,像素/米 */
        LONG        biYPelsPerMeter;    /*目标设备的垂直分辨率,像素/米 */
        DWORD    biClrUsed;    /* 说明图像实际用到的颜色数,如果为0
                                                       则颜色数为2的biBitCount次方 */
        DWORD    biClrImportant;  /*说明对图像显示有重要影响的颜色         
                                   索引的数目,如果是0,表示都重要。*/
}  BITMAPINFOHEADER;

(3)调色板,这个部分是可选的,有些位图需要调色板,有些位图,比如真彩色图(24位的 BMP)就不需要调色板:

//调色板实际上是一个数组,它所包含的元素与位图所具有的颜色数相同,决定于biClrUsed和biBitCount字段。数组中每个元素的类型是一个RGBQUAD结构。真彩色无调色板部分。
 
typedef struct tagRGBQUAD { 
       BYTE    rgbBlue;           /*指定蓝色分量*/
       BYTE    rgbGreen;        /*指定绿色分量*/
       BYTE    rgbRed;            /*指定红色分量*/
       BYTE    rgbReserved;   /*保留,指定为0*/
}  RGBQUAD;

(4)位图数据,这部分的内容根据 BMP 位图使用的位数不同而不同,在 24 位图中直接使用 RGB,而其他的小于 24 位的使用调色板中颜色索引值。

打开实验BMP文件:

“42 4D”:说明文件类型为BMP

“18 00”:(0x"0018"),说明24位/像素

二、字节序

 不同的计算机系统采用不同的字节序存储数据,同样一个 4 字节的 32 位整数,在内存中存储的方式不同。字节序分为小尾字节序(Little Endian)和大尾字节序(Big Endian)。Intel 处理器大多数使用小尾字节序,Motorola 处理器大多数使用大尾(Big Endian)字节序。小尾就是低位字节排放在内存的低端,高位字节排放在内存的高端,即所谓的“低位在前,高位在后”。大尾就是高位字节排放在内存的低端,低位字节排放在内存的高端,即所谓的“高位在前,低位在后”。 TCP/IP 各层协议将字节序定义为大尾,因此 TCP/IP 协议中使用的字节序通常称之为网络字节序。 
       在实现 BMP 文件头信息的写入时,需要注意整数保存时的字节序。例如:文件大小是以Intel 序保存的。在编程前先用二进制打开方式观察 BMP 文件各个部分的数据存储格式。

BMP文件存储数据时,图像的扫描方式是按从左到右、从下到上的顺序。而RGB文件存储数据时,图像的扫描方式是按从左到右、从上到下的顺序。因此在将BMP文件格式转化为RGB文件格式时,要注意“倒叙读写”的问题。  

三、实验流程分析

1.程序初始化(打开两个文件,定义变量和缓冲区等)

2.读取BMP文件,抽取或生成RGB数据写入缓冲区

读位文件头,判断是否可读出以及是否是BMP文件
读位图信息头,判断是否可以读出
判断像素的实际点阵数
开辟缓冲区,读数据,倒序存放
最后根据每像素位数的不同,执行不同的操作
24/32bit:直接取像素数据写RGB缓冲区
16bit:位与移位取像素数据转换为8bit/彩色分量写RGB缓冲区
8bit及以下:构造调色板,位与移位取像素数据查调色板写RGB缓冲区。
3.调用RGB2YUV的函数实现RGB到YUV数据的转换

4.写YUV文件

5.程序收尾工作(关闭文件,释放缓冲区)

四. 实验原理及代码

1.头文件BMP2YUV.h
头文件进行函数声明,方便函数调用

#pragma once
#include<windows.h>
#ifndef BMP2YUV_H_
#define BMP2YUV_H_
 
void BMP2RGB(int width, int height, FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, unsigned char* rgbout);
bool MakePalette(FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, RGBQUAD* rgbout);
void RGB2YUV(int width, int height, unsigned char* rgbbuf, unsigned char* ybuf, unsigned char* ubuf, unsigned char* vbuf);
void DownYUV(int width, int height, unsigned char* utmp, unsigned char* vtmp, unsigned char* ubuf, unsigned char* vbuf);
#endif

2.main.cpp
(1)程序初始化,定义变量和缓冲区

 FILE* bmpfile = NULL, * yuvfile = NULL;
	BITMAPFILEHEADER file_header;
	BITMAPINFOHEADER info_header;
	unsigned char* rgbbuf = NULL;
	unsigned char* ybuf = NULL;
	unsigned char* ubuf = NULL;
	unsigned char* vbuf = NULL;
	int width, height;

(2) 定义输入输出

main()函数带有参数argc和argv,argc表示输入变量的个数,argv[0]为生成的.exe文件,后面依次为输入参数。
本实验输入参数分别为五个bmp文件的文件名以及重复次数。在项目属性-调试-命令参数中,可以提前设置好输入参数。

int repeat_num;
	const char* bmpfilename[5] = {0};
	const char* yuvfilename;
	bmpfilename[0] = argv[1];
	bmpfilename[1] = argv[2];
	bmpfilename[2] = argv[3];
	bmpfilename[3] = argv[4];
	bmpfilename[4] = argv[5];
	repeat_num = atoi(argv[6]);
	yuvfilename = "out.yuv";

 

(3)读位图文件头及位图信息头

for (int k = 0; k < 5; k++)
	{
		//判断文件是否能打开
		if ((bmpfile = fopen(bmpfilename[k], "rb")) == NULL)
		{
			printf("can't find bmpfilie");
			exit(0);
		}
		if ((yuvfile = fopen(yuvfilename, "ab")) == NULL)
		{
			printf("can't find yuvfilie");
			exit(0);
		}
		//读取文件头
		if (fread(&file_header, sizeof(BITMAPFILEHEADER), 1, bmpfile) != 1)
		{
			printf("read file header error!");
			exit(0);
		}
		//判断是否为BMP文件
		if (file_header.bfType != 0x4D42)
		{
			printf("not bmp file");
			exit(0);
		}
		else
		{
			printf("this is a bmp file!");
		}
		//读取信息头
		if (fread(&info_header, sizeof(BITMAPINFOHEADER), 1, bmpfile) != 1)
		{
			printf("read info header error!");
			exit(0);
		}

(4) 保证每一行的数据量为4的整数倍,保证行数为偶数

//为了保证图像大小是4的倍数,所以每一行的数据量是4字节的整数倍
		if (info_header.biWidth % 4 == 0)
		{
			width = info_header.biWidth;
		}
		else
		{
			width = (info_header.biWidth * info_header.biBitCount + 31) / 32 * (32 / 8);
			//(x+31)/32,结果为下取整,得到结果以后,再乘以32,就能得到比原位数大且是32倍数的数字,再除以8得到字节数
		}
		//保证行数是偶数
		if (info_header.biHeight % 2 == 0)
		{
			height = info_header.biHeight;
		}
		else
		{
			height = info_header.biHeight + 1;
		}

(5)开辟缓冲区

rgbbuf = new unsigned char[height * width * 3];
		ybuf = new unsigned char[height * width];
		ubuf = new unsigned char[height * width / 4];
		vbuf = new unsigned char[height * width / 4];
		unsigned char* utmp = new unsigned char[height * width];
		unsigned char* vtmp = new unsigned char[height * width];

(6)之后调用BMP2RGB函数将BMP转为RGB;
调用RGB2YUV函数将RGB转为YUV,
此时的YUV为4:4:4的取样结构,因此还需调用DownYUV进行下采样,使YUV的取样格式为4:2:0

BMP2RGB(width, height, bmpfile, file_header, info_header, rgbbuf);
        RGB2YUV(width, height, rgbbuf, ybuf, utmp, vtmp);
		DownYUV(width, height, utmp, vtmp, ubuf, vbuf);

(7)写入YUV,释放缓冲区,关闭文件

for (int i = 0; i < repeat_num; i++)
		{
			fwrite(ybuf, 1, width * height, yuvfile);
			fwrite(ubuf, 1, width * height / 4, yuvfile);
			fwrite(vbuf, 1, width * height / 4, yuvfile);
		}
 
		if (rgbbuf)
			delete[] rgbbuf;
		if (ybuf)
			delete[] ybuf;
		if (ubuf)
			delete[] ubuf;
		if (vbuf)
			delete[] vbuf;
		if (utmp)
			delete[] utmp;
		if (vtmp)
			delete[] vtmp;
 
		fclose(bmpfile);
 
		fclose(yuvfile);
	}
	return;

3.BMP2RGB.cpp

void BMP2RGB(int width, int height, FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, unsigned char* rgbout)
  {
	unsigned long loop;
	unsigned char mask, * index_data, * data;
	int w, h;
	w = width * info_header.biBitCount/8;
	h = height;
	//倒序前数据缓冲区
	index_data = new unsigned char[h * w];
	//倒序后数据缓冲区
	data = new unsigned char[h * w];
	fseek(bmpfile, file_header.bfOffBits, 0);
	if (fread(index_data, h * w, 1, bmpfile) != 1)
	{
		printf("read file error!");
		exit(0);
	}
	//倒序变正序
	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			data[i * w + j] = index_data[(h - 1 - i) * w + j];
		}
	}
	//24位,直接将倒序后的缓存区数据复制给缓存输出区
	if (info_header.biBitCount == 24)
	{
		memcpy(rgbout, data, h * w);//保存顺序是BGR
	}
	//16位,位与移位取像素数据转换为8bit/彩色分量,写RGB缓冲区
	if (info_header.biBitCount == 16)
	{
		for (loop = 0; loop < h * w; loop += 2)
		{
			*rgbout = (data[loop] & 0x1F) << 3;
			*(rgbout + 1) = ((data[loop] & 0xE0) >> 2) + ((data[loop + 1] & 0x03) << 6);
			*(rgbout + 2) = (data[loop + 1] & 0x7C) << 1;
			rgbout += 3;
		}
	}
	//8bit以下,位与移位取像素数据,查调色板,写RGB缓冲区
	RGBQUAD* pRGB = (RGBQUAD*)malloc(sizeof(RGBQUAD) * (unsigned int)pow((float)2, (float)info_header.biBitCount));
	if (!MakePalette(bmpfile, file_header, info_header, pRGB))
		printf("no palette");
	if (info_header.biBitCount <= 8)
	{
		for (loop = 0; loop < h * w; loop++)
		{
			switch (info_header.biBitCount)
			{
			case 1:
				mask = 0x80;
				break;
			case 2:
				mask = 0xC0;
				break;
			case 4:
				mask = 0xF0;
				break;
			case 8:
				mask = 0xFF;
			}
 
			int shiftcnt = 1;
			while (mask)
			{
				unsigned char index = mask == 0xFF ? data[loop] : ((data[loop] & mask) >> (8 - shiftcnt * info_header.biBitCount));
				*rgbout = pRGB[index].rgbBlue;
				*(rgbout + 1) = pRGB[index].rgbGreen;
				*(rgbout + 2) = pRGB[index].rgbRed;
				if (info_header.biBitCount == 8)
					mask = 0;
				else
					mask >>= info_header.biBitCount;
				rgbout += 3;
				shiftcnt++;
			}
		}
	}
}

调色板MakePalette

bool MakePalette(FILE* bmpfile, BITMAPFILEHEADER& file_header, BITMAPINFOHEADER& info_header, RGBQUAD* rgbout)
  {
	if ((file_header.bfOffBits - sizeof(BITMAPFILEHEADER) - info_header.biSize) == sizeof(RGBQUAD) * pow(2, info_header.biBitCount))
	{
		fseek(bmpfile, sizeof(BITMAPFILEHEADER) + info_header.biSize, 0);
		fread(rgbout, sizeof(RGBQUAD), (unsigned int)pow(2, info_header.biBitCount), bmpfile);
		return true;
	}
	else
		return false;

4.RGB2YUV.cpp

采用部分查找表法,提高运行效率

void initLookupTable()
{
	for (int i = 0; i < 256; i++)
	{
		RGBYUV02990[i] = (float)0.2990 * i;
		RGBYUV05870[i] = (float)0.5870 * i;
		RGBYUV01140[i] = (float)0.1140 * i;
		RGBYUV01684[i] = (float)0.1684 * i;
		RGBYUV03316[i] = (float)0.3316 * i;
		RGBYUV04187[i] = (float)0.4187 * i;
		RGBYUV00813[i] = (float)0.0813 * i;
	}
}
void adjust(unsigned char* b, unsigned char* g, unsigned char* r, unsigned char* y, unsigned char* u, unsigned char* v)
{
	float temp = 0;
	temp = (float)(RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
	temp > 235 ? 235 : temp;
	temp < 16 ? 16 : temp;
	*y = (unsigned char)temp;
 
	temp = (float)(-RGBYUV01684[*r] - RGBYUV03316[*g] +(*b)/2+128);
	temp > 240 ? 240 : temp;
	temp < 16 ? 16 : temp;
	*u = (unsigned char)temp;
 
	temp = (float)((*r)/2-RGBYUV04187[*g] - RGBYUV00813[*b]+ 128);
	temp > 240 ? 240 : temp;
	temp < 16 ? 16 : temp;
	*v = (unsigned char)temp;
}
void RGB2YUV(int width, int height, unsigned char* rgbbuf, unsigned char* ybuf, unsigned char* ubuf, unsigned char* vbuf)
{
	initLookupTable();
	long size;
	unsigned char *r, * g, * b;
	unsigned char *y, * u, * v;
 
	size = width * height;
	b = rgbbuf;
	y = ybuf;
	u = ubuf;
	v = vbuf;
	for (int i = 0; i < size; i++)
	{
		g = b + 1;
		r = b + 2;
		adjust(b,g,r,y,u,v);
		b += 3;
		y++;
		u++;
		v++;
	}
	ybuf = y;
	ubuf = u;
	vbuf = v;
}

5.DownYUV.cpp

void DownYUV(int width, int height, unsigned char* utmp, unsigned char* vtmp, unsigned char* ubuf, unsigned char* vbuf)
{
	unsigned char* ut = utmp;
	unsigned char* vt = vtmp;
	unsigned char* ub = ubuf;
	unsigned char* vb = vbuf;
	int k = 0;
	float u, v;
	for (int i = 0; i < height * width / 4; i++)
	{
		u = (float)(*(ut + k) + *(ut + k + 1) + *(ut + width + k) + *(ut + width + k + 1))/4.0;
		v = (float)(*(vt + k) + *(vt + k + 1) + *(vt + width + k) + *(vt + width + k + 1)) / 4.0;
		*ub = (unsigned char)u;
		*vb = (unsigned char)v;
		ub++;
		vb++;
		if ((i + 1) % (width / 2) == 0)
		{
			k = k + 2 + width;
		}
		else
			k = k + 2;
	}
	ubuf = ub;
	vbuf = vb;
}

四. 实验结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值