/***************************************************
 * Defines and Globals for Bud Workshop file-suite *
 ***************************************************/

//Title bar texts

	 char szCtrlWindowTxt[]      = "Bud Workshop -- BWtiny1 2018" ;
     char szCtrlWindowTit[180]   = "" ;
	 char szBudWindowTxt1[]      = "Bud Image:  " ;
	 char szBudWindowTit1[180]   = "" ;
	 char szScanWindowTxt[]      = "Line Scan" ;
	 char szScanWindowTit[180]   = "" ;
	 char szResWindowTxt[]       = "Results:  " ;
	 char szResWindowTit[180]    = "" ;
	 char szCollWindowTxt[]      = "On Archived Data" ;
	 char szCollWindowTit[180]   = "" ;
	 char szArchWindowTxt[]      = "Archived" ;
	 char szArchWindowTit[180]   = "" ;
	 char szAgrWindowTxt[]       = "Aggregate Graph" ;
	 char szAgrWindowTit[180]    = "" ;


#define gHiPo  0   // drag-box indices.  Not in use yet.
#define gLoPo  1
#define gLtPC  2
#define gRtPC  3
#define gTAxis 4
	 
#define btn_up   0
#define btn_down 1
#define reset    0
#define set      1
#define PROJ     0
#define REGR     1

#define REMEAS      0
#define RECALC      1
#define REVERTPOLE  2

#define THMAX 10

//"Recognised file type" IDs
//~~~~~~~~~~~~~~~~~~~~~~~~~~
#define BMtyp 0 // Bitmap (.BMP) 
#define BGtyp 1 // Graham's bud format
#define BLtyp 2 // Lawrence's bud format
#define JPtyp 3 // JPEG (.JPG) file conforming to JFIF
#define OTtyp 4 // Other Type

#define ACTUALINSETS     6 //indices of insets
#define  MeasureWindow   0 // Measure Window
#define  ResultsWindow   1 // Results Window
#define   Scan1Window    2 //    Scan Window
#define   Scan2Window    3 //    Scan Window

#define COLOUR 0
#define   GRAY 1
#define SIGNIF 2

#define  FIRST 0
#define  FINAL 1
#define    ALL 2

#define MAXVIEWRATE 2000 			// Viewer: Animation rate control
   UINT     ViewRate ;	// Viewer: Animation rate control
 double  FieldRadius =  10.0 ;	// Viewer: Field radius in a.u.

    int  ImgSig =  0 ;
	int OnePass =  0 ;
 UINT32    xpch = 31 ;
 UINT32    ypch = 31 ;

int       ShowLambdaScale = 0 ;
int AlignmentScaleShowing ;
int    DegreeScaleShowing ;
int      DaysScaleShowing ;

int     do_geoc_dirs = 1 ;
int   do_helioc_dirs = 0 ;
int do_meanbody_dirs = 0 ;

int			  banner = 0 ;

HCURSOR hcurSave ; // Hourglass Cursor Macros
#define StartWait() hcurSave = SetCursor(LoadCursor(NULL,IDC_WAIT))
#define EndWait() SetCursor(hcurSave) ;

//LPBUDID        Bud_ID ;
//LPBUDID     Search_ID ;
//LPBUDSEARCH BudSearch ;

// Client Rectangles

RECT rcDesk ;  // of the Desktop

RECT rcb ;     // Bud Window client rectangle
RECT rcctrl ;  // Control Window client rectangle
RECT rcscan ;  // Scan-graph Window client rectangle
RECT rcrslt ;  // Results Window client rectangle
RECT rcflag ;  // Banner Window client rectangle
RECT rcimg ;   // Image rectangle in bud window (a sort of sub-client!)
RECT rcresimg ;// Image rectangle in result window
RECT rcoll ;   // Collation Window client rectangle
RECT rcacq ;   // TWAIN Image-acquisition Window client rectangle
RECT rcgraf;   // General Graphing Window client rectangle.
RECT rcagr ;   // Agraphing	Window client rectangle.
RECT rclay ;   // Layout Window for print preview
RECT rctune ;  // Client of Selectivity Curve window
RECT rcview ;  // Client of Viewer window
RECT rcrast ;  // Client of Perigeal Alignment Raster window
RECT rclist ;  // Client of Listings window

