Esempio n. 1
0
        public void TestHashCodes()
        {
            var gray = new MemoryBitmap <Byte>(256, 256, Pixel.Luminance8.Format);

            Assert.AreEqual(256, gray.Info.StepByteSize);
            var grayWithStride = new MemoryBitmap <Byte>(256, 256, Pixel.Luminance8.Format, 320);

            Assert.AreEqual(320, grayWithStride.Info.StepByteSize);


            var rnd = new Random(117);

            for (int i = 0; i < 256 * 256; ++i)
            {
                gray.SetPixel(i & 255, i / 256, (Byte)rnd.Next());
            }

            grayWithStride.SetPixels(0, 0, gray);

            Assert.AreEqual(gray.GetHashCode(), grayWithStride.GetHashCode());

            var span = gray.AsSpanBitmap();
            var less = span.AsTypeless();
            var hash = gray.GetHashCode();

            Assert.AreEqual(1953103375, hash);
            Assert.AreEqual(hash, gray.AsTypeless().GetHashCode());
            Assert.AreEqual(hash, span.GetHashCode());
            Assert.AreEqual(hash, less.GetHashCode());
        }
Esempio n. 2
0
        public void TestConvertBitmaps()
        {
            var src = new MemoryBitmap(1920, 1080, Pixel.BGRA32.Format);

            var dst = new MemoryBitmap(src.Info.WithPixelFormat<Pixel.RGBA32>());
            dst.SetPixels(0, 0, src);
        }
Esempio n. 3
0
        public void TestPointerBitmapSlice()
        {
            void _testSlice(PointerBitmap ptr)
            {
                ptr = ptr.Slice((1, 1, 2, 2));

                Assert.AreEqual(true, ptr.IsReadOnly);

                Assert.AreEqual(2, ptr.Width);
                Assert.AreEqual(2, ptr.Height);
                Assert.AreEqual(16, ptr.StepByteSize);

                var ptrSpan = ptr.AsSpanBitmapOfType <int>();

                Assert.AreEqual(0, ptrSpan.GetPixel(0, 0));
                Assert.AreEqual(0, ptrSpan.GetPixel(1, 0));
                Assert.AreEqual(0, ptrSpan.GetPixel(0, 1));
                Assert.AreEqual(0, ptrSpan.GetPixel(1, 1));
            }

            var bmp = new MemoryBitmap <int>(4, 4, Pixel.ARGB32.Format);

            bmp.SetPixels(int.MaxValue);
            bmp.Slice((1, 1, 2, 2)).SetPixels(0);

            bmp.AsSpanBitmap().PinReadablePointer(_testSlice);
        }
Esempio n. 4
0
        public void CalculateImageBlurFactor(string filePath)
        {
            filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, filePath);

            // load image using GDI
            var bitmap = MemoryBitmap
                         .Load(filePath, Codecs.GDICodec.Default)
                         .AsSpanBitmap();

            var blurfactor1 = Processing.SharpnessEvaluator.Evaluate(bitmap, 0.25);

            // blur with openCV
            bitmap.WithOpenCv().ApplyBlur((5, 5));

            var blurfactor2 = Processing.SharpnessEvaluator.Evaluate(bitmap, 0.25);

            // blur with openCV
            bitmap.WithOpenCv().ApplyBlur((32, 32));

            var blurfactor3 = Processing.SharpnessEvaluator.Evaluate(bitmap, 0.25);

            TestContext.WriteLine($"{filePath} => {blurfactor1}, {blurfactor2}, {blurfactor3}");

            bitmap.AttachToCurrentTestAll("final.png");
        }
Esempio n. 5
0
        public void Example2()
        {
            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.webp");

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

            // Use OpenCV to resize the image:
            var span = bmp
                       .WithOpenCv()
                       .ToResizedMemoryBitmap(50, 50, OpenCvSharp.InterpolationFlags.Lanczos4)
                       .AsSpanBitmap();

            // Use GDI to draw a triangle:
            var a = new System.Drawing.Point(5, 5);
            var b = new System.Drawing.Point(45, 45);
            var c = new System.Drawing.Point(5, 45);

            span
            .WithGDI()
            .Draw(dc => dc.DrawPolygon(System.Drawing.Pens.Red, new[] { a, b, c }));

            // Use Imagesharp to save to PNG
            span.AttachToCurrentTestAll("shannon.png");
        }
Esempio n. 6
0
        public void Example1()
        {
            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.webp");

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

            // Use OpenCV to resize the image:

            /*
             * bmp = bmp
             *  .WithOpenCv()
             *  .ToResizedMemoryBitmap(50, 50, OpenCvSharp.InterpolationFlags.Lanczos4);
             */

            using (var proxy = bmp.UsingOpenCv())
            {
                proxy.Mat.Circle(new OpenCvSharp.Point(150, 150), 35, OpenCvSharp.Scalar.Red, 10);

                // proxy.Mat.Blur(new OpenCvSharp.Size(4, 4));
                // OpenCvSharp.Cv2.Blur(proxy.Mat, proxy.Mat, new OpenCvSharp.Size(4, 4));
            }

            using (var proxy = bmp.UsingGDI())
            {
                // Use GDI to draw a triangle:
                var a = new System.Drawing.Point(5, 5);
                var b = new System.Drawing.Point(50, 50);
                var c = new System.Drawing.Point(5, 50);

                proxy.Canvas.DrawPolygon(System.Drawing.Pens.Red, new[] { a, b, c });
            }

            using (var proxy = bmp.UsingImageSharp())
            {
                proxy.Image.Mutate(ipc => ipc.DrawPolygon(SixLabors.ImageSharp.Color.Blue, 2, (50, 5), (50, 50), (5, 5)));
            }

            using (var proxy = bmp.UsingSkiaSharp())
            {
                var p0 = new SkiaSharp.SKPoint(5, 25);
                var p1 = new SkiaSharp.SKPoint(45, 25);

                using var skiaPaint = new SkiaSharp.SKPaint
                      {
                          TextSize    = 64.0f,
                          IsAntialias = true,
                          StrokeWidth = 20,
                          Color       = new SkiaSharp.SKColor(0, 255, 0),
                          Style       = SkiaSharp.SKPaintStyle.Fill
                      };

                proxy.Canvas.DrawLine(p0, p1, skiaPaint);
                proxy.Canvas.DrawText("SkiaSharp", new SkiaSharp.SKPoint(5, 200), skiaPaint);
            }

            // Use Imagesharp to save to PNG
            bmp.Save(AttachmentInfo.From("shannon.png"));
        }
Esempio n. 7
0
        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); });
        }
Esempio n. 8
0
        public void CreateMemoryBitmap()
        {
            var m1 = new MemoryBitmap <UInt32>(16, 16, Pixel.BGRA32.Format);

            m1.SetPixels(0xff406040);

            m1.Save(new AttachmentInfo("result.png"));
        }
        public void LoadImage(string filePath)
        {
            filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, filePath);

            var membmp = MemoryBitmap.Load(filePath, Codecs.OpenCvCodec.Default);

            membmp.Save(AttachmentInfo.From("Result.png"));
        }
Esempio n. 10
0
        public void TestSampler()
        {
            var map = new MemoryBitmap <Pixel.BGRA32>(16, 16);

            map.SetPixels(new Pixel.BGRA32(255, 127, 63, 31));

            var sampler = new Processing._PixelsTransformImplementation.SpanQuantized8Sampler <Pixel.BGRA32, Pixel.BGRA32>(map);

            var pix = sampler.GetSourcePixelOrDefault(8, 8);
        }
Esempio n. 11
0
        public void CreateMemoryBitmapWithKnownType()
        {
            var m1 = new MemoryBitmap <Pixel.BGRA32>(16, 16);

            Assert.AreEqual(Pixel.BGRA32.Format, m1.PixelFormat);

            var m2 = new MemoryBitmap <Pixel.BGRA32>(new Byte[256 * 4], 16, 16);

            Assert.AreEqual(Pixel.BGRA32.Format, m2.PixelFormat);
        }
