Add ToImage/FromImage helpers for Go image.Image interface

This commit is contained in:
Milan Nikolic 2017-10-27 13:59:05 +02:00
parent bc794349c5
commit fb585b0065
2 changed files with 55 additions and 19 deletions

View file

@ -24,33 +24,37 @@ func main() {
raylib.TraceLog(raylib.LogError, err.Error()) raylib.TraceLog(raylib.LogError, err.Error())
} }
// Get image size // Create raylib.Image from Go image.Image and create texture
size := img.Bounds().Size() im := raylib.NewImageFromImage(img)
texture := raylib.LoadTextureFromImage(im)
// 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)
// Unload CPU (RAM) image data // Unload CPU (RAM) image data
raylib.UnloadImage(imEx) raylib.UnloadImage(im)
raylib.SetTargetFPS(60) raylib.SetTargetFPS(60)
for !raylib.WindowShouldClose() { 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.BeginDrawing()
raylib.ClearBackground(raylib.RayWhite) 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.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) raylib.DrawText("this IS a texture loaded from an image.Image!", 285, 370, 10, raylib.Gray)

View file

@ -5,7 +5,11 @@ package raylib
#include <stdlib.h> #include <stdlib.h>
*/ */
import "C" import "C"
import "unsafe"
import (
"image"
"unsafe"
)
// TextureFormat - Texture format // TextureFormat - Texture format
type TextureFormat int32 type TextureFormat int32
@ -101,6 +105,18 @@ func (i *Image) cptr() *C.Image {
return (*C.Image)(unsafe.Pointer(i)) 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 // NewImage - Returns new Image
func NewImage(data unsafe.Pointer, width, height, mipmaps int32, format TextureFormat) *Image { func NewImage(data unsafe.Pointer, width, height, mipmaps int32, format TextureFormat) *Image {
return &Image{data, width, height, mipmaps, format} return &Image{data, width, height, mipmaps, format}
@ -111,6 +127,22 @@ func NewImageFromPointer(ptr unsafe.Pointer) *Image {
return (*Image)(ptr) 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) // Texture2D type, bpp always RGBA (32bit)
// NOTE: Data stored in GPU memory // NOTE: Data stored in GPU memory
type Texture2D struct { type Texture2D struct {