BMP概述
BMP是比较常见的使用RGB模式的文件格式,BMP全称Bitmap-File,是微软出的图像文件格式。
BMP 格式 由以下部分组成。
- BMP文件头(14 bytes) ,存放一些文件相关的信息。
- BMP信息头,通常是 40 bytes 大小,也可以理解成图像信息头,存放一些图像相关的信息,例如宽高之类的数据。
- 调色板,大小由颜色索引决定。
- 位图数据,里面就是RGB数据,一个像素占3个字节。
所以,将rgb24图像转为BMP格式,只需要以下步骤:
- 将RGB数据前面加上文件头。
- 将RGB数据中每个像素的“B”和“R”的位置互换。
BMP文件是由BITMAPFILEHEADER、BITMAPINFOHEADER、RGB像素数据共3个部分构成,它的结构如下图所示。
typedef struct tagBITMAPFILEHEADER
{
unsigned short int bfType; //位图文件的类型,必须为BM
unsigned long bfSize; //文件大小,以字节为单位
unsigned short int bfReserverd1; //位图文件保留字,必须为0
unsigned short int bfReserverd2; //位图文件保留字,必须为0
unsigned long bfbfOffBits; //位图文件头到数据的偏移量,以字节为单位
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
long biSize; //该结构大小,字节为单位
long biWidth; //图形宽度以象素为单位
long biHeight; //图形高度以象素为单位
short int biPlanes; //目标设备的级别,必须为1
short int biBitcount; //颜色深度,每个象素所需要的位数
short int biCompression; //位图的压缩类型
long biSizeImage; //位图的大小,以字节为单位
long biXPelsPermeter; //位图水平分辨率,每米像素数
long biYPelsPermeter; //位图垂直分辨率,每米像素数
long biClrUsed; //位图实际使用的颜色表中的颜色数
long biClrImportant; //位图显示过程中重要的颜色数
}BITMAPINFOHEADER;
RGB24转BMP实现
#include <iostream>
#include <fstream>
#include <vector>
#pragma pack(push, 1)
struct BMPHeader {
char signature[2] = {'B', 'M'}; // BMP file signature
uint32_t fileSize; // File size in bytes
uint16_t reserved1 = 0; // Reserved (set to zero)
uint16_t reserved2 = 0; // Reserved (set to zero)
uint32_t offset; // Offset in bytes from the beginning of the file to the image data
uint32_t size; // Size of this header in bytes
int32_t width; // Width of the image in pixels
int32_t height; // Height of the image in pixels
uint16_t planes = 1; // Number of color planes (must be 1)
uint16_t bitCount = 24; // Number of bits per pixel
uint32_t compression = 0; // Compression method being used
uint32_t imageSize = 0; // Image size (set to zero for uncompressed images)
int32_t xPixelsPerMeter = 0; // Horizontal resolution of the image in pixels per meter
int32_t yPixelsPerMeter = 0; // Vertical resolution of the image in pixels per meter
uint32_t colorsUsed = 0; // Number of colors in the color palette, or 0 to default to 2^n
uint32_t importantColors = 0; // Number of important colors used, or 0 when every color is important
};
#pragma pack(pop)
std::vector<char> readRGB24(const std::string& filePath, int width, int height) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
std::cout << "Failed to open file" << std::endl;
return std::vector<char>();
}
int dataSize = width * height * 3;
std::vector<char> rgb24Data(dataSize);
file.read(rgb24Data.data(), dataSize);
return rgb24Data;
}
void convertRGB24ToBMP(std::vector<char>& rgb24Data, const std::string& fileName, int width, int height) {
BMPHeader header;
header.fileSize = sizeof(header) + width * height * 3;
header.offset = sizeof(header);
header.size = sizeof(header) - 14;
header.width = width;
header.height = -height;
std::ofstream file(fileName, std::ios::binary);
if (!file) {
std::cout << "Failed to open file" << std::endl;
return;
}
file.write(reinterpret_cast<const char*>(&header), sizeof(header));
for (int i = 0; i < width * height; ++i) {
char temp = rgb24Data[i * 3]; // Swap R and B
rgb24Data[i * 3] = rgb24Data[i * 3 + 2];
rgb24Data[i * 3 + 2] = temp;
}
file.write(rgb24Data.data(), width * height * 3);
std::cout << "BMP file created: " << fileName << std::endl;
}
int main() {
std::string filePath = "inRgb.rgb"; // Replace with the path to the RGB24 file
int width = 100;
int height = 100;
std::vector<char> rgb24Data = readRGB24(filePath, width, height);
if (rgb24Data.empty()) {
return 0;
}
convertRGB24ToBMP(rgb24Data, "outBmp.bmp", width, height);
return 0;
}