Beispiel #1
0
 public void TestComputeHash()
 {
     using (var imageStream = TestImage.Flora.GetStream())
         using (var bmp = (Bitmap)Image.FromStream(imageStream))
         {
             var hash = BitmapHash.Compute(bmp);
             Assert.NotNull(hash);
         }
 }
Beispiel #2
0
        //public float Compare(Hash x, Hash y)
        //{
        //    var hashVariants = new List<byte[]> { x.HashData }; // 0°
        //    hashVariants.Add(BitmapHash.RotateBitmapHash(hashVariants.Last())); // 90°
        //    hashVariants.Add(BitmapHash.RotateBitmapHash(hashVariants.Last())); // 180°
        //    hashVariants.Add(BitmapHash.RotateBitmapHash(hashVariants.Last())); // 270°

        //    return hashVariants.Select(x => BinaryUtils.ComputeByteArrayEquality(x, y.HashData)).Max();
        //}

        public HashContext CreateContext(Hash hash)
        {
            var variants = new ReadOnlyMemory <byte> [4];

            variants[0] = hash.HashData.AsMemory(0, 32);
            variants[1] = BitmapHash.RotateBitmapHash(variants[0].Span); // 90°
            variants[2] = BitmapHash.RotateBitmapHash(variants[1].Span); // 180°
            variants[3] = BitmapHash.RotateBitmapHash(variants[2].Span); // 270°

            var colorData = hash.HashData.AsMemory(32);

            return(new HashContext(colorData, variants));
        }
Beispiel #3
0
        public void TestCompareCompressedPicture()
        {
            using (var imageStream1 = TestImage.Flora.GetStream())
                using (var bmp1 = (Bitmap)Image.FromStream(imageStream1))
                    using (var imageStream2 = TestImage.FloraCompressed.GetStream())
                        using (var bmp2 = (Bitmap)Image.FromStream(imageStream2))
                        {
                            var hash1 = BitmapHash.Compute(bmp1);
                            var hash2 = BitmapHash.Compute(bmp2);

                            var diff = BinaryUtils.ComputeByteArrayEquality(hash1, hash2);
                            Assert.True(diff > 0.9);
                        }
        }
Beispiel #4
0
        public void TestRotateBitmapHash()
        {
            byte[] matrix = new byte[] {
                0b00010001, 0b00110001,
                0b01100111, 0b01011101,
                0b00111010, 0b10001010,
                0b01110101, 0b11111101,
                0b00011101, 0b01011101,
                0b10100100, 0b00110011,
                0b01101011, 0b11000100,
                0b01110011, 0b10000111,
                0b00010100, 0b01100101,
                0b11001111, 0b00111110,
                0b01100011, 0b10011100,
                0b01000110, 0b01100000,
                0b00011111, 0b01010110,
                0b00010111, 0b11110001,
                0b10011001, 0b00101101,
                0b00010100, 0b10010010
            };

            byte[] expectedResult = new byte[]
            {
                0b01000010, 0b00100000,
                0b00001110, 0b11001010,
                0b00000100, 0b11101110,
                0b11110001, 0b10011101,
                0b01010010, 0b01010100,
                0b10111011, 0b00111010,
                0b00111110, 0b11000110,
                0b01110110, 0b11011011,
                0b10100100, 0b11001100,
                0b00111001, 0b01011010,
                0b01101011, 0b00101001,
                0b10110110, 0b00111011,
                0b01000110, 0b00011110,
                0b01010111, 0b11011010,
                0b10010010, 0b10100100,
                0b01100001, 0b10111011,
            };

            var result = BitmapHash.RotateBitmapHash(matrix);

            Assert.Equal(expectedResult, result);
        }
Beispiel #5
0
        public FileInformation Load(IFile file, Stream stream)
        {
            _logger.LogDebug("Load file information of {filename}", file.Filename);

            PhotoProperties?properties;
            Hash            fileHash;
            DateTimeOffset  fileCreatedOn;

            fileCreatedOn = GetCreationDate(stream, file);

            stream.Position = 0;
            try
            {
                using var bmp = new Bitmap(stream);

                var width   = bmp.Width;
                var height  = bmp.Height;
                var bmpHash = new Hash(BitmapHash.Compute(bmp));

                properties = new PhotoProperties(bmpHash, width, height);
            }
            catch (Exception)
            {
                properties = null;
            }

            stream.Position = 0;
            fileHash        = _fileHasher.ComputeHash(stream);

            var fileInformation = new FileInformation(file.Filename, file.CreatedOn, file.ModifiedOn, fileHash, file.Length,
                                                      fileCreatedOn, properties, file.RelativeFilename);

            _logger.LogDebug("FileInformation of file {filename} loaded: {@data}", file.Filename, fileInformation);

            return(fileInformation);
        }