freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ft] Questions using FT_RENDER_MODE_MONO


From: kevinkreiser
Subject: Re: [ft] Questions using FT_RENDER_MODE_MONO
Date: Tue, 12 Feb 2013 06:30:26 -0800 (PST)

This is how I do it:

vector<unsigned char> getImage(const FT_Bitmap& bitmap)
{
      //make some space, plus a little just in case we are doing 1bit image
      vector<unsigned char> image;
      image.resize(bitmap.width * bitmap.height + 8, 0);
      //copy it in
      if(image.size() > 0)
      {
         //for each row in the image
         for(int r = 0; r < bitmap.rows; r++)
         {
            //where does this row go in our texture
            int index = r * width;

            //if its 1bit
            if(bitmap.pixel_mode == FT_PIXEL_MODE_MONO)
            {
               //for each byte
               for(int c = 0, byteIndex = r * bitmap.pitch; c <
bitmap.width; c+=8, byteIndex++)
               {
                  //unroll this byte into its bits, note here we can write
some bits that we dont actually use
                  //if the width doesn't line up to exact byte boundaries,
might only happen on last pix
                  image[index++] = ((bitmap.buffer[byteIndex] & 128) == 128  
? 255 : 0);
                  image[index++] = ((bitmap.buffer[byteIndex] & 64)  == 64   
? 255 : 0);
                  image[index++] = ((bitmap.buffer[byteIndex] & 32)  == 32   
? 255 : 0);
                  image[index++] = ((bitmap.buffer[byteIndex] & 16)  == 16   
? 255 : 0);
                  image[index++] = ((bitmap.buffer[byteIndex] & 8)   == 8    
? 255 : 0);
                  image[index++] = ((bitmap.buffer[byteIndex] & 4)   == 4    
? 255 : 0);
                  image[index++] = ((bitmap.buffer[byteIndex] & 2)   == 2    
? 255 : 0);
                  image[index++] = ((bitmap.buffer[byteIndex] & 1)   == 1    
? 255 : 0);
               }
            }//grayscale 8bit
            else if(bitmap.pixel_mode == FT_PIXEL_MODE_GRAY)
            {
               //copy the whole row of pixels
               memcpy((void*)&this->image[index], (void*)&bitmap.buffer[r *
bitmap.width], bitmap.width);
            }
            else
            {
               //TODO: throw exception
               //for now silently fail...
            }
         }
         //incase we had to take more than we needed trim the image back to
its real size
         image.resize(size, 0);
         //give it back
         return image;
}


Graham Hemingway wrote:
> 
> Hello,   I am relatively new to FreeType, and first I want to say thank
> you
> to the developers.  Well, my question is about the format of the bitmap
> buffer returned by using FT_RENDER_MODE_MONO.  I want to get bitmaps that
> are not anti-aliased and my end target is OpenGL textures that use 32-bits
> per pixel (8 per channel RGBA).  So I need to do the conversion from the
> 1-bit per pixel output.  What I would like is for all four components of
> the
> texture (unsigned bytes) to be either 0 or 255 based on the glyph bitmap.
>  Below is some code that I tried but it only rendered garbage.  Any
> suggestions on how to go about this?
> 
> //Allocate memory for the texture data (four component)
> GLubyte *data = new GLubyte[4 * slot->bitmap.width * slot->bitmap.rows];
> 
> //Set color to white and alpha to bitmap value
> unsigned int bmIndex = 0, dataIndex = 0, byteIndex, bitOffset;
> unsigned char byte;
> for(int j=0; j < slot->bitmap.rows; j++) {
> for(int i=0; i < slot->bitmap.width; i++){
> 
> //Get the byte index and bit offset
>                         byteIndex = bmIndex / 8;
>                         bitOffset = bmIndex % 8;
> 
>                         //Get the byte
> byte = slot->bitmap.buffer[byteIndex];
> 
>                         //Rotate, AND, and multiply to get 0 or 255 value
>                         byte = ((byte >> bitOffset) & 1) * 255;
> 
>                         //Set all four components of the GL texture
> data[dataIndex++] = byte;
> data[dataIndex++] = byte;
> data[dataIndex++] = byte;
> data[dataIndex++] = byte;
>                         //Increment bmIndex
>                         bmIndex++;
> }
> }
> 
> Thanks,
>     Graham
> 
> _______________________________________________
> Freetype mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/freetype
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Questions-using-FT_RENDER_MODE_MONO-tp16711089p35014137.html
Sent from the Freetype - User mailing list archive at Nabble.com.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]