/*
 *  Brno University of Technology
 *
 *  Copyright (C) 2009-2010 by Brno University of Technology and the contributors
 *
 *  Complete list of developers available at our web site:
 *
 *       http://spl.utko.feec.vutbr.cz
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package cz.vutbr.feec.utko.opencv.operators.imageProcessing;


import java.util.List;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_video.*;

import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.parameter.ParameterType;
import com.rapidminer.parameter.ParameterTypeCategory;
import com.rapidminer.parameter.ParameterTypeDouble;
import com.rapidminer.parameter.UndefinedParameterError;

public class MotionGradient extends BaseProcessingOperator2 {

	private static final String PARAM_DELTA1 = "delta_1";
	private static final String PARAM_DELTA2 = "delta_2";
	private static final String PARAM_APERTURE = "aperture";
	private static final String[] APERTURES = { "SCHARR", "SOBEL 1x1",
			"SOBEL 3x3", "SOBEL 5x5", "SOBEL 7x7" };

	public MotionGradient(OperatorDescription description) {
		super(description);
	}

	@Override
	public IplImage doWork(IplImage cvImg1, IplImage cvImg2)
			throws UndefinedParameterError {
		IplImage orientation = cvImg2.clone();
		IplImage mask = cvCreateImage(cvSize(cvImg1.width(), cvImg1.height()),
				IPL_DEPTH_8U, 1);

		int aperture = 3;

		// see
		// http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_Motion_Tracking.htm#decl_cvCalcMotionGradient
		// for details
		switch (getParameterAsInt(PARAM_APERTURE)) {
		case 0:
			aperture = -1;
			break;
		case 1:
			aperture = 1;
			break;
		case 2:
			aperture = 3;
			break;
		case 3:
			aperture = 5;
			break;
		case 4:
			aperture = 7;
			break;
		}

		cvCalcMotionGradient(cvImg1, mask, orientation,
				getParameterAsDouble(PARAM_DELTA1),
				getParameterAsDouble(PARAM_DELTA2), aperture);

		return orientation;
	}

	@Override
	public List<ParameterType> getParameterTypes() {
		List<ParameterType> p = super.getParameterTypes();
		p.add(new ParameterTypeDouble(
				PARAM_DELTA1,
				"The function finds minimum (m(x,y)) and maximum (M(x,y)) MHI values over each pixel (x,y) neihborhood and assumes the gradient is valid only if min(delta1,delta2) <= M(x,y)-m(x,y) <= max(delta1,delta2).",
				0, Double.MAX_VALUE, 0));
		p.add(new ParameterTypeDouble(
				PARAM_DELTA2,
				"The function finds minimum (m(x,y)) and maximum (M(x,y)) MHI values over each pixel (x,y) neihborhood and assumes the gradient is valid only if min(delta1,delta2) <= M(x,y)-m(x,y) <= max(delta1,delta2).",
				0, Double.MAX_VALUE, 200));
		p.add(new ParameterTypeCategory(PARAM_APERTURE,
				"Aperture size of derivative operators used by the function",
				APERTURES, 2));
		return p;
	}
}
