Golang image.NRGBA and NRGBA64 types examples

package image

NRGBA is an in-memory image whose At method returns color.NRGBA values.

NRGBA64 is an in-memory image whose At method returns color.NRGBA64 values.

Golang image.NRGBA and NRGBA64 types usage examples

Example 1: (taken https://github.com/disintegration/imaging/blob/master/tools.go )

 // Crop cuts out a rectangular region with the specified bounds
 // from the image and returns the cropped image.
 func Crop(img image.Image, rect image.Rectangle) *image.NRGBA {
  src := toNRGBA(img)
  srcRect := rect.Sub(img.Bounds().Min)
  sub := src.SubImage(srcRect)
  return Clone(sub) // New image Bounds().Min point will be (0, 0)
 }

Example 2:

 func getSubImage(src image.Image, rect image.Rectangle) image.Image {
  if img, ok := src.(*image.NRGBA); ok {
 return img.SubImage(rect)
  }
  if img, ok := src.(*image.NRGBA64); ok {
 return img.SubImage(rect)
  }
 ...

References :

https://github.com/disintegration/imaging/blob/master/tools.go

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

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

Advertisement