/*
 * gc_geometry.cpp
 *
 */


#include "gc_geometry_smart.h"

namespace gc_cfg
{

//typedef int pixel_num_t;

pixel_num_t XY_LOGICAL_DISPLAY_DIMENSION  = gc_cfg_k::K_XY_DEFAULT_DISPLAY_SIZE;
pixel_num_t X_PHYSICAL_DISPLAY_DIMENSION = gc_cfg_k::K_X_PHYSICAL_DISPLAY_DIMENSION; //XY_DEFAULT_DISPLAY_SIZE+gc_cfg_k::K_X_EXTRA_DIM; //640;
pixel_num_t Y_PHYSICAL_DISPLAY_DIMENSION = gc_cfg_k::K_Y_PHYSICAL_DISPLAY_DIMENSION; //XY_DEFAULT_DISPLAY_SIZE+gc_cfg_k::K_Y_EXTRA_DIM; //512; //real dimension is 484, extend for some debug info to be displayed only at higher resolutions GC_DISPLAY_ASPECT_RATIO(X_PHYSICAL_DISPLAY_DIMENSION),

pixel_num_t X_PHYSICAL_DISPLAY_STRIDE = gc_cfg_k::K_X_PHYSICAL_DISPLAY_DIMENSION; //real dimension is 484, extend for some debug info to be displayed only at higher resolutions GC_DISPLAY_ASPECT_RATIO(X_PHYSICAL_DISPLAY_DIMENSION),

pixel_num_t Y_PHYSICAL_DISPLAY_OUTPUT = gc_cfg_k::K_XY_DEFAULT_DISPLAY_SIZE; //real dimension is 484, extend for some debug info to be displayed only at higher resolutions GC_DISPLAY_ASPECT_RATIO(X_PHYSICAL_DISPLAY_DIMENSION),

pixel_num_t X_LOGICAL_DISPLAY_OFFSET=gc_cfg_k::K_X_LOGICAL_DISPLAY_OFFSET; //(gc_cfg_k::K_X_PHYSICAL_DISPLAY_DIMENSION-gc_cfg_k::K_XY_LOGICAL_DISPLAY_DIMENSION)/2;
pixel_num_t Y_LOGICAL_DISPLAY_OFFSET=0; //(Y_PHYSICAL_DISPLAY_DIMENSION-XY_LOGICAL_DISPLAY_DIMENSION)/2;

pixel_num_t X_ABS_DIMENSION = gc_cfg_k::K_X_ABS_DIMENSION;
pixel_num_t Y_ABS_DIMENSION = gc_cfg_k::K_Y_ABS_DIMENSION;;

pixel_num_t X_ABS_CENTRE_DISPL=gc_cfg_k::K_X_ABS_CENTRE_DISPL;
pixel_num_t Y_ABS_CENTRE_DISPL=gc_cfg_k::K_Y_ABS_CENTRE_DISPL;

pixel_num_t X_MOVWIN_OFFSET=gc_cfg_k::K_X_MOVWIN_OFFSET;
pixel_num_t Y_MOVWIN_OFFSET=gc_cfg_k::K_Y_MOVWIN_OFFSET;

pixel_num_t X_SCALE_OFFSET_TOP=1;
pixel_num_t X_SCALE_OFFSET_BOTTOM=2;
pixel_num_t Y_SCALE_OFFSET_TOP=1;
pixel_num_t Y_SCALE_OFFSET_BOTTOM=2;

pixel_num_t X_DIMENSION = gc_cfg_k::K_X_DIMENSION; //(X_ABS_DIMENSION-X_MOVWIN_OFFSET*2);
pixel_num_t Y_DIMENSION = gc_cfg_k::K_Y_DIMENSION; //(Y_ABS_DIMENSION-Y_MOVWIN_OFFSET*2);

pixel_num_t X_CENTRE_DISPL=gc_cfg_k::K_X_CENTRE_DISPL; //(X_DIMENSION/2)-1;
pixel_num_t Y_CENTRE_DISPL=gc_cfg_k::K_Y_CENTRE_DISPL; //(X_DIMENSION/2)-1;

pixel_num_t Y_ZERO_RANGE = gc_cfg_k::K_Y_ZERO_RANGE; //(Y_DIMENSION-1)-Y_SCALE_OFFSET_BOTTOM;
pixel_num_t Y_FULL_RANGE = gc_cfg_k::K_Y_FULL_RANGE; //Y_SCALE_OFFSET_TOP;
pixel_num_t LEN_1D4_RNG  = gc_cfg_k::K_LEN_1D4_RNG; //((Y_ZERO_RANGE-Y_FULL_RANGE)/4);

pixel_num_t X_ZERO_RNG_PPI=gc_cfg_k::K_X_ZERO_RNG_PPI;
pixel_num_t Y_ZERO_RNG_PPI=gc_cfg_k::K_Y_ZERO_RNG_PPI;
pixel_num_t Y_FULL_RNG_PPI=gc_cfg_k::K_Y_FULL_RNG_PPI;

pixel_num_t LEN_1D4_RNG_PPI=gc_cfg_k::K_LEN_1D2_RNG_PPI; //((Y_ZERO_RNG_PPI-Y_FULL_RNG_PPI)/4);
pixel_num_t LEN_1D2_RNG_PPI=gc_cfg_k::K_LEN_1D2_RNG_PPI; //((Y_ZERO_RNG_PPI-Y_FULL_RNG_PPI)/2);
pixel_num_t LEN_3D4_RNG_PPI=gc_cfg_k::K_LEN_3D4_RNG_PPI; //(((Y_ZERO_RNG_PPI-Y_FULL_RNG_PPI)*3)/4);

pixel_num_t X_MIN_PIX_DISPL=0;
pixel_num_t X_MAX_PIX_DISPL=gc_cfg_k::K_X_MAX_PIX_DISPL; //(X_DIMENSION-1);
pixel_num_t Y_MIN_PIX_DISPL=0;
pixel_num_t Y_MAX_PIX_DISPL=gc_cfg_k::K_Y_MAX_PIX_DISPL; //(Y_DIMENSION-1);

}//namespace


