public void CalculateHash(byte[] data) { TlshBuilder hashBuilder = new TlshBuilder(); hashBuilder.Update(data); hashTLSH = hashBuilder.IsValid(false) ? hashBuilder.GetHash(false) : null; using (MD5 md5Builder = MD5.Create()) { hashMD5 = md5Builder.ComputeHash(data); } }
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), }); }