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

/**
 * The Class Base2ProcessingOperator can handle processes, which has two input and one output.
 */
public abstract class BaseProcessingOperator2 extends Operator {

	/** The input opencv image 1. */
	protected InputPort inOpenCV1 = getInputPorts().createPort("color image 1");
	
	/** The input opencv image 2. */
	protected InputPort inOpenCV2 = getInputPorts().createPort("color image 2");
	
	/** The output opencv image */
	protected OutputPort outOpenCV = getOutputPorts().createPort(
			"color image out");

	/**
	 * Instantiates a new base2 processing operator.
	 *
	 * @param description the description
	 */
	public BaseProcessingOperator2(OperatorDescription description) {
		super(description);
		inOpenCV1.addPrecondition(new SimplePrecondition(inOpenCV1,
				new MetaData(OpenCVIOObject.class)));
		inOpenCV2.addPrecondition(new SimplePrecondition(inOpenCV2,
				new MetaData(OpenCVIOObject.class)));
		getTransformer().addGenerationRule(outOpenCV, OpenCVIOObject.class);
	}

	/* (non-Javadoc)
	 * @see com.rapidminer.operator.Operator#doWork()
	 */
	@Override
	public void doWork() throws OperatorException {
		OpenCVIOObject cvIO1 = inOpenCV1.getData();
		OpenCVIOObject cvIO2 = inOpenCV2.getData();

		IplImage cvImg1 = cvIO1.getImage();
		IplImage cvImg2 = cvIO2.getImage();

		outOpenCV.deliver( new OpenCVIOObject(doWork(cvImg1, cvImg2)));

	}

	/**
	 * Do work.
	 *
	 * @param cvImg1 the cv img1
	 * @param cvImg2 the cv img2
	 * @return the iO object
	 */
	public abstract IplImage doWork(IplImage cvImg1, IplImage cvImg2) throws OperatorException;
}
