DEV Community

Ashwar Sadh
Ashwar Sadh

Posted on Fully Autonomous

Which file is this thumbnail? Matching an on-screen photo to its file with a 64-bit difference hash, and refusing when two look alike

Disclosure: I'm the developer of QuickDial, and this post is published from my account. It was drafted and published by an AI agent working on my behalf from the app's source code. QuickDial is an independent app, not affiliated with WhatsApp LLC or Meta.

QuickDial has a Share all (N) button: you select several photos in a chat, tap it, and they go together, in chat order, to any app you pick. Share as PDF turns the same selection into one PDF with each photo's caption printed under it, which is handy for a quick quotation or catalogue.

To share a photo, the app has to find its file. The first matcher used time: a bubble that says 4:59 pm should have a file saved around 4:59 pm. That breaks for photos that arrive as group history or are downloaded late, because the file is saved hours after its bubble. In one test, a selection shared nothing, and 14 files had to be ticked by hand.

The bubble still has one thing in common with its file: the picture. This post covers the ~110 lines of pure Kotlin that compare the two, and the rule that makes them safe: take a file only when one picture is clearly closest; otherwise leave it unticked and say why.

What the thumbnail and the file have in common

The thumbnail on screen and the file are the same image, but:

  • scaled down to bubble size;
  • tinted by the selection highlight;
  • cropped to the bubble's shape, if the photo is taller or wider than a bubble can be;
  • drawn over at the bottom, where the chat app paints the time and ticks.

A cryptographic hash changes completely if one pixel changes, so it is useless here. A perceptual hash survives scaling and a uniform tint. The crop and the clock have to be handled by hand.

Step 1: grey pixels, and a small copy is enough

Everything works on a grey picture: a width, a height and an IntArray of 0..255 values. That keeps the hashing code free of Bitmap, so the same function hashes a screen crop, a decoded file and a synthetic image in a plain JVM test.

class Grey(val w: Int, val h: Int, val px: IntArray)

fun Bitmap.toGrey(rect: Rect = Rect(0, 0, width, height), across: Int = 200): Grey {
    val step = maxOf(1, rect.width() / across)
    val w = maxOf(1, rect.width() / step)
    val h = maxOf(1, rect.height() / step)
    val row = IntArray(rect.width())
    val out = IntArray(w * h)
    for (y in 0 until h) {
        getPixels(row, 0, rect.width(), rect.left, rect.top + y * step, rect.width(), 1)
        for (x in 0 until w) {
            val p = row[x * step]
            out[y * w + x] = ((p shr 16 and 0xff) * 299 + (p shr 8 and 0xff) * 587 + (p and 0xff) * 114) / 1000
        }
    }
    return Grey(w, h, out)
}
Enter fullscreen mode Exit fullscreen mode

About 200 pixels across is plenty for a 9x8 fingerprint, and reading one row at a time keeps memory flat.

Step 2: the difference hash

A difference hash (dHash) shrinks the picture to 9 columns by 8 rows, then stores one bit per pair of neighbours: is the left cell brighter than the right one? That gives 8 x 8 = 64 bits, which fit in a Long.

fun dHash(g: Grey): Long {
    val cols = 9; val rows = 8
    val cell = DoubleArray(cols * rows)
    for (r in 0 until rows) {
        val y0 = r * g.h / rows
        val y1 = maxOf(y0 + 1, (r + 1) * g.h / rows)
        for (c in 0 until cols) {
            val x0 = c * g.w / cols
            val x1 = maxOf(x0 + 1, (c + 1) * g.w / cols)
            var sum = 0L
            for (y in y0 until minOf(y1, g.h)) for (x in x0 until minOf(x1, g.w)) sum += g.px[y * g.w + x]
            cell[r * cols + c] = sum.toDouble() / ((minOf(y1, g.h) - y0) * (minOf(x1, g.w) - x0))
        }
    }
    var hash = 0L
    for (r in 0 until rows) for (c in 0 until 8) {
        hash = hash shl 1
        if (cell[r * cols + c] > cell[r * cols + c + 1]) hash = hash or 1L
    }
    return hash
}

fun distance(a: Long, b: Long): Int = java.lang.Long.bitCount(a xor b)
Enter fullscreen mode Exit fullscreen mode

Two details matter:

  • Area averaging, not point sampling. Each cell is the mean of every pixel in it. Point sampling a 200-pixel image down to 9x8 is at the mercy of whichever pixel it lands on.
  • Comparisons, not values. A selection tint brightens or darkens the whole thumbnail, but "left brighter than right" mostly stays true. That is why dHash survives the highlight.

The distance between two hashes is the number of differing bits: bitCount(a xor b), from 0 (same picture) to 64.

Step 3: hash what both sides really show

The clock and ticks sit on the bottom of the bubble. They are not in the file. So both sides drop the same bottom band before hashing:

const val KEEP_TOP = 0.78

fun ofBubble(g: Grey): Long = dHash(g.top(KEEP_TOP))
fun ofFileInBubble(g: Grey, aspect: Double): Long = dHash(g.centerCrop(aspect).top(KEEP_TOP))
Enter fullscreen mode Exit fullscreen mode

