Statistics
| Branch: | Tag: | Revision:

securesmartcam / app / src / main / java / org / witness / obscuracam / photo / filters / ConvolutionMatrix.java @ 41590feb

History | View | Annotate | Download (2.45 KB)

1

    
2
/*
3
 * Many thanks to Pete Houston
4
 * http://xjaphx.wordpress.com/2011/06/22/image-processing-convolution-matrix/
5
 */
6
package org.witness.obscuracam.photo.filters;
7

    
8
import android.graphics.Bitmap;
9
import android.graphics.Color;
10

    
11

    
12
public class ConvolutionMatrix
13
{
14
    public static final int SIZE = 3;
15

    
16
    public double[][] Matrix;
17
    public double Factor = 1;
18
    public double Offset = 1;
19

    
20
    public ConvolutionMatrix(int size) {
21
        Matrix = new double[size][size];
22
    }
23

    
24
    public void setAll(double value) {
25
        for (int x = 0; x < SIZE; ++x) {
26
            for (int y = 0; y < SIZE; ++y) {
27
                Matrix[x][y] = value;
28
            }
29
        }
30
    }
31

    
32
    public void applyConfig(double[][] config) {
33
            for(int x = 0; x < SIZE; ++x) {
34
                    for(int y = 0; y < SIZE; ++y) {
35
                            Matrix[x][y] = config[x][y];
36
                    }
37
            }
38
    }
39

    
40
    public static Bitmap computeConvolution3x3(Bitmap src, ConvolutionMatrix matrix, int left, int right, int top, int bottom) {
41
            
42
            Bitmap result = Bitmap.createBitmap(right-left, bottom-top, src.getConfig());
43

    
44
            int A, R, G, B;
45
            int sumR, sumG, sumB;
46
            int[][] pixels = new int[SIZE][SIZE];
47

    
48
            for(int y = top; y < bottom - 2; ++y) {
49
                    for(int x = left; x < right - 2; ++x) {
50

    
51
                            // get pixel matrix
52
                            for(int i = 0; i < SIZE; ++i) {
53
                                    for(int j = 0; j < SIZE; ++j) {
54
                                            pixels[i][j] = src.getPixel(x + i, y + j);
55
                                    }
56
                            }
57

    
58
                            // get alpha of center pixel
59
                            A = Color.alpha(pixels[1][1]);
60

    
61
                            // init color sum
62
                            sumR = sumG = sumB = 0;
63

    
64
                            // get sum of RGB on matrix
65
                            for(int i = 0; i < SIZE; ++i) {
66
                                    for(int j = 0; j < SIZE; ++j) {
67
                                            sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
68
                                            sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
69
                                            sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
70
                                    }
71
                            }
72

    
73
                            // get final Red
74
                            R = (int)(sumR / matrix.Factor + matrix.Offset);
75
                            if(R < 0) { R = 0; }
76
                            else if(R > 255) { R = 255; }
77

    
78
                            // get final Green
79
                            G = (int)(sumG / matrix.Factor + matrix.Offset);
80
                            if(G < 0) { G = 0; }
81
                            else if(G > 255) { G = 255; }
82

    
83
                            // get final Blue
84
                            B = (int)(sumB / matrix.Factor + matrix.Offset);
85
                            if(B < 0) { B = 0; }
86
                            else if(B > 255) { B = 255; }
87

    
88
                            // apply new pixel
89
                            result.setPixel(x - left + 1, y - top + 1, Color.argb(A, R, G, B));
90
                    }
91
            }
92

    
93
            // final image
94
            return result;
95
    }
96
}