Move SSD RGBA packing to typed Kotlin

ober

301ec9bb002f28f3f4e0ea03aa8c42ae475a6a2f

diff --git a/.build.yml b/.build.yml
index b222cf6..5abf74b 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#b0c66c1bb8042f3c48fcb3eae95222bc50d0a410"
+  - "https://git.sr.ht/~lisp/jerboa#bc870112a90a626c20a6795579d205fed1b59518"
   # 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)" = b0c66c1bb8042f3c48fcb3eae95222bc50d0a410
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = cde344bb6e2cda51bf460b54492ee3f42d953850
+      test "$(git -C ../jerboa rev-parse HEAD)" = bc870112a90a626c20a6795579d205fed1b59518
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = e510a741cc68717b998f02980153702e61bda563
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index 6da1126..c861938 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": "b0c66c1bb8042f3c48fcb3eae95222bc50d0a410",
-    "tree": "cde344bb6e2cda51bf460b54492ee3f42d953850"
+    "commit": "bc870112a90a626c20a6795579d205fed1b59518",
+    "tree": "e510a741cc68717b998f02980153702e61bda563"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 03e4828..7925f02 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=b0c66c1bb8042f3c48fcb3eae95222bc50d0a410 # gitsafe:ignore
-jerboa_tree=cde344bb6e2cda51bf460b54492ee3f42d953850 # gitsafe:ignore
+jerboa_commit=bc870112a90a626c20a6795579d205fed1b59518 # gitsafe:ignore
+jerboa_tree=e510a741cc68717b998f02980153702e61bda563 # 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 f25473e..55a4954 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -721,7 +721,7 @@
 
     (typed-kotlin-file "com/sfb/ssdreview/VisionPixels.kt"
       (typed-library (com sfb ssdreview)
-        (export grayscale isColorMask looksLikeInk looksLikeBoxFill)
+        (export grayscale pixelsToRgba isColorMask looksLikeInk looksLikeBoxFill)
         (type Int32)
         (type IntArray)
         (def (pixelRed (pixel : Int32)) : Int32
@@ -734,6 +734,10 @@
             (int32 255)))
         (def (pixelBlue (pixel : Int32)) : Int32
           (bitwise-and pixel (int32 255)))
+        (def (pixelAlpha (pixel : Int32)) : Int32
+          (bitwise-and
+            (bitwise-arithmetic-shift-right pixel (int32 24))
+            (int32 255)))
         (def (pixel-max-int32 (a : Int32) (b : Int32)) : Int32
           (if (> a b) a b))
         (def (pixel-min-int32 (a : Int32) (b : Int32)) : Int32
@@ -779,6 +783,22 @@
                         (* (int32 150) (pixelGreen pixel)))
                      (* (int32 29) (pixelBlue pixel)))
                   (int32 8))))))
+        (def (pixelRgbaByte (pixel : Int32) (channel : Int32)) : Int32
+          (if (= channel (int32 0))
+            (pixelRed pixel)
+            (if (= channel (int32 1))
+              (pixelGreen pixel)
+              (if (= channel (int32 2))
+                (pixelBlue pixel)
+                (pixelAlpha pixel)))))
+        (def (pixelsToRgba (pixels : IntArray)) : Bytes
+          (bytes-build
+            (* (int-array-length pixels) (int32 4))
+            (index
+              (let ((byteIndex (int32 index)))
+                (pixelRgbaByte
+                  (int-array-ref pixels (/ byteIndex (int32 4)))
+                  (mod byteIndex (int32 4)))))))
         (def (looksLikeBoxFill (pixel : Int32)) : Bool
           (let ((maxC (pixelMaxChannel pixel))
                 (minC (pixelMinChannel pixel))
@@ -3613,16 +3633,7 @@
        "        val height = bitmap.height"
        "        val pixels = IntArray(width * height)"
        "        bitmap.getPixels(pixels, 0, width, 0, 0, width, height)"
-       "        val out = ByteArray(width * height * 4)"
-       "        for (index in pixels.indices) {"
-       "            val pixel = pixels[index]"
-       "            val offset = index * 4"
-       "            out[offset] = ((pixel shr 16) and 0xff).toByte()"
-       "            out[offset + 1] = ((pixel shr 8) and 0xff).toByte()"
-       "            out[offset + 2] = (pixel and 0xff).toByte()"
-       "            out[offset + 3] = ((pixel ushr 24) and 0xff).toByte()"
-       "        }"
-       "        return out"
+       "        return pixelsToRgba(pixels)"
        "    }"
        ""
        "    private fun parseCells(raw: JSONArray?): MutableList<SsdCell> {"