80 using System.Drawing.Imaging;
81 using System.Runtime.InteropServices;
83 namespace SiliconStudio.
Paradox.Assets.SpriteFont.Compiler
86 internal static class BitmapUtils
89 public static void CopyRect(Bitmap source,
Rectangle sourceRegion, Bitmap output,
Rectangle outputRegion)
91 if (sourceRegion.Width != outputRegion.Width ||
92 sourceRegion.Height != outputRegion.Height)
94 throw new ArgumentException();
97 using (var sourceData =
new PixelAccessor(source, ImageLockMode.ReadOnly, sourceRegion))
98 using (var outputData =
new PixelAccessor(output, ImageLockMode.WriteOnly, outputRegion))
100 for (
int y = 0;
y < sourceRegion.Height;
y++)
102 for (
int x = 0; x < sourceRegion.Width; x++)
104 outputData[x,
y] = sourceData[x,
y];
112 public static bool IsAlphaEntirely(byte expectedAlpha, Bitmap bitmap,
Rectangle? region = null)
114 using (var bitmapData =
new PixelAccessor(bitmap, ImageLockMode.ReadOnly, region))
116 for (
int y = 0;
y < bitmapData.Region.Height;
y++)
118 for (
int x = 0; x < bitmapData.Region.Width; x++)
120 byte alpha = bitmapData[x,
y].A;
122 if (alpha != expectedAlpha)
133 public static bool IsRgbEntirely(
Color expectedRgb, Bitmap bitmap)
135 using (var bitmapData =
new PixelAccessor(bitmap, ImageLockMode.ReadOnly))
137 for (
int y = 0;
y < bitmap.Height;
y++)
139 for (
int x = 0; x < bitmap.Width; x++)
141 Color color = bitmapData[x,
y];
146 if ((color.R != expectedRgb.R) ||
147 (color.G != expectedRgb.G) ||
148 (color.B != expectedRgb.B))
161 public static void ConvertGreyToAlpha(Bitmap bitmap,
Rectangle region)
163 using (var bitmapData =
new PixelAccessor(bitmap, ImageLockMode.ReadWrite, region))
165 for (
int y = 0;
y < region.Height;
y++)
167 for (
int x = 0; x < region.Width; x++)
169 var color = bitmapData[x,
y];
172 var alpha = (color.R + color.G + color.B) / 3;
174 bitmapData[x,
y] = Color.FromArgb(alpha, 255, 255, 255);
181 public static void PremultiplyAlphaClearType(Bitmap bitmap)
183 using (var bitmapData =
new PixelAccessor(bitmap, ImageLockMode.ReadWrite))
185 for (
int y = 0;
y < bitmap.Height;
y++)
187 for (
int x = 0; x < bitmap.Width; x++)
189 Color color = bitmapData[x,
y];
191 int a = (color.R + color.G + color.B) / 3;
196 bitmapData[x,
y] = Color.FromArgb(
a, r, g,
b);
205 using (var bitmapData =
new PixelAccessor(bitmap, ImageLockMode.ReadWrite))
207 for (
int y = 0;
y < bitmap.Height;
y++)
209 for (
int x = 0; x < bitmap.Width; x++)
211 Color color = bitmapData[x,
y];
214 int r = color.R * a / 255;
215 int g = color.G * a / 255;
216 int b = color.B * a / 255;
218 bitmapData[x,
y] = Color.FromArgb(
a, r, g,
b);
229 public static void PadBorderPixels(Bitmap bitmap,
Rectangle region)
231 using (var bitmapData =
new PixelAccessor(bitmap, ImageLockMode.ReadWrite))
234 for (
int x = region.Left; x < region.Right; x++)
236 CopyBorderPixel(bitmapData, x, region.Top, x, region.Top - 1);
237 CopyBorderPixel(bitmapData, x, region.Bottom - 1, x, region.Bottom);
241 for (
int y = region.Top;
y < region.Bottom;
y++)
243 CopyBorderPixel(bitmapData, region.Left,
y, region.Left - 1,
y);
244 CopyBorderPixel(bitmapData, region.Right - 1,
y, region.Right,
y);
248 CopyBorderPixel(bitmapData, region.Left, region.Top, region.Left - 1, region.Top - 1);
249 CopyBorderPixel(bitmapData, region.Right - 1, region.Top, region.Right, region.Top - 1);
250 CopyBorderPixel(bitmapData, region.Left, region.Bottom - 1, region.Left - 1, region.Bottom);
251 CopyBorderPixel(bitmapData, region.Right - 1, region.Bottom - 1, region.Right, region.Bottom);
257 static void CopyBorderPixel(PixelAccessor bitmapData,
int sourceX,
int sourceY,
int destX,
int destY)
259 Color color = bitmapData[sourceX, sourceY];
261 bitmapData[destX, destY] = Color.FromArgb(0, color);
270 return bitmap.Clone(bounds,
format);
280 this.bitmap = bitmap;
282 Region = region.GetValueOrDefault(
new Rectangle(0, 0, bitmap.Width, bitmap.Height));
284 data = bitmap.LockBits(Region, mode, PixelFormat.Format32bppArgb);
293 bitmap.UnlockBits(data);
305 public Color this[
int x,
int y]
309 return Color.FromArgb(Marshal.ReadInt32(PixelAddress(x,
y)));
314 Marshal.WriteInt32(PixelAddress(x,
y), value.ToArgb());
320 unsafe IntPtr PixelAddress(
int x,
int y)
322 return new IntPtr((byte*)data.Scan0 + (y * data.Stride) + (x *
sizeof(
int)));
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
SiliconStudio.Core.Mathematics.Color Color
PixelAccessor(Bitmap bitmap, ImageLockMode mode, Rectangle?region=null)
System.Windows.Shapes.Rectangle Rectangle
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
PixelFormat
Defines various types of pixel formats.
HRESULT PremultiplyAlpha(_In_ const Image &srcImage, _In_ DWORD flags, _Out_ ScratchImage &image)