-
Notifications
You must be signed in to change notification settings - Fork 12
/
imagemeta.go
220 lines (186 loc) · 5.53 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
// Package imagemeta provides functions for parsing and extracting Metadata from Images.
// Different image types such as JPEG, Camera Raw, DNG, TIFF, HEIF, and AVIF.
package imagemeta
import (
"bufio"
"io"
"sync"
"github.com/evanoberholster/imagemeta/exif2"
"github.com/evanoberholster/imagemeta/imagetype"
"github.com/evanoberholster/imagemeta/isobmff"
"github.com/evanoberholster/imagemeta/jpeg"
"github.com/evanoberholster/imagemeta/meta"
"github.com/evanoberholster/imagemeta/png"
"github.com/evanoberholster/imagemeta/preview"
"github.com/evanoberholster/imagemeta/tiff"
"github.com/pkg/errors"
)
// Errors
var (
ErrNoExif = meta.ErrNoExif
ErrNoExifDecodeFn = errors.New("error no Exif Decode Func set")
ErrNoXmpDecodeFn = errors.New("error no Xmp Decode Func set")
ErrImageTypeNotFound = imagetype.ErrImageTypeNotFound
ErrMetadataNotSupported = errors.New("error metadata reading not supported for this imagetype")
)
// readerPool for buffer
var readerPool = sync.Pool{
New: func() interface{} { return bufio.NewReaderSize(nil, 4*1024) },
}
func Decode(r io.ReadSeeker) (exif2.Exif, error) {
rr := readerPool.Get().(*bufio.Reader)
rr.Reset(r)
defer readerPool.Put(rr)
ir := exif2.NewIfdReader(exif2.Logger)
defer ir.Close()
it, err := imagetype.ScanBuf(rr)
if err != nil {
return exif2.Exif{}, err
}
ir.Exif.ImageType = it
switch it {
case imagetype.ImageJPEG:
if err = jpeg.ScanJPEG(rr, ir.DecodeJPEGIfd, nil); err != nil {
return exif2.Exif{}, err
}
case imagetype.ImageCR2, imagetype.ImageTiff, imagetype.ImagePanaRAW, imagetype.ImageDNG:
header, err := tiff.ScanTiffHeader(rr, it)
if err != nil {
return exif2.Exif{}, err
}
if err := ir.DecodeTiff(rr, header); err != nil {
return ir.Exif, err
}
case imagetype.ImageCR3, imagetype.ImageAVIF:
bmr := isobmff.NewReader(rr)
defer bmr.Close()
bmr.ExifReader = ir.DecodeIfd
if err := bmr.ReadFTYP(); err != nil {
return ir.Exif, errors.Wrapf(err, "ReadFtypBox")
}
if err := bmr.ReadMetadata(); err != nil {
return ir.Exif, err
}
case imagetype.ImageHEIF:
header, err := tiff.ScanTiffHeader(rr, it)
if err != nil {
return exif2.Exif{}, err
}
if err := ir.DecodeTiff(rr, header); err != nil {
return ir.Exif, err
}
default:
return exif2.Exif{}, ErrMetadataNotSupported
}
return ir.Exif, nil
}
// DecodeCR3 decodes a CR3 file from an io.Reader returning Exif or an error.
func DecodeCR3(r io.ReadSeeker) (exif2.Exif, error) {
rr := readerPool.Get().(*bufio.Reader)
defer readerPool.Put(rr)
rr.Reset(r)
ir := exif2.NewIfdReader(exif2.Logger)
defer ir.Close()
bmr := isobmff.NewReader(rr)
defer bmr.Close()
bmr.ExifReader = ir.DecodeIfd
if err := bmr.ReadFTYP(); err != nil {
return ir.Exif, errors.Wrapf(err, "ReadFtypBox")
}
if err := bmr.ReadMetadata(); err != nil {
return ir.Exif, err
}
if err := bmr.ReadMetadata(); err != nil {
return ir.Exif, err
}
return ir.Exif, nil
}
// DecodeTiff decodes a Tiff/DNG file from an io.Reader returning Exif or an error.
func DecodeTiff(r io.ReadSeeker) (exif2.Exif, error) {
rr := readerPool.Get().(*bufio.Reader)
rr.Reset(r)
defer readerPool.Put(rr)
it, err := imagetype.ScanBuf(rr)
if err != nil {
return exif2.Exif{}, err
}
header, err := tiff.ScanTiffHeader(rr, it)
if err != nil {
return exif2.Exif{}, err
}
ir := exif2.NewIfdReader(exif2.Logger)
defer ir.Close()
if err := ir.DecodeTiff(rr, header); err != nil {
return ir.Exif, err
}
return ir.Exif, nil
//return exif2.DecodeHeader(r, moov.Meta.Exif[0], moov.Meta.Exif[1], moov.Meta.Exif[3])
}
// DecodeCR2 decodes a CR2 file from an io.Reader returning Exif or an error.
func DecodeCR2(r io.ReadSeeker) (exif2.Exif, error) {
return DecodeTiff(r)
}
// DecodeHeif decodes a Heif file from an io.Reader returning Exif or an error.
// Needs improvement
func DecodeHeif(r io.ReadSeeker) (exif2.Exif, error) {
return DecodeTiff(r)
}
// DecodeJPEG decodes a JPEG file from an io.Reader returning Exif or an error.
func DecodeJPEG(r io.ReadSeeker) (exif2.Exif, error) {
rr := readerPool.Get().(*bufio.Reader)
rr.Reset(r)
defer readerPool.Put(rr)
ir := exif2.NewIfdReader(exif2.Logger)
defer ir.Close()
it, err := imagetype.ScanBuf(rr)
if err != nil {
return exif2.Exif{}, err
}
if it != imagetype.ImageJPEG {
return exif2.Exif{}, ErrMetadataNotSupported
}
if err = jpeg.ScanJPEG(rr, ir.DecodeJPEGIfd, nil); err != nil {
return exif2.Exif{}, err
}
ir.Exif.ImageType = it
return ir.Exif, nil
}
// DecodePng decodes a PNG file from an io.Reader returning Exif or an error.
func DecodePng(r io.ReadSeeker) (exif2.Exif, error) {
header, err := png.ScanPngHeader(r)
if err != nil {
return exif2.Exif{}, err
}
ir := exif2.NewIfdReader(exif2.Logger)
defer ir.Close()
if err := ir.DecodeTiff(r, header); err != nil {
return ir.Exif, err
}
return ir.Exif, nil
}
// PreviewCR3 previews a CR3 file from an io.Reader returning preview image binary or an error.
func PreviewCR3(r io.ReadSeeker) ([]byte, error) {
rr := readerPool.Get().(*bufio.Reader)
defer readerPool.Put(rr)
rr.Reset(r)
pr := preview.NewPreviewReader(preview.Logger)
bmr := isobmff.NewReader(rr)
bmr.PreviewImageReader = pr.RenderPreview
defer bmr.Close()
if err := bmr.ReadFTYP(); err != nil {
return nil, errors.Wrapf(err, "ReadFtypBox")
}
// moov
if err := bmr.ReadMetadata(); err != nil {
return nil, err
}
// uuid xpacket
if err := bmr.ReadMetadata(); err != nil {
return nil, err
}
// uuid preview
if err := bmr.ReadMetadata(); err != nil {
return nil, err
}
return pr.PreviewImage, nil
}