Esempio n. 12
0
        public void TestTransform(bool useBilinear)
        {
            var src = LoadShannonImage().OfType <Pixel.BGR24>();

            src.Save(new AttachmentInfo("input.png"));

            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\cat.png");
            var cat00    = MemoryBitmap
                           .Load(filePath, Codecs.GDICodec.Default)
                           .OfType <Pixel.BGRA32>();


            filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\QRCode.png");
            var qrcode = MemoryBitmap
                         .Load(filePath, Codecs.GDICodec.Default)
                         .OfType <Pixel.BGRA32>();

            var x = Matrix3x2.CreateScale(0.75f, 0.75f) * Matrix3x2.CreateRotation(0.25f);

            x.Translation = new Vector2(20, 20);
            Matrix3x2.Invert(x, out var xx);

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

            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"Transform {(int)t.TotalMilliseconds}ms")))
            {
                dst.AsSpanBitmap().SetPixels(xx, src.AsSpanBitmap(), useBilinear);
            }

            for (float r = 0.1f; r < 1; r += 0.2f)
            {
                // 1st API

                var xform = Matrix3x2.CreateRotation(r) * Matrix3x2.CreateTranslation(50, 15);
                xform = Matrix3x2.CreateTranslation(-50, -50) * xform * Matrix3x2.CreateTranslation(50, 50);
                xform = xform * Matrix3x2.CreateScale(3, 3);

                dst.AsSpanBitmap().SetPixels(xform, cat00.AsSpanBitmap(), useBilinear, r);
                DrawBounds(dst, cat00.Bounds, xform, Colors.Red);

                // 2nd API

                xform *= Matrix3x2.CreateTranslation(0, 150);

                dst.AsSpanBitmap().SetPixels(xform, cat00.AsSpanBitmap(), useBilinear, r);
                DrawBounds(dst, cat00.Bounds, xform, Colors.Red);
            }

            dst.AsSpanBitmap().SetPixels(Matrix3x2.CreateScale(3), cat00.AsSpanBitmap(), useBilinear);
            dst.AsSpanBitmap().SetPixels(Matrix3x2.CreateScale(-.6f) * Matrix3x2.CreateTranslation(40, 200), cat00.AsSpanBitmap(), useBilinear);
            dst.AsSpanBitmap().SetPixels(Matrix3x2.CreateScale(.3f) * Matrix3x2.CreateRotation(1) * Matrix3x2.CreateTranslation(150, 300), qrcode.AsSpanBitmap(), useBilinear);

            dst.Save(new AttachmentInfo("transformed.png"));
        }
Esempio n. 13
0
        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"));
        }
Esempio n. 14
0
        public void TestTransformWithConversion()
        {
            var src = LoadShannonImage().OfType <Pixel.BGR24>();

            src.Save(new AttachmentInfo("input.png"));

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

            dst.AsSpanBitmap().SetPixels(Matrix3x2.CreateScale(0.7f), src.AsSpanBitmap(), true, 1);

            dst.Save(new AttachmentInfo("transformed.png"));
        }
Esempio n. 15
0
        private static MemoryBitmap LoadShannonImage()
        {
            var filePath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources\\shannon.jpg");

            // load a bitmap with SkiaSharp, which is known to support WEBP.
            var mem = MemoryBitmap.Load(filePath, Codecs.GDICodec.Default);

            var mem2 = new MemoryBitmap(mem.Width, mem.Height, Pixel.BGR24.Format);

            mem2.SetPixels(0, 0, mem);

            return(mem2);
        }
Esempio n. 16
0
        public void TestFlipPerformance(int w, int h, bool multiThread, bool hflip, bool vflip)
        {
            var bmp = new MemoryBitmap <Pixel.RGB24>(w, h).AsSpanBitmap();

            var mirrorEffect = new Processing.BitmapMirror(hflip, vflip, multiThread);

            using (PerformanceBenchmark.Run(t => TestContext.WriteLine($"{w}x{h} HFlip:{hflip} VFlip:{vflip} {Math.Round(t.TotalMilliseconds / 1000)}ms")))
            {
                for (int r = 0; r < 1000; ++r)
                {
                    bmp.ApplyEffect(mirrorEffect);
                }
            }
        }
