Accessors

This commit is contained in:
glax 2024-05-12 22:51:23 +02:00
parent de527220a1
commit 6258cfbd3d
4 changed files with 45 additions and 38 deletions

View File

@ -1,27 +1,29 @@
using System.Drawing;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
namespace MSDF_Test;
public class Glyph
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
internal class Glyph
{
private Bitmap originalBitmap;
private readonly Bitmap _originalBitmap;
private const float MaxColor = 255; //255 for Bitmaps
private const float DistanceRange = 255; //255 for Bitmaps
public Glyph(Bitmap texture)
{
this.originalBitmap = texture;
this._originalBitmap = texture;
}
internal Bitmap GetBitmap(int size, Color foreground, Color background)
{
Bitmap ret = new(size, size, originalBitmap.PixelFormat);
Bitmap ret = new(size, size, _originalBitmap.PixelFormat);
for (int x = 0; x < ret.Width; x++)
{
for (int y = 0; y < ret.Height; y++)
{
PointF scaledPoint = new (x, y);
ret.SetPixel(x, y, GeneratePixel(scaledPoint.Scale((float)originalBitmap.Width / size)) ? foreground : background);
ret.SetPixel(x, y, GeneratePixel(scaledPoint.Scale((float)_originalBitmap.Width / size)) ? foreground : background);
}
}
@ -45,12 +47,12 @@ public class Glyph
double wx = point.X - x1 - .5;
double wy = point.Y - y1 - .5;
if (x1 >= 0 && y1 >= 0 && x2 <= (originalBitmap.Width - 1) && y2 <= (originalBitmap.Height - 1))
if (x1 >= 0 && y1 >= 0 && x2 <= (_originalBitmap.Width - 1) && y2 <= (_originalBitmap.Height - 1))
{
Color x1y1 = originalBitmap.GetPixel(x1, y1).MultiplyWith((1 - wx) * (1 - wy));
Color x1y2 = originalBitmap.GetPixel(x1, y2).MultiplyWith((1 - wx) * wy);
Color x2y1 = originalBitmap.GetPixel(x2, y1).MultiplyWith(wx * (1 - wy));
Color x2y2 = originalBitmap.GetPixel(x2, y2).MultiplyWith(wx * wy);
Color x1y1 = _originalBitmap.GetPixel(x1, y1).MultiplyWith((1 - wx) * (1 - wy));
Color x1y2 = _originalBitmap.GetPixel(x1, y2).MultiplyWith((1 - wx) * wy);
Color x2y1 = _originalBitmap.GetPixel(x2, y1).MultiplyWith(wx * (1 - wy));
Color x2y2 = _originalBitmap.GetPixel(x2, y2).MultiplyWith(wx * wy);
return x1y1.Add(x1y2).Add(x2y1).Add(x2y2);
}
@ -58,14 +60,14 @@ public class Glyph
return Color.Black; //Edges can not be interpolated. Just return 0
}
internal static float Median(float r, float g, float b)
private static float Median(float r, float g, float b)
{
return Math.Max(Math.Min(r, g), Math.Min(Math.Max(r, g), b));
}
internal static float Median(Color c) => Median(c.R, c.G, c.B);
private static float Median(Color c) => Median(c.R, c.G, c.B);
internal static float ColorDistance(float sample)
private static float ColorDistance(float sample)
{
return ((sample / MaxColor) - .5f) * DistanceRange;
}

View File

@ -1,16 +1,18 @@
using System.Drawing;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
namespace MSDF_Test;
// ReSharper disable once InconsistentNaming
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
public class MSDF
{
private readonly Texture _texture;
private readonly Dictionary<char, Glyph> _glyphs = new();
public MSDF(string texturePath, int glyphSize, int padding)
{
this._texture = new Texture(texturePath, glyphSize, padding);
List<Bitmap> glyphBitmaps = this._texture.GetGlyphBitmaps();
Texture texture = new (texturePath, glyphSize, padding);
List<Bitmap> glyphBitmaps = texture.GetGlyphBitmaps();
for(int i = 32; i < 128; i++)
_glyphs.Add((char)(i+1), new Glyph(glyphBitmaps[i]));
}
@ -18,17 +20,16 @@ public class MSDF
public Bitmap Render(string str, int size)
{
Bitmap ret = new(size * str.Length, size);
using (Graphics grD = Graphics.FromImage(ret))
using Graphics grD = Graphics.FromImage(ret);
for(int i = 0; i < str.Length; i++)
{
for(int i = 0; i < str.Length; i++)
if (_glyphs.TryGetValue(str[i], out Glyph? glyph))
{
if (_glyphs.TryGetValue(str[i], out Glyph? glyph))
{
int destOffsetX = i * size;
grD.DrawImage(glyph.GetBitmap(size, Color.White, Color.Transparent), new PointF(destOffsetX, 0));
}
int destOffsetX = i * size;
grD.DrawImage(glyph.GetBitmap(size, Color.White, Color.Transparent), new PointF(destOffsetX, 0));
}
}
return ret;
}
}

View File

@ -20,5 +20,7 @@ do
Console.WriteLine("Rendering...");
Bitmap render = msdf.Render(str, size);
#pragma warning disable CA1416
render.Save("render.png", ImageFormat.Png);
#pragma warning restore CA1416
Console.WriteLine("Done.");

View File

@ -1,36 +1,38 @@
using System.Drawing;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
namespace MSDF_Test;
public struct Texture
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
internal readonly struct Texture
{
internal Bitmap Image;
internal int GlyphSize;
internal int Padding;
private readonly Bitmap _image;
private readonly int _glyphSize;
private readonly int _padding;
public Texture(Bitmap image, int glyphSize, int padding)
internal Texture(Bitmap image, int glyphSize, int padding)
{
this.Image = image;
this.GlyphSize = glyphSize;
this.Padding = padding;
this._image = image;
this._glyphSize = glyphSize;
this._padding = padding;
}
internal List<Bitmap> GetGlyphBitmaps()
{
List<Bitmap> ret = new();
for (int y = Padding; y < Image.Width - Padding; y += GlyphSize + Padding)
for (int y = _padding; y < _image.Width - _padding; y += _glyphSize + _padding)
{
for (int x = Padding; x < Image.Height - Padding; x += GlyphSize + Padding)
for (int x = _padding; x < _image.Height - _padding; x += _glyphSize + _padding)
{
Point topLeft = new (x, y);
ret.Add(Image.Clone(new Rectangle(topLeft, new Size(GlyphSize, GlyphSize)), Image.PixelFormat));
ret.Add(_image.Clone(new Rectangle(topLeft, new Size(_glyphSize, _glyphSize)), _image.PixelFormat));
}
}
return ret;
}
public Texture(string imagePath, int glyphSize, int padding) : this((Bitmap)Bitmap.FromFile(imagePath), glyphSize, padding)
public Texture(string imagePath, int glyphSize, int padding) : this((Bitmap)Image.FromFile(imagePath), glyphSize, padding)
{
}