Skip to content

Commit

Permalink
Dependency updates (#42)
Browse files Browse the repository at this point in the history
Also reduced Ktor dependencies a bit.
  • Loading branch information
StefanOltmann authored Dec 10, 2023
1 parent e73b7a4 commit cf686df
Show file tree
Hide file tree
Showing 30 changed files with 169 additions and 133 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ of Ashampoo Photos, which, in turn, is driven by user community feedback.
## Installation

```
implementation("com.ashampoo:kim:0.7.4")
implementation("com.ashampoo:kim:0.7.5")
```

## Sample usages
Expand Down
25 changes: 8 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ repositories {

val productName = "Ashampoo Kim"

val ktorVersion: String = "2.3.6"
val xmpCoreVersion: String = "0.2.2"
val dateTimeVersion: String = "0.4.1"
val ktorVersion: String = "2.3.7"
val xmpCoreVersion: String = "0.2.3"
val dateTimeVersion: String = "0.5.0"
val testRessourcesVersion: String = "0.4.0"
val ioCoreVersion: String = "0.3.0"

Expand Down Expand Up @@ -84,25 +84,16 @@ sonar {
)
)

/* Include Android Lint */
property("sonar.android.lint.report", "${project.buildDir}/reports/lint-results.xml")

/* Include Detekt issues */
val detektPath = "${project.buildDir}/reports/detekt/detekt.xml"
println("Detekt report: $detektPath")
property("sonar.kotlin.detekt.reportPaths", detektPath)

/* Include Kover code coverage */
val koverPath = "${project.buildDir}/reports/kover/xml/report.xml"
println("Kover report: $koverPath")
property("sonar.coverage.jacoco.xmlReportPaths", koverPath)
property("sonar.android.lint.report", "build/reports/lint-results.xml")
property("sonar.kotlin.detekt.reportPaths", "build/reports/detekt/detekt.xml")
property("sonar.coverage.jacoco.xmlReportPaths", "build/reports/kover/xml/report.xml")
}
}

detekt {
source = files("src", "build.gradle.kts")
source.from("src", "build.gradle.kts")
allRules = true
config = files("detekt.yml")
config.setFrom("$projectDir/detekt.yml")
parallel = true
ignoreFailures = true
autoCorrect = true
Expand Down
1 change: 1 addition & 0 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ style:
- '0'
- '1'
- '2'
- '0xFF'
ignoreHashCodeFunction: true
ignorePropertyDeclaration: false
ignoreLocalVariableDeclaration: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.ashampoo.kim.input

import com.ashampoo.kim.common.slice
import java.io.InputStream

open class AndroidInputStreamByteReader(
Expand Down Expand Up @@ -46,7 +47,7 @@ open class AndroidInputStreamByteReader(
return if (bytes == count)
buffer
else
buffer.sliceArray(0..count)
buffer.slice(startIndex = 0, count = count)
}

override fun close() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.ashampoo.kim.common

import io.ktor.utils.io.charsets.Charsets

private const val FF = 0xFF

const val HEX_RADIX = 16
Expand All @@ -39,8 +41,11 @@ fun ByteArray.toSingleNumberHexes(): String =
joinToString(", ") { "0x" + it.toHex() }

@Suppress("MagicNumber")
fun ByteArray.toAsciiString(): String =
this.decodeToString()
fun ByteArray.decodeToIso8859String(): String =
io.ktor.utils.io.core.String(
bytes = this,
charset = Charsets.ISO_8859_1
)

fun ByteArray.indexOfNullTerminator(): Int =
indexOfNullTerminator(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ object PhotoValueFormatter {
*/
lensName.replaceFirst(cameraName, "").trim()

} else lensName
} else {
lensName
}
}

fun createCameraAndLensName(
Expand All @@ -237,7 +239,9 @@ object PhotoValueFormatter {

"$cameraName | $modLensName"

} else cameraName ?: lensName
} else {
cameraName ?: lensName
}
}

