-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathFTBitmapExtensions.cs
More file actions
175 lines (145 loc) · 6.33 KB
/
FTBitmapExtensions.cs
File metadata and controls
175 lines (145 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace SharpFont.Gdi
{
public static class FTBitmapExtensions
{
//HACK these variables exist to reduce the cost of reflection at runtime.
//Meant to be a temporary fix to https://github.com/Robmaister/SharpFont/issues/62
//until libgdiplus gets patched.
private static bool hasCheckedForMono;
private static bool isRunningOnMono;
private static System.Reflection.FieldInfo monoPaletteFlagsField;
/// <summary>
/// Copies the contents of the <see cref="FTBitmap"/> to a GDI+ <see cref="Bitmap"/>.
/// </summary>
/// <returns>A GDI+ <see cref="Bitmap"/> containing this bitmap's data.</returns>
public static Bitmap ToGdipBitmap(this FTBitmap ftBitmap)
{
return ToGdipBitmap(ftBitmap, Color.Black);
}
/// <summary>
/// Copies the contents of the <see cref="FTBitmap"/> to a GDI+ <see cref="Bitmap"/>.
/// </summary>
/// <param name="color">The color of the text.</param>
/// <returns>A GDI+ <see cref="Bitmap"/> containing this bitmap's data with a transparent background.</returns>
public static Bitmap ToGdipBitmap(this FTBitmap b, Color color)
{
if (b.IsDisposed)
throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
if (b.Width == 0 || b.Rows == 0)
throw new InvalidOperationException("Invalid image size - one or both dimensions are 0.");
//TODO deal with negative pitch
switch (b.PixelMode)
{
case PixelMode.Mono:
{
Bitmap bmp = new Bitmap(b.Width, b.Rows, PixelFormat.Format1bppIndexed);
var locked = bmp.LockBits(new Rectangle(0, 0, b.Width, b.Rows), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
for (int i = 0; i < b.Rows; i++)
Copy(b.Buffer, i * b.Pitch, locked.Scan0, i * locked.Stride, locked.Stride);
bmp.UnlockBits(locked);
ColorPalette palette = bmp.Palette;
palette.Entries[0] = Color.FromArgb(0, color);
palette.Entries[1] = Color.FromArgb(255, color);
bmp.Palette = palette;
return bmp;
}
case PixelMode.Gray4:
{
Bitmap bmp = new Bitmap(b.Width, b.Rows, PixelFormat.Format4bppIndexed);
var locked = bmp.LockBits(new Rectangle(0, 0, b.Width, b.Rows), ImageLockMode.ReadWrite, PixelFormat.Format4bppIndexed);
for (int i = 0; i < b.Rows; i++)
Copy(b.Buffer, i * b.Pitch, locked.Scan0, i * locked.Stride, locked.Stride);
bmp.UnlockBits(locked);
ColorPalette palette = bmp.Palette;
for (int i = 0; i < palette.Entries.Length; i++)
{
float a = (i * 17) / 255f;
palette.Entries[i] = Color.FromArgb(i * 17, (int)(color.R * a), (int)(color.G * a), (int)(color.B * a));
}
bmp.Palette = palette;
return bmp;
}
case PixelMode.Gray:
{
Bitmap bmp = new Bitmap(b.Width, b.Rows, PixelFormat.Format8bppIndexed);
var locked = bmp.LockBits(new Rectangle(0, 0, b.Width, b.Rows), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
for (int i = 0; i < b.Rows; i++)
Copy(b.Buffer, i * b.Pitch, locked.Scan0, i * locked.Stride, locked.Stride);
bmp.UnlockBits(locked);
ColorPalette palette = bmp.Palette;
for (int i = 0; i < palette.Entries.Length; i++)
{
float a = i / 255f;
palette.Entries[i] = Color.FromArgb(i, (int)(color.R * a), (int)(color.G * a), (int)(color.B * a));
}
//HACK There's a bug in Mono's libgdiplus requiring the "PaletteHasAlpha" flag to be set for transparency to work properly
//See https://github.com/Robmaister/SharpFont/issues/62
if (!hasCheckedForMono)
{
hasCheckedForMono = true;
isRunningOnMono = Type.GetType("Mono.Runtime") != null;
if (isRunningOnMono)
{
// The field needed may be named "flags" or "_flags", dependin on the version of Mono. To be thorough, check for the first Name that contains "lags".
var fields = typeof(ColorPalette).GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
for (int i = 0; i < fields.Length; i++)
{
if (fields[i].Name.Contains("lags"))
{
monoPaletteFlagsField = fields[i];
break;
}
}
}
}
if (isRunningOnMono)
monoPaletteFlagsField.SetValue(palette, palette.Flags | 1);
bmp.Palette = palette;
return bmp;
}
case PixelMode.Lcd:
{
//TODO apply color
int bmpWidth = b.Width / 3;
Bitmap bmp = new Bitmap(bmpWidth, b.Rows, PixelFormat.Format24bppRgb);
var locked = bmp.LockBits(new Rectangle(0, 0, bmpWidth, b.Rows), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
for (int i = 0; i < b.Rows; i++)
Copy(b.Buffer, i * b.Pitch, locked.Scan0, i * locked.Stride, locked.Stride);
bmp.UnlockBits(locked);
return bmp;
}
/*case PixelMode.VerticalLcd:
{
int bmpHeight = b.Rows / 3;
Bitmap bmp = new Bitmap(b.Width, bmpHeight, PixelFormat.Format24bppRgb);
var locked = bmp.LockBits(new Rectangle(0, 0, b.Width, bmpHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
for (int i = 0; i < bmpHeight; i++)
PInvokeHelper.Copy(Buffer, i * b.Pitch, locked.Scan0, i * locked.Stride, b.Width);
bmp.UnlockBits(locked);
return bmp;
}*/
default:
throw new InvalidOperationException("System.Drawing.Bitmap does not support this pixel mode.");
}
}
/// <summary>
/// A method to copy data from one pointer to another, byte by byte.
/// </summary>
/// <param name="source">The source pointer.</param>
/// <param name="sourceOffset">An offset into the source buffer.</param>
/// <param name="destination">The destination pointer.</param>
/// <param name="destinationOffset">An offset into the destination buffer.</param>
/// <param name="count">The number of bytes to copy.</param>
static unsafe void Copy(IntPtr source, int sourceOffset, IntPtr destination, int destinationOffset, int count)
{
byte* src = (byte*)source + sourceOffset;
byte* dst = (byte*)destination + destinationOffset;
byte* end = dst + count;
while (dst != end)
*dst++ = *src++;
}
}
}