From c364099a8c26b6f04a8ec2a87b4a1f4cd9fa1864 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Tue, 1 Sep 2026 21:09:08 -0700 Subject: [PATCH] Files app: Gamma correct and dither images when viewing --- .../faceclaw/app/FaceclawMediaController.java | 10 ++--- .../com/faceclaw/app/ImageFileLoader.java | 28 ++++++++++-- CHANGELOG | 3 ++ app/apps/files/image-viewer.ts | 9 ++-- app/native/image-files.ts | 45 ++++++++++++++++++- app/native/media-controller.ts | 18 +++----- 6 files changed, 85 insertions(+), 28 deletions(-) diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java index 9bc21eac..3762aafb 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java @@ -205,11 +205,11 @@ public class FaceclawMediaController { * Album art for the active session's current item, grayscale, scaled to * fit within maxSize x maxSize preserving aspect. Returns a gray packet * (see ImageFileLoader.bitmapToGrayPacket) or an empty array when no art - * is available. Cover art is continuous-tone, so it is tone-mapped by - * gamma and dithered onto the display's 16 levels rather than rounded - * per pixel. + * is available. gamma and dither are the photographic tone handling + * described on ImageFileLoader.bitmapToGrayPacket; the TS side passes + * its shared photo preset. */ - public byte[] getAlbumArtGray(int maxSize, float gamma) { + public byte[] getAlbumArtGray(int maxSize, float gamma, boolean dither) { Bitmap art; synchronized (lock) { if (activeController == null) { @@ -227,7 +227,7 @@ public class FaceclawMediaController { art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON); } } - return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, true); + return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, dither); } /** diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java index 84e300eb..ef6debbb 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java @@ -8,7 +8,9 @@ import android.util.Log; * Decodes image files (and bitmaps generally) to the grayscale packet format * used across the TS bridge: [widthLo, widthHi, heightLo, heightHi, * pixels...] with one byte per pixel, row-major, or an empty array on - * failure. FaceclawMediaController's album art shares bitmapToGrayPacket. + * failure. FaceclawMediaController's album art shares bitmapToGrayPacket; + * the TS side (app/native/image-files.ts) owns the shared photo-tone preset + * passed as (gamma, dither). */ public final class ImageFileLoader { private static final String TAG = "ImageFileLoader"; @@ -17,9 +19,21 @@ public final class ImageFileLoader { /** * Decode an image file and downscale it to fit within maxWidth x - * maxHeight, preserving aspect ratio and never upscaling. + * maxHeight, preserving aspect ratio and never upscaling. Flat + * conversion (no tone curve, per-pixel rounding): right for UI raster + * such as icons. */ public static byte[] loadGray(String path, int maxWidth, int maxHeight) { + return loadGray(path, maxWidth, maxHeight, 1f, false); + } + + /** + * As above, with the photographic tone handling described on + * bitmapToGrayPacket(Bitmap, int, int, float, boolean): use for + * continuous-tone images (photos) viewed on the glasses. + */ + public static byte[] loadGray( + String path, int maxWidth, int maxHeight, float gamma, boolean dither) { if (path == null || maxWidth <= 0 || maxHeight <= 0) { return new byte[0]; } @@ -43,7 +57,7 @@ public final class ImageFileLoader { if (bitmap == null) { return new byte[0]; } - return bitmapToGrayPacket(bitmap, maxWidth, maxHeight); + return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither); } catch (Exception | OutOfMemoryError e) { Log.w(TAG, "image decode failed: " + path, e); return new byte[0]; @@ -56,6 +70,12 @@ public final class ImageFileLoader { * ByteBuffer because NativeScript marshals a JS ArrayBuffer to one. */ public static byte[] loadGrayFromBytes(java.nio.ByteBuffer buffer, int maxWidth, int maxHeight) { + return loadGrayFromBytes(buffer, maxWidth, maxHeight, 1f, false); + } + + /** As above, with photographic tone handling (see bitmapToGrayPacket). */ + public static byte[] loadGrayFromBytes( + java.nio.ByteBuffer buffer, int maxWidth, int maxHeight, float gamma, boolean dither) { if (buffer == null || maxWidth <= 0 || maxHeight <= 0) { return new byte[0]; } @@ -71,7 +91,7 @@ public final class ImageFileLoader { if (bitmap == null) { return new byte[0]; } - return bitmapToGrayPacket(bitmap, maxWidth, maxHeight); + return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither); } catch (Exception | OutOfMemoryError e) { Log.w(TAG, "byte-array image decode failed", e); return new byte[0]; diff --git a/CHANGELOG b/CHANGELOG index 57fb6b82..f1f462b9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,6 +9,9 @@ Compass app: * Compass now handles the distincitn between true north and magnetic north * EvenHub compass readings now include calibrated magnetic, declination and true headings +Files app: +* Viewing image files gamma-corrects and dithers them + ## 0.6.3 - 2026-09-01 * Update custom firmware base version from 2.2.6 to 2.2.9 * Add support for the short-then-long-press gesture diff --git a/app/apps/files/image-viewer.ts b/app/apps/files/image-viewer.ts index 52d9072d..974dae06 100644 --- a/app/apps/files/image-viewer.ts +++ b/app/apps/files/image-viewer.ts @@ -2,7 +2,7 @@ import { type BdfFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { truncateText } from "../../graphics/textwrap"; import { GrayImage } from "../../graphics/image"; -import { loadImageFileAsGray } from "../../native/image-files"; +import { loadImageFileAsGray, PHOTO_TONE } from "../../native/image-files"; import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; @@ -13,8 +13,9 @@ const BODY_MARGIN = 2; /** * Image viewer for a file on disk: title line, then the image decoded to - * grayscale and downscaled to fit the remaining area (never upscaled). - * Double-click closes it. Sized to its hosting stack (a Files document + * grayscale with the shared photo tone (sRGB gamma, dithered onto the 16 + * display levels) and downscaled to fit the remaining area (never + * upscaled). Double-click closes it. Sized to its hosting stack (a Files document * window, or pushed over the file browser for view-in-place). */ export class ImageViewerLayer implements Layer { @@ -35,7 +36,7 @@ export class ImageViewerLayer implements Layer { const bodyHeight = height - BODY_TOP - BODY_MARGIN; if (!this.loadAttempted) { this.loadAttempted = true; - this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight); + this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight, PHOTO_TONE); } if (!this.image) { out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load image)", 160); diff --git a/app/native/image-files.ts b/app/native/image-files.ts index 78a335e7..932861a8 100644 --- a/app/native/image-files.ts +++ b/app/native/image-files.ts @@ -15,6 +15,39 @@ export function isDecodableImageFile(name: string): boolean { return DECODABLE_IMAGE.test(name); } +/** + * How source gray maps onto the display's 16 levels when converting to a + * GrayImage. The default (gamma 1, no dither) is a flat conversion with + * per-pixel rounding, right for UI raster such as icons and map tiles. + */ +export type GrayImageTone = { + /** + * Tone curve out = in^gamma (in 0..1). gamma > 1 darkens midtones: source + * pixels are sRGB-encoded but the G2 drives its levels roughly linearly, + * so untouched photos read washed out. + */ + gamma?: number; + /** + * Floyd-Steinberg error diffusion onto the 16 display levels instead of + * rounding each pixel independently, which bands smooth gradients. + */ + dither?: boolean; +}; + +/** + * Gamma for continuous-tone images (photos, album art): 2.2 undoes the sRGB + * encoding outright. Lower it toward ~1.8 if shadows crush. + */ +export const PHOTO_GAMMA = 2.2; + +/** Tone preset for continuous-tone images: sRGB gamma plus dithering. */ +export const PHOTO_TONE: Readonly> = { gamma: PHOTO_GAMMA, dither: true }; + +/** Resolve a tone to the (gamma, dither) pair the Java helpers take. */ +export function resolveGrayImageTone(tone?: GrayImageTone): { gamma: number; dither: boolean } { + return { gamma: tone?.gamma ?? 1, dither: tone?.dither ?? false }; +} + /** * Decode a gray packet ([widthLo, widthHi, heightLo, heightHi, pixels...], * the format the Java helpers return) into a GrayImage; null when empty or @@ -34,12 +67,20 @@ export function grayImageFromPacket(raw: ArrayLike | null | undefined): /** * Load an image file as grayscale, downscaled to fit maxWidth x maxHeight * (aspect preserved, never upscaled); null when unreadable or undecodable. + * Pass PHOTO_TONE for continuous-tone images; the default is a flat + * conversion for UI raster. */ -export function loadImageFileAsGray(path: string, maxWidth: number, maxHeight: number): GrayImage | null { +export function loadImageFileAsGray( + path: string, + maxWidth: number, + maxHeight: number, + tone?: GrayImageTone, +): GrayImage | null { if (!global.isAndroid) return null; try { + const { gamma, dither } = resolveGrayImageTone(tone); return grayImageFromPacket( - com.faceclaw.app.ImageFileLoader.loadGray(path, Math.round(maxWidth), Math.round(maxHeight)), + com.faceclaw.app.ImageFileLoader.loadGray(path, Math.round(maxWidth), Math.round(maxHeight), gamma, dither), ); } catch (error) { console.warn(`loadImageFileAsGray failed for ${path}: ${error}`); diff --git a/app/native/media-controller.ts b/app/native/media-controller.ts index 0f629651..600206c3 100644 --- a/app/native/media-controller.ts +++ b/app/native/media-controller.ts @@ -1,17 +1,9 @@ import { Utils } from "@nativescript/core"; import { GrayImage } from "../graphics/image"; -import { grayImageFromPacket } from "./image-files"; +import { grayImageFromPacket, PHOTO_TONE } from "./image-files"; declare const com: any; -/** - * Tone curve applied to album art before it is dithered down to the display's - * 16 gray levels (out = in^gamma, in 0..1). Cover art is sRGB-encoded but the - * G2 drives its levels roughly linearly, so untouched art reads washed out; - * 2.2 undoes the sRGB encoding. Lower it toward ~1.8 if shadows crush. - */ -const ALBUM_ART_GAMMA = 2.2; - export type MediaQueueItem = { id: number; title: string; @@ -141,16 +133,16 @@ export class FaceclawMediaControllerBridge { } /** - * Album art for the current item, grayscale (gamma-corrected and dithered - * onto the display's 16 levels), scaled to fit maxSize; null when the - * player provides none. + * Album art for the current item, grayscale with the shared photo tone + * (gamma-corrected and dithered onto the display's 16 levels), scaled to + * fit maxSize; null when the player provides none. */ getAlbumArt(maxSize: number): GrayImage | null { if (!global.isAndroid) return null; this.ensureController(); if (!this.controller) return null; return grayImageFromPacket( - this.controller.getAlbumArtGray(Math.round(maxSize), ALBUM_ART_GAMMA), + this.controller.getAlbumArtGray(Math.round(maxSize), PHOTO_TONE.gamma, PHOTO_TONE.dither), ); }