mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
Add image.pixels_to_image helper.
This commit is contained in:
+28
-1
@@ -112,7 +112,8 @@ Image_Option:
|
|||||||
|
|
||||||
`.alpha_drop_if_present`
|
`.alpha_drop_if_present`
|
||||||
If the image has an alpha channel, drop it.
|
If the image has an alpha channel, drop it.
|
||||||
You may want to use `.alpha_premultiply` in this case.
|
You may want to use `.alpha_
|
||||||
|
tiply` in this case.
|
||||||
|
|
||||||
NOTE: For PNG, this also skips handling of the tRNS chunk, if present,
|
NOTE: For PNG, this also skips handling of the tRNS chunk, if present,
|
||||||
unless you select `alpha_premultiply`.
|
unless you select `alpha_premultiply`.
|
||||||
@@ -587,6 +588,32 @@ Channel :: enum u8 {
|
|||||||
A = 4,
|
A = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Take a slice of pixels (`[]RGBA_Pixel`, etc), and return an `Image`
|
||||||
|
// Don't call `destroy` on the resulting `Image`. Instead, delete the original `pixels` slice.
|
||||||
|
pixels_to_image :: proc(pixels: [][$N]$E, width: int, height: int) -> (img: Image, ok: bool) where E == u8 || E == u16, N >= 1 && N <= 4 {
|
||||||
|
if len(pixels) != width * height {
|
||||||
|
return {}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
img.height = height
|
||||||
|
img.width = width
|
||||||
|
img.depth = 8 when E == u8 else 16
|
||||||
|
img.channels = N
|
||||||
|
|
||||||
|
s := transmute(runtime.Raw_Slice)pixels
|
||||||
|
d := runtime.Raw_Dynamic_Array{
|
||||||
|
data = s.data,
|
||||||
|
len = s.len * size_of(E) * N,
|
||||||
|
cap = s.len * size_of(E) * N,
|
||||||
|
allocator = runtime.nil_allocator(),
|
||||||
|
}
|
||||||
|
img.pixels = bytes.Buffer{
|
||||||
|
buf = transmute([dynamic]u8)d,
|
||||||
|
}
|
||||||
|
|
||||||
|
return img, true
|
||||||
|
}
|
||||||
|
|
||||||
// When you have an RGB(A) image, but want a particular channel.
|
// When you have an RGB(A) image, but want a particular channel.
|
||||||
return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) {
|
return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) {
|
||||||
// Were we actually given a valid image?
|
// Were we actually given a valid image?
|
||||||
|
|||||||
Reference in New Issue
Block a user