From 8b7ddd49ebc18ab283f2f6d89e93c94bcc539cdb Mon Sep 17 00:00:00 2001 From: Stefan Oltmann Date: Mon, 29 Jan 2024 11:36:21 +0100 Subject: [PATCH] TiffField: toInt(), toShort() & toDouble() cover more cases (#69) --- README.md | 2 +- .../com/ashampoo/kim/format/tiff/TiffField.kt | 44 ++++++++++--------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 9443aafa..67d7220e 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ of Ashampoo Photos, which, in turn, is driven by user community feedback. ## Installation ``` -implementation("com.ashampoo:kim:0.12") +implementation("com.ashampoo:kim:0.12.1") ``` ## Sample usages diff --git a/src/commonMain/kotlin/com/ashampoo/kim/format/tiff/TiffField.kt b/src/commonMain/kotlin/com/ashampoo/kim/format/tiff/TiffField.kt index c95a6ff6..d13db6c6 100644 --- a/src/commonMain/kotlin/com/ashampoo/kim/format/tiff/TiffField.kt +++ b/src/commonMain/kotlin/com/ashampoo/kim/format/tiff/TiffField.kt @@ -166,27 +166,29 @@ class TiffField( throw ImageReadException("Can't format value of tag $tagFormatted as int: $value") } - fun toInt(): Int = - if (value is IntArray) - value.first() - else if (value is ShortArray) - (value.first() as Number).toInt() - else - (value as Number).toInt() - - fun toShort(): Short = - if (value is ShortArray) - value.first() - else - (value as Number).toShort() - - fun toDouble(): Double = - if (value is RationalNumbers) - value.values.first().doubleValue() - else if (value is RationalNumber) - value.doubleValue() - else - (value as Number).toDouble() + fun toInt(): Int = when (value) { + is ByteArray -> value.first().toInt() + is ShortArray -> value.first().toInt() + is IntArray -> value.first().toInt() + else -> (value as Number).toInt() + } + + fun toShort(): Short = when (value) { + is ByteArray -> value.first().toShort() + is ShortArray -> value.first() + is IntArray -> value.first().toShort() + else -> (value as Number).toShort() + } + + fun toDouble(): Double = when (value) { + is RationalNumbers -> value.values.first().doubleValue() + is RationalNumber -> value.doubleValue() + is ByteArray -> value.first().toDouble() + is ShortArray -> value.first().toDouble() + is IntArray -> value.first().toDouble() + is FloatArray -> value.first().toDouble() + else -> (value as Number).toDouble() + } /* * Note that we need to show the local 'tagFormatted', because