gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:48:05 25 4
gpt4 key购买 nike

这是一个代码片段:

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 我也无法弄清楚它freetype2的,谁能帮忙?

最佳答案

根据文档,FT_RENDER_MODE_MONO 表示您正在使用每像素一位格式渲染单色位图。这意味着您刚刚渲染的位图的每个字节都代表一行和 8 列。为了检查位图中第 j 列的像素是否已设置,您必须检查一行中的第 j 位。

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

但是,您提供的测试代码看起来可能有问题。第二个版本使用 128,其二进制表示为 10000000>> (x & 7) 依次获取每个连续的位,然后可以将其与像素所在的字节进行按位与,以检查像素是否实际上已打开。代码的第一个版本有 0xC0,它具有按位表示 11000000,因此测试实际上根本没有执行您想要的操作(它正在检查 j 设置第 和 j+1 个像素,除非 j % 87)

关于c++ - 如何使用 freetype2 访问单色位图中的像素状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52254639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com