From 40082b3f5b35a4d43beafcac00ef11215a5d3373 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 23 Oct 2026 22:26:36 -0700 Subject: [PATCH] Show images attached to Android notifications --- ...aceclawMediaNotificationListenerService.kt | 103 ++++++++++++++++++ .../java/com/faceclaw/app/ImageFileLoader.kt | 9 +- CHANGELOG | 1 + app/native/notification-icons.ios.ts | 6 +- app/native/notification-icons.ts | 72 ++++++++++++ app/native/notification-types.ts | 2 + app/ui/notifications.ts | 49 ++++++++- .../com/faceclaw/app/graphics/GrayPacket.kt | 68 +++++++++++- .../com/faceclaw/app/GraphicsCoresTest.kt | 35 ++++++ 9 files changed, 332 insertions(+), 13 deletions(-) diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt index 5ca84df2..67aa713e 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt @@ -9,6 +9,7 @@ import android.graphics.Color import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable import android.graphics.drawable.Icon +import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper @@ -174,6 +175,33 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() { return out.toByteArray() } + /** + * The picture attached to one active notification (BigPictureStyle: a + * photo, a screenshot, Dexcom's glucose graph), scaled to fit + * maxWidth x maxHeight as a gray packet with the tone handling (and + * light-background inversion) of ImageFileLoader.bitmapToGrayPacket; + * empty when the notification or its picture is unavailable. + */ + @JvmStatic + fun getNotificationPictureGray( + key: String?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean, invertLightBackground: Boolean, + ): ByteArray { + val service = activeService + if (service == null || maxWidth <= 0 || maxHeight <= 0) { + return ByteArray(0) + } + val statusBarNotification = findActiveNotificationByKey(service, key) + if (statusBarNotification == null || statusBarNotification.notification == null) { + return ByteArray(0) + } + val picture = loadNotificationPicture(service, statusBarNotification.notification) + if (picture == null) { + return ByteArray(0) + } + return ImageFileLoader.bitmapToGrayPacket( + halveToward(picture, maxWidth, maxHeight), maxWidth, maxHeight, gamma, dither, invertLightBackground) + } + @JvmStatic fun getActiveNotificationsJson(maxNotifications: Int): String { val service = activeService @@ -483,6 +511,79 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() { return bitmap } + /** Whether a notification carries a picture for loadNotificationPicture, without loading it. */ + @Suppress("DEPRECATION") + private fun hasNotificationPicture(extras: Bundle): Boolean { + return extras.get(Notification.EXTRA_PICTURE) != null + || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && extras.get(Notification.EXTRA_PICTURE_ICON) != null) + } + + /** + * A notification's attached picture: BigPictureStyle's bitmap + * (EXTRA_PICTURE) or, from Android 12, its Icon (EXTRA_PICTURE_ICON). + * Newer releases swap large pictures for a content URI to a bitmap + * the system has offloaded (Dexcom's graph arrives that way); the + * offload provider lets enabled notification listeners open those. + */ + @Suppress("DEPRECATION") + private fun loadNotificationPicture(service: FaceclawMediaNotificationListenerService, notification: Notification): Bitmap? { + val extras: Bundle = notification.extras ?: return null + try { + val picture: Bitmap? = extras.getParcelable(Notification.EXTRA_PICTURE) + if (picture != null) { + return softwareBitmap(picture) + } + } catch (t: Throwable) { + Log.w(TAG, "failed to read notification picture", t) + } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { + return null + } + try { + val icon: Icon? = extras.getParcelable(Notification.EXTRA_PICTURE_ICON) + val drawable: Drawable? = icon?.loadDrawable(service) + if (drawable is BitmapDrawable && drawable.bitmap != null) { + return softwareBitmap(drawable.bitmap) + } + if (drawable != null && drawable.intrinsicWidth > 0 && drawable.intrinsicHeight > 0) { + val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888) + drawable.setBounds(0, 0, bitmap.width, bitmap.height) + drawable.draw(Canvas(bitmap)) + return bitmap + } + } catch (t: Throwable) { + Log.w(TAG, "failed to load notification picture icon", t) + } + return null + } + + /** Hardware bitmaps can't be read back pixel by pixel or drawn on a software canvas; copy those. */ + private fun softwareBitmap(bitmap: Bitmap): Bitmap { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && bitmap.config == Bitmap.Config.HARDWARE) { + return bitmap.copy(Bitmap.Config.ARGB_8888, false) + } + return bitmap + } + + /** + * Halve a picture while it is still at least twice the size it will + * be shown at, leaving the last (under 2x) step of fitting it into + * maxWidth x maxHeight to bitmapToGrayPacket. As with icons + * (renderIconScaled), one filtered pass from a much larger source + * samples too sparsely: a graph's thin lines break up into dots. + */ + private fun halveToward(source: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap { + var bitmap = source + while ((bitmap.width >= maxWidth * 2 || bitmap.height >= maxHeight * 2) && bitmap.width >= 2 && bitmap.height >= 2) { + val halved = Bitmap.createScaledBitmap(bitmap, bitmap.width / 2, bitmap.height / 2, true) + if (bitmap !== source) { + bitmap.recycle() + } + bitmap = halved + } + return bitmap + } + /** * Debug aid for garbled-icon reports: saves each rendered icon to * /debug-icons/ (adb-pullable) so extraction problems can @@ -563,8 +664,10 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() { } } out.put("lines", lines) + out.put("hasPicture", hasNotificationPicture(extras)) } else { out.put("lines", JSONArray()) + out.put("hasPicture", false) } val actionsJson = JSONArray() diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt index b73baee1..1df754f9 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt @@ -132,10 +132,15 @@ class ImageFileLoader private constructor() { * 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. + * + * invertLightBackground shows a picture drawn on a light background + * (a chart, a screenshot) as its negative; see GrayPacket.fromArgb. */ @JvmStatic + @JvmOverloads fun bitmapToGrayPacket( - source: Bitmap?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray { + source: Bitmap?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean, + invertLightBackground: Boolean = false): ByteArray { if (source == null || source.width <= 0 || source.height <= 0 || maxWidth <= 0 || maxHeight <= 0) { return ByteArray(0) @@ -152,7 +157,7 @@ class ImageFileLoader private constructor() { } val pixels = IntArray(width * height) scaled.getPixels(pixels, 0, width, 0, 0, width, height) - return GrayPacket.fromArgb(pixels, width, height, gamma, dither) + return GrayPacket.fromArgb(pixels, width, height, gamma, dither, invertLightBackground) } catch (e: Exception) { Log.w(TAG, "bitmap conversion failed", e) return ByteArray(0) diff --git a/CHANGELOG b/CHANGELOG index 47b23ce0..0ba3c545 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ ### Features * Use Open-Meteo for weather in regions that the US National Weather Service doesn't cover +* Android: Show pictures attached to notifications (such as Dexcom's glucose graph) in the notification view ### Bug fixes diff --git a/app/native/notification-icons.ios.ts b/app/native/notification-icons.ios.ts index 31cec178..c908c838 100644 --- a/app/native/notification-icons.ios.ts +++ b/app/native/notification-icons.ios.ts @@ -4,7 +4,7 @@ import { AncsClient, ANCS_CONNECT_MESSAGE, type AncsState } from '../g2/ancs-cli import { rememberNotificationSources } from './notification-sources' import type { AndroidNotification } from './notification-types' export type { AndroidNotification, AndroidNotificationAction } from './notification-types' -export type { NotificationIconsResult, NotificationIconResult } from './notification-icons' +export type { NotificationIconsResult, NotificationIconResult, NotificationPictureResult } from './notification-icons' export const ALL_NOTIFICATIONS = 0x7fffffff let active: AncsClient | null = null @@ -61,3 +61,7 @@ export function readNotificationIconByKey(key: string, _allowStale: boolean) { const notification = readActiveNotifications(ALL_NOTIFICATIONS).find(n => n.key === key) return {icon: notification ? categoryIcon(notification.category) : null,stale:false} } +// ANCS carries no attachments. +export function readNotificationPicture(_notification: AndroidNotification, _maxWidth: number, _maxHeight: number, _allowStale: boolean) { + return {picture: null,stale:false} +} diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index c0ded10b..f3ec39db 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -2,6 +2,7 @@ import { GrayImage } from "../graphics/image"; import { logCurrent, spanCurrent } from "./frame-timings"; import { toUint8Array } from "../util/array-util"; import { rememberNotificationSources } from "./notification-sources"; +import { PHOTO_TONE, grayImageFromPacket } from "./image-files"; declare const com: any; declare const java: any; @@ -22,6 +23,11 @@ let cachedKeys: string[] = []; let cachedAtMs = 0; const keyedIconCache = new Map(); const KEYED_ICON_CACHE_MAX = 128; +/** Pictures are the size of a detail view, not 24px icons: keep just the recently viewed few. */ +const pictureCache = new Map(); +const PICTURE_CACHE_MAX = 8; let notificationListenerProxy: any = null; const notificationPostedListeners = new Set<(notificationKey: string) => void>(); const notificationRemovedListeners = new Set<(notificationKey: string) => void>(); @@ -151,6 +157,71 @@ export function readNotificationIconByKey(key: string, allowStale: boolean): Not return { icon: icon?.clone() ?? null, stale: false }; } +export type NotificationPictureResult = { + /** Shared with the cache: blit it, don't draw on it. */ + picture: GrayImage | null; + /** True when allowStale served an earlier fetch (or nothing) in place of a fetch for these arguments. */ + stale: boolean; +}; + +/** + * The picture attached to a notification (hasPicture), scaled to fit + * maxWidth x maxHeight with the photo tone and inverted if it is drawn on a + * light background (GrayPacket.hasLightBackground), under the allow-stale contract of + * readNotificationIconByKey. Cached per post of the notification: an ongoing + * notification updated in place (Dexcom redraws its graph every few minutes) + * fires no posted event, but its postTime moves. ICON_CACHE_MS is the + * backstop here too. Under allowStale an earlier fetch stands in, if it fits. + */ +export function readNotificationPicture( + notification: AndroidNotification, + maxWidth: number, + maxHeight: number, + allowStale: boolean, +): NotificationPictureResult { + const width = Math.round(maxWidth); + const height = Math.round(maxHeight); + if (!global.isAndroid || !notification.hasPicture || width <= 0 || height <= 0) { + return { picture: null, stale: false }; + } + + const now = Date.now(); + const cached = pictureCache.get(notification.key); + if ( + cached && cached.postTime === notification.postTime && cached.maxWidth === width + && cached.maxHeight === height && now - cached.atMs < ICON_CACHE_MS + ) { + return { picture: cached.picture, stale: false }; + } + if (allowStale) { + const fits = cached?.picture && cached.picture.width <= width && cached.picture.height <= height; + return { picture: fits ? cached!.picture : null, stale: true }; + } + + const picture = spanCurrent("fetch-notification-picture", () => + grayImageFromPacket( + com.faceclaw.app.FaceclawMediaNotificationListenerService.getNotificationPictureGray( + notification.key, + width, + height, + PHOTO_TONE.gamma, + PHOTO_TONE.dither, + // A chart or screenshot made for a light theme would be a glaring + // lit panel on the glasses: show it light-on-dark. + true, + ), + ), + ); + pictureCache.delete(notification.key); + pictureCache.set(notification.key, { postTime: notification.postTime, maxWidth: width, maxHeight: height, picture, atMs: now }); + while (pictureCache.size > PICTURE_CACHE_MAX) { + const oldestKey = pictureCache.keys().next().value; + if (oldestKey === undefined) break; + pictureCache.delete(oldestKey); + } + return { picture, stale: false }; +} + export function readActiveNotifications(maxNotifications = 50): AndroidNotification[] { if (!global.isAndroid || maxNotifications <= 0) return []; try { @@ -250,6 +321,7 @@ function normalizeNotification(value: any): AndroidNotification | null { when: Number(value.when) || 0, actions, foregroundService: Boolean(value.foregroundService), + hasPicture: Boolean(value.hasPicture), }; } diff --git a/app/native/notification-types.ts b/app/native/notification-types.ts index a8cd93f7..78d35ac0 100644 --- a/app/native/notification-types.ts +++ b/app/native/notification-types.ts @@ -7,6 +7,8 @@ export type AndroidNotification = { actions: AndroidNotificationAction[]; dismissLabel?: string; /** Posted by a foreground service (Android): a "this app is working" status notification. */ foregroundService?: boolean; + /** Carries a picture (Android BigPictureStyle), fetched separately by readNotificationPicture. */ + hasPicture?: boolean; /** Full message size reported by iOS, even when the fetched text is bounded. */ messageSize?: number; }; diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index 268e7030..8f05a1b6 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -10,6 +10,7 @@ import { invokeNotificationAction, readActiveNotifications, readNotificationIconByKey, + readNotificationPicture, type AndroidNotification, type AndroidNotificationAction, } from "../native/notification-icons"; @@ -44,6 +45,13 @@ const DETAIL_TITLE_Y = 21; /** Gaps below the title's line box and below the icon/app-name header row. */ const DETAIL_HEADER_GAP = 3; const DETAIL_BODY_GAP = 4; +/** Text lines an attached picture leaves room for above it (when there are that many); it takes the rest. */ +const DETAIL_PICTURE_TEXT_LINES = 2; +/** Gap between the text and an attached picture, and the space kept below the picture. */ +const DETAIL_PICTURE_GAP = 6; +const DETAIL_PICTURE_BOTTOM_MARGIN = 12; +/** An attached picture with less height than this to fit in (a large UI font) is left out. */ +const DETAIL_PICTURE_MIN_HEIGHT = 32; // Disable-source confirmation: actions on top, the explanation below them. const CONFIRM_X = 24; const CONFIRM_Y = 8; @@ -59,6 +67,16 @@ function iconForNotification(key: string): GrayImage | null { return icon; } +/** Attached picture for a paint pass, fitting maxWidth x maxHeight; allow-stale like iconForNotification. */ +function pictureForNotification(notification: AndroidNotification, maxWidth: number, maxHeight: number): GrayImage | null { + if (!notification.hasPicture || maxHeight < DETAIL_PICTURE_MIN_HEIGHT) return null; + const { picture, stale } = readNotificationPicture(notification, maxWidth, maxHeight, renderPassAllowsStaleData()); + if (stale) { + noteStaleDataUsed(); + } + return picture; +} + type CardLayout = { notification: AndroidNotification; height: number; @@ -422,23 +440,46 @@ function drawDetailContent( const bodyTop = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`); const lines: string[] = []; - lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth)); + // A picture can be the whole notification (Dexcom's has no text), so it + // stands in for the "(untitled)" placeholder. + const hasTitle = Boolean(notification.title) || !notification.hasPicture; + if (hasTitle) { + lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth)); + } const body = detailNotificationBody(notification); if (body) { - lines.push(""); + if (lines.length) lines.push(""); lines.push(...wrapText(font, body, contentWidth)); } const meta = [notification.subText, notification.infoText, notification.summaryText].filter(Boolean).join(" "); if (meta) { - lines.push(""); + if (lines.length) lines.push(""); lines.push(...wrapText(font, meta, contentWidth)); } const step = lineStep(font); + const textValue = (index: number) => index === 0 && hasTitle ? 230 : 190; + const reservedTextHeight = Math.min(lines.length, DETAIL_PICTURE_TEXT_LINES) * step; + const picture = pictureForNotification(notification, contentWidth, + height - DETAIL_PICTURE_BOTTOM_MARGIN - bodyTop - reservedTextHeight - (reservedTextHeight ? DETAIL_PICTURE_GAP : 0)); + if (picture) { + // The text gets whatever the picture leaves, above it, with its last + // shown line marked when some is cut off. + const roomForText = height - DETAIL_PICTURE_BOTTOM_MARGIN - picture.height - DETAIL_PICTURE_GAP - bodyTop; + const shownLines = Math.min(lines.length, Math.max(0, (roomForText / step) | 0)); + for (let index = 0; index < shownLines; index++) { + const line = index === shownLines - 1 && lines.length > shownLines + ? truncateText(font, lines[index] + "...", contentWidth) : lines[index]!; + image.drawText(font, contentX, bodyTop + index * step, line, textValue(index)); + } + image.bitBlt(picture, contentX, bodyTop + shownLines * step + (shownLines ? DETAIL_PICTURE_GAP : 0)); + return; + } + const maxLines = Math.max(1, ((height - bodyTop - step) / step) | 0); for (let index = 0; index < Math.min(lines.length, maxLines); index++) { const line = lines[index]!; - image.drawText(font, contentX, bodyTop + index * step, line, index === 0 ? 230 : 190); + image.drawText(font, contentX, bodyTop + index * step, line, textValue(index)); } if (lines.length > maxLines) { image.drawText(font, contentX, height - 24, "...", 140); diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GrayPacket.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GrayPacket.kt index f11f1af9..172663f8 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GrayPacket.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GrayPacket.kt @@ -1,5 +1,6 @@ package com.faceclaw.app +import kotlin.jvm.JvmOverloads import kotlin.jvm.JvmStatic import kotlin.math.pow import kotlin.math.roundToInt @@ -23,20 +24,28 @@ object GrayPacket { * (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. + * + * invertLightBackground converts a picture drawn on a light background (hasLightBackground) + * as its negative, so a chart or screenshot made for a light theme shows light-on-dark: the + * display lights what is light, so a light background is a glaring panel, while black is + * see-through. The colors are inverted, not their alpha, before the tone curve, where the + * sRGB-encoded values are roughly perceptually even; inverting the curve's output instead + * would turn dim strokes into ones barely darker than the background. */ @JvmStatic - fun fromArgb(pixels: IntArray, width: Int, height: Int, gamma: Float, dither: Boolean): ByteArray { + @JvmOverloads + fun fromArgb( + pixels: IntArray, width: Int, height: Int, gamma: Float, dither: Boolean, invertLightBackground: Boolean = false, + ): ByteArray { require(width > 0 && height > 0 && pixels.size >= width * height) { "bad pixel plane" } val out = ByteArray(HEADER_SIZE + width * height) writeHeader(out, width, height) val tone = toneCurve(gamma) + val invert = invertLightBackground && hasLightBackground(pixels, width, height) for (i in 0 until width * height) { val p = pixels[i] - val a = (p ushr 24) and 0xff - val r = (p shr 16) and 0xff - val g = (p shr 8) and 0xff - val b = p and 0xff - out[HEADER_SIZE + i] = tone[((r * 299 + g * 587 + b * 114) / 1000) * a / 255].toByte() + val luma = luma(p) + out[HEADER_SIZE + i] = tone[(if (invert) 255 - luma else luma) * alpha(p) / 255].toByte() } if (dither) { ditherToDisplayLevels(out, HEADER_SIZE, width, height) @@ -44,6 +53,53 @@ object GrayPacket { return out } + /** Grays within this span of each other count as one background (compression noise, faint shading). */ + private const val BACKGROUND_SPAN = 16 + /** The share of the pixels a background covers; a photo's tones mostly spread wider than this. */ + private const val BACKGROUND_MIN_SHARE = 0.4f + /** A background at least this light (source gray, before the tone curve) counts as light. */ + private const val LIGHT_BACKGROUND_MIN_GRAY = 160 + + /** + * Whether a picture is drawn on a light background: the most populous span of + * BACKGROUND_SPAN adjacent grays (as displayed: composited over black, before the tone + * curve) covers BACKGROUND_MIN_SHARE of the picture and is light. The flat backgrounds of + * charts, screenshots and documents qualify; photographs mostly don't, though a large + * blown-out sky can. + */ + @JvmStatic + fun hasLightBackground(pixels: IntArray, width: Int, height: Int): Boolean { + val count = width * height + val histogram = IntArray(256) + for (i in 0 until count) { + val p = pixels[i] + histogram[luma(p) * alpha(p) / 255] += 1 + } + var window = 0 + var best = 0 + var bestStart = 0 + for (v in 0 until 256) { + window += histogram[v] + if (v >= BACKGROUND_SPAN) { + window -= histogram[v - BACKGROUND_SPAN] + } + if (window > best) { + best = window + bestStart = maxOf(0, v - BACKGROUND_SPAN + 1) + } + } + return best >= BACKGROUND_MIN_SHARE * count && bestStart + BACKGROUND_SPAN / 2 >= LIGHT_BACKGROUND_MIN_GRAY + } + + private fun luma(p: Int): Int { + val r = (p shr 16) and 0xff + val g = (p shr 8) and 0xff + val b = p and 0xff + return (r * 299 + g * 587 + b * 114) / 1000 + } + + private fun alpha(p: Int): Int = (p ushr 24) and 0xff + @JvmStatic fun writeHeader(out: ByteArray, width: Int, height: Int) { out[0] = (width and 0xff).toByte() diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GraphicsCoresTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GraphicsCoresTest.kt index 1fc78bbd..3d228f5e 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GraphicsCoresTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GraphicsCoresTest.kt @@ -33,6 +33,41 @@ class GraphicsCoresTest { assertContentEquals(byteArrayOf(0x2c, 1, 2, 0), wide.copyOfRange(0, 4)) } + @Test + fun lightBackgroundsInvertBeforeTheToneCurve() { + val white = 0xffffffff.toInt() + val black = 0xff000000.toInt() + // A white chart with a black stroke and a fully transparent corner. + val chart = IntArray(10 * 10) { white } + chart[55] = black + chart[0] = 0x00ffffff + assertTrue(GrayPacket.hasLightBackground(chart, 10, 10)) + val inverted = GrayPacket.fromArgb(chart, 10, 10, 2.2f, false, true) + assertEquals(0, inverted[4 + 1].toInt() and 0xff) // background goes black + assertEquals(255, inverted[4 + 55].toInt() and 0xff) // stroke goes white + assertEquals(0, inverted[4 + 0].toInt() and 0xff) // transparent stays black + // Mid-gray inverts to its sRGB complement, then takes the tone curve. + chart[56] = 0xff404040.toInt() + val curve = GrayPacket.toneCurve(2.2f) + assertEquals(curve[255 - 0x40], GrayPacket.fromArgb(chart, 10, 10, 2.2f, false, true)[4 + 56].toInt() and 0xff) + // Without the flag the conversion is unchanged. + assertContentEquals(GrayPacket.fromArgb(chart, 10, 10, 2.2f, false), GrayPacket.fromArgb(chart, 10, 10, 2.2f, false, false)) + } + + @Test + fun darkBackgroundsAndPhotosKeepTheirTones() { + // A black chart with a white stroke: already light-on-dark. + val dark = IntArray(10 * 10) { 0xff000000.toInt() } + dark[55] = 0xffffffff.toInt() + assertTrue(!GrayPacket.hasLightBackground(dark, 10, 10)) + assertContentEquals(GrayPacket.fromArgb(dark, 10, 10, 1f, false), GrayPacket.fromArgb(dark, 10, 10, 1f, false, true)) + // A photo-like spread of tones, even a bright one, has no background to invert. + val ramp = IntArray(16 * 16) { i -> val v = 64 + (i * 191) / 255; 0xff000000.toInt() or (v shl 16) or (v shl 8) or v } + assertTrue(!GrayPacket.hasLightBackground(ramp, 16, 16)) + // A mid-gray background is not light enough to be worth inverting. + assertTrue(!GrayPacket.hasLightBackground(IntArray(8 * 8) { 0xff808080.toInt() }, 8, 8)) + } + @Test fun toneCurveDarkensMidtones() { val identity = GrayPacket.toneCurve(1f)