Files app: Gamma correct and dither images when viewing

This commit is contained in:
James Babcock committed 2026-09-01 21:17:42 -07:00
1 parent a72e29a04f
commit c364099a8c
6 files changed
+85 -28

No files matched your search

+43 -2
View File
@@ -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<Required<GrayImageTone>> = { 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<number> | 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}`);
+5 -13
View File
@@ -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),
);
}