Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public void DrawPathSystemDrawing()
{
graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
graphics.DrawBeziers(pen, new[] {
new PointF(10, 500),
new PointF(30, 10),
new PointF(240, 30),
new PointF(300, 500)
});
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
{
graphics.DrawBeziers(pen, new[] {
new PointF(10, 500),
new PointF(30, 10),
new PointF(240, 30),
new PointF(300, 500)
});
}
}

using (MemoryStream ms = new MemoryStream())
Expand Down
14 changes: 8 additions & 6 deletions tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public void DrawPathSystemDrawing()
{
graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
var pen = new Pen(System.Drawing.Color.HotPink, 10);
graphics.DrawLines(pen, new[] {
new PointF(10, 10),
new PointF(550, 50),
new PointF(200, 400)
});
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
{
graphics.DrawLines(pen, new[] {
new PointF(10, 10),
new PointF(550, 50),
new PointF(200, 400)
});
}
}

using (var ms = new MemoryStream())
Expand Down
14 changes: 8 additions & 6 deletions tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public void DrawPolygonSystemDrawing()
{
graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
graphics.DrawPolygon(pen, new[] {
new PointF(10, 10),
new PointF(550, 50),
new PointF(200, 400)
});
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
{
graphics.DrawPolygon(pen, new[] {
new PointF(10, 10),
new PointF(550, 50),
new PointF(200, 400)
});
}
}

using (MemoryStream ms = new MemoryStream())
Expand Down
7 changes: 4 additions & 3 deletions tests/ImageSharp.Benchmarks/Drawing/DrawText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public void DrawTextSystemDrawing()
{
graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
var font = new Font("Arial", 12, GraphicsUnit.Point);
graphics.DrawString(TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780));
using (var font = new Font("Arial", 12, GraphicsUnit.Point))
{
graphics.DrawString(TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780));
}
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public void DrawTextSystemDrawing()
{
graphics.InterpolationMode = InterpolationMode.Default;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
var font = new Font("Arial", 12, GraphicsUnit.Point);
var gp = new GraphicsPath();
gp.AddString(TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat());
graphics.DrawPath(pen, gp);
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
using (var font = new Font("Arial", 12, GraphicsUnit.Point))
using (var gp = new GraphicsPath())
{
gp.AddString(TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat());
graphics.DrawPath(pen, gp);
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public void DrawPatternPolygonSystemDrawing()
using (Graphics graphics = Graphics.FromImage(destination))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
HatchBrush 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
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
}
}
using (MemoryStream ms = new MemoryStream())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,39 @@ internal static unsafe Image<TPixel> From32bppArgbSystemDrawingBitmap<TPixel>(Bi
}

BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
byte* sourcePtrBase = (byte*)data.Scan0;
var image = new Image<TPixel>(w, h);
try
{
byte* sourcePtrBase = (byte*)data.Scan0;

long sourceRowByteCount = data.Stride;
long destRowByteCount = w * sizeof(Bgra32);
long sourceRowByteCount = data.Stride;
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))
{
fixed (Bgra32* destPtr = &workBuffer.GetReference())
using (IMemoryOwner<Bgra32> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgra32>(w))
{
for (int y = 0; y < h; y++)
fixed (Bgra32* destPtr = &workBuffer.GetReference())
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);

byte* sourcePtr = sourcePtrBase + (data.Stride * y);

Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
PixelOperations<TPixel>.Instance.FromBgra32(
configuration,
workBuffer.GetSpan().Slice(0, w),
row);
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);

byte* sourcePtr = sourcePtrBase + (data.Stride * y);

Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
PixelOperations<TPixel>.Instance.FromBgra32(
configuration,
workBuffer.GetSpan().Slice(0, w),
row);
}
}
}
}
finally
{
bmp.UnlockBits(data);
}

return image;
}
Expand All @@ -91,33 +98,36 @@ internal static unsafe Image<TPixel> From24bppRgbSystemDrawingBitmap<TPixel>(Bit
}

BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
byte* sourcePtrBase = (byte*)data.Scan0;
var image = new Image<TPixel>(w, h);
try
{
byte* sourcePtrBase = (byte*)data.Scan0;

long sourceRowByteCount = data.Stride;
long destRowByteCount = w * sizeof(Bgr24);
long sourceRowByteCount = data.Stride;
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))
{
fixed (Bgr24* destPtr = &workBuffer.GetReference())
using (IMemoryOwner<Bgr24> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgr24>(w))
{
for (int y = 0; y < h; y++)
fixed (Bgr24* destPtr = &workBuffer.GetReference())
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);

byte* sourcePtr = sourcePtrBase + (data.Stride * y);
byte* sourcePtr = sourcePtrBase + (data.Stride * y);

Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
PixelOperations<TPixel>.Instance.FromBgr24(
configuration,
workBuffer.GetSpan().Slice(0, w),
row);
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
PixelOperations<TPixel>.Instance.FromBgr24(configuration, workBuffer.GetSpan().Slice(0, w), row);
}
}
}
}

finally
{
bmp.UnlockBits(data);
}
return image;
}

Expand All @@ -131,27 +141,32 @@ internal static unsafe Bitmap To32bppArgbSystemDrawingBitmap<TPixel>(Image<TPixe
var resultBitmap = new Bitmap(w, h, PixelFormat.Format32bppArgb);
var fullRect = new Rectangle(0, 0, w, h);
BitmapData data = resultBitmap.LockBits(fullRect, ImageLockMode.ReadWrite, resultBitmap.PixelFormat);
byte* destPtrBase = (byte*)data.Scan0;
try
{
byte* destPtrBase = (byte*)data.Scan0;

long destRowByteCount = data.Stride;
long sourceRowByteCount = w * sizeof(Bgra32);
long destRowByteCount = data.Stride;
long sourceRowByteCount = w * sizeof(Bgra32);

using (IMemoryOwner<Bgra32> workBuffer = image.GetConfiguration().MemoryAllocator.Allocate<Bgra32>(w))
{
fixed (Bgra32* sourcePtr = &workBuffer.GetReference())
using (IMemoryOwner<Bgra32> workBuffer = image.GetConfiguration().MemoryAllocator.Allocate<Bgra32>(w))
{
for (int y = 0; y < h; y++)
fixed (Bgra32* sourcePtr = &workBuffer.GetReference())
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.ToBgra32(configuration, row, workBuffer.GetSpan());
byte* destPtr = destPtrBase + (data.Stride * y);

Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
PixelOperations<TPixel>.Instance.ToBgra32(configuration, row, workBuffer.GetSpan());
byte* destPtr = destPtrBase + (data.Stride * y);

Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
}
}
}
}

resultBitmap.UnlockBits(data);
finally
{
resultBitmap.UnlockBits(data);
}

return resultBitmap;
}
Expand Down