Changeset 7309


Ignore:
Timestamp:
12/02/10 03:18:04 (9 years ago)
Author:
callan
Message:

Fix min/max calculation in the "slush" area outside the actual pixel count written to the buffer. (See  omero:#3599)

Location:
trunk/components/bio-formats
Files:
2 edited

Legend:

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

    r7284 r7309  
    259259    FormatTools.assertId(getCurrentFile(), true, 2); 
    260260    super.openBytes(no, buf, x, y, w, h); 
    261     updateMinMax(buf, no); 
     261     
     262    updateMinMax(no, buf, FormatTools.getBytesPerPixel(getPixelType()) * w * h); 
    262263    return buf; 
    263264  } 
     
    277278  // -- Helper methods -- 
    278279 
    279   /** Updates min/max values based on the given byte array. */ 
    280   protected void updateMinMax(byte[] b, int ndx) 
    281     throws FormatException, IOException 
    282   { 
    283     if (b == null) return; 
     280  /** 
     281   * Updates min/max values based on the given byte array. 
     282   * @param no the image index within the file. 
     283   * @param buf a pre-allocated buffer. 
     284   * @param len as <code>buf</code> may be larger than the actual pixel count 
     285   * having been written to it, the length (in bytes) of the those pixels. 
     286   */ 
     287  protected void updateMinMax(int no, byte[] buf, int len) 
     288    throws FormatException, IOException 
     289  { 
     290    if (buf == null) return; 
    284291    initMinMax(); 
    285292 
     
    287294    int series = getSeries(); 
    288295    int pixelType = getPixelType(); 
    289     int planeSize = getSizeX() * getSizeY(); 
    290     planeSize *= FormatTools.getBytesPerPixel(pixelType); 
     296    int bpp = FormatTools.getBytesPerPixel(pixelType); 
     297    int planeSize = getSizeX() * getSizeY() * bpp; 
    291298    // check whether min/max values have already been computed for this plane 
    292299    // and that the buffer requested is actually the entire plane 
    293     if (b.length >= planeSize 
    294         && !Double.isNaN(planeMin[series][ndx * numRGB])) return; 
     300    if (len == planeSize 
     301        && !Double.isNaN(planeMin[series][no * numRGB])) return; 
    295302 
    296303    boolean little = isLittleEndian(); 
    297     int bytes = FormatTools.getBytesPerPixel(getPixelType()); 
    298     int pixels = b.length / (bytes * numRGB); 
     304     
     305    int pixels = len / (bpp * numRGB); 
    299306    boolean interleaved = isInterleaved(); 
    300307 
    301     int[] coords = getZCTCoords(ndx); 
     308    int[] coords = getZCTCoords(no); 
    302309    int cBase = coords[1] * numRGB; 
    303     int pBase = ndx * numRGB; 
     310    int pBase = no * numRGB; 
    304311    for (int c=0; c<numRGB; c++) { 
    305312      planeMin[series][pBase + c] = Double.POSITIVE_INFINITY; 
     
    309316    boolean signed = FormatTools.isSigned(pixelType); 
    310317 
    311     long threshold = (long) Math.pow(2, bytes * 8 - 1); 
     318    long threshold = (long) Math.pow(2, bpp * 8 - 1); 
    312319    for (int i=0; i<pixels; i++) { 
    313320      for (int c=0; c<numRGB; c++) { 
    314         int idx = bytes * (interleaved ? i * numRGB + c : c * pixels + i); 
    315         long bits = DataTools.bytesToLong(b, idx, bytes, little); 
     321        int idx = bpp * (interleaved ? i * numRGB + c : c * pixels + i); 
     322        long bits = DataTools.bytesToLong(buf, idx, bpp, little); 
    316323        if (signed) { 
    317324          if (bits >= threshold) bits -= 2*threshold; 
     
    342349      } 
    343350    } 
    344     minMaxDone[series] = Math.max(minMaxDone[series], ndx + 1); 
     351    minMaxDone[series] = Math.max(minMaxDone[series], no + 1); 
    345352 
    346353    if (minMaxDone[getSeries()] == getImageCount() && minMaxStore != null) { 
  • trunk/components/bio-formats/test/loci/formats/utests/MinMaxCalculatorTest.java

    r7284 r7309  
    148148 
    149149  @Test 
     150  public void testValidMinMaxNoOutOfBufferInspection() throws Exception { 
     151    byte[] buf = new byte[planeSize * 2]; 
     152    buf[buf.length - 1] = 120;  // This should not be calculated against 
     153    minMaxCalculator.openBytes(0, buf, 0, 0, sizeX, sizeY); 
     154    assertMinMax(-2.0, 101.0); 
     155  } 
     156 
     157  @Test 
    150158  public void testValidMinMaxDoesntRecalculateOnFullPlane() throws Exception { 
    151159    minMaxCalculator.openBytes(0); 
Note: See TracChangeset for help on using the changeset viewer.