summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/disintegration/imaging/helpers.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/disintegration/imaging/helpers.go')
-rw-r--r--vendor/github.com/disintegration/imaging/helpers.go68
1 files changed, 30 insertions, 38 deletions
diff --git a/vendor/github.com/disintegration/imaging/helpers.go b/vendor/github.com/disintegration/imaging/helpers.go
index 7193e473c..dcb4d7ebb 100644
--- a/vendor/github.com/disintegration/imaging/helpers.go
+++ b/vendor/github.com/disintegration/imaging/helpers.go
@@ -1,6 +1,7 @@
package imaging
import (
+ "bytes"
"errors"
"image"
"image/color"
@@ -46,6 +47,26 @@ func (f Format) String() string {
}
}
+var formatFromExt = map[string]Format{
+ ".jpg": JPEG,
+ ".jpeg": JPEG,
+ ".png": PNG,
+ ".tif": TIFF,
+ ".tiff": TIFF,
+ ".bmp": BMP,
+ ".gif": GIF,
+}
+
+// FormatFromFilename parses image format from filename extension:
+// "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff") and "bmp" are supported.
+func FormatFromFilename(filename string) (Format, error) {
+ ext := strings.ToLower(filepath.Ext(filename))
+ if f, ok := formatFromExt[ext]; ok {
+ return f, nil
+ }
+ return -1, ErrUnsupportedFormat
+}
+
var (
// ErrUnsupportedFormat means the given image format (or file extension) is unsupported.
ErrUnsupportedFormat = errors.New("imaging: unsupported image format")
@@ -199,22 +220,10 @@ func Encode(w io.Writer, img image.Image, format Format, opts ...EncodeOption) e
// err := imaging.Save(img, "out.jpg", imaging.JPEGQuality(80))
//
func Save(img image.Image, filename string, opts ...EncodeOption) (err error) {
- formats := map[string]Format{
- ".jpg": JPEG,
- ".jpeg": JPEG,
- ".png": PNG,
- ".tif": TIFF,
- ".tiff": TIFF,
- ".bmp": BMP,
- ".gif": GIF,
- }
-
- ext := strings.ToLower(filepath.Ext(filename))
- f, ok := formats[ext]
- if !ok {
- return ErrUnsupportedFormat
+ f, err := FormatFromFilename(filename)
+ if err != nil {
+ return err
}
-
file, err := fs.Create(filename)
if err != nil {
return err
@@ -236,33 +245,16 @@ func New(width, height int, fillColor color.Color) *image.NRGBA {
return &image.NRGBA{}
}
- dst := image.NewNRGBA(image.Rect(0, 0, width, height))
c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
-
- if c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0 {
- return dst
+ if (c == color.NRGBA{0, 0, 0, 0}) {
+ return image.NewNRGBA(image.Rect(0, 0, width, height))
}
- // Fill the first row.
- i := 0
- for x := 0; x < width; x++ {
- dst.Pix[i+0] = c.R
- dst.Pix[i+1] = c.G
- dst.Pix[i+2] = c.B
- dst.Pix[i+3] = c.A
- i += 4
+ return &image.NRGBA{
+ Pix: bytes.Repeat([]byte{c.R, c.G, c.B, c.A}, width*height),
+ Stride: 4 * width,
+ Rect: image.Rect(0, 0, width, height),
}
-
- // Copy the first row to other rows.
- size := width * 4
- parallel(1, height, func(ys <-chan int) {
- for y := range ys {
- i = y * dst.Stride
- copy(dst.Pix[i:i+size], dst.Pix[0:size])
- }
- })
-
- return dst
}
// Clone returns a copy of the given image.