Changeset 7349


Ignore:
Timestamp:
12/06/10 17:36:10 (9 years ago)
Author:
melissa
Message:

Added method to AWTImageTools for retrieving subimages.

Readers now use AWTImageTools.getSubimage(...) to retrieve a subimage of a BufferedImage, as the getSubimage(...) method in BufferedImage does not behave as we would like.

Location:
trunk/components/bio-formats/src/loci/formats
Files:
6 edited

Legend:

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

    r6881 r7349  
    938938   */ 
    939939  public static byte[][] getPixelBytes(BufferedImage img, boolean little) { 
     940    return getPixelBytes(img, little, 0, 0, img.getWidth(), img.getHeight()); 
     941  } 
     942 
     943  /** 
     944   * Return a 2D array of bytes representing the image.  If the transfer type 
     945   * is something other than DataBuffer.TYPE_BYTE, then each pixel value is 
     946   * converted to the appropriate number of bytes.  In other words, if we 
     947   * are given an image with 16-bit data, each channel of the resulting array 
     948   * will have width * height * 2 bytes. 
     949   */ 
     950  public static byte[][] getPixelBytes(BufferedImage img, boolean little, 
     951    int x, int y, int w, int h) 
     952  { 
    940953    Object pixels = getPixels(img); 
    941954    int imageType = img.getType(); 
     955    byte[][] pixelBytes = null; 
    942956 
    943957    if (pixels instanceof byte[][]) { 
    944       return (byte[][]) pixels; 
     958      pixelBytes = (byte[][]) pixels; 
    945959    } 
    946960    else if (pixels instanceof short[][]) { 
    947961      short[][] s = (short[][]) pixels; 
    948       byte[][] b = new byte[s.length][s[0].length * 2]; 
    949       for (int i=0; i<b.length; i++) { 
     962      pixelBytes = new byte[s.length][s[0].length * 2]; 
     963      for (int i=0; i<pixelBytes.length; i++) { 
    950964        for (int j=0; j<s[0].length; j++) { 
    951           DataTools.unpackBytes(s[i][j], b[i], j * 2, 2, little); 
    952         } 
    953       } 
    954       return b; 
     965          DataTools.unpackBytes(s[i][j], pixelBytes[i], j * 2, 2, little); 
     966        } 
     967      } 
    955968    } 
    956969    else if (pixels instanceof int[][]) { 
    957       byte[][] b = null; 
    958970      int[][] in = (int[][]) pixels; 
    959971 
     
    962974        imageType == BufferedImage.TYPE_INT_ARGB) 
    963975      { 
    964         b = new byte[in.length][in[0].length]; 
     976        pixelBytes = new byte[in.length][in[0].length]; 
    965977        for (int c=0; c<in.length; c++) { 
    966978          for (int i=0; i<in[0].length; i++) { 
    967979            if (imageType != BufferedImage.TYPE_INT_BGR) { 
    968               b[c][i] = (byte) (in[c][i] & 0xff); 
     980              pixelBytes[c][i] = (byte) (in[c][i] & 0xff); 
    969981            } 
    970982            else { 
    971               b[in.length - c - 1][i] = (byte) (in[c][i] & 0xff); 
     983              pixelBytes[in.length - c - 1][i] = (byte) (in[c][i] & 0xff); 
    972984            } 
    973985          } 
     
    975987      } 
    976988      else { 
    977         b = new byte[in.length][in[0].length * 4]; 
    978         for (int i=0; i<b.length; i++) { 
     989        pixelBytes = new byte[in.length][in[0].length * 4]; 
     990        for (int i=0; i<pixelBytes.length; i++) { 
    979991          for (int j=0; j<in[0].length; j++) { 
    980             DataTools.unpackBytes(in[i][j], b[i], j * 4, 4, little); 
     992            DataTools.unpackBytes(in[i][j], pixelBytes[i], j * 4, 4, little); 
    981993          } 
    982994        } 
    983995      } 
    984       return b; 
    985996    } 
    986997    else if (pixels instanceof float[][]) { 
    987998      float[][] in = (float[][]) pixels; 
    988       byte[][] b = new byte[in.length][in[0].length * 4]; 
    989       for (int i=0; i<b.length; i++) { 
     999      pixelBytes = new byte[in.length][in[0].length * 4]; 
     1000      for (int i=0; i<pixelBytes.length; i++) { 
    9901001        for (int j=0; j<in[0].length; j++) { 
    9911002          int v = Float.floatToIntBits(in[i][j]); 
    992           DataTools.unpackBytes(v, b[i], j * 4, 4, little); 
    993         } 
    994       } 
    995       return b; 
     1003          DataTools.unpackBytes(v, pixelBytes[i], j * 4, 4, little); 
     1004        } 
     1005      } 
    9961006    } 
    9971007    else if (pixels instanceof double[][]) { 
    9981008      double[][] in = (double[][]) pixels; 
    999       byte[][] b = new byte[in.length][in[0].length * 8]; 
    1000       for (int i=0; i<b.length; i++) { 
     1009      pixelBytes = new byte[in.length][in[0].length * 8]; 
     1010      for (int i=0; i<pixelBytes.length; i++) { 
    10011011        for (int j=0; j<in[0].length; j++) { 
    10021012          long v = Double.doubleToLongBits(in[i][j]); 
    1003           DataTools.unpackBytes(v, b[i], j * 8, 8, little); 
    1004         } 
    1005       } 
    1006       return b; 
    1007     } 
    1008     return null; 
     1013          DataTools.unpackBytes(v, pixelBytes[i], j * 8, 8, little); 
     1014        } 
     1015      } 
     1016    } 
     1017 
     1018    if (x == 0 && y == 0 && w == img.getWidth() && h == img.getHeight()) { 
     1019      return pixelBytes; 
     1020    } 
     1021 
     1022    int bpp = FormatTools.getBytesPerPixel(getPixelType(img)); 
     1023    byte[][] croppedBytes = new byte[pixelBytes.length][w * h * bpp]; 
     1024    for (int c=0; c<croppedBytes.length; c++) { 
     1025      for (int row=0; row<h; row++) { 
     1026        int src = (row + y) * img.getWidth() * bpp + x * bpp; 
     1027        int dest = row * w * bpp; 
     1028        System.arraycopy(pixelBytes[c], src, croppedBytes[c], dest, w * bpp); 
     1029      } 
     1030    } 
     1031    return croppedBytes; 
    10091032  } 
    10101033 
     
    11651188  // -- Image manipulation -- 
    11661189 
     1190  /** Returns a subimage of the specified image. */ 
     1191  public static BufferedImage getSubimage(BufferedImage image, 
     1192    boolean littleEndian, int x, int y, int w, int h) 
     1193  { 
     1194    int pixelType = getPixelType(image); 
     1195    byte[][] pix = getPixelBytes(image, littleEndian, x, y, w, h); 
     1196    return makeImage(pix, w, h, FormatTools.getBytesPerPixel(pixelType), 
     1197      FormatTools.isFloatingPoint(pixelType), littleEndian, 
     1198      FormatTools.isSigned(pixelType)); 
     1199  } 
     1200 
    11671201  /** Splits the given multi-channel image into single-channel images. */ 
    11681202  public static BufferedImage[] splitChannels(BufferedImage image) { 
  • trunk/components/bio-formats/src/loci/formats/in/APNGReader.java

    r6881 r7349  
    9595      DataInputStream dis = 
    9696        new DataInputStream(new BufferedInputStream(in, 4096)); 
    97       return ImageIO.read(dis).getSubimage(x, y, w, h); 
     97      return AWTImageTools.getSubimage(ImageIO.read(dis), isLittleEndian(), 
     98        x, y, w, h); 
    9899    } 
    99100 
  • trunk/components/bio-formats/src/loci/formats/in/ImageIOReader.java

    r6881 r7349  
    7878    ras.close(); 
    7979    dis.close(); 
    80     return b.getSubimage(x, y, w, h); 
     80    return AWTImageTools.getSubimage(b, isLittleEndian(), x, y, w, h); 
    8181  } 
    8282 
  • trunk/components/bio-formats/src/loci/formats/in/LegacyQTReader.java

    r6881 r7349  
    9595      throw new FormatException("Open movie failed", re); 
    9696    } 
    97     return AWTImageTools.makeBuffered(image).getSubimage(x, y, w, h); 
     97    return AWTImageTools.getSubimage(AWTImageTools.makeBuffered(image), 
     98      isLittleEndian(), x, y, w, h); 
    9899  } 
    99100 
  • trunk/components/bio-formats/src/loci/formats/in/MNGReader.java

    r6881 r7349  
    8686    long end = info.lengths.get(no); 
    8787    BufferedImage img = readImage(end); 
    88     img = img.getSubimage(x, y, w, h); 
    8988 
    9089    // reconstruct the image to use an appropriate raster 
    9190    // ImageIO often returns images that cannot be scaled because a 
    9291    // BytePackedRaster is used 
    93     int pixelType = getPixelType(); 
    94     boolean little = isLittleEndian(); 
    95     byte[][] pix = AWTImageTools.getPixelBytes(img, little); 
    96     img = AWTImageTools.makeImage(pix, w, h, 
    97       FormatTools.getBytesPerPixel(pixelType), 
    98       FormatTools.isFloatingPoint(pixelType), little, 
    99       FormatTools.isSigned(pixelType)); 
    100  
    101     return img; 
     92    return AWTImageTools.getSubimage(img, isLittleEndian(), x, y, w, h); 
    10293  } 
    10394 
  • trunk/components/bio-formats/src/loci/formats/in/TiffJAIReader.java

    r6881 r7349  
    8484    FormatTools.checkPlaneParameters(this, no, -1, x, y, w, h); 
    8585    BufferedImage img = openBufferedImage(no); 
    86     return img.getSubimage(x, y, w, h); 
     86    return AWTImageTools.getSubimage(img, isLittleEndian(), x, y, w, h); 
    8787  } 
    8888 
Note: See TracChangeset for help on using the changeset viewer.