/* ========================================================================= * * Copyright 2011 Matthias Schlachter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *=========================================================================*/ import ij.*; import ij.io.*; import ij.plugin.*; import ij.plugin.filter.PlugInFilter; import ij.process.*; import ij.gui.*; import java.awt.*; import java.lang.Math; import java.io.IOException; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.swing.tree.DefaultMutableTreeNode; import ncsa.hdf.object.*; // the common object package import ncsa.hdf.object.h5.*; // the HDF5 implementation import ncsa.hdf.hdf5lib.exceptions.HDF5Exception; import ncsa.hdf.hdflib.HDFException; public class HDF5_Writer_ implements PlugInFilter { private Boolean _batchMode = false; private String[] _batchVarNames = null; 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 // http://rsb.info.nih.gov/ij/developer/api/ij/plugin/filter/PlugInFilter.html return DOES_8G + DOES_16 + DOES_32 + DOES_RGB + NO_CHANGES; } public void setToBatchMode(String filename, String [] varnames) { _batchMode = true; _batchFileName = filename; _batchVarNames = varnames; } public void run(ImageProcessor ip) { int[] wList = WindowManager.getIDList(); if (wList == 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 == "") return; } // Retrieve an instance of the implementing class for the HDF5 format FileFormat fileFormat = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5); // If the implementing class wasn't found, it's an error. if (fileFormat == null) { System.err.println("Cannot find HDF5 FileFormat."); return; } String[] varNames = null; if(_batchMode) { varNames = _batchVarNames; } ImagePlus imp = WindowManager.getCurrentImage(); GenericDialog gd = null; gd = new GenericDialog("Variable Name Selection"); // check for hyperstack if(imp.getOpenAsHyperStack() || imp.isHyperStack()) { System.out.println("This is a hyperstack"); boolean splitChannels = true; gd.addCheckbox("Split frames and channels",splitChannels); gd.addStringField(imp.getTitle(), "/t$T/channel$C"); String title = imp.getTitle(); int nDims = imp.getNDimensions(); int nFrames = imp.getNFrames(); int nChannels = imp.getNChannels(); int nLevs = imp.getNSlices(); int nRows = imp.getHeight(); int nCols = imp.getWidth(); boolean isComposite = imp.isComposite() ; System.out.println("isComposite: "+Boolean.toString(isComposite)); System.out.println("Saving image \""+title+"\""); System.out.println("nDims: "+Integer.toString(nDims)); System.out.println("nFrames: "+Integer.toString(nFrames)); System.out.println("nChannels: "+Integer.toString(nChannels)); System.out.println("nSlices: "+Integer.toString(nLevs)); System.out.println("nRows: "+Integer.toString(nRows)); System.out.println("nCols: "+Integer.toString(nCols)); gd.showDialog(); if (gd.wasCanceled()) { IJ.error("Plugin canceled!"); return; } splitChannels = gd.getNextBoolean(); String formatString = gd.getNextString(); System.out.println("formatString: "+formatString); System.out.println("Bitdepth: "+ imp.getBitDepth()); System.out.println("Saving HDF5 File: " + filename); int imgColorDepth = imp.getBitDepth(); int imgColorType = imp.getType(); Datatype type = null; if (imgColorType == ImagePlus.GRAY8) { System.out.println(" bit depth: " + imgColorDepth + ", type: GRAY8"); type = new H5Datatype(Datatype.CLASS_CHAR, Datatype.NATIVE, Datatype.NATIVE, Datatype.SIGN_NONE); } else if (imgColorType == ImagePlus.GRAY16) { System.out.println(" bit depth: " + imgColorDepth + ", type: GRAY16"); int typeSizeInByte = 2; type = new H5Datatype(Datatype.CLASS_INTEGER, typeSizeInByte, Datatype.NATIVE, Datatype.SIGN_NONE); } else if (imgColorType == ImagePlus.GRAY32) { System.out.println(" bit depth: " + imgColorDepth + ", type: GRAY32"); int typeSizeInByte = 4; type = new H5Datatype(Datatype.CLASS_FLOAT, Datatype.NATIVE, Datatype.NATIVE, -1); } // open the outfile H5File outFile = null; try { outFile = (H5File) fileFormat.createFile(filename, FileFormat.FILE_CREATE_OPEN); if (!outFile.canWrite()) { IJ.error("File `" + filename + "`is readonly!"); return; } // open the file outFile.open(); if(splitChannels) { // parse format string String[] formatTokens = HDF5_GroupedVarnames. parseFormatString(formatString, "[0-9]+"); // dummy regexp long[] channelDims = null; if(nLevs>1) { channelDims = new long[3]; channelDims[0] = nLevs; channelDims[1] = nRows; channelDims[2] = nCols; } else { channelDims = new long[2]; channelDims[0] = nRows; channelDims[1] = nCols; } // iterate over frames and channels ImageStack stack = imp.getStack(); for(int f=0;f 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 { System.out.println(((Attribute) obj).getName()); attributes.add((Attribute) obj); } catch (java.lang.UnsupportedOperationException e) { System.out .println("Caught UnsupportedOperationException datasets2.add((Dataset) obj)"); System.out.println(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; rgbslice = new byte[size*3]; for(int i=0;i> 16; int green= (((int[]) pixels)[i] & 0x00ff00) >> 8; int blue= ((int[]) pixels)[i] & 0x0000ff; rgbslice[3*i + 0] = (byte) red; rgbslice[3*i + 1] = (byte) green; rgbslice[3*i + 2] =(byte) blue; } return rgbslice; } private String makeDataSetName(String[] toks,int frame,int channel) { String dName = toks[0] + Integer.toString(frame) + toks[1] + Integer.toString(channel); if(toks.length>2) dName = dName + toks[2]; return dName; } // end of class }