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.
19 void estmateWhite(unsigned char *src, int len, int *wr, int *wb, int *wg){
23 int *histR = (int *) malloc(256*sizeof(int));
24 int *histG = (int *) malloc(256*sizeof(int));
25 int *histB = (int *) malloc(256*sizeof(int));
27 for (i = 0; i < 255; i++) {
28 histR[i] = histG[i] = histB[i] =0;
31 for (i = 0; i < len; i+=STEP) {
33 histG[(src[GREEN])]++;
36 int min_r = -1, min_g = -1,min_b = -1;
37 int max_r = 0, max_g = 0,max_b = 0;
38 int sum_r = 0,sum_g=0,sum_b=0;
40 for (i = 1; i < RANGE-1; i++) {
49 if (min_r < 0) min_r = i;
53 if (min_g < 0) min_g = i;
57 if (min_b < 0) min_b = i;
62 int sum15r = 0,sum15g=0,sum15b=0;
63 int count15r = 0,count15g=0,count15b=0;
64 int tmp_r = 0,tmp_g=0,tmp_b=0;
66 for (i = RANGE-2; i >0; i--) {
74 if ((tmp_r > sum_r/20) && (tmp_r < sum_r/5)) {
78 if ((tmp_g > sum_g/20) && (tmp_g < sum_g/5)) {
82 if ((tmp_b > sum_b/20) && (tmp_b < sum_b/5)) {
92 if ((count15r>0) && (count15g>0) && (count15b>0) ){
93 *wr = sum15r/count15r;
94 *wb = sum15g/count15g;
95 *wg = sum15b/count15b;
101 void estmateWhiteBox(unsigned char *src, int iw, int ih, int x,int y, int *wr, int *wb, int *wg){
110 if (x>=(iw-bounds)) x = (iw-bounds-1);
111 if (y>=(ih-bounds)) y = (ih-bounds-1);
112 int startx = x - bounds;
113 int starty = y - bounds;
114 int endx = x + bounds;
115 int endy = y + bounds;
117 for(yp= starty;yp<endy;yp++) {
118 for(xp= startx;xp<endx;xp++) {
119 int i = 4*(xp+yp*iw);
131 void JNIFUNCF(ImageFilterWBalance, nativeApplyFilter, jobject bitmap, jint width, jint height, int locX,int locY)
133 char* destination = 0;
134 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
136 int len = width * height * 4;
137 unsigned char * rgb = (unsigned char * )destination;
143 estmateWhite(rgb,len,&wr,&wg,&wb);
145 estmateWhiteBox(rgb, width, height,locX,locY,&wr,&wg,&wb);
147 int min = MIN(wr, MIN(wg, wb));
148 int max = MAX(wr, MAX(wg, wb));
149 float avg = (min+max)/2.f;
150 float scaleR = avg/wr;
151 float scaleG = avg/wg;
152 float scaleB = avg/wb;
154 for (i = 0; i < len; i+=4)
164 rgb[RED] = clamp(Rc);
165 rgb[GREEN] = clamp(Gc);
166 rgb[BLUE] = clamp(Bc);
168 AndroidBitmap_unlockPixels(env, bitmap);