use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class ReadWriteInMemory method main.
public static void main(String[] args) throws DependencyException, FormatException, IOException, ServiceException {
if (args.length < 1) {
System.out.println("Please specify a (small) image file.");
System.exit(1);
}
String path = args[0];
/* file-read-start */
// read in entire file
System.out.println("Reading file into memory from disk...");
File inputFile = new File(path);
int fileSize = (int) inputFile.length();
DataInputStream in = new DataInputStream(new FileInputStream(inputFile));
byte[] inBytes = new byte[fileSize];
in.readFully(inBytes);
System.out.println(fileSize + " bytes read.");
/* file-read-end */
/* mapping-start */
// determine input file suffix
String fileName = inputFile.getName();
int dot = fileName.lastIndexOf(".");
String suffix = dot < 0 ? "" : fileName.substring(dot);
// map input id string to input byte array
String inId = "inBytes" + suffix;
Location.mapFile(inId, new ByteArrayHandle(inBytes));
/* mapping-end */
// read data from byte array using ImageReader
System.out.println();
System.out.println("Reading image data from memory...");
/* read-start */
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata omeMeta = service.createOMEXMLMetadata();
ImageReader reader = new ImageReader();
reader.setMetadataStore(omeMeta);
reader.setId(inId);
/* read-end */
int seriesCount = reader.getSeriesCount();
int imageCount = reader.getImageCount();
int sizeX = reader.getSizeX();
int sizeY = reader.getSizeY();
int sizeZ = reader.getSizeZ();
int sizeC = reader.getSizeC();
int sizeT = reader.getSizeT();
// output some details
System.out.println("Series count: " + seriesCount);
System.out.println("First series:");
System.out.println("\tImage count = " + imageCount);
System.out.println("\tSizeX = " + sizeX);
System.out.println("\tSizeY = " + sizeY);
System.out.println("\tSizeZ = " + sizeZ);
System.out.println("\tSizeC = " + sizeC);
System.out.println("\tSizeT = " + sizeT);
/* out—mapping-start */
// map output id string to output byte array
String outId = fileName + ".ome.tif";
ByteArrayHandle outputFile = new ByteArrayHandle();
Location.mapFile(outId, outputFile);
/* out—mapping-end */
/* write—init-start */
// write data to byte array using ImageWriter
System.out.println();
System.out.print("Writing planes to destination in memory: ");
ImageWriter writer = new ImageWriter();
writer.setMetadataRetrieve(omeMeta);
writer.setId(outId);
/* write—init-end */
/* write-start */
byte[] plane = null;
for (int i = 0; i < imageCount; i++) {
if (plane == null) {
// allow reader to allocate a new byte array
plane = reader.openBytes(i);
} else {
// reuse previously allocated byte array
reader.openBytes(i, plane);
}
writer.saveBytes(i, plane);
System.out.print(".");
}
reader.close();
writer.close();
System.out.println();
byte[] outBytes = outputFile.getBytes();
outputFile.close();
/* write-end */
/* flush-start */
// flush output byte array to disk
System.out.println();
System.out.println("Flushing image data to disk...");
File outFile = new File(fileName + ".ome.tif");
DataOutputStream out = new DataOutputStream(new FileOutputStream(outFile));
out.write(outBytes);
out.close();
/* flush-end */
}
use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class TiledReaderWriter 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.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class SewTiffs method main.
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: java SewTiffs base_name channel_num [time_count]");
System.exit(1);
}
String base = args[0];
int c = Integer.parseInt(args[1]);
int num;
if (args.length < 3) {
FilePattern fp = new FilePattern(new Location(base + "_C" + c + "_TP1.tiff"));
int[] count = fp.getCount();
num = count[count.length - 1];
} else
num = Integer.parseInt(args[2]);
System.out.println("Fixing " + base + "_C" + c + "_TP<1-" + num + ">.tiff");
TiffReader in = new TiffReader();
TiffWriter out = new TiffWriter();
String outId = base + "_C" + c + ".tiff";
System.out.println("Writing " + outId);
out.setId(outId);
System.out.print(" ");
boolean comment = false;
for (int t = 0; t < num; t++) {
String inId = base + "_C" + c + "_TP" + (t + 1) + ".tiff";
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata meta = service.createOMEXMLMetadata();
in.setMetadataStore(meta);
in.setId(inId);
out.setMetadataRetrieve(meta);
// read first image plane
byte[] image = in.openBytes(0);
in.close();
if (t == 0) {
// read first IFD
RandomAccessInputStream ras = new RandomAccessInputStream(inId);
TiffParser parser = new TiffParser(ras);
IFD ifd = parser.getFirstIFD();
ras.close();
// preserve TIFF comment
String desc = ifd.getComment();
if (desc != null) {
ifd = new IFD();
ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, desc);
comment = true;
out.saveBytes(t, image, ifd);
System.out.print(".");
continue;
}
}
// write image plane
out.saveBytes(t, image);
// update status
System.out.print(".");
if (t % DOTS == DOTS - 1) {
System.out.println(" " + (t + 1));
System.out.print(" ");
}
}
System.out.println();
if (comment)
System.out.println("OME-TIFF comment saved.");
else
System.out.println("No OME-TIFF comment found.");
}
use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class TiledExportExample method main.
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: java TiledExportExample <infile> <outfile>");
System.exit(1);
}
ImageReader reader = new ImageReader();
ImageWriter writer = new ImageWriter();
IMetadata meta;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
meta = service.createOMEXMLMetadata();
} catch (DependencyException exc) {
throw new FormatException("Could not create OME-XML store.", exc);
} catch (ServiceException exc) {
throw new FormatException("Could not create OME-XML store.", exc);
}
reader.setMetadataStore(meta);
reader.setId(args[0]);
writer.setMetadataRetrieve(meta);
writer.setId(args[1]);
for (int series = 0; series < reader.getSeriesCount(); series++) {
reader.setSeries(series);
writer.setSeries(series);
for (int image = 0; image < reader.getImageCount(); image++) {
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) {
int w = reader.getSizeX() / 2;
int h = reader.getSizeY() / 2;
int x = col * w;
int y = row * h;
/* debug */
System.out.println("[" + x + ", " + y + ", " + w + ", " + h + "]");
byte[] buf = reader.openBytes(image, x, y, w, h);
writer.saveBytes(image, buf, x, y, w, h);
}
}
}
}
reader.close();
writer.close();
}
use of loci.formats.meta.IMetadata in project bioformats by openmicroscopy.
the class WriteRGBMovie 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 20 blank 512x512 image planes
System.out.println("Creating random image planes...");
int w = 511, h = 507, numFrames = 20, numChannels = 3;
int pixelType = FormatTools.UINT8;
int bpp = FormatTools.getBytesPerPixel(pixelType);
int planeSize = h * w * numChannels * bpp;
byte[][] img = new byte[numFrames][planeSize];
// fill with random data
for (int t = 0; t < numFrames; t++) {
for (int i = 0; i < img[t].length; i += numChannels) {
for (int c = 0; c < numChannels; c++) {
img[t][i + c] = (byte) (256 * Math.random());
}
}
}
// create metadata object with required metadata fields
System.out.println("Populating metadata...");
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata meta = service.createOMEXMLMetadata();
MetadataTools.populateMetadata(meta, 0, null, false, "XYZCT", FormatTools.getPixelTypeString(pixelType), w, h, 1, numChannels, numFrames, numChannels);
// write image planes to disk
System.out.print("Writing planes to '" + id + "'");
IFormatWriter writer = new ImageWriter();
writer.setMetadataRetrieve(meta);
writer.setId(id);
for (int t = 0; t < numFrames; t++) {
System.out.print(".");
writer.saveBytes(t, img[t]);
}
writer.close();
System.out.println("Done.");
}
Aggregations