2017-01-07 4 views
0

イメージフォーマットを解釈するのが初めてです。 私はimagemagickについて読んだことがありますが、使用できる他のライブラリがありますか?
または、rgbaファイルを読み込んでデータを並べ替えるのと同じですか?任意のコードスニペット? ここに助けがあれば助けになるでしょう。私はC++でBMPに変換したいRGBAイメージファイルを持っています

EDIT:RGBAイメージをBMPに変換するコードを書きましたが、出力イメージは表示されても無効です。ここで助けを受けることはできますか? @bRadギブソン 呼ばポスト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; 
} 

答えて

0

画像データを変換するために多くの画像ライブラリがあります。あなたが指摘したように、ImageMagickは、RAW(RGB [A])データをBMPに変換できるライブラリの1つです。

しかし、BMPフォーマットは他の多くのイメージフォーマットと比較して比較的簡単です。 RAWデータは少量のコードでBMPに変換できるので、外部依存関係を減らしたい場合(例えば外部コードを使用しない場合)、独自のBMP直列化コードを記述しても、必ずしも外部ライブラリを統合する必要はありません。

RAWデータの読み書きを行う簡単な例については、hereを参照してください。

+0

ありがとうございました!上記のハイパーリンクの上に「https://en.wikipedia.org/wiki/BMP_file_format」が表示されます。正しいリンクを教えてください。 – Midas

+0

私が書いたコードで上記の私の編集を見つけるが、私が得るbmpは無効である。 – Midas

関連する問題