use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.
the class FileExportSPW method initializeMetadata.
/**
* Populate the minimum amount of metadata required to export a Plate.
*
* @param width
* the width (in pixels) of the image
* @param height
* the height (in pixels) of the image
* @param pixelType
* the pixel type of the image; @see loci.formats.FormatTools
*/
private IMetadata initializeMetadata(int width, int height, int pixelType) {
Exception exception = null;
try {
// create the OME-XML metadata storage object
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
OMEXMLMetadata meta = service.createOMEXMLMetadata();
meta.createRoot();
int plateIndex = 0;
// count of images
int series = 0;
int well = 0;
// Create Minimal 2x2 Plate
meta.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
meta.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
meta.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);
meta.setPlateRows(new PositiveInteger(rows), 0);
meta.setPlateColumns(new PositiveInteger(cols), 0);
meta.setPlateName("First test Plate", 0);
PositiveInteger pwidth = new PositiveInteger(width);
PositiveInteger pheight = new PositiveInteger(height);
char rowChar = 'A';
for (int row = 0; row < rows; row++) {
for (int column = 0; column < cols; column++) {
// set up well
String wellID = MetadataTools.createLSID("Well:", well);
meta.setWellID(wellID, plateIndex, well);
meta.setWellRow(new NonNegativeInteger(row), plateIndex, well);
meta.setWellColumn(new NonNegativeInteger(column), plateIndex, well);
for (int fov = 0; fov < fovPerWell; fov++) {
// Create Image
String imageName = rowChar + ":" + Integer.toString(column + 1) + ":FOV:" + Integer.toString(fov + 1);
String imageID = MetadataTools.createLSID("Image", well, fov);
meta.setImageID(imageID, series);
meta.setImageName(imageName, series);
String pixelsID = MetadataTools.createLSID("Pixels", row, well, fov);
meta.setPixelsID(pixelsID, series);
// specify that the pixel data is stored in big-endian format
// change 'TRUE' to 'FALSE' to specify little-endian format
meta.setPixelsBinDataBigEndian(Boolean.TRUE, series, 0);
// specify that the image is stored in ZCT order
meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, series);
// specify the pixel type of the image
meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), series);
// specify the dimensions of the image
meta.setPixelsSizeX(pwidth, series);
meta.setPixelsSizeY(pheight, series);
meta.setPixelsSizeZ(new PositiveInteger(1), series);
meta.setPixelsSizeC(new PositiveInteger(1), series);
meta.setPixelsSizeT(new PositiveInteger(sizeT), series);
// define each channel and specify the number of samples in the
// channel the number of samples is 3 for RGB images and 1 otherwise
String channelID = MetadataTools.createLSID("Channel", well, fov);
meta.setChannelID(channelID, series, 0);
meta.setChannelSamplesPerPixel(new PositiveInteger(1), series, 0);
// set sample
String wellSampleID = MetadataTools.createLSID("WellSample", well, fov);
meta.setWellSampleID(wellSampleID, 0, well, fov);
// NB sampleIndex here == series ie the image No
meta.setWellSampleIndex(new NonNegativeInteger(series), 0, well, fov);
meta.setWellSampleImageRef(imageID, 0, well, fov);
// add FLIM ModuloAlongT annotation if required
// CoreMetadata modlo = createModuloAnn();
// meta.addModuloAlong(meta, modlo, series);
series++;
}
// end of samples
well++;
}
rowChar++;
}
return meta;
} catch (DependencyException e) {
exception = e;
} catch (ServiceException e) {
exception = e;
} catch (EnumerationException e) {
exception = e;
}
System.err.println("Failed to populate OME-XML metadata object.");
exception.printStackTrace();
return null;
}
use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.
the class OverlappedTiledWriter method initialize.
/**
* Set up the file reader and writer, ensuring that the input file is
* associated with the reader and the output file is associated with the
* writer.
*
* @return true if the reader and writer were successfully set up, or false
* if an error occurred
* @throws DependencyException thrown if failed to create an OMEXMLService
* @throws IOException thrown if unable to setup input or output stream for reader or writer
* @throws FormatException thrown if invalid ID set for reader or writer or invalid tile size set
* @throws ServiceException thrown if unable to create OME-XML meta data
*/
private void initialize() throws DependencyException, FormatException, IOException, ServiceException {
// construct the object that stores OME-XML metadata
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata omexml = service.createOMEXMLMetadata();
// set up the reader and associate it with the input file
reader = new ImageReader();
reader.setMetadataStore(omexml);
reader.setId(inputFile);
// set up the writer and associate it with the output file
writer = new OMETiffWriter();
writer.setMetadataRetrieve(omexml);
writer.setInterleaved(reader.isInterleaved());
// set the tile size height and width for writing
this.tileSizeX = writer.setTileSizeX(tileSizeX);
this.tileSizeY = writer.setTileSizeY(tileSizeY);
writer.setId(outputFile);
}
use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.
the class writeMapAnnotationsExample method main.
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.out.println("Please specify an output file name.");
System.exit(1);
}
String id = args[0];
// create blank 512x512 image
System.out.println("Creating random image...");
int w = 512, h = 512, c = 1;
int pixelType = FormatTools.UINT16;
byte[] img = new byte[w * h * c * FormatTools.getBytesPerPixel(pixelType)];
// fill with random data
for (int i = 0; i < img.length; i++) img[i] = (byte) (256 * Math.random());
// Create MapPair Object and add to List
List<MapPair> mapList = new ArrayList<MapPair>();
mapList.add(new MapPair("Example Key", "Example Value"));
mapList.add(new MapPair("Bio-Formats Version", FormatTools.VERSION));
// create metadata object with minimum required metadata fields
System.out.println("Populating metadata...");
// add (minimum+Map)Annotations to the metadata object
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata metadata = service.createOMEXMLMetadata();
metadata.createRoot();
MetadataTools.populateMetadata(metadata, 0, null, false, "XYZCT", FormatTools.getPixelTypeString(pixelType), w, h, 1, c, 1, c);
int mapAnnotationIndex = 0;
int annotationRefIndex = 0;
String mapAnnotationID = MetadataTools.createLSID("MapAnnotation", 0, mapAnnotationIndex);
metadata.setMapAnnotationID(mapAnnotationID, mapAnnotationIndex);
metadata.setMapAnnotationValue(mapList, mapAnnotationIndex);
metadata.setMapAnnotationAnnotator("Example Map Annotation", mapAnnotationIndex);
metadata.setMapAnnotationDescription("Example Description", mapAnnotationIndex);
metadata.setMapAnnotationNamespace("Example NameSpace", mapAnnotationIndex);
metadata.setImageAnnotationRef(mapAnnotationID, 0, annotationRefIndex);
mapAnnotationIndex = 1;
annotationRefIndex = 1;
mapAnnotationID = MetadataTools.createLSID("MapAnnotation", 0, mapAnnotationIndex);
metadata.setMapAnnotationID(mapAnnotationID, mapAnnotationIndex);
metadata.setMapAnnotationValue(mapList, mapAnnotationIndex);
metadata.setMapAnnotationAnnotator("Example Map Annotation 1", mapAnnotationIndex);
metadata.setMapAnnotationDescription("Example Description 1", mapAnnotationIndex);
metadata.setMapAnnotationNamespace("Example NameSpace 1", mapAnnotationIndex);
metadata.setImageAnnotationRef(mapAnnotationID, 0, annotationRefIndex);
// Initialize writer and save file
ImageWriter writer = new ImageWriter();
writer.setMetadataRetrieve(metadata);
writer.setId(id);
writer.saveBytes(0, img);
writer.close();
System.out.println("Done.");
}
use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.
the class FileExport method initializeMetadata.
/**
* Populate the minimum amount of metadata required to export an image.
*
* @param width the width (in pixels) of the image
* @param height the height (in pixels) of the image
* @param pixelType the pixel type of the image; @see loci.formats.FormatTools
*/
private IMetadata initializeMetadata(int width, int height, int pixelType) {
Exception exception = null;
try {
// create the OME-XML metadata storage object
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata meta = service.createOMEXMLMetadata();
meta.createRoot();
// define each stack of images - this defines a single stack of images
meta.setImageID("Image:0", 0);
meta.setPixelsID("Pixels:0", 0);
// specify that the pixel data is stored in big-endian format
// change 'TRUE' to 'FALSE' to specify little-endian format
meta.setPixelsBinDataBigEndian(Boolean.TRUE, 0, 0);
// specify that the images are stored in ZCT order
meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, 0);
// specify that the pixel type of the images
meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), 0);
// specify the dimensions of the images
meta.setPixelsSizeX(new PositiveInteger(width), 0);
meta.setPixelsSizeY(new PositiveInteger(height), 0);
meta.setPixelsSizeZ(new PositiveInteger(1), 0);
meta.setPixelsSizeC(new PositiveInteger(1), 0);
meta.setPixelsSizeT(new PositiveInteger(1), 0);
// define each channel and specify the number of samples in the channel
// the number of samples is 3 for RGB images and 1 otherwise
meta.setChannelID("Channel:0:0", 0, 0);
meta.setChannelSamplesPerPixel(new PositiveInteger(1), 0, 0);
return meta;
} catch (DependencyException e) {
exception = e;
} catch (ServiceException e) {
exception = e;
} catch (EnumerationException e) {
exception = e;
}
System.err.println("Failed to populate OME-XML metadata object.");
exception.printStackTrace();
return null;
}
use of loci.common.services.ServiceFactory 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;
}
Aggregations