Skip to content

Commit

Permalink
Add image.pixels_to_image helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelimion committed Aug 2, 2024
1 parent 500c117 commit 1a16585
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion core/image/common.odin
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ Image_Option:
`.alpha_drop_if_present`
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,
unless you select `alpha_premultiply`.
Expand Down Expand Up @@ -587,6 +588,32 @@ Channel :: enum u8 {
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.
return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) {
// Were we actually given a valid image?
Expand Down

0 comments on commit 1a16585

Please sign in to comment.