示例#1
0
        public void TestByRegion(string resourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);

            Assert.IsNotNull(sourceImage);

            Rectangle region = new(0, 0, sourceImage.Width / 2, sourceImage.Height / 2);

            // Crop region of image
            using Image croppedImage = ImageCrop.ByRegion(sourceImage, region);

            Assert.IsNotNull(croppedImage);

            // Check size
            Assert.AreEqual(region.Width, croppedImage.Width);
            Assert.AreEqual(region.Height, croppedImage.Height);

            // Get bytes for each image
            byte[] sourceBytes  = ImageBytes.FromImage(sourceImage);
            byte[] croppedBytes = ImageBytes.FromImage(sourceImage);

            // Compare to ensure correct region cropped
            int limit = croppedImage.Width * croppedImage.Height;

            for (int i = 0; i < limit; i++)
            {
                Assert.AreEqual(sourceBytes[i], croppedBytes[i]);
            }
        }
示例#2
0
        public void TestFromSourceToDestination(string resourcePath)
        {
            // Load source image
            using Bitmap sourceBitmap = TestImage.FromResource(resourcePath) as Bitmap;

            Assert.IsNotNull(sourceBitmap);

            // Create blank bitmap
            using Bitmap copyBitmap = new(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat);

            // Crop region of image
            ImageCopy.FromSourceToDestination(sourceBitmap, copyBitmap);

            // Check size
            Assert.AreEqual(sourceBitmap.Width, copyBitmap.Width);
            Assert.AreEqual(sourceBitmap.Height, copyBitmap.Height);

            // Get bytes for each image
            byte[] sourceBytes = ImageBytes.FromImage(sourceBitmap);
            byte[] copiedBytes = ImageBytes.FromImage(copyBitmap);

            // Compare to ensure correct copy matches
            int limit = System.Math.Min(sourceBytes.Length, copiedBytes.Length);

            for (int i = 0; i < limit; i++)
            {
                Assert.AreEqual(sourceBytes[i], copiedBytes[i]);
            }
        }
示例#3
0
        public void TestCombineAllWithNegative()
        {
            string resourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);
            Assert.IsNotNull(sourceImage);

            using Image negativeCopy = ImageColor.ToNegative(sourceImage);
            Assert.IsNotNull(negativeCopy);

            Image[] imagesToCombine = new Image[]
            {
                sourceImage,
                negativeCopy
            };

            Bitmap combinedImage = ImageCombine.All(imagesToCombine);

            // Get bytes for images
            byte[] combinedBytes = ImageBytes.FromImage(combinedImage);

            // Just check first row of bytes has been combined
            for (int i = 0; i < combinedImage.Width; i++)
            {
                Assert.AreEqual(byte.MaxValue, combinedBytes[i]);
            }

            int pixelDepth = 3;
            int stride     = 1056;
            int width      = 1053;
            int height     = combinedImage.Height;

            int limit = combinedBytes.Length - 4;

            // Compare combined bytes excluding stride padding
            for (int y = 0; y < height; y++)
            {
                int offset = y * stride;

                for (int x = 0; x < width; x += pixelDepth)
                {
                    int i = offset + x;

                    if (i < limit)
                    {
                        Assert.AreEqual(byte.MaxValue, combinedBytes[i]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
示例#4
0
        public static bool Compare(Image image1, Image image2)
        {
            byte[] imageBytes1 = ImageBytes.FromImage(image1);
            byte[] imageBytes2 = ImageBytes.FromImage(image2);

            bool match = imageBytes1.Length == imageBytes2.Length;

            for (int i = 0; match && i < imageBytes1.Length; i++)
            {
                match = imageBytes1[i] == imageBytes2[i];
            }

            return(match);
        }
        public void TestApplyThresholdValue(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            // Apply threshold
            using Image thresholdImage = ImageThreshold.Apply(sourceImage, 0x40);
            Assert.IsNotNull(thresholdImage);

            byte[] withoutAlphaBytes = RemoveAlphaLayerBytes(ImageBytes.FromImage(thresholdImage), sourceImage.PixelFormat);

            // Check bytes thresholded
            Assert.IsTrue(withoutAlphaBytes.Take(sourceImage.Width).All(b => b == byte.MinValue || b == byte.MaxValue));
        }
        public void TestApplyMin(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            // Apply min value
            using Image minImage = ImageThreshold.ApplyMin(sourceImage, 0x20);

            Assert.IsNotNull(minImage);

            byte[] withoutAlphaBytes = RemoveAlphaLayerBytes(ImageBytes.FromImage(minImage), sourceImage.PixelFormat);

            // Check all greater than value
            Assert.IsTrue(withoutAlphaBytes.All(b => b >= 0x20));
        }
        public void TestApplyMinMax(string sourceResourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(sourceResourcePath);

            Assert.IsNotNull(sourceImage);

            // Apply min/max value
            using Image minMaxImage = ImageThreshold.ApplyMinMax(sourceImage, 0x25, 0xc3);

            Assert.IsNotNull(minMaxImage);

            byte[] withoutAlphaBytes = RemoveAlphaLayerBytes(ImageBytes.FromImage(minMaxImage), sourceImage.PixelFormat);

            // Check all within range
            Assert.IsTrue(withoutAlphaBytes.All(b => b >= 0x25 && b <= 0xc3));
        }
示例#8
0
        public void TestCombineAllOne()
        {
            using Image sourceImage = TestImage.FromResource("Freedom35.ImageProcessing.Tests.Resources.clock.bmp");
            Assert.IsNotNull(sourceImage);

            Image[] imagesToCombine = new Image[]
            {
                sourceImage
            };

            // Combine
            Bitmap combinedBitmap = ImageCombine.All(imagesToCombine);

            // Convert for byte comparison
            byte[] sourceBytes   = ImageBytes.FromImage(sourceImage);
            byte[] combinedBytes = ImageBytes.FromImage(combinedBitmap);

            // Should return the same image when only combining one
            for (int i = 0; i < sourceImage.Width; i++)
            {
                Assert.AreEqual(sourceBytes[i], combinedBytes[i]);
            }
        }