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

#include "../finalpreview.h"


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

template <class MT,class VT>
void ApplyMask(i3d::Image3d<MT> const &mask, i3d::Image3d<VT> &pha) {
	const MT *pm=mask.GetFirstVoxelAddr();
	const MT *pmL=mask.GetFirstVoxelAddr() + mask.GetImageSize();
	VT *pp=pha.GetFirstVoxelAddr();

	while (pm != pmL) {
		if (*pm == 0) *pp=0;
		++pm; ++pp;
	}
}

int main(void) {
	//type of mask images
	typedef i3d::GRAY16 MV;
	typedef i3d::GRAY16 PV;

	//span of processed mask 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/man_track%.2lu.tif",T);
		cout << "reading mask: " << fn << endl;
		i3d::Image3d<MV> mask(fn);

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

		ApplyMask(mask,phantom);
		PrepareFinalPreviewImage(phantom,mask);

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

	return(0);
}
