/******************************************************************** ** Image Component Library (ICL) ** ** ** ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** ** Neuroinformatics Group ** ** Website: www.iclcv.org and ** ** http://opensource.cit-ec.de/projects/icl ** ** ** ** File : ICLFilter/src/ICLFilter/BinaryArithmeticalOp.cpp ** ** Module : ICLFilter ** ** Authors: Christof Elbrechter ** ** ** ** ** ** GNU LESSER GENERAL PUBLIC LICENSE ** ** This file may be used under the terms of the GNU Lesser General ** ** Public License version 3.0 as published by the ** ** ** ** Free Software Foundation and appearing in the file LICENSE.LGPL ** ** included in the packaging of this file. Please review the ** ** following information to ensure the license requirements will ** ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** ** ** ** The development of this software was supported by the ** ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** ** Forschungsgemeinschaft (DFG) in the context of the German ** ** Excellence Initiative. ** ** ** ********************************************************************/ #include #include #include using namespace icl::utils; using namespace icl::core; namespace icl { namespace filter{ namespace{ template struct PixelFunc{ static inline T apply(const T t1, const T t2){ return t1+t2; } }; template struct PixelFunc{ static inline T apply(const T t1, const T t2){ return t1+t2; } }; template struct PixelFunc{ static inline T apply(const T t1, const T t2){ return t1-t2; } }; template struct PixelFunc{ static inline T apply(const T t1, const T t2){ return t1*t2; } }; template struct PixelFunc{ static inline T apply(const T t1, const T t2){ return t1/t2; } }; template struct PixelFunc{ static inline T apply(const T t1, const T t2){ return std::abs(t1-t2); } }; template struct LoopFunc{ // {{{ open static inline void apply(const Img *src1, const Img *src2, Img *dst ){ for(int c=src1->getChannels()-1; c >= 0; --c) { ImgIterator itDst = dst->beginROI(c); for(const ImgIterator itSrc1 = src1->beginROI(c),itSrc2 = src2->beginROI(c), itSrc1End = src1->endROI(c); itSrc1 != itSrc1End; ++itSrc1, ++itSrc2, ++itDst){ *itDst = PixelFunc::apply(*itSrc1,*itSrc2); } } } }; // }}} #ifdef ICL_HAVE_IPP template inline void ipp_call(const Img *src1,const Img *src2, Img *dst){ // {{{ open for (int c=src1->getChannels()-1; c >= 0; --c) { func (src1->getROIData (c), src1->getLineStep(), src2->getROIData (c), src2->getLineStep(), dst->getROIData (c), dst->getLineStep(), dst->getROISize()); } } // }}} template inline void ipp_call_sfs(const Img *src1,const Img *src2, Img *dst){ // {{{ open for (int c=src1->getChannels()-1; c >= 0; --c) { func (src1->getROIData (c), src1->getLineStep(), src2->getROIData (c), src2->getLineStep(), dst->getROIData (c), dst->getLineStep(), dst->getROISize(), 0); } } // }}} #define CREATE_IPP_FUNCTIONS_FOR_OP(OP,IPPOP) \ template<> struct LoopFunc{ \ static inline void apply(const Img *src1,const Img *src2, Img *dst ){ \ ipp_call_sfs(src2,src1,dst); \ } \ }; \ template<> struct LoopFunc{ \ static inline void apply(const Img *src1,const Img *src2, Img *dst ){ \ ipp_call_sfs(src2,src1, dst); \ } \ }; \ template<> struct LoopFunc{ \ static inline void apply(const Img *src1,const Img *src2, Img *dst ){ \ ipp_call(src2,src1, dst); \ } \ } CREATE_IPP_FUNCTIONS_FOR_OP(add,Add); CREATE_IPP_FUNCTIONS_FOR_OP(sub,Sub); CREATE_IPP_FUNCTIONS_FOR_OP(mul,Mul); CREATE_IPP_FUNCTIONS_FOR_OP(div,Div); /// for absDiff only 8u and 32f all supported by the IPP template<> struct LoopFunc{ static inline void apply(const Img *src1,const Img *src2, Img *dst ){ ipp_call(src2,src1,dst); } }; template<> struct LoopFunc{ static inline void apply(const Img *src1,const Img *src2, Img *dst ){ ipp_call(src2,src1,dst); } }; #undef CREATE_IPP_FUNCTIONS_FOR_OP #endif template void apply_op(const ImgBase *src1,const ImgBase *src2, ImgBase *dst){ // {{{ open switch(src1->getDepth()){ #define ICL_INSTANTIATE_DEPTH(D) case depth##D: LoopFunc::apply(src1->asImg(),src2->asImg(), dst->asImg()); break; ICL_INSTANTIATE_ALL_DEPTHS; #undef ICL_INSTANTIATE_DEPTH } } // }}} } // end of anonymous namespace void BinaryArithmeticalOp::apply(const ImgBase *poSrc1,const ImgBase *poSrc2, ImgBase **poDst){ // {{{ open ICLASSERT_RETURN( poSrc1 ); ICLASSERT_RETURN( poSrc2 ); ICLASSERT_RETURN( poDst ); if(!BinaryOp::check(poSrc1,poSrc2) || !BinaryOp::prepare(poDst,poSrc1)){ ERROR_LOG("source and destination images are not compatible [operation skipped!]"); return; } switch(m_eOpType){ case addOp: apply_op(poSrc1,poSrc2,*poDst); break; case mulOp: apply_op(poSrc1,poSrc2,*poDst); break; case divOp: apply_op(poSrc1,poSrc2,*poDst); break; case subOp: apply_op(poSrc1,poSrc2,*poDst); break; case absSubOp: apply_op(poSrc1,poSrc2,*poDst); break; } } // }}} } // namespace filter }