2014-12-25 2 views
0
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.util.concurrent.ForkJoinPool; 
import java.util.concurrent.RecursiveAction; 
import javax.imageio.ImageIO; 
import java.awt.*; 

class Multicore1 extends RecursiveAction 
{ 
    private int[] msource; 
    private int mstart; 
    private int mlength; 
    private int[] mDestination; 
     private int window=15; 
    private BufferedImage mimage; 
    public Multicore1(int[] src,int start,int length,int[] dest,BufferedImage image) 
    { 
     msource=src; 
     mstart=start; 
     mlength=length; 
     mDestination=dest; 
     mimage=image; 
    } 
    protected void computeDirectly(BufferedImage Img) 
    { 
     int rgb; 
     int lum; 
     int grayval; 
     int rgbVal; 
     int x,y; 
     int sliding=(window-1)/2; 
     System.out.println(" mlenght valu is " + mlength); 
     System.out.println(" mstart valu is " + mstart); 
     float r=0; float g=0;float b=0; 
     int red=0,green=0,blue=0; 

// 변환 후의 이미지가 제대로 회색 음영으로 변환되지 않은 것 같습니다. 에 대한 어떤 실수 가있는 경우 내 코드에 찾아보세요 (X = mstart, X < (더블) mstart + mlength는, X ++)에 대한 {
(Y = 슬라이딩, 슬라이딩 Y < =; Y는 ++) {포크 및 조인 프레임 워크를 사용하여 이미지를 그레이 스케일로 변환합니다.

  int mindex = Math.min(Math.max(x+y , 0), msource.length - 1); 
      rgb=msource[mindex]; 
      //rgbVal = msource.getRGB(x,y); 
      r = (float)((rgb & 0xff0000) >>16) ; 
      g = (float)((rgb & 0x00ff00)>> 8); 
      b = (float)((rgb & 0x0000ff) >>0) ; 

    } 
      lum=(int) Math.round(0.2126*r+0.7152*g+0.0722*b); 
      grayval=(lum<<16)|(lum<<8)|lum; 
      mDestination[x] =(grayval); 
    } 
    } 


     protected static int sThreshold = 10000; 

    @Override 
    protected void compute() { 
     if (mlength < sThreshold) { 
      computeDirectly(mimage); 
      return; 
     } 

     int split = mlength/2; 

     invokeAll(new Multicore1(msource, mstart, split, mDestination,mimage), 
       new Multicore1(msource, mstart + split, mlength - split, 
       mDestination,mimage)); 
    } 

public static void main(String args[]) throws Exception 
{ 
    BufferedImage image=ImageIO.read(new File("xyz.jpg")); 
    int w=image.getWidth(); 
    int h=image.getHeight(); 
    BufferedImage convertedimg=grayscale(w,h,image); 

    String dstName = "Gray.jpg"; 
    File dstFile = new File(dstName); 
    ImageIO.write(convertedimg,"jpg",dstFile); 
} 
public static BufferedImage grayscale(int w,int h,BufferedImage image) 
{ 
    int[] src=image.getRGB(0, 0, w, h, null, 0, w); 
    int[] dst=new int[src.length]; 
     System.out.println("hello"); 
     System.out.println("Array size is " + src.length); 
     System.out.println("Threshold is " + sThreshold); 
    Multicore1 m1=new Multicore1(src,0,src.length,dst,image); 
    ForkJoinPool pool=new ForkJoinPool(); 

    long starttime=System.currentTimeMillis(); 
    pool.invoke(m1); 
    long endtime=System.currentTimeMillis(); 
    System.out.println("Time took:" +(endtime-starttime)); 
    BufferedImage dstimage=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB); 
    //Color c=new Color(0,0,0); 
    dstimage.setRGB(0, 0, w, h, dst, 0, w); 
    //dstimage.setRGB(0,0,c.getRGB()); 
    return dstimage; 
} 
} 

답변

0
grayval**=*(0xff000000)*|**(lum<<16)|(lum<<8)|lum; 

비트를 이동하기 전에 마스크를 적용해야합니다.

관련 문제