func Encode(w io.Writer, m image.Image) (err error) {
	b := m.Bounds()
	mw, mh := b.Dx(), b.Dy()
	h := rawHeader{
		Width:  uint16(mw),
		Height: uint16(mh),
	}
	if int(h.Width) != mw || int(h.Height) != mh {
		return errors.New("uint16 width/height overflow")
	}
	h.Flags = flagOriginTop
	switch tm := m.(type) {
	case *image.Gray:
		h.ImageType = imageTypeMonoChrome
		err = encodeGray(w, tm, h)
	case *image.NRGBA:
		h.ImageType = imageTypeTrueColor
		err = encodeRGBA(w, tm, h, attrTypeAlpha)
	case *image.RGBA:
		h.ImageType = imageTypeTrueColor
		err = encodeRGBA(w, (*image.NRGBA)(tm), h, attrTypePremultipliedAlpha)
	default:
		h.ImageType = imageTypeTrueColor
		newm := image.NewNRGBA(b)
		draw.Draw(newm, b, m, b.Min, draw.Src)
		err = encodeRGBA(w, newm, h, attrTypeAlpha)
	}
	return
}
Encode encodes an image into TARGA format.
package tga
import (
	"encoding/binary"
	"errors"
	"image"
	"image/draw"
	"io"
)