Esempio n. 17
0
        public static void AttachToCurrentTestAll(this SpanBitmap bmp, string filePath)
        {
            var mem = bmp.ToMemoryBitmap();

            TestContext.WriteLine($"{filePath} {bmp.Info.ToDebuggerDisplayString()}");


            if (bmp.PixelFormat == Pixel.BGR96F.Format || bmp.PixelFormat == Pixel.RGB96F.Format)
            {
                var tmp = new MemoryBitmap(bmp.Width, bmp.Height, Pixel.BGR24.Format);
                SpanBitmap.CopyPixels(bmp.OfType <System.Numerics.Vector3>(), tmp, (0, 1), (0, 255));
                bmp = tmp;
            }

            AttachmentInfo _injectExt(string fp, string extPrefix)
            {
                var ext = System.IO.Path.GetExtension(fp);

                fp = fp.Substring(0, fp.Length - ext.Length);
                return(new AttachmentInfo($"{fp}.{extPrefix}{ext}"));
            }

            var f1 = _injectExt(filePath, "WPF");

            mem.Save(f1, WPFCodec.Default);

            var f2 = _injectExt(filePath, "GDI");

            mem.Save(f2, GDICodec.Default);

            var f3 = _injectExt(filePath, "ImageSharp");

            mem.Save(f3, ImageSharpCodec.Default);

            var f4 = _injectExt(filePath, "SkiaSharp");

            mem.Save(f4, SkiaCodec.Default);

            var f5 = _injectExt(filePath, "OpenCvSharp");

            mem.Save(f5, OpenCvCodec.Default);

            var f6 = _injectExt(filePath, "STB");

            mem.Save(f6, STBCodec.WithQuality(80));

            // TODO: it should compare saved files against bmp
        }
Esempio n. 18
0
        public void CopyPixelsWithStride()
        {
            var m1 = new MemoryBitmap <UInt32>(16, 16, Pixel.BGRA32.Format);

            m1.SetPixel(0, 0, 0xff00ff00);
            m1.SetPixel(0, 1, 0xffff00ff);

            var m2 = new MemoryBitmap <UInt32>(16, 16, Pixel.BGRA32.Format, 17 * 4);

            m2.SetPixels(0, 0, m1);

            m2.Save(new AttachmentInfo("result.png"));

            Assert.AreEqual(0xff00ff00, m2.GetPixel(0, 0));
            Assert.AreEqual(0xffff00ff, m2.GetPixel(0, 1));
        }
Esempio n. 19
0
        public void ResizeTest1(int dstSize)
        {
            var src = LoadShannonImage();
            var dst = new MemoryBitmap(dstSize, dstSize, Pixel.BGR24.Format, dstSize * 3 + 17);

            BitmapsToolkit.FitPixels(src, dst, (0, 1));

            var sw = System.Diagnostics.Stopwatch.StartNew();

            BitmapsToolkit.FitPixels(src, dst, (0, 1));
            var elapsed = sw.Elapsed;

            TestContext.WriteLine($"{elapsed.Milliseconds}ms {elapsed.Ticks}ticks");

            src.Save(AttachmentInfo.From("input.png"));
            dst.Save(AttachmentInfo.From($"output_{dstSize}.png"));
        }
Esempio n. 20
0
        public void CopyGreyPixels()
        {
            var src = new MemoryBitmap <float>(177, 177).Slice((10, 10, 150, 150));
            var dst = new MemoryBitmap <Byte>(177, 177).Slice((10, 10, 150, 150));

            src.SetPixels(1);

            var(min, max) = SpanBitmap.MinMax(src);
            Assert.AreEqual(min, 1);
            Assert.AreEqual(max, 1);

            SpanBitmap.CopyPixels(src, dst, (0, 128), (0, 255));
            Assert.IsTrue(dst.EnumeratePixels().All(p => p.Pixel == 128));

            SpanBitmap.CopyPixels(src, dst, (0, 1), (10, 255));
            Assert.IsTrue(dst.EnumeratePixels().All(p => p.Pixel == 10));

            SpanBitmap.CopyPixels(src, dst, (0, 1), (2, 3));
            Assert.IsTrue(dst.EnumeratePixels().All(p => p.Pixel == 2));
        }
