package eu.friedcutlet.yokan.services.download import java.awt.Color import java.awt.image.BufferedImage import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import javax.imageio.ImageIO import kotlinx.coroutines.test.runTest import kotlin.test.* class TallImageSplitterTest { @Test fun realCodecPreservesTileOrderWidthAndLastRow() = runTest { val image = BufferedImage(128, 2501, BufferedImage.TYPE_INT_RGB) val graphics = image.createGraphics() try { graphics.color = Color.RED; graphics.fillRect(0, 0, 128, 1024) graphics.color = Color.GREEN; graphics.fillRect(0, 1024, 128, 1024) graphics.color = Color.BLUE; graphics.fillRect(0, 2048, 128, 453) } finally { graphics.dispose() } val bytes = ByteArrayOutputStream().use { ImageIO.write(image, "png", it); it.toByteArray() } val tiles = mutableListOf() assertTrue(splitTallImage(bytes, 1024) { index, encoded -> assertEquals(tiles.size, index) tiles += requireNotNull(ImageIO.read(ByteArrayInputStream(encoded))) }) assertEquals(listOf(1024, 1024, 453), tiles.map { it.height }) assertTrue(tiles.all { it.width == 128 }) val colors = tiles.map { Color(it.getRGB(64, it.height - 1)) } assertTrue(colors[0].red > 240 && colors[0].green < 15) assertTrue(colors[1].green > 240 && colors[1].blue < 15) assertTrue(colors[2].blue > 240 && colors[2].red < 15) } @Test fun transparentPagesUseWhiteBackground() = runTest { val image = BufferedImage(64, 2049, BufferedImage.TYPE_INT_ARGB) val bytes = ByteArrayOutputStream().use { ImageIO.write(image, "png", it); it.toByteArray() } var count = 0 assertTrue(splitTallImage(bytes, 1024) { _, encoded -> val tile = requireNotNull(ImageIO.read(ByteArrayInputStream(encoded))) val color = Color(tile.getRGB(0, 0)) assertTrue(color.red > 250 && color.green > 250 && color.blue > 250) count++ }) assertEquals(3, count) } }