Search in sources :

Example 81 with MetadataStore

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

the class PCXReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    LOGGER.info("Reading file header");
    CoreMetadata m = core.get(0);
    m.littleEndian = true;
    in.order(isLittleEndian());
    in.seek(1);
    int version = in.read();
    in.skipBytes(1);
    int bitsPerPixel = in.read();
    int xMin = in.readShort();
    int yMin = in.readShort();
    int xMax = in.readShort();
    int yMax = in.readShort();
    m.sizeX = xMax - xMin;
    m.sizeY = yMax - yMin;
    in.skipBytes(version == 5 ? 53 : 51);
    nColorPlanes = in.read();
    bytesPerLine = in.readShort();
    int paletteType = in.readShort();
    offset = in.getFilePointer() + 58;
    if (version == 5 && nColorPlanes == 1) {
        in.seek(in.length() - 768);
        lut = new byte[3][256];
        for (int i = 0; i < lut[0].length; i++) {
            for (int j = 0; j < lut.length; j++) {
                lut[j][i] = in.readByte();
            }
        }
        m.indexed = true;
    }
    addGlobalMeta("Palette type", paletteType);
    m.sizeZ = 1;
    m.sizeT = 1;
    m.sizeC = nColorPlanes;
    m.rgb = nColorPlanes > 1;
    m.imageCount = 1;
    m.pixelType = FormatTools.UINT8;
    m.dimensionOrder = "XYCZT";
    m.interleaved = false;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata)

Example 82 with MetadataStore

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

the class TextReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    // read file into memory
    LOGGER.info("Reading file");
    List<String> lines = readFile(id);
    // parse file header
    LOGGER.info("Parsing file header");
    final int headerRows = parseFileHeader(lines);
    LOGGER.info("Creating images");
    // allocate memory for image data
    // no Z or T for now
    final int sizeZ = 1, sizeT = 1;
    final int sizeC = channels.length;
    final int imageCount = sizeZ * sizeC * sizeT;
    final int planeSize = sizeX * sizeY;
    data = new float[imageCount][planeSize];
    // flag all values as missing by default
    for (int i = 0; i < imageCount; i++) Arrays.fill(data[i], Float.NaN);
    // read data into float array
    parseTableData(lines, headerRows);
    LOGGER.info("Populating metadata");
    // populate core metadata
    populateCoreMetadata(sizeX, sizeY, sizeZ, sizeC, sizeT);
    // populate OME metadata
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore)

Example 83 with MetadataStore

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

the class PGMReader method initFile.

// -- Internal FormatReader API 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);
    // Plain     Raw
    // B&W            P1      P4
    // Grayscale      P2      P5
    // RGB            P3      P6
    final String magic = in.readString(2);
    rawBits = magic.equals("P4") || magic.equals("P5") || magic.equals("P6");
    m.sizeC = (magic.equals("P3") || magic.equals("P6")) ? 3 : 1;
    final boolean isBlackAndWhite = magic.equals("P1") || magic.equals("P4");
    StreamTokenizer st = new StreamTokenizer(in);
    st.commentChar('#');
    int token;
    token = st.nextToken();
    m.sizeX = (int) st.nval;
    token = st.nextToken();
    m.sizeY = (int) st.nval;
    if (!isBlackAndWhite) {
        token = st.nextToken();
        final int max = (int) st.nval;
        if (max > 255)
            m.pixelType = FormatTools.UINT16;
        else
            m.pixelType = FormatTools.UINT8;
    }
    // After reading the 2 or 3 header entries, it is possible that there are
    // multiple comments before the actual pixel data.
    Byte c;
    do {
        c = in.readByte();
        if (c == 35) {
            // Found a # for comment
            do {
                c = in.readByte();
            } while (// comment only ends after CR or LF
            c != 13 && c != 10);
        }
    } while (c == 32 || c == 13 || c == 10 || c == 9 || c == 11 || c == 12);
    // space      CR         LF         TAB       VT         FF
    offset = in.getFilePointer() - 1;
    addGlobalMeta("Black and white", isBlackAndWhite);
    m.rgb = getSizeC() == 3;
    m.dimensionOrder = "XYCZT";
    m.littleEndian = true;
    m.interleaved = true;
    m.sizeZ = 1;
    m.sizeT = 1;
    m.indexed = false;
    m.falseColor = false;
    // This may not be true. Multiple images were added to the netpbm
    // formats in July 2000.
    m.imageCount = 1;
    // 1. the comments are being ignored;
    // 2. we may be missing some images (allowed in raw)
    m.metadataComplete = false;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) StreamTokenizer(java.io.StreamTokenizer)

Example 84 with MetadataStore

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

the class ImageInfo method readCoreMetadata.

