generated from bep/golibtemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagemeta.go
455 lines (383 loc) · 9.3 KB
/
imagemeta.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright 2024 Bjørn Erik Pedersen
// SPDX-License-Identifier: MIT
package imagemeta
import (
"encoding/binary"
"fmt"
"io"
"maps"
"math"
"os"
"strings"
"time"
)
// UnknownPrefix is used as prefix for unknown tags.
const UnknownPrefix = "UnknownTag_"
const (
// EXIF is the EXIF tag source.
EXIF Source = 1 << iota
// IPTC is the IPTC tag source.
IPTC
// XMP is the XMP tag source.
XMP
)
var (
// ErrStopWalking is a sentinel error to signal that the walk should stop.
ErrStopWalking = fmt.Errorf("stop walking")
// Internal error to signal that we should stop any further processing.
errStop = fmt.Errorf("stop")
)
const (
// ImageFormatAuto signals that the image format should be detected automatically (not implemented yet).
ImageFormatAuto ImageFormat = iota
// JPEG is the JPEG image format.
JPEG
// TIFF is the TIFF image format.
TIFF
// PNG is the PNG image format.
PNG
// WebP is the WebP image format.
WebP
)
// Decode reads EXIF and IPTC metadata from r and returns a Meta struct.
func Decode(opts Options) (err error) {
var base *baseStreamingDecoder
defer func() {
if r := recover(); r != nil {
if errp, ok := r.(error); ok {
if isInvalidFormatErrorCandidate(errp) {
err = newInvalidFormatError(errp)
} else {
err = errp
if err != errStop {
printStackTrace(os.Stderr)
}
}
} else {
err = fmt.Errorf("unknown panic: %v", r)
printStackTrace(os.Stderr)
}
}
if err == ErrStopWalking {
err = nil
return
}
if err == errStop {
err = nil
}
if err == nil {
if base != nil {
err = base.streamErr()
}
}
if err == nil {
return
}
if err == io.EOF {
err = nil
return
}
if isInvalidFormatErrorCandidate(err) {
err = newInvalidFormatError(err)
}
}()
if opts.R == nil {
return fmt.Errorf("no reader provided")
}
if opts.ImageFormat == ImageFormatAuto {
return fmt.Errorf("no image format provided; format detection not implemented yet")
}
if opts.ShouldHandleTag == nil {
opts.ShouldHandleTag = func(ti TagInfo) bool {
if ti.Source != EXIF {
return true
}
// Skip all tags in the thumbnails IFD (IFD1).
return strings.HasPrefix(ti.Namespace, "IFD0")
}
}
if opts.HandleTag == nil {
opts.HandleTag = func(TagInfo) error { return nil }
}
if opts.Sources == 0 {
opts.Sources = EXIF | IPTC | XMP
}
if opts.Warnf == nil {
opts.Warnf = func(string, ...any) {}
}
var sourceSet Source
// Remove sources not supported by the format.
switch opts.ImageFormat {
case JPEG:
sourceSet = EXIF | XMP | IPTC
case TIFF:
sourceSet = EXIF | XMP | IPTC
case WebP:
sourceSet = EXIF | XMP
case PNG:
sourceSet = EXIF | XMP | IPTC
default:
return fmt.Errorf("unsupported image format")
}
// Remove sources that are not requested.
sourceSet = sourceSet & opts.Sources
opts.Sources = sourceSet
if opts.Sources.IsZero() {
return nil
}
br := &streamReader{
r: opts.R,
byteOrder: binary.BigEndian,
}
base = &baseStreamingDecoder{
streamReader: br,
opts: opts,
}
var dec decoder
switch opts.ImageFormat {
case JPEG:
dec = &imageDecoderJPEG{baseStreamingDecoder: base}
case TIFF:
dec = &imageDecoderTIF{baseStreamingDecoder: base}
case WebP:
base.byteOrder = binary.LittleEndian
dec = &decoderWebP{baseStreamingDecoder: base}
case PNG:
dec = &imageDecoderPNG{baseStreamingDecoder: base}
}
if opts.Timeout > 0 {
errc := make(chan error, 1)
go func() {
defer func() {
if r := recover(); r != nil {
if errp, ok := r.(error); ok {
if errp != errStop {
printStackTrace(os.Stderr)
}
errc <- errp
} else {
errc <- fmt.Errorf("unknown panic: %v", r)
printStackTrace(os.Stderr)
}
}
}()
select {
case <-time.After(opts.Timeout):
printStackTrace(os.Stderr)
errc <- fmt.Errorf("timed out after %s", opts.Timeout)
case errc <- dec.decode():
}
}()
err = <-errc
} else {
err = dec.decode()
}
return
}
// HandleTagFunc is the function that is called for each tag.
type HandleTagFunc func(info TagInfo) error
// ImageFormat is the image format.
//
//go:generate stringer -type=ImageFormat
type ImageFormat int
// Options contains the options for the Decode function.
type Options struct {
// The Reader (typically a *os.File) to read image metadata from.
R io.ReadSeeker
// The image format in R.
ImageFormat ImageFormat
// If set, the decoder skip tags in which this function returns false.
// If not set, a default function is used that skips all EXIF tags except those in IFD0.
ShouldHandleTag func(tag TagInfo) bool
// The function to call for each tag.
HandleTag HandleTagFunc
// The default XMP handler is currently very simple:
// It decodes the RDF.Description.Attrs using Go's xml package and passes each tag to HandleTag.
// If HandleXMP is set, the decoder will call this function for each XMP packet instead.
// Note that r must be read completely.
HandleXMP func(r io.Reader) error
// If set, the decoder will only read the given tag sources.
// Note that this is a bitmask and you may send multiple sources at once.
Sources Source
// Warnf will be called for each warning.
Warnf func(string, ...any)
// Timeout is the maximum time the decoder will spend on reading metadata.
// Mostly useful for testing.
// If set to 0, the decoder will not time out.
Timeout time.Duration
}
// TagInfo contains information about a tag.
type TagInfo struct {
// The tag source.
Source Source
// The tag name.
Tag string
// The tag namespace.
// For EXIF, this is the path to the IFD, e.g. "IFD0/GPSInfoIFD"
// For XMP, this is the namespace, e.g. "http://ns.adobe.com/camera-raw-settings/1.0/"
// For IPTC, this is the record tag name as defined https://exiftool.org/TagNames/IPTC.html
Namespace string
// The tag value.
Value any
}
// Source is a bitmask and you may send multiple sources at once.
//
//go:generate stringer -type=Source
type Source uint32
// Remove removes the given source.
func (t Source) Remove(source Source) Source {
t &= ^source
return t
}
// Has returns true if the given source is set.
func (t Source) Has(source Source) bool {
return t&source != 0
}
// IsZero returns true if the source is zero.
func (t Source) IsZero() bool {
return t == 0
}
// Tags is a collection of tags grouped per source.
type Tags struct {
exif map[string]TagInfo
iptc map[string]TagInfo
xmp map[string]TagInfo
}
// Add adds a tag to the correct source.
func (t *Tags) Add(tag TagInfo) {
t.getSourceMap(tag.Source)[tag.Tag] = tag
}
// Has reports if a tag is already added.
func (t *Tags) Has(tag TagInfo) bool {
_, found := t.getSourceMap(tag.Source)[tag.Tag]
return found
}
// EXIF returns the EXIF tags.
func (t *Tags) EXIF() map[string]TagInfo {
if t.exif == nil {
t.exif = make(map[string]TagInfo)
}
return t.exif
}
// IPTC returns the IPTC tags.
func (t *Tags) IPTC() map[string]TagInfo {
if t.iptc == nil {
t.iptc = make(map[string]TagInfo)
}
return t.iptc
}
// XMP returns the XMP tags.
func (t *Tags) XMP() map[string]TagInfo {
if t.xmp == nil {
t.xmp = make(map[string]TagInfo)
}
return t.xmp
}
// All returns all tags in a map.
func (t Tags) All() map[string]TagInfo {
all := make(map[string]TagInfo)
maps.Copy(all, t.EXIF())
maps.Copy(all, t.IPTC())
maps.Copy(all, t.XMP())
return all
}
// GetDateTime tries DateTimeOriginal and then DateTime,
// in the EXIF tags, and returns the parsed time.Time value if found.
func (t Tags) GetDateTime() (time.Time, error) {
dateStr := t.dateTime()
if dateStr == "" {
return time.Time{}, nil
}
loc := time.Local
if v := t.location(); v != nil {
loc = v
}
const layout = "2006:01:02 15:04:05"
return time.ParseInLocation(layout, dateStr, loc)
}
// GetLatLong returns the latitude and longitude from the EXIF GPS tags.
func (t Tags) GetLatLong() (lat float64, long float64, err error) {
var ns, ew string
exif := t.EXIF()
longTag, found := exif["GPSLongitude"]
if !found {
return
}
ewTag, found := exif["GPSLongitudeRef"]
if found {
ew = ewTag.Value.(string)
}
latTag, found := exif["GPSLatitude"]
if !found {
return
}
nsTag, found := exif["GPSLatitudeRef"]
if found {
ns = nsTag.Value.(string)
}
lat = latTag.Value.(float64)
long = longTag.Value.(float64)
if ns == "S" {
lat = -lat
}
if ew == "W" {
long = -long
}
if math.IsNaN(lat) {
lat = 0
}
if math.IsNaN(long) {
long = 0
}
return
}
func (t *Tags) getSourceMap(source Source) map[string]TagInfo {
switch source {
case EXIF:
return t.EXIF()
case IPTC:
return t.IPTC()
case XMP:
return t.XMP()
default:
return nil
}
}
func (t Tags) dateTime() string {
exif := t.EXIF()
if ti, ok := exif["DateTimeOriginal"]; ok {
return ti.Value.(string)
}
if ti, ok := exif["DateTime"]; ok {
return ti.Value.(string)
}
return ""
}
// Borrowed from github.com/rwcarlsen/goexif
// TODO(bep: look for timezone offset, GPS time, etc.
func (t Tags) location() *time.Location {
exif := t.EXIF()
timeInfo, found := exif["Canon.TimeInfo"]
if !found {
return nil
}
vals := timeInfo.Value.([]uint32)
if len(vals) < 2 {
return nil
}
return time.FixedZone("", int(vals[1]*60))
}
type baseStreamingDecoder struct {
*streamReader
opts Options
err error
}
func (d *baseStreamingDecoder) streamErr() error {
if d.err != nil {
return d.err
}
return d.readErr
}
type decoderWebP struct {
*baseStreamingDecoder
}