/
compressor.go
79 lines (63 loc) · 1.39 KB
/
compressor.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
package exr
import (
"bytes"
"compress/zlib"
)
type Compressor interface {
Compress(src []byte) ([]byte, error)
}
func NewNopCompressor() Compressor {
return &nopCompressor{}
}
type nopCompressor struct{}
func (d *nopCompressor) Compress(src []byte) ([]byte, error) {
return src, nil
}
func NewZipCompressor() Compressor {
return &zipCompressor{}
}
type zipCompressor struct{}
func (d *zipCompressor) Compress(src []byte) ([]byte, error) {
out := &bytes.Buffer{}
// interleave scalar
result := make([]byte, len(src))
i1 := 0
i2 := (len(src) + 1) / 2
j := 0
for j < len(result) {
result[i1] = src[j]
i1++
j++
if j < len(result) {
result[i2] = src[j]
i2++
j++
}
}
// delta encode
p := int(result[0])
for i := 1; i < len(result); i++ {
v := int(result[i]) - p + 128 + 256
p = int(result[i])
result[i] = byte(v)
}
// Use Zip level 4 not default 6 to improve performance:
// https://aras-p.info/blog/2021/08/05/EXR-Zip-compression-levels/
// https://github.com/AcademySoftwareFoundation/openexr/pull/1125
w, err := zlib.NewWriterLevelDict(out, 4, nil)
if err != nil {
return nil, err
}
if _, err := w.Write(result); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
dst := out.Bytes()
// Note: as per the exr spec, we return the smallest of src or data
if len(dst) < len(src) {
return dst, nil
}
return src, nil
}