aboutsummaryrefslogtreecommitdiff
path: root/core/image/common.odin
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2024-06-09 16:10:06 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2024-06-09 16:10:06 +0200
commitd2a2c1e74e62ab3fdb11d93f4ef8f74e4727bcda (patch)
treedeaab08106ec3128a7d5decd8d64cef8a750ae8f /core/image/common.odin
parent8fcfd8c506752d3e0d65e4d9ef7856486a65ca5f (diff)
Image: Add improved blending method and test it.
Diffstat (limited to 'core/image/common.odin')
-rw-r--r--core/image/common.odin17
1 files changed, 17 insertions, 0 deletions
diff --git a/core/image/common.odin b/core/image/common.odin
index fe75371b3..15c5bbbce 100644
--- a/core/image/common.odin
+++ b/core/image/common.odin
@@ -42,6 +42,23 @@ GA_Pixel :: [2]u8
G_Pixel_16 :: [1]u16
GA_Pixel_16 :: [2]u16
+blend_single_channel :: #force_inline proc(fg, alpha, bg: $T) -> (res: T) where T == u8 || T == u16 {
+ MAX :: 256 when T == u8 else 65536
+
+ c := u32(fg) * (MAX - u32(alpha)) + u32(bg) * (1 + u32(alpha))
+ return T(c & (MAX - 1))
+}
+
+blend_pixel :: #force_inline proc(fg: [$N]$T, alpha: T, bg: [N]T) -> (res: [N]T) where (T == u8 || T == u16), N >= 1 && N <= 4 {
+ MAX :: 256 when T == u8 else 65536
+
+ r_a := u32(fg) * (MAX - u32(alpha)) + u32(bg) * (1 + u32(alpha))
+
+ return
+}
+
+blend :: proc{blend_single_channel, blend_pixel}
+
Image :: struct {
width: int,
height: int,