Skip to content

Commit 1a16585

Browse files
committed
Add image.pixels_to_image helper.
1 parent 500c117 commit 1a16585

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

core/image/common.odin

+28-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ Image_Option:
112112
113113
`.alpha_drop_if_present`
114114
If the image has an alpha channel, drop it.
115-
You may want to use `.alpha_premultiply` in this case.
115+
You may want to use `.alpha_
116+
tiply` in this case.
116117
117118
NOTE: For PNG, this also skips handling of the tRNS chunk, if present,
118119
unless you select `alpha_premultiply`.
@@ -587,6 +588,32 @@ Channel :: enum u8 {
587588
A = 4,
588589
}
589590

591+
// Take a slice of pixels (`[]RGBA_Pixel`, etc), and return an `Image`
592+
// Don't call `destroy` on the resulting `Image`. Instead, delete the original `pixels` slice.
593+
pixels_to_image :: proc(pixels: [][$N]$E, width: int, height: int) -> (img: Image, ok: bool) where E == u8 || E == u16, N >= 1 && N <= 4 {
594+
if len(pixels) != width * height {
595+
return {}, false
596+
}
597+
598+
img.height = height
599+
img.width = width
600+
img.depth = 8 when E == u8 else 16
601+
img.channels = N
602+
603+
s := transmute(runtime.Raw_Slice)pixels
604+
d := runtime.Raw_Dynamic_Array{
605+
data = s.data,
606+
len = s.len * size_of(E) * N,
607+
cap = s.len * size_of(E) * N,
608+
allocator = runtime.nil_allocator(),
609+
}
610+
img.pixels = bytes.Buffer{
611+
buf = transmute([dynamic]u8)d,
612+
}
613+
614+
return img, true
615+
}
616+
590617
// When you have an RGB(A) image, but want a particular channel.
591618
return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) {
592619
// Were we actually given a valid image?

0 commit comments

Comments
 (0)