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    scalex = 1.f / radiusx;
42    scaley = 1.f / radiusy;
43
44    float S = 1 + finalSaturation / 100.f;
45    float MS = 1 - S;
46    float Rt = Rf * MS;
47    float Gt = Gf * MS;
48    float Bt = Bf * MS;
49
50    float b = 1 + finalBright / 100.f;
51    float c = 1 + finalContrast / 100.f;
52    b *= c;
53    offset = .5f - c / 2.f - finalSubtract / 100.f;
54    rsMatrixSet(&colorMatrix, 0, 0, b * (Rt + S));
55    rsMatrixSet(&colorMatrix, 1, 0, b * Gt);
56    rsMatrixSet(&colorMatrix, 2, 0, b * Bt);
57    rsMatrixSet(&colorMatrix, 0, 1, b * Rt);
58    rsMatrixSet(&colorMatrix, 1, 1, b * (Gt + S));
59    rsMatrixSet(&colorMatrix, 2, 1, b * Bt);
60    rsMatrixSet(&colorMatrix, 0, 2, b * Rt);
61    rsMatrixSet(&colorMatrix, 1, 2, b * Gt);
62    rsMatrixSet(&colorMatrix, 2, 2, b * (Bt + S));
63}
64
65uchar4 __attribute__((kernel)) vignette(const uchar4 in, uint32_t x,  uint32_t y) {
66    float4 pixel = rsUnpackColor8888(in);
67    float radx = (x - centerx) * scalex;
68    float rady = (y - centery) * scaley;
69    float dist = strength * (sqrt(radx * radx + rady * rady) - 1.f);
70    float t  =  (1.f + dist / sqrt(1.f + dist* dist)) * .5f;
71    float4 wsum = pixel;
72    wsum.xyz = wsum.xyz * (1 - t) + t * (rsMatrixMultiply(&colorMatrix, wsum.xyz) + offset);
73    wsum.a = 1.0f;
74    uchar4 out = rsPackColorTo8888(clamp(wsum, 0.f, 1.0f));
75    return out;
76}
77