Example #1
0
  /* @see loci.formats.FormatReader#initFile(String) */
  protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    reader = new ImageReader();

    reader.setMetadataOptions(getMetadataOptions());
    reader.setMetadataFiltered(isMetadataFiltered());
    reader.setOriginalMetadataPopulated(isOriginalMetadataPopulated());
    reader.setNormalized(isNormalized());
    reader.setMetadataStore(getMetadataStore());

    // NB: We need a raw handle on the ZIP data itself, not a ZipHandle.
    IRandomAccess rawHandle = Location.getHandle(id, false, false);
    in = new RandomAccessInputStream(rawHandle, id);

    ZipInputStream zip = new ZipInputStream(in);
    while (true) {
      ZipEntry ze = zip.getNextEntry();
      if (ze == null) break;
      ZipHandle handle = new ZipHandle(id, ze);
      Location.mapFile(ze.getName(), handle);
      mappedFiles.add(ze.getName());
    }

    ZipHandle base = new ZipHandle(id);
    reader.setId(base.getEntryName());

    metadataStore = reader.getMetadataStore();
    core = reader.getCoreMetadata();
    metadata = reader.getGlobalMetadata();

    base.close();
  }
  public static void readPlane(
      int z, int c, int t, byte[] buf, int xStart, int yStart, int xCount, int yCount) {
    int no = reader.getIndex(z, c, t);

    try {
      reader.openBytes(no, buf, xStart, yStart, xCount, yCount);
    } catch (FormatException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #3
0
  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);
  }
Example #4
0
 /** @return true if reader being used is LeicaReader */
 public boolean isLeicaReader() {
   if (iReader.getReader() instanceof LeicaReader) {
     return true;
   } else {
     return false;
   }
 }
 public static void close() {
   try {
     reader.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  public static boolean canReadFile(String id) {
    boolean h = false;
    try {
      h = reader.isThisType(id);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return h;
  }
Example #7
0
 /* @see loci.formats.IFormatReader#close(boolean) */
 public void close(boolean fileOnly) throws IOException {
   super.close(fileOnly);
   if (reader != null) reader.close(fileOnly);
   if (!fileOnly) reader = null;
   for (String name : mappedFiles) {
     IRandomAccess handle = Location.getMappedFile(name);
     Location.mapFile(name, null);
     if (handle != null) {
       handle.close();
     }
   }
   mappedFiles.clear();
 }
  private void parseROIs(MetadataStore store) throws FormatException, IOException {
    if (MetadataTools.isOMEXMLMetadata(store) || MetadataTools.isOMEXMLRoot(store.getRoot())) {
      return;
    }

    String roiID = MetadataTools.createLSID("ROI", 0, 0);
    String maskID = MetadataTools.createLSID("Shape", 0, 0);

    store.setROIID(roiID, 0);
    store.setMaskID(maskID, 0, 0);

    String positionData = DataTools.readFile(roiDrawFile);
    String[] coordinates = positionData.split("[ ,]");

    double x1 = Double.parseDouble(coordinates[1]);
    double y1 = Double.parseDouble(coordinates[2]);
    double x2 = Double.parseDouble(coordinates[3]);
    double y2 = Double.parseDouble(coordinates[5]);

    store.setMaskX(x1, 0, 0);
    store.setMaskY(y1, 0, 0);
    store.setMaskWidth(x2 - x1, 0, 0);
    store.setMaskHeight(y2 - y1, 0, 0);

    store.setImageROIRef(roiID, 0, 0);

    ImageReader roiReader = new ImageReader();
    roiReader.setId(roiFile);
    byte[] roiPixels = roiReader.openBytes(0);
    roiReader.close();

    BitWriter bits = new BitWriter(roiPixels.length / 8);
    for (int i = 0; i < roiPixels.length; i++) {
      bits.write(roiPixels[i] == 0 ? 0 : 1, 1);
    }
    store.setMaskBinData(bits.toByteArray(), 0, 0);
  }
Example #9
0
  /** Constructs a new pattern reader. */
  public FilePatternReader() {
    super("File pattern", new String[] {"pattern"});

    ClassList<IFormatReader> classes = ImageReader.getDefaultReaderClasses();
    Class<? extends IFormatReader>[] classArray = classes.getClasses();
    ClassList<IFormatReader> newClasses = new ClassList<IFormatReader>(IFormatReader.class);
    for (Class<? extends IFormatReader> c : classArray) {
      if (!c.equals(FilePatternReader.class)) {
        newClasses.addClass(c);
      }
    }
    helper = new FileStitcher(new ImageReader(newClasses));

    suffixSufficient = true;
  }
Example #10
0
  /**
   * Wrapper for bio-formats
   *
   * @param config - ImportConfit
   */
  public OMEROWrapper(ImportConfig config) {
    if (config == null) {
      throw new IllegalArgumentException(
          "An ImportConfig must be instantitated \n "
              + "in order to properly configure all readers.");
    }

    this.config = config;
    try {
      String readersPath = config.readersPath.get();
      // Since we now use all readers apart from the ZipReader, just
      // initialize in this manner which helps us by not requiring
      // us to keep up with all changes in readers.txt and removes
      // the requirement for importer_readers.txt (See #2859).
      // Chris Allan <*****@*****.**> -- Fri 10 Sep 2010 17:24:49 BST
      ClassList<IFormatReader> readers = ImageReader.getDefaultReaderClasses();
      readers.removeClass(ZipReader.class);
      if (readersPath != null) {
        Class<?> k = getClass();
        if (new File(readersPath).exists()) {
          k = null;
        }
        readers = new ClassList<IFormatReader>(readersPath, IFormatReader.class, k);
      }
      iReader = new ImageReader(readers);
      filler = new ChannelFiller(iReader);
    } catch (IOException e) {
      throw new RuntimeException("Unable to load readers.txt.");
    }
    reader = separator = new ChannelSeparator(filler);

    // Force unreadable characters to be removed from metadata key/value pairs
    iReader.setMetadataFiltered(true);
    filler.setMetadataFiltered(true);
    separator.setMetadataFiltered(true);
  };
Example #11
0
 /**
  * Obtains an object which represents a given sub-image of a plane within the file.
  *
  * @param id The path to the file.
  * @param planeNumber The plane or section within the file to obtain.
  * @param buf Pre-allocated buffer which has a <i>length</i> that can fit the byte count of the
  *     sub-image.
  * @param x X coordinate of the upper-left corner of the sub-image
  * @param y Y coordinate of the upper-left corner of the sub-image
  * @param w width of the sub-image
  * @param h height of the sub-image
  * @return an object which represents the sub-image of the plane.
  * @throws FormatException If there is an error parsing the file.
  * @throws IOException If there is an error reading from the file or acquiring permissions to read
  *     the file.
  */
 public PixelData openPlane2D(String id, int planeNumber, byte[] buf, int x, int y, int w, int h)
     throws FormatException, IOException {
   // FIXME: HACK! The ChannelSeparator isn't exactly what one would call
   // "complete" so we have to work around the fact that it still copies
   // all of the plane data (all three channels) from the file if the file
   // is RGB.
   ByteBuffer plane;
   if (iReader.isRGB() || isLeicaReader()) {
     // System.err.println("RGB, not using cached buffer.");
     byte[] bytePlane = openBytes(planeNumber, x, y, w, h);
     plane = ByteBuffer.wrap(bytePlane);
   } else {
     // System.err.println("Not RGB, using cached buffer.");
     plane = ByteBuffer.wrap(openBytes(planeNumber, buf, x, y, w, h));
   }
   plane.order(isLittleEndian() ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
   return new PixelData(FormatTools.getPixelTypeString(getPixelType()), plane);
 }
Example #12
0
 /* @see loci.formats.IFormatReader#get8BitLookupTable() */
 public byte[][] get8BitLookupTable() throws FormatException, IOException {
   return reader.get8BitLookupTable();
 }
Example #13
0
 /* (non-Javadoc)
  * @see loci.formats.ReaderWrapper#getMetadataOptions()
  */
 @Override
 public MetadataOptions getMetadataOptions() {
   return iReader.getMetadataOptions();
 }
Example #14
0
 /* (non-Javadoc)
  * @see loci.formats.ReaderWrapper#setMetadataOptions(loci.formats.in.MetadataOptions)
  */
 @Override
 public void setMetadataOptions(MetadataOptions options) {
   iReader.setMetadataOptions(options);
 }
  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;
  }
Example #16
0
 /* (non-Javadoc)
  * @see loci.formats.ReaderWrapper#getSupportedMetadataLevels()
  */
 @Override
 public Set<MetadataLevel> getSupportedMetadataLevels() {
   return iReader.getSupportedMetadataLevels();
 }
 public static int getBytesPerPixel() {
   return FormatTools.getBytesPerPixel(reader.getPixelType());
 }
Example #18
0
 /** @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int) */
 public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
     throws FormatException, IOException {
   return reader.openBytes(no, buf, x, y, w, h);
 }
Example #19
0
 /* @see loci.formats.IFormatReader#get16BitLookupTable() */
 public short[][] get16BitLookupTable() throws FormatException, IOException {
   return reader.get16BitLookupTable();
 }
Example #20
0
 /* @see loci.formats.IFormatReader#setGroupFiles(boolean) */
 public void setGroupFiles(boolean groupFiles) {
   super.setGroupFiles(groupFiles);
   if (reader != null) reader.setGroupFiles(groupFiles);
 }
 public static boolean getIsInterleaved() {
   return reader.isInterleaved();
 }
Example #22
0
  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 int getImageCount() {
   return reader.getImageCount();
 }