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

import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.parameter.ParameterType;
import com.rapidminer.parameter.ParameterTypeCategory;
import com.rapidminer.parameter.ParameterTypeInt;
import com.rapidminer.parameter.conditions.EqualTypeCondition;

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

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 AddBorder extends BaseProcessingOperator{
	
	private static final String OFFSET_X = "Offset x";
	private static final String OFFSET_Y = "Offset y";
	private static final String COLOR_R = "Red";
	private static final String COLOR_G = "Green";
	private static final String COLOR_B = "Blue";
	private static final String TYPE_OF_BORDER = "Units";
	private static final String[] TYPES_OF_BORDER = new String[] {
		"Replicate",
		"Constant"
		};
	
	private static final int BORDER_REPLICATE = 0;
	private static final int BORDER_CONSTANT = 1;

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

	@Override
	public IplImage doWork(IplImage img) throws OperatorException {
		int border = 0;
		int red = 0;
		int green = 0;
		int blue = 0;
		
		if(getParameterAsInt(TYPE_OF_BORDER)==BORDER_CONSTANT){
			red = getParameterAsInt(COLOR_R);
			green = getParameterAsInt(COLOR_G);
			blue = getParameterAsInt(COLOR_B);
			border = IPL_BORDER_CONSTANT;
		} else {
			border = IPL_BORDER_REPLICATE;
		}
		int xBorder = getParameterAsInt(OFFSET_X);
		int yBorder = getParameterAsInt(OFFSET_Y);
		
		IplImage outImage = cvCreateImage(cvSize(img.width()+2*xBorder,img.height()+2*yBorder), img.depth(), 3);
		
		cvCopyMakeBorder(img, outImage,cvPoint(xBorder, yBorder), border,cvScalar(blue, green, red, 0));
		
		return outImage;
	}
	
	public List<ParameterType> getParameterTypes() {
		List<ParameterType> p = super.getParameterTypes();
		
		p.add(new ParameterTypeInt(OFFSET_X, "Size of left and right border in pixels.", 1, 999, 1, false));
		p.add(new ParameterTypeInt(OFFSET_Y, "Size of top and bottom border in pixels.", 1, 999, 1, false));
		p.add(new ParameterTypeCategory(TYPE_OF_BORDER,
				"Repliacate will set borde pixel value to value of nereast pixel in original image. Constant will fill border with constant color.",
				TYPES_OF_BORDER, 0, true));
		
		ParameterType red = new ParameterTypeInt(COLOR_R, "Red channel of border color.", 0, 255, true);
		red.setDefaultValue(0);
		ParameterType green = new ParameterTypeInt(COLOR_G, "Green channel of border color.", 0, 255, true);
		green.setDefaultValue(0);
		ParameterType blue = new ParameterTypeInt(COLOR_B, "Blue channel of border color.", 0, 255, true);
		blue.setDefaultValue(0);
		
		red.registerDependencyCondition(new EqualTypeCondition(this, TYPE_OF_BORDER, TYPES_OF_BORDER, true, BORDER_CONSTANT));
		green.registerDependencyCondition(new EqualTypeCondition(this, TYPE_OF_BORDER, TYPES_OF_BORDER, true, BORDER_CONSTANT));
		blue.registerDependencyCondition(new EqualTypeCondition(this, TYPE_OF_BORDER, TYPES_OF_BORDER, true, BORDER_CONSTANT));
		
		p.add(red);
		p.add(green);
		p.add(blue);
		return p;
	}
}
