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

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


void *create_Reader_01(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 *create_Reader_02(const char *file, int num_threads)
{

	TIFF_reader *reader = NULL;
	MPT_Exception *exception;

	set_USETHREADS(num_threads);
	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_01(void *reader, unsigned int *data, size_t count)
{
	unsigned int state = MPT_NOSUCCESS;
	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 MPT_NOSUCCESS;
		state = MPT_SUCCESS;

	}

	return state;
}

unsigned int get_PreviewData_02(void *reader, unsigned short *data, size_t count)
{
	unsigned int state = MPT_NOSUCCESS;
	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 MPT_NOSUCCESS;
		state = MPT_SUCCESS;

	}

	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;
}


unsigned int get_Exif_ExposureTime(void *reader, unsigned int return_value[2])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_ExposureTime(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_FNumber(void *reader, unsigned int return_value[2])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_FNumber(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}


unsigned int get_Exif_ExposureProgram(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_ExposureProgram(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_ISOSpeedRatings(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_ISOSpeedRatings(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}


unsigned int get_Exif_DateTimeOriginal(void *reader, char return_value[20])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_DateTimeOriginal(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_SubsecTimeOriginal(void *reader, char return_value[20])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_SubsecTimeOriginal(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}


unsigned int get_Exif_DateTimeDigitized(void *reader, char return_value[20])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_DateTimeDigitized(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}


unsigned int get_Exif_SubsecTimeDigitized(void *reader, char return_value[20])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_SubsecTimeDigitized(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}


unsigned int get_Exif_ExposureBiasValue(void *reader, int return_value[2])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_ExposureBiasValue(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_MeteringMode(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_MeteringMode(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_Flash(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_Flash(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_FlashEnergy(void *reader, unsigned int return_value[2])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_FlashEnergy(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_FocalLength(void *reader, unsigned int return_value[2])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_FocalLength(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_FocalLengthIn35mmFilm(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_FocalLengthIn35mmFilm(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_SensingMethod(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_SensingMethod(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_CustomRendered(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_CustomRendered(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_ExposureMode(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_ExposureMode(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_WhiteBalance(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_WhiteBalance(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_SceneCaptureType(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_SceneCaptureType(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_Contrast(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_Contrast(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_Saturation(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_Saturation(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_Sharpness(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_Sharpness(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_BaselineSharpness(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_BaselineSharpness(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}

unsigned int get_Exif_SubjectDistanceRange(void *reader, unsigned int return_value[1])
{
	unsigned int back = MPT_NOSUCCESS;
	TIFF_reader *reader1;
	MPT_Exception *exception;


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

		if(reader1->get_Exif_SubjectDistanceRange(exception, return_value) != MPT_SUCCESS){
			return back;
		}else{
			MPT_EXCEPTION_TEST back;
			back = MPT_SUCCESS;
		}
	}

	return back;
}


