Я создаю небольшие BMP-файлы для своего приложения, и некоторые из них, в зависимости от количества пикселей, вызывают сбой моего приложения, и Windows видит их поврежденными. Пример рабочего размера - 60 x 60 пикселей, но 61 x 61 - нет (переменные m_width и m_height).
Используемые структуры (#pragma pack применяется к BMP-связанным):
struct Rgb /// vector's content
{
uint8_t r;
uint8_t g;
uint8_t b;
};
#pragma pack(push, 1)
struct FileHeader
{
int16_t bfType;
int32_t bfSize;
int16_t bfReserved1;
int16_t bfReserved2;
int32_t bfOffBits;
};
struct BitMapInfoHeader
{
int32_t biSize;
int32_t biWidth;
int32_t biHeight;
int16_t biPlanes;
int16_t biBitCount;
int32_t biCompression;
int32_t biSizeImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
int32_t biClrUsed;
int8_t biClrImportant;
int8_t biClrRotation;
int16_t biReserved;
};
struct RGBQuad
{
int8_t rgbBlue;
int8_t rgbGreen;
int8_t rgbRed;
int8_t rgbReserved;
};
#pragma pack(pop)
Я выделяю память для всего файла BMP, назначаю указатели для каждой области файла, заполняю структуры, копирую данные пикселей из другого массива и сохраняю файл. Код:
int m_width = 60, m_height = 60;
uint8_t* data = new uint8_t[ m_width * m_height ];
memset( data, 0, m_width * m_height );
data[ 2 ] = 1; /// one pixel differs
std::vector< Rgb > RGBVec = { { 223, 223, 123 }, { 230, 0, 12 } };
int numberOfSymbols = RGBVec.size();
FileHeader* fileHeader;
BitMapInfoHeader* infoHeader;
RGBQuad* colorTable;
uint8_t* m_pBMPFile; /// pointer to bitmap in memory
uint8_t* m_BMPData; /// begin of pixel data
int m_BMPFileLength = sizeof( FileHeader ) + sizeof( BitMapInfoHeader )
+ numberOfSymbols * sizeof( RGBQuad ) + m_width * m_height;
/// assign pointers to specific parts of bitmap:
m_pBMPFile = new uint8_t[ m_BMPFileLength ];
memset( m_pBMPFile, 0, m_BMPFileLength );
fileHeader = reinterpret_cast< FileHeader* >( m_pBMPFile );
infoHeader = reinterpret_cast< BitMapInfoHeader* >( m_pBMPFile + sizeof( FileHeader ) );
colorTable =
reinterpret_cast< RGBQuad* >( m_pBMPFile + sizeof( FileHeader ) + sizeof( BitMapInfoHeader ) );
m_BMPData = reinterpret_cast< uint8_t* >( m_pBMPFile + sizeof( FileHeader ) + sizeof( BitMapInfoHeader )
+ numberOfSymbols * sizeof( RGBQuad ) );
///////////
/// FileHeader:
fileHeader->bfType = 0x4d42; /// magic number
fileHeader->bfSize = m_BMPFileLength;
fileHeader->bfOffBits = int( m_BMPData - m_pBMPFile );
/// BitMapInfoHeader:
infoHeader->biSize = 40;
infoHeader->biWidth = m_width;
infoHeader->biHeight = -m_height; /// multiplied by -1 so pixels are displayed top-down
infoHeader->biPlanes = 1;
infoHeader->biBitCount = 8;
infoHeader->biCompression = 0;
infoHeader->biSizeImage = 0;
infoHeader->biXPelsPerMeter = 2835;
infoHeader->biYPelsPerMeter = 2835;
infoHeader->biClrUsed = numberOfSymbols;
infoHeader->biClrImportant = 0;
infoHeader->biClrRotation = 0;
/// palette:
int i = 0;
for( auto& s : RGBVec )
{
( &colorTable[ i ] )->rgbRed = s.r;
( &colorTable[ i ] )->rgbGreen = s.g;
( &colorTable[ i ] )->rgbBlue = s.b;
++i;
}
/// apply pixel data:
memcpy( m_BMPData, data, m_width * m_height );
/// save:
std::ofstream file2( "out.bmp", std::ios::binary | std::ios::trunc );
file2.write( ( char* )m_pBMPFile, m_BMPFileLength );
file2.close();
delete[] m_pBMPFile;
Скомпилировано на VS2015, 64 бит.