/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // Intel License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective icvers. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of Intel Corporation may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #ifndef __OPENCV_FAST_NLMEANS_DENOISING_INVOKER_COMMONS_HPP__ #define __OPENCV_FAST_NLMEANS_DENOISING_INVOKER_COMMONS_HPP__ using namespace cv; // std::isnan is a part of C++11 and it is not supported in MSVS2010/2012 #if defined _MSC_VER && _MSC_VER < 1800 /* MSVC 2013 */ #include namespace std { template bool isnan(T value) { return _isnan(value) != 0; } } #endif template struct pixelInfo_ { static const int channels = 1; typedef T sampleType; }; template struct pixelInfo_ > { static const int channels = n; typedef ET sampleType; }; template struct pixelInfo: public pixelInfo_ { typedef typename pixelInfo_::sampleType sampleType; static inline sampleType sampleMax() { return std::numeric_limits::max(); } static inline sampleType sampleMin() { return std::numeric_limits::min(); } static inline size_t sampleBytes() { return sizeof(sampleType); } static inline size_t sampleBits() { return 8*sampleBytes(); } }; class DistAbs { template struct calcDist_ { static inline int f(const T a, const T b) { return std::abs((int)(a-b)); } }; template struct calcDist_ > { static inline int f(const Vec a, const Vec b) { return std::abs((int)(a[0]-b[0])) + std::abs((int)(a[1]-b[1])); } }; template struct calcDist_ > { static inline int f(const Vec a, const Vec b) { return std::abs((int)(a[0]-b[0])) + std::abs((int)(a[1]-b[1])) + std::abs((int)(a[2]-b[2])); } }; template struct calcDist_ > { static inline int f(const Vec a, const Vec b) { return std::abs((int)(a[0]-b[0])) + std::abs((int)(a[1]-b[1])) + std::abs((int)(a[2]-b[2])) + std::abs((int)(a[3]-b[3])); } }; template struct calcWeight_ { static inline WT f(double dist, const float *h, WT fixed_point_mult) { double w = std::exp(-dist*dist / (h[0]*h[0] * pixelInfo::channels)); if (std::isnan(w)) w = 1.0; // Handle h = 0.0 static const double WEIGHT_THRESHOLD = 0.001; WT weight = (WT)cvRound(fixed_point_mult * w); if (weight < WEIGHT_THRESHOLD * fixed_point_mult) weight = 0; return weight; } }; template struct calcWeight_ > { static inline Vec f(double dist, const float *h, ET fixed_point_mult) { Vec res; for (int i=0; i(dist, &h[i], fixed_point_mult); return res; } }; public: template static inline int calcDist(const T a, const T b) { return calcDist_::f(a, b); } template static inline int calcDist(const Mat& m, int i1, int j1, int i2, int j2) { const T a = m.at(i1, j1); const T b = m.at(i2, j2); return calcDist(a,b); } template static inline int calcUpDownDist(T a_up, T a_down, T b_up, T b_down) { return calcDist(a_down, b_down) - calcDist(a_up, b_up); }; template static inline WT calcWeight(double dist, const float *h, typename pixelInfo::sampleType fixed_point_mult) { return calcWeight_::f(dist, h, fixed_point_mult); } template static inline int maxDist() { return (int)pixelInfo::sampleMax() * pixelInfo::channels; } }; class DistSquared { template struct calcDist_ { static inline int f(const T a, const T b) { return (int)(a-b) * (int)(a-b); } }; template struct calcDist_ > { static inline int f(const Vec a, const Vec b) { return (int)(a[0]-b[0])*(int)(a[0]-b[0]) + (int)(a[1]-b[1])*(int)(a[1]-b[1]); } }; template struct calcDist_ > { static inline int f(const Vec a, const Vec b) { return (int)(a[0]-b[0])*(int)(a[0]-b[0]) + (int)(a[1]-b[1])*(int)(a[1]-b[1]) + (int)(a[2]-b[2])*(int)(a[2]-b[2]); } }; template struct calcDist_ > { static inline int f(const Vec a, const Vec b) { return (int)(a[0]-b[0])*(int)(a[0]-b[0]) + (int)(a[1]-b[1])*(int)(a[1]-b[1]) + (int)(a[2]-b[2])*(int)(a[2]-b[2]) + (int)(a[3]-b[3])*(int)(a[3]-b[3]); } }; template struct calcUpDownDist_ { static inline int f(T a_up, T a_down, T b_up, T b_down) { int A = a_down - b_down; int B = a_up - b_up; return (A-B)*(A+B); } }; template struct calcUpDownDist_ > { private: typedef Vec T; public: static inline int f(T a_up, T a_down, T b_up, T b_down) { return calcDist(a_down, b_down) - calcDist(a_up, b_up); } }; template struct calcWeight_ { static inline WT f(double dist, const float *h, WT fixed_point_mult) { double w = std::exp(-dist / (h[0]*h[0] * pixelInfo::channels)); if (std::isnan(w)) w = 1.0; // Handle h = 0.0 static const double WEIGHT_THRESHOLD = 0.001; WT weight = (WT)cvRound(fixed_point_mult * w); if (weight < WEIGHT_THRESHOLD * fixed_point_mult) weight = 0; return weight; } }; template struct calcWeight_ > { static inline Vec f(double dist, const float *h, ET fixed_point_mult) { Vec res; for (int i=0; i(dist, &h[i], fixed_point_mult); return res; } }; public: template static inline int calcDist(const T a, const T b) { return calcDist_::f(a, b); } template static inline int calcDist(const Mat& m, int i1, int j1, int i2, int j2) { const T a = m.at(i1, j1); const T b = m.at(i2, j2); return calcDist(a,b); } template static inline int calcUpDownDist(T a_up, T a_down, T b_up, T b_down) { return calcUpDownDist_::f(a_up, a_down, b_up, b_down); }; template static inline WT calcWeight(double dist, const float *h, typename pixelInfo::sampleType fixed_point_mult) { return calcWeight_::f(dist, h, fixed_point_mult); } template static inline int maxDist() { return (int)pixelInfo::sampleMax() * (int)pixelInfo::sampleMax() * pixelInfo::channels; } }; template struct incWithWeight_ { static inline void f(IT* estimation, IT* weights_sum, WT weight, T p) { estimation[0] += (IT)weight * p; weights_sum[0] += (IT)weight; } }; template struct incWithWeight_, IT, WT> { static inline void f(IT* estimation, IT* weights_sum, WT weight, Vec p) { estimation[0] += (IT)weight * p[0]; estimation[1] += (IT)weight * p[1]; weights_sum[0] += (IT)weight; } }; template struct incWithWeight_, IT, WT> { static inline void f(IT* estimation, IT* weights_sum, WT weight, Vec p) { estimation[0] += (IT)weight * p[0]; estimation[1] += (IT)weight * p[1]; estimation[2] += (IT)weight * p[2]; weights_sum[0] += (IT)weight; } }; template struct incWithWeight_, IT, WT> { static inline void f(IT* estimation, IT* weights_sum, WT weight, Vec p) { estimation[0] += (IT)weight * p[0]; estimation[1] += (IT)weight * p[1]; estimation[2] += (IT)weight * p[2]; estimation[3] += (IT)weight * p[3]; weights_sum[0] += (IT)weight; } }; template struct incWithWeight_, IT, Vec > { static inline void f(IT* estimation, IT* weights_sum, Vec weight, Vec p) { estimation[0] += (IT)weight[0] * p[0]; estimation[1] += (IT)weight[1] * p[1]; weights_sum[0] += (IT)weight[0]; weights_sum[1] += (IT)weight[1]; } }; template struct incWithWeight_, IT, Vec > { static inline void f(IT* estimation, IT* weights_sum, Vec weight, Vec p) { estimation[0] += (IT)weight[0] * p[0]; estimation[1] += (IT)weight[1] * p[1]; estimation[2] += (IT)weight[2] * p[2]; weights_sum[0] += (IT)weight[0]; weights_sum[1] += (IT)weight[1]; weights_sum[2] += (IT)weight[2]; } }; template struct incWithWeight_, IT, Vec > { static inline void f(IT* estimation, IT* weights_sum, Vec weight, Vec p) { estimation[0] += (IT)weight[0] * p[0]; estimation[1] += (IT)weight[1] * p[1]; estimation[2] += (IT)weight[2] * p[2]; estimation[3] += (IT)weight[3] * p[3]; weights_sum[0] += (IT)weight[0]; weights_sum[1] += (IT)weight[1]; weights_sum[2] += (IT)weight[2]; weights_sum[3] += (IT)weight[3]; } }; template static inline void incWithWeight(IT* estimation, IT* weights_sum, WT weight, T p) { return incWithWeight_::f(estimation, weights_sum, weight, p); } template struct divByWeightsSum_ { static inline void f(IT* estimation, IT* weights_sum); }; template struct divByWeightsSum_ { static inline void f(IT* estimation, IT* weights_sum) { estimation[0] = (static_cast(estimation[0]) + weights_sum[0]/2) / weights_sum[0]; } }; template struct divByWeightsSum_ { static inline void f(IT* estimation, IT* weights_sum) { for (size_t i = 0; i < n; i++) estimation[i] = (static_cast(estimation[i]) + weights_sum[0]/2) / weights_sum[0]; } }; template struct divByWeightsSum_ { static inline void f(IT* estimation, IT* weights_sum) { for (size_t i = 0; i < n; i++) estimation[i] = (static_cast(estimation[i]) + weights_sum[i]/2) / weights_sum[i]; } }; template static inline void divByWeightsSum(IT* estimation, IT* weights_sum) { return divByWeightsSum_::f(estimation, weights_sum); } template struct saturateCastFromArray_ { static inline T f(IT* estimation) { return saturate_cast(estimation[0]); } }; template struct saturateCastFromArray_, IT> { static inline Vec f(IT* estimation) { Vec res; res[0] = saturate_cast(estimation[0]); res[1] = saturate_cast(estimation[1]); return res; } }; template struct saturateCastFromArray_, IT> { static inline Vec f(IT* estimation) { Vec res; res[0] = saturate_cast(estimation[0]); res[1] = saturate_cast(estimation[1]); res[2] = saturate_cast(estimation[2]); return res; } }; template struct saturateCastFromArray_, IT> { static inline Vec f(IT* estimation) { Vec res; res[0] = saturate_cast(estimation[0]); res[1] = saturate_cast(estimation[1]); res[2] = saturate_cast(estimation[2]); res[3] = saturate_cast(estimation[3]); return res; } }; template static inline T saturateCastFromArray(IT* estimation) { return saturateCastFromArray_::f(estimation); } #endif