Freetype renders crap in bitmap

1.7k views Asked by At

I'm trying to create monochrome glyph atlas but encountered a problem. Freetype renders 'crap' in glyph's bitmap. I blame freetype because some of the glyphs are still rendered correctly.

The resulting texture atlas:

Texture atlas

Why could it be and how can i fix it?

However i still could be wrong and here is bitmap processing code:

static std::vector<unsigned char> generateBitmap(FT_Face &face, unsigned int glyph, size_t *width, size_t *height) {
        FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_RENDER | FT_LOAD_MONOCHROME );

        FT_Bitmap bitmap;
        FT_Bitmap_New(&bitmap);
        FT_Bitmap_Convert(ftLib, &face->glyph->bitmap, &bitmap, 1);
        *width = bitmap.width;
        *height = bitmap.rows;


        std::vector<unsigned char> result(bitmap.width * bitmap.rows);//
        for (size_t y = 0; y < bitmap.rows; ++y)
        {
            for (size_t x = 0; x < bitmap.width; ++x)
            {
                result[(bitmap.width * y) + x] = bitmap.buffer[(bitmap.width * y) + x];
            }

        }
        FT_Bitmap_Done(ftLib, &bitmap);
        return result;
    }

And code for putting it on main buffer:

        static void putOnBuffer(std::vector<unsigned char> &buffer, std::vector<unsigned char> &bitmap, size_t height, size_t width) {
        int r = 0;
        while (r < height) {
            int w = 0;
            while (w < width) {
                //assume buffer is enough large
                size_t mainBufPos = ((currentBufferPositionY + r) * imageWidth) + (currentBufferPositionX + w);
                size_t bitmapBufPos = (r * width) + w;
                buffer[mainBufPos] = clamp(int(bitmap[bitmapBufPos] * 0x100), 0xff);
                w++;
            }
            r++;
        }
    }
1

There are 1 answers

0
Cee McSharpface On BEST ANSWER

From documentation:

Convert a bitmap object with depth 1bpp, 2bpp, 4bpp, 8bpp or 32bpp to a bitmap object with depth 8bpp, making the number of used bytes [per] line (a.k.a. the ‘pitch’) a multiple of ‘alignment’.

In your code, you pass 1 as the value of the alignment parameter in the call to FT_Bitmap_Convert. In monochrome, one byte will be eight pixels, so the horizontal render loop needs to enforce a multiple of eight for the width.

Reference: https://www.freetype.org/freetype2/docs/reference/ft2-bitmap_handling.html