-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
102 lines (79 loc) · 2.14 KB
/
image.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"image"
"image/gif"
"image/jpeg"
"io"
ico "github.com/biessek/golang-ico"
"github.com/gen2brain/avif"
"github.com/gen2brain/jpegxl"
"github.com/gen2brain/webp"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
)
type Encoder func(io.Writer, image.Image) error
func ReadImage(input io.ReadSeeker) (image.Image, error) {
decoder, err := GetDecoderFromContent(input)
if err != nil {
return nil, err
}
return decoder(input)
}
func ResolveImageEncoder() (Encoder, error) {
table := NewOptionsTable()
switch opts.Format {
case "webp":
options := GetWebPOptions()
table.AddWebPOptions(options)
return func(output io.Writer, img image.Image) error {
return webp.Encode(output, img, options)
}, nil
case "jpeg":
options := GetJpegOptions()
table.AddJpegOptions(options)
return func(output io.Writer, img image.Image) error {
return jpeg.Encode(output, img, options)
}, nil
case "png":
encoder := GetPNGOptions()
table.AddPNGOptions(encoder)
return func(output io.Writer, img image.Image) error {
return encoder.Encode(output, img)
}, nil
case "gif":
options := GetGifOptions()
table.AddGifOptions(options)
return func(output io.Writer, img image.Image) error {
return gif.Encode(output, img, options)
}, nil
case "bmp":
return func(output io.Writer, img image.Image) error {
return bmp.Encode(output, img)
}, nil
case "tiff":
options := GetTiffOptions()
table.AddTiffOptions(options)
return func(output io.Writer, img image.Image) error {
return tiff.Encode(output, img, options)
}, nil
case "avif":
options := GetAvifOptions()
table.AddAvifOptions(options)
return func(output io.Writer, img image.Image) error {
return avif.Encode(output, img, options)
}, nil
case "jxl":
options := GetJxlOptions()
table.AddJxlOptions(options)
return func(output io.Writer, img image.Image) error {
return jpegxl.Encode(output, img, options)
}, nil
case "ico":
table.Print()
return func(output io.Writer, img image.Image) error {
return ico.Encode(output, img)
}, nil
}
return nil, fmt.Errorf("unsupported output format: %s", opts.Format)
}