BMP基础

BMP概述

BMP是比较常见的使用RGB模式的文件格式,BMP全称Bitmap-File,是微软出的图像文件格式。

BMP 格式 由以下部分组成。

  1. BMP文件头(14 bytes) ,存放一些文件相关的信息。
  2. BMP信息头,通常是 40 bytes 大小,也可以理解成图像信息头,存放一些图像相关的信息,例如宽高之类的数据。
  3. 调色板,大小由颜色索引决定。
  4. 位图数据,里面就是RGB数据,一个像素占3个字节。

所以,将rgb24图像转为BMP格式,只需要以下步骤:

  1. 将RGB数据前面加上文件头。
  2. 将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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值