forked from chai2010/tiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
104 lines (90 loc) · 2.4 KB
/
decoder.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
// Copyright 2015 <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 (
"image"
"io"
)
// DecodeConfig returns the color model and dimensions of a TIFF image without
// decoding the entire image.
func DecodeConfig(r io.Reader) (cfg image.Config, err error) {
var p *Reader
if p, err = OpenReader(r); err != nil {
return
}
defer p.Close()
return p.ImageConfig(0, 0)
}
func DecodeConfigAll(r io.Reader) (cfg [][]image.Config, errors [][]error, err error) {
var p *Reader
if p, err = OpenReader(r); err != nil {
return
}
defer p.Close()
cfg = make([][]image.Config, len(p.Ifd))
errors = make([][]error, len(p.Ifd))
Loop:
for i := 0; i < len(p.Ifd); i++ {
errors[i] = make([]error, len(p.Ifd[i]))
for j := 0; j < len(p.Ifd[i]); j++ {
if cfg[i][j], errors[i][j] = p.Ifd[i][j].ImageConfig(); errors[i][j] != nil {
break Loop
}
}
}
for i := 0; i < len(errors); i++ {
for j := 0; j < len(errors[i]); j++ {
if errors[i][j] != nil {
err = errors[i][j]
return
}
}
}
return
}
// Decode reads a TIFF image from r and returns it as an image.Image.
// The type of Image returned depends on the contents of the TIFF.
func Decode(r io.Reader) (m image.Image, err error) {
var p *Reader
if p, err = OpenReader(r); err != nil {
return
}
defer p.Close()
m, err = p.DecodeImage(0, 0)
return
}
func DecodeAll(r io.Reader) (m [][]image.Image, errors [][]error, err error) {
var p *Reader
if p, err = OpenReader(r); err != nil {
return
}
defer p.Close()
m = make([][]image.Image, p.ImageNum())
errors = make([][]error, p.ImageNum())
Loop:
for i := 0; i < p.ImageNum(); i++ {
m[i] = make([]image.Image, p.SubImageNum(i))
errors[i] = make([]error, len(p.Ifd[i]))
for j := 0; j < p.SubImageNum(i); j++ {
if m[i][j], errors[i][j] = p.DecodeImage(i, j); errors[i][j] != nil {
break Loop
}
}
}
for i := 0; i < len(errors); i++ {
for j := 0; j < len(errors[i]); j++ {
if errors[i][j] != nil {
err = errors[i][j]
return
}
}
}
return
}
func init() {
image.RegisterFormat("tiff", ClassicTiffLittleEnding, Decode, DecodeConfig)
image.RegisterFormat("tiff", ClassicTiffBigEnding, Decode, DecodeConfig)
image.RegisterFormat("tiff", BigTiffLittleEnding, Decode, DecodeConfig)
image.RegisterFormat("tiff", BigTiffBigEnding, Decode, DecodeConfig)
}