Search in sources :

Example 61 with RandomAccessInputStream

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

the class VolocityClippingReader 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);
    m.littleEndian = in.read() == 'I';
    in.order(isLittleEndian());
    in.skipBytes(4);
    String magicString = in.readString(4);
    if (!magicString.equals(CLIPPING_MAGIC_STRING)) {
        throw new FormatException("Found invalid magic string: " + magicString);
    }
    int check = in.readInt();
    while (check != 0x208 && check != AISF) {
        in.seek(in.getFilePointer() - 3);
        check = in.readInt();
    }
    if (check == AISF) {
        m.littleEndian = false;
        in.order(isLittleEndian());
        in.skipBytes(28);
    }
    m.sizeX = in.readInt();
    m.sizeY = in.readInt();
    m.sizeZ = in.readInt();
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = getSizeZ() * getSizeT();
    m.dimensionOrder = "XYCZT";
    m.pixelType = FormatTools.UINT8;
    pixelOffset = in.getFilePointer() + 65;
    if (getSizeX() * getSizeY() * 100 >= in.length()) {
        while (in.getFilePointer() < in.length()) {
            try {
                byte[] b = new LZOCodec().decompress(in, null);
                if (b.length > 0 && (b.length % (getSizeX() * getSizeY())) == 0) {
                    int bytes = b.length / (getSizeX() * getSizeY());
                    m.pixelType = FormatTools.pixelTypeFromBytes(bytes, false, false);
                    break;
                }
            } catch (EOFException e) {
            }
            pixelOffset++;
            in.seek(pixelOffset);
        }
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) LZOCodec(loci.formats.codec.LZOCodec) EOFException(java.io.EOFException) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 62 with RandomAccessInputStream

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

