20230201_1811_emb/1811Project_LPc/Src/Usr/Tool/ToolRgb565ToBmp.c
Rjh913828050 78f3903d1d 类型:重构
内容:1811项目下位机软件第一版完整程序
人员:任家豪
2023-10-08 13:56:23 +08:00

133 lines
5.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ToolRgb565ToBmp.h"
#include <stdio.h>
#pragma pack (1)
/***********
第一部分 位图文件头
该结构的长度是固定的为14个字节各个域的依次如下
2byte 文件类型必须是0x4d42即字符串"BM"。
4byte :整个文件大小
4byte 保留字为0
4byte :从文件头到实际的位图图像数据的偏移字节数。
*************/
typedef struct {
unsigned short FileType;
unsigned int ImageSize;
unsigned int Blank;
unsigned int StartPosition;
} BMP_HEAD;
/***********
第二部分 位图信息头
该结构的长度也是固定的为40个字节各个域的依次说明如下
4byte 本结构的长度值为40
4byte :图像的宽度是多少象素。
4byte :图像的高度是多少象素。
2Byte 必须是1。
2Byte :表示颜色时用到的位数,
常用的值为1(黑白二色图)、4(16色图)、8(256色图)、24(真彩色图)。
4byte :指定位图是否压缩,
有效值为BI_RGBBI_RLE8BI_RLE4BI_BITFIELDS。
Windows位图可采用RLE4和RLE8的压缩格式BI_RGB表示不压缩。
4byte :指定实际的位图图像数据占用的字节数,可用以下的公式计算出来:
图像数据 = Width' * Height * 表示每个象素颜色占用的byte数(即颜色位数/8,24bit图为3256色为1
要注意的是上述公式中的biWidth'必须是4的整数倍
(不是biWidth而是大于或等于biWidth的最小4的整数倍)。
如果biCompression为BI_RGB则该项可能为0。
4byte :目标设备的水平分辨率。
4byte :目标设备的垂直分辨率。
4byte :本图像实际用到的颜色数,
如果该值为0则用到的颜色数为2的(颜色位数)次幂,
如颜色位数为82^8=256,即256色的位图
4byte 指定本图像中重要的颜色数如果该值为0则认为所有的颜色都是重要的。
***********************************/
typedef struct {
unsigned int Length;
unsigned int Width;
unsigned int Height;
unsigned short ColorPlane;
unsigned short BitColor;
unsigned int ZipFormat;
unsigned int RealSize;
unsigned int Xpels;
unsigned int Ypels;
unsigned int ColorUse;
unsigned int ColorImportant;
} BMP_INFO_HEAD;
/***************************
第三部分 调色盘结构 颜色表
对于256色BMP位图颜色位数为8需要2^8 = 256个调色盘
对于24bitBMP位图各象素RGB值直接保存在图像数据区不需要调色盘不存在调色盘区
rgbBlue 该颜色的蓝色分量。
rgbGreen 该颜色的绿色分量。
rgbRed 该颜色的红色分量。
rgbReserved保留值。
************************/
typedef struct {
unsigned char RgbBlue;
unsigned char RgbGreen;
unsigned char RgbRed;
unsigned char Reserve;
} RGB_MIX_PLATE;
#pragma pack()
/****************************
写入BMP图片的信息
参数說明:
nWidth :图像宽度的像素数
nHeight :图像高度的像素数
fp1 :所存放的文件
*****************************/
int ToolRgb565ToBmpInfo(unsigned int nWidth, unsigned int nHeight, FIL *fp1)
{
BMP_HEAD BmpHeader;
BMP_INFO_HEAD BmpInfoHeader;
unsigned int WritenLen = 0;
/* 写入文件属性信息 */
BmpHeader.FileType = 0x4d42;
BmpHeader.ImageSize = nWidth * nHeight * 3 + 54;
BmpHeader.Blank = 0;
BmpHeader.StartPosition = 54;
f_write(fp1, &BmpHeader.FileType, sizeof(BmpHeader.FileType), &WritenLen);
f_write(fp1, &BmpHeader.ImageSize, sizeof(BmpHeader.ImageSize), &WritenLen);
f_write(fp1, &BmpHeader.Blank, sizeof(BmpHeader.Blank), &WritenLen);
f_write(fp1, &BmpHeader.StartPosition, sizeof(BmpHeader.StartPosition), &WritenLen);
/* 写入位图信息 */
BmpInfoHeader.Length = 40; // 固定值
BmpInfoHeader.Width = nWidth;
BmpInfoHeader.Height = nHeight;
BmpInfoHeader.ColorPlane = 1; // 固定值
BmpInfoHeader.BitColor = 24;
BmpInfoHeader.ZipFormat = 0;
BmpInfoHeader.RealSize = nWidth * nHeight * 3;
BmpInfoHeader.Xpels = 0;
BmpInfoHeader.Ypels = 0;
BmpInfoHeader.ColorUse = 0;
BmpInfoHeader.ColorImportant = 0;
f_write(fp1, &BmpInfoHeader.Length, sizeof(BmpInfoHeader.Length), &WritenLen);
f_write(fp1, &BmpInfoHeader.Width, sizeof(BmpInfoHeader.Width), &WritenLen);
f_write(fp1, &BmpInfoHeader.Height, sizeof(BmpInfoHeader.Height), &WritenLen);
f_write(fp1, &BmpInfoHeader.ColorPlane, sizeof(BmpInfoHeader.ColorPlane), &WritenLen);
f_write(fp1, &BmpInfoHeader.BitColor, sizeof(BmpInfoHeader.BitColor), &WritenLen);
f_write(fp1, &BmpInfoHeader.ZipFormat, sizeof(BmpInfoHeader.ZipFormat), &WritenLen);
f_write(fp1, &BmpInfoHeader.RealSize, sizeof(BmpInfoHeader.RealSize), &WritenLen);
f_write(fp1, &BmpInfoHeader.Xpels, sizeof(BmpInfoHeader.Xpels), &WritenLen);
f_write(fp1, &BmpInfoHeader.Ypels, sizeof(BmpInfoHeader.Ypels), &WritenLen);
f_write(fp1, &BmpInfoHeader.ColorUse, sizeof(BmpInfoHeader.ColorUse), &WritenLen);
f_write(fp1, &BmpInfoHeader.ColorImportant, sizeof(BmpInfoHeader.ColorImportant), &WritenLen);
return 0;
}
/****************************
RGB加上头部信息转换成BMP
参数說明:
rgb_buffer :RGB数据文件中的信息
RgbBufferLen :RGB数据的长度
fp1 :所存放的文件
*****************************/
int ToolRgb565ToBmpByte(char *RgbBuffer, uint32_t RgbBufferLen, FIL *fp1)
{
unsigned int WritenLen = 0;
f_write(fp1, RgbBuffer, RgbBufferLen, &WritenLen);
return 0;
}