Add ToImage/FromImage helpers for Go image.Image interface
This commit is contained in:
parent
bc794349c5
commit
fb585b0065
2 changed files with 55 additions and 19 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue