private void InitAreas(string arrkey, string prefix, ScanAreas scanArea, ScanAreaDictionary lookup, KeyValuePair <string, ScanArea> a)
        {
            var key = arrkey.Substring(prefix.Length);

            if (!lookup.ContainsKey(key))
            {
                // lookup[key] = new Tuple<ulong, IDictionary<int, Rectangle>>(a.Value.Hash, new Dictionary<int, Rectangle>());
                lookup[key] = new Dictionary <int, ScanArea>();
            }

            var tmp = lookup[key];

            if (!tmp.ContainsKey(scanArea.BaseResolution))
            {
                tmp[scanArea.BaseResolution] = a.Value;
            }
        }
        /// <summary>The detect.</summary>
        /// <param name="lookup">The lookup.</param>
        /// <param name="key">The key.</param>
        /// <param name="useArea"></param>
        /// <param name="i"></param>
        /// <returns>The <see cref="bool"/>.</returns>
        private DetectionResult Detect(ScanAreaDictionary lookup, string key, IDictionary <int, ScanArea> useArea = null, int threshold = -1)
        {
            threshold = threshold >= 0 ? threshold : ThreshHold;
            if (!lookup.ContainsKey(key))
            {
                Log.Error("No scan data found for requested template: {0}", key);
                return(new DetectionResult(false, -1));
            }

            var template = lookup[key];
            // var rect = new Rectangle();
            int theResolution;
            // ulong matchhash;
            ScanArea area = null;

            if (template.ContainsKey(this.lastResolution))
            {
                // rect = template[this.lastResolution].Rect;
                // matchhash = template[this.lastResolution].Hash;
                area          = template[this.lastResolution];
                theResolution = this.lastResolution;
            }
            else if (template.ContainsKey(this.BaseResolution))
            {
                // rect = template[this.BaseResolution].Rect;
                // matchhash = template[this.BaseResolution].Hash;
                area          = template[this.BaseResolution];
                theResolution = this.BaseResolution;
            }
            else
            {
                Log.Error("No scan data found for requested template: " + key);
                return(new DetectionResult(false, -1));
            }
            if (useArea != null)
            {
                if (useArea.ContainsKey(this.lastResolution))
                {
                    // rect = useArea[this.lastResolution].Rect;
                    area          = useArea[this.lastResolution];
                    theResolution = this.lastResolution;
                }
                else if (useArea.ContainsKey(this.BaseResolution))
                {
                    // rect = useArea[this.BaseResolution].Rect;
                    area          = useArea[this.BaseResolution];
                    theResolution = this.BaseResolution;
                }
                else
                {
                    Log.Error("No scan data found for requested template: " + key);
                    return(new DetectionResult(false, -1));
                }
            }
            var             source   = this.image;
            int             distance = -1;
            DetectionResult result   = new DetectionResult();

            using (var roi = source.Lock(ResolutionHelper.CorrectRectangle(source.Size, area.Rect, theResolution), source.PixelFormat))
            {
                // var roi = source.Clone(ResolutionHelper.CorrectRectangle(source.Size, area.Rect, theResolution), source.PixelFormat);
                //Tuple<byte[], DetectionResult> cached = null;
                //byte[] roiBytes = roi.GetBytes();
                //if (hashCache.TryGetValue(key, out cached))
                //{
                //    if (ImageUtils.AreEqual(cached.Item1, roiBytes))
                //    {
                //        TraceLog.Log("hash cache hit: {0}, hit: {1} distance: {2}", key, cached.Item2.Found, cached.Item2.Distance);
                //        return cached.Item2;
                //    }
                //}

                var hash = this.imageHasher.Create(roi.Data);
                distance = PerceptualHash.HammingDistance(hash, area.Hash);
                TraceLog.Log("Detecting '{0}'. Distance: {1}", key, distance);

                if (distance <= threshold)
                {
                    TraceLog.Log("Detected '{0}'. Distance: {1}", key, distance);
                    Mostly mostly;
                    if (!String.IsNullOrEmpty(area.Mostly) && Enum.TryParse(area.Mostly, out mostly))
                    {
                        result = new DetectionResult(roi.Data.IsMostly(mostly), distance);
                    }
                    else
                    {
                        result = new DetectionResult(true, distance);
                    }
                }
                else
                {
                    result = new DetectionResult(false, distance);
                }
                // hashCache.Set(key, new Tuple<byte[], DetectionResult>(roiBytes, result));
            }
            return(result);
        }