RECT rcTpole ; // Grab rectangle for hi pole
RECT rcBpole ; // Grab rectangle for lo pole
RECT rcTPico ; // Rectangle for hi pole grab handle icon
RECT rcBPico ; // Rectangle for lo pole	grab handle icon

RECT rcwipe ;	  // text area at bottom of measure window
RECT rctxtwipe ;  // text area at top of print preview window.

RECT rctxt ;
RECT rcscl ;    // Rectangle containing scan window vertical scales
RECT rcinDIB ;  // Rectangle representing the inset in the DDB
RECT rcSearch ; // Rectangle containing the region for best-pole search

RECT rcmidpt[MAXLEVELS] ; // rectangles containing level midpoints in results image 

// Bitmap Controls
// ~~~~~~~~~~~~~~~
// Rectangles

RECT rccol ;   // Rectangle containing measure-level selection-buttons
RECT rccolrt ; // Rectangle to right of buttons and left of image
RECT rcscb ;   // Rectangle containing scan window buttons

RECT rclv[MAXLEVELS] ;	   // rectangles for level-choice buttons
RECT * aRC[4] ;            // an array of 4 pointers to arrays of pointers to RECT

RECT rcNull[MAXLEVELS] ;   // rectangles containing Null Level data points
 int LevelPicked = 0 ;     // Null data point under cursor
 int MouseInLevBtn = 0 ;   // Level selection button currently under the cursor

// handles


HACCEL hAccel, hAccel2, hAccel2a, hAccel4, hAccel5 ;
HBRUSH hOvBrush ;          //    tip background
HBRUSH hbkBrush ;          // standard window background

// icons

HICON  icMean, icPhase,   icPhShift, icDate,  icDate2 ;
HICON   icDipRaw,   icDipMean ;		// raw  untuned
HICON  ictDipRaw,  ictDipMean ;		// mean untuned
HICON  icPeakRaw,  icPeakMean ;
HICON ictPeakRaw, ictPeakMean ;
HICON   icSun,   icMer,    icVen,    icMar, icJup, icSat, icUra, icNep ;
HICON icAries, icStart,    icEnd ;

// control bitmap handles

HBITMAP bmOPF, bmOPFO ;
HBITMAP bmCOL, bmCOLO ;
HBITMAP bmVOC, bmVOCO ;
HBITMAP bmCMT, bmCMTO ;
HBITMAP bmXIT, bmXITO ;
HBITMAP bmLSC, bmLSCO, bmLSG ;
HBITMAP bmGRA, bmGRAO ;
HBITMAP bmGRY, bmCLR ;
HBITMAP bmSGF ;
HBITMAP bmHIP, bmHIPO ;
HBITMAP bmLOP, bmLOPO ;
HBITMAP bmLEV, bmLEVO ;
HBITMAP bmBIP, bmBOP ;
HBITMAP bmHID, bmLOD ;

HBITMAP bmAUT, bmAUTO, bmAUTD ;	// autotrack
HBITMAP bmALF, bmALFO, bmALFD ;	// pick start on  left side
HBITMAP bmART, bmARTO, bmARTD ;	// pick start on right side
HBITMAP bmACP, bmACPO ;		  ; // accept current track
HBITMAP bmPRM, bmPRMO ;		  ; // tracking parameters dialog


HBITMAP bmOEX, bmOEXO ; // open existing
HBITMAP bmGRB, bmGRBO ; // grab
HBITMAP bmSAC, bmSACO ; // save current image
HBITMAP bmUSE, bmUSEO ; // use in workshop
HBITMAP bmPRN, bmPRNO ; // print now button
HBITMAP bmLAY, bmLAYO ; // print preview button
HBITMAP bmROT, bmROTO ;	// rotate
HBITMAP bmSLR, bmSLRO ;	// select rect
HBITMAP bmCRP, bmCRPO ;	// crop
HBITMAP bmRSZ, bmRSZO ; // resize
HBITMAP bmBRC, bmBRCO ; // brightness/contrast
HBITMAP bmTST, bmTSTO ; // make test bud image
HBITMAP bmPMX, bmPMXO ; // rename and move

