head	1.5;
access;
symbols
	r0_10:1.5
	Eglin_alpha_1:1.5;
locks; strict;
comment	@// @;


1.5
date	93.05.27.11.48.55;	author thoth;	state Exp;
branches;
next	1.4;

1.4
date	93.04.29.11.20.56;	author thoth;	state Exp;
branches;
next	1.3;

1.3
date	93.04.17.18.56.44;	author jnw;	state Exp;
branches;
next	1.2;

1.2
date	93.04.08.13.21.59;	author thoth;	state Exp;
branches;
next	1.1;

1.1
date	93.03.18.11.39.42;	author thoth;	state Exp;
branches;
next	;


desc
@code template for a convolution in which the image may be
extended to a zero value for the operation ring.
@


1.5
log
@Copyright Notices
@
text
@// Emacs: -*- C++ -*-

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


//
// $Log:	absorber,v $
// Revision 1.4  93/04/29  11:20:56  thoth
// Faster(?) extension for VectorDIs.
// 
// Revision 1.3  93/04/17  18:56:44  jnw
// Fixed to match IA_IntPoint membername (dim)
// 
// Revision 1.2  93/04/08  13:21:59  thoth
// internal helper functions are now static.
// 
// Revision 1.1  93/03/18  11:39:42  thoth
// Initial revision
// 

#ifndef zero_extend_scan_IBTYPE
#define zero_extend_scan_IBTYPE

static void zero_extend_vec_scan(const IA_SetStructure &ss,
				 const IBTYPE **src,
				 IBTYPE **dest,
				 IBTYPE zero)
{
    for (unsigned i=0; i<ss.nintervals(); i++) {
	IA_ss_interval	temp(ss.retrieve_interval(i));
	if (temp.substructure == IA_SetStructure::FIRST_ONLY) {
	    (*src) += temp.count;
	} else if (temp.substructure == IA_SetStructure::BOTH) {
	    for (unsigned j=0; j<temp.count; j++) {
		*((*dest)++) = *((*src)++);
	    }
	} else if (temp.substructure == IA_SetStructure::SECOND_ONLY) {
	    for (unsigned j=0; j<temp.count; j++)
		*((*dest)++) = zero;
	} else {
	    for (unsigned j=0; j<temp.count; j++)
		zero_extend_vec_scan(temp.substructure, src, dest, zero);
	}
    }
}

static void zero_extend_iter_scan(const IA_SetStructure &ss,
				  IA_DIVIter<IBTYPE> *srciter,
				  IBTYPE **dest,
				  IBTYPE zero)
{
    for (unsigned i=0; i<ss.nintervals(); i++) {
	IA_ss_interval	temp(ss.retrieve_interval(i));
	if (temp.substructure == IA_SetStructure::FIRST_ONLY) {
	    IBTYPE	blah;
	    for (unsigned j=0; j<temp.count; j++) 
		(*srciter)(blah);
	} else if (temp.substructure == IA_SetStructure::BOTH) {
	    IBTYPE	blah;
	    for (unsigned j=0; j<temp.count; j++) {
		(*srciter)(blah);
		*((*dest)++) = blah;
	    }
	} else if (temp.substructure == IA_SetStructure::SECOND_ONLY) {
	    for (unsigned j=0; j<temp.count; j++)
		*((*dest)++) = zero;
	} else {
	    for (unsigned j=0; j<temp.count; j++)
		zero_extend_iter_scan(temp.substructure, srciter, dest, zero);
	}
    }
}

static void zero_extend(const IA_DiscreteImage<IBTYPE> &srcimg,
			const IA_IntPointSet &dest_ps, IBTYPE *dest_data,
			IBTYPE zero)
{
    IA_SetStructure	ss;
    intersect_with_dualstruct(srcimg.domain(), dest_ps, &ss);

    if (srcimg.type() == IA_VectorDI<IBTYPE>::s_type()) {
	IBTYPE	*srcdata = ((IA_VectorDI<IBTYPE>*)srcimg.bdip)->vec;
	zero_extend_vec_scan(ss, &srcdata, &dest_data, zero);
    } else {
	IA_DIVIter<IBTYPE>	iter(srcimg);
	zero_extend_iter_scan(ss, &iter, &dest_data, zero);
    }
}

#endif

static IA_DiscreteImage<RTYPE>
CONV_inv_core(IA_IntPoint src_infimum,
	      IA_IntPoint src_width,
	      const IBTYPE *src_data, // length is prod(src_width)
	      const IA_DiscreteImage<TBTYPE> &templ,
	      IA_IntPointSet dest_ps)
{
    const int	dimen = src_width.dim();

    IA_IntPointSet	templ_ps = templ.domain();
    int		templ_sz = templ_ps.card();
    TBTYPE	*const templ_data = new TBTYPE[templ_sz];
    int	*const templ_offsets = new int[templ_sz];

    {
	TBTYPE	*d_scan = templ_data;
	int	*o_scan = templ_offsets;
	IA_DIPIter<TBTYPE>	iter(templ);
	IA_IntPoint	ip;
	while (iter(ip, *d_scan)) {
	    *o_scan = ip[0];
	    for (unsigned i=1; i<dimen; i++) {
		*o_scan *= src_width[i];
		*o_scan += ip[i];
	    }
	    d_scan++;
	    o_scan++;
	}
    }

    IA_IPSIter	iter(dest_ps);
    IA_IntPoint	ip;
    RTYPE *const	dest_data = new RTYPE[dest_ps.card()];
    RTYPE *	valp = dest_data;
    while (iter(ip)) {
	int	offset= ip[0] - src_infimum[0];
	unsigned i;
	for (i=1; i<dimen; i++) {
	    offset *= src_width[i];
	    offset += ip[i] - src_infimum[i];
	}
	const RTYPE *const base = src_data + offset;

	// _IVAL_=(base[templ_offsets[i]])
	// _TVAL_=(templ_data[i])
	// _IRESULT_=(*valp)

	INITIALIZE ;

	for (i=0; i<templ_sz; i++) {
	    ACCUMULATE ;
	}
	{
	    RESULT ;
	}
	valp++;
    }
    delete[] templ_offsets;
    delete[] templ_data;

    return IA_DiscreteImage<RTYPE>(dest_ps, dest_data, dest_ps.card(), 1);
}

