// Masking.cpp
// Chris Pocock 2014

#include "Masking.h"

Masking::Masking(Mat anOriginal) {

	original = anOriginal;
	zeroMask = Mat::zeros(original.size(),CV_8UC1);

	// Default padding values
	maskPaddingX = 100;
	maskPaddingY = 50;

	// Trackbar  upper limits
	maxMaskPaddingX = anOriginal.cols/2;
	maxMaskPaddingY = anOriginal.rows/2;
}

Masking::~Masking() {
}

// Masks input image and returns masked image
// Masks out pixels defined by a padded rectangle
Mat Masking::mask(int left, int right, int top, int bot, Mat &orig){

	masked = orig;

	if (left - maskPaddingX > 0)
		maskLeft = left - maskPaddingX;
	else
		maskLeft = 0;

	if (right+maskPaddingX < original.cols)
		maskRight = right+maskPaddingX ;
	else
		maskRight = original.cols;

	if (top-maskPaddingY > 0)
		maskTop = top-maskPaddingY;
	else
		maskTop = 0;

	if (bot+maskPaddingY < original.rows)
		maskBot = bot+maskPaddingY;
	else
		maskBot = original.rows;


	topLeftMask = Point2f(maskLeft ,maskTop);
	botRightMask = Point2f(maskRight, maskBot);
	maskRect = Rect(topLeftMask,botRightMask);

	zeroMask = Mat::zeros(original.size(),CV_8UC1);
	zeroMask(maskRect) = Scalar(255);
	masked.setTo(0,255-zeroMask);

	return masked;

}

// Return the padded rectangle
Rect Masking::getRect(){

	return maskRect;
}

// Create windows and trackbars for observation & live parameter tweaking
void Masking::setupUi(){

	namedWindow( "Masking", CV_WINDOW_AUTOSIZE );
	moveWindow("Masking", 0, 400);

	createTrackbar( "Mask Padding X", "Masking",
			&maskPaddingX,maxMaskPaddingX);

	createTrackbar( "Mask Padding Y", "Masking",
			&maskPaddingY,maxMaskPaddingY);
}
