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 57d91429..9bc21eac 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,9 +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. + * 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. */ - public byte[] getAlbumArtGray(int maxSize) { + public byte[] getAlbumArtGray(int maxSize, float gamma) { Bitmap art; synchronized (lock) { if (activeController == null) { @@ -225,7 +227,7 @@ public class FaceclawMediaController { art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON); } } - return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize); + return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, true); } /** 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 bed62dc2..84e300eb 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 @@ -84,6 +84,26 @@ public final class ImageFileLoader { * Transparent pixels darken toward black (the on-glasses background). */ public static byte[] bitmapToGrayPacket(Bitmap source, int maxWidth, int maxHeight) { + return bitmapToGrayPacket(source, maxWidth, maxHeight, 1f, false); + } + + /** + * As above, with photographic tone handling for continuous-tone images + * (album art, photos) rather than UI raster: + * + * gamma > 1 darkens midtones (out = 255 * (in/255)^gamma). Source pixels + * are sRGB-encoded but the G2 drives its 16 levels roughly linearly, so + * mid-gray otherwise displays far brighter than intended; 2.2 undoes the + * sRGB encoding outright. + * + * dither error-diffuses against the 16 levels the frame is eventually + * packed to (BmpUtil.GRAY_TO_NIBBLE) instead of leaving each pixel to + * round on its own, which turns smooth gradients into visible bands. + * Dithered output is already quantized: every byte is a level times 16, + * which BmpUtil re-quantizes back to exactly that level. + */ + public static byte[] bitmapToGrayPacket( + Bitmap source, int maxWidth, int maxHeight, float gamma, boolean dither) { if (source == null || source.getWidth() <= 0 || source.getHeight() <= 0 || maxWidth <= 0 || maxHeight <= 0) { return new byte[0]; @@ -105,13 +125,17 @@ public final class ImageFileLoader { out[1] = (byte) ((width >> 8) & 0xff); out[2] = (byte) (height & 0xff); out[3] = (byte) ((height >> 8) & 0xff); + int[] tone = toneCurve(gamma); for (int i = 0; i < pixels.length; i++) { int p = pixels[i]; int a = (p >>> 24) & 0xff; int r = (p >> 16) & 0xff; int g = (p >> 8) & 0xff; int b = p & 0xff; - out[4 + i] = (byte) (((r * 299 + g * 587 + b * 114) / 1000) * a / 255); + out[4 + i] = (byte) tone[((r * 299 + g * 587 + b * 114) / 1000) * a / 255]; + } + if (dither) { + ditherToDisplayLevels(out, 4, width, height); } return out; } catch (Exception | OutOfMemoryError e) { @@ -119,4 +143,67 @@ public final class ImageFileLoader { return new byte[0]; } } + + /** 256-entry map of source gray to displayed gray for out = in^gamma. */ + private static int[] toneCurve(float gamma) { + int[] curve = new int[256]; + for (int v = 0; v < 256; v++) { + curve[v] = gamma == 1f + ? v + : Math.round(255f * (float) Math.pow(v / 255f, gamma)); + } + return curve; + } + + /** + * Floyd-Steinberg error diffusion onto the display's 16 gray levels, in + * place over an 8bpp plane. Serpentine scanning keeps the diffusion from + * building up a directional texture across wide flat areas. + * + * Level n is written back as n * 16 rather than the n * 17 it stands for + * so that BmpUtil's (v + 8) >> 4 reproduces n exactly; level 0 is written + * as 1, not 0, because 0 is the shell's color-key for transparent. + */ + private static void ditherToDisplayLevels(byte[] plane, int offset, int width, int height) { + float[] curr = new float[width]; + float[] next = new float[width]; + for (int x = 0; x < width; x++) { + curr[x] = plane[offset + x] & 0xff; + } + for (int y = 0; y < height; y++) { + int rowStart = offset + y * width; + int nextStart = rowStart + width; + boolean hasNext = y + 1 < height; + for (int x = 0; x < width; x++) { + next[x] = hasNext ? (plane[nextStart + x] & 0xff) : 0f; + } + boolean leftToRight = (y & 1) == 0; + int start = leftToRight ? 0 : width - 1; + int step = leftToRight ? 1 : -1; + for (int i = 0; i < width; i++) { + int x = start + i * step; + float wanted = curr[x]; + int level = Math.round(Math.min(255f, Math.max(0f, wanted)) / 17f); + float error = wanted - level * 17f; + plane[rowStart + x] = (byte) (level == 0 ? 1 : level * 16); + int ahead = x + step; + if (ahead >= 0 && ahead < width) { + curr[ahead] += error * (7f / 16f); + } + if (hasNext) { + if (ahead >= 0 && ahead < width) { + next[ahead] += error * (1f / 16f); + } + next[x] += error * (5f / 16f); + int behind = x - step; + if (behind >= 0 && behind < width) { + next[behind] += error * (3f / 16f); + } + } + } + float[] swap = curr; + curr = next; + next = swap; + } + } } diff --git a/app/native/media-controller.ts b/app/native/media-controller.ts index c17a8480..0f629651 100644 --- a/app/native/media-controller.ts +++ b/app/native/media-controller.ts @@ -4,6 +4,14 @@ import { grayImageFromPacket } 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; @@ -133,14 +141,17 @@ export class FaceclawMediaControllerBridge { } /** - * Album art for the current item, grayscale, scaled to fit maxSize; null - * when the player provides none. + * 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. */ 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))); + return grayImageFromPacket( + this.controller.getAlbumArtGray(Math.round(maxSize), ALBUM_ART_GAMMA), + ); } /** The player's queue (playlist), empty when the player exposes none. */