Esempio n. 1
0
        public static MemoryBitmap ToMemoryBitmap(this Image src)
        {
            var dst = new MemoryBitmap(_Implementation.GetBitmapInfo(src));

            _Implementation.CopyPixels(src, dst);
            return(dst);
        }
Esempio n. 2
0
        public void SaveMjpeg()
        {
            var f1 = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.jpg");
            var f2 = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon-blurred.jpg");

            var ff1 = MemoryBitmap.Load(f1, GDICodec.Default);
            var ff2 = MemoryBitmap.Load(f2, GDICodec.Default);

            IEnumerable <MemoryBitmap> _getFrames()
            {
                for (int i = 0; i < 10; ++i)
                {
                    yield return(ff1);

                    yield return(ff1);

                    yield return(ff1);

                    yield return(ff1);

                    yield return(ff2);

                    yield return(ff2);

                    yield return(ff2);

                    yield return(ff2);
                }
            }

            AttachmentInfo
            .From("video.avi")
            .WriteAVI(_getFrames());
        }
Esempio n. 3
0
        public Bitmap GetPreviewIFSBitmap(Point centerPoint, float zoom)
        {
            Bitmap previewBM = (Bitmap)MemoryBitmap.Clone();

            DrawCoordinateAxes(centerPoint, previewBM);
            return(previewBM);
        }
Esempio n. 4
0
        public void WarpAffineTransform()
        {
            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.jpg");

            var src = MemoryBitmap.Load(filePath, Codecs.OpenCvCodec.Default);
            var dst = new MemoryBitmap(512, 512, src.Info.PixelFormat);

            var xform = System.Numerics.Matrix3x2.CreateScale(1.3f, 1.3f) * System.Numerics.Matrix3x2.CreateRotation(0.25f);

            xform.Translation = new System.Numerics.Vector2(5, 40);

            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"OpenCV {t}")))
            {
                using var bm = PerformanceBenchmark.Run(result => TestContext.WriteLine(result.TotalMilliseconds));

                OpenCvSharp4Toolkit.WarpAffine(src, dst, xform);
            }
            dst.Save(new AttachmentInfo("result.opencv.jpg"));

            dst.AsSpanBitmap().WritableBytes.Fill(0);
            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"Soft {t}")))
            {
                using var bm = PerformanceBenchmark.Run(result => TestContext.WriteLine(result.TotalMilliseconds));

                dst.AsSpanBitmap().SetPixels(xform, src);
            }
            dst.Save(new AttachmentInfo("result.soft.jpg"));
        }
Esempio n. 5
0
        public void ZXingFindQRCode(string filePath, string expected)
        {
            // http://www.cvsandbox.com/

            filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, filePath);

            var image = MemoryBitmap.Load(filePath, STBCodec.Default);

            // detect code:

            var result = new ZXingCode();

            using var detector = new ZXingCode.Detector();

            detector.Inference(result, image, new System.Drawing.Rectangle(20, 20, 1000, 1000));

            var code = result.Results[0];

            if (string.IsNullOrWhiteSpace(expected))
            {
                Assert.Null(code); return;
            }

            Assert.AreEqual(expected, code.Text);

            // report result:

            TestContext.WriteLine($"Code found: {code?.Text}");

            image
            .CreateDrawingContext()
            .DrawAsset(System.Numerics.Matrix3x2.Identity, result);

            image.Save(new AttachmentInfo("result.png"));
        }
