1 public class Convolution {
5 public Convolution(Image img, int x0, int x1, int y0, int y1) {
16 double[][] kernel = new double[kernelHeight][kernelWidth];
19 double tempinput[][] = img.inputImage;
20 double tempout[][] = img.outputImage;
22 double tinput0[] = tempinput[x0];
23 double tinput1[] = tempinput[x0+1];
24 double tinput2[] = tempinput[x0+2];
25 double tinput3[] = tempinput[x0+3];
26 double tinput4[] = tempinput[x0+4];
28 for(int i=x0;i<x1;++i){
29 double tout[] = tempout[x0];
30 for(int j=y0;j<y1;++j){
32 for(int b=0;b<kernelHeight;++b){
33 tout[y0] = tout[y0] + (tinput0[j+b] * kernel[0][b] + tinput1[j+b] * kernel[1][b] + tinput2[j+b]*kernel[2][b] +
34 tinput3[j+b]*kernel[3][b] + tinput4[j+b]*kernel[4][b]);
38 tinput0 = tinput1; tinput1=tinput2; tinput2=tinput3; tinput3=tinput4; tinput4=tempinput[i+5];
43 public static void main(String[] args) {
47 NUM_THREADS = Integer.parseInt(args[0]);
49 SIZE = Integer.parseInt(args[1]);
58 img = new Image(SIZE,SIZE,kernelHeight,kernelWidth);
60 conv = new Convolution[NUM_THREADS];
61 int increment=SIZE/NUM_THREADS;
63 for(int i = 0; i<NUM_THREADS; i++) {
64 if((i+1)==NUM_THREADS)
65 conv[i] = new Convolution(img, base, SIZE, 0, SIZE);
67 conv[i] = new Convolution(img, base, base+increment, 0, SIZE);
71 for(int i = 0; i <NUM_THREADS; i++) {
76 System.printString("Done!");
79 //define 5X5 Gaussian kernel
80 public void initKernel(double[][] kernel) {
81 kernel[0][0] = 1/256.0;
82 kernel[0][1] = 4/256.0;
83 kernel[0][2] = 6/256.0;
84 kernel[0][3] = 4/256.0;
85 kernel[0][4] = 1/256.0;
86 kernel[1][0] = 4/256.0;
87 kernel[1][1] = 16/256.0;
88 kernel[1][2] = 24/256.0;
89 kernel[1][3] = 16/256.0;
90 kernel[1][4] = 4/256.0;
91 kernel[2][0] = 6/256.0;
92 kernel[2][1] = 24/256.0;
93 kernel[2][2] = 36/256.0;
94 kernel[2][3] = 24/256.0;
95 kernel[2][4] = 6/256.0;
96 kernel[3][0] = 4/256.0;
97 kernel[3][1] = 16/256.0;
98 kernel[3][2] = 24/256.0;
99 kernel[3][3] = 16/256.0;
100 kernel[3][4] = 4/256.0;
101 kernel[4][0] = 1/256.0;
102 kernel[4][1] = 4/256.0;
103 kernel[4][2] = 6/256.0;
104 kernel[4][3] = 4/256.0;
105 kernel[4][4] = 1/256.0;
111 int kernelWidth, kernelHeight;
112 double[][] inputImage;
113 double[][] outputImage;
115 public Image(int width, int height, int kernelWidth, int kernelHeight) {
117 this.height = height;
118 this.kernelWidth = kernelWidth;
119 this.kernelHeight = kernelHeight;
120 inputImage = new double[height+kernelHeight-1][width+kernelWidth-1];
121 outputImage = new double[height][width];
124 /* Create a valid image */
125 public void setValues() {
126 for (int i = 0; i < (height+kernelHeight - 1); i++) {
127 double ainput[] = inputImage[i];
128 for(int j = 0; j < (width+kernelWidth - 1); j++) {
133 for (int i = 0; i < height; i++){
134 double aout[] = outputImage[i];
135 for(int j = 0; j < width; j++) {