// Emacs: -*- C++ -*-

//
//	Copyright 1993, Center for Computer Vision and Visualization,
//	University of Florida.  All rights reserved.
//


//
// $Log: unary-op.templ,v $
// Revision 1.4.1.2  1995/01/13  19:37:42  thoth
// CoreImage has been re-merged with Image.  All special stuff should be
// friends of FBI now.
//
// Revision 1.4.1.1  1994/12/28  17:22:11  thoth
// image operations are friends of FBI now.
//
// Revision 1.4  1994/07/25  17:26:39  thoth
// Name sanitization
//
// Revision 1.3  1994/01/07  15:15:14  thoth
// Image class is now CoreImage and named image types are
// Image<P,T>.
//
// Revision 1.2  1993/12/29  17:33:18  thoth
// New operator scheme that prevents the need for trivial Image conversions.
//
// Revision 1.1  1993/09/15  13:07:16  thoth
// Initial revision
//
// Revision 1.2  93/05/27  11:49:04  thoth
// Copyright Notices
// 
// Revision 1.1  93/03/10  13:50:00  thoth
// Initial revision
// 

#ifndef UOT_ATYPE_LTYPE_OPNAME
#define UOT_ATYPE_LTYPE_OPNAME
IA_UnaryOperationsTable<IA_BaseImage<IA_Point<int>, ATYPE>*,
	IA_BaseImage<IA_Point<int>, LTYPE> >
	*IA_Image_IP_ATYPE_LTYPE_OPNAME_tbl;

void IA_fill_Image_IP_ATYPE_LTYPE_OPNAME_tbl();
#endif


IA_Image<IA_Point<int>, ATYPE>
OP ( const IA_Image<IA_Point<int>, LTYPE> &arg)
{
    if (!IA_Image_IP_ATYPE_LTYPE_OPNAME_tbl)
	IA_fill_Image_IP_ATYPE_LTYPE_OPNAME_tbl();

    IA_BaseImage<IA_Point<int>, ATYPE>*(*f)
	(IA_BaseImage<IA_Point<int>, LTYPE> &);
    f = IA_Image_IP_ATYPE_LTYPE_OPNAME_tbl->lookup_operation (arg.type());

    
    IA_BaseImage<IA_Point<int>, LTYPE>	&bip =
	*IA_FBI<IA_Point<int>,IA_Point<int>,LTYPE,LTYPE,LTYPE>
	    ::extract_baseptr(arg);

    return IA_FBI<IA_Point<int>,IA_Point<int>, ATYPE,ATYPE,ATYPE>
	 ::make_Image(f(bip));
}

#ifndef vec_uop_ATYPE_LTYPE_OPNAME
#define vec_uop_ATYPE_LTYPE_OPNAME
static IA_BaseImage<IA_Point<int>, ATYPE>*
vec_uo_OPNAME(IA_BaseImage<IA_Point<int>, LTYPE> &lhs_)
{
    const IA_VectorI<IA_Point<int>, LTYPE> *lhs =
	(IA_VectorI<IA_Point<int>, LTYPE> *) &lhs_;

    const IA_Set<IA_Point<int> > domain = lhs->domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	const LTYPE *ls = lhs->vec;
	for (unsigned i=0; i<sz; i++) {
	    *(ds++) = SUBOP(*(ls++));
	}
    }
    return new IA_VectorI<IA_Point<int>, ATYPE>(domain, vec, sz, 1);
}
#endif // vec_uop_ATYPE_LTYPE_OPNAME

#ifndef scalar_uop_ATYPE_LTYPE_OPNAME
#define scalar_uop_ATYPE_LTYPE_OPNAME
static IA_BaseImage<IA_Point<int>, ATYPE>*
scalar_uo_OPNAME(IA_BaseImage<IA_Point<int>, LTYPE> &lhs_)
{
    const IA_ConstI<IA_Point<int>, LTYPE> *lhs =
	(IA_ConstI<IA_Point<int>, LTYPE> *) &lhs_;

    const IA_Set<IA_Point<int> > domain = lhs->domain();

    return new IA_ConstI<IA_Point<int>, ATYPE>(domain, SUBOP(lhs->value));
}
#endif // scalar_uop_ATYPE_LTYPE_OPNAME

// scalar-scalar operation is trivial

#ifndef iter_uop_ATYPE_LTYPE_OPNAME
#define iter_uop_ATYPE_LTYPE_OPNAME
static IA_BaseImage<IA_Point<int>, ATYPE>*
iter_uo_OPNAME(IA_BaseImage<IA_Point<int>, LTYPE> &lhs)
{
    const IA_Set<IA_Point<int> > domain = lhs.domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	LTYPE ltmp;
	IA_BaseIVIter<IA_Point<int>, LTYPE>	*liter = lhs.value_iterator();
	for (unsigned i=0; i<sz; i++) {
	    liter->next(ltmp);
	    *(ds++) = SUBOP(ltmp);
	}
	delete liter;
    }
    return new IA_VectorI<IA_Point<int>, ATYPE>(domain, vec, sz, 1);
}
#endif // iter_uop_ATYPE_LTYPE_OPNAME

#ifndef FILL_UOT_ATYPE_LTYPE_OPNAME
#define FILL_UOT_ATYPE_LTYPE_OPNAME
void IA_fill_Image_IP_ATYPE_LTYPE_OPNAME_tbl()
{
    if (IA_Image_IP_ATYPE_LTYPE_OPNAME_tbl)
	return;

    IA_UnaryOperationsTable<IA_BaseImage<IA_Point<int>, ATYPE>*,
	IA_BaseImage<IA_Point<int>, LTYPE> >::function f;
    f = iter_uo_OPNAME;

    IA_Image_IP_ATYPE_LTYPE_OPNAME_tbl = new IA_UnaryOperationsTable<IA_BaseImage<IA_Point<int>, ATYPE>*,
	IA_BaseImage<IA_Point<int>, LTYPE> >(iter_uo_OPNAME);
    IA_Image_IP_ATYPE_LTYPE_OPNAME_tbl->add_operation
	(IA_VectorI<IA_Point<int>, LTYPE>::s_type(),
	 vec_uo_OPNAME);
    IA_Image_IP_ATYPE_LTYPE_OPNAME_tbl->add_operation
	(IA_ConstI<IA_Point<int>, LTYPE>::s_type(),
	 scalar_uo_OPNAME);

}
#endif // UOT_ATYPE_LTYPE_OPNAME
