summary refs log tree commit diff
path: root/bin/gfxx.c
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2018-02-06 20:18:38 -0500
committerJune McEnroe <programble@gmail.com>2018-02-06 20:18:38 -0500
commit27d47977f145c8af24d1e4ccf86a26658f7f75eb (patch)
treee576d73c47e117de59cb0cb4907d272a22890f41 /bin/gfxx.c
parentRename gfxx space indexed and add palette sampling (diff)
downloadsrc-27d47977f145c8af24d1e4ccf86a26658f7f75eb.tar.gz
src-27d47977f145c8af24d1e4ccf86a26658f7f75eb.zip
Replace gfxx SCALE macro with interp function
Short-circuits for b = 8.
Diffstat (limited to 'bin/gfxx.c')
-rw-r--r--bin/gfxx.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/bin/gfxx.c b/bin/gfxx.c
index 509dd533..8a124b3a 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));
         }