about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--505colors.mu46
-rw-r--r--img.mu17
2 files changed, 50 insertions, 13 deletions
diff --git a/505colors.mu b/505colors.mu
index d4c297b3..b45b33e0 100644
--- a/505colors.mu
+++ b/505colors.mu
@@ -1,3 +1,49 @@
+fn nearest-color-euclidean r: int, g: int, b: int -> _/eax: int {
+  var result/edi: int <- copy 0x100/invalid
+  var max-distance/esi: int <- copy 0x30000/max  # 3 * 0x100*0x100
+  var r2/ecx: int <- copy 0
+  var g2/edx: int <- copy 0
+  var b2/ebx: int <- copy 0
+  var color/eax: int <- copy 0
+  {
+    compare color, 0x100
+    break-if->=
+    $nearest-color-euclidean:body: {
+      r2, g2, b2 <- color-rgb color
+      {
+        var curr-distance/eax: int <- euclidean-distance-squared r, g, b, r2, g2, b2
+        compare curr-distance, max-distance
+        break-if->= $nearest-color-euclidean:body
+        max-distance <- copy curr-distance
+      }
+      result <- copy color
+    }
+    color <- increment
+    loop
+  }
+  return result
+}
+
+fn euclidean-distance-squared r1: int, g1: int, b1: int, r2: int, g2: int, b2: int -> _/eax: int {
+  var result/edi: int <- copy 0
+  # red
+  var tmp/eax: int <- copy r1
+  tmp <- subtract r2
+  tmp <- multiply tmp
+  result <- add tmp
+  # green
+  tmp <- copy g1
+  tmp <- subtract g2
+  tmp <- multiply tmp
+  result <- add tmp
+  # blue
+  tmp <- copy b1
+  tmp <- subtract b2
+  tmp <- multiply tmp
+  result <- add tmp
+  return result
+}
+
 # Hue/saturation/luminance for an rgb triple.
 # rgb are in [0, 256)
 # hsl are also returned in [0, 256)
diff --git a/img.mu b/img.mu
index cca03989..2016f571 100644
--- a/img.mu
+++ b/img.mu
@@ -27,7 +27,7 @@ fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk)
   var img-storage: image
   var img/esi: (addr image) <- address img-storage
   load-image img, data-disk
-  render-image screen, img, 0/x, 0/y, 0x400/width, 0x400/height
+  render-image screen, img, 0/x, 0/y, 0x100/width, 0x100/height
 #?   render-image screen, img, 0x20/x, 0x180/y, 0x12c/width=300, 0xc8/height=200
 #?   render-pgm-image screen, img, 0x220/x, 0x180/y, 0x12c/width=300, 0xc8/height=200
 #?   render-image screen, img, 0x320/x, 0x280/y, 0x60/width=96, 0x1c/height=28
@@ -833,22 +833,13 @@ fn render-ppm-image screen: (addr screen), _img: (addr image), xmin: int, ymin:
         copy-to b, src
       }
       idx <- increment
-      # color-int = nearest-hsl(r, g, b)
-      var color-int: int
-      {
-        var h/ecx: int <- copy 0
-        var s/edx: int <- copy 0
-        var l/ebx: int <- copy 0
-        h, s, l <- hsl r, g, b
-        var tmp/eax: int <- nearest-color-euclidean-hsl h, s, l
-        copy-to color-int, tmp
-      }
-      #
+      # plot nearest color
+      var color/eax: int <- nearest-color-euclidean r, g, b
       var screenx/ecx: int <- convert x
       screenx <- add xmin
       var screeny/edx: int <- convert y
       screeny <- add ymin
-      pixel screen, screenx, screeny, color-int
+      pixel screen, screenx, screeny, color
       x <- add one-f
       loop
     }