00001 #ifndef _OKAN_MISC_H_ 00002 #define _OKAN_MISC_H_ 00003 00004 #include "cv.h" 00005 00006 #define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type ) \ 00007 void func_name( T *array, size_t total, user_data_type aux ) \ 00008 { \ 00009 int isort_thresh = 7; \ 00010 T t; \ 00011 int sp = 0; \ 00012 \ 00013 struct \ 00014 { \ 00015 T *lb; \ 00016 T *ub; \ 00017 } \ 00018 stack[48]; \ 00019 \ 00020 aux = aux; \ 00021 \ 00022 if( total <= 1 ) \ 00023 return; \ 00024 \ 00025 stack[0].lb = array; \ 00026 stack[0].ub = array + (total - 1); \ 00027 \ 00028 while( sp >= 0 ) \ 00029 { \ 00030 T* left = stack[sp].lb; \ 00031 T* right = stack[sp--].ub; \ 00032 \ 00033 for(;;) \ 00034 { \ 00035 int i, n = (int)(right - left) + 1, m; \ 00036 T* ptr; \ 00037 T* ptr2; \ 00038 \ 00039 if( n <= isort_thresh ) \ 00040 { \ 00041 insert_sort: \ 00042 for( ptr = left + 1; ptr <= right; ptr++ ) \ 00043 { \ 00044 for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--) \ 00045 CV_SWAP( ptr2[0], ptr2[-1], t ); \ 00046 } \ 00047 break; \ 00048 } \ 00049 else \ 00050 { \ 00051 T* left0; \ 00052 T* left1; \ 00053 T* right0; \ 00054 T* right1; \ 00055 T* pivot; \ 00056 T* a; \ 00057 T* b; \ 00058 T* c; \ 00059 int swap_cnt = 0; \ 00060 \ 00061 left0 = left; \ 00062 right0 = right; \ 00063 pivot = left + (n/2); \ 00064 \ 00065 if( n > 40 ) \ 00066 { \ 00067 int d = n / 8; \ 00068 a = left, b = left + d, c = left + 2*d; \ 00069 left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \ 00070 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \ 00071 \ 00072 a = pivot - d, b = pivot, c = pivot + d; \ 00073 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \ 00074 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \ 00075 \ 00076 a = right - 2*d, b = right - d, c = right; \ 00077 right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \ 00078 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \ 00079 } \ 00080 \ 00081 a = left, b = pivot, c = right; \ 00082 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \ 00083 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \ 00084 if( pivot != left0 ) \ 00085 { \ 00086 CV_SWAP( *pivot, *left0, t ); \ 00087 pivot = left0; \ 00088 } \ 00089 left = left1 = left0 + 1; \ 00090 right = right1 = right0; \ 00091 \ 00092 for(;;) \ 00093 { \ 00094 while( left <= right && !LT(*pivot, *left) ) \ 00095 { \ 00096 if( !LT(*left, *pivot) ) \ 00097 { \ 00098 if( left > left1 ) \ 00099 CV_SWAP( *left1, *left, t ); \ 00100 swap_cnt = 1; \ 00101 left1++; \ 00102 } \ 00103 left++; \ 00104 } \ 00105 \ 00106 while( left <= right && !LT(*right, *pivot) ) \ 00107 { \ 00108 if( !LT(*pivot, *right) ) \ 00109 { \ 00110 if( right < right1 ) \ 00111 CV_SWAP( *right1, *right, t ); \ 00112 swap_cnt = 1; \ 00113 right1--; \ 00114 } \ 00115 right--; \ 00116 } \ 00117 \ 00118 if( left > right ) \ 00119 break; \ 00120 CV_SWAP( *left, *right, t ); \ 00121 swap_cnt = 1; \ 00122 left++; \ 00123 right--; \ 00124 } \ 00125 \ 00126 if( swap_cnt == 0 ) \ 00127 { \ 00128 left = left0, right = right0; \ 00129 goto insert_sort; \ 00130 } \ 00131 \ 00132 n = MIN( (int)(left1 - left0), (int)(left - left1) ); \ 00133 for( i = 0; i < n; i++ ) \ 00134 CV_SWAP( left0[i], left[i-n], t ); \ 00135 \ 00136 n = MIN( (int)(right0 - right1), (int)(right1 - right) ); \ 00137 for( i = 0; i < n; i++ ) \ 00138 CV_SWAP( left[i], right0[i-n+1], t ); \ 00139 n = (int)(left - left1); \ 00140 m = (int)(right1 - right); \ 00141 if( n > 1 ) \ 00142 { \ 00143 if( m > 1 ) \ 00144 { \ 00145 if( n > m ) \ 00146 { \ 00147 stack[++sp].lb = left0; \ 00148 stack[sp].ub = left0 + n - 1; \ 00149 left = right0 - m + 1, right = right0; \ 00150 } \ 00151 else \ 00152 { \ 00153 stack[++sp].lb = right0 - m + 1; \ 00154 stack[sp].ub = right0; \ 00155 left = left0, right = left0 + n - 1; \ 00156 } \ 00157 } \ 00158 else \ 00159 left = left0, right = left0 + n - 1; \ 00160 } \ 00161 else if( m > 1 ) \ 00162 left = right0 - m + 1, right = right0; \ 00163 else \ 00164 break; \ 00165 } \ 00166 } \ 00167 } \ 00168 } 00169 00170 #define CV_IMPLEMENT_QSORT( func_name, T, cmp ) \ 00171 CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int ) 00172 00173 00174 using namespace std; 00175 00176 template<class T> class Image 00177 { 00178 private: 00179 IplImage* imgp; 00180 00181 public: 00182 Image( IplImage* img=0 ) 00183 { 00184 imgp=img; 00185 } 00186 00187 ~Image() 00188 { 00189 imgp=0; 00190 } 00191 00192 void operator=(IplImage* img) 00193 { 00194 imgp=img; 00195 } 00196 00197 inline T* operator[](const int rowIndx) 00198 { 00199 return ((T *)(imgp->imageData + rowIndx*imgp->widthStep)); 00200 } 00201 00202 T getPoint( int rowIndx , int colIndx ) 00203 { 00204 if ( rowIndx < 0 ) 00205 rowIndx = 0; 00206 else if ( rowIndx >= imgp->height ) 00207 rowIndx = imgp->height - 1; 00208 if ( colIndx < 0 ) 00209 colIndx = 0; 00210 else if ( colIndx >= imgp->width ) 00211 colIndx = imgp->width - 1; 00212 return ((T *)(imgp->imageData + rowIndx*imgp->widthStep))[colIndx]; 00213 } 00214 }; 00215 00216 typedef struct{ 00217 unsigned char b,g,r; 00218 } RgbPixel; 00219 00220 typedef struct{ 00221 float b,g,r; 00222 } RgbPixelFloat; 00223 00224 typedef Image<RgbPixel> RgbImage; 00225 typedef Image<RgbPixelFloat> RgbImageFloat; 00226 typedef Image<unsigned char> BwImage; 00227 typedef Image<float> BwImageFloat; 00228 00229 #endif