|
| 1 | +package org.jetbrains.kotlinx.dataframe.io |
| 2 | + |
| 3 | +import com.beust.klaxon.JsonArray |
| 4 | +import com.beust.klaxon.JsonObject |
| 5 | +import io.kotest.matchers.shouldBe |
| 6 | +import io.kotest.matchers.string.shouldContain |
| 7 | +import org.jetbrains.kotlinx.dataframe.api.dataFrameOf |
| 8 | +import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.KOTLIN_DATAFRAME |
| 9 | +import org.jetbrains.kotlinx.dataframe.impl.io.resizeKeepingAspectRatio |
| 10 | +import org.jetbrains.kotlinx.dataframe.io.Base64ImageEncodingOptions.Companion.ALL_OFF |
| 11 | +import org.jetbrains.kotlinx.dataframe.io.Base64ImageEncodingOptions.Companion.GZIP_ON |
| 12 | +import org.jetbrains.kotlinx.dataframe.io.Base64ImageEncodingOptions.Companion.LIMIT_SIZE_ON |
| 13 | +import org.jetbrains.kotlinx.dataframe.parseJsonStr |
| 14 | +import org.jetbrains.kotlinx.dataframe.testResource |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.junit.runners.Parameterized |
| 18 | +import java.awt.image.BufferedImage |
| 19 | +import java.io.ByteArrayInputStream |
| 20 | +import java.io.ByteArrayOutputStream |
| 21 | +import java.io.File |
| 22 | +import java.util.* |
| 23 | +import java.util.zip.GZIPInputStream |
| 24 | +import javax.imageio.ImageIO |
| 25 | + |
| 26 | +@RunWith(Parameterized::class) |
| 27 | +class ImageSerializationTests(private val encodingOptions: Base64ImageEncodingOptions?) { |
| 28 | + @Test |
| 29 | + fun `serialize images as base64`() { |
| 30 | + val images = readImagesFromResources() |
| 31 | + val json = encodeImagesAsJson(images, encodingOptions) |
| 32 | + |
| 33 | + if (encodingOptions == DISABLED) { |
| 34 | + checkImagesEncodedAsStrings(json, images.size) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + val decodedImages = decodeImagesFromJson(json, images.size, encodingOptions!!) |
| 39 | + |
| 40 | + for ((decodedImage, original) in decodedImages.zip(images)) { |
| 41 | + val expectedImage = resizeIfNeeded(original, encodingOptions) |
| 42 | + isImagesIdentical(decodedImage, expectedImage, 2) shouldBe true |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private fun readImagesFromResources(): List<BufferedImage> { |
| 47 | + val dir = File(testResource("imgs").path) |
| 48 | + |
| 49 | + return dir.listFiles()?.map { file -> |
| 50 | + try { |
| 51 | + ImageIO.read(file) |
| 52 | + } catch (ex: Exception) { |
| 53 | + throw IllegalArgumentException("Error reading ${file.name}: ${ex.message}") |
| 54 | + } |
| 55 | + } ?: emptyList() |
| 56 | + } |
| 57 | + |
| 58 | + private fun encodeImagesAsJson( |
| 59 | + images: List<BufferedImage>, |
| 60 | + encodingOptions: Base64ImageEncodingOptions? |
| 61 | + ): JsonObject { |
| 62 | + val df = dataFrameOf(listOf("imgs"), images) |
| 63 | + val jsonStr = df.toJsonWithMetadata(20, nestedRowLimit = 20, imageEncodingOptions = encodingOptions) |
| 64 | + |
| 65 | + return parseJsonStr(jsonStr) |
| 66 | + } |
| 67 | + |
| 68 | + private fun checkImagesEncodedAsStrings(json: JsonObject, numImgs: Int) { |
| 69 | + for (i in 0..<numImgs) { |
| 70 | + val row = (json[KOTLIN_DATAFRAME] as JsonArray<*>)[i] as JsonObject |
| 71 | + val img = row["imgs"] as String |
| 72 | + |
| 73 | + img shouldContain "BufferedImage" |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private fun decodeImagesFromJson( |
| 78 | + json: JsonObject, |
| 79 | + imgsNum: Int, |
| 80 | + encodingOptions: Base64ImageEncodingOptions |
| 81 | + ): List<BufferedImage> { |
| 82 | + val result = mutableListOf<BufferedImage>() |
| 83 | + for (i in 0..<imgsNum) { |
| 84 | + val row = (json[KOTLIN_DATAFRAME] as JsonArray<*>)[i] as JsonObject |
| 85 | + val imgString = row["imgs"] as String |
| 86 | + |
| 87 | + val bytes = decodeBase64Image(imgString, encodingOptions) |
| 88 | + val decodedImage = createImageFromBytes(bytes) |
| 89 | + |
| 90 | + result.add(decodedImage) |
| 91 | + } |
| 92 | + |
| 93 | + return result |
| 94 | + } |
| 95 | + |
| 96 | + private fun decodeBase64Image(imgString: String, encodingOptions: Base64ImageEncodingOptions): ByteArray = |
| 97 | + when { |
| 98 | + encodingOptions.isGzipOn -> decompressGzip(Base64.getDecoder().decode(imgString)) |
| 99 | + else -> Base64.getDecoder().decode(imgString) |
| 100 | + } |
| 101 | + |
| 102 | + private fun decompressGzip(input: ByteArray): ByteArray { |
| 103 | + return ByteArrayOutputStream().use { byteArrayOutputStream -> |
| 104 | + GZIPInputStream(input.inputStream()).use { inputStream -> |
| 105 | + inputStream.copyTo(byteArrayOutputStream) |
| 106 | + } |
| 107 | + byteArrayOutputStream.toByteArray() |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private fun resizeIfNeeded(image: BufferedImage, encodingOptions: Base64ImageEncodingOptions): BufferedImage = |
| 112 | + when { |
| 113 | + !encodingOptions.isLimitSizeOn -> image |
| 114 | + else -> image.resizeKeepingAspectRatio(encodingOptions.imageSizeLimit) |
| 115 | + } |
| 116 | + |
| 117 | + private fun createImageFromBytes(bytes: ByteArray): BufferedImage { |
| 118 | + val bais = ByteArrayInputStream(bytes) |
| 119 | + return ImageIO.read(bais) |
| 120 | + } |
| 121 | + |
| 122 | + private fun isImagesIdentical(img1: BufferedImage, img2: BufferedImage, allowedDelta: Int): Boolean { |
| 123 | + // First check dimensions |
| 124 | + if (img1.width != img2.width || img1.height != img2.height) { |
| 125 | + return false |
| 126 | + } |
| 127 | + |
| 128 | + // Then check each pixel |
| 129 | + for (y in 0 until img1.height) { |
| 130 | + for (x in 0 until img1.width) { |
| 131 | + val rgb1 = img1.getRGB(x, y) |
| 132 | + val rgb2 = img2.getRGB(x, y) |
| 133 | + |
| 134 | + val r1 = (rgb1 shr 16) and 0xFF |
| 135 | + val g1 = (rgb1 shr 8) and 0xFF |
| 136 | + val b1 = rgb1 and 0xFF |
| 137 | + |
| 138 | + val r2 = (rgb2 shr 16) and 0xFF |
| 139 | + val g2 = (rgb2 shr 8) and 0xFF |
| 140 | + val b2 = rgb2 and 0xFF |
| 141 | + |
| 142 | + val diff = kotlin.math.abs(r1 - r2) + kotlin.math.abs(g1 - g2) + kotlin.math.abs(b1 - b2) |
| 143 | + |
| 144 | + // If the difference in color components exceed our allowance return false |
| 145 | + if (diff > allowedDelta) { |
| 146 | + return false |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // If no exceeding difference was found, the images are identical within our allowedDelta |
| 152 | + return true |
| 153 | + } |
| 154 | + |
| 155 | + companion object { |
| 156 | + private val DEFAULT = Base64ImageEncodingOptions() |
| 157 | + private val GZIP_ON_RESIZE_OFF = Base64ImageEncodingOptions(options = GZIP_ON) |
| 158 | + private val GZIP_OFF_RESIZE_OFF = Base64ImageEncodingOptions(options = ALL_OFF) |
| 159 | + private val GZIP_ON_RESIZE_TO_700 = Base64ImageEncodingOptions(imageSizeLimit = 700, options = GZIP_ON or LIMIT_SIZE_ON) |
| 160 | + private val DISABLED = null |
| 161 | + |
| 162 | + @JvmStatic |
| 163 | + @Parameterized.Parameters |
| 164 | + fun imageEncodingOptionsToTest(): Collection<Base64ImageEncodingOptions?> { |
| 165 | + return listOf( |
| 166 | + DEFAULT, |
| 167 | + GZIP_ON_RESIZE_OFF, |
| 168 | + GZIP_OFF_RESIZE_OFF, |
| 169 | + GZIP_ON_RESIZE_TO_700, |
| 170 | + null, |
| 171 | + ) |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments