use of gov.nih.mipav.model.file.FileInfoImageXML in project bioformats by openmicroscopy.
the class PlugInBioFormatsImporter method readImage.
public void readImage() {
final ViewUserInterface mipav = ViewUserInterface.getReference();
// prompt user to choose a file
if (chooser == null) {
chooser = GUITools.buildFileChooser(reader);
chooser.setCurrentDirectory(new File(Preferences.getImageDirectory()));
}
JFrame parent = mipav.getMainFrame();
int rval = chooser.showOpenDialog(parent);
// user canceled
if (rval != JFileChooser.APPROVE_OPTION)
return;
final File file = chooser.getSelectedFile();
// load the image in a separate thread
Thread importerThread = new Thread("BioFormats-Importer") {
public void run() {
String name = file.getName();
String dir = file.getParent();
// open file using Bio-Formats
setMessage(mipav, "Importing " + name + "...", true);
String id = file.getPath();
try {
long tic = System.currentTimeMillis();
IMetadata store;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
store = service.createOMEXMLMetadata();
} catch (DependencyException exc) {
throw new FormatException("Could not create OME-XML store.", exc);
} catch (ServiceException exc) {
throw new FormatException("Could not create OME-XML store.", exc);
}
reader.setMetadataStore(store);
reader.setId(id);
// MIPAV assumes 4-D data in XYZT order
reader.setOutputOrder("XYZTC");
// harvest some core metadata
int imageCount = reader.getImageCount();
boolean little = reader.isLittleEndian();
int pixelType = reader.getPixelType();
int bpp = FormatTools.getBytesPerPixel(pixelType);
boolean floating = FormatTools.isFloatingPoint(pixelType);
int sizeX = reader.getSizeX();
int sizeY = reader.getSizeY();
int sizeZ = reader.getSizeZ();
int sizeT = reader.getSizeT();
int sizeC = reader.getSizeC();
String imageName = store.getImageName(0);
if (sizeC > 1) {
throw new FormatException("Multichannel data is unsupported at the moment");
}
// compute MIPAV buffer type
int mipavType;
switch(pixelType) {
case FormatTools.INT8:
mipavType = ModelStorageBase.BYTE;
break;
case FormatTools.UINT8:
mipavType = ModelStorageBase.UBYTE;
break;
case FormatTools.INT16:
mipavType = ModelStorageBase.SHORT;
break;
case FormatTools.UINT16:
mipavType = ModelStorageBase.USHORT;
break;
case FormatTools.INT32:
mipavType = ModelStorageBase.INTEGER;
break;
case FormatTools.UINT32:
mipavType = ModelStorageBase.UINTEGER;
break;
case FormatTools.FLOAT:
mipavType = ModelStorageBase.FLOAT;
break;
case FormatTools.DOUBLE:
mipavType = ModelStorageBase.DOUBLE;
break;
default:
throw new FormatException("Unsupported pixel type: " + pixelType);
}
// harvest physical resolution
Length dimPhysSizeX = store.getPixelsPhysicalSizeX(0);
Length dimPhysSizeY = store.getPixelsPhysicalSizeY(0);
Length dimPhysSizeZ = store.getPixelsPhysicalSizeZ(0);
Time dimTimeInc = store.getPixelsTimeIncrement(0);
float physSizeX = dimPhysSizeX == null ? 1.0f : dimPhysSizeX.value(UNITS.MICROMETER).floatValue();
float physSizeY = dimPhysSizeY == null ? 1.0f : dimPhysSizeY.value(UNITS.MICROMETER).floatValue();
float physSizeZ = dimPhysSizeZ == null ? 1.0f : dimPhysSizeZ.value(UNITS.MICROMETER).floatValue();
float timeInc = dimTimeInc == null ? 1.0f : dimTimeInc.value(UNITS.SECOND).floatValue();
// compute dimensional extents
int[] dimExtents = { sizeX, sizeY, sizeZ, sizeT };
float[] res = { physSizeX, physSizeY, physSizeZ, timeInc };
int[] units = { FileInfoBase.MICROMETERS, FileInfoBase.MICROMETERS, FileInfoBase.MICROMETERS, FileInfoBase.SECONDS };
// create MIPAV image object
ModelImage modelImage = new ModelImage(mipavType, dimExtents, imageName);
// import planes into MIPAV image
byte[] buf = new byte[bpp * sizeX * sizeY];
for (int i = 0; i < imageCount; i++) {
setMessage(mipav, "Reading plane #" + (i + 1) + "/" + imageCount, false);
reader.openBytes(i, buf);
// convert byte array to appropriate primitive type
int offset = i * buf.length;
Object array = DataTools.makeDataArray(buf, bpp, floating, little);
// assign data to MIPAV image object
switch(mipavType) {
case ModelStorageBase.BYTE:
case ModelStorageBase.UBYTE:
modelImage.importData(offset, (byte[]) array, false);
break;
case ModelStorageBase.SHORT:
case ModelStorageBase.USHORT:
modelImage.importData(offset, (short[]) array, false);
break;
case ModelStorageBase.INTEGER:
case ModelStorageBase.UINTEGER:
modelImage.importData(offset, (int[]) array, false);
break;
case ModelStorageBase.FLOAT:
modelImage.importData(offset, (float[]) array, false);
break;
case ModelStorageBase.DOUBLE:
modelImage.importData(offset, (double[]) array, false);
break;
default:
throw new FormatException("Unknown buffer type: " + mipavType);
}
}
setMessage(mipav, "Finishing import...", true);
// create a FileInfo object for each image plane
FileInfoBase[] fileInfo = new FileInfoBase[imageCount];
for (int i = 0; i < imageCount; i++) {
// HACK: Use FileInfoImageXML since FileInfoBase is abstract.
fileInfo[i] = new FileInfoImageXML(name, dir, FileUtility.XML);
fileInfo[i].setExtents(dimExtents);
fileInfo[i].setResolutions(res);
fileInfo[i].setUnitsOfMeasure(units);
fileInfo[i].setDataType(mipavType);
}
modelImage.setFileInfo(fileInfo);
// scale color range and display MIPAV image
modelImage.calcMinMax();
new ViewJFrameImage(modelImage);
long toc = System.currentTimeMillis();
long time = toc - tic;
long avg = time / imageCount;
setMessage(mipav, name + ": Read " + imageCount + " planes in " + (time / 1000f) + " seconds (" + avg + " ms/plane)", true);
} catch (FormatException exc) {
exc.printStackTrace();
MipavUtil.displayError("An error occurred parsing the file: " + exc.getMessage());
} catch (IOException exc) {
exc.printStackTrace();
MipavUtil.displayError("An I/O error occurred reading the file: " + exc.getMessage());
}
}
};
importerThread.start();
}
Aggregations