feat(phase-1/m4.4e): real JVM EXIF orientation normalizer

The §6.3 EXIF row deferred implementation to "M4+ when the JVM
attachment intake lands" — it landed in M4.4c, so the last NoOp
placeholder is gone. JvmImageOrientationNormalizer keeps the Android
contract exactly: orientation 6/3/8 -> bake 90/180/270 into pixels,
re-encode JPEG @ 0.92, return input UNCHANGED for normal/absent/mirror
tags or any decode/rotate failure.

Tooling differs, contract doesn't: com.drewnoakes:metadata-extractor
2.19.0 (first Central pull this phase — recorded) for the tag read,
BufferedImage + AffineTransform for the rotate, ImageIO JPEG writer at
explicit 0.92 for the encode. DesktopContainer now wires it into
NoteRepositoryImpl (was ImageOrientationNormalizer.NoOp).

New probe: :core:data-jvm:orientationSmoke hand-builds an
EXIF-Orientation-90 JPEG (APP1 splice — metadata-extractor is
read-only) and asserts: no-EXIF passthrough, EXIF-90 rewrite, pixels
rotated (4x2 -> 2x4), output stability, garbage passthrough.

Verified: :core:data-jvm:orientationSmoke (RESULT: M4.4e PASS, 5/5)
+ ./gradlew assembleDebug :desktopApp:build SUCCESSFUL
+ :desktopApp:run --self-test (SELFTEST id=1 -> RENDER notes=1
selected=1); objectbox-models clean.
This commit is contained in:
Incido Developer committed 2026-10-06 19:04:09 +13:00
1 parent 018db4819c
commit 0dc5935d1d
5 files changed
+207 -2

No files matched your search

