Move SSD grayscale helper to typed Kotlin

ober

4d411199b2270c802414aed2bafc83f999989536

diff --git a/.build.yml b/.build.yml
index 76f119c..2474009 100644
--- a/.build.yml
+++ b/.build.yml
@@ -5,7 +5,7 @@ packages:
   - make=4.4.1-r4
 sources:
   # Build dependency: full immutable commit, mirrored in dependencies.lock.json.
-  - "https://git.sr.ht/~lisp/jerboa#b461a2e2042f0f7ec9621a8c8f7d4997b7f875ac"
+  - "https://git.sr.ht/~lisp/jerboa#f4a8b5bf5b2add10b3350766671a25742d25da0a"
   # The second source is the build subject selected by the SourceHut submitter.
   - https://git.sr.ht/~lisp/jerboa-android
 tasks:
@@ -14,6 +14,6 @@ tasks:
       test "$(apk info -v chez-scheme)" = chez-scheme-10.3.0-r2
       test "$(apk info -v git)" = git-2.54.0-r0
       test "$(apk info -v make)" = make-4.4.1-r4
-      test "$(git -C ../jerboa rev-parse HEAD)" = b461a2e2042f0f7ec9621a8c8f7d4997b7f875ac
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = 306cb8d6e192e79f5e78b673efa4eedc04b3c244
+      test "$(git -C ../jerboa rev-parse HEAD)" = f4a8b5bf5b2add10b3350766671a25742d25da0a
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = a44fa3d5a701ba78c4eaffb78fbd68c59ee34792
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index 653043c..03c047e 100644
--- a/dependencies.lock.json
+++ b/dependencies.lock.json
@@ -11,8 +11,8 @@
   "generator_runtime": {
     "name": "jerboa",
     "repository": "https://git.sr.ht/~lisp/jerboa",
-    "commit": "b461a2e2042f0f7ec9621a8c8f7d4997b7f875ac",
-    "tree": "306cb8d6e192e79f5e78b673efa4eedc04b3c244"
+    "commit": "f4a8b5bf5b2add10b3350766671a25742d25da0a",
+    "tree": "a44fa3d5a701ba78c4eaffb78fbd68c59ee34792"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 59ce214..d234f3b 100755
--- a/scripts/verify-supply-chain.sh
+++ b/scripts/verify-supply-chain.sh
@@ -3,8 +3,8 @@ set -eu
 
 repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd -P)
 lock="$repo/dependencies.lock.json"
-jerboa_commit=b461a2e2042f0f7ec9621a8c8f7d4997b7f875ac # gitsafe:ignore
-jerboa_tree=306cb8d6e192e79f5e78b673efa4eedc04b3c244 # gitsafe:ignore
+jerboa_commit=f4a8b5bf5b2add10b3350766671a25742d25da0a # gitsafe:ignore
+jerboa_tree=a44fa3d5a701ba78c4eaffb78fbd68c59ee34792 # gitsafe:ignore
 gradle_sha=20f1b1176237254a6fc204d8434196fa11a4cfb387567519c61556e8710aed78
 jdk_macos_sha=8fa1eff40bb637a33613b2ccb8b12c70dc3661cc22cf8e784943715769a05336
 jdk_linux_sha=d8afc263758141a66e0e3aafc321e783f7016696f4eaea067d340a269037d331
diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 0167b3b..4bb426c 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -578,8 +578,9 @@
 
     (typed-kotlin-file "com/sfb/ssdreview/VisionPixels.kt"
       (typed-library (com sfb ssdreview)
-        (export isColorMask looksLikeInk looksLikeBoxFill)
+        (export grayscale isColorMask looksLikeInk looksLikeBoxFill)
         (type Int32)
+        (type IntArray)
         (def (pixelRed (pixel : Int32)) : Int32
           (bitwise-and
             (bitwise-arithmetic-shift-right pixel (int32 16))
@@ -625,6 +626,16 @@
             (or (< lum 226.0)
                 (and (> (- maxC minC) (int32 28))
                      (< lum 248.0)))))
+        (def (grayscale (pixels : IntArray)) : IntArray
+          (int-array-build
+            (int-array-length pixels)
+            (index
+              (let ((pixel (int-array-ref pixels index)))
+                (bitwise-arithmetic-shift-right
+                  (+ (+ (* (int32 77) (pixelRed pixel))
+                        (* (int32 150) (pixelGreen pixel)))
+                     (* (int32 29) (pixelBlue pixel)))
+                  (int32 8))))))
         (def (looksLikeBoxFill (pixel : Int32)) : Bool
           (let ((maxC (pixelMaxChannel pixel))
                 (minC (pixelMinChannel pixel))
@@ -3151,14 +3162,6 @@
        "        return merged.mapIndexed { index, cell -> cell.copy(id = \"c%04d\".format(index + 1)) }.toMutableList()"
        "    }"
        ""
-       "    private fun grayscale(pixels: IntArray): IntArray = IntArray(pixels.size) { index ->"
-       "        val pixel = pixels[index]"
-       "        val r = (pixel shr 16) and 0xff"
-       "        val g = (pixel shr 8) and 0xff"
-       "        val b = pixel and 0xff"
-       "        ((77 * r + 150 * g + 29 * b) shr 8).coerceIn(0, 255)"
-       "    }"
-       ""
        "    private fun darkSideCoverage(gray: IntArray, width: Int, height: Int, x: Int, y: Int, w: Int, h: Int): FloatArray {"
        "        val band = max(1, min(2, min(w / 2, h / 2)))"
        "        val counts = IntArray(4)"