// This is the main DLL file.

#include "stdafx.h"

#include "OpenCVWrapper.h"

namespace OpenCVWrapper {  
  bool OCVWrap::initialize(){
    camera = new CvCapture*;

    // create all necessary instances
    *camera = cvCreateCameraCapture(CV_CAP_ANY);
    cascade = (CvHaarClassifierCascade*) cvLoad (CASCADE_NAME, 0, 0, 0);
    storage = cvCreateMemStorage(0);
    assert (storage);

    // you do own an iSight, don't you ?!?
    if (! *camera)
      return false;

    // did we load the cascade?!?
    if (! cascade)
      return false;

    return true;
  }

  bool OCVWrap::pollFaceCoordinates(){
    // get an initial frame and duplicate it for later work
    IplImage *  current_frame = cvQueryFrame (*camera);
    if (current_frame == NULL){
      return false;
    }
    IplImage *  gray_image    = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
    IplImage *  small_image   = cvCreateImage(cvSize (current_frame->width / 2, current_frame->height / 2), IPL_DEPTH_8U, 1);
    //assert (current_frame && gray_image);
    
    // convert to gray and downsize
    cvCvtColor (current_frame, gray_image, CV_BGR2GRAY);
    cvResize (gray_image, small_image, CV_INTER_LINEAR);
    
    CvSeq* faces;

    // Check the origin of image. 
    // if Top-Left, do normal recognition
    if( current_frame->origin == IPL_ORIGIN_TL ){
      // detect faces
      faces = cvHaarDetectObjects(small_image, cascade, storage,
                                    1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                                    cvSize (30, 30));


    // Else flip the image and do recognition
    }else{
      IplImage* flipped_image = cvCreateImage(cvSize (small_image->width, small_image->height), IPL_DEPTH_8U, 1);
      cvFlip( small_image, flipped_image, 0 );

      // detect faces
      faces = cvHaarDetectObjects(flipped_image, cascade, storage,
                                    1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                                    cvSize (30, 30));

      cvReleaseImage(&flipped_image);
    }

    // save height and width for face coordinates
    m_width = small_image->width;
    m_height = small_image->height;

    m_numFaces = 0;
    
    // draw faces
    for (int i = 0; i < (faces ? faces->total : 0); i++){			
      CvRect* r = (CvRect*) cvGetSeqElem (faces, i);
            
      m_lastX[m_numFaces] = r->x;
      m_lastY[m_numFaces] = r->y;
      m_lastWidth[m_numFaces] = r->width;
      m_lastHeight[m_numFaces] = r->height;

      m_numFaces++;
    }

    cvReleaseImage(&gray_image);
    cvReleaseImage(&small_image);
    return true;
  }

  void OCVWrap::deinitialize(){    
    cvReleaseCapture(camera);
    delete camera;
    camera = NULL;
    return;
  }

  int OCVWrap::getNumFaces(){
    return m_numFaces;
  }

  int OCVWrap::getX(int fID){
    if ((fID < 0) || (fID >= m_numFaces)){
      return -1;
    }
    return m_lastX[fID];
  }
  int OCVWrap::getY(int fID){
    if ((fID < 0) || (fID >= m_numFaces)){
      return -1;
    }
    return m_lastY[fID];
  }
  int OCVWrap::getWidth(int fID){
    if ((fID < 0) || (fID >= m_numFaces)){
      return -1;
    }
    return m_lastWidth[fID];
  }
  int OCVWrap::getHeight(int fID){
    if ((fID < 0) || (fID >= m_numFaces)){
      return -1;
    }
    return m_lastHeight[fID];
  }
      

}