Ejemplo n.º 1
0
        private void ShowHashCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            const int    hashLength = 32; // bits * bits / 8
            const string table      = "0123456789abcdef";

            var hash = new byte[hashLength];

            Blockhash.ComputeHash(new Bgr2432InputImage(this._screenImage), hash);

            var s = new string('\0', hashLength * 2);

            unsafe
            {
                fixed(char *c = s)
                {
                    for (var i = 0; i < hashLength; i++)
                    {
                        c[i * 2]     = table[hash[i] >> 4];
                        c[i * 2 + 1] = table[hash[i] & 0xf];
                    }
                }
            }

            Clipboard.SetText(s);
            MessageBox.Show(this, s, "画像ハッシュ");
        }
Ejemplo n.º 2
0
        static void TestImpl(int bits, Method method, string expect, string filename)
        {
            var filenames = new[] { "puffy_white.png", "00133601.jpg", "clipper_ship.jpg", "00136101.jpg", "32499201.jpg", "00094701.jpg", "stoplights.jpg", "00011601.jpg", "01109801.jpg", "00002701.jpg", "Babylonian.png", "06855701.jpg", "00106901.jpg", "24442301.jpg", "00631801.jpg", "00631701.jpg", "emptyBasket.png" };
            //var filenames = new[] { "Babylonian.png",};

            var datadir = "testdata/";

            using (var bitmap = (Bitmap)Image.FromFile(datadir + filename))
            {
                var result = Blockhash.Calc(bits, bitmap, method);
                //var hash = BitConverter.ToString(result.ToHashBytes());
                var hash = result.ToHashString();

                Console.WriteLine($"{hash}  {filename}");
                AreEqual(expect, hash);
            }
        }
Ejemplo n.º 3
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = (TransactionId != null ? TransactionId.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Hash != null ? Hash.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ Size.GetHashCode();
         hashCode = (hashCode * 397) ^ Vsize.GetHashCode();
         hashCode = (hashCode * 397) ^ Version.GetHashCode();
         hashCode = (hashCode * 397) ^ LockTime.GetHashCode();
         hashCode = (hashCode * 397) ^ (Blockhash != null ? Blockhash.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ BlockNumber.GetHashCode();
         hashCode = (hashCode * 397) ^ Confirmations.GetHashCode();
         hashCode = (hashCode * 397) ^ (Time != null ? Time.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (VIn != null ? VIn.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (VOut != null ? VOut.GetHashCode() : 0);
         return(hashCode);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// スクリーンショットを撮影し、最も近い選択肢画面を返す
        /// </summary>
        /// <returns>
        /// 閾値より距離の小さい選択肢画面が見つかった場合、その <see cref="SelectionInfo"/> と、距離を返します。
        /// 見つからなかった場合、 null と、見つかった中での最短距離を返します。
        /// </returns>
        private async Task <(SelectionInfo, int)> GetMostSimilarSelectionAsync()
        {
            // ハミング距離の閾値
            // 各々の最短距離より十分に小さいので、距離がこれ以下のものがあれば、それを返す
            const int threshold = 10;

            var arrayPool = ArrayPool <byte> .Shared;
            var hashArray = arrayPool.Rent(32);
            var hash      = new ArraySegment <byte>(hashArray, 0, 32);

            var minDistance = int.MaxValue;

            try
            {
                using (var screenshot = await this._wagahighOperator.CaptureContentAsync().ConfigureAwait(false))
                {
                    Blockhash.ComputeHash(new Bgr2432InputImage(screenshot), hash);
                }

                foreach (var si in SelectionInfo.Selections)
                {
                    var distance = Blockhash.GetDistance(hash, si.ScreenshotHash);
                    if (distance <= threshold)
                    {
                        return(si, distance);
                    }
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                    }
                }
            }
            finally
            {
                arrayPool.Return(hashArray);
            }

            return(null, minDistance);
        }
Ejemplo n.º 5
0
        private int OnExecute()
        {
            var hashLength  = this.Bits * this.Bits / 8;
            var inputLength = this.InputFiles.Length;
            var results     = new byte[hashLength * inputLength];

            // 順番にハッシュを計算
            var successAll = true;

            for (var i = 0; i < this.InputFiles.Length; i++)
            {
                try
                {
                    var span = new Span <byte>(results, hashLength * i, hashLength);

                    using (var image = Image.Load(this.InputFiles[i]))
                    {
                        Blockhash.ComputeHash(new Rgba32InputImage(image), span, this.Bits);
                    }

                    Console.WriteLine("{0}: {1}", i, ToHashString(span));
                }
                catch (Exception ex)
                {
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }
                    Console.Error.WriteLine("{0}: {1}", i, ex);
                    successAll = false;
                }
            }

            if (!successAll)
            {
                return(1);
            }

            // 最短距離を計算
            if (inputLength > 1)
            {
                var minDistance = int.MaxValue;

                for (var i = 0; i < inputLength; i++)
                {
                    for (var j = 0; j < inputLength; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        var bs1      = new ArraySegment <byte>(results, hashLength * i, hashLength);
                        var bs2      = new ArraySegment <byte>(results, hashLength * j, hashLength);
                        var distance = Blockhash.GetDistance(bs1, bs2);

                        if (distance < minDistance)
                        {
                            minDistance = distance;
                        }
                    }
                }

                Console.WriteLine("Min Distance: {0}", minDistance);
            }

            return(0);
        }