HBITMAP bmACQ, bmACQO, bmACQD ;         // work with images
HBITMAP bmREZ, bmRZG,  bmREZO, bmREZD, bmREZH ; // re-dimension
HBITMAP bmLUM, bmLUMO, bmHUE,  bmHUEO ; // luminance, hue
HBITMAP bmSMU, bmSMUO, bmSMD,  bmSMDO ; // smoothing (not used)

HBITMAP bmCAL1,  bmCAL1B, bmCAL1O ;	   // calibrate 1
HBITMAP bmCAL2,  bmCAL2B, bmCAL2O ;	   // calibrate 2
HBITMAP bmCAL1D, bmCAL2D, bmHIT ;
HBITMAP bmCWH,   bmCWHO ;			   // colour wheel

HBITMAP bmHYTN, bmHYTNO, bmHYTL, bmHYTLO ;
HBITMAP bmHYTR, bmHYTRO, bmHYTB, bmHYTBO ;

HBITMAP bmEXCLU ;  // Bud Height excluded from calculations
HBITMAP bmEXCLO ;  // Bud Height excluded from calculations but selected for scan
HBITMAP LftArrow ;

HBITMAP hPatch  = (HBITMAP)NULL ;
HBITMAP hPatch1 = (HBITMAP)NULL ;
HBITMAP hPatch2 = (HBITMAP)NULL ;
HBITMAP hPatch3 = (HBITMAP)NULL ;


HBITMAP bmPRJ, bmPRJO, bmIMG, bmIMGO ;
HBITMAP bmDIA, bmDIAO, bmLEF, bmLEFO ;
HBITMAP bmRYT, bmRYTO, bmDTL, bmDTLO ;
HBITMAP bmNUL, bmNULO, bmNULD ;
HBITMAP bmITM, bmITMO, bmLST, bmLSTO ;
HBITMAP bmAST, bmASTO, bmACV, bmACVO ;
HBITMAP bmFOU, bmFOUO, bmFOS, bmFOSO ;
HBITMAP bmNXT, bmNXTO, bmCAP, bmCAPO ;
HBITMAP bmPRV, bmPRVO, bmSLA, bmSLAO ;
HBITMAP bmLSA, bmLSR,  bmLSG, bmLSB  ;
HBITMAP bmTGR, bmTGRO, bmSVI, bmSVIO ;
HBITMAP bmGPS, bmGPSO ;

HBITMAP bmPRJa, bmPRJOa, bmIMGa, bmIMGOa ;
HBITMAP bmDIAa, bmDIAOa, bmLEFa, bmLEFOa ;
HBITMAP bmRYTa, bmRYTOa, bmDTLa, bmDTLOa ;
HBITMAP bmNULa, bmNULOa, bmNULDa ;
HBITMAP bmITMa, bmITMOa, bmLSTa, bmLSTOa ;
HBITMAP bmASTa, bmASTOa, bmACVa, bmACVOa ;
HBITMAP bmFOUa, bmFOUOa, bmFOSa, bmFOSOa ;

// Capture

HBITMAP hCapt ;
   HWND hWndCapt ;

// Memory Pool

  DWORD Availbefore, Availafter ;

// Cursor handles

HCURSOR hTarget ;      // handle of      unlocked cursor for measurement
HCURSOR hTargetF ;     // handle of full unlocked cursor for measurement
HCURSOR hTargetLock ;  // handle of        locked cursor for measurement
HCURSOR hTargetLockF ; // handle of   full locked cursor for measurement
HCURSOR hTargetW ;     // handle of unlocked wide cursor for measurement
HCURSOR hTargetLockW ; // handle of   locked wide cursor for measurement

HCURSOR hRotCur[5] ;   // handles of the rotating cursor
HCURSOR hDpeak ;
HCURSOR hTracklftCur ;
HCURSOR hTrackrytCur ;

// Icons

HICON icoTP ;
HICON icoBP ;
HICON icoPtr ;

