Search in sources :

Example 1 with IRandomAccess

use of loci.common.IRandomAccess in project bioformats by openmicroscopy.

the class VolocityReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    if (!checkSuffix(id, "mvd2")) {
        Location file = new Location(id).getAbsoluteFile();
        Location parent = file.getParentFile().getParentFile();
        String[] files = parent.list(true);
        for (String f : files) {
            if (checkSuffix(f, "mvd2")) {
                id = new Location(parent, f).getAbsolutePath();
                break;
            }
        }
    }
    super.initFile(id);
    stacks = new ArrayList<Stack>();
    extraFiles = new ArrayList<String>();
    Location file = new Location(id).getAbsoluteFile();
    extraFiles.add(file.getAbsolutePath());
    Location parentDir = file.getParentFile();
    dir = new Location(parentDir, DATA_DIR);
    if (dir.exists()) {
        String[] files = dir.list(true);
        for (String f : files) {
            if (!checkSuffix(f, "aisf") && !checkSuffix(f, "atsf")) {
                extraFiles.add(new Location(dir, f).getAbsolutePath());
            }
        }
    }
    try {
        ServiceFactory factory = new ServiceFactory();
        MetakitService reader = factory.getInstance(MetakitService.class);
        reader.initialize(id);
        sampleTable = reader.getTableData(1);
        stringTable = reader.getTableData(2);
        reader.close();
    } catch (DependencyException e) {
        throw new MissingLibraryException("Could not find Metakit library", e);
    }
    ArrayList<String> stackNames = new ArrayList<String>();
    ArrayList<Integer> parentIDs = new ArrayList<Integer>();
    for (int i = 0; i < sampleTable.length; i++) {
        Integer stringID = (Integer) sampleTable[i][11];
        String name = getString(stringID);
        int channelIndex = getChildIndex((Integer) sampleTable[i][0], "Channels");
        if (i > 0 && (Integer) sampleTable[i][2] == 1 && (channelIndex >= 0 || (sampleTable[i][14] != null && !sampleTable[i][14].equals(0)) || ((byte[]) sampleTable[i][13]).length > 21)) {
            if (channelIndex < 0) {
                RandomAccessInputStream s = getStream(i);
                s.seek(0);
                if (s.read() != 'I') {
                    s.order(false);
                }
                s.seek(22);
                int x = s.readInt();
                int y = s.readInt();
                int z = s.readInt();
                if (x * y * z > 0 && x * y * z < (s.length() * 3)) {
                    stackNames.add(name);
                    parentIDs.add((Integer) sampleTable[i][0]);
                }
                s.close();
            } else {
                stackNames.add(name);
                parentIDs.add((Integer) sampleTable[i][0]);
            }
        }
    }
    for (int i = 0; i < parentIDs.size(); i++) {
        Stack stack = new Stack();
        stack.core = new CoreMetadata();
        Integer parent = parentIDs.get(i);
        int channelIndex = getChildIndex(parent, "Channels");
        if (channelIndex >= 0) {
            Integer[] channels = getAllChildren((Integer) sampleTable[channelIndex][0]);
            stack.core.sizeC = channels.length;
            stack.pixelsFiles = new String[stack.core.sizeC];
            stack.channelNames = new String[channels.length];
            for (int c = 0; c < channels.length; c++) {
                stack.channelNames[c] = getString((Integer) sampleTable[channels[c]][11]);
                RandomAccessInputStream data = getStream(channels[c]);
                if (data.length() > 22) {
                    data.seek(22);
                    int stackID = data.readInt();
                    Location f = new Location(dir, stackID + ".aisf");
                    if (!f.exists()) {
                        f = new Location(dir, DataTools.swap(stackID) + ".aisf");
                    }
                    stack.pixelsFiles[c] = f.getAbsolutePath();
                } else {
                    Integer child = getAllChildren((Integer) sampleTable[channels[c]][0])[0];
                    stack.pixelsFiles[c] = getFile((Integer) sampleTable[child][0], dir);
                }
                data.close();
            }
        } else {
            stack.pixelsFiles = new String[1];
            stack.pixelsFiles[0] = getFile(parent, dir);
            if (stack.pixelsFiles[0] == null || !new Location(stack.pixelsFiles[0]).exists()) {
                int row = -1;
                for (int r = 0; r < sampleTable.length; r++) {
                    if (sampleTable[r][0].equals(parent)) {
                        row = r;
                        break;
                    }
                }
                stack.pixelsFiles[0] = EMBEDDED_STREAM;
                IRandomAccess data = new ByteArrayHandle((byte[]) sampleTable[row][13]);
                Location.mapFile(stack.pixelsFiles[0], data);
            }
        }
        RandomAccessInputStream data = null;
        int timestampIndex = getChildIndex(parent, "Timepoint times stream");
        if (timestampIndex >= 0) {
            data = getStream(timestampIndex);
            data.seek(22);
            int timestampID = data.readInt();
            Location f = new Location(dir, timestampID + ".atsf");
            if (!f.exists()) {
                f = new Location(dir, DataTools.swap(timestampID) + ".atsf");
            }
            stack.timestampFile = f.getAbsolutePath();
            data.close();
        }
        int xIndex = getChildIndex(parent, "um/pixel (X)");
        if (xIndex >= 0) {
            data = getStream(xIndex);
            data.seek(SIGNATURE_SIZE);
            stack.physicalX = data.readDouble();
            data.close();
        }
        int yIndex = getChildIndex(parent, "um/pixel (Y)");
        if (yIndex >= 0) {
            data = getStream(yIndex);
            data.seek(SIGNATURE_SIZE);
            stack.physicalY = data.readDouble();
            data.close();
        }
        int zIndex = getChildIndex(parent, "um/pixel (Z)");
        if (zIndex >= 0) {
            data = getStream(zIndex);
            data.seek(SIGNATURE_SIZE);
            stack.physicalZ = data.readDouble();
            data.close();
        }
        timestampIndex = getChildIndex(parent, "TimepointTimes");
        if (timestampIndex >= 0) {
            data = getStream(timestampIndex);
            data.seek(SIGNATURE_SIZE);
            data.close();
        }
        int objectiveIndex = getChildIndex(parent, "Microscope Objective");
        if (objectiveIndex >= 0) {
            data = getStream(objectiveIndex);
            data.seek(SIGNATURE_SIZE);
            stack.magnification = data.readDouble();
            data.close();
        }
        int detectorIndex = getChildIndex(parent, "Camera/Detector");
        if (detectorIndex >= 0) {
            data = getStream(detectorIndex);
            data.seek(SIGNATURE_SIZE);
            int len = data.readInt();
            stack.detector = data.readString(len);
            data.close();
        }
        int descriptionIndex = getChildIndex(parent, "Experiment Description");
        if (descriptionIndex >= 0) {
            data = getStream(descriptionIndex);
            data.seek(SIGNATURE_SIZE);
            int len = data.readInt();
            stack.description = data.readString(len);
            data.close();
        }
        int xLocationIndex = getChildIndex(parent, "X Location");
        if (xLocationIndex >= 0) {
            data = getStream(xLocationIndex);
            data.seek(SIGNATURE_SIZE);
            stack.xLocation = data.readDouble();
            data.close();
        }
        int yLocationIndex = getChildIndex(parent, "Y Location");
        if (yLocationIndex >= 0) {
            data = getStream(yLocationIndex);
            data.seek(SIGNATURE_SIZE);
            stack.yLocation = data.readDouble();
            data.close();
        }
        int zLocationIndex = getChildIndex(parent, "Z Location");
        if (zLocationIndex >= 0) {
            data = getStream(zLocationIndex);
            data.seek(SIGNATURE_SIZE);
            stack.zLocation = data.readDouble();
            data.close();
        }
        stacks.add(stack);
    }
    for (int i = 0; i < stacks.size(); i++) {
        Stack stack = stacks.get(i);
        if (!new Location(stack.pixelsFiles[0]).exists()) {
            stacks.remove(i);
            i--;
            continue;
        }
        RandomAccessInputStream base = new RandomAccessInputStream(stack.pixelsFiles[0]);
        long baseLength = base.length();
        base.close();
        for (int q = 1; q < stack.pixelsFiles.length; q++) {
            if (!new Location(stack.pixelsFiles[q]).exists()) {
                continue;
            }
            base = new RandomAccessInputStream(stack.pixelsFiles[q]);
            long length = base.length();
            base.close();
            if (length > baseLength) {
                // split the stack
                Stack newStack = new Stack();
                newStack.timestampFile = stack.timestampFile;
                newStack.core = new CoreMetadata();
                newStack.physicalX = stack.physicalX;
                newStack.physicalY = stack.physicalY;
                newStack.physicalZ = stack.physicalZ;
                newStack.magnification = stack.magnification;
                newStack.detector = stack.detector;
                newStack.description = stack.description;
                newStack.xLocation = stack.xLocation;
                newStack.yLocation = stack.yLocation;
                newStack.zLocation = stack.zLocation;
                String[] pixels = stack.pixelsFiles;
                newStack.pixelsFiles = new String[pixels.length - q];
                System.arraycopy(pixels, q, newStack.pixelsFiles, 0, newStack.pixelsFiles.length);
                stack.pixelsFiles = new String[q];
                System.arraycopy(pixels, 0, stack.pixelsFiles, 0, q);
                String[] channels = stack.channelNames;
                newStack.channelNames = new String[channels.length - q];
                System.arraycopy(channels, q, newStack.channelNames, 0, newStack.channelNames.length);
                stack.channelNames = new String[q];
                System.arraycopy(channels, 0, stack.channelNames, 0, q);
                newStack.core.sizeC = newStack.channelNames.length;
                stack.core.sizeC = stack.channelNames.length;
                stacks.add(i + 1, newStack);
                stackNames.add(i + 1, stackNames.get(i));
            }
        }
    }
    int seriesCount = stacks.size();
    core.clear();
    for (int i = 0; i < seriesCount; i++) {
        Stack stack = stacks.get(i);
        CoreMetadata ms = stack.core;
        core.add(ms);
        setSeries(i);
        ms.littleEndian = true;
        if (stack.timestampFile != null) {
            RandomAccessInputStream s = new RandomAccessInputStream(stack.timestampFile);
            s.seek(0);
            if (s.read() != 'I') {
                ms.littleEndian = false;
            }
            s.seek(17);
            s.order(isLittleEndian());
            ms.sizeT = s.readInt();
            Double firstStamp = null;
            Double[] stamps = new Double[ms.sizeT];
            for (int t = 0; t < ms.sizeT; t++) {
                // timestamps are stored in microseconds
                double timestamp = s.readLong() / 1000000.0;
                if (firstStamp == null) {
                    firstStamp = timestamp;
                }
                stamps[t] = timestamp - firstStamp;
            }
            timestamps.add(stamps);
            s.close();
        } else {
            ms.sizeT = 1;
        }
        ms.rgb = false;
        ms.interleaved = true;
        ms.dimensionOrder = "XYCZT";
        RandomAccessInputStream s = new RandomAccessInputStream(stack.pixelsFiles[0]);
        s.order(isLittleEndian());
        if (checkSuffix(stack.pixelsFiles[0], "aisf")) {
            s.seek(18);
            stack.blockSize = s.readShort() * 256;
            s.skipBytes(5);
            int x = s.readInt();
            int y = s.readInt();
            int zStart = s.readInt();
            int w = s.readInt();
            int h = s.readInt();
            if (w - x < 0 || h - y < 0 || (w - x) * (h - y) < 0) {
                ms.littleEndian = !isLittleEndian();
                s.order(isLittleEndian());
                s.seek(s.getFilePointer() - 20);
                x = s.readInt();
                y = s.readInt();
                zStart = s.readInt();
                w = s.readInt();
                h = s.readInt();
            }
            ms.sizeX = w - x;
            ms.sizeY = h - y;
            ms.sizeZ = s.readInt() - zStart;
            ms.imageCount = getSizeZ() * getSizeC() * getSizeT();
            ms.pixelType = FormatTools.INT8;
            int planesPerFile = getSizeZ() * getSizeT();
            int planeSize = FormatTools.getPlaneSize(this);
            int bytesPerPlane = (int) ((s.length() - stack.blockSize) / planesPerFile);
            int bytesPerPixel = 0;
            while (bytesPerPlane >= planeSize) {
                bytesPerPixel++;
                bytesPerPlane -= planeSize;
            }
            if ((bytesPerPixel % 3) == 0) {
                ms.sizeC *= 3;
                ms.rgb = true;
                bytesPerPixel /= 3;
            }
            ms.pixelType = FormatTools.pixelTypeFromBytes(bytesPerPixel, false, bytesPerPixel > 2);
            // full timepoints are padded to have a multiple of 256 bytes
            int timepoint = FormatTools.getPlaneSize(this) * getSizeZ();
            stack.planePadding = stack.blockSize - (timepoint % stack.blockSize);
            if (stack.planePadding == stack.blockSize) {
                stack.planePadding = 0;
            }
        } else {
            boolean embedded = Location.getMappedFile(EMBEDDED_STREAM) != null;
            s.seek(0);
            if (s.read() != 'I') {
                ms.littleEndian = false;
                s.order(false);
            }
            s.seek(22);
            ms.sizeX = s.readInt();
            ms.sizeY = s.readInt();
            ms.sizeZ = s.readInt();
            ms.sizeC = embedded ? 1 : 4;
            ms.imageCount = getSizeZ() * getSizeT();
            ms.rgb = ms.sizeC > 1;
            ms.pixelType = FormatTools.UINT8;
            stack.blockSize = embedded ? (int) s.getFilePointer() : 99;
            stack.planePadding = 0;
            if (s.length() > ms.sizeX * ms.sizeY * ms.sizeZ * 6) {
                ms.pixelType = FormatTools.UINT16;
                ms.sizeC = 3;
                ms.rgb = true;
            }
            if (s.length() < (ms.sizeX * ms.sizeY * ms.sizeZ * ms.sizeC)) {
                ms.rgb = false;
                ms.sizeC = 1;
                long pixels = ms.sizeX * ms.sizeY * ms.sizeZ;
                double approximateBytes = (double) s.length() / pixels;
                int bytes = (int) Math.ceil(approximateBytes);
                if (bytes == 0) {
                    bytes = 1;
                } else if (bytes == 3) {
                    bytes = 2;
                }
                ms.pixelType = FormatTools.pixelTypeFromBytes(bytes, false, false);
                s.seek(70);
                stack.blockSize = s.readInt();
                stack.clippingData = true;
            }
        }
        s.close();
    }
    setSeries(0);
    for (int i = 0; i < getSeriesCount(); i++) {
        setSeries(i);
        Stack stack = stacks.get(i);
        addSeriesMeta("Name", stackNames.get(i));
        addSeriesMeta("Pixel width (in microns)", stack.physicalX);
        addSeriesMeta("Pixel height (in microns)", stack.physicalY);
        addSeriesMeta("Z step (in microns)", stack.physicalZ);
        addSeriesMeta("Objective magnification", stack.magnification);
        addSeriesMeta("Camera/Detector", stack.detector);
        addSeriesMeta("Description", stack.description);
        addSeriesMeta("X Location", stack.xLocation);
        addSeriesMeta("Y Location", stack.yLocation);
        addSeriesMeta("Z Location", stack.zLocation);
        if (stack.channelNames != null) {
            for (int c = 0; c < stack.channelNames.length; c++) {
                addSeriesMetaList("Channel", stack.channelNames[c]);
            }
        }
    }
    setSeries(0);
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    String instrument = MetadataTools.createLSID("Instrument", 0);
    store.setInstrumentID(instrument, 0);
    for (int i = 0; i < getSeriesCount(); i++) {
        store.setImageInstrumentRef(instrument, i);
        setSeries(i);
        Stack stack = stacks.get(i);
        store.setImageName(stackNames.get(i), i);
        store.setImageDescription(stack.description, i);
        if (stack.channelNames != null) {
            for (int c = 0; c < getEffectiveSizeC(); c++) {
                store.setChannelName(stack.channelNames[c], i, c);
            }
        }
        Length sizeX = FormatTools.getPhysicalSizeX(stack.physicalX);
        Length sizeY = FormatTools.getPhysicalSizeY(stack.physicalY);
        Length sizeZ = FormatTools.getPhysicalSizeZ(stack.physicalZ);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, i);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, i);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, i);
        }
        String objective = MetadataTools.createLSID("Objective", 0, i);
        store.setObjectiveID(objective, 0, i);
        store.setObjectiveNominalMagnification(stack.magnification, 0, i);
        store.setObjectiveCorrection(getCorrection("Other"), 0, i);
        store.setObjectiveImmersion(getImmersion("Other"), 0, i);
        store.setObjectiveSettingsID(objective, i);
        String detectorID = MetadataTools.createLSID("Detector", 0, i);
        store.setDetectorID(detectorID, 0, i);
        store.setDetectorModel(stack.detector, 0, i);
        for (int c = 0; c < getEffectiveSizeC(); c++) {
            store.setDetectorSettingsID(detectorID, i, c);
        }
        for (int img = 0; img < getImageCount(); img++) {
            int[] coords = getZCTCoords(img);
            int z = coords[0];
            final Length xLoc = new Length(stack.xLocation, UNITS.REFERENCEFRAME);
            final Length yLoc = new Length(stack.yLocation, UNITS.REFERENCEFRAME);
            store.setPlanePositionX(xLoc, i, img);
            store.setPlanePositionY(yLoc, i, img);
            if (stack.physicalZ != null) {
                final double zLocNumber = stack.zLocation + z * stack.physicalZ;
                final Length zLoc = new Length(zLocNumber, UNITS.REFERENCEFRAME);
                store.setPlanePositionZ(zLoc, i, img);
            }
            if (i < timestamps.size() && coords[2] < timestamps.get(i).length && timestamps.get(i)[coords[2]] != null) {
                store.setPlaneDeltaT(new Time(timestamps.get(i)[coords[2]], UNITS.SECOND), i, img);
            }
        }
    }
    setSeries(0);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) ArrayList(java.util.ArrayList) Time(ome.units.quantity.Time) IRandomAccess(loci.common.IRandomAccess) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) MetadataStore(loci.formats.meta.MetadataStore) MetakitService(loci.formats.services.MetakitService) Length(ome.units.quantity.Length) MissingLibraryException(loci.formats.MissingLibraryException) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) Location(loci.common.Location)

