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 float sr = 0.f;
21static float sg = 0.f;
22static float sb = 0.f;
23
24void prepareBwFilter(uint32_t rw, uint32_t gw, uint32_t bw) {
25
26    sr = rw;
27    sg = gw;
28    sb = bw;
29
30    float imageMin = min(sg,sb);
31    imageMin = fmin(sr,imageMin);
32    float imageMax = max(sg,sb);
33    imageMax = fmax(sr,imageMax);
34    float avg = (imageMin + imageMax)/2;
35    sb /= avg;
36    sg /= avg;
37    sr /= avg;
38
39}
40
41void bwFilterKernel(const uchar4 *in, uchar4 *out) {
42    float r = in->r * sr;
43    float g = in->g * sg;
44    float b = in->b * sb;
45    float localMin, localMax, avg;
46    localMin = fmin(g,b);
47    localMin = fmin(r,localMin);
48    localMax = fmax(g,b);
49    localMax = fmax(r,localMax);
50    avg = (localMin+localMax) * 0.5f;
51    out->r = out->g = out->b = rsClamp(avg, 0, 255);
52}
53