Golang image.Alpha.PixOffset function example

package image

 func (p *Alpha) PixOffset(x, y int) int
  

PixOffset returns the index of the first element of Pix that corresponds to the pixel at (x, y).

Golang image.Alpha.PixOffset function usage example

 package main

 import (
 "fmt"
 "image"
 "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.NewAlpha(bounds)

 poffset := canvas.PixOffset(100, 100)
 fmt.Println(poffset)

 poffset = canvas.PixOffset(10, 10)
 fmt.Println(poffset)
 }

Sample output (depending on your image file) :

60100

6010

Reference :

http://golang.org/pkg/image/#Alpha.PixOffset

Advertisement