forked from polyfloyd/shady
/
format.go
48 lines (42 loc) · 1.06 KB
/
format.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
package encode
import (
"image"
"io"
"path"
"time"
)
var Formats = map[string]Format{
"ansi": &AnsiDisplay{},
"gif": GIFFormat{},
"jpg": JPGFormat{},
"png": PNGFormat{},
"rgb24": RGB24Format{},
"rgba32": RGBA32Format{},
}
func DetectFormat(filename string) (Format, bool) {
ext := path.Ext(filename)
if len(ext) == 0 {
return nil, false
}
for _, f := range Formats {
for _, e := range f.Extensions() {
if e == ext[1:] {
return f, true
}
}
}
return nil, false
}
type Format interface {
// Extensions returns all file extensions excluding '.' that this format is
// commonly encoded into.
Extensions() []string
// Encode encodes a single image to the specfied io.Writer.
Encode(w io.Writer, img image.Image) error
// EncodeAnimation encodes a series of successive images to the specified
// io.Writer.
//
// The function should consume all images from the stream until it closes.
// The interval parameter is the time between two images.
EncodeAnimation(w io.Writer, stream <-chan image.Image, interval time.Duration) error
}