Search in sources :

Example 61 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class JPEGXRCodec method initialize.

// -- Helper methods --
/**
 * Initializes the JPEG-XR dependency service. This is called at the
 * beginning of the {@link #decompress} method to avoid having the
 * constructor's method definition contain a checked exception.
 *
 * @throws FormatException If there is an error initializing JPEG-XR
 * services.
 */
private void initialize() throws FormatException {
    if (service != null)
        return;
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(JPEGXRService.class);
    } catch (DependencyException e) {
        throw new MissingLibraryException("JPEG-XR library not available", e);
    }
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) MissingLibraryException(loci.formats.MissingLibraryException) JPEGXRService(loci.formats.services.JPEGXRService) DependencyException(loci.common.services.DependencyException)

Example 62 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class APLReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    LOGGER.debug("Initializing {}", id);
    // find the corresponding .mtb file
    if (!checkSuffix(id, "mtb")) {
        if (checkSuffix(id, METADATA_SUFFIXES)) {
            int separator = id.lastIndexOf(File.separator);
            if (separator < 0)
                separator = 0;
            int underscore = id.lastIndexOf("_");
            if (underscore < separator || checkSuffix(id, "apl")) {
                underscore = id.lastIndexOf(".");
            }
            String mtbFile = id.substring(0, underscore) + "_d.mtb";
            if (!new Location(mtbFile).exists()) {
                throw new FormatException(".mtb file not found");
            }
            currentId = new Location(mtbFile).getAbsolutePath();
        } else {
            Location parent = new Location(id).getAbsoluteFile().getParentFile();
            parent = parent.getParentFile();
            String[] list = parent.list(true);
            for (String f : list) {
                if (checkSuffix(f, "mtb")) {
                    currentId = new Location(parent, f).getAbsolutePath();
                    break;
                }
            }
            if (!checkSuffix(currentId, "mtb")) {
                throw new FormatException(".mtb file not found");
            }
        }
    }
    String mtb = new Location(currentId).getAbsolutePath();
    LOGGER.debug("Reading .mtb file '{}'", mtb);
    MDBService mdb = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        mdb = factory.getInstance(MDBService.class);
    } catch (DependencyException de) {
        throw new FormatException("MDB Tools Java library not found", de);
    }
    String[] columnNames = null;
    List<String[]> rows = null;
    try {
        mdb.initialize(mtb);
        rows = mdb.parseDatabase().get(0);
        columnNames = rows.get(0);
        String[] tmpNames = columnNames;
        columnNames = new String[tmpNames.length - 1];
        System.arraycopy(tmpNames, 1, columnNames, 0, columnNames.length);
    } finally {
        mdb.close();
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        for (int i = 1; i < rows.size(); i++) {
            String[] row = rows.get(i);
            for (int q = 0; q < row.length; q++) {
                addGlobalMetaList(columnNames[q], row[q]);
            }
        }
    }
    used = new ArrayList<String>();
    used.add(mtb);
    String tnb = mtb.substring(0, mtb.lastIndexOf("."));
    if (tnb.lastIndexOf("_") > tnb.lastIndexOf(File.separator)) {
        tnb = tnb.substring(0, tnb.lastIndexOf("_"));
    }
    used.add(tnb + "_1.tnb");
    used.add(tnb + ".apl");
    String idPath = new Location(id).getAbsolutePath();
    if (!used.contains(idPath) && checkSuffix(idPath, METADATA_SUFFIXES)) {
        used.add(idPath);
    }
    // calculate indexes to relevant metadata
    int calibrationUnit = DataTools.indexOf(columnNames, "Calibration Unit");
    int colorChannels = DataTools.indexOf(columnNames, "Color Channels");
    int frames = DataTools.indexOf(columnNames, "Frames");
    int calibratedHeight = DataTools.indexOf(columnNames, "Height");
    int calibratedWidth = DataTools.indexOf(columnNames, "Width");
    int path = DataTools.indexOf(columnNames, "Image Path");
    if (path == -1) {
        path = DataTools.indexOf(columnNames, "Path");
    }
    int filename = DataTools.indexOf(columnNames, "File Name");
    int magnification = DataTools.indexOf(columnNames, "Magnification");
    int width = DataTools.indexOf(columnNames, "X-Resolution");
    int height = DataTools.indexOf(columnNames, "Y-Resolution");
    int imageType = DataTools.indexOf(columnNames, "Image Type");
    int imageName = DataTools.indexOf(columnNames, "Image Name");
    int zLayers = DataTools.indexOf(columnNames, "Z-Layers");
    String parentDirectory = mtb.substring(0, mtb.lastIndexOf(File.separator));
    // look for the directory that contains TIFF and XML files
    LOGGER.debug("Searching {} for a directory with TIFFs", parentDirectory);
    Location dir = new Location(parentDirectory);
    String topDirectory = null;
    int index = 2;
    String pathName = rows.get(index++)[path].trim();
    while (pathName.equals("") && index < rows.size()) {
        pathName = rows.get(index++)[path].trim();
    }
    pathName = pathName.replace('\\', File.separatorChar);
    pathName = pathName.replaceAll("/", File.separator);
    String[] dirs = pathName.split(File.separatorChar == '\\' ? "\\\\" : File.separator);
    for (int i = dirs.length - 1; i >= 0; i--) {
        if (dirs[i].indexOf("_DocumentFiles") > 0) {
            Location file = new Location(dir, dirs[i]);
            if (file.exists()) {
                topDirectory = file.getAbsolutePath();
                break;
            }
        }
    }
    if (topDirectory == null) {
        String[] list = dir.list();
        for (String f : list) {
            LOGGER.debug("  '{}'", f);
            Location file = new Location(dir, f);
            if (file.isDirectory() && f.indexOf("_DocumentFiles") > 0) {
                topDirectory = file.getAbsolutePath();
                LOGGER.debug("Found {}", topDirectory);
                break;
            }
        }
    }
    if (topDirectory == null) {
        throw new FormatException("Could not find a directory with TIFF files.");
    }
    final List<Integer> seriesIndexes = new ArrayList<Integer>();
    for (int i = 1; i < rows.size(); i++) {
        String file = parseFilename(rows.get(i), filename, path);
        if (file.equals(""))
            continue;
        file = topDirectory + File.separator + file;
        if (new Location(file).exists() && checkSuffix(file, "tif")) {
            seriesIndexes.add(i);
        }
    }
    int seriesCount = seriesIndexes.size();
    core.clear();
    tiffFiles = new String[seriesCount];
    xmlFiles = new String[seriesCount];
    parser = new TiffParser[seriesCount];
    ifds = new IFDList[seriesCount];
    for (int i = 0; i < seriesCount; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        int secondRow = seriesIndexes.get(i);
        int firstRow = secondRow - 1;
        String[] row2 = rows.get(firstRow);
        String[] row3 = rows.get(secondRow);
        if (frames > -1) {
            ms.sizeT = parseDimension(row3[frames]);
        }
        if (zLayers > -1) {
            ms.sizeZ = parseDimension(row3[zLayers]);
        }
        if (colorChannels > -1) {
            ms.sizeC = parseDimension(row3[colorChannels]);
        } else if (imageType > -1) {
            if (row3[imageType] != null && row3[imageType].equals("RGB")) {
                ms.sizeC = 3;
            }
        }
        ms.dimensionOrder = "XYCZT";
        if (ms.sizeZ == 0)
            ms.sizeZ = 1;
        if (ms.sizeC == 0)
            ms.sizeC = 1;
        if (ms.sizeT == 0)
            ms.sizeT = 1;
        xmlFiles[i] = topDirectory + File.separator + parseFilename(row2, filename, path);
        tiffFiles[i] = topDirectory + File.separator + parseFilename(row3, filename, path);
        parser[i] = new TiffParser(tiffFiles[i]);
        parser[i].setDoCaching(false);
        ifds[i] = parser[i].getIFDs();
        for (IFD ifd : ifds[i]) {
            parser[i].fillInIFD(ifd);
        }
        // get core metadata from TIFF file
        IFD ifd = ifds[i].get(0);
        PhotoInterp photo = ifd.getPhotometricInterpretation();
        int samples = ifd.getSamplesPerPixel();
        ms.sizeX = (int) ifd.getImageWidth();
        ms.sizeY = (int) ifd.getImageLength();
        ms.rgb = samples > 1 || photo == PhotoInterp.RGB;
        ms.pixelType = ifd.getPixelType();
        ms.littleEndian = ifd.isLittleEndian();
        ms.indexed = photo == PhotoInterp.RGB_PALETTE && ifd.containsKey(IFD.COLOR_MAP);
        ms.imageCount = ifds[i].size();
        if (ms.sizeZ * ms.sizeT * (ms.rgb ? 1 : ms.sizeC) != ms.imageCount) {
            ms.sizeT = ms.imageCount / (ms.rgb ? 1 : ms.sizeC);
            ms.sizeZ = 1;
        }
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    for (int i = 0; i < seriesCount; i++) {
        String[] row = rows.get(seriesIndexes.get(i));
        // populate Image data
        MetadataTools.setDefaultCreationDate(store, mtb, i);
        store.setImageName(row[imageName].trim(), i);
        if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
            // populate Dimensions data
            // calculate physical X and Y sizes
            double realWidth = Double.parseDouble(row[calibratedWidth]);
            double realHeight = Double.parseDouble(row[calibratedHeight]);
            String units = row[calibrationUnit];
            CoreMetadata ms = core.get(i);
            double px = realWidth / ms.sizeX;
            double py = realHeight / ms.sizeY;
            Length physicalSizeX = FormatTools.getPhysicalSizeX(px, units);
            Length physicalSizeY = FormatTools.getPhysicalSizeY(py, units);
            if (physicalSizeX != null) {
                store.setPixelsPhysicalSizeX(physicalSizeX, i);
            }
            if (physicalSizeY != null) {
                store.setPixelsPhysicalSizeY(physicalSizeY, i);
            }
        }
    }
    setSeries(0);
}
Also used : MDBService(loci.formats.services.MDBService) ServiceFactory(loci.common.services.ServiceFactory) IFD(loci.formats.tiff.IFD) ArrayList(java.util.ArrayList) PhotoInterp(loci.formats.tiff.PhotoInterp) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) TiffParser(loci.formats.tiff.TiffParser) Location(loci.common.Location)

