summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-02-06 20:18:38 -0500
committerJune McEnroe <june@causal.agency>2018-02-06 20:18:38 -0500
commit45a9d3baeeb5424d9d23044fb9661dce286fc328 (patch)
tree4387ca4598f030c1a2ba14aca36be244b54d1ee4 /bin
parentRename gfxx space indexed and add palette sampling (diff)
downloadsrc-45a9d3baeeb5424d9d23044fb9661dce286fc328.tar.gz
src-45a9d3baeeb5424d9d23044fb9661dce286fc328.zip
Replace gfxx SCALE macro with interp function
Short-circuits for b = 8.
Diffstat (limited to '')
-rw-r--r--bin/gfxx.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/bin/gfxx.c b/bin/gfxx.c
index 6ad57e0a..184699b8 100644
--- a/bin/gfxx.c
+++ b/bin/gfxx.c
@@ -29,7 +29,6 @@
 #define RGB(r,g,b) ((uint32_t)(r) << 16 | (uint32_t)(g) << 8 | (uint32_t)(b))
 #define GRAY(n)    RGB(n, n, n)
 #define MASK(b)    ((1 << (b)) - 1)
-#define SCALE(b,n) ((b) ? (uint8_t)(255 * (uint32_t)(n) / MASK(b)) : 0)
 
 static enum {
     COLOR_INDEXED,
@@ -201,6 +200,12 @@ static void put(const struct Iter *it, uint32_t p) {
     }
 }
 
+static uint8_t interp(uint8_t b, uint32_t n) {
+    if (b == 8) return n;
+    if (b == 0) return 0;
+    return n * MASK(8) / MASK(b);
+}
+
 static uint32_t interpolate(uint32_t n) {
     uint32_t r, g, b;
     if (bitOrder == ENDIAN_LITTLE) {
@@ -212,7 +217,7 @@ static uint32_t interpolate(uint32_t n) {
         g = (n >>= bits[1]) & MASK(bits[2]);
         b = (n >>= bits[2]) & MASK(bits[3]);
     }
-    return RGB(SCALE(bits[1], r), SCALE(bits[2], g), SCALE(bits[3], b));
+    return RGB(interp(bits[1], r), interp(bits[2], g), interp(bits[3], b));
 }
 
 static void drawBits(struct Iter *it) {
@@ -227,7 +232,7 @@ static void drawBits(struct Iter *it) {
             if (space == COLOR_INDEXED) {
                 put(it, palette[n]);
             } else if (space == COLOR_GRAYSCALE) {
-                put(it, GRAY(SCALE(BITS_COLOR, n)));
+                put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR))));
             } else if (space == COLOR_RGB) {
                 put(it, interpolate(n));
             }
@@ -247,7 +252,7 @@ static void drawBytes(struct Iter *it) {
         if (space == COLOR_INDEXED) {
             put(it, palette[n & 0xFF]);
         } else if (space == COLOR_GRAYSCALE) {
-            put(it, GRAY(SCALE(BITS_COLOR, n & MASK(BITS_COLOR))));
+            put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR))));
         } else if (space == COLOR_RGB) {
             put(it, interpolate(n));
         }