/
exif_A302_cfa_pattern.go
97 lines (69 loc) · 2.12 KB
/
exif_A302_cfa_pattern.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
package exifundefined
import (
"bytes"
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type TagA302CfaPattern struct {
HorizontalRepeat uint16
VerticalRepeat uint16
CfaValue []byte
}
func (TagA302CfaPattern) EncoderName() string {
return "CodecA302CfaPattern"
}
func (cp TagA302CfaPattern) String() string {
return fmt.Sprintf("TagA302CfaPattern<HORZ-REPEAT=(%d) VERT-REPEAT=(%d) CFA-VALUE=(%d)>", cp.HorizontalRepeat, cp.VerticalRepeat, len(cp.CfaValue))
}
type CodecA302CfaPattern struct {
}
func (CodecA302CfaPattern) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test.
cp, ok := value.(TagA302CfaPattern)
if ok == false {
log.Panicf("can only encode a TagA302CfaPattern")
}
b := new(bytes.Buffer)
err = binary.Write(b, byteOrder, cp.HorizontalRepeat)
log.PanicIf(err)
err = binary.Write(b, byteOrder, cp.VerticalRepeat)
log.PanicIf(err)
_, err = b.Write(cp.CfaValue)
log.PanicIf(err)
encoded = b.Bytes()
// TODO(dustin): Confirm this size against the specification.
return encoded, uint32(len(encoded)), nil
}
func (CodecA302CfaPattern) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test using known good data.
valueContext.SetUndefinedValueType(exifcommon.TypeByte)
valueBytes, err := valueContext.ReadBytes()
log.PanicIf(err)
cp := TagA302CfaPattern{}
cp.HorizontalRepeat = valueContext.ByteOrder().Uint16(valueBytes[0:2])
cp.VerticalRepeat = valueContext.ByteOrder().Uint16(valueBytes[2:4])
expectedLength := int(cp.HorizontalRepeat * cp.VerticalRepeat)
cp.CfaValue = valueBytes[4 : 4+expectedLength]
return cp, nil
}
func init() {
registerEncoder(
TagA302CfaPattern{},
CodecA302CfaPattern{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0xa302,
CodecA302CfaPattern{})
}