
#ifdef JAVA_MODUL

#include "java_raw_TIFF_reader_native.h"
#include "TIFF_reader.h"
using namespace MobilePhotoTool;

/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    create
 * Signature: (Ljava/lang/String;)J
 */
JNIEXPORT jlong JNICALL Java_java_1raw_TIFF_1reader_1native_create
  (JNIEnv *env, jobject obj, jstring s)
{
	jlong pointer = 0;
	const char *file;
	file = env->GetStringUTFChars(s,NULL);
	MPT_Exception *exception = new MPT_Exception();

	TIFF_reader *reader = NULL;
	
	reader = new TIFF_reader(exception, file);
	pointer = (jlong)reader;

	if(exception->exception == true){
		reader->~TIFF_reader();
		pointer = NULL;
	}

	return pointer;
}

/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    delete
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_java_1raw_TIFF_1reader_1native_delete
  (JNIEnv *env, jobject obj, jlong pointer)
{
	TIFF_reader *reader = NULL;
	if(pointer != 0){
		reader = (TIFF_reader*)pointer;
		reader->~TIFF_reader();
	}
}

/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    get_PreviewWidth
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_java_1raw_TIFF_1reader_1native_get_1PreviewWidth
  (JNIEnv *env, jobject obj, jlong pointer)
{
	jlong width = 0;
	TIFF_reader *reader = NULL;
	MPT_Exception *exception;
	if(pointer != 0){
		reader = (TIFF_reader*)pointer;
		
		exception = reader->get_exception();
		width = reader->get_preview_width(exception);

		if(exception->exception == true){
			jclass Exception = env->FindClass("java/lang/Exception");
			env->ThrowNew(Exception, exception->toString());
		}
	}

	return width;
}

/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    get_PreviewLength
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_java_1raw_TIFF_1reader_1native_get_1PreviewLength
  (JNIEnv *env, jobject obj, jlong pointer)
{
	jlong length = 0;
	TIFF_reader *reader = NULL;
	MPT_Exception *exception = NULL;
	if(pointer != 0){
		reader = (TIFF_reader*)pointer;
		
		exception = reader->get_exception();
		pointer = (jlong)reader;
		length = reader->get_preview_length(exception);

		if(exception->exception == true){
			jclass Exception = env->FindClass("java/lang/Exception");
			env->ThrowNew(Exception, exception->toString());
		}
	}

	return length;
}
/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    get_Preview
 * Signature: (J[I)J
 */
JNIEXPORT jlong JNICALL Java_java_1raw_TIFF_1reader_1native_get_1Preview
  (JNIEnv *env, jobject obj, jlong pointer, jintArray jiarray)
{
	TIFF_reader *reader = NULL;
	MPT_Exception *exception = NULL;
	int state = 0;
	unsigned int size;
	jint *jdata;

	if(pointer != 0){
		reader		= (TIFF_reader*)pointer;
		exception	= reader->get_exception();
		
		size = env->GetArrayLength(jiarray);
		if(size > 0){
			jdata = env->GetIntArrayElements(jiarray, NULL);
			state = reader->getPreview_int_RGB(exception, (unsigned int*)jdata, size);
			env->ReleaseIntArrayElements(jiarray, jdata, NULL);
		}

		if(exception->exception == true){
			jclass Exception = env->FindClass("java/lang/Exception");
			env->ThrowNew(Exception, exception->toString());
		}
	}

	return state;
}

/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    get_RawWidth
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_java_1raw_TIFF_1reader_1native_get_1RawWidth
  (JNIEnv *env, jobject obj, jlong pointer)
{
	jlong width					= 0;
	TIFF_reader *reader			= NULL;
	Raw_Processor *raw_p		= NULL;
	MPT_Exception *exception	= NULL;

	if(pointer != 0){
		reader		= (TIFF_reader*)pointer;
		exception	= reader->get_exception();
		
		raw_p = reader->get_Raw_Processor(exception);
		width = raw_p->get_Width(exception);
		MPT_EXCEPTION_TEST 0;

		if(exception->exception == true){
			jclass Exception = env->FindClass("java/lang/Exception");
			env->ThrowNew(Exception, exception->toString());
		}
		
	}

	return width;
}

/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    get_RawLength
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_java_1raw_TIFF_1reader_1native_get_1RawLength
  (JNIEnv *env, jobject obj, jlong pointer)
{
	jlong length				= 0;
	TIFF_reader *reader			= NULL;
	Raw_Processor *raw_p		= NULL;
	MPT_Exception *exception	= NULL;
	if(pointer != 0){
		reader = (TIFF_reader*)pointer;
		exception = reader->get_exception();

		raw_p = reader->get_Raw_Processor(exception);
		length = raw_p->get_Length(exception);

		if(exception->exception == true){
			jclass Exception = env->FindClass("java/lang/Exception");
			env->ThrowNew(Exception, exception->toString());
		}
	}

	return length;
}

/*
 * Class:     java_raw_TIFF_reader_native
 * Method:    get_Raw
 * Signature: (J[I)J
 */
JNIEXPORT jlong JNICALL Java_java_1raw_TIFF_1reader_1native_get_1Raw
  (JNIEnv *env, jobject obj, jlong pointer, jintArray jiarray)
{
	TIFF_reader *reader			= NULL;
	Raw_Processor *raw_p		= NULL;
	MPT_Exception *exception	= NULL;
	int state					= 0;
	unsigned int size;
	jint *jdata;

	if(pointer != 0){
		reader = (TIFF_reader*)pointer;
		exception = reader->get_exception();

		size = env->GetArrayLength(jiarray);
		if(size > 0){
			jdata = env->GetIntArrayElements(jiarray, NULL);

			raw_p = reader->get_Raw_Processor(exception);
			raw_p->updata_RGB(exception, (unsigned int*)jdata, size);
				
			env->ReleaseIntArrayElements(jiarray, jdata, NULL);
		}
		if(exception->exception == true){
			jclass Exception = env->FindClass("java/lang/Exception");
			env->ThrowNew(Exception, exception->toString());
		}
	}

	return state;

}

#endif