Esempio n. 6
0
        public void FillRuleTest()
        {
            int scale = 1;

            var bmp = new MemoryBitmap <Pixel.BGR24>(16 * scale, 8 * scale);

            for (int y = 0; y < bmp.Height; ++y)
            {
                for (int x = 0; x < bmp.Width; ++x)
                {
                    var z = ((x / scale) & 1) ^ ((y / scale) & 1);
                    if (z == 1)
                    {
                        bmp.SetPixel(x, y, Pixel.GetColor <Pixel.BGR24>(System.Drawing.Color.DarkGray));
                    }
                }
            }

            var dc = bmp.CreateDrawingContext();

            foreach (var tri in _Triangle.GetFillRuleTriangles())
            {
                dc.DrawPolygon(System.Drawing.Color.Red, tri.A * scale, tri.B * scale, tri.C * scale);
            }

            bmp.Save(new AttachmentInfo("result.png"));
        }
        public static Image TryWrapAsImageSharp(MemoryBitmap src)
        {
            switch (src.PixelFormat.Code)
            {
            case Pixel.Alpha8.Code: return(WrapAsImageSharp <A8>(src));

            case Pixel.Luminance8.Code: return(WrapAsImageSharp <L8>(src));

            case Pixel.Luminance16.Code: return(WrapAsImageSharp <L16>(src));

            case Pixel.BGR565.Code: return(WrapAsImageSharp <Bgr565>(src));

            case Pixel.BGRA5551.Code: return(WrapAsImageSharp <Bgra5551>(src));

            case Pixel.BGRA4444.Code: return(WrapAsImageSharp <Bgra4444>(src));

            case Pixel.BGR24.Code: return(WrapAsImageSharp <Bgr24>(src));

            case Pixel.RGB24.Code: return(WrapAsImageSharp <Rgb24>(src));

            case Pixel.BGRA32.Code: return(WrapAsImageSharp <Bgra32>(src));

            case Pixel.RGBA32.Code: return(WrapAsImageSharp <Rgba32>(src));

            case Pixel.ARGB32.Code: return(WrapAsImageSharp <Argb32>(src));

            case Pixel.RGBA128F.Code: return(WrapAsImageSharp <RgbaVector>(src));

            default: throw src.PixelFormat.ThrowArgument(nameof(src));
            }
        }
Esempio n. 8
0
        public void DrawBitmapFont()
        {
            TestContext.CurrentContext.AttachFolderBrowserShortcut();

            // test glyph splitter

            var spriteFont = Graphics.Bitmaps.Fonts.XnaSpriteFont.Load("Resources\\SegoeUiMono16.png");

            Assert.AreEqual(224, spriteFont.Glyphs.Count);

            // for(int i=0; i < glyphs.Length; ++i) { glyphs[i].Save(new AttachmentInfo($"glyph {i}.png")); }

            // create font and raw some text:

            var font = MemoryBitmap <Pixel.BGRA32> .Load("Resources\\SegoeUiMono16.png").ToBitmapFont();

            var dst = new MemoryBitmap <Pixel.BGR24>(512, 512);

            var xform = Matrix3x2.CreateScale(2) * Matrix3x2.CreateRotation(0.2f) * Matrix3x2.CreateTranslation(5, 5);

            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", -1, (font, System.Drawing.Color.White));

            xform *= Matrix3x2.CreateTranslation(0, 40);
            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", 20, (font, System.Drawing.Color.White));

            xform *= Matrix3x2.CreateTranslation(0, 40);
            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", 20, (Fonts.HersheyFont.Simplex, System.Drawing.Color.White));

            xform *= Matrix3x2.CreateTranslation(0, 40);
            dst.CreateDrawingContext().DrawTextLine(xform, "Hello world!", 20, System.Drawing.Color.Red);

            dst.Save(new AttachmentInfo("text.png"));
        }
Esempio n. 9
0
        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);
                }
            }
        }
Esempio n. 10
0
 public static bool CopyTo(this ANDROIDBITMAP src, ref MemoryBitmap dst)
 {
     if (src == null)
     {
         throw new NotImplementedException(nameof(src));
     }
     return(_Implementation.CopyTo(src, ref dst));
 }
