kyheader.hpp 6.83 KB
Newer Older
jaco's avatar
jaco committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*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.
 //
 //
 //                           License Agreement
 //                For Open Source Computer Vision Library
 //
 // Copyright (C) 2014, OpenCV Foundation, all rights reserved.
 // Third party copyrights are property of their respective owners.
 //
 // 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 the copyright holders 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 KYHEADER_H
#define KYHEADER_H

#include <assert.h>
#include <string>
#include <vector>
#include <functional>
#include <list>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <time.h>
#include <fstream>
#include <stdint.h>

// TODO: reference additional headers your program requires here
jaco's avatar
jaco committed
58

jaco's avatar
jaco committed
59 60 61 62 63 64 65 66 67
#include "opencv2/core.hpp"

#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#ifdef _DEBUG
#define cvLIB(name) "opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) "opencv_" name CV_VERSION_ID
#endif

68
#ifdef _WIN32
jaco's avatar
jaco committed
69 70 71 72 73 74 75 76 77 78 79 80
/* windows stuff */
#else
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef unsigned int UNINT32;
typedef bool BOOL;
typedef void *HANDLE;
#endif

#ifndef _MSC_VER
typedef unsigned char BYTE;
#else
Alexander Alekhin's avatar
Alexander Alekhin committed
81
#define WIN32_LEAN_AND_MEAN
jaco's avatar
jaco committed
82
#include <windows.h>
Alexander Alekhin's avatar
Alexander Alekhin committed
83 84
#undef min
#undef max
jaco's avatar
jaco committed
85 86 87 88 89 90 91 92 93 94
#endif

typedef std::vector<int> vecI;
typedef const std::string CStr;
typedef const cv::Mat CMat;
typedef std::vector<std::string> vecS;
typedef std::vector<cv::Mat> vecM;
typedef std::vector<float> vecF;
typedef std::vector<double> vecD;

