[英]Writing correct bitmap file results
我一直在為Directx9創建字體渲染器以繪制文本,實際問題是由另一個問題引起的,我想知道為什么紋理沒有繪制任何東西(我的位圖錯誤),所以我嘗試將位圖復制到文件中並意識到當前的問題。 好極了
我到底在做什么錯? 我的意思是,我只是簡單地將位圖包裝器中的CURRENT像素數組復制到具有其他內容(位圖信息)的文件中,我已經在十六進制編輯器中看到,位圖標題后面有顏色。
CFont :: DrawGlyphToBitmap
此代碼從的位圖復制freetype的字形(其具有由方式FT_PIXEL_MODE_BGRA的像素格式)的字體位圖包裝類實例
void CFont::DrawGlyphToBitmap ( unsigned char * buffer, int rows, int pitch, int destx, int desty, int format )
{
CColor color = CColor ( 0 );
for ( int row = 0; row < rows; row++ )
{
int x = 0;
for ( int left = 0; left < pitch * 3; left += 3,x++ )
{
int y = row;
unsigned char* cursor = &buffer [ ( row*pitch ) + left ];
color.SetAlphab ( 255 );
color.SetBlueb ( cursor [ 0 ] );
color.SetGreenb ( cursor [ 1 ] );
color.SetRedb ( cursor [ 2 ] );
m_pBitmap->SetPixelColor ( color, destx + x, desty + y );
}
}
}
CBitmap :: SetPixelColor
此代碼確實在其本地像素存儲中設置了一個“像素” /顏色。
void CBitmap::SetPixelColor ( const CColor & color, int left, int top )
{
unsigned char* cursor = &m_pBuffer [ ( m_iPitch * top ) + ( left * bytes_per_px ) ];
cursor [ px_red ] = color.GetRedb ( );
cursor [ px_green ] = color.GetGreenb ( );
cursor [ px_blue ] = color.GetBlueb ( );
if ( px_alpha != 0xFFFFFFFF )
cursor [ px_alpha ] = color.GetAlphab ( );
}
CBitmap ::保存
這是將位圖寫入文件系統的函數的局部顯示,它確實顯示了如何初始化位圖信息容器(文件頭和“ dib”頭)
void CBitmap::Save ( const std::wstring & path )
{
BITMAPFILEHEADER bitmap_header;
BITMAPV5HEADER bitmap_info;
memset ( &bitmap_header, 0, sizeof ( BITMAPFILEHEADER ) );
memset ( &bitmap_info, 0, /**/sizeof ( BITMAPV5HEADER ) );
bitmap_header.bfType = 'B' + ( 'M' << 8 );//0x424D;
bitmap_header.bfSize = bitmap_header.bfOffBits + ( m_iRows * m_iPitch ) * 3;
bitmap_header.bfOffBits = sizeof ( BITMAPFILEHEADER ) + sizeof ( BITMAPV5HEADER );
double _1px_p_m = 0.0002645833333333f;
bitmap_info.bV5Size = sizeof ( BITMAPV5HEADER );
bitmap_info.bV5Width = m_iPitch;
bitmap_info.bV5Height = m_iRows;
bitmap_info.bV5Planes = 1;
bitmap_info.bV5BitCount = bytes_per_px * 8;
bitmap_info.bV5Compression = BI_BITFIELDS;
bitmap_info.bV5SizeImage = ( m_iPitch * m_iRows ) * 3;
bitmap_info.bV5XPelsPerMeter = m_iPitch * _1px_p_m;
bitmap_info.bV5YPelsPerMeter = m_iRows * _1px_p_m;
bitmap_info.bV5ClrUsed = 0;
bitmap_info.bV5ClrImportant = 0;
bitmap_info.bV5RedMask = 0xFF000000;
bitmap_info.bV5GreenMask = 0x00FF0000;
bitmap_info.bV5BlueMask = 0x0000FF00;
bitmap_info.bV5AlphaMask = 0x000000FF;
bitmap_info.bV5CSType = LCS_WINDOWS_COLOR_SPACE;
...
-> the other part does just write those structures & my px array to file
}
CBitmap“有用的”宏
我為像素數組制作了宏,因為我多次“更改”了像素“格式”->使它“更容易”,我制作了這些宏,使其更容易做到這一點
#define bytes_per_px 4
#define px_red 0
#define px_green 1
#define px_blue 2
#define px_alpha 3
此計算是錯誤的:
&m_pBuffer [ ( m_iPitch * top ) + ( left * bytes_per_px ) ]
它應該是:
&m_pBuffer [ ( ( m_iPitch * top ) + left ) * bytes_per_px ]
每行的m_iPitch * bytes_per_px
為m_iPitch * bytes_per_px
個字節。 如果僅乘以m_iPitch
,那么您的“行”就會相互重疊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.