1/*
2 * Copyright (C) 2013 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
20uint32_t inputWidth;
21uint32_t inputHeight;
22float centerx;
23float centery;
24float radiusx;
25float radiusy;
26float strength;
27float finalBright;
28float finalSaturation;
29float finalContrast;
30float finalSubtract;
31rs_matrix3x3 colorMatrix;
32float scalex;
33float scaley;
34float offset;
35static const float Rf = 0.2999f;
36static const float Gf = 0.587f;
37static const float Bf = 0.114f;
38
39
40void setupVignetteParams() {
41    int k = 0;
42
43    scalex = 1.f / radiusx;
44    scaley = 1.f / radiusy;
45
46    float S = 1 + finalSaturation / 100.f;
47    float MS = 1 - S;
48    float Rt = Rf * MS;
49    float Gt = Gf * MS;
50    float Bt = Bf * MS;
51
52    float b = 1 + finalBright / 100.f;
53    float c = 1 + finalContrast / 100.f;
54    b *= c;
55    offset = .5f - c / 2.f - finalSubtract / 100.f;
56    rsMatrixSet(&colorMatrix, 0, 0, b * (Rt + S));
57    rsMatrixSet(&colorMatrix, 1, 0, b * Gt);
58    rsMatrixSet(&colorMatrix, 2, 0, b * Bt);
59    rsMatrixSet(&colorMatrix, 0, 1, b * Rt);
60    rsMatrixSet(&colorMatrix, 1, 1, b * (Gt + S));
61    rsMatrixSet(&colorMatrix, 2, 1, b * Bt);
62    rsMatrixSet(&colorMatrix, 0, 2, b * Rt);
63    rsMatrixSet(&colorMatrix, 1, 2, b * Gt);
64    rsMatrixSet(&colorMatrix, 2, 2, b * (Bt + S));
65}
66
67uchar4 __attribute__((kernel)) vignette(const uchar4 in, uint32_t x,  uint32_t y) {
68    float4 pixel = rsUnpackColor8888(in);
69    float radx = (x - centerx) * scalex;
70    float rady = (y - centery) * scaley;
71    float dist = strength * (sqrt(radx * radx + rady * rady) - 1.f);
72    float t  =  (1.f + dist / sqrt(1.f + dist* dist)) * .5f;
73    float4 wsum = pixel;
74    wsum.xyz = wsum.xyz * (1 - t) + t * (rsMatrixMultiply(&colorMatrix, wsum.xyz) + offset);
75    wsum.a = 1.0f;
76    uchar4 out = rsPackColorTo8888(clamp(wsum, 0.f, 1.0f));
77    return out;
78}