/*
 *  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 com.googlecode.javacv.JavaCvErrorCallback;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.rapidminer.operator.IOObject;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.ports.InputPort;
import com.rapidminer.operator.ports.OutputPort;
import com.rapidminer.operator.ports.metadata.MetaData;
import com.rapidminer.operator.ports.metadata.SimplePrecondition;

import cz.vutbr.feec.utko.opencv.io.OpenCVIOObject;

public abstract class BaseProcessingOperator extends Operator {

	protected InputPort inOpenCV = getInputPorts().createPort("image");
	protected OutputPort outOpenCV = getOutputPorts().createPort("image");

	public BaseProcessingOperator(OperatorDescription description) {
		this(description, OpenCVIOObject.class, OpenCVIOObject.class);
	}
			
	public BaseProcessingOperator(OperatorDescription description,
			Class<? extends IOObject> inType, Class<? extends IOObject> outType) {
		super(description);
		inOpenCV.addPrecondition(new SimplePrecondition(inOpenCV, new MetaData(
				inType)));
		getTransformer().addGenerationRule(outOpenCV, outType);
	}

	public final void doWork() throws OperatorException {
		OpenCVIOObject ioObject = inOpenCV.getData();
		IplImage original = ioObject.getImage();

		OpenCVIOObject outObj = new OpenCVIOObject(doWork(original));
		outOpenCV.deliver(outObj);
	}

	public abstract IplImage doWork(IplImage img) throws OperatorException;

}
