3

I've been playing around with FreeType to render text in OpenGL/DirectX. For debugging purposes, I'm wanting to render glyphs to a file (bmp, png, ect), so that I can pack them together as a font sprite sheet. To do this I'm using FreeImage. I'm creating a FreeImage bitmap via the FreeImage_ConvertFromRawBits function. Since FreeType generates 8 bit glyphs, I'm passing in 0 for the mask parameters for FreeImage_ConvertFromRawBits. See code below.

/* Initialize FreeType */
/* Create Face object */
/* Set character size */

int charCode = (int)'a';
int charIndex = FT_Get_Char_Index(ftFace, CHAR_CODE);
FT_Load_Char(face, charIndex, FT_LOAD_DEFAULT);
FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)

/* Check for errors */

int bpp = 8;
int width = face->glyph->bitmap.width;
int height = face->glyph->bitmap.rows;
int pitch = face->glyph->bitmap.pitch;
unsigned char* buffer = face->glyph->bitmap.buffer;
FIBITMAP* dib = FreeImage_ConvertFromRawBits(buffer, width, height, pitch, bpp, 0, 0, 0);

/* Check for errors */

FreeImage_Save(FIF_BMP, dib, "Glyph.bmp");

The issue is that the image comes out looking weird. You can kind of tell it looks like a glyph, but it's all messed up. If I try and save the image as a png, the image is still all messed up, but with random colors everywhere. I can't seem to find an example of saving a FreeType bitmap to an image file anywhere. Thanks!

Ramon Guerrero
  • 205
  • 1
  • 12
  • Just a note: FreeType outputs 8bpp greyscale. If you're improperly inserting that into 24bpp RGB or something that could cause the problem you describe. – Ed Halferty Oct 12 '15 at 16:43
  • I'm specifying 8bpp as the fifth parameter to FreeImage_ConvertFromRawBits to indicate 8bpp greyscale. – Ramon Guerrero Oct 13 '15 at 20:23

0 Answers0