/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
#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__  "interpola_filter"
#define __init__Filter__      initinterpola_filter

static char Filter_doc[]="";

static PyObject *
Filter(PyObject *self_a, PyObject *args)
{
  PyObject * A         ;
  PyObject * pos_      ;
  PyObject * siz_      ;
  PyObject * pos       ;
  PyObject * siz       ;
  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 = 0, c1 = 0;
  int c0_,c1_;
  float temp[9];
  float swap;
  int bool;
  int count;


  PyObject * dic_key;
  PyObject * dic_value;
  double threshold ;

  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;
  double shift;
  double fact1, fact2;
  int ishift1, ishift2;



 if(!PyArg_ParseTuple(args,"OOOOOf:CDD_Filter",&A,&pos_,&siz_, &pos, &siz, &shift ))
   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");

  /* 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 ); 
  }



  /* setting better the limits ( they were indexed by iptr )*/

  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) ) ;
  memset(m_data , 0  , dim0*dim1*sizeof(float) );


  



#define AA(i,j)  a_data[(i)*dim1+(j)    ]
#define MM(i,j)  m_data[(i)*dim1+(j)    ]  

  ishift1= (int) shift;

  if(shift<0) ishift1--;

  ishift2= ishift1+1;

  fact1 = ishift2-shift;
  fact2 = shift-ishift1;





  if( x0 !=0 ) {   /* verticale */

    for(c0=x0  ; c0< X0 ; c0++) {
      if( c0+ishift1 < x0_  || c0+ishift2 >= X0_ ) {
	MM(c0,c1) =AA(c0,c1) ;
      } else {
	for(c1=x1; c1< X1 ; c1++) {
	  MM(c0,c1) = fact1 * AA(c0+ishift1, c1) + fact2 * AA( c0+ishift2, c1);
	}
      }
    }
  }  else {
    
    for(c1=x1  ; c1< X1 ; c1++) {
      if( c1+ishift1 < x1_  || c1+ishift2 >= X1_ ) {
	MM(c0,c1) =AA(c0,c1) ;
      } else {
	for(c0=x0; c0< X0 ; c0++) {
	  MM(c0,c1) = fact1 * AA(c0, c1+ishift1) + fact2 * AA( c0, c1+ishift2);
	}
      }
    }
  }
  
  
  
  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
}