
#if !defined(SHARED_H__INCLUDED_)
#define SHARED_H__INCLUDED_

#include "windows.h"
#include "Shared.h"

const double fPAL = 4433618.75;

const double M_PI = 3.14159265358;

typedef signed int     int32_t;
typedef signed char    int8_t;
typedef signed short   int16_t;
typedef signed int     int32_t;
typedef unsigned short uint16_t;
typedef unsigned char  uint8_t;
typedef unsigned int   uint32_t;

enum
{
  _nSub = 32,

  _nSample = 59,

  _fADCMHz = 72/2,

  _ImageOfs0 = 128, // "Burst Messpunkt#2" (s. "ArrArr[]" in "main.c") bis Bildbeginn (PAL:10.5µs)

  _ImageOfs = (52*_fADCMHz-59*32)/2, // für gleichen Rand (links/rechts)

  _dphiColor_degree = 0, // der Theoriewert 0 passt gut
};

typedef struct
{
  int32_t Init;       // Init
  int32_t ImageBias;  // 0..4095; Schwarzreferenzwert in ADC-Einheiten
  int32_t ImageScale; // 0..65535; bei 4096 wird ein 12 Bit-ADC zu einem 8 Bit-Image[]-Wert skaliert
  int32_t ImageOfs;   // Bildlage in 1/4 Pixel (bei 0 : 10.5µs)
  int32_t Mode;       // 0:1888_576; ...
  int32_t dADC2;      // 0: nur ADC1 mit 8 Bit in Image[]; sonst: mit ADC2 um "dADC2"-Takte vor ADC1 mit je 4 Bit in Image[]
} TConfig;

typedef struct
{
  int32_t Flags;  // 0:gültige Daten; 1:VSync(bei Zeile 311 und 624); 2:Extrazeilentakt; 3..
  int32_t Error;  // Fehler vom Typ TError
  int32_t VSyncCnt;
  int32_t SubImageOfs;
  int32_t SndBufMinErrCnt;
  int32_t ADC_OverrunCnt;
  int32_t HSync_ErrCnt;    //
  int32_t HSync_Min;       // Low-Wert vom HSync-Pegel in ADC-Einheiten
  int32_t HSync_Threshold; // High/Low-Schwellwert vom HSync-Pegel in ADC-Einheiten
  int32_t HSync_Raw;       // je Zeile angewendete statische Korrektur für die Video-Zeile in CPU-Zyklen
  int32_t HSync_dL16;      // Abweichung einer Video-Zeile in 1/65536 CPU-Zyklen
  int32_t RefBlank;
  int32_t BurstAmp;
  int32_t kk0;
  int32_t kk1;
} TUSBItem_State;

typedef struct
{
  uint8_t  Flags;
  uint8_t  MinMax;
  uint16_t BurstPhi;
  uint8_t  Image[59];
} TUSBItem_Line;

typedef struct
{
  bool      Odd;
  bool      Adc2;
  uint8_t   SubImageOfs;
  bool      BurstOk;
  uint16_t  BurstPhi;
  int       y;

  int16_t   xSub;
  uint16_t  Phi; 
  uint16_t* Samples;

  uint16_t ADC1[_nSample];
  uint16_t ADC2[_nSample];
} TUSBItem_LineEx;

struct TImg
{
  TUSBItem_LineEx Img[320];
};

class TBlit
{
public:
  TBlit(int nx, int ny, LPCSTR InfoTxt, int nxWnd=0, int nyWnd=0);
  virtual ~TBlit();
  HDC  GetDC();
  void DoBlit();
  DWORD GetPixel(int x, int y);
  void SetPixel(int x, int y, DWORD RGB);
  int  SaveImg(LPCSTR Mask=NULL);
private:
  static LRESULT CALLBACK WndProc(HWND   hWnd,
                                  UINT   uMsg,
                                  WPARAM wParam,
                                  LPARAM lParam);
  HWND    _hWnd;
  HDC     _MemDC;
  HBITMAP _DibHBitmap;
  HDC     _PnDC;
  DWORD*  _DibPtr;
  int     _DibPitch;
  int     _nx;
  int     _ny;
};


void AddToImgArr(TUSBItem_Line* USBItem, int iFrame);

DWORD YUV_to_RGB(double Y, double U, double V);

extern int  _Mode;

extern int  _BrightnessBias;
extern int  _ContrastScale;
extern int  _ColorScale;

extern int  _iLine;     // Videozeile innerhalb eines Halbbildes
extern int  _TotalLine; // insgesamt empfangene Videozeilen

extern double _dPPM_PAL; // gemessener Wert der angewendet wird

extern TConfig _Config;

extern TUSBItem_State _State;

extern TImg*  ImgArr;

const int ImgMsk = 512-1;

extern TBlit* Blit;

double sin16(uint16_t v);
double cos16(uint16_t v);

void InitLine(TUSBItem_LineEx* LineRef,
              TUSBItem_LineEx* LineCur,
              int dADCRef=0,
              int dADCCur=0);

void LineToUV(TUSBItem_LineEx* Line0,
              TUSBItem_LineEx* Line1,
              TUSBItem_LineEx* Line2,
              TUSBItem_LineEx* Line3,
              uint16_t Phi0,
              uint16_t iSample,
              double*  ResU,
              double*  ResV);

void LineToY(TUSBItem_LineEx* Line0,
             TUSBItem_LineEx* Line1,
             uint16_t Phi0,
             uint16_t iSample,
             double   U,
             double   V,
             double*  ResY);

#endif // !defined(SHARED_H__INCLUDED_)
