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(); }
public static double[] readImageInfo(String id) { try { reader.setId(id); } catch (FormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // ORDERING: 0 little, 1 seriesCount, 2 pixelType, 3 bpp, 4 itkComponentType, 5 sizeX, // 6 sizeY, 7 sizeZ, 8 sizeT, 9 sizeC, 10 effSizeC, 11 rgbChannelCount, 12 imageCount // 13 physX, 14 physY, 15 physZ, 16 timeIncrement double[] returnValues = new double[17]; // return this and use SetByteOrderToLittleEndian or SetByteOrderToBigEndian in C++ land boolean little = reader.isLittleEndian(); if (little) { returnValues[0] = 1; } else { returnValues[0] = 0; } returnValues[1] = reader.getSeriesCount(); // return bpp and set an IOComponent based on it int pixelType = reader.getPixelType(); returnValues[2] = (double) pixelType; returnValues[3] = (double) FormatTools.getBytesPerPixel((int) returnValues[2]); // 0 UCHAR, 1 CHAR, 2 USHORT, 3 SHORT, 4 UINT, 5 INT, 6 FLOAT, 7 DOUBLE, 8 UNKNOWN if (pixelType == FormatTools.UINT8) returnValues[4] = (double) 0; else if (pixelType == FormatTools.INT8) returnValues[4] = (double) 1; else if (pixelType == FormatTools.UINT16) returnValues[4] = (double) 2; else if (pixelType == FormatTools.INT16) returnValues[4] = (double) 3; else if (pixelType == FormatTools.UINT32) returnValues[4] = (double) 4; else if (pixelType == FormatTools.INT32) returnValues[4] = (double) 5; else if (pixelType == FormatTools.FLOAT) returnValues[4] = (double) 6; else if (pixelType == FormatTools.DOUBLE) returnValues[4] = (double) 7; else returnValues[4] = (double) 8; // return these returnValues[5] = (double) reader.getSizeX(); returnValues[6] = (double) reader.getSizeY(); returnValues[7] = (double) reader.getSizeZ(); returnValues[8] = (double) reader.getSizeT(); returnValues[9] = (double) reader.getSizeC(); returnValues[10] = (double) reader.getEffectiveSizeC(); returnValues[11] = (double) reader.getRGBChannelCount(); returnValues[12] = (double) reader.getImageCount(); MetadataRetrieve retrieve = MetadataTools.asRetrieve(reader.getMetadataStore()); Double d = retrieve.getPixelsPhysicalSizeX(0).getValue(); double d2 = d == null ? 1.0 : d.doubleValue(); returnValues[13] = d2; d = retrieve.getPixelsPhysicalSizeY(0).getValue(); d2 = d == null ? 1.0 : d.doubleValue(); returnValues[14] = d2; d = retrieve.getPixelsPhysicalSizeZ(0).getValue(); d2 = d == null ? 1.0 : d.doubleValue(); returnValues[15] = d2; d = retrieve.getPixelsTimeIncrement(0); d2 = d == null ? 1.0 : d.doubleValue(); returnValues[16] = d2; return returnValues; }