/*
 *  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.imageTransforms;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;

import java.util.List;

import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.UserError;
import com.rapidminer.parameter.ParameterType;
import com.rapidminer.parameter.ParameterTypeDouble;
import com.rapidminer.parameter.ParameterTypeInt;

import cz.vutbr.feec.utko.opencv.operators.imageProcessing.BaseErodeDilate;

public class Canny extends BaseErodeDilate {

	private static final String PARAM_APERT_SIZE = "apperture_size";
	private static final String PARAM_MAX_THR = "max_threshold";
	private static final String PARAM_MIN_THR = "min_threshold";

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

	@Override
	public IplImage doWork(IplImage cvImg) throws OperatorException {

		if (getParameterAsDouble(PARAM_MIN_THR) > getParameterAsDouble(PARAM_MAX_THR)) {
			throw new UserError(this, 116,
					"Minimal threshold must be greater or equal the maximal threshold.");
		}

		IplImage out = cvCreateImage(cvGetSize(cvImg), IPL_DEPTH_8U, 3);
		cvCanny(cvImg, cvImg, getParameterAsDouble(PARAM_MIN_THR),
				getParameterAsDouble(PARAM_MAX_THR),
				getParameterAsInt(PARAM_APERT_SIZE));

		return out;

	}

	@Override
	public List<ParameterType> getParameterTypes() {
		List<ParameterType> p = super.getParameterTypes();
		p.add(new ParameterTypeDouble(PARAM_MIN_THR, "Minimal threshold", 0,
				Double.MAX_VALUE, 10));
		p.add(new ParameterTypeDouble(PARAM_MAX_THR, "Maximal threshold", 0,
				Double.MAX_VALUE, 100));
		p.add(new ParameterTypeInt(PARAM_APERT_SIZE, "Apperture size", 0,
				Integer.MAX_VALUE, 3));
		return p;
	}
}
