Golang image.NRGBA.PixOffset function example
package image
func (p *NRGBA) 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.NRGBA.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.NewNRGBA(bounds) // or NewNRGBA64
poffset := canvas.PixOffset(100, 100)
fmt.Println(poffset)
poffset = canvas.PixOffset(10, 10)
fmt.Println(poffset)
}
Sample output (depending on input image )
240400
24040
Reference :
Advertisement
Something interesting
Tutorials
+17.5k Golang : Clone with pointer and modify value
+6k Golang : Compound interest over time example
+9.9k Golang : Check if user agent is a robot or crawler example
+7.9k Swift : Convert (cast) String to Float
+16.7k Golang : Gzip file example
+16.5k Golang : Check if a string contains multiple sub-strings in []string?
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+7.5k Golang : How to handle file size larger than available memory panic issue
+11k Golang : Generate random elements without repetition or duplicate
+17k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+5.8k Linux : Disable and enable IPv4 forwarding
+4.7k Javascript : Access JSON data example