Changeset 1777


Ignore:
Timestamp:
11/09/06 14:18:45 (14 years ago)
Author:
melissa
Message:
  • Added ND2Reader for JPEG 2000 and NIS-Elements ND2 files
  • Added "byte[][] getPixelBytes(BufferedImage, boolean)" to ImageTools; retrieves the pixels in an image as a 2D byte array of length [channels][width * height *bytes per pixel]
  • Added methods to DataTools that convert primitive types to an array of bytes.
Location:
trunk/loci/formats
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/DataTools.java

    r1603 r1777  
    159159 
    160160  /** Get a pair of bytes representing a short value. */ 
    161   public static byte[] shortToBytes(short v, boolean little) throws IOException 
    162   { 
     161  public static byte[] shortToBytes(short v, boolean little) { 
    163162    byte[] rtn = new byte[2]; 
    164163    if (little) { 
     
    172171    return rtn; 
    173172  } 
     173 
     174  /** Get four bytes representing an int value. */ 
     175  public static byte[] intToBytes(int v, boolean little) { 
     176    byte[] rtn = new byte[4]; 
     177    if (little) { 
     178      rtn[0] = (byte) (v & 0xff); 
     179      rtn[1] = (byte) ((v >> 8) & 0xff); 
     180      rtn[2] = (byte) ((v >> 16) & 0xff); 
     181      rtn[3] = (byte) ((v >> 24) & 0xff); 
     182    } 
     183    else { 
     184      rtn[0] = (byte) ((v >> 24) & 0xff); 
     185      rtn[1] = (byte) ((v >> 16) & 0xff); 
     186      rtn[2] = (byte) ((v >> 8) & 0xff); 
     187      rtn[3] = (byte) (v & 0xff); 
     188    } 
     189    return rtn; 
     190  } 
     191 
     192  /** Get four bytes representing a float value. */ 
     193  public static byte[] floatToBytes(float v, boolean little) { 
     194    int i = Float.floatToIntBits(v); 
     195    return intToBytes(i, little); 
     196  } 
     197 
     198  /** Get eight bytes representing a double value. */ 
     199  public static byte[] doubleToBytes(double v, boolean little) { 
     200    byte[] rtn = new byte[8]; 
     201    long l = Double.doubleToLongBits(v); 
     202     
     203    if (little) { 
     204      rtn[0] = (byte) (l & 0xff); 
     205      rtn[1] = (byte) ((l >> 8) & 0xff); 
     206      rtn[2] = (byte) ((l >> 16) & 0xff); 
     207      rtn[3] = (byte) ((l >> 24) & 0xff); 
     208      rtn[4] = (byte) ((l >> 32) & 0xff); 
     209      rtn[5] = (byte) ((l >> 40) & 0xff); 
     210      rtn[6] = (byte) ((l >> 48) & 0xff); 
     211      rtn[7] = (byte) ((l >> 56) & 0xff); 
     212    } 
     213    else { 
     214      rtn[0] = (byte) ((l >> 56) & 0xff); 
     215      rtn[1] = (byte) ((l >> 48) & 0xff); 
     216      rtn[2] = (byte) ((l >> 40) & 0xff); 
     217      rtn[3] = (byte) ((l >> 32) & 0xff); 
     218      rtn[4] = (byte) ((l >> 24) & 0xff); 
     219      rtn[5] = (byte) ((l >> 16) & 0xff); 
     220      rtn[6] = (byte) ((l >> 8) & 0xff); 
     221      rtn[7] = (byte) (l & 0xff); 
     222    } 
     223    return rtn; 
     224  }  
     225 
    174226 
    175227  // -- Word decoding -- 
  • trunk/loci/formats/ImageTools.java

    r1645 r1777  
    7474    } 
    7575    else if (bps == 4) { 
    76       float[] floats = new float[data.length / bps]; 
    77       for (int i=0; i<floats.length; i++) { 
    78         floats[i] = 
    79           Float.intBitsToFloat(DataTools.bytesToInt(data, i*4, 4, little)); 
    80       } 
    81       return makeImage(floats, w, h, c, interleaved); 
     76      int[] ints = new int[data.length / bps]; 
     77      for (int i=0; i<ints.length; i++) { 
     78        ints[i] = DataTools.bytesToInt(data, i*4, 4, little); 
     79      } 
     80      return makeImage(ints, w, h, c, interleaved); 
    8281    } 
    8382    else if (bps == 6) { 
     
    459458      } 
    460459    } 
    461     return getInts(makeType(image, DataBuffer.TYPE_INT)); 
     460     
     461    int w = image.getWidth(), h = image.getHeight(), c = r.getNumBands(); 
     462    int[][] samples = new int[c][w * h]; 
     463    for (int i=0; i<c; i++) r.getSamples(0, 0, w, h, i, samples[i]); 
     464    return samples; 
     465    //return getInts(makeType(image, DataBuffer.TYPE_INT)); 
    462466  } 
    463467 
     
    504508    return samples; 
    505509    //return getDoubles(makeType(image, DataBuffer.TYPE_DOUBLE)); 
     510  } 
     511 
     512  /** 
     513   * Return a 2D array of bytes representing the image.  If the transfer type 
     514   * is something other than DataBuffer.TYPE_BYTE, then each pixel value is 
     515   * converted to the appropriate number of bytes.  In other words, if we 
     516   * are given an image with 16-bit data, each channel of the resulting array 
     517   * will have width * height * 2 bytes. 
     518   */ 
     519  public static byte[][] getPixelBytes(BufferedImage img, boolean little) { 
     520    Object pixels = getPixels(img); 
     521 
     522    if (pixels instanceof byte[][]) return (byte[][]) pixels; 
     523    else if (pixels instanceof short[][]) { 
     524      short[][] s = (short[][]) pixels; 
     525      byte[][] b = new byte[s.length][s[0].length * 2]; 
     526      for (int i=0; i<b.length; i++) { 
     527        for (int j=0; j<s[0].length; j++) { 
     528          byte[] v = DataTools.shortToBytes(s[i][j], little); 
     529          b[i][j*2] = v[0]; 
     530          b[i][j*2 + 1] = v[1]; 
     531        } 
     532      } 
     533      return b; 
     534    } 
     535    else if (pixels instanceof int[][]) { 
     536      int[][] in = (int[][]) pixels; 
     537      byte[][] b = new byte[in.length][in[0].length * 4]; 
     538      for (int i=0; i<b.length; i++) { 
     539        for (int j=0; j<in[0].length; j++) { 
     540          byte[] v = DataTools.intToBytes(in[i][j], little); 
     541          b[i][j*4] = v[0]; 
     542          b[i][j*4 + 1] = v[1]; 
     543          b[i][j*4 + 2] = v[2]; 
     544          b[i][j*4 + 3] = v[3]; 
     545        } 
     546      } 
     547      return b; 
     548    } 
     549    else if (pixels instanceof float[][]) { 
     550      float[][] in = (float[][]) pixels; 
     551      byte[][] b = new byte[in.length][in[0].length * 4]; 
     552      for (int i=0; i<b.length; i++) { 
     553        for (int j=0; j<in[0].length; j++) { 
     554          byte[] v = DataTools.floatToBytes(in[i][j], little); 
     555          b[i][j*4] = v[0]; 
     556          b[i][j*4 + 1] = v[1]; 
     557          b[i][j*4 + 2] = v[2]; 
     558          b[i][j*4 + 3] = v[3]; 
     559        } 
     560      } 
     561      return b; 
     562    } 
     563    else if (pixels instanceof double[][]) { 
     564 
     565    } 
     566    return null; 
    506567  } 
    507568 
  • trunk/loci/formats/readers.txt

    r1712 r1777  
    5050loci.formats.in.PerkinElmerReader # csv, htm, tim, zpo [TIFF+companion] 
    5151 
    52 # POI readers 
     52# readers requiring third-party libraries 
    5353loci.formats.in.ZeissZVIReader    # zvi [POIFS] 
    5454loci.formats.in.OIBReader         # oib [POIFS] 
    5555loci.formats.in.IPWReader         # ipw [POIFS] 
     56loci.formats.in.ND2Reader         # nd2, jp2 [JJ2000] 
    5657 
    5758# TIFF-based readers with unique file extensions 
  • trunk/loci/formats/todo.txt

    r1706 r1777  
    99Implement readers for other file formats: 
    1010* Anything listed online: http://www.loci.wisc.edu/ome/formats.html 
    11 * JPEG-2000 formats: .j2k, .jbig, .jp2, .jpc, etc. 
    1211* MNG (low) 
    1312 
Note: See TracChangeset for help on using the changeset viewer.