Esempio n. 11
0
        public static MemoryBitmap <TPixel> ToMemoryBitmap <TPixel>(this Image src)
            where TPixel : unmanaged
        {
            var dst = new MemoryBitmap <TPixel>(_Implementation.GetBitmapInfo(src));

            _Implementation.CopyPixels(src, dst);
            return(dst);
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
 public static bool CopyTo(this MemoryBitmap src, ANDROIDBITMAP dst)
 {
     if (dst == null)
     {
         throw new ArgumentNullException(nameof(dst));
     }
     return(_Implementation.CopyTo(src, dst));
 }
        public void LoadImage(string filePath)
        {
            filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, filePath);

            var bitmap = MemoryBitmap.Load(filePath, Codecs.ImageSharpCodec.Default);

            bitmap.Save(new AttachmentInfo("Result.png"));
        }
Esempio n. 15
0
 public static bool CopyTo <TPixel>(this ANDROIDBITMAP src, ref MemoryBitmap <TPixel> dst)
     where TPixel : unmanaged
 {
     if (src == null)
     {
         throw new NotImplementedException(nameof(src));
     }
     return(_Implementation.CopyTo(src, ref dst));
 }
        public OpenCvSharp4MemoryAdapter(MemoryBitmap bmp)
        {
            _Handle = bmp.Memory.Pin();

            _SourcePointer = new PointerBitmap(_Handle.Value, bmp.Info);
            _SourceBitmap  = bmp;

            _ProxyBitmap = _Implementation.WrapAsMat(_SourcePointer);
        }
Esempio n. 17
0
        public Bitmap GetPreviewBitmap(List <Vertex> vertices, float zoom, bool drawSides, Color sideColor)
        {
            Bitmap previewBM = (Bitmap)MemoryBitmap.Clone();

            DrawSides = drawSides;
            SideColor = sideColor;
            DrawVertices(vertices, previewBM);
            return(previewBM);
        }
Esempio n. 18
0
        public static bool CopyTo(ANDROIDBITMAP src, ref MemoryBitmap dst)
        {
            var info = src.GetBitmapInfo().ToInterop();

            var ptr = src.LockPixels();

            try { return(new PointerBitmap(ptr, info).CopyTo(ref dst)); }
            finally { src.UnlockPixels(); }
        }
Esempio n. 19
0
        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();
        }
Esempio n. 20
0
        public MemoryBitmap GetDisplayBitmap()
        {
            var(min, max) = SpanBitmap.MinMax(_Depth);

            var rgb = new MemoryBitmap <Byte>(_Depth.Width, _Depth.Height, Pixel.Luminance8.Format);

            SpanBitmap.CopyPixels(_Depth, rgb, (-min, 255.0f / (max - min)), (0, 255));

            return(rgb);
        }
Esempio n. 21
0
        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"));
            }
        }
Esempio n. 22
0
        public static MemoryBitmap ToMemoryBitmap(WIC_READABLE src)
        {
            var binfo = src.GetBitmapInfo();

            var dst = new MemoryBitmap(binfo);

            src.CopyPixels(dst.ToByteArray(), binfo.StepByteSize, 0);

            return(dst);
        }
Esempio n. 23
0
        public MemoryBitmap <Byte> GetGrayBitmap()
        {
            var minmax = SpanBitmap.MinMax(_Depth);

            var gray = new MemoryBitmap <Byte>(_Depth.Width, _Depth.Height, Pixel.Luminance8.Format);

            SpanBitmap.CopyPixels(_Depth, gray, (0, 255), (0, 255));

            return(gray);
        }
Esempio n. 24
0
        static unsafe void _GetPixelsFromDC(MemoryBitmap mb, RECT r, uint *pixels, GraphicsPath path = null)
        {
            var bi = new Api.BITMAPINFO(r.Width, -r.Height, 32);
            var n  = Api.GetDIBits(mb.Hdc, mb.Hbitmap, 0, r.Height, pixels, ref bi, 0);            //DIB_RGB_COLORS

            if (n != r.Height)
            {
                throw new AuException("GetDIBits");
            }
            _SetAlpha(pixels, r, path);
        }