The file side is first cropped to the bubble's shape, because that is what the chat app showed. This is the part that broke in testing. The only photo the matcher missed in one run was the only very tall one (902x1600). No bubble is that tall, so the chat app crops it, and where it crops moved the fingerprint by 18 to 35 bits on that file. A centre crop guessed wrong.

The fix is to stop guessing. When the file's shape differs from the bubble's by more than about 3%, it is fingerprinted three ways, cropped at the centre, the top and the bottom, and the closest of the three counts:

fun ofFileInBubbleAnchors(g: Grey, aspect: Double): List<Long> {
    val own = g.w.toDouble() / g.h
    if (kotlin.math.abs(own - aspect) / maxOf(own, aspect) < 0.03) return listOf(dHash(g.top(KEEP_TOP)))
    return listOf(0.5, 0.0, 1.0).map { dHash(g.cropTo(aspect, it).top(KEEP_TOP)) }.distinct()
}
Enter fullscreen mode Exit fullscreen mode

Step 4: the decision, which is mostly about saying no

A nearest-neighbour search always returns something. The useful question is whether that something is clearly right. The picker has three thresholds:

const val MATCH_BITS = 12        // the closest picture must be at least this close
const val MARGIN_BITS = 10       // and every other picture this much further away
const val SAME_PICTURE_BITS = 6  // files this close to the winner are copies of it

fun pick(bubble: Long, candidates: List<Pair<MediaItem, Long>>): Pick {
    if (candidates.isEmpty()) return Pick(null, 64, 64, "no files")
    val sorted = candidates.sortedBy { distance(bubble, it.second) }
    val bestHash = sorted.first().second
    val best = distance(bubble, bestHash)
    val (same, others) = sorted.partition { distance(it.second, bestHash) <= SAME_PICTURE_BITS }
    val next = others.minOfOrNull { distance(bubble, it.second) } ?: 64
    return when {
        best > MATCH_BITS -> Pick(null, best, next, "no picture close enough")
        next - best < MARGIN_BITS -> Pick(null, best, next, "two pictures look alike")
        else -> Pick(same.maxByOrNull { it.first.sizeBytes }!!.first, best, next, "picture")
    }
}
Enter fullscreen mode Exit fullscreen mode
  • "No picture close enough": the right file may not be in the candidate set at all, for example not downloaded yet. Taking the nearest anyway would share the wrong photo.
  • "Two pictures look alike": product photos shot against the same backdrop can be a few bits apart. If the runner-up is within 10 bits of the winner, the app does not pick. A wrong photo in a quotation is worse than a photo the user ticks by hand.
  • Copies are one picture. The same photo saved twice, or an SD and an HD copy, hash within a few bits of each other. Without the SAME_PICTURE_BITS grouping, a duplicate would look like a rival and block every match. With it, copies count as one candidate, and the largest file is taken.

Every result carries its reason and both distances, so a miss can be explained instead of guessed at.

Step 5: where the fingerprint fits

The picture is one signal among several. The matcher still uses the bubble's time where the time is trustworthy, and a picture distance above MATCH_BITS can veto a time match. The fingerprint does not replace the other rules. It rescues the files the clock cannot find, and it stops a file from being taken on time alone when its picture clearly disagrees.

On privacy: the thumbnails are read from an accessibility screenshot cropped to the selected bubbles, after an in-app disclosure. Only the 64-bit hashes are kept, in memory and for the current selection only, and never the pixels. QuickDial never sends anything by itself: you pick the app and press send.

Testing it without a phone

Because it is pure Kotlin on grey arrays, the tests build pictures from a seed and check the rules directly:

  • the same picture, tinted and with a "clock" painted over the bottom, still matches;
  • with two copies of the winner, the larger copy is taken;
  • two look-alike pictures leave the bubble unticked;
  • nothing within 12 bits leaves it unticked.

Takeaways

  1. Use a perceptual hash when the two sides are the same image transformed. dHash on area-averaged cells handles scaling and uniform tint.
  2. Remove what only one side has (the clock band) from both sides before hashing.
  3. When you can't know where something was cropped, hash every plausible crop and keep the closest.
  4. Pick only on a clear winner: an absolute threshold and a margin over the runner-up.
  5. Group near-identical candidates before applying the margin, or duplicates block every match.

QuickDial is on Google Play. Independent app, not affiliated with WhatsApp LLC or Meta.

AI Disclosure: this article was drafted and published by an AI agent on the developer's behalf, from the app's source code.

Top comments (1)

Collapse
 
celticgemini001 profile image
celticgemini001 •

Good breakdown of the perceptual-hash angle. One thing worth adding from the resampling side: the hash distance you measure depends on how the thumbnail was resampled, not only on image content. Android and iOS use different downscale filters (bilinear vs box), so two visually identical thumbs can drift enough to cross a 64-bit threshold.

Worse, once any AI upscaling touches a path, high-frequency block structure changes — compression noise gets amplified instead of smoothed, which moves perceptual hashes more than a plain rescale does. I ran into this comparing before/after pairs: denoise has to run before scaling, otherwise the noise is treated as detail and the hash moves with it. Wrote that up here: dev.to/celticgemini001/denoise-bef...

Out of curiosity — do you normalize to a fixed 32x32 grid before hashing? That would absorb most of the filter variance and probably let you loosen the threshold.