123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- /*
- Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.).
- This package is based on the standard Go image package and works best along with it.
- Image manipulation functions provided by the package take any image type
- that implements `image.Image` interface as an input, and return a new image of
- `*image.NRGBA` type (32bit RGBA colors, not premultiplied by alpha).
- Imaging package uses parallel goroutines for faster image processing.
- To achieve maximum performance, make sure to allow Go to utilize all CPU cores:
- runtime.GOMAXPROCS(runtime.NumCPU())
- */
- package imaging
- import (
- "errors"
- "image"
- "image/color"
- "image/gif"
- "image/jpeg"
- "image/png"
- "io"
- "os"
- "path/filepath"
- "strings"
- "golang.org/x/image/bmp"
- "golang.org/x/image/tiff"
- )
- type Format int
- const (
- JPEG Format = iota
- PNG
- GIF
- TIFF
- BMP
- )
- func (f Format) String() string {
- switch f {
- case JPEG:
- return "JPEG"
- case PNG:
- return "PNG"
- case GIF:
- return "GIF"
- case TIFF:
- return "TIFF"
- case BMP:
- return "BMP"
- default:
- return "Unsupported"
- }
- }
- var (
- ErrUnsupportedFormat = errors.New("imaging: unsupported image format")
- )
- // Decode reads an image from r.
- func Decode(r io.Reader) (image.Image, error) {
- img, _, err := image.Decode(r)
- if err != nil {
- return nil, err
- }
- return toNRGBA(img), nil
- }
- // Open loads an image from file
- func Open(filename string) (image.Image, error) {
- file, err := os.Open(filename)
- if err != nil {
- return nil, err
- }
- defer file.Close()
- img, err := Decode(file)
- return img, err
- }
- // Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF or BMP).
- func Encode(w io.Writer, img image.Image, format Format) error {
- var err error
- switch format {
- case JPEG:
- var rgba *image.RGBA
- if nrgba, ok := img.(*image.NRGBA); ok {
- if nrgba.Opaque() {
- rgba = &image.RGBA{
- Pix: nrgba.Pix,
- Stride: nrgba.Stride,
- Rect: nrgba.Rect,
- }
- }
- }
- if rgba != nil {
- err = jpeg.Encode(w, rgba, &jpeg.Options{Quality: 95})
- } else {
- err = jpeg.Encode(w, img, &jpeg.Options{Quality: 95})
- }
- case PNG:
- err = png.Encode(w, img)
- case GIF:
- err = gif.Encode(w, img, &gif.Options{NumColors: 256})
- case TIFF:
- err = tiff.Encode(w, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
- case BMP:
- err = bmp.Encode(w, img)
- default:
- err = ErrUnsupportedFormat
- }
- return err
- }
- // Save saves the image to file with the specified filename.
- // The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.
- func Save(img image.Image, filename string) (err error) {
- formats := map[string]Format{
- ".jpg": JPEG,
- ".jpeg": JPEG,
- ".png": PNG,
- ".tif": TIFF,
- ".tiff": TIFF,
- ".bmp": BMP,
- ".gif": GIF,
- }
- ext := strings.ToLower(filepath.Ext(filename))
- f, ok := formats[ext]
- if !ok {
- return ErrUnsupportedFormat
- }
- file, err := os.Create(filename)
- if err != nil {
- return err
- }
- defer file.Close()
- return Encode(file, img, f)
- }
- // New creates a new image with the specified width and height, and fills it with the specified color.
- func New(width, height int, fillColor color.Color) *image.NRGBA {
- if width <= 0 || height <= 0 {
- return &image.NRGBA{}
- }
- dst := image.NewNRGBA(image.Rect(0, 0, width, height))
- c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
- if c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0 {
- return dst
- }
- cs := []uint8{c.R, c.G, c.B, c.A}
- // fill the first row
- for x := 0; x < width; x++ {
- copy(dst.Pix[x*4:(x+1)*4], cs)
- }
- // copy the first row to other rows
- for y := 1; y < height; y++ {
- copy(dst.Pix[y*dst.Stride:y*dst.Stride+width*4], dst.Pix[0:width*4])
- }
- return dst
- }
- // Clone returns a copy of the given image.
- func Clone(img image.Image) *image.NRGBA {
- srcBounds := img.Bounds()
- dstBounds := srcBounds.Sub(srcBounds.Min)
- dst := image.NewNRGBA(dstBounds)
- dstMinX := dstBounds.Min.X
- dstMinY := dstBounds.Min.Y
- srcMinX := srcBounds.Min.X
- srcMinY := srcBounds.Min.Y
- srcMaxX := srcBounds.Max.X
- srcMaxY := srcBounds.Max.Y
- switch src0 := img.(type) {
- case *image.NRGBA:
- rowSize := srcBounds.Dx() * 4
- numRows := srcBounds.Dy()
- i0 := dst.PixOffset(dstMinX, dstMinY)
- j0 := src0.PixOffset(srcMinX, srcMinY)
- di := dst.Stride
- dj := src0.Stride
- for row := 0; row < numRows; row++ {
- copy(dst.Pix[i0:i0+rowSize], src0.Pix[j0:j0+rowSize])
- i0 += di
- j0 += dj
- }
- case *image.NRGBA64:
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- j := src0.PixOffset(x, y)
- dst.Pix[i+0] = src0.Pix[j+0]
- dst.Pix[i+1] = src0.Pix[j+2]
- dst.Pix[i+2] = src0.Pix[j+4]
- dst.Pix[i+3] = src0.Pix[j+6]
- }
- }
- case *image.RGBA:
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- j := src0.PixOffset(x, y)
- a := src0.Pix[j+3]
- dst.Pix[i+3] = a
- switch a {
- case 0:
- dst.Pix[i+0] = 0
- dst.Pix[i+1] = 0
- dst.Pix[i+2] = 0
- case 0xff:
- dst.Pix[i+0] = src0.Pix[j+0]
- dst.Pix[i+1] = src0.Pix[j+1]
- dst.Pix[i+2] = src0.Pix[j+2]
- default:
- dst.Pix[i+0] = uint8(uint16(src0.Pix[j+0]) * 0xff / uint16(a))
- dst.Pix[i+1] = uint8(uint16(src0.Pix[j+1]) * 0xff / uint16(a))
- dst.Pix[i+2] = uint8(uint16(src0.Pix[j+2]) * 0xff / uint16(a))
- }
- }
- }
- case *image.RGBA64:
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- j := src0.PixOffset(x, y)
- a := src0.Pix[j+6]
- dst.Pix[i+3] = a
- switch a {
- case 0:
- dst.Pix[i+0] = 0
- dst.Pix[i+1] = 0
- dst.Pix[i+2] = 0
- case 0xff:
- dst.Pix[i+0] = src0.Pix[j+0]
- dst.Pix[i+1] = src0.Pix[j+2]
- dst.Pix[i+2] = src0.Pix[j+4]
- default:
- dst.Pix[i+0] = uint8(uint16(src0.Pix[j+0]) * 0xff / uint16(a))
- dst.Pix[i+1] = uint8(uint16(src0.Pix[j+2]) * 0xff / uint16(a))
- dst.Pix[i+2] = uint8(uint16(src0.Pix[j+4]) * 0xff / uint16(a))
- }
- }
- }
- case *image.Gray:
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- j := src0.PixOffset(x, y)
- c := src0.Pix[j]
- dst.Pix[i+0] = c
- dst.Pix[i+1] = c
- dst.Pix[i+2] = c
- dst.Pix[i+3] = 0xff
- }
- }
- case *image.Gray16:
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- j := src0.PixOffset(x, y)
- c := src0.Pix[j]
- dst.Pix[i+0] = c
- dst.Pix[i+1] = c
- dst.Pix[i+2] = c
- dst.Pix[i+3] = 0xff
- }
- }
- case *image.YCbCr:
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- yj := src0.YOffset(x, y)
- cj := src0.COffset(x, y)
- r, g, b := color.YCbCrToRGB(src0.Y[yj], src0.Cb[cj], src0.Cr[cj])
- dst.Pix[i+0] = r
- dst.Pix[i+1] = g
- dst.Pix[i+2] = b
- dst.Pix[i+3] = 0xff
- }
- }
- case *image.Paletted:
- plen := len(src0.Palette)
- pnew := make([]color.NRGBA, plen)
- for i := 0; i < plen; i++ {
- pnew[i] = color.NRGBAModel.Convert(src0.Palette[i]).(color.NRGBA)
- }
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- j := src0.PixOffset(x, y)
- c := pnew[src0.Pix[j]]
- dst.Pix[i+0] = c.R
- dst.Pix[i+1] = c.G
- dst.Pix[i+2] = c.B
- dst.Pix[i+3] = c.A
- }
- }
- default:
- i0 := dst.PixOffset(dstMinX, dstMinY)
- for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
- for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
- c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
- dst.Pix[i+0] = c.R
- dst.Pix[i+1] = c.G
- dst.Pix[i+2] = c.B
- dst.Pix[i+3] = c.A
- }
- }
- }
- return dst
- }
- // This function used internally to convert any image type to NRGBA if needed.
- func toNRGBA(img image.Image) *image.NRGBA {
- srcBounds := img.Bounds()
- if srcBounds.Min.X == 0 && srcBounds.Min.Y == 0 {
- if src0, ok := img.(*image.NRGBA); ok {
- return src0
- }
- }
- return Clone(img)
- }
|