
#ifndef __UIIRIDAS_H
#define __UIIRIDAS_H

#include "uibutton.h"
#include "../../../3rdParty/Iridas/Redist/irlooklibrary.h"
#include "../../../3rdParty/Iridas/Redist/irtoken.h"
#include "uilookfile.h"
#include "irihistogram.h"
#include "vscope.h"


#define IRI_BUTTON_X 11
#define IRI_BUTTON_Y 10
#define IRI_BUTTON_W 206
#define IRI_BUTTON_H 54
#define IRI_BUTTON_DY 58
#define IRI_RBUTTON_X 1064

#define IRI_VIDEO_X 233
#define IRI_VIDEO_Y 13
#define IRI_VIDEO_W 816
#define IRI_VIDEO_H 459

#define IRI_ILOGO_X 18
#define IRI_ILOGO_Y 644
#define IRI_ILOGO_W 37
#define IRI_ILOGO_H 59

#define IRI_INC_X1 309.0f
#define IRI_INC_X2 729.0f
#define IRI_INC_X3 1184.0f
#define IRI_INC_Y 506.0f
#define IRI_INC_W 68.0f
#define IRI_INC_H 47.0f
#define IRI_INC_DY 145.0f

#define IRI_RAMP_X1 (IRI_INC_X1+20)
#define IRI_RAMP_X2 (IRI_INC_X2+20)
#define IRI_RAMP_X3 (IRI_INC_X3+20)
#define IRI_RAMP_Y (IRI_INC_Y+51)
#define IRI_RAMP_W 28
#define IRI_RAMP_H 90

#define IRI_HUERANGE_MIN 0.0036
#define IRI_HUERANGE_MAX 180
#define IRI_HUERANGE_DEFAULT 3.0
#define IRI_HUERANGE_INC 1

#define IRI_LUMARANGE_MIN 0.00001
#define IRI_LUMARANGE_MAX 0.5
#define IRI_LUMARANGE_DEFAULT 0.00001
#define IRI_LUMARANGE_INC 0.005

#define IRI_SATRANGE_MIN 0.00001
#define IRI_SATRANGE_MAX 0.5
#define IRI_SATRANGE_DEFAULT 0.00001
#define IRI_SATRANGE_INC 0.005

#define IRI_HUEFEATHER_MIN 0
#define IRI_HUEFEATHER_MAX 360
#define IRI_HUEFEATHER_DEFAULT 15

#define IRI_THRESHOLD_MIN 0.0005
#define IRI_THRESHOLD_MAX 1.0
#define IRI_THRESHOLD_DEFAULT 1.0

#define IRI_LOG_INC 1.114268

#define IRI_DEFAULT_SAT_FEATHER 1.0
#define IRI_DEFAULT_LUM_FEATHER 1.0


#define IRI_HIST_X IRI_BUTTON_X
#define IRI_HIST_Y 272
#define IRI_HIST_W IRI_BUTTON_W
#define IRI_HIST_H 188

#define IRI_VSCOPE_X 10
#define IRI_VSCOPE_Y 264
#define IRI_VSCOPE_W 206 
#define IRI_VSCOPE_H 206

#define IRI_SAT_X 274
#define IRI_SAT_Y 500
#define IRI_SAT_W IRI_INC_W
#define IRI_SAT_H IRI_INC_H
#define IRI_SAT_DX 212
#define IRI_SAT_DY 74
#define IRI_SAT_DX2 454

#define IRI_SAT_RAMP_X (IRI_SAT_X+74)
#define IRI_SAT_RAMP_Y (IRI_SAT_Y+8)
#define IRI_SAT_RAMP_W 131
#define IRI_SAT_RAMP_H 28

#define IRI_HUERANGE_X 231
#define IRI_HUERANGE_Y 504
#define IRI_HUERANGE_RAMP_X (IRI_HUERANGE_X+74)
#define IRI_HUERANGE_RAMP_Y (IRI_HUERANGE_Y+8)
#define IRI_HUEFEATHER_X 231
#define IRI_HUEFEATHER_RAMP_X (IRI_HUEFEATHER_X+74)
#define IRI_HUEFEATHER_RAMP_Y (IRI_HUERANGE_Y+8)

#define IRI_GADGET_X1 186
#define IRI_GADGET_X2 604
#define IRI_GADGET_X3 1064
#define IRI_GADGET_Y  598
#define IRI_GADGET_W  208
#define IRI_GADGET_H  208

#define IRI_GADGET_WB_X 1030
#define IRI_GADGET_WB_Y 485

#define IRI_CWHEEL_RADIUS 77
#define IRI_CWHEEL_W 196
#define IRI_CWHEEL_H 196


#define IRI_INCREMENT 0.02f


#define IRI_BRACKET_S 14
#define IRI_BRACKET_L 42 

#define IRI_CWHEEL_BOX_W 22
#define IRI_CWHEEL_BOX_H 22
#define IRI_CWHEEL_LINE_W 2
#define CWHEEL_SCALAR 8.0f
#define TRACK_PIXELS 3000
#define CWHEEL_PIXELS 16.0f

extern SIrexILook* gIrex;


