Changeset 3893


Ignore:
Timestamp:
04/08/08 09:08:01 (12 years ago)
Author:
melissa
Message:

Use multiple series to handle MNG stacks with varying pixel types.

File:
1 edited

Legend:

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

    r3854 r3893  
    2727import java.awt.image.BufferedImage; 
    2828import java.io.*; 
    29 import java.util.Vector; 
     29import java.util.*; 
    3030import javax.imageio.ImageIO; 
    3131import loci.formats.*; 
     
    4646 
    4747  /** Offsets to each plane. */ 
    48   private Vector offsets; 
     48  private Vector[] offsets; 
    4949 
    5050  /** Length (in bytes) of each plane. */ 
    51   private Vector lengths; 
     51  private Vector[] lengths; 
    5252 
    5353  // -- Constructor -- 
     
    7272    throws FormatException, IOException 
    7373  { 
    74     buf = ImageTools.getBytes(openImage(no, x, y, w, h), true, core.sizeC[0]); 
     74    buf = ImageTools.getBytes(openImage(no, x, y, w, h), true, 
     75      core.sizeC[series]); 
    7576    return buf; 
    7677  } 
     
    8384    FormatTools.checkPlaneNumber(this, no); 
    8485 
    85     long offset = ((Long) offsets.get(no)).longValue(); 
     86    long offset = ((Long) offsets[series].get(no)).longValue(); 
    8687    in.seek(offset); 
    87     long end = ((Long) lengths.get(no)).longValue(); 
     88    long end = ((Long) lengths[series].get(no)).longValue(); 
    8889    byte[] b = new byte[(int) (end - offset + 8)]; 
    8990    in.read(b, 8, b.length - 8); 
     
    119120    status("Verifying MNG format"); 
    120121 
    121     offsets = new Vector(); 
    122     lengths = new Vector(); 
     122    offsets = new Vector[1]; 
     123    lengths = new Vector[1]; 
     124 
     125    offsets[0] = new Vector(); 
     126    lengths[0] = new Vector(); 
    123127 
    124128    in.skipBytes(12); 
     
    147151 
    148152      if (code.equals("IHDR")) { 
    149         offsets.add(new Long(fp - 8)); 
    150         core.imageCount[0]++; 
     153        offsets[0].add(new Long(fp - 8)); 
    151154      } 
    152155      else if (code.equals("IEND")) { 
    153         lengths.add(new Long(fp + len + 4)); 
     156        lengths[0].add(new Long(fp + len + 4)); 
    154157      } 
    155158      else if (code.equals("LOOP")) { 
     
    176179    status("Populating metadata"); 
    177180 
    178     core.sizeZ[0] = 1; 
    179  
    180181    // easiest way to get image dimensions is by opening the first plane 
    181182 
    182     long offset = ((Long) offsets.get(0)).longValue(); 
    183     in.seek(offset); 
    184     long end = ((Long) lengths.get(0)).longValue(); 
    185     byte[] b = new byte[(int) (end - offset + 8)]; 
    186     in.read(b, 8, b.length - 8); 
    187     b[0] = (byte) 0x89; 
    188     b[1] = 0x50; 
    189     b[2] = 0x4e; 
    190     b[3] = 0x47; 
    191     b[4] = 0x0d; 
    192     b[5] = 0x0a; 
    193     b[6] = 0x1a; 
    194     b[7] = 0x0a; 
    195  
    196     BufferedImage img = ImageIO.read(new ByteArrayInputStream(b)); 
    197  
    198     core.sizeX[0] = img.getWidth(); 
    199     core.sizeY[0] = img.getHeight(); 
    200     core.sizeC[0] = img.getRaster().getNumBands(); 
    201  
    202     core.sizeT[0] = core.imageCount[0]; 
    203     core.currentOrder[0] = "XYCZT"; 
    204     core.pixelType[0] = FormatTools.UINT8; 
    205     core.rgb[0] = core.sizeC[0] > 1; 
    206     core.interleaved[0] = false; 
    207     core.littleEndian[0] = false; 
    208     core.metadataComplete[0] = true; 
    209     core.indexed[0] = false; 
    210     core.falseColor[0] = false; 
     183    Hashtable seriesOffsets = new Hashtable(); 
     184    Hashtable seriesLengths = new Hashtable(); 
     185 
     186    for (int i=0; i<offsets[0].size(); i++) { 
     187      long offset = ((Long) offsets[0].get(i)).longValue(); 
     188      in.seek(offset); 
     189      long end = ((Long) lengths[0].get(i)).longValue(); 
     190      byte[] b = new byte[(int) (end - offset + 8)]; 
     191      in.read(b, 8, b.length - 8); 
     192      b[0] = (byte) 0x89; 
     193      b[1] = 0x50; 
     194      b[2] = 0x4e; 
     195      b[3] = 0x47; 
     196      b[4] = 0x0d; 
     197      b[5] = 0x0a; 
     198      b[6] = 0x1a; 
     199      b[7] = 0x0a; 
     200 
     201      BufferedImage img = ImageIO.read(new ByteArrayInputStream(b)); 
     202      String data = img.getWidth() + "-" + img.getHeight() + "-" + 
     203        img.getRaster().getNumBands() + "-" + ImageTools.getPixelType(img); 
     204      Vector v = new Vector(); 
     205      if (seriesOffsets.containsKey(data)) { 
     206        v = (Vector) seriesOffsets.get(data); 
     207      } 
     208      v.add(new Long(offset)); 
     209      seriesOffsets.put(data, v); 
     210 
     211      v = new Vector(); 
     212      if (seriesLengths.containsKey(data)) { 
     213        v = (Vector) seriesLengths.get(data); 
     214      } 
     215      v.add(new Long(end)); 
     216      seriesLengths.put(data, v); 
     217    } 
     218 
     219    String[] keys = (String[]) seriesOffsets.keySet().toArray(new String[0]); 
     220    core = new CoreMetadata(keys.length); 
     221    offsets = new Vector[keys.length]; 
     222    lengths = new Vector[keys.length]; 
     223    for (int i=0; i<keys.length; i++) { 
     224      StringTokenizer st = new StringTokenizer(keys[i], "-"); 
     225      core.sizeX[i] = Integer.parseInt(st.nextToken()); 
     226      core.sizeY[i] = Integer.parseInt(st.nextToken()); 
     227      core.sizeC[i] = Integer.parseInt(st.nextToken()); 
     228      core.pixelType[i] = Integer.parseInt(st.nextToken()); 
     229      core.rgb[i] = core.sizeC[i] > 1; 
     230      offsets[i] = (Vector) seriesOffsets.get(keys[i]); 
     231      core.imageCount[i] = offsets[i].size(); 
     232      core.sizeT[i] = core.imageCount[i]; 
     233      lengths[i] = (Vector) seriesLengths.get(keys[i]); 
     234    } 
     235 
     236    Arrays.fill(core.sizeZ, 1); 
     237    Arrays.fill(core.currentOrder, "XYCZT"); 
     238    Arrays.fill(core.interleaved, false); 
     239    Arrays.fill(core.metadataComplete, true); 
     240    Arrays.fill(core.indexed, false); 
     241    Arrays.fill(core.littleEndian, false); 
     242    Arrays.fill(core.falseColor, false); 
    211243 
    212244    MetadataStore store = 
    213245      new FilterMetadata(getMetadataStore(), isMetadataFiltered()); 
    214     store.setImageName("", 0); 
    215     store.setImageCreationDate( 
    216       DataTools.convertDate(System.currentTimeMillis(), DataTools.UNIX), 0); 
     246    for (int i=0; i<core.sizeX.length; i++) { 
     247      store.setImageName("Series " + i, i); 
     248      store.setImageCreationDate( 
     249        DataTools.convertDate(System.currentTimeMillis(), DataTools.UNIX), i); 
     250    } 
    217251    MetadataTools.populatePixels(store, this); 
    218     // CTR CHECK 
    219 //    for (int i=0; i<core.sizeC[0]; i++) { 
    220 //      store.setLogicalChannel(i, null, null, null, null, null, null, null, null, 
    221 //        null, null, null, null, null, null, null, null, null, null, null, null, 
    222 //        null, null, null, null); 
    223 //    } 
    224252  } 
    225253 
Note: See TracChangeset for help on using the changeset viewer.