Search in sources :

Example 91 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class LegacyQTReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    LOGGER.info("Checking for QuickTime Java");
    if (tools == null) {
        tools = new LegacyQTTools();
        r = tools.getUniverse();
    }
    tools.checkQTLibrary();
    super.initFile(id);
    LOGGER.info("Reading movie dimensions");
    try {
        r.exec("QTSession.open()");
        // open movie file
        Location file = new Location(id);
        r.setVar("path", file.getAbsolutePath());
        r.exec("qtf = new QTFile(path)");
        r.exec("openMovieFile = OpenMovieFile.asRead(qtf)");
        r.exec("m = Movie.fromFile(openMovieFile)");
        int numTracks = ((Integer) r.exec("m.getTrackCount()")).intValue();
        int trackMostLikely = 0;
        int trackNum = 0;
        while (++trackNum <= numTracks && trackMostLikely == 0) {
            r.setVar("trackNum", trackNum);
            r.exec("imageTrack = m.getTrack(trackNum)");
            r.exec("d = imageTrack.getSize()");
            Integer w = (Integer) r.exec("d.getWidth()");
            if (w.intValue() > 0)
                trackMostLikely = trackNum;
        }
        r.setVar("trackMostLikely", trackMostLikely);
        r.exec("imageTrack = m.getTrack(trackMostLikely)");
        r.exec("d = imageTrack.getSize()");
        Integer w = (Integer) r.exec("d.getWidth()");
        Integer h = (Integer) r.exec("d.getHeight()");
        r.exec("moviePlayer = new MoviePlayer(m)");
        r.setVar("dim", new Dimension(w.intValue(), h.intValue()));
        ImageProducer qtip = (ImageProducer) r.exec("qtip = new QTImageProducer(moviePlayer, dim)");
        image = Toolkit.getDefaultToolkit().createImage(qtip);
        r.setVar("zero", 0);
        r.setVar("one", 1f);
        r.exec("timeInfo = new TimeInfo(zero, zero)");
        r.exec("moviePlayer.setTime(zero)");
        Vector<Integer> v = new Vector<Integer>();
        int time = 0;
        Integer q = new Integer(time);
        do {
            v.add(q);
            r.exec("timeInfo = imageTrack.getNextInterestingTime(" + "StdQTConstants.nextTimeMediaSample, timeInfo.time, one)");
            q = (Integer) r.getVar("timeInfo.time");
            time = q.intValue();
        } while (time >= 0);
        CoreMetadata m = core.get(0);
        m.imageCount = v.size();
        times = new int[getImageCount()];
        for (int i = 0; i < times.length; i++) {
            q = v.elementAt(i);
            times[i] = q.intValue();
        }
        LOGGER.info("Populating metadata");
        BufferedImage img = AWTImageTools.makeBuffered(image);
        m.sizeX = img.getWidth();
        m.sizeY = img.getHeight();
        m.sizeZ = 1;
        m.sizeC = img.getRaster().getNumBands();
        m.sizeT = getImageCount();
        m.pixelType = AWTImageTools.getPixelType(img);
        m.dimensionOrder = "XYCTZ";
        m.rgb = true;
        m.interleaved = false;
        m.littleEndian = false;
        m.indexed = false;
        m.falseColor = false;
        MetadataStore store = makeFilterMetadata();
        MetadataTools.populatePixels(store, this);
    } catch (ReflectException e) {
        throw new FormatException("Open movie failed", e);
    }
}
Also used : Dimension(java.awt.Dimension) CoreMetadata(loci.formats.CoreMetadata) ReflectException(loci.common.ReflectException) BufferedImage(java.awt.image.BufferedImage) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) LegacyQTTools(loci.formats.gui.LegacyQTTools) ImageProducer(java.awt.image.ImageProducer) Vector(java.util.Vector) Location(loci.common.Location)

Example 92 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class ChannelFiller method setId.

