All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.alchemist.boundary.webui.common.renderer.Bitmap32Serializer.kt Maven / Gradle / Ivy

Go to download

Alchemist module that starts a web server and allows to visualize an environment on a web browser.

There is a newer version: 34.1.4
Show newest version
/*
 * Copyright (C) 2010-2023, Danilo Pianini and contributors
 * listed, for each module, in the respective subproject's build.gradle.kts file.
 *
 * This file is part of Alchemist, and is distributed under the terms of the
 * GNU General Public License, with a linking exception,
 * as described in the file LICENSE in the Alchemist distribution's top directory.
 */

package it.unibo.alchemist.boundary.webui.common.renderer

import korlibs.image.bitmap.Bitmap32
import korlibs.image.color.RgbaArray
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.IntArraySerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.decodeStructure
import kotlinx.serialization.encoding.encodeStructure

/**
 * Serializer for [Bitmap32] class.
 */
object Bitmap32Serializer : KSerializer {
    override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Bitmap32") {
        element("ints")
        element("width")
        element("height")
    }

    override fun serialize(encoder: Encoder, value: Bitmap32) =
        encoder.encodeStructure(descriptor) {
            encodeSerializableElement(descriptor, 0, IntArraySerializer(), value.ints)
            encodeIntElement(descriptor, 1, value.width)
            encodeIntElement(descriptor, 2, value.height)
        }

    override fun deserialize(decoder: Decoder): Bitmap32 =
        decoder.decodeStructure(descriptor) {
            var ints = intArrayOf()
            var width = -1
            var height = -1
            while (true) {
                when (val index = decodeElementIndex(descriptor)) {
                    0 -> ints = decodeSerializableElement(descriptor, 0, IntArraySerializer())
                    1 -> width = decodeIntElement(descriptor, 1)
                    2 -> height = decodeIntElement(descriptor, 2)
                    CompositeDecoder.DECODE_DONE -> break
                    else -> error("Unexpected index: $index")
                }
            }
            Bitmap32(width, height, RgbaArray(ints))
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy