mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-13 01:21:38 -07:00
Add image.premultiply_alpha helper.
This commit is contained in:
@@ -1320,6 +1320,42 @@ blend_pixel :: #force_inline proc(fg: [$N]$T, alpha: T, bg: [N]T) -> (res: [N]T)
|
||||
}
|
||||
blend :: proc{blend_single_channel, blend_pixel}
|
||||
|
||||
// For all pixels of the image, multiplies R, G and B by Alpha. This is useful mainly for games rendering anti-aliased transparent sprites.
|
||||
// Grayscale with alpha images are supported as well.
|
||||
// Note that some image formats like QOI explicitly do NOT support premultiplied alpha, so you will end up with a non-standard file.
|
||||
premultiply_alpha :: proc(img: ^Image) -> (ok: bool) {
|
||||
switch {
|
||||
case img.channels == 2 && img.depth == 8:
|
||||
pixels := mem.slice_data_cast([]GA_Pixel, img.pixels.buf[:])
|
||||
for &pixel in pixels {
|
||||
pixel.r = u8(u32(pixel.r) * u32(pixel.g) / 0xFF)
|
||||
}
|
||||
return true
|
||||
case img.channels == 2 && img.depth == 16:
|
||||
pixels := mem.slice_data_cast([]GA_Pixel_16, img.pixels.buf[:])
|
||||
for &pixel in pixels {
|
||||
pixel.r = u16(u32(pixel.r) * u32(pixel.g) / 0xFFFF)
|
||||
}
|
||||
return true
|
||||
case img.channels == 4 && img.depth == 8:
|
||||
pixels := mem.slice_data_cast([]RGBA_Pixel, img.pixels.buf[:])
|
||||
for &pixel in pixels {
|
||||
pixel.r = u8(u32(pixel.r) * u32(pixel.a) / 0xFF)
|
||||
pixel.g = u8(u32(pixel.g) * u32(pixel.a) / 0xFF)
|
||||
pixel.b = u8(u32(pixel.b) * u32(pixel.a) / 0xFF)
|
||||
}
|
||||
return true
|
||||
case img.channels == 4 && img.depth == 16:
|
||||
pixels := mem.slice_data_cast([]RGBA_Pixel_16, img.pixels.buf[:])
|
||||
for &pixel in pixels {
|
||||
pixel.r = u16(u32(pixel.r) * u32(pixel.a) / 0xFFFF)
|
||||
pixel.g = u16(u32(pixel.g) * u32(pixel.a) / 0xFFFF)
|
||||
pixel.b = u16(u32(pixel.b) * u32(pixel.a) / 0xFFFF)
|
||||
}
|
||||
return true
|
||||
case: return false
|
||||
}
|
||||
}
|
||||
|
||||
// Replicates grayscale values into RGB(A) 8- or 16-bit images as appropriate.
|
||||
// Returns early with `false` if already an RGB(A) image.
|
||||
|
||||
Reference in New Issue
Block a user