Object
hdf.view.Tools
The "Tools" class contains various tools for HDF files such as jpeg to HDF
converter.
- Version:
- 2.4 9/6/2007
- Author:
- Peter X. Cao
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Key for BMP image file type.static final String
Key for GIF image file type.static final String
Key for all image file type.static final String
Key for JPEG image file type.static final String
Key for PNG image file type.static final String
Key for TIFF image file type.static final long
static final long
static final long
static final long
static final long
static final long
static final BigInteger
static final long
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic final boolean
applyBitmask
(Object theData, BitSet theMask, ViewProperties.BITMASK_OP op) Apply bitmask to a data array.static Object
autoContrastApply
(Object dataIN, Object dataOUT, double[] params, double[] minmax, boolean isUnsigned) Apply autocontrast parameters to the original data in place (destructive)static int
autoContrastCompute
(Object data, double[] params, boolean isUnsigned) Computes autocontrast parameters (gain equates to contrast and bias equates to brightness) for integers.static int
autoContrastComputeMinMax
(Object data, double[] minmax) Computes autocontrast parameters bystatic int
autoContrastConvertImageBuffer
(Object src, byte[] dst, boolean isUnsigned) Converts image raw data to bytes.static final File
checkNewFile
(String path, String ext) Check and find a non-exist file.static boolean
checkValidJavaArrayIndex
(long value) Since Java does not allow array indices to be larger than int type, check the given value to see if it is within the valid range of a Java int.static int
computeStatistics
(Object data, double[] avgstd, Object fillValue) Computes mean and standard deviation of a data arraystatic void
convertImageToHDF
(String imgFileName, String hFileName, String fromType, String toType) Converts an image file into HDF4/5 file.static BigInteger
Converts unsigned 64-bit integer data to a BigInteger since Java does not have unsigned types.static final byte[][]
Creates the gray palette of the indexed 256-color table.static final byte[][]
Creates the gray wave palette of the indexed 256-color table.static Image
createIndexedImage
(BufferedImage bufferedImage, byte[] imageData, byte[][] palette, long w, long h) Creates a RGB indexed image of 256 colors.static final byte[][]
Creates the nature palette of the indexed 256-color table.static FileFormat
createNewFile
(String filename, String dir, String type, List<FileFormat> openFiles) Create a new HDF file with default file creation propertiesstatic final byte[][]
Creates the rainbow palette of the indexed 256-color table.static final byte[][]
Creates the reverse gray palette of the indexed 256-color table.static Image
createTrueColorImage
(byte[] imageData, boolean planeInterlace, long w, long h) Creates a true color image.static final byte[][]
Creates the wave palette of the indexed 256-color table.static int
findDataDist
(Object data, int[] dataDist, double[] minmax) Finds the distribution of data valuesstatic int
findMinMax
(Object data, double[] minmax, Object fillValue) Finds the min and max values of the data arraystatic boolean
getBinaryDataFromFile
(Object dataOut, String fileName, ByteOrder order) Reads data from a binary file into a buffer.static byte[]
Convert an array of raw data into array of a byte data.static byte[]
getBytes
(Object rawData, double[] minmax, long w, long h, boolean isTransposed, List<Number> invalidValues, boolean convertByteData, byte[] byteData) Convert an array of raw data into array of a byte data.static byte[]
getBytes
(Object rawData, double[] minmax, long w, long h, boolean isTransposed, List<Number> invalidValues, boolean convertByteData, byte[] byteData, List<Integer> list) Convert an array of raw data into array of a byte data.static byte[]
getBytes
(Object rawData, double[] minmax, long w, long h, boolean isTransposed, List<Number> invalidValues, byte[] byteData) Convert an array of raw data into array of a byte data.static byte[]
getHDF5UserBlock
(String filename) Read HDF5 user block data into byte array.static boolean
This method returns true if the specified image has transparent pixels.static boolean
look at the first 4 bytes of the file to see if it is an HDF4 file.static boolean
look at the first 8 bytes of the file to see if it is an HDF5 file.static final boolean
isNaNINF
(double val) Check if a given number if NaN or INF.static boolean
look at the first 4 bytes of the file to see if it is a netCDF file byte[0]=67, byte[1]=68, byte[2]=70, byte[3]=1 orstatic final void
launchBrowser
(String url) Launch default browser for a given URL.static Object
newInstance
(Class<?> cls, Object[] initargs) Create and initialize a new instance of the given class.static final byte[][]
readPalette
(String filename) read an image palette from a file.static void
saveAsBinary
(DataOutputStream out, Object data, ByteOrder order) Save the data as binarystatic void
saveImageAs
(BufferedImage image, File file, String type) Save a BufferedImage into an image file.static boolean
setHDF5UserBlock
(String fin, String fout, byte[] buf) Write HDF5 user block data into byte array.static boolean
showConfirm
(org.eclipse.swt.widgets.Shell parent, String title, String confMsg) Show an SWT Confirm dialog with the given message.static void
Show an SWT error dialog with the given error message.static void
showInformation
(org.eclipse.swt.widgets.Shell parent, String title, String infoMsg) Show an SWT Information dialog with the given message.static void
showWarning
(org.eclipse.swt.widgets.Shell parent, String title, String warnMsg) Show an SWT Warning dialog with the given message.static final String
toBinaryString
(long v, int nbytes) Returns a string representation of the long argument as an unsigned integer in base 2.static final String
toBinaryString
(BigDecimal v, int nbytes) Returns a string representation of the BigDecimal argument as an unsigned integer in base 2.static final String
toBinaryString
(BigInteger v, int nbytes) Returns a string representation of the BigInteger argument as an unsigned integer in base 2.static BufferedImage
toBufferedImage
(Image image) This method returns a buffered image with the contents of an image.static final String
toHexString
(long v, int nbytes) Returns a string representation of the long argument as an unsigned integer in base 16.static final String
toHexString
(BigDecimal v, int nbytes) Returns a string representation of the BigDecimal argument as an unsigned integer in base 16.static final String
toHexString
(BigInteger v, int nbytes) Returns a string representation of the BigInteger argument as an unsigned integer in base 16.
-
Field Details
-
MAX_INT8
- See Also:
-
MAX_UINT8
- See Also:
-
MAX_INT16
- See Also:
-
MAX_UINT16
- See Also:
-
MAX_INT32
- See Also:
-
MAX_UINT32
- See Also:
-
MAX_INT64
- See Also:
-
MAX_UINT64
-
FILE_TYPE_JPEG
Key for JPEG image file type.- See Also:
-
FILE_TYPE_TIFF
Key for TIFF image file type.- See Also:
-
FILE_TYPE_PNG
Key for PNG image file type.- See Also:
-
FILE_TYPE_GIF
Key for GIF image file type.- See Also:
-
FILE_TYPE_BMP
Key for BMP image file type.- See Also:
-
FILE_TYPE_IMAGE
Key for all image file type.- See Also:
-
-
Constructor Details
-
Tools
public Tools()
-
-
Method Details
-
convertUINT64toBigInt
Converts unsigned 64-bit integer data to a BigInteger since Java does not have unsigned types.- Parameters:
l
- The long value to convert to a BigInteger- Returns:
- A BigInteger representing the unsigned value of the given long.
-
convertImageToHDF
public static void convertImageToHDF(String imgFileName, String hFileName, String fromType, String toType) throws Exception Converts an image file into HDF4/5 file.- Parameters:
imgFileName
- the input image file.hFileName
- the name of the HDF4/5 file.fromType
- the type of image.toType
- the type of file converted to.- Throws:
Exception
- if a failure occurred
-
saveImageAs
Save a BufferedImage into an image file.- Parameters:
image
- the BufferedImage to save.file
- the image file.type
- the image type.- Throws:
IOException
- if a failure occurred
-
createGrayPalette
Creates the gray palette of the indexed 256-color table. 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.- Returns:
- the gray palette in the form of byte[3][256]
-
createReverseGrayPalette
Creates the reverse gray palette of the indexed 256-color table. 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.- Returns:
- the gray palette in the form of byte[3][256]
-
createGrayWavePalette
Creates the gray wave palette of the indexed 256-color table. 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.- Returns:
- the gray palette in the form of byte[3][256]
-
createRainbowPalette
Creates the rainbow palette of the indexed 256-color table. 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.- Returns:
- the rainbow palette in the form of byte[3][256]
-
createNaturePalette
Creates the nature palette of the indexed 256-color table. 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.- Returns:
- the nature palette in the form of byte[3][256]
-
createWavePalette
Creates the wave palette of the indexed 256-color table. 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.- Returns:
- the wave palette in the form of byte[3][256]
-
readPalette
read an image palette from a file. A palette file has format of (value, red, green, blue). The color value in palette file can be either unsigned char [0..255] or float [0..1]. Float value will be converted to [0..255]. The color table in file can have any number of entries between 2 to 256. It will be converted to a color table of 256 entries. Any missing index will calculated by linear interpolation between the neighboring index values. For example, index 11 is missing in the following table 10 200 60 20 12 100 100 60 Index 11 will be calculated based on index 10 and index 12, i.e. 11 150 80 40- Parameters:
filename
- the name of the palette file.- Returns:
- the wave palette in the form of byte[3][256]
-
hasAlpha
This method returns true if the specified image has transparent pixels.- Parameters:
image
- the image to be check if has alpha.- Returns:
- true if the image has alpha setting.
-
createIndexedImage
public static Image createIndexedImage(BufferedImage bufferedImage, byte[] imageData, byte[][] palette, long w, long h) Creates a RGB indexed image of 256 colors.- Parameters:
bufferedImage
- the target image.imageData
- the byte array of the image data.palette
- the color lookup table.w
- the width of the image.h
- the height of the image.- Returns:
- the image.
-
createTrueColorImage
Creates a true color image. DirectColorModel is used to construct the image from raw data. The DirectColorModel model is similar to an X11 TrueColor visual, which has the following parameters:
Number of bits: 32 Red mask: 0x00ff0000 Green mask: 0x0000ff00 Blue mask: 0x000000ff Alpha mask: 0xff000000 Color space: sRGB isAlphaPremultiplied: False Transparency: Transparency.TRANSLUCENT transferType: DataBuffer.TYPE_INT
The data may be arranged in one of two ways: by pixel or by plane. In both cases, the dataset will have a dataspace with three dimensions, height, width, and components. For HDF4, the interlace modes specify orders for the dimensions as:INTERLACE_PIXEL = [width][height][pixel components] INTERLACE_PLANE = [pixel components][width][height]
For HDF5, the interlace modes specify orders for the dimensions as:INTERLACE_PIXEL = [height][width][pixel components] INTERLACE_PLANE = [pixel components][height][width]
- Parameters:
imageData
- the byte array of the image data.planeInterlace
- flag if the image is plane interlace.w
- the width of the image.h
- the height of the image.- Returns:
- the image.
-
toBufferedImage
This method returns a buffered image with the contents of an image.- Parameters:
image
- the plain image object.- Returns:
- buffered image for the given image.
-
getBytes
public static byte[] getBytes(Object rawData, double[] minmax, long w, long h, boolean isTransposed, byte[] byteData) Convert an array of raw data into array of a byte data.- Parameters:
rawData
- The input raw data.minmax
- the range of the raw data.w
- the width of the raw data.h
- the height of the raw data.isTransposed
- if the data is transposed.byteData
- the data in.- Returns:
- the byte array of pixel data.
-
getBytes
public static byte[] getBytes(Object rawData, double[] minmax, long w, long h, boolean isTransposed, List<Number> invalidValues, byte[] byteData) Convert an array of raw data into array of a byte data.- Parameters:
rawData
- The input raw data.minmax
- the range of the raw data.w
- the width of the raw data.h
- the height of the raw data.isTransposed
- if the data is transposed.invalidValues
- list of values out of range.byteData
- the data in.- Returns:
- the byte array of pixel data.
-
getBytes
public static byte[] getBytes(Object rawData, double[] minmax, long w, long h, boolean isTransposed, List<Number> invalidValues, boolean convertByteData, byte[] byteData) Convert an array of raw data into array of a byte data.- Parameters:
rawData
- The input raw data.minmax
- the range of the raw data.w
- the width of the raw data.h
- the height of the raw data.isTransposed
- if the data is transposed.invalidValues
- list of values out of range.convertByteData
- the converted data out.byteData
- the data in.- Returns:
- the byte array of pixel data.
-
getBytes
public static byte[] getBytes(Object rawData, double[] minmax, long w, long h, boolean isTransposed, List<Number> invalidValues, boolean convertByteData, byte[] byteData, List<Integer> list) Convert an array of raw data into array of a byte data.- Parameters:
rawData
- The input raw data.minmax
- the range of the raw data.w
- the width of the raw data.h
- the height of the raw data.isTransposed
- if the data is transposed.invalidValues
- the list of invalid values.convertByteData
- the converted data out.byteData
- the data in.list
- the list of integers.- Returns:
- the byte array of pixel data.
-
newInstance
Create and initialize a new instance of the given class.- Parameters:
cls
- the class of the instanceinitargs
- array of objects to be passed as arguments.- Returns:
- a new instance of the given class.
- Throws:
Exception
- if a failure occurred
-
autoContrastCompute
Computes autocontrast parameters (gain equates to contrast and bias equates to brightness) for integers. The computation is based on the following scalingint_8 [0, 127] uint_8 [0, 255] int_16 [0, 32767] uint_16 [0, 65535] int_32 [0, 2147483647] uint_32 [0, 4294967295] int_64 [0, 9223372036854775807] uint_64 [0, 18446744073709551615] // Not supported.
- Parameters:
data
- the raw data array of signed/unsigned integersparams
- the auto gain parameter. params[0]=gain, params[1]=bias,isUnsigned
- the flag to indicate if the data array is unsigned integer.- Returns:
- non-negative if successful; otherwise, returns negative
-
autoContrastApply
public static Object autoContrastApply(Object dataIN, Object dataOUT, double[] params, double[] minmax, boolean isUnsigned) Apply autocontrast parameters to the original data in place (destructive)- Parameters:
dataIN
- the original data array of signed/unsigned integersdataOUT
- the converted data array of signed/unsigned integersparams
- the auto gain parameter. params[0]=gain, params[1]=biasminmax
- the data range. minmax[0]=min, minmax[1]=maxisUnsigned
- the flag to indicate if the data array is unsigned integer- Returns:
- the data array with the auto contrast conversion; otherwise, returns null
-
autoContrastConvertImageBuffer
Converts image raw data to bytes. The integer data is converted to byte data based on the following ruleuint_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
- Parameters:
src
- the source data array of signed integers or unsigned shortsdst
- the destination data array of bytesisUnsigned
- the flag to indicate if the data array is unsigned integer.- Returns:
- non-negative if successful; otherwise, returns negative
-
autoContrastComputeMinMax
Computes autocontrast parameters bymin = mean - 3 * std.dev max = mean + 3 * std.dev
- Parameters:
data
- the raw data arrayminmax
- the min and max values.- Returns:
- non-negative if successful; otherwise, returns negative
-
findMinMax
Finds the min and max values of the data array- Parameters:
data
- the raw data arrayminmax
- the mmin and max values of the array.fillValue
- the missing value or fill value. Exclude this value when check for min/max- Returns:
- non-negative if successful; otherwise, returns negative
-
findDataDist
Finds the distribution of data values- Parameters:
data
- the raw data arraydataDist
- the data distirbution.minmax
- the data range- Returns:
- non-negative if successful; otherwise, returns negative
-
computeStatistics
Computes mean and standard deviation of a data array- Parameters:
data
- the raw data arrayavgstd
- the statistics: avgstd[0]=mean and avgstd[1]=stdev.fillValue
- the missing value or fill value. Exclude this value when compute statistics- Returns:
- non-negative if successful; otherwise, returns negative
-
saveAsBinary
public static void saveAsBinary(DataOutputStream out, Object data, ByteOrder order) throws Exception Save the data as binary- Parameters:
out
- the output streamdata
- the raw data arrayorder
- the order of bytes- Throws:
Exception
- if a failure occurred
-
getBinaryDataFromFile
Reads data from a binary file into a buffer.- Parameters:
dataOut
- the output streamfileName
- the file to read binary data fromorder
- the new byte order, either BIG_ENDIAN or LITTLE_ENDIAN- Returns:
- true if successful; otherwise, false.
-
toBinaryString
Returns a string representation of the long argument as an unsigned integer in base 2. This is different from Long.toBinaryString(long i). This function add padding (0's) to the string based on the nbytes. For example, if v=15, nbytes=1, the string will be "00001111".- Parameters:
v
- the long valuenbytes
- number of bytes in the integer- Returns:
- the string representation of the unsigned long value represented by the argument in binary (base 2).
-
toBinaryString
Returns a string representation of the BigDecimal argument as an unsigned integer in base 2. This is different from BigDecimal.toBinaryString(long i). This function add padding (0's) to the string based on the nbytes. For example, if v=15, nbytes=1, the string will be "00001111".- Parameters:
v
- the BigDecimal valuenbytes
- number of bytes in the integer- Returns:
- the string representation of the BigDecimal value represented by the argument in binary (base 2).
-
toBinaryString
Returns a string representation of the BigInteger argument as an unsigned integer in base 2. This is different from BigInteger.toBinaryString(long i). This function add padding (0's) to the string based on the nbytes. For example, if v=15, nbytes=1, the string will be "00001111".- Parameters:
v
- the BigInteger valuenbytes
- number of bytes in the integer- Returns:
- the string representation of the BigInteger value represented by the argument in binary (base 2).
-
toHexString
Returns a string representation of the long argument as an unsigned integer in base 16. This is different from Long.toHexString(long i). This function add padding (0's) to the string based on the nbytes. For example, if v=42543, nbytes=4, the string will be "0000A62F".- Parameters:
v
- the long valuenbytes
- number of bytes in the integer- Returns:
- the string representation of the unsigned long value represented by the argument in hexadecimal (base 16).
-
toHexString
Returns a string representation of the BigInteger argument as an unsigned integer in base 16. This is different from BigInteger.toString(16). This function adds padding (0's) to the string based on the nbytes. For example, if v=42543, nbytes=4, the string will be "0000A62F".- Parameters:
v
- the BigInteger valuenbytes
- number of bytes in the integer- Returns:
- the string representation of the BigInteger value represented by the argument in hexadecimal (base 16).
-
toHexString
Returns a string representation of the BigDecimal argument as an unsigned integer in base 16. This is different from BigDecimal.toString(16). This function adds padding (0's) to the string based on the nbytes. For example, if v=42543, nbytes=4, the string will be "0000A62F".- Parameters:
v
- the BigDecimal valuenbytes
- number of bytes in the integer- Returns:
- the string representation of the BigDecimal value represented by the argument in hexadecimal (base 16).
-
applyBitmask
public static final boolean applyBitmask(Object theData, BitSet theMask, ViewProperties.BITMASK_OP op) Apply bitmask to a data array.- Parameters:
theData
- the data array which the bitmask is applied to.theMask
- the bitmask to be applied to the data array.op
- the bitmask op to be applied- Returns:
- true if bitmask is applied successfully; otherwise, false.
-
getHDF5UserBlock
Read HDF5 user block data into byte array.- Parameters:
filename
- the HDF5 file from which to get the user block- Returns:
- a byte array of user block, or null if there is user data.
-
setHDF5UserBlock
Write HDF5 user block data into byte array.- Parameters:
fin
- the input filenamefout
- the output filenamebuf
- the data to write into the user block- Returns:
- a byte array of user block, or null if there is user data.
-
isHDF4
look at the first 4 bytes of the file to see if it is an HDF4 file. byte[0]=14, byte[1]=3, byte[2]=19, byte[3]=1 or if it is a netCDF file byte[0]=67, byte[1]=68, byte[2]=70, byte[3]=1- Parameters:
filename
- the file to test if HDF4- Returns:
- true if the file is of type HDF4
-
isHDF5
look at the first 8 bytes of the file to see if it is an HDF5 file. byte[0]=-199 which is 137 in unsigned byte, byte[1]=72, byte[2]=68, byte[3]=70, byte[4]=13, byte[5]=10, byte[6]=26, byte[7]=10- Parameters:
filename
- the file to test if HDF5- Returns:
- true if the file is of type HDF5
-
isNetcdf
look at the first 4 bytes of the file to see if it is a netCDF file byte[0]=67, byte[1]=68, byte[2]=70, byte[3]=1 or- Parameters:
filename
- the file to test if netcdf- Returns:
- true if the file is of type netcdf
-
launchBrowser
Launch default browser for a given URL.- Parameters:
url
- the URL to open.- Throws:
Exception
- if a failure occurred
-
createNewFile
public static FileFormat createNewFile(String filename, String dir, String type, List<FileFormat> openFiles) throws Exception Create a new HDF file with default file creation properties- Parameters:
filename
- the file to createdir
- the directory for filetype
- the type of the fileopenFiles
- the list of already opened files- Returns:
- the FileFormat instance of the newly created file
- Throws:
Exception
- if a failure occurred
-
checkNewFile
Check and find a non-exist file.- Parameters:
path
- -- the path that the new file will be checked.ext
- -- the extention of the new file.- Returns:
- -- the new file.
-
isNaNINF
Check if a given number if NaN or INF.- Parameters:
val
- the number to be checked- Returns:
- true if the number is Nan or INF; otherwise, false.
-
checkValidJavaArrayIndex
Since Java does not allow array indices to be larger than int type, check the given value to see if it is within the valid range of a Java int.- Parameters:
value
- The value to check- Returns:
- false if the value is outside the range of a Java int, true otherwise.
-
showError
Show an SWT error dialog with the given error message.- Parameters:
parent
- The parent Shell of the MessageDialogtitle
- The title to set for the MessageDialogerrorMsg
- The error message to display in the MessageDialog
-
showInformation
public static void showInformation(org.eclipse.swt.widgets.Shell parent, String title, String infoMsg) Show an SWT Information dialog with the given message.- Parameters:
parent
- The parent Shell of the MessageDialogtitle
- The title to set for the MessageDialoginfoMsg
- The message to display in the MessageDialog
-
showConfirm
public static boolean showConfirm(org.eclipse.swt.widgets.Shell parent, String title, String confMsg) Show an SWT Confirm dialog with the given message.- Parameters:
parent
- The parent Shell of the MessageDialogtitle
- The title to set for the MessageDialogconfMsg
- The message to display in the MessageDialog- Returns:
- The status of the dialog after closing
-
showWarning
Show an SWT Warning dialog with the given message.- Parameters:
parent
- The parent Shell of the MessageDialogtitle
- The title to set for the MessageDialogwarnMsg
- The message to display in the MessageDialog
-