20240801 C++ 保存二进制图像数据为bmp

特点:

  • C++单个头文件,include 可用;
  • 单例模式,不需要 new instance;

使用方式:

bmpWriter.saveBinaryDataAsBMP(path + ".bmp", static_cast<const unsigned char *>(ptr), m_width, m_height, m_channel);

头文件内容

// *********************************************
// FileName: bmp_writer.hpp
// Brief:
//
// Date:   30 Jul 2024
// Author: Dan
// *********************************************

#ifndef TOOLS_BMP_WRITER_HPP
#define TOOLS_BMP_WRITER_HPP

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

namespace TOOLS {
// BMP file header
#pragma pack(push, 1)
struct BMPHeader {
    uint16_t bfType      = 0;
    uint32_t bfSize      = 0;
    uint16_t bfReserved1 = 0;
    uint16_t bfReserved2 = 0;
    uint32_t bfOffBits   = 0;
};
#pragma pack(pop)

// BMP information header
#pragma pack(push, 1)
struct BMPInfoHeader {
    uint32_t biSize          = 0;
    int32_t  biWidth         = 0;
    int32_t  biHeight        = 0;
    uint16_t biPlanes        = 0;
    uint16_t biBitCount      = 0;
    uint32_t biCompression   = 0;
    uint32_t biSizeImage     = 0;
    int32_t  biXPelsPerMeter = 0;
    int32_t  biYPelsPerMeter = 0;
    uint32_t biClrUsed       = 0;
    uint32_t biClrImportant  = 0;
};
#pragma pack(pop)

class BMPWriter {
private:
    size_t grayPaletteSize = 256 * 4;

    // gray image palette, for gray image only!
    unsigned char grayPalette[256 * 4] = {0};

public:
    // for single channel image
    BMPWriter()
    {
        for (int i = 0; i < 256; ++i)
        {
            grayPalette[i * 4]     = i;
            grayPalette[i * 4 + 1] = i;
            grayPalette[i * 4 + 2] = i;
            grayPalette[i * 4 + 3] = 0;
        }
    }

    ~BMPWriter() = default;

    static BMPWriter &getInstance()
    {
        static BMPWriter instance;
        return instance;
    }

    void saveBinaryDataAsBMP(const std::string filename, const unsigned char *data, int width, int height, int depth)
    {
        std::ofstream file(filename, std::ios::binary);

        if (!file)
        {
            std::cerr << "Failed to open file for writing." << std::endl;
            return;
        }

        BMPHeader bmpHeader;
        bmpHeader.bfType      = 0x4D42;  // "BM"
        bmpHeader.bfSize      = sizeof(BMPHeader) + sizeof(BMPInfoHeader) + (1 == depth ? grayPaletteSize : 0) + width * height * depth;
        bmpHeader.bfReserved1 = 0;
        bmpHeader.bfReserved2 = 0;
        bmpHeader.bfOffBits   = sizeof(BMPHeader) + sizeof(BMPInfoHeader) + (1 == depth ? grayPaletteSize : 0);

        BMPInfoHeader bmpInfoHeader;
        bmpInfoHeader.biSize          = sizeof(BMPInfoHeader);
        bmpInfoHeader.biWidth         = width;
        bmpInfoHeader.biHeight        = height;
        bmpInfoHeader.biPlanes        = 1;
        bmpInfoHeader.biBitCount      = 8 * depth;
        bmpInfoHeader.biCompression   = 0;
        bmpInfoHeader.biSizeImage     = width * height * depth;
        bmpInfoHeader.biXPelsPerMeter = 0;
        bmpInfoHeader.biYPelsPerMeter = 0;
        bmpInfoHeader.biClrUsed       = 0;
        bmpInfoHeader.biClrImportant  = 0;

        file.write(reinterpret_cast<const char *>(&bmpHeader), sizeof(BMPHeader));
        file.write(reinterpret_cast<const char *>(&bmpInfoHeader), sizeof(BMPInfoHeader));
        if (1 == depth)
        {
            file.write(reinterpret_cast<const char *>(grayPalette), sizeof(grayPalette));
        }

        for (int y = height - 1; y >= 0; y--)
        {
            for (int x = 0; x < width; x++)
            {
                file.write(reinterpret_cast<const char *>(data + (y * width + x) * depth), depth);
            }
        }

        file.close();
    }
};

static BMPWriter &bmpWriter = BMPWriter::getInstance();

}

#endif //TOOLS_BMP_WRITER_HPP

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 将Byte*图像转换为二进制数据: ```c++ unsigned char* imageData = // Byte*图像数据 int imageSize = // 图像数据的大小 std::vector<unsigned char> binaryData(imageSize * 8); for (int i = 0; i < imageSize; i++) { for (int j = 0; j < 8; j++) { binaryData[i * 8 + j] = (imageData[i] >> j) & 1; } } ``` 2. 将二进制数据保存图像文件: ```c++ int width = // 图像的宽度 int height = // 图像的高度 std::ofstream outFile("binary_image.bmp", std::ios::binary); if (outFile) { // BMP文件头 char bmpFileHeader[14] = { 'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0 }; // BMP信息头 char bmpInfoHeader[40] = { 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // 计算图像数据的大小 int imageDataSize = ((width * height * 1 + 31) / 32) * 4; // BMP文件头中的文件大小 *(int*)&bmpFileHeader[2] = 54 + imageDataSize; // BMP信息头中的图像宽度 *(int*)&bmpInfoHeader[4] = width; // BMP信息头中的图像高度 *(int*)&bmpInfoHeader[8] = height; // BMP信息头中的图像数据大小 *(int*)&bmpInfoHeader[20] = imageDataSize; // 写入BMP文件头和BMP信息头 outFile.write(bmpFileHeader, 14); outFile.write(bmpInfoHeader, 40); // 写入图像数据 for (int i = height - 1; i >= 0; i--) { for (int j = 0; j < width; j++) { unsigned char pixel = 255 * binaryData[i * width + j]; outFile.write((char*)&pixel, 1); } // 每行的字节数必须是4的倍数 int paddingSize = ((width * 1 + 3) / 4) * 4 - width; if (paddingSize > 0) { char padding[3] = { 0, 0, 0 }; outFile.write(padding, paddingSize); } } outFile.close(); } ``` 保存图像文件是黑白图像,每个像素只有一个二进制值,它表示原始图像中的一个字节的每个位的值。其中,值为1的像素是白色的,值为0的像素是黑色的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值