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
+10.5k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+34.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+5.2k Golang : PGX CopyFrom to insert rows into Postgres database
+35.3k Golang : Strip slashes from string example
+15.2k Golang : Get timezone offset from date or timestamp
+18.6k Golang : Generate thumbnails from images
+11.2k Golang : Fix - does not implement sort.Interface (missing Len method)
+15.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+23.5k Golang : Check if element exist in map
+7.8k Golang : Reverse a string with unicode
+29.3k Golang : Save map/struct to JSON or XML file
+13.1k Golang : How to get a user home directory path?