-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute.go
83 lines (69 loc) · 2.3 KB
/
attribute.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
package exr
import (
"encoding/binary"
"fmt"
"io"
"math"
)
func ReadAttributeName(in io.Reader, target *AttributeName) error {
return ReadNullTerminatedString(in, target)
}
const (
AttributeNameChannels AttributeName = "channels"
AttributeNameCompression AttributeName = "compression"
AttributeNameDataWindow AttributeName = "dataWindow"
AttributeNameDisplayWindow AttributeName = "displayWindow"
AttributeNameLineOrder AttributeName = "lineOrder"
AttributeNamePixelAspectRatio AttributeName = "pixelAspectRatio"
AttributeNameScreenWindowCenter AttributeName = "screenWindowCenter"
AttributeNameScreenWindowWidth AttributeName = "screenWindowWidth"
)
type AttributeName string
func ReadAttributeType(in io.Reader, target *AttributeType) error {
return ReadNullTerminatedString(in, target)
}
const (
AttributeTypeChannelList AttributeType = "chlist"
AttributeTypeCompression AttributeType = "compression"
AttributeTypeBox2i AttributeType = "box2i"
AttributeTypeLineOrder AttributeType = "lineOrder"
AttributeTypeFloat AttributeType = "float"
AttributeTypeV2f AttributeType = "v2f"
)
type AttributeType string
type BytesAttribute interface {
Bytes() []byte
}
func WriteAttribute(w io.Writer, n AttributeName, t AttributeType, a any) error {
if v, ok := a.(BytesAttribute); ok {
b := v.Bytes()
return WriteAttributeBytes(w, n, t, int32(len(b)), b)
}
if b, ok := a.([]byte); ok {
return WriteAttributeBytes(w, n, t, int32(len(b)), b)
}
if i, ok := a.(*float32); ok {
b := binary.LittleEndian.AppendUint32(nil, math.Float32bits(*i))
return WriteAttributeBytes(w, n, t, int32(len(b)), b)
}
return fmt.Errorf("unsupported Attribute %q %q %T", n, t, a)
}
func WriteAttributeBytes(w io.Writer, n AttributeName, t AttributeType, s int32, b []byte) error {
err := WriteAttributeHeader(w, n, t, s)
if err == nil {
_, err = w.Write(b)
}
return err
}
// WriteAttributeHeader writes an attribute minus its data. It's used for when the size is known
// but the data is not available as a single []byte
func WriteAttributeHeader(w io.Writer, n AttributeName, t AttributeType, s int32) error {
err := WriteNullTerminatedString(w, n)
if err == nil {
err = WriteNullTerminatedString(w, t)
}
if err == nil {
err = Write(w, &s)
}
return err
}