Esempio n. 25
0
        public GDIMemoryAdapter(MemoryBitmap bmp)
        {
            _SourceBitmap = bmp;

            _Handle = bmp.Memory.Pin();

            var ptr = new PointerBitmap(_Handle.Value, bmp.Info);

            _ProxyBitmap   = _Implementation.WrapAsGDIBitmap(ptr);
            _DeviceContext = System.Drawing.Graphics.FromImage(_ProxyBitmap);
        }
Esempio n. 26
0
        public SkiaMemoryAdapter(MemoryBitmap bmp)
        {
            _SourceBitmap = bmp;

            _Handle = bmp.Memory.Pin();

            var ptr = new PointerBitmap(_Handle.Value, bmp.Info);

            _ProxyBitmap   = _Implementation.WrapAsSKBitmap(ptr);
            _DeviceContext = new SkiaSharp.SKCanvas(_ProxyBitmap);
        }
Esempio n. 27
0
        public void Laplacian1()
        {
            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.webp");

            // Use SkiaSharp to load a WEBP image:
            var bmp = MemoryBitmap <Pixel.BGR24> .Load(filePath, Codecs.SkiaCodec.Default);

            bmp.AsSpanBitmap().Apply <Pixel.BGR24, Pixel.RGBA128F>(Laplacian);

            bmp.Save(new AttachmentInfo("laplacian.jpg"));
        }
Esempio n. 28
0
        public void TestResnet50(string imagePath, string expectedResult)
        {
            // https://onnxruntime.ai/docs/tutorials/resnet50_csharp.html

            var srcImage = MemoryBitmap.Load(imagePath);

            var model = OnnxModel.FromFile("Models\\resnet50-v2-7.onnx");

            // image normalization
            // https://github.com/onnx/models/tree/master/vision/classification/resnet#preprocessing
            var modelXform = MultiplyAdd
                             .CreateMul(0.229f, 0.224f, 0.225f)
                             .ConcatAdd(0.485f, 0.456f, 0.406f)
                             // .GetTransposedZYXW()
                             .GetInverse();

            var imagePreprocessor = new ImageProcessor <Pixel.RGB96F>(modelXform);

            using (var session = model.CreateSession())
            {
                var workingTensor = session
                                    .GetInputTensor <float>(0)
                                    .AsSpanTensor4()
                                    .GetSubTensor(0)
                                    .SetImage(srcImage, imagePreprocessor);

                // run

                session.Inference();

                // get results

                var result = session
                             .GetOutputTensor <float>(0)
                             .AsSpanTensor2()
                             .GetSubTensor(0);

                result.ApplySoftMax();

                var scores = result.ToArray();

                var pairs = Labels.resnet50_v2_7_Labels
                            .Zip(scores, (Label, Score) => (Label, Score))
                            .OrderByDescending(item => item.Score)
                            .ToList();

                foreach (var pair in pairs)
                {
                    TestContext.WriteLine($"{pair.Label} = {pair.Score}");
                }

                Assert.AreEqual(expectedResult, pairs[0].Label);
            }
        }
Esempio n. 29
0
        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"));
        }
Esempio n. 30
0
        // [TestCase("Resources\\yukikaze.jpg")]
        public void TestAnime2Sketch(string imagePath)
        {
            var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default);
            MemoryBitmap <Pixel.Luminance8> dstImage = default;

            using (var filter = new Anime2SketchFilter())
            {
                filter.Filter(srcImage, ref dstImage);
            }

            dstImage.Save(new AttachmentInfo(imagePath));
        }
Esempio n. 31
0
        public MemoryBitmap ToBitmap()
        {
            var mbmp = new MemoryBitmap(Width, Height);
            mbmp.Lock();

            for (uint y = 0; y < Height; y++) {
                for (uint x = 0; x < Width; x++) {
                    byte val = Data[y * Width + x];
                    var color = new RGB888(Palette[val]);
                    mbmp[x, y] = color;
                }
            }

            return mbmp;
        }