Parcourir la source

Add image.pixels_to_image helper.

Jeroen van Rijn il y a 1 an
Parent
commit
1a16585b10
1 fichiers modifiés avec 28 ajouts et 1 suppressions
  1. 28 1
      core/image/common.odin

+ 28 - 1
core/image/common.odin

@@ -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`.
@@ -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?