-
Notifications
You must be signed in to change notification settings - Fork 6
/
xmp.go
72 lines (60 loc) · 1.41 KB
/
xmp.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
package metadata
import (
"bytes"
"fmt"
"github.com/tajtiattila/metadata/xmp"
)
func FromXMPBytes(p []byte) (*Metadata, error) {
x, err := xmp.Decode(bytes.NewReader(p))
if err != nil {
return nil, err
}
return FromXMP(x), nil
}
// FromXMP decodes XMP metadata.
func FromXMP(x *xmp.Meta) *Metadata {
m := new(Metadata)
for _, a := range xmpAttr {
if v, ok := a.getf(x); ok {
m.Set(a.metaName, v)
}
}
return m
}
var xmpAttr = []struct {
metaName string
getf func(x *xmp.Meta) (string, bool)
}{
{DateTimeCreated, xmpString(xmp.CreateDate)},
{DateTimeOriginal, xmpString(xmp.DateTimeOriginal)},
{GPSDateTime, xmpString(xmp.GPSTimeStamp)},
{Rating, xmpInt(xmp.Rating)},
{GPSLatitude, xmpFloat(xmp.GPSLatitude)},
{GPSLongitude, xmpFloat(xmp.GPSLongitude)},
{Orientation, xmpInt(xmp.Orientation)},
{Make, xmpString(xmp.Make)},
{Model, xmpString(xmp.Model)},
}
func xmpString(a xmp.StringFunc) func(x *xmp.Meta) (string, bool) {
return func(x *xmp.Meta) (string, bool) {
return x.String(a)
}
}
func xmpFloat(a xmp.Float64Func) func(x *xmp.Meta) (string, bool) {
return func(x *xmp.Meta) (string, bool) {
f, ok := x.Float64(a)
if ok {
return fmt.Sprintf("%v", f), true
}
return "", false
}
}
func xmpInt(a xmp.IntFunc) func(x *xmp.Meta) (string, bool) {
return func(x *xmp.Meta) (string, bool) {
i, ok := x.Int(a)
if ok {
return fmt.Sprintf("%d", i), true
}
return "", false
}
}