/tomo/pyhst

To get this branch, use:
bzr branch http://darksoft.org/webbzr/tomo/pyhst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include"Python.h"
#include"structmember.h"
#include<string.h>

#include "numpy/arrayobject.h"

#ifdef LINUX
 #define _FILE_OFFSET_BITS  64
#endif


#define DEBUG(a)


 /* The error object to expose 
 */

static PyObject *ErrorObject;
#define onError(message)\
  { PyErr_SetString(ErrorObject, message); return NULL;}


#define  __CCD_Filter_name__  "CCD_FilterAbsRel"
#define __init__Filter__      initCCD_FilterAbsRel

static char Filter_doc[]=
"/* "
""
"   Filter( item , pos_edf_, size_edf_, pos_edf, size_edf,   CCD_FILTER_PARA     )"
"  "
"   CCD_FILTER_PARA  is a python dictionary with parameters"
" */"
;

static PyObject *
Filter(PyObject *self_a, PyObject *args)
{
  PyObject * A         ;
  PyObject * pos_      ;
  PyObject * siz_      ;
  PyObject * pos       ;
  PyObject * siz       ;
  PyObject * para_dict ;
  PyObject * tmp;
  char msg[400];
  
  int x0_,x1_, x0,x1, s0_,s1_,s0,s1;
  int X0,X1,X0_, X1_;
  int dim0, dim1;
  int nsum;
  int ds;


  int c0,c1;
  int c0_,c1_;
  float temp[9];
  float swap;
  int bool;
  int count;


  PyObject * dic_key;
  PyObject * dic_value;

  double threshold ;
  int fracornot;
  int absornot;

  double valtocomp;
 

  int i;


  int *iptr[] = { &x0_ , &x1_  ,&x0 , &x1 , &s0_ , &s1_ , &s0 , &s1 };

  int shift_x[] = { -1,-1,-1, 0, 0,  0, 1,  1, 1  };
  int shift_y[] = { -1, 0, 1,-1, 0, 1,-1,  0, 1  };
  int si;

  PyObject ** containers[] = {&pos_ , &pos, &siz_, &siz   };


  float * a_data;
  float * m_data;





 if(!PyArg_ParseTuple(args,"OOOOOO:CDD_Filter",&A,&pos_,&siz_, &pos, &siz, &para_dict  ))
   return NULL;

 /* check the Objects */

  if(!PyArray_Check(A ))    onError("not a PyArray, argument 1");
  if(!PyList_Check (pos_ ) )     onError("not a PyList, argument 2");
  if(!PyList_Check (siz_ ))    onError("not a PyList, argument 3");
  if(!PyList_Check (pos ))         onError("not a PyList, argument 4");
  if(!PyList_Check(siz ))       onError("not a PyList, argument 5");
  if(!PyDict_Check(para_dict ))        onError("not a PyDict, argument 6");

  /* check the types */

  if( ((PyArrayObject *) A )  ->descr->type_num != PyArray_FLOAT ) onError(" arg 1 is not an array of float " ) ;

  /* check n of dimensione */

  if( ((PyArrayObject *) A)->nd !=2)  onError(" arg 1 is not a matrix " ) ;
 

  /* check contiguity */

  if(  ((PyArrayObject *) A) ->flags %2 == 0) onError(" array has to be contiguous");

  a_data = (float *) ((PyArrayObject *) A)->data;
  /* getting positions and sizes */

  for (i=0; i<8; i++) {
    tmp =  PyList_GetItem( *(containers[i/2])  , i%2);
    if(!PyInt_Check( tmp )) {
      sprintf(msg, " not an integer, item %d of argument %d", i%2, i/2+2);
      onError(msg);
    }
    *iptr[i] = PyInt_AS_LONG( tmp ); 
  }


  /* getting parameter(s) contained in the dictionary */

  dic_key = PyString_FromString("threshold");
  dic_value =  PyDict_GetItem(para_dict , dic_key);
  Py_DECREF(dic_key);

  if( dic_value == NULL) 
    onError(" unable to find the threshold key in the passed dictionary");

  if( !PyFloat_Check(dic_value) ) 
    onError("supplied value for threshold is not a float ");
  
  threshold = PyFloat_AsDouble (dic_value  );



  /* ABS???????????????????/ */
  dic_key = PyString_FromString("ABS");
  dic_value =  PyDict_GetItem(para_dict , dic_key);
  Py_DECREF(dic_key);
  if( dic_value == NULL) 
    onError(" unable to find the ABS key in the passed dictionary");
  if( !PyInt_Check(dic_value) ) 
    onError("supplied value for ABS  is not a integer ");
  
  absornot = PyInt_AsLong  (dic_value  );
  if( absornot !=0 && absornot!=1 ) {
    onError("supplied value for ABS  must be 1 or 0 ");
  }
  
  
  
  /* FRACTIONNAIRE???????????????????/ */
  dic_key = PyString_FromString("FRAC");
  dic_value =  PyDict_GetItem(para_dict , dic_key);
  Py_DECREF(dic_key);
  if( dic_value == NULL) 
    onError(" unable to find the FRAC key in the passed dictionary");
  if( !PyInt_Check(dic_value) ) 
    onError("supplied value for  FRAC is not a integer ");
  
  fracornot = PyInt_AsLong  (dic_value  );
  if( fracornot !=0 && fracornot !=1 ) {
    onError("supplied value for  FRAC  must be 1 or 0 ");
  }
  
  
  /* setting better the limits */

  x0 = x0 - x0_ ;
  x1 = x1 - x1_ ;
  
  x0_ = 0 ;
  x1_ = 0 ;
  

  X0 = x0 + s0 ;
  X1 = x1 + s1 ;
  
  X0_ = x0_ + s0_ ;
  X1_ = x1_ + s1_ ;


  /* checking that the supllied limits are not completely wrong */

  if ( ((PyArrayObject *) A)->dimensions[0] != s0_  || ((PyArrayObject *) A)->dimensions[1] != s1_  ) {
    onError(" array dimension and supplied limits are not consistent");
  }
  
  dim0 = s0_;
  dim1 = s1_;
  
  /* straighforward addressing scheme in case of contiguous memory. Or you'll need to use strides */


  m_data=(float*) malloc( dim0*dim1 * sizeof(float) ) ;
  memcpy(m_data ,a_data  , dim0*dim1*sizeof(float) );
  
  
#define AA(i,j)  a_data[(i)*dim1+(j)    ]
#define MM(i,j)  m_data[(i)*dim1+(j)    ]
  
  for(c0=x0+1 ; c0< X0 -1; c0++) {
    
    for(c1=x1+1; c1< X1 -1; c1++) {
      
      
      //Naechste Nachbarn einlesen
      temp[0] = AA(c0-1,c1-1);
      temp[1] = AA(c0-1,c1);
      temp[2] = AA(c0-1,c1+1);
      temp[3] = AA(c0,c1-1);
      temp[4] = AA(c0,c1);
      temp[5] = AA(c0,c1+1);
      temp[6] = AA(c0+1,c1-1);
      temp[7] = AA(c0+1,c1);
      temp[8] = AA(c0+1,c1+1);
      
      //Bubble-Sort
      do
	{
	  bool = 0;
	  for (count = 0; count < 8; count++)
	    {
	      if (temp[count] > temp[count+1])
		{
		  swap = temp[count];
		  temp[count] = temp[count+1];
		  temp[count+1] = swap;
		  bool = 1;
		}
	    }
	} while(bool!=0);

      valtocomp =       AA(c0,c1) -temp[4] ;
      if( absornot){
	valtocomp = fabs( valtocomp);
      }
      if( fracornot ) {
	valtocomp /= fabs(AA(c0,c1));
      }
      if (  valtocomp >= threshold ) MM(c0,c1) = temp[4];

      else MM(c0,c1) = AA(c0,c1);
    }
  }
  
  ds=(s0-1);
  if(ds==0) ds=1;
  for(c0= x0 ; c0<X0 ; c0 +=ds) {  
    for(c1=x1; c1<X1; c1 ++ ) {   
      nsum=0;
      
      for(si=0; si<9; si++) {
	
	c0_ = c0+ shift_x[si];
	c1_ = c1+ shift_y[si];
	
	
	
	if( c0_>=x0_ &&  c0_ <s0_ &&    c1_>=x1_ &&  c1_ <X1_  ) {
	  temp[nsum ] = AA(c0_,c1_); ;
	  nsum++;
	}
      }
      
      //Bubble-Sort
      do
	{
	  bool = 0;
	  for (count = 0; count < nsum-1; count++)
	    {
	      if (temp[count] > temp[count+1])
		{
		  swap = temp[count];
		  temp[count] = temp[count+1];
		  temp[count+1] = swap;
		  bool = 1;
		}
	    }
	} while(bool!=0);
      
      if (  AA(c0,c1) -temp[nsum/2] >= threshold ) MM(c0,c1) = temp[nsum/2];
      else MM(c0,c1) = AA(c0,c1);
      
    }
    
  }
  for(c0= x0 ; c0<X0 ; c0 ++) {
    ds=(s1-1);
    if(ds==0) ds=1;

    for(c1=x1; c1<X1; c1 += ds) {
      nsum=0;

      for(si=0; si<9; si++) {

        c0_ = c0+ shift_x[si];
        c1_ = c1+ shift_y[si];



        if( c0_>=x0_ &&  c0_ <s0_ &&    c1_>=x1_ &&  c1_ <X1_  ) {
          temp[nsum ] = AA(c0_,c1_); ;
          nsum++;
        }
      }

      //Bubble-Sort
      do
        {
          bool = 0;
          for (count = 0; count < nsum-1; count++)
            {
              if (temp[count] > temp[count+1])
                {
                  swap = temp[count];
                  temp[count] = temp[count+1];
                  temp[count+1] = swap;
                  bool = 1;
                }
            }
        } while(bool!=0);

      if (  AA(c0,c1) -temp[nsum/2] >= threshold ) MM(c0,c1) = temp[nsum/2];
      else MM(c0,c1) = AA(c0,c1);

    }

    
    
  } 
  
  
  memcpy( a_data, m_data, dim0*dim1*sizeof(float) );
  free(m_data);
  Py_INCREF(Py_None);
  return Py_None;
    
}



static PyMethodDef CCD_Filter_functions [] = {
  {"Filter", Filter,  METH_VARARGS, Filter_doc},
  { NULL, NULL}
};






void 
__init__Filter__   ()
{
  PyObject *m, *d;
  m = Py_InitModule(  __CCD_Filter_name__    ,  CCD_Filter_functions  );
  d = PyModule_GetDict(m);
  ErrorObject = Py_BuildValue("s",  __CCD_Filter_name__  ".error");
  PyDict_SetItemString(d,"error", ErrorObject);
  if(PyErr_Occurred())
    Py_FatalError("can't initialize module " __CCD_Filter_name__  );

#ifdef import_array
  import_array();
#endif
}