Repository navigation
Expand file tree
/
Copy pathexif.go
More file actions
597 lines (524 loc) · 15.5 KB
/
Copy pathexif.go
File metadata and controls
597 lines (524 loc) · 15.5 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package webp
import (
"encoding/binary"
"errors"
"fmt"
"image"
"io"
)
// ErrNoExif is returned by DecodeExif when the WEBP has no EXIF chunk.
var ErrNoExif = errors.New("webp: no exif data")
// Exif holds the EXIF metadata decoded from a WEBP image.
type Exif struct {
// Basic image info
Orientation int // EXIF orientation (1-8). 1 = normal, values 2-8 indicate rotation/flip.
Width int // Image width in pixels.
Height int // Image height in pixels.
// Camera info
Make string // Camera manufacturer (e.g., "Canon").
Model string // Camera model (e.g., "Canon EOS 5D Mark III").
Software string // Software used to process/create the image.
// Date/Time (format: "YYYY:MM:DD HH:MM:SS")
DateTime string // File modification date/time.
DateTimeOriginal string // Original capture date/time (when photo was taken).
// Exposure settings
ExposureTime float64 // Shutter speed in seconds (e.g., 0.004 = 1/250s).
FNumber float64 // Aperture f-number (e.g., 5.6 = f/5.6).
ISOSpeed int // ISO speed rating (e.g., 800).
FocalLength float64 // Lens focal length in millimeters.
Flash int // Flash mode/status (0 = no flash, non-zero = flash fired).
// GPS location
GPSLatitude float64 // Latitude in decimal degrees (positive = North, negative = South).
GPSLongitude float64 // Longitude in decimal degrees (positive = East, negative = West).
GPSAltitude float64 // Altitude in meters above sea level.
// Copyright/Author
Copyright string // Copyright notice.
Artist string // Creator/photographer name.
}
// DecodeExif reads the EXIF metadata from a WEBP image. It returns ErrNoExif if the image carries no EXIF chunk.
func DecodeExif(r io.Reader) (*Exif, error) {
tiff := exifChunkReader(r)
if tiff == nil {
return nil, ErrNoExif
}
exif := &Exif{Orientation: 1}
if err := parseExifData(tiff, exif); err != nil {
return nil, fmt.Errorf("webp: %w", err)
}
return exif, nil
}
// exifChunkReader streams the RIFF chunks, discarding chunk bodies until the EXIF payload is found.
func exifChunkReader(r io.Reader) []byte {
var hdr [12]byte
if _, err := io.ReadFull(r, hdr[:]); err != nil {
return nil
}
if string(hdr[0:4]) != "RIFF" || string(hdr[8:12]) != "WEBP" {
return nil
}
var ch [8]byte
for {
if _, err := io.ReadFull(r, ch[:]); err != nil {
return nil
}
fourcc := string(ch[0:4])
size := int64(binary.LittleEndian.Uint32(ch[4:8]))
if fourcc == "EXIF" {
payload := make([]byte, size)
if _, err := io.ReadFull(r, payload); err != nil {
return nil
}
// Some encoders prefix the chunk with the JPEG-style "Exif\0\0" header.
if len(payload) >= 6 && string(payload[0:4]) == "Exif" && payload[4] == 0 && payload[5] == 0 {
payload = payload[6:]
}
return payload
}
if size%2 == 1 {
size++ // chunks are padded to an even size
}
if _, err := io.CopyN(io.Discard, r, size); err != nil {
return nil
}
}
}
// exifChunk returns the raw TIFF/EXIF payload of the WEBP "EXIF" RIFF chunk, or nil if absent.
func exifChunk(data []byte) []byte {
if len(data) < 12 || string(data[0:4]) != "RIFF" || string(data[8:12]) != "WEBP" {
return nil
}
off := 12
for off+8 <= len(data) {
fourcc := string(data[off : off+4])
size := int(binary.LittleEndian.Uint32(data[off+4 : off+8]))
off += 8
if size < 0 || off+size > len(data) {
break
}
if fourcc == "EXIF" {
payload := data[off : off+size]
// Some encoders prefix the chunk with the JPEG-style "Exif\0\0" header.
if len(payload) >= 6 && string(payload[0:4]) == "Exif" && payload[4] == 0 && payload[5] == 0 {
payload = payload[6:]
}
return payload
}
off += size
if size%2 == 1 {
off++ // chunks are padded to an even size
}
}
return nil
}
// exifOrientation returns the EXIF orientation (1-8) of a WEBP image, or 1 when absent.
func exifOrientation(data []byte) int {
tiff := exifChunk(data)
if tiff == nil {
return 1
}
exif := &Exif{Orientation: 1}
if parseExifData(tiff, exif) != nil || exif.Orientation < 1 || exif.Orientation > 8 {
return 1
}
return exif.Orientation
}
// orientTarget maps a source pixel to its destination for the given EXIF orientation.
func orientTarget(orientation, sx, sy, sw, sh int) (int, int) {
switch orientation {
case 2: // flip horizontal
return sw - 1 - sx, sy
case 3: // rotate 180
return sw - 1 - sx, sh - 1 - sy
case 4: // flip vertical
return sx, sh - 1 - sy
case 5: // transpose
return sy, sx
case 6: // rotate 90 CW
return sh - 1 - sy, sx
case 7: // transverse
return sh - 1 - sy, sw - 1 - sx
case 8: // rotate 270 CW
return sy, sw - 1 - sx
}
return sx, sy
}
// applyOrientation returns img rotated/flipped per the EXIF orientation (unchanged for 1 or out of range).
func applyOrientation(img image.Image, orientation int) image.Image {
if orientation <= 1 || orientation > 8 {
return img
}
b := img.Bounds()
sw, sh := b.Dx(), b.Dy()
dw, dh := sw, sh
if orientation >= 5 {
dw, dh = sh, sw // 90/270 rotations swap width and height
}
dst := image.NewRGBA(image.Rect(0, 0, dw, dh))
if src, ok := img.(*image.RGBA); ok {
for sy := 0; sy < sh; sy++ {
srow := src.Pix[(b.Min.Y+sy)*src.Stride+b.Min.X*4:]
for sx := 0; sx < sw; sx++ {
dx, dy := orientTarget(orientation, sx, sy, sw, sh)
si := sx * 4
di := dy*dst.Stride + dx*4
copy(dst.Pix[di:di+4], srow[si:si+4])
}
}
return dst
}
for sy := 0; sy < sh; sy++ {
for sx := 0; sx < sw; sx++ {
dx, dy := orientTarget(orientation, sx, sy, sw, sh)
dst.Set(dx, dy, img.At(b.Min.X+sx, b.Min.Y+sy))
}
}
return dst
}
// EXIF tag constants
const (
// Main IFD tags
tagOrientation = 0x0112
tagImageWidth = 0x0100
tagImageLength = 0x0101
tagMake = 0x010F
tagModel = 0x0110
tagSoftware = 0x0131
tagDateTime = 0x0132
tagArtist = 0x013B
tagCopyright = 0x8298
tagExifIFDPointer = 0x8769
tagGPSIFDPointer = 0x8825
// EXIF SubIFD tags
tagExposureTime = 0x829A
tagFNumber = 0x829D
tagISOSpeedRatings = 0x8827
tagDateTimeOriginal = 0x9003
tagFlash = 0x9209
tagFocalLength = 0x920A
// GPS SubIFD tags
tagGPSLatitudeRef = 0x0001
tagGPSLatitude = 0x0002
tagGPSLongitudeRef = 0x0003
tagGPSLongitude = 0x0004
tagGPSAltitudeRef = 0x0005
tagGPSAltitude = 0x0006
)
// EXIF data type constants
const (
typeUnsignedByte = 1
typeASCIIString = 2
typeUnsignedShort = 3
typeUnsignedLong = 4
typeUnsignedRational = 5
typeSignedByte = 6
typeUndefined = 7
typeSignedShort = 8
typeSignedLong = 9
typeSignedRational = 10
typeSingleFloat = 11
typeDoubleFloat = 12
)
// exifReader wraps the EXIF data with helper functions for reading different data types
type exifReader struct {
data []byte
littleEndian bool
}
func (r *exifReader) uint16(offset int) uint16 {
if offset+1 >= len(r.data) {
return 0
}
if r.littleEndian {
return uint16(r.data[offset]) | (uint16(r.data[offset+1]) << 8)
}
return (uint16(r.data[offset]) << 8) | uint16(r.data[offset+1])
}
func (r *exifReader) uint32(offset int) uint32 {
if offset+3 >= len(r.data) {
return 0
}
if r.littleEndian {
return uint32(r.data[offset]) | (uint32(r.data[offset+1]) << 8) |
(uint32(r.data[offset+2]) << 16) | (uint32(r.data[offset+3]) << 24)
}
return (uint32(r.data[offset]) << 24) | (uint32(r.data[offset+1]) << 16) |
(uint32(r.data[offset+2]) << 8) | uint32(r.data[offset+3])
}
func (r *exifReader) readString(offset, maxLen int) string {
if offset >= len(r.data) {
return ""
}
end := offset
for end < len(r.data) && end < offset+maxLen && r.data[end] != 0 {
end++
}
return string(r.data[offset:end])
}
func (r *exifReader) readRational(offset int) float64 {
if offset+7 >= len(r.data) {
return 0
}
numerator := r.uint32(offset)
denominator := r.uint32(offset + 4)
if denominator == 0 {
return 0
}
return float64(numerator) / float64(denominator)
}
// parseExifData parses the TIFF/EXIF data structure and populates the Exif struct
func parseExifData(data []byte, exif *Exif) error {
if len(data) < 8 {
return fmt.Errorf("EXIF data too short")
}
reader := &exifReader{data: data}
// Check byte order
if data[0] == 0x49 && data[1] == 0x49 {
reader.littleEndian = true // Intel (little-endian)
} else if data[0] == 0x4D && data[1] == 0x4D {
reader.littleEndian = false // Motorola (big-endian)
} else {
return fmt.Errorf("invalid EXIF byte order marker")
}
// Check magic number (42)
if reader.uint16(2) != 42 {
return fmt.Errorf("invalid EXIF magic number")
}
// Get offset to first IFD
ifdOffset := reader.uint32(4)
if ifdOffset < 8 || int(ifdOffset) >= len(data) {
return fmt.Errorf("invalid IFD offset")
}
// Parse main IFD (IFD0)
exifIFDOffset, gpsIFDOffset := parseIFD(reader, int(ifdOffset), exif)
// Parse EXIF SubIFD if present
if exifIFDOffset > 0 {
parseExifSubIFD(reader, exifIFDOffset, exif)
}
// Parse GPS SubIFD if present
if gpsIFDOffset > 0 {
parseGPSSubIFD(reader, gpsIFDOffset, exif)
}
return nil
}
// parseIFD parses an Image File Directory and returns pointers to EXIF and GPS SubIFDs
func parseIFD(reader *exifReader, offset int, exif *Exif) (exifIFDOffset, gpsIFDOffset int) {
if offset+1 >= len(reader.data) {
return 0, 0
}
numEntries := reader.uint16(offset)
offset += 2
for i := 0; i < int(numEntries); i++ {
entryOffset := offset + i*12
if entryOffset+11 >= len(reader.data) {
break
}
tag := reader.uint16(entryOffset)
dataType := reader.uint16(entryOffset + 2)
count := reader.uint32(entryOffset + 4)
valueOffset := entryOffset + 8
// For values > 4 bytes, the value field contains an offset
dataSize := getDataSize(dataType, count)
if dataSize > 4 {
valueOffset = int(reader.uint32(valueOffset))
if valueOffset >= len(reader.data) {
continue
}
}
switch tag {
case tagOrientation:
if dataType == typeUnsignedShort {
exif.Orientation = int(reader.uint16(valueOffset))
}
case tagImageWidth:
if dataType == typeUnsignedShort {
exif.Width = int(reader.uint16(valueOffset))
} else if dataType == typeUnsignedLong {
exif.Width = int(reader.uint32(valueOffset))
}
case tagImageLength:
if dataType == typeUnsignedShort {
exif.Height = int(reader.uint16(valueOffset))
} else if dataType == typeUnsignedLong {
exif.Height = int(reader.uint32(valueOffset))
}
case tagMake:
if dataType == typeASCIIString {
exif.Make = reader.readString(valueOffset, int(count))
}
case tagModel:
if dataType == typeASCIIString {
exif.Model = reader.readString(valueOffset, int(count))
}
case tagSoftware:
if dataType == typeASCIIString {
exif.Software = reader.readString(valueOffset, int(count))
}
case tagDateTime:
if dataType == typeASCIIString {
exif.DateTime = reader.readString(valueOffset, int(count))
}
case tagArtist:
if dataType == typeASCIIString {
exif.Artist = reader.readString(valueOffset, int(count))
}
case tagCopyright:
if dataType == typeASCIIString {
exif.Copyright = reader.readString(valueOffset, int(count))
}
case tagExifIFDPointer:
if dataType == typeUnsignedLong {
exifIFDOffset = int(reader.uint32(valueOffset))
}
case tagGPSIFDPointer:
if dataType == typeUnsignedLong {
gpsIFDOffset = int(reader.uint32(valueOffset))
}
}
}
return exifIFDOffset, gpsIFDOffset
}
// parseExifSubIFD parses the EXIF SubIFD for camera settings
func parseExifSubIFD(reader *exifReader, offset int, exif *Exif) {
if offset+1 >= len(reader.data) {
return
}
numEntries := reader.uint16(offset)
offset += 2
for i := 0; i < int(numEntries); i++ {
entryOffset := offset + i*12
if entryOffset+11 >= len(reader.data) {
break
}
tag := reader.uint16(entryOffset)
dataType := reader.uint16(entryOffset + 2)
count := reader.uint32(entryOffset + 4)
valueOffset := entryOffset + 8
dataSize := getDataSize(dataType, count)
if dataSize > 4 {
valueOffset = int(reader.uint32(valueOffset))
if valueOffset >= len(reader.data) {
continue
}
}
switch tag {
case tagExposureTime:
if dataType == typeUnsignedRational {
exif.ExposureTime = reader.readRational(valueOffset)
}
case tagFNumber:
if dataType == typeUnsignedRational {
exif.FNumber = reader.readRational(valueOffset)
}
case tagISOSpeedRatings:
if dataType == typeUnsignedShort {
exif.ISOSpeed = int(reader.uint16(valueOffset))
}
case tagDateTimeOriginal:
if dataType == typeASCIIString {
exif.DateTimeOriginal = reader.readString(valueOffset, int(count))
}
case tagFlash:
if dataType == typeUnsignedShort {
exif.Flash = int(reader.uint16(valueOffset))
}
case tagFocalLength:
if dataType == typeUnsignedRational {
exif.FocalLength = reader.readRational(valueOffset)
}
}
}
}
// parseGPSSubIFD parses the GPS SubIFD for location data
func parseGPSSubIFD(reader *exifReader, offset int, exif *Exif) {
if offset+1 >= len(reader.data) {
return
}
numEntries := reader.uint16(offset)
offset += 2
var latRef, lonRef string
var latValues, lonValues []float64
for i := 0; i < int(numEntries); i++ {
entryOffset := offset + i*12
if entryOffset+11 >= len(reader.data) {
break
}
tag := reader.uint16(entryOffset)
dataType := reader.uint16(entryOffset + 2)
count := reader.uint32(entryOffset + 4)
valueOffset := entryOffset + 8
dataSize := getDataSize(dataType, count)
if dataSize > 4 {
valueOffset = int(reader.uint32(valueOffset))
if valueOffset >= len(reader.data) {
continue
}
}
switch tag {
case tagGPSLatitudeRef:
if dataType == typeASCIIString {
latRef = reader.readString(valueOffset, 2)
}
case tagGPSLatitude:
if dataType == typeUnsignedRational && count == 3 {
latValues = []float64{
reader.readRational(valueOffset),
reader.readRational(valueOffset + 8),
reader.readRational(valueOffset + 16),
}
}
case tagGPSLongitudeRef:
if dataType == typeASCIIString {
lonRef = reader.readString(valueOffset, 2)
}
case tagGPSLongitude:
if dataType == typeUnsignedRational && count == 3 {
lonValues = []float64{
reader.readRational(valueOffset),
reader.readRational(valueOffset + 8),
reader.readRational(valueOffset + 16),
}
}
case tagGPSAltitude:
if dataType == typeUnsignedRational {
alt := reader.readRational(valueOffset)
// Check altitude reference (0 = above sea level, 1 = below)
if entryOffset+12 < len(reader.data) {
altRef := reader.data[valueOffset-4] // Previous tag should be altitude ref
if altRef == 1 {
alt = -alt
}
}
exif.GPSAltitude = alt
}
}
}
// Convert GPS coordinates from degrees/minutes/seconds to decimal degrees
if len(latValues) == 3 {
exif.GPSLatitude = latValues[0] + latValues[1]/60.0 + latValues[2]/3600.0
if latRef == "S" {
exif.GPSLatitude = -exif.GPSLatitude
}
}
if len(lonValues) == 3 {
exif.GPSLongitude = lonValues[0] + lonValues[1]/60.0 + lonValues[2]/3600.0
if lonRef == "W" {
exif.GPSLongitude = -exif.GPSLongitude
}
}
}
// getDataSize calculates the size in bytes for a given EXIF data type and count
func getDataSize(dataType uint16, count uint32) int {
var componentSize int
switch dataType {
case typeUnsignedByte, typeSignedByte, typeASCIIString, typeUndefined:
componentSize = 1
case typeUnsignedShort, typeSignedShort:
componentSize = 2
case typeUnsignedLong, typeSignedLong, typeSingleFloat:
componentSize = 4
case typeUnsignedRational, typeSignedRational, typeDoubleFloat:
componentSize = 8
default:
componentSize = 1
}
return componentSize * int(count)
}