#include <iostream>
#include <i3d/image3d.h>


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

template <class MT>
void Merge(i3d::Image3d<MT> &left, i3d::Image3d<MT> const &right) {
	MT *pl=left.GetFirstVoxelAddr();
	const MT *plL=left.GetFirstVoxelAddr() + left.GetImageSize();
	const MT *pr=right.GetFirstVoxelAddr();

	while (pl != plL) {
		*pl = std::max(*pl,*pr);
		++pl; ++pr;
	}
}

int main(int argc,char **argv) {
	//type of mask images
	typedef i3d::GRAY16 MV;

	i3d::Image3d<MV> left(argv[1]);
	i3d::Image3d<MV> right(argv[2]);
	Merge(left,right);
	left.SaveImage(argv[3]);

/*
	//span of processed files
	const size_t countFrom=0;
	const size_t countTo=99;

	//MAIN CYCLE
	for (size_t T=countFrom; T <= countTo; ++T) {
		//open input mask
		char fn[50];
		sprintf(fn,"GT/%.2lu.tif",T);
		cout << "reading mask: " << fn << endl;
		i3d::Image3d<MV> mask(fn);

		sprintf(fn,"%.2lu.tif",T);
		cout << "reading phantom: " << fn << endl;
		i3d::Image3d<PV> phantom(fn);

		ApplyMask(mask,phantom);

		//write polished phantom
		sprintf(fn,"%.2lu_cleaned.tif",T);
		cout << "writing phantom: " << fn << endl;
		phantom.SaveImage(fn);
	}
*/

	return(0);
}
