// MobilePhotoTool.cpp : Definiert den Einstiegspunkt f�r die Konsolenanwendung.
//

#include "TIFF_reader.h"
#include "Raw_Processor.h"
#include "MobilePhotoTool.h"
//#include <iostream>
using namespace std;
using namespace MobilePhotoTool;


void *create_Reader(const char *file)
{

	TIFF_reader *reader = NULL;
	MPT_Exception *exception;

	exception = new MPT_Exception();
	reader = new TIFF_reader(exception, file);

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

	return reader;
}


void delete_Reader(void *reader)
{
	TIFF_reader *reader1;
	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		delete reader1;
	}
}

unsigned int get_PreviewWidth(void *reader)
{
	unsigned int width = 0;
	TIFF_reader *reader1;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		width = reader1->get_preview_width(exception);
		MPT_EXCEPTION_TEST 0;

	}
	return width;
}

unsigned int get_PreviewLength(void *reader)
{
	unsigned int length = 0;
	TIFF_reader *reader1;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();

		length = reader1->get_preview_length(exception);
		MPT_EXCEPTION_TEST 0;

	}
	return length;
}

unsigned int get_PreviewData(void *reader, unsigned int *data, size_t count)
{
	unsigned int state = 100;
	TIFF_reader *reader1;
	MPT_Exception *exception;
	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();

		state = reader1->getPreview_int_RGB(exception, data, count) ;
		MPT_EXCEPTION_TEST 0;


	}

	return state;
}



unsigned int get_RawWidth(void *reader)
{
	unsigned int width = 0;
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();

		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST 0;
		width = raw_p->get_Width(exception);
		MPT_EXCEPTION_TEST 0;

	}
	return width;

}

unsigned int get_RawLength(void *reader)
{
	unsigned int length = 0;
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;


	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST 0;
		length = raw_p->get_Length(exception);
		MPT_EXCEPTION_TEST 0;


	}
	return length;

}

unsigned int get_RawData(void *reader, unsigned int *data, size_t count)
{
	unsigned int result = 0;
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST 0;
		raw_p->updata_RGB(exception, data, count);
		MPT_EXCEPTION_TEST 0;


	}
	return result;

}

unsigned int get_ZoomWidth(void *reader)
{
	unsigned int width = 0;
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST 0;
		width = raw_p->get_ZoomWidth(exception);
		MPT_EXCEPTION_TEST 0;
	}
	return width;
}

unsigned int get_ZoomLength(void *reader)
{
	unsigned int length = 0;
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST 0;
		length = raw_p->get_ZoomLength(exception);
		MPT_EXCEPTION_TEST 0;
	}
	return length;
}
/*
unsigned int get_ZoomData(void *reader, unsigned int *data, size_t count)
{
	unsigned int result = 0;

	return result;
}
*/

int set_Zoom(void *reader, float zoom, int size[2])
{
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST MPT_ERROR;

		raw_p->set_zoom(exception, zoom, (unsigned int*)size);
		MPT_EXCEPTION_TEST MPT_ERROR;
	}else{
		return MPT_ERROR;
	}

	return MPT_SUCCESS;

}

void get_zoom_image(	void *reader,
						unsigned int *data_out,
						unsigned int linestride,
						unsigned int width,
						unsigned int length,
						unsigned int out_x0,
						unsigned int out_y0,
						unsigned int in_x0,
						unsigned int in_y0)
{
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST;
		raw_p->get_zoom_image(	exception,
								data_out,
								linestride,
								width,
								length,
								out_x0,
								out_y0,
								in_x0,
								in_y0);
	}
}

/*
int main(int argc, char* argv[])
{
	unsigned int a, b, c;
	a = 0xFFFFFFFF;
	b = 3;
	c = a * b;

	if(argc > 0){
		TIFF_reader *reader = NULL;
		string *path;
		try{
			path = new string(argv[1]);
			reader = new TIFF_reader(path);


			reader->~TIFF_reader();
		}catch(MPT_Exception *exception){
			cout<< exception->toString() <<endl;
			if(reader != NULL){
				reader->~TIFF_reader();
				path->~string();
			}
		}catch(bad_alloc& ex){
			cout<< ex.what() <<endl;
		}catch(ifstream::failure& ex){
			cout<< ex.what() <<endl;
		}
	}
	return 0;
}

*/

int get_zoom_image_02(	void *reader,
							unsigned short *data_out,
							unsigned int linestride,
							unsigned int width,
							unsigned int length,
							unsigned int out_x0,
							unsigned int out_y0,
							unsigned int in_x0,
							unsigned int in_y0)
{
	TIFF_reader *reader1;
	Raw_Processor *raw_p = NULL;
	MPT_Exception *exception;

	if(reader != NULL){
		reader1 = (TIFF_reader*)reader;
		exception = reader1->get_exception();


		raw_p = reader1->get_Raw_Processor(exception);
		MPT_EXCEPTION_TEST MPT_ERROR;
		raw_p->get_zoom_image(	exception,
								data_out,
								linestride,
								width,
								length,
								out_x0,
								out_y0,
								in_x0,
								in_y0);
	}
	return MPT_SUCCESS;
}
