C语言实现RGB888转BMP格式图片功能

1、bmp格式介绍

bmp格式图片里实际存储的也是RGB原始数据,可以分为8bit、16bit、24bit、32bit的bmp格式,也就是指bmp图片中保存的RGB是用8bit、16bit、24bit、32bit来表示。简单理解就是在原始RGB的数据前面加bmp文件头,文件头里会说明保存的RGB的位数、宽高、文件大小等信息。更多bmp格式的细节参考博客:https://blog.csdn.net/aidem_brown/article/details/80500637

2、bmp格式的注意点

(1)保存的RGB原始数据,排列方式是BGR、BGR······格式;
(2)保存的RGB图像是倒立的,是从下往上一行一行保存的;
(3)RGB数据的一行长度要4字节对齐。比如:宽是102,那每行数据是102*3=306,这时候要向上对齐,每行是308,对的两个字节用0填充。

3、代码思路

(1)把RGB图像转换成BGR、BGR排列顺序;
(2)把BGR数据旋转180度,需要得到倒立的BGR图像;
(2)根据BGR图像的宽、高等信息构建bmp文件头;
(3)将bmp文件头、BGR数据依次写入到xxx.bmp文件中,得到bmp格式的图像;

4、实现代码

#define align(value, align)   ((( (value) + ( (align) - 1 ) ) \
                                     / (align) ) * (align) )

//BMP文件头(14字节)
typedef struct                       /**** BMP file header structure ****/  
{  
    unsigned int   bfSize;           /* Size of file */  
    unsigned short bfReserved1;      /* Reserved */  
    unsigned short bfReserved2;      /* ... */  
    unsigned int   bfOffBits;        /* Offset to bitmap data */  
} BITMAPFILEHEADER; 

//位图信息头(40字节)
typedef struct                       /**** BMP file info structure ****/  
{  
    unsigned int   biSize;           /* Size of info header */  
    int            biWidth;          /* Width of image */  
    int            biHeight;         /* Height of image */  
    unsigned short biPlanes;         /* Number of color planes */  
    unsigned short biBitCount;       /* Number of bits per pixel */  
    unsigned int   biCompression;    /* Type of compression to use */  
    unsigned int   biSizeImage;      /* Size of image data */  
    int            biXPelsPerMeter;  /* X pixels per meter */  
    int            biYPelsPerMeter;  /* Y pixels per meter */  
    unsigned int   biClrUsed;        /* Number of colors used */  
    unsigned int   biClrImportant;   /* Number of important colors */  
} BITMAPINFOHEADER;  


/*******************************************************************************
* 函数名  : BGR888_TO_BMP
* 描  述	: 将BGR888格式的图片转换成bmp格式
* 输  入	: dstLength	--输出BMP数据的缓存空间长度
*		  	  bgrbuf	--BGR888数据,需要倒立的图像
*		  	  width		--BGR图像的宽
*		  	  height	--BGR图像的高
* 输  出	: dst	--保存BMP图像
* 返回值    : 成功返回 0
*		   失败返回 -1
*******************************************************************************/
int BGR888_TO_BMP(char *dst, int dstLength, unsigned char *bgrbuf,int width,int height)  
{  
    BITMAPFILEHEADER bfh;  
    BITMAPINFOHEADER bih;  
	int i = 0, row_align;

	if(dstLength < width * height * 3 + 54)
	{
		printf("dst buffer is too small!\n");
		return OSA_EFAIL;
	}

	//bmp的每行数据的长度要4字节对齐
	row_align = align(width * 3, 4);
    unsigned short bfType=0x4d42;    //'BM'             
    bfh.bfReserved1 = 0;  
    bfh.bfReserved2 = 0;  
    bfh.bfSize = 2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + row_align * height;  
    bfh.bfOffBits = 2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
  
    bih.biSize = sizeof(BITMAPINFOHEADER);  
    bih.biWidth = width;  
    bih.biHeight = height;  
    bih.biPlanes = 1;  
    bih.biBitCount = 24;  
    bih.biCompression = 0;  
    bih.biSizeImage = 0;  
    bih.biXPelsPerMeter = 0;  
    bih.biYPelsPerMeter = 0;  
    bih.biClrUsed = 0;  
    bih.biClrImportant = 0;  

	memset(dst, 0, dstLength);

	memcpy(dst, &bfType, sizeof(bfType));
	memcpy(dst + sizeof(bfType), &bfh, sizeof(bfh));
	memcpy(dst + sizeof(bfType) + sizeof(bfh), &bih, sizeof(bih));

	for(i = 0; i < height; i++)
	{
		memcpy(dst + sizeof(bfType) + sizeof(bfh) + sizeof(bih) + row_align * i, bgrbuf + width * 3 * i, width * 3);
	}
	
	return 0;
}  
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正在起飞的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值