Sign in to follow this  

Save FreeType bitmap to file

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this