the class SPCReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    allFiles = new ArrayList<String>();
    // get the working directory
    Location tmpFile = new Location(id).getAbsoluteFile();
    Location workingDir = tmpFile.getParentFile();
    if (workingDir == null)
        workingDir = new Location(".");
    String workingDirPath = workingDir.getPath();
    if (!workingDirPath.equals(""))
        workingDirPath += File.separator;
    String[] ls = workingDir.list(true);
    if (!new Location(id).exists()) {
        ls = Location.getIdMap().keySet().toArray(new String[0]);
        workingDirPath = "";
    }
    String name = tmpFile.getName();
    // generate the name of the two matching files
    String setName = null;
    String spcName = null;
    int pos = name.lastIndexOf(".");
    if (pos != -1) {
        setName = tmpFile.getName().substring(0, pos) + ".set";
        spcName = tmpFile.getName().substring(0, pos) + ".spc";
        for (String l : ls) {
            if (l.equalsIgnoreCase(setName))
                setName = l;
            if (l.equalsIgnoreCase(spcName))
                spcName = l;
        }
    }
    if (setName == null) {
        throw new FormatException("Failed to find a matching .set file!");
    }
    if (spcName == null) {
        throw new FormatException("Failed to find a matching .spc file!");
    }
    frameClockList = new ArrayList<>();
    endOfFrameList = new ArrayList<>();
    allFiles.add(workingDirPath + setName);
    allFiles.add(workingDirPath + spcName);
    in = new RandomAccessInputStream(workingDirPath + setName);
    LOGGER.info("Reading info from .set file");
    in.order(true);
    in.skip(8);
    Integer setuppos = in.readInt();
    Short setupcount = in.readShort();
    String module = "";
    try {
        // Arbitrary length established by trial and error
        String header = in.readString(600);
        int index = header.indexOf("module SPC-");
        module = header.substring(index + 7, index + 14);
    } catch (IOException exc) {
        LOGGER.debug("Failed to read header from .set file!", exc);
    }
    if (!module.equalsIgnoreCase("SPC-134") && !module.equalsIgnoreCase("SPC-144") && !module.equalsIgnoreCase("SPC-154") && !module.equalsIgnoreCase("SPC-830")) {
        throw new FormatException("Failed to find a matching .set file!");
    }
    // goto start of setup information
    in.seek(setuppos);
    String setup = in.readString(setupcount);
    in.close();
    // get the tac range from the setup information
    double tacRange = parseSetup(TAC_RANGE, setup);
    // get the tac range from the setup information
    double tacGain = parseSetup(TAC_GAIN, setup);
    double timeBase;
    if (tacGain != 0.0 && tacRange != 0.0) {
        timeBase = 4095 * tacRange / (tacGain * 4096);
        // convert from s to ps
        timeBase *= 1.000e12;
    } else {
        throw new FormatException("Failed to parse setup file!");
    }
    LOGGER.debug("timeBase = " + Double.toString(timeBase));
    // Now read .spc file
    spcId = workingDirPath + spcName;
    in = new RandomAccessInputStream(spcId);
    in.order(true);
    LOGGER.info("Reading info from .spc file");
    /* The first 3 bytes in the file contain information about the 
     * macro time clock in 0.1 ns units ("500" for 50ns clock)
    */
    in.skip(3);
    /*
     * The 4th byte contains information about the number of 
     * routing channels in bits 3 to 6.
     * Bits 0-2 reserved bit 7 = 1 ("Data invalid")
    */
    byte routing = in.readByte();
    if ((routing & 0x10) != 0) {
        throw new FormatException("Invalid data!");
    }
    nChannels = (routing & 0x78) >> 3;
    currentPixel = 0;
    currentLine = -1;
    currentFrame = -1;
    rawBuf = new byte[bufLength];
    endOfFrameFlag = false;
    nBuffers = 0;
    bufLength = 1024;
    rawBuf = new byte[bufLength];
    int noOfBytes;
    nBuffers = 0;
    byte adcL;
    byte adcLM;
    while ((noOfBytes = in.read(rawBuf)) != -1) {
        for (int bb = 3; bb < noOfBytes; bb += 4) {
            // even nybble adc
            // get upper byte containing ADC data
            adcL = rawBuf[bb];
            // mask out upper 4 bits
            adcLM = (byte) (adcL & 0xF0);
            // at this point only the various clocks are of interest
            switch(adcLM) {
                case (byte) 0x90:
                    invalidAndMarkInit(bb);
                    break;
                case // Invalid, Mark and MTOV all set.
                (byte) 0xd0:
                    // Unsure about this-- Not well documented!
                    invalidAndMarkInit(bb);
                    break;
                default:
                    break;
            }
        // end switch
        }
        // end for
        nBuffers++;
    }
    nTimebins = (0xFFF >> adcResShift) + 1;
    nFrames = currentFrame - 1;
    addGlobalMeta("time bins", nTimebins);
    addGlobalMeta("nChannels", nChannels);
    addGlobalMeta("time base", timeBase);
    LOGGER.info("Populating metadata");
    CoreMetadata m = core.get(0);
    // Duplicates the behaviour of U.Lorenzo's Matlab code.
    if (nLines < 530) {
        // return an image
        lineMode = false;
        m.sizeY = nLines;
    } else {
        LOGGER.info("Single line mode");
        // return a single line
        lineMode = true;
        m.sizeY = 1;
    }
    Integer frameLength;
    Integer maxFrameLength = 0;
    for (int T = 0; T < nFrames; T++) {
        frameLength = (int) (endOfFrameList.get(T + 1) - frameClockList.get(T));
        if (frameLength > maxFrameLength)
            maxFrameLength = frameLength;
    }
    rawBuf = new byte[maxFrameLength];
    m.sizeX = nPixels;
    m.sizeZ = 1;
    m.sizeT = nTimebins * nFrames;
    m.sizeC = nChannels;
    m.dimensionOrder = "XYZTC";
    m.pixelType = FormatTools.UINT16;
    m.rgb = false;
    m.littleEndian = true;
    m.imageCount = m.sizeZ * m.sizeC * m.sizeT;
    m.indexed = false;
    m.falseColor = false;
    m.metadataComplete = true;
    m.moduloT.type = FormatTools.LIFETIME;
    m.moduloT.parentType = FormatTools.SPECTRA;
    m.moduloT.typeDescription = "TCSPC";
    m.moduloT.start = 0;
    m.moduloT.step = timeBase / nTimebins;
    m.moduloT.end = m.moduloT.step * (nTimebins - 1);
    m.moduloT.unit = "ps";
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : IOException(java.io.IOException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Example 63 with RandomAccessInputStream

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

the class SVSReader method isThisType.

/* (non-Javadoc)
   * @see loci.formats.FormatReader#isThisType(java.lang.String, boolean)
   */
@Override
public boolean isThisType(String name, boolean open) {
    boolean isThisType = super.isThisType(name, open);
    if (!isThisType && open) {
        RandomAccessInputStream stream = null;
        try {
            stream = new RandomAccessInputStream(name);
            TiffParser tiffParser = new TiffParser(stream);
            tiffParser.setDoCaching(false);
            if (!tiffParser.isValidHeader()) {
                return false;
            }
            IFD ifd = tiffParser.getFirstIFD();
            if (ifd == null) {
                return false;
            }
            Object description = ifd.get(IFD.IMAGE_DESCRIPTION);
            if (description != null) {
                String imageDescription = null;
                if (description instanceof TiffIFDEntry) {
                    Object value = tiffParser.getIFDValue((TiffIFDEntry) description);
                    if (value != null) {
                        imageDescription = value.toString();
                    }
                } else if (description instanceof String) {
                    imageDescription = (String) description;
                }
                if (imageDescription != null && imageDescription.startsWith(APERIO_IMAGE_DESCRIPTION_PREFIX)) {
                    return true;
                }
            }
            return false;
        } catch (IOException e) {
            LOGGER.debug("I/O exception during isThisType() evaluation.", e);
            return false;
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException e) {
                LOGGER.debug("I/O exception during stream closure.", e);
            }
        }
    }
    return isThisType;
}
Also used : TiffIFDEntry(loci.formats.tiff.TiffIFDEntry) IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException)

