#include #include #include namespace icl { namespace{ ///----------------------------------------------------------------------------------------------- /// no val ///----------------------------------------------------------------------------------------------- template struct AbsFunc{ static inline T f(const T t) { return abs(t); } }; template<> struct AbsFunc{ static inline icl32f f(const icl32f t) { return fabs(t); } }; template<> struct AbsFunc{ static inline icl64f f(const icl64f t) { return fabs(t); } }; template struct PixelFuncNoVal{ static inline T apply(const T t){ return t; } }; template struct PixelFuncNoVal{ static inline T apply(const T t){ return t*t; } }; template struct PixelFuncNoVal{ static inline T apply(const T t){ return clipped_cast(sqrt((double)t)); } }; template struct PixelFuncNoVal{ static inline T apply(const T t){ return clipped_cast(log((double)t)); } }; template struct PixelFuncNoVal{ static inline T apply(const T t){ return clipped_cast(exp((double)t)); } }; template struct PixelFuncNoVal{ static inline T apply(const T t){ return AbsFunc::f(t); } }; template struct LoopFuncNoVal{ // {{{ open static inline void apply(const Img *src, Img *dst ){ for(int c=src->getChannels()-1; c >= 0; --c) { ImgIterator itDst = dst->beginROI(c); for(const ImgIterator itSrc = src->beginROI(c),itSrcEnd=src->endROI(c) ; itSrc != itSrcEnd; ++itSrc, ++itDst){ *itDst = PixelFuncNoVal::apply(*itSrc); } } } }; // }}} ///----------------------------------------------------------------------------------------------- /// with val ///----------------------------------------------------------------------------------------------- template struct PixelFuncWithVal{ static inline T apply(const T t, T val){ return t+val; } }; template struct PixelFuncWithVal{ static inline T apply(const T t, T val){ return t+val; } }; template struct PixelFuncWithVal{ static inline T apply(const T t, T val){ return t-val; } }; template struct PixelFuncWithVal{ static inline T apply(const T t, T val){ return t/val; } }; template struct PixelFuncWithVal{ static inline T apply(const T t, T val){ return t*val; } }; template struct LoopFuncWithVal{ // {{{ open static inline void apply(const Img *src, Img *dst, T val ){ for(int c=src->getChannels()-1; c >= 0; --c) { ImgIterator itDst = dst->beginROI(c); for(const ImgIterator itSrc = src->beginROI(c), itSrcEnd=src->endROI(c) ; itSrc != itSrcEnd; ++itSrc, ++itDst){ *itDst = PixelFuncWithVal::apply(*itSrc,val); } } } }; // }}} #ifdef HAVE_IPP /*** IPP function specializations for "no val": sqrOp=10, sqrtOp=11, lnOp=12, expOp=13, absOp=14 ***/ template inline void ipp_call_no_val(const Img *src, Img *dst){ // {{{ open for (int c=src->getChannels()-1; c >= 0; --c) { func (src->getROIData (c), src->getLineStep(), dst->getROIData (c), dst->getLineStep(), dst->getROISize()); } } // }}} template inline void ipp_call_no_val_sfs(const Img *src, Img *dst){ // {{{ open for (int c=src->getChannels()-1; c >= 0; --c) { func (src->getROIData (c), src->getLineStep(), dst->getROIData (c), dst->getLineStep(), dst->getROISize(), 0); } } // }}} #define CREATE_IPP_FUNCTIONS_FOR_OP(OP,IPPOP) \ template<> struct LoopFuncNoVal{ \ static inline void apply(const Img *src, Img *dst ){ ipp_call_no_val_sfs(src, dst); } \ }; \ template<> struct LoopFuncNoVal{ \ static inline void apply(const Img *src, Img *dst ){ ipp_call_no_val_sfs(src, dst); } \ }; \ template<> struct LoopFuncNoVal{ \ static inline void apply(const Img *src, Img *dst ){ ipp_call_no_val(src, dst); } \ } CREATE_IPP_FUNCTIONS_FOR_OP(sqr,Sqr); CREATE_IPP_FUNCTIONS_FOR_OP(sqrt,Sqrt); CREATE_IPP_FUNCTIONS_FOR_OP(ln,Ln); CREATE_IPP_FUNCTIONS_FOR_OP(exp,Exp); #undef CREATE_IPP_FUNCTIONS_FOR_OP template<> struct LoopFuncNoVal{ static inline void apply(const Img *src, Img *dst ){ ipp_call_no_val(src, dst); } }; template<> struct LoopFuncNoVal{ static inline void apply(const Img *src, Img *dst ){ ipp_call_no_val(src, dst); } }; /*** IPP function specializations for "with val": addOp=0, subOp=1, divOp=2, mulOp=3, ***/ template inline void ipp_call_with_val(const Img *src, Img *dst, T val){ // {{{ open for (int c=src->getChannels()-1; c >= 0; --c) { func (src->getROIData (c), src->getLineStep(),val, dst->getROIData (c), dst->getLineStep(), dst->getROISize()); } } // }}} template inline void ipp_call_with_val_sfs(const Img *src, Img *dst, T val){ // {{{ open for (int c=src->getChannels()-1; c >= 0; --c) { func (src->getROIData (c), src->getLineStep(),val, dst->getROIData (c), dst->getLineStep(), dst->getROISize(),0); } } // }}} #define CREATE_IPP_FUNCTIONS_FOR_OP(OP,IPPOP) \ template<> struct LoopFuncWithVal{ \ static inline void apply(const Img *src, Img *dst, icl8u val ){ \ ipp_call_with_val_sfs(src, dst,val); \ } \ }; \ template<> struct LoopFuncWithVal{ \ static inline void apply(const Img *src, Img *dst, icl16s val ){ \ ipp_call_with_val_sfs(src, dst,val); \ } \ }; \ template<> struct LoopFuncWithVal{ \ static inline void apply(const Img *src, Img *dst, icl32f val ){ \ ipp_call_with_val(src, dst,val); \ } \ } CREATE_IPP_FUNCTIONS_FOR_OP(add,AddC); CREATE_IPP_FUNCTIONS_FOR_OP(sub,SubC); CREATE_IPP_FUNCTIONS_FOR_OP(div,DivC); CREATE_IPP_FUNCTIONS_FOR_OP(mul,MulC); #undef CREATE_IPP_FUNCTIONS_FOR_OP #endif template void apply_unary_arithmetical_op_no_val(const ImgBase *src, ImgBase *dst){ // {{{ open switch(src->getDepth()){ #define ICL_INSTANTIATE_DEPTH(D) case depth##D: LoopFuncNoVal::apply(src->asImg(),dst->asImg()); break; ICL_INSTANTIATE_ALL_DEPTHS; #undef ICL_INSTANTIATE_DEPTH default: ICL_INVALID_DEPTH; } } // }}} template void apply_unary_arithmetical_op_with_val(const ImgBase *src, ImgBase *dst, icl64f val){ // {{{ open switch(src->getDepth()){ #define ICL_INSTANTIATE_DEPTH(D) \ case depth##D: LoopFuncWithVal::apply(src->asImg(), \ dst->asImg(), clipped_cast(val)); break; ICL_INSTANTIATE_ALL_DEPTHS; #undef ICL_INSTANTIATE_DEPTH default: ICL_INVALID_DEPTH; } } // }}} } // end of anonymous namespace void UnaryArithmeticalOp::apply(const ImgBase *poSrc, ImgBase **poDst){ // {{{ open ICLASSERT_RETURN( poSrc ); if(!UnaryOp::prepare(poDst,poSrc)) return; switch(m_eOpType){ case addOp: apply_unary_arithmetical_op_with_val(poSrc,*poDst,m_dValue); break; case mulOp: apply_unary_arithmetical_op_with_val(poSrc,*poDst,m_dValue); break; case divOp: apply_unary_arithmetical_op_with_val(poSrc,*poDst,m_dValue); break; case subOp: apply_unary_arithmetical_op_with_val(poSrc,*poDst,m_dValue); break; case sqrOp: apply_unary_arithmetical_op_no_val(poSrc,*poDst); break; case sqrtOp: apply_unary_arithmetical_op_no_val(poSrc,*poDst); break; case lnOp: apply_unary_arithmetical_op_no_val(poSrc,*poDst); break; case expOp: apply_unary_arithmetical_op_no_val(poSrc,*poDst); break; case absOp: apply_unary_arithmetical_op_no_val(poSrc,*poDst); break; } } // }}} }