static IA_DiscreteImage<RTYPE>
CONV_inv(const IA_DiscreteImage<IBTYPE> &img,
	 const IA_DiscreteImage<TBTYPE> &invtempl,
	 IA_IntPointSet dest_ps)
{
    IA_IntPoint	inf_ = dest_ps.inf()+invtempl.domain().inf();
    IA_IntPoint	sup_ = dest_ps.sup()+invtempl.domain().sup();
    IA_IntPointSet	src_ps = IA_IntPointSet(inf_,sup_);

    IBTYPE	*const src_data = new IBTYPE[src_ps.card()];

    zero_extend(img, src_ps, src_data, ZERO);
    IA_DiscreteImage<RTYPE>	rval =
	CONV_inv_core(inf_, sup_-inf_ + 1, src_data, invtempl, dest_ps);

    delete[] src_data;

    return rval;
}

IA_DiscreteImage<RTYPE> CONV(const IA_DiscreteImage<IBTYPE> &img,
			     const IA_DDTemplate<TITYPE> &templ,
			     IA_IntPointSet dest_ps)
{
    if (templ.type() == IA_InvariantDT<TITYPE>::s_type()) {
	return CONV_inv(img, ((IA_InvariantDT<TITYPE>*)templ.bdtp)->value, dest_ps);
    } else {
	RTYPE *const	dest_data = new RTYPE[dest_ps.card()];
	RTYPE	*valp = dest_data;
	IA_IPSIter	dest_iter(dest_ps);
	IA_IntPoint	base_ip;
	while (dest_iter(base_ip)) {
	    IA_DiscreteImage<TBTYPE>	tv = templ(base_ip);

	    IA_DIPIter<TBTYPE>	templ_iter(tv);
	    IA_IntPoint	templ_ip;
	    TBTYPE	templ_val;

	    // _IVAL_=img(ip)
	    // _TVAL_=templ_val
	    // _IRESULT_=(*valp)

	    INITIALIZE ;

	    while ( templ_iter(templ_ip, templ_val) ) {
		const IA_IntPoint	ip = templ_ip; //+base_ip;
		if (! img.domain().contains(ip))
		    continue;
		ACCUMULATEVAR ;
	    }
	    {
		RESULT ;
	    }
	    valp++;
	}
	return IA_DiscreteImage<RTYPE>(dest_ps, dest_data, dest_ps.card(), 1);
    }
}
@


1.4
log
@Faster(?) extension for VectorDIs.
@
text
@d1 1
a1 1
// Emacs -*- C++ -*-
d4 6
d11 3
@


1.3
log
@Fixed to match IA_IntPoint membername (dim)
@
text
@d5 3
d15 27
a41 1
static void CONV_zero_extend_scan(const IA_SetStructure &ss,
d43 2
a44 1
				  IBTYPE **dest)
d60 1
a60 1
		*((*dest)++) = ZERO;
d63 1
a63 1
		CONV_zero_extend_scan(temp.substructure, srciter, dest);
d68 3
a70 2
static void CONV_zero_extend(const IA_DiscreteImage<IBTYPE> &srcimg,
			     const IA_IntPointSet &dest_ps, IBTYPE *dest_data)
d75 7
a81 2
    IA_DIVIter<IBTYPE>	iter(srcimg);
    CONV_zero_extend_scan(ss, &iter, &dest_data);
d84 2
d160 1
a160 1
    CONV_zero_extend(img, src_ps, src_data);
@


1.2
log
@internal helper functions are now static.
@
text
@d5 3
d55 1
a55 1
    const int	dimen = src_width.dimensions();
@


1.1
log
@Initial revision
@
text
@d4 4
a7 1
// $Log$
d9 3
a11 3
void CONV_zero_extend_scan(const IA_SetStructure &ss,
		      IA_DIVIter<IBTYPE> *srciter,
		      IBTYPE **dest)
d35 2
a36 2
void CONV_zero_extend(const IA_DiscreteImage<IBTYPE> &srcimg,
		      const IA_IntPointSet &dest_ps, IBTYPE *dest_data)
d47 4
a50 4
	  IA_IntPoint src_width,
	  const IBTYPE *src_data, // length is prod(src_width)
	  const IA_DiscreteImage<TBTYPE> &templ,
	  IA_IntPointSet dest_ps)
d108 4
a111 3
IA_DiscreteImage<RTYPE> CONV_inv(const IA_DiscreteImage<IBTYPE> &img,
				 const IA_DiscreteImage<TBTYPE> &invtempl,
				 IA_IntPointSet dest_ps)
d133 1
a133 1
	return CONV_inv(img, ((IA_InvariantDT<TITYPE>*)templ.bdip)->value, dest_ps);
@
