Changeset 3498


Ignore:
Timestamp:
12/26/07 14:50:29 (12 years ago)
Author:
melissa
Message:

Rewrote LegacyND2Reader to use our own native bindings.

Location:
trunk
Files:
3 added
1 deleted
1 edited

Legend:

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

    r3426 r3498  
    2929import loci.formats.*; 
    3030 
     31/** 
     32 * LegacyND2Reader is a file format reader for Nikon ND2 files that uses 
     33 * the Nikon ND2 SDK - it is only usable on Windows machines. 
     34 * 
     35 * <dl><dt><b>Source code:</b></dt> 
     36 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/loci/formats/in/LegacyND2Reader.java">Trac</a>, 
     37 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/formats/in/LegacyND2Reader.java">SVN</a></dd></dl> 
     38 */ 
    3139public class LegacyND2Reader extends FormatReader { 
    3240 
    33   private static boolean noSDK = false; 
     41  // -- Constants -- 
    3442 
    35   private static ReflectedUniverse r = createReflectedUniverse(); 
    36   private static ReflectedUniverse createReflectedUniverse() { 
    37     r = null; 
     43  /** Modality types. */ 
     44  private static final int WIDE_FIELD = 0; 
     45  private static final int BRIGHT_FIELD = 1; 
     46  private static final int LASER_SCAN_CONFOCAL = 2; 
     47  private static final int SPIN_DISK_CONFOCAL = 3; 
     48  private static final int SWEPT_FIELD_CONFOCAL = 4; 
     49  private static final int MULTI_PHOTON = 5; 
     50 
     51  // -- Static initializers -- 
     52 
     53  private static boolean libraryFound = true; 
     54 
     55  static { 
    3856    try { 
    39       r = new ReflectedUniverse(); 
    40       r.exec("import ND_to_Image6D"); 
    41       System.loadLibrary("Nd2SdkWrapperI6D"); 
    42     } 
    43     catch (ReflectException e) { 
    44       if (debug) LogTools.trace(e); 
    45       noSDK = true; 
     57      System.loadLibrary("LegacyND2Reader"); 
    4658    } 
    4759    catch (UnsatisfiedLinkError e) { 
    4860      if (debug) LogTools.trace(e); 
    49       noSDK = true; 
     61      libraryFound = false; 
    5062    } 
    51     return r; 
    5263  } 
    5364 
    5465  // -- Constructor -- 
    5566 
    56   public LegacyND2Reader() { super("Nikon ND2 (Legacy)", "nd2"); } 
     67  public LegacyND2Reader() { 
     68    super("Nikon ND2 (Legacy)", new String[] {"jp2", "nd2"}); 
     69  } 
    5770 
    5871  // -- IFormatReader API methods -- 
     
    7285 
    7386    int[] zct = FormatTools.getZCTCoords(this, no); 
     87    getImage(buf, getSeries(), zct[0], zct[1], zct[2]); 
    7488 
    75     r.setVar("z", zct[0]); 
    76     r.setVar("c", zct[1]); 
    77     r.setVar("t", zct[2]); 
    78     r.setVar("series", series); 
    79  
    80     if (core.pixelType[series] == FormatTools.UINT8) { 
    81       r.setVar("pix", buf); 
    82       try { 
    83         r.exec("helper.copyNd2ImageByte(pix, c, z, t, series)"); 
    84       } 
    85       catch (ReflectException e) { 
    86         if (debug) LogTools.trace(e); 
    87       } 
    88     } 
    89     else { 
    90       int size = getRGBChannelCount() * core.sizeX[series] * core.sizeY[series]; 
    91       short[] s = new short[size]; 
    92       r.setVar("s", s); 
    93       try { 
    94         r.exec("helper.copyNd2ImageShort(s, c, z, t, series)"); 
    95       } 
    96       catch (ReflectException e) { 
    97         if (debug) LogTools.trace(e); 
    98       } 
    99       for (int j=0; j<s.length; j++) { 
    100         buf[j*2] = (byte) ((s[j] >> 8) & 0xff); 
    101         buf[j*2 + 1] = (byte) (s[j] & 0xff); 
    102       } 
    103     } 
    104  
    105     if (getRGBChannelCount() > 1) { 
    106       int b = FormatTools.getBytesPerPixel(core.pixelType[series]); 
    107       for (int i=0; i<buf.length; i+=b*getRGBChannelCount()) { 
    108         for (int j=0; j<b; j++) { 
    109           byte t = buf[i + j]; 
    110           buf[i + j] = buf[i + (getRGBChannelCount() - 1)*b + j]; 
    111           buf[i + (getRGBChannelCount() - 1)*b + j] = t; 
     89    if (core.rgb[series]) { 
     90      int bpc = FormatTools.getBytesPerPixel(core.pixelType[series]); 
     91      int bpp = core.sizeC[series] * bpc; 
     92      int line = core.sizeX[series] * bpp; 
     93      for (int y=0; y<core.sizeY[series]; y++) { 
     94        for (int x=0; x<core.sizeX[series]; x++) { 
     95          for (int b=0; b<bpc; b++) { 
     96            byte blue = buf[y*line + x*bpp + bpc*(core.sizeC[series] - 1) + b]; 
     97            buf[y*line + x*bpp + bpc*(core.sizeC[series] - 1) + b] = 
     98              buf[y*line + x*bpp + b]; 
     99            buf[y*line + x*bpp + b] = blue; 
     100          } 
    112101        } 
    113102      } 
     
    119108  // -- IFormatHandler API methods -- 
    120109 
    121   /* @see loci.formats.IFormatHandler#close() */ 
    122   public void close() throws IOException { 
    123     if (currentId != null) { 
    124       try { 
    125         r.exec("helper.closeNd2()"); 
    126       } 
    127       catch (ReflectException e) { } 
    128     } 
    129     super.close(); 
    130   } 
    131  
    132   /* @see loci.formats.IFormatHandler#isThisType(String, boolean) */ 
    133   public boolean isThisType(String name, boolean open) { 
    134     return !noSDK; 
     110  /* @see IFormatHandler#isThisType(String, boolean) */ 
     111  public boolean isThisType(String file, boolean open) { 
     112    if (!super.isThisType(file, open)) return false; 
     113    return libraryFound; 
    135114  } 
    136115 
     
    142121    super.initFile(id); 
    143122 
    144     String parent = 
    145       new Location(id).getAbsoluteFile().getParentFile().getAbsolutePath(); 
    146     String relPath = new Location(id).getPath(); 
    147     relPath = relPath.substring(relPath.lastIndexOf(File.separator) + 1); 
    148     if (!parent.endsWith(File.separator)) parent += File.separator; 
    149     r.setVar("parent", parent); 
    150     r.setVar("relPath", relPath); 
    151     try { 
    152       r.exec("helper = new ND_to_Image6D()"); 
    153       r.exec("success = helper.openNd2(parent, relPath)"); 
    154       boolean success = ((Boolean) r.getVar("success")).booleanValue(); 
    155       if (!success) { 
    156         throw new FormatException("Failed to open ND2 file."); 
     123    openFile(id); 
     124    int numSeries = getNumSeries(); 
     125    core = new CoreMetadata(numSeries); 
     126 
     127    for (int i=0; i<numSeries; i++) { 
     128      core.sizeX[i] = getWidth(i); 
     129      if (core.sizeX[i] % 2 != 0) core.sizeX[i]++; 
     130      core.sizeY[i] = getHeight(i); 
     131      core.sizeZ[i] = getZSlices(i); 
     132      core.sizeT[i] = getTFrames(i); 
     133      core.sizeC[i] = getChannels(i); 
     134      int bytes = getBytesPerPixel(i); 
     135      if (bytes % 3 == 0) { 
     136        core.sizeC[i] *= 3; 
     137        bytes /= 3; 
    157138      } 
    158  
    159       // populate core metadata 
    160  
    161       r.setVar("name", "points"); 
    162       r.exec("seriesCount = helper.getNd2Param(name)"); 
    163       core = new CoreMetadata(((Integer) r.getVar("seriesCount")).intValue()); 
    164       r.setVar("name", "width"); 
    165       r.exec("val = helper.getNd2Param(name)"); 
    166       Arrays.fill(core.sizeX, ((Integer) r.getVar("val")).intValue()); 
    167       if (core.sizeX[0] % 2 != 0) Arrays.fill(core.sizeX, core.sizeX[0] + 1); 
    168       r.setVar("name", "height"); 
    169       r.exec("val = helper.getNd2Param(name)"); 
    170       Arrays.fill(core.sizeY, ((Integer) r.getVar("val")).intValue()); 
    171       r.setVar("name", "zstacks"); 
    172       r.exec("val = helper.getNd2Param(name)"); 
    173       Arrays.fill(core.sizeZ, ((Integer) r.getVar("val")).intValue()); 
    174       r.setVar("name", "channels"); 
    175       r.exec("val = helper.getNd2Param(name)"); 
    176       Arrays.fill(core.sizeC, ((Integer) r.getVar("val")).intValue()); 
    177       r.setVar("name", "timeslices"); 
    178       r.exec("val = helper.getNd2Param(name)"); 
    179       Arrays.fill(core.sizeT, ((Integer) r.getVar("val")).intValue()); 
    180       r.setVar("name", "bpp"); 
    181       r.exec("val = helper.getNd2Param(name)"); 
    182       Arrays.fill(core.pixelType, ((Integer) r.getVar("val")).intValue() == 8 ? 
    183         FormatTools.UINT8 : FormatTools.UINT16); 
    184       r.setVar("name", "color"); 
    185       r.exec("val = helper.getNd2Param(name)"); 
    186       Arrays.fill(core.rgb, ((Integer) r.getVar("val")).intValue() > 1); 
    187       Arrays.fill(core.indexed, false); 
    188  
    189       if (core.rgb[0]) { 
    190         Arrays.fill(core.sizeC, ((Integer) r.getVar("val")).intValue()); 
     139      switch (bytes) { 
     140        case 1: 
     141          core.pixelType[i] = FormatTools.UINT8; 
     142          break; 
     143        case 2: 
     144          core.pixelType[i] = FormatTools.UINT16; 
     145          break; 
     146        case 4: 
     147          core.pixelType[i] = FormatTools.FLOAT; 
     148          break; 
    191149      } 
     150      core.rgb[i] = core.sizeC[i] > 1; 
     151      core.imageCount[i] = core.sizeZ[i] * core.sizeT[i]; 
    192152    } 
    193     catch (ReflectException e) { 
    194       throw new FormatException(e); 
    195     } 
    196  
    197     Arrays.fill(core.imageCount, core.sizeZ[0] * core.sizeT[0] * 
    198       (core.rgb[0] ? 1 : core.sizeC[0])); 
    199153    Arrays.fill(core.interleaved, true); 
     154    Arrays.fill(core.littleEndian, true); 
    200155    Arrays.fill(core.currentOrder, "XYCZT"); 
    201     Arrays.fill(core.littleEndian, false); 
     156    Arrays.fill(core.indexed, false); 
     157    Arrays.fill(core.falseColor, false); 
    202158 
    203159    MetadataStore store = getMetadataStore(); 
    204     for (int i=0; i<core.sizeX.length; i++) { 
    205       store.setImage("Series " + i, null, null, new Integer(i)); 
     160    for (int i=0; i<numSeries; i++) { 
     161      store.setImage(null, null, null, new Integer(i)); 
    206162    } 
    207163    FormatTools.populatePixels(store, this); 
    208     for (int i=0; i<core.sizeX.length; i++) { 
    209       for (int j=0; j<core.sizeC[i]; j++) { 
    210         store.setLogicalChannel(j, null, null, null, null, null, null, null, 
    211           null, null, null, null, null, null, null, null, null, null, null, 
    212           null, null, null, null, null, new Integer(i)); 
    213       } 
    214     } 
    215164  } 
    216165 
     166  // -- Native methods -- 
     167 
     168  public native void openFile(String filename); 
     169  public native int getNumSeries(); 
     170  public native int getWidth(int s); 
     171  public native int getHeight(int s); 
     172  public native int getZSlices(int s); 
     173  public native int getTFrames(int s); 
     174  public native int getChannels(int s); 
     175  public native int getBytesPerPixel(int s); 
     176  public native byte[] getImage(byte[] buf, int s, int z, int c, int t); 
     177  public native double getDX(int s, int z, int c, int t); 
     178  public native double getDY(int s, int z, int c, int t); 
     179  public native double getDZ(int s, int z, int c, int t); 
     180  public native double getDT(int s, int z, int c, int t); 
     181  public native double getWavelength(int s, int z, int c, int t); 
     182  public native String getChannelName(int s, int z, int c, int t); 
     183  public native double getMagnification(int s, int z, int c, int t); 
     184  public native double getNA(int s, int z, int c, int t); 
     185  public native String getObjectiveName(int s, int z, int c, int t); 
     186  public native int getModality(int s, int z, int c, int t); 
     187 
    217188} 
Note: See TracChangeset for help on using the changeset viewer.