helpers.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. }