Advertisement Jump to content
Sign in to follow this  

Save FreeType bitmap to file

This topic is 1171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 error = FT_Load_Char(face, charCode, FT_LOAD_RENDER);

/* Check for errors */

/* Initialize FreeImage */

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!

Share this post

Link to post
Share on other sites
I'm not very proficient in FreeType but from glancing at the tutorial I would say you need to call FT_Render_Glyph:

The field face?>glyph?>format describes the format used for storing the glyph image in the slot. If it is not FT_GLYPH_FORMAT_BITMAP, one can immediately convert it to a bitmap through FT_Render_Glyph.

Share this post

Link to post
Share on other sites

I wrote an application ( that exports FreeType data out to a RAW file format, and also an internal file format used by my game engine.


To save a raw file you can just write out each pixel directly from the glyph->bitmap.buffer.  Give the file name a .raw extension and then open it in Photoshop.  Photoshop will ask you for the dimensions of the file so that it can organize the pixels correctly.  Give it a try!

Edited by

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!