fix: Only use supported PixelFormats

This commit is contained in:
Marc Rousavy 2023-10-03 14:05:23 +02:00
parent 224bffd4dc
commit 9a777ba240

View File

@ -3,7 +3,6 @@ package com.mrousavy.camera.parsers
import android.graphics.ImageFormat import android.graphics.ImageFormat
import com.mrousavy.camera.PixelFormatNotSupportedError import com.mrousavy.camera.PixelFormatNotSupportedError
@Suppress("FoldInitializerAndIfToElvis")
enum class PixelFormat(override val unionValue: String) : JSUnionValue { enum class PixelFormat(override val unionValue: String) : JSUnionValue {
YUV("yuv"), YUV("yuv"),
RGB("rgb"), RGB("rgb"),
@ -11,29 +10,24 @@ enum class PixelFormat(override val unionValue: String) : JSUnionValue {
UNKNOWN("unknown"); UNKNOWN("unknown");
fun toImageFormat(): Int { fun toImageFormat(): Int {
val result = when (this) { return when (this) {
YUV -> ImageFormat.YUV_420_888 YUV -> ImageFormat.YUV_420_888
RGB -> android.graphics.PixelFormat.RGBA_8888
NATIVE -> ImageFormat.PRIVATE NATIVE -> ImageFormat.PRIVATE
UNKNOWN -> null else -> throw PixelFormatNotSupportedError(this.unionValue)
} }
if (result == null) {
throw PixelFormatNotSupportedError(this.unionValue)
}
return result
} }
companion object : JSUnionValue.Companion<PixelFormat> { companion object : JSUnionValue.Companion<PixelFormat> {
fun fromImageFormat(imageFormat: Int): PixelFormat = fun fromImageFormat(imageFormat: Int): PixelFormat {
when (imageFormat) { return when (imageFormat) {
ImageFormat.YUV_420_888 -> YUV ImageFormat.YUV_420_888 -> YUV
android.graphics.PixelFormat.RGBA_8888 -> RGB
ImageFormat.PRIVATE -> NATIVE ImageFormat.PRIVATE -> NATIVE
else -> UNKNOWN else -> UNKNOWN
} }
}
override fun fromUnionValue(unionValue: String?): PixelFormat? = override fun fromUnionValue(unionValue: String?): PixelFormat? {
when (unionValue) { return when (unionValue) {
"yuv" -> YUV "yuv" -> YUV
"rgb" -> RGB "rgb" -> RGB
"native" -> NATIVE "native" -> NATIVE
@ -41,4 +35,5 @@ enum class PixelFormat(override val unionValue: String) : JSUnionValue {
else -> null else -> null
} }
} }
}
} }