/*********************************************************************
 * This file is part of the PRAPI library.
 *
 * Copyright (C) 2001-2003 Topi Mäenpää
 * Copyright (C) 2001 Jaakko Viertola
 * All rights reserved.
 *
 * This program is free software. You can redistribute and/or modify
 * it under the terms of the free software licence found in the
 * accompanying file "COPYING". The licence terms must always be
 * redistributed with this source file. The above copyright notice
 * must be reproduced in all modified and unmodified copies of this
 * source file.
 *
 * $Revision: 1.8 $
 *********************************************************************/

#ifndef _CONTRAST_H
#define _CONTRAST_H

#include "../FeatureExtractor.h"
#include "../ImageTransform.h"
#include "CircularLocalSampler.h"
#include <Math.h>

namespace prapi { namespace texture {
	
	/**
	 * A class for calculating local contrast. The contrast for a pixel
	 * is defined to be the difference bethween the average gray level of
	 * those pixels which have value 1 (bigger than center pixel) and
	 * those which have value 0 (less than center pixel).
	 **/
	class Contrast : public CircularLocalSampler<double, double>
	{
	public:
		Contrast(unsigned int samples=8, int predicate=1, bool interpolate=true) :
			CircularLocalSampler<double,double>(samples,predicate,interpolate) {}

		double getValue(double center, List<double>& surrounding)
		{
			int sumOnes= 0,sumZeros=0;
			double ones = 0.0,zeros = 0.0,tmp = 0.0;
			// calculate the contrast.
			for(int i=surrounding.getLength();i--;)
				{
					tmp = surrounding[i];
					tmp >= center ? ones+=tmp,sumOnes++:zeros+=tmp,sumZeros++;
				}
			return (ones/double(sumOnes)-zeros/double(sumZeros));
		}
	};

}}
#endif
