Search in sources :

Example 56 with MetadataRetrieve

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

the class JavaWriter 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 (!isFullPlane(x, y, w, h)) {
        throw new FormatException("JavaWriter does not yet support saving image tiles.");
    }
    // check pixel type
    MetadataRetrieve meta = getMetadataRetrieve();
    String pixelType = meta.getPixelsType(series).toString();
    int type = FormatTools.pixelTypeFromString(pixelType);
    if (!DataTools.containsValue(getPixelTypes(), type)) {
        throw new FormatException("Unsupported image type '" + pixelType + "'.");
    }
    int bpp = FormatTools.getBytesPerPixel(type);
    boolean fp = FormatTools.isFloatingPoint(type);
    boolean little = false;
    if (meta.getPixelsBigEndian(series) != null) {
        little = !meta.getPixelsBigEndian(series).booleanValue();
    } else if (meta.getPixelsBinDataCount(series) == 0) {
        little = !meta.getPixelsBinDataBigEndian(series, 0).booleanValue();
    }
    // write array
    String varName = "series" + series + "Plane" + no;
    Object array = DataTools.makeDataArray(buf, bpp, fp, little);
    out.seek(out.length());
    if (array instanceof byte[]) {
        writePlane(varName, (byte[]) array, w, h);
    } else if (array instanceof short[]) {
        writePlane(varName, (short[]) array, w, h);
    } else if (array instanceof int[]) {
        writePlane(varName, (int[]) array, w, h);
    } else if (array instanceof long[]) {
        writePlane(varName, (long[]) array, w, h);
    } else if (array instanceof float[]) {
        writePlane(varName, (float[]) array, w, h);
    } else if (array instanceof double[]) {
        writePlane(varName, (double[]) array, w, h);
    }
}
Also used : MetadataRetrieve(loci.formats.meta.MetadataRetrieve) FormatException(loci.formats.FormatException)

Example 57 with MetadataRetrieve

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

the class OMETiffWriter method setupServiceAndMetadata.

