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

commonMain.net.iriscan.sdk.io.image.impl.InputOutputImageOperationsImpl.kt Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
package net.iriscan.sdk.io.image.impl

import net.iriscan.sdk.core.image.Image
import net.iriscan.sdk.core.image.NativeImage
import net.iriscan.sdk.core.image.internalReadNativeImage
import net.iriscan.sdk.core.image.internalWriteNativeImage
import net.iriscan.sdk.core.io.DataBytes
import net.iriscan.sdk.io.InputOutputImageOperations
import net.iriscan.sdk.io.exception.UnknownFormatException
import net.iriscan.sdk.io.image.ImageFormat
import net.iriscan.sdk.io.image.serializers.BMP
import net.iriscan.sdk.io.image.serializers.PNG

/**
 * @author Slava Gornostal
 */
internal class InputOutputImageOperationsImpl : InputOutputImageOperations {

    private val serializers = listOf(BMP, PNG)

    override fun readImage(data: ByteArray): Image {
        val serializer = serializers.firstOrNull { it.canRead(data) }
            ?: throw UnknownFormatException("Unknown image format")
        return serializer.read(data)
    }

    override fun readNativeImage(data: DataBytes): NativeImage =
        internalReadNativeImage(data)

    override fun writeImage(image: Image, format: ImageFormat): ByteArray {
        val serializer = serializers.firstOrNull { it.format == format }
            ?: throw UnknownFormatException("Unsupported image format")
        return serializer.write(image)
    }

    override fun writeNativeImage(image: NativeImage, format: ImageFormat): DataBytes =
        internalWriteNativeImage(image, format)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy