Search in sources :

Example 11 with RandomAccessOutputStream

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

the class TiffSaverTest method setUp.

@BeforeMethod
public void setUp() throws IOException {
    ByteArrayHandle handle = new ByteArrayHandle(INITIAL_CAPACITY);
    out = new RandomAccessOutputStream(handle);
    in = new RandomAccessInputStream(handle);
    tiffSaver = new TiffSaver(out, handle);
    tiffParser = new TiffParser(in);
    ifd = new IFD();
    ifd.putIFDValue(IFD.IMAGE_WIDTH, 512);
    ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, "comment");
}
Also used : IFD(loci.formats.tiff.IFD) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 12 with RandomAccessOutputStream

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

the class PGMReader 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);
    if (rawBits) {
        readPlane(in, x, y, w, h, buf);
    } else {
        ByteArrayHandle handle = new ByteArrayHandle();
        RandomAccessOutputStream out = new RandomAccessOutputStream(handle);
        out.order(isLittleEndian());
        while (in.getFilePointer() < in.length()) {
            String line = in.readLine().trim();
            line = line.replaceAll("[^0-9]", " ");
            StringTokenizer t = new StringTokenizer(line, " ");
            while (t.hasMoreTokens()) {
                int q = Integer.parseInt(t.nextToken().trim());
                if (getPixelType() == FormatTools.UINT16) {
                    out.writeShort(q);
                } else
                    out.writeByte(q);
            }
        }
        out.close();
        RandomAccessInputStream s = new RandomAccessInputStream(handle);
        s.seek(0);
        readPlane(s, x, y, w, h, buf);
        s.close();
    }
    return buf;
}
Also used : StringTokenizer(java.util.StringTokenizer) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 13 with RandomAccessOutputStream

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

the class ICSWriter method saveBytes.

// -- IFormatWriter API methods --
/**
 * @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int, int)
 */
@Override
public void saveBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    checkParams(no, buf, x, y, w, h);
    if (pixels == null) {
        pixels = new RandomAccessOutputStream(currentId);
    }
    MetadataRetrieve meta = getMetadataRetrieve();
    int rgbChannels = getSamplesPerPixel();
    String order = meta.getPixelsDimensionOrder(series).getValue();
    int sizeZ = meta.getPixelsSizeZ(series).getValue().intValue();
    int sizeC = meta.getChannelCount(series);
    if (rgbChannels <= sizeC) {
        sizeC /= rgbChannels;
    }
    int sizeT = meta.getPixelsSizeT(series).getValue().intValue();
    int planes = sizeZ * sizeC * sizeT;
    int[] coords = FormatTools.getZCTCoords(order, sizeZ, sizeC, sizeT, planes, no);
    int realIndex = FormatTools.getIndex(outputOrder, sizeZ, sizeC, sizeT, planes, coords[0], coords[1], coords[2]);
    if (uniqueFiles.size() > 1) {
        realIndex = no;
    }
    int sizeX = meta.getPixelsSizeX(series).getValue().intValue();
    int sizeY = meta.getPixelsSizeY(series).getValue().intValue();
    int pixelType = FormatTools.pixelTypeFromString(meta.getPixelsType(series).toString());
    int bytesPerPixel = FormatTools.getBytesPerPixel(pixelType);
    long planeSize = sizeX * sizeY * rgbChannels * bytesPerPixel;
    if (!initialized[series][realIndex]) {
        initialized[series][realIndex] = true;
        if (!isFullPlane(x, y, w, h)) {
            // write a dummy plane that will be overwritten in sections
            pixels.seek(pixelOffset + (realIndex + 1) * planeSize);
        }
    }
    pixels.seek(pixelOffset + realIndex * planeSize);
    if (isFullPlane(x, y, w, h) && (interleaved || rgbChannels == 1)) {
        pixels.write(buf);
    } else {
        pixels.skipBytes(bytesPerPixel * rgbChannels * sizeX * y);
        for (int row = 0; row < h; row++) {
            ByteArrayOutputStream strip = new ByteArrayOutputStream();
            for (int col = 0; col < w; col++) {
                for (int c = 0; c < rgbChannels; c++) {
                    int index = interleaved ? rgbChannels * (row * w + col) + c : w * (c * h + row) + col;
                    strip.write(buf, index * bytesPerPixel, bytesPerPixel);
                }
            }
            pixels.skipBytes(bytesPerPixel * rgbChannels * x);
            pixels.write(strip.toByteArray());
            pixels.skipBytes(bytesPerPixel * rgbChannels * (sizeX - w - x));
        }
    }
    lastPlane = realIndex;
    overwriteDimensions(getMetadataRetrieve());
    pixels.close();
    pixels = null;
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 14 with RandomAccessOutputStream

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

