/*
 *  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.awt.Canvas;
import java.util.List;

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 com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.JavaCvErrorCallback;
import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.UserError;
import com.rapidminer.parameter.ParameterType;
import com.rapidminer.parameter.ParameterTypeCategory;
import com.rapidminer.parameter.ParameterTypeDouble;
import com.rapidminer.parameter.ParameterTypeInt;
import com.sun.jna.ptr.DoubleByReference;

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

public class DistTransform extends BaseProcessingOperator {

	private static final String TYPE_OF_DISTANCE = "Type of distance";
	private static final String[] TYPES_OF_DISTANCE = new String[] { 
		"L1 Distance",
		"L2 Distance (Euclidian)", 
		"C Distance"};
	
	public static final int DIST_L1 = 0;
	public static final int DIST_L2 = 1;
	public static final int DIST_C = 2;
	
	private static final String PARAM1 = "size";
	
	public DistTransform(OperatorDescription description) {
		super(description);
	}

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

	/*	IplImage imgIn = null;
		if (cvImg.nChannels == 3)
		{
			imgIn = cvCreateImage(cvGetSize(cvImg), IPL_DEPTH_8U, 1);
			cvCvtColor( cvImg, imgIn, CV_RGB2GRAY );
		
		}
		else
		{
			imgIn = cvCloneImage(cvImg);
		}
		
		
		
		IplImage out = cvCreateImage(cvGetSize(cvImg), IPL_DEPTH_8U, 1);
		cvEqualizeHist(imgIn, out);
		
		IplImage out3 = cvCreateImage(cvGetSize(cvImg), IPL_DEPTH_8U, 3);
		cvCvtColor( out, out3, CV_GRAY2RGB );
		
		
		return out3;
*/
		
		IplImage imgIn = null;
		imgIn = cvCreateImage(cvGetSize(cvImg), IPL_DEPTH_8U, 1);
		cvCvtColor( cvImg, imgIn, CV_RGB2GRAY );
		
		IplImage out = cvCreateImage(cvGetSize(cvImg), IPL_DEPTH_32F, 1);
		
		
		int type = getParameterAsInt(TYPE_OF_DISTANCE);
		int cv_type = CV_DIFF_L1;
		switch (type) {
			case DIST_L1:
				cv_type = CV_DIST_L1;
				break;
			case DIST_L2:
				cv_type = CV_DIST_L2;
				break;
			case DIST_C:
				cv_type = CV_DIST_C;
				break;
				
				
		}
		int param1 = getParameterAsInt(PARAM1);
		cvDistTransform(imgIn, out, cv_type, param1, null, null );
			
		IplImage out255 = cvCreateImage(cvGetSize(cvImg), IPL_DEPTH_8U, 1);
		
		double[] min = new double[]{0};
		double[] max = new double[]{0};
		CvPoint min_pos = new CvPoint(0,0), max_pos = new CvPoint(0,0);
		cvMinMaxLoc(out,min, max, min_pos, max_pos, null);
		cvConvertScale(out, out255, 255./(max[0]-min[0]),min[0]);
		
		CanvasFrame canvas1 = new CanvasFrame("Out");
		canvas1.showImage(out255);
		
		return out255;
	}
	
	@Override
	public List<ParameterType> getParameterTypes() {
		List<ParameterType> p = super.getParameterTypes();
		
		p.add(new ParameterTypeCategory(TYPE_OF_DISTANCE,
				"Type of distance",
				TYPES_OF_DISTANCE, 1));
		
		p.add(new ParameterTypeInt(PARAM1, "Size of kernel.",1, 11, 3));
		
		return p;
	}
	
}
