Golang image.PalettedImage type example

package image

PalettedImage is an image whose colors may come from a limited palette. If m is a PalettedImage and m.ColorModel() returns a PalettedColorModel p, then m.At(x, y) should be equivalent to p[m.ColorIndexAt(x, y)]. If m's color model is not a PalettedColorModel, then ColorIndexAt's behavior is undefined.

Golang image.PalettedImage type usage example

 package main

  import (
 "fmt"
 "image"
 "image/png"
 "image/color"
 "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)

 var pal color.Palette

 if _, ok := img.(image.PalettedImage); ok {
 pal, _ = img.ColorModel().(color.Palette)

 // test
 fmt.Println(pal)
 }

 // otherwise image is not PalettedImage type
 fmt.Println("img.png is not PalettedImage type")


 }

Sample output (depending on input image ) :

img.png is not PalettedImage type

Reference :

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

Advertisement