Browse Source

Some changes to make the usages of GDI in benchmarks and tests more stable, this should hopefully prevent "a generic error occurred in GDI+". Hopefully I didn't miss one... (#835)

af/merge-core
Robin Krom 8 years ago
committed by James Jackson-South
parent
commit
dae7c7a2d8
  1. 4
      tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs
  2. 4
      tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs
  3. 4
      tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs
  4. 5
      tests/ImageSharp.Benchmarks/Drawing/DrawText.cs
  5. 8
      tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs
  6. 4
      tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs
  7. 31
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs

4
tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs

@ -26,7 +26,8 @@ namespace SixLabors.ImageSharp.Benchmarks
{ {
graphics.InterpolationMode = InterpolationMode.Default; graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10); using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
{
graphics.DrawBeziers(pen, new[] { graphics.DrawBeziers(pen, new[] {
new PointF(10, 500), new PointF(10, 500),
new PointF(30, 10), new PointF(30, 10),
@ -34,6 +35,7 @@ namespace SixLabors.ImageSharp.Benchmarks
new PointF(300, 500) new PointF(300, 500)
}); });
} }
}
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream())
{ {

4
tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs

@ -26,13 +26,15 @@ namespace SixLabors.ImageSharp.Benchmarks
{ {
graphics.InterpolationMode = InterpolationMode.Default; graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
var pen = new Pen(System.Drawing.Color.HotPink, 10); using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
{
graphics.DrawLines(pen, new[] { graphics.DrawLines(pen, new[] {
new PointF(10, 10), new PointF(10, 10),
new PointF(550, 50), new PointF(550, 50),
new PointF(200, 400) new PointF(200, 400)
}); });
} }
}
using (var ms = new MemoryStream()) using (var ms = new MemoryStream())
{ {

4
tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs

@ -26,13 +26,15 @@ namespace SixLabors.ImageSharp.Benchmarks
{ {
graphics.InterpolationMode = InterpolationMode.Default; graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10); using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
{
graphics.DrawPolygon(pen, new[] { graphics.DrawPolygon(pen, new[] {
new PointF(10, 10), new PointF(10, 10),
new PointF(550, 50), new PointF(550, 50),
new PointF(200, 400) new PointF(200, 400)
}); });
} }
}
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream())
{ {

5
tests/ImageSharp.Benchmarks/Drawing/DrawText.cs

@ -35,12 +35,13 @@ namespace SixLabors.ImageSharp.Benchmarks
{ {
graphics.InterpolationMode = InterpolationMode.Default; graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10); using (var font = new Font("Arial", 12, GraphicsUnit.Point))
var font = new Font("Arial", 12, GraphicsUnit.Point); {
graphics.DrawString(TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780)); graphics.DrawString(TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780));
} }
} }
} }
}
[Benchmark(Description = "ImageSharp Draw Text - Cached Glyphs")] [Benchmark(Description = "ImageSharp Draw Text - Cached Glyphs")]

8
tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs

@ -34,14 +34,16 @@ namespace SixLabors.ImageSharp.Benchmarks
{ {
graphics.InterpolationMode = InterpolationMode.Default; graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10); using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
var font = new Font("Arial", 12, GraphicsUnit.Point); using (var font = new Font("Arial", 12, GraphicsUnit.Point))
var gp = new GraphicsPath(); using (var gp = new GraphicsPath())
{
gp.AddString(TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat()); gp.AddString(TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat());
graphics.DrawPath(pen, gp); graphics.DrawPath(pen, gp);
} }
} }
} }
}
[Benchmark(Description = "ImageSharp Draw Text Outline - Cached Glyphs")] [Benchmark(Description = "ImageSharp Draw Text Outline - Cached Glyphs")]
public void DrawTextCore() public void DrawTextCore()

4
tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs

@ -26,9 +26,11 @@ namespace SixLabors.ImageSharp.Benchmarks
using (Graphics graphics = Graphics.FromImage(destination)) using (Graphics graphics = Graphics.FromImage(destination))
{ {
graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.SmoothingMode = SmoothingMode.AntiAlias;
HatchBrush brush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.HotPink); using (var brush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.HotPink))
{
graphics.FillRectangle(brush, new Rectangle(0, 0, 800, 800)); // can't find a way to flood fill with a brush graphics.FillRectangle(brush, new Rectangle(0, 0, 800, 800)); // can't find a way to flood fill with a brush
} }
}
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream())
{ {
destination.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); destination.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

31
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs

@ -39,12 +39,14 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
} }
BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat); BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
var image = new Image<TPixel>(w, h);
try
{
byte* sourcePtrBase = (byte*)data.Scan0; byte* sourcePtrBase = (byte*)data.Scan0;
long sourceRowByteCount = data.Stride; long sourceRowByteCount = data.Stride;
long destRowByteCount = w * sizeof(Bgra32); long destRowByteCount = w * sizeof(Bgra32);
var image = new Image<TPixel>(w, h);
Configuration configuration = image.GetConfiguration(); Configuration configuration = image.GetConfiguration();
using (IMemoryOwner<Bgra32> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgra32>(w)) using (IMemoryOwner<Bgra32> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgra32>(w))
@ -65,6 +67,11 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
} }
} }
} }
}
finally
{
bmp.UnlockBits(data);
}
return image; return image;
} }
@ -91,12 +98,14 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
} }
BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat); BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
var image = new Image<TPixel>(w, h);
try
{
byte* sourcePtrBase = (byte*)data.Scan0; byte* sourcePtrBase = (byte*)data.Scan0;
long sourceRowByteCount = data.Stride; long sourceRowByteCount = data.Stride;
long destRowByteCount = w * sizeof(Bgr24); long destRowByteCount = w * sizeof(Bgr24);
var image = new Image<TPixel>(w, h);
Configuration configuration = image.GetConfiguration(); Configuration configuration = image.GetConfiguration();
using (IMemoryOwner<Bgr24> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgr24>(w)) using (IMemoryOwner<Bgr24> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgr24>(w))
@ -110,14 +119,15 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
byte* sourcePtr = sourcePtrBase + (data.Stride * y); byte* sourcePtr = sourcePtrBase + (data.Stride * y);
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount); Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
PixelOperations<TPixel>.Instance.FromBgr24( PixelOperations<TPixel>.Instance.FromBgr24(configuration, workBuffer.GetSpan().Slice(0, w), row);
configuration,
workBuffer.GetSpan().Slice(0, w),
row);
} }
} }
} }
}
finally
{
bmp.UnlockBits(data);
}
return image; return image;
} }
@ -131,6 +141,8 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
var resultBitmap = new Bitmap(w, h, PixelFormat.Format32bppArgb); var resultBitmap = new Bitmap(w, h, PixelFormat.Format32bppArgb);
var fullRect = new Rectangle(0, 0, w, h); var fullRect = new Rectangle(0, 0, w, h);
BitmapData data = resultBitmap.LockBits(fullRect, ImageLockMode.ReadWrite, resultBitmap.PixelFormat); BitmapData data = resultBitmap.LockBits(fullRect, ImageLockMode.ReadWrite, resultBitmap.PixelFormat);
try
{
byte* destPtrBase = (byte*)data.Scan0; byte* destPtrBase = (byte*)data.Scan0;
long destRowByteCount = data.Stride; long destRowByteCount = data.Stride;
@ -150,8 +162,11 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
} }
} }
} }
}
finally
{
resultBitmap.UnlockBits(data); resultBitmap.UnlockBits(data);
}
return resultBitmap; return resultBitmap;
} }

Loading…
Cancel
Save