いくつの悪くないJ 2 MEピクチャーのスケーリングの関数


コード1、関数resizeImage
public static Image resizeImage(Image src, int destW, int destH) {   
        int srcW = src.getWidth();   
        int srcH = src.getHeight();   
        // create pixel arrays 
        int[] destPixels = new int[destW * destH]; // array to hold destination   
        // pixels 
        int[] srcPixels = new int[srcW * srcH]; // array with source's pixels   
        src.getRGB(srcPixels, 0, srcW, 0, 0, srcW, srcH);   
        // simple point smapled resizing 
        // loop through the destination pixels, find the matching pixel on 
        // the source and use that 
        for (int destY = 0; destY < destH; ++destY) {   
            for (int destX = 0; destX < destW; ++destX) {   
                int srcX = (destX * srcW) / destW; 
                int srcY = (destY * srcH) / destH; 
                destPixels[destX + destY * destW] = srcPixels[srcX + srcY * srcW];   
            }   
        }   
        // return a new image created from the destination pixel buffer 
        return Image.createRGBImage(destPixels, destW, destH, true);   
    }  

コード2、関数ZoomImage
 
public static Image ZoomImage(Image src, int desW, int desH) { 
        Image desImg = null; 
        int srcW = src.getWidth(); //   
        int srcH = src.getHeight(); //   
        int[] srcBuf = new int[srcW * srcH]; //   
        src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH); 
        //   
        int[] tabY = new int[desH]; 
        int[] tabX = new int[desW]; 
        int sb = 0; 
        int db = 0; 
        int tems = 0; 
        int temd = 0; 
        int distance = srcH > desH ? srcH : desH; 
        for (int i = 0; i <= distance; i++) { /*   */
            tabY[db] = sb; 
            tems += srcH; 
            temd += desH; 
            if (tems > distance) { 
                tems -= distance; 
                sb++; 
            } 
            if (temd > distance) { 
                temd -= distance; 
                db++; 
            } 
        } 
        sb = 0; 
        db = 0; 
        tems = 0; 
        temd = 0; 
        distance = srcW > desW ? srcW : desW; 
        for (int i = 0; i <= distance; i++) { /*   */
            tabX[db] = (short) sb; 
            tems += srcW; 
            temd += desW; 
            if (tems > distance) { 
                tems -= distance; 
                sb++; 
            } 
            if (temd > distance) { 
                temd -= distance; 
                db++; 
            } 
        } 
        //  buf 
        int[] desBuf = new int[desW * desH]; 
        int dx = 0; 
        int dy = 0; 
        int sy = 0; 
        int oldy = -1; 
        for (int i = 0; i < desH; i++) { 
            if (oldy == tabY[i]) { 
                System.arraycopy(desBuf, dy - desW, desBuf, dy, desW); 
            } else { 
                dx = 0; 
                for (int j = 0; j < desW; j++) { 
                    desBuf[dy + dx] = srcBuf[sy + tabX[j]]; 
                    dx++; 
                } 
                sy += (tabY[i] - oldy) * srcW; 
            } 
            oldy = tabY[i]; 
            dy += desW; 
        } 
        //   
        desImg = Image.createRGBImage(desBuf, desW, desH, true); 
        return desImg; 
    }    

コード3、
public static Image scaleImage(Image original, int newWidth, int newHeight) { 
        int[] rawInput = new int[original.getHeight() * original.getWidth()]; 
        original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight()); 
        int[] rawOutput = new int[newWidth * newHeight]; 
        // YD compensates for the x loop by subtracting the width back out 
        int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth(); 
        int YR = original.getHeight() % newHeight; 
        int XD = original.getWidth() / newWidth; 
        int XR = original.getWidth() % newWidth; 
        
        int outOffset = 0; 
        int inOffset = 0; 
        for (int y = newHeight, YE = 0; y > 0; y--) { 
            for (int x = newWidth, XE = 0; x > 0; x--) { 
                rawOutput[outOffset++] = rawInput[inOffset]; 
                inOffset += XD; 
                XE += XR; 
                if (XE >= newWidth) { 
                    XE -= newWidth; 
                    inOffset++; 
                } 
            } 
            inOffset += YD; 
            YE += YR; 
            if (YE >= newHeight) { 
                YE -= newHeight; 
                inOffset += original.getWidth(); 
            } 
        } 
        return Image.createRGBImage(rawOutput, newWidth, newHeight, true); 
    }  

100*100の図を200*200に拡大して比較したところ、ZoomImageで生成された画像と他の2つの関数で生成された画像が少し違い、ZoomImageの効果が少し良いと感じました.もう少し拡大するとほぼ同じで違いは見えません.
 
コード4
 /* */
 public Image scaleImage(Image src,int scales1,int scales2)
 {
  return transImage(src,src.getWidth()*scales1/scales2,src.getHeight()*scales1/scales2);
 }
 public Image transImage(Image src, int w, int h)
 {
    int srcW = src.getWidth();
    int srcH = src.getHeight();
    int dstW=w,dstH=h;
    Image tmp = Image.createImage(dstW, srcH);
    Graphics g = tmp.getGraphics();
    int scale=16;    
    int delta = (srcW << scale) / dstW;// 
    int pos = delta / 2;// 
    for (int x = 0; x < dstW; x++)
    {
      g.setClip(x, 0, 1, srcH);
      g.drawImage(src, x - (pos >> scale), 0, Graphics.LEFT | Graphics.TOP);
      pos += delta;
    }
    Image dst = Image.createImage( dstW, dstH);
    g = dst.getGraphics();
    delta = (srcH << scale) / dstH;
    pos = delta / 2;
    for (int y = 0; y < dstH; y++) 
    {
      g.setClip(0,y, dstW, 1);
      g.drawImage(tmp, 0, y - (pos >> scale), Graphics.LEFT | Graphics.TOP);
      pos += delta;
    }
    return dst;
 } 

 
使用例:
1.1枚のピクチャーpicを176*208の図に変換する
pic=transImage(pic,176,208);
2.1枚の画像picを元の2倍の大きさに変換する
pic=scaleImage(pic,2,1);
3.1枚のピクチャーpicを元の3分の2に変換する
pic=scaleImage(pic,2,3);
上の例から、皆さんはどのように使うか知っているでしょう、ほほほ
 
コード5,
 
public static final Image scale (Image srcImage, int newW, int newH) {    
  int srcW = srcImage.getWidth();    
  int srcH = srcImage.getHeight();    
 
  //     
  Image tmp = Image.createImage(newW, srcH);    
  Graphics g = tmp.getGraphics();     
  for (int x = 0; x < newW; x++) {    
      g.setClip(x, 0, 1, srcH);        //         
    g.drawImage(srcImage,x-x*srcW/newW,0,Graphics.LEFT | Graphics.TOP);     
  }     
 
  //     
  Image dst = Image.createImage(newW, newH);    
  g = dst.getGraphics();     
  for (int y = 0; y < newH; y++) {        
    g.setClip(0, y, newW, 1);        //         
    g.drawImage(tmp,0,y-y*srcH/newH,Graphics.LEFT | Graphics.TOP);    
  }     
 
  return dst;        
}