Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void blur(int[] pixels, int width, int height) {
- int[][] matrix = new int[][]{
- {1, 1, 1},
- {1, 1, 1},
- {1, 1, 1}
- };
- int parts = 9;
- convolve(pixels, 0, width, 15, 30, 5, 5, matrix, parts);
- }
- public void emboss(int[] pixels, int width, int height) {
- int[][] matrix = new int[][]{
- {2, 0, 0},
- {0, -1, 0},
- {0, 0, -1}
- };
- int parts = 1;
- convolve(pixels, 0, width, 0, 0, width - 2, height - 2, matrix, parts);
- }
- public static void convolve(int[] pixels, int offset, int stride, int x, int y, int width, int height, int[][] matrix, int parts) {
- int startIndex = x + (y * stride);
- int lastIndex = (x + width-1) + ((y + height-1) * stride);
- int x_pos = x;
- int y_pos = y;
- int indexInc = 1;
- int yinc = 0;
- int xinc = 1;
- for (int i = 0, s = width + height; i < s; i++) {
- if (i==width) {
- indexInc=stride;
- yinc = 1;
- xinc = 0;
- }
- int x_counter = x_pos;
- int index = startIndex;
- while (x_counter >= x && index <= lastIndex) {
- pixels[offset + index] = convolve(pixels, stride, offset + index, matrix, parts);
- x_counter--;
- index += stride - 1;
- }
- startIndex += indexInc;
- x_pos += xinc;
- y_pos += yinc;
- }
- }
- private static int crimp(int color) {
- return (color >= 0xFF) ? 0xFF : (color < 0) ? 0 : color;
- }
- private static int convolve(int[] pixels, int stride, int index, int[][] matrix, int parts) {
- int redSum = 0;
- int greenSum = 0;
- int blueSum = 0;
- int pixel, factor;
- for (int j = 0, m = matrix.length; j < m; j++, index++) {
- for (int k = 0, n = matrix[j].length, q = index; k < n; k++, q += stride) {
- pixel = pixels[q];
- factor = matrix[j][k];
- redSum += factor * ((pixel >> 16) & 0xFF);
- greenSum += factor * ((pixel >> 8) & 0xFF);
- blueSum += factor * ((pixel) & 0xFF);
- }
- }
- return 0xFF000000 | ((crimp(redSum / parts) << 16) | (crimp(greenSum / parts) << 8) | (crimp(blueSum / parts)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement