Skip to content

Commit 91cd482

Browse files
committed
Make image hashing deterministic and format/DPI-agnostic (#4116)
1 parent d9e9067 commit 91cd482

1 file changed

Lines changed: 5 additions & 29 deletions

File tree

Flow.Launcher.Infrastructure/Image/ImageHashGenerator.cs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,15 @@ public string GetHashFromImage(ImageSource imageSource)
3737
normalized = converted;
3838
}
3939

40-
// Draw into a fixed-size RenderTarget at 96 DPI to ensure deterministic pixel buffer
41-
var rtb = new RenderTargetBitmap(HashSize, HashSize, 96, 96, PixelFormats.Pbgra32);
42-
var dv = new DrawingVisual();
43-
44-
using (var dc = dv.RenderOpen())
45-
{
46-
// Uniformly scale to fit into HashSize x HashSize
47-
var scale = Math.Min((double)HashSize / normalized.PixelWidth, (double)HashSize / normalized.PixelHeight);
48-
var drawWidth = normalized.PixelWidth * scale;
49-
var drawHeight = normalized.PixelHeight * scale;
50-
51-
var offsetX = (HashSize - drawWidth) / 2;
52-
var offsetY = (HashSize - drawHeight) / 2;
53-
dc.DrawImage(normalized, new System.Windows.Rect(offsetX, offsetY, drawWidth, drawHeight));
54-
}
55-
56-
rtb.Render(dv);
57-
rtb.Freeze();
58-
59-
// Extract raw pixel bytes
60-
var bpp = rtb.Format.BitsPerPixel;
40+
// Prepare final buffer (should be HashSize x HashSize)
41+
var bpp = normalized.Format.BitsPerPixel;
6142
if (bpp < 1)
6243
return null;
6344

6445
var bytesPerPixel = (bpp + 7) / 8;
65-
var stride = rtb.PixelWidth * bytesPerPixel;
66-
67-
var bufferSize = stride * rtb.PixelHeight;
68-
if (bufferSize < 1)
69-
return null;
70-
71-
var pixels = new byte[bufferSize];
72-
rtb.CopyPixels(pixels, stride, 0);
46+
var stride = normalized.PixelWidth * bytesPerPixel;
47+
var pixels = new byte[stride * normalized.PixelHeight];
48+
normalized.CopyPixels(pixels, stride, 0);
7349

7450
var hashBytes = SHA1.HashData(pixels);
7551
return Convert.ToBase64String(hashBytes);

0 commit comments

Comments
 (0)