Yokan / rebuild
Course 1 8624561

TallImageSplitterTest.kt

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

test / fixture46 lines
1package eu.friedcutlet.yokan.services.download23import java.awt.Color4import java.awt.image.BufferedImage5import java.io.ByteArrayInputStream6import java.io.ByteArrayOutputStream7import javax.imageio.ImageIO8import kotlinx.coroutines.test.runTest9import kotlin.test.*1011class TallImageSplitterTest {12    @Test fun realCodecPreservesTileOrderWidthAndLastRow() = runTest {13        val image = BufferedImage(128, 2501, BufferedImage.TYPE_INT_RGB)14        val graphics = image.createGraphics()15        try {16            graphics.color = Color.RED; graphics.fillRect(0, 0, 128, 1024)17            graphics.color = Color.GREEN; graphics.fillRect(0, 1024, 128, 1024)18            graphics.color = Color.BLUE; graphics.fillRect(0, 2048, 128, 453)19        } finally { graphics.dispose() }20        val bytes = ByteArrayOutputStream().use { ImageIO.write(image, "png", it); it.toByteArray() }21        val tiles = mutableListOf<BufferedImage>()22        assertTrue(splitTallImage(bytes, 1024) { index, encoded ->23            assertEquals(tiles.size, index)24            tiles += requireNotNull(ImageIO.read(ByteArrayInputStream(encoded)))25        })26        assertEquals(listOf(1024, 1024, 453), tiles.map { it.height })27        assertTrue(tiles.all { it.width == 128 })28        val colors = tiles.map { Color(it.getRGB(64, it.height - 1)) }29        assertTrue(colors[0].red > 240 && colors[0].green < 15)30        assertTrue(colors[1].green > 240 && colors[1].blue < 15)31        assertTrue(colors[2].blue > 240 && colors[2].red < 15)32    }3334    @Test fun transparentPagesUseWhiteBackground() = runTest {35        val image = BufferedImage(64, 2049, BufferedImage.TYPE_INT_ARGB)36        val bytes = ByteArrayOutputStream().use { ImageIO.write(image, "png", it); it.toByteArray() }37        var count = 038        assertTrue(splitTallImage(bytes, 1024) { _, encoded ->39            val tile = requireNotNull(ImageIO.read(ByteArrayInputStream(encoded)))40            val color = Color(tile.getRGB(0, 0))41            assertTrue(color.red > 250 && color.green > 250 && color.blue > 250)42            count++43        })44        assertEquals(3, count)45    }46}47

Search chapters, reference and source filenames.