-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimageinput_test.go
241 lines (199 loc) · 5.16 KB
/
imageinput_test.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
package oiio
import (
"testing"
)
func TestOpenImageInput(t *testing.T) {
// Open New
in, err := OpenImageInput(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
if !in.ValidFile(TEST_IMAGE) {
t.Errorf("Test image %q should have been a valid file", TEST_IMAGE)
}
// Aribitrary feature name, just to test
in.Supports("xyz")
actual := in.FormatName()
if actual != "png" {
t.Errorf("Expected FormatName 'png' but got %q", actual)
}
checkFatalError(t, in.Close())
// Re-open
checkFatalError(t, in.Open(TEST_IMAGE))
if actual = in.FormatName(); actual != "png" {
t.Errorf("Expected FormatName 'png' but got %q", actual)
}
}
func TestImageInputReadImage(t *testing.T) {
in, err := OpenImageInput(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
// Simple read
var pixels []float32
pixels, err = in.ReadImage()
if err != nil {
t.Fatal(err.Error())
}
if pixels[0] == 0 {
t.Fatal("First pixel of test image was 0")
}
}
func TestImageInputReadImageFormat(t *testing.T) {
in, err := OpenImageInput(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
var pixel_iface interface{}
// nil Callback read
//
pixel_iface, err = in.ReadImageFormat(TypeFloat, nil)
if err != nil {
t.Fatal(err.Error())
}
float_pixels, ok := pixel_iface.([]float32)
if !ok {
t.Fatal("Interface could not be converted to a []float21")
}
if float_pixels[0] == 0 {
t.Fatal("First pixel of test image was 0")
}
// With callback
//
var progress ProgressCallback = func(done float32) bool {
// no cancel
return false
}
pixel_iface, err = in.ReadImageFormat(TypeFloat, &progress)
if err != nil {
t.Fatal(err.Error())
}
float_pixels, _ = pixel_iface.([]float32)
if float_pixels[0] == 0 {
t.Fatal("First pixel of test image was 0")
}
// With callback
//
progress = func(done float32) bool {
// cancel
return true
}
pixel_iface, err = in.ReadImageFormat(TypeFloat, &progress)
if err != nil {
t.Fatal(err.Error())
}
float_pixels, _ = pixel_iface.([]float32)
if float_pixels[0] != 0 {
t.Fatal("First pixel of test image should be 0, since callback issued a cancel")
}
}
func TestImageInputReadScanline(t *testing.T) {
in, err := OpenImageInput(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
var pixels []float32
pixels, err = in.ReadScanline(0, 0)
if err != nil {
t.Fatal(err.Error())
}
if pixels[0] == 0 {
t.Fatal("First pixel of test image was 0")
}
}
func TestImageInputReadTile(t *testing.T) {
filepath := `testdata/checker_mip.tx`
in, err := OpenImageInput(filepath)
checkFatalError(t, err)
var pixels []float32
pixels, err = in.ReadTile(0, 0, 0)
checkFatalError(t, err)
size := 64
expected := size * size
actual := len(pixels)
if actual != expected {
t.Fatalf("Expected tile to have %d pixels, got %d", expected, actual)
}
if pixels[0] != 0 {
t.Fatalf("Expected first pixel of first tile to be black. Got %0.2f", pixels[0])
}
pixels, err = in.ReadTile(size, size, 0)
checkFatalError(t, err)
pixel := pixels[63]
if pixel != 1 {
t.Fatalf("Expected pixel at the end of first line of last tile to be white. Got %0.2f", pixel)
}
}
func TestImageInputSubimage(t *testing.T) {
filepath := `testdata/subimages.exr`
in, err := OpenImageInput(filepath)
checkFatalError(t, err)
expected := 0
actual := in.CurrentSubimage()
if actual != expected {
t.Fatalf("Expected subimage %d, got %d", expected, actual)
}
expected = 1
ok := in.SeekSubimage(expected, nil)
if !ok {
t.Fatalf("Failed while seeking to subimage %d", expected)
}
actual = in.CurrentSubimage()
if actual != expected {
t.Fatalf("Expected subimage %d, got %d", expected, actual)
}
expected = 2
newSpec := NewImageSpec(TypeUnknown)
ok = in.SeekSubimage(expected, newSpec)
if !ok {
t.Fatalf("Failed while seeking to subimage %d", expected)
}
actual = in.CurrentSubimage()
if actual != expected {
t.Fatalf("Expected subimage %d, got %d", expected, actual)
}
nchans := newSpec.NumChannels()
if nchans != 1 {
t.Fatalf("Expected to find 1 channel in subimage %d, got %d", expected, nchans)
}
if newSpec.Format() != in.Spec().Format() {
t.Fatalf("Expected subimage format %v, got %v", in.Spec().Format(), newSpec.Format())
}
}
func TestImageInputMipLevel(t *testing.T) {
filepath := `testdata/checker_mip.tx`
in, err := OpenImageInput(filepath)
checkFatalError(t, err)
expected := 0
actual := in.CurrentMipLevel()
if actual != expected {
t.Fatalf("Expected mip level %d, got %d", expected, actual)
}
expected = 1
ok := in.SeekMipLevel(0, expected, nil)
if !ok {
t.Fatalf("Failed while seeking to Mip level %d", expected)
}
actual = in.CurrentMipLevel()
if actual != expected {
t.Fatalf("Expected mip level %d, got %d", expected, actual)
}
expected = 2
newSpec := NewImageSpec(TypeUnknown)
ok = in.SeekMipLevel(0, expected, newSpec)
if !ok {
t.Fatalf("Failed while seeking to mip level %d", expected)
}
actual = in.CurrentMipLevel()
if actual != expected {
t.Fatalf("Expected mip level %d, got %d", expected, actual)
}
expected = 8
actual = 0
for i := 0; in.SeekMipLevel(0, i, newSpec); i++ {
actual++
}
if actual != expected {
t.Fatalf("Expected total number of mip levels to be %d, got %d", expected, actual)
}
}