helpers.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package imaging
  2. import (
  3. "errors"
  4. "image"
  5. "image/color"
  6. "image/gif"
  7. "image/jpeg"
  8. "image/png"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "golang.org/x/image/bmp"
  14. "golang.org/x/image/tiff"
  15. )
  16. // Format is an image file format.
  17. type Format int
  18. // Image file formats.
  19. const (
  20. JPEG Format = iota
  21. PNG
  22. GIF
  23. TIFF
  24. BMP
  25. )
  26. func (f Format) String() string {
  27. switch f {
  28. case JPEG:
  29. return "JPEG"
  30. case PNG:
  31. return "PNG"
  32. case GIF:
  33. return "GIF"
  34. case TIFF:
  35. return "TIFF"
  36. case BMP:
  37. return "BMP"
  38. default:
  39. return "Unsupported"
  40. }
  41. }
  42. var (
  43. // ErrUnsupportedFormat means the given image format (or file extension) is unsupported.
  44. ErrUnsupportedFormat = errors.New("imaging: unsupported image format")
  45. )
  46. // Decode reads an image from r.
  47. func Decode(r io.Reader) (image.Image, error) {
  48. img, _, err := image.Decode(r)
  49. return img, err
  50. }
  51. // Open loads an image from file
  52. func Open(filename string) (image.Image, error) {
  53. file, err := os.Open(filename)
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer file.Close()
  58. return Decode(file)
  59. }
  60. type encodeConfig struct {
  61. jpegQuality int
  62. }
  63. var defaultEncodeConfig = encodeConfig{
  64. jpegQuality: 95,
  65. }
  66. // EncodeOption sets an optional parameter for the Encode and Save functions.
  67. type EncodeOption func(*encodeConfig)
  68. // JPEGQuality returns an EncodeOption that sets the output JPEG quality.
  69. // Quality ranges from 1 to 100 inclusive, higher is better. Default is 95.
  70. func JPEGQuality(quality int) EncodeOption {
  71. return func(c *encodeConfig) {
  72. c.jpegQuality = quality
  73. }
  74. }
  75. // Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF or BMP).
  76. func Encode(w io.Writer, img image.Image, format Format, opts ...EncodeOption) error {
  77. cfg := defaultEncodeConfig
  78. for _, option := range opts {
  79. option(&cfg)
  80. }
  81. var err error
  82. switch format {
  83. case JPEG:
  84. var rgba *image.RGBA
  85. if nrgba, ok := img.(*image.NRGBA); ok {
  86. if nrgba.Opaque() {
  87. rgba = &image.RGBA{
  88. Pix: nrgba.Pix,
  89. Stride: nrgba.Stride,
  90. Rect: nrgba.Rect,
  91. }
  92. }
  93. }
  94. if rgba != nil {
  95. err = jpeg.Encode(w, rgba, &jpeg.Options{Quality: cfg.jpegQuality})
  96. } else {
  97. err = jpeg.Encode(w, img, &jpeg.Options{Quality: cfg.jpegQuality})
  98. }
  99. case PNG:
  100. err = png.Encode(w, img)
  101. case GIF:
  102. err = gif.Encode(w, img, &gif.Options{NumColors: 256})
  103. case TIFF:
  104. err = tiff.Encode(w, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
  105. case BMP:
  106. err = bmp.Encode(w, img)
  107. default:
  108. err = ErrUnsupportedFormat
  109. }
  110. return err
  111. }
  112. // Save saves the image to file with the specified filename.
  113. // The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.
  114. //
  115. // Examples:
  116. //
  117. // // Save the image as PNG.
  118. // err := imaging.Save(img, "out.png")
  119. //
  120. // // Save the image as JPEG with optional quality parameter set to 80.
  121. // err := imaging.Save(img, "out.jpg", imaging.JPEGQuality(80))
  122. //
  123. func Save(img image.Image, filename string, opts ...EncodeOption) (err error) {
  124. formats := map[string]Format{
  125. ".jpg": JPEG,
  126. ".jpeg": JPEG,
  127. ".png": PNG,
  128. ".tif": TIFF,
  129. ".tiff": TIFF,
  130. ".bmp": BMP,
  131. ".gif": GIF,
  132. }
  133. ext := strings.ToLower(filepath.Ext(filename))
  134. f, ok := formats[ext]
  135. if !ok {
  136. return ErrUnsupportedFormat
  137. }
  138. file, err := os.Create(filename)
  139. if err != nil {
  140. return err
  141. }
  142. defer file.Close()
  143. return Encode(file, img, f, opts...)
  144. }
  145. // New creates a new image with the specified width and height, and fills it with the specified color.
  146. func New(width, height int, fillColor color.Color) *image.NRGBA {
  147. if width <= 0 || height <= 0 {
  148. return &image.NRGBA{}
  149. }
  150. dst := image.NewNRGBA(image.Rect(0, 0, width, height))
  151. c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
  152. if c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0 {
  153. return dst
  154. }
  155. cs := []uint8{c.R, c.G, c.B, c.A}
  156. // fill the first row
  157. for x := 0; x < width; x++ {
  158. copy(dst.Pix[x*4:(x+1)*4], cs)
  159. }
  160. // copy the first row to other rows
  161. for y := 1; y < height; y++ {
  162. copy(dst.Pix[y*dst.Stride:y*dst.Stride+width*4], dst.Pix[0:width*4])
  163. }
  164. return dst
  165. }