/******************************************************************** ** 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/BinaryCompareOp.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 using namespace icl::utils; using namespace icl::core; namespace icl { namespace filter{ #define ICL_COMP_ZERO 0 #define ICL_COMP_NONZERO 255 namespace{ #define CREATE_COMPARE_OP(NAME,OPERATOR) \ template struct CompareOp_##NAME { \ static inline icl8u cmp(T val1, T val2){ \ return val1 OPERATOR val2 ? ICL_COMP_NONZERO : ICL_COMP_ZERO; \ } \ } CREATE_COMPARE_OP(eq,==); CREATE_COMPARE_OP(lt,<); CREATE_COMPARE_OP(lteq,<=); CREATE_COMPARE_OP(gteq,>=); CREATE_COMPARE_OP(gt,>); #undef CREATE_COMPARE_OP template struct CompareOp_eqt { // {{{ open static inline icl8u cmp(T val1, T val2, T tolerance){ return std::abs(val1-val2)<=tolerance ? ICL_COMP_NONZERO : ICL_COMP_ZERO; } }; // }}} template class C> inline void fallbackCompare(const Img *src1,const Img *src2,Img *dst){ // {{{ open for(int c=src1->getChannels()-1; c >= 0; --c) { const ImgIterator itSrc1 = src1->beginROI(c); const ImgIterator itEnd = src1->endROI(c); const ImgIterator itSrc2 = src2->beginROI(c); ImgIterator itDst = dst->beginROI(c); for(;itSrc1 != itEnd ; ++itSrc1,++itSrc2, ++itDst){ *itDst = C::cmp(*itSrc1,*itSrc2); } } } // }}} template inline void fallbackCompareWithTolerance(const Img *src1,const Img *src2, Img8u *dst,T tolerance) { // {{{ open for(int c=src1->getChannels()-1; c >= 0; --c) { const ImgIterator itSrc1 = src1->beginROI(c); const ImgIterator itEnd = src1->endROI(c); const ImgIterator itSrc2 = src2->beginROI(c); ImgIterator itDst = dst->beginROI(c); for(;itSrc1 != itEnd; ++itSrc1,++itSrc2, ++itDst){ *itDst = CompareOp_eqt::cmp(*itSrc1,*itSrc2,tolerance); } } } // }}} template void cmp(const Img *src1,const Img *src2, Img8u *dst, T tolerance, BinaryCompareOp::optype ot){ // {{{ open switch(ot){ case BinaryCompareOp::lt: fallbackCompare(src1,src2,dst); break; case BinaryCompareOp::gt: fallbackCompare(src1,src2,dst); break; case BinaryCompareOp::lteq: fallbackCompare(src1,src2,dst); break; case BinaryCompareOp::gteq: fallbackCompare(src1,src2,dst); break; case BinaryCompareOp::eq: fallbackCompare(src1,src2,dst); break; case BinaryCompareOp::eqt: fallbackCompareWithTolerance(src1,src2,dst,tolerance); break; } } // }}} #ifdef ICL_HAVE_IPP template inline void ippCall(const Img *src1,const Img*src2, Img8u *dst, BinaryCompareOp::optype cmpOp){ // {{{ open for (int c=src1->getChannels()-1; c >= 0; --c) { ippiFunc (src1->getROIData (c), src1->getLineStep(), src2->getROIData(c), src2->getLineStep(), dst->getROIData (c), dst->getLineStep(), dst->getROISize(),(IppCmpOp) cmpOp); } } // }}} template<> void cmp(const Img8u *src1, const Img8u *src2, Img8u *dst, icl8u tolerance, BinaryCompareOp::optype ot){ // {{{ open if(ot == BinaryCompareOp::eqt){ fallbackCompareWithTolerance(src1,src2,dst,tolerance); }else{ ippCall(src1,src2,dst,ot); } } // }}} template<> void cmp(const Img16s *src1,const Img16s *src2, Img8u *dst, icl16s tolerance, BinaryCompareOp::optype ot){ // {{{ open if(ot == BinaryCompareOp::eqt){ fallbackCompareWithTolerance(src1,src2,dst,tolerance); }else{ ippCall(src1,src2,dst,ot); } } // }}} template<> void cmp(const Img32f *src1,const Img32f *src2, Img8u *dst, icl32f tolerance, BinaryCompareOp::optype ot){ // {{{ open if(ot == BinaryCompareOp::eqt){ for (int c=src1->getChannels()-1; c >= 0; --c) { ippiCompareEqualEps_32f_C1R (src1->getROIData (c), src1->getLineStep(), src2->getROIData (c), src2->getLineStep(), dst->getROIData (c), dst->getLineStep(), dst->getROISize(), tolerance); } }else{ ippCall(src1,src2,dst,ot); } } // }}} #endif } // end of anonymous namespace void BinaryCompareOp::apply(const ImgBase *poSrc1, const ImgBase *poSrc2, ImgBase **ppoDst){ // {{{ open ICLASSERT_RETURN( poSrc1 ); ICLASSERT_RETURN( poSrc2 ); ICLASSERT_RETURN( ppoDst ); if(!BinaryOp::check(poSrc1,poSrc2)){ ERROR_LOG("source images are not compatible: src 1:" << *poSrc1 << " src 2:" << *poSrc2); } if(!BinaryOp::prepare(ppoDst,poSrc1,depth8u)){ ERROR_LOG("unable to prepare the destintaion imaage to source image params and depth8u, src 1/2: " << *poSrc1); } switch (poSrc1->getDepth()){ #define ICL_INSTANTIATE_DEPTH(T) case depth##T: \ cmp(poSrc1->asImg(), \ poSrc2->asImg(), \ (*ppoDst)->asImg(), \ clipped_cast(m_dTolerance), \ m_eOpType); break; ICL_INSTANTIATE_ALL_DEPTHS; default: ICL_INVALID_FORMAT; break; #undef ICL_INSTANTIATE_DEPTH } } // }}} } // namespace filter }// end of namespace icl