Skip to content

Instantly share code, notes, and snippets.

@amarkwirth
Created October 4, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amarkwirth/021297554f63fa1e8f89d4cfd3f4fea3 to your computer and use it in GitHub Desktop.
Save amarkwirth/021297554f63fa1e8f89d4cfd3f4fea3 to your computer and use it in GitHub Desktop.
minimumwriter
/*
* #%L
* OME Bio-Formats package for reading and converting biological file formats.
* %%
* Copyright (C) 2005 - 2017 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
import loci.common.services.ServiceFactory;
import loci.formats.out.OMETiffWriter;
import loci.formats.*;
import loci.formats.meta.IMetadata;
import loci.formats.services.OMEXMLService;
import ome.xml.model.enums.DimensionOrder;
import ome.xml.model.enums.PixelType;
import ome.xml.model.primitives.PositiveInteger;
import ome.xml.model.enums.*;
import ome.xml.model.primitives.*;
/**
* Demonstrates the minimum amount of metadata
* necessary to write out an image plane.
*/
public class MinimumWriter {
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 = 4, h = 4, 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) (255);
// create metadata object with minimum 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, c, 1, c);
meta.setBooleanAnnotationID("XXXX", 0);
meta.setBooleanAnnotationDescription("XXXX", 0);
meta.setBooleanAnnotationNamespace("XXXX", 0);
meta.setBooleanAnnotationValue(false, 0);
meta.setBooleanAnnotationAnnotator("XXXX", 0);
meta.setInstrumentID("fastSIM:1",0);
meta.setObjectiveID("fastSIM-Olympus:1", 0, 0);
meta.setObjectiveImmersion(Immersion.OIL, 0, 0);
meta.setObjectiveLensNA(1.45, 0, 0);
meta.setObjectiveManufacturer("Olympus", 0, 0);
meta.setObjectiveModel("TIRF", 0, 0);
meta.setObjectiveNominalMagnification(60., 0, 0);
// write image plane to disk
System.out.println("Writing image to '" + id + "'...");
IFormatWriter writer = new OMETiffWriter();
writer.setMetadataRetrieve(meta);
writer.setId(id);
writer.saveBytes(0, img);
writer.close();
System.out.println("Done.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment