Slicer  5.1
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
misc.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Extract Skeleton
4  Module: $HeadURL$
5  Language: C++
6  Date: $Date$
7  Version: $Revision$
8 
9  Copyright (c) Brigham and Women's Hospital (BWH) All Rights Reserved.
10 
11  See License.txt or http://www.slicer.org/copyright/copyright.txt for details.
12 
13 ==========================================================================*/
14 /*
15  * misc.h
16  *
17  * author: msturm
18  * created: 27 Mar 1997
19  * changes: mastyner
20  */
21 
22 #ifndef __IP_MISC_H__
23 #define __IP_MISC_H__
24 
25 #include <cstdlib>
26 #include <cstdio>
27 #include <cerrno>
28 #include <sys/types.h>
29 
30 typedef enum
31  {
32  IP_BYTE = 0, /* AVS_TYPE_BYTE = 0 */
33  IP_INT, /* AVS_TYPE_INTEGER = 1 */
34  IP_FLOAT, /* AVS_TYPE_REAL = 2 */
35  IP_DOUBLE, /* AVS_TYPE_DOUBLE = 3 */
36  IP_SHORT /* AVS_TYPE_SHORT = 4 */
37  } ipDataType;
38 
39 typedef union
40  {
41  void *_void;
42  unsigned char *_byte;
43  short *_short;
44  int *_int;
45  float *_float;
46  double *_double;
47  } ipDataUnion;
48 
49 // memory allocation & handling
50 size_t ipGetDataSize(const ipDataType type);
51 
52 void * ipAllocateData(const int size, const size_t elemsize);
53 
54 // misc functions
55 
56 template <class T>
57 inline void ipSwap(T *a, T *b)
58 {
59  T temp = *a; *a = *b; *b = temp;
60 
61 }
62 
63 template <class T>
64 inline T sqr(T x)
65 {
66  return x * x;
67 }
68 
69 // thresholding operators
70 template <class T>
71 inline void ipUpperThreshold(T *data, const int size, const T threshold)
72 {
73  T *dp = data;
74 
75  for( int i = 0; i < size; i++, dp++ )
76  {
77  if( *dp < threshold )
78  {
79  *dp = (T) 0.0;
80  }
81  }
82 }
83 
84 template <class T>
85 inline void ipLowerThreshold(T *data, const int size, const T threshold)
86 {
87  T *dp = data;
88 
89  for( int i = 0; i < size; i++, dp++ )
90  {
91  if( *dp > threshold )
92  {
93  *dp = (T) 0.0;
94  }
95  }
96 }
97 
98 template <class T>
99 inline void ipUpperBinaryThreshold(T *data, const int size, const T threshold)
100 {
101  T *dp = data;
102 
103  for( int i = 0; i < size; i++, dp++ )
104  {
105  *dp = (*dp < threshold ? (T) 0.0 : (T) 1.0);
106  }
107 }
108 
109 template <class T>
110 inline void ipLowerBinaryThreshold(T *data, const int size, const T threshold)
111 {
112  T *dp = data;
113 
114  for( int i = 0; i < size; i++, dp++ )
115  {
116  *dp = (*dp > threshold ? (T) 0.0 : (T) 1.0);
117  }
118 }
119 
120 #endif
void ipUpperBinaryThreshold(T *data, const int size, const T threshold)
Definition: misc.h:99
short * _short
Definition: misc.h:43
void * ipAllocateData(const int size, const size_t elemsize)
void ipLowerThreshold(T *data, const int size, const T threshold)
Definition: misc.h:85
Definition: misc.h:33
unsigned char * _byte
Definition: misc.h:42
double * _double
Definition: misc.h:46
void ipUpperThreshold(T *data, const int size, const T threshold)
Definition: misc.h:71
void ipLowerBinaryThreshold(T *data, const int size, const T threshold)
Definition: misc.h:110
void * _void
Definition: misc.h:41
size_t ipGetDataSize(const ipDataType type)
Definition: misc.h:34
int * _int
Definition: misc.h:44
Definition: misc.h:32
float * _float
Definition: misc.h:45
ipDataType
Definition: misc.h:30
Definition: misc.h:35
Definition: misc.h:36
void ipSwap(T *a, T *b)
Definition: misc.h:57
T sqr(T x)
Definition: misc.h:64