1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
void CBMPLoader::Export( string sFileName, const ILoader::IRessourceInfos& ri )
{
const ILoader::CTextureInfos* pInfos = static_cast< const ILoader::CTextureInfos* >( &ri );
int nWidth = pInfos->m_nWidth;
int nHeight = pInfos->m_nHeight;
if( nWidth < 0 || nHeight < 0 )
{
CEException e( "CBMPLoader::Export() : la taille de la texture doit être positive" );
throw e;
}
const vector< unsigned char >& vData = pInfos->m_vTexels;
int nBitPerPixel = 0;
switch( pInfos->m_ePixelFormat )
{
case TPixelFormat::eRGB:
case TPixelFormat::eBGR:
nBitPerPixel = 24;
break;
case TPixelFormat::eRGBA:
case TPixelFormat::eBGRA:
nBitPerPixel = 32;
}
// file header
FILE* pFile = fopen( sFileName.c_str(), "w" );
char* sMagic = "BM";
fwrite( sMagic, 1, 2, pFile );
int nSize = 0;
fwrite( &nSize, 4, 1, pFile );
int id = 0;
fwrite( &id, 1, 2, pFile );
fwrite( &id, 1, 2, pFile );
int DataAdress = 0;
int nDataAdressOffset = ftell(pFile);
fwrite( &DataAdress, 4, 1, pFile );
// image header
int nHeaderSize = 40;
fwrite( &nHeaderSize, 4, 1, pFile );
fwrite( &nWidth, 4, 1, pFile );
fwrite( &nHeight, 4, 1, pFile );
int nPlanes = 1;
fwrite( &nPlanes, 2, 1, pFile );
fwrite( &nBitPerPixel, 2, 1, pFile );
int iData = 0;
fwrite( &iData, 4, 6, pFile );
int nDataAdress = ftell(pFile);
// Pixel data
fwrite( &vData[ 0 ], sizeof( unsigned char ), vData.size(), pFile );
nSize = ftell(pFile);
// complete infos
fseek( pFile, 2, SEEK_SET );
fwrite( &nSize, 4, 1, pFile );
fseek( pFile, nDataAdressOffset, SEEK_SET );
fwrite( &nDataAdress, 4, 1, pFile );
fclose( pFile );
} |
Partager