private void findTIFFs() throws IOException {
    Location baseFile = new Location(currentId).getAbsoluteFile();
    Location parent = baseFile.getParentFile();
    FilePattern pattern = new FilePattern(baseFile);
    String[] tiffs = pattern.getFiles();
    NumericComparator comparator = new NumericComparator();
    Arrays.sort(tiffs, comparator);

    Vector<String> validTIFFs = new Vector<String>();

    for (String tiff : tiffs) {
      if (!new Location(tiff).exists()) {
        String base = tiff.substring(tiff.lastIndexOf(File.separator) + 1);
        base = base.substring(0, base.indexOf("."));
        String suffix = tiff.substring(tiff.lastIndexOf("."));
        while (base.length() < 3) {
          base = "0" + base;
        }
        Location test = new Location(parent, base + suffix);
        if (test.exists()) {
          tiff = test.getAbsolutePath();
        } else continue;
      }
      validTIFFs.add(tiff);
    }

    files = validTIFFs.toArray(new String[validTIFFs.size()]);
  }
  /**
   * Return a list of all of the files in this directory. If 'noHiddenFiles' is set to true, then
   * hidden files are omitted.
   *
   * @see java.io.File#list()
   */
  public String[] list(boolean noHiddenFiles) {
    String key = getAbsolutePath() + Boolean.toString(noHiddenFiles);
    String[] result = null;
    if (cacheListings) {
      cleanStaleCacheEntries();
      ListingsResult listingsResult = fileListings.get(key);
      if (listingsResult != null) {
        return listingsResult.listing;
      }
    }
    ArrayList<String> files = new ArrayList<String>();
    if (isURL) {
      try {
        URLConnection c = url.openConnection();
        InputStream is = c.getInputStream();
        boolean foundEnd = false;

        while (!foundEnd) {
          byte[] b = new byte[is.available()];
          is.read(b);
          String s = new String(b, Constants.ENCODING);
          if (s.toLowerCase().indexOf("</html>") != -1) foundEnd = true;

          while (s.indexOf("a href") != -1) {
            int ndx = s.indexOf("a href") + 8;
            int idx = s.indexOf("\"", ndx);
            if (idx < 0) break;
            String f = s.substring(ndx, idx);
            if (files.size() > 0 && f.startsWith("/")) {
              return null;
            }
            s = s.substring(idx + 1);
            if (f.startsWith("?")) continue;
            Location check = new Location(getAbsolutePath(), f);
            if (check.exists() && (!noHiddenFiles || !check.isHidden())) {
              files.add(check.getName());
            }
          }
        }
      } catch (IOException e) {
        LOGGER.trace("Could not retrieve directory listing", e);
        return null;
      }
    } else {
      if (file == null) return null;
      String[] f = file.list();
      if (f == null) return null;
      String path = file.getAbsolutePath();
      for (String name : f) {
        if (!noHiddenFiles || !(name.startsWith(".") || new Location(path, name).isHidden())) {
          files.add(name);
        }
      }
    }
    result = files.toArray(new String[files.size()]);
    if (cacheListings) {
      fileListings.put(key, new ListingsResult(result, System.nanoTime()));
    }
    return result;
  }
