Search code examples
freetype

Indexing pixels in a monochrome FreeType glyph buffer


I want to translate a monochrome FreeType glyph to an RGBA unsigned byte OpenGL texture. The colour of the texture at pixel (x, y) would be (255, 255, alpha), where

alpha = glyph->bitmap.buffer[pixelIndex(x, y)] * 255

I load my glyph using

FT_Load_Char(face, glyphChar, FT_LOAD_RENDER | FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO)

The target texture has dimensions of glyph->bitmap.width * glyph->bitmap.rows. I've been able to index a greyscale glyph (loaded using just FT_Load_Char(face, glyphChar, FT_LOAD_RENDER)) with

glyph->bitmap.buffer[(glyph->bitmap.width * y) + x]

This does not appear work on a monochrome buffer though and the characters in my final texture are scrambled.

What is the correct way to get the value of pixel (x, y) in a monochrome glyph buffer?


Solution

  • Based on this thread I started on Gamedev.net, I've come up with the following function to get the filled/empty state of the pixel at (x, y):

    bool glyphBit(const FT_GlyphSlot &glyph, const int x, const int y)
    {
        int pitch = abs(glyph->bitmap.pitch);
        unsigned char *row = &glyph->bitmap.buffer[pitch * y];
        char cValue = row[x >> 3];
    
        return (cValue & (128 >> (x & 7))) != 0;
    }