将FreeType GlyphSlot位图转换为BGRA

问题描述 投票:0回答:1

我正在尝试将FreeType GlyphSlot位图转换为BGRA格式。

void DrawText(const std::string &text) {
    FT_GlyphSlot  Slot = face->glyph;
    buffer.resize(0);

    for (auto c : text) {
        FT_Error error = FT_Load_Char(face, c, FT_LOAD_RENDER);
        if (error)
            continue;

        auto width = Slot->bitmap.width;
        auto height = Slot->bitmap.rows;
        auto bufferSize = width * height * 4;
        buffer.resize(buffer.size()+bufferSize);

        uint8_t* src = Slot->bitmap.buffer;

        uint8_t* startOfLine = src;
        int dst = 0;

        for (int y = 0; y < height; ++y) {
            src = startOfLine;
            for (int x = 0; x < width; ++x) {
                auto value = *src;
                src++;

                buffer[dst++] = 0xff;
                buffer[dst++] = 0xff;
                buffer[dst++] = 0xff;
                buffer[dst++] = value;
            }
            startOfLine += Slot->bitmap.pitch;
        }
    }
}

这给我输出乱码。我不确定要正确转换为B8G8R8A8所需执行的操作。

c++ data-conversion freetype2 image-formats
1个回答
0
投票

一个问题是您用每个字符调整buffer的大小(这会将先前的数据保留在新分配的空间的开头),但是当存储新字符c的数据时,您覆盖了缓冲区的开始,因为dst为0。您可能想在dst调用之前将buffer.size()设置为resize

int dst = /*previous buffer size*/;
© www.soinside.com 2019 - 2024. All rights reserved.