Changeset 6862


Ignore:
Timestamp:
09/01/10 11:58:48 (9 years ago)
Author:
melissa
Message:

Simplified search for BinData offsets.

Location:
trunk/components/bio-formats/src/loci/formats/in
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/bio-formats/src/loci/formats/in/BaseTiffReader.java

    r6791 r6862  
    420420      // set the X and Y pixel dimensions 
    421421 
    422       int resolutionUnit = firstIFD.getIFDIntValue(IFD.RESOLUTION_UNIT); 
    423       TiffRational xResolution = firstIFD.getIFDRationalValue(IFD.X_RESOLUTION); 
    424       TiffRational yResolution = firstIFD.getIFDRationalValue(IFD.Y_RESOLUTION); 
    425       double pixX = xResolution == null ? 0 : 1 / xResolution.doubleValue(); 
    426       double pixY = yResolution == null ? 0 : 1 / yResolution.doubleValue(); 
    427  
    428       switch (resolutionUnit) { 
    429         case 2: 
    430           // resolution is expressed in pixels per inch 
    431           pixX *= 25400; 
    432           pixY *= 25400; 
    433           break; 
    434         case 3: 
    435           // resolution is expressed in pixels per centimeter 
    436           pixX *= 10000; 
    437           pixY *= 10000; 
    438           break; 
    439       } 
     422      double pixX = firstIFD.getXResolution(); 
     423      double pixY = firstIFD.getYResolution(); 
    440424 
    441425      store.setPixelsPhysicalSizeX(pixX, 0); 
  • trunk/components/bio-formats/src/loci/formats/in/OMEXMLReader.java

    r6861 r6862  
    5151 
    5252import org.xml.sax.Attributes; 
     53import org.xml.sax.Locator; 
    5354import org.xml.sax.helpers.DefaultHandler; 
    5455 
     
    8788 
    8889  // compression value and offset for each BinData element 
     90  private Vector<BinData> binData; 
    8991  private Vector<Long> binDataOffsets; 
    90   private Vector<Long> binDataLengths; 
    9192  private Vector<String> compression; 
    9293 
     
    136137 
    137138    long offset = binDataOffsets.get(index).longValue(); 
    138     long length = binDataLengths.get(index).longValue(); 
    139139    String compress = compression.get(index); 
    140140 
    141     in.seek(offset - 64); 
    142  
    143     // offset is approximate, we will need to skip a few bytes 
    144     boolean foundBinData = false; 
    145     byte[] check = new byte[8192]; 
    146     int overlap = 14; 
    147     int n = in.read(check, 0, overlap); 
    148  
    149     while (!foundBinData) { 
    150       int r = in.read(check, overlap, check.length - overlap); 
    151       if (r <= 0) throw new IOException("Cannot read from input stream"); 
    152       n += r; 
    153       String checkString = new String(check); 
    154       int pos = 0; 
    155       while (checkString.indexOf("BinData", pos) != -1 && 
    156         pos < checkString.length() && pos >= 0) 
    157       { 
    158         int idx = checkString.indexOf("BinData", pos) + 7; 
    159         pos = idx + 1; 
    160         boolean foundBeginning = false; 
    161         int openBracket = idx; 
    162         while (!foundBeginning && openBracket >= 1) { 
    163           openBracket--; 
    164           foundBeginning = checkString.charAt(openBracket) == '<'; 
    165         } 
    166         if (checkString.charAt(openBracket + 1) == '/') continue; 
    167         foundBinData = true; 
    168         in.seek(in.getFilePointer() - n + idx); 
    169         while (true) { 
    170           r = in.read(); 
    171           if (r <= 0) { 
    172             throw new IOException("EOF looking for terminating > character"); 
    173           } 
    174           if (r == '>') break; 
    175         } 
    176         if (foundBinData) break; 
    177       } 
    178       if (!foundBinData) { 
    179         System.arraycopy(check, check.length - overlap, check, 0, overlap); 
    180         n = overlap; 
    181       } 
    182     } 
    183  
    184     if (length < 0 && index + 1 < binDataOffsets.size()) { 
    185       length = binDataOffsets.get(index + 1).longValue() - offset; 
    186     } 
    187     else if (length < 0) { 
    188       length = in.length() - offset; 
    189     } 
     141    in.seek(offset); 
    190142 
    191143    int depth = FormatTools.getBytesPerPixel(getPixelType()); 
     
    243195      compression = null; 
    244196      binDataOffsets = null; 
    245       binDataLengths = null; 
     197      binData = null; 
    246198      omexml = null; 
    247199      hasSPW = false; 
     
    258210    in = new RandomAccessInputStream(id); 
    259211 
     212    binData = new Vector<BinData>(); 
    260213    binDataOffsets = new Vector<Long>(); 
    261     binDataLengths = new Vector<Long>(); 
    262214    compression = new Vector<String>(); 
    263215 
     
    270222    catch (IOException e) { 
    271223      throw new FormatException("Malformed OME-XML", e); 
     224    } 
     225 
     226    int lineNumber = 1; 
     227    for (BinData bin : binData) { 
     228      int line = bin.getRow(); 
     229      int col = bin.getColumn(); 
     230 
     231      while (lineNumber < line) { 
     232        in.readLine(); 
     233        lineNumber++; 
     234      } 
     235      binDataOffsets.add(in.getFilePointer() + col - 1); 
    272236    } 
    273237 
     
    349313  class OMEXMLHandler extends DefaultHandler { 
    350314    private StringBuffer xmlBuffer; 
    351     private long nextBinDataOffset; 
    352315    private String currentQName; 
    353     private boolean hadCharData; 
    354     private int binDataChars; 
     316    private Locator locator; 
    355317 
    356318    public OMEXMLHandler() { 
    357319      xmlBuffer = new StringBuffer(); 
    358       nextBinDataOffset = 0; 
    359320    } 
    360321 
    361322    public void characters(char[] ch, int start, int length) { 
    362       if (currentQName.indexOf("BinData") != -1) { 
    363         binDataChars += length; 
     323      if (currentQName.indexOf("BinData") < 0) { 
     324        xmlBuffer.append(new String(ch, start, length)); 
    364325      } 
    365       else xmlBuffer.append(new String(ch, start, length)); 
    366       nextBinDataOffset += length; 
    367       hadCharData = true; 
    368326    } 
    369327 
     
    372330      xmlBuffer.append(qName); 
    373331      xmlBuffer.append(">"); 
    374       if (qName.indexOf("BinData") >= 0) { 
    375         binDataOffsets.add(new Long(nextBinDataOffset - binDataChars)); 
    376       } 
    377  
    378       nextBinDataOffset += 2; 
    379       if (!qName.equals(currentQName) || hadCharData) { 
    380         nextBinDataOffset += qName.length(); 
    381       } 
    382     } 
    383  
    384     public void ignorableWhitespace(char[] ch, int start, int length) { 
    385       nextBinDataOffset += length; 
    386332    } 
    387333 
     
    389335      Attributes attributes) 
    390336    { 
    391       hadCharData = false; 
    392337      currentQName = qName; 
    393338 
     
    417362      } 
    418363      else { 
    419         String length = attributes.getValue("Length"); 
    420         if (length == null) { 
    421           binDataLengths.add(new Long(-1)); 
    422         } 
    423         else binDataLengths.add(new Long(length)); 
     364        binData.add( 
     365          new BinData(locator.getLineNumber(), locator.getColumnNumber())); 
    424366        String compress = attributes.getValue("Compression"); 
    425367        compression.add(compress == null ? "" : compress); 
    426         binDataChars = 0; 
    427368 
    428369        xmlBuffer.append("<"); 
     
    440381        xmlBuffer.append(">"); 
    441382      } 
    442  
    443       nextBinDataOffset += 2 + qName.length() + 4*attributes.getLength(); 
    444       for (int i=0; i<attributes.getLength(); i++) { 
    445         nextBinDataOffset += attributes.getQName(i).length(); 
    446         nextBinDataOffset += attributes.getValue(i).length(); 
    447       } 
    448383    } 
    449384 
     
    452387    } 
    453388 
     389    public void setDocumentLocator(Locator locator) { 
     390      this.locator = locator; 
     391    } 
     392  } 
     393 
     394  class BinData { 
     395    private int row; 
     396    private int column; 
     397 
     398    public BinData(int row, int column) { 
     399      this.row = row; 
     400      this.column = column; 
     401    } 
     402 
     403    public int getRow() { return row; } 
     404    public int getColumn() { return column; } 
    454405  } 
    455406 
Note: See TracChangeset for help on using the changeset viewer.