-
Notifications
You must be signed in to change notification settings - Fork 11
/
tiff_ifd_helper.go
324 lines (298 loc) · 7.21 KB
/
tiff_ifd_helper.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
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
// Copyright 2014 <chaishushan{AT}gmail.com>. 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 (
"bytes"
"encoding/binary"
"fmt"
"image"
"image/color"
"sort"
)
func (p *IFD) Valid() bool {
if p == nil {
return false
}
if !p.Header.Valid() || len(p.EntryMap) == 0 {
return false
}
return true
}
func (p *IFD) TagGetter() TagGetter {
return &tifTagGetter{
EntryMap: p.EntryMap,
}
}
func (p *IFD) TagSetter() TagSetter {
return &tifTagSetter{
EntryMap: p.EntryMap,
}
}
func (p *IFD) Bounds() image.Rectangle {
var width, height int
if v, ok := p.TagGetter().GetImageWidth(); ok {
width = int(v)
}
if v, ok := p.TagGetter().GetImageLength(); ok {
height = int(v)
}
return image.Rect(0, 0, width, height)
}
func (p *IFD) Depth() int {
if v, ok := p.TagGetter().GetBitsPerSample(); ok {
for i := 1; i < len(v); i++ {
if v[i] != v[0] {
return 0
}
}
if len(v) > 0 {
return int(v[0])
}
}
return 0
}
func (p *IFD) Channels() int {
if v, ok := p.TagGetter().GetBitsPerSample(); ok {
return len(v)
}
return 0
}
func (p *IFD) ImageType() ImageType {
var requiredTags = []TagType{
TagType_ImageWidth,
TagType_ImageLength,
TagType_PhotometricInterpretation,
}
var requiredTiledTags = []TagType{
TagType_TileWidth,
TagType_TileLength,
TagType_TileOffsets,
TagType_TileByteCounts,
}
var requiredStripTags = []TagType{
TagType_RowsPerStrip, // default is (2^32-1)
TagType_StripOffsets,
TagType_StripByteCounts,
}
var isTiled bool
for _, tag := range requiredTiledTags {
if _, ok := p.EntryMap[tag]; ok {
isTiled = true
}
}
if isTiled {
for _, tag := range requiredTags {
if _, ok := p.EntryMap[tag]; !ok {
return ImageType_Nil
}
}
for _, tag := range requiredTiledTags {
if _, ok := p.EntryMap[tag]; !ok {
return ImageType_Nil
}
}
} else {
for _, tag := range requiredTags {
if _, ok := p.EntryMap[tag]; !ok {
return ImageType_Nil
}
}
for _, tag := range requiredStripTags {
if _, ok := p.EntryMap[tag]; !ok {
if tag != TagType_RowsPerStrip {
return ImageType_Nil
}
}
}
}
var (
photometric, _ = p.TagGetter().GetPhotometricInterpretation()
bitsPerSample, _ = p.TagGetter().GetBitsPerSample()
extraSamples, hasExtraSamples = p.TagGetter().GetExtraSamples()
)
switch photometric {
case TagValue_PhotometricType_WhiteIsZero:
if len(bitsPerSample) == 1 && bitsPerSample[0] < 8 {
return ImageType_BilevelInvert
} else {
return ImageType_GrayInvert
}
case TagValue_PhotometricType_BlackIsZero:
if len(bitsPerSample) == 1 && bitsPerSample[0] < 8 {
return ImageType_Bilevel
} else {
return ImageType_Gray
}
case TagValue_PhotometricType_RGB:
if p.Channels() == 3 {
return ImageType_RGB
}
if p.Channels() == 4 {
if hasExtraSamples && extraSamples == 1 {
return ImageType_RGBA
}
if hasExtraSamples && extraSamples == 2 {
return ImageType_NRGBA
}
}
return ImageType_Nil
case TagValue_PhotometricType_Paletted:
return ImageType_Paletted
case TagValue_PhotometricType_TransMask:
return ImageType_Nil
case TagValue_PhotometricType_CMYK:
return ImageType_Nil
case TagValue_PhotometricType_YCbCr:
return ImageType_Nil
case TagValue_PhotometricType_CIELab:
return ImageType_Nil
}
return ImageType_Nil
}
func (p *IFD) ImageConfig() (config image.Config, err error) {
var (
imageWidth, _ = p.TagGetter().GetImageWidth()
imageHeight, _ = p.TagGetter().GetImageLength()
photometric, _ = p.TagGetter().GetPhotometricInterpretation()
bitsPerSample, _ = p.TagGetter().GetBitsPerSample()
extraSamples, _ = p.TagGetter().GetExtraSamples()
)
if len(bitsPerSample) == 0 {
err = fmt.Errorf("tiff: IFD.ColorModel, bad bitsPerSample length")
return
}
config.Width = int(imageWidth)
config.Height = int(imageHeight)
switch photometric {
case TagValue_PhotometricType_RGB:
if bitsPerSample[0] == 16 {
for _, b := range bitsPerSample {
if b != 16 {
err = fmt.Errorf("tiff: IFD.ColorModel, wrong number of samples for 16bit RGB")
return
}
}
} else {
for _, b := range bitsPerSample {
if b != 8 {
err = fmt.Errorf("tiff: IFD.ColorModel, wrong number of samples for 8bit RGB")
return
}
}
}
switch len(bitsPerSample) {
case 3:
if bitsPerSample[0] == 16 {
config.ColorModel = color.RGBA64Model
} else {
config.ColorModel = color.RGBAModel
}
case 4:
switch extraSamples {
case 1:
if bitsPerSample[0] == 16 {
config.ColorModel = color.RGBA64Model
} else {
config.ColorModel = color.RGBAModel
}
case 2:
if bitsPerSample[0] == 16 {
config.ColorModel = color.NRGBA64Model
} else {
config.ColorModel = color.NRGBAModel
}
default:
err = fmt.Errorf("tiff: IFD.ColorModel, wrong number of samples for RGB")
return
}
default:
err = fmt.Errorf("tiff: IFD.ColorModel, wrong number of samples for RGB")
return
}
case TagValue_PhotometricType_Paletted:
config.ColorModel = color.Palette(p.ColorMap())
case TagValue_PhotometricType_WhiteIsZero:
if bitsPerSample[0] == 16 {
config.ColorModel = color.Gray16Model
} else {
config.ColorModel = color.GrayModel
}
case TagValue_PhotometricType_BlackIsZero:
if bitsPerSample[0] == 16 {
config.ColorModel = color.Gray16Model
} else {
config.ColorModel = color.GrayModel
}
default:
err = fmt.Errorf("tiff: decoder.Decode, unsupport color model")
return
}
return
}
func (p *IFD) Compression() TagValue_CompressionType {
if tag, ok := p.EntryMap[TagType_Compression]; ok {
if v := tag.GetInts(); len(v) == 1 {
return TagValue_CompressionType(v[0])
}
}
return TagValue_CompressionType_Nil
}
func (p *IFD) ColorMap() (palette color.Palette) {
v, ok := p.TagGetter().GetColorMap()
if !ok {
return
}
palette = make([]color.Color, len(v))
for i := 0; i < len(palette); i++ {
palette[i] = color.RGBA64{
uint16(v[i][0]),
uint16(v[i][1]),
uint16(v[i][2]),
0xffff,
}
}
return
}
func (p *IFD) Bytes() []byte {
if !p.Valid() {
return nil
}
tagList := make([]*IFDEntry, 0, len(p.EntryMap))
for _, v := range p.EntryMap {
tagList = append(tagList, v)
}
sort.Sort(byIFDEntry(tagList))
var buf bytes.Buffer
if p.Header.TiffType == TiffType_ClassicTIFF {
binary.Write(&buf, p.Header.ByteOrder, uint32(len(tagList)))
for i := 0; i < len(tagList); i++ {
entryBytes, _ := tagList[i].Bytes()
buf.Write(entryBytes)
}
binary.Write(&buf, p.Header.ByteOrder, uint32(p.NextIFD))
} else {
binary.Write(&buf, p.Header.ByteOrder, uint32(len(tagList)))
for i := 0; i < len(tagList); i++ {
entryBytes, _ := tagList[i].Bytes()
buf.Write(entryBytes)
}
binary.Write(&buf, p.Header.ByteOrder, uint32(p.NextIFD))
}
return buf.Bytes()
}
func (p *IFD) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "tiff.IFD(%#08x) {\n", p.ThisIFD)
tagList := make([]*IFDEntry, 0, len(p.EntryMap))
for _, v := range p.EntryMap {
tagList = append(tagList, v)
}
sort.Sort(byIFDEntry(tagList))
for _, v := range tagList {
fmt.Fprintf(&buf, "\t%v,\n", v)
}
fmt.Fprintf(&buf, "\tNext: %#08x,\n", p.NextIFD)
fmt.Fprintf(&buf, "}")
return buf.String()
}