Skip to content

Commit

Permalink
TiffField: toInt(), toShort() & toDouble() cover more cases (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanOltmann authored Jan 29, 2024
1 parent dfd07ab commit 8b7ddd4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 23 additions & 21 deletions src/commonMain/kotlin/com/ashampoo/kim/format/tiff/TiffField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8b7ddd4

Please sign in to comment.