// Some global memory handles 

	HGLOBAL      hbuf  = (HGLOBAL)NULL ;
	HGLOBAL      hRGB  = (HGLOBAL)NULL ;
	HGLOBAL		 hGRY  = (HGLOBAL)NULL ; 
	HGLOBAL      hSTA  = (HGLOBAL)NULL ; // for statistical image operations
    HGLOBAL   hResults = (HGLOBAL)NULL ; // for the Results struct. allocation
    HGLOBAL   hMeasure = (HGLOBAL)NULL ; // for measure struct allocation

	HGLOBAL       hTmp = (HGLOBAL)NULL ; // a temporary handle
    HGLOBAL      hTemp = (HGLOBAL)NULL ; // a temporary handle
	HGLOBAL     hTempo = (HGLOBAL)NULL ; // a temporary handle
    HGLOBAL     newbuf = (HGLOBAL)NULL ; // all for use in Imagery

/***
    HGLOBAL      hsto  = (HGLOBAL)NULL ;
    HGLOBAL      hsto1 = (HGLOBAL)NULL ;
	HGLOBAL      hsto2 = (HGLOBAL)NULL ;
	HGLOBAL      hsto3 = (HGLOBAL)NULL ;
***/
	HGLOBAL       hpic = (HGLOBAL)NULL ;

	HGLOBAL   hJPGText = NULL ;          // for text fields in JPEGs
    
// For bud RGB-style Buffers

   BYTE * pRGB ;   // colour
   BYTE * pGRY ;   // grayscale
   BYTE * pSTA ;   // statistical

// Globals

UINT Tim, Blink, Blink1, RotCurTim, CurTimer, LevTim ;
UINT blinkhi, blinklo, blinkbutton ; // associations with timed events
UINT blinkhi1, blinklo1 ;
 int Interval  = 2000 ;		        // for sign on banner, 4 seconds
 int   tBlink  =  250 ;		        // blink rate is about 4 per second
 int   tBlink1 =  250 ;				// second blink rate
 int  tRotCur  =   50 ;				// cursor rotate 20 steps per second
 int currotate =    0 ;
 int    which  = COLOUR ;			// bud bitmap display first set for colour

// data collation

UINT getpL     = 1 ;
UINT getrL     = 0 ;
UINT getLdiam  = 1 ;
UINT getLright = 0 ;
UINT getLleft  = 0 ;
UINT getDdiam  = 0 ;
UINT getDright = 0 ;
UINT getDleft  = 0 ;
UINT getMRDs   = 0 ;
UINT getIPDs   = 0 ;

// earliest and latest instance-epoch limits

double first_bud_ep = 0.0 ;
double final_bud_ep = 1.0e20 ;

// flags etc

int    graphing = 0 ;
int     Collate = 0 ;
int     setpole = 0 ;
int   setlevels = 0 ;
int AskIfToSave = 0 ;

int   shownullgraph = 0 ;
int ShowDragHandles = 0 ;	// Whether to reveal the pole drag-handles (don't, for now)

int      autoRes = 0 ;		// Turn off lock and scan when meas. done
int      autoMax = 0 ;		// Go full screen on completion of measurement
int NoHorizontal = 0 ;		// No plot of Angles or Lambda graticule

int       SETTOP = 1 ;		// Default is top pole to set
int       TOPSET = 0 ;		// and no pole set
int       BOTSET = 0 ;

int PoleSettings = 0 ;		// number of pole-settings this session

int         xtop = 0 ;
int         ytop = 0 ;
int         xbot = 0 ;
int         ybot = 0 ;
int            wcli ;
int            hcli ;
int        xacli = 0 ;
int        yacli = 0 ;
int        xbcli = 0 ;
int        ybcli = 0 ;

int     resizing = 0 ;
int      resized = 0 ;
int     noselect = 1 ;  // disable select button by default. re-enable in Prefs.

int    setsearch = 0 ;
int    searchset = 0 ;

int   ignoreinst = 0 ; // ignore changed image, leave archive as is.

int       Xnudge = 6 ;  // default cursor stepping
int       Ynudge = 6 ;

int      GotData = 0 ;

int DimensionItems ;
int    ResultItems ;

int    levels_exist ;	// whether or not per-instance measurement levels found on record
int      Dfault		;
int      dfault = 7 ;	// Fallback Default number of measure heights.
int      Locked = 0 ;	// Whether a level is selected
int  JustLocked = 0 ;	// Whether a level has just been selected
int        wide = 0 ;	// Use wide cursor style
int LevelSelect = 0 ;	// Current measure height selection
int      invert = 0 ;	// Invert Drawing colour

