【发布时间】:2010-05-01 14:43:54
【问题描述】:
我正在尝试使用FreeType2 引擎创建一个工具/资产转换器,将字体光栅化为 XNA 游戏的纹理页面。
下面,第一张图片是 FreeType2]1 引擎的直接输出。第二张图片是尝试将其转换为System::Drawing::Bitmap 后的结果。
target http://www.freeimagehosting.net/uploads/fb102ee6da.jpgcurrentresult http://www.freeimagehosting.net/uploads/9ea77fa307.jpg
任何关于这里发生的事情的提示/提示/想法将不胜感激。解释字节布局和像素格式的文章链接也会有所帮助。
FT_Bitmap *bitmap = &face->glyph->bitmap;
int width = (face->bitmap->metrics.width / 64);
int height = (face->bitmap->metrics.height / 64);
// must be aligned on a 32 bit boundary or 4 bytes
int depth = 8;
int stride = ((width * depth + 31) & ~31) >> 3;
int bytes = (int)(stride * height);
// as *.bmp
array<Byte>^ values = gcnew array<Byte>(bytes);
Marshal::Copy((IntPtr)glyph->buffer, values, 0, bytes);
Bitmap^ systemBitmap = gcnew Bitmap(width, height, PixelFormat::Format24bppRgb);
// create bitmap data, lock pixels to be written.
BitmapData^ bitmapData = systemBitmap->LockBits(Rectangle(0, 0, width, height), ImageLockMode::WriteOnly, bitmap->PixelFormat);
Marshal::Copy(values, 0, bitmapData->Scan0, bytes);
systemBitmap->UnlockBits(bitmapData);
systemBitmap->Save("Test.bmp");
更新。将 PixelFormat 更改为 8bppIndexed。
FT_Bitmap *bitmap = &face->glyph->bitmap;
// stride must be aligned on a 32 bit boundary or 4 bytes
int depth = 8;
int stride = ((width * depth + 31) & ~31) >> 3;
int bytes = (int)(stride * height);
target = gcnew Bitmap(width, height, PixelFormat::Format8bppIndexed);
// create bitmap data, lock pixels to be written.
BitmapData^ bitmapData = target->LockBits(Rectangle(0, 0, width, height), ImageLockMode::WriteOnly, target->PixelFormat);
array<Byte>^ values = gcnew array<Byte>(bytes);
Marshal::Copy((IntPtr)bitmap->buffer, values, 0, bytes);
Marshal::Copy(values, 0, bitmapData->Scan0, bytes);
target->UnlockBits(bitmapData);
【问题讨论】:
-
bitmapData->scan0是位图中第一个像素数据的地址。
-
FT_Bitmap 是 FT_PIXEL_MODE_GRAY(8 位位图)。 bit.ly/9lgh69.
标签: visual-c++ c++-cli freetype freetype2