Search in sources :

Example 16 with MetadataRetrieve

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

the class FormatWriter method getSamplesPerPixel.

/**
 * Retrieve the number of samples per pixel for given series.
 */
protected int getSamplesPerPixel(int series) {
    MetadataRetrieve r = getMetadataRetrieve();
    PositiveInteger samples = r.getChannelSamplesPerPixel(series, 0);
    if (samples == null) {
        LOGGER.warn("SamplesPerPixel #0 is null. It is assumed to be 1.");
    }
    return samples == null ? 1 : samples.getValue();
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 17 with MetadataRetrieve

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

the class OrthogonalReader method initialiseWriter.

private OMETiffWriter initialiseWriter(String fileName, ImageReader reader) throws Exception {
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata metadata = service.createOMEXMLMetadata();
    MetadataRetrieve mr = (MetadataRetrieve) reader.getMetadataStore();
    Length originalSizeX = mr.getPixelsPhysicalSizeX(0);
    Length originalSizeY = mr.getPixelsPhysicalSizeY(0);
    Length originalSizeZ = mr.getPixelsPhysicalSizeZ(0);
    // Original XY planes
    // XZ planes
    MetadataTools.populateMetadata(metadata, 0, "XZ", reader.isLittleEndian(), reader.getDimensionOrder(), FormatTools.getPixelTypeString(reader.getPixelType()), reader.getSizeX(), reader.getSizeZ(), reader.getSizeY(), 1, 1, 1);
    metadata.setPixelsPhysicalSizeX(originalSizeX, 0);
    metadata.setPixelsPhysicalSizeY(originalSizeZ, 0);
    metadata.setPixelsPhysicalSizeZ(originalSizeY, 0);
    // YZ planes
    MetadataTools.populateMetadata(metadata, 1, "YZ", reader.isLittleEndian(), reader.getDimensionOrder(), FormatTools.getPixelTypeString(reader.getPixelType()), reader.getSizeY(), reader.getSizeZ(), reader.getSizeX(), 1, 1, 1);
    metadata.setPixelsPhysicalSizeX(originalSizeY, 1);
    metadata.setPixelsPhysicalSizeY(originalSizeZ, 1);
    metadata.setPixelsPhysicalSizeZ(originalSizeX, 1);
    OMETiffWriter writer = new OMETiffWriter();
    writer.setMetadataRetrieve(metadata);
    writer.setId(fileName);
    return writer;
}
Also used : OMETiffWriter(loci.formats.out.OMETiffWriter) IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) Length(ome.units.quantity.Length) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Example 18 with MetadataRetrieve

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

the class APNGWriter method setId.

// -- FormatWriter API methods --
/* @see loci.formats.FormatWriter#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    super.setId(id);
    if (out.length() == 0) {
        numFrames = 0;
        MetadataRetrieve r = getMetadataRetrieve();
        int width = r.getPixelsSizeX(series).getValue().intValue();
        int height = r.getPixelsSizeY(series).getValue().intValue();
        int bytesPerPixel = FormatTools.getBytesPerPixel(r.getPixelsType(series).toString());
        int nChannels = getSamplesPerPixel();
        boolean indexed = getColorModel() != null && (getColorModel() instanceof IndexColorModel);
        if (r.getPixelsBigEndian(series) != null) {
            littleEndian = !r.getPixelsBigEndian(series).booleanValue();
        } else if (r.getPixelsBinDataCount(series) == 0) {
            littleEndian = !r.getPixelsBinDataBigEndian(series, 0).booleanValue();
        }
        // write 8-byte PNG signature
        out.write(PNG_SIG);
        // write IHDR chunk
        out.writeInt(13);
        byte[] b = new byte[17];
        b[0] = 'I';
        b[1] = 'H';
        b[2] = 'D';
        b[3] = 'R';
        DataTools.unpackBytes(width, b, 4, 4, false);
        DataTools.unpackBytes(height, b, 8, 4, false);
        b[12] = (byte) (bytesPerPixel * 8);
        if (indexed)
            b[13] = (byte) 3;
        else if (nChannels == 1)
            b[13] = (byte) 0;
        else if (nChannels == 2)
            b[13] = (byte) 4;
        else if (nChannels == 3)
            b[13] = (byte) 2;
        else if (nChannels == 4)
            b[13] = (byte) 6;
        b[14] = (byte) 0;
        b[15] = (byte) 0;
        b[16] = (byte) 0;
        out.write(b);
        out.writeInt(crc(b));
        // write acTL chunk
        out.writeInt(8);
        out.writeBytes("acTL");
        numFramesPointer = out.getFilePointer();
        out.writeInt(0);
        out.writeInt(0);
        // save a place for the CRC
        out.writeInt(0);
        footerPointer = out.getFilePointer();
    } else {
        numFramesPointer = PNG_SIG.length + 33;
        RandomAccessInputStream in = new RandomAccessInputStream(id);
        in.order(littleEndian);
        in.seek(8);
        while (in.getFilePointer() < in.length()) {
            int length = in.readInt();
            String type = in.readString(4);
            if (type.equals("fcTL") || type.equals("fdAT")) {
                nextSequenceNumber = in.readInt() + 1;
                length -= 4;
            }
            in.skipBytes(length + 4);
        }
        in.seek(numFramesPointer);
        numFrames = in.readInt();
        in.close();
        footerPointer = out.length() - 12;
    }
    if (numFrames == 0) {
        nextSequenceNumber = 0;
    }
}
Also used : RandomAccessInputStream(loci.common.RandomAccessInputStream) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) IndexColorModel(java.awt.image.IndexColorModel)

Example 19 with MetadataRetrieve

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

the class EPSWriter method writeHeader.

// -- Helper methods --
private void writeHeader() throws IOException {
    MetadataRetrieve r = getMetadataRetrieve();
    int width = r.getPixelsSizeX(series).getValue().intValue();
    int height = r.getPixelsSizeY(series).getValue().intValue();
    int nChannels = getSamplesPerPixel();
    out.writeBytes("%!PS-Adobe-2.0 EPSF-1.2\n");
    out.writeBytes("%%Title: " + currentId + "\n");
    out.writeBytes("%%Creator: " + FormatTools.CREATOR + "\n");
    out.writeBytes("%%Pages: 1\n");
    out.writeBytes("%%BoundingBox: 0 0 " + width + " " + height + "\n");
    out.writeBytes("%%EndComments\n\n");
    out.writeBytes("/ld {load def} bind def\n");
    out.writeBytes("/s /stroke ld /f /fill ld /m /moveto ld /l " + "/lineto ld /c /curveto ld /rgb {255 div 3 1 roll 255 div 3 1 " + "roll 255 div 3 1 roll setrgbcolor} def\n");
    out.writeBytes("0 0 translate\n");
    out.writeBytes(((float) width) + " " + ((float) height) + " scale\n");
    out.writeBytes("/picstr 40 string def\n");
    out.writeBytes(width + " " + height + " 8 [" + width + " 0 0 " + (-1 * height) + " 0 " + height + "] {currentfile picstr readhexstring pop} ");
    if (nChannels == 1) {
        out.writeBytes("image\n");
    } else {
        out.writeBytes("false 3 colorimage\n");
    }
    planeOffset = out.getFilePointer();
}
Also used : MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 20 with MetadataRetrieve

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

the class OMEXMLWriter method compress.

// -- Helper methods --
/**
 * Compress the given byte array using the current codec.
 * The compressed data is then base64-encoded.
 */
private byte[] compress(byte[] b) throws FormatException, IOException {
    MetadataRetrieve r = getMetadataRetrieve();
    String type = r.getPixelsType(series).toString();
    int pixelType = FormatTools.pixelTypeFromString(type);
    int bytes = FormatTools.getBytesPerPixel(pixelType);
    CodecOptions options = new CodecOptions();
    options.width = r.getPixelsSizeX(series).getValue().intValue();
    options.height = r.getPixelsSizeY(series).getValue().intValue();
    options.channels = 1;
    options.interleaved = false;
    options.signed = FormatTools.isSigned(pixelType);
    boolean littleEndian = false;
    if (r.getPixelsBigEndian(series) != null) {
        littleEndian = !r.getPixelsBigEndian(series).booleanValue();
    } else if (r.getPixelsBinDataCount(series) == 0) {
        littleEndian = !r.getPixelsBinDataBigEndian(series, 0).booleanValue();
    }
    options.littleEndian = littleEndian;
    options.bitsPerSample = bytes * 8;
    if (compression.equals("J2K")) {
        b = new JPEG2000Codec().compress(b, options);
    } else if (compression.equals("JPEG")) {
        b = new JPEGCodec().compress(b, options);
    } else if (compression.equals("zlib")) {
        b = new ZlibCodec().compress(b, options);
    }
    return new Base64Codec().compress(b, options);
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) Base64Codec(loci.formats.codec.Base64Codec) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) JPEGCodec(loci.formats.codec.JPEGCodec)

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