How to render 8bit indexed bitmap using GDI/GDI+?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to render a freetypes glyph to a window using windows GDI. It seems the bitmap generated by freetype is 8bit index bitmap or something, because in its official tutorial example4.cpp, it use Qt to render the glyph as follows:<br/>
<br/>

<pre class="prettyprint 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);
[/code]
<br/>
<br/>
So I try to render it using gdi as follows:<br/>
<pre class="prettyprint BITMAPINFO bmi = {0};
BITMAPINFOHEADER &bmh = bmi.bmiHeader;
bmh.biSize = sizeof(BITMAPINFOHEADER);
bmh.biBitCount = 8;
bmh.biCompression = BI_RGB;
bmh.biPlanes = 1;
//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,

&bmi, DIB_PAL_COLORS, SRCCOPY);[/code]
<br/>
<br/>
Then I try to use GDI+ to render, but failed too.<br/>
<br/>

<pre class="prettyprint 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 = Color::MakeARGB(i, 0, 0, 0);
}
bitmap.SetPalette(pal);
Graphics graphics(hdc);
graphics.DrawImage(&bitmap, 100, 100);[/code]
<br/>
<br/>
<br/>
<br/>
Could you please tell me, how to render 8bit per pixel indexed bitmap in Windows using GDI/GDI+.<br/>
Thanks.<br/>
<br/>

View the full article
 
Back
Top