/* @see IFormatHandler#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    super.setId(id);
    lutLength = getLookupTableComponentCount();
    MetadataStore store = getMetadataStore();
    MetadataTools.populatePixelsOnly(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore)

Example 93 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class APNGReader method initFile.

// -- Internal FormatReader methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    CoreMetadata m = core.get(0);
    // check that this is a valid PNG file
    byte[] signature = new byte[8];
    in.read(signature);
    if (signature[0] != (byte) 0x89 || signature[1] != 0x50 || signature[2] != 0x4e || signature[3] != 0x47 || signature[4] != 0x0d || signature[5] != 0x0a || signature[6] != 0x1a || signature[7] != 0x0a) {
        throw new FormatException("Invalid PNG signature.");
    }
    // read data chunks - each chunk consists of the following:
    // 1) 32 bit length
    // 2) 4 char type
    // 3) 'length' bytes of data
    // 4) 32 bit CRC
    blocks = new Vector<PNGBlock>();
    frameCoordinates = new Vector<int[]>();
    while (in.getFilePointer() < in.length()) {
        int length = in.readInt();
        String type = in.readString(4);
        PNGBlock block = new PNGBlock();
        block.length = length;
        block.type = type;
        block.offset = in.getFilePointer();
        blocks.add(block);
        if (type.equals("acTL")) {
            // APNG-specific chunk
            m.imageCount = in.readInt();
            int loop = in.readInt();
            addGlobalMeta("Loop count", loop);
        } else if (type.equals("fcTL")) {
            in.skipBytes(4);
            int w = in.readInt();
            int h = in.readInt();
            int x = in.readInt();
            int y = in.readInt();
            frameCoordinates.add(new int[] { x, y, w, h });
            in.skipBytes(length - 20);
        } else if (type.equals("IDAT")) {
            idatCount++;
        } else if (type.equals("PLTE")) {
            // lookup table
            m.indexed = true;
            lut = new byte[3][256];
            for (int i = 0; i < length / 3; i++) {
                for (int c = 0; c < 3; c++) {
                    lut[c][i] = in.readByte();
                }
            }
        } else if (type.equals("IHDR")) {
            m.sizeX = in.readInt();
            m.sizeY = in.readInt();
            m.bitsPerPixel = in.read();
            int colorType = in.read();
            compression = in.read();
            int filter = in.read();
            interlace = in.read();
            if (filter != 0) {
                throw new FormatException("Invalid filter mode: " + filter);
            }
            switch(colorType) {
                case GRAYSCALE:
                case INDEXED:
                    m.sizeC = 1;
                    break;
                case GRAY_ALPHA:
                    m.sizeC = 2;
                    break;
                case TRUE_COLOR:
                    m.sizeC = 3;
                    break;
                case TRUE_ALPHA:
                    m.sizeC = 4;
                    break;
            }
            m.pixelType = getBitsPerPixel() <= 8 ? FormatTools.UINT8 : FormatTools.UINT16;
            m.rgb = getSizeC() > 1;
        } else if (type.equals("IEND")) {
            break;
        }
        in.seek(block.offset + length);
        if (in.getFilePointer() < in.length() - 4) {
            // skip the CRC
            in.skipBytes(4);
        }
    }
    if (m.imageCount == 0)
        m.imageCount = 1;
    m.sizeZ = 1;
    m.sizeT = getImageCount();
    m.dimensionOrder = "XYCTZ";
    m.interleaved = isRGB();
    m.falseColor = false;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 94 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class BaseTiffReader method initMetadataStore.

/**
 * Populates the metadata store using the data parsed in
 * {@link #initStandardMetadata()} along with some further parsing done in
 * the method itself.
 *
 * All calls to the active <code>MetadataStore</code> should be made in this
 * method and <b>only</b> in this method. This is especially important for
 * sub-classes that override the getters for pixel set array size, etc.
 */
