-
Notifications
You must be signed in to change notification settings - Fork 5
/
imagebuf_test.go
342 lines (279 loc) · 7.83 KB
/
imagebuf_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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package oiio
import (
"fmt"
"os"
"testing"
)
func TestNewImageBuf(t *testing.T) {
buf := NewImageBuf()
if buf.Initialized() {
t.Fatal("ImageBuf should not be considered initialized")
}
}
func TestNewImageBufInitSpec(t *testing.T) {
buf := NewImageBuf()
if buf.Initialized() {
t.Fatal("Expected ImageBuf not to be initialized")
}
err := buf.InitSpec(TEST_IMAGE, 0, 0)
if err != nil {
t.Fatal(err.Error())
}
spec := buf.Spec()
width, height := spec.Width(), spec.Height()
if width != 128 || height != 64 {
t.Errorf("Expected ImageSpec width==128, height==64, got %v, %v", width, height)
}
}
func TestImageBufReadImage(t *testing.T) {
// Open New
cache := CreateImageCache(true)
buf, err := NewImageBufPathCache(TEST_IMAGE, cache)
if err != nil {
t.Fatal(err.Error())
}
if !buf.Initialized() {
t.Fatal("ImageBuf was not initialized")
}
err = buf.Read(true)
if err != nil {
t.Fatal(err.Error())
}
storage := buf.Storage()
if storage == IBStorageUninitialized {
t.Error("Got IBStorage value: Uninitialized")
}
if buf.Name() != TEST_IMAGE {
t.Errorf("Expected name %v; got %v", TEST_IMAGE, buf.Name())
}
if buf.FileFormatName() != "png" {
t.Errorf("Expected format png; got %v", buf.FileFormatName())
}
curr, total := buf.SubImage(), buf.NumSubImages()
if curr != 0 && total != 1 {
t.Errorf("Expected SubImage 0 and total of 1; got %v, %v", curr, total)
}
curr, total = buf.MipLevel(), buf.NumMipLevels()
if curr != 0 && total != 1 {
t.Errorf("Expected MipLevel 0 and total of 1; got %v, %v", curr, total)
}
if buf.NumChannels() != 3 {
t.Errorf("Expected 3 channels in image, got %v", buf.NumChannels())
}
buf.Orientation()
width, height := buf.OrientedWidth(), buf.OrientedHeight()
if width != 128 || height != 64 {
t.Errorf("Expected width==128, height==64, got %v, %v", width, height)
}
width, height = buf.OrientedFullWidth(), buf.OrientedFullHeight()
if width != 128 || height != 64 {
t.Errorf("Expected width==128, height==64, got %v, %v", width, height)
}
x, y := buf.OrientedX(), buf.OrientedY()
if x != 0 || y != 0 {
t.Errorf("Expected orientation (0,0), got (%v,%v)", x, y)
}
x, y = buf.OrientedFullX(), buf.OrientedFullY()
if x != 0 || y != 0 {
t.Errorf("Expected orientation (0,0), got (%v,%v)", x, y)
}
x, y = buf.XMin(), buf.YMin()
if x != 0 || y != 0 {
t.Errorf("Expected origin (0,0), got (%v,%v)", x, y)
}
x, y = buf.XMax(), buf.YMax()
if x != width-1 || y != height-1 {
t.Errorf("Expected x/y max (%v,%v), got (%v,%v)", width-1, height-1, x, y)
}
}
func TestImageBufSpec(t *testing.T) {
// Open New
cache := CreateImageCache(true)
buf, err := NewImageBufPathCache(TEST_IMAGE, cache)
if err != nil {
t.Fatal(err.Error())
}
spec := buf.Spec()
width, height := spec.Width(), spec.Height()
if width != 128 || height != 64 {
t.Errorf("Expected ImageSpec width==128, height==64, got %v, %v", width, height)
}
specmod := buf.SpecMod()
width, height = specmod.Width(), specmod.Height()
if width != 128 || height != 64 {
t.Errorf("Expected ImageSpec width==128, height==64, got %v, %v", width, height)
}
if spec.Format() != buf.PixelType() {
t.Errorf("Expected TypeDesc %v, got %v", spec.Format(), buf.PixelType())
}
if !buf.PixelsValid() {
t.Error("Expected ImageBuf.PixelsValid() == true")
}
if !buf.CachedPixels() {
t.Error("Expected ImageBuf.CachedPixels() == true")
}
if cache.ptr != buf.ImageCache().ptr {
t.Error("Expected ImageBuf.ImageCache() to return the same ImageCache as originally used")
}
names := specmod.ChannelNames()
expected := []string{"B", "G", "A"}
specmod.SetChannelNames(expected)
specmod = buf.SpecMod()
names = specmod.ChannelNames()
if fmt.Sprintf("%v", names) != fmt.Sprintf("%v", expected) {
t.Errorf("Expected %v names; got %v", expected, names)
}
roi := buf.ROI()
width, height = roi.Width(), roi.Height()
if width != 128 || height != 64 {
t.Errorf("Expected ROI width==128, height==64, got %v, %v", width, height)
}
roi = buf.ROIFull()
width, height = roi.Width(), roi.Height()
if width != 128 || height != 64 {
t.Errorf("Expected ROI width==128, height==64, got %v, %v", width, height)
}
roi.SetXEnd(64)
roi.SetYEnd(32)
buf.SetROIFull(roi)
roi = buf.ROIFull()
width, height = roi.Width(), roi.Height()
if width != 64 || height != 32 {
t.Errorf("Expected ROI width==64, height==32, got %v, %v", width, height)
}
if buf.Deep() {
t.Error("Expected ImageBuf.Deep() == false")
}
}
func TestImageBufReadImageCallbacks(t *testing.T) {
// Open New
buf, err := NewImageBufPath(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
// With success callback
//
buf.Clear()
var progress ProgressCallback = func(done float32) bool {
// no cancel
return false
}
err = buf.ReadCallback(true, &progress)
if err != nil {
t.Fatal(err.Error())
}
// With stop callback
//
buf.Clear()
progress = func(done float32) bool {
// cancel
return true
}
err = buf.ReadCallback(true, &progress)
if err != nil {
t.Fatal(err.Error())
}
}
func TestImageBufWriteFile(t *testing.T) {
// Open New
buf, err := NewImageBufPath(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
outfile := createOutputFile()
defer os.Remove(outfile)
buf.SetWriteTiles(0, 0, 0)
buf.SetWriteFormat(TypeUint8)
checkFatalError(t, buf.WriteFile(outfile, ""))
info, _ := os.Stat(outfile)
if info.Size() == 0 {
t.Fatal("Image file was 0 bytes. Write failed")
}
checkFatalError(t, buf.WriteFile(outfile, "png"))
var progress ProgressCallback = func(done float32) bool {
// no cancel
return false
}
checkFatalError(t, buf.WriteFileProgress(outfile, "", &progress))
}
func TestImageBufCopySwap(t *testing.T) {
src, err := NewImageBufPath(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
other := NewImageBuf()
checkFatalError(t, other.Copy(src))
checkFatalError(t, other.CopyMetadata(src))
checkFatalError(t, other.CopyPixels(src))
checkFatalError(t, other.Swap(src))
}
func TestImageBufGetPixels(t *testing.T) {
src, err := NewImageBufPath(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
var pixel_iface interface{}
pixel_iface, err = src.GetPixels(TypeFloat)
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")
}
expected := src.OrientedWidth() * src.OrientedHeight() * src.Spec().Depth() * src.NumChannels()
actual := len(float_pixels)
if expected != actual {
t.Fatalf("Expected to get a total of %d pixels; Got %d", expected, actual)
}
}
func TestImageBufGetFloatPixels(t *testing.T) {
src, err := NewImageBufPath(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
pixels, err := src.GetFloatPixels()
if err != nil {
t.Fatal(err.Error())
}
if pixels[0] == 0 {
t.Fatal("First pixel of test image was 0")
}
expected := src.OrientedWidth() * src.OrientedHeight() * src.Spec().Depth() * src.NumChannels()
actual := len(pixels)
if expected != actual {
t.Fatalf("Expected to get a total of %d pixels; Got %d", expected, actual)
}
}
func TestImageBufGetPixelRegion(t *testing.T) {
src, err := NewImageBufPath(TEST_IMAGE)
if err != nil {
t.Fatal(err.Error())
}
all_pixels, err := src.GetFloatPixels()
if err != nil {
t.Fatal(err.Error())
}
roi := src.ROI()
roi.SetXEnd(10)
roi.SetYEnd(10)
pixel_iface, err := src.GetPixelRegion(roi, TypeFloat)
if err != nil {
t.Fatal(err.Error())
}
some_pixels := pixel_iface.([]float32)
spec := src.Spec()
expected := 10 * 10 * spec.Depth() * spec.NumChannels()
actual := len(some_pixels)
if expected != actual {
t.Fatalf("Expected to get a total of %d pixels; Got %d", expected, actual)
}
for i := 0; i < len(some_pixels); i++ {
if some_pixels[i] != all_pixels[i] {
t.Fatalf("Pixel values do not match; Expected %v. got %v", all_pixels[i], some_pixels[i])
}
}
}