helpers.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  helpers.cpp
00037 // Author:    Pedram Azad
00038 // Date:      2004
00039 // ****************************************************************************
00040 // Changes:   01.12.2008, Moritz Hassert
00041 //            * added time functions for VC cameras
00042 // ****************************************************************************
00043 
00044 
00045 // ****************************************************************************
00046 // Includes
00047 // ****************************************************************************
00048 
00049 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00050 
00051 #include "helpers.h"
00052 
00053 #include <stdlib.h>
00054 #include <math.h>
00055 
00056 #ifdef WIN32
00057 #include <windows.h>
00058 #ifndef _ATL_VER
00059 #define _ATL_VER 0x0400 // set this to 0x0300 if getting an error with Visual Studio 6
00060 #endif
00061 #elif defined _TMS320C6X
00062 #include <sysvar.h>
00063 #include <vcrt.h>
00064 #else
00065 #include <sys/time.h>
00066 #include <unistd.h>
00067 #endif
00068 
00069 const char *pVersion = "1.3.22";
00070 
00071 
00072 
00073 float invert_byte_order_float(float x)
00074 {
00075         const unsigned int nResult = invert_byte_order_int(*((unsigned int *) &x));
00076         return *((float *) &nResult);
00077 }
00078 
00079 unsigned long invert_byte_order_long(unsigned long x)
00080 {
00081         unsigned long result = 0;
00082         result |= (x & 0x000000ff) << 24;
00083         result |= (x & 0x0000ff00) << 8;
00084         result |= (x & 0x00ff0000) >> 8;
00085         result |= (x & 0xff000000) >> 24;
00086         return result;
00087 }
00088 
00089 unsigned int invert_byte_order_int(unsigned int x)
00090 {
00091         unsigned int result = 0;
00092         result |= (x & 0x000000ff) << 24;
00093         result |= (x & 0x0000ff00) << 8;
00094         result |= (x & 0x00ff0000) >> 8;
00095         result |= (x & 0xff000000) >> 24;
00096         return result;
00097 }
00098 
00099 unsigned short invert_byte_order_short(unsigned short x)
00100 {
00101         unsigned short result = 0;
00102         result |= (x & 0x00ff) << 8;
00103         result |= (x & 0xff00) >> 8;
00104         return result;
00105 }
00106 
00107 long invert_byte_order_long(long x)
00108 {
00109         long result = 0;
00110         result |= (x & 0x000000ff) << 24;
00111         result |= (x & 0x0000ff00) << 8;
00112         result |= (x & 0x00ff0000) >> 8;
00113         result |= (x & 0xff000000) >> 24;
00114         return result;
00115 }
00116 
00117 int invert_byte_order_int(int x)
00118 {
00119         int result = 0;
00120         result |= (x & 0x000000ff) << 24;
00121         result |= (x & 0x0000ff00) << 8;
00122         result |= (x & 0x00ff0000) >> 8;
00123         result |= (x & 0xff000000) >> 24;
00124         return result;
00125 }
00126 
00127 short invert_byte_order_short(short x)
00128 {
00129         short result = 0;
00130         result |= (x & 0x00ff) << 8;
00131         result |= (x & 0xff00) >> 8;
00132         return result;
00133 }
00134 
00135 void get_timer_value(unsigned int &sec, unsigned int &usec)
00136 {
00137         #ifdef WIN32
00138                 // set the define _ATL_VER to 0x0300 if getting an error with Visual Studio 6
00139                 #if _ATL_VER > 0x0300
00140                 DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0); // necessary on some Multicore systems
00141                 #endif
00142 
00143                 static bool bInit = true;
00144                 static LARGE_INTEGER startTime;
00145                 static LARGE_INTEGER freq;
00146 
00147                 if (bInit)
00148                 {
00149                         QueryPerformanceFrequency(&freq);
00150                         QueryPerformanceCounter(&startTime);
00151                         bInit = false;
00152                 }
00153 
00154                 LARGE_INTEGER tempTime;
00155                 QueryPerformanceCounter(&tempTime);
00156                 tempTime.QuadPart = tempTime.QuadPart - startTime.QuadPart;
00157                 sec = (unsigned int) (tempTime.QuadPart / freq.QuadPart);
00158                 __int64 remainder = (__int64) tempTime.QuadPart % (__int64) freq.QuadPart;
00159                 usec = (unsigned int) (remainder * (1000000.0 / freq.QuadPart));
00160 
00161                 #if _ATL_VER > 0x0300
00162                 SetThreadAffinityMask(GetCurrentThread(), oldmask); // necessary on some Multicore systems
00163                 #endif
00164 
00165         #elif defined(_TMS320C6X)
00166                 sec  =  (unsigned int) getlvar(SEC);
00167                 usec = ((unsigned int) getvar(MSEC))*1000;
00168         #else
00169                 timeval t;
00170                 
00171                 // get time and fill timeval structure
00172                 gettimeofday(&t, 0);
00173                 
00174                 // if first time save tv_sec
00175                 sec = t.tv_sec;
00176                 usec = t.tv_usec;
00177         #endif
00178 }
00179 
00180 unsigned int get_timer_value(bool bResetTimer)
00181 {
00182         static int nStaticSec = 0;
00183         static int nStaticUSec = 0;
00184 
00185         #ifdef WIN32
00186                 #if _ATL_VER > 0x0300
00187                 DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0); // necessary on some Multicore systems
00188                 #endif
00189                 
00190                 static bool bInit = true;
00191                 static LARGE_INTEGER startTime;
00192                 static LARGE_INTEGER freq;
00193 
00194                 if (bInit)
00195                 {
00196                         QueryPerformanceFrequency(&freq);
00197                         QueryPerformanceCounter(&startTime);
00198                         bInit = false;
00199                 }
00200 
00201                 LARGE_INTEGER tempTime;
00202                 QueryPerformanceCounter(&tempTime);
00203                 tempTime.QuadPart = tempTime.QuadPart - startTime.QuadPart;
00204                 unsigned int sec = (unsigned int) (tempTime.QuadPart / freq.QuadPart);
00205                 __int64 remainder = (__int64) tempTime.QuadPart % (__int64) freq.QuadPart;
00206                 unsigned int usec = (unsigned int) (remainder * (1000000.0 / freq.QuadPart));
00207                 
00208                 // if first time save tv_sec
00209                 if (bResetTimer)
00210                 {
00211                         nStaticSec = sec;
00212                         nStaticUSec = usec;
00213                 }
00214 
00215                 #if _ATL_VER > 0x0300
00216                 SetThreadAffinityMask(GetCurrentThread(), oldmask); // necessary on some Multicore systems
00217                 #endif
00218 
00219                 return (sec - nStaticSec) * 1000000 + ((int) usec - nStaticUSec);
00220 
00221         #elif defined(_TMS320C6X)
00222                 int tv_sec  =  getlvar(SEC);
00223                 int tv_usec =  getvar(MSEC)*1000;
00224 
00225                 // if first time save tv_sec
00226                 if (bResetTimer)
00227                 {
00228                         nStaticSec  = tv_sec;
00229                         nStaticUSec = tv_usec;
00230                 }
00231                 
00232                 return (tv_sec - nStaticSec) * 1000000 + ((int) tv_usec - nStaticUSec);
00233 
00234         #else
00235                 timeval t;
00236                 
00237                 // get time and fill timeval structure
00238                 gettimeofday(&t, 0);
00239                 
00240                 // if first time save tv_sec
00241                 if (bResetTimer)
00242                 {
00243                         nStaticSec = t.tv_sec;
00244                         nStaticUSec = t.tv_usec;
00245                 }
00246                 
00247                 return (t.tv_sec - nStaticSec) * 1000000 + ((int) t.tv_usec - nStaticUSec);
00248         #endif
00249 }
00250 
00251 double uniform_random()
00252 {
00253         return rand() / double(RAND_MAX);
00254 }
00255 
00256 float uniform_random_float()
00257 {
00258         return rand() / float(RAND_MAX);
00259 }
00260 
00261 
00262 // This Gaussian routine is taken from Numerical Recipes and is their copyright
00263 double gaussian_random()
00264 {
00265         static int next_gaussian = 0;
00266         static double saved_gaussian_value;
00267 
00268         double fac, rsq, v1, v2;
00269 
00270         if (next_gaussian == 0)
00271         {
00272                 do
00273                 {
00274                         v1 = 2.0 * uniform_random() - 1.0;
00275                         v2 = 2.0 * uniform_random() - 1.0;
00276                         rsq = v1 * v1 + v2 * v2;
00277                 }
00278                 while (rsq >= 1.0 || rsq == 0.0);
00279     
00280                 fac = sqrt(-2.0 * log(rsq) / rsq);
00281                 saved_gaussian_value = v1 * fac;
00282                 next_gaussian = 1;
00283     
00284                 return v2 * fac;
00285         }
00286         else
00287         {
00288                 next_gaussian = 0;
00289                 return saved_gaussian_value;
00290         }
00291 }
00292 
00293 // This Gaussian routine is taken from Numerical Recipes and is their copyright
00294 float gaussian_random_float()
00295 {
00296         static int next_gaussian = 0;
00297         static float saved_gaussian_value;
00298 
00299         float fac, rsq, v1, v2;
00300 
00301         if (next_gaussian == 0)
00302         {
00303                 do
00304                 {
00305                         v1 = 2.0f * uniform_random_float() - 1.0f;
00306                         v2 = 2.0f * uniform_random_float() - 1.0f;
00307                         rsq = v1 * v1 + v2 * v2;
00308                 }
00309                 while (rsq >= 1.0f || rsq == 0.0f);
00310     
00311                 fac = sqrtf(-2.0f * logf(rsq) / rsq);
00312                 saved_gaussian_value = v1 * fac;
00313                 next_gaussian = 1;
00314     
00315                 return v2 * fac;
00316         }
00317         else
00318         {
00319                 next_gaussian = 0;
00320                 return saved_gaussian_value;
00321         }
00322 }
00323 
00324 
00325 int my_round(double x)
00326 {
00327         return (x > 0) ? int(x + 0.5) : int(x - 0.5);
00328 }
00329 
00330 int my_round(float x)
00331 {
00332         return (x > 0) ? int(x + 0.5f) : int(x - 0.5f);
00333 }
00334 
00335 
00336 void sleep_ms(unsigned int ms)
00337 {
00338         #ifdef WIN32
00339         Sleep(ms);
00340         #elif defined(_TMS320C6X)
00341         time_delay(ms);
00342         #else
00343         usleep((useconds_t) (1000 * ms));
00344         #endif
00345 }
00346 
00347 
00348 void hsv2rgb(int h, int s, int v, int &r, int &g, int &b)
00349 {
00350         float R, G, B;
00351         float S = s / 255.0f, V = v / 255.0f;
00352         
00353         if (h < 120)
00354         {
00355                 R = (120 - h) / 60.0f;
00356                 G = h / 60.0f;
00357                 B = 0;
00358         }
00359         else if (h < 240)
00360         {
00361                 R = 0;
00362                 G = (240 - h) / 60.0f;
00363                 B = (h - 120) / 60.0f;
00364         }
00365         else
00366         {
00367                 R = (h - 240) / 60.0f;
00368                 G = 0.0f;
00369                 B = (360 - h) / 60.0f;
00370         }
00371         
00372         if (R > 1) R = 1.0f;
00373         if (G > 1) G = 1.0f;
00374         if (B > 1) B = 1.0f;
00375         
00376         r = int(255.0f * (1.0f + S * (R - 1.0f)) * V + 0.5f);
00377         g = int(255.0f * (1.0f + S * (G - 1.0f)) * V + 0.5f);
00378         b = int(255.0f * (1.0f + S * (B - 1.0f)) * V + 0.5f);
00379 }
00380 
00381 void rgb2hsv(int r, int g, int b, int &h, int &s, int &v)
00382 {
00383         v = MY_MAX(MY_MAX(r, g), b);
00384         const int min = MY_MIN(MY_MIN(r, g), b);
00385         const int delta = v - min;
00386 
00387         if (delta == 0)
00388         {
00389                 h = 0;
00390                 s = 0;
00391         }
00392         else
00393         {
00394                 s = 255 * delta / v;
00395 
00396                 if (r == v)
00397                         h = 60 * (g - b) / delta;
00398                 else if (g == v)
00399                         h = 120 + (60 * (b - r) / delta);
00400                 else
00401                         h = 240 + (60 * (r - g) / delta);
00402         }
00403 
00404         while (h < 0) h += 360;
00405         while (h > 360) h -= 360;
00406 }
00407 
00408 
00409 void *aligned_malloc(unsigned int size, unsigned int align_size)
00410 {
00411         const int align_mask = align_size - 1;
00412 
00413         char *ptr = (char *) malloc(size + align_size + sizeof(int));
00414         if (ptr == 0)
00415                 return 0;
00416 
00417         char *ptr2 = ptr + sizeof(int);
00418         char *aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask));
00419 
00420         ptr2 = aligned_ptr - sizeof(int);
00421         *((int *) ptr2) = (int) (aligned_ptr - ptr);
00422 
00423         return aligned_ptr;
00424 }
00425 
00426 void aligned_free(void *ptr)
00427 {
00428         int *ptr2 = (int *) ptr - 1;
00429         char *ptr3 = (char *) ptr;
00430         ptr3  -= *ptr2;
00431         free(ptr3);
00432 }
00433 
00434 
00435 const char *GetVersionIVT()
00436 {
00437         return pVersion;
00438 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57