# Memory Free Bias Convolution

Feb 19th, 2015
370
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. public static void convolve(int[] pixels, int offset, int stride, int x, int y, int width, int height, int[][] matrix, int parts) {
2. int index = offset + x + (y*stride);
3. for (int j = 0; j < height; j++, index += stride) {
4. for (int k = 0; k < width; k++) {
5. int pos = index + k;
6. pixels[pos] = convolve(pixels,stride,pos, matrix, parts);
7. }
8. }
9. }
10.
11. private static int crimp(int color) {
12. return (color >= 0xFF) ? 0xFF : (color < 0) ? 0 : color;
13. }
14.
15. private static int convolve(int[] pixels, int stride, int index, int[][] matrix, int parts) {
16. int redSum = 0;
17. int greenSum = 0;
18. int blueSum = 0;
19.
20. int pixel, factor;
21.
22. for (int j = 0, m = matrix.length; j < m; j++, index+=stride) {
23. for (int k = 0, n = matrix[j].length; k < n; k++) {
24. pixel = pixels[index + k];
25. factor = matrix[j][k];
26.
27. redSum += factor * ((pixel >> 16) & 0xFF);
28. greenSum += factor * ((pixel >> 8) & 0xFF);
29. blueSum += factor * ((pixel) & 0xFF);
30. }
31. }
32. return 0xFF000000 | ((crimp(redSum / parts) << 16) | (crimp(greenSum / parts) << 8) | (crimp(blueSum / parts)));
33. }