mirror of https://github.com/opencv/opencv.git
Implementation of bit-exact resize. Internal calls to linear resize updated to use bit-exact version. (#9468)
parent
84ee4d701a
commit
51cb56ef2c
57 changed files with 1300 additions and 112 deletions
@ -0,0 +1,291 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2017, Intel Corporation, all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
|
||||
#ifndef _CV_FIXEDPOINT_HPP_ |
||||
#define _CV_FIXEDPOINT_HPP_ |
||||
|
||||
#include "opencv2/core/softfloat.hpp" |
||||
|
||||
#ifndef CV_ALWAYS_INLINE |
||||
#if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) |
||||
#define CV_ALWAYS_INLINE inline __attribute__((always_inline)) |
||||
#elif defined(_MSC_VER) |
||||
#define CV_ALWAYS_INLINE __forceinline |
||||
#else |
||||
#define CV_ALWAYS_INLINE inline |
||||
#endif |
||||
#endif |
||||
|
||||
namespace |
||||
{ |
||||
|
||||
class fixedpoint64 |
||||
{ |
||||
private: |
||||
static const int fixedShift = 32; |
||||
|
||||
int64_t val; |
||||
fixedpoint64(int64_t _val) : val(_val) {} |
||||
static CV_ALWAYS_INLINE uint64_t fixedround(const uint64_t& _val) { return (_val + ((1LL << fixedShift) >> 1)); } |
||||
public: |
||||
typedef fixedpoint64 WT; |
||||
CV_ALWAYS_INLINE fixedpoint64() { val = 0; } |
||||
CV_ALWAYS_INLINE fixedpoint64(const int8_t& _val) { val = ((int64_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint64(const int16_t& _val) { val = ((int64_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint64(const int32_t& _val) { val = ((int64_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint64(const cv::softdouble& _val) { val = cvRound64(_val * cv::softdouble((int64_t)(1LL << fixedShift))); } |
||||
CV_ALWAYS_INLINE fixedpoint64& operator = (const int8_t& _val) { val = ((int64_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint64& operator = (const int16_t& _val) { val = ((int64_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint64& operator = (const int32_t& _val) { val = ((int64_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint64& operator = (const cv::softdouble& _val) { val = cvRound64(_val * cv::softdouble((int64_t)(1LL << fixedShift))); return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint64& operator = (const fixedpoint64& _val) { val = _val.val; return *this; } |
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE fixedpoint64 operator * (const ET& val2) const { return val * val2; } // Wrong rounding is possible for floating point types
|
||||
CV_ALWAYS_INLINE fixedpoint64 operator * (const fixedpoint64& val2) const |
||||
{ |
||||
//Assume -0x00000000C0000000 <= val2 <=0x0000000100000000 INT64_MIN <= val <= INT64_MAX, so shifted multiplication result is inside [INT64_MIN, INT64_MAX] range
|
||||
uint64_t uval = (uint64_t)((val ^ (val >> 63)) - (val >> 63)); |
||||
uint64_t umul = (uint64_t)((val2.val ^ (val2.val >> 63)) - (val2.val >> 63)); |
||||
int64_t ressign = (val >> 63) ^ (val2.val >> 63); |
||||
|
||||
uint64_t sh0 = fixedround((uval & 0xFFFFFFFF) * (umul & 0xFFFFFFFF)); |
||||
uint64_t sh1_0 = (uval >> 32) * (umul & 0xFFFFFFFF); |
||||
uint64_t sh1_1 = (uval & 0xFFFFFFFF) * (umul >> 32); |
||||
uint64_t sh2 = (uval >> 32) * (umul >> 32); |
||||
uint64_t val0_l = (sh1_0 & 0xFFFFFFFF) + (sh1_1 & 0xFFFFFFFF) + (sh0 >> 32); |
||||
uint64_t val0_h = (sh2 & 0xFFFFFFFF) + (sh1_0 >> 32) + (sh1_1 >> 32) + (val0_l >> 32); |
||||
val0_l &= 0xFFFFFFFF; |
||||
|
||||
if (ressign) |
||||
{ |
||||
val0_l = (~val0_l + 1) & 0xFFFFFFFF; |
||||
val0_h = val0_l ? ~val0_h : (~val0_h + 1); |
||||
} |
||||
return (int64_t)(val0_h << 32 | val0_l); |
||||
} |
||||
CV_ALWAYS_INLINE fixedpoint64 operator + (const fixedpoint64& val2) const { return fixedpoint64(val + val2.val); } |
||||
CV_ALWAYS_INLINE fixedpoint64 operator - (const fixedpoint64& val2) const { return fixedpoint64(val - val2.val); } |
||||
// CV_ALWAYS_INLINE fixedpoint64 operator + (const fixedpoint64& val2) const
|
||||
// {
|
||||
// int64_t nfrac = (int64_t)frac + val2.frac;
|
||||
// int64_t nval = (int64_t)val + val2.val + nfrac >> 32;
|
||||
// return nval > MAXINT32 ? beConv(MAXINT32, MAXINT32) : beConv((int32_t)(nval), 0);
|
||||
// }
|
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE operator ET() const { return cv::saturate_cast<ET>((int64_t)fixedround((uint64_t)val) >> fixedShift); } |
||||
CV_ALWAYS_INLINE operator double() const { return (double)val / (1LL << fixedShift); } |
||||
CV_ALWAYS_INLINE operator float() const { return (float)val / (1LL << fixedShift); } |
||||
CV_ALWAYS_INLINE bool isZero() { return val == 0; } |
||||
static CV_ALWAYS_INLINE fixedpoint64 zero() { return fixedpoint64(); } |
||||
static CV_ALWAYS_INLINE fixedpoint64 one() { return fixedpoint64((int64_t)(1LL << fixedShift)); } |
||||
friend class fixedpoint32; |
||||
}; |
||||
|
||||
class ufixedpoint64 |
||||
{ |
||||
private: |
||||
static const int fixedShift = 32; |
||||
|
||||
uint64_t val; |
||||
ufixedpoint64(uint64_t _val) : val(_val) {} |
||||
static CV_ALWAYS_INLINE uint64_t fixedround(const uint64_t& _val) { return (_val + ((1LL << fixedShift) >> 1)); } |
||||
public: |
||||
typedef ufixedpoint64 WT; |
||||
CV_ALWAYS_INLINE ufixedpoint64() { val = 0; } |
||||
CV_ALWAYS_INLINE ufixedpoint64(const uint8_t& _val) { val = ((uint64_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE ufixedpoint64(const uint16_t& _val) { val = ((uint64_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE ufixedpoint64(const uint32_t& _val) { val = ((uint64_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE ufixedpoint64(const cv::softdouble& _val) { val = _val.getSign() ? 0 : (uint64_t)cvRound64(_val * cv::softdouble((int64_t)(1LL << fixedShift))); } |
||||
CV_ALWAYS_INLINE ufixedpoint64& operator = (const uint8_t& _val) { val = ((uint64_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint64& operator = (const uint16_t& _val) { val = ((uint64_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint64& operator = (const uint32_t& _val) { val = ((uint64_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint64& operator = (const cv::softdouble& _val) { val = _val.getSign() ? 0 : (uint64_t)cvRound64(_val * cv::softdouble((int64_t)(1LL << fixedShift))); return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint64& operator = (const ufixedpoint64& _val) { val = _val.val; return *this; } |
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE ufixedpoint64 operator * (const ET& val2) const { return val * val2; } // Wrong rounding is possible for floating point types
|
||||
CV_ALWAYS_INLINE ufixedpoint64 operator * (const ufixedpoint64& val2) const |
||||
{ |
||||
//Assume val2 <=0x0000000100000000, so shifted multiplication result is less than val and therefore than UINT64_MAX
|
||||
uint64_t sh0 = fixedround((val & 0xFFFFFFFF) * (val2.val & 0xFFFFFFFF)); |
||||
uint64_t sh1_0 = (val >> 32) * (val2.val & 0xFFFFFFFF); |
||||
uint64_t sh1_1 = (val & 0xFFFFFFFF) * (val2.val >> 32); |
||||
uint64_t sh2 = (val >> 32) * (val2.val >> 32); |
||||
uint64_t val0_l = (sh1_0 & 0xFFFFFFFF) + (sh1_1 & 0xFFFFFFFF) + (sh0 >> 32); |
||||
uint64_t val0_h = (sh2 & 0xFFFFFFFF) + (sh1_0 >> 32) + (sh1_1 >> 32) + (val0_l >> 32); |
||||
val0_l &= 0xFFFFFFFF; |
||||
|
||||
return val0_h << 32 | val0_l; |
||||
} |
||||
CV_ALWAYS_INLINE ufixedpoint64 operator + (const ufixedpoint64& val2) const { return ufixedpoint64(val + val2.val); } |
||||
CV_ALWAYS_INLINE ufixedpoint64 operator - (const ufixedpoint64& val2) const { return ufixedpoint64(val - val2.val); } |
||||
// CV_ALWAYS_INLINE fixedpoint64 operator + (const fixedpoint64& val2) const
|
||||
// {
|
||||
// int64_t nfrac = (int64_t)frac + val2.frac;
|
||||
// int64_t nval = (int64_t)val + val2.val + nfrac >> 32;
|
||||
// return nval > MAXINT32 ? beConv(MAXINT32, MAXINT32) : beConv((int32_t)(nval), 0);
|
||||
// }
|
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE operator ET() const { return cv::saturate_cast<ET>(fixedround(val) >> fixedShift); } |
||||
CV_ALWAYS_INLINE operator double() const { return (double)val / (1LL << fixedShift); } |
||||
CV_ALWAYS_INLINE operator float() const { return (float)val / (1LL << fixedShift); } |
||||
CV_ALWAYS_INLINE bool isZero() { return val == 0; } |
||||
static CV_ALWAYS_INLINE ufixedpoint64 zero() { return ufixedpoint64(); } |
||||
static CV_ALWAYS_INLINE ufixedpoint64 one() { return ufixedpoint64((uint64_t)(1ULL << fixedShift)); } |
||||
friend class ufixedpoint32; |
||||
}; |
||||
|
||||
class fixedpoint32 |
||||
{ |
||||
private: |
||||
static const int fixedShift = 16; |
||||
|
||||
int32_t val; |
||||
fixedpoint32(int32_t _val) : val(_val) {} |
||||
static CV_ALWAYS_INLINE uint32_t fixedround(const uint32_t& _val) { return (_val + ((1 << fixedShift) >> 1)); } |
||||
public: |
||||
typedef fixedpoint64 WT; |
||||
CV_ALWAYS_INLINE fixedpoint32() { val = 0; } |
||||
CV_ALWAYS_INLINE fixedpoint32(const int8_t& _val) { val = ((int32_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint32(const uint8_t& _val) { val = ((int32_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint32(const int16_t& _val) { val = ((int32_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint32(const cv::softdouble& _val) { val = (int32_t)cvRound(_val * cv::softdouble((1 << fixedShift))); } |
||||
CV_ALWAYS_INLINE fixedpoint32& operator = (const int8_t& _val) { val = ((int32_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint32& operator = (const uint8_t& _val) { val = ((int32_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint32& operator = (const int16_t& _val) { val = ((int32_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint32& operator = (const cv::softdouble& _val) { val = (int32_t)cvRound(_val * cv::softdouble((1 << fixedShift))); return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint32& operator = (const fixedpoint32& _val) { val = _val.val; return *this; } |
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE fixedpoint32 operator * (const ET& val2) const { return val * val2; } // Wrong rounding is possible for floating point types
|
||||
CV_ALWAYS_INLINE fixedpoint64 operator * (const fixedpoint32& val2) const { return (int64_t)val * (int64_t)(val2.val); } |
||||
CV_ALWAYS_INLINE fixedpoint32 operator + (const fixedpoint32& val2) const { return fixedpoint32(val + val2.val); } |
||||
CV_ALWAYS_INLINE fixedpoint32 operator - (const fixedpoint32& val2) const { return fixedpoint32(val - val2.val); } |
||||
// CV_ALWAYS_INLINE fixedpoint32 operator + (const fixedpoint32& val2) const
|
||||
// {
|
||||
// int32_t nfrac = (int32_t)frac + val2.frac;
|
||||
// int32_t nval = (int32_t)val + val2.val + nfrac >> 32;
|
||||
// return nval > MAXINT32 ? beConv(MAXINT32, MAXINT32) : beConv((int32_t)(nval), 0);
|
||||
// }
|
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE operator ET() const { return cv::saturate_cast<ET>((int32_t)fixedround((uint32_t)val) >> fixedShift); } |
||||
CV_ALWAYS_INLINE operator double() const { return (double)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE operator float() const { return (float)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE bool isZero() { return val == 0; } |
||||
static CV_ALWAYS_INLINE fixedpoint32 zero() { return fixedpoint32(); } |
||||
static CV_ALWAYS_INLINE fixedpoint32 one() { return fixedpoint32((1 << fixedShift)); } |
||||
friend class fixedpoint16; |
||||
}; |
||||
|
||||
class ufixedpoint32 |
||||
{ |
||||
private: |
||||
static const int fixedShift = 16; |
||||
|
||||
uint32_t val; |
||||
ufixedpoint32(uint32_t _val) : val(_val) {} |
||||
static CV_ALWAYS_INLINE uint32_t fixedround(const uint32_t& _val) { return (_val + ((1 << fixedShift) >> 1)); } |
||||
public: |
||||
typedef ufixedpoint64 WT; |
||||
CV_ALWAYS_INLINE ufixedpoint32() { val = 0; } |
||||
CV_ALWAYS_INLINE ufixedpoint32(const uint8_t& _val) { val = ((uint32_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE ufixedpoint32(const uint16_t& _val) { val = ((uint32_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE ufixedpoint32(const cv::softdouble& _val) { val = _val.getSign() ? 0 : (uint32_t)cvRound(_val * cv::softdouble((1 << fixedShift))); } |
||||
CV_ALWAYS_INLINE ufixedpoint32& operator = (const uint8_t& _val) { val = ((uint32_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint32& operator = (const uint16_t& _val) { val = ((uint32_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint32& operator = (const cv::softdouble& _val) { val = _val.getSign() ? 0 : (uint32_t)cvRound(_val * cv::softdouble((1 << fixedShift))); return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint32& operator = (const ufixedpoint32& _val) { val = _val.val; return *this; } |
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE ufixedpoint32 operator * (const ET& val2) const { return val * val2; } // Wrong rounding is possible for floating point types
|
||||
CV_ALWAYS_INLINE ufixedpoint64 operator * (const ufixedpoint32& val2) const { return (uint64_t)val * (uint64_t)(val2.val); } |
||||
CV_ALWAYS_INLINE ufixedpoint32 operator + (const ufixedpoint32& val2) const { return ufixedpoint32(val + val2.val); } |
||||
CV_ALWAYS_INLINE ufixedpoint32 operator - (const ufixedpoint32& val2) const { return ufixedpoint32(val - val2.val); } |
||||
// CV_ALWAYS_INLINE fixedpoint32 operator + (const fixedpoint32& val2) const
|
||||
// {
|
||||
// int32_t nfrac = (int32_t)frac + val2.frac;
|
||||
// int32_t nval = (int32_t)val + val2.val + nfrac >> 32;
|
||||
// return nval > MAXINT32 ? beConv(MAXINT32, MAXINT32) : beConv((int32_t)(nval), 0);
|
||||
// }
|
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE operator ET() const { return cv::saturate_cast<ET>(fixedround(val) >> fixedShift); } |
||||
CV_ALWAYS_INLINE operator double() const { return (double)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE operator float() const { return (float)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE bool isZero() { return val == 0; } |
||||
static CV_ALWAYS_INLINE ufixedpoint32 zero() { return ufixedpoint32(); } |
||||
static CV_ALWAYS_INLINE ufixedpoint32 one() { return ufixedpoint32((1U << fixedShift)); } |
||||
friend class ufixedpoint16; |
||||
}; |
||||
|
||||
class fixedpoint16 |
||||
{ |
||||
private: |
||||
static const int fixedShift = 8; |
||||
|
||||
int16_t val; |
||||
fixedpoint16(int16_t _val) : val(_val) {} |
||||
static CV_ALWAYS_INLINE uint16_t fixedround(const uint16_t& _val) { return (_val + ((1 << fixedShift) >> 1)); } |
||||
public: |
||||
typedef fixedpoint32 WT; |
||||
CV_ALWAYS_INLINE fixedpoint16() { val = 0; } |
||||
CV_ALWAYS_INLINE fixedpoint16(const int8_t& _val) { val = ((int16_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint16(const uint8_t& _val) { val = ((int16_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE fixedpoint16(const cv::softdouble& _val) { val = (int16_t)cvRound(_val * cv::softdouble((1 << fixedShift))); } |
||||
CV_ALWAYS_INLINE fixedpoint16& operator = (const int8_t& _val) { val = ((int16_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint16& operator = (const cv::softdouble& _val) { val = (int16_t)cvRound(_val * cv::softdouble((1 << fixedShift))); return *this; } |
||||
CV_ALWAYS_INLINE fixedpoint16& operator = (const fixedpoint16& _val) { val = _val.val; return *this; } |
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE fixedpoint16 operator * (const ET& val2) const { return (int16_t)(val * val2); } // Wrong rounding is possible for floating point types
|
||||
CV_ALWAYS_INLINE fixedpoint32 operator * (const fixedpoint16& val2) const { return (int32_t)val * (int32_t)(val2.val); } |
||||
CV_ALWAYS_INLINE fixedpoint16 operator + (const fixedpoint16& val2) const { return fixedpoint16((int16_t)(val + val2.val)); } |
||||
CV_ALWAYS_INLINE fixedpoint16 operator - (const fixedpoint16& val2) const { return fixedpoint16((int16_t)(val - val2.val)); } |
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE operator ET() const { return cv::saturate_cast<ET>((int16_t)fixedround((uint16_t)val) >> fixedShift); } |
||||
CV_ALWAYS_INLINE operator double() const { return (double)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE operator float() const { return (float)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE bool isZero() { return val == 0; } |
||||
static CV_ALWAYS_INLINE fixedpoint16 zero() { return fixedpoint16(); } |
||||
static CV_ALWAYS_INLINE fixedpoint16 one() { return fixedpoint16((int16_t)(1 << fixedShift)); } |
||||
}; |
||||
|
||||
class ufixedpoint16 |
||||
{ |
||||
private: |
||||
static const int fixedShift = 8; |
||||
|
||||
uint16_t val; |
||||
ufixedpoint16(uint16_t _val) : val(_val) {} |
||||
static CV_ALWAYS_INLINE uint16_t fixedround(const uint16_t& _val) { return (_val + ((1 << fixedShift) >> 1)); } |
||||
public: |
||||
typedef ufixedpoint32 WT; |
||||
CV_ALWAYS_INLINE ufixedpoint16() { val = 0; } |
||||
CV_ALWAYS_INLINE ufixedpoint16(const uint8_t& _val) { val = ((uint16_t)_val) << fixedShift; } |
||||
CV_ALWAYS_INLINE ufixedpoint16(const cv::softdouble& _val) { val = _val.getSign() ? 0 : (uint16_t)cvRound(_val * cv::softdouble((int32_t)(1 << fixedShift))); } |
||||
CV_ALWAYS_INLINE ufixedpoint16& operator = (const uint8_t& _val) { val = ((uint16_t)_val) << fixedShift; return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint16& operator = (const cv::softdouble& _val) { val = _val.getSign() ? 0 : (uint16_t)cvRound(_val * cv::softdouble((int32_t)(1 << fixedShift))); return *this; } |
||||
CV_ALWAYS_INLINE ufixedpoint16& operator = (const ufixedpoint16& _val) { val = _val.val; return *this; } |
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE ufixedpoint16 operator * (const ET& val2) const { return (uint16_t)(val * val2); } // Wrong rounding is possible for floating point types
|
||||
CV_ALWAYS_INLINE ufixedpoint32 operator * (const ufixedpoint16& val2) const { return ((uint32_t)val * (uint32_t)(val2.val)); } |
||||
CV_ALWAYS_INLINE ufixedpoint16 operator + (const ufixedpoint16& val2) const { return ufixedpoint16((uint16_t)(val + val2.val)); } |
||||
CV_ALWAYS_INLINE ufixedpoint16 operator - (const ufixedpoint16& val2) const { return ufixedpoint16((uint16_t)(val - val2.val)); } |
||||
// CV_ALWAYS_INLINE fixedpoint16 operator + (const fixedpoint16& val2) const
|
||||
// {
|
||||
// int16_t nfrac = (int32_t)frac + val2.frac;
|
||||
// int16_t nval = (int32_t)val + val2.val + nfrac >> 16;
|
||||
// return nval > MAXINT32 ? beConv(MAXINT16, MAXINT16) : beConv((int16_t)(nval), 0);
|
||||
// }
|
||||
template <typename ET> |
||||
CV_ALWAYS_INLINE operator ET() const { return cv::saturate_cast<ET>(fixedround(val) >> fixedShift); } |
||||
CV_ALWAYS_INLINE operator double() const { return (double)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE operator float() const { return (float)val / (1 << fixedShift); } |
||||
CV_ALWAYS_INLINE bool isZero() { return val == 0; } |
||||
static CV_ALWAYS_INLINE ufixedpoint16 zero() { return ufixedpoint16(); } |
||||
static CV_ALWAYS_INLINE ufixedpoint16 one() { return ufixedpoint16((uint16_t)(1 << fixedShift)); } |
||||
}; |
||||
|
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,118 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#include "test_precomp.hpp" |
||||
|
||||
using namespace cv; |
||||
using namespace std; |
||||
|
||||
namespace |
||||
{ |
||||
static const int fixedShiftU8 = 8; |
||||
|
||||
template <typename T, int fixedShift> |
||||
void eval4(int64_t xcoeff0, int64_t xcoeff1, int64_t ycoeff0, int64_t ycoeff1, int cn, |
||||
uint8_t* src_pt00, uint8_t* src_pt01, uint8_t* src_pt10, uint8_t* src_pt11, uint8_t* dst_pt) |
||||
{ |
||||
static const int64_t fixedRound = ((1LL << (fixedShift * 2)) >> 1); |
||||
int64_t val = (((T*)src_pt00)[cn] * xcoeff0 + ((T*)src_pt01)[cn] * xcoeff1) * ycoeff0 + |
||||
(((T*)src_pt10)[cn] * xcoeff0 + ((T*)src_pt11)[cn] * xcoeff1) * ycoeff1 ; |
||||
((T*)dst_pt)[cn] = saturate_cast<T>((val + fixedRound) >> (fixedShift * 2)); |
||||
} |
||||
} |
||||
|
||||
TEST(Resize_Bitexact, Linear8U) |
||||
{ |
||||
static const int64_t fixedOne = (1L << fixedShiftU8); |
||||
|
||||
int types[] = { CV_8UC1, CV_8UC4 }; |
||||
// NOTICE: 2x downscaling ommitted since it use different rounding
|
||||
// 1/2 1 1 1/2 1/2 1/2 1/4 1/4 1/256 1/256 1/3 1/2 1/3 1/3 1/2 1/3 1/7 1/7
|
||||
Size dstsizes[] = {Size(512, 768), Size(1024, 384), Size(512, 384), Size(256, 192), Size(4, 3), Size(342, 384), Size(342, 256), Size(512, 256), Size(146, 110), |
||||
// 10/11 10/11 10/12 10/12 251/256 2 2 3 3 7 7
|
||||
Size(931, 698), Size(853, 640), Size(1004, 753), Size(2048,1536), Size(3072,2304), Size(7168,5376) }; |
||||
|
||||
for (int dsizeind = 0, _dsizecnt = sizeof(dstsizes) / sizeof(dstsizes[0]); dsizeind < _dsizecnt; ++dsizeind) |
||||
for (int typeind = 0, _typecnt = sizeof(types) / sizeof(types[0]); typeind < _typecnt; ++typeind) |
||||
{ |
||||
int type = types[typeind], depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
||||
int dcols = dstsizes[dsizeind].width, drows = dstsizes[dsizeind].height; |
||||
int cols = 1024, rows = 768; |
||||
|
||||
double inv_scale_x = (double)dcols / cols; |
||||
double inv_scale_y = (double)drows / rows; |
||||
softdouble scale_x = softdouble::one() / softdouble(inv_scale_x); |
||||
softdouble scale_y = softdouble::one() / softdouble(inv_scale_y); |
||||
|
||||
Mat src(rows, cols, type), refdst(drows, dcols, type), dst; |
||||
for (int j = 0; j < rows; j++) |
||||
{ |
||||
uint8_t* line = src.ptr(j); |
||||
for (int i = 0; i < cols; i++) |
||||
for (int c = 0; c < cn; c++) |
||||
{ |
||||
RNG rnd(0x123456789abcdefULL); |
||||
double val = j < rows / 2 ? ( i < cols / 2 ? ((sin((i + 1)*CV_PI / 256.)*sin((j + 1)*CV_PI / 256.)*sin((cn + 4)*CV_PI / 8.) + 1.)*128.) : |
||||
(((i / 128 + j / 128) % 2) * 250 + (j / 128) % 2) ) : |
||||
( i < cols / 2 ? ((i / 128) * (85 - j / 256 * 40) * ((j / 128) % 2) + (7 - i / 128) * (85 - j / 256 * 40) * ((j / 128 + 1) % 2)) : |
||||
((uchar)rnd) ) ; |
||||
if (depth == CV_8U) |
||||
line[i*cn + c] = (uint8_t)val; |
||||
else if (depth == CV_16U) |
||||
((uint16_t*)line)[i*cn + c] = (uint16_t)val; |
||||
else if (depth == CV_16S) |
||||
((int16_t*)line)[i*cn + c] = (int16_t)val; |
||||
else if (depth == CV_32S) |
||||
((int32_t*)line)[i*cn + c] = (int32_t)val; |
||||
else |
||||
CV_Assert(0); |
||||
} |
||||
} |
||||
|
||||
for (int j = 0; j < drows; j++) |
||||
{ |
||||
softdouble src_row_flt = scale_y*(softdouble(j) + softdouble(0.5)) - softdouble(0.5); |
||||
int src_row = cvFloor(src_row_flt); |
||||
int64_t ycoeff1 = cvRound64((src_row_flt - softdouble(src_row))*softdouble(fixedOne)); |
||||
int64_t ycoeff0 = fixedOne - ycoeff1; |
||||
|
||||
for (int i = 0; i < dcols; i++) |
||||
{ |
||||
softdouble src_col_flt = scale_x*(softdouble(i) + softdouble(0.5)) - softdouble(0.5); |
||||
int src_col = cvFloor(src_col_flt); |
||||
int64_t xcoeff1 = cvRound64((src_col_flt - softdouble(src_col))*softdouble(fixedOne)); |
||||
int64_t xcoeff0 = fixedOne - xcoeff1; |
||||
|
||||
uint8_t* dst_pt = refdst.ptr(j, i); |
||||
uint8_t* src_pt00 = src.ptr( src_row < 0 ? 0 : src_row >= rows ? rows - 1 : src_row , |
||||
src_col < 0 ? 0 : src_col >= cols ? cols - 1 : src_col ); |
||||
uint8_t* src_pt01 = src.ptr( src_row < 0 ? 0 : src_row >= rows ? rows - 1 : src_row , |
||||
(src_col + 1) < 0 ? 0 : (src_col + 1) >= cols ? cols - 1 : (src_col + 1)); |
||||
uint8_t* src_pt10 = src.ptr((src_row + 1) < 0 ? 0 : (src_row + 1) >= rows ? rows - 1 : (src_row + 1), |
||||
src_col < 0 ? 0 : src_col >= cols ? cols - 1 : src_col ); |
||||
uint8_t* src_pt11 = src.ptr((src_row + 1) < 0 ? 0 : (src_row + 1) >= rows ? rows - 1 : (src_row + 1), |
||||
(src_col + 1) < 0 ? 0 : (src_col + 1) >= cols ? cols - 1 : (src_col + 1)); |
||||
for (int c = 0; c < cn; c++) |
||||
{ |
||||
if (depth == CV_8U) |
||||
eval4< uint8_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt); |
||||
else if (depth == CV_16U) |
||||
eval4<uint16_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt); |
||||
else if (depth == CV_16S) |
||||
eval4< int16_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt); |
||||
else if (depth == CV_32S) |
||||
eval4< int32_t, fixedShiftU8>(xcoeff0, xcoeff1, ycoeff0, ycoeff1, c, src_pt00, src_pt01, src_pt10, src_pt11, dst_pt); |
||||
else |
||||
CV_Assert(0); |
||||
} |
||||
} |
||||
} |
||||
|
||||
cv::resize(src, dst, Size(dcols, drows), 0, 0, cv::INTER_LINEAR_EXACT); |
||||
EXPECT_GE(0, cvtest::norm(refdst, dst, cv::NORM_L1)) |
||||
<< "Resize from " << cols << "x" << rows << " to " << dcols << "x" << drows << " failed with max diff " << cvtest::norm(refdst, dst, cv::NORM_INF); |
||||
} |
||||
} |
||||
|
||||
///* End of file. */
|
Loading…
Reference in new issue