
#ifdef JAVA_MODUL
#include <android/log.h>
#include <android/bitmap.h>

#include "de_uwesoftware_mobilRaw_MobilRawWrapper.h"
#include "MobilePhotoTool.h"
//using namespace MobilePhotoTool;
#define  LOG_TAG    "libmobilRaw"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

void move(unsigned short *image, int imagesizeX, int imagesizeY, int moveX, int moveY);


JNIEXPORT jlong JNICALL Java_de_uwesoftware_mobilRaw_MobilRawWrapper_setFile
  (JNIEnv *env, jobject obj, jstring path){

	jlong pointer = 0;
	const char *file;


	file = env->GetStringUTFChars(path,NULL);
	pointer = (jlong) create_Reader(file);


	return pointer;

}

JNIEXPORT jint JNICALL Java_de_uwesoftware_mobilRaw_MobilRawWrapper_setZoom
  (JNIEnv *env, jobject obj, jlong pointer, jfloat zoom, jintArray intarray)
{
	jint *zoom_size;
	unsigned int vector_size, result;
	void *reader = (void*) pointer;

	result = MPT_ERROR ;

	vector_size = env->GetArrayLength(intarray);
	if(vector_size >= 2){
		zoom_size = env->GetIntArrayElements(intarray, NULL);
		result = set_Zoom(reader, zoom, zoom_size);
		env->ReleaseIntArrayElements(intarray, zoom_size, NULL);
	}

	return result;
}

JNIEXPORT jint JNICALL Java_de_uwesoftware_mobilRaw_MobilRawWrapper_updateImage
  (JNIEnv *env, jobject obj, jlong pointer , jobject bitmap, jintArray variablen){
	AndroidBitmapInfo  info;
	void *reader = (void*)pointer;
	unsigned int result = MPT_ERROR;
	unsigned int vector_size;
	int ret;
	void * pixels;
	unsigned int *variable;

	vector_size = env->GetArrayLength(variablen);
	if(vector_size == 7){
		variable = (unsigned int*)env->GetIntArrayElements(variablen, NULL);

	    if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
	        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
	        return result;
	    }

	    if (info.format != ANDROID_BITMAP_FORMAT_RGB_565) {
	        LOGE("Bitmap format is not RGB_565 !");
	        return result;
	    }

	    if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
	        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
	        return result;
	    }
	    get_zoom_image_02(	reader,
							(unsigned short *)pixels,
							variable[0],
							variable[1],
							variable[2],
							variable[3],
							variable[4],
							variable[5],
							variable[6]);

	    env->ReleaseIntArrayElements(variablen, (jint*)variable, NULL);
	    AndroidBitmap_unlockPixels(env, bitmap);

	}
	return result;
}

jint JNICALL Java_de_uwesoftware_mobilRaw_MobilRawWrapper_moveBitmap2
  (JNIEnv *env, jobject obj, jobject bitmap, jint x, jint y)
{
	AndroidBitmapInfo  info;
	int sizeX, sizeY;
	void *pixels;
	int ret;
	int result = MPT_ERROR;

	if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
		LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
		return result;
	}

	if (info.format != ANDROID_BITMAP_FORMAT_RGB_565) {
		LOGE("Bitmap format is not RGB_565 !");
		return result;
	}

	if ((ret = AndroidBitmap_lockPixels(env, bitmap, (&pixels))) < 0) {
		LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
		return result;
	}
	sizeY = info.height;
	sizeX = info.width;

	move((unsigned short *)pixels, sizeX, sizeY, x, y);

	AndroidBitmap_unlockPixels(env, bitmap);
	result = MPT_SUCCESS;
	return result;
}

void move(unsigned short *image, int imagesizeX, int imagesizeY, int moveX, int moveY)
{
	int temp_moveX, temp_moveY;
	int posX, posY, end_posX, end_posY, posX2;

	if(moveX < 0){
		temp_moveX = -moveX;
	}else{
		temp_moveX = moveX;
	}

	if(moveY < 0){
		temp_moveY = -moveY;
	}else{
		temp_moveY = moveY;
	}

	if(imagesizeX > temp_moveX && imagesizeY > temp_moveY){
		 if(moveX != 0){
			 if(moveX < 0){
				 for(posY = 0; posY < imagesizeY; posY++){
					 posX		= posY * imagesizeX;
					 end_posX	= posX + imagesizeX + moveX;
					 for(; posX < end_posX; posX++){
						 image[posX] = image[posX - moveX];
					 }
				 }
			 }else{
				 for(posY = 0; posY < imagesizeY; posY++){
					 posX 		= (posY + 1) * imagesizeX - 1;
					 end_posX 	= posX - imagesizeX + moveX;
					 for(; posX > end_posX; posX--){
						 image[posX] = image[posX - moveX];
					 }
				 }
			 }
		 }

		 if(moveY != 0){
			 if(moveY < 0){
				 posY = 0;
				 end_posY = imagesizeY + moveY;
				 for(;posY < end_posY; posY++){
					 posX2		= (posY - moveY) * imagesizeX;
					 posX		= posY * imagesizeX;
					 end_posX	= posX + imagesizeX;
					 for(; posX < end_posX; posX++, posX2++){
						 image[posX] = image[posX2];
					 }
				 }
			 }else{
				 posY 		= imagesizeY - 1;
				 end_posY 	= posY - imagesizeY + moveY;
				 for(;posY > end_posY; posY--){
					 posX2 = (posY - moveY) * imagesizeX;
					 posX  = posY *imagesizeX;
					 end_posX = posX + imagesizeX;
					 for(;posX < end_posX; posX++, posX2++){
						 image[posX] = image[posX2];
					 }
				 }
			 }
		 }
	}

}

#endif