Example 64 with RandomAccessInputStream

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

the class SlidebookTiffReader method getFirstChannel.

private String getFirstChannel(String path) throws FormatException, IOException {
    RandomAccessInputStream s = new RandomAccessInputStream(path);
    TiffParser parser = new TiffParser(s);
    IFD ifd = parser.getFirstIFD();
    Object channel = ifd.getIFDValue(CHANNEL_TAG);
    s.close();
    parser.getStream().close();
    return channel == null ? null : channel.toString();
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 65 with RandomAccessInputStream

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

the class TargaReader method openBytes.

/**
 * @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
 */
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    in.seek(offset);
    int planeSize = FormatTools.getPlaneSize(this);
    TargaRLECodec codec = new TargaRLECodec();
    CodecOptions options = new CodecOptions();
    options.maxBytes = planeSize;
    options.bitsPerSample = bits;
    RandomAccessInputStream s = in;
    if (compressed) {
        byte[] b = codec.decompress(in, options);
        s = new RandomAccessInputStream(b);
        s.order(isLittleEndian());
    }
    int bpp = bits;
    while (bpp % 8 != 0) bpp++;
    bpp /= 8;
    int rowSkip = orientation < 2 ? (getSizeY() - h - y) : y;
    int colSkip = (orientation % 2) == 1 ? (getSizeX() - w - x) : x;
    s.skipBytes(rowSkip * getSizeX() * bpp);
    for (int row = 0; row < h; row++) {
        if (s.getFilePointer() >= s.length())
            break;
        s.skipBytes(colSkip * bpp);
        for (int col = 0; col < w; col++) {
            if (s.getFilePointer() >= s.length())
                break;
            int rowIndex = orientation < 2 ? h - row - 1 : row;
            int colIndex = (orientation % 2) == 1 ? w - col - 1 : col;
            int index = getSizeC() * (rowIndex * w + colIndex);
            if (bpp == 2) {
                int v = s.readShort();
                buf[index] = (byte) ((v & 0x7c00) >> 10);
                buf[index + 1] = (byte) ((v & 0x3e0) >> 5);
                buf[index + 2] = (byte) (v & 0x1f);
            } else if (bpp == 4) {
                buf[index + 2] = s.readByte();
                buf[index + 1] = s.readByte();
                buf[index] = s.readByte();
                s.skipBytes(1);
            } else {
                for (int c = getSizeC() - 1; c >= 0; c--) {
                    buf[index + c] = s.readByte();
                }
            }
        }
        s.skipBytes(bpp * (getSizeX() - w - colSkip));
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) TargaRLECodec(loci.formats.codec.TargaRLECodec) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

RandomAccessInputStream (loci.common.RandomAccessInputStream)246 CoreMetadata (loci.formats.CoreMetadata)108 MetadataStore (loci.formats.meta.MetadataStore)97 FormatException (loci.formats.FormatException)75 TiffParser (loci.formats.tiff.TiffParser)56 IFD (loci.formats.tiff.IFD)51 Length (ome.units.quantity.Length)48 Location (loci.common.Location)47 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)30 Timestamp (ome.xml.model.primitives.Timestamp)28 Time (ome.units.quantity.Time)21 ByteArrayHandle (loci.common.ByteArrayHandle)18 IFDList (loci.formats.tiff.IFDList)16 CodecOptions (loci.formats.codec.CodecOptions)9 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)8 ServiceException (loci.common.services.ServiceException)7 PhotoInterp (loci.formats.tiff.PhotoInterp)7 TiffSaver (loci.formats.tiff.TiffSaver)7 BufferedReader (java.io.BufferedReader)6