This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtiff.go
186 lines (154 loc) · 4.09 KB
/
tiff.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
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tiff
import (
"encoding/binary"
"fmt"
"io"
"sync"
)
// These constants represents the first 4 bytes of the file for each kind of
// TIFF along with each byte ordering. This is mostly useful for registration
// with the "image" package from the Go standard library.
const (
MagicBigEndian = "MM\x00\x2A"
MagicLitEndian = "II\x2A\x00"
Version uint16 = 0x2A
VersionName string = "TIFF"
)
// These constants represent the byte order options present at the beginning of
// a TIFF file.
const (
BigEndian uint16 = 0x4D4D // "MM" or 19789
LitEndian uint16 = 0x4949 // "II" or 18761
)
func GetByteOrder(bo uint16) binary.ByteOrder {
switch bo {
case BigEndian:
return binary.BigEndian
case LitEndian:
return binary.LittleEndian
}
return nil
}
type ErrInvalidByteOrder struct {
Order [2]byte
}
func (e ErrInvalidByteOrder) Error() string {
return fmt.Sprintf("tiff: invalid byte order %q", e.Order)
}
type ErrUnsuppTIFFVersion struct {
Version uint16
}
func (e ErrUnsuppTIFFVersion) Error() string {
return fmt.Sprintf("tiff: unsupported version %d", e.Version)
}
type Header interface {
Order() string
Version() uint16
OffsetSize() uint16
FirstOffset() uint64
}
type TIFFParser func(ordr [2]byte, vers uint16, br BReader, tsp TagSpace, ftsp FieldTypeSpace) (TIFF, error)
type TIFF interface {
Header
IFDs() []IFD
R() BReader
}
func Parse(r ReadAtReadSeeker, tsp TagSpace, ftsp FieldTypeSpace) (TIFF, error) {
if tsp == nil {
tsp = DefaultTagSpace
}
if ftsp == nil {
ftsp = DefaultFieldTypeSpace
}
var magicBytes [4]byte
if _, err := io.ReadFull(r, magicBytes[:]); err != nil {
return nil, fmt.Errorf("tiff: unable to read byte order and version: %v", err)
}
orderBytes := [2]byte{magicBytes[0], magicBytes[1]}
byteOrder := GetByteOrder(binary.BigEndian.Uint16(orderBytes[:]))
if byteOrder == nil {
return nil, ErrInvalidByteOrder{orderBytes}
}
vers := byteOrder.Uint16(magicBytes[2:])
tp := GetVersionParser(vers)
if tp == nil {
return nil, ErrUnsuppTIFFVersion{vers}
}
return tp(orderBytes, vers, NewBReader(r, byteOrder), tsp, ftsp)
}
// Type tiff represents a standard tiff structure with 32 bit offsets.
type tiff struct {
ordr [2]byte
vers uint16
firstOff uint32
ifds []IFD
r BReader
}
func (t *tiff) Order() string {
return string(t.ordr[:])
}
func (t *tiff) Version() uint16 {
return t.vers
}
func (t *tiff) OffsetSize() uint16 {
return 4
}
func (t *tiff) FirstOffset() uint64 {
return uint64(t.firstOff)
}
func (t *tiff) IFDs() []IFD {
return t.ifds
}
func (t *tiff) R() BReader {
return t.r
}
func ParseTIFF(ordr [2]byte, vers uint16, br BReader, tsp TagSpace, ftsp FieldTypeSpace) (out TIFF, err error) {
if tsp == nil {
tsp = DefaultTagSpace
}
if ftsp == nil {
ftsp = DefaultFieldTypeSpace
}
var firstOffset uint32
// Get the offset to the first IFD
if err = br.BRead(&firstOffset); err != nil {
return nil, fmt.Errorf("tiff: unable to read offset to first ifd: %v", err)
}
// Check the offset to the first IFD (ensure it is past the end of the header)
if firstOffset < 8 {
return nil, fmt.Errorf("tiff: invalid offset to first IFD, %d < 8", firstOffset)
}
t := &tiff{ordr: ordr, vers: vers, firstOff: firstOffset, r: br}
// Locate and decode IFDs
for nextOffset := uint64(firstOffset); nextOffset != 0; {
var ifd IFD
if ifd, err = ParseIFD(br, nextOffset, tsp, ftsp); err != nil {
return
}
t.ifds = append(t.ifds, ifd)
nextOffset = ifd.NextOffset()
}
return t, nil
}
var versionParsers = struct {
mu sync.RWMutex
parsers map[uint16]TIFFParser
}{
parsers: make(map[uint16]TIFFParser, 1),
}
func RegisterVersion(v uint16, tp TIFFParser) {
versionParsers.mu.Lock()
defer versionParsers.mu.Unlock()
versionParsers.parsers[v] = tp
}
func GetVersionParser(v uint16) TIFFParser {
versionParsers.mu.RLock()
defer versionParsers.mu.RUnlock()
return versionParsers.parsers[v]
}
func init() {
RegisterVersion(Version, ParseTIFF)
}