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);
}
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);
}
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;
}
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;
}
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);
}
Aggregations