I want to implement invert effect in image processing. I decode color channels in java side the I pass a 2D array to C side, I invert (255-value) then I return a processed 2D array.
Here is my C code:
#include <jni.h>
#include<stddef.h>
#include <stdio.h>
#include<com_example_invert_MainActivity.h>
JNIEXPORT jobjectArray JNICALL Java_com_example_invert_MainActivity_inv
(JNIEnv *env, jobject obj, jobjectArray arr, jint w, jint h)
{
double a[w][h][3];
int i,j,k;
double x = 0;
///////////////////READING THE INPUT ARRAY////////////////////////
jsize dim1 = (*env)->GetArrayLength(env, arr);
for (i=0; i<dim1; i++){
jdoubleArray *line1 = (*env)->GetObjectArrayElement(env, arr, i);
int dim2 = (*env)->GetArrayLength(env, line1);
jdouble *pos1 = (*env)->GetDoubleArrayElements(env, line1, 0);
for (j=0; j<dim2; j++){
jdoubleArray *line2 = (*env)->GetObjectArrayElement(env, line1, j);
int dim3 = (*env)->GetArrayLength(env, line2);
jdouble *pos2 = (*env)->GetDoubleArrayElements(env, line2, 0);
for (k=0; k<dim3; k++){
a[i][j][k]= pos2[k];
}
(*env)->ReleaseDoubleArrayElements(env, arr, pos2, 0);
(*env)->ReleaseDoubleArrayElements(env, arr, line2, 0);
}
(*env)->ReleaseDoubleArrayElements(env, arr, pos1, 0);
(*env)->ReleaseDoubleArrayElements(env, arr, line1, 0);
}
/////////////////PROCESSING...///////////////////
for( i = 0; i<w; i++){
for( j = 0; j<h; j++){
for( k = 0; k<3; k++){
a[i][j][k] = 255-a[i][j][k];
}
}
}
//////////////RETURNING THE ARRAY////////////////////////////
jclass doubleArrayArrayClass = (*env)->FindClass(env,"[[D");
jclass doubleArrayClass = (*env)->FindClass(env,"[D");
jobjectArray ret = (*env)->NewObjectArray(env,w, doubleArrayArrayClass, NULL);
for( i = 0; i<w; i++){
jobjectArray dim2 = (*env)->NewObjectArray(env, w, doubleArrayClass, NULL);
for( j = 0; j<h; j++) {
jdoubleArray dim1 = (*env)->NewDoubleArray(env,h);
jdouble tmp[256];
for( k = 0; k<3; k++){
tmp[k] = a[i][j][k];
}
(*env)->SetDoubleArrayRegion(env,dim1 , 0, 3, tmp);
(*env)->SetObjectArrayElement(env, dim2, j, dim1);
(*env)->DeleteLocalRef(env, dim1);
}
(*env)->SetObjectArrayElement(env,ret, i, dim2);
(*env)->DeleteLocalRef(env,dim2);
}
return ret;
}
And here is the java code:
public class MainActivity extends ActionBarActivity {
ImageView imageView2;
double[][][] imgArray;
int w,h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView2 = (ImageView) findViewById(R.id.imageView1);
imageView2.setDrawingCacheEnabled(true);
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView2.getDrawable();
final Bitmap bitmap = bitmapDrawable.getBitmap();
Button button = (Button) findViewById(R.id.button1);
w = bitmap.getWidth();
h = bitmap.getHeight();
imgArray = new double[w][h][3];
for(int i = 0 ; i<w; i++){
for(int j =0; j<h; j++){
imgArray[i][j][0] = Color.red(bitmap.getPixel(i, j));
imgArray[i][j][1] = Color.green(bitmap.getPixel(i, j));
imgArray[i][j][2] = Color.blue(bitmap.getPixel(i, j));
}
}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imgArray = inv(imgArray, w, h);
Bitmap newBitmap = Bitmap.createBitmap(w,h,bitmap.getConfig());
for(int i = 0 ; i<w; i++){
for(int j =0; j<h; j++){
newBitmap.setPixel(i, j, Color.rgb((int)(imgArray[i][j][0]), (int)(imgArray[i][j][1]), (int)(imgArray[i][j][2])));
}
}
imageView2.setImageBitmap(newBitmap);
}
});
}
static{
System.loadLibrary("inv");
}
// internal, private
public native double[][][] inv(double[][][] inputArr, int w, int h);
...
}
The application crashes with logcat error: W/dalvikvm(5009): JNI: unpinPrimitiveArray(0x424eaea0) failed to find entry (valid=1)