From c049510d7040311931ea05b6d76e55406097a34b Mon Sep 17 00:00:00 2001 From: Simon Gregor Ebner Date: Thu, 28 Aug 2014 14:16:50 +0200 Subject: [PATCH] Some more refactoring ... hoping that things get in shape some time ... --- .../psi/imagej/hdf5/HDF5GroupedVarnames.java | 2 +- .../java/ch/psi/imagej/hdf5/HDF5Reader.java | 887 +++++++++--------- .../ch/psi/imagej/hdf5/HDF5Utilities.java | 39 + .../java/ch/psi/imagej/hdf5/HDF5Writer.java | 105 +-- .../java/ch/psi/imagej/hdf5/TimeFrame.java | 2 +- .../ch/psi/imagej/hdf5/TimeFrameTest.java | 25 + 6 files changed, 521 insertions(+), 539 deletions(-) create mode 100644 src/main/java/ch/psi/imagej/hdf5/HDF5Utilities.java create mode 100644 src/test/java/ch/psi/imagej/hdf5/TimeFrameTest.java diff --git a/src/main/java/ch/psi/imagej/hdf5/HDF5GroupedVarnames.java b/src/main/java/ch/psi/imagej/hdf5/HDF5GroupedVarnames.java index a6ffd67..506cc63 100644 --- a/src/main/java/ch/psi/imagej/hdf5/HDF5GroupedVarnames.java +++ b/src/main/java/ch/psi/imagej/hdf5/HDF5GroupedVarnames.java @@ -132,7 +132,7 @@ public class HDF5GroupedVarnames { TimeFrame frame = new TimeFrame(frameIndex.intValue()); int idx = frameList.indexOf(frame); if (idx != -1) { - frame = (TimeFrame) frameList.get(idx); + frame = frameList.get(idx); frame.addChannel(channelIndex.intValue()); } else { frame.addChannel(channelIndex.intValue()); diff --git a/src/main/java/ch/psi/imagej/hdf5/HDF5Reader.java b/src/main/java/ch/psi/imagej/hdf5/HDF5Reader.java index 0ecd03b..099b0ad 100644 --- a/src/main/java/ch/psi/imagej/hdf5/HDF5Reader.java +++ b/src/main/java/ch/psi/imagej/hdf5/HDF5Reader.java @@ -32,9 +32,7 @@ import ij.process.ImageProcessor; import java.io.File; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import java.awt.*; @@ -98,7 +96,7 @@ public class HDF5Reader implements PlugIn { // Parse the file Group rootNode = (Group) ((javax.swing.tree.DefaultMutableTreeNode) inFile.getRootNode()).getUserObject(); - List varList = getDataSetList(rootNode, new ArrayList()); + List varList = getDatasets(rootNode, new ArrayList()); GenericDialog gd = new GenericDialog("Variable Name Selection"); gd.addMessage("Please select variables to be loaded.\n"); @@ -190,7 +188,7 @@ public class HDF5Reader implements PlugIn { logger.info(""); IJ.showStatus("Reading Variable: " + var.getName() + " (" + extent[0] + " slices)"); - Attribute elemsize_att = getAttributes(var).get("element_size_um"); + Attribute elemsize_att = HDF5Utilities.getAttributes(var).get("element_size_um"); double[] elem_sizes = new double[3]; if (elemsize_att == null) { elem_sizes[0] = 1.0; @@ -311,151 +309,7 @@ public class HDF5Reader implements PlugIn { // long numElements = stackSize * 3; int endIdx = (int)(startIdx+stackSize*3-1); - if (wholeDataset instanceof byte[]) { - byte[] tmp = Arrays.copyOfRange((byte[]) wholeDataset, startIdx, endIdx); - byte[] rChannel = new byte[size]; - byte[] gChannel = new byte[size]; - byte[] bChannel = new byte[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof short[]) { - short[] tmp = Arrays.copyOfRange((short[]) wholeDataset, startIdx, endIdx); - short[] rChannel = new short[size]; - short[] gChannel = new short[size]; - short[] bChannel = new short[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof int[]) { - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { - float[] tmp = convertInt32ToFloat(Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx)); - float[] rChannel = new float[size]; - float[] gChannel = new float[size]; - float[] bChannel = new float[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { - short[] tmp = convertInt32ToShort(Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx)); - short[] rChannel = new short[size]; - short[] gChannel = new short[size]; - short[] bChannel = new short[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } - } else if (wholeDataset instanceof long[]) { - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { - float[] tmp = convertInt64ToFloat(Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx)); - float[] rChannel = new float[size]; - float[] gChannel = new float[size]; - float[] bChannel = new float[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { - short[] tmp = convertInt64ToShort(Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx)); - short[] rChannel = new short[size]; - short[] gChannel = new short[size]; - short[] bChannel = new short[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } - } else if (wholeDataset instanceof float[]) { - float[] tmp = Arrays.copyOfRange((float[]) wholeDataset, startIdx, endIdx); - float[] rChannel = new float[size]; - float[] gChannel = new float[size]; - float[] bChannel = new float[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof double[]) { - float[] tmp = convertDoubleToFloat(Arrays.copyOfRange((double[]) wholeDataset, startIdx, endIdx)); - float[] rChannel = new float[size]; - float[] gChannel = new float[size]; - float[] bChannel = new float[size]; - for (int row = 0; row < extent[2]; ++row) { - for (int col = 0; col < extent[3]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else { - logger.warning("Datatype not supported"); - } + copyPixels3(datatypeIfUnsupported, extent, stack, wholeDataset, size, startIdx, endIdx); } } @@ -544,109 +398,7 @@ public class HDF5Reader implements PlugIn { // Object slice = extractSubarray(wholeDataset, startIdx, numElements); int size = (int) (extent[2] * extent[1]); - if (wholeDataset instanceof byte[]) { - byte[] tmp = Arrays.copyOfRange((byte[]) wholeDataset, startIdx, endIdx); - byte[] rChannel = new byte[size]; - byte[] gChannel = new byte[size]; - byte[] bChannel = new byte[size]; - for (int row = 0; row < extent[1]; ++row) { - for (int col = 0; col < extent[2]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof short[]) { - short[] tmp = Arrays.copyOfRange((short[]) wholeDataset, startIdx, endIdx); - short[] rChannel = new short[size]; - short[] gChannel = new short[size]; - short[] bChannel = new short[size]; - for (int row = 0; row < extent[1]; ++row) { - for (int col = 0; col < extent[2]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof int[]) { - int[] tmp = Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx); - int[] rChannel = new int[size]; - int[] gChannel = new int[size]; - int[] bChannel = new int[size]; - for (int row = 0; row < extent[1]; ++row) { - for (int col = 0; col < extent[2]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof long[]) { - long[] tmp = Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx); - long[] rChannel = new long[size]; - long[] gChannel = new long[size]; - long[] bChannel = new long[size]; - for (int row = 0; row < extent[1]; ++row) { - for (int col = 0; col < extent[2]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof float[]) { - float[] tmp = Arrays.copyOfRange((float[]) wholeDataset, startIdx, endIdx); - float[] rChannel = new float[size]; - float[] gChannel = new float[size]; - float[] bChannel = new float[size]; - for (int row = 0; row < extent[1]; ++row) { - for (int col = 0; col < extent[2]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (wholeDataset instanceof double[]) { - double[] tmp = Arrays.copyOfRange((double[]) wholeDataset, startIdx, endIdx); - double[] rChannel = new double[size]; - double[] gChannel = new double[size]; - double[] bChannel = new double[size]; - for (int row = 0; row < extent[1]; ++row) { - for (int col = 0; col < extent[2]; ++col) { - int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); - int offset = (row * (int) extent[2]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } + copyPixel1(extent, stack, wholeDataset, startIdx, endIdx, size); } ImagePlus imp = new ImagePlus(directory + name + " " + var.getName(), stack); // new for hyperstack @@ -722,37 +474,7 @@ public class HDF5Reader implements PlugIn { int endIdx = (int)(startIdx+stackSize-1); // long numElements = stackSize; - if (wholeDataset instanceof byte[]) { - byte[] tmp = Arrays.copyOfRange((byte[]) wholeDataset, startIdx, endIdx); - stack.addSlice(null, tmp); - } else if (wholeDataset instanceof short[]) { - short[] tmp = Arrays.copyOfRange((short[]) wholeDataset, startIdx, endIdx); - stack.addSlice(null, tmp); - } else if (wholeDataset instanceof int[]) { - int[] tmp = Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx); - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { - stack.addSlice(null, convertInt32ToFloat(tmp)); - } - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { - stack.addSlice(null, convertInt32ToShort(tmp)); - } - } else if (wholeDataset instanceof long[]) { - long[] tmp = Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx); - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { - stack.addSlice(null, convertInt64ToFloat(tmp)); - } - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { - stack.addSlice(null, convertInt64ToShort(tmp)); - } - } else if (wholeDataset instanceof float[]) { - float[] tmp = Arrays.copyOfRange((float[]) wholeDataset, startIdx, endIdx); - stack.addSlice(null, tmp); - } else if (wholeDataset instanceof double[]) { - float[] tmp = convertDoubleToFloat(Arrays.copyOfRange((double[]) wholeDataset, startIdx, endIdx)); - stack.addSlice(null, tmp); - } else { - logger.warning("Datatype not supported"); - } + convertDatatypesAndSlice(datatypeIfUnsupported, stack, wholeDataset, startIdx, endIdx); } } @@ -816,109 +538,8 @@ public class HDF5Reader implements PlugIn { int size = (int) (extent[1] * extent[0]); // ugly but working: copy pixel by pixel - if (slice instanceof byte[]) { - byte[] tmp = (byte[]) slice; - byte[] rChannel = new byte[size]; - byte[] gChannel = new byte[size]; - byte[] bChannel = new byte[size]; - for (int row = 0; row < extent[0]; ++row) { - for (int col = 0; col < extent[1]; ++col) { - int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); - int offset = (row * (int) extent[1]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (slice instanceof short[]) { - short[] tmp = (short[]) slice; - short[] rChannel = new short[size]; - short[] gChannel = new short[size]; - short[] bChannel = new short[size]; - for (int row = 0; row < extent[0]; ++row) { - for (int col = 0; col < extent[1]; ++col) { - int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); - int offset = (row * (int) extent[1]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (slice instanceof int[]) { - int[] tmp = (int[]) slice; - int[] rChannel = new int[size]; - int[] gChannel = new int[size]; - int[] bChannel = new int[size]; - for (int row = 0; row < extent[0]; ++row) { - for (int col = 0; col < extent[1]; ++col) { - int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); - int offset = (row * (int) extent[1]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (slice instanceof long[]) { - long[] tmp = (long[]) slice; - long[] rChannel = new long[size]; - long[] gChannel = new long[size]; - long[] bChannel = new long[size]; - for (int row = 0; row < extent[0]; ++row) { - for (int col = 0; col < extent[1]; ++col) { - int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); - int offset = (row * (int) extent[1]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (slice instanceof float[]) { - float[] tmp = (float[]) slice; - float[] rChannel = new float[size]; - float[] gChannel = new float[size]; - float[] bChannel = new float[size]; - for (int row = 0; row < extent[0]; ++row) { - for (int col = 0; col < extent[1]; ++col) { - int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); - int offset = (row * (int) extent[1]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } else if (slice instanceof double[]) { - double[] tmp = (double[]) slice; - double[] rChannel = new double[size]; - double[] gChannel = new double[size]; - double[] bChannel = new double[size]; - for (int row = 0; row < extent[0]; ++row) { - for (int col = 0; col < extent[1]; ++col) { - int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); - int offset = (row * (int) extent[1]) + col; - rChannel[offset] = tmp[offsetRGB + 0]; - gChannel[offset] = tmp[offsetRGB + 1]; - bChannel[offset] = tmp[offsetRGB + 2]; - } - } - stack.addSlice(null, rChannel); - stack.addSlice(null, gChannel); - stack.addSlice(null, bChannel); - } + copyPixels(extent, stack, slice, size); + IJ.showProgress(1.f); ImagePlus imp = new ImagePlus(directory + name + " " + var.getName(), stack); // new for hyperstack @@ -982,38 +603,7 @@ public class HDF5Reader implements PlugIn { int startIdx = (int)(lev * stackSize); int endIdx = (int)(startIdx+stackSize-1); // long numElements = stackSize; - if (wholeDataset instanceof byte[]) { - byte[] tmp = Arrays.copyOfRange((byte[]) wholeDataset, startIdx, endIdx); - stack.addSlice(null, tmp); - } else if (wholeDataset instanceof short[]) { - short[] tmp = Arrays.copyOfRange((short[]) wholeDataset, startIdx, endIdx); - stack.addSlice(null, tmp); - } else if (wholeDataset instanceof int[]) { - int[] tmp = Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx); - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { - stack.addSlice(null, convertInt32ToFloat(tmp)); - } - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { - stack.addSlice(null, convertInt32ToShort(tmp)); - } - } else if (wholeDataset instanceof long[]) { - - long[] tmp = Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx); - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { - stack.addSlice(null, convertInt64ToFloat(tmp)); - } - if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { - stack.addSlice(null, convertInt64ToShort(tmp)); - } - } else if (wholeDataset instanceof float[]) { - float[] tmp = Arrays.copyOfRange((float[]) wholeDataset, startIdx, endIdx); - stack.addSlice(null, tmp); - } else if (wholeDataset instanceof double[]) { - float[] tmp = convertDoubleToFloat(Arrays.copyOfRange((double[]) wholeDataset, startIdx, endIdx)); - stack.addSlice(null, tmp); - } else { - logger.warning("Not supported array type"); - } + convertDatatypesAndSlice(datatypeIfUnsupported, stack, wholeDataset, startIdx, endIdx); } IJ.showProgress(1.f); ImagePlus imp = new ImagePlus(directory + name + " " + var.getName(), stack); @@ -1149,13 +739,440 @@ public class HDF5Reader implements PlugIn { IJ.showProgress(1.0); } + + /** + * @param datatypeIfUnsupported + * @param stack + * @param wholeDataset + * @param startIdx + * @param endIdx + */ + private void convertDatatypesAndSlice(Datatype datatypeIfUnsupported, ImageStack stack, Object wholeDataset, int startIdx, int endIdx) { + if (wholeDataset instanceof byte[]) { + byte[] tmp = Arrays.copyOfRange((byte[]) wholeDataset, startIdx, endIdx); + stack.addSlice(null, tmp); + } else if (wholeDataset instanceof short[]) { + short[] tmp = Arrays.copyOfRange((short[]) wholeDataset, startIdx, endIdx); + stack.addSlice(null, tmp); + } else if (wholeDataset instanceof int[]) { + int[] tmp = Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx); + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { + stack.addSlice(null, convertInt32ToFloat(tmp)); + } + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { + stack.addSlice(null, convertInt32ToShort(tmp)); + } + } else if (wholeDataset instanceof long[]) { + long[] tmp = Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx); + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { + stack.addSlice(null, convertInt64ToFloat(tmp)); + } + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { + stack.addSlice(null, convertInt64ToShort(tmp)); + } + } else if (wholeDataset instanceof float[]) { + float[] tmp = Arrays.copyOfRange((float[]) wholeDataset, startIdx, endIdx); + stack.addSlice(null, tmp); + } else if (wholeDataset instanceof double[]) { + float[] tmp = convertDoubleToFloat(Arrays.copyOfRange((double[]) wholeDataset, startIdx, endIdx)); + stack.addSlice(null, tmp); + } else { + logger.warning("Not supported array type"); + } + } + + /** + * @param datatypeIfUnsupported + * @param extent + * @param stack + * @param wholeDataset + * @param size + * @param startIdx + * @param endIdx + */ + private void copyPixels3(Datatype datatypeIfUnsupported, long[] extent, ImageStack stack, Object wholeDataset, int size, int startIdx, int endIdx) { + if (wholeDataset instanceof byte[]) { + byte[] tmp = Arrays.copyOfRange((byte[]) wholeDataset, startIdx, endIdx); + byte[] rChannel = new byte[size]; + byte[] gChannel = new byte[size]; + byte[] bChannel = new byte[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof short[]) { + short[] tmp = Arrays.copyOfRange((short[]) wholeDataset, startIdx, endIdx); + short[] rChannel = new short[size]; + short[] gChannel = new short[size]; + short[] bChannel = new short[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof int[]) { + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { + float[] tmp = convertInt32ToFloat(Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx)); + float[] rChannel = new float[size]; + float[] gChannel = new float[size]; + float[] bChannel = new float[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { + short[] tmp = convertInt32ToShort(Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx)); + short[] rChannel = new short[size]; + short[] gChannel = new short[size]; + short[] bChannel = new short[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } + } else if (wholeDataset instanceof long[]) { + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_FLOAT) { + float[] tmp = convertInt64ToFloat(Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx)); + float[] rChannel = new float[size]; + float[] gChannel = new float[size]; + float[] bChannel = new float[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } + if (datatypeIfUnsupported.getDatatypeClass() == Datatype.CLASS_INTEGER) { + short[] tmp = convertInt64ToShort(Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx)); + short[] rChannel = new short[size]; + short[] gChannel = new short[size]; + short[] bChannel = new short[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } + } else if (wholeDataset instanceof float[]) { + float[] tmp = Arrays.copyOfRange((float[]) wholeDataset, startIdx, endIdx); + float[] rChannel = new float[size]; + float[] gChannel = new float[size]; + float[] bChannel = new float[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof double[]) { + float[] tmp = convertDoubleToFloat(Arrays.copyOfRange((double[]) wholeDataset, startIdx, endIdx)); + float[] rChannel = new float[size]; + float[] gChannel = new float[size]; + float[] bChannel = new float[size]; + for (int row = 0; row < extent[2]; ++row) { + for (int col = 0; col < extent[3]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else { + logger.warning("Datatype not supported"); + } + } + + + /** + * @param extent + * @param stack + * @param slice + * @param size + */ + private void copyPixels(long[] extent, ImageStack stack, Object slice, int size) { + if (slice instanceof byte[]) { + byte[] tmp = (byte[]) slice; + byte[] rChannel = new byte[size]; + byte[] gChannel = new byte[size]; + byte[] bChannel = new byte[size]; + for (int row = 0; row < extent[0]; ++row) { + for (int col = 0; col < extent[1]; ++col) { + int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); + int offset = (row * (int) extent[1]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (slice instanceof short[]) { + short[] tmp = (short[]) slice; + short[] rChannel = new short[size]; + short[] gChannel = new short[size]; + short[] bChannel = new short[size]; + for (int row = 0; row < extent[0]; ++row) { + for (int col = 0; col < extent[1]; ++col) { + int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); + int offset = (row * (int) extent[1]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (slice instanceof int[]) { + int[] tmp = (int[]) slice; + int[] rChannel = new int[size]; + int[] gChannel = new int[size]; + int[] bChannel = new int[size]; + for (int row = 0; row < extent[0]; ++row) { + for (int col = 0; col < extent[1]; ++col) { + int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); + int offset = (row * (int) extent[1]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (slice instanceof long[]) { + long[] tmp = (long[]) slice; + long[] rChannel = new long[size]; + long[] gChannel = new long[size]; + long[] bChannel = new long[size]; + for (int row = 0; row < extent[0]; ++row) { + for (int col = 0; col < extent[1]; ++col) { + int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); + int offset = (row * (int) extent[1]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (slice instanceof float[]) { + float[] tmp = (float[]) slice; + float[] rChannel = new float[size]; + float[] gChannel = new float[size]; + float[] bChannel = new float[size]; + for (int row = 0; row < extent[0]; ++row) { + for (int col = 0; col < extent[1]; ++col) { + int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); + int offset = (row * (int) extent[1]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (slice instanceof double[]) { + double[] tmp = (double[]) slice; + double[] rChannel = new double[size]; + double[] gChannel = new double[size]; + double[] bChannel = new double[size]; + for (int row = 0; row < extent[0]; ++row) { + for (int col = 0; col < extent[1]; ++col) { + int offsetRGB = (row * (int) extent[1] * 3) + (col * 3); + int offset = (row * (int) extent[1]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } + } + + + /** + * @param extent + * @param stack + * @param wholeDataset + * @param startIdx + * @param endIdx + * @param size + */ + private void copyPixel1(long[] extent, ImageStack stack, Object wholeDataset, int startIdx, int endIdx, int size) { + if (wholeDataset instanceof byte[]) { + byte[] tmp = Arrays.copyOfRange((byte[]) wholeDataset, startIdx, endIdx); + byte[] rChannel = new byte[size]; + byte[] gChannel = new byte[size]; + byte[] bChannel = new byte[size]; + for (int row = 0; row < extent[1]; ++row) { + for (int col = 0; col < extent[2]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof short[]) { + short[] tmp = Arrays.copyOfRange((short[]) wholeDataset, startIdx, endIdx); + short[] rChannel = new short[size]; + short[] gChannel = new short[size]; + short[] bChannel = new short[size]; + for (int row = 0; row < extent[1]; ++row) { + for (int col = 0; col < extent[2]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof int[]) { + int[] tmp = Arrays.copyOfRange((int[]) wholeDataset, startIdx, endIdx); + int[] rChannel = new int[size]; + int[] gChannel = new int[size]; + int[] bChannel = new int[size]; + for (int row = 0; row < extent[1]; ++row) { + for (int col = 0; col < extent[2]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof long[]) { + long[] tmp = Arrays.copyOfRange((long[]) wholeDataset, startIdx, endIdx); + long[] rChannel = new long[size]; + long[] gChannel = new long[size]; + long[] bChannel = new long[size]; + for (int row = 0; row < extent[1]; ++row) { + for (int col = 0; col < extent[2]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof float[]) { + float[] tmp = Arrays.copyOfRange((float[]) wholeDataset, startIdx, endIdx); + float[] rChannel = new float[size]; + float[] gChannel = new float[size]; + float[] bChannel = new float[size]; + for (int row = 0; row < extent[1]; ++row) { + for (int col = 0; col < extent[2]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } else if (wholeDataset instanceof double[]) { + double[] tmp = Arrays.copyOfRange((double[]) wholeDataset, startIdx, endIdx); + double[] rChannel = new double[size]; + double[] gChannel = new double[size]; + double[] bChannel = new double[size]; + for (int row = 0; row < extent[1]; ++row) { + for (int col = 0; col < extent[2]; ++col) { + int offsetRGB = (row * (int) extent[2] * 3) + (col * 3); + int offset = (row * (int) extent[2]) + col; + rChannel[offset] = tmp[offsetRGB + 0]; + gChannel[offset] = tmp[offsetRGB + 1]; + bChannel[offset] = tmp[offsetRGB + 2]; + } + } + stack.addSlice(null, rChannel); + stack.addSlice(null, gChannel); + stack.addSlice(null, bChannel); + } + } + /** * Recursively get list of all datasets in file * @param group Group to search for datasets * @param datasets List of datasets * @return List of datasets or null if group is null */ - private List getDataSetList(Group group, List datasets) { + private List getDatasets(Group group, List datasets) { if (group == null){ return datasets; } @@ -1165,38 +1182,14 @@ public class HDF5Reader implements PlugIn { ((Dataset) o).init(); datasets.add((Dataset) o); } else if (o instanceof Group) { - datasets = (getDataSetList((Group) o, datasets)); + datasets = (getDatasets((Group) o, datasets)); } } return datasets; } - /** - * Get attributes from object - * @param object Object to retrieve the attributes from - * @return Map of attributes or null if an error occurred while retrieving the attributes or the passed object is null - */ - private Map getAttributes(HObject object) { - if (object == null){ - return null; - } - - Map attributes = new HashMap<>(); - try{ - for(Object m: object.getMetadata()){ - if(m instanceof Attribute){ - attributes.put(((Attribute) m).getName(), (Attribute) m); - } - } - } - catch(Exception e){ - logger.warning("Unable to retrieve metadata from object"); - return null; - } - - return attributes; - } + private float[] convertDoubleToFloat(double[] dataIn) { @@ -1249,7 +1242,7 @@ public class HDF5Reader implements PlugIn { for (int i = 0; i < tmp.length; i++) if (tmp[i] < 0) tmp[i] = 0; - dataOut = (Object) tmp; + dataOut = tmp; } } else if (unsignedConvSelec == 1) { // convert to float @@ -1259,7 +1252,7 @@ public class HDF5Reader implements PlugIn { float[] tmp = new float[tmpIn.length]; for (int i = 0; i < tmp.length; i++) tmp[i] = (float) tmpIn[i]; - dataOut = (Object) tmp; + dataOut = tmp; } } return dataOut; diff --git a/src/main/java/ch/psi/imagej/hdf5/HDF5Utilities.java b/src/main/java/ch/psi/imagej/hdf5/HDF5Utilities.java new file mode 100644 index 0000000..4ca513f --- /dev/null +++ b/src/main/java/ch/psi/imagej/hdf5/HDF5Utilities.java @@ -0,0 +1,39 @@ +package ch.psi.imagej.hdf5; + +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +import ncsa.hdf.object.Attribute; +import ncsa.hdf.object.HObject; + +public class HDF5Utilities { + + private static final Logger logger = Logger.getLogger(HDF5Utilities.class.getName()); + + /** + * Get attributes from object + * @param object Object to retrieve the attributes from + * @return Map of attributes or null if an error occurred while retrieving the attributes or the passed object is null + */ + public static Map getAttributes(HObject object) { + if (object == null){ + return null; + } + + Map attributes = new HashMap<>(); + try{ + for(Object m: object.getMetadata()){ + if(m instanceof Attribute){ + attributes.put(((Attribute) m).getName(), (Attribute) m); + } + } + } + catch(Exception e){ + logger.warning("Unable to retrieve metadata from object"); + return null; + } + + return attributes; + } +} diff --git a/src/main/java/ch/psi/imagej/hdf5/HDF5Writer.java b/src/main/java/ch/psi/imagej/hdf5/HDF5Writer.java index a6124ef..261ef53 100644 --- a/src/main/java/ch/psi/imagej/hdf5/HDF5Writer.java +++ b/src/main/java/ch/psi/imagej/hdf5/HDF5Writer.java @@ -26,9 +26,6 @@ import ij.plugin.filter.PlugInFilter; import ij.process.*; import ij.gui.*; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; import java.util.logging.Logger; import javax.swing.tree.DefaultMutableTreeNode; @@ -40,15 +37,8 @@ import ncsa.hdf.hdf5lib.exceptions.HDF5Exception; public class HDF5Writer implements PlugInFilter { private static final Logger logger = Logger.getLogger(HDF5Writer.class.getName()); - - private Boolean _batchMode = false; - private String _batchFileName = null; public int setup(String arg, ImagePlus imp) { - if (arg.equals("about")) { - showAbout(); - return DONE; - } // FIXME: set DOES_xx for image type here: // currently RGB-Types are still missing // see @@ -56,34 +46,24 @@ public class HDF5Writer implements PlugInFilter { return DOES_8G + DOES_16 + DOES_32 + DOES_RGB + NO_CHANGES; } - public void setToBatchMode(String filename, String[] varnames) { - _batchMode = true; - _batchFileName = filename; - } - public void run(ImageProcessor ip) { - int[] wList = WindowManager.getIDList(); - if (wList == null) { + // Check whether windows are open + if (WindowManager.getIDList() == null) { IJ.error("No windows are open."); return; } - String filename = null; - if (_batchMode) { - filename = _batchFileName; - } else { - SaveDialog sd = new SaveDialog("Save HDF5 ...", "", ".h5"); - String directory = sd.getDirectory(); - String name = sd.getFileName(); - filename = directory + name; - - if (name == null) - return; - if (name == "") + // Query for filename to save datat to + SaveDialog sd = new SaveDialog("Save HDF5 ...", "", ".h5"); + String directory = sd.getDirectory(); + String name = sd.getFileName(); + if (name == null || name.equals("")){ return; } + String filename = directory + name; + // Retrieve an instance of the implementing class for the HDF5 format FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5); @@ -216,9 +196,7 @@ public class HDF5Writer implements PlugInFilter { logger.info("selected.length: " + Integer.toString(selected.length)); logger.info("channelDims.length: " + Integer.toString(channelDims.length)); if (nLevs == 1) { - for (int d = 0; d < selected.length; d++) { - selected[d] = channelDims[d]; - } + System.arraycopy(channelDims, 0, selected, 0, selected.length); int stackIndex = imp.getStackIndex(c + 1, 1, f + 1); logger.info("Stackindex: " + Integer.toString(stackIndex)); // get raw data @@ -232,9 +210,7 @@ public class HDF5Writer implements PlugInFilter { } } else { selected[0] = 1; - for (int d = 1; d < selected.length; d++) { - selected[d] = channelDims[d]; - } + System.arraycopy(channelDims, 1, selected, 1, selected.length - 1); long[] start = dataset.getStartDims(); // the // off // set @@ -438,9 +414,7 @@ public class HDF5Writer implements PlugInFilter { // dataet ImageStack stack = imp.getStack(); if (nLevels == 1) { - for (int d = 0; d < selected.length; d++) { - selected[d] = dims[d]; - } + System.arraycopy(dims, 0, selected, 0, selected.length); // get raw data Object slice = stack.getPixels(nLevels); if (imgColorType == ImagePlus.COLOR_RGB) @@ -450,9 +424,7 @@ public class HDF5Writer implements PlugInFilter { } else { selected[0] = 1; - for (int d = 1; d < selected.length; d++) { - selected[d] = dims[d]; - } + System.arraycopy(dims, 1, selected, 1, selected.length - 1); long[] start = dataset.getStartDims(); // the off set of // the selection for (int lvl = 0; lvl < nLevels; ++lvl) { @@ -480,7 +452,7 @@ public class HDF5Writer implements PlugInFilter { long[] attrDims = { 3 }; Attribute element_size_um = null; try { - element_size_um = getAttribute(dataset, "element_size_um"); + element_size_um = HDF5Utilities.getAttributes(dataset).get("element_size_um"); } catch (Exception e) { element_size_um = null; } @@ -504,16 +476,6 @@ public class HDF5Writer implements PlugInFilter { } - int byteToUnsignedByte(int n) { - if (n < 0) - return (256 + n); - return n; - } - - void showAbout() { - IJ.showMessage("About HDF5 Writer:", "Written by Matthias Schlachter\n" + "University of Freiburg, 2010"); - } - private static Group createGroupRecursive(String groupRelativName, Group group, FileFormat file) { if (groupRelativName == null || file == null) return null; @@ -618,49 +580,12 @@ public class HDF5Writer implements PlugInFilter { for (int d = 0; d < Rank; ++d) data_volume *= dataDims[d]; if (data_volume < maxChunkVol) { - for (int d = 0; d < Rank; ++d) - best_chunksize[d] = dataDims[d]; + System.arraycopy(dataDims, 0, best_chunksize, 0, Rank); return best_chunksize; } else return null; } - private static List getAttrList(Dataset ds) throws Exception { - if (ds == null) - return null; - - List attributes = new ArrayList(); - List members = ds.getMetadata(); - int n = members.size(); - Metadata obj = null; - for (int i = 0; i < n; i++) { - obj = (Metadata) members.get(i); - if (obj instanceof Attribute) { - try { - logger.info(((Attribute) obj).getName()); - attributes.add((Attribute) obj); - } catch (java.lang.UnsupportedOperationException e) { - logger.info("Caught UnsupportedOperationException datasets2.add((Dataset) obj)"); - logger.info(e.getMessage()); - } - } - } - return attributes; - } - - private static Attribute getAttribute(Dataset ds, String attrName) throws Exception { - List attrList = getAttrList(ds); - Iterator attrIter = attrList.iterator(); - - while (attrIter.hasNext()) { - Attribute attr = attrIter.next(); - if (attr.getName().equals(attrName)) { - return attr; - } - } - return null; - } - private Object computeRgbSlice(Object pixels) { byte rgbslice[]; int size = ((int[]) pixels).length; diff --git a/src/main/java/ch/psi/imagej/hdf5/TimeFrame.java b/src/main/java/ch/psi/imagej/hdf5/TimeFrame.java index 0b7210e..311c4a3 100644 --- a/src/main/java/ch/psi/imagej/hdf5/TimeFrame.java +++ b/src/main/java/ch/psi/imagej/hdf5/TimeFrame.java @@ -19,7 +19,7 @@ public class TimeFrame implements Comparable { public void addChannel(Integer index) { if (!channels.contains(index)){ - channels.add(new Integer(index)); + channels.add(index); } } diff --git a/src/test/java/ch/psi/imagej/hdf5/TimeFrameTest.java b/src/test/java/ch/psi/imagej/hdf5/TimeFrameTest.java new file mode 100644 index 0000000..2e53e86 --- /dev/null +++ b/src/test/java/ch/psi/imagej/hdf5/TimeFrameTest.java @@ -0,0 +1,25 @@ +package ch.psi.imagej.hdf5; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TimeFrameTest { + + private TimeFrame timeframe; + + @Before + public void setUp() throws Exception { + timeframe = new TimeFrame(1); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + System.out.println(timeframe.toString()); + } + +}