Cotton code
178 lines · 10.4 KB · KotlinRaw
package com.cottondb.albumsimport com.cottondb.albums.analysis.Exposureimport com.cottondb.albums.analysis.Kindsimport com.cottondb.albums.analysis.KotlinFeaturesimport com.cottondb.albums.analysis.ScreenFactsimport com.cottondb.albums.analysis.YuNetimport com.cottondb.albums.library.LibraryItemimport org.junit.Assert.assertArrayEqualsimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Testimport kotlin.math.ln/** * The Kotlin stand-in for the core's features, against values worked by hand: the numbers the core's port * (albums.features.*) must give back exactly, so both phones agree. */class AnalysisFeaturesTest {    private val f = KotlinFeatures    @Test    fun area_averages_exactly_with_fractional_edges() {        val four = FloatArray(16) { it.toFloat() }        assertArrayEquals(floatArrayOf(2.5f, 4.5f, 10.5f, 12.5f), f.area(four, 4, 4, 2, 2), 1e-6f)        // 3 to 2: [0, 1.5) takes 0 whole and half of 3 → 1.5 / 1.5 = 1; [1.5, 3) half of 3 and 6 whole → 7.5 / 1.5 = 5.        assertArrayEquals(floatArrayOf(1f, 5f), f.area(floatArrayOf(0f, 3f, 6f), 3, 1, 2, 1), 1e-6f)    }    @Test    fun grey_is_bt601_luma() {        val red = 0xFFFF0000.toInt(); val green = 0xFF00FF00.toInt(); val blue = 0xFF0000FF.toInt(); val white = -1        assertArrayEquals(floatArrayOf(76.245f, 149.685f, 29.07f, 255f), f.grey(intArrayOf(red, green, blue, white), 4, 1), 1e-3f)    }    @Test    fun dhash_sets_bit_y8_plus_x_when_brighter_than_the_right() {        // 9 x 8 is drawn as is: falling to the right sets every bit, rising sets none.        assertEquals(-1L, f.dhash64(FloatArray(72) { 100f - (it % 9) * 10 }, 9, 8))        assertEquals(0L, f.dhash64(FloatArray(72) { (it % 9) * 10f }, 9, 8))        val flat = FloatArray(72) { 50f }        flat[0] = 60f            // row 0, x 0 → bit 0        flat[1 * 9 + 3] = 60f    // row 1, x 3 → bit 11 (x 2 is darker than it: no bit)        assertEquals(1L or (1L shl 11), f.dhash64(flat, 9, 8))        // A bigger picture is area-averaged to 9 x 8 first: 18 x 16 of the falling ramp, each column pair alike.        assertEquals(-1L, f.dhash64(FloatArray(18 * 16) { 200f - ((it % 18) / 2) * 10 }, 18, 16))    }    @Test    fun sharpness_is_the_laplacians_variance_over_the_inside() {        assertEquals(0f, f.sharpness(FloatArray(64 * 64) { 128f }), 0f)        // A 0/255 checkerboard: every inside Laplacian is ±1020, half each → variance 1020² = 1,040,400.        val board = FloatArray(64 * 64) { i -> if (((i / 64) + (i % 64)) % 2 == 0) 255f else 0f }        assertEquals(1_040_400f, f.sharpness(board), 0.5f)        // One white pixel at (10, 10): −1020 there and +255 at its four neighbours over 62² = 3,844 → 1,300,500 / 3,844.        val dot = FloatArray(64 * 64).also { it[10 * 64 + 10] = 255f }        assertEquals(338.31946f, f.sharpness(dot), 1e-3f)    }    @Test    fun exposure_is_mean_clipped_share_and_nearest_rank_percentiles() {        val half = FloatArray(4096) { if (it < 2048) 0f else 255f }        assertEquals(Exposure(0.5f, 1f, 0f, 0f, 1f), f.exposure(half))        // 0..255 sixteen times: mean 127.5; under 4 or over 251 is 8 of 256; ranks 204, 2047, 3890 → 12, 127, 243.        val ramp = FloatArray(4096) { (it % 256).toFloat() }        val e = f.exposure(ramp)        assertEquals(0.5f, e.light, 1e-6f)        assertEquals(0.03125f, e.clipped, 1e-6f)        assertEquals(12f / 255, e.p05, 1e-6f)        assertEquals(127f / 255, e.p50, 1e-6f)        assertEquals(243f / 255, e.p95, 1e-6f)    }    @Test    fun print16_rounds_the_16px_grey_and_its_distance_is_0_to_2() {        val ramp = FloatArray(32 * 32) { i -> (i % 32) * 8f }        val p = f.print16(ramp, 32, 32)        assertEquals(256, p.size)        // Columns 0 and 1 (0 and 8) average to 4, columns 30 and 31 (240, 248) to 244.        assertEquals(4, p[0].toInt() and 0xFF)        assertEquals(244, p[15].toInt() and 0xFF)        assertEquals(0f, f.printDistance(p, p), 1e-6f)        val inverted = ByteArray(256) { (255 - (p[it].toInt() and 0xFF)).toByte() }        assertEquals(2f, f.printDistance(p, inverted), 1e-5f)    }    @Test    fun the_similarity_is_least_squares_without_reflection() {        val t = KotlinFeatures.TEMPLATE        assertArrayEquals(doubleArrayOf(1.0, 0.0, 0.0, 0.0), f.similarity(t, t), 1e-9)        // Half size and moved by (10, 20): twice the scale back, and t = template − 2·(template/2 + (10, 20)).        val small = DoubleArray(10) { t[it] * 0.5 + if (it % 2 == 0) 10 else 20 }        assertArrayEquals(doubleArrayOf(2.0, 0.0, -20.0, -40.0), f.similarity(small, t), 1e-9)        // Turned a quarter: src = (−y, x) of each template point → c = 0, s = −1.        val turned = DoubleArray(10) { i -> if (i % 2 == 0) -t[i + 1] else t[i - 1] }        assertArrayEquals(doubleArrayOf(0.0, -1.0, 0.0, 0.0), f.similarity(turned, t), 1e-9)    }    @Test    fun align_face_reads_pixel_centres_and_rounds_half_up() {        val img = IntArray(112 * 112) { i -> (0xFF shl 24) or ((i % 251) shl 16) or (((i / 112) * 2 % 256) shl 8) or ((i * 7) % 256) }        val same = f.alignFace(img, 112, 112, KotlinFeatures.TEMPLATE)!!        for (i in 0 until 112 * 112) {            assertEquals((img[i] shr 16) and 0xFF, same[3 * i].toInt() and 0xFF)            assertEquals((img[i] shr 8) and 0xFF, same[3 * i + 1].toInt() and 0xFF)            assertEquals(img[i] and 0xFF, same[3 * i + 2].toInt() and 0xFF)        }        // Twice as big: output (x, y) is input (2x, 2y), exactly.        val big = IntArray(224 * 224) { i -> (0xFF shl 24) or ((i % 241) shl 16) or ((i % 13) shl 8) or (i % 199) }        val half = f.alignFace(big, 224, 224, DoubleArray(10) { KotlinFeatures.TEMPLATE[it] * 2 })!!        for (y in 0 until 112) for (x in 0 until 112) assertEquals((big[(2 * y) * 224 + 2 * x] shr 16) and 0xFF, half[3 * (y * 112 + x)].toInt() and 0xFF)        // Half a pixel to the right between 10 and 21 rounds half up to 16; outside the picture is black.        val two = IntArray(112 * 112) { i -> if (i % 112 % 2 == 0) 0xFF0A0A0A.toInt() else 0xFF151515.toInt() }        val shifted = f.alignFace(two, 112, 112, DoubleArray(10) { KotlinFeatures.TEMPLATE[it] + if (it % 2 == 0) 0.5 else 0.0 })!!        assertEquals(16, shifted[3 * (50 * 112 + 40)].toInt() and 0xFF)        val far = f.alignFace(two, 112, 112, DoubleArray(10) { KotlinFeatures.TEMPLATE[it] + if (it % 2 == 0) 500.0 else 0.0 })!!        assertEquals(0, far[3 * (50 * 112 + 40)].toInt() and 0xFF)    }    private fun item(path: String, name: String, mime: String, w: Int, h: Int) =        LibraryItem(1, false, "external_primary", 1, path.trimEnd('/').substringAfterLast('/'), path, name, 0, 0, 0, 1, w, h, mime, null, false, null, 0, false)    @Test    fun screenshot_facts_are_folder_name_or_a_png_the_screens_size_without_a_make() {        assertTrue(ScreenFacts.of(item("Pictures/Screenshots/", "Screenshot_20240101-101010.png", "image/png", 1080, 2400), 1080, 2400).screenshot)        assertTrue(ScreenFacts.of(item("DCIM/Camera/", "Screenshot_x.jpg", "image/jpeg", 100, 100), 1080, 2400).screenshot)        val bare = ScreenFacts.of(item("Download/", "a.png", "image/png", 2400, 1080), 1080, 2400)        assertTrue(bare.askMake)        assertTrue(bare.screenshot) // not yet read: the size and PNG say so        assertFalse(bare.copy(noMake = false).screenshot)        assertTrue(bare.copy(noMake = true).screenshot)        assertFalse(ScreenFacts.of(item("DCIM/Camera/", "IMG_1.jpg", "image/jpeg", 1080, 2400), 1080, 2400).screenshot)        assertFalse(ScreenFacts.of(item("Download/", "b.png", "image/png", 1000, 2000), 1080, 2400).screenshot)        assertEquals("folder,png,screen_size", ScreenFacts.of(item("Pictures/Screenshots/", "s.png", "image/png", 1080, 2400), 1080, 2400).said())    }    @Test    fun document_candidates_are_light_colourless_paper_with_ink_and_text_decides() {        assertTrue(Kinds.documentCandidate(Exposure(0.8f, 0f, 0.2f, 0.85f, 0.95f), 0.05f))        assertFalse(Kinds.documentCandidate(Exposure(0.4f, 0f, 0.1f, 0.4f, 0.9f), 0.05f)) // dark        assertFalse(Kinds.documentCandidate(Exposure(0.8f, 0f, 0.2f, 0.85f, 0.95f), 0.3f)) // colourful        assertFalse(Kinds.documentCandidate(Exposure(0.8f, 0f, 0.75f, 0.8f, 0.85f), 0.05f)) // blank        assertTrue(Kinds.document(0.05f, 3))        assertFalse(Kinds.document(0.05f, 2))        assertFalse(Kinds.document(0.01f, 10))        assertEquals(1f, Kinds.chroma(intArrayOf(0xFFFF0000.toInt()), 1), 0f)        assertEquals(0f, Kinds.chroma(intArrayOf(0xFF808080.toInt()), 1), 0f)    }    @Test    fun yunet_heads_decode_as_opencv_does_and_overlaps_are_kept_once() {        val side = 32        fun heads(stride: Int): YuNet.YuHeads {            val cells = (side / stride) * (side / stride)            return YuNet.YuHeads(stride, FloatArray(cells), FloatArray(cells), FloatArray(cells * 4), FloatArray(cells * 10))        }        val h8 = heads(8); val h16 = heads(16); val h32 = heads(32)        // Stride 8, row 1, column 2: score √(0.81·1) = 0.9; centre ((2 + .5)·8, (1 + .5)·8) = (20, 12); 2·8 = 16 across.        val i = 1 * 4 + 2        h8.cls[i] = 0.81f; h8.obj[i] = 1f        h8.bbox[i * 4] = 0.5f; h8.bbox[i * 4 + 1] = 0.5f; h8.bbox[i * 4 + 2] = ln(2f); h8.bbox[i * 4 + 3] = ln(2f)        h8.kps[i * 10] = 0.25f // first point x: (0.25 + 2)·8 = 18        // Stride 16, row 0, column 1: score 0.6, box (12, −4, 16, 16): a third of the first → kept once.        h16.cls[1] = 0.36f; h16.obj[1] = 1f; h16.bbox[4] = 0.25f; h16.bbox[5] = 0.25f        // Below the threshold: never a face.        h32.cls[0] = 0.2f; h32.obj[0] = 1f        val found = YuNet.decode(listOf(h8, h16, h32), side, 0.5f)        assertEquals(1, found.size)        val face = found[0]        assertEquals(0.9f, face.score, 1e-6f)        assertEquals(12f, face.x, 1e-4f); assertEquals(4f, face.y, 1e-4f); assertEquals(16f, face.w, 1e-4f); assertEquals(16f, face.h, 1e-4f)        assertEquals(18f, face.points[0], 1e-5f); assertEquals(8f, face.points[1], 1e-5f); assertEquals(16f, face.points[2], 1e-5f)        // Points ordered as alignment takes them: the eye on the picture's left first, the left mouth corner first.        val ordered = YuNet.ordered(floatArrayOf(70f, 50f, 40f, 52f, 55f, 70f, 68f, 90f, 42f, 91f))        assertArrayEquals(doubleArrayOf(40.0, 52.0, 70.0, 50.0, 55.0, 70.0, 42.0, 91.0, 68.0, 90.0), ordered, 0.0)    }}