Dither when darkening for the system context menu

This commit is contained in:
James Babcock committed 2026-09-25 01:30:26 -07:00
1 parent 4f3ad37c20
commit 90d60cee10
9 files changed
+132 -25

No files matched your search

@@ -1,6 +1,6 @@
package com.faceclaw.app
/** Revision 29 wire grammar, shared by scene planning and the local renderer. */
/** Revision 33 wire grammar, shared by scene planning and the local renderer. */
object DrawProtocol {
/** Mirrors g2flash/patches/zlib_glue.c. */
const val DRAW = CFW_MSG_DRAW_CALLS
@@ -154,20 +154,26 @@ object DrawProtocol {
fun playList(id: Int, target: Int? = null, depth: Int? = null): ByteArray =
call(DRAW_OP_DISPLAY_LIST, target, depth) { writeU16(id) }
fun lut(width: Int, height: Int, factor: Int): ByteArray {
val table = ByteArray(8) { i ->
(((i * 2 * factor / 256).coerceIn(0, 15) shl 4) or
((i * 2 + 1) * factor / 256).coerceIn(0, 15)).toByte()
}
/** Revision 33: target pixels with even x+y map through [even], odd ones through [odd]. */
fun remapColors(x: Int, y: Int, width: Int, height: Int, even: IntArray, odd: IntArray): ByteArray {
require(even.size == 16 && odd.size == 16 && (even + odd).all { it in 0..15 })
fun packed(table: IntArray) = ByteArray(8) { i -> ((table[i * 2] shl 4) or table[i * 2 + 1]).toByte() }
return call(DRAW_OP_REMAP_COLORS) {
writeU16(0)
writeU16(0)
writeU16(x)
writeU16(y)
writeU16(width)
writeU16(height)
writeBytes(table)
writeBytes(packed(even))
writeBytes(packed(odd))
}
}
/** Dim the whole target's light to factor/256 with DimDither's checkerboard. */
fun lut(width: Int, height: Int, factor: Int): ByteArray {
val (even, odd) = DimDither.tables(factor)
return remapColors(0, 0, width, height, even, odd)
}
fun rawImage(width: Int, height: Int, pixels: ByteArray = ByteArray(((width + 1) / 2) * height)): ByteArray {
require(width in 1..640 && height in 1..480 && pixels.size == ((width + 1) / 2) * height)
val result = byteArrayOf(LARGE.toByte()) + word(width) + word(height) + pixels
@@ -0,0 +1,42 @@
package com.faceclaw.app
import kotlin.math.abs
import kotlin.math.pow
/**
* Color-remap tables that dim the display by a light factor using a checkerboard dither.
*
* The panel's response looks roughly linear in light, so level 1 already emits 1/15 of full
* brightness, and remapping each level to a single darker level has too little resolution
* near black. A dimmed level 1 is either unchanged or gone. Instead each level maps to a
* pair of adjacent levels, one for pixels with even x+y and one for odd x+y. That gives
* half-step averages, e.g. level 1 becomes a 1/0 halftone.
*/
object DimDither {
/** Relative light output of each level; replace with measurements if the panel isn't linear. */
private val levelLight = DoubleArray(16) { it.toDouble() }
/** Half-step k shows ceil(k/2) on even pixels and floor(k/2) on odd ones. */
private val halfStepLight = DoubleArray(31) { k -> (levelLight[(k + 1) / 2] + levelLight[k / 2]) / 2 }
/** Comparable perceived lightness (CIE L* is affine in the cube root at these levels). */
private fun lightness(light: Double) = light.pow(1.0 / 3)
/**
* Even- and odd-parity tables scaling light to factor256/256. Each level takes the half-step
* perceptually nearest its dimmed light, never brighter than the original, and a visible
* level keeps at least the 1/0 halftone.
*/
fun tables(factor256: Int): Pair<IntArray, IntArray> {
require(factor256 in 0..256)
val even = IntArray(16)
val odd = IntArray(16)
for (level in 1..15) {
val target = lightness(levelLight[level] * factor256 / 256)
val step = (1..2 * level).minBy { abs(lightness(halfStepLight[it]) - target) }
even[level] = (step + 1) / 2
odd[level] = step / 2
}
return even to odd
}
}
@@ -353,15 +353,17 @@ class DisplayListRenderer(
val y = reader.readU16()
val width = reader.readU16()
val height = reader.readU16()
val table = reader.readBytes(8)
val tables = reader.readBytes(16)
reader.requireDone()
require(width > 0 && height > 0 && x + width <= target.width && y + height <= target.height)
if (!walk.apply) return
for (yy in y until y + height) for (xx in x until x + width) {
if (xx + target.shiftX !in 0 until target.width) continue
val value = target.get(xx + target.shiftX, yy)
val tx = xx + target.shiftX
if (tx !in 0 until target.width) continue
val value = target.get(tx, yy)
val shift = if (value % 2 == 0) 4 else 0
target.put(xx, yy, (table[value / 2].toInt() ushr shift) and 15)
// The even-parity table comes first; parity is of the shifted target pixel.
target.put(xx, yy, (tables[((tx + yy) and 1) * 8 + value / 2].toInt() ushr shift) and 15)
}
}