Golang image.NewAlpha function examples

package image

 func NewAlpha(r Rectangle) *Alpha
  

NewAlpha returns a new Alpha with the given bounds.

Golang image.NewAlpha function usage examples

Example 1:

 package main

 import (
 "fmt"
 "image"
 "image/jpeg"
 "os"
 )

 func init() {
 // damn important or else At(), Bounds() functions will
 // caused memory pointer error!!
 image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig)
 }

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

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

 defer imgfile.Close()

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

 fmt.Println(img.At(10, 10))

 bounds := img.Bounds()

 fmt.Println(bounds)

 canvas := image.NewAlpha(bounds) // <-- here

 // is this image opaque
 op := canvas.Opaque()

 fmt.Println(op)
 }

Sample output :

{255 128 128}

(0,0)-(760,479)

false

Example 2:

 inFile, openErr := os.Open(GIFFileInPath)
 if openErr != nil {
 fmt.Printf("couldn't open %v: %v\n", inPath, openErr)
 os.Exit(1)
 }

 decoded, _, decodeErr := image.Decode(inFile)

 inFile.Close()
 // optimize image, converting colorspace if requested
 bounds := decoded.Bounds()
 switch optimizee := decoded.(type) {
 case *image.Alpha16:
 converted := image.NewAlpha(bounds)
 ...

Reference :

http://golang.org/pkg/image/#NewAlpha

Advertisement