I am new to interpreting image formats.
I have read about imagemagick but are there any other libraries available which I can use ?
Or is it much like reading rgba file and rearranging the data. any code snippet?
Any help here would be of great help.
EDIT: I have written below code to convert RGBA image to BMP but output image is invalid when viewed. Can i get help here? Referred post by @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;
}