/*
* This file is part of robotreality
*
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
* http://opensource.cit-ec.de/projects/robotreality
*
* This file may be licensed under the terms of the
* GNU General Public License Version 3 (the ``GPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the GPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the GPL along with this
* program. If not, go to http://www.gnu.org/licenses/gpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*
*/
#ifndef BLOBFINDER_H
#define BLOBFINDER_H

#include <cstdio>
#include <cv.h>
#include <highgui.h>
#include "FastBlob.h"
#include <boost/thread/thread.hpp>
using namespace boost;

using namespace cv;
using namespace std;

typedef vector<FastBlob> blob_vector_t;

#define BLOBFINDER_USE_THREADS 1
#define BLOBFINDER_THREADS_OVERLAP_REGION_PX 20


class BlobFinder{
public:
	BlobFinder();
	~BlobFinder();
	void process_image(Mat *image);
	void overlay_blobs_on_image(Mat *image, int colorclass=-1);
	void overlay_text_on_image(Mat *image);
	void check_and_allocate(Size size);
	void set_lookuptable(int *table);
	blob_vector_t get_blob_vector(int colorclass=-1);
	
private:
	void remove_overlapping_regions();
	bool value_in_range(double value, double min, double max);
	bool blobs_overlap(FastBlob a, FastBlob b);
	void fill(int s, int col);
	void grow(Mat *m);
	
	#if BLOBFINDER_USE_THREADS
	BlobFinder *blobfinder_thread_obj[2];
	#endif
	
	FastBlob current_blob;
	
	std::vector<int> places_to_visit;
	blob_vector_t found_objects;
	
	
	bool uninitialised;
	uchar *visited_matrix;
	int visited_matrix_size;
	int visited_matrix_maxid;
	
	Mat *current_image;
	int image_width;
	int image_height;
	bool allocated_matrix;
	
	int *color_lookup;
};

#endif