Esempio n. 21
0
        public void CopyRGBPixels()
        {
            var src = new MemoryBitmap <Vector3>(177, 177).Slice((10, 10, 150, 150));
            var dst = new MemoryBitmap <PixelBGR>(177, 177).Slice((10, 10, 150, 150));

            src.SetPixels(Vector3.One);

            Assert.IsTrue(SpanBitmap.ArePixelsEqual(src, src));

            SpanBitmap.CopyPixels(src, dst, (0, 128), (0, 255));
            Assert.IsTrue(dst.EnumeratePixels().All(p => p.Pixel.Equals(new PixelBGR(128))));

            SpanBitmap.CopyPixels(src, dst, (0, 1), (10, 255));
            Assert.IsTrue(dst.EnumeratePixels().All(p => p.Pixel.Equals(new PixelBGR(10))));

            SpanBitmap.CopyPixels(src, dst, (0, 1), (2, 3));
            Assert.IsTrue(dst.EnumeratePixels().All(p => p.Pixel.Equals(new PixelBGR(2))));

            Assert.IsTrue(SpanBitmap.ArePixelsEqual(dst, dst));
        }
Esempio n. 22
0
        private void _AsyncUpdateBitmap()
        {
            var bmp = new Graphics.Bitmaps.MemoryBitmap(256, 256, Graphics.Bitmaps.Pixel.BGR24.Format);
            var rnd = new Random();

            while (true)
            {
                System.Threading.Thread.Sleep(1000 / 100);

                if (bmp.TryGetBuffer(out var buffer))
                {
                    var data = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, int>(buffer.Array);

                    for (int i = 0; i < data.Length; ++i)
                    {
                        data[i] = rnd.Next();
                    }
                }
            }
        }
Esempio n. 23
0
        public void TestReinterpretBitmaps()
        {
            var src = new MemoryBitmap<Pixel.BGRA32>(16, 16);

            src.SetPixel(0, 0, (255, 0, 0, 255));

            var dst = src.AsSpanBitmap().ReinterpretAs<Pixel.RGBA32>();            

            var p = dst.GetPixel(0, 0);

            Assert.AreEqual(0, p.R);
            Assert.AreEqual(0, p.G);
            Assert.AreEqual(255, p.B); // formerly R
            Assert.AreEqual(255, p.A);

            // float RGB to Vector3

            var bgr = new MemoryBitmap<Pixel.BGR96F>(16, 16);

            var xyz = bgr.AsSpanBitmap().ReinterpretAs<System.Numerics.Vector3>();
        }
Esempio n. 24
0
        public void SetPixels()
        {
            var dst = new MemoryBitmap <Byte>(16, 16, Pixel.Luminance8.Format);
            var src = new MemoryBitmap <Byte>(8, 8, Pixel.Luminance8.Format);

            src.SetPixels(50);
            dst.SetPixels(4, 4, src);

            src.SetPixels(255);
            dst.SetPixels(-4, -4, src);

            src.SetPixels(130);
            dst.SetPixels(12, -4, src);

            src.SetPixels(255);
            dst.SetPixels(12, 12, src);

            src.SetPixels(70);
            dst.SetPixels(-4, 12, src);

            dst.SetPixels(-50, 0, src);

            dst.Save(new AttachmentInfo("Result.png"));
        }
Esempio n. 25
0
 public static void DrawBounds <TPixel>(MemoryBitmap <TPixel> target, in BitmapBounds bounds, in Matrix3x2 xform, TPixel color)
Esempio n. 26
0
 public BindableBitmap(MemoryBitmap bmp)
 {
     _Bitmap = bmp;
 }
Esempio n. 27
0
 public static System.IO.FileInfo WriteImage(this AttachmentInfo ainfo, MemoryBitmap image)
 {
     return(ainfo.WriteObject(f => image.Save(f)));
 }