Repository navigation
Expand file tree
/
Copy pathToolsTest.java
More file actions
448 lines (383 loc) · 14.3 KB
/
Copy pathToolsTest.java
File metadata and controls
448 lines (383 loc) · 14.3 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
*
*/
package test.view;
import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.ScalarDS;
import ncsa.hdf.object.h5.H5File;
import ncsa.hdf.object.h5.H5ScalarDS;
import test.unittests.H5TestFile;
import ncsa.hdf.view.Tools;
import java.lang.reflect.*;
import junit.framework.TestCase;
/**
* @author xcao
*
*/
public class ToolsTest extends TestCase {
private static final H5File H5FILE = new H5File();
private static final double EPSILON = 0.0000001;
private H5File testFile = null;
private H5ScalarDS testDataset = null;
/**
* @param arg0
*/
public ToolsTest(String arg0) {
super(arg0);
}
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
testFile = (H5File)H5FILE.open(H5TestFile.NAME_FILE_H5, FileFormat.WRITE);
assertNotNull(testFile);
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// make sure all objects are closed
final int fid = testFile.getFID();
if (fid > 0) {
int nObjs = 0;
try { nObjs = H5.H5Fget_obj_count(fid, HDF5Constants.H5F_OBJ_LOCAL); }
catch (final Exception ex) { fail("H5.H5Fget_obj_count() failed. "+ ex); }
assertEquals(1, nObjs); // file id should be the only one left open
}
if (testFile != null) {
try { testFile.close(); } catch (final Exception ex) {}
testFile = null;
}
}
/**
* Test method for {@link ncsa.hdf.view.Tools#autoContrastCompute(java.lang.Object, double[], boolean)}.
*/
public final void testAutoContrastCompute() {
double[] params = {0, 0};
ScalarDS dset = null;
Object data = null;
double min=0, max=1;
double gain=0, bias=0;
for (int i=0; i<TestFile.OBJ_NAMES.length; i++) {
dset = null;
data = null;
min = TestFile.DATA_AUTO_MIN;
max = TestFile.DATA_AUTO_MAX;
try {
dset = (ScalarDS)testFile.get(TestFile.OBJ_NAMES[i]);
data = dset.getData();
dset.convertFromUnsignedC();
data = dset.getData();
} catch (Exception ex) {
fail("file.get() failed. "+ex);
}
assertNotNull(data);
Tools.autoContrastCompute(data, params, TestFile.isUnsigned[i]);
if (TestFile.DATA_NEW_MIN >= 0) {
min = TestFile.DATA_NEW_MIN;
}
if (TestFile.DATA_NEW_MAX <= TestFile.DATA_MAX_VALUES[i]) {
max = TestFile.DATA_NEW_MAX;
}
gain = TestFile.DATA_MAX_VALUES[i]/(max-min);
bias = -min;
assertEquals(gain, params[0], gain*EPSILON);
assertEquals(bias, params[1], bias*EPSILON);
}
}
/**
* Test method for {@link ncsa.hdf.view.Tools#autoContrastApply(java.lang.Object, java.lang.Object, double[], boolean)}.
*/
public final void testAutoContrastApply() {
double[] params = {0, 0};
ScalarDS dset = null;
Object data = null;
double min=0, max=1;
double gain=0, bias=0, expected=0;
Object data_out = null;
for (int i=0; i<TestFile.OBJ_NAMES.length; i++) {
dset = null;
data = null;
data_out = null;
min = TestFile.DATA_AUTO_MIN;
max = TestFile.DATA_AUTO_MAX;
try {
dset = (ScalarDS)testFile.get(TestFile.OBJ_NAMES[i]);
data = dset.getData();
dset.convertFromUnsignedC();
data = dset.getData();
} catch (Exception ex) {
fail("file.get() failed. "+ex);
}
assertNotNull(data);
Tools.autoContrastCompute(data, params, TestFile.isUnsigned[i]);
data_out = Tools.autoContrastApply(data, data_out, params, TestFile.isUnsigned[i]);
assertNotNull(data_out);
if (TestFile.DATA_NEW_MIN >= 0) {
min = TestFile.DATA_NEW_MIN;
}
if (TestFile.DATA_NEW_MAX <= TestFile.DATA_MAX_VALUES[i]) {
max = TestFile.DATA_NEW_MAX;
}
gain = TestFile.DATA_MAX_VALUES[i]/(max-min);
bias = -min;
for (int j=0; j<TestFile.DATA_INT8.length; j++) {
expected = (TestFile.DATA_INT8[j] + bias)*gain;
if (expected < 0) {
expected = 0;
} else if (expected>TestFile.DATA_MAX_VALUES[i]) {
expected = TestFile.DATA_MAX_VALUES[i];
}
assertEquals(expected, Array.getDouble(data_out, j), Math.max(1, expected*EPSILON));
}
}
}
/**
* Test method for {@link ncsa.hdf.view.Tools#autoContrastConvertImageBuffer(java.lang.Object, byte[], boolean)}.
* <p>
* Test if the byte data is calculated based on the following
* <pre>
uint_8 x
int_8 (x & 0x7F) << 1
uint_16 (x >> 8) & 0xFF
int_16 (x >> 7) & 0xFF
uint_32 (x >> 24) & 0xFF
int_32 (x >> 23) & 0xFF
uint_64 (x >> 56) & 0xFF
int_64 (x >> 55) & 0xFF
</pre>
*/
public final void testAutoContrastConvertImageBuffer() {
double[] params = {0, 0};
ScalarDS dset = null;
Object data = null;
byte expected=0;
Object data_out = null;
byte[] imgData = new byte[TestFile.DIM_SIZE];
int retValue = 0;
for (int i=0; i<TestFile.OBJ_NAMES.length; i++) {
dset = null;
data = null;
data_out = null;
try {
dset = (ScalarDS)testFile.get(TestFile.OBJ_NAMES[i]);
data = dset.getData();
dset.convertFromUnsignedC();
data = dset.getData();
} catch (Exception ex) {
fail("file.get() failed. "+ex);
}
assertNotNull(data);
retValue = Tools.autoContrastCompute(data, params, TestFile.isUnsigned[i]);
assertTrue(retValue>=0);
data_out = Tools.autoContrastApply(data, data_out, params, TestFile.isUnsigned[i]);
assertNotNull(data_out);
retValue = Tools.autoContrastConvertImageBuffer(data_out, imgData, TestFile.isUnsigned[i]);
assertTrue(retValue>=0);
for (int j=0; j<TestFile.DATA_INT8.length; j++) {
switch (i) {
case 0:
expected = (byte) ((Array.getByte(data_out, j) & 0x7F) << 1);
break;
case 1:
expected = (byte) Array.getShort(data_out, j);
break;
case 2:
expected = (byte) ((Array.getShort(data_out, j) >> 7) & 0xFF);
break;
case 3:
expected = (byte) ((Array.getInt(data_out, j) >> 8) & 0xFF);
break;
case 4:
expected = (byte) ((Array.getInt(data_out, j) >> 23) & 0xFF);
break;
case 5:
expected = (byte) ((Array.getLong(data_out, j) >> 24) & 0xFF);
break;
case 6:
expected = (byte) ((Array.getLong(data_out, j) >> 55) & 0xFF);
break;
}
assertEquals(expected, imgData[j]);
}
}
}
/**
* Test method for {@link ncsa.hdf.view.Tools#autoContrastComputeSliderRange(double[], double[], double[])}.
public final void testAutoContrastComputeSliderRange() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#autoContrastComputeMinMax(java.lang.Object, double[])}.
*/
public final void testAutoContrastComputeMinMax() {
double[] minmax = {0, 0};
ScalarDS dset = null;
Object data = null;
for (int i=0; i<TestFile.OBJ_NAMES.length; i++) {
dset = null;
data = null;
try {
dset = (ScalarDS)testFile.get(TestFile.OBJ_NAMES[i]);
data = dset.getData();
dset.convertFromUnsignedC();
data = dset.getData();
} catch (Exception ex) {
fail("file.get() failed. "+ex);
}
assertNotNull(data);
Tools.autoContrastComputeMinMax(data, minmax);
assertEquals(TestFile.DATA_AUTO_MIN, minmax[0], EPSILON);
assertEquals(TestFile.DATA_AUTO_MAX, minmax[1], EPSILON);
}
}
/**
* Test method for {@link ncsa.hdf.view.Tools#findMinMax(java.lang.Object, double[])}.
*/
public final void testFindMinMax() {
double[] minmax = {0, 0};
ScalarDS dset = null;
Object data = null;
for (int i=0; i<TestFile.OBJ_NAMES.length; i++) {
dset = null;
data = null;
try {
dset = (ScalarDS)testFile.get(TestFile.OBJ_NAMES[i]);
data = dset.getData();
dset.convertFromUnsignedC();
data = dset.getData();
} catch (Exception ex) {
fail("file.get() failed. "+ex);
}
assertNotNull(data);
Tools.findMinMax(data, minmax);
assertEquals(TestFile.DATA_MIN, minmax[0], EPSILON);
assertEquals(TestFile.DATA_MAX, minmax[1], EPSILON);
}
}
/**
* Test method for {@link ncsa.hdf.view.Tools#computeStatistics(java.lang.Object, double[])}.
*/
public final void testComputeStatistics() {
double[] avgstd = {0, 0};
ScalarDS dset = null;
Object data = null;
for (int i=0; i<TestFile.OBJ_NAMES.length; i++) {
dset = null;
data = null;
try {
dset = (ScalarDS)testFile.get(TestFile.OBJ_NAMES[i]);
data = dset.getData();
dset.convertFromUnsignedC();
data = dset.getData();
} catch (Exception ex) {
fail("file.get() failed. "+ex);
}
assertNotNull(data);
Tools.computeStatistics(data, avgstd);
assertEquals(TestFile.DATA_AVG, avgstd[0], EPSILON);
assertEquals(TestFile.DATA_STD, avgstd[1], EPSILON);
}
}
/**
* Test method for {@link ncsa.hdf.view.Tools#convertImageToHDF(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
public final void testConvertImageToHDF() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#saveImageAs(java.awt.image.BufferedImage, java.io.File, java.lang.String)}.
public final void testSaveImageAs() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createGrayPalette()}.
public final void testCreateGrayPalette() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createReverseGrayPalette()}.
public final void testCreateReverseGrayPalette() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createGrayWavePalette()}.
public final void testCreateGrayWavePalette() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createRainbowPalette()}.
public final void testCreateRainbowPalette() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createNaturePalette()}.
public final void testCreateNaturePalette() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createWavePalette()}.
public final void testCreateWavePalette() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#hasAlpha(java.awt.Image)}.
public final void testHasAlpha() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createIndexedImage(byte[], byte[][], int, int)}.
public final void testCreateIndexedImage() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#createTrueColorImage(byte[], boolean, int, int)}.
public final void testCreateTrueColorImage() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#getBytes(java.lang.Object, double[], byte[])}.
public final void testGetBytesObjectDoubleArrayByteArray() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#getBytes(java.lang.Object, double[], int, int, boolean, byte[])}.
public final void testGetBytesObjectDoubleArrayIntIntBooleanByteArray() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#getBytes(java.lang.Object, double[], java.lang.Object, byte[])}.
public final void testGetBytesObjectDoubleArrayObjectByteArray() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#getBytes(java.lang.Object, double[], int, int, boolean, java.lang.Object, byte[])}.
public final void testGetBytesObjectDoubleArrayIntIntBooleanObjectByteArray() {
fail("Not yet implemented"); // TODO
}
*/
/**
* Test method for {@link ncsa.hdf.view.Tools#newInstance(java.lang.Class, java.lang.Object[])}.
public final void testNewInstance() {
fail("Not yet implemented"); // TODO
}
*/
}