Skip to content

Commit

Permalink
Kotlin update + Refactorings (#50)
Browse files Browse the repository at this point in the history
Update to Kotlin 1.9.22 and some refactorings
  • Loading branch information
StefanOltmann authored Dec 22, 2023
1 parent 35c988d commit b3dac7e
Show file tree
Hide file tree
Showing 174 changed files with 9,202 additions and 8,260 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Deploy to GitHub Pages

on:
push:
branches: [ "main" ]
tags:
- '*'

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Kim - Kotlin Image Metadata

[![Kotlin](https://img.shields.io/badge/kotlin-1.9.21-blue.svg?logo=kotlin)](httpw://kotlinlang.org)
[![Kotlin](https://img.shields.io/badge/kotlin-1.9.22-blue.svg?logo=kotlin)](httpw://kotlinlang.org)
![JVM](https://img.shields.io/badge/-JVM-gray.svg?style=flat)
![Android](https://img.shields.io/badge/-Android-gray.svg?style=flat)
![macOS](https://img.shields.io/badge/-macOS-gray.svg?style=flat)
Expand Down Expand Up @@ -33,7 +33,7 @@ of Ashampoo Photos, which, in turn, is driven by user community feedback.
## Installation

```
implementation("com.ashampoo:kim:0.8.2")
implementation("com.ashampoo:kim:0.8.3")
```

## Sample usages
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform") version "1.9.21"
kotlin("multiplatform") version "1.9.22"
id("com.android.library") version "8.0.2"
id("maven-publish")
id("signing")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,30 @@ object JpegOrientationOffsetFinder {
/* We are only looking for the EXIF segment. */
if (segmentType != APP1_MARKER) {

byteReader.skipBytes("skip segment", segmentLength.toLong())
byteReader.skipBytes("skip segment", segmentLength)

positionCounter += segmentLength

continue
}

val exifIdentifierBytes = byteReader.readBytes("EXIF identifier", 6)
val exifIdentifierBytes = byteReader.readBytes(
"EXIF identifier",
JpegConstants.EXIF_IDENTIFIER_CODE.size
)

positionCounter += 6
positionCounter += JpegConstants.EXIF_IDENTIFIER_CODE.size

/* Skip the APP1 XMP segment. */
if (!exifIdentifierBytes.contentEquals(JpegConstants.EXIF_IDENTIFIER_CODE)) {
byteReader.skipBytes("skip segment", segmentLength.toLong() - 6)
positionCounter += segmentLength - 6

byteReader.skipBytes(
"skip segment",
segmentLength - JpegConstants.EXIF_IDENTIFIER_CODE.size
)

positionCounter += segmentLength - JpegConstants.EXIF_IDENTIFIER_CODE.size

continue
}

Expand Down Expand Up @@ -142,7 +151,7 @@ object JpegOrientationOffsetFinder {

} else {

byteReader.skipBytes("skip TIFF entry", TIFF_ENTRY_LENGTH - 2L)
byteReader.skipBytes("skip TIFF entry", TIFF_ENTRY_LENGTH - 2)

positionCounter += TIFF_ENTRY_LENGTH
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object JpegSegmentAnalyzer {
)
)

var positionCounter: Long = ImageFormatMagicNumbers.jpeg.size.toLong()
var positionCounter: Int = ImageFormatMagicNumbers.jpeg.size

@Suppress("LoopWithTooManyJumpStatements")
do {
Expand Down Expand Up @@ -98,9 +98,9 @@ object JpegSegmentAnalyzer {
)
)

byteReader.skipBytes("image bytes", remainingBytesCount - 2)
byteReader.skipBytes("image bytes", (remainingBytesCount - 2).toInt())

positionCounter += remainingBytesCount
positionCounter += remainingBytesCount.toInt()

val eoiMarker = byteReader.read2BytesAsInt("EOI", JPEG_BYTE_ORDER)

Expand Down Expand Up @@ -137,7 +137,7 @@ object JpegSegmentAnalyzer {
if (remainingSegmentLength <= 0)
throw ImageReadException("Illegal JPEG segment length: $remainingSegmentLength")

byteReader.skipBytes("skip segment", remainingSegmentLength.toLong())
byteReader.skipBytes("skip segment", remainingSegmentLength)

positionCounter += remainingSegmentLength

Expand All @@ -147,7 +147,7 @@ object JpegSegmentAnalyzer {
}

data class JpegSegmentInfo(
val offset: Long,
val offset: Int,
val marker: Int,
val length: Int
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JfifSegment(marker: Int, markerLength: Int, byteReader: ByteReader) : Segm
thumbnailSize = xThumbnail * yThumbnail

if (thumbnailSize > 0)
byteReader.skipBytes("Skip thumbnail", thumbnailSize.toLong())
byteReader.skipBytes("Skip thumbnail", thumbnailSize)
}

constructor(marker: Int, segmentBytes: ByteArray) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ object PngImageParser : ImageParser {

val exifChunk = chunks.find { it.chunkType == ChunkType.EXIF } ?: return null

return exifChunk.bytes to TiffReader.read(ByteArrayByteReader(exifChunk.bytes))
return exifChunk.bytes to TiffReader.read(exifChunk.bytes)
}

/*
Expand Down Expand Up @@ -158,7 +158,7 @@ object PngImageParser : ImageParser {
* This should be fine now to be fed into the TIFF reader.
*/
return exifBytesWithoutIdentifier to
TiffReader.read(ByteArrayByteReader(exifBytesWithoutIdentifier))
TiffReader.read(exifBytesWithoutIdentifier)
}

private fun getIptcFromTextChunk(chunks: List<PngChunk>): IptcMetadata? {
Expand Down Expand Up @@ -257,7 +257,7 @@ object PngImageParser : ImageParser {
if (keep)
bytes = byteReader.readBytes("chunk data", length)
else
byteReader.skipBytes("chunk data", length.toLong())
byteReader.skipBytes("chunk data", length)

val crc = byteReader.read4BytesAsInt("crc", pngByteOrder)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.ashampoo.kim.format.tiff

open class ImageDataElement(
offset: Long,
offset: Int,
length: Int
) : TiffElement(
debugDescription = "ImageDataElement offset=$offset, length=$length",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.ashampoo.kim.format.tiff

class JpegImageDataElement(
offset: Long,
offset: Int,
length: Int,
val bytes: ByteArray
) : ImageDataElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ import com.ashampoo.kim.model.TiffOrientation
class TiffDirectory(
val type: Int,
val entries: List<TiffField>,
offset: Long,
val nextDirectoryOffset: Long,
offset: Int,
val nextDirectoryOffset: Int,
val byteOrder: ByteOrder
) : TiffElement(
debugDescription = "Directory " + description(type),
debugDescription = "Directory " + description(type) + " @ $offset",
offset = offset,
length = TiffConstants.TIFF_DIRECTORY_HEADER_LENGTH + entries.size *
TiffConstants.TIFF_ENTRY_LENGTH + TiffConstants.TIFF_DIRECTORY_FOOTER_LENGTH
Expand Down Expand Up @@ -99,7 +99,7 @@ class TiffDirectory(
if (!tag.dataTypes.contains(field.fieldType))
throw ImageReadException("Required field ${tag.name} has incorrect type ${field.fieldType.name}")

if (field.count != 1L)
if (field.count != 1)
throw ImageReadException("Field ${tag.name} has wrong count ${field.count}")

return tag.getValue(field.byteOrder, field.byteArrayValue)
Expand All @@ -123,9 +123,11 @@ class TiffDirectory(
val jpegInterchangeFormatLength = findField(TiffTag.TIFF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH)

if (jpegInterchangeFormat != null && jpegInterchangeFormatLength != null) {
val offSet = jpegInterchangeFormat.toIntArray()[0]

val offset = jpegInterchangeFormat.toIntArray()[0]
val byteCount = jpegInterchangeFormatLength.toIntArray()[0]
return ImageDataElement(offSet.toLong(), byteCount)

return ImageDataElement(offset, byteCount)
}

throw ImageReadException("Couldn't find image data.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.ashampoo.kim.format.tiff

open class TiffElement(
val debugDescription: String,
val offset: Long,
val offset: Int,
val length: Int
) {

Expand Down
26 changes: 18 additions & 8 deletions src/commonMain/kotlin/com/ashampoo/kim/format/tiff/TiffField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,34 @@ import com.ashampoo.kim.format.tiff.taginfos.TagInfo
* A TIFF field in a TIFF directory.
*/
class TiffField(
/** Offset relative to TIFF header */
val offset: Int,
val tag: Int,
val directoryType: Int,
val fieldType: FieldType,
val count: Long,
val offset: Long,
val count: Int,
/** Set if field has a local value. */
val localValue: Int?,
/** Set if field has a offset pointer to its value. */
val valueOffset: Int?,
val valueBytes: ByteArray,
val byteOrder: ByteOrder,
val sortHint: Int
) {

/**
* Returns the offset with padding.
* Because TIFF files can be as big as 4 GB we need 10 digits to present that.
*/
val offsetFormatted: String =
offset.toString().padStart(10, '0')

/** Return a proper Tag ID like 0x0100 */
val tagFormatted: String =
"0x" + tag.toString(HEX_RADIX).padStart(4, '0')

val tagInfo: TagInfo = getTag(directoryType, tag)

val isLocalValue: Boolean = count * fieldType.size <= TiffConstants.TIFF_ENTRY_MAX_VALUE_LENGTH

val bytesLength: Int = count.toInt() * fieldType.size

val byteArrayValue: ByteArray = valueBytes.head(bytesLength)
Expand Down Expand Up @@ -152,14 +162,14 @@ class TiffField(
* 'tagInfo' might be an Unknown tag and show a placeholder.
*/
override fun toString(): String =
"$tagFormatted ${tagInfo.name} = $valueDescription"
"$offsetFormatted $tagFormatted ${tagInfo.name} = $valueDescription"

fun createOversizeValueElement(): TiffElement? =
if (isLocalValue) null else OversizeValueElement(offset.toInt(), valueBytes.size)
valueOffset?.let { OversizeValueElement(it.toInt(), valueBytes.size) }

inner class OversizeValueElement(offset: Int, length: Int) : TiffElement(
debugDescription = "Value of $tagInfo ($fieldType)",
offset = offset.toLong(),
debugDescription = "Value of $tagInfo ($fieldType) @ $offset",
offset = offset,
length = length
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.ashampoo.kim.format.tiff.constants.TiffConstants
data class TiffHeader(
val byteOrder: ByteOrder,
val tiffVersion: Int,
val offsetToFirstIFD: Long
val offsetToFirstIFD: Int
) : TiffElement(
debugDescription = "Version $tiffVersion\n$byteOrder",
offset = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ object TiffImageParser : ImageParser {
override fun parseMetadata(byteReader: ByteReader): ImageMetadata =
tryWithImageReadException {

/**
* TIFF files, such as CR2 RAW files, can be extremely large.
* It is not advisable to load them entirely into a ByteArray.
*/
val randomAccessByteReader = DefaultRandomAccessByteReader(byteReader)

val exif = TiffReader.read(randomAccessByteReader)
Expand Down
Loading

0 comments on commit b3dac7e

Please sign in to comment.