Golang image.NRGBA and NRGBA64.Set and SetNRGBA(64) functions example

package image

Golang image.NRGBA and NRGBA64.Set and SetNRGBA(64) functions usage example

 package main

 import (
 "fmt"
 "image"
 "image/color" // for color.NRGBA or color.NRGBA64
 "image/png"
 "os"
 )

 func init() {
 // without this register .. At(), Bounds() functions will
 // caused memory pointer error!!
 image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
 }

 func main() {
 imgfile, err := os.Open("./img.png")

 if err != nil {
 fmt.Println("img.png file not found!")
 os.Exit(1)
 }

 defer imgfile.Close()

 img, _, err := image.Decode(imgfile)

 bounds := img.Bounds()

 canvas := image.NewNRGBA(bounds) // or NewNRGBA64

 canvas.Set(100, 100, image.Transparent)

 // http://golang.org/pkg/image/color/#NRGBA
 // NRGBA represents a non-alpha-premultiplied 32-bit color.

 x := 512
 y := 512
 r := uint8(255) // type NRGBA or uint16 for type NRGBA64

 g := uint8(255) // type NRGBA or uint16 for type NRGBA64

 b := uint8(255) // type NRGBA or uint16 for type NRGBA64

 a := uint8(255) // type NRGBA or uint16 for type NRGBA64


 canvas.SetNRGBA(x, y, color.NRGBA{r,g,b,a})

 }

References :

http://golang.org/pkg/image/#NRGBA.Set

http://golang.org/pkg/image/#NRGBA.SetNRGBA

http://golang.org/pkg/image/#NRGBA64.Set

http://golang.org/pkg/image/#NRGBA64.SetNRGBA64

Advertisement