# Pastebin BMpR1PCe block convolute_greyscale(clock, enable, unsigned<6>[3] input) => (register unsigned<6>[3] output) { int i = 0, j = 0, k = 0; stream2d,5,5,1024> greyscale; //Convert pixel to greyscale, and push it into the stream greyscale << ((input[0] + input[1] + input[2]) / 3); //5x5 sobel matrices signed<5>[5][5] m1 = {{1, 2, 0, -2, -1}, {4, 8, 0, -8, -4}, {6, 12, 0, -12, -6}, {4, 8, 0, -8, -4}, {1, 2, 0, -2, -1}}; signed<5>[5][5] m2 = {{-1, -4, -6, -4, -1}, {-2, -8, -12, -8, -2}, {0, 0, 0, 0, 0}, {2, 8, 12, 8, 2}, {1, 4, 6, 4, 1}}; //convolute with horizontal and vertical sobel filters signed<16> sum1 = 0, sum2 = 0; for(j = 0; j < 5; j++) { for(k = 0; k < 5; k++) { sum1 += greyscale[j][k] * m1[j][k]; sum2 += greyscale[j][k] * m2[j][k]; } } //take the absolute value of both sums if(sum1 < 0) { sum1 = 0-sum1; } if(sum2 < 0) { sum2 = 0-sum2; } //add the absolute values of the two convolution results, //and normalise by dividing by 32 unsigned<9> sum = (sum1 + sum2) >> 5; unsigned<6> result; //clamp the output value to 63 if(sum > 63) { result = 63; } else { result = sum; } for(i = 0; i < 3; i++) { output[i] = result; } }