我正在尝试使用 windows GDI 将 freetype 的字形渲染到窗口。貌似freetype生成的位图是8bit索引位图什么的,因为在它的官方教程example4.cpp中,它使用Qt来渲染字形如下:
error = FT_Render_Glyph(m_face->glyph,
FT_RENDER_MODE_NORMAL);
QImage glyphImage(m_face->glyph->bitmap.buffer,
m_face->glyph->bitmap.width,
m_face->glyph->bitmap.rows,
m_face->glyph->bitmap.pitch,
QImage::Format_Indexed8);
/*painter.translate(m_glyphRect.x(),
m_glyphRect.y());*/
QVector<QRgb> colorTable;
for (int i = 0; i < 256; ++i)
colorTable << qRgba(0, 0, 0, i);
glyphImage.setColorTable(colorTable);
painter.drawImage(QPoint(0, 0),
glyphImage);
所以我尝试使用 gdi 渲染它,如下所示:
BITMAPINFO* pBmi = (BITMAPINFO*)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 255);
BITMAPINFOHEADER &bmh = pBmi->bmiHeader;
bmh.biSize = sizeof(BITMAPINFOHEADER);
bmh.biBitCount = 8;
bmh.biCompression = BI_RGB;
bmh.biPlanes = 1;
RGBQUAD* palette = &pBmi->bmiColors[0];
for (int i = 0; i < 256; ++i) {
RGBQUAD rgb = {0};
palette[i] = rgb;
}
//assert(pBm->bitmap);
bmh.biWidth = pBm->bitmap.width;
bmh.biHeight = - pBm->bitmap.rows;
StretchDIBits(hdc, 200, 200, pBm->bitmap.width, pBm->bitmap.rows, 0, 0, pBm->bitmap.width, pBm->bitmap.rows, pBm->bitmap.buffer,
pBmi, DIB_PAL_COLORS, SRCCOPY);
然后我尝试使用 GDI+ 进行渲染,但也失败了。
error = FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_NORMAL);
Bitmap bitmap(pBm->bitmap.width, pBm->bitmap.rows, pBm->bitmap.pitch, PixelFormat8bppIndexed, pBm->bitmap.buffer);
//Bitmap bitmap(pBm->bitmap.width, pBm->bitmap.rows, 64, PixelFormat8bppIndexed, pBm->bitmap.buffer);
ColorPalette* pal = (ColorPalette*)malloc(sizeof(ColorPalette) + 255 * sizeof(ARGB));
pal->Flags = PaletteFlagsGrayScale;
pal->Count = 256;
for (int i = 0; i <= 255; i++) {
pal->Entries[i] = Color::MakeARGB(i, 0, 0, 0);
}
bitmap.SetPalette(pal);
Graphics graphics(hdc);
graphics.DrawImage(&bitmap, 100, 100);
您能否告诉我,如何使用 GDI/GDI+ 在 Windows 中呈现每像素 8 位索引位图。谢谢。