Search in sources :

Example 46 with IMetadata

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");
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) IMetadata(loci.formats.meta.IMetadata) Length(ome.units.quantity.Length) Time(ome.units.quantity.Time) Calibration(ij.measure.Calibration)

Example 47 with IMetadata

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();
    }
}
Also used : IFormatWriter(loci.formats.IFormatWriter) JPEG2000Writer(loci.formats.out.JPEG2000Writer) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) File(java.io.File) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 48 with IMetadata

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);
}
Also used : IMetadata(loci.formats.meta.IMetadata) Length(ome.units.quantity.Length) SkipException(org.testng.SkipException) Test(org.testng.annotations.Test)

Example 49 with IMetadata

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);
}
Also used : IMetadata(loci.formats.meta.IMetadata) SkipException(org.testng.SkipException) Test(org.testng.annotations.Test)

Example 50 with IMetadata

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);
}
Also used : IMetadata(loci.formats.meta.IMetadata) Time(ome.units.quantity.Time) SkipException(org.testng.SkipException) Test(org.testng.annotations.Test)

Aggregations

IMetadata (loci.formats.meta.IMetadata)62 ServiceFactory (loci.common.services.ServiceFactory)35 OMEXMLService (loci.formats.services.OMEXMLService)35 FormatException (loci.formats.FormatException)19 ImageReader (loci.formats.ImageReader)19 ServiceException (loci.common.services.ServiceException)17 Test (org.testng.annotations.Test)17 DependencyException (loci.common.services.DependencyException)15 SkipException (org.testng.SkipException)15 Length (ome.units.quantity.Length)13 IOException (java.io.IOException)9 ImageWriter (loci.formats.ImageWriter)6 Time (ome.units.quantity.Time)6 PositiveInteger (ome.xml.model.primitives.PositiveInteger)6 File (java.io.File)5 IFormatReader (loci.formats.IFormatReader)5 IFormatWriter (loci.formats.IFormatWriter)5 OMETiffWriter (loci.formats.out.OMETiffWriter)5 TiffWriter (loci.formats.out.TiffWriter)5 EnumerationException (ome.xml.model.enums.EnumerationException)4