Changeset 1415


Ignore:
Timestamp:
09/05/06 15:41:55 (14 years ago)
Author:
curtis
Message:

Fix Openlab bugs causing slow initialization and slow isRGB.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/in/OpenlabReader.java

    r1408 r1415  
    3737 * @author Eric Kjellman egkjellman at wisc.edu 
    3838 * @author Curtis Rueden ctrueden at wisc.edu 
     39 * @author Melissa Linkert linkert at wisc.edu 
    3940 */ 
    4041public class OpenlabReader extends FormatReader { 
    4142 
     43  // -- Constants -- 
     44 
     45  /** Color type indicating grayscale image planes. */ 
     46  private static final int TYPE_GRAYSCALE = 1; 
     47 
     48  /** Color type indicating RGB image planes. */ 
     49  private static final int TYPE_RGB = 2; 
     50 
     51  /** Color type indicating a mix of RGB and grayscale image planes. */ 
     52  private static final int TYPE_MIXED = 3; 
     53 
    4254  // -- Static fields -- 
    4355 
     
    7082  /** First plane. */ 
    7183  private BufferedImage firstPlane; 
     84 
     85  /** 
     86   * The pattern of grayscale and RGB image planes. 
     87   * Should be one of: TYPE_GRAYSCALE, TYPE_RGB or TYPE_MIXED. 
     88   */ 
     89  private int colorType; 
    7290 
    7391  // -- Constructor -- 
     
    115133  public boolean isRGB(String id) throws FormatException, IOException { 
    116134    if (!id.equals(currentId)) initFile(id); 
    117     int type = checkType(); 
    118     return type == 2 || type == 3; 
     135    return colorType != TYPE_GRAYSCALE; 
    119136  } 
    120137 
     
    507524        metadata.put("Other", new String(other)); 
    508525      } 
    509  
    510       // Populate metadata store 
    511  
    512       // The metadata store we're working with. 
    513       MetadataStore store = getMetadataStore(currentId); 
    514  
    515       String type = "int8"; 
    516       if (metadata.get("BitDepth") != null) { 
    517         int bitDepth = ((Short) metadata.get("BitDepth")).intValue(); 
    518  
    519         if (bitDepth <= 8) type = "int8"; 
    520         else if (bitDepth <= 16) type = "int16"; 
    521         else type = "int32"; 
    522       } 
    523  
    524       store.setImage(null, metadata.get("Timestamp").toString(), null, null); 
    525  
    526       // FIXME: There is a loss of precision here as we are down-casting from 
    527       // double to float. 
    528       store.setStageLabel(null, (Float) metadata.get("XOrigin"), 
    529         (Float) metadata.get("YOrigin"), null, null); 
    530  
    531       // FIXME: There is a loss of precision here as we are down-casting from 
    532       // double to float. 
    533       store.setDimensions((Float) metadata.get("XScale"), 
    534         (Float) metadata.get("YScale"), null, null, null, null); 
    535  
    536       in.seek(offset); 
    537  
    538       // We need to poke at least one plane so that we can get "sizeX" and 
    539       // "sizeY" set. to populate the pixels set. 
    540       try { firstPlane = openImage(currentId, 0); } 
    541       catch (FormatException e) { e.printStackTrace(); } 
    542  
    543       store.setPixels(sizeX, sizeY, new Integer(numBlocks), 
    544         new Integer(getSizeC(currentId)), new Integer(1), type, 
    545         new Boolean(!little), "XYCZT", null); 
    546     } 
    547   } 
    548  
    549   /** 
    550    * Checks which type of Openlab file this is. 
    551    * 1 =&gt; all planes are greyscale. 
    552    * 2 =&gt; all planes are RGB. 
    553    * 3 =&gt; every 4th plane is RGB, remaining planes are greyscale. 
    554    */ 
    555   private int checkType() throws FormatException, IOException { 
    556     if (firstPlane == null) firstPlane = openImage(currentId, 0); 
     526    } 
     527 
     528    // Populate metadata store 
     529 
     530    // The metadata store we're working with. 
     531    MetadataStore store = getMetadataStore(currentId); 
     532 
     533    String type = "int8"; 
     534    if (metadata.get("BitDepth") != null) { 
     535      int bitDepth = ((Short) metadata.get("BitDepth")).intValue(); 
     536 
     537      if (bitDepth <= 8) type = "int8"; 
     538      else if (bitDepth <= 16) type = "int16"; 
     539      else type = "int32"; 
     540    } 
     541 
     542    store.setImage(null, metadata.get("Timestamp").toString(), null, null); 
     543 
     544    // FIXME: There is a loss of precision here as we are down-casting from 
     545    // double to float. 
     546    store.setStageLabel(null, (Float) metadata.get("XOrigin"), 
     547      (Float) metadata.get("YOrigin"), null, null); 
     548 
     549    // FIXME: There is a loss of precision here as we are down-casting from 
     550    // double to float. 
     551    store.setDimensions((Float) metadata.get("XScale"), 
     552      (Float) metadata.get("YScale"), null, null, null, null); 
     553 
     554    in.seek(offset); 
     555 
     556    // we need to poke at least one plane so that we can get sizeX, sizeY 
     557    // and colorType to populate the pixels set 
     558    try { firstPlane = openImage(currentId, 0); } 
     559    catch (FormatException e) { e.printStackTrace(); } 
     560 
     561    // determine colorType: 
     562    // 1 => all planes are greyscale 
     563    // 2 => all planes are RGB 
     564    // 3 => every 4th plane is RGB, remaining planes are greyscale 
     565    colorType = -1; 
    557566    WritableRaster r = firstPlane.getRaster(); 
    558567    int b = r.getNumBands(); 
    559     if (b == 3) return 2; 
    560  
    561     if (offsets.length <= 3) return 1; 
    562  
    563     in.seek(offsets[3] + 12); 
    564  
    565     int blockSize = in.readInt(); 
    566  
    567     // right now I'm gonna skip all the header info 
    568     // check to see whether or not this is v2 data 
    569     if (in.read() == 1) in.skipBytes(128); 
    570     in.skipBytes(169); 
    571     // read in the block of data 
    572     byte[] toRead = new byte[blockSize]; 
    573     in.read(toRead); 
    574  
    575     Dimension dim; 
    576     try { dim = pictReader.getDimensions(toRead); } 
    577     catch (Exception e) { dim = new Dimension(0, 0); } 
    578  
    579     int length = toRead.length; 
    580     int totalBlocks = -1; // set to allow loop to start. 
    581     int expectedBlock = 0; 
    582     int pos = 0; 
    583  
    584     sizeX = new Integer(dim.width); 
    585     sizeY = new Integer(dim.height); 
    586  
    587     if (expectedBlock != totalBlocks) { 
    588  
    589       while (pos + 7 < length && 
    590         (toRead[pos] != 73 || toRead[pos + 1] != 86 || 
    591         toRead[pos + 2] != 69 || toRead[pos + 3] != 65 || 
    592         toRead[pos + 4] != 100 || toRead[pos + 5] != 98 || 
    593         toRead[pos + 6] != 112 || toRead[pos + 7] != 113)) 
    594       { 
    595         pos++; 
    596       } 
    597  
    598       if (pos + 32 > length) { // The header is 32 bytes long. 
    599         if (expectedBlock == 0 && imageType[3] < 9) { 
    600           return 3; 
    601         } 
    602         return 1; 
    603       } 
    604       return 1; 
    605     } 
    606     return 1; 
     568    if (b == 3) colorType = TYPE_RGB; 
     569    else if (offsets.length <= 3) colorType = TYPE_GRAYSCALE; 
     570    else { 
     571      in.seek(offsets[3] + 12); 
     572 
     573      int blockSize = in.readInt(); 
     574 
     575      // right now I'm gonna skip all the header info 
     576      // check to see whether or not this is v2 data 
     577      if (in.read() == 1) in.skipBytes(128); 
     578      in.skipBytes(169); 
     579      // read in the block of data 
     580      byte[] toRead = new byte[blockSize]; 
     581      in.read(toRead); 
     582 
     583      Dimension dim; 
     584      try { dim = pictReader.getDimensions(toRead); } 
     585      catch (Exception e) { dim = new Dimension(0, 0); } 
     586 
     587      int length = toRead.length; 
     588      int totalBlocks = -1; // set to allow loop to start 
     589      int expectedBlock = 0; 
     590      int pos = 0; 
     591 
     592      sizeX = new Integer(dim.width); 
     593      sizeY = new Integer(dim.height); 
     594 
     595      if (expectedBlock != totalBlocks) { 
     596        while (pos + 7 < length && 
     597          (toRead[pos] != 73 || toRead[pos + 1] != 86 || 
     598          toRead[pos + 2] != 69 || toRead[pos + 3] != 65 || 
     599          toRead[pos + 4] != 100 || toRead[pos + 5] != 98 || 
     600          toRead[pos + 6] != 112 || toRead[pos + 7] != 113)) 
     601        { 
     602          pos++; 
     603        } 
     604 
     605        if (pos + 32 > length) { // the header is 32 bytes long 
     606          if (expectedBlock == 0 && imageType[3] < 9) colorType = TYPE_MIXED; 
     607        } 
     608      } 
     609      if (colorType < 0) colorType = TYPE_GRAYSCALE; 
     610    } 
     611 
     612    store.setPixels(sizeX, sizeY, new Integer(numBlocks), 
     613      new Integer(getSizeC(currentId)), new Integer(1), type, 
     614      new Boolean(!little), "XYCZT", null); 
    607615  } 
    608616 
Note: See TracChangeset for help on using the changeset viewer.