我正在为我的应用程序创建小的BMP文件,其中一些(取决于像素数)使我的应用程序崩溃,Windows认为它们已损坏。工作尺寸的示例是60 x 60像素,但不是61 x 61(变量m_width和m_height)。
使用的结构(#pragma包应用于与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;
在64位VS2015上编译。
答案 0 :(得分:0)
主要问题是fetchPlaceDetail() {
this.isLoading = true;
getPlaceDetail()
.where("placeId", "==", "p002")
.onSnapshot(docs => {
docs.forEach(doc => {
const { placeName, description, todo } = doc.data();
this.placeName = placeName;
this.description = description;
this.todo = todo;
this.isLoading = false
})
})
}
应该被填充,以使字节宽度可以被4整除。您可以对m_width
使用以下公式来保证这一点。在这种情况下,它将宽度从61更改为64。多余的字节可以忽略。
此外,您可以通过避免过度使用指针来简化代码。将标头声明为局部变量就足够了。使用width_in_bytes
分配std::vector
而不是data
。并使用可以管理数据的单个RGBQ标头。示例:
new/delete