【问题标题】:I have a RGBA image file which I want to convert to BMP in C++我有一个 RGBA 图像文件,我想用 C++ 将其转换为 BMP
【发布时间】:2017-01-12 01:12:03
【问题描述】:

我是解释图像格式的新手。 我已阅读有关 imagemagick 的信息,但是否还有其他可用的库可供我使用?
还是很像读取 rgba 文件并重新排列数据。任何代码sn-p? 这里的任何帮助都会有很大帮助。

编辑:我在下面编写了将 RGBA 图像转换为 BMP 的代码,但查看时输出图像无效。我可以在这里寻求帮助吗? @bRad Gibson IntegerfileToBMP 推荐的帖子

#include <iostream>
#include <fstream>
#include <cstdint>
#include <iterator>
#include <vector>
using namespace std;

typedef unsigned char  BYTE; // 1byte
typedef unsigned short  WORD; // 2bytes uint16_t
typedef unsigned long  DWORD; //4bytes uint32_t
typedef unsigned long int LONG; 

struct BMP
{
    BMP();
    struct
    {
        WORD ID;
        DWORD fileSizeInBytes;
        WORD reserved1;
        WORD reserved2;
        DWORD pixelArrayOffsetInBytes;
    } FileHeader;

    enum class CompressionMethod : DWORD {   BI_RGB              = 0x00, 
                                                BI_RLE8             = 0x01,
                                                BI_RLE4             = 0x02,
                                                BI_BITFIELDS        = 0x03,
                                                BI_JPEG             = 0x04,
                                                BI_PNG              = 0x05,
                                                BI_ALPHABITFIELDS   = 0x06 };

    struct
    {
        DWORD headerSizeInBytes;
        DWORD bitmapWidthInPixels;
        DWORD bitmapHeightInPixels;
        WORD colorPlaneCount;
        WORD bitsPerPixel;
        CompressionMethod compressionMethod;
        DWORD bitmapSizeInBytes;
        int32_t horizontalResolutionInPixelsPerMeter;
        int32_t verticalResolutionInPixelsPerMeter;
        DWORD paletteColorCount;
        DWORD importantColorCount;
    } DIBHeader;
};

BMP::BMP()
{
    //Initialized fields
    FileHeader.ID                                   = 0x4d42; // == 'BM' (little-endian)
    FileHeader.reserved1                            = 0;
    FileHeader.reserved2                            = 0;
    FileHeader.pixelArrayOffsetInBytes              = sizeof( FileHeader ) + sizeof( DIBHeader );
    DIBHeader.headerSizeInBytes                     = 40;
    DIBHeader.colorPlaneCount                       = 1;
    DIBHeader.bitsPerPixel                          = 32;
    DIBHeader.compressionMethod                     = CompressionMethod::BI_RGB;
    DIBHeader.horizontalResolutionInPixelsPerMeter  = 2835; // == 72 ppi
    DIBHeader.verticalResolutionInPixelsPerMeter    = 2835; // == 72 ppi
    DIBHeader.paletteColorCount                     = 0;
    DIBHeader.importantColorCount                   = 0;
}

int main()
{
    const std::string rgbaFilename = "rgbaFile.rgba";
    const std::string bitmapFilename = "bitmapFile.bmp";

    //Read RGBA file
    ifstream readRGBA(rgbaFilename, ios::binary);

    if(!readRGBA)
        return 0;

    std::vector<char> buffer((
            std::istreambuf_iterator<char>(readRGBA)), 
            (std::istreambuf_iterator<char>()));

    //Construct .bmp
    BMP bmp;
    size_t charCount = buffer.size();

    bmp.DIBHeader.bitmapSizeInBytes = charCount * sizeof( buffer[ 0 ] );
    bmp.FileHeader.fileSizeInBytes = bmp.FileHeader.pixelArrayOffsetInBytes + bmp.DIBHeader.bitmapSizeInBytes;
    bmp.DIBHeader.bitmapWidthInPixels = static_cast< uint32_t >( ceil( (sqrt( (float)charCount ) ) ));
    bmp.DIBHeader.bitmapHeightInPixels = static_cast< uint32_t >( ceil( charCount / static_cast< float >( bmp.DIBHeader.bitmapWidthInPixels ) ) );

    std::ofstream writeBMP( bitmapFilename, std::ofstream::binary );

    if( !writeBMP )
        return 0;

    writeBMP.write( reinterpret_cast< char * >( &bmp ), sizeof( bmp ) );
    writeBMP.write( reinterpret_cast< char * >( &buffer[ 0 ] ), bmp.DIBHeader.bitmapSizeInBytes );
    return 0;
}

【问题讨论】:

    标签: visual-c++ bmp rgba


    【解决方案1】:

    有很多图像库可以转换图像数据。正如您所指出的,ImageMagick 就是这样一个库,它能够将 RAW (RGB[A]) 数据转换为 BMP。

    但是,与许多其他图像格式相比,BMP 格式相对简单。 RAW 数据可以通过少量代码转换为 BMP,因此如果您想减少外部依赖(例如,不使用外部代码),编写自己的 BMP 序列化代码并不一定需要集成外部库。

    请参阅 here 了解一个简单的示例,该示例将格式读取到/从 RAW 数据中写入。

    【讨论】:

    • 谢谢! “这里”中的超链接再次将我带到“en.wikipedia.org/wiki/BMP_file_format”您能帮我提供正确的链接吗?
    • 请在我的编辑上方找到我编写的代码,但我得到的 bmp 无效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2021-05-27
    • 2019-12-03
    相关资源
    最近更新 更多