bmp转yuv


数据压缩实验二

1.实验原理:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.实验材料:

10张带有自己姓名学号的bmp图片。
在这里插入图片描述

3.代码实现:

主函数:

#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include"bmp2yuv.h"

int main(char argc,char *argv[])
{
	int height,width;
	BITMAPFILEHEADER File_header;
	BITMAPINFOHEADER Info_header;
	char* bmpfile_name[10];
	char* yuvfile_name=NULL;
	FILE* bmp;
	FILE* yuv;
	unsigned char *rgb_buffer,*y_buffer,*u_buffer,*v_buffer;
	bool flip=true;

	yuvfile_name=argv[11];
	yuv=fopen(yuvfile_name,"wb");
		if(yuv==NULL)
		{
			printf("cannot find yuv file!\n");
			exit(1);
		}
		else
		{
			printf("The input yuv file is %s\n",yuvfile_name);
		}

	/*一共10张bmp图片*/
	for(int num=0;num<10;num++)
	{
		bmpfile_name[num]=argv[num+1];
	}
	
	height=atoi(argv[12]);
	width=atoi(argv[13]);

	for(int i=0;i<10;i++)
	{
		bmp=fopen(bmpfile_name[i],"rb");
	
		if(bmp==NULL)
		{
			printf("cannot find bmp file!\n");
			exit(1);
		}
		else
		{
			printf("The input bmp file is %s\n",bmpfile_name[i]);
		}

		if((fread(&File_header,sizeof(BITMAPFILEHEADER),1,bmp))!=1)
		{
			printf("Read File header error!");
			exit(0);
		}

		/*判断是否为bmp文件*/
		if(File_header.bfType!=0x4D42)
		{
			printf("Not bmp file!");
			exit(0);
		}
		else
		{
			printf("This is bmp file!\n");
		}

		if((fread(&Info_header,sizeof(BITMAPINFOHEADER),1,bmp))!=1)
		{
			printf("Read Info header error!");
			exit(0);
		}

		/*判断行数是否为4的倍数*/
		if(Info_header.biWidth%4==0)
		{
			width=Info_header.biWidth;
		}
		else
		{
			width=(Info_header.biWidth*Info_header.biBitCount+31)/32*(32/8); 
			//每行的像素数*每个像素的位数=每行像素的总位数,必须为32bit的整数倍(4字节)
		}
	
		/*判断列数是否为偶数*/
		if(Info_header.biHeight%2==0)
		{
			height=Info_header.biHeight;
		}
		else
		{
			height=Info_header.biHeight+1;
		}

		int size=width*height;
		rgb_buffer=(unsigned char*)malloc(size*3);
		y_buffer=(unsigned char*)malloc(size);
		u_buffer=(unsigned char*)malloc(size/4);
		v_buffer=(unsigned char*)malloc(size/4);

		/*从bmp得到rgb数据*/
		readbmp(bmp,rgb_buffer,File_header,Info_header);
		
		
		/*rgb转yuv*/
		if(RGB2YUV (width, height, rgb_buffer, y_buffer, u_buffer, v_buffer, flip))
		{
			printf("rgb2yuv failed!\n");
		}
		else
		{
			printf("rgb2yuv successfully!\n");
		}

		int j=0;
		for(j=0;j<size;j++)
		{
			if(y_buffer[j]<16) y_buffer[j]=16;
			if(y_buffer[j]>235) y_buffer[j]=235;
		}

		for(j=0;j<size/4;j++)
		{
			if(u_buffer[j]<16) y_buffer[j]=16;
			if(u_buffer[j]>240) y_buffer[j]=240;
		}

		for(j=0;j<size/4;j++)
		{
			if(v_buffer[j]<16) v_buffer[j]=16;
			if(v_buffer[j]>240) v_buffer[j]=240;
		}
		/*每张bmp图片播放30帧*/
		for(int k=0;k<30;k++)
		{
			fwrite(y_buffer,1,size,yuv);
			fwrite(u_buffer,1,size/4,yuv);
			fwrite(v_buffer,1,size/4,yuv);
		}
		free(rgb_buffer);
		free(y_buffer);
		free(u_buffer);
		free(v_buffer);	
	}
	
		fclose(yuv);
	return 0;
}

rgb2yuv.cpp:

#include "stdlib.h"

static float RGBYUV02990[256], RGBYUV05870[256], RGBYUV01140[256];
static float RGBYUV01684[256], RGBYUV03316[256];
static float RGBYUV04187[256], RGBYUV00813[256];

void InitLookupTable()
{
	int i;

	for (i = 0; i < 256; i++) RGBYUV02990[i] = (float)0.2990 * i;
	for (i = 0; i < 256; i++) RGBYUV05870[i] = (float)0.5870 * i;
	for (i = 0; i < 256; i++) RGBYUV01140[i] = (float)0.1140 * i;
	for (i = 0; i < 256; i++) RGBYUV01684[i] = (float)0.1684 * i;
	for (i = 0; i < 256; i++) RGBYUV03316[i] = (float)0.3316 * i;
	for (i = 0; i < 256; i++) RGBYUV04187[i] = (float)0.4187 * i;
	for (i = 0; i < 256; i++) RGBYUV00813[i] = (float)0.0813 * i;
}

int RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out, int flip)
{
	static int init_done = 0;

	long i, j, size;
	unsigned char *r, *g, *b;
	unsigned char *y, *u, *v;
	unsigned char *pu1, *pu2, *pv1, *pv2, *psu, *psv;
	unsigned char *y_buffer, *u_buffer, *v_buffer;
	unsigned char *sub_u_buf, *sub_v_buf;

	if (init_done == 0)
	{
		InitLookupTable();
		init_done = 1;
	}

	// check to see if x_dim and y_dim are divisible by 2
	if ((x_dim % 2) || (y_dim % 2)) return 1;
	size = x_dim * y_dim;

	// allocate memory
	y_buffer = (unsigned char *)y_out;
	sub_u_buf = (unsigned char *)u_out;
	sub_v_buf = (unsigned char *)v_out;
	u_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
	v_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
	if (!(u_buffer && v_buffer))
	{
		if (u_buffer) free(u_buffer);
		if (v_buffer) free(v_buffer);
		return 2;
	}

	b = (unsigned char *)bmp;
	y = y_buffer;
	u = u_buffer;
	v = v_buffer;

	// convert RGB to YUV
	if (!flip) {
		for (j = 0; j < y_dim; j ++)
		{
			y = y_buffer + (y_dim - j - 1) * x_dim;
			u = u_buffer + (y_dim - j - 1) * x_dim;
			v = v_buffer + (y_dim - j - 1) * x_dim;

			for (i = 0; i < x_dim; i ++) 
			{
				g = b + 1;
				r = b + 2;
				*y = (unsigned char)(  RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
				*u = (unsigned char)(- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2          + 128);
				*v = (unsigned char)(  (*r)/2          - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
				b += 3;
				y ++;
				u ++;
				v ++;
			}
		}
	} else {
		for (i = 0; i < size; i++)
		{
			g = b + 1;
			r = b + 2;
			*y = (unsigned char)(  RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
			*u = (unsigned char)(- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2          + 128);
			*v = (unsigned char)(  (*r)/2          - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
			b += 3;
			y ++;
			u ++;
			v ++;
		}
	}

	// subsample UV
	for (j = 0; j < y_dim/2; j ++)
	{
		psu = sub_u_buf + j * x_dim / 2;
		psv = sub_v_buf + j * x_dim / 2;
		pu1 = u_buffer + 2 * j * x_dim;
		pu2 = u_buffer + (2 * j + 1) * x_dim;
		pv1 = v_buffer + 2 * j * x_dim;
		pv2 = v_buffer + (2 * j + 1) * x_dim;
		for (i = 0; i < x_dim/2; i ++)
		{
			*psu = (*pu1 + *(pu1+1) + *pu2 + *(pu2+1)) / 4;
			*psv = (*pv1 + *(pv1+1) + *pv2 + *(pv2+1)) / 4;
			psu ++;
			psv ++;
			pu1 += 2;
			pu2 += 2;
			pv1 += 2;
			pv2 += 2;
		}
	}

	free(u_buffer);
	free(v_buffer);

	return 0;
}

readbmp.cpp:

#include"stdlib.h"
#include"stdio.h"
#include"windows.h"
#include"malloc.h"

void readbmp(FILE* bmp,unsigned char* rgb_out,BITMAPFILEHEADER &file_header,BITMAPINFOHEADER &info_header)
{
	int width,height,w,h;
	unsigned char *inverse_buffer,*buffer;
	int x=0,y=0;

	//行数为4的倍数
	if(info_header.biWidth%4==0)
	{
		w=info_header.biWidth;
	}
	else
	{
		w=(info_header.biWidth*info_header.biBitCount+31)/32*(32/8);
	}

	//列数为偶数
	if(info_header.biHeight%2==0)
	{
		h=info_header.biHeight;
	}
	else
	{
		h=info_header.biHeight+1;
	}

	width=w/8*info_header.biBitCount;
	height=h;

	inverse_buffer=(unsigned char*)malloc(height*width);
	buffer=(unsigned char*)malloc(height*width);
	
	fseek(bmp,file_header.bfOffBits,0);
	fread(inverse_buffer,height*width,1,bmp);

	for(y=0;y<height;y++)
	{
		for(x=0;x<width;x++)
		{
		buffer[y*width+x]=inverse_buffer[width*(height-y-1)+x];
		}
	}

	if(info_header.biBitCount==24)
	{
		memcpy(rgb_out,buffer,height*width);
		free(inverse_buffer);
		free(buffer);
	}
}

头文件:

#include"stdio.h"
#include"windows.h"
void readbmp(FILE* bmp,unsigned char* rgb_out,BITMAPFILEHEADER &file_header,BITMAPINFOHEADER &info_header);
int RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out, int flip);
void InitLookupTable();

4.运行结果:

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值