Yokan / rebuild
Course 1 8624561

EinkPageTranscoderTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/download/EinkPageTranscoderTest.kt

test / fixture46 lines
1package eu.friedcutlet.yokan.services.download23import org.jetbrains.skia.Bitmap4import org.jetbrains.skia.Color5import org.jetbrains.skia.EncodedImageFormat6import org.jetbrains.skia.Image7import kotlin.test.Test8import kotlin.test.assertEquals9import kotlin.test.assertNotNull10import kotlin.test.assertNull11import kotlin.test.assertTrue1213class EinkPageTranscoderTest {14    @Test fun colorPageBecomesSmallerGrayscaleJpegAtPanelSize() {15        val page = colorPage(2000, 2880)16        val result = assertNotNull(transcodeEinkPage(page, 1264))17        assertTrue(result.size < page.size)18        assertEquals(0xFF.toByte(), result[0]); assertEquals(0xD8.toByte(), result[1]) // JPEG SOI19        Image.makeFromEncoded(result).use { image ->20            assertEquals(1264, image.width)21            assertEquals(1820, image.height)22            val pixels = Bitmap.makeFromImage(image)23            listOf(10 to 10, 632 to 900, 1200 to 1800).forEach { (x, y) ->24                val color = pixels.getColor(x, y)25                assertEquals(Color.getR(color), Color.getG(color))26                assertEquals(Color.getG(color), Color.getB(color))27            }28        }29    }3031    @Test fun gifsAndUndecodableBytesAreKept() {32        assertNull(transcodeEinkPage("GIF89a-animated".encodeToByteArray(), 1264))33        assertNull(transcodeEinkPage(byteArrayOf(1, 2, 3, 4, 5), 1264))34    }3536    /** Noisy color pixels, like scanned artwork: PNG cannot shrink them, so the size check is meaningful. */37    private fun colorPage(width: Int, height: Int): ByteArray {38        val random = kotlin.random.Random(7)39        val pixels = ByteArray(width * height * 4) { if (it % 4 == 3) -1 else random.nextInt(256).toByte() }40        val bitmap = Bitmap().apply {41            allocPixels(org.jetbrains.skia.ImageInfo.makeN32(width, height, org.jetbrains.skia.ColorAlphaType.OPAQUE))42            installPixels(pixels)43        }44        return Image.makeFromBitmap(bitmap).use { assertNotNull(it.encodeToData(EncodedImageFormat.PNG)).bytes }45    }46}47

Search chapters, reference and source filenames.