forked from NectGmbH/metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpeg.go
87 lines (72 loc) · 1.38 KB
/
jpeg.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
package metadata
import (
"io"
xjpeg "github.com/tajtiattila/metadata/jpeg"
)
var jpegExifPfx = []byte("Exif\x00\x00")
var jpegXMPPfx = []byte("http://ns.adobe.com/xap/1.0/\x00")
func parseJpeg(r io.Reader) (*Metadata, error) {
j, err := xjpeg.NewScanner(r)
if err != nil {
return nil, err
}
var ex, xmp []byte
for (ex == nil || xmp == nil) && j.NextChunk() {
p := j.Bytes()
if len(p) < 4 || p[0] != 0xff || p[1] != 0xe1 {
continue
}
var pdata *[]byte
var trim int
switch {
case ex == nil && j.IsChunk(0xe1, jpegExifPfx):
pdata, trim = &ex, len(jpegExifPfx)
case xmp == nil && j.IsChunk(0xe1, jpegXMPPfx):
pdata, trim = &xmp, len(jpegXMPPfx)
}
if pdata == nil {
continue
}
_, p, err := j.ReadChunk()
if err != nil {
return nil, err
}
*pdata = p[trim:]
}
if ex == nil && xmp == nil {
if err = j.Err(); err != nil {
return nil, err
}
return nil, ErrNoMeta
}
var meta []*Metadata
var firstErr error
if ex != nil {
m, err := FromExifBytes(ex)
if m != nil {
meta = append(meta, m)
}
if err != nil {
firstErr = err
}
}
if xmp != nil {
m, err := FromXMPBytes(xmp)
if m != nil {
meta = append(meta, m)
}
if err != nil {
if firstErr == nil {
firstErr = err
}
}
}
if len(meta) == 0 {
err := firstErr
if err == nil {
err = ErrNoMeta
}
return nil, err
}
return Merge(meta...), nil
}