From fb585b0065c8e458b460a3f8d67c71048b9ca5d8 Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Fri, 27 Oct 2017 13:59:05 +0200 Subject: [PATCH] Add ToImage/FromImage helpers for Go image.Image interface --- examples/textures/image_image/main.go | 40 +++++++++++++++------------ raylib/textures.go | 34 ++++++++++++++++++++++- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/examples/textures/image_image/main.go b/examples/textures/image_image/main.go index 272d07b..82d3e5a 100644 --- a/examples/textures/image_image/main.go +++ b/examples/textures/image_image/main.go @@ -24,33 +24,37 @@ func main() { raylib.TraceLog(raylib.LogError, err.Error()) } - // Get image size - size := img.Bounds().Size() - - // Dynamic memory allocation to store pixels data (Color type) - pixels := make([]raylib.Color, size.X*size.Y) - - for y := 0; y < size.Y; y++ { - for x := 0; x < size.X; x++ { - color := img.At(x, y) - r, g, b, a := color.RGBA() - pixels[x+y*size.Y] = raylib.NewColor(uint8(r), uint8(g), uint8(b), uint8(a)) - } - } - - // Load pixels data into an image structure and create texture - imEx := raylib.LoadImageEx(pixels, int32(size.X), int32(size.Y)) - texture := raylib.LoadTextureFromImage(imEx) + // Create raylib.Image from Go image.Image and create texture + im := raylib.NewImageFromImage(img) + texture := raylib.LoadTextureFromImage(im) // Unload CPU (RAM) image data - raylib.UnloadImage(imEx) + raylib.UnloadImage(im) raylib.SetTargetFPS(60) for !raylib.WindowShouldClose() { + if raylib.IsKeyPressed(raylib.KeyS) { + rimg := raylib.GetTextureData(texture) + + f, err := os.Create("image_saved.png") + if err != nil { + raylib.TraceLog(raylib.LogError, err.Error()) + } + + err = png.Encode(f, rimg.ToImage()) + if err != nil { + raylib.TraceLog(raylib.LogError, err.Error()) + } + + f.Close() + } + raylib.BeginDrawing() raylib.ClearBackground(raylib.RayWhite) + + raylib.DrawText("PRESS S TO SAVE IMAGE FROM TEXTURE", 20, 20, 12, raylib.LightGray) raylib.DrawTexture(texture, screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2, raylib.White) raylib.DrawText("this IS a texture loaded from an image.Image!", 285, 370, 10, raylib.Gray) diff --git a/raylib/textures.go b/raylib/textures.go index 41d5714..2fe8ac5 100644 --- a/raylib/textures.go +++ b/raylib/textures.go @@ -5,7 +5,11 @@ package raylib #include */ import "C" -import "unsafe" + +import ( + "image" + "unsafe" +) // TextureFormat - Texture format type TextureFormat int32 @@ -101,6 +105,18 @@ func (i *Image) cptr() *C.Image { return (*C.Image)(unsafe.Pointer(i)) } +// ToImage converts a Image to Go image.Image +func (i *Image) ToImage() image.Image { + img := image.NewRGBA(image.Rect(0, 0, int(i.Width), int(i.Height))) + + // Get pixel data from image (RGBA 32bit) + pixels := GetImageData(i) + + img.Pix = (*[1 << 30]uint8)(pixels)[:] + + return img +} + // NewImage - Returns new Image func NewImage(data unsafe.Pointer, width, height, mipmaps int32, format TextureFormat) *Image { return &Image{data, width, height, mipmaps, format} @@ -111,6 +127,22 @@ func NewImageFromPointer(ptr unsafe.Pointer) *Image { return (*Image)(ptr) } +// NewImageFromImage - Returns new Image from Go image.Image +func NewImageFromImage(img image.Image) *Image { + size := img.Bounds().Size() + pixels := make([]Color, size.X*size.Y) + + for y := 0; y < size.Y; y++ { + for x := 0; x < size.X; x++ { + color := img.At(x, y) + r, g, b, a := color.RGBA() + pixels[x+y*size.Y] = NewColor(uint8(r), uint8(g), uint8(b), uint8(a)) + } + } + + return LoadImageEx(pixels, int32(size.X), int32(size.Y)) +} + // Texture2D type, bpp always RGBA (32bit) // NOTE: Data stored in GPU memory type Texture2D struct {