-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.go
63 lines (52 loc) · 1.26 KB
/
codec.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
package main
import "ImageServer/native"
var decoders = map[string]func([]byte) *native.Frame{
"png": native.DecPNG,
"webp": native.DecWEBP,
"avif": native.DecAVIF,
"jpeg": native.DecJPEG,
}
var detectors = map[string]func([]byte) (bool, uint32, uint32){
"png": native.IsPNG,
"webp": native.IsWEBP,
"avif": native.IsAVIF,
"jpeg": native.IsJPEG,
}
var encoders = map[string]func(*native.Frame) []byte{
"jpeg": native.EncJPEG,
"webp": native.EncWEBP,
"avif": native.EncAVIF,
}
func Decode(d []byte, t string) (*native.Frame, string) {
if detector, exist := detectors[t]; exist {
ok, width, height := detector(d)
if !ok || width > uint32(config.Site.MaxSize) || height > uint32(config.Site.MaxSize) {
return nil, ""
}
if f := decoders[t](d); f != nil {
return f, t
}
}
for codec, detector := range detectors {
if codec == t {
continue
}
ok, width, height := detector(d)
if !ok {
continue
} else if width > uint32(config.Site.MaxSize) || height > uint32(config.Site.MaxSize) {
return nil, ""
}
if f := decoders[codec](d); f != nil {
return f, codec
}
}
return nil, ""
}
func Encode(f *native.Frame, t string) []byte {
if encoder, exist := encoders[t]; exist {
return encoder(f)
} else {
return nil
}
}