私は何かが欠けていることを知っていますが、何がわかりません。このコードは、画像を BMP ファイルに保存するのに役立ちます。しかし、それを使用すると、画像の上部に黒いピクセルの行が表示され、画像が右にシフトします。何か案は ?ありがとう !
struct CVImporter::BITMAPFILEHEADER
{
ushort bfType;
uint bfSize;
uint bfReserved;
uint bfOffBits;
};
struct CVImporter::BITMAPINFOHEADER
{
uint biSize;
int biWidth;
int biHeight;
short biPlanes;
short biBitCount;
uint biCompression;
uint biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
uint biClrUsed;
uint biClrImportant;
};
struct CVImporter::RGBQUAD
{
uchar rgbBlue;
uchar rgbGreen;
uchar rgbRed;
uchar rgbReserved;
};
struct CVImporter::BITMAPINFO
{
TBITMAPINFOHEADER bmiHeader;
TRGBQUAD bmiColors[256];
};
//-----------------------------------------------------------------------------
//
void
CVImporter::WriteBitmapFileHeader( std::ofstream& stream,
const BITMAPFILEHEADER& header )
{
WriteToStream( stream, header.bfType );
WriteToStream( stream, header.bfSize );
WriteToStream( stream, header.bfReserved );
WriteToStream( stream, header.bfOffBits );
}
//-----------------------------------------------------------------------------
//
void
CVImporter::WriteBitmapInfoHeader( std::ofstream& stream,
const BITMAPINFOHEADER& infoHeader )
{
WriteToStream( stream, infoHeader.biSize );
WriteToStream( stream, infoHeader.biWidth );
WriteToStream( stream, infoHeader.biHeight );
WriteToStream( stream, infoHeader.biPlanes );
WriteToStream( stream, infoHeader.biBitCount );
WriteToStream( stream, infoHeader.biCompression );
WriteToStream( stream, infoHeader.biSizeImage );
WriteToStream( stream, infoHeader.biXPelsPerMeter );
WriteToStream( stream, infoHeader.biYPelsPerMeter );
WriteToStream( stream, infoHeader.biClrUsed );
WriteToStream( stream, infoHeader.biClrImportant );
}
//-----------------------------------------------------------------------------
//
void
CVImporter::WriteBitmapRGBQuad( std::ofstream& stream, const RGBQUAD& quad )
{
WriteToStream( stream, quad.rgbBlue );
WriteToStream( stream, quad.rgbGreen );
WriteToStream( stream, quad.rgbRed );
WriteToStream( stream, quad.rgbReserved );
}
//-----------------------------------------------------------------------------
//
void
CVImporter::WriteBitmapInfo( std::ofstream& stream,
const BITMAPINFO& info )
{
WriteBitmapInfoHeader( stream, info.bmiHeader );
for( uint i = 0; i < 256; ++i )
WriteBitmapRGBQuad( stream, info.bmiColors[i] );
}
//-----------------------------------------------------------------------------
//
void
CVImporter::LoadBitmapFile( const CLString& fileName,
CVBitmap::Ptr bm ) throw(IOException)
{
if( bm.IsNull() ) throw( IOException("Pointer should not be null", CL_ORIGIN) );
// Verify the extension of the file
CLString ext("");
CLFileSystem::GetExtension( fileName, ext );
if( ext != "bmp" )
throw( IOException("Bad file extension (should be bmp)", CL_ORIGIN) );
uint bytesPerPixel {3};
uint imgDataSize = (bm->GetWidth()*bytesPerPixel)*bm->GetHeight();
BITMAPFILEHEADER bmFile;
BITMAPINFO bmInfo;
bmFile.bfType = (ushort)0x4D42;
bmFile.bfSize = imgDataSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO);
bmFile.bfReserved = 0;
bmFile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO);
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = (ulong)bm->GetWidth();
bmInfo.bmiHeader.biHeight = (ulong)bm->GetHeight();
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = (ushort) 8*bytesPerPixel;
bmInfo.bmiHeader.biCompression = 0; //BI_RGB;
bmInfo.bmiHeader.biSizeImage = imgDataSize;
bmInfo.bmiHeader.biXPelsPerMeter = 0;
bmInfo.bmiHeader.biYPelsPerMeter = 0;
bmInfo.bmiHeader.biClrUsed = 256;
bmInfo.bmiHeader.biClrImportant = 256;
for( uint i = 0; i < 256; ++i )
{
bmInfo.bmiColors[i].rgbBlue = i;
bmInfo.bmiColors[i].rgbGreen = i;
bmInfo.bmiColors[i].rgbRed = i;
bmInfo.bmiColors[i].rgbReserved = 0;
}
std::ofstream stream( fileName.c_str(), std::ios::binary );
WriteBitmapFileHeader( stream, bmFile );
WriteBitmapInfo( stream, bmInfo );
uint padding = (4 - ((3 * bm->GetWidth()) % 4)) % 4;
char padding_data[4] {0, 0, 0, 0};
for( uint i = 0; i < bm->GetHeight(); ++i )
{
uchar* data_ptr = bm->GetBits() + ((bm->GetHeight()-i)*bm->GetWidthStep());
stream.write( reinterpret_cast<char*>(data_ptr), sizeof(char) *
bm->GetByteSize() * bm->GetWidth() );
stream.write( padding_data, padding );
}
stream.close();
}
//-----------------------------------------------------------------------------
//
template<typename T>
inline void
CVImporter::WriteToStream( std::ofstream& stream, const T& t )
{
stream.write( reinterpret_cast<const char*>(&t), sizeof(T) );
}