2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 void estmateWhite(unsigned char *src, int len, int *wr, int *wb, int *wg){
25 int *histR = (int *) malloc(256*sizeof(int));
26 int *histG = (int *) malloc(256*sizeof(int));
27 int *histB = (int *) malloc(256*sizeof(int));
29 for (i = 0; i < 255; i++) {
30 histR[i] = histG[i] = histB[i] =0;
33 for (i = 0; i < len; i+=STEP) {
35 histG[(src[GREEN])]++;
38 int min_r = -1, min_g = -1,min_b = -1;
39 int max_r = 0, max_g = 0,max_b = 0;
40 int sum_r = 0,sum_g=0,sum_b=0;
42 for (i = 1; i < RANGE-1; i++) {
51 if (min_r < 0) min_r = i;
55 if (min_g < 0) min_g = i;
59 if (min_b < 0) min_b = i;
64 int sum15r = 0,sum15g=0,sum15b=0;
65 int count15r = 0,count15g=0,count15b=0;
66 int tmp_r = 0,tmp_g=0,tmp_b=0;
68 for (i = RANGE-2; i >0; i--) {
76 if ((tmp_r > sum_r/20) && (tmp_r < sum_r/5)) {
80 if ((tmp_g > sum_g/20) && (tmp_g < sum_g/5)) {
84 if ((tmp_b > sum_b/20) && (tmp_b < sum_b/5)) {
94 if ((count15r>0) && (count15g>0) && (count15b>0) ){
95 *wr = sum15r/count15r;
96 *wb = sum15g/count15g;
97 *wg = sum15b/count15b;
103 void estmateWhiteBox(unsigned char *src, int iw, int ih, int x,int y, int *wr, int *wb, int *wg){
112 if (x>=(iw-bounds)) x = (iw-bounds-1);
113 if (y>=(ih-bounds)) y = (ih-bounds-1);
114 int startx = x - bounds;
115 int starty = y - bounds;
116 int endx = x + bounds;
117 int endy = y + bounds;
119 for(yp= starty;yp<endy;yp++) {
120 for(xp= startx;xp<endx;xp++) {
121 int i = 4*(xp+yp*iw);
133 void JNIFUNCF(ImageFilterWBalance, nativeApplyFilter, jobject bitmap, jint width, jint height, int locX,int locY)
135 char* destination = 0;
136 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
138 int len = width * height * 4;
139 unsigned char * rgb = (unsigned char * )destination;
145 estmateWhite(rgb,len,&wr,&wg,&wb);
147 estmateWhiteBox(rgb, width, height,locX,locY,&wr,&wg,&wb);
149 int min = MIN(wr, MIN(wg, wb));
150 int max = MAX(wr, MAX(wg, wb));
151 float avg = (min+max)/2.f;
152 float scaleR = avg/wr;
153 float scaleG = avg/wg;
154 float scaleB = avg/wb;
156 for (i = 0; i < len; i+=4)
166 rgb[RED] = clamp(Rc);
167 rgb[GREEN] = clamp(Gc);
168 rgb[BLUE] = clamp(Bc);
170 AndroidBitmap_unlockPixels(env, bitmap);