use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class AnalyzeReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
// the dataset has two files - we want the one ending in '.hdr'
if (id.endsWith(".img")) {
LOGGER.info("Looking for header file");
String header = id.substring(0, id.lastIndexOf(".")) + ".hdr";
if (new Location(header).exists()) {
setId(header);
return;
} else
throw new FormatException("Header file not found.");
}
super.initFile(id);
in = new RandomAccessInputStream(id);
pixelsFilename = id.substring(0, id.lastIndexOf(".")) + ".img";
pixelFile = new RandomAccessInputStream(pixelsFilename);
LOGGER.info("Reading header");
int fileSize = in.readInt();
boolean little = fileSize != in.length();
in.order(little);
pixelFile.order(little);
in.skipBytes(10);
String imageName = in.readString(18);
in.skipBytes(8);
int ndims = in.readShort();
int x = in.readShort();
int y = in.readShort();
int z = in.readShort();
int t = in.readShort();
in.skipBytes(20);
int dataType = in.readShort();
int nBitsPerPixel = in.readShort();
String description = null;
double voxelWidth = 0d, voxelHeight = 0d, sliceThickness = 0d, deltaT = 0d;
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
in.skipBytes(6);
voxelWidth = in.readFloat();
voxelHeight = in.readFloat();
sliceThickness = in.readFloat();
deltaT = in.readFloat();
in.skipBytes(12);
pixelOffset = (int) in.readFloat();
in.skipBytes(12);
float calibratedMax = in.readFloat();
float calibratedMin = in.readFloat();
float compressed = in.readFloat();
float verified = in.readFloat();
float pixelMax = in.readFloat();
float pixelMin = in.readFloat();
description = in.readString(80);
String auxFile = in.readString(24);
char orient = (char) in.readByte();
String originator = in.readString(10);
String generated = in.readString(10);
String scannum = in.readString(10);
String patientID = in.readString(10);
String expDate = in.readString(10);
String expTime = in.readString(10);
in.skipBytes(3);
int views = in.readInt();
int volsAdded = in.readInt();
int startField = in.readInt();
int fieldSkip = in.readInt();
int omax = in.readInt();
int omin = in.readInt();
int smax = in.readInt();
int smin = in.readInt();
addGlobalMeta("Database name", imageName);
addGlobalMeta("Number of dimensions", ndims);
addGlobalMeta("Data type", dataType);
addGlobalMeta("Number of bits per pixel", nBitsPerPixel);
addGlobalMeta("Voxel width", voxelWidth);
addGlobalMeta("Voxel height", voxelHeight);
addGlobalMeta("Slice thickness", sliceThickness);
addGlobalMeta("Exposure time", deltaT);
addGlobalMeta("Pixel offset", pixelOffset);
addGlobalMeta("Calibrated maximum", calibratedMax);
addGlobalMeta("Calibrated minimum", calibratedMin);
addGlobalMeta("Compressed", compressed);
addGlobalMeta("Verified", verified);
addGlobalMeta("Pixel maximum", pixelMax);
addGlobalMeta("Pixel minimum", pixelMin);
addGlobalMeta("Description", description);
addGlobalMeta("Auxiliary file", auxFile);
addGlobalMeta("Orientation", orient);
addGlobalMeta("Originator", originator);
addGlobalMeta("Generated", generated);
addGlobalMeta("Scan Number", scannum);
addGlobalMeta("Patient ID", patientID);
addGlobalMeta("Acquisition Date", expDate);
addGlobalMeta("Acquisition Time", expTime);
} else {
in.skipBytes(34);
pixelOffset = (int) in.readFloat();
}
LOGGER.info("Populating core metadata");
CoreMetadata m = core.get(0);
m.littleEndian = little;
m.sizeX = x;
m.sizeY = y;
m.sizeZ = z;
m.sizeT = t;
m.sizeC = 1;
if (getSizeZ() == 0)
m.sizeZ = 1;
if (getSizeT() == 0)
m.sizeT = 1;
m.imageCount = getSizeZ() * getSizeT();
m.rgb = false;
m.interleaved = false;
m.indexed = false;
m.dimensionOrder = "XYZTC";
switch(dataType) {
case 1:
case 2:
m.pixelType = FormatTools.UINT8;
break;
case 4:
m.pixelType = FormatTools.INT16;
break;
case 8:
m.pixelType = FormatTools.INT32;
break;
case 16:
m.pixelType = FormatTools.FLOAT;
break;
case 64:
m.pixelType = FormatTools.DOUBLE;
break;
case 128:
m.pixelType = FormatTools.UINT8;
m.sizeC = 3;
m.rgb = true;
m.interleaved = true;
m.dimensionOrder = "XYCZT";
default:
throw new FormatException("Unsupported data type: " + dataType);
}
LOGGER.info("Populating MetadataStore");
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
store.setImageName(imageName, 0);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
store.setImageDescription(description, 0);
Length sizeX = FormatTools.getPhysicalSizeX(voxelWidth, UNITS.MILLIMETER);
Length sizeY = FormatTools.getPhysicalSizeY(voxelHeight, UNITS.MILLIMETER);
Length sizeZ = FormatTools.getPhysicalSizeZ(sliceThickness, UNITS.MILLIMETER);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, 0);
}
store.setPixelsTimeIncrement(new Time(deltaT, UNITS.MILLISECOND), 0);
}
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class AmiraReader method getLookupTable.
// -- Helper methods --
byte[][] getLookupTable(Map materials) throws FormatException {
byte[][] result = new byte[3][256];
int i = -1;
for (Object label : materials.keySet()) {
i++;
Object object = materials.get(label);
if (!(object instanceof Map)) {
throw new FormatException("Invalid material: " + label);
}
Map material = (Map) object;
object = material.get("Color");
// black
if (object == null)
continue;
if (!(object instanceof Number[])) {
throw new FormatException("Invalid material: " + label);
}
Number[] color = (Number[]) object;
if (color.length != 3) {
throw new FormatException("Invalid color: " + color.length + " channels");
}
for (int j = 0; j < 3; j++) {
result[j][i] = (byte) (int) (255 * color[j].floatValue());
}
}
return result;
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class BDReader method isThisType.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(String, boolean) */
@Override
public boolean isThisType(String name, boolean open) {
Location location = new Location(name);
String id = location.getAbsolutePath();
boolean dirCheck = location.isDirectory();
if (dirCheck)
return false;
if (name.endsWith(EXPERIMENT_FILE))
return true;
if (!open)
return false;
try {
id = locateExperimentFile(id);
} catch (FormatException f) {
return false;
} catch (IOException f) {
return false;
} catch (NullPointerException e) {
return false;
}
if (id.endsWith(EXPERIMENT_FILE)) {
return true;
}
return super.isThisType(name, open);
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class BDReader method locateExperimentFile.
// -- Helper methods --
/* Locate the experiment file given any file in set */
private String locateExperimentFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "exp")) {
Location parent = new Location(id).getAbsoluteFile().getParentFile();
if (checkSuffix(id, "tif"))
parent = parent.getParentFile();
Location expFile = new Location(parent, EXPERIMENT_FILE);
if (expFile.exists()) {
return expFile.getAbsolutePath();
}
throw new FormatException("Could not find " + EXPERIMENT_FILE + " in " + parent.getAbsolutePath());
}
return id;
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class WriterUtilities method createMetadata.
public static IMetadata createMetadata(String pixelType, int rgbChannels, int seriesCount, boolean littleEndian, int sizeT) throws Exception {
IMetadata metadata;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
metadata = 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);
}
for (int i = 0; i < seriesCount; i++) {
MetadataTools.populateMetadata(metadata, i, "image #" + i, littleEndian, "XYCZT", pixelType, 160, 160, 1, rgbChannels, sizeT, rgbChannels);
}
return metadata;
}
Aggregations