2013-02-10 44 views
0

我想將單色FreeType字形轉換爲RGBA無符號字節OpenGL紋理。紋理中的像素的顏色(X,Y)是(255,255,阿爾法),其中在單色FreeType字形緩衝區中索引像素

alpha = glyph->bitmap.buffer[pixelIndex(x, y)] * 255 

我使用

FT_Load_Char(face, glyphChar, FT_LOAD_RENDER | FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO) 

目標紋理具有的glyph->bitmap.width * glyph->bitmap.rows尺寸加載我字形。我已經能夠索引灰度字形(加載只使用FT_Load_Char(face, glyphChar, FT_LOAD_RENDER))與

glyph->bitmap.buffer[(glyph->bitmap.width * y) + x] 

這似乎不是一個單色的緩衝工作,雖然和我的最終結構的字符混亂。

在單色字形緩衝區中獲得像素(x,y)值的正確方法是什麼?

回答

2

基於this thread I started on Gamedev.net,我想出了下面的函數來獲得在(X,Y)的像素的填充/空狀態:

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

前段時間我有個類似的問題。所以我會盡力幫助你。

目標紋理具有的glyph->bitmap.width * glyph->bitmap.rows

尺寸這是對OpenGL非常具體的尺寸。如果你把它變成兩個的權力會更好。

通常的做法是讓你得到每一個字形的循環。然後循環從0到glyph->bitmap.rows的每一行。然後從0行到glyph->pitch的每一個字節(unsigned char)循環。在哪裏通過處理glyph->bitmap.buffer[pitch * row + i](i是內部循環的索引並且行是外部索引)來獲取字節。例如:

if(s[i] == ' ') left += 20; else 
    for (int row = 0; row < g->bitmap.rows; ++row) { 
      if(kerning) 
      for(int b = 0; b < pitch; b++){ 
       if(data[left + 64*(strSize*(row + 64 - g->bitmap_top)) + b] + g->bitmap.buffer[pitch * row + b] < UCHAR_MAX) 
        data[left + 64*(strSize*(row + 64 - g->bitmap_top)) + b] += g->bitmap.buffer[pitch * row + b]; 
       else 
        data[left + 64*(strSize*(row + 64 - g->bitmap_top)) + b] = UCHAR_MAX; 
      } else 
     std::memcpy(data + left + 64*(strSize*(row + 64 - g->bitmap_top)) , g->bitmap.buffer + pitch * row, pitch); 
    } 
    left += g->advance.x >> 6; 

該代碼是相關的8位位圖(非標準FT_Load_Char(face, glyphChar, FT_LOAD_RENDER))。 現在我試圖使用單色標誌,它給我帶來了麻煩。所以我的回答不是解決您的問題。如果你只是想顯示這封信,那麼你應該看到我的question

0

下面的Python函數解包一個FT_LOAD_TARGET_MONO字形位轉換成更方便的表示,其中緩衝區中的每個字節映射到一個像素。

我有單色字體一些更多的信息Python和FreeType的加在我的博客更多的例子代碼渲染:http://dbader.org/blog/monochrome-font-rendering-with-freetype-and-python

def unpack_mono_bitmap(bitmap): 
    """ 
    Unpack a freetype FT_LOAD_TARGET_MONO glyph bitmap into a bytearray where each 
    pixel is represented by a single byte. 
    """ 
    # Allocate a bytearray of sufficient size to hold the glyph bitmap. 
    data = bytearray(bitmap.rows * bitmap.width) 

    # Iterate over every byte in the glyph bitmap. Note that we're not 
    # iterating over every pixel in the resulting unpacked bitmap -- 
    # we're iterating over the packed bytes in the input bitmap. 
    for y in range(bitmap.rows): 
     for byte_index in range(bitmap.pitch): 

     # Read the byte that contains the packed pixel data. 
     byte_value = bitmap.buffer[y * bitmap.pitch + byte_index] 

     # We've processed this many bits (=pixels) so far. This determines 
     # where we'll read the next batch of pixels from. 
     num_bits_done = byte_index * 8 

     # Pre-compute where to write the pixels that we're going 
     # to unpack from the current byte in the glyph bitmap. 
     rowstart = y * bitmap.width + byte_index * 8 

     # Iterate over every bit (=pixel) that's still a part of the 
     # output bitmap. Sometimes we're only unpacking a fraction of a byte 
     # because glyphs may not always fit on a byte boundary. So we make sure 
     # to stop if we unpack past the current row of pixels. 
     for bit_index in range(min(8, bitmap.width - num_bits_done)): 

      # Unpack the next pixel from the current glyph byte. 
      bit = byte_value & (1 << (7 - bit_index)) 

      # Write the pixel to the output bytearray. We ensure that `off` 
      # pixels have a value of 0 and `on` pixels have a value of 1. 
      data[rowstart + bit_index] = 1 if bit else 0 

    return data