1/*
2 * Copyright (C) 2012 The Android Open Source Project
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#include "ip.rsh"
18#pragma rs_fp_relaxed
19
20float vibrance = 0.f;
21
22static const float Rf = 0.2999f;
23static const float Gf = 0.587f;
24static const float Bf = 0.114f;
25
26static float Vib = 0.f;
27
28uchar4 RS_KERNEL vibranceKernel(uchar4 in) {
29    int r = in.r;
30    int g = in.g;
31    int b = in.b;
32    float red = (r-max(g, b)) * (1.f / 256.f);
33    float S = (float)(Vib/(1+native_exp(-red*3)))+1;
34    float MS = 1.0f - S;
35    float Rt = Rf * MS;
36    float Gt = Gf * MS;
37    float Bt = Bf * MS;
38    int t = (r + g) >> 1;
39
40    float R = r;
41    float G = g;
42    float B = b;
43
44    float Rc = R * (Rt + S) + G * Gt + B * Bt;
45    float Gc = R * Rt + G * (Gt + S) + B * Bt;
46    float Bc = R * Rt + G * Gt + B * (Bt + S);
47
48    uchar4 o;
49    o.r = rsClamp(Rc, 0, 255);
50    o.g = rsClamp(Gc, 0, 255);
51    o.b = rsClamp(Bc, 0, 255);
52    o.a = 0xff;
53    return o;
54}
55
56void prepareVibrance() {
57    Vib = vibrance/100.f;
58}
59