package cz.vutbr.feec.utko.opencv.operators;

import java.util.List;

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.parameter.ParameterType;

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


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

public class FFTOperator extends Operator
{
	
	private OutputPort outOpenCv = getOutputPorts().createPort("color image");
	
	private InputPort inOpenCv = getInputPorts().createPort("color image");

	public FFTOperator(OperatorDescription description)
	{
		super(description);
		getTransformer().addGenerationRule(outOpenCv, OpenCVIOObject.class);
	}

	@Override
	public void doWork() throws OperatorException
	{
		OpenCVIOObject ioObject = inOpenCv.getData();
		IplImage original = ioObject.getImage();
		
		int dft_M = cvGetOptimalDFTSize(2*original.height() - 1);
		int dft_N = cvGetOptimalDFTSize(2*original.width() - 1);
		CvMat dft_A = CvMat.create(dft_M, dft_N);
		//original.depth
		CvMat tmp  = CvMat.create(original.height(), original.width(), CV_32FC1);
		
		// copy A to dft_A and pad dft_A with zeros
		
		cvGetSubRect( dft_A, tmp, cvRect(0,0,original.width(), original.height()));
		//cvCopy( original, tmp );
		cvGetSubRect(dft_A,	tmp,cvRect( original.width(), 0, dft_A.cols()-original.width(), original.height() ));
		cvZero( tmp );
		// no need to pad bottom part of dft_A with zeros because of
		// use nonzero_rows parameter in cvDFT() call below
				
		//IplImage newImg = IplImage.create(original.width, original.height, CV_32FC1, 3);
		cvDFT( dft_A, dft_A, CV_DXT_FORWARD,  original.height());		
		
		cvDFT( dft_A, dft_A, CV_DXT_INV_SCALE, original.height() );
		cvGetSubRect( dft_A, tmp, cvRect(0,0,original.width(),original.height()) );
		
		original = cvGetImage(dft_A, original);
		
		OpenCVIOObject outObj = new OpenCVIOObject(original);
		outOpenCv.deliver(outObj);
	}

	@Override
	public List<ParameterType> getParameterTypes()
	{
		// TODO Auto-generated method stub
		return super.getParameterTypes();
	}

}
