forked from dsoprea/go-exif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifd_tag_entry.go
297 lines (237 loc) · 8.28 KB
/
ifd_tag_entry.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package exif
import (
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v2/common"
"github.com/dsoprea/go-exif/v2/undefined"
)
var (
iteLogger = log.NewLogger("exif.ifd_tag_entry")
)
// IfdTagEntry refers to a tag in the loaded EXIF block.
type IfdTagEntry struct {
tagId uint16
tagIndex int
tagType exifcommon.TagTypePrimitive
unitCount uint32
valueOffset uint32
rawValueOffset []byte
// childIfdName is the right most atom in the IFD-path. We need this to
// construct the fully-qualified IFD-path.
childIfdName string
// childIfdPath is the IFD-path of the child if this tag represents a child
// IFD.
childIfdPath string
// childFqIfdPath is the IFD-path of the child if this tag represents a
// child IFD. Includes indices.
childFqIfdPath string
// TODO(dustin): !! IB's host the child-IBs directly in the tag, but that's not the case here. Refactor to accommodate it for a consistent experience.
ifdIdentity *exifcommon.IfdIdentity
isUnhandledUnknown bool
addressableData []byte
byteOrder binary.ByteOrder
tagName string
}
func newIfdTagEntry(ii *exifcommon.IfdIdentity, tagId uint16, tagIndex int, tagType exifcommon.TagTypePrimitive, unitCount uint32, valueOffset uint32, rawValueOffset []byte, addressableData []byte, byteOrder binary.ByteOrder) *IfdTagEntry {
return &IfdTagEntry{
ifdIdentity: ii,
tagId: tagId,
tagIndex: tagIndex,
tagType: tagType,
unitCount: unitCount,
valueOffset: valueOffset,
rawValueOffset: rawValueOffset,
addressableData: addressableData,
byteOrder: byteOrder,
}
}
// String returns a stringified representation of the struct.
func (ite *IfdTagEntry) String() string {
return fmt.Sprintf("IfdTagEntry<TAG-IFD-PATH=[%s] TAG-ID=(0x%04x) TAG-TYPE=[%s] UNIT-COUNT=(%d)>", ite.ifdIdentity.String(), ite.tagId, ite.tagType.String(), ite.unitCount)
}
// TagName returns the name of the tag. This is determined else and set after
// the parse (since it's not actually stored in the stream). If it's empty, it
// is because it is an unknown tag (nonstandard or otherwise unavailable in the
// tag-index).
func (ite *IfdTagEntry) TagName() string {
return ite.tagName
}
// setTagName sets the tag-name. This provides the name for convenience and
// efficiency by determining it when most efficient while we're parsing rather
// than delegating it to the caller (or, worse, the user).
func (ite *IfdTagEntry) setTagName(tagName string) {
ite.tagName = tagName
}
// IfdPath returns the fully-qualified path of the IFD that owns this tag.
func (ite *IfdTagEntry) IfdPath() string {
return ite.ifdIdentity.String()
}
// TagId returns the ID of the tag that we represent. The combination of
// (IfdPath(), TagId()) is unique.
func (ite *IfdTagEntry) TagId() uint16 {
return ite.tagId
}
// IsThumbnailOffset returns true if the tag has the IFD and tag-ID of a
// thumbnail offset.
func (ite *IfdTagEntry) IsThumbnailOffset() bool {
return ite.tagId == ThumbnailOffsetTagId && ite.ifdIdentity.String() == ThumbnailFqIfdPath
}
// IsThumbnailSize returns true if the tag has the IFD and tag-ID of a thumbnail
// size.
func (ite *IfdTagEntry) IsThumbnailSize() bool {
return ite.tagId == ThumbnailSizeTagId && ite.ifdIdentity.String() == ThumbnailFqIfdPath
}
// TagType is the type of value for this tag.
func (ite *IfdTagEntry) TagType() exifcommon.TagTypePrimitive {
return ite.tagType
}
// updateTagType sets an alternatively interpreted tag-type.
func (ite *IfdTagEntry) updateTagType(tagType exifcommon.TagTypePrimitive) {
ite.tagType = tagType
}
// UnitCount returns the unit-count of the tag's value.
func (ite *IfdTagEntry) UnitCount() uint32 {
return ite.unitCount
}
// updateUnitCount sets an alternatively interpreted unit-count.
func (ite *IfdTagEntry) updateUnitCount(unitCount uint32) {
ite.unitCount = unitCount
}
// getValueOffset is the four-byte offset converted to an integer to point to
// the location of its value in the EXIF block. The "get" parameter is obviously
// used in order to differentiate the naming of the method from the field.
func (ite *IfdTagEntry) getValueOffset() uint32 {
return ite.valueOffset
}
// GetRawBytes renders a specific list of bytes from the value in this tag.
func (ite *IfdTagEntry) GetRawBytes() (rawBytes []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext := ite.getValueContext()
if ite.tagType == exifcommon.TypeUndefined {
value, err := exifundefined.Decode(valueContext)
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag {
ite.setIsUnhandledUnknown(true)
return nil, exifundefined.ErrUnparseableValue
} else if err == exifundefined.ErrUnparseableValue {
return nil, err
} else {
log.Panic(err)
}
}
// Encode it back, in order to get the raw bytes. This is the best,
// general way to do it with an undefined tag.
rawBytes, _, err := exifundefined.Encode(value, ite.byteOrder)
log.PanicIf(err)
return rawBytes, nil
}
rawBytes, err = valueContext.ReadRawEncoded()
log.PanicIf(err)
return rawBytes, nil
}
// Value returns the specific, parsed, typed value from the tag.
func (ite *IfdTagEntry) Value() (value interface{}, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext := ite.getValueContext()
if ite.tagType == exifcommon.TypeUndefined {
var err error
value, err = exifundefined.Decode(valueContext)
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag || err == exifundefined.ErrUnparseableValue {
return nil, err
}
log.Panic(err)
}
} else {
var err error
value, err = valueContext.Values()
log.PanicIf(err)
}
return value, nil
}
// Format returns the tag's value as a string.
func (ite *IfdTagEntry) Format() (phrase string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
value, err := ite.Value()
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag {
return exifundefined.UnparseableUnknownTagValuePlaceholder, nil
} else if err == exifundefined.ErrUnparseableValue {
return exifundefined.UnparseableHandledTagValuePlaceholder, nil
}
log.Panic(err)
}
phrase, err = exifcommon.FormatFromType(value, false)
log.PanicIf(err)
return phrase, nil
}
// FormatFirst returns the same as Format() but only the first item.
func (ite *IfdTagEntry) FormatFirst() (phrase string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): We should add a convenience type "timestamp", to simplify translating to and from the physical ASCII and provide validation.
value, err := ite.Value()
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag {
return exifundefined.UnparseableUnknownTagValuePlaceholder, nil
}
log.Panic(err)
}
phrase, err = exifcommon.FormatFromType(value, true)
log.PanicIf(err)
return phrase, nil
}
func (ite *IfdTagEntry) setIsUnhandledUnknown(isUnhandledUnknown bool) {
ite.isUnhandledUnknown = isUnhandledUnknown
}
// SetChildIfd sets child-IFD information (if we represent a child IFD).
func (ite *IfdTagEntry) SetChildIfd(ii *exifcommon.IfdIdentity) {
ite.childFqIfdPath = ii.String()
ite.childIfdPath = ii.UnindexedString()
ite.childIfdName = ii.Name()
}
// ChildIfdName returns the name of the child IFD
func (ite *IfdTagEntry) ChildIfdName() string {
return ite.childIfdName
}
// ChildIfdPath returns the path of the child IFD.
func (ite *IfdTagEntry) ChildIfdPath() string {
return ite.childIfdPath
}
// ChildFqIfdPath returns the complete path of the child IFD along with the
// numeric suffixes differentiating sibling occurrences of the same type. "0"
// indices are omitted.
func (ite *IfdTagEntry) ChildFqIfdPath() string {
return ite.childFqIfdPath
}
// IfdIdentity returns the IfdIdentity associated with this tag.
func (ite *IfdTagEntry) IfdIdentity() *exifcommon.IfdIdentity {
return ite.ifdIdentity
}
func (ite *IfdTagEntry) getValueContext() *exifcommon.ValueContext {
return exifcommon.NewValueContext(
ite.ifdIdentity.String(),
ite.tagId,
ite.unitCount,
ite.valueOffset,
ite.rawValueOffset,
ite.addressableData,
ite.tagType,
ite.byteOrder)
}