helpers.go 3.5 KB

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