Jump to content

  • Log In with Google      Sign In   
  • Create Account


Indexing pixels in monochrome FreeType glyph


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 AaronWizardstar   Members   -  Reputation: 244

Like
0Likes
Like

Posted 14 February 2013 - 12:51 AM

I need to render some monochrome (not greyscale, not antialiased) FreeType fonts, with the ultimate goal of implementing distance field fonts. I don't know how to properly access a monochrome glyph's pixels though.

 

For a greyscale glyph I can get the pixel at (x, y) with glyph.bitmap.buffer[(glyph->bitmap.width * y) + x].  I can't figure out what the equivalent offset is for a monochrome glyph though beyond it having something to do with the glyph's bitmap.pitch value.



Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 2157

Like
1Likes
Like

Posted 14 February 2013 - 02:05 AM

This seems to cover it http://old.nabble.com/monochrome-bitmaps-td21054168.html pretty well.

 

I think your function would look something like this - However, this is untested. I'm a little unsure about the "(cValue & (1 << (x & 7))) != 0" bit, if it doesn't work try replacing with "(cValue & (128 >> (x & 7))) != 0" - because it's not clear to me from a glance at the docs what the bit ordering is.

 

EDIT: Just changed "char cValue = pRow[x];" to "char cValue = pRow[x >> 3];"

 

bool GetPixelValue(const char *pBuffer, int iPitch, int x, int y)
{
    const char *pRow = &pBuffer[y*iPitch];
    char cValue = pRow[x >> 3];
    bool bRet = (cValue & (1 << (x & 7))) != 0;
    return bRet;
}

Edited by C0lumbo, 14 February 2013 - 02:30 AM.


#3 Lightness1024   Members   -  Reputation: 702

Like
0Likes
Like

Posted 14 February 2013 - 09:49 AM

if you are talking about the libfreetype you just need to use FT_RENDER_MODE_MONO.

http://www.freetype.org/freetype2/docs/tutorial/step2.html



#4 AaronWizardstar   Members   -  Reputation: 244

Like
1Likes
Like

Posted 14 February 2013 - 04:53 PM

 

if it doesn't work try replacing with "(cValue & (128 >> (x & 7))) != 0"

That turned out to be the code to use. My final code looks like this:

 

GLubyte *imgBuffer = new GLubyte[glyph->bitmap.width * glyph->bitmap.rows * 4];

int pitch = abs(glyph->bitmap.pitch);

for (size_t y = 0; y < glyph->bitmap.rows; ++y)
{
    for (size_t x = 0; x < glyph->bitmap.width; ++x)
    {
        int imageIndex = (glyph->bitmap.width * y) + x;
        for (int i = 0; i < 3; ++i) // R, G, and B
        {
            imgBuffer[(imageIndex * 4) + i] = 255;
        }

        unsigned char *byteRow = &glyph->bitmap.buffer[pitch * y];
        char cValue = byteRow[x >> 3];
        bool filled = (cValue & (128 >> (x & 7))) != 0;

        // A
        if (filled)
        {
            imgBuffer[(imageIndex * 4) + 3] = 255;
        }
        else
        {
            imgBuffer[(imageIndex * 4) + 3] = 0;
        }
    }
}

Where imgBuffer is my final RGBA texture data (the fonts are sharing texture space with the sprites).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS