/**
  * 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);
 }
Exemple #2
0
  /** Thumbnail loading routine. */
  public void run() {
    while (loaderAlive) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException exc) {
        LOGGER.info("", exc);
      }

      try { // catch-all for unanticipated exceptions
        final String id = loadId;
        if (id == lastId) continue;
        if (id != null && lastId != null) {
          String[] files = reader.getUsedFiles();
          boolean found = false;
          for (int i = 0; i < files.length; i++) {
            if (id.equals(files[i])) {
              found = true;
              lastId = id;
              break;
            }
          }
          if (found) continue;
        }
        lastId = id;

        icon = null;
        iconText = id == null ? "" : "Reading...";
        formatText = resText = zctText = typeText = "";
        iconTip = id;
        formatTip = resTip = zctTip = typeTip = "";

        if (id == null) {
          SwingUtilities.invokeLater(refresher);
          continue;
        }

        try {
          reader.setId(id);
        } catch (FormatException exc) {
          LOGGER.debug("Failed to initialize {}", id, exc);
          boolean badFormat = exc.getMessage().startsWith("Unknown file format");
          iconText = "Unsupported " + (badFormat ? "format" : "file");
          formatText = resText = "";
          SwingUtilities.invokeLater(refresher);
          lastId = null;
          continue;
        } catch (IOException exc) {
          LOGGER.debug("Failed to initialize {}", id, exc);
          iconText = "Unsupported file";
          formatText = resText = "";
          SwingUtilities.invokeLater(refresher);
          lastId = null;
          continue;
        }
        if (id != loadId) {
          SwingUtilities.invokeLater(refresher);
          continue;
        }

        icon = id == null ? null : new ImageIcon(makeImage("Loading..."));
        iconText = "";
        String format = reader.getFormat();
        formatText = format;
        formatTip = format;
        resText = reader.getSizeX() + " x " + reader.getSizeY();
        zctText = reader.getSizeZ() + "Z x " + reader.getSizeT() + "T x " + reader.getSizeC() + "C";
        typeText =
            reader.getRGBChannelCount()
                + " x "
                + FormatTools.getPixelTypeString(reader.getPixelType());
        SwingUtilities.invokeLater(refresher);

        // open middle image thumbnail
        int z = reader.getSizeZ() / 2;
        int t = reader.getSizeT() / 2;
        int ndx = reader.getIndex(z, 0, t);
        BufferedImage thumb = null;
        try {
          thumb = reader.openThumbImage(ndx);
        } catch (FormatException exc) {
          LOGGER.debug("Failed to read thumbnail #{} from {}", new Object[] {ndx, id}, exc);
        } catch (IOException exc) {
          LOGGER.debug("Failed to read thumbnail #{} from {}", new Object[] {ndx, id}, exc);
        }
        icon = new ImageIcon(thumb == null ? makeImage("Failed") : thumb);
        iconText = "";

        SwingUtilities.invokeLater(refresher);
      } catch (Exception exc) {
        LOGGER.info("", exc);
        icon = null;
        iconText = "Thumbnail failure";
        formatText = resText = zctText = typeText = "";
        iconTip = loadId;
        formatTip = resTip = zctTip = typeTip = "";
        SwingUtilities.invokeLater(refresher);
      }
    }
  }