helpers.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. dstBounds := img.Bounds().Sub(img.Bounds().Min)
  151. dst := image.NewNRGBA(dstBounds)
  152. switch src := img.(type) {
  153. case *image.NRGBA:
  154. copyNRGBA(dst, src)
  155. case *image.NRGBA64:
  156. copyNRGBA64(dst, src)
  157. case *image.RGBA:
  158. copyRGBA(dst, src)
  159. case *image.RGBA64:
  160. copyRGBA64(dst, src)
  161. case *image.Gray:
  162. copyGray(dst, src)
  163. case *image.Gray16:
  164. copyGray16(dst, src)
  165. case *image.YCbCr:
  166. copyYCbCr(dst, src)
  167. case *image.Paletted:
  168. copyPaletted(dst, src)
  169. default:
  170. copyImage(dst, src)
  171. }
  172. return dst
  173. }
  174. func copyNRGBA(dst *image.NRGBA, src *image.NRGBA) {
  175. srcMinX := src.Rect.Min.X
  176. srcMinY := src.Rect.Min.Y
  177. dstW := dst.Rect.Dx()
  178. dstH := dst.Rect.Dy()
  179. rowSize := dstW * 4
  180. parallel(dstH, func(partStart, partEnd int) {
  181. for dstY := partStart; dstY < partEnd; dstY++ {
  182. di := dst.PixOffset(0, dstY)
  183. si := src.PixOffset(srcMinX, srcMinY+dstY)
  184. copy(dst.Pix[di:di+rowSize], src.Pix[si:si+rowSize])
  185. }
  186. })
  187. }
  188. func copyNRGBA64(dst *image.NRGBA, src *image.NRGBA64) {
  189. srcMinX := src.Rect.Min.X
  190. srcMinY := src.Rect.Min.Y
  191. dstW := dst.Rect.Dx()
  192. dstH := dst.Rect.Dy()
  193. parallel(dstH, func(partStart, partEnd int) {
  194. for dstY := partStart; dstY < partEnd; dstY++ {
  195. di := dst.PixOffset(0, dstY)
  196. si := src.PixOffset(srcMinX, srcMinY+dstY)
  197. for dstX := 0; dstX < dstW; dstX++ {
  198. dst.Pix[di+0] = src.Pix[si+0]
  199. dst.Pix[di+1] = src.Pix[si+2]
  200. dst.Pix[di+2] = src.Pix[si+4]
  201. dst.Pix[di+3] = src.Pix[si+6]
  202. di += 4
  203. si += 8
  204. }
  205. }
  206. })
  207. }
  208. func copyRGBA(dst *image.NRGBA, src *image.RGBA) {
  209. srcMinX := src.Rect.Min.X
  210. srcMinY := src.Rect.Min.Y
  211. dstW := dst.Rect.Dx()
  212. dstH := dst.Rect.Dy()
  213. parallel(dstH, func(partStart, partEnd int) {
  214. for dstY := partStart; dstY < partEnd; dstY++ {
  215. di := dst.PixOffset(0, dstY)
  216. si := src.PixOffset(srcMinX, srcMinY+dstY)
  217. for dstX := 0; dstX < dstW; dstX++ {
  218. a := src.Pix[si+3]
  219. dst.Pix[di+3] = a
  220. switch a {
  221. case 0:
  222. dst.Pix[di+0] = 0
  223. dst.Pix[di+1] = 0
  224. dst.Pix[di+2] = 0
  225. case 0xff:
  226. dst.Pix[di+0] = src.Pix[si+0]
  227. dst.Pix[di+1] = src.Pix[si+1]
  228. dst.Pix[di+2] = src.Pix[si+2]
  229. default:
  230. var tmp uint16
  231. tmp = uint16(src.Pix[si+0]) * 0xff / uint16(a)
  232. dst.Pix[di+0] = uint8(tmp)
  233. tmp = uint16(src.Pix[si+1]) * 0xff / uint16(a)
  234. dst.Pix[di+1] = uint8(tmp)
  235. tmp = uint16(src.Pix[si+2]) * 0xff / uint16(a)
  236. dst.Pix[di+2] = uint8(tmp)
  237. }
  238. di += 4
  239. si += 4
  240. }
  241. }
  242. })
  243. }
  244. func copyRGBA64(dst *image.NRGBA, src *image.RGBA64) {
  245. srcMinX := src.Rect.Min.X
  246. srcMinY := src.Rect.Min.Y
  247. dstW := dst.Rect.Dx()
  248. dstH := dst.Rect.Dy()
  249. parallel(dstH, func(partStart, partEnd int) {
  250. for dstY := partStart; dstY < partEnd; dstY++ {
  251. di := dst.PixOffset(0, dstY)
  252. si := src.PixOffset(srcMinX, srcMinY+dstY)
  253. for dstX := 0; dstX < dstW; dstX++ {
  254. a := src.Pix[si+6]
  255. dst.Pix[di+3] = a
  256. switch a {
  257. case 0:
  258. dst.Pix[di+0] = 0
  259. dst.Pix[di+1] = 0
  260. dst.Pix[di+2] = 0
  261. case 0xff:
  262. dst.Pix[di+0] = src.Pix[si+0]
  263. dst.Pix[di+1] = src.Pix[si+2]
  264. dst.Pix[di+2] = src.Pix[si+4]
  265. default:
  266. var tmp uint16
  267. tmp = uint16(src.Pix[si+0]) * 0xff / uint16(a)
  268. dst.Pix[di+0] = uint8(tmp)
  269. tmp = uint16(src.Pix[si+2]) * 0xff / uint16(a)
  270. dst.Pix[di+1] = uint8(tmp)
  271. tmp = uint16(src.Pix[si+4]) * 0xff / uint16(a)
  272. dst.Pix[di+2] = uint8(tmp)
  273. }
  274. di += 4
  275. si += 8
  276. }
  277. }
  278. })
  279. }
  280. func copyGray(dst *image.NRGBA, src *image.Gray) {
  281. srcMinX := src.Rect.Min.X
  282. srcMinY := src.Rect.Min.Y
  283. dstW := dst.Rect.Dx()
  284. dstH := dst.Rect.Dy()
  285. parallel(dstH, func(partStart, partEnd int) {
  286. for dstY := partStart; dstY < partEnd; dstY++ {
  287. di := dst.PixOffset(0, dstY)
  288. si := src.PixOffset(srcMinX, srcMinY+dstY)
  289. for dstX := 0; dstX < dstW; dstX++ {
  290. c := src.Pix[si]
  291. dst.Pix[di+0] = c
  292. dst.Pix[di+1] = c
  293. dst.Pix[di+2] = c
  294. dst.Pix[di+3] = 0xff
  295. di += 4
  296. si++
  297. }
  298. }
  299. })
  300. }
  301. func copyGray16(dst *image.NRGBA, src *image.Gray16) {
  302. srcMinX := src.Rect.Min.X
  303. srcMinY := src.Rect.Min.Y
  304. dstW := dst.Rect.Dx()
  305. dstH := dst.Rect.Dy()
  306. parallel(dstH, func(partStart, partEnd int) {
  307. for dstY := partStart; dstY < partEnd; dstY++ {
  308. di := dst.PixOffset(0, dstY)
  309. si := src.PixOffset(srcMinX, srcMinY+dstY)
  310. for dstX := 0; dstX < dstW; dstX++ {
  311. c := src.Pix[si]
  312. dst.Pix[di+0] = c
  313. dst.Pix[di+1] = c
  314. dst.Pix[di+2] = c
  315. dst.Pix[di+3] = 0xff
  316. di += 4
  317. si += 2
  318. }
  319. }
  320. })
  321. }
  322. func copyYCbCr(dst *image.NRGBA, src *image.YCbCr) {
  323. srcMinX := src.Rect.Min.X
  324. srcMinY := src.Rect.Min.Y
  325. dstW := dst.Rect.Dx()
  326. dstH := dst.Rect.Dy()
  327. parallel(dstH, func(partStart, partEnd int) {
  328. for dstY := partStart; dstY < partEnd; dstY++ {
  329. di := dst.PixOffset(0, dstY)
  330. for dstX := 0; dstX < dstW; dstX++ {
  331. srcX := srcMinX + dstX
  332. srcY := srcMinY + dstY
  333. siy := src.YOffset(srcX, srcY)
  334. sic := src.COffset(srcX, srcY)
  335. r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
  336. dst.Pix[di+0] = r
  337. dst.Pix[di+1] = g
  338. dst.Pix[di+2] = b
  339. dst.Pix[di+3] = 0xff
  340. di += 4
  341. }
  342. }
  343. })
  344. }
  345. func copyPaletted(dst *image.NRGBA, src *image.Paletted) {
  346. srcMinX := src.Rect.Min.X
  347. srcMinY := src.Rect.Min.Y
  348. dstW := dst.Rect.Dx()
  349. dstH := dst.Rect.Dy()
  350. plen := len(src.Palette)
  351. pnew := make([]color.NRGBA, plen)
  352. for i := 0; i < plen; i++ {
  353. pnew[i] = color.NRGBAModel.Convert(src.Palette[i]).(color.NRGBA)
  354. }
  355. parallel(dstH, func(partStart, partEnd int) {
  356. for dstY := partStart; dstY < partEnd; dstY++ {
  357. di := dst.PixOffset(0, dstY)
  358. si := src.PixOffset(srcMinX, srcMinY+dstY)
  359. for dstX := 0; dstX < dstW; dstX++ {
  360. c := pnew[src.Pix[si]]
  361. dst.Pix[di+0] = c.R
  362. dst.Pix[di+1] = c.G
  363. dst.Pix[di+2] = c.B
  364. dst.Pix[di+3] = c.A
  365. di += 4
  366. si++
  367. }
  368. }
  369. })
  370. }
  371. func copyImage(dst *image.NRGBA, src image.Image) {
  372. srcMinX := src.Bounds().Min.X
  373. srcMinY := src.Bounds().Min.Y
  374. dstW := dst.Bounds().Dx()
  375. dstH := dst.Bounds().Dy()
  376. parallel(dstH, func(partStart, partEnd int) {
  377. for dstY := partStart; dstY < partEnd; dstY++ {
  378. di := dst.PixOffset(0, dstY)
  379. for dstX := 0; dstX < dstW; dstX++ {
  380. c := color.NRGBAModel.Convert(src.At(srcMinX+dstX, srcMinY+dstY)).(color.NRGBA)
  381. dst.Pix[di+0] = c.R
  382. dst.Pix[di+1] = c.G
  383. dst.Pix[di+2] = c.B
  384. dst.Pix[di+3] = c.A
  385. di += 4
  386. }
  387. }
  388. })
  389. }
  390. // toNRGBA converts any image type to *image.NRGBA with min-point at (0, 0).
  391. func toNRGBA(img image.Image) *image.NRGBA {
  392. if img, ok := img.(*image.NRGBA); ok && img.Bounds().Min.Eq(image.ZP) {
  393. return img
  394. }
  395. return Clone(img)
  396. }