1 /*
2  * Copyright (C) 2012 Unknown
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma version(1)
18 #pragma rs java_package_name(com.android.gallery3d.filtershow.filters)
19 
20 #define MAX_POINTS 16
21 
22 uint32_t inputWidth;
23 uint32_t inputHeight;
24 static const float Rf = 0.2999f;
25 static const float Gf = 0.587f;
26 static const float Bf = 0.114f;
27 //static const float size_scale = 0.01f;
28 
29 typedef struct {
30     rs_matrix3x3 colorMatrix;
31     float rgbOff;
32     float dx;
33     float dy;
34     float off;
35 } UPointData;
36 int mNumberOfLines;
37 // input data
38 bool mask[MAX_POINTS];
39 int xPos1[MAX_POINTS];
40 int yPos1[MAX_POINTS];
41 int xPos2[MAX_POINTS];
42 int yPos2[MAX_POINTS];
43 int size[MAX_POINTS];
44 int brightness[MAX_POINTS];
45 int contrast[MAX_POINTS];
46 int saturation[MAX_POINTS];
47 
48 // generated data
49 static UPointData grads[MAX_POINTS];
50 
51 void setupGradParams() {
52     int k = 0;
53     for (int i = 0; i < MAX_POINTS; i++) {
54       if (!mask[i]) {
55          continue;
56       }
57       float x1 = xPos1[i];
58       float y1 = yPos1[i];
59       float x2 = xPos2[i];
60       float y2 = yPos2[i];
61 
62       float denom = (y2 * y2 - 2 * y1 * y2 + x2 * x2 - 2 * x1 * x2 + y1 * y1 + x1 * x1);
63       if (denom == 0) {
64          continue;
65       }
66       grads[k].dy = (y1 - y2) / denom;
67       grads[k].dx = (x1 - x2) / denom;
68       grads[k].off = (y2 * y2 + x2 * x2 - x1 * x2 - y1 * y2) / denom;
69 
70       float S = 1+saturation[i]/100.f;
71       float MS = 1-S;
72       float Rt = Rf * MS;
73       float Gt = Gf * MS;
74       float Bt = Bf * MS;
75 
76       float b = 1+brightness[i]/100.f;
77       float c = 1+contrast[i]/100.f;
78       b *= c;
79       grads[k].rgbOff = .5f - c/2.f;
80       rsMatrixSet(&grads[i].colorMatrix, 0, 0, b * (Rt + S));
81       rsMatrixSet(&grads[i].colorMatrix, 1, 0, b * Gt);
82       rsMatrixSet(&grads[i].colorMatrix, 2, 0, b * Bt);
83       rsMatrixSet(&grads[i].colorMatrix, 0, 1, b * Rt);
84       rsMatrixSet(&grads[i].colorMatrix, 1, 1, b * (Gt + S));
85       rsMatrixSet(&grads[i].colorMatrix, 2, 1, b * Bt);
86       rsMatrixSet(&grads[i].colorMatrix, 0, 2, b * Rt);
87       rsMatrixSet(&grads[i].colorMatrix, 1, 2, b * Gt);
88       rsMatrixSet(&grads[i].colorMatrix, 2, 2, b * (Bt + S));
89 
90       k++;
91     }
92     mNumberOfLines = k;
93 }
94 
95 void init() {
96 
97 }
98 
99 uchar4 __attribute__((kernel)) selectiveAdjust(const uchar4 in, uint32_t x,
100     uint32_t y) {
101     float4 pixel = rsUnpackColor8888(in);
102 
103     float4 wsum = pixel;
104     wsum.a = 0.f;
105     for (int i = 0; i < mNumberOfLines; i++) {
106         UPointData* grad = &grads[i];
107         float t = clamp(x*grad->dx+y*grad->dy+grad->off,0.f,1.0f);
108         wsum.xyz = wsum.xyz*(1-t)+
109             t*(rsMatrixMultiply(&grad->colorMatrix ,wsum.xyz)+grad->rgbOff);
110 
111     }
112 
113     pixel.rgb = wsum.rgb;
114     pixel.a = 1.0f;
115 
116     uchar4 out = rsPackColorTo8888(clamp(pixel, 0.f, 1.0f));
117     return out;
118 }
119 
120 
121 
122