use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class Calibrator method applyCalibration.
// -- Calibrator methods --
/**
* Applies spatial calibrations to an image stack.
*/
public void applyCalibration(ImagePlus imp) {
final IMetadata meta = process.getOMEMetadata();
final int series = (Integer) imp.getProperty(ImagePlusReader.PROP_SERIES);
double xcal = Double.NaN, ycal = Double.NaN;
double zcal = Double.NaN, tcal = Double.NaN;
Length xd = meta.getPixelsPhysicalSizeX(series);
if (xd != null && xd.unit().isConvertible(UNITS.MICROMETER))
xcal = xd.value(UNITS.MICROMETER).doubleValue();
Length yd = meta.getPixelsPhysicalSizeY(series);
if (yd != null && yd.unit().isConvertible(UNITS.MICROMETER))
ycal = yd.value(UNITS.MICROMETER).doubleValue();
Length zd = meta.getPixelsPhysicalSizeZ(series);
if (zd != null && zd.unit().isConvertible(UNITS.MICROMETER))
zcal = zd.value(UNITS.MICROMETER).doubleValue();
Time td = meta.getPixelsTimeIncrement(series);
if (td != null)
tcal = td.value(UNITS.SECOND).doubleValue();
boolean xcalPresent = !Double.isNaN(xcal);
boolean ycalPresent = !Double.isNaN(ycal);
boolean zcalPresent = !Double.isNaN(zcal);
boolean tcalPresent = !Double.isNaN(tcal) && tcal != 0;
// assume that the width and height are equal.
if (xcalPresent && !ycalPresent)
ycal = xcal;
else if (ycalPresent && !xcalPresent)
xcal = ycal;
// average any variable time interval values.
if (!tcalPresent)
tcal = computeVariableTimeInterval(meta, series);
xcalPresent = !Double.isNaN(xcal);
ycalPresent = !Double.isNaN(ycal);
zcalPresent = !Double.isNaN(zcal);
tcalPresent = !Double.isNaN(tcal);
final boolean hasSpatial = xcalPresent || ycalPresent || zcalPresent;
final boolean hasCalibration = hasSpatial || ycalPresent;
if (hasCalibration) {
// set calibration only if at least one value is present
Calibration cal = new Calibration();
if (hasSpatial)
cal.setUnit("micron");
if (xcalPresent)
cal.pixelWidth = xcal == 0 ? 1 : xcal;
if (ycalPresent)
cal.pixelHeight = ycal == 0 ? 1 : ycal;
if (zcalPresent)
cal.pixelDepth = zcal == 0 ? 1 : zcal;
if (tcalPresent)
cal.frameInterval = tcal == 0 ? 1 : tcal;
imp.setCalibration(cal);
}
String type = meta.getPixelsType(series).toString();
int pixelType = FormatTools.pixelTypeFromString(type);
// NB: INT32 is represented with FloatProcessor, so no need to calibrate.
boolean signed = pixelType == FormatTools.INT8 || // || pixelType == FormatTools.INT32;
pixelType == FormatTools.INT16;
// values are shown
if (signed) {
int bitsPerPixel = FormatTools.getBytesPerPixel(pixelType) * 8;
double min = -1 * Math.pow(2, bitsPerPixel - 1);
imp.getLocalCalibration().setFunction(Calibration.STRAIGHT_LINE, new double[] { min, 1.0 }, "gray value");
}
}
use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class EightBitLosslessJPEG2000Test method setUp.
@BeforeMethod
public void setUp() throws Exception {
for (byte v = Byte.MIN_VALUE; v < Byte.MAX_VALUE; v++) {
int index = v + Byte.MAX_VALUE + 1;
pixels[index][0] = v;
String file = index + ".jp2";
File tempFile = File.createTempFile("test", ".jp2");
tempFile.deleteOnExit();
Location.mapId(file, tempFile.getAbsolutePath());
files.add(file);
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);
}
MetadataTools.populateMetadata(metadata, 0, "foo", false, "XYCZT", "uint8", 1, 1, 1, 1, 1, 1);
IFormatWriter writer = new JPEG2000Writer();
writer.setMetadataRetrieve(metadata);
writer.setId(file);
writer.saveBytes(0, pixels[index]);
writer.close();
}
}
use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class FormatReaderTest method testPhysicalSizeZ.
@Test(groups = { "all", "fast", "automated" })
public void testPhysicalSizeZ() {
if (config == null)
throw new SkipException("No config tree");
String testName = "PhysicalSizeZ";
if (!initFile())
result(testName, false, "initFile");
IMetadata retrieve = (IMetadata) reader.getMetadataStore();
for (int i = 0; i < reader.getSeriesCount(); i++) {
config.setSeries(i);
Length expectedSize = config.getPhysicalSizeZ();
Length realSize = retrieve.getPixelsPhysicalSizeZ(i);
if (!isAlmostEqual(realSize, expectedSize)) {
result(testName, false, "Series " + i + " (expected " + expectedSize + ", actual " + realSize + ")");
}
}
result(testName, true);
}
use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class FormatReaderTest method testImageDescriptions.
@Test(groups = { "all", "fast", "automated" })
public void testImageDescriptions() {
if (config == null)
throw new SkipException("No config tree");
String testName = "ImageDescriptions";
if (!initFile())
result(testName, false, "initFile");
IMetadata retrieve = (IMetadata) reader.getMetadataStore();
for (int i = 0; i < reader.getSeriesCount(); i++) {
config.setSeries(i);
String realDescription = retrieve.getImageDescription(i);
if (realDescription != null) {
realDescription = realDescription.trim();
}
if (config.hasImageDescription()) {
String expectedDescription = config.getImageDescription();
if (expectedDescription != null) {
expectedDescription = expectedDescription.trim();
}
if (!expectedDescription.equals(realDescription) && !(realDescription == null && expectedDescription.equals("null"))) {
result(testName, false, "Series " + i + " (got '" + realDescription + "', expected '" + expectedDescription + "')");
}
}
}
result(testName, true);
}
use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class FormatReaderTest method testTimeIncrement.
@Test(groups = { "all", "fast", "automated" })
public void testTimeIncrement() {
if (config == null)
throw new SkipException("No config tree");
String testName = "TimeIncrement";
if (!initFile())
result(testName, false, "initFile");
IMetadata retrieve = (IMetadata) reader.getMetadataStore();
for (int i = 0; i < reader.getSeriesCount(); i++) {
config.setSeries(i);
Time expectedIncrement = config.getTimeIncrement();
Time realIncrement = retrieve.getPixelsTimeIncrement(i);
if (!isAlmostEqual(expectedIncrement, realIncrement)) {
result(testName, false, "Series " + i + " (expected " + expectedIncrement + ", actual " + realIncrement + ")");
}
}
result(testName, true);
}
Aggregations