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