helpers.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: helpers.cpp
37 // Author: Pedram Azad
38 // Date: 2004
39 // ****************************************************************************
40 // Changes: 01.12.2008, Moritz Hassert
41 // * added time functions for VC cameras
42 // ****************************************************************************
43 
44 
45 // ****************************************************************************
46 // Includes
47 // ****************************************************************************
48 
49 #include <new> // for explicitly using correct new/delete operators on VC DSPs
50 
51 #include "helpers.h"
52 
53 #include <stdlib.h>
54 #include <math.h>
55 
56 #ifdef WIN32
57 #include <windows.h>
58 #ifndef _ATL_VER
59 #define _ATL_VER 0x0400 // set this to 0x0300 if getting an error with Visual Studio 6
60 #endif
61 #elif defined _TMS320C6X
62 #include <sysvar.h>
63 #include <vcrt.h>
64 #else
65 #include <sys/time.h>
66 #include <unistd.h>
67 #endif
68 
69 const char *pVersion = "1.3.22";
70 
71 
72 
74 {
75  const unsigned int nResult = invert_byte_order_int(*((unsigned int *) &x));
76  return *((float *) &nResult);
77 }
78 
79 unsigned long invert_byte_order_long(unsigned long x)
80 {
81  unsigned long result = 0;
82  result |= (x & 0x000000ff) << 24;
83  result |= (x & 0x0000ff00) << 8;
84  result |= (x & 0x00ff0000) >> 8;
85  result |= (x & 0xff000000) >> 24;
86  return result;
87 }
88 
89 unsigned int invert_byte_order_int(unsigned int x)
90 {
91  unsigned int result = 0;
92  result |= (x & 0x000000ff) << 24;
93  result |= (x & 0x0000ff00) << 8;
94  result |= (x & 0x00ff0000) >> 8;
95  result |= (x & 0xff000000) >> 24;
96  return result;
97 }
98 
99 unsigned short invert_byte_order_short(unsigned short x)
100 {
101  unsigned short result = 0;
102  result |= (x & 0x00ff) << 8;
103  result |= (x & 0xff00) >> 8;
104  return result;
105 }
106 
108 {
109  long result = 0;
110  result |= (x & 0x000000ff) << 24;
111  result |= (x & 0x0000ff00) << 8;
112  result |= (x & 0x00ff0000) >> 8;
113  result |= (x & 0xff000000) >> 24;
114  return result;
115 }
116 
118 {
119  int result = 0;
120  result |= (x & 0x000000ff) << 24;
121  result |= (x & 0x0000ff00) << 8;
122  result |= (x & 0x00ff0000) >> 8;
123  result |= (x & 0xff000000) >> 24;
124  return result;
125 }
126 
128 {
129  short result = 0;
130  result |= (x & 0x00ff) << 8;
131  result |= (x & 0xff00) >> 8;
132  return result;
133 }
134 
135 void get_timer_value(unsigned int &sec, unsigned int &usec)
136 {
137  #ifdef WIN32
138  // set the define _ATL_VER to 0x0300 if getting an error with Visual Studio 6
139  #if _ATL_VER > 0x0300
140  DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0); // necessary on some Multicore systems
141  #endif
142 
143  static bool bInit = true;
144  static LARGE_INTEGER startTime;
145  static LARGE_INTEGER freq;
146 
147  if (bInit)
148  {
149  QueryPerformanceFrequency(&freq);
150  QueryPerformanceCounter(&startTime);
151  bInit = false;
152  }
153 
154  LARGE_INTEGER tempTime;
155  QueryPerformanceCounter(&tempTime);
156  tempTime.QuadPart = tempTime.QuadPart - startTime.QuadPart;
157  sec = (unsigned int) (tempTime.QuadPart / freq.QuadPart);
158  __int64 remainder = (__int64) tempTime.QuadPart % (__int64) freq.QuadPart;
159  usec = (unsigned int) (remainder * (1000000.0 / freq.QuadPart));
160 
161  #if _ATL_VER > 0x0300
162  SetThreadAffinityMask(GetCurrentThread(), oldmask); // necessary on some Multicore systems
163  #endif
164 
165  #elif defined(_TMS320C6X)
166  sec = (unsigned int) getlvar(SEC);
167  usec = ((unsigned int) getvar(MSEC))*1000;
168  #else
169  timeval t;
170 
171  // get time and fill timeval structure
172  gettimeofday(&t, 0);
173 
174  // if first time save tv_sec
175  sec = t.tv_sec;
176  usec = t.tv_usec;
177  #endif
178 }
179 
180 unsigned int get_timer_value(bool bResetTimer)
181 {
182  static int nStaticSec = 0;
183  static int nStaticUSec = 0;
184 
185  #ifdef WIN32
186  #if _ATL_VER > 0x0300
187  DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0); // necessary on some Multicore systems
188  #endif
189 
190  static bool bInit = true;
191  static LARGE_INTEGER startTime;
192  static LARGE_INTEGER freq;
193 
194  if (bInit)
195  {
196  QueryPerformanceFrequency(&freq);
197  QueryPerformanceCounter(&startTime);
198  bInit = false;
199  }
200 
201  LARGE_INTEGER tempTime;
202  QueryPerformanceCounter(&tempTime);
203  tempTime.QuadPart = tempTime.QuadPart - startTime.QuadPart;
204  unsigned int sec = (unsigned int) (tempTime.QuadPart / freq.QuadPart);
205  __int64 remainder = (__int64) tempTime.QuadPart % (__int64) freq.QuadPart;
206  unsigned int usec = (unsigned int) (remainder * (1000000.0 / freq.QuadPart));
207 
208  // if first time save tv_sec
209  if (bResetTimer)
210  {
211  nStaticSec = sec;
212  nStaticUSec = usec;
213  }
214 
215  #if _ATL_VER > 0x0300
216  SetThreadAffinityMask(GetCurrentThread(), oldmask); // necessary on some Multicore systems
217  #endif
218 
219  return (sec - nStaticSec) * 1000000 + ((int) usec - nStaticUSec);
220 
221  #elif defined(_TMS320C6X)
222  int tv_sec = getlvar(SEC);
223  int tv_usec = getvar(MSEC)*1000;
224 
225  // if first time save tv_sec
226  if (bResetTimer)
227  {
228  nStaticSec = tv_sec;
229  nStaticUSec = tv_usec;
230  }
231 
232  return (tv_sec - nStaticSec) * 1000000 + ((int) tv_usec - nStaticUSec);
233 
234  #else
235  timeval t;
236 
237  // get time and fill timeval structure
238  gettimeofday(&t, 0);
239 
240  // if first time save tv_sec
241  if (bResetTimer)
242  {
243  nStaticSec = t.tv_sec;
244  nStaticUSec = t.tv_usec;
245  }
246 
247  return (t.tv_sec - nStaticSec) * 1000000 + ((int) t.tv_usec - nStaticUSec);
248  #endif
249 }
250 
252 {
253  return rand() / double(RAND_MAX);
254 }
255 
257 {
258  return rand() / float(RAND_MAX);
259 }
260 
261 
262 // This Gaussian routine is taken from Numerical Recipes and is their copyright
264 {
265  static int next_gaussian = 0;
266  static double saved_gaussian_value;
267 
268  double fac, rsq, v1, v2;
269 
270  if (next_gaussian == 0)
271  {
272  do
273  {
274  v1 = 2.0 * uniform_random() - 1.0;
275  v2 = 2.0 * uniform_random() - 1.0;
276  rsq = v1 * v1 + v2 * v2;
277  }
278  while (rsq >= 1.0 || rsq == 0.0);
279 
280  fac = sqrt(-2.0 * log(rsq) / rsq);
281  saved_gaussian_value = v1 * fac;
282  next_gaussian = 1;
283 
284  return v2 * fac;
285  }
286  else
287  {
288  next_gaussian = 0;
289  return saved_gaussian_value;
290  }
291 }
292 
293 // This Gaussian routine is taken from Numerical Recipes and is their copyright
295 {
296  static int next_gaussian = 0;
297  static float saved_gaussian_value;
298 
299  float fac, rsq, v1, v2;
300 
301  if (next_gaussian == 0)
302  {
303  do
304  {
305  v1 = 2.0f * uniform_random_float() - 1.0f;
306  v2 = 2.0f * uniform_random_float() - 1.0f;
307  rsq = v1 * v1 + v2 * v2;
308  }
309  while (rsq >= 1.0f || rsq == 0.0f);
310 
311  fac = sqrtf(-2.0f * logf(rsq) / rsq);
312  saved_gaussian_value = v1 * fac;
313  next_gaussian = 1;
314 
315  return v2 * fac;
316  }
317  else
318  {
319  next_gaussian = 0;
320  return saved_gaussian_value;
321  }
322 }
323 
324 
325 int my_round(double x)
326 {
327  return (x > 0) ? int(x + 0.5) : int(x - 0.5);
328 }
329 
330 int my_round(float x)
331 {
332  return (x > 0) ? int(x + 0.5f) : int(x - 0.5f);
333 }
334 
335 
336 void sleep_ms(unsigned int ms)
337 {
338  #ifdef WIN32
339  Sleep(ms);
340  #elif defined(_TMS320C6X)
341  time_delay(ms);
342  #else
343  usleep((useconds_t) (1000 * ms));
344  #endif
345 }
346 
347 
348 void hsv2rgb(int h, int s, int v, int &r, int &g, int &b)
349 {
350  float R, G, B;
351  float S = s / 255.0f, V = v / 255.0f;
352 
353  if (h < 120)
354  {
355  R = (120 - h) / 60.0f;
356  G = h / 60.0f;
357  B = 0;
358  }
359  else if (h < 240)
360  {
361  R = 0;
362  G = (240 - h) / 60.0f;
363  B = (h - 120) / 60.0f;
364  }
365  else
366  {
367  R = (h - 240) / 60.0f;
368  G = 0.0f;
369  B = (360 - h) / 60.0f;
370  }
371 
372  if (R > 1) R = 1.0f;
373  if (G > 1) G = 1.0f;
374  if (B > 1) B = 1.0f;
375 
376  r = int(255.0f * (1.0f + S * (R - 1.0f)) * V + 0.5f);
377  g = int(255.0f * (1.0f + S * (G - 1.0f)) * V + 0.5f);
378  b = int(255.0f * (1.0f + S * (B - 1.0f)) * V + 0.5f);
379 }
380 
381 void rgb2hsv(int r, int g, int b, int &h, int &s, int &v)
382 {
383  v = MY_MAX(MY_MAX(r, g), b);
384  const int min = MY_MIN(MY_MIN(r, g), b);
385  const int delta = v - min;
386 
387  if (delta == 0)
388  {
389  h = 0;
390  s = 0;
391  }
392  else
393  {
394  s = 255 * delta / v;
395 
396  if (r == v)
397  h = 60 * (g - b) / delta;
398  else if (g == v)
399  h = 120 + (60 * (b - r) / delta);
400  else
401  h = 240 + (60 * (r - g) / delta);
402  }
403 
404  while (h < 0) h += 360;
405  while (h > 360) h -= 360;
406 }
407 
408 
409 void *aligned_malloc(unsigned int size, unsigned int align_size)
410 {
411  const int align_mask = align_size - 1;
412 
413  char *ptr = (char *) malloc(size + align_size + sizeof(int));
414  if (ptr == 0)
415  return 0;
416 
417  char *ptr2 = ptr + sizeof(int);
418  char *aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask));
419 
420  ptr2 = aligned_ptr - sizeof(int);
421  *((int *) ptr2) = (int) (aligned_ptr - ptr);
422 
423  return aligned_ptr;
424 }
425 
426 void aligned_free(void *ptr)
427 {
428  int *ptr2 = (int *) ptr - 1;
429  char *ptr3 = (char *) ptr;
430  ptr3 -= *ptr2;
431  free(ptr3);
432 }
433 
434 
435 const char *GetVersionIVT()
436 {
437  return pVersion;
438 }
GLubyte g
Definition: glext.h:5166
const char * pVersion
Definition: helpers.cpp:69
GLfloat GLfloat GLfloat v2
Definition: glext.h:3532
GLdouble GLdouble t
Definition: glext.h:3219
float gaussian_random_float()
Definition: helpers.cpp:294
double uniform_random()
Definition: helpers.cpp:251
float invert_byte_order_float(float x)
Definition: helpers.cpp:73
unsigned long invert_byte_order_long(unsigned long x)
Definition: helpers.cpp:79
float uniform_random_float()
Definition: helpers.cpp:256
void hsv2rgb(int h, int s, int v, int &r, int &g, int &b)
Definition: helpers.cpp:348
const char * GetVersionIVT()
Definition: helpers.cpp:435
GLdouble s
Definition: glext.h:3211
void rgb2hsv(int r, int g, int b, int &h, int &s, int &v)
Definition: helpers.cpp:381
#define MY_MAX(a, b)
Definition: helpers.h:63
unsigned int invert_byte_order_int(unsigned int x)
Definition: helpers.cpp:89
#define S
GLenum GLint x
Definition: glext.h:3125
void aligned_free(void *ptr)
Definition: helpers.cpp:426
void * aligned_malloc(unsigned int size, unsigned int align_size)
Definition: helpers.cpp:409
double gaussian_random()
Definition: helpers.cpp:263
void get_timer_value(unsigned int &sec, unsigned int &usec)
Definition: helpers.cpp:135
int my_round(double x)
Definition: helpers.cpp:325
GLubyte GLubyte b
Definition: glext.h:5166
void sleep_ms(unsigned int ms)
Definition: helpers.cpp:336
#define MY_MIN(a, b)
Definition: helpers.h:64
GLdouble GLdouble GLdouble r
Definition: glext.h:3227
GLsizeiptr size
Definition: glext.h:3388
const GLdouble * v
Definition: glext.h:3212
GLfloat GLfloat v1
Definition: glext.h:3531
unsigned short invert_byte_order_short(unsigned short x)
Definition: helpers.cpp:99


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28