#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "cvaux.h"
#include "highgui.h"
#endif

#include "feature.h"
#include <vector>
#include <math.h>

#include "edgeFeature.h"
#define eohbins 6

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

edgeFeature::edgeFeature() {
	dominantOrientation=0;
	dominantOrientation2=-1;
}

edgeFeature::edgeFeature(int d) {
	dominantOrientation=d;
}

edgeFeature::edgeFeature(int d, int d2) {
	dominantOrientation=d;
	dominantOrientation2=d2;
}

edgeFeature::~edgeFeature() {

}

int edgeFeature::calcValue(IplImage *input, IplImage **bins, IplImage *red) {
	//printf("widths = %i\n", scaleX);
	float *integrals=new float[eohbins];
	float sum=0;
	
	for (int di=0;di<eohbins;++di){
		
		//get 4 points for integral image calculation
		int Ax=offsetX;
		int Ay=offsetY;
		int Dx=width*scaleX + offsetX;
		int Dy=height*scaleY + offsetY;
		int Bx=Ax;
		int By=Dy;
		int Cx=Dx;
		int Cy=Ay;

		float A=((float*)(bins[di]->imageData + bins[di]->widthStep*Ay))[Ax];
		float B=((float*)(bins[di]->imageData + bins[di]->widthStep*By))[Bx];
		float C=((float*)(bins[di]->imageData + bins[di]->widthStep*Cy))[Cx];
		float D=((float*)(bins[di]->imageData + bins[di]->widthStep*Dy))[Dx];
		integrals[di]=A+D-C-B;
		sum+=integrals[di];
	}

	float ret=0;
	if (sum>0) {
		if (dominantOrientation2>-1) {
			ret=((integrals[dominantOrientation]+integrals[dominantOrientation2])/sum)*10000;
		}else {
			ret=(integrals[dominantOrientation]/sum)*10000;	//multiply by 10000 to make integer (easier to sort, store)
		}	
	}

	delete [] integrals;
	return (int) ret;
}

#ifdef _EiC
main(1,"edgefeature.cpp");
#endif