the class TiffSaver method writeIFD.

public void writeIFD(IFD ifd, long nextOffset) throws FormatException, IOException {
    TreeSet<Integer> keys = new TreeSet<Integer>(ifd.keySet());
    int keyCount = keys.size();
    if (ifd.containsKey(new Integer(IFD.LITTLE_ENDIAN)))
        keyCount--;
    if (ifd.containsKey(new Integer(IFD.BIG_TIFF)))
        keyCount--;
    if (ifd.containsKey(new Integer(IFD.REUSE)))
        keyCount--;
    long fp = out.getFilePointer();
    int bytesPerEntry = bigTiff ? TiffConstants.BIG_TIFF_BYTES_PER_ENTRY : TiffConstants.BYTES_PER_ENTRY;
    int ifdBytes = (bigTiff ? 16 : 6) + bytesPerEntry * keyCount;
    if (bigTiff)
        out.writeLong(keyCount);
    else
        out.writeShort(keyCount);
    ByteArrayHandle extra = new ByteArrayHandle();
    RandomAccessOutputStream extraStream = new RandomAccessOutputStream(extra);
    for (Integer key : keys) {
        if (key.equals(IFD.LITTLE_ENDIAN) || key.equals(IFD.BIG_TIFF) || key.equals(IFD.REUSE))
            continue;
        Object value = ifd.get(key);
        writeIFDValue(extraStream, ifdBytes + fp, key.intValue(), value);
    }
    if (bigTiff)
        out.seek(out.getFilePointer());
    writeIntValue(out, nextOffset);
    out.write(extra.getBytes(), 0, (int) extra.length());
    extraStream.close();
}
Also used : TreeSet(java.util.TreeSet) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 15 with RandomAccessOutputStream

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

the class OMETiffWriter method saveComment.

private void saveComment(String file, String xml) throws IOException {
    if (out != null)
        out.close();
    out = new RandomAccessOutputStream(file);
    RandomAccessInputStream in = null;
    try {
        TiffSaver saver = new TiffSaver(out, file);
        saver.setBigTiff(isBigTiff);
        in = new RandomAccessInputStream(file);
        saver.overwriteLastIFDOffset(in);
        saver.overwriteComment(in, xml);
    } catch (FormatException exc) {
        IOException io = new IOException("Unable to append OME-XML comment");
        io.initCause(exc);
        throw io;
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
    }
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) FormatException(loci.formats.FormatException)

Aggregations

RandomAccessOutputStream (loci.common.RandomAccessOutputStream)15 TiffSaver (loci.formats.tiff.TiffSaver)9 RandomAccessInputStream (loci.common.RandomAccessInputStream)8 ByteArrayHandle (loci.common.ByteArrayHandle)7 FormatException (loci.formats.FormatException)5 IOException (java.io.IOException)3 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)3 Test (org.testng.annotations.Test)3 IFD (loci.formats.tiff.IFD)2 TiffParser (loci.formats.tiff.TiffParser)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 StringTokenizer (java.util.StringTokenizer)1 TreeSet (java.util.TreeSet)1 ImageReader (loci.formats.ImageReader)1 Time (ome.units.quantity.Time)1 BeforeMethod (org.testng.annotations.BeforeMethod)1