helpers.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.).
  2. // This package is based on the standard Go image package and works best along with it.
  3. //
  4. // Image manipulation functions provided by the package take any image type
  5. // that implements `image.Image` interface as an input, and return a new image of
  6. // `*image.NRGBA` type (32bit RGBA colors, not premultiplied by alpha).
  7. package imaging
  8. import (
  9. "errors"
  10. "image"
  11. "image/color"
  12. "image/gif"
  13. "image/jpeg"
  14. "image/png"
  15. "io"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "golang.org/x/image/bmp"
  20. "golang.org/x/image/tiff"
  21. )
  22. // Format is an image file format.
  23. type Format int
  24. // Image file formats.
  25. const (
  26. JPEG Format = iota
  27. PNG
  28. GIF
  29. TIFF
  30. BMP
  31. )
  32. func (f Format) String() string {
  33. switch f {
  34. case JPEG:
  35. return "JPEG"
  36. case PNG:
  37. return "PNG"
  38. case GIF:
  39. return "GIF"
  40. case TIFF:
  41. return "TIFF"
  42. case BMP:
  43. return "BMP"
  44. default:
  45. return "Unsupported"
  46. }
  47. }
  48. var (
  49. // ErrUnsupportedFormat means the given image format (or file extension) is unsupported.
  50. ErrUnsupportedFormat = errors.New("imaging: unsupported image format")
  51. )
  52. // Decode reads an image from r.
  53. func Decode(r io.Reader) (image.Image, error) {
  54. img, _, err := image.Decode(r)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return toNRGBA(img), nil
  59. }
  60. // Open loads an image from file
  61. func Open(filename string) (image.Image, error) {
  62. file, err := os.Open(filename)
  63. if err != nil {
  64. return nil, err
  65. }
  66. defer file.Close()
  67. img, err := Decode(file)
  68. return img, err
  69. }
  70. // Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF or BMP).
  71. func Encode(w io.Writer, img image.Image, format Format) error {
  72. var err error
  73. switch format {
  74. case JPEG:
  75. var rgba *image.RGBA
  76. if nrgba, ok := img.(*image.NRGBA); ok {
  77. if nrgba.Opaque() {
  78. rgba = &image.RGBA{
  79. Pix: nrgba.Pix,
  80. Stride: nrgba.Stride,
  81. Rect: nrgba.Rect,
  82. }
  83. }
  84. }
  85. if rgba != nil {
  86. err = jpeg.Encode(w, rgba, &jpeg.Options{Quality: 95})
  87. } else {
  88. err = jpeg.Encode(w, img, &jpeg.Options{Quality: 95})
  89. }
  90. case PNG:
  91. err = png.Encode(w, img)
  92. case GIF:
  93. err = gif.Encode(w, img, &gif.Options{NumColors: 256})
  94. case TIFF:
  95. err = tiff.Encode(w, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
  96. case BMP:
  97. err = bmp.Encode(w, img)
  98. default:
  99. err = ErrUnsupportedFormat
  100. }
  101. return err
  102. }
  103. // Save saves the image to file with the specified filename.
  104. // The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.
  105. func Save(img image.Image, filename string) (err error) {
  106. formats := map[string]Format{
  107. ".jpg": JPEG,
  108. ".jpeg": JPEG,
  109. ".png": PNG,
  110. ".tif": TIFF,
  111. ".tiff": TIFF,
  112. ".bmp": BMP,
  113. ".gif": GIF,
  114. }
  115. ext := strings.ToLower(filepath.Ext(filename))
  116. f, ok := formats[ext]
  117. if !ok {
  118. return ErrUnsupportedFormat
  119. }
  120. file, err := os.Create(filename)
  121. if err != nil {
  122. return err
  123. }
  124. defer file.Close()
  125. return Encode(file, img, f)
  126. }
  127. // New creates a new image with the specified width and height, and fills it with the specified color.
  128. func New(width, height int, fillColor color.Color) *image.NRGBA {
  129. if width <= 0 || height <= 0 {
  130. return &image.NRGBA{}
  131. }
  132. dst := image.NewNRGBA(image.Rect(0, 0, width, height))
  133. c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
  134. if c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0 {
  135. return dst
  136. }
  137. cs := []uint8{c.R, c.G, c.B, c.A}
  138. // fill the first row
  139. for x := 0; x < width; x++ {
  140. copy(dst.Pix[x*4:(x+1)*4], cs)
  141. }
  142. // copy the first row to other rows
  143. for y := 1; y < height; y++ {
  144. copy(dst.Pix[y*dst.Stride:y*dst.Stride+width*4], dst.Pix[0:width*4])
  145. }
  146. return dst
  147. }
  148. // Clone returns a copy of the given image.
  149. func Clone(img image.Image) *image.NRGBA {
  150. srcBounds := img.Bounds()
  151. srcMinX := srcBounds.Min.X
  152. srcMinY := srcBounds.Min.Y
  153. dstBounds := srcBounds.Sub(srcBounds.Min)
  154. dstW := dstBounds.Dx()
  155. dstH := dstBounds.Dy()
  156. dst := image.NewNRGBA(dstBounds)
  157. switch src := img.(type) {
  158. case *image.NRGBA:
  159. rowSize := srcBounds.Dx() * 4
  160. parallel(dstH, func(partStart, partEnd int) {
  161. for dstY := partStart; dstY < partEnd; dstY++ {
  162. di := dst.PixOffset(0, dstY)
  163. si := src.PixOffset(srcMinX, srcMinY+dstY)
  164. copy(dst.Pix[di:di+rowSize], src.Pix[si:si+rowSize])
  165. }
  166. })
  167. case *image.NRGBA64:
  168. parallel(dstH, func(partStart, partEnd int) {
  169. for dstY := partStart; dstY < partEnd; dstY++ {
  170. di := dst.PixOffset(0, dstY)
  171. si := src.PixOffset(srcMinX, srcMinY+dstY)
  172. for dstX := 0; dstX < dstW; dstX++ {
  173. dst.Pix[di+0] = src.Pix[si+0]
  174. dst.Pix[di+1] = src.Pix[si+2]
  175. dst.Pix[di+2] = src.Pix[si+4]
  176. dst.Pix[di+3] = src.Pix[si+6]
  177. di += 4
  178. si += 8
  179. }
  180. }
  181. })
  182. case *image.RGBA:
  183. parallel(dstH, func(partStart, partEnd int) {
  184. for dstY := partStart; dstY < partEnd; dstY++ {
  185. di := dst.PixOffset(0, dstY)
  186. si := src.PixOffset(srcMinX, srcMinY+dstY)
  187. for dstX := 0; dstX < dstW; dstX++ {
  188. a := src.Pix[si+3]
  189. dst.Pix[di+3] = a
  190. switch a {
  191. case 0:
  192. dst.Pix[di+0] = 0
  193. dst.Pix[di+1] = 0
  194. dst.Pix[di+2] = 0
  195. case 0xff:
  196. dst.Pix[di+0] = src.Pix[si+0]
  197. dst.Pix[di+1] = src.Pix[si+1]
  198. dst.Pix[di+2] = src.Pix[si+2]
  199. default:
  200. var tmp uint16
  201. tmp = uint16(src.Pix[si+0]) * 0xff / uint16(a)
  202. dst.Pix[di+0] = uint8(tmp)
  203. tmp = uint16(src.Pix[si+1]) * 0xff / uint16(a)
  204. dst.Pix[di+1] = uint8(tmp)
  205. tmp = uint16(src.Pix[si+2]) * 0xff / uint16(a)
  206. dst.Pix[di+2] = uint8(tmp)
  207. }
  208. di += 4
  209. si += 4
  210. }
  211. }
  212. })
  213. case *image.RGBA64:
  214. parallel(dstH, func(partStart, partEnd int) {
  215. for dstY := partStart; dstY < partEnd; dstY++ {
  216. di := dst.PixOffset(0, dstY)
  217. si := src.PixOffset(srcMinX, srcMinY+dstY)
  218. for dstX := 0; dstX < dstW; dstX++ {
  219. a := src.Pix[si+6]
  220. dst.Pix[di+3] = a
  221. switch a {
  222. case 0:
  223. dst.Pix[di+0] = 0
  224. dst.Pix[di+1] = 0
  225. dst.Pix[di+2] = 0
  226. case 0xff:
  227. dst.Pix[di+0] = src.Pix[si+0]
  228. dst.Pix[di+1] = src.Pix[si+2]
  229. dst.Pix[di+2] = src.Pix[si+4]
  230. default:
  231. var tmp uint16
  232. tmp = uint16(src.Pix[si+0]) * 0xff / uint16(a)
  233. dst.Pix[di+0] = uint8(tmp)
  234. tmp = uint16(src.Pix[si+2]) * 0xff / uint16(a)
  235. dst.Pix[di+1] = uint8(tmp)
  236. tmp = uint16(src.Pix[si+4]) * 0xff / uint16(a)
  237. dst.Pix[di+2] = uint8(tmp)
  238. }
  239. di += 4
  240. si += 8
  241. }
  242. }
  243. })
  244. case *image.Gray:
  245. parallel(dstH, func(partStart, partEnd int) {
  246. for dstY := partStart; dstY < partEnd; dstY++ {
  247. di := dst.PixOffset(0, dstY)
  248. si := src.PixOffset(srcMinX, srcMinY+dstY)
  249. for dstX := 0; dstX < dstW; dstX++ {
  250. c := src.Pix[si]
  251. dst.Pix[di+0] = c
  252. dst.Pix[di+1] = c
  253. dst.Pix[di+2] = c
  254. dst.Pix[di+3] = 0xff
  255. di += 4
  256. si += 1
  257. }
  258. }
  259. })
  260. case *image.Gray16:
  261. parallel(dstH, func(partStart, partEnd int) {
  262. for dstY := partStart; dstY < partEnd; dstY++ {
  263. di := dst.PixOffset(0, dstY)
  264. si := src.PixOffset(srcMinX, srcMinY+dstY)
  265. for dstX := 0; dstX < dstW; dstX++ {
  266. c := src.Pix[si]
  267. dst.Pix[di+0] = c
  268. dst.Pix[di+1] = c
  269. dst.Pix[di+2] = c
  270. dst.Pix[di+3] = 0xff
  271. di += 4
  272. si += 2
  273. }
  274. }
  275. })
  276. case *image.YCbCr:
  277. parallel(dstH, func(partStart, partEnd int) {
  278. for dstY := partStart; dstY < partEnd; dstY++ {
  279. di := dst.PixOffset(0, dstY)
  280. for dstX := 0; dstX < dstW; dstX++ {
  281. srcX := srcMinX + dstX
  282. srcY := srcMinY + dstY
  283. siy := src.YOffset(srcX, srcY)
  284. sic := src.COffset(srcX, srcY)
  285. r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
  286. dst.Pix[di+0] = r
  287. dst.Pix[di+1] = g
  288. dst.Pix[di+2] = b
  289. dst.Pix[di+3] = 0xff
  290. di += 4
  291. }
  292. }
  293. })
  294. case *image.Paletted:
  295. plen := len(src.Palette)
  296. pnew := make([]color.NRGBA, plen)
  297. for i := 0; i < plen; i++ {
  298. pnew[i] = color.NRGBAModel.Convert(src.Palette[i]).(color.NRGBA)
  299. }
  300. parallel(dstH, func(partStart, partEnd int) {
  301. for dstY := partStart; dstY < partEnd; dstY++ {
  302. di := dst.PixOffset(0, dstY)
  303. si := src.PixOffset(srcMinX, srcMinY+dstY)
  304. for dstX := 0; dstX < dstW; dstX++ {
  305. c := pnew[src.Pix[si]]
  306. dst.Pix[di+0] = c.R
  307. dst.Pix[di+1] = c.G
  308. dst.Pix[di+2] = c.B
  309. dst.Pix[di+3] = c.A
  310. di += 4
  311. si += 1
  312. }
  313. }
  314. })
  315. default:
  316. parallel(dstH, func(partStart, partEnd int) {
  317. for dstY := partStart; dstY < partEnd; dstY++ {
  318. di := dst.PixOffset(0, dstY)
  319. for dstX := 0; dstX < dstW; dstX++ {
  320. c := color.NRGBAModel.Convert(img.At(srcMinX+dstX, srcMinY+dstY)).(color.NRGBA)
  321. dst.Pix[di+0] = c.R
  322. dst.Pix[di+1] = c.G
  323. dst.Pix[di+2] = c.B
  324. dst.Pix[di+3] = c.A
  325. di += 4
  326. }
  327. }
  328. })
  329. }
  330. return dst
  331. }
  332. // toNRGBA converts any image type to *image.NRGBA with min-point at (0, 0).
  333. func toNRGBA(img image.Image) *image.NRGBA {
  334. srcBounds := img.Bounds()
  335. if srcBounds.Min.X == 0 && srcBounds.Min.Y == 0 {
  336. if src0, ok := img.(*image.NRGBA); ok {
  337. return src0
  338. }
  339. }
  340. return Clone(img)
  341. }