代码之家  ›  专栏  ›  技术社区  ›  Finley

如何使用freetype2访问单色位图中像素的状态

  •  0
  • Finley  · 技术社区  · 6 年前

    下面是一段代码片段:

    wchar_t wc=L"か";
    FT_UInt glyph_index = FT_Get_Char_Index(face, wc);
    FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
    if(face->glyph->format !=ft_glyph_format_bitmap)
    {
        FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO);
    }
    FT_GlyphSlot slot = face->glyph;
    int rows = slot->bitmap.rows;
    int cols = slot->bitmap.width; 
    for (int i = 0; i < rows; ++i) 
    {
        for (int j = 0; j < cols; ++j) 
        {
            int off  = i * slot->bitmap.pitch + j / 8;
            if (slot->bitmap.buffer[off] & (0xC0 >> (j % 8))) 
            {
                  //do something if above condition met
            }
    }
    

    off 和条件 slot->bitmap.buffer[off] & (0x80 >> (j % 8))

    实际上,类似的情况也可以找到 here . 函数定义如下:

    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;
    }
    

    它基本上等同于上面的代码片段,所以我相信存在一个像素索引标准,但即使使用 official document

    1 回复  |  直到 6 年前
        1
  •  1
  •   Rook    6 年前

    根据文件, FT_RENDER_MODE_MONO 意味着您正在使用 每像素一位

    off 现在应该更有意义了。。。 i * slot->bitmap.pitch + j / 8 字节 像素所在的位置。这就是为什么每8列只增加1。毕竟,用位指定的偏移量不能进行内存读写。

    但是,您提供的测试代码看起来可能有bug。第二个版本使用 128 10000000 . 这个 >> (x & 7) 依次获取每个连续的位,然后可以将其与像素所在的字节按位AND,以检查像素是否实际打开。代码的第一个版本改为0xC0,它是按位表示的 11000000 ,因此测试实际上根本不是做你想做的事情(它是检查 j th和 设置像素,除非 j % 8 7 )