/* ImLib3D
 * Copyright (c) 2001, ULP-IPB Strasbourg.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program 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
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*!   ConvenienceProcessors.hxx*/
#ifndef _ConvenienceProcessors_hxx
#define _ConvenienceProcessors_hxx

#include<ImLib3D/ConvenienceProcessors.hpp>
#include<ImLib3D/Threshold.hpp>


template <class ImageType>
void
IP3D::Crop(const ImageType& src,const RectZone3Di &zone,ImageType& res0)
{
	ImageProcessorArgs<ImageType> args(ImArgs(src),res0);
	ImageType &res=args.GetDest();
	res.properties=src.properties;
	// check coherence
	if(!(src.IsInside(zone.GetP0()) && src.IsInside(zone.GetP1())))
	{
		ThrowError("IP3D::Crop: subimage not in image");
	}
	typename ImageType::const_iteratorZone p(zone);
	res.Resize(Size3D(zone.Width(),zone.Height(),zone.Depth()));
	for( p=src.begin(); p!=src.end(); ++p )
	{
		res(p.Pos()-zone.GetP0())=*p;
	}
	// also crop mask if needed 
	if(src.HasMask())
	{
		IP3D::Crop(src.Mask(),zone,res.Mask());
	}
}



template <class ImageType>
void
IP3D::ApplyMask(const ImageType& src, ImageType& res, byte maskvalue, const Mask3D* optmask)
{
	Mask3D mask;
	if (optmask){ mask = *optmask;}
	else
	{ 
		if(!src.HasMask()){ThrowError("GetMask: no mask");}
		mask = src.Mask();
	}
	if (maskvalue > 0) {IP3D::SimpleThresholds2(mask, maskvalue, maskvalue, mask);}
	res = src;
	mask.ApplyOn(res);
}

#endif// _ConvenienceProcessors_hxx
