1 public class Convolution extends Thread {
5 public Convolution(Image img, int x0, int x1, int y0, int y1) {
16 double[][] kernel = new double[kernelHeight][kernelWidth];
20 double tempinput[][] = img.inputImage;
21 double tempout[][] = img.outputImage;
23 double tinput1[] = tempinput[x0];
24 double tinput2[] = tempinput[x0+1];
25 double tinput3[] = tempinput[x0+2];
26 double tinput4[] = tempinput[x0+3];
27 double tinput0[] = tinput1;
30 for(int i=x0;i<x1;i++,l++){
31 double tout[] = tempout[i];
32 tinput0 = tinput1; tinput1=tinput2; tinput2=tinput3; tinput3=tinput4; tinput4=tempinput[l];
33 for(int j=y0;j<y1;++j){
35 for(int b=0;b<kernelHeight;++b){
36 tout[j] = tout[j] + (tinput0[j+b] * kernel[0][b] + tinput1[j+b] * kernel[1][b] + tinput2[j+b]*kernel[2][b] +
37 tinput3[j+b]*kernel[3][b] + tinput4[j+b]*kernel[4][b]);
44 public static void main(String[] args) {
48 NUM_THREADS = Integer.parseInt(args[0]);
50 SIZE = Integer.parseInt(args[1]);
53 int[] mid = new int[8];
54 mid[0] = (128<<24)|(195<<16)|(136<<8)|162; //dw-10
55 mid[1] = (128<<24)|(195<<16)|(136<<8)|163; //dw-11
56 mid[2] = (128<<24)|(195<<16)|(136<<8)|164; //dw-12
57 mid[3] = (128<<24)|(195<<16)|(136<<8)|165; //dw-13
58 mid[4] = (128<<24)|(195<<16)|(136<<8)|166; //dw-14
59 mid[5] = (128<<24)|(195<<16)|(136<<8)|167; //dw-15
60 mid[6] = (128<<24)|(195<<16)|(136<<8)|168; //dw-16
61 mid[7] = (128<<24)|(195<<16)|(136<<8)|169; //dw-17
70 img = global new Image(SIZE,SIZE,kernelHeight,kernelWidth);
72 conv = global new Convolution[NUM_THREADS];
73 int increment=SIZE/NUM_THREADS;
75 for(int i = 0; i<NUM_THREADS; i++) {
76 if((i+1)==NUM_THREADS)
77 conv[i] = global new Convolution(img, base, SIZE, 0, SIZE);
79 conv[i] = global new Convolution(img, base, base+increment, 0, SIZE);
86 System.printString("img.outputImage[10][20] = " +(int) img.outputImage[10][20] + "\n");
87 System.printString("img.outputImage[256][890] = " +(int) img.outputImage[256][890] + "\n");
91 for(int i = 0; i <NUM_THREADS; i++) {
98 for(int i = 0; i < NUM_THREADS; i++) {
104 System.printString("Done!");
108 System.printString("img.outputImage[10][20] = " +(int) img.outputImage[10][20] + "\n");
109 System.printString("img.outputImage[256][890] = " +(int) img.outputImage[256][890] + "\n");
114 //define 5X5 Gaussian kernel
115 public void initKernel(double[][] kernel) {
116 kernel[0][0] = 1/256.0;
117 kernel[0][1] = 4/256.0;
118 kernel[0][2] = 6/256.0;
119 kernel[0][3] = 4/256.0;
120 kernel[0][4] = 1/256.0;
121 kernel[1][0] = 4/256.0;
122 kernel[1][1] = 16/256.0;
123 kernel[1][2] = 24/256.0;
124 kernel[1][3] = 16/256.0;
125 kernel[1][4] = 4/256.0;
126 kernel[2][0] = 6/256.0;
127 kernel[2][1] = 24/256.0;
128 kernel[2][2] = 36/256.0;
129 kernel[2][3] = 24/256.0;
130 kernel[2][4] = 6/256.0;
131 kernel[3][0] = 4/256.0;
132 kernel[3][1] = 16/256.0;
133 kernel[3][2] = 24/256.0;
134 kernel[3][3] = 16/256.0;
135 kernel[3][4] = 4/256.0;
136 kernel[4][0] = 1/256.0;
137 kernel[4][1] = 4/256.0;
138 kernel[4][2] = 6/256.0;
139 kernel[4][3] = 4/256.0;
140 kernel[4][4] = 1/256.0;
146 int kernelWidth, kernelHeight;
147 double[][] inputImage;
148 double[][] outputImage;
150 public Image(int width, int height, int kernelWidth, int kernelHeight) {
152 this.height = height;
153 this.kernelWidth = kernelWidth;
154 this.kernelHeight = kernelHeight;
155 inputImage = global new double[height+kernelHeight-1][width+kernelWidth-1];
156 outputImage = global new double[height][width];
159 /* Create a valid image */
160 public void setValues() {
161 for (int i = 0; i < (height+kernelHeight - 1); i++) {
162 double ainput[] = inputImage[i];
163 for(int j = 0; j < (width+kernelWidth - 1); j++) {
168 for (int i = 0; i < height; i++){
169 double aout[] = outputImage[i];
170 for(int j = 0; j < width; j++) {