public void readCoreMetadata() throws FormatException, IOException {
    // skip core metadata printout
    if (!doCore)
        return;
    // read basic metadata
    LOGGER.info("");
    LOGGER.info("Reading core metadata");
    LOGGER.info("{} = {}", stitch ? "File pattern" : "filename", stitch ? id : reader.getCurrentFile());
    if (map != null)
        LOGGER.info("Mapped filename = {}", map);
    if (usedFiles) {
        String[] used = reader.getUsedFiles();
        boolean usedValid = used != null && used.length > 0;
        if (usedValid) {
            for (int u = 0; u < used.length; u++) {
                if (used[u] == null) {
                    usedValid = false;
                    break;
                }
            }
        }
        if (!usedValid) {
            LOGGER.warn("************ invalid used files list ************");
        }
        if (used == null) {
            LOGGER.info("Used files = null");
        } else if (used.length == 0) {
            LOGGER.info("Used files = []");
        } else if (used.length > 1) {
            LOGGER.info("Used files:");
            for (int u = 0; u < used.length; u++) LOGGER.info("\t{}", used[u]);
        } else if (!id.equals(used[0])) {
            LOGGER.info("Used files = [{}]", used[0]);
        }
    }
    int seriesCount = reader.getSeriesCount();
    LOGGER.info("Series count = {}", seriesCount);
    MetadataStore ms = reader.getMetadataStore();
    MetadataRetrieve mr = ms instanceof MetadataRetrieve ? (MetadataRetrieve) ms : null;
    for (int j = 0; j < seriesCount; j++) {
        reader.setSeries(j);
        // read basic metadata for series #i
        int imageCount = reader.getImageCount();
        int resolutions = reader.getResolutionCount();
        boolean rgb = reader.isRGB();
        int sizeX = reader.getSizeX();
        int sizeY = reader.getSizeY();
        int sizeZ = reader.getSizeZ();
        int sizeC = reader.getSizeC();
        int sizeT = reader.getSizeT();
        int pixelType = reader.getPixelType();
        int validBits = reader.getBitsPerPixel();
        int effSizeC = reader.getEffectiveSizeC();
        int rgbChanCount = reader.getRGBChannelCount();
        boolean indexed = reader.isIndexed();
        boolean falseColor = reader.isFalseColor();
        byte[][] table8 = reader.get8BitLookupTable();
        short[][] table16 = reader.get16BitLookupTable();
        Modulo moduloZ = reader.getModuloZ();
        Modulo moduloC = reader.getModuloC();
        Modulo moduloT = reader.getModuloT();
        int thumbSizeX = reader.getThumbSizeX();
        int thumbSizeY = reader.getThumbSizeY();
        int tileSizeX = reader.getOptimalTileWidth();
        int tileSizeY = reader.getOptimalTileHeight();
        boolean little = reader.isLittleEndian();
        String dimOrder = reader.getDimensionOrder();
        boolean orderCertain = reader.isOrderCertain();
        boolean thumbnail = reader.isThumbnailSeries();
        boolean interleaved = reader.isInterleaved();
        boolean metadataComplete = reader.isMetadataComplete();
        // output basic metadata for series #i
        String seriesName = mr == null ? null : mr.getImageName(j);
        LOGGER.info("Series #{}{}{}:", new Object[] { j, seriesName == null ? " " : " -- ", seriesName == null ? "" : seriesName });
        if (flat == false && resolutions > 1) {
            LOGGER.info("\tResolutions = {}", resolutions);
            for (int i = 0; i < resolutions; i++) {
                reader.setResolution(i);
                LOGGER.info("\t\tsizeX[{}] = {}", i, reader.getSizeX());
            }
            reader.setResolution(0);
        }
        LOGGER.info("\tImage count = {}", imageCount);
        LOGGER.info("\tRGB = {} ({}) {}", new Object[] { rgb, rgbChanCount, merge ? "(merged)" : separate ? "(separated)" : "" });
        if (rgb != (rgbChanCount != 1)) {
            LOGGER.warn("\t************ RGB mismatch ************");
        }
        LOGGER.info("\tInterleaved = {}", interleaved);
        StringBuilder sb = new StringBuilder();
        sb.append("\tIndexed = ");
        sb.append(indexed);
        sb.append(" (");
        sb.append(!falseColor);
        sb.append(" color");
        if (table8 != null) {
            sb.append(", 8-bit LUT: ");
            sb.append(table8.length);
            sb.append(" x ");
            sb.append(table8[0] == null ? "null" : "" + table8[0].length);
        }
        if (table16 != null) {
            sb.append(", 16-bit LUT: ");
            sb.append(table16.length);
            sb.append(" x ");
            sb.append(table16[0] == null ? "null" : "" + table16[0].length);
        }
        sb.append(")");
        LOGGER.info(sb.toString());
        if (table8 != null && table16 != null) {
            LOGGER.warn("\t************ multiple LUTs ************");
        }
        LOGGER.info("\tWidth = {}", sizeX);
        LOGGER.info("\tHeight = {}", sizeY);
        printDimension("SizeZ", sizeZ, sizeZ, moduloZ);
        printDimension("SizeT", sizeT, sizeT, moduloT);
        printDimension("SizeC", sizeC, effSizeC, moduloC);
        if (imageCount != sizeZ * effSizeC * sizeT) {
            LOGGER.info("\t************ ZCT mismatch ************");
        }
        LOGGER.info("\tTile size = {} x {}", tileSizeX, tileSizeY);
        LOGGER.info("\tThumbnail size = {} x {}", thumbSizeX, thumbSizeY);
        LOGGER.info("\tEndianness = {}", little ? "intel (little)" : "motorola (big)");
        LOGGER.info("\tDimension order = {} ({})", dimOrder, orderCertain ? "certain" : "uncertain");
        LOGGER.info("\tPixel type = {}", FormatTools.getPixelTypeString(pixelType));
        LOGGER.info("\tValid bits per pixel = {}", validBits);
        LOGGER.info("\tMetadata complete = {}", metadataComplete);
        LOGGER.info("\tThumbnail series = {}", thumbnail);
        if (doMeta) {
            LOGGER.info("\t-----");
            int[] indices;
            if (imageCount > 6) {
                int q = imageCount / 2;
                indices = new int[] { 0, q - 2, q - 1, q, q + 1, q + 2, imageCount - 1 };
            } else if (imageCount > 2) {
                indices = new int[] { 0, imageCount / 2, imageCount - 1 };
            } else if (imageCount > 1)
                indices = new int[] { 0, 1 };
            else
                indices = new int[] { 0 };
            int[][] zct = new int[indices.length][];
            int[] indices2 = new int[indices.length];
            sb.setLength(0);
            for (int i = 0; i < indices.length; i++) {
                zct[i] = reader.getZCTCoords(indices[i]);
                indices2[i] = reader.getIndex(zct[i][0], zct[i][1], zct[i][2]);
                sb.append("\tPlane #");
                sb.append(indices[i]);
                sb.append(" <=> Z ");
                sb.append(zct[i][0]);
                sb.append(", C ");
                sb.append(zct[i][1]);
                sb.append(", T ");
                sb.append(zct[i][2]);
                if (indices[i] != indices2[i]) {
                    sb.append(" [mismatch: ");
                    sb.append(indices2[i]);
                    sb.append("]");
                    sb.append(NEWLINE);
                } else
                    sb.append(NEWLINE);
            }
            LOGGER.info(sb.toString());
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Modulo(loci.formats.Modulo) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 85 with MetadataStore

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

the class FormatReader method setId.

/**
 * Initializes a reader from the input file name.
 *
 * Calls {@link #initFile(String id)} to initializes the input file, reads
 * all of the metadata and sets the reader up for reading planes.
 * The performance of this method depends on the format and can be up to
 * several minutes for large file sets.
 *
 *  @param id a {@link String} specifying the path to the file
 */
@Override
public void setId(String id) throws FormatException, IOException {
    LOGGER.debug("{} initializing {}", this.getClass().getSimpleName(), id);
    if (currentId == null || !new Location(id).getAbsolutePath().equals(new Location(currentId).getAbsolutePath())) {
        initFile(id);
        MetadataStore store = getMetadataStore();
        if (saveOriginalMetadata) {
            if (store instanceof OMEXMLMetadata) {
                setupService();
                Hashtable<String, Object> allMetadata = new Hashtable<String, Object>();
                allMetadata.putAll(metadata);
                for (int series = 0; series < getSeriesCount(); series++) {
                    String name = "Series " + series;
                    try {
                        String realName = ((IMetadata) store).getImageName(series);
                        if (realName != null && realName.trim().length() != 0) {
                            name = realName;
                        }
                    } catch (Exception e) {
                    }
                    setSeries(series);
                    MetadataTools.merge(getSeriesMetadata(), allMetadata, name + " ");
                }
                setSeries(0);
                service.populateOriginalMetadata((OMEXMLMetadata) store, allMetadata);
            }
        }
        if (store instanceof OMEXMLMetadata) {
            ((OMEXMLMetadata) store).resolveReferences();
            setupService();
            if (getMetadataOptions().isValidate()) {
                try {
                    String omexml = service.getOMEXML((MetadataRetrieve) store);
                    service.validateOMEXML(omexml);
                } catch (ServiceException | NullPointerException e) {
                    LOGGER.warn("OMEXMLService unable to create OME-XML metadata object.", e);
                }
            }
            for (int series = 0; series < getSeriesCount(); series++) {
                setSeries(series);
                if (getModuloZ().length() > 1 || getModuloC().length() > 1 || getModuloT().length() > 1) {
                    service.addModuloAlong((OMEXMLMetadata) store, core.get(series), series);
                }
            }
            setSeries(0);
        }
    }
}
Also used : Hashtable(java.util.Hashtable) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) IOException(java.io.IOException) MetadataStore(loci.formats.meta.MetadataStore) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) Location(loci.common.Location)

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