jaco's avatar
jaco committed
95 96 97 98 99
namespace cv
{
namespace saliency
{

jaco's avatar
jaco committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
enum
{
  CV_FLIP_BOTH = -1,
  CV_FLIP_VERTICAL = 0,
  CV_FLIP_HORIZONTAL = 1
};
#define CHK_IND(p) ((p).x >= 0 && (p).x < _w && (p).y >= 0 && (p).y < _h)
#define CV_Assert_(expr, args) \
{\
    if(!(expr)) {\
    String msg = cv::format args; \
    printf("%s in %s:%d\n", msg.c_str(), __FILE__, __LINE__); \
    cv::error(cv::Exception(CV_StsAssert, msg, __FUNCTION__, __FILE__, __LINE__) ); }\
}


// Return -1 if not in the list
template<typename T>
static inline int findFromList( const T &word, const std::vector<T> &strList )
{

  std::vector<cv::String>::iterator it = std::find( strList.begin(), strList.end(), word );
  if( it == strList.end() )
  {
    return -1;
  }
  else
  {
    int index = it - strList.begin();
    return index;
  }
}

template<typename T> inline T sqr( T x )
{
  return x * x;
}  // out of range risk for T = byte, ...
template<class T, int D> inline T vecSqrDist( const cv::Vec<T, D> &v1, const cv::Vec<T, D> &v2 )
{
  T s = 0;
  for ( int i = 0; i < D; i++ )
    s += sqr( v1[i] - v2[i] );
  return s;
}  // out of range risk for T = byte, ...
template<class T, int D> inline T vecDist( const cv::Vec<T, D> &v1, const cv::Vec<T, D> &v2 )
{
  return sqrt( vecSqrDist( v1, v2 ) );
}  // out of range risk for T = byte, ...

inline cv::Rect Vec4i2Rect( cv::Vec4i &v )
{
  return cv::Rect( cv::Point( v[0] - 1, v[1] - 1 ), cv::Point( v[2], v[3] ) );
}
jaco's avatar
jaco committed
153

jaco's avatar
jaco committed
154

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
155 156 157 158 159
#if defined(_MSC_VER)
# include <intrin.h>
# define POPCNT(x) __popcnt(x)
# define POPCNT64(x) (__popcnt((unsigned)(x)) + __popcnt((unsigned)((uint64_t)(x) >> 32)))
#endif
jaco's avatar
jaco committed
160 161 162 163 164 165

#if defined(__GNUC__)
# define POPCNT(x) __builtin_popcount(x)
# define POPCNT64(x) __builtin_popcountll(x)
#endif

166
inline int popcnt64( uint64_t u )
jaco's avatar
jaco committed
167 168 169 170 171 172 173 174 175 176
{
  u = ( u & 0x5555555555555555 ) + ( ( u >> 1 ) & 0x5555555555555555 );
  u = ( u & 0x3333333333333333 ) + ( ( u >> 2 ) & 0x3333333333333333 );
  u = ( u & 0x0f0f0f0f0f0f0f0f ) + ( ( u >> 4 ) & 0x0f0f0f0f0f0f0f0f );
  u = ( u & 0x00ff00ff00ff00ff ) + ( ( u >> 8 ) & 0x00ff00ff00ff00ff );
  u = ( u & 0x0000ffff0000ffff ) + ( ( u >> 16 ) & 0x0000ffff0000ffff );
  u = ( u & 0x00000000ffffffff ) + ( ( u >> 32 ) & 0x00000000ffffffff );
  return (int)u;
}

177
inline int popcnt( uint32_t u )
jaco's avatar
jaco committed
178 179 180 181 182 183 184 185 186
{
  u = ( u & 0x55555555 ) + ( ( u >> 1 ) & 0x55555555 );
  u = ( u & 0x33333333 ) + ( ( u >> 2 ) & 0x33333333 );
  u = ( u & 0x0f0f0f0f ) + ( ( u >> 4 ) & 0x0f0f0f0f );
  u = ( u & 0x00ff00ff ) + ( ( u >> 8 ) & 0x00ff00ff );
  u = ( u & 0x0000ffff ) + ( ( u >> 16 ) & 0x0000ffff );
  return (int)u;
}

187
inline int popcnt64_nibble( uint64_t u )
jaco's avatar
jaco committed
188 189 190 191 192 193 194 195 196 197 198 199 200
{
  static const uint8_t Table[] =
  { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };

  int c = 0;
  while ( u )
  {
    c += Table[u & 0xf];
    u >>= 4;
  }
  return (int)c;
}

201
inline int popcnt_nibble( uint32_t u )
jaco's avatar
jaco committed
202 203 204 205 206 207 208 209 210 211 212 213 214
{
  static const uint8_t Table[] =
  { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };

  int c = 0;
  while ( u )
  {
    c += Table[u & 0xf];
    u >>= 4;
  }
  return (int)c;
}

215
inline int popcnt64_byte( uint64_t u )
jaco's avatar
jaco committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
#define B2(k) k, k+1, k+1, k+2
#define B4(k) B2(k), B2(k+1), B2(k+1), B2(k+2)
#define B6(k) B4(k), B4(k+1), B4(k+1), B4(k+2)
  static const uint8_t Table[] =
  { B6( 0 ), B6( 1 ), B6( 1 ), B6( 2 ) };
#undef B6
#undef B4
#undef B2

  int c = 0;
  while ( u )
  {
    c += Table[u & 0xff];
    u >>= 8;
  }
  return (int)c;
}

235
inline int popcnt_byte( uint32_t u )
jaco's avatar
jaco committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
{
#define B2(k) k, k+1, k+1, k+2
#define B4(k) B2(k), B2(k+1), B2(k+1), B2(k+2)
#define B6(k) B4(k), B4(k+1), B4(k+1), B4(k+2)
  static const uint8_t Table[] =
  { B6( 0 ), B6( 1 ), B6( 1 ), B6( 2 ) };
#undef B6
#undef B4
#undef B2

  int c = 0;
  while ( u )
  {
    c += Table[u & 0xff];
    u >>= 8;
  }
  return (int)c;
}

}
}

#endif // KYHEADER_H