using namespace gc_cfg;

//#include "bsp_interrupts.h"

extern void gc_config_geometry_resize(int pyh_dx, int phy_dy, int rdr_dxy, int x_offset, int y_offset);
extern void gc_algorithms_resize(int pyh_dx, int phy_dy, int rdr_dxy, int x_offset, int y_offset);

void gc_geometry_recalculate(int phy_dx, int phy_dy, int rdr_dxy, int x_offset, int y_offset)
{
	//unsigned int ists=bsp_interrupts_disable();
	int pdx=rdr_dxy+gc_cfg_k::K_X_EXTRA_DIM; //(gc_cfg::X_PHYSICAL_DISPLAY_DIMENSION-gc_cfg::XY_LOGICAL_DISPLAY_DIMENSION);
	int pdy=rdr_dxy+gc_cfg_k::K_Y_EXTRA_DIM; //(gc_cfg::Y_PHYSICAL_DISPLAY_DIMENSION-gc_cfg::XY_LOGICAL_DISPLAY_DIMENSION);

	gc_cfg::X_LOGICAL_DISPLAY_OFFSET=0;
	gc_cfg::Y_LOGICAL_DISPLAY_OFFSET=0;

	if (pdx<800)
		pdx=800;
	if (pdy<600)
		pdy=600;
#if 0
	if (rdr_dxy==484)
	{
		pdx=800;
		pdy=600;
	}
#endif

	if (pdx>gc_cfg::X_MAX_PHYSICAL_DISPLAY_DIMENSION)
	{
		pdx=gc_cfg::X_MAX_PHYSICAL_DISPLAY_DIMENSION;
	}
	if (pdy>gc_cfg::Y_MAX_PHYSICAL_DISPLAY_DIMENSION)
	{
		pdy=gc_cfg::Y_MAX_PHYSICAL_DISPLAY_DIMENSION;
	}

	gc_cfg::X_PHYSICAL_DISPLAY_DIMENSION=pdx;
	gc_cfg::Y_PHYSICAL_DISPLAY_DIMENSION=pdy;
	gc_cfg::XY_LOGICAL_DISPLAY_DIMENSION=rdr_dxy;

	int round_stride=(pdx+0x7) & (~7);

	if (round_stride>gc_cfg::X_MAX_PHYSICAL_DISPLAY_DIMENSION)

		round_stride=gc_cfg::X_MAX_PHYSICAL_DISPLAY_DIMENSION;

	gc_cfg::X_PHYSICAL_DISPLAY_STRIDE=round_stride;

	Y_PHYSICAL_DISPLAY_OUTPUT = gc_cfg::XY_LOGICAL_DISPLAY_DIMENSION;

	X_ABS_DIMENSION = XY_LOGICAL_DISPLAY_DIMENSION;
	Y_ABS_DIMENSION = XY_LOGICAL_DISPLAY_DIMENSION;

	X_ABS_CENTRE_DISPL=X_ABS_DIMENSION/2;
	Y_ABS_CENTRE_DISPL=Y_ABS_DIMENSION/2;

	X_MOVWIN_OFFSET=0;
	Y_MOVWIN_OFFSET=0;

	X_SCALE_OFFSET_TOP=1;
	X_SCALE_OFFSET_BOTTOM=2;
	Y_SCALE_OFFSET_TOP=1;
	Y_SCALE_OFFSET_BOTTOM=2;

	X_DIMENSION = (X_ABS_DIMENSION-X_MOVWIN_OFFSET*2);
	Y_DIMENSION = (Y_ABS_DIMENSION-Y_MOVWIN_OFFSET*2);

	X_CENTRE_DISPL=(X_DIMENSION/2)-1;
	Y_CENTRE_DISPL=(X_DIMENSION/2)-1;

	Y_ZERO_RANGE = (Y_DIMENSION-1)-Y_SCALE_OFFSET_BOTTOM;
	Y_FULL_RANGE = Y_SCALE_OFFSET_TOP;
	LEN_1D4_RNG  = ((Y_ZERO_RANGE-Y_FULL_RANGE)/4);

	X_ZERO_RNG_PPI=X_CENTRE_DISPL;
	Y_ZERO_RNG_PPI=Y_ZERO_RANGE;
	Y_FULL_RNG_PPI=Y_FULL_RANGE;

	LEN_1D4_RNG_PPI=((Y_ZERO_RNG_PPI-Y_FULL_RNG_PPI)/4);
	LEN_1D2_RNG_PPI=((Y_ZERO_RNG_PPI-Y_FULL_RNG_PPI)/2);
	LEN_3D4_RNG_PPI=(((Y_ZERO_RNG_PPI-Y_FULL_RNG_PPI)*3)/4);

	X_MIN_PIX_DISPL=0;
	X_MAX_PIX_DISPL=(X_DIMENSION-1);
	Y_MIN_PIX_DISPL=0;
	Y_MAX_PIX_DISPL=(Y_DIMENSION-1);

	gc_config_geometry_resize(phy_dx, phy_dy, rdr_dxy, x_offset, y_offset);
	gc_algorithms_resize(phy_dx, phy_dy, rdr_dxy, x_offset, y_offset);
	//bsp_interrupts_restore(ists);
}

