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 <math.h>
18#include "filters.h"
19
20void JNIFUNCF(ImageFilterVibrance, nativeApplyFilter, jobject bitmap, jint width, jint height,  jfloat vibrance)
21{
22    char* destination = 0;
23    AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
24    int i;
25    int len = width * height * 4;
26    float Rf = 0.2999f;
27    float Gf = 0.587f;
28    float Bf = 0.114f;
29    float Vib = vibrance/100.f;
30    float S  = Vib+1;
31    float MS = 1.0f - S;
32    float Rt = Rf * MS;
33    float Gt = Gf * MS;
34    float Bt = Bf * MS;
35    float R, G, B;
36    for (i = 0; i < len; i+=4)
37    {
38        int r = destination[RED];
39        int g = destination[GREEN];
40        int b = destination[BLUE];
41        float red = (r-MAX(g, b))/256.f;
42        float sx = (float)(Vib/(1+exp(-red*3)));
43        S = sx+1;
44        MS = 1.0f - S;
45        Rt = Rf * MS;
46        Gt = Gf * MS;
47        Bt = Bf * MS;
48        R = r;
49        G = g;
50        B = b;
51
52        float Rc = R * (Rt + S) + G * Gt + B * Bt;
53        float Gc = R * Rt + G * (Gt + S) + B * Bt;
54        float Bc = R * Rt + G * Gt + B * (Bt + S);
55
56        destination[RED] = CLAMP(Rc);
57        destination[GREEN] = CLAMP(Gc);
58        destination[BLUE] = CLAMP(Bc);
59    }
60    AndroidBitmap_unlockPixels(env, bitmap);
61}
62