Changeset 4804


Ignore:
Timestamp:
02/12/09 14:35:12 (11 years ago)
Author:
curtis
Message:

Fix up DataTools and AWTImageTools data manipulation and image construction
methods to use signed flag where appropriate. Throw IllegalArgumentException
for unsupported bytes-per-pixel/signedness combinations (int8 and uint32).

Location:
trunk/components
Files:
11 edited

Legend:

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

    r4688 r4804  
    3838 * use the {@link ImageTools} class. 
    3939 * 
    40  * Much code was stolen and adapted from DrLaszloJamf's posts at: 
    41  *   http://forum.java.sun.com/thread.jspa?threadID=522483 
     40 * Much code was stolen and adapted from 
     41 * <a href="http://forum.java.sun.com/thread.jspa?threadID=522483">DrLaszloJamf's posts</a> 
     42 * on the Java forums. 
    4243 * 
    4344 * <dl><dt><b>Source code:</b></dt> 
     
    6263  /** 
    6364   * Creates an image from the given single-channel unsigned byte data. 
     65   * 
     66   * @deprecated Use {@link #makeImage(byte[], int, int, boolean)} instead. 
     67   */ 
     68  public static BufferedImage makeImage(byte[] data, int w, int h) { 
     69    return makeImage(data, w, h, false); 
     70  } 
     71 
     72  /** 
     73   * Creates an image from the given single-channel byte data. 
     74   * 
    6475   * @param data Array containing image data. 
    6576   * @param w Width of image plane. 
    6677   * @param h Height of image plane. 
    67    */ 
    68   public static BufferedImage makeImage(byte[] data, int w, int h) { 
    69     return makeImage(new byte[][] {data}, w, h); 
     78   * @param signed Whether the byte values should be treated as signed 
     79   *   (-128 to 127) instead of unsigned (0 to 255). 
     80   *   <b>** Only unsigned byte data is supported for now. **</b> 
     81   */ 
     82  public static BufferedImage makeImage(byte[] data, 
     83    int w, int h, boolean signed) 
     84  { 
     85    return makeImage(new byte[][] {data}, w, h, signed); 
    7086  } 
    7187 
    7288  /** 
    7389   * Creates an image from the given single-channel unsigned short data. 
     90   * 
     91   * @deprecated Use {@link #makeImage(short[], int, int, boolean)} instead. 
     92   */ 
     93  public static BufferedImage makeImage(short[] data, int w, int h) { 
     94    return makeImage(data, w, h, false); 
     95  } 
     96 
     97  /** 
     98   * Creates an image from the given single-channel short data. 
     99   * 
    74100   * @param data Array containing image data. 
    75101   * @param w Width of image plane. 
    76102   * @param h Height of image plane. 
    77    */ 
    78   public static BufferedImage makeImage(short[] data, int w, int h) { 
    79     return makeImage(new short[][] {data}, w, h); 
     103   * @param signed Whether the short values should be treated as signed 
     104   *   (-32768 to 32767) instead of unsigned (0 to 65535). 
     105   */ 
     106  public static BufferedImage makeImage(short[] data, 
     107    int w, int h, boolean signed) 
     108  { 
     109    return makeImage(new short[][] {data}, w, h, signed); 
    80110  } 
    81111 
    82112  /** 
    83113   * Creates an image from the given single-channel signed int data. 
     114   * 
     115   * @deprecated Use {@link Use #makeImage(int[], int, int, boolean)} instead. 
     116   */ 
     117  public static BufferedImage makeImage(int[] data, int w, int h) { 
     118    return makeImage(data, w, h, true); 
     119  } 
     120 
     121  /** 
     122   * Creates an image from the given single-channel int data. 
     123   * 
    84124   * @param data Array containing image data. 
    85125   * @param w Width of image plane. 
    86126   * @param h Height of image plane. 
    87    */ 
    88   public static BufferedImage makeImage(int[] data, int w, int h) { 
    89     return makeImage(new int[][] {data}, w, h); 
     127   * @param signed Whether the int values should be treated as signed 
     128   *   (-2^31 to 2^31-1) instead of unsigned (0 to 2^32-1). 
     129   *   <b>** Only signed int data is supported for now. **</b> 
     130   */ 
     131  public static BufferedImage makeImage(int[] data, 
     132    int w, int h, boolean signed) 
     133  { 
     134    return makeImage(new int[][] {data}, w, h, signed); 
    90135  } 
    91136 
    92137  /** 
    93138   * Creates an image from the given single-channel float data. 
     139   * 
    94140   * @param data Array containing image data. 
    95141   * @param w Width of image plane. 
     
    102148  /** 
    103149   * Creates an image from the given single-channel double data. 
     150   * 
    104151   * @param data Array containing image data. 
    105152   * @param w Width of image plane. 
     
    114161  /** 
    115162   * Creates an image from the given unsigned byte data. 
     163   * 
     164   * @deprecated Use 
     165   *   {@link #makeImage(byte[], int, int, int, boolean, boolean)} instead. 
     166   */ 
     167  public static BufferedImage makeImage(byte[] data, 
     168    int w, int h, int c, boolean interleaved) 
     169  { 
     170    return makeImage(data, w, h, c, interleaved, false); 
     171  } 
     172 
     173  /** 
     174   * Creates an image from the given byte data. 
     175   * 
    116176   * @param data Array containing image data. 
    117177   * @param w Width of image plane. 
     
    122182   *   For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved, 
    123183   *   while "RRR...GGG...BBB..." is sequential. 
     184   * @param signed Whether the byte values should be treated as signed 
     185   *   (-128 to 127) instead of unsigned (0 to 255). 
     186   *   <b>** Only unsigned byte data is supported for now. **</b> 
    124187   */ 
    125188  public static BufferedImage makeImage(byte[] data, 
    126     int w, int h, int c, boolean interleaved) 
     189    int w, int h, int c, boolean interleaved, boolean signed) 
    127190  { 
    128191    if (c == 1) return makeImage(data, w, h); 
    129192    if (c > 2) return makeRGBImage(data, c, w, h, interleaved); 
    130     int dataType = DataBuffer.TYPE_BYTE; 
    131     DataBuffer buffer = new DataBufferByte(data, c * w * h); 
     193    int dataType; 
     194    DataBuffer buffer; 
     195    if (signed) { 
     196      // NB: No built-in data buffer type for signed byte data. 
     197      throw new IllegalArgumentException( 
     198        "Support for int8 pixel type (signed bytes) is unimplemented"); 
     199    } 
     200    else { 
     201      dataType = DataBuffer.TYPE_BYTE; 
     202      buffer = new DataBufferByte(data, c * w * h); 
     203    } 
    132204    return constructImage(c, dataType, w, h, interleaved, false, buffer); 
    133205  } 
     
    135207  /** 
    136208   * Creates an image from the given unsigned short data. 
     209   * 
     210   * @deprecated Use 
     211   *   {@link #makeImage(short[], int, int, int, boolean, boolean)} instead. 
     212   */ 
     213  public static BufferedImage makeImage(short[] data, 
     214    int w, int h, int c, boolean interleaved) 
     215  { 
     216    return makeImage(data, w, h, c, interleaved, false); 
     217  } 
     218 
     219  /** 
     220   * Creates an image from the given short data. 
     221   * 
    137222   * @param data Array containing image data. 
    138223   * @param w Width of image plane. 
     
    143228   *   For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved, 
    144229   *   while "RRR...GGG...BBB..." is sequential. 
     230   * @param signed Whether the short values should be treated as signed 
     231   *   (-32768 to 32767) instead of unsigned (0 to 65535). 
    145232   */ 
    146233  public static BufferedImage makeImage(short[] data, 
     234    int w, int h, int c, boolean interleaved, boolean signed) 
     235  { 
     236    if (c == 1) return makeImage(data, w, h); 
     237    int dataType; 
     238    DataBuffer buffer; 
     239    if (signed) { 
     240      dataType = DataBuffer.TYPE_SHORT; 
     241      buffer = new DataBufferShort(data, c * w * h); 
     242    } 
     243    else { 
     244      dataType = DataBuffer.TYPE_USHORT; 
     245      buffer = new DataBufferUShort(data, c * w * h); 
     246    } 
     247    return constructImage(c, dataType, w, h, interleaved, false, buffer); 
     248  } 
     249 
     250  /** 
     251   * Creates an image from the given signed int data. 
     252   * 
     253   * @deprecated Use 
     254   *   {@link #makeImage(int[], int, int, int, boolean, boolean)} instead. 
     255   */ 
     256  public static BufferedImage makeImage(int[] data, 
    147257    int w, int h, int c, boolean interleaved) 
    148258  { 
    149     if (c == 1) return makeImage(data, w, h); 
    150     int dataType = DataBuffer.TYPE_USHORT; 
    151     DataBuffer buffer = new DataBufferUShort(data, c * w * h); 
    152     return constructImage(c, dataType, w, h, interleaved, false, buffer); 
    153   } 
    154  
    155   /** 
    156    * Creates an image from the given signed int data. 
     259    return makeImage(data, w, h, c, interleaved, true); 
     260  } 
     261 
     262  /** 
     263   * Creates an image from the given int data. 
     264   * 
    157265   * @param data Array containing image data. 
    158266   * @param w Width of image plane. 
     
    163271   *   For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved, 
    164272   *   while "RRR...GGG...BBB..." is sequential. 
     273   * @param signed Whether the int values should be treated as signed 
     274   *   (-2^31 to 2^31-1) instead of unsigned (0 to 2^32-1). 
     275   *   <b>** Only signed int data is supported for now. **</b> 
    165276   */ 
    166277  public static BufferedImage makeImage(int[] data, 
    167     int w, int h, int c, boolean interleaved) 
     278    int w, int h, int c, boolean interleaved, boolean signed) 
    168279  { 
    169280    if (c == 1) return makeImage(data, w, h); 
    170     int dataType = DataBuffer.TYPE_INT; 
    171     DataBuffer buffer = new DataBufferInt(data, c * w * h); 
     281    int dataType; 
     282    DataBuffer buffer; 
     283    if (signed) { 
     284      dataType = DataBuffer.TYPE_INT; 
     285      buffer = new DataBufferInt(data, c * w * h); 
     286    } 
     287    else { 
     288      // NB: No built-in data buffer type for unsigned int data. 
     289      throw new IllegalArgumentException( 
     290        "Support for uint32 pixel type (unsigned ints) is unimplemented"); 
     291    } 
    172292    return constructImage(c, dataType, w, h, interleaved, false, buffer); 
    173293  } 
     
    175295  /** 
    176296   * Creates an image from the given float data. 
     297   * 
    177298   * @param data Array containing image data. 
    178299   * @param w Width of image plane. 
     
    195316  /** 
    196317   * Creates an image from the given double data. 
     318   * 
    197319   * @param data Array containing image data. 
    198320   * @param w Width of image plane. 
     
    217339  /** 
    218340   * Creates an image from the given unsigned byte data. 
     341   * 
     342   * @deprecated Use {@link #makeImage(byte[][], int, int, boolean)} instead. 
     343   */ 
     344  public static BufferedImage makeImage(byte[][] data, int w, int h) { 
     345    return makeImage(data, w, h, false); 
     346  } 
     347 
     348  /** 
     349   * Creates an image from the given byte data. 
     350   * 
    219351   * @param data Array containing image data. 
    220352   *   It is assumed that each channel corresponds to one element of the array. 
     
    222354   * @param w Width of image plane. 
    223355   * @param h Height of image plane. 
    224    */ 
    225   public static BufferedImage makeImage(byte[][] data, int w, int h) { 
     356   * @param signed Whether the byte values should be treated as signed 
     357   *   (-128 to 127) instead of unsigned (0 to 255). 
     358   *   <b>** Only unsigned byte data is supported for now. **</b> 
     359   */ 
     360  public static BufferedImage makeImage(byte[][] data, 
     361    int w, int h, boolean signed) 
     362  { 
    226363    if (data.length > 2) return makeRGBImage(data, w, h); 
    227     int dataType = DataBuffer.TYPE_BYTE; 
    228     DataBuffer buffer = new DataBufferByte(data, data[0].length); 
     364    int dataType; 
     365    DataBuffer buffer; 
     366    if (signed) { 
     367      // NB: No built-in data buffer type for signed byte data. 
     368      throw new IllegalArgumentException( 
     369        "Support for int8 pixel type (signed bytes) is unimplemented"); 
     370    } 
     371    else { 
     372      dataType = DataBuffer.TYPE_BYTE; 
     373      buffer = new DataBufferByte(data, data[0].length); 
     374    } 
    229375    return constructImage(data.length, dataType, w, h, false, true, buffer); 
    230376  } 
     
    232378  /** 
    233379   * Creates an image from the given unsigned short data. 
     380   * 
     381   * @deprecated Use {@link #makeImage(short[][], int, int, boolean)} instead. 
     382   */ 
     383  public static BufferedImage makeImage(short[][] data, int w, int h) { 
     384    return makeImage(data, w, h, false); 
     385  } 
     386 
     387  /** 
     388   * Creates an image from the given short data. 
     389   * 
    234390   * @param data Array containing image data. 
    235391   *   It is assumed that each channel corresponds to one element of the array. 
     
    237393   * @param w Width of image plane. 
    238394   * @param h Height of image plane. 
    239    */ 
    240   public static BufferedImage makeImage(short[][] data, int w, int h) { 
    241     int dataType = DataBuffer.TYPE_USHORT; 
    242     DataBuffer buffer = new DataBufferUShort(data, data[0].length); 
     395   * @param signed Whether the short values should be treated as signed 
     396   *   (-32768 to 32767) instead of unsigned (0 to 65535). 
     397   */ 
     398  public static BufferedImage makeImage(short[][] data, 
     399    int w, int h, boolean signed) 
     400  { 
     401    int dataType; 
     402    DataBuffer buffer; 
     403    if (signed) { 
     404      dataType = DataBuffer.TYPE_SHORT; 
     405      buffer = new DataBufferShort(data, data[0].length); 
     406    } 
     407    else { 
     408      dataType = DataBuffer.TYPE_USHORT; 
     409      buffer = new DataBufferUShort(data, data[0].length); 
     410    } 
    243411    return constructImage(data.length, dataType, w, h, false, true, buffer); 
    244412  } 
     
    246414  /** 
    247415   * Creates an image from the given signed int data. 
     416   * 
     417   * @deprecated Use {@link #makeImage(int[][], int, int, boolean)} instead. 
     418   */ 
     419  public static BufferedImage makeImage(int[][] data, int w, int h) { 
     420    return makeImage(data, w, h, true); 
     421  } 
     422 
     423  /** 
     424   * Creates an image from the given int data. 
     425   * 
    248426   * @param data Array containing image data. 
    249427   *   It is assumed that each channel corresponds to one element of the array. 
     
    251429   * @param w Width of image plane. 
    252430   * @param h Height of image plane. 
    253    */ 
    254   public static BufferedImage makeImage(int[][] data, int w, int h) { 
    255     int dataType = DataBuffer.TYPE_INT; 
    256     DataBuffer buffer = new DataBufferInt(data, data[0].length); 
     431   * @param signed Whether the int values should be treated as signed 
     432   *   (-2^31 to 2^31-1) instead of unsigned (0 to 2^32-1). 
     433   *   <b>** Only signed int data is supported for now. **</b> 
     434   */ 
     435  public static BufferedImage makeImage(int[][] data, 
     436    int w, int h, boolean signed) 
     437  { 
     438    int dataType; 
     439    DataBuffer buffer; 
     440    if (signed) { 
     441      dataType = DataBuffer.TYPE_INT; 
     442      buffer = new DataBufferInt(data, data[0].length); 
     443    } 
     444    else { 
     445      // NB: No built-in data buffer type for unsigned int data. 
     446      throw new IllegalArgumentException( 
     447        "Support for uint32 pixel type (unsigned ints) is unimplemented"); 
     448    } 
    257449    return constructImage(data.length, dataType, w, h, false, true, buffer); 
    258450  } 
     
    260452  /** 
    261453   * Creates an image from the given single-precision floating point data. 
     454   * 
    262455   * @param data Array containing image data. 
    263456   *   It is assumed that each channel corresponds to one element of the array. 
     
    274467  /** 
    275468   * Creates an image from the given double-precision floating point data. 
     469   * 
    276470   * @param data Array containing image data. 
    277471   *   It is assumed that each channel corresponds to one element of the array. 
     
    289483 
    290484  /** 
    291    * Creates an image from the given data, 
    292    * performing type conversions as necessary. 
    293    * @param data Array containing image data. 
    294    * @param w Width of image plane. 
    295    * @param h Height of image plane. 
    296    * @param c Number of channels. 
    297    * @param interleaved If set, the channels are assumed to be interleaved; 
    298    *   otherwise they are assumed to be sequential. 
    299    *   For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved, 
    300    *   while "RRR...GGG...BBB..." is sequential. 
    301    * @param bpp Denotes the number of bytes in the returned primitive type 
    302    *   (e.g. if bpp == 2, we should return an array of type short). 
    303    * @param little Whether byte array is in little-endian order. 
     485   * Creates an image from the given raw byte array, 
     486   * performing any necessary type conversions. 
     487   * 
     488   * @deprecated Use {@link #makeImage(byte[], int, int, int, 
     489   *    boolean, int, boolean, boolean, boolean)} instead. 
    304490   */ 
    305491  public static BufferedImage makeImage(byte[] data, int w, int h, int c, 
    306492    boolean interleaved, int bpp, boolean little) 
    307493  { 
    308     return makeImage(data, w, h, c, interleaved, bpp, false, little, false); 
    309   } 
    310  
    311   /** 
    312    * Creates an image from the given data, 
    313    * performing type conversions as necessary. 
     494    return makeImage(data, w, h, c, interleaved, bpp, false, little, bpp >= 4); 
     495  } 
     496 
     497  /** 
     498   * Creates an image from the given raw byte array, 
     499   * performing any necessary type conversions. 
     500   * 
    314501   * @param data Array containing image data. 
    315502   * @param w Width of image plane. 
     
    324511   * @param fp If set and bpp == 4 or bpp == 8, then return floats or doubles. 
    325512   * @param little Whether byte array is in little-endian order. 
     513   * @param signed Whether the data values should be treated as signed 
     514   *   instead of unsigned. 
    326515   */ 
    327516  public static BufferedImage makeImage(byte[] data, int w, int h, int c, 
     
    329518  { 
    330519    Object pixels = DataTools.makeDataArray(data, 
    331       bpp % 3 == 0 ? bpp / 3 : bpp, fp, little, signed); 
     520      bpp % 3 == 0 ? bpp / 3 : bpp, fp, little); 
    332521 
    333522    if (pixels instanceof byte[]) { 
    334       return makeImage((byte[]) pixels, w, h, c, interleaved); 
     523      return makeImage((byte[]) pixels, w, h, c, interleaved, signed); 
    335524    } 
    336525    else if (pixels instanceof short[]) { 
    337       return makeImage((short[]) pixels, w, h, c, interleaved); 
     526      return makeImage((short[]) pixels, w, h, c, interleaved, signed); 
    338527    } 
    339528    else if (pixels instanceof int[]) { 
    340       return makeImage((int[]) pixels, w, h, c, interleaved); 
     529      return makeImage((int[]) pixels, w, h, c, interleaved, signed); 
    341530    } 
    342531    else if (pixels instanceof float[]) { 
     
    350539 
    351540  /** 
    352    * Creates an image from the given data, 
    353    * performing type conversions as necessary. 
    354    * @param data Array containing image data, one channel per element. 
    355    * @param w Width of image plane. 
    356    * @param h Height of image plane. 
    357    * @param bpp Denotes the number of bytes in the returned primitive type 
    358    *   (e.g. if bpp == 2, we should return an array of type short). 
    359    * @param little Whether byte array is in little-endian order. 
     541   * Creates an image from the given raw byte array, 
     542   * performing any necessary type conversions. 
     543   * 
     544   * @deprecated Use {@link #makeImage(byte[][], int, int, int, 
     545   *    boolean, boolean, boolean)} instead. 
    360546   */ 
    361547  public static BufferedImage makeImage(byte[][] data, 
     
    366552 
    367553  /** 
    368    * Creates an image from the given data, 
    369    * performing type conversions as necessary. 
     554   * Creates an image from the given raw byte array, 
     555   * performing any necessary type conversions. 
     556   * 
    370557   * @param data Array containing image data, one channel per element. 
    371558   * @param w Width of image plane. 
     
    375562   * @param fp If set and bpp == 4 or bpp == 8, then return floats or doubles. 
    376563   * @param little Whether byte array is in little-endian order. 
     564   * @param signed Whether the data values should be treated as signed 
     565   *   instead of unsigned. 
    377566   */ 
    378567  public static BufferedImage makeImage(byte[][] data, 
     
    383572    for (int i=0; i<c; i++) { 
    384573      Object pixels = DataTools.makeDataArray(data[i], 
    385         bpp % 3 == 0 ? bpp / 3 : bpp, fp, little, signed); 
     574        bpp % 3 == 0 ? bpp / 3 : bpp, fp, little); 
    386575      if (pixels instanceof byte[]) { 
    387576        if (v == null) v = new byte[c][]; 
     
    405594      } 
    406595    } 
    407     if (v instanceof byte[][]) return makeImage((byte[][]) v, w, h); 
    408     else if (v instanceof short[][]) return makeImage((short[][]) v, w, h); 
    409     else if (v instanceof int[][]) return makeImage((int[][]) v, w, h); 
    410     else if (v instanceof float[][]) return makeImage((float[][]) v, w, h); 
    411     else if (v instanceof double[][]) return makeImage((double[][]) v, w, h); 
     596    if (v instanceof byte[][]) { 
     597      return makeImage((byte[][]) v, w, h, signed); 
     598    } 
     599    else if (v instanceof short[][]) { 
     600      return makeImage((short[][]) v, w, h, signed); 
     601    } 
     602    else if (v instanceof int[][]) { 
     603      return makeImage((int[][]) v, w, h, signed); 
     604    } 
     605    else if (v instanceof float[][]) { 
     606      return makeImage((float[][]) v, w, h); 
     607    } 
     608    else if (v instanceof double[][]) { 
     609      return makeImage((double[][]) v, w, h); 
     610    } 
    412611    return null; 
    413612  } 
     
    457656   * @param c Number of channels. 
    458657   * @param type One of the following types:<ul> 
    459    *   <li>FormatReader.INT8</li> 
     658   *   <li>FormatReader.INT8 <b>** unsupported for now **</b></li> 
    460659   *   <li>FormatReader.UINT8</li> 
    461660   *   <li>FormatReader.INT16</li> 
    462661   *   <li>FormatReader.UINT16</li> 
    463662   *   <li>FormatReader.INT32</li> 
    464    *   <li>FormatReader.UINT32</li> 
     663   *   <li>FormatReader.UINT32 <b>** unsupported for now **</b></li> 
    465664   *   <li>FormatReader.FLOAT</li> 
    466665   *   <li>FormatReader.DOUBLE</li> 
     
    470669    switch (type) { 
    471670      case FormatTools.INT8: 
     671        return makeImage(new byte[c][w * h], w, h, true); 
    472672      case FormatTools.UINT8: 
    473         return makeImage(new byte[c][w * h], w, h); 
     673        return makeImage(new byte[c][w * h], w, h, false); 
    474674      case FormatTools.INT16: 
     675        return makeImage(new short[c][w * h], w, h, true); 
    475676      case FormatTools.UINT16: 
    476         return makeImage(new short[c][w * h], w, h); 
     677        return makeImage(new short[c][w * h], w, h, false); 
    477678      case FormatTools.INT32: 
     679        return makeImage(new int[c][w * h], w, h, true); 
    478680      case FormatTools.UINT32: 
    479         return makeImage(new int[c][w * h], w, h); 
     681        return makeImage(new int[c][w * h], w, h, false); 
    480682      case FormatTools.FLOAT: 
    481683        return makeImage(new float[c][w * h], w, h); 
     
    559761  { 
    560762    int pixelType = r.getPixelType(); 
     763    boolean little = r.isLittleEndian(); 
     764    boolean normal = r.isNormalized(); 
     765    int rgbChanCount = r.getRGBChannelCount(); 
     766    boolean interleaved = r.isInterleaved(); 
     767    boolean indexed = r.isIndexed(); 
     768 
    561769    if (pixelType == FormatTools.FLOAT) { 
    562       float[] f = (float[]) DataTools.makeDataArray(buf, 4, true, 
    563         r.isLittleEndian(), false); 
    564       if (r.isNormalized()) f = DataTools.normalizeFloats(f); 
    565       return makeImage(f, w, h, r.getRGBChannelCount(), r.isInterleaved()); 
     770      float[] f = (float[]) DataTools.makeDataArray(buf, 4, true, little); 
     771      if (normal) f = DataTools.normalizeFloats(f); 
     772      return makeImage(f, w, h, rgbChanCount, interleaved); 
    566773    } 
    567774    else if (pixelType == FormatTools.DOUBLE) { 
    568       double[] d = (double[]) DataTools.makeDataArray(buf, 8, true, 
    569         r.isLittleEndian(), false); 
    570       if (r.isNormalized()) d = DataTools.normalizeDoubles(d); 
    571       return makeImage(d, w, h, r.getRGBChannelCount(), r.isInterleaved()); 
    572     } 
    573  
    574     boolean signed = pixelType == FormatTools.INT8 || 
    575       pixelType == FormatTools.INT16 || pixelType == FormatTools.INT32; 
    576  
     775      double[] d = (double[]) DataTools.makeDataArray(buf, 8, true, little); 
     776      if (normal) d = DataTools.normalizeDoubles(d); 
     777      return makeImage(d, w, h, rgbChanCount, interleaved); 
     778    } 
     779 
     780    boolean signed = FormatTools.isSigned(pixelType); 
    577781    ColorModel model = null; 
    578782 
    579783    if (signed) { 
    580784      if (pixelType == FormatTools.INT8) { 
    581         model = new SignedColorModel(8, DataBuffer.TYPE_BYTE, 
    582           r.getRGBChannelCount()); 
     785        model = new SignedColorModel(8, DataBuffer.TYPE_BYTE, rgbChanCount); 
    583786      } 
    584787      else if (pixelType == FormatTools.INT16) { 
    585         model = new SignedColorModel(16, DataBuffer.TYPE_USHORT, 
    586           r.getRGBChannelCount()); 
     788        model = new SignedColorModel(16, DataBuffer.TYPE_SHORT, rgbChanCount); 
    587789      } 
    588790      else if (pixelType == FormatTools.INT32) { 
    589         model = new SignedColorModel(32, DataBuffer.TYPE_INT, 
    590           r.getRGBChannelCount()); 
    591       } 
    592     } 
    593  
    594     BufferedImage b = makeImage(buf, w, h, 
    595       r.isIndexed() ? 1 : r.getRGBChannelCount(), r.isInterleaved(), 
    596       FormatTools.getBytesPerPixel(r.getPixelType()), false, 
    597       r.isLittleEndian(), signed); 
    598  
    599     if (r.isIndexed()) { 
     791        model = new SignedColorModel(32, DataBuffer.TYPE_INT, rgbChanCount); 
     792      } 
     793    } 
     794 
     795    int bpp = FormatTools.getBytesPerPixel(pixelType); 
     796    BufferedImage b = makeImage(buf, w, h, indexed ? 1 : rgbChanCount, 
     797      interleaved, bpp, false, little, signed); 
     798 
     799    if (indexed) { 
    600800      if (pixelType == FormatTools.UINT8 || pixelType == FormatTools.INT8) { 
    601801        byte[][] table = r.get8BitLookupTable(); 
  • trunk/components/bio-formats/src/loci/formats/ChannelFiller.java

    r4570 r4804  
    175175    throws FormatException, IOException 
    176176  { 
     177    int pixelType = getPixelType(); 
    177178    return AWTImageTools.makeImage(openBytes(no, x, y, w, h), w, h, 
    178179      getRGBChannelCount(), isInterleaved(), 
    179       FormatTools.getBytesPerPixel(getPixelType()), isLittleEndian()); 
     180      FormatTools.getBytesPerPixel(pixelType), 
     181      FormatTools.isFloatingPoint(pixelType), isLittleEndian(), 
     182      FormatTools.isSigned(pixelType)); 
    180183  } 
    181184 
  • trunk/components/bio-formats/src/loci/formats/FormatTools.java

    r4521 r4804  
    441441  } 
    442442 
     443  /** 
     444   * Determines whether the given pixel type is floating point or integer. 
     445   * @param pixelType the pixel type as retrieved from 
     446   *   {@link IFormatReader#getPixelType()}. 
     447   * @return true if the pixel type is floating point. 
     448   * @see IFormatReader#getPixelType() 
     449   */ 
     450  public static boolean isFloatingPoint(int pixelType) { 
     451    switch (pixelType) { 
     452      case INT8: 
     453      case UINT8: 
     454      case INT16: 
     455      case UINT16: 
     456      case INT32: 
     457      case UINT32: 
     458        return false; 
     459      case FLOAT: 
     460      case DOUBLE: 
     461        return true; 
     462    } 
     463    throw new IllegalArgumentException("Unknown pixel type: " + pixelType); 
     464  } 
     465 
     466  /** 
     467   * Determines whether the given pixel type is signed or unsigned. 
     468   * @param pixelType the pixel type as retrieved from 
     469   *   {@link IFormatReader#getPixelType()}. 
     470   * @return true if the pixel type is signed. 
     471   * @see IFormatReader#getPixelType() 
     472   */ 
     473  public static boolean isSigned(int pixelType) { 
     474    switch (pixelType) { 
     475      case INT8: 
     476      case INT16: 
     477      case INT32: 
     478      case FLOAT: 
     479      case DOUBLE: 
     480        return true; 
     481      case UINT8: 
     482      case UINT16: 
     483      case UINT32: 
     484        return false; 
     485    } 
     486    throw new IllegalArgumentException("Unknown pixel type: " + pixelType); 
     487  } 
     488 
    443489  // -- Utility methods - sanity checking 
    444490 
  • trunk/components/bio-formats/src/loci/formats/FormatWriter.java

    r4550 r4804  
    9999 
    100100    BufferedImage img = AWTImageTools.makeImage(bytes, width, height, channels, 
    101       true, FormatTools.getBytesPerPixel(type), littleEndian); 
     101      true, FormatTools.getBytesPerPixel(type), 
     102      FormatTools.isFloatingPoint(type), littleEndian, 
     103      FormatTools.isSigned(type)); 
    102104    saveImage(img, series, lastInSeries, last); 
    103105  } 
  • trunk/components/bio-formats/src/loci/formats/gui/ImageViewer.java

    r4584 r4804  
    203203 
    204204    // image icon 
    205     BufferedImage dummy = AWTImageTools.makeImage(new byte[1][1], 1, 1); 
     205    BufferedImage dummy = AWTImageTools.makeImage(new byte[1][1], 1, 1, false); 
    206206    icon = new ImageIcon(dummy); 
    207207    iconLabel = new JLabel(icon, SwingConstants.LEFT); 
  • trunk/components/bio-formats/src/loci/formats/in/MNGReader.java

    r4677 r4804  
    104104    // ImageIO often returns images that cannot be scaled because a 
    105105    // BytePackedRaster is used 
    106     byte[][] pix = AWTImageTools.getPixelBytes(img, isLittleEndian()); 
     106    int pixelType = getPixelType(); 
     107    boolean little = isLittleEndian(); 
     108    byte[][] pix = AWTImageTools.getPixelBytes(img, little); 
    107109    img = AWTImageTools.makeImage(pix, w, h, 
    108       FormatTools.getBytesPerPixel(getPixelType()), isLittleEndian()); 
     110      FormatTools.getBytesPerPixel(pixelType), 
     111      FormatTools.isFloatingPoint(pixelType), little, 
     112      FormatTools.isSigned(pixelType)); 
    109113 
    110114    return img; 
  • trunk/components/bio-formats/src/loci/formats/tools/ImageInfo.java

    r4741 r4804  
    492492            reader.openBytes(i, xCoordinate, yCoordinate, width, height); 
    493493          Object pix = DataTools.makeDataArray(b, 
    494             FormatTools.getBytesPerPixel(reader.getPixelType()), 
    495             reader.getPixelType() == FormatTools.FLOAT, 
     494            FormatTools.getBytesPerPixel(pixelType), 
     495            FormatTools.isFloatingPoint(pixelType), 
    496496            reader.isLittleEndian()); 
    497497          images[i - start] = AWTImageTools.makeImage( 
    498             ImageTools.make24Bits(pix, x, y, false, false), x, y); 
     498            ImageTools.make24Bits(pix, x, y, false, false), x, y, 
     499            FormatTools.isSigned(pixelType)); 
    499500        } 
    500501 
  • trunk/components/common/src/loci/common/DataTools.java

    r4802 r4804  
    515515    boolean little) 
    516516  { 
    517     return makeDataArray(b, bpp, fp, little, false); 
    518   } 
    519  
    520   /** 
    521    * Convert a byte array to the appropriate primitive type array. 
    522    * @param b Byte array to convert. 
    523    * @param bpp Denotes the number of bytes in the returned primitive type 
    524    *   (e.g. if bpp == 2, we should return an array of type short). 
    525    * @param fp If set and bpp == 4 or bpp == 8, then return floats or doubles. 
    526    * @param little Whether byte array is in little-endian order. 
    527    */ 
    528   public static Object makeDataArray(byte[] b, 
    529     int bpp, boolean fp, boolean little, boolean signed) 
    530   { 
    531517    if (bpp == 1) { 
    532518      return b; 
     
    568554    } 
    569555    return null; 
     556  } 
     557 
     558  /** 
     559   * @deprecated Use {@link #makeDataArray(byte[], int, boolean, boolean)} 
     560   *   regardless of signedness. 
     561   */ 
     562  public static Object makeDataArray(byte[] b, 
     563    int bpp, boolean fp, boolean little, boolean signed) 
     564  { 
     565    return makeDataArray(b, bpp, fp, little); 
    570566  } 
    571567 
  • trunk/components/legacy/ome-notes/src/loci/ome/notes/TemplateField.java

    r4550 r4804  
    147147    else if (type.equals("thumbnail")) { 
    148148      ImageIcon icon = 
    149         new ImageIcon(AWTImageTools.makeImage(new byte[1][1], 1, 1)); 
     149        new ImageIcon(AWTImageTools.makeImage(new byte[1][1], 1, 1, false)); 
    150150      component = new JLabel(icon, SwingConstants.LEFT); 
    151151    } 
     
    193193    else if (type.equals("thumbnail")) { 
    194194      ImageIcon icon = 
    195         new ImageIcon(AWTImageTools.makeImage(new byte[1][1], 1, 1)); 
     195        new ImageIcon(AWTImageTools.makeImage(new byte[1][1], 1, 1, false)); 
    196196      comp = new JLabel(icon, SwingConstants.LEFT); 
    197197    } 
  • trunk/components/loci-plugins/src/loci/plugins/Exporter.java

    r4685 r4804  
    216216              b[j] = (byte[]) is.getProcessor(i + j + 1).getPixels(); 
    217217            } 
    218             img = AWTImageTools.makeImage(b, x, y); 
     218            img = AWTImageTools.makeImage(b, x, y, false); 
    219219          } 
    220220          else { 
    221221            byte[] b = (byte[]) proc.getPixels(); 
    222             img = AWTImageTools.makeImage(b, x, y); 
     222            img = AWTImageTools.makeImage(b, x, y, false); 
    223223          } 
    224224        } 
     
    229229              s[j] = (short[]) is.getProcessor(i + j + 1).getPixels(); 
    230230            } 
    231             img = AWTImageTools.makeImage(s, x, y); 
     231            img = AWTImageTools.makeImage(s, x, y, false); 
    232232          } 
    233233          else { 
    234234            short[] s = (short[]) proc.getPixels(); 
    235             img = AWTImageTools.makeImage(s, x, y); 
     235            img = AWTImageTools.makeImage(s, x, y, false); 
    236236          } 
    237237        } 
     
    252252          byte[][] pix = new byte[3][x*y]; 
    253253          ((ColorProcessor) proc).getRGB(pix[0], pix[1], pix[2]); 
    254           img = AWTImageTools.makeImage(pix, x, y); 
     254          img = AWTImageTools.makeImage(pix, x, y, false); 
    255255        } 
    256256 
  • trunk/components/test-suite/src/loci/tests/ImageTester.java

    r4550 r4804  
    166166        } 
    167167      } 
    168       bimg1[q] = AWTImageTools.makeImage(bdata1, w, h); 
    169       bimg2[q] = AWTImageTools.makeImage(bdata2, w, h, c, true); 
    170       bimg3[q] = AWTImageTools.makeImage(bdata3, w, h, c, false); 
    171       simg1[q] = AWTImageTools.makeImage(sdata1, w, h); 
    172       simg2[q] = AWTImageTools.makeImage(sdata2, w, h, c, true); 
    173       simg3[q] = AWTImageTools.makeImage(sdata3, w, h, c, false); 
    174 //      iimg1[q] = AWTImageTools.makeImage(idata1, w, h); 
    175 //      iimg2[q] = AWTImageTools.makeImage(idata2, w, h, c, true); 
    176 //      iimg3[q] = AWTImageTools.makeImage(idata3, w, h, c, false); 
     168      boolean signedBytes = false; 
     169      bimg1[q] = AWTImageTools.makeImage(bdata1, w, h, signedBytes); 
     170      bimg2[q] = AWTImageTools.makeImage(bdata2, w, h, c, true, signedBytes); 
     171      bimg3[q] = AWTImageTools.makeImage(bdata3, w, h, c, false, signedBytes); 
     172      boolean signedShorts = false; 
     173      simg1[q] = AWTImageTools.makeImage(sdata1, w, h, signedShorts); 
     174      simg2[q] = AWTImageTools.makeImage(sdata2, w, h, c, true, signedShorts); 
     175      simg3[q] = AWTImageTools.makeImage(sdata3, w, h, c, false, signedShorts); 
     176//      boolean signedInts = true; 
     177//      iimg1[q] = AWTImageTools.makeImage(idata1, w, h, signedInts); 
     178//      iimg2[q] = AWTImageTools.makeImage(idata2, w, h, c, true, signedInts); 
     179//      iimg3[q] = AWTImageTools.makeImage(idata3, w, h, c, false, signedInts); 
    177180    } 
    178181 
Note: See TracChangeset for help on using the changeset viewer.