#include "stdafx.h"
#include "analyze3.h"
#include "BGModel.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////

BGModel::BGModel(int w, int h)
{
   vw = w;
   vh = h;
   nPixels = vw  * vh;
   pixels = new PixelProcess[nPixels];
}

//////////////////////////////////////////////////////////////////////

BGModel::~BGModel()
{
   DeleteCleanA(pixels);
}

//////////////////////////////////////////////////////////////////////

/*
 "Process" every pixel in the image.
  
 This entails classifying the current pixel according to the
  background model, labelling each pixel in the FgMask image,
  and updating the background model.
 */
bool BGModel::Process(IplImage *pFrame, IplImage *pFgMask)
{
   if (!pFrame || !pFgMask) return false;
   
   BYTE *p = (BYTE*)pFrame->imageData;
   BYTE *fg = (BYTE*)pFgMask->imageData;   
   PixelCategory cat;

   int i;
   for( i = 0; i<nPixels; i++)
   {
      cat = pixels[i].Process(p);

      if (cat == PC_Foreground) 
      {
          *fg = 255;
      }
      else 
      {
          if (cat == PC_Background) 
          {
              *fg = 0;
          }
          else
          {
              *fg = 128;
          }
      }
      
      p += 3;
      fg++;
   }

   return true;
}

//////////////////////////////////////////////////////////////////////
