// AbstractAnalyzer.cpp: implementation of the AbstractAnalyzer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "analyze3.h"
#include "AbstractAnalyzer.h"
#include "MainPS.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////

AbstractAnalyzer::AbstractAnalyzer()
{
   dbg("AbstractAnalyzer constructor\n");
   nBytes = 0;
   nPixels = 0;
   vw = 0;
   vh = 0;   
   pFrame = NULL;
   int i;
   for( i = 0; i<NTEMP; i++)
   {
      pTempf1[i] = NULL;
   }
   vwnds = NULL;
   nVwnds = 0;
   ops = NULL;
}

//////////////////////////////////////////////////////////////////////

AbstractAnalyzer::~AbstractAnalyzer()
{
   Cleanup();
}

//////////////////////////////////////////////////////////////////////

void AbstractAnalyzer::SetOptionPages(OptionPages *ops)
{
   AbstractAnalyzer::ops = ops;
}

//////////////////////////////////////////////////////////////////////

int AbstractAnalyzer::CreateVwnds(int n)
{
   if (vwnds) return FALSE;

   nVwnds = n;
   vwnds = (CVideoWnd**)malloc(nVwnds * sizeof(CVideoWnd*));

   return TRUE;
}

//////////////////////////////////////////////////////////////////////

void AbstractAnalyzer::Cleanup()
{
   cvReleaseImage(&pFrame);

   int i;
   for( i = 0; i<NTEMP; i++)
   {
      cvReleaseImage(&pTempf1[i]);
   }

   for( i = 0; i<nVwnds; i++)
   {
      DeleteClean(vwnds[i]);
   }
   FreeClean(vwnds);
   nVwnds = 0;

   nPixels = nBytes = 0;
   vw = vh = 0;   
}

//////////////////////////////////////////////////////////////////////


void AbstractAnalyzer::OnStart(VIDEOINFOHEADER *pvih)
{
   dbg("AbstractAnalyzer OnStart...\n");
   Cleanup();

   // extract video dimensions from the structure
   vw = pvih->bmiHeader.biWidth;
   vh = pvih->bmiHeader.biHeight;      
   
   // calc some useful values, note we assume RGB = 3x8bit
   nPixels = vw*vh;
   nBytes = nPixels*3;

   dbg(" Video: %d x %d => %d pixels, %d bytes/image\n", vw, vh, nPixels, nBytes);
   
   // construct our various image buffers ahead of time   
   CvSize sz;
   sz.width = vw;
   sz.height = vh;   
   
   pFrame = cvCreateImage(sz, 8, 3);   
   iplSet(pFrame, 0);

   int i;
   for( i = 0; i<NTEMP; i++)
   {
      pTempf1[i] = cvCreateImage(sz, 32, 1);
   }

   // this doesn't work yet - area for improvement
   AviGetDuration(CAMERA, &duration);

   dbg("Finished Abstract OnStart -- duration: %d\n", (int)duration);
}

//////////////////////////////////////////////////////////////////////
