title: implement water color effect on image using JNI
body: <p>i implement some code for water color effect on image in android but it was to slow(it's take more then 2 minute) now i try to implement this in JNI for batter speed ,
  hear is my java code for </p>
  
  <p>the inPixels is pixel of Bitmap .</p>
  
  <pre><code> protected int[] filterPixels( int width, int height, int[] inPixels ) 
      {
          int levels = 256;
          int index = 0;
  
          int[] rHistogram = new int[levels];
          int[] gHistogram = new int[levels];
          int[] bHistogram = new int[levels];
          int[] rTotal = new int[levels];
          int[] gTotal = new int[levels];
          int[] bTotal = new int[levels];
          int[] outPixels = new int[width * height];
  
          for (int y = 0; y &lt; height; y++) 
          {
              for (int x = 0; x &lt; width; x++) 
              {
                  for (int i = 0; i &lt; levels; i++)
                      rHistogram[i] = gHistogram[i] = bHistogram[i] = rTotal[i] = gTotal[i] = bTotal[i] = 0;
  
                  for (int row = -range; row &lt;= range; row++) 
                  {
                      int iy = y+row;
                      int ioffset;
                      if (0 &lt;= iy &amp;&amp; iy &lt; height) 
                      {
                          ioffset = iy*width;
                          for (int col = -range; col &lt;= range; col++) 
                          {
                              int ix = x+col;
                              if (0 &lt;= ix &amp;&amp; ix &lt; width) {
                                  int rgb = inPixels[ioffset+ix];
                                  int r = (rgb &gt;&gt; 16) &amp; 0xff;
                                  int g = (rgb &gt;&gt; 8) &amp; 0xff;
                                  int b = rgb &amp; 0xff;
                                  int ri = r*levels/256;
                                  int gi = g*levels/256;
                                  int bi = b*levels/256;
                                  rTotal[ri] += r;
                                  gTotal[gi] += g;
                                  bTotal[bi] += b;
                                  rHistogram[ri]++;
                                  gHistogram[gi]++;
                                  bHistogram[bi]++;
                              }
                          }
                      }
                  }
  
                  int r = 0, g = 0, b = 0;
                  for (int i = 1; i &lt; levels; i++) 
                  {
                      if (rHistogram[i] &gt; rHistogram[r])
                          r = i;
                      if (gHistogram[i] &gt; gHistogram[g])
                          g = i;
                      if (bHistogram[i] &gt; bHistogram[b])
                          b = i;
                  }
                  r = rTotal[r] / rHistogram[r];
                  g = gTotal[g] / gHistogram[g];
                  b = bTotal[b] / bHistogram[b];
                  outPixels[index] = (inPixels[index] &amp; 0xff000000) | ( r &lt;&lt; 16 ) | ( g &lt;&lt; 8 ) | b;
                  index++;
              }
          }
  
          return outPixels;
      }
  </code></pre>
  
  <p>**OUTPUT image **
  <img src="https://i.stack.imgur.com/arZP0.png" alt="enter image description here"></p>
  
  <p>and i try to convert this <code>java</code> code to <code>c</code> code but i don't what is the wrong ,
  hear the code for <code>C</code> </p>
  
  <pre><code> void filterPixels( int width, int height, int inPixels[] )
      {
  
  
  
          int levels = 256;
          int index = 0;
  
          int rHistogram [levels];
          int gHistogram [levels];
          int bHistogram [levels];
          int rTotal   [levels];
          int gTotal [levels];
          int bTotal [levels];
          int outPixels [width * height];
  
          //Loop Variables
          int y ;
          int x ;
          int i ;
          int row ;
          int col ;
          int j ;
  
          int range = 5 ;
  
          for ( y = 0; y &lt; height; y++)
          {
              for ( x = 0; x &lt; width; x++)
              {
                  for ( i = 0; i &lt; levels; i++)
                      rHistogram[i] = gHistogram[i] = bHistogram[i] = rTotal[i] = gTotal[i] = bTotal[i] = 0;
  
                  for ( row = -range; row &lt;= range; row++)
                  {
                      int iy = y+row;
                      int ioffset;
                      if (0 &lt;= iy &amp;&amp; iy &lt; height)
                      {
                          ioffset = iy*width;
                          for ( col = -range; col &lt;= range; col++)
                          {
                              int ix = x+col;
                              if (0 &lt;= ix &amp;&amp; ix &lt; width) {
                                  int rgb = inPixels[ioffset+ix];
                                  int r = (rgb &gt;&gt; 16) &amp; 0xff;
                                  int g = (rgb &gt;&gt; 8) &amp; 0xff;
                                  int b = rgb &amp; 0xff;
                                  int ri = r*levels/256;
                                  int gi = g*levels/256;
                                  int bi = b*levels/256;
                                  rTotal[ri] += r;
                                  gTotal[gi] += g;
                                  bTotal[bi] += b;
                                  rHistogram[ri]++;
                                  gHistogram[gi]++;
                                  bHistogram[bi]++;
                              }
                          }
                      }
                  }
  
                  int r = 0, g = 0, b = 0;
                  for ( j = 1; j &lt; levels; j++)
                  {
                      if (rHistogram[j] &gt; rHistogram[r])
                          r = j;
                      if (gHistogram[j] &gt; gHistogram[g])
                          g = j;
                      if (bHistogram[j] &gt; bHistogram[b])
                          b = j;
                  }
                  r = rTotal[r] / rHistogram[r];
                  g = gTotal[g] / gHistogram[g];
                  b = bTotal[b] / bHistogram[b];
                  outPixels[index] = (inPixels[index] &amp; 0xff000000) | ( r &lt;&lt; 16 ) | ( g &lt;&lt; 8 ) | b;
                  index++;
              }
          }
      }
  </code></pre>
  
  <p>i check the pixel value of <code>java</code> code and <code>c</code> code both are same(for same image)</p>
  
  <p>code for call native function from my android activity .</p>
  
  <pre><code>int[] pix = new int[oraginal.getWidth() * oraginal.getHeight()];
  
                  Bitmap bitmap = oraginal.copy(oraginal.getConfig(), true);
                  bitmap.getPixels(pix, 0, bitmap.getWidth(), 0, 0,bitmap.getWidth(), bitmap.getHeight());
                  filterPixelsJNI(bitmap.getWidth(), bitmap.getHeight(), pix);
  
                   bitmap.setPixels(pix, 0, bitmap.getWidth(), 0, 0,bitmap.getWidth(), bitmap.getHeight());
                   myView.setImageBitmap(bitmap);
  </code></pre>
  
  <p>this is my first try for JNI so plz help me in this . </p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>public native void filterPixelsJNI( int width, int height, int inPixels[] );
  </code></pre>
  
  <p><strong>JNI</strong> </p>
  
  <pre><code> JNIEXPORT void JNICALL Java_com_testndk_HelloWorldActivity_filterPixelsJNI (JNIEnv * env, jobject obj , jint width,jint height,jint inPixels[]){
  
       filterPixels( width, height, inPixels);
   }
  </code></pre>
  
  <p>filterPixels method witch is call from c code . </p>