aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-10-22 20:32:44 +0200
committerbptato <nincsnevem662@gmail.com>2024-10-22 20:53:33 +0200
commitb0bb61e830e76169f41db186b5946ae6859f4e6a (patch)
tree1ec2d343fe6bfa3f5fdacd1ca0e69cb57c558539
parentb9455ef09f4d33dd9e3cb291ed6e689b51301e22 (diff)
downloadchawan-b0bb61e830e76169f41db186b5946ae6859f4e6a.tar.gz
color: use 64-bit SWAR
I originally left this at 32-bits because I was on a 32-bit arch, but that is no longer the case.
-rw-r--r--adapter/img/sixel.nim4
-rw-r--r--src/types/color.nim49
2 files changed, 17 insertions, 36 deletions
diff --git a/adapter/img/sixel.nim b/adapter/img/sixel.nim
index 7b501d38..49a8a969 100644
--- a/adapter/img/sixel.nim
+++ b/adapter/img/sixel.nim
@@ -168,7 +168,7 @@ proc quantize(img: openArray[RGBAColorBE]; outk: var uint;
for c0 in img:
let c0 = c0.argb()
transparent = transparent or c0.a != 255
- let c = RGBColor(uint32(c0).fastmul(100))
+ let c = RGBColor(ARGBColor(uint32(c0) or 0xFF000000u32).fastmul(100))
K += root.insert(c, trimMap)
while K > palette:
trimMap.trim(K)
@@ -414,7 +414,7 @@ proc encode(os: PosixStream; img: openArray[RGBAColorBE];
for j in 0 ..< realw:
let m = n + offx + j
let c0 = img[m].argb()
- let c1 = ARGBColor(uint32(c0).fastmul1(100))
+ let c1 = c0.fastmul(100)
let c2 = c1.correctDither(j, dither)
if c2.a < 50: # transparent
let diff = (int32(c2.a), 0i32, 0i32, 0i32)
diff --git a/src/types/color.nim b/src/types/color.nim
index 815f769f..7e9d9d8d 100644
--- a/src/types/color.nim
+++ b/src/types/color.nim
@@ -290,41 +290,22 @@ func serialize*(color: ARGBColor): string =
func `$`*(argbcolor: ARGBColor): string =
return argbcolor.serialize()
-# https://arxiv.org/pdf/2202.02864.pdf
-func fastmul*(c, ca: uint32): uint32 =
- let u = c or 0xFF000000u32
- var rb = u and 0x00FF00FFu32
- rb *= ca
- rb += 0x00800080
- rb += (rb shr 8) and 0x00FF00FFu32
- rb = rb and 0xFF00FF00u32
- var ga = (u shr 8) and 0x00FF00FFu32
- ga *= ca
- ga += 0x00800080
- ga += (ga shr 8) and 0x00FF00FFu32
- ga = ga and 0xFF00FF00u32
- return ga or (rb shr 8)
-
-# fastmul, but preserves alpha
-func fastmul1*(c, ca: uint32): uint32 =
- let u = c
- var rb = u and 0x00FF00FFu32
- rb *= ca
- rb += 0x00800080
- rb += (rb shr 8) and 0x00FF00FFu32
- rb = rb and 0xFF00FF00u32
- var ga = (u shr 8) and 0x00FF00FFu32
- ga *= ca
- ga += 0x00800080
- ga += (ga shr 8) and 0x00FF00FFu32
- ga = ga and 0xFF00FF00u32
- return ga or (rb shr 8)
-
-func fastmul1(c: ARGBColor; ca: uint32): ARGBColor =
- return ARGBColor(fastmul1(uint32(c), ca))
+# Divide each component by 255, multiply them by n, and discard the fractions.
+# See https://arxiv.org/pdf/2202.02864.pdf for details.
+func fastmul*(c: ARGBColor; n: uint32): ARGBColor =
+ var c = (uint64(c) shl 24) or uint64(c)
+ c = c and 0x00FF00FF00FF00FFu64
+ c *= n
+ c += 0x80008000800080u64
+ c += (c shr 8) and 0x00FF00FF00FF00FFu64
+ c = c and 0xFF00FF00FF00FF00u64
+ c = (c shr 32) or (c shr 8)
+ return ARGBColor(c)
func premul(c: ARGBColor): ARGBColor =
- return ARGBColor(fastmul(uint32(c), uint32(c.a)))
+ let a = uint32(c.a)
+ let c = ARGBColor(uint32(c) or 0xFF000000u32)
+ return c.fastmul(a)
# This is somewhat faster than floats or a lookup table, and is correct for
# all inputs.
@@ -342,7 +323,7 @@ func blend*(c0, c1: ARGBColor): ARGBColor =
let pc0 = c0.premul()
let pc1 = c1.premul()
let k = 255 - pc1.a
- let mc = fastmul1(pc0, uint32(k))
+ let mc = pc0.fastmul(uint32(k))
let rr = pc1.r + mc.r
let rg = pc1.g + mc.g
let rb = pc1.b + mc.b