/*
 * Tools related to the MitoPacq, the CBIA time-lapse cell observation simulator.
 *
 * Copyright (C) 2005-2013   Centre for Biomedical Image Analysis (CBIA)
 *
 * This library-related tools is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <iostream>
#include <i3d/image3d.h>
#include <cytopacq/optigen.h>
#include <ini/iniparser.h>

using std::cout;
using std::endl;
using std::cerr;

using namespace i3d;

//---------------------------------------------------------------------------
// the function describing the use of this console application
// it is called whenever the program is called without parameters
// or with bad combination of parameters
//----------------------------------------------------------------------------
void usage(const char *name)
{
	 cerr << "APPLICATION NAME" << endl <<
				"\t" << name << " ... application first blurs the given image respecting the provided mask and then writes max. intensity value " <<
				endl << endl;
	 cerr << "SYNTAX" << endl << "\t" << name << " <phantom image> <mask image> <config_file>" << endl << endl;
}

//-----------------------------------------------------------------------------

template <class T> T GetMaxValue(const char *imgPhantomName, 
											const char *imgMaskName,
											const char *configFile)
{
	 IniHandler 
				iniParams(configFile);

	 Image3d<T> 
				imgPhantom(imgPhantomName), 
				imgMask(imgMaskName);

	 int cytoLevel = iniParams["cell"]["intensity of nonspecific staining"];

	 cout << "cyto level: " << cytoLevel << endl;

	 for (size_t i=0; i<imgPhantom.GetImageSize(); i++)
	 {
		  if (imgMask.GetVoxel(i))
		  {
				T *value = imgPhantom.GetVoxelAddr(i);
				*value += cytoLevel;
		  }
	 }

	 CallOptigen(iniParams, imgPhantom);

	 return imgPhantom.GetMaxValue();
}

//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
    cout << argv[0] << " - part of the tools set of the OpticalFlow library \n"
         "Copyright (C) 2005-2013 Centre for Biomedical Image Analysis (CBIA)\n"
         "This is free software; see the source for copying conditions. "
         "There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR "
         "A PARTICULAR PURPOSE.\n\n";

	 if (argc < 4)
	 {
		  usage(argv[0]);
		  exit(-1);
	 }

	 try {
		  ImgVoxelType vt = ReadImageType(argv[1]);

		  switch (vt)
		  {
				case Gray16Voxel:
						  {
								cout << "MAXX: " << GetMaxValue<GRAY16>(argv[1],
																	 argv[2],
																	 argv[3]) << endl;
								break;
						  }
				case Gray8Voxel:
						  {
								cout << "MAXX: " << GetMaxValue<GRAY8>(argv[1],
																	argv[2],
																	argv[3]) << endl;
								break;
						  }
				default:
						  cerr << "Other voxel types not supported" << endl;
		  }
	 }
	 catch (const char *e)
	 {
		cout << e << endl;
	 }
	 catch (std::string &e)
	 {
		cout << e << endl;
	 }
	 catch (i3d::IOException& e)
	 {
		cout << e << endl;
	 }
	 catch (i3d::InternalException& e)
	 {
		cout << e << endl;
	 }
	 catch (std::bad_alloc&)
	 {
		cout << "Not enough memory." << endl;
	 }
	 catch (...)
	 {
		cout << "System exception." << endl;
	 }

	 return 0;
}