private void setupServiceAndMetadata() throws DependencyException, ServiceException {
    // extract OME-XML string from metadata object
    MetadataRetrieve retrieve = getMetadataRetrieve();
    ServiceFactory factory = new ServiceFactory();
    service = factory.getInstance(OMEXMLService.class);
    OMEXMLMetadata originalOMEMeta = service.getOMEMetadata(retrieve);
    originalOMEMeta.resolveReferences();
    String omexml = service.getOMEXML(originalOMEMeta);
    omeMeta = service.createOMEXMLMetadata(omexml);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Example 58 with MetadataRetrieve

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

the class QTWriter method setId.

// -- FormatWriter API methods --
/* @see loci.formats.FormatWriter#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    super.setId(id);
    MetadataRetrieve r = getMetadataRetrieve();
    MetadataTools.verifyMinimumPopulated(r, series);
    int width = r.getPixelsSizeX(series).getValue().intValue();
    int height = r.getPixelsSizeY(series).getValue().intValue();
    int nChannels = getSamplesPerPixel();
    int planeSize = width * height * nChannels;
    pad = nChannels > 1 ? 0 : (4 - (width % 4)) % 4;
    if (legacy == null) {
        legacy = new LegacyQTWriter();
        legacy.setCodec(codec);
        legacy.setMetadataRetrieve(r);
    }
    offsets = new ArrayList<Integer>();
    created = (int) System.currentTimeMillis();
    numBytes = 0;
    if (out.length() == 0) {
        // -- write the first header --
        writeAtom(8, "wide");
        writeAtom(numBytes + 8, "mdat");
        numWritten = 0;
    } else {
        out.seek(BYTE_COUNT_OFFSET);
        RandomAccessInputStream in = new RandomAccessInputStream(currentId);
        in.seek(BYTE_COUNT_OFFSET);
        numBytes = in.readInt() - 8;
        numWritten = numBytes / (planeSize + pad * height);
        in.close();
    }
    for (int i = 0; i < getPlaneCount(); i++) {
        offsets.add(16 + i * (planeSize + pad * height));
    }
}
Also used : RandomAccessInputStream(loci.common.RandomAccessInputStream) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 59 with MetadataRetrieve

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

the class QTWriter method writeFooter.

private void writeFooter() throws IOException {
    out.seek(out.length());
    MetadataRetrieve r = getMetadataRetrieve();
    int width = r.getPixelsSizeX(series).getValue().intValue();
    int height = r.getPixelsSizeY(series).getValue().intValue();
    int nChannels = getSamplesPerPixel();
    int timeScale = 1000;
    int duration = (int) (numWritten * ((double) timeScale / fps));
    int bitsPerPixel = (nChannels > 1) ? 24 : 40;
    int channels = (bitsPerPixel >= 40) ? 1 : 3;
    // -- write moov atom --
    int atomLength = 685 + 8 * numWritten;
    writeAtom(atomLength, "moov");
    // -- write mvhd atom --
    writeAtom(108, "mvhd");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // creation time
    out.writeInt(created);
    out.writeInt((int) System.currentTimeMillis());
    // time scale
    out.writeInt(timeScale);
    // duration
    out.writeInt(duration);
    // preferred rate & volume
    out.write(new byte[] { 0, 1, 0, 0 });
    // reserved
    out.write(new byte[] { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0 });
    writeRotationMatrix();
    // not sure what this is
    out.writeShort(0);
    // preview duration
    out.writeInt(0);
    // preview time
    out.writeInt(0);
    // poster time
    out.writeInt(0);
    // selection time
    out.writeInt(0);
    // selection duration
    out.writeInt(0);
    // current time
    out.writeInt(0);
    // next track's id
    out.writeInt(2);
    // -- write trak atom --
    atomLength -= 116;
    writeAtom(atomLength, "trak");
    // -- write tkhd atom --
    writeAtom(92, "tkhd");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(15);
    // creation time
    out.writeInt(created);
    out.writeInt((int) System.currentTimeMillis());
    // track id
    out.writeInt(1);
    // reserved
    out.writeInt(0);
    // duration
    out.writeInt(duration);
    // reserved
    out.writeInt(0);
    // reserved
    out.writeInt(0);
    // reserved
    out.writeShort(0);
    // unknown
    out.writeInt(0);
    writeRotationMatrix();
    // image width
    out.writeInt(width);
    // image height
    out.writeInt(height);
    // reserved
    out.writeShort(0);
    // -- write edts atom --
    writeAtom(36, "edts");
    // -- write elst atom --
    writeAtom(28, "elst");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // number of entries in the table
    out.writeInt(1);
    // duration
    out.writeInt(duration);
    // time
    out.writeShort(0);
    // rate
    out.writeInt(1);
    // unknown
    out.writeShort(0);
    // -- write mdia atom --
    atomLength -= 136;
    writeAtom(atomLength, "mdia");
    // -- write mdhd atom --
    writeAtom(32, "mdhd");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // creation time
    out.writeInt(created);
    out.writeInt((int) System.currentTimeMillis());
    // time scale
    out.writeInt(timeScale);
    // duration
    out.writeInt(duration);
    // language
    out.writeShort(0);
    // quality
    out.writeShort(0);
    // -- write hdlr atom --
    writeAtom(58, "hdlr");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    out.writeBytes("mhlr");
    out.writeBytes("vide");
    out.writeBytes("appl");
    out.write(new byte[] { 16, 0, 0, 0, 0, 1, 1, 11, 25 });
    out.writeBytes("Apple Video Media Handler");
    // -- write minf atom --
    atomLength -= 98;
    writeAtom(atomLength, "minf");
    // -- write vmhd atom --
    writeAtom(20, "vmhd");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(1);
    // graphics mode
    out.writeShort(64);
    // opcolor 1
    out.writeShort(32768);
    // opcolor 2
    out.writeShort(32768);
    // opcolor 3
    out.writeShort(32768);
    // -- write hdlr atom --
    writeAtom(57, "hdlr");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    out.writeBytes("dhlr");
    out.writeBytes("alis");
    out.writeBytes("appl");
    out.write(new byte[] { 16, 0, 0, 1, 0, 1, 1, 31, 24 });
    out.writeBytes("Apple Alias Data Handler");
    // -- write dinf atom --
    writeAtom(36, "dinf");
    // -- write dref atom --
    writeAtom(28, "dref");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // version 2
    out.writeShort(0);
    // flags 2
    out.writeShort(1);
    out.write(new byte[] { 0, 0, 0, 12 });
    out.writeBytes("alis");
    // version 3
    out.writeShort(0);
    // flags 3
    out.writeShort(1);
    // -- write stbl atom --
    atomLength -= 121;
    writeAtom(atomLength, "stbl");
    // -- write stsd atom --
    writeAtom(118, "stsd");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // number of entries in the table
    out.writeInt(1);
    out.write(new byte[] { 0, 0, 0, 102 });
    // codec
    out.writeBytes("raw ");
    // reserved
    out.write(new byte[] { 0, 0, 0, 0, 0, 0 });
    // data reference
    out.writeShort(1);
    // version
    out.writeShort(1);
    // revision
    out.writeShort(1);
    out.writeBytes("appl");
    // temporal quality
    out.writeInt(0);
    // spatial quality
    out.writeInt(768);
    // image width
    out.writeShort(width);
    // image height
    out.writeShort(height);
    byte[] dpi = new byte[] { 0, 72, 0, 0 };
    // horizontal dpi
    out.write(dpi);
    // vertical dpi
    out.write(dpi);
    // data size
    out.writeInt(0);
    // frames per sample
    out.writeShort(1);
    // length of compressor name
    out.writeShort(12);
    // compressor name
    out.writeBytes("Uncompressed");
    // unknown
    out.writeInt(bitsPerPixel);
    // unknown
    out.writeInt(bitsPerPixel);
    // unknown
    out.writeInt(bitsPerPixel);
    // unknown
    out.writeInt(bitsPerPixel);
    // unknown
    out.writeInt(bitsPerPixel);
    // bits per pixel
    out.writeShort(bitsPerPixel);
    // ctab ID
    out.writeInt(65535);
    // gamma
    out.write(new byte[] { 12, 103, 97, 108 });
    // unknown
    out.write(new byte[] { 97, 1, -52, -52, 0, 0, 0, 0 });
    // -- write stts atom --
    writeAtom(24, "stts");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // number of entries in the table
    out.writeInt(1);
    // number of planes
    out.writeInt(numWritten);
    // milliseconds per frame
    out.writeInt((int) ((double) timeScale / fps));
    // -- write stsc atom --
    writeAtom(28, "stsc");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // number of entries in the table
    out.writeInt(1);
    // chunk
    out.writeInt(1);
    // samples
    out.writeInt(1);
    // id
    out.writeInt(1);
    // -- write stsz atom --
    writeAtom(20 + 4 * numWritten, "stsz");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // sample size
    out.writeInt(0);
    // number of planes
    out.writeInt(numWritten);
    for (int i = 0; i < numWritten; i++) {
        // sample size
        out.writeInt(channels * height * (width + pad));
    }
    // -- write stco atom --
    writeAtom(16 + 4 * numWritten, "stco");
    // version
    out.writeShort(0);
    // flags
    out.writeShort(0);
    // number of planes
    out.writeInt(numWritten);
    for (int i = 0; i < numWritten; i++) {
        // write the plane offset
        out.writeInt(offsets.get(i));
    }
}
Also used : MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 60 with MetadataRetrieve

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

the class TiffWriter method saveBytes.

/**
 * Saves the given image to the specified series in the current file.
 * The IFD hashtable allows specification of TIFF parameters such as bit
 * depth, compression and units.
 */
public void saveBytes(int no, byte[] buf, IFD ifd, int x, int y, int w, int h) throws IOException, FormatException {
    if (checkParams)
        checkParams(no, buf, x, y, w, h);
    if (ifd == null)
        ifd = new IFD();
    MetadataRetrieve retrieve = getMetadataRetrieve();
    int type = FormatTools.pixelTypeFromString(retrieve.getPixelsType(series).toString());
    int index = no;
    int imageWidth = retrieve.getPixelsSizeX(series).getValue().intValue();
    int imageHeight = retrieve.getPixelsSizeY(series).getValue().intValue();
    int currentTileSizeX = getTileSizeX();
    int currentTileSizeY = getTileSizeY();
    if (currentTileSizeX != imageWidth || currentTileSizeY != imageHeight) {
        ifd.put(new Integer(IFD.TILE_WIDTH), new Long(currentTileSizeX));
        ifd.put(new Integer(IFD.TILE_LENGTH), new Long(currentTileSizeY));
    }
    if (currentTileSizeX < w || currentTileSizeY < h) {
        int numTilesX = (w + (x % currentTileSizeX) + currentTileSizeX - 1) / currentTileSizeX;
        int numTilesY = (h + (y % currentTileSizeY) + currentTileSizeY - 1) / currentTileSizeY;
        for (int yTileIndex = 0; yTileIndex < numTilesY; yTileIndex++) {
            for (int xTileIndex = 0; xTileIndex < numTilesX; xTileIndex++) {
                Region tileParams = new Region();
                tileParams.width = xTileIndex < numTilesX - 1 ? currentTileSizeX - (x % currentTileSizeX) : w - (currentTileSizeX * xTileIndex);
                tileParams.height = yTileIndex < numTilesY - 1 ? currentTileSizeY - (y % currentTileSizeY) : h - (currentTileSizeY * yTileIndex);
                tileParams.x = x + (xTileIndex * currentTileSizeX) - (xTileIndex > 0 ? (x % currentTileSizeX) : 0);
                tileParams.y = y + (yTileIndex * currentTileSizeY) - (yTileIndex > 0 ? (y % currentTileSizeY) : 0);
                byte[] tileBuf = getTile(buf, tileParams, new Region(x, y, w, h));
                // This operation is synchronized
                synchronized (this) {
                    // This operation is synchronized against the TIFF saver.
                    synchronized (tiffSaver) {
                        index = prepareToWriteImage(no, tileBuf, ifd, tileParams.x, tileParams.y, tileParams.width, tileParams.height);
                        if (index == -1) {
                            return;
                        }
                    }
                }
                tiffSaver.writeImage(tileBuf, ifd, index, type, tileParams.x, tileParams.y, tileParams.width, tileParams.height, no == getPlaneCount() - 1 && getSeries() == retrieve.getImageCount() - 1);
            }
        }
    } else {
        // This operation is synchronized
        synchronized (this) {
            // This operation is synchronized against the TIFF saver.
            synchronized (tiffSaver) {
                index = prepareToWriteImage(no, buf, ifd, x, y, w, h);
                if (index == -1) {
                    return;
                }
            }
        }
        tiffSaver.writeImage(buf, ifd, index, type, x, y, w, h, no == getPlaneCount() - 1 && getSeries() == retrieve.getImageCount() - 1);
    }
}
Also used : IFD(loci.formats.tiff.IFD) Region(loci.common.Region) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Aggregations

MetadataRetrieve (loci.formats.meta.MetadataRetrieve)64 FormatException (loci.formats.FormatException)11 MetadataStore (loci.formats.meta.MetadataStore)11 Length (ome.units.quantity.Length)10 ServiceFactory (loci.common.services.ServiceFactory)8 DependencyException (loci.common.services.DependencyException)7 OMEXMLService (loci.formats.services.OMEXMLService)6 RandomAccessInputStream (loci.common.RandomAccessInputStream)5 Time (ome.units.quantity.Time)5 PositiveInteger (ome.xml.model.primitives.PositiveInteger)5 ServiceException (loci.common.services.ServiceException)4 MissingLibraryException (loci.formats.MissingLibraryException)4 Test (org.testng.annotations.Test)4 IndexColorModel (java.awt.image.IndexColorModel)3 Location (loci.common.Location)3 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)3 ImageReader (loci.formats.ImageReader)3 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2