Exemple #3
0
 /* Locate the experiment file given any file in set */
 private String locateExperimentFile(String id) throws FormatException, IOException {
   if (!checkSuffix(id, "exp")) {
     Location parent = new Location(id).getAbsoluteFile().getParentFile();
     if (checkSuffix(id, "tif")) parent = parent.getParentFile();
     Location expFile = new Location(parent, EXPERIMENT_FILE);
     if (expFile.exists()) {
       return expFile.getAbsolutePath();
     }
     throw new FormatException(
         "Could not find " + EXPERIMENT_FILE + " in " + parent.getAbsolutePath());
   }
   return id;
 }
  /* @see loci.formats.FormatReader#initFile(String) */
  public void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    tiffReader = new MinimalTiffReader();
    positions = new Vector<Position>();

    LOGGER.info("Reading metadata file");

    // find metadata.txt

    Location file = new Location(currentId).getAbsoluteFile();
    Location parentFile = file.getParentFile();
    String metadataFile = METADATA;
    if (file.exists()) {
      metadataFile = new Location(parentFile, METADATA).getAbsolutePath();

      // look for other positions

      if (parentFile.getName().indexOf("Pos_") >= 0) {
        parentFile = parentFile.getParentFile();
        String[] dirs = parentFile.list(true);
        Arrays.sort(dirs);
        for (String dir : dirs) {
          if (dir.indexOf("Pos_") >= 0) {
            Position pos = new Position();
            Location posDir = new Location(parentFile, dir);
            pos.metadataFile = new Location(posDir, METADATA).getAbsolutePath();
            positions.add(pos);
          }
        }
      } else {
        Position pos = new Position();
        pos.metadataFile = metadataFile;
        positions.add(pos);
      }
    }

    core = new CoreMetadata[positions.size()];

    for (int i = 0; i < positions.size(); i++) {
      core[i] = new CoreMetadata();
      setSeries(i);
      parsePosition(i);
    }
    setSeries(0);

    populateMetadata();
  }
  /* @see loci.formats.IFormatReader#isThisType(String, boolean) */
  public boolean isThisType(String name, boolean open) {
    if (super.isThisType(name, open)) return true;

    if (!checkSuffix(name, "tif") && open) {
      Location current = new Location(name).getAbsoluteFile();
      Location parent = current.getParentFile();

      String tiff = current.getName();
      int index = tiff.lastIndexOf(".");
      if (index >= 0) {
        tiff = tiff.substring(0, index);
      }
      tiff += ".tif";

      Location tiffFile = new Location(parent, tiff);
      return tiffFile.exists() && isThisType(tiffFile.getAbsolutePath(), open);
    }
    return false;
  }
  /* @see loci.formats.FormatReader#initFile(String) */
  protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);

    if (!checkSuffix(id, "vsi")) {
      Location current = new Location(id).getAbsoluteFile();
      Location parent = current.getParentFile();
      parent = parent.getParentFile();
      Location grandparent = parent.getParentFile();
      String vsi = parent.getName();
      vsi = vsi.substring(1, vsi.length() - 1) + ".vsi";

      Location vsiFile = new Location(grandparent, vsi);
      if (!vsiFile.exists()) {
        throw new FormatException("Could not find .vsi file.");
      } else {
        id = vsiFile.getAbsolutePath();
      }
    }

    parser = new TiffParser(id);
    ifds = parser.getIFDs();

    RandomAccessInputStream vsi = new RandomAccessInputStream(id);
    vsi.order(parser.getStream().isLittleEndian());
    vsi.seek(8);
    readTags(vsi);
    vsi.seek(parser.getStream().getFilePointer());

    vsi.skipBytes(273);

    ArrayList<String> files = new ArrayList<String>();
    Location file = new Location(id).getAbsoluteFile();

    Location dir = file.getParentFile();

    String name = file.getName();
    name = name.substring(0, name.lastIndexOf("."));

    Location pixelsDir = new Location(dir, "_" + name + "_");
    String[] stackDirs = pixelsDir.list(true);
    if (stackDirs != null) {
      for (String f : stackDirs) {
        Location stackDir = new Location(pixelsDir, f);
        String[] pixelsFiles = stackDir.list(true);
        if (pixelsFiles != null) {
          for (String pixelsFile : pixelsFiles) {
            if (checkSuffix(pixelsFile, "ets")) {
              files.add(new Location(stackDir, pixelsFile).getAbsolutePath());
            }
          }
        }
      }
    }
    files.add(file.getAbsolutePath());
    usedFiles = files.toArray(new String[files.size()]);

    core = new CoreMetadata[files.size() - 1 + ifds.size()];

    tileOffsets = new Long[files.size() - 1][];
    rows = new int[files.size() - 1];
    cols = new int[files.size() - 1];
    nDimensions = new int[core.length];

    IFDList exifs = parser.getExifIFDs();

    compressionType = new int[core.length];
    tileX = new int[core.length];
    tileY = new int[core.length];
    tileMap = new HashMap[core.length];

    for (int s = 0; s < core.length; s++) {
      core[s] = new CoreMetadata();
    }

    for (int s = 0; s < core.length; s++) {
      tileMap[s] = new HashMap<TileCoordinate, Integer>();

      if (s == 0 && !hasFlattenedResolutions()) {
        core[s].resolutionCount = ifds.size() + (files.size() == 1 ? 0 : 1);
      }

      if (s < files.size() - 1) {
        setSeries(s);
        parseETSFile(files.get(s), s);

        core[s].littleEndian = compressionType[s] == RAW;
        core[s].interleaved = core[s].rgb;

        if (s == 0 && exifs.size() > 0) {
          IFD exif = exifs.get(0);

          int newX = exif.getIFDIntValue(IFD.PIXEL_X_DIMENSION);
          int newY = exif.getIFDIntValue(IFD.PIXEL_Y_DIMENSION);

          if (getSizeX() > newX || getSizeY() > newY) {
            core[s].sizeX = newX;
            core[s].sizeY = newY;
          }
        }

        setSeries(0);
      } else {
        IFD ifd = ifds.get(s - files.size() + 1);
        PhotoInterp p = ifd.getPhotometricInterpretation();
        int samples = ifd.getSamplesPerPixel();
        core[s].rgb = samples > 1 || p == PhotoInterp.RGB;
        core[s].sizeX = (int) ifd.getImageWidth();
        core[s].sizeY = (int) ifd.getImageLength();
        core[s].sizeZ = 1;
        core[s].sizeT = 1;
        core[s].sizeC = core[s].rgb ? samples : 1;
        core[s].littleEndian = ifd.isLittleEndian();
        core[s].indexed =
            p == PhotoInterp.RGB_PALETTE
                && (get8BitLookupTable() != null || get16BitLookupTable() != null);
        core[s].imageCount = 1;
        core[s].pixelType = ifd.getPixelType();
        core[s].interleaved = false;
        core[s].falseColor = false;
        core[s].thumbnail = s != 0;
      }
      core[s].metadataComplete = true;
      core[s].dimensionOrder = "XYCZT";
    }
    vsi.close();

    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
  }