Repository navigation
Expand file tree
/
Copy pathScalarDS.java
More file actions
465 lines (407 loc) · 14.9 KB
/
Copy pathScalarDS.java
File metadata and controls
465 lines (407 loc) · 14.9 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*****************************************************************************
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of the HDF Java Products distribution. *
* The full copyright notice, including terms governing use, modification, *
* and redistribution, is contained in the files COPYING and Copyright.html. *
* COPYING can be found at the root of the source code distribution tree. *
* Or, see http://hdfgroup.org/products/hdf-java/doc/Copyright.html. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
****************************************************************************/
package ncsa.hdf.object;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
* A scalar dataset is a multiple dimension array of scalar points. The Datatype of a scalar dataset must be an atomic
* datatype. Common datatypes of scalar datasets include char, byte, short, int, long, float, double and string.
* <p>
* A ScalarDS can be an image or spreadsheet data. ScalarDS defines few methods to deal with both images and
* spreadsheets.
* <p>
* ScalarDS is an abstract class. Current implementing classes are the H4SDS, H5GRImage and H5ScalarDS.
* <p>
*
* @version 1.1 9/4/2007
* @author Peter X. Cao
*/
public abstract class ScalarDS extends Dataset {
/**
*
*/
private static final long serialVersionUID = 8925371455928203981L;
private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ScalarDS.class);
// The following constant strings are copied from
// http://hdf.ncsa.uiuc.edu/HDF5/doc/ADGuide/ImageSpec.html
// to make the definition consistent with the image specs.
/**
* Indicates that the pixel RGB values are contiguous.
*/
public final static int INTERLACE_PIXEL = 0;
/** Indicates that each pixel component of RGB is stored as a scan line. */
public static final int INTERLACE_LINE = 1;
/** Indicates that each pixel component of RGB is stored as a plane. */
public final static int INTERLACE_PLANE = 2;
/**
* The interlace mode of the stored raster image data. Valid values are INTERLACE_PIXEL, INTERLACE_LINE and
* INTERLACE_PLANE.
*/
protected int interlace;
/**
* The min-max range of image data values. For example, [0, 255] indicates the min is 0, and the max is 255.
*/
protected double[] imageDataRange;
/**
* The indexed RGB color model with 256 colors.
* <p>
* The palette values are stored in a two-dimensional byte array and arrange by color components of red, green and
* blue. palette[][] = byte[3][256], where, palette[0][], palette[1][] and palette[2][] are the red, green and blue
* components respectively.
*/
protected byte[][] palette;
/**
* True if this dataset is an image.
*/
protected boolean isImage;
/**
* True if this dataset is a true color image.
*/
protected boolean isTrueColor;
/**
* True if this dataset is ASCII text.
*/
protected boolean isText;
/**
* Flag to indicate if the original C data is unsigned integer.
*/
protected boolean isUnsigned;
/**
* Flag to indicate is the original unsigned C data is converted.
*/
protected boolean unsignedConverted;
/** The fill value of the dataset. */
protected Object fillValue = null;
private List<Number> filteredImageValues;
/** Flag to indicate if the dataset is displayed as an image */
protected boolean isImageDisplay;
/**
* Flag to indicate if the dataset is displayed as an image with default order of dimensions
*/
protected boolean isDefaultImageOrder;
/**
* Flag to indicate if the FillValue is converted from unsigned C.
*/
public boolean isFillValueConverted;
/**
* Constructs an instance of a ScalarDS with specific name and path. An HDF data object must have a name. The path
* is the group path starting from the root.
* <p>
* For example, in H5ScalarDS(h5file, "dset", "/arrays/"), "dset" is the name of the dataset, "/arrays" is the group
* path of the dataset.
*
* @param theFile
* the file that contains the data object.
* @param theName
* the name of the data object, e.g. "dset".
* @param thePath
* the full path of the data object, e.g. "/arrays/".
*/
public ScalarDS(FileFormat theFile, String theName, String thePath) {
this(theFile, theName, thePath, null);
}
/**
* @deprecated Not for public use in the future.<br>
* Using {@link #ScalarDS(FileFormat, String, String)}
*/
@Deprecated
public ScalarDS(FileFormat theFile, String theName, String thePath, long[] oid) {
super(theFile, theName, thePath, oid);
palette = null;
isImage = false;
isTrueColor = false;
isText = false;
isUnsigned = false;
interlace = -1;
datatype = null;
imageDataRange = null;
isImageDisplay = false;
isDefaultImageOrder = true;
isFillValueConverted = false;
filteredImageValues = new Vector<Number>();
}
/*
* (non-Javadoc)
*
* @see ncsa.hdf.object.Dataset#clearData()
*/
@Override
public void clearData() {
super.clearData();
unsignedConverted = false;
}
/**
* Converts the data values of this dataset to appropriate Java integer if they are unsigned integers.
*
* @see ncsa.hdf.object.Dataset#convertToUnsignedC(Object)
* @see ncsa.hdf.object.Dataset#convertFromUnsignedC(Object, Object)
*
* @return the converted data buffer.
*/
public Object convertFromUnsignedC() {
// keep a copy of original buffer and the converted buffer
// so that they can be reused later to save memory
log.trace("convertFromUnsignedC: start");
if ((data != null) && isUnsigned && !unsignedConverted) {
log.trace("convertFromUnsignedC: convert");
originalBuf = data;
convertedBuf = convertFromUnsignedC(originalBuf, convertedBuf);
data = convertedBuf;
unsignedConverted = true;
if (fillValue != null) {
if (!isFillValueConverted) {
fillValue = convertFromUnsignedC(fillValue, null);
isFillValueConverted = true;
}
}
}
log.trace("convertFromUnsignedC: finish");
return data;
}
/**
* Converts Java integer data of this dataset back to unsigned C-type integer data if they are unsigned integers.
*
* @see ncsa.hdf.object.Dataset#convertToUnsignedC(Object)
* @see ncsa.hdf.object.Dataset#convertToUnsignedC(Object, Object)
* @see #convertFromUnsignedC(Object data_in)
*
* @return the converted data buffer.
*/
public Object convertToUnsignedC() {
// keep a copy of original buffer and the converted buffer
// so that they can be reused later to save memory
if ((data != null) && isUnsigned) {
convertedBuf = data;
originalBuf = convertToUnsignedC(convertedBuf, originalBuf);
data = originalBuf;
}
return data;
}
/**
* Returns the palette of this scalar dataset or null if palette does not exist.
* <p>
* Scalar dataset can be displayed as spreadsheet data or image. When a scalar dataset is chosen to display as an
* image, the palette or color table may be needed to translate a pixel value to color components (for example, red,
* green, and blue). Some scalar datasets have no palette and some datasets have one or more than one palettes. If
* an associated palette exists but not loaded, this interface retrieves the palette from the file and returns the
* palette. If the palette is loaded, it returnd the palette. It returns null if there is no palette assciated with
* the dataset.
* <p>
* Current implementation only supports palette model of indexed RGB with 256 colors. Other models such as
* YUV", "CMY", "CMYK", "YCbCr", "HSV will be supported in the future.
* <p>
* The palette values are stored in a two-dimensional byte array and arrange by color components of red, green and
* blue. palette[][] = byte[3][256], where, palette[0][], palette[1][] and palette[2][] are the red, green and blue
* components respectively.
* <p>
* Sub-classes have to implement this interface. HDF4 and HDF5 images use different libraries to retrieve the
* associated palette.
*
* @return the 2D palette byte array.
*/
public abstract byte[][] getPalette();
/**
* Sets the palette for this dataset.
*
* @param pal
* the 2D palette byte array.
*/
public final void setPalette(byte[][] pal) {
palette = pal;
}
/**
* Reads a specific image palette from file.
* <p>
* A scalar dataset may have multiple palettes attached to it. readPalette(int idx) returns a specific palette
* identified by its index.
*
* @param idx
* the index of the palette to read.
*/
public abstract byte[][] readPalette(int idx);
/**
* Get the name of a specific image palette from file.
* <p>
* A scalar dataset may have multiple palettes attached to it. getPaletteName(int idx) returns the name of a
* specific palette identified by its index.
*
* @param idx
* the index of the palette to retrieve the name.
* @return The name of the palette
*/
public String getPaletteName(int idx) {
String paletteName = "Default ";
if (idx != 0)
paletteName = "Default " + idx;
return paletteName;
}
/**
* Returns the byte array of palette refs.
* <p>
* A palette reference is an object reference that points to the palette dataset.
* <p>
* For example, Dataset "Iceberg" has an attribute of object reference "Palette". The arrtibute "Palette" has value
* "2538" that is the object reference of the palette data set "Iceberg Palette".
*
* @return null if there is no palette attribute attached to this dataset.
*/
public abstract byte[] getPaletteRefs();
/**
* Returns true if this dataset is an image.
* <p>
* For all Images, they must have an attribute called "CLASS". The value of this attribute is "IMAGE". For more
* details, read <a href="http://hdfgroup.org/HDF5/doc/ADGuide/ImageSpec.html"> HDF5 Image and Palette Specification
* </a>
*
* @return true if the dataset is an image; otherwise, returns false.
*/
public final boolean isImage() {
return isImage;
}
/**
* Returns true if this dataset is displayed as an image.
* <p>
* A ScalarDS can be displayed as an image or table.
*
* @return true if this dataset is displayed as an image; otherwise, returns false.
*/
public final boolean isImageDisplay() {
return isImageDisplay;
}
/**
* Returns true if this dataset is displayed as an image with default image order.
* <p>
* A ScalarDS can be displayed as an image with different orders of dimensions.
*
* @return true if this dataset is displayed as an image with default image order; otherwise, returns false.
*/
public final boolean isDefaultImageOrder() {
return isDefaultImageOrder;
}
/**
* Sets the flag to display the dataset as an image.
*
* @param b
* if b is true, display the dataset as an image
*/
public final void setIsImageDisplay(boolean b) {
isImageDisplay = b;
if (isImageDisplay) {
enumConverted = false;
}
}
/**
* Sets the flag to indicate this dataset is an image.
*
* @param b
* if b is true, the dataset is an image.
*/
public final void setIsImage(boolean b) {
isImage = b;
if (isImage) {
enumConverted = false;
}
}
/**
* Sets data range for an image.
*
* @param min
* the data range start.
* @param max
* the data range end.
*/
public final void setImageDataRange(double min, double max) {
if (max <= min)
return;
if (imageDataRange == null)
imageDataRange = new double[2];
imageDataRange[0] = min;
imageDataRange[1] = max;
}
/**
* Add a value that will be filtered out in image
*
* @param x
* value to be filtered
*/
public void addFilteredImageValue(Number x) {
Iterator<Number> it = filteredImageValues.iterator();
while (it.hasNext()) {
if (it.next().toString().equals(x.toString()))
return;
}
filteredImageValues.add(x);
}
/**
* get a list of values that will be filtered out in image
*/
public List<Number> getFilteredImageValues() {
return filteredImageValues;
}
/**
* Returns true if this dataset is a true color image.
*/
public final boolean isTrueColor() {
return isTrueColor;
}
/**
* Returns true if this dataset is ASCII text.
*
* @return true if this dataset is ASCII text.
*/
public final boolean isText() {
return isText;
}
/**
* Returns the interlace mode of a true color image (RGB).
*
* Valid values:
*
* <pre>
* INTERLACE_PIXEL -- RGB components are contiguous, i.e. rgb, rgb, rgb, ...
* INTERLACE_LINE -- each RGB component is stored as a scan line
* INTERLACE_PLANE -- each RGB component is stored as a plane
* </pre>
*
* @return the interlace mode of a true color image (RGB).
*/
public final int getInterlace() {
return interlace;
}
/**
* Returns true if the original C data are unsigned integers.
*
* @return true if the original C data are unsigned integers.
*/
public final boolean isUnsigned() {
return isUnsigned;
}
/**
* Returns the (min, max) pair of image data range.
*
* @return the (min, max) pair of image data range.
*/
public double[] getImageDataRange() {
return imageDataRange;
}
/**
* Returns the fill values for the dataset.
*
* @return the fill values for the dataset.
*/
public final Object getFillValue() {
return fillValue;
}
}