#pragma once

#include <stdio.h>
#include <math.h>

namespace MobilePhotoTool{

typedef unsigned short 	pixeltype;
#define	 pixeltypeMAX		65535
#define pixeltypeMIN		0


	typedef unsigned int streamoff;


	class Mathe
	{
	public:
		Mathe(void);
		~Mathe(void);

		static float det_matrix_2x2(const float matrix[4]);

		static float det_matrix_3x3(const float matrix[9]);

		static void invers_matrix3x3(const float in_matrix[9], float out_matrix[9]);

		static void mul_matrix3x3(const float in1_matrix[9], const float in2_matrix[9], float out_matrix[9]);

		static void mul_matrix3x3_vector(const float in1_matrix[9], const float in2_vector[3], float out_vector[3]);

		static void normal_matrix3x3(float in_out_matrix[9], float value, float out_vector[3]); 

		static void min_max_vector3(float vector[3], float *min, float *max);

		static void scalar_vector3(float vector[3], float scalar);

		static float gamma(float in);

		static pixeltype crop(float in);

		static unsigned int round_up(float in);

		static unsigned int round_down(float in);

		static unsigned int round(float in);
	};

	inline unsigned int Mathe::round(float in){
		return (unsigned int) (in + 0.5f);
	}

	inline 	pixeltype Mathe::crop(float in){
		unsigned int v = (unsigned int)in;
		if(v <= 0xFFFF){
			return (pixeltype) in;
		}else{
			if((v & 0x80000000)){
				return 0;
			}else{
				return 0xFFFF;
			}
		}
	}

	inline unsigned int Mathe::round_down(float in){
		unsigned int back = 0;
		if(in > 0){
			back = (unsigned int)in;
		}
		return back;
	}

	inline unsigned int Mathe::round_up(float in){
		unsigned int back = 0;
		if(in > 0){
			back = (unsigned int)in;

			if((in - (float)back) > 0.000001f){
				back += 1;
			}
		}
		return back;
	}
}