//int  HaveOvly[NUMCONTROLS] ;	Now in Bud32.h

int SavefromImagery = 0 ;
int   OpeninImagery = 0 ;
int       HaveImage = 0 ;
int      timed_grab = 0 ;
int       grabbeeps = 0 ;
int        grabbusy = 0 ;
int        grabbing = 0 ;
int       ShowGrabs = 0 ;
int       SaveGrabs = 0 ;
int      grabwindow = 0 ;
int	   UseLocalTime = 0 ;
int		   RotateCW = 0 ;
int		  RotateACW = 0 ;
int          saving = 0 ;
UINT         GrabmS = 0 ;
UINT	   GrabMins = 0 ;
UINT       GrabSecs = 5 ;// 5 second default
int         grabwid = 0 ;
int         grabhyt = 0 ;
int           grabx = 0 ;
int           graby = 0 ;
int        pickgrab = 0 ;
int		showJPGtext = 0 ;

char    jpgtxt[256] = {'\0'} ;

char   GrabDir[256] = "C:\\Grabbed\\" ;
char GrabPrefix[80] = "Grab_" ;
HWND       hWndGrab = (HWND)NULL ;


int      stored = 0 ;	  // whether storage allocated for scans

int    scanning = 0 ;	  // initially no scan
int  scansmooth = 2 ;	  // iteration level of scan-graph smoothing
int      huesat = 0 ;	  // initially lum. and diff.lum graphs
int       colwh = 0 ;     // initially no colour wheel

int     AllDone = 0 ;	  // is set when all levels measured
int    SomeDone = 0 ;	  // is set if at least one measurement is made



// often used constants: calculate once

double pi ;
double r2d ;			     // radians to degrees multiplier
double onethird ;

double SiYR ;				 // seconds in one julian year
double SiMO ;				 // seconds in one month of 31 days
double SiWK ;				 // seconds in one week 
double SiDY ;				 // seconds in one day
double SiHR ;				 // seconds in one hour
double SiMN ;				 // seconds in one minute
double SiSC ;				 // seconds in one second (-:

double tval[10] ;

double t0, tr, tg, tb, tc ;	 // angles of chromatogram sectors

double pmag = 1.0 ;          // pixel magnification

// Brightness/Contrast

double    fBright = 0.0 ;
double  fContrast = 0.0 ;
   int    nBright = 500 ;
   int  nContrast = 500 ;

// Default Resolution of Alignment search (days)

double AlignResolution = 0.04 ;

// Calibration

int       SETCAL1 = 1 ;	  // Defaults are to calibrate to mark 1
int       CAL1SET = 0 ;	  // and no calibrations done
int       CAL2SET = 0 ;
int   calibrating = 0 ;	  // calibration in progress
int         Calib ;	      // calibration dialogue box return value
int      xMk, yMk ;       // intermediary values
double xMark1, yMark1 ;	  // position of first  calibration mark in DIB
double xMark2, yMark2 ;	  // position of second calibration mark in DIB
double DIBitsPerMm	;	  //   metric conversion factor relative to DIB 
double DIBitsPerIn	;     // imperial conversion factor ditto
double DIBitsPerIm ;   // image-units conversion factor ditto
int    DIBitsPerPx ;   // pixels to DIB bits, calculated in AdjustDisplays.

// Line scans

int * lum ;              // line-scan luminosity and differential...
int * dlum ;			 // ...luminosity data arrays
int threshold =   4 ; 	 // differential luminance min-detect threshold
int  diffgain =  32 ;	 // sets amplitude of diff. lum. graph

double * hue  ;	         // line-scan hue and saturation data arrays
double * dhue ;
double * sat  ;

double xm, ym ;          // mouse position in bitmap

typedef struct tagINSET	 // data for insets in windows ("sub windows")
           {
        int xo ; // x offset
        int yo ; // y offset
        int w ;	 // width
        int h ;  // height
           } INSET, * LPINSET ;

INSET Inset[ACTUALINSETS] ;     // array of inset structs


