public void LoadImage(string filePath) { TestContext.CurrentContext.AttachFolderBrowserShortcut(); var codecs = new IBitmapDecoder[] { OpenCvCodec.Default, GDICodec.Default, WPFCodec.Default, ImageSharpCodec.Default, STBCodec.Default, SkiaCodec.Default }; foreach (var decoder in codecs.OfType <IBitmapDecoder>()) { var sw = System.Diagnostics.Stopwatch.StartNew(); var bitmap = MemoryBitmap.Load(ResourceInfo.From(filePath), decoder); sw.Stop(); TestContext.WriteLine($"Loading {System.IO.Path.GetFileName(filePath)} with {decoder} tool {sw.ElapsedMilliseconds}"); foreach (var encoder in codecs.OfType <IBitmapEncoder>()) { // System.Diagnostics.Debug.Assert(!(decoder is WPFCodec && encoder is SkiaCodec)); var fname = $"{decoder.GetType().Name}-To-{encoder.GetType().Name}.png"; bitmap.Save(new AttachmentInfo(fname), encoder); fname = $"{decoder.GetType().Name}-To-{encoder.GetType().Name}.jpg"; bitmap.Save(new AttachmentInfo(fname), encoder); fname = $"{decoder.GetType().Name}-To-{encoder.GetType().Name}-Gray.jpg"; bitmap .AsSpanBitmap() .ToMemoryBitmap(Pixel.Luminance8.Format) .Save(AttachmentInfo.From(fname), encoder); } } }
public void DrawMemoryAsImageSharp() { var mem = MemoryBitmap.Load(ResourceInfo.From("shannon.jpg"), Codecs.GDICodec.Default); mem.OfType <Pixel.BGRA32>() .AsSpanBitmap() .ReadAsImageSharp(img => { AttachmentInfo.From("result.png").WriteImage(img); return(0); }); }
public void LoadWithMultiCodec(string filePath) { var img = MemoryBitmap.Load(ResourceInfo.From(filePath), Codecs.STBCodec.Default, Codecs.OpenCvCodec.Default, Codecs.ImageSharpCodec.Default, Codecs.GDICodec.Default, Codecs.SkiaCodec.Default); Assert.NotNull(img); Assert.AreEqual(512, img.Width); Assert.AreEqual(512, img.Height); }
public void LoadImageWithDefaultCodec(string filePath) { var sw = System.Diagnostics.Stopwatch.StartNew(); var bitmap = MemoryBitmap.Load(ResourceInfo.From(filePath)); Assert.IsFalse(bitmap.IsEmpty); TestContext.WriteLine(bitmap.Info); sw.Stop(); }
public void LoadWebpToImageSharp() { // load a bitmap with SkiaSharp, which is known to support WEBP. var mem = MemoryBitmap.Load(ResourceInfo.From("shannon.webp"), Codecs.SkiaCodec.Default); using (var proxy = mem.UsingImageSharp()) { mem.Save(AttachmentInfo.From("ImageSharp.png")); } }
public void DrawGDIAsImageSharp() { using var gdi = System.Drawing.Image.FromFile(ResourceInfo.From("shannon.jpg")); using var bmp = new System.Drawing.Bitmap(gdi); using var ptr = bmp.UsingPointerBitmap(); ptr.AsPointerBitmap() .AsSpanBitmapOfType <Pixel.BGRA32>() .ReadAsImageSharp(img => AttachmentInfo.From("result.png").WriteImage(img)); }
public void LoadKinect2ColorFrame() { TestContext.CurrentContext.AttachFolderBrowserShortcut(); var src = MemoryBitmap <ushort> .Load(ResourceInfo.From("Kinect2Color.InteropBmp")); var dst = new MemoryBitmap <Pixel.BGR24>(src.Width, src.Height); dst.AsSpanBitmap().SetPixelsFromYUY2(src); dst.Save(AttachmentInfo.From("kinect2color.jpg")); }
public void LoadAndSaveInteropFormat() { TestContext.CurrentContext.AttachFolderBrowserShortcut(); var img1 = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From("shannon.jpg")); var tmpPath = AttachmentInfo.From("shannon.interopbmp").WriteObject(f => img1.Save(f)); var img2 = MemoryBitmap <Pixel.BGR24> .Load(tmpPath.FullName); img2.Save(AttachmentInfo.From("shannon.png")); }
public void DrawToPlanesTest() { var src = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From("shannon.jpg")); var dst = new SpanPlanesXYZ <float>(256, 256); var xform = System.Numerics.Matrix3x2.CreateScale(dst.Width / (float)src.Width, dst.Height / (float)src.Height); dst.SetPixels <Pixel.BGR24>(xform, src, true); dst.Save(AttachmentInfo.From("result.png")); }
public void LoadWithConversion(string filePath) { var bgr = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From(filePath), STBCodec.Default, OpenCvCodec.Default, ImageSharpCodec.Default, GDICodec.Default, SkiaCodec.Default); Assert.NotNull(bgr); var rgb = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From(filePath), STBCodec.Default, OpenCvCodec.Default, ImageSharpCodec.Default, GDICodec.Default, SkiaCodec.Default); Assert.NotNull(rgb); Assert.AreEqual(512, bgr.Width); Assert.AreEqual(512, bgr.Height); }
private static void _DrawTransformedBitmapWithMulAdd <TPixel>(float mul, float add) where TPixel : unmanaged { var src = MemoryBitmap <Pixel.BGR24> .Load(ResourceInfo.From("shannon.jpg")); var dst = new MemoryBitmap <TPixel>(256, 256); var xform = System.Numerics.Matrix3x2.CreateScale(dst.Width / (float)src.Width, dst.Height / (float)src.Height); dst.AsSpanBitmap().SetPixels <Pixel.BGR24>(xform, src, true, (mul, add)); dst.Save(AttachmentInfo.From($"result-{typeof(TPixel).Name}.jpg")); }
public void EncodeH264Frames() { var inputFrames = FFmpegAutoGen.DecodeFrames(ResourceInfo.From("count-video.mp4")); var finfo = AttachmentInfo .From("output.h264") .WriteVideo(inputFrames.Select(item => item.bitmap)); foreach (var(bitmap, state) in FFmpegAutoGen.DecodeFrames(finfo.FullName)) { var data = string.Join(" ", state.State); TestContext.WriteLine(data); } }
public void DecodeMp4Frames() { foreach (var(bitmap, state) in FFmpegAutoGen.DecodeFrames(ResourceInfo.From("count-video.mp4"))) { var data = string.Join(" ", state.State); TestContext.WriteLine(data); var idx = state.State["index"]; bitmap .AsSpanBitmap() .ToMemoryBitmap() .Save(AttachmentInfo.From($"frame{idx:D3}.jpg")); } }
public void DrawPrimitivesWithMultipleAdapters() { void _drawUsingMultipleDevices(SpanBitmap <Pixel.BGRA32> img) { var slice = img.Slice((10, 10, img.Width - 20, img.Height - 20)); // crop the source image with a 10 pixels margin // cast to OpenCV adapter to blur and find edges in the image. slice .WithOpenCv() .ApplyBlur((4, 4)) .ApplyCanny(100, 200); // cast to GDI Adapter to draw primitives. slice.MutateAsGDI(dc => { var a = new System.Drawing.Point(5, 5); var b = new System.Drawing.Point(50, 50); var c = new System.Drawing.Point(5, 50); var f = new System.Drawing.Font("arial", 30); // draw a triangle dc.DrawPolygon(System.Drawing.Pens.Yellow, new[] { a, b, c }); // draw text dc.DrawString("GDI Text", f, System.Drawing.Brushes.Yellow, new System.Drawing.PointF(5, 60)); }); // cast to SkiaSharp Adapter to draw primitives. slice .WithSkiaSharp() .Draw (canvas => { var p0 = new SkiaSharp.SKPoint(5, 120); var p1 = new SkiaSharp.SKPoint(250, 120); using var skiaPaint = new SkiaSharp.SKPaint { TextSize = 64.0f, IsAntialias = true, StrokeWidth = 20, Color = new SkiaSharp.SKColor(0, 0, 255), Style = SkiaSharp.SKPaintStyle.Fill }; canvas.DrawLine(p0, p1, skiaPaint); canvas.DrawText("SkiaSharp Text", new SkiaSharp.SKPoint(5, 200), skiaPaint); }); // cast to imagesharp Adapter to draw primitives slice.MutateAsImageSharp(ipc => { ipc.FillPolygon(SixLabors.ImageSharp.Color.Green, (5, 250), (50, 250), (5, 300)); ipc.DrawText("ImageSharp Text", SixLabors.Fonts.SystemFonts.CreateFont("Arial", 40), SixLabors.ImageSharp.Color.Green, new SixLabors.ImageSharp.PointF(80, 250)); }); // wpf /* not working yet * slice * .WithWPF() * .Draw * (dc => * { * dc.DrawEllipse(System.Windows.Media.Brushes.Red, null, new System.Windows.Point(5, 5), 10, 10); * dc.DrawEllipse(System.Windows.Media.Brushes.Blue, null, new System.Windows.Point(50, 50), 100, 100); * } * );*/ } // load an image with Sixlabors Imagesharp, notice we use BGRA32 because RGBA32 is NOT supported by GDI. var img = SixLabors.ImageSharp.Image.Load <SixLabors.ImageSharp.PixelFormats.Bgra32>(ResourceInfo.From("shannon.jpg")); // render using multiple devices img.WriteAsSpanBitmap(self => _drawUsingMultipleDevices(self)); // save the result back with ImageSharp AttachmentInfo .From("result.jpg") .WriteObject(f => img.Save(f)); img.Dispose(); }
public void LoadJpegPerformanceTest(string filePath) { TestContext.WriteLine(filePath); TestContext.WriteLine(""); void _writeToTest(string hdr, TimeSpan t) { TestContext.WriteLine($"{hdr} {t.TotalMilliseconds}ms"); } void _doNothing(string hdr, TimeSpan t) { } Action <string, TimeSpan> _action1 = _writeToTest; Action <string, TimeSpan> _action2 = _writeToTest; for (int i = 0; i < 3; ++i) { _action1 = _writeToTest; if (i < 2) { _action1 = _doNothing; } _action2 = _doNothing; if (i == 0) { _action2 = _writeToTest; } using (var perf = PerformanceBenchmark.Run(result => _action1("OpenCV", result))) { var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), OpenCvCodec.Default); _action2($"OpenCV OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero); } using (var perf = PerformanceBenchmark.Run(result => _action1("Skia", result))) { var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), SkiaCodec.Default); _action2($"Skia OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero); } using (var perf = PerformanceBenchmark.Run(result => _action1("WPF", result))) { var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), WPFCodec.Default); _action2($"WPF OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero); } using (var perf = PerformanceBenchmark.Run(result => _action1("ImageSharp", result))) { var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), ImageSharpCodec.Default); _action2($"ImageSharp OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero); } using (var perf = PerformanceBenchmark.Run(result => _action1("GDI", result))) { var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), GDICodec.Default); _action2($"GDI OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero); } using (var perf = PerformanceBenchmark.Run(result => _action1("STB", result))) { var bmp = MemoryBitmap.Load(ResourceInfo.From(filePath), STBCodec.Default); _action2($"STB OutputFmt: {bmp.PixelFormat}", TimeSpan.Zero); } TestContext.WriteLine(""); } }