aboutsummaryrefslogtreecommitdiff
path: root/core/image/common.odin
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2024-06-09 16:37:27 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2024-06-09 16:37:27 +0200
commit6b88d0a8206e78f2bf84bba39dc2261f906d0a58 (patch)
tree0d2a2cf19b1e6eaf9ffafb0adc791a0bd9b92192 /core/image/common.odin
parentd2a2c1e74e62ab3fdb11d93f4ef8f74e4727bcda (diff)
Use new blend helper
Diffstat (limited to 'core/image/common.odin')
-rw-r--r--core/image/common.odin53
1 files changed, 35 insertions, 18 deletions
diff --git a/core/image/common.odin b/core/image/common.odin
index 15c5bbbce..fed2c1470 100644
--- a/core/image/common.odin
+++ b/core/image/common.odin
@@ -42,23 +42,6 @@ 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,
@@ -1276,6 +1259,40 @@ apply_palette_rgba :: proc(img: ^Image, palette: [256]RGBA_Pixel, allocator := c
}
apply_palette :: proc{apply_palette_rgb, apply_palette_rgba}
+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
+
+ when N == 1 {
+ r := u32(fg.r) * (MAX - u32(alpha)) + u32(bg.r) * (1 + u32(alpha))
+ return {T(r & (MAX - 1))}
+ }
+ when N == 2 {
+ r := u32(fg.r) * (MAX - u32(alpha)) + u32(bg.r) * (1 + u32(alpha))
+ g := u32(fg.g) * (MAX - u32(alpha)) + u32(bg.g) * (1 + u32(alpha))
+ return {T(r & (MAX - 1)), T(g & (MAX - 1))}
+ }
+ when N == 3 || N == 4 {
+ r := u32(fg.r) * (MAX - u32(alpha)) + u32(bg.r) * (1 + u32(alpha))
+ g := u32(fg.g) * (MAX - u32(alpha)) + u32(bg.g) * (1 + u32(alpha))
+ b := u32(fg.b) * (MAX - u32(alpha)) + u32(bg.b) * (1 + u32(alpha))
+
+ when N == 3 {
+ return {T(r & (MAX - 1)), T(g & (MAX - 1)), T(b & (MAX - 1))}
+ } else {
+ return {T(r & (MAX - 1)), T(g & (MAX - 1)), T(b & (MAX - 1)), MAX - 1}
+ }
+ }
+ unreachable()
+}
+blend :: proc{blend_single_channel, blend_pixel}
+
// Replicates grayscale values into RGB(A) 8- or 16-bit images as appropriate.
// Returns early with `false` if already an RGB(A) image.
@@ -1388,4 +1405,4 @@ write_bytes :: proc(buf: ^bytes.Buffer, data: []u8) -> (err: compress.General_Er
return .Resize_Failed
}
return nil
-}
+} \ No newline at end of file