public static void dumpOMEXML(String path) throws FormatException, IOException, DependencyException, ServiceException { ServiceFactory serviceFactory = new ServiceFactory(); OMEXMLService omexmlService = serviceFactory.getInstance(OMEXMLService.class); IMetadata meta = omexmlService.createOMEXMLMetadata(); ImageReader r = new ImageReader(); r.setMetadataStore(meta); r.setOriginalMetadataPopulated(true); r.setId(path); r.close(); String xml = omexmlService.getOMEXML(meta); System.out.println(xml); }
public OmeisImporter(boolean stitchFiles) { stitch = stitchFiles; reader = new ChannelSeparator(new ChannelFiller()); if (stitch) reader = new FileStitcher(reader); try { ServiceFactory factory = new ServiceFactory(); OMEXMLService service = factory.getInstance(OMEXMLService.class); omexmlMeta = (AbstractOMEXMLMetadata) service.createOMEXMLMetadata(); } catch (DependencyException de) { } catch (ServiceException se) { } reader.setOriginalMetadataPopulated(true); reader.setMetadataStore(omexmlMeta); }
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]; // 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."); // 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)); // read data from byte array using ImageReader System.out.println(); System.out.println("Reading image data from memory..."); ServiceFactory factory = new ServiceFactory(); OMEXMLService service = factory.getInstance(OMEXMLService.class); IMetadata omeMeta = service.createOMEXMLMetadata(); ImageReader reader = new ImageReader(); reader.setMetadataStore(omeMeta); reader.setId(inId); 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); // map output id string to output byte array String outId = fileName + ".ome.tif"; ByteArrayHandle outputFile = new ByteArrayHandle(); Location.mapFile(outId, outputFile); // 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); 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(); // 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(); }
/* @see loci.formats.FormatReader#initFile(String) */ protected void initFile(String id) throws FormatException, IOException { if (noOME) { throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG); } super.initFile(id); in = new RandomAccessInputStream(id); binData = new Vector<BinData>(); binDataOffsets = new Vector<Long>(); compression = new Vector<String>(); DefaultHandler handler = new OMEXMLHandler(); try { RandomAccessInputStream s = new RandomAccessInputStream(id); XMLTools.parseXML(s, handler); s.close(); } catch (IOException e) { throw new FormatException("Malformed OME-XML", e); } int lineNumber = 1; for (BinData bin : binData) { int line = bin.getRow(); int col = bin.getColumn(); while (lineNumber < line) { in.readLine(); lineNumber++; } binDataOffsets.add(in.getFilePointer() + col - 1); } if (binDataOffsets.size() == 0) { throw new FormatException("Pixel data not found"); } LOGGER.info("Populating metadata"); OMEXMLMetadata omexmlMeta; OMEXMLService service; try { ServiceFactory factory = new ServiceFactory(); service = factory.getInstance(OMEXMLService.class); omexmlMeta = service.createOMEXMLMetadata(omexml); } catch (DependencyException de) { throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de); } catch (ServiceException se) { throw new FormatException(se); } hasSPW = omexmlMeta.getPlateCount() > 0; addGlobalMeta("Is SPW file", hasSPW); // TODO // Hashtable originalMetadata = omexmlMeta.getOriginalMetadata(); // if (originalMetadata != null) metadata = originalMetadata; int numDatasets = omexmlMeta.getImageCount(); core = new CoreMetadata[numDatasets]; int oldSeries = getSeries(); for (int i = 0; i < numDatasets; i++) { setSeries(i); core[i] = new CoreMetadata(); Integer w = omexmlMeta.getPixelsSizeX(i).getValue(); Integer h = omexmlMeta.getPixelsSizeY(i).getValue(); Integer t = omexmlMeta.getPixelsSizeT(i).getValue(); Integer z = omexmlMeta.getPixelsSizeZ(i).getValue(); Integer c = omexmlMeta.getPixelsSizeC(i).getValue(); if (w == null || h == null || t == null || z == null | c == null) { throw new FormatException("Image dimensions not found"); } Boolean endian = omexmlMeta.getPixelsBinDataBigEndian(i, 0); String pixType = omexmlMeta.getPixelsType(i).toString(); core[i].dimensionOrder = omexmlMeta.getPixelsDimensionOrder(i).toString(); core[i].sizeX = w.intValue(); core[i].sizeY = h.intValue(); core[i].sizeT = t.intValue(); core[i].sizeZ = z.intValue(); core[i].sizeC = c.intValue(); core[i].imageCount = getSizeZ() * getSizeC() * getSizeT(); core[i].littleEndian = endian == null ? false : !endian.booleanValue(); core[i].rgb = false; core[i].interleaved = false; core[i].indexed = false; core[i].falseColor = true; core[i].pixelType = FormatTools.pixelTypeFromString(pixType); core[i].orderCertain = true; } setSeries(oldSeries); // populate assigned metadata store with the // contents of the internal OME-XML metadata object MetadataStore store = getMetadataStore(); MetadataTools.populatePixels(store, this); service.convertMetadata(omexmlMeta, store); }