Check for Potential Null Pointer

After calling the function AndroidBitmap_lockPixels(), neither the
return value nor the returned pointers were checked for the possible
failure. So the pointers "source" & "destination" could have invalid
value.

Along with changes above, we also have some compile warnings and code
format cleaned.

Test: mmm packages/apps/Gallery2, presubmit check.

Change-Id: I28a5d678311cb963bacd6f7b96ca2e12d498d965
Signed-off-by: Jing Mike <jingyangliu@eswincomputing.com>
diff --git a/jni/filters/edge.c b/jni/filters/edge.c
index 5e7be3c..75e99a1 100644
--- a/jni/filters/edge.c
+++ b/jni/filters/edge.c
@@ -46,7 +46,7 @@
     // set initial buffer to black
     memset(buf, 0, buf_len * sizeof(char));
     for (j = 3; j < buf_len; j+=4) {
-        *(buf + j) = 255;  // set initial alphas
+        *(buf + j) = (uint8_t) 255;  // set initial alphas
     }
 
     // apply sobel filter
@@ -120,7 +120,7 @@
     int last_row = row_stride * (height - 1);
     memset((dst + last_row), 0, row_stride * sizeof(char));
     for (j = 3; j < row_stride; j+=4) {
-        *(dst + last_row + j) = 255;  // set alphas
+        *(dst + last_row + j) = (uint8_t) 255;  // set alphas
     }
     AndroidBitmap_unlockPixels(env, bitmap);
 }
diff --git a/jni/filters/geometry.c b/jni/filters/geometry.c
index c2d80f3..0230922 100644
--- a/jni/filters/geometry.c
+++ b/jni/filters/geometry.c
@@ -170,11 +170,13 @@
     AndroidBitmap_lockPixels(env, src, (void**) &source);
     AndroidBitmap_lockPixels(env, dst, (void**) &destination);
     // TODO: implement straighten
-    int i = 0;
-    for (; i < len; i += 4) {
-        destination[RED] = 128;
-        destination[GREEN] = source[GREEN];
-        destination[BLUE] = 128;
+    if (source != NULL && destination != NULL) {
+        int i = 0;
+        for (; i < len; i += 4) {
+            destination[RED] = (uint8_t) 128;
+            destination[GREEN] = (uint8_t) source[GREEN];
+            destination[BLUE] = (uint8_t) 128;
+        }
     }
     AndroidBitmap_unlockPixels(env, dst);
     AndroidBitmap_unlockPixels(env, src);
diff --git a/jni/filters/wbalance.c b/jni/filters/wbalance.c
index 99a7d2e..2ca4c72 100644
--- a/jni/filters/wbalance.c
+++ b/jni/filters/wbalance.c
@@ -18,7 +18,7 @@
 
 #include "filters.h"
 
-void estmateWhite(unsigned char *src, int len, int *wr, int *wb, int *wg){
+void estmateWhite(unsigned char *src, int len, int *wr, int *wb, int *wg) {
 
     int STEP = 4;
     int RANGE = 256;
@@ -27,7 +27,7 @@
     int *histB = (int *) malloc(256*sizeof(int));
     int i;
     for (i = 0; i < 255; i++) {
-        histR[i] = histG[i] = histB[i] =0;
+        histR[i] = histG[i] = histB[i] = 0;
     }
 
     for (i = 0; i < len; i+=STEP) {
@@ -35,9 +35,8 @@
         histG[(src[GREEN])]++;
         histB[(src[BLUE])]++;
     }
-    int min_r = -1, min_g = -1,min_b = -1;
-    int max_r = 0, max_g = 0,max_b = 0;
-    int sum_r = 0,sum_g=0,sum_b=0;
+    int min_r = -1, min_g = -1, min_b = -1;
+    int sum_r = 0, sum_g = 0, sum_b = 0;
 
     for (i = 1; i < RANGE-1; i++) {
         int r = histR[i];
@@ -47,25 +46,22 @@
         sum_g += g;
         sum_b += b;
 
-        if (r>0){
-            if (min_r < 0) min_r = i;
-            max_r = i;
+        if (r > 0 && min_r < 0) {
+            min_r = i;
         }
-        if (g>0){
-            if (min_g < 0) min_g = i;
-            max_g = i;
+        if (g > 0 && min_g < 0) {
+            min_g = i;
         }
-        if (b>0){
-            if (min_b < 0) min_b = i;
-            max_b = i;
+        if (b > 0 && min_b < 0) {
+            min_b = i;
         }
     }
 
-    int sum15r = 0,sum15g=0,sum15b=0;
-    int count15r = 0,count15g=0,count15b=0;
-    int tmp_r = 0,tmp_g=0,tmp_b=0;
+    int sum15r = 0, sum15g = 0, sum15b = 0;
+    int count15r = 0, count15g=0, count15b = 0;
+    int tmp_r = 0, tmp_g = 0, tmp_b = 0;
 
-    for (i = RANGE-2; i >0; i--) {
+    for (i = RANGE-2; i > 0; i--) {
         int r = histR[i];
         int g = histG[i];
         int b = histB[i];
@@ -91,33 +87,33 @@
     free(histG);
     free(histB);
 
-    if ((count15r>0) && (count15g>0) && (count15b>0) ){
+    if ((count15r > 0) && (count15g > 0) && (count15b > 0)) {
         *wr = sum15r/count15r;
         *wb = sum15g/count15g;
         *wg = sum15b/count15b;
-    }else {
+    } else {
         *wg  = *wb = *wr=255;
     }
 }
 
-void estmateWhiteBox(unsigned char *src, int iw, int ih, int x,int y, int *wr, int *wb, int *wg){
+void estmateWhiteBox(unsigned char *src, int iw, int ih, int x,int y, int *wr, int *wb, int *wg) {
     int r = 0;
     int g = 0;
     int b = 0;
     int sum = 0;
-    int xp,yp;
+    int xp, yp;
     int bounds = 5;
-    if (x<0) x = bounds;
-    if (y<0) y = bounds;
-    if (x>=(iw-bounds)) x = (iw-bounds-1);
-    if (y>=(ih-bounds)) y = (ih-bounds-1);
+    if (x < 0) x = bounds;
+    if (y < 0) y = bounds;
+    if (x >= (iw-bounds)) x = (iw-bounds-1);
+    if (y >= (ih-bounds)) y = (ih-bounds-1);
     int startx = x - bounds;
     int starty = y - bounds;
     int endx = x + bounds;
     int endy = y + bounds;
 
-    for(yp= starty;yp<endy;yp++) {
-        for(xp= startx;xp<endx;xp++) {
+    for (yp = starty; yp < endy; yp++) {
+        for (xp = startx; xp < endx; xp++) {
             int i = 4*(xp+yp*iw);
             r += src[RED];
             g += src[GREEN];
@@ -141,7 +137,7 @@
     int wg;
     int wb;
 
-    if (locX==-1)
+    if (locX == -1)
         estmateWhite(rgb,len,&wr,&wg,&wb);
     else
         estmateWhiteBox(rgb, width, height,locX,locY,&wr,&wg,&wb);
@@ -153,7 +149,7 @@
     float scaleG =  avg/wg;
     float scaleB =  avg/wb;
 
-    for (i = 0; i < len; i+=4)
+    for (i = 0; i < len; i += 4)
     {
         int r = rgb[RED];
         int g = rgb[GREEN];