Example 63 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class Schema2011_06_File_Upgrade_Test method setUp.

@BeforeClass
public void setUp() throws Exception {
    InputStream source = this.getClass().getResourceAsStream(ref.FILE_LOCATION);
    byte[] b = new byte[source.available()];
    source.read(b);
    source.close();
    String xml = new String(b);
    ServiceFactory sf = new ServiceFactory();
    OMEXMLService service = sf.getInstance(OMEXMLService.class);
    ome = (OME) service.createOMEXMLRoot(xml);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) InputStream(java.io.InputStream) OMEXMLService(loci.formats.services.OMEXMLService) BeforeClass(org.testng.annotations.BeforeClass)

Example 64 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class OMEXMLServiceTest method setUp.

@BeforeMethod
public void setUp() throws DependencyException {
    ServiceFactory sf = new ServiceFactory();
    service = sf.getInstance(OMEXMLService.class);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) OMEXMLService(loci.formats.services.OMEXMLService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 65 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class OMETiffWriterUnicodeTest method setUp.

@BeforeClass
public void setUp() throws Exception {
    target = File.createTempFile("OMETiffWriterUnicodeTest", ".ome.tiff");
    ServiceFactory sf = new ServiceFactory();
    OMEXMLService service = sf.getInstance(OMEXMLService.class);
    ms = service.createOMEXMLMetadata();
    ms.setImageID("Image:1", 0);
    ms.setPixelsID("Pixels:1", 0);
    ms.setPixelsDimensionOrder(DimensionOrder.XYZCT, 0);
    ms.setPixelsSizeX(new PositiveInteger(SIZE_X), 0);
    ms.setPixelsSizeY(new PositiveInteger(SIZE_Y), 0);
    ms.setPixelsSizeZ(new PositiveInteger(SIZE_Z), 0);
    ms.setPixelsSizeC(new PositiveInteger(SIZE_C), 0);
    ms.setPixelsSizeT(new PositiveInteger(SIZE_T), 0);
    ms.setPixelsPhysicalSizeX(new Length(10, UNITS.MICROMETER), 0);
    ms.setPixelsPhysicalSizeX(new Length(10, UNITS.MICROMETER), 0);
    ms.setPixelsPhysicalSizeX(new Length(10, UNITS.MICROMETER), 0);
    ms.setPixelsType(PixelType.UINT8, 0);
    ms.setPixelsBinDataBigEndian(true, 0, 0);
    ms.setChannelID("Channel:1", 0, 0);
    ms.setChannelSamplesPerPixel(new PositiveInteger(1), 0, 0);
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) ServiceFactory(loci.common.services.ServiceFactory) Length(ome.units.quantity.Length) OMEXMLService(loci.formats.services.OMEXMLService) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

ServiceFactory (loci.common.services.ServiceFactory)94 OMEXMLService (loci.formats.services.OMEXMLService)73 DependencyException (loci.common.services.DependencyException)49 FormatException (loci.formats.FormatException)38 IMetadata (loci.formats.meta.IMetadata)35 ServiceException (loci.common.services.ServiceException)30 ImageReader (loci.formats.ImageReader)23 MissingLibraryException (loci.formats.MissingLibraryException)15 BeforeMethod (org.testng.annotations.BeforeMethod)15 MetadataStore (loci.formats.meta.MetadataStore)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)14 IOException (java.io.IOException)13 CoreMetadata (loci.formats.CoreMetadata)13 Length (ome.units.quantity.Length)12 Location (loci.common.Location)11 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)10 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)8 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)8 BeforeClass (org.testng.annotations.BeforeClass)8