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
20static int histR[256] = {0}, histG[256] = {0}, histB[256] = {0};
21
22rs_allocation histogramSource;
23uint32_t histogramHeight;
24uint32_t histogramWidth;
25
26static float3 scale;
27
28static uchar4 estimateWhite() {
29
30    for (int i = 0; i < 256; i++) {
31        histR[i] = 0; histG[i] = 0; histB[i] = 0;
32    }
33
34    for (uint32_t i = 0; i < histogramHeight; i++) {
35        for (uint32_t j = 0; j < histogramWidth; j++) {
36            uchar4 in = rsGetElementAt_uchar4(histogramSource, j, i);
37            histR[in.r]++;
38            histG[in.g]++;
39            histB[in.b]++;
40        }
41    }
42
43    int min_r = -1, min_g = -1, min_b = -1;
44    int max_r =  0, max_g =  0, max_b =  0;
45    int sum_r =  0, sum_g =  0, sum_b =  0;
46
47    for (int i = 1; i < 255; i++) {
48        int r = histR[i];
49        int g = histG[i];
50        int b = histB[i];
51        sum_r += r;
52        sum_g += g;
53        sum_b += b;
54
55        if (r>0){
56            if (min_r < 0) min_r = i;
57            max_r = i;
58        }
59        if (g>0){
60            if (min_g < 0) min_g = i;
61            max_g = i;
62        }
63        if (b>0){
64            if (min_b < 0) min_b = i;
65            max_b = i;
66        }
67    }
68
69    int sum15r = 0, sum15g = 0, sum15b = 0;
70    int count15r = 0, count15g = 0, count15b = 0;
71    int tmp_r = 0, tmp_g = 0, tmp_b = 0;
72
73    for (int i = 254; i >0; i--) {
74        int r = histR[i];
75        int g = histG[i];
76        int b = histB[i];
77        tmp_r += r;
78        tmp_g += g;
79        tmp_b += b;
80
81        if ((tmp_r > sum_r/20) && (tmp_r < sum_r/5)) {
82            sum15r += r*i;
83            count15r += r;
84        }
85        if ((tmp_g > sum_g/20) && (tmp_g < sum_g/5)) {
86            sum15g += g*i;
87            count15g += g;
88        }
89        if ((tmp_b > sum_b/20) && (tmp_b < sum_b/5)) {
90            sum15b += b*i;
91            count15b += b;
92        }
93
94    }
95
96    uchar4 out;
97
98    if ((count15r>0) && (count15g>0) && (count15b>0) ){
99        out.r = sum15r/count15r;
100        out.g = sum15g/count15g;
101        out.b = sum15b/count15b;
102    }else {
103        out.r = out.g = out.b = 255;
104    }
105
106    return out;
107
108}
109
110void prepareWhiteBalance() {
111    uchar4 estimation = estimateWhite();
112    int minimum = min(estimation.r, min(estimation.g, estimation.b));
113    int maximum = max(estimation.r, max(estimation.g, estimation.b));
114    float avg = (minimum + maximum) / 2.f;
115
116    scale.r =  avg/estimation.r;
117    scale.g =  avg/estimation.g;
118    scale.b =  avg/estimation.b;
119}
120
121uchar4 RS_KERNEL whiteBalanceKernel(uchar4 in) {
122    float3 t = convert_float3(in.rgb);
123    t *= scale;
124    t = min(t, 255.f);
125
126    uchar4 out;
127    out.rgb = convert_uchar3(t);
128    out.a = 255;
129    return out;
130}
131
132