00001 #include <unistd.h>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005
00006 #include "fmedian_src.h"
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define QSRT_k_func2(TYPE) \
00019 QSRT_k_func2_sig(TYPE) \
00020 { \
00021 TYPE av; \
00022 long i,j,l,r,test; \
00023 l = 0; \
00024 r = n-1; \
00025 \
00026 while (r > l) { \
00027 av = a[r]; \
00028 i = l - 1; \
00029 j = r; \
00030 \
00031 test = 1; \
00032 while (test) { \
00033 ++i; \
00034 while ((a[i] < av) && (i < j)) ++i; \
00035 --j; \
00036 while ((a[j] > av) && (j > i)) --j; \
00037 if (i < j) SWAP2(TYPE,a[i],a[j]) \
00038 else test = 0; \
00039 } \
00040 \
00041 SWAP2(TYPE,a[i],a[r]) \
00042 \
00043 \
00044 if (i == k) return; \
00045 \
00046 \
00047 if (k < i) r = i-1; \
00048 else l = i+1; \
00049 } \
00050 return; \
00051 }
00052
00053 QSRT_k_func2(UCHAR)
00054 QSRT_k_func2(IDL_INT)
00055 QSRT_k_func2(IDL_UINT)
00056 QSRT_k_func2(IDL_LONG)
00057 QSRT_k_func2(IDL_ULONG)
00058 QSRT_k_func2(IDL_LONG64)
00059 QSRT_k_func2(IDL_ULONG64)
00060 QSRT_k_func2(float)
00061 QSRT_k_func2(double)
00062
00063 #define FMEDIAN_CORE_FUNC2(TYPE) \
00064 FMEDIAN_CORE_FUNC2_SIG(TYPE) \
00065 { \
00066 \
00067 \
00068 long midx = (xbox - 1)/2; \
00069 long midy = (ybox - 1)/2; \
00070 \
00071 long outx, outy; \
00072 \
00073 \
00074 \
00075 \
00076 \
00077 if ((((xbox > ybox) ? xbox : ybox) < 2) ^ (nx < 1)) { \
00078 long i; \
00079 for (i=0; i < (nx * ny); ++i) out[i] = in[i]; \
00080 } else { \
00081 \
00082 if (find_missing) { \
00083 long i; \
00084 for (i=0; i < nx*ny; i++) \
00085 missing = MIN2(*(in+i),missing); \
00086 missing -= 1; \
00087 } \
00088 \
00089 \
00090 \
00091 \
00092 for (outy=0; outy < ny; ++outy) { \
00093 \
00094 long iny_bot = MAX2(outy - midy,0); \
00095 long iny_top = MIN2(outy - midy + ybox,ny) - 1; \
00096 \
00097 for (outx=0; outx < nx; ++outx) { \
00098 \
00099 long inx_lft = MAX2(outx-midx,0); \
00100 long inx_rgt = MIN2(outx - midx + xbox,nx) - 1; \
00101 \
00102 out[outx+nx*outy] = in[outx+nx*outy]; \
00103 \
00104 \
00105 if (always || in[outx+nx*outy]==missing) { \
00106 long np=0; \
00107 long iny, inx; \
00108 for (iny=iny_bot; iny<=iny_top; ++iny) { \
00109 for (inx=inx_lft; inx<=inx_rgt; ++inx) { \
00110 if (in[inx+nx*iny] != missing) work[np++] = in[inx+nx*iny]; \
00111 } \
00112 } \
00113 \
00114 \
00115 if (np==0 || np==1) continue; \
00116 \
00117 \
00118 if ( np == 2) { \
00119 out[outx+nx*outy] = (work[0]+work[1])/2; \
00120 continue; \
00121 } \
00122 \
00123 QSRT_k_call2(TYPE,work,np/2,np); \
00124 out[outx+nx*outy] = work[np/2]; \
00125 } \
00126 } \
00127 } \
00128 } \
00129 \
00130 }
00131
00132
00133 FMEDIAN_CORE_FUNC2(UCHAR)
00134
00135 FMEDIAN_CORE_FUNC2(IDL_INT)
00136
00137 FMEDIAN_CORE_FUNC2(IDL_UINT)
00138
00139 FMEDIAN_CORE_FUNC2(IDL_LONG)
00140
00141 FMEDIAN_CORE_FUNC2(IDL_ULONG)
00142
00143 FMEDIAN_CORE_FUNC2(IDL_LONG64)
00144
00145 FMEDIAN_CORE_FUNC2(IDL_ULONG64)
00146
00147 FMEDIAN_CORE_FUNC2(float)
00148
00149 FMEDIAN_CORE_FUNC2(double)