Ejemplo n.º 1
0
        public void TestBitsToBytes()
        {
            byte[] bitValues = new byte[]
            {
                1, 1, 0, 1, 0, 1, 0, 1,
                0, 0, 1, 1, 1, 1, 0, 0,
            };

            byte[] byteValues = ImageBytes.BitsToBytes(bitValues);

            byte[] expectedByteValues = new byte[]
            {
                0xD5,
                0x3C
            };

            // Check expected number in array
            Assert.AreEqual(expectedByteValues.Length, byteValues.Length);

            // Check each bit
            for (int i = 0; i < expectedByteValues.Length; i++)
            {
                Assert.AreEqual(expectedByteValues[i], byteValues[i]);
            }
        }
Ejemplo n.º 2
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]);
            }
        }
Ejemplo n.º 3
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]);
            }
        }
Ejemplo n.º 4
0
        public void TestBytesGetMaxValue()
        {
            const string ResourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            using Image image = TestImage.FromResource(ResourcePath);

            byte max = ImageBytes.GetMaxValue(image);

            Assert.AreEqual(0xff, max);
        }
Ejemplo n.º 5
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;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void TestBytesGetMinMaxValue()
        {
            const string ResourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            using Image image = TestImage.FromResource(ResourcePath);

            Tuple <byte, byte> minMax = ImageBytes.GetMinMaxValue(image);

            Assert.AreEqual(0x00, minMax.Item1);
            Assert.AreEqual(0xff, minMax.Item2);
        }
Ejemplo n.º 7
0
        public void TestFromResource(string resourcePath)
        {
            byte[] imageBytes = ImageBytes.FromResource(resourcePath);

            Assert.IsNotNull(imageBytes);

            // Check not an empty array
            Assert.IsTrue(imageBytes.Length > 0);

            // Check for variation in values
            Assert.IsTrue(imageBytes.Any(b => b > byte.MinValue && b < byte.MaxValue));
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public void TestTryGetImageType(byte[] imageBytes, ImageType expectedType)
        {
            ImageType type;

            if (expectedType == ImageType.Unknown)
            {
                // Should fail - unable to determine type
                Assert.IsFalse(ImageBytes.TryGetImageType(imageBytes, out type));
            }
            else
            {
                Assert.IsTrue(ImageBytes.TryGetImageType(imageBytes, out type));
            }

            Assert.AreEqual(expectedType, type);
        }
Ejemplo n.º 10
0
        public void TestBytesGetAvgValue()
        {
            const string ResourcePath = "Freedom35.ImageProcessing.Tests.Resources.clock.bmp";

            using Image image = TestImage.FromResource(ResourcePath);

            byte avg = ImageBytes.GetAverageValue(image);

            // Average for image
            Assert.AreEqual(0x8d, avg);

            avg = ImageBytes.GetAverageValue(image, 0x22, 0x24);

            // Average in range
            Assert.AreEqual(0x23, avg);
        }
        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));
        }
Ejemplo n.º 14
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (Id.Length != 0)
            {
                hash ^= Id.GetHashCode();
            }
            if (Message.Length != 0)
            {
                hash ^= Message.GetHashCode();
            }
            if (Value != 0D)
            {
                hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(Value);
            }
            if (ImageBytes.Length != 0)
            {
                hash ^= ImageBytes.GetHashCode();
            }
            if (oneOfFieldCase_ == OneOfFieldOneofCase.StringOneof)
            {
                hash ^= StringOneof.GetHashCode();
            }
            if (oneOfFieldCase_ == OneOfFieldOneofCase.BoolOneof)
            {
                hash ^= BoolOneof.GetHashCode();
            }
            if (oneOfFieldCase_ == OneOfFieldOneofCase.MessageOneof)
            {
                hash ^= MessageOneof.GetHashCode();
            }
            hash ^= (int)oneOfFieldCase_;
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
Ejemplo n.º 15
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]);
            }
        }
Ejemplo n.º 16
0
 public void TestIsImageType(byte[] imageBytes, ImageType expectedType)
 {
     Assert.IsTrue(ImageBytes.IsImageType(imageBytes, expectedType));
 }