+18
View File
@@ -68,6 +68,12 @@ dependencies {
implementation(project(":core:backup"))
implementation(libs.kotlinx.serialization.json)
// Phase 1 (M4.4e): EXIF orientation for the JVM normalizer — the
// desktop counterpart of androidx.exifinterface that :core:data's
// Android side uses (§6.3 EXIF row: "not cached, Maven Central
// reachable" — pulled once here).
implementation(libs.metadata.extractor)
// JVM counterparts of the objectbox-android the Android build uses.
implementation(libs.objectbox.java)
implementation(libs.objectbox.kotlin)
@@ -95,6 +101,18 @@ tasks.register<JavaExec>("smoke") {
doFirst { smokeDir.deleteRecursively() }
}
/**
* `./gradlew :core:data-jvm:orientationSmoke` — Phase 1 (M4.4e): feeds a
* hand-crafted EXIF-Orientation-90 JPEG through JvmImageOrientationNormalizer
* and checks the rotation is baked into the pixels (Android contract kept).
*/
tasks.register<JavaExec>("orientationSmoke") {
group = "verification"
description = "Proves EXIF orientation handling on the JVM."
classpath = sourceSets["main"].runtimeClasspath
mainClass.set("org.incido.data.jvm.OrientationSmokeKt")
}
/**
* `./gradlew :core:data-jvm:backupSmoke --args="<backup.incido.json> [libDir]"`
* — feeds a REAL Android backup file through the :core:backup bridge on the
@@ -0,0 +1,95 @@
package org.incido.data.jvm
import com.drew.imaging.ImageMetadataReader
import com.drew.metadata.exif.ExifIFD0Directory
import java.awt.geom.AffineTransform
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import javax.imageio.IIOImage
import javax.imageio.ImageIO
import javax.imageio.ImageWriteParam
import kotlin.math.abs
import kotlin.math.roundToInt
import kotlin.math.sin
import org.incido.data.ImageOrientationNormalizer
/**
* Phase 1 (M4.4e) — JVM [ImageOrientationNormalizer], replacing the
* NoOp placeholder the §6.3 EXIF row deferred ("metadata-extractor not
* cached, Maven Central reachable — feasible in JVM impl").
*
* Same contract as `AndroidImageOrientationNormalizer`: read the EXIF
* Orientation tag, bake 90/180/270 rotations into the pixels, re-encode
* JPEG @ 0.92 so the stored bytes carry no orientation flag; return the
* input UNCHANGED when orientation is normal/absent, the image can't be
* decoded, or anything else goes wrong.
*
* Tooling differs, contract doesn't:
* - tag read: com.drewnoakes:metadata-extractor (the Central-side
* peer of androidx.exifinterface),
* - rotate: BufferedImage + AffineTransform (no android.graphics),
* - encode: ImageIO JPEG writer at explicit 0.92 quality (Android
* uses Bitmap.compress(JPEG, 92)).
*
* Like the Android side, only the rotate-90/180/270 orientations do
* work; the mirror orientations (2–5, 7) fall through unchanged there
* too, so behaviour matches tag-for-tag.
*/
class JvmImageOrientationNormalizer : ImageOrientationNormalizer {
override fun normalize(bytes: ByteArray): ByteArray = runCatching {
val metadata = ImageMetadataReader.readMetadata(ByteArrayInputStream(bytes))
val ifd0 = metadata.getFirstDirectoryOfType(ExifIFD0Directory::class.java)
?: return@runCatching bytes
if (ifd0.hasErrors()) return@runCatching bytes
val angle = when (ifd0.getInteger(ExifIFD0Directory.TAG_ORIENTATION)) {
6 -> 90
3 -> 180
8 -> 270
else -> return@runCatching bytes // normal, absent, or a mirror tag
}
val img = ImageIO.read(ByteArrayInputStream(bytes)) ?: return@runCatching bytes
val rotated = rotate(img, angle)
encodeJpeg(rotated)
}.getOrDefault(bytes)
private fun rotate(img: BufferedImage, degrees: Int): BufferedImage {
val radians = Math.toRadians(degrees.toDouble())
val sin = abs(sin(radians))
val cos = abs(kotlin.math.cos(radians))
val w = img.width
val h = img.height
val out = BufferedImage(
(sin * h + cos * w).roundToInt(),
(cos * h + sin * w).roundToInt(),
BufferedImage.TYPE_INT_RGB,
)
val g = out.createGraphics()
val tx = AffineTransform()
tx.translate(out.width / 2.0, out.height / 2.0)
tx.rotate(radians)
tx.translate(-w / 2.0, -h / 2.0)
g.drawImage(img, tx, null)
g.dispose()
return out
}
private fun encodeJpeg(img: BufferedImage): ByteArray {
val writer = ImageIO.getImageWritersByFormatName("jpeg").next()
try {
val params = (writer.defaultWriteParam as ImageWriteParam).apply {
setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
compressionQuality = 0.92f // matches the Android side's compress(JPEG, 92)
}
val bos = ByteArrayOutputStream()
ImageIO.createImageOutputStream(bos).use { out ->
writer.output = out
writer.write(null, IIOImage(img, null, null), params)
}
return bos.toByteArray()
} finally {
writer.dispose()
}
}
}
@@ -0,0 +1,87 @@
package org.incido.data.jvm
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import javax.imageio.ImageIO
import org.incido.data.ImageOrientationNormalizer
/**
* Phase 1 (M4.4e) — `./gradlew :core:data-jvm:orientationSmoke`.
*
* Proves [JvmImageOrientationNormalizer] end-to-end without needing a
* phone: hand-builds a JPEG whose EXIF Orientation tag says
* "rotate 90", feeds it through the same [ImageOrientationNormalizer]
* seam `NoteRepositoryImpl` stores images through, and checks the
* rotation was baked into the pixels (dimensions swapped) while a
* no-EXIF / garbage input comes back byte-identical — the exact
* contract the Android normalizer established.
*/
fun main() {
val normalizer = JvmImageOrientationNormalizer()
var failures = 0
fun check(name: String, ok: Boolean) {
println("EXIF ${if (ok) "PASS" else "FAIL"} $name")
if (!ok) failures++
}
// 4x2 base JPEG (deliberately non-square so a 90° rotation swaps
// the dimensions in an observable way).
val base = jpegOf(BufferedImage(4, 2, BufferedImage.TYPE_INT_RGB))
check("no-EXIF input returned unchanged", normalizer.normalize(base).contentEquals(base))
val tagged = injectExifOrientation90(base)
val normalized = normalizer.normalize(tagged)
check("EXIF-90 input was rewritten", !normalized.contentEquals(tagged))
val decoded = ImageIO.read(ByteArrayInputStream(normalized))
check(
"rotation baked into pixels (4x2 -> 2x4)",
decoded != null && decoded.width == 2 && decoded.height == 4,
)
// The stored bytes carry no orientation flag — a second pass must
// be a no-op (this is WHY the app bakes the rotation at store time).
check("normalized output is stable (no orientation flag left)", normalizer.normalize(normalized).contentEquals(normalized))
check("garbage input returned unchanged", normalizer.normalize(byteArrayOf(1, 2, 3)).contentEquals(byteArrayOf(1, 2, 3)))
if (failures == 0) {
println("RESULT: M4.4e PASS — EXIF orientation is honoured on the JVM (Android contract kept).")
} else {
println("RESULT: M4.4e FAIL — $failures check(s) failed.")
kotlin.system.exitProcess(1)
}
}
private fun jpegOf(img: BufferedImage): ByteArray {
val bos = ByteArrayOutputStream()
ImageIO.write(img, "jpeg", bos)
return bos.toByteArray()
}
/**
* Splice a minimal APP1/EXIF segment (big-endian TIFF, one IFD0 entry:
* 0x0112 Orientation = 6 → "rotate 90 CW") in right after the JPEG SOI
* marker. metadata-extractor is read-only, so the probe writes its own.
*/
private fun injectExifOrientation90(jpeg: ByteArray): ByteArray {
require(jpeg[0] == 0xFF.toByte() && jpeg[1] == 0xD8.toByte()) { "not a JPEG" }
val tiff = byteArrayOf(
'M'.code.toByte(), 'M'.code.toByte(), // big-endian
0x00, 0x2A, // TIFF magic
0x00, 0x00, 0x00, 0x08, // IFD0 at offset 8
0x00, 0x01, // one entry
0x01, 0x12, // tag: Orientation
0x00, 0x03, // type: SHORT
0x00, 0x00, 0x00, 0x01, // count: 1
0x00, 0x06, 0x00, 0x00, // value: 6 (rotate 90 CW)
0x00, 0x00, 0x00, 0x00, // no next IFD
)
val payload = "Exif".toByteArray(Charsets.US_ASCII) + byteArrayOf(0, 0) + tiff
val segmentLen = payload.size + 2
val segment = byteArrayOf(
0xFF.toByte(), 0xE1.toByte(),
(segmentLen ushr 8).toByte(), (segmentLen and 0xFF).toByte(),
) + payload
return jpeg.copyOfRange(0, 2) + segment + jpeg.copyOfRange(2, jpeg.size)
}
@@ -15,7 +15,7 @@ import org.incido.compose.di.AppGraph
import org.incido.compose.di.EncryptionBadge
import org.incido.compose.di.LibraryVerification
import kotlinx.coroutines.flow.map
import org.incido.data.ImageOrientationNormalizer
import org.incido.data.jvm.JvmImageOrientationNormalizer
import org.incido.data.ObjectBoxStore
import org.incido.data.crypto.AesGcmFieldCipher
import org.incido.data.crypto.CipherHolder
@@ -81,7 +81,10 @@ class DesktopContainer(val dataDir: File) : AppGraph {
override val noteRepository: NoteRepository = NoteRepositoryImpl(
boxStore = boxStore,
cipherHolder = cipherHolder,
orientationNormalizer = ImageOrientationNormalizer.NoOp,
// M4.4e: real EXIF orientation on desktop too (metadata-extractor
// + BufferedImage) — the last NoOp placeholder is gone; see
// JvmImageOrientationNormalizer for the Android-contract mapping.
orientationNormalizer = JvmImageOrientationNormalizer(),
)
/** Migration sweep + startup integrity checker — the shared service. */
+2
View File
@@ -27,6 +27,7 @@ objectbox = "4.0.3"
# Exif
exifinterface = "1.3.7"
metadata-extractor = "2.19.0"
[libraries]
# Compose Multiplatform
@@ -51,6 +52,7 @@ androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process",
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidxNavigationCompose" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version = "1.13.1" }
androidx-exifinterface = { module = "androidx.exifinterface:exifinterface", version.ref = "exifinterface" }
metadata-extractor = { module = "com.drewnoakes:metadata-extractor", version.ref = "metadata-extractor" }
# v0.3.2.0: FragmentActivity (for BiometricPrompt host)
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version = "1.8.5" }