-
Notifications
You must be signed in to change notification settings - Fork 2
/
readwrite.go
70 lines (65 loc) · 1.6 KB
/
readwrite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package plates
import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
ico "github.com/biessek/golang-ico"
"github.com/chai2010/webp"
bmp "github.com/jsummers/gobmp"
"github.com/xyproto/xpm"
)
// Read tries to read the given image filename and return an image.Image
// The supported extensions are: .png, .jpg, .jpeg, .gif, .ico, .bmp and .webp
func Read(filename string) (image.Image, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
switch strings.ToLower(filepath.Ext(filename)) {
case ".png":
return png.Decode(f)
case ".jpg", ".jpeg":
return jpeg.Decode(f)
case ".gif":
return gif.Decode(f)
case ".ico":
return ico.Decode(f)
case ".bmp":
return bmp.Decode(f)
case ".webp":
return webp.Decode(f)
}
return nil, errors.New("unrecognized file extension: " + filepath.Ext(filename))
}
// Write tries to write then given image.Image to a file.
// The supported extensions are: .png, .jpg, .jpeg. .gif, .ico, .bmp, .webp and .xpm
func Write(filename string, img image.Image) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
switch strings.ToLower(filepath.Ext(filename)) {
case ".png":
return png.Encode(f, img)
case ".jpg", ".jpeg":
return jpeg.Encode(f, img, nil)
case ".gif":
return gif.Encode(f, img, nil)
case ".ico":
return ico.Encode(f, img)
case ".bmp":
return bmp.Encode(f, img)
case ".webp":
return webp.Encode(f, img, nil)
case ".xpm":
return xpm.Encode(f, img)
}
return errors.New("unrecognized file extension: " + filepath.Ext(filename))
}