about summary refs log tree commit diff stats
path: root/img.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-07-11 09:25:24 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-07-11 09:25:24 -0700
commit70a03be0d0c8c30ea9ba81fd7d5bef393c9d6d8d (patch)
tree4612cc8468d6cfd1934e99ef2ead22eac8a3d696 /img.mu
parent7e21abe0c917d91b8f5c196f7ce69c5b14a15f3f (diff)
downloadmu-70a03be0d0c8c30ea9ba81fd7d5bef393c9d6d8d.tar.gz
.
Extract a helper.
Diffstat (limited to 'img.mu')
-rw-r--r--img.mu72
1 files changed, 39 insertions, 33 deletions
diff --git a/img.mu b/img.mu
index 2d540f43..7e306bb4 100644
--- a/img.mu
+++ b/img.mu
@@ -486,39 +486,10 @@ fn _dither-pgm-unordered src: (addr array byte), width: int, height: int, buf: (
     {
       compare x, width
       break-if->=
-      var error/ebx: int <- _read-dithering-error buf, x, y, width
-      # error += src_color << 16
-      var curr/eax: byte <- _read-pgm-buffer src, x, y, width
-      var curr-int/eax: int <- copy curr
-#?       draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, curr-int, 2/fg 0/bg
-      curr-int <- shift-left 0x10  # we have 32 bits; we'll use 16 bits for the fraction and leave 8 for unanticipated overflow
-      error <- add curr-int
-      # tmp = max(error, 0)
-      var tmp/eax: int <- copy error
-      {
-        compare tmp, 0
-        break-if->=
-        tmp <- copy 0
-      }
-      # round tmp to nearest multiple of 0x100000
-      {
-        var tmp2/ecx: int <- copy tmp
-        tmp2 <- and   0xfffff
-        compare tmp2, 0x80000
-        break-if-<
-        tmp <- add    0x80000
-      }
-      tmp <- and 0xf00000
-      # error -= tmp
-      error <- subtract tmp
-      # color = tmp >> 20 + 16
-      var color/eax: int <- copy tmp
-      color <- shift-right-signed 0x14
-      color <- add 0x10
-#?       draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, color, 3/fg 0/bg
-#?       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " ", 7/fg 0/bg
-      var color-byte/eax: byte <- copy-byte color
-      _write-raw-buffer dest, x, y, width, color-byte
+      var color/eax: byte <- _read-pgm-buffer src, x, y, width
+      var error/ebx: int <- copy 0
+      color, error <- compute-color-and-error buf, color, x, y, width
+      _write-raw-buffer dest, x, y, width, color
       _diffuse-dithering-error-floyd-steinberg buf, x, y, width, height, error
       x <- increment
       loop
@@ -529,6 +500,41 @@ fn _dither-pgm-unordered src: (addr array byte), width: int, height: int, buf: (
   }
 }
 
+fn compute-color-and-error buf: (addr array int), initial-color: byte, x: int, y: int, width: int -> _/eax: byte, _/ebx: int {
+  var error/ebx: int <- _read-dithering-error buf, x, y, width
+  # error += initial-color << 16
+  var color-int/eax: int <- copy initial-color
+#?       draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, initial-color-int, 2/fg 0/bg
+  color-int <- shift-left 0x10  # we have 32 bits; we'll use 16 bits for the fraction and leave 8 for unanticipated overflow
+  error <- add color-int
+  # tmp = max(error, 0)
+  var tmp/eax: int <- copy error
+  {
+    compare tmp, 0
+    break-if->=
+    tmp <- copy 0
+  }
+  # round tmp to nearest multiple of 0x100000
+  {
+    var tmp2/ecx: int <- copy tmp
+    tmp2 <- and   0xfffff
+    compare tmp2, 0x80000
+    break-if-<
+    tmp <- add    0x80000
+  }
+  tmp <- and 0xf00000
+  # error -= tmp
+  error <- subtract tmp
+  # color = tmp >> 20 + 16
+  var color/eax: int <- copy tmp
+  color <- shift-right-signed 0x14
+  color <- add 0x10
+#?       draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, color, 3/fg 0/bg
+#?       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " ", 7/fg 0/bg
+  var color-byte/eax: byte <- copy-byte color
+  return color-byte, error
+}
+
 # Use Floyd-Steinberg algorithm for turning an image of greyscale pixels into
 # one of pure black or white pixels.
 #