enum EUIIridas {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S_CONT_BAR,
	UIIRIDAS_MAG_BAR,
	UIIRIDAS_THRESHOLD_BAR,
};


enum EIridasMode {
	IRIDAS_MODE_OVERALL = 0,
	IRIDAS_MODE_HIGHLIGHTS,
	IRIDAS_MODE_MIDTONES,
	IRIDAS_MODE_SHADOWS,
	IRIDAS_MODE_LAST,
};

enum EKeyerMode {
	IRIDAS_KEYERMODE_FG = 0,
	IRIDAS_KEYERMODE_COMP,
	IRIDAS_KEYERMODE_A,
	IRIDAS_KEYERMODE_BG,
};

enum EIridasSlider {
	IRIDAS_SLIDER_SAT = 0,
	IRIDAS_SLIDER_PIVOT,
	IRIDAS_SLIDER_TEMP,
	IRIDAS_SLIDER_OVERALL,
	IRIDAS_SLIDER_CONTRAST,
	IRIDAS_SLIDER_MAGENTA,
	IRIDAS_SLIDER_LAST,
};

typedef struct TKeyerInfo {
	bool   mExists;
	double mColor;
	double mScale;
	char   mColorName[32];
	char   mScaleName[32];
} TKeyerInfo;



class CUIIridas {

public:

	CUIIridas( char *filename, int index );
	~CUIIridas();
	void Render( void );
	void Init( void );
	void Load( char *filename );
	void OnClick( int x, int y );
	void AddButton( CUIButton *button, int set );
	bool Inc( double *value, double max, double inc );
	bool Dec( double *value, double min, double inc );
	bool Track( double &value, double min, double max, int dx );
	void DrawBracket( float x, float y, bool isLandscape );
	void Reset( void );
	void SaveLookFileOk( void );
	void LookFileCancel( void );
	void OpenSaveLookFileDlg( void );
	void ReplacePlayerLookFile();
	void SavePlayerLookFile();
	void UpdateGraphs( unsigned char *data, int width, int height );
	void Trilin8( unsigned char r, unsigned char g, unsigned char b, float *rgb );
	void Trilin8( unsigned char &r, unsigned char &g, unsigned char &b );
	void RgbToHsl_VSCOPE( float *rgb, float &h, float &s, float &l );
	bool HandleRightClick( int x, int y );
	void UpdateTrackball( int dx, int dy );
	int GetActiveLook();
	LRESULT OnMouseWheel( UINT uKeys, short zDelta );

	int mOldVideoDisplayMode;
	EKeyerMode mKeyerMode;
	bool mGreyOn;
	bool mTrackballOn;
	CUITextButton * mGreyMatte;

private:

	void XyzToHsl( double *xyz, float &h, float &s, float &l );
	void HsToXy( float h, float s, float &x, float &y );
	void CWheelClick( EUIIridas button, int x, int y );
	void CWheelTrack( EUIIridas button, int dx, int dy );
	void DrawCWheelBox( float cx, float cy );
	void UpdateOffset( void );
	void UpdateGamma( void );
	void UpdateColorGain( void );
	void Update( void );
	void ComputeXYZ( float *pos, float luma, double *xyz );
	void UpdateHSL( void );
	void UpdateSliders( EIridasSlider slider );
	void Trilin( float r, float g, float b, float *pr, float *pg, float *pb );
	void AddKeyerLayer( void );

	CUIButton *mSet1List;
	CUIButton *mSet2List;
	CUIButton *mSet3List;
	CUITextButton *mOkButton;
	CUITextButton *mDoneButton;
	CUITextButton * mControlSet1;
	CUITextButton * mControlSet2;
	CUITextButton * mKeyer;
	CUITextButton * mOverall;
	CUITextButton * mHighlights;
	CUITextButton * mMidtones;
	CUITextButton * mShadows;
	CUITextButton * mHistogramButton;
	CUITextButton * mGreen;
	CUITextButton * mBlue;
	CUITextButton * mForeground;
	CUITextButton * mBackground;
	CUITextButton * mComposite;
	CUITextButton * mAlpha;
	CUILookFileDialog *mLookFileDialog;
	int mSet;

	EIridasMode mIridasMode;
	int   mTrackballButton;

	bool mHistogramMode;
	char lookPath[256];
	float mLut8[256];

	TKeyerInfo mKeyerInfo;

	double mSliderValues[IRIDAS_SLIDER_LAST][IRIDAS_MODE_LAST];

	double mOffsetXYZ[IRIDAS_MODE_LAST][3];
	double mGammaXYZ[IRIDAS_MODE_LAST][3];
	double mColorGainXYZ[IRIDAS_MODE_LAST][3];


	float mOffsetPos[2];
	double mOffsetLuma;

	float mGammaPos[2];
	double mGammaLuma;

	float mColorGainPos[2];
	double mColorGainLuma;

	double mThresholdPart;

	char mFilename[ 256 ];
	char mLoadLook[ 256 ];
	char mSaveLook[ 256 ];
	int mActiveLook;
	int mIndex;

	float mr, mg, mb;

	int  mTrackballX;
	int  mTrackballY;

	bool mValChanged;

	CIriHistogram mHistogram;
	CVScope       mVScope;

};

extern CUIIridas *gIridas;


#endif 