/*
 * Copyright (c) 2015-2017, Sumio Morioka
 * All rights reserved.
 *
 * This source code was originally written by Dr.Sumio Morioka for use in the 2015-2016 issues of 
 * "the Interface magazine", published by CQ publishing Co.Ltd in Japan (http://www.cqpub.co.jp).
 * The author has no responsibility on any results caused by using this code.
 *
 * - Author: Dr.Sumio Morioka (http://www002.upp.so-net.ne.jp/morioka, FB:Sumio Morioka)
 *
 *
 * IMPORTANT NOTICE:
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the copyright holder nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <time.h>

#include "cv.h"
#include "highgui.h"
//using namespace cv;		// comment in if preferable
#include <opencv2/opencv.hpp>		// added; OpenCV 3.1
#include <opencv2/tracking.hpp>		// added; OpenCV 3.1

#include "opencv_func.h"

////////////////////////////////////////////////////////////////////////////
// >> ADD YOUR ORIGINAL CODE HERE
// any functions
////////////////////////////////////////////////////////////////////////////

void opencv_func_init(int width, int height)
{
	cv::namedWindow(WINNAME, CV_WINDOW_AUTOSIZE);

////////////////////////////////////////////////////////////////////////////
// >> ADD YOUR ORIGINAL CODE HERE
// preparation for image processing
////////////////////////////////////////////////////////////////////////////
}


void opencv_func_run(IMG_PROCESSING_PARAM *param)
{
////////////////////////////////////////////////////////////////////////////
// >> ADD YOUR ORIGINAL CODE HERE
// image processing
	{
		std::vector<cv::Rect>	faces;
		cv::Rect				r;

//		(*(param->srcImage)).copyTo(*(param->dstImage));
		cv::cvtColor(*(param->srcImage), *(param->dstImage), CV_BGR2RGB);	// modify color

		if (find_face_flag_G == 0) {
			(*(param->cascade)).detectMultiScale(*(param->srcImage), faces, 1.2, 2, 
//					cv::CASCADE_FIND_BIGGEST_OBJECT
						  cv::CASCADE_SCALE_IMAGE
						| cv::CASCADE_DO_ROUGH_SEARCH
//						| cv::CASCADE_DO_CANNY_PRUNING
					, cv::Size(20, 20));

			if (faces.size() > 0) {	// detect face(s)
				r = faces[0];
				cv::rectangle(*(param->dstImage), cv::Point(r.x, r.y), cv::Point(r.x + r.width, r.y + r.height), CV_RGB(255, 0, 0), 2, CV_AA);

				roi_G.x			= r.x;
				roi_G.y			= r.y;
				roi_G.width		= r.width;
				roi_G.height	= r.height;

//				tracker_G	= cv::Tracker::create("KCF");			// Opencv 3.1 required
//				tracker_G	= cv::Tracker::create("BOOSTING");		// Opencv 3.1 required
//				tracker_G	= cv::Tracker::create("MIL");			// Opencv 3.1 required
				tracker_G	= cv::Tracker::create("TLD");			// Opencv 3.1 required
//				tracker_G	= cv::Tracker::create("MEDIANFLOW");	// Opencv 3.1 required

				tracker_G->init(*(param->srcImage), roi_G);
				find_face_flag_G	= 1;
			}
		}
		else {	// find_face_flag_G == 1
			if (tracker_G->update(*(param->srcImage), roi_G) == true) {
				cv::rectangle(*(param->dstImage), cv::Point(roi_G.x, roi_G.y), cv::Point(roi_G.x + roi_G.width, roi_G.y + roi_G.height), CV_RGB(0, 0, 255), 2, CV_AA);
			}
			else {
				tracker_G->clear();
				find_face_flag_G	= 0;
			}
		}
	}
////////////////////////////////////////////////////////////////////////////
}


void opencv_func_stop(void)
{
	cv::destroyWindow(WINNAME);

////////////////////////////////////////////////////////////////////////////
// ---> append preparation code for your image processing here
// <--- append preparation code for your image processing here
////////////////////////////////////////////////////////////////////////////
}

// end of file
