-
Notifications
You must be signed in to change notification settings - Fork 9
/
loader.go
165 lines (156 loc) · 4.1 KB
/
loader.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
package cogger
import (
"fmt"
"io"
"sort"
"github.com/google/tiff"
)
func loadMultipleTIFFs(tifs []tiff.TIFF) ([]*ifd, error) {
ifds := make([]*ifd, 0)
for it, tif := range tifs {
tifds := tif.IFDs()
for i := range tifds {
ifd, err := loadIFD(tif.R(), tifds[i])
if err != nil {
return nil, err
}
if it != 0 {
//check that the additional files are smaller than the first, i.e. that they represent an overview
if ifd.ImageLength >= ifds[0].ImageLength || ifd.ImageWidth >= ifds[0].ImageWidth {
return nil, fmt.Errorf("provided tiff %d size %dx%d is larger than first tiff size %dx%d. when using multiple files, the subsequent ones must be overviews of the first one",
it, ifd.ImageWidth, ifd.ImageLength, ifds[0].ImageWidth, ifds[0].ImageLength)
}
//force to overview
ifd.SubfileType |= subfileTypeReducedImage
}
ifds = append(ifds, ifd)
}
}
return ifds, nil
}
func loadSingleTIFF(tif tiff.TIFF) ([]*ifd, error) {
tifds := tif.IFDs()
ifds := make([]*ifd, len(tifds))
var err error
for i := range tifds {
ifds[i], err = loadIFD(tif.R(), tifds[i])
if err != nil {
return nil, err
}
}
return ifds, nil
}
func loadIFD(r tiff.BReader, tifd tiff.IFD) (*ifd, error) {
ifd := &ifd{r: r}
err := tiff.UnmarshalIFD(tifd, ifd)
if err != nil {
return nil, err
}
if len(ifd.TempTileByteCounts) > 0 {
ifd.TileByteCounts = make([]uint32, len(ifd.TempTileByteCounts))
for i := range ifd.TempTileByteCounts {
ifd.TileByteCounts[i] = uint32(ifd.TempTileByteCounts[i])
}
ifd.TempTileByteCounts = nil //reclaim mem
}
return ifd, nil
}
// Rewrite reshuffles the tiff bytes provided as readers into a COG output
// to out
func Rewrite(out io.Writer, readers ...tiff.ReadAtReadSeeker) error {
tiffs := []tiff.TIFF{}
if len(readers) == 0 {
return fmt.Errorf("missing readers")
}
for i, r := range readers {
tif, err := tiff.Parse(r, nil, nil)
if err != nil {
return fmt.Errorf("parse tiff %d: %w", i, err)
}
tiffs = append(tiffs, tif)
}
err := sanityCheck(tiffs)
if err != nil {
return fmt.Errorf("consistency check: %w", err)
}
var ifds []*ifd
if len(tiffs) > 1 {
ifds, err = loadMultipleTIFFs(tiffs)
if err != nil {
return fmt.Errorf("load: %w", err)
}
} else {
ifds, err = loadSingleTIFF(tiffs[0])
if err != nil {
return fmt.Errorf("load: %w", err)
}
}
sort.Slice(ifds, func(i, j int) bool {
//return in order: fullres, fullresmasks, ovr1, ovr1masks, ovr2, ....
if ifds[i].ImageLength*ifds[i].ImageWidth != ifds[j].ImageLength*ifds[j].ImageWidth {
return ifds[i].ImageLength*ifds[i].ImageWidth > ifds[j].ImageLength*ifds[j].ImageWidth
}
return ifds[i].SubfileType < ifds[j].SubfileType
})
if ifds[0].SubfileType != 0 {
return fmt.Errorf("failed sort: first px=%dx%d type=%d", ifds[0].ImageLength, ifds[0].ImageWidth, ifds[0].SubfileType)
}
cog := new()
cog.ifd = ifds[0]
curOvr := cog.ifd
s := curOvr.ImageLength * curOvr.ImageWidth
for _, ci := range ifds[1:] {
if ci.ImageLength*ci.ImageWidth == s {
err = curOvr.AddMask(ci)
if err != nil {
return err
}
} else {
curOvr.AddOverview(ci)
curOvr = ci
s = curOvr.ImageLength * curOvr.ImageWidth
}
}
err = cog.write(out)
if err != nil {
return fmt.Errorf("mucog write: %w", err)
}
return nil
}
func sanityCheck(tiffs []tiff.TIFF) error {
if len(tiffs) == 0 {
return fmt.Errorf("no tiffs")
}
order := tiffs[0].Order()
if order != "MM" && order != "II" {
return fmt.Errorf("unknown byte order")
}
for it, tif := range tiffs {
if tif.Order() != order {
return fmt.Errorf("inconsistent byte order")
}
for ii, ifd := range tif.IFDs() {
err := sanityCheckIFD(ifd)
if err != nil {
return fmt.Errorf("tif %d ifd %d: %w", it, ii, err)
}
}
}
return nil
}
func sanityCheckIFD(ifd tiff.IFD) error {
to := ifd.GetField(324)
tl := ifd.GetField(325)
if to == nil || tl == nil {
return fmt.Errorf("no tiles")
}
if to.Count() != tl.Count() {
return fmt.Errorf("inconsistent tile off/len count")
}
so := ifd.GetField(272)
sl := ifd.GetField(279)
if so != nil || sl != nil {
return fmt.Errorf("tif has strips")
}
return nil
}