Exemplo n.º 1
0
        public bool IsMatching(HashCollection other, out int distance)
        {
            distance = FindDistance(other);
            int maxMatchDistance = (ComplexHash != null) ? 19 : 0;

            return(distance <= maxMatchDistance);
        }
Exemplo n.º 2
0
 public ImageHashData(object owner, HashCollection hash, EImageHashType type, object guideOb)
 {
     Owner   = owner;
     Hash    = hash;
     Type    = type;
     GuideOb = guideOb;
 }
Exemplo n.º 3
0
        public int FindDistance(HashCollection other)
        {
            if (ComplexHash != null && other.ComplexHash != null)
            {
                return(ComplexHash.TotalDiff(other.ComplexHash, false));
            }

            if (SimpleHash != null && other.SimpleHash != null)
            {
                return(SimpleHash.GetDistance(other.SimpleHash));
            }

            return(int.MaxValue);
        }
Exemplo n.º 4
0
        public bool IsLockedHash(HashCollection hash)
        {
            foreach (ImageHashData testData in lockedHashes)
            {
                testData.IsHashMatching(hash, out int testDistance);

                // exact match only
                if (testDistance == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        public ImageHashData LoadHashEntry(XmlElement xmlElem)
        {
            ImageHashData result = null;

            if (xmlElem != null && xmlElem.Name == "hash" && xmlElem.HasAttribute("type") && (xmlElem.HasAttribute("value") || xmlElem.HasAttribute("valueS")))
            {
                string typeName = xmlElem.GetAttribute("type");

                string hashValueC = xmlElem.HasAttribute("value") ? xmlElem.GetAttribute("value") : null;
                string hashValueS = xmlElem.HasAttribute("valueS") ? xmlElem.GetAttribute("valueS") : null;

                HashCollection hashData = new HashCollection(hashValueC, hashValueS);

                if (typeName.Equals("rule", StringComparison.InvariantCultureIgnoreCase))
                {
                    string            ruleName = xmlElem.GetAttribute("name");
                    TriadGameModifier ruleMod  = ParseRule(ruleName);

                    result = new ImageHashData(ruleMod, hashData, EImageHashType.Rule);
                }
                else if (typeName.Equals("card", StringComparison.InvariantCultureIgnoreCase))
                {
                    string    cardIdName = xmlElem.GetAttribute("id");
                    int       cardId     = int.Parse(cardIdName);
                    TriadCard cardOb     = TriadCardDB.Get().cards[cardId];

                    result = new ImageHashData(cardOb, hashData, EImageHashType.Card);
                }
                else if (typeName.Equals("cactpot", StringComparison.InvariantCultureIgnoreCase))
                {
                    string numIdName = xmlElem.GetAttribute("id");
                    int    numId     = int.Parse(numIdName);
                    if (numId >= 1 && numId <= 9)
                    {
                        result = new ImageHashData(CactpotGame.hashDB[numId - 1], hashData, EImageHashType.Cactpot);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        public List <ImageHashData> LoadImageHashes(JsonParser.ObjectValue jsonOb)
        {
            List <ImageHashData> list = new List <ImageHashData>();

            string[] enumArr = Enum.GetNames(typeof(EImageHashType));
            foreach (KeyValuePair <string, JsonParser.Value> kvp in jsonOb.entries)
            {
                EImageHashType        groupType = (EImageHashType)Array.IndexOf(enumArr, kvp.Key);
                JsonParser.ArrayValue typeArr   = (JsonParser.ArrayValue)kvp.Value;

                foreach (JsonParser.Value value in typeArr.entries)
                {
                    JsonParser.ObjectValue jsonHashOb = (JsonParser.ObjectValue)value;
                    string idStr = jsonHashOb["id"];

                    object hashOwner = null;
                    switch (groupType)
                    {
                    case EImageHashType.Rule: hashOwner = ParseRule(idStr); break;

                    case EImageHashType.Card: hashOwner = TriadCardDB.Get().cards[int.Parse(idStr)]; break;

                    case EImageHashType.Cactpot: hashOwner = CactpotGame.hashDB[int.Parse(idStr) - 1]; break;

                    default: break;
                    }

                    if (hashOwner != null)
                    {
                        HashCollection hashes    = new HashCollection(jsonHashOb["hashC", JsonParser.StringValue.Empty], jsonHashOb["hashS", JsonParser.StringValue.Empty]);
                        ImageHashData  hashEntry = new ImageHashData(hashOwner, hashes, groupType);
                        list.Add(hashEntry);
                    }
                }
            }

            return(list);
        }
Exemplo n.º 7
0
        public bool IsHashMatching(HashCollection testHash)
        {
            int dummyDistance = 0;

            return(Hash.IsMatching(testHash, out dummyDistance));
        }
Exemplo n.º 8
0
 public ImageHashData(object owner, HashCollection hash, EImageHashType type)
 {
     Owner = owner;
     Hash  = hash;
     Type  = type;
 }
Exemplo n.º 9
0
 public bool IsHashMatching(HashCollection testHash, out int distance)
 {
     return(Hash.IsMatching(testHash, out distance));
 }
Exemplo n.º 10
0
        public static FastBitmapHash CalculateImageHash(FastBitmapHSV bitmap, Rectangle box, Rectangle contextBox, FastPixelMatch colorMatch, int hashWidth, int hashHeight, bool bNormalizeColors = false)
        {
            byte[] hashImage = new byte[hashWidth * hashHeight];

            // scale to requested size
            float scaleX = (float)hashWidth / box.Width;
            float scaleY = (float)hashHeight / box.Height;
            float endY   = 0.0f;

            for (int hashY = 0; hashY < hashHeight; hashY++)
            {
                float startY = endY;
                endY = (hashY + 1) / scaleY;
                if (endY >= box.Height)
                {
                    endY = box.Height - 0.00001f;
                }
                float endX = 0.0f;

                for (int hashX = 0; hashX < hashWidth; hashX++)
                {
                    float startX = endX;
                    endX = (hashX + 1) / scaleX;
                    if (endX >= box.Width)
                    {
                        endX = box.Width - 0.00001f;
                    }
                    float sum = 0.0f;

                    int sumDivNum = 0;
                    for (int srcY = (int)startY; srcY <= (int)endY; srcY++)
                    {
                        float partY = 1.0f;
                        if (srcY == (int)startY)
                        {
                            partY -= startY - srcY;
                        }
                        if (srcY == (int)endY)
                        {
                            partY -= srcY + 1 - endY;
                        }

                        for (int srcX = (int)startX; srcX <= (int)endX; srcX++)
                        {
                            float partX = 1.0f;
                            if (srcX == (int)startX)
                            {
                                partX -= startX - srcX;
                            }
                            if (srcX == (int)endX)
                            {
                                partX -= srcX + 1 - endX;
                            }

                            FastPixelHSV valuePx      = bitmap.GetPixel(box.Left + srcX, box.Top + srcY);
                            float        srcValueNorm = colorMatch.IsMatching(valuePx) ? (bNormalizeColors ? 1.0f : (valuePx.Monochrome / 255.0f)) : 0.0f;
                            sum += srcValueNorm * partY * partX;
                            sumDivNum++;
                        }
                    }

                    float storeValueNorm = sum / sumDivNum;
                    hashImage[hashX + (hashY * hashWidth)] = (byte)Math.Min(15, 15 * storeValueNorm);
                }
            }

            TlshBuilder hashBuilder = new TlshBuilder();

            hashBuilder.Update(hashImage);
            TlshHash       complexHash    = hashBuilder.IsValid(false) ? hashBuilder.GetHash(false) : null;
            ScanLineHash   simpleHash     = ScanLineHash.CreateFromImage(hashImage, hashWidth, hashHeight);
            HashCollection hashCollection = new HashCollection(complexHash, simpleHash);

            return(new FastBitmapHash
            {
                Hash = hashCollection,
                Height = hashHeight,
                Width = hashWidth,
                Pixels = hashImage,
                SourceBounds = box,
                ContextBounds = contextBox,
                DrawPos = new Point(box.Right + 1, box.Top),
            });
        }