protected void initMetadataStore() throws FormatException {
    LOGGER.info("Populating OME metadata");
    // the metadata store we're working with
    MetadataStore store = makeFilterMetadata();
    IFD firstIFD = ifds.get(0);
    IFD exif = null;
    if (ifds.get(0).containsKey(IFD.EXIF)) {
        try {
            IFDList exifIFDs = tiffParser.getExifIFDs();
            if (exifIFDs.size() > 0) {
                exif = exifIFDs.get(0);
            }
            tiffParser.fillInIFD(exif);
        } catch (IOException e) {
            LOGGER.debug("Could not read EXIF IFDs", e);
        }
    }
    MetadataTools.populatePixels(store, this, exif != null);
    // format the creation date to ISO 8601
    String creationDate = getImageCreationDate();
    String date = DateTools.formatDate(creationDate, DATE_FORMATS, ".");
    if (creationDate != null && date == null) {
        LOGGER.warn("unknown creation date format: {}", creationDate);
    }
    creationDate = date;
    if (creationDate != null) {
        store.setImageAcquisitionDate(new Timestamp(creationDate), 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        // populate Experimenter
        String artist = firstIFD.getIFDTextValue(IFD.ARTIST);
        if (artist != null) {
            String firstName = null, lastName = null;
            int ndx = artist.indexOf(' ');
            if (ndx < 0)
                lastName = artist;
            else {
                firstName = artist.substring(0, ndx);
                lastName = artist.substring(ndx + 1);
            }
            String email = firstIFD.getIFDStringValue(IFD.HOST_COMPUTER);
            store.setExperimenterFirstName(firstName, 0);
            store.setExperimenterLastName(lastName, 0);
            store.setExperimenterEmail(email, 0);
            store.setExperimenterID(MetadataTools.createLSID("Experimenter", 0), 0);
        }
        store.setImageDescription(firstIFD.getComment(), 0);
        // set the X and Y pixel dimensions
        double pixX = firstIFD.getXResolution();
        double pixY = firstIFD.getYResolution();
        String unit = getResolutionUnitFromComment(firstIFD);
        Length sizeX = FormatTools.getPhysicalSizeX(pixX, unit);
        Length sizeY = FormatTools.getPhysicalSizeY(pixY, unit);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
        store.setPixelsPhysicalSizeZ(null, 0);
        if (exif != null) {
            if (exif.containsKey(IFD.EXPOSURE_TIME)) {
                Object exp = exif.get(IFD.EXPOSURE_TIME);
                if (exp instanceof TiffRational) {
                    Time exposure = new Time(((TiffRational) exp).doubleValue(), UNITS.SECOND);
                    for (int i = 0; i < getImageCount(); i++) {
                        store.setPlaneExposureTime(exposure, 0, i);
                    }
                }
            }
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) IFD(loci.formats.tiff.IFD) IFDList(loci.formats.tiff.IFDList) TiffRational(loci.formats.tiff.TiffRational) Time(ome.units.quantity.Time) IOException(java.io.IOException) Timestamp(ome.xml.model.primitives.Timestamp)

Example 95 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class DimensionSwapper method swapDimensions.

// -- DimensionSwapper API methods --
/**
 * Sets the input dimension order according to the given string (e.g.,
 * "XYZCT"). This string indicates the planar rasterization order from the
 * source, overriding the detected order. It may result in the dimensional
 * axis sizes changing.
 *
 * If the given order is identical to the file's native order, then
 * nothing happens. Note that this method will throw an exception if X and Y
 * do not appear in positions 0 and 1 (although X and Y can be reversed).
 */
public void swapDimensions(String order) {
    FormatTools.assertId(getCurrentFile(), true, 2);
    if (order == null)
        throw new IllegalArgumentException("order is null");
    String oldOrder = getInputOrder();
    if (order.equals(oldOrder))
        return;
    if (order.length() != 5) {
        throw new IllegalArgumentException("order is unexpected length (" + order.length() + ")");
    }
    int newX = order.indexOf('X');
    int newY = order.indexOf('Y');
    int newZ = order.indexOf('Z');
    int newC = order.indexOf('C');
    int newT = order.indexOf('T');
    if (newX < 0)
        throw new IllegalArgumentException("X does not appear");
    if (newY < 0)
        throw new IllegalArgumentException("Y does not appear");
    if (newZ < 0)
        throw new IllegalArgumentException("Z does not appear");
    if (newC < 0)
        throw new IllegalArgumentException("C does not appear");
    if (newT < 0)
        throw new IllegalArgumentException("T does not appear");
    if (newX > 1) {
        throw new IllegalArgumentException("X in unexpected position (" + newX + ")");
    }
    if (newY > 1) {
        throw new IllegalArgumentException("Y in unexpected position (" + newY + ")");
    }
    int[] dims = new int[5];
    int oldX = oldOrder.indexOf('X');
    int oldY = oldOrder.indexOf('Y');
    int oldZ = oldOrder.indexOf('Z');
    int oldC = oldOrder.indexOf('C');
    int oldT = oldOrder.indexOf('T');
    if (oldC != newC && reader.getRGBChannelCount() > 1) {
        throw new IllegalArgumentException("Cannot swap C dimension when RGB channel count > 1");
    }
    dims[oldX] = getSizeX();
    dims[oldY] = getSizeY();
    dims[oldZ] = getSizeZ();
    dims[oldC] = getSizeC();
    dims[oldT] = getSizeT();
    Modulo[] moduli = new Modulo[3];
    moduli[oldZ - 2] = getModuloZ();
    moduli[oldC - 2] = getModuloC();
    moduli[oldT - 2] = getModuloT();
    SwappableMetadata ms = (SwappableMetadata) core.get(getCoreIndex());
    ms.sizeX = dims[newX];
    ms.sizeY = dims[newY];
    ms.sizeZ = dims[newZ];
    ms.sizeC = dims[newC];
    ms.sizeT = dims[newT];
    ms.moduloZ = moduli[newZ - 2];
    ms.moduloC = moduli[newC - 2];
    ms.moduloT = moduli[newT - 2];
    ms.inputOrder = order;
    MetadataStore store = getMetadataStore();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore)

Aggregations

MetadataStore (loci.formats.meta.MetadataStore)180 CoreMetadata (loci.formats.CoreMetadata)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)97 Length (ome.units.quantity.Length)82 FormatException (loci.formats.FormatException)66 Timestamp (ome.xml.model.primitives.Timestamp)54 Location (loci.common.Location)51 Time (ome.units.quantity.Time)41 ArrayList (java.util.ArrayList)34 IFD (loci.formats.tiff.IFD)21 DependencyException (loci.common.services.DependencyException)16 IOException (java.io.IOException)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 ServiceFactory (loci.common.services.ServiceFactory)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IFDList (loci.formats.tiff.IFDList)12 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)11 ServiceException (loci.common.services.ServiceException)10 Map (java.util.Map)9