Example 2 with IRandomAccess

use of loci.common.IRandomAccess in project bioformats by openmicroscopy.

the class IM3Reader method initFile.

/* (non-Javadoc)
   * @see loci.formats.FormatReader#initFile(java.lang.String)
   */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    core.clear();
    IRandomAccess is = Location.getHandle(id, false);
    try {
        is.setOrder(ByteOrder.LITTLE_ENDIAN);
        final int cookie = is.readInt();
        if (cookie != COOKIE) {
            throw new FormatException(String.format("Expected file cookie of %d, but got %d.", COOKIE, cookie));
        }
        long fileLength = is.length();
        while (is.getFilePointer() < fileLength) {
            final IM3Record rec = parseRecord(is);
            if (rec == null) {
                if (is.getFilePointer() > fileLength - 16)
                    break;
                /*
           * # of bytes in chunk.
           */
                @SuppressWarnings("unused") final int chunkLength = is.readInt();
                /*
           * Is always zero? Chunk #?
           */
                @SuppressWarnings("unused") final int unknown = is.readInt();
                /*
           * Is always one? Chunk #?
           */
                @SuppressWarnings("unused") final int unknown1 = is.readInt();
                /*
           * # of records to follow
           */
                @SuppressWarnings("unused") final int nRecords = is.readInt();
            } else {
                if (rec instanceof ContainerRecord) {
                    final ContainerRecord bRec = (ContainerRecord) rec;
                    for (IM3Record subDS : bRec.parseChunks(is)) {
                        if ((subDS instanceof ContainerRecord) && (subDS.name.equals(FIELD_DATA_SET))) {
                            final ContainerRecord bSubDS = (ContainerRecord) subDS;
                            for (IM3Record subSubDS : bSubDS.parseChunks(is)) {
                                if (subSubDS instanceof ContainerRecord) {
                                    final ContainerRecord bDataSet = (ContainerRecord) subSubDS;
                                    dataSets.add(bDataSet);
                                    List<IM3Record> subRecs = bDataSet.parseChunks(is);
                                    final CoreMetadata cm = new CoreMetadata();
                                    cm.dimensionOrder = DimensionOrder.XYCZT.getValue();
                                    cm.littleEndian = true;
                                    // TODO: Detect pixel type
                                    cm.pixelType = FormatTools.UINT16;
                                    for (IM3Record subRec : subRecs) {
                                        if (subRec.name.equals(FIELD_SHAPE) && (subRec instanceof IntIM3Record)) {
                                            final IntIM3Record iRec = (IntIM3Record) subRec;
                                            cm.sizeX = iRec.getEntry(is, 0);
                                            cm.sizeY = iRec.getEntry(is, 1);
                                            cm.sizeC = iRec.getEntry(is, 2);
                                            cm.sizeZ = 1;
                                            cm.sizeT = 1;
                                            cm.imageCount = cm.sizeC;
                                            cm.metadataComplete = true;
                                        }
                                    }
                                    core.add(cm);
                                }
                            }
                        } else if ((subDS instanceof ContainerRecord) && subDS.name.equals(FIELD_SPECTRAL_LIBRARY)) {
                            /*
                 * SpectralLibrary
                 *   (unnamed container record)
                 *       Spectra
                 *           Keys (integers)
                 *           Values
                 *               (unnamed container record for spectrum #1)
                 *               (unnamed container record for spectrum #2)...
                 *
                 */
                            for (IM3Record slContainer : ((ContainerRecord) subDS).parseChunks(is)) {
                                /* unnamed container */
                                if (slContainer instanceof ContainerRecord) {
                                    for (IM3Record slSpectra : ((ContainerRecord) slContainer).parseChunks(is)) {
                                        if ((slSpectra instanceof ContainerRecord) && (slSpectra.name.equals(FIELD_SPECTRA))) {
                                            for (IM3Record slRec : ((ContainerRecord) slSpectra).parseChunks(is)) {
                                                if (slRec.name.equals(FIELD_VALUES) && (slRec instanceof ContainerRecord)) {
                                                    for (IM3Record spectrumRec : ((ContainerRecord) slRec).parseChunks(is)) {
                                                        if (spectrumRec instanceof ContainerRecord) {
                                                            spectra.add(new Spectrum(is, (ContainerRecord) spectrumRec));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                records.add(rec);
            }
        }
    } finally {
        is.close();
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) IRandomAccess(loci.common.IRandomAccess) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 3 with IRandomAccess

use of loci.common.IRandomAccess in project bioformats by openmicroscopy.

the class ZipReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    reader = new ImageReader();
    reader.setMetadataOptions(getMetadataOptions());
    reader.setMetadataFiltered(isMetadataFiltered());
    reader.setOriginalMetadataPopulated(isOriginalMetadataPopulated());
    reader.setNormalized(isNormalized());
    reader.setMetadataStore(getMetadataStore());
    String innerFile = id;
    if (checkSuffix(id, "zip")) {
        innerFile = id.substring(0, id.length() - 4);
    }
    int sep = innerFile.lastIndexOf(File.separator);
    if (sep < 0) {
        sep = innerFile.lastIndexOf("/");
    }
    if (sep >= 0) {
        innerFile = innerFile.substring(sep + 1);
    }
    // NB: We need a raw handle on the ZIP data itself, not a ZipHandle.
    IRandomAccess rawHandle = Location.getHandle(id, false, false);
    in = new RandomAccessInputStream(rawHandle, id);
    ZipInputStream zip = new ZipInputStream(in);
    ZipEntry ze = null;
    entryName = null;
    boolean matchFound = false;
    while (true) {
        ze = zip.getNextEntry();
        if (ze == null)
            break;
        if (entryName == null) {
            entryName = ze.getName();
        }
        if (!matchFound && ze.getName().startsWith(innerFile)) {
            entryName = ze.getName();
            matchFound = true;
        }
        ZipHandle handle = new ZipHandle(id, ze);
        Location.mapFile(ze.getName(), handle);
        mappedFiles.add(ze.getName());
    }
    if (entryName == null) {
        throw new FormatException("Zip file does not contain any valid files");
    }
    reader.setId(entryName);
    metadataStore = reader.getMetadataStore();
    core = new ArrayList<CoreMetadata>(reader.getCoreMetadataList());
    metadata = reader.getGlobalMetadata();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipHandle(loci.common.ZipHandle) ZipEntry(java.util.zip.ZipEntry) IRandomAccess(loci.common.IRandomAccess) RandomAccessInputStream(loci.common.RandomAccessInputStream) ImageReader(loci.formats.ImageReader) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 4 with IRandomAccess

use of loci.common.IRandomAccess in project bioformats by openmicroscopy.

the class TextReader method readFile.

// -- Helper methods --
private List<String> readFile(String id) throws IOException {
    List<String> lines = new ArrayList<String>();
    long time = System.currentTimeMillis();
    IRandomAccess handle = Location.getMappedFile(id);
    if (handle == null) {
        // HACK: Read using vanilla BufferedReader, since it's faster.
        String mapId = Location.getMappedId(id);
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(mapId), Constants.ENCODING));
        int no = 0;
        while (true) {
            no++;
            time = checkTime(time, no, 0, 0);
            String line = in.readLine();
            // eof
            if (line == null)
                break;
            lines.add(line);
        }
        in.close();
    } else {
        // read data using RandomAccessInputStream (data may not be a file)
        RandomAccessInputStream in = new RandomAccessInputStream(handle);
        int no = 0;
        while (true) {
            no++;
            time = checkTime(time, no, in.getFilePointer(), in.length());
            String line = in.readLine();
            // eof
            if (line == null)
                break;
            lines.add(line);
        }
        in.close();
    }
    return lines;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IRandomAccess(loci.common.IRandomAccess) RandomAccessInputStream(loci.common.RandomAccessInputStream) FileInputStream(java.io.FileInputStream)

Example 5 with IRandomAccess

use of loci.common.IRandomAccess in project bioformats by openmicroscopy.

the class TiffJAIReader 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 JAI");
    try {
        r = new ReflectedUniverse();
        r.exec("import javax.media.jai.NullOpImage");
        r.exec("import javax.media.jai.OpImage");
        r.exec("import com.sun.media.jai.codec.FileSeekableStream");
        r.exec("import com.sun.media.jai.codec.ImageDecoder");
        r.exec("import com.sun.media.jai.codec.ImageCodec");
    } catch (ReflectException exc) {
        throw new MissingLibraryException(NO_JAI_MSG, exc);
    }
    super.initFile(id);
    LOGGER.info("Reading movie dimensions");
    // map Location to File or RandomAccessFile, if possible
    IRandomAccess ira = Location.getMappedFile(id);
    if (ira != null) {
        if (ira instanceof FileHandle) {
            FileHandle fh = (FileHandle) ira;
            r.setVar("file", fh.getRandomAccessFile());
        } else {
            throw new FormatException("Unsupported handle type" + ira.getClass().getName());
        }
    } else {
        String mapId = Location.getMappedId(id);
        File file = new File(mapId);
        if (file.exists()) {
            r.setVar("file", file);
        } else
            throw new FileNotFoundException(id);
    }
    r.setVar("tiff", "tiff");
    r.setVar("param", null);
    // create TIFF decoder
    int numPages;
    try {
        r.exec("s = new FileSeekableStream(file)");
        r.exec("dec = ImageCodec.createImageDecoder(tiff, s, param)");
        numPages = ((Integer) r.exec("dec.getNumPages()")).intValue();
    } catch (ReflectException exc) {
        throw new FormatException(exc);
    }
    if (numPages < 0) {
        throw new FormatException("Invalid page count: " + numPages);
    }
    // decode first image plane
    BufferedImage img = openBufferedImage(0);
    if (img == null)
        throw new FormatException("Invalid image stream");
    LOGGER.info("Populating metadata");
    CoreMetadata m = core.get(0);
    m.imageCount = numPages;
    m.sizeX = img.getWidth();
    m.sizeY = img.getHeight();
    m.sizeZ = 1;
    m.sizeC = img.getSampleModel().getNumBands();
    m.sizeT = numPages;
    m.rgb = m.sizeC > 1;
    m.dimensionOrder = "XYCZT";
    m.pixelType = AWTImageTools.getPixelType(img);
    m.interleaved = true;
    m.littleEndian = false;
    m.metadataComplete = true;
    m.indexed = false;
    m.falseColor = false;
    // populate the metadata store
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : FileHandle(loci.common.FileHandle) FileNotFoundException(java.io.FileNotFoundException) ReflectException(loci.common.ReflectException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) BufferedImage(java.awt.image.BufferedImage) MetadataStore(loci.formats.meta.MetadataStore) MissingLibraryException(loci.formats.MissingLibraryException) ReflectedUniverse(loci.common.ReflectedUniverse) IRandomAccess(loci.common.IRandomAccess) File(java.io.File)

Aggregations

IRandomAccess (loci.common.IRandomAccess)7 CoreMetadata (loci.formats.CoreMetadata)4 RandomAccessInputStream (loci.common.RandomAccessInputStream)3 FormatException (loci.formats.FormatException)3 MetadataStore (loci.formats.meta.MetadataStore)3 ArrayList (java.util.ArrayList)2 MissingLibraryException (loci.formats.MissingLibraryException)2 BufferedImage (java.awt.image.BufferedImage)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ByteArrayHandle (loci.common.ByteArrayHandle)1 FileHandle (loci.common.FileHandle)1 Location (loci.common.Location)1 ReflectException (loci.common.ReflectException)1 ReflectedUniverse (loci.common.ReflectedUniverse)1