system :: drawing :: bitmapへのunsigned char*バッファー:: bitmap
-
02-10-2019 - |
質問
XNAゲームのフォントをテクスチャページにラスターするツール/アセットコンバーターを作成しようとしています。 Freetype2 エンジン。
以下では、最初の画像はFreetype2からの直接出力です21 エンジン。 2番目の画像は、それを変換しようとした後の結果です System::Drawing::Bitmap
.
ターゲットhttp://www.freeimagehosting.net/uploads/fb102ee6da.jpg CurrentResult 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);
解決
ああハハ。それを解決しました。
FT_Bitmap
8ビット画像なので、正しいです PixelFormat
だった 8bppIndexed
, 、この出力をもたらしました。32byte境界に整列していないhttp://www.freeimagehosting.net/uploads/dd90fa2252.jpg
System::Drawing::Bitmap
32ビットの境界で整列する必要があります。
私はストライドを計算していましたが、ビットマップを書くときにそれをパディングしていませんでした。コピーしました FT_Bitmap
aへのバッファー byte[]
そして、それをaに書きました MemoryStream
, 、必要なパディングを追加します。
int stride = ((width * pixelDepth + 31) & ~31) >> 3;
int padding = stride - (((width * pixelDepth) + 7) / 8);
array<Byte>^ pad = gcnew array<Byte>(padding);
array<Byte>^ buffer = gcnew array<Byte>(size);
Marshal::Copy((IntPtr)source->buffer, buffer, 0, size);
MemoryStream^ ms = gcnew MemoryStream();
for (int i = 0; i < height; ++i)
{
ms->Write(buffer, i * width, width);
ms->Write(pad, 0, padding);
}
GCがそのままにするようにメモリをピン留めしました。
// pin memory and create bitmap
GCHandle handle = GCHandle::Alloc(ms->ToArray(), GCHandleType::Pinned);
target = gcnew Bitmap(width, height, stride, PixelFormat::Format8bppIndexed, handle.AddrOfPinnedObject());
ms->Close();
ないので Format8bppIndexed
灰色画像はまだ正しくありませんでした。
Alt Text http://www.freeimagehosting.net/uploads/8a883b7dce.png
次に、ビットマップパレットをグレースケール256に変更しました。
// 256-level greyscale palette
ColorPalette^ palette = target->Palette;
for (int i = 0; i < palette->Entries->Length; ++i)
palette->Entries[i] = Color::FromArgb(i,i,i);
target->Palette = palette;
Alt Text http://www.freeimagehosting.net/uploads/59a745269e.jpg
最終的解決。
error = FT_Load_Char(face, ch, FT_LOAD_RENDER);
if (error)
throw gcnew InvalidOperationException("Failed to load and render character");
FT_Bitmap *source = &face->glyph->bitmap;
int width = (face->glyph->metrics.width / 64);
int height = (face->glyph->metrics.height / 64);
int pixelDepth = 8;
int size = width * height;
// stride must be aligned on a 32 bit boundary or 4 bytes
// padding is the number of bytes to add to make each row a 32bit aligned row
int stride = ((width * pixelDepth + 31) & ~31) >> 3;
int padding = stride - (((width * pixelDepth) + 7) / 8);
array<Byte>^ pad = gcnew array<Byte>(padding);
array<Byte>^ buffer = gcnew array<Byte>(size);
Marshal::Copy((IntPtr)source->buffer, buffer, 0, size);
MemoryStream^ ms = gcnew MemoryStream();
for (int i = 0; i < height; ++i)
{
ms->Write(buffer, i * width, width);
ms->Write(pad, 0, padding);
}
// pin memory and create bitmap
GCHandle handle = GCHandle::Alloc(ms->ToArray(), GCHandleType::Pinned);
target = gcnew Bitmap(width, height, stride, PixelFormat::Format8bppIndexed, handle.AddrOfPinnedObject());
ms->Close();
// 256-level greyscale palette
ColorPalette^ palette = target->Palette;
for (int i = 0; i < palette->Entries->Length; ++i)
palette->Entries[i] = Color::FromArgb(i,i,i);
target->Palette = palette;
FT_Done_FreeType(library);
他のヒント
「深さ」の値は、ビットマップのPixelformatと一致しません。 Format24bpprgbに一致するには24である必要があります。ビットマップのPFは、FT_BITMAPのPFとストライドにも一致する必要があります。