构造100x100的黑白图像的BMP文件(50行黑50行白)

如题目,使用c++。

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;  // 使用命名空间 std

#pragma pack(push, 1)

// BMP文件头
struct BitmapFileHeader {
    uint16_t type;
    uint32_t size;
    uint16_t reserved1;
    uint16_t reserved2;
    uint32_t offset;
};

// BMP信息头
struct BitmapInfoHeader {
    uint32_t size;
    int32_t width;
    int32_t height;
    uint16_t planes;
    uint16_t bitCount;
    uint32_t compression;
    uint32_t sizeImage;
    int32_t xPelsPerMeter;
    int32_t yPelsPerMeter;
    uint32_t clrUsed;
    uint32_t clrImportant;
};

#pragma pack(pop)

int main() {
    int width = 100;
    int height = 100;

    vector<uint8_t> imageData(width * height, 255);  // 初始化为白色
    for (int i = 0; i < 50 * width; ++i) {
        imageData[i] = 0;  // 设置前50行为黑色
    }

    // 创建BMP文件头
    BitmapFileHeader fileHeader;
    fileHeader.type = 0x4D42;  // "BM"
    fileHeader.size = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + 256 * 4 + imageData.size();
    fileHeader.reserved1 = 0;
    fileHeader.reserved2 = 0;
    fileHeader.offset = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + 256 * 4;

    // 创建BMP信息头
    BitmapInfoHeader infoHeader;
    infoHeader.size = sizeof(BitmapInfoHeader);
    infoHeader.width = width;
    infoHeader.height = -height;  // 注意高度为负值
    infoHeader.planes = 1;
    infoHeader.bitCount = 8;  // 8位色深
    infoHeader.compression = 0;
    infoHeader.sizeImage = imageData.size();
    infoHeader.xPelsPerMeter = 0;
    infoHeader.yPelsPerMeter = 0;
    infoHeader.clrUsed = 0;
    infoHeader.clrImportant = 0;

    // 写入BMP文件
    ofstream bmpFile("output.bmp", ios::binary);
    bmpFile.write(reinterpret_cast<char*>(&fileHeader), sizeof(fileHeader));
    bmpFile.write(reinterpret_cast<char*>(&infoHeader), sizeof(infoHeader));

    for (int i = 0; i < 256; ++i) {
        bmpFile.put(static_cast<char>(i));
        bmpFile.put(static_cast<char>(i));
        bmpFile.put(static_cast<char>(i));
        bmpFile.put(0);  // 保留字段,设为0
    }

    bmpFile.write(reinterpret_cast<char*>(imageData.data()), imageData.size());
    bmpFile.close();

    cout << "BMP文件已生成为output.bmp" << endl;

    return 0;
}

解释:

  1. BMP文件头(BitmapFileHeader):
    type:这是一个16位字段,通常为0x4D42,表示文件类型为BMP。
    size:32位字段,表示整个BMP文件的大小,包括文件头、信息头、颜色表和像素数据。
    reserved1 和 reserved2:两个16位字段,保留字段,通常设置为0。
    offset:32位字段,表示位图数据的偏移量,也就是颜色表结束后,真正的像素数据开始的位置。
    BMP文件头告诉查看器或解析器文件的基本信息,如文件类型和像素数据的位置。

  2. BMP信息头(BitmapInfoHeader):
    size:32位字段,表示信息头的大小,一般为40字节。
    width 和 height:32位字段,分别表示图像的宽度和高度(以像素为单位)。
    planes:16位字段,始终设置为1,表示位平面的数量。
    bitCount:16位字段,表示每个像素的位数(色深)。在黑白图像中,通常设置为8位。
    compression:32位字段,表示压缩方式,通常为0表示不压缩。
    sizeImage:32位字段,表示图像数据部分的大小,通常等于像素数据的大小。
    xPelsPerMeter 和 yPelsPerMeter:32位字段,表示水平和垂直分辨率,一般为0。
    clrUsed:32位字段,表示颜色表中的颜色数。在黑白图像中,通常设置为0。
    clrImportant:32位字段,表示重要颜色数,通常设置为0。
    BMP信息头包含了有关图像的详细信息,如尺寸、颜色深度、压缩方式等。这些信息在文件的头部,使解析器能够正确读取和显示图像。

== 待续==

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值