To make the preceding code usable, we add two more routines that save and load images from disk files. The first one, FreeImage_LoadBitmapFromFile()
, loads the bitmap:
bool FreeImage_LoadBitmapFromFile( const std::string& FileName, std::vector<ubyte>& OutData, int& W, int& H, int& BitsPP ) { std::ifstream InFile( FileName.c_str(), std::ios::in | std::ifstream::binary ); std::vector<char> Data( ( std::istreambuf_iterator<char>( InFile ) ), std::istreambuf_iterator<char>() ); return FreeImage_LoadFromStream( ( ubyte* )&Data[0], ( int )data.size(), OutData, W, H, BitsPP, true ); }
We use a simple function to extract the file extension, which serves as a file type tag:
std::string ExtractExtension( const std::string& FileName ) { size_t pos = FileName.find_last_of( '.' ); return ( pos == std::string::npos ) ? FileName : FileName.substr( pos ); }
The FreeImage_SaveBitmapToFile()
function saves...