Changeset 3357


Ignore:
Timestamp:
11/09/07 13:14:57 (12 years ago)
Author:
melissa
Message:

Added new API methods to read sub-images:

  • BufferedImage openImage(int no, int x, int y, int width, int height)
  • byte[] openBytes(int no, int x, int y, int width, int height)
  • byte[] openBytes(int no, byte[] buf, int x, int y, int width, int height)
Location:
trunk/loci/formats
Files:
4 edited

Legend:

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

    r3197 r3357  
    379379  } 
    380380 
     381  /* @see IFormatReader#openImage(int, int, int, int, int) */ 
     382  public BufferedImage openImage(int no, int x, int y, int w, int h) 
     383    throws FormatException, IOException 
     384  { 
     385    return openImage(no).getSubimage(x, y, w, h); 
     386  } 
     387 
    381388  /* @see IFormatReader#openBytes(int) */ 
    382389  public byte[] openBytes(int no) throws FormatException, IOException { 
     
    399406  } 
    400407 
     408  /* @see IFormatReader#openBytes(int, int, int, int, int) */ 
     409  public byte[] openBytes(int no, int x, int y, int w, int h) 
     410    throws FormatException, IOException 
     411  { 
     412    byte[] buffer = new byte[w * h * 
     413      FormatTools.getBytesPerPixel(getPixelType()) * getRGBChannelCount()]; 
     414    return openBytes(no, buffer, x, y, w, h); 
     415  } 
     416 
    401417  /* @see IFormatReader#openBytes(int, byte[]) */ 
    402418  public byte[] openBytes(int no, byte[] buf) 
     
    413429    // this file does not contain enough image planes 
    414430    Arrays.fill(buf, (byte) 0); 
     431    return buf; 
     432  } 
     433 
     434  /* @see IFormatReader#openBytes(int, byte[], int, int, int, int) */ 
     435  public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) 
     436    throws FormatException, IOException 
     437  { 
     438    byte[] bytes = openBytes(no); 
     439    int bpp = FormatTools.getBytesPerPixel(getPixelType()); 
     440    int ch = getRGBChannelCount(); 
     441    if (buf.length < w * h * bpp * ch) { 
     442      throw new FormatException("Buffer too small."); 
     443    } 
     444 
     445    for (int yy=y; yy<y + h; yy++) { 
     446      for (int xx=x; xx<x + w; xx++) { 
     447        for (int cc=0; cc<ch; cc++) { 
     448          int oldNdx = -1, newNdx = -1; 
     449          if (isInterleaved()) { 
     450            oldNdx = yy*getSizeX()*bpp*ch + xx*bpp*ch + cc*bpp; 
     451            newNdx = (yy - y)*w*bpp*ch + (xx - x)*bpp*ch + cc*bpp; 
     452          } 
     453          else { 
     454            oldNdx = cc*getSizeX()*getSizeY()*bpp + yy*getSizeX()*bpp + xx*bpp; 
     455            newNdx = cc*w*h*bpp + (yy - y)*w*bpp + (xx - x)*bpp; 
     456          } 
     457          System.arraycopy(bytes, oldNdx, buf, newNdx, bpp); 
     458        } 
     459      } 
     460    } 
    415461    return buf; 
    416462  } 
  • trunk/loci/formats/FormatReader.java

    r3281 r3357  
    419419  } 
    420420 
     421  /* @see IFormatReader#openBytes(int, int, int, int, int) */ 
     422  public byte[] openBytes(int no, int x, int y, int w, int h) 
     423    throws FormatException, IOException 
     424  { 
     425    int bpp = FormatTools.getBytesPerPixel(getPixelType()); 
     426    int ch = getRGBChannelCount(); 
     427    byte[] newBuffer = new byte[w * h * ch * bpp]; 
     428    return openBytes(no, newBuffer, x, y, w, h); 
     429  } 
     430 
     431  /* @see IFormatReader#openBytes(int, byte[], int, int, int, int) */ 
     432  public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) 
     433    throws FormatException, IOException 
     434  { 
     435    byte[] bytes = openBytes(no); 
     436    int bpp = FormatTools.getBytesPerPixel(getPixelType()); 
     437    int ch = getRGBChannelCount(); 
     438    if (buf.length < w * h * bpp * ch) { 
     439      throw new FormatException("Buffer too small."); 
     440    } 
     441 
     442    for (int yy=y; yy<y + h; yy++) { 
     443      for (int xx=x; xx<x + w; xx++) { 
     444        for (int cc=0; cc<ch; cc++) { 
     445          int oldNdx = -1, newNdx = -1; 
     446          if (isInterleaved()) { 
     447            oldNdx = yy*getSizeX()*bpp*ch + xx*bpp*ch + cc*bpp; 
     448            newNdx = (yy - y)*w*bpp*ch + (xx - x)*bpp*ch + cc*bpp; 
     449          } 
     450          else { 
     451            oldNdx = cc*getSizeX()*getSizeY()*bpp + yy*getSizeX()*bpp + xx*bpp; 
     452            newNdx = cc*w*h*bpp + (yy - y)*w*bpp + (xx - x)*bpp; 
     453          } 
     454          System.arraycopy(bytes, oldNdx, buf, newNdx, bpp); 
     455        } 
     456      } 
     457    } 
     458 
     459    return buf; 
     460  } 
     461 
     462  /* @see IFormatReader#openImage(int, int, int, int, int) */ 
     463  public BufferedImage openImage(int no, int x, int y, int w, int h) 
     464    throws FormatException, IOException 
     465  { 
     466    return openImage(no).getSubimage(x, y, w, h); 
     467  } 
     468 
    421469  /* @see IFormatReader#openThumbImage(int) */ 
    422470  public BufferedImage openThumbImage(int no) 
  • trunk/loci/formats/IFormatReader.java

    r3197 r3357  
    181181 
    182182  /** 
     183   * Obtains a sub-image of the specified image, whose upper-left corner is 
     184   * given by (x, y). 
     185   */ 
     186  byte[] openBytes(int no, int x, int y, int width, int height) 
     187    throws FormatException, IOException; 
     188 
     189  /** 
    183190   * Obtains the specified image from the current file into a pre-allocated byte 
    184191   * array of (sizeX * sizeY * bytesPerPixel). 
     
    193200    throws FormatException, IOException; 
    194201 
     202  /** 
     203   * Obtains a sub-image of the specified image into a pre-allocated byte array. 
     204   * @param no the image index within the file. 
     205   * @param buf a pre-allocated buffer. 
     206   * @param x X coordinate of the upper-left corner of the sub-image 
     207   * @param y Y coordinate of the upper-left corner of the sub-image 
     208   * @param width of the sub-image 
     209   * @param height of the sub-image 
     210   * @return the pre-allocated buffer <code>buf</code> for convenience. 
     211   * @throws FormatException if there was a problem parsing the metadata of the 
     212   * file. 
     213   * @throws IOException if there was a problem reading the file. 
     214   */ 
     215  byte[] openBytes(int no, byte[] buf, int x, int y, int width, int height) 
     216    throws FormatException, IOException; 
     217 
    195218  /** Obtains the specified image from the current file. */ 
    196219  BufferedImage openImage(int no) 
     220    throws FormatException, IOException; 
     221 
     222  /** 
     223   * Obtains a sub-image of the specified image, whose upper-left corner is 
     224   * given by (x, y). 
     225   */ 
     226  BufferedImage openImage(int no, int x, int y, int width, int height) 
    197227    throws FormatException, IOException; 
    198228 
  • trunk/loci/formats/ImageReader.java

    r3312 r3357  
    314314  } 
    315315 
     316  /* @see IFormatReader#openImage(int, int, int, int, int) */ 
     317  public BufferedImage openImage(int no, int x, int y, int w, int h) 
     318    throws FormatException, IOException 
     319  { 
     320    FormatTools.assertId(currentId, true, 2); 
     321    return getReader().openImage(no, x, y, w, h); 
     322  } 
     323 
    316324  /* @see IFormatReader#openBytes(int) */ 
    317325  public byte[] openBytes(int no) throws FormatException, IOException { 
     
    320328  } 
    321329 
     330  /* @see IFormatReader#openBytes(int, int, int, int, int) */ 
     331  public byte[] openBytes(int no, int x, int y, int w, int h) 
     332    throws FormatException, IOException 
     333  { 
     334    FormatTools.assertId(currentId, true, 2); 
     335    return getReader().openBytes(no, x, y, w, h); 
     336  } 
     337 
    322338  /* @see IFormatReader#openBytes(int, byte[]) */ 
    323339  public byte[] openBytes(int no, byte[] buf) 
     
    326342    FormatTools.assertId(currentId, true, 2); 
    327343    return getReader().openBytes(no, buf); 
     344  } 
     345 
     346  /* @see IFormatReader#openBytes(int, byte[], int, int, int, int) */ 
     347  public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) 
     348    throws FormatException, IOException 
     349  { 
     350    FormatTools.assertId(currentId, true, 2); 
     351    return getReader().openBytes(no, buf, x, y, w, h); 
    328352  } 
    329353 
Note: See TracChangeset for help on using the changeset viewer.