Changeset 1838


Ignore:
Timestamp:
11/21/06 15:09:57 (13 years ago)
Author:
curtis
Message:

Fix ImageTools.scale and ImageTools.pad.

Location:
trunk/loci/formats
Files:
4 edited

Legend:

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

    r1806 r1838  
    114114    if (!canMerge(id)) return super.openThumbImage(id, no); 
    115115    return ImageTools.scale(openImage(id, no), getThumbSizeX(id), 
    116       getThumbSizeY(id), true, true); 
     116      getThumbSizeY(id), true); 
    117117  } 
    118118 
  • trunk/loci/formats/ChannelSeparator.java

    r1834 r1838  
    132132  { 
    133133    return ImageTools.scale(openImage(id, no), getThumbSizeX(id), 
    134       getThumbSizeY(id), true, true); 
     134      getThumbSizeY(id), true); 
    135135  } 
    136136 
  • trunk/loci/formats/FormatReader.java

    r1834 r1838  
    292292  { 
    293293    return ImageTools.scale(openImage(id, no), 
    294       getThumbSizeX(id), getThumbSizeY(id), true, true); 
     294      getThumbSizeX(id), getThumbSizeY(id), true); 
    295295  } 
    296296 
  • trunk/loci/formats/ImageTools.java

    r1835 r1838  
    12641264    int oldWidth, int width, int height) 
    12651265  { 
    1266     boolean needsPadding = 
    1267       (oldWidth != width) || ((b.length / (oldWidth*c)) != height); 
    1268  
    1269     if (needsPadding) { 
    1270       int oldHeight = b.length / (oldWidth * c); 
    1271  
    1272       // adjust to correct width 
    1273  
    1274       byte[] padded = new byte[width * oldHeight * c]; 
    1275       int clip = (width - oldWidth) / 2; 
    1276  
    1277       for (int i=0; i<oldHeight; i++) { 
    1278         System.arraycopy(b, i*oldWidth*c, padded, i*width*c,  
    1279           clip < 0 ? width*c : oldWidth*c); 
    1280       } 
    1281  
    1282       // adjust to correct height 
    1283  
    1284       byte[] rtn = new byte[width * height * c]; 
    1285       clip = (height - oldHeight) / 2; 
    1286  
    1287       if (clip < 0) { 
    1288         clip *= -1; 
    1289         for (int i=0; i<height; i++) { 
    1290           System.arraycopy(padded, (i+clip)*width*c, rtn, i*width*c, width*c); 
    1291         } 
    1292       } 
    1293       else { 
    1294         for (int i=clip; i<height+clip; i++) { 
    1295           System.arraycopy(padded, i*width*c, rtn, (i-clip)*width*c, width*c); 
    1296         } 
    1297       } 
    1298       return rtn; 
    1299     } 
    1300     return b;  
     1266    int oldHeight = b.length / (oldWidth * c); 
     1267    if (width < oldWidth) width = oldWidth; 
     1268    if (height < oldHeight) height = oldHeight; 
     1269    if (width == oldWidth && height == oldHeight) return b; 
     1270 
     1271    int wClip = (width - oldWidth) / 2; 
     1272    int hClip = (height - oldHeight) / 2; 
     1273 
     1274    byte[] padded = new byte[width * height * c]; 
     1275 
     1276    for (int oy=0, y=0; oy<oldHeight; oy++, y++) { 
     1277      int oldIndex = oldWidth * c * oy; 
     1278      int index = width * c * (y + hClip) + c * wClip; 
     1279      System.arraycopy(b, oldIndex, padded, index, oldWidth * c); 
     1280    } 
     1281 
     1282    return padded; 
    13011283  } 
    13021284 
     
    18421824   */ 
    18431825  public static BufferedImage scale(BufferedImage source, 
    1844     int width, int height, boolean preserve, boolean pad) 
     1826    int width, int height, boolean pad) 
    18451827  { 
    18461828    int w = source.getWidth(); 
     
    18481830    if (w == width && h == height) return source; 
    18491831 
    1850     int oldHeight = height; 
    1851  
    1852     if (preserve) { 
    1853       // keep the width the same, but adjust height accordingly 
    1854       height = (h * width) / w; 
    1855     } 
    1856  
     1832    int finalWidth = width, finalHeight = height; 
     1833 
     1834    if (pad) { 
     1835      // keep aspect ratio the same 
     1836      double r = (double) w / h; 
     1837      double ratio = (double) width / height; 
     1838      if (r > ratio) { 
     1839        // bounded by width; adjust height 
     1840        height = (h * width) / w; 
     1841      } 
     1842      else { 
     1843        // bounded by height; adjust width 
     1844        width = (w * height) / h; 
     1845      } 
     1846    } 
     1847 
     1848    BufferedImage result; 
    18571849    if ((width * height) / (w * h) > 0) { 
    18581850      // use Java2D to enlarge 
    1859       source = scale2D(source, width, height, null, source.getColorModel()); 
    1860       if (pad) return padImage(source, width, oldHeight); 
    1861       return source; 
     1851      result = scale2D(source, width, height, null, source.getColorModel()); 
    18621852    } 
    18631853    else { 
    18641854      // use AWT to shrink 
    1865       source = makeBuffered(scaleAWT(source, width, height, 
     1855      result = makeBuffered(scaleAWT(source, width, height, 
    18661856        Image.SCALE_AREA_AVERAGING), source.getColorModel()); 
    1867       if (pad) return padImage(source, width, oldHeight); 
    1868       return source; 
    1869     } 
     1857    } 
     1858    return padImage(result, finalWidth, finalHeight); 
    18701859  } 
    18711860 
Note: See TracChangeset for help on using the changeset viewer.