Yokan / rebuild
Course 1 8624561

ReaderPageProcessingSkiaTest.kt

composeApp/src/skikoTest/kotlin/eu/friedcutlet/yokan/ui/screens/reader/ReaderPageProcessingSkiaTest.kt

test / fixture72 lines
1package eu.friedcutlet.yokan.ui.screens.reader23import coil3.size.Size4import kotlin.test.Test5import kotlin.test.assertEquals6import kotlin.test.assertNotNull7import kotlin.test.assertNull8import kotlin.test.assertSame9import kotlin.test.assertTrue10import kotlinx.coroutines.test.runTest11import org.jetbrains.skia.Bitmap12import org.jetbrains.skia.EncodedImageFormat13import org.jetbrains.skia.IRect14import org.jetbrains.skia.Image15import org.jetbrains.skia.Surface1617class ReaderPageProcessingSkiaTest {18    private val red = 0xFFFF0000.toInt()19    private val blue = 0xFF0000FF.toInt()20    private val white = 0xFFFFFFFF.toInt()2122    @Test23    fun halvesKeepTheRequestedSideOfASpread() = runTest {24        val spread = bitmap(4, 2, white).apply {25            erase(red, IRect.makeXYWH(0, 0, 2, 2))26            erase(blue, IRect.makeXYWH(2, 0, 2, 2))27        }28        val left = createPageHalfTransformation(left = true).transform(spread, Size.ORIGINAL)29        val right = createPageHalfTransformation(left = false).transform(spread, Size.ORIGINAL)30        assertEquals(2 to 2, left.width to left.height)31        assertEquals(red, left.getColor(1, 1))32        assertEquals(blue, right.getColor(0, 0))33    }3435    @Test36    fun rotationTurnsOnlyLandscapePagesInTheChosenDirection() = runTest {37        val landscape = bitmap(4, 2, white).apply { erase(red, IRect.makeXYWH(0, 0, 1, 1)) }38        val clockwise = createWidePageRotation(90).transform(landscape, Size.ORIGINAL)39        val counter = createWidePageRotation(-90).transform(landscape, Size.ORIGINAL)40        assertEquals(2 to 4, clockwise.width to clockwise.height)41        assertEquals(red, clockwise.getColor(1, 0))42        assertEquals(red, counter.getColor(0, 3))43        val portrait = bitmap(2, 4, white)44        assertSame(portrait, createWidePageRotation(90).transform(portrait, Size.ORIGINAL))45    }4647    @Test48    fun spreadMergeScalesToTheTallerPageAndKeepsSides() = runTest {49        val merged = assertNotNull(mergeSpreadImages(png(10, 20, red), png(20, 40, blue)))50        val image = Image.makeFromEncoded(merged)51        assertEquals(40 to 40, image.width to image.height)52        val pixels = Bitmap().apply { check(allocN32Pixels(40, 40)) }53        assertTrue(image.readPixels(pixels, 0, 0))54        assertTrue(pixels.getColor(5, 20).isMostly(red))55        assertTrue(pixels.getColor(35, 20).isMostly(blue))56        assertNull(mergeSpreadImages(byteArrayOf(1, 2, 3), png(2, 2, red)))57    }5859    private fun bitmap(width: Int, height: Int, color: Int) = Bitmap().apply {60        check(allocN32Pixels(width, height))61        erase(color)62    }6364    private fun png(width: Int, height: Int, color: Int): ByteArray {65        val surface = Surface.makeRasterN32Premul(width, height)66        surface.canvas.clear(color)67        return surface.makeImageSnapshot().encodeToData(EncodedImageFormat.PNG)!!.bytes68    }6970    private fun Int.isMostly(color: Int): Boolean =71        listOf(16, 8, 0).all { shift -> kotlin.math.abs((this shr shift and 0xFF) - (color shr shift and 0xFF)) < 24 }72}73

Search chapters, reference and source filenames.