fun formatIso(iso: Int): String = "ISO $iso"
Expand Down
14 changes: 7 additions & 7 deletions src/commonMain/kotlin/com/ashampoo/kim/common/RationalNumber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ class RationalNumber {
*/
if (isUnsignedType && negatedNumerator < 0) {

if (commonDivisor != 0L) {

val reducedNumerator = numerator / commonDivisor
val reducedDivisor = divisor / commonDivisor
return RationalNumber(-reducedNumerator, reducedDivisor, false)

} else
if (commonDivisor == 0L)
throw NumberFormatException("Unsigned numerator is too large to negate: $numerator")

return RationalNumber(
numerator = -(numerator / commonDivisor),
divisor = divisor / commonDivisor,
unsignedType = false
)
}

return RationalNumber(negatedNumerator, divisor, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.ashampoo.kim.format.jpeg.iptc

import com.ashampoo.kim.common.ByteOrder
import com.ashampoo.kim.common.ImageReadException
import com.ashampoo.kim.common.decodeToIso8859String
import com.ashampoo.kim.common.slice
import com.ashampoo.kim.common.startsWith
import com.ashampoo.kim.common.toInt
Expand All @@ -26,9 +27,6 @@ import com.ashampoo.kim.common.toUInt8
import com.ashampoo.kim.format.jpeg.JpegConstants
import com.ashampoo.kim.format.jpeg.iptc.IptcTypes.Companion.getIptcType
import com.ashampoo.kim.input.ByteArrayByteReader
import io.ktor.utils.io.charsets.Charset
import io.ktor.utils.io.charsets.Charsets
import io.ktor.utils.io.core.String

object IptcParser {

Expand All @@ -43,8 +41,6 @@ object IptcParser {
@Suppress("MagicNumber")
private val PHOTOSHOP_IGNORED_BLOCK_TYPE = listOf(1084, 1085, 1086, 1087)

val DEFAULT_CHARSET = Charsets.ISO_8859_1

const val CODED_CHARACTER_SET_IPTC_CODE = 90

/* "ESC % G" as bytes */
Expand Down Expand Up @@ -97,7 +93,7 @@ object IptcParser {

private fun parseIPTCBlock(bytes: ByteArray): List<IptcRecord> {

var charset = DEFAULT_CHARSET
var isUtf8 = false

val records = mutableListOf<IptcRecord>()

Expand Down Expand Up @@ -131,7 +127,7 @@ object IptcParser {
if (recordNumber == IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER &&
recordType == CODED_CHARACTER_SET_IPTC_CODE
) {
charset = findCharset(recordData)
isUtf8 = isUtf8(recordData)
continue
}

Expand All @@ -144,7 +140,10 @@ object IptcParser {
records.add(
IptcRecord(
iptcType = getIptcType(recordType),
value = String(recordData, charset = charset)
value = if (isUtf8)
recordData.decodeToString()
else
recordData.decodeToIso8859String()
)
)
}
Expand Down Expand Up @@ -260,7 +259,7 @@ object IptcParser {
return blocks
}

private fun findCharset(codedCharset: ByteArray): Charset {
private fun isUtf8(codedCharset: ByteArray): Boolean {

/*
* check if encoding is a escape sequence
Expand All @@ -273,8 +272,6 @@ object IptcParser {
if (element != ' '.code.toByte())
codedCharsetNormalized[index++] = element

val utf8 = UTF8_CHARACTER_ESCAPE_SEQUENCE.contentEquals(codedCharsetNormalized)

return if (utf8) Charsets.UTF_8 else DEFAULT_CHARSET
return UTF8_CHARACTER_ESCAPE_SEQUENCE.contentEquals(codedCharsetNormalized)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.ashampoo.kim.format.jpeg.iptc.IptcParser.APP13_BYTE_ORDER
import com.ashampoo.kim.output.BigEndianBinaryByteWriter
import com.ashampoo.kim.output.BinaryByteWriter
import com.ashampoo.kim.output.ByteArrayByteWriter
import io.ktor.utils.io.core.toByteArray

object IptcWriter {

Expand Down Expand Up @@ -120,7 +119,7 @@ object IptcWriter {

binaryWriter.write(iptcType.type)

val recordData = value.toByteArray()
val recordData = value.encodeToByteArray()

binaryWriter.write2Bytes(recordData.size)
binaryWriter.write(recordData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package com.ashampoo.kim.format.jpeg.xmp

import com.ashampoo.kim.common.ImageReadException
import com.ashampoo.kim.common.slice
import com.ashampoo.kim.common.startsWith
import com.ashampoo.kim.format.jpeg.JpegConstants
import io.ktor.utils.io.charsets.Charsets
import io.ktor.utils.io.core.String

object JpegXmpParser {

Expand All @@ -35,6 +34,9 @@ object JpegXmpParser {
val index = JpegConstants.XMP_IDENTIFIER.size

/* The data is UTF-8 encoded XML */
return String(segmentData, index, segmentData.size - index, Charsets.UTF_8)
return segmentData.slice(
startIndex = index,
count = segmentData.size - index
).decodeToString()
}
}
19 changes: 9 additions & 10 deletions src/commonMain/kotlin/com/ashampoo/kim/format/png/ChunkType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package com.ashampoo.kim.format.png

import io.ktor.utils.io.core.toByteArray

/**
* Type of a PNG chunk.
*
Expand Down Expand Up @@ -46,29 +44,30 @@ data class ChunkType internal constructor(
companion object {

/** Image header */
val IHDR = of("IHDR".toByteArray())
val IHDR = of("IHDR".encodeToByteArray())

/** Image data */
val IDAT = of("IDAT".toByteArray())
val IDAT = of("IDAT".encodeToByteArray())

/** Image end */
val IEND = of("IEND".toByteArray())
val IEND = of("IEND".encodeToByteArray())

/** Time */
val TIME = of("tIME".toByteArray())
val TIME = of("tIME".encodeToByteArray())

/** Text */
val TEXT = of("tEXt".toByteArray())
val TEXT = of("tEXt".encodeToByteArray())

/** Compressed text */
val ZTXT = of("zTXt".toByteArray())
val ZTXT = of("zTXt".encodeToByteArray())

/** UTF-8 text, for example XMP */
val ITXT = of("iTXt".toByteArray())
val ITXT = of("iTXt".encodeToByteArray())

/** EXIF (since 2017) */
val EXIF = of("eXIf".toByteArray())
val EXIF = of("eXIf".encodeToByteArray())

@Suppress("MagicNumber")
fun of(typeBytes: ByteArray): ChunkType {

require(typeBytes.size == PngConstants.TPYE_LENGTH) { "ChunkType must be always 4 bytes!" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.ashampoo.kim.format.png.chunks.PngTextChunk
import com.ashampoo.kim.input.ByteReader
import com.ashampoo.kim.output.ByteArrayByteWriter
import com.ashampoo.kim.output.ByteWriter
import io.ktor.utils.io.core.toByteArray

object PngWriter {

Expand Down Expand Up @@ -166,11 +165,11 @@ object PngWriter {
writer.write(0) // No language tag

/* XMP keyword - null-terminated */
writer.write(PngConstants.XMP_KEYWORD.toByteArray())
writer.write(PngConstants.XMP_KEYWORD.encodeToByteArray())
writer.write(0)

/* XMP bytes */
writer.write(xmpXml.toByteArray())
writer.write(xmpXml.encodeToByteArray())

writeChunk(byteWriter, ChunkType.ITXT, writer.toByteArray())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package com.ashampoo.kim.format.png.chunks

import com.ashampoo.kim.common.ImageReadException
import com.ashampoo.kim.common.decodeToIso8859String
import com.ashampoo.kim.common.decompress
import com.ashampoo.kim.common.indexOfNullTerminator
import com.ashampoo.kim.common.slice
import com.ashampoo.kim.format.png.ChunkType
import com.ashampoo.kim.format.png.PngConstants
import io.ktor.utils.io.charsets.Charsets
import io.ktor.utils.io.core.String

class PngChunkItxt(
length: Int,
Expand All @@ -48,7 +48,10 @@ class PngChunkItxt(
if (terminatorIndex < 0)
throw ImageReadException("PNG iTXt chunk keyword is not terminated.")

keyword = String(bytes, 0, terminatorIndex, Charsets.ISO_8859_1)
keyword = bytes.slice(
startIndex = 0,
count = terminatorIndex
).decodeToIso8859String()

var index = terminatorIndex + 1

Expand All @@ -69,7 +72,10 @@ class PngChunkItxt(
if (terminatorIndex < 0)
throw ImageReadException("PNG iTXt chunk language tag is not terminated.")

languageTag = String(bytes, index, terminatorIndex - index, Charsets.ISO_8859_1)
languageTag = bytes.copyOfRange(
fromIndex = index,
toIndex = terminatorIndex
).decodeToIso8859String()

index = terminatorIndex + 1

Expand All @@ -78,14 +84,22 @@ class PngChunkItxt(
if (terminatorIndex < 0)
throw ImageReadException("PNG iTXt chunk translated keyword is not terminated.")

translatedKeyword = String(bytes, index, terminatorIndex - index, Charsets.UTF_8)
translatedKeyword = bytes.copyOfRange(
fromIndex = index,
toIndex = terminatorIndex
).decodeToString()

index = terminatorIndex + 1

val subBytes = bytes.copyOfRange(
fromIndex = index,
toIndex = bytes.size
)

text = if (compressed)
decompress(bytes.copyOfRange(index, bytes.size))
decompress(subBytes)
else
String(bytes, index, bytes.size - index, Charsets.UTF_8)
subBytes.decodeToString()
}

/**
Expand Down
Loading

0 comments on commit cf686df

Please sign in to comment.