|
| 1 | +package com.getcode.codes.kikcode |
| 2 | + |
| 3 | +import kotlin.test.Test |
| 4 | +import kotlin.test.assertContentEquals |
| 5 | +import kotlin.test.assertEquals |
| 6 | +import kotlin.test.assertFalse |
| 7 | +import kotlin.test.assertSame |
| 8 | +import kotlin.test.assertTrue |
| 9 | + |
| 10 | +/** |
| 11 | + * Covers the packing rule every analyzed camera frame goes through, on both platforms. |
| 12 | + * |
| 13 | + * Android's guard used to read `pixelStride != -1`, which is vacuously true for a YUV_420_888 Y |
| 14 | + * plane (whose pixel stride is always 1), so the `||` short-circuited to always-true and the |
| 15 | + * per-pixel copy ran on every frame even for tightly packed buffers. iOS had the opposite bug: it |
| 16 | + * never unpadded at all. These tests run on both targets so the rule cannot drift again. |
| 17 | + */ |
| 18 | +class LuminancePlaneTest { |
| 19 | + |
| 20 | + /** Android's pre-fix guard, kept verbatim so we can assert the two agree on output. */ |
| 21 | + private fun legacyUnpad( |
| 22 | + data: ByteArray, |
| 23 | + width: Int, |
| 24 | + height: Int, |
| 25 | + rowStride: Int, |
| 26 | + pixelStride: Int, |
| 27 | + ): ByteArray { |
| 28 | + if (width != rowStride || pixelStride != -1) { |
| 29 | + val cleanData = ByteArray(width * height) |
| 30 | + for (y in 0 until height) { |
| 31 | + for (x in 0 until width) { |
| 32 | + cleanData[y * width + x] = data[y * rowStride + x * pixelStride] |
| 33 | + } |
| 34 | + } |
| 35 | + return cleanData |
| 36 | + } |
| 37 | + return data |
| 38 | + } |
| 39 | + |
| 40 | + private fun plane(height: Int, rowStride: Int): ByteArray = |
| 41 | + ByteArray(rowStride * height) { (it % 251).toByte() } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `a plane is tightly packed only when the row stride matches the width`() { |
| 45 | + assertTrue(LuminancePlane.isTightlyPacked(width = 1920, rowStride = 1920, pixelStride = 1)) |
| 46 | + // 64-byte row alignment, the usual source of padding on both platforms |
| 47 | + assertFalse(LuminancePlane.isTightlyPacked(width = 1440, rowStride = 1472, pixelStride = 1)) |
| 48 | + assertFalse(LuminancePlane.isTightlyPacked(width = 1000, rowStride = 1024, pixelStride = 1)) |
| 49 | + // interleaved planes are never packed, even when the arithmetic happens to line up |
| 50 | + assertFalse(LuminancePlane.isTightlyPacked(width = 64, rowStride = 64, pixelStride = 2)) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `the scanner reads exactly width times height bytes`() { |
| 55 | + assertEquals(1920 * 1080, LuminancePlane.scannedByteCount(1920, 1080)) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `tightly packed plane takes the fast path and avoids a copy`() { |
| 60 | + val width = 640 |
| 61 | + val height = 480 |
| 62 | + val data = plane(height, rowStride = width) |
| 63 | + |
| 64 | + val result = LuminancePlane.unpad(data, width, height, rowStride = width, pixelStride = 1) |
| 65 | + |
| 66 | + assertSame(data, result, "tightly packed plane should be returned without copying") |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `padded plane still strips row padding`() { |
| 71 | + val width = 640 |
| 72 | + val height = 480 |
| 73 | + val rowStride = 768 // 128 bytes of row padding |
| 74 | + val data = plane(height, rowStride) |
| 75 | + |
| 76 | + val result = LuminancePlane.unpad(data, width, height, rowStride, pixelStride = 1) |
| 77 | + |
| 78 | + assertEquals(width * height, result.size) |
| 79 | + for (y in 0 until height) { |
| 80 | + for (x in 0 until width) { |
| 81 | + assertEquals(data[y * rowStride + x], result[y * width + x], "mismatch at ($x,$y)") |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `interleaved plane still honours pixel stride`() { |
| 88 | + val width = 32 |
| 89 | + val height = 16 |
| 90 | + val pixelStride = 2 |
| 91 | + val rowStride = width * pixelStride |
| 92 | + val data = plane(height, rowStride) |
| 93 | + |
| 94 | + val result = LuminancePlane.unpad(data, width, height, rowStride, pixelStride) |
| 95 | + |
| 96 | + assertEquals(width * height, result.size) |
| 97 | + for (y in 0 until height) { |
| 98 | + for (x in 0 until width) { |
| 99 | + assertEquals(data[y * rowStride + x * pixelStride], result[y * width + x]) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * The Android fix is a pure speedup: for every plane geometry the camera can hand us, the bytes |
| 106 | + * the scanner sees must be byte-identical to what the old code produced. |
| 107 | + */ |
| 108 | + @Test |
| 109 | + fun `output is byte-identical to the pre-fix implementation`() { |
| 110 | + val geometries = listOf( |
| 111 | + Triple(640, 480, 640), |
| 112 | + Triple(640, 480, 768), |
| 113 | + Triple(1280, 720, 1280), |
| 114 | + Triple(1280, 720, 1408), |
| 115 | + Triple(1920, 1080, 1920), |
| 116 | + Triple(1920, 1080, 2048), |
| 117 | + ) |
| 118 | + |
| 119 | + for ((width, height, rowStride) in geometries) { |
| 120 | + val data = plane(height, rowStride) |
| 121 | + val fixed = LuminancePlane.unpad(data, width, height, rowStride, pixelStride = 1) |
| 122 | + val legacy = legacyUnpad(data, width, height, rowStride, pixelStride = 1) |
| 123 | + |
| 124 | + // The fast path hands back the backing array, which is longer than width*height when |
| 125 | + // the buffer is over-allocated; the scanner only reads the first width*height. |
| 126 | + val scanned = LuminancePlane.scannedByteCount(width, height) |
| 127 | + assertTrue(fixed.size >= scanned, "${width}x$height/$rowStride too small") |
| 128 | + assertContentEquals( |
| 129 | + legacy.copyOf(scanned), |
| 130 | + fixed.copyOf(scanned), |
| 131 | + "content drift at ${width}x$height rowStride=$rowStride", |
| 132 | + ) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments