Search in sources :

Example 51 with MetadataRetrieve

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

the class LegacyQTWriter 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);
    MetadataRetrieve meta = getMetadataRetrieve();
    BufferedImage image = AWTImageTools.makeImage(buf, interleaved, meta, series);
    savePlane(no, image, x, y, w, h);
}
Also used : MetadataRetrieve(loci.formats.meta.MetadataRetrieve) BufferedImage(java.awt.image.BufferedImage)

Example 52 with MetadataRetrieve

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

the class OMEXMLWriter method setId.

// -- FormatWriter API methods --
/* @see loci.formats.FormatWriter#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    if (id.equals(currentId)) {
        return;
    }
    super.setId(id);
    MetadataRetrieve retrieve = getMetadataRetrieve();
    String xml;
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
        xml = service.getOMEXML(retrieve);
        OMEXMLMetadata noBin = service.createOMEXMLMetadata(xml);
        service.removeBinData(noBin);
        OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) noBin.getRoot();
        root.setCreator(FormatTools.CREATOR);
        xml = service.getOMEXML(noBin);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    xmlFragments = new ArrayList<String>();
    currentFragment = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    XMLTools.parseXML(xml, new OMEHandler());
    xmlFragments.add(currentFragment);
}
Also used : ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) MissingLibraryException(loci.formats.MissingLibraryException) DependencyException(loci.common.services.DependencyException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 53 with MetadataRetrieve

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

the class OMEXMLWriter method close.

/* @see loci.formats.IFormatHandler#close() */
@Override
public void close() throws IOException {
    if (out != null) {
        out.writeBytes(xmlFragments.get(xmlFragments.size() - 1));
    }
    if (getMetadataOptions().isValidate()) {
        try {
            MetadataRetrieve r = getMetadataRetrieve();
            String omexml = service.getOMEXML(r);
            service.validateOMEXML(omexml);
        } catch (ServiceException | NullPointerException e) {
            LOGGER.warn("OMEXMLService unable to create OME-XML metadata object.", e);
        }
    }
    super.close();
    xmlFragments = null;
    service = null;
}
Also used : ServiceException(loci.common.services.ServiceException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 54 with MetadataRetrieve

use of loci.formats.meta.MetadataRetrieve 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 55 with MetadataRetrieve

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

the class JPEG2000Writer method compressBuffer.

/**
 * Compresses the buffer.
 *
 * @param no the image index within the current file, starting from 0.
 * @param buf the byte array that represents the image tile.
 * @param x the X coordinate of the upper-left corner of the image tile.
 * @param y the Y coordinate of the upper-left corner of the image tile.
 * @param w the width (in pixels) of the image tile.
 * @param h the height (in pixels) of the image tile.
 * @throws FormatException if one of the parameters is invalid.
 * @throws IOException if there was a problem writing to the file.
 */
public byte[] compressBuffer(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    checkParams(no, buf, x, y, w, h);
    MetadataRetrieve retrieve = getMetadataRetrieve();
    boolean littleEndian = false;
    if (retrieve.getPixelsBigEndian(series) != null) {
        littleEndian = !retrieve.getPixelsBigEndian(series).booleanValue();
    } else if (retrieve.getPixelsBinDataCount(series) == 0) {
        littleEndian = !retrieve.getPixelsBinDataBigEndian(series, 0).booleanValue();
    }
    int bytesPerPixel = FormatTools.getBytesPerPixel(FormatTools.pixelTypeFromString(retrieve.getPixelsType(series).toString()));
    int nChannels = getSamplesPerPixel();
    // To be on the save-side
    if (options == null)
        options = JPEG2000CodecOptions.getDefaultOptions();
    options = new JPEG2000CodecOptions(options);
    options.width = w;
    options.height = h;
    options.channels = nChannels;
    options.bitsPerSample = bytesPerPixel * 8;
    options.littleEndian = littleEndian;
    options.interleaved = interleaved;
    options.lossless = compression == null || compression.equals(CompressionType.J2K.getCompression());
    options.colorModel = getColorModel();
    return new JPEG2000Codec().compress(buf, options);
}
Also used : JPEG2000Codec(loci.formats.codec.JPEG2000Codec) JPEG2000CodecOptions(loci.formats.codec.JPEG2000CodecOptions) 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