1 #include "octhelpers.h"
5 int OctSwigObject_Check(octave_value op);
7 octave_value OctTuple_FromIntArray(int * arr, int len){
8 octave_value obj = OctTuple_New(len);
9 for(int i=0; i<len; i++){
10 OctTuple_SetItem(obj, i, OctLong_FromLong( arr[i] ) );
15 octave_value SWIG_SetResult(octave_value result, octave_value obj){
16 result = OctTuple_New(1);
17 OctTuple_SetItem(result, 0, obj);
21 octave_value_list* SWIG_AppendResult(octave_value_list* result, octave_value* to_add, int num){
22 for (int j=0;j<num;++j)
23 result->append(to_add[j]);
28 std::ostream & cv_arr_write(std::ostream & out, T * data, int rows, int nch, int step){
30 char * cdata = (char *) data;
31 std::string chdelim1="", chdelim2="";
33 // only output channel parens if > 1
40 for(i=0; i<rows; i++){
45 out<<double(((T*)(cdata+i*step))[0]);
47 out<<", "<<double(((T*)(cdata+i*step))[k]);
52 for(j=nch*sizeof(T); j<step; j+=(nch*sizeof(T))){
54 out<<double(((T*)(cdata+i*step+j))[0]);
56 out<<", "<<double(((T*)(cdata+i*step+j))[k]);
66 void cvArrPrint(CvArr * arr){
67 CV_FUNCNAME( "cvArrPrint" );
73 mat = cvGetMat(arr, &stub);
75 int cn = CV_MAT_CN(mat->type);
76 int depth = CV_MAT_DEPTH(mat->type);
77 int step = MAX(mat->step, cn*mat->cols*CV_ELEM_SIZE(depth));
78 std::ostringstream str;
82 cv_arr_write(str, (uchar *)mat->data.ptr, mat->rows, cn, step);
85 cv_arr_write(str, (char *)mat->data.ptr, mat->rows, cn, step);
88 cv_arr_write(str, (ushort *)mat->data.ptr, mat->rows, cn, step);
91 cv_arr_write(str, (short *)mat->data.ptr, mat->rows, cn, step);
94 cv_arr_write(str, (int *)mat->data.ptr, mat->rows, cn, step);
97 cv_arr_write(str, (float *)mat->data.ptr, mat->rows, cn, step);
100 cv_arr_write(str, (double *)mat->data.ptr, mat->rows, cn, step);
103 CV_ERROR( CV_StsError, "Unknown element type");
106 std::cout<<str.str()<<std::endl;
111 // deal with negative array indices
112 int OctLong_AsIndex( octave_value idx_object, int len ){
113 int idx = OctLong_AsLong( idx_object );
114 if(idx<0) return len+idx;
118 CvRect OctSlice_to_CvRect(CvArr * src, octave_value idx_object){
119 CvSize sz = cvGetSize(src);
120 //printf("Size %dx%d\n", sz.height, sz.width);
121 int lower[2], upper[2];
122 Oct_ssize_t len, start, stop, step, slicelength;
124 if(OctInt_Check(idx_object) || OctLong_Check(idx_object)){
125 // if array is a row vector, assume index into columns
127 lower[0] = OctLong_AsIndex( idx_object, sz.height );
128 upper[0] = lower[0] + 1;
134 upper[0] = sz.height;
135 lower[1] = OctLong_AsIndex( idx_object, sz.width );
136 upper[1] = lower[1]+1;
141 else if(OctSlice_Check(idx_object)){
143 if(OctSlice_GetIndicesEx( idx_object, len, &start, &stop, &step, &slicelength )!=0){
144 error("Error in OctSlice_GetIndicesEx: returning NULL");
145 return cvRect(0,0,0,0);
147 // if array is a row vector, assume index bounds are into columns
149 lower[0] = (int) start; // use c convention of start index = 0
150 upper[0] = (int) stop; // use c convention
155 lower[1] = (int) start; // use c convention of start index = 0
156 upper[1] = (int) stop; // use c convention
158 upper[0] = sz.height;
163 else if(OctTuple_Check(idx_object)){
164 //printf("OctTuple{\n");
165 if(OctObject_Length(idx_object)!=2){
166 error("Expected a sequence with 2 elements");
167 return cvRect(0,0,0,0);
169 for(int i=0; i<2; i++){
170 octave_value o = OctTuple_GetItem(idx_object, i);
172 // 2a. Slice -- same as above
173 if(OctSlice_Check(o)){
174 //printf("OctSlice\n");
175 len = (i==0 ? sz.height : sz.width);
176 if(OctSlice_GetIndicesEx(o, len, &start, &stop, &step, &slicelength )!=0){
177 error("Error in OctSlice_GetIndicesEx: returning NULL");
178 return cvRect(0,0,0,0);
180 //printf("OctSlice_GetIndecesEx(%d, %d, %d, %d, %d)\n", len, start, stop, step, slicelength);
187 else if(OctInt_Check(o) || OctLong_Check(o)){
188 //printf("OctInt\n");
189 lower[i] = OctLong_AsIndex(o, i==0 ? sz.height : sz.width);
190 upper[i] = lower[i]+1;
194 error("Expected a slice or int as sequence item: returning NULL");
195 return cvRect(0,0,0,0);
201 error("Expected a slice or sequence: returning NULL");
202 return cvRect(0,0,0,0);
205 return cvRect(lower[1], lower[0], upper[1]-lower[1], upper[0]-lower[0]);
208 double OctObject_AsDouble(octave_value obj){
209 if(OctNumber_Check(obj)){
210 if(OctFloat_Check(obj)){
211 return OctFloat_AsDouble(obj);
213 else if(OctInt_Check(obj) || OctLong_Check(obj)){
214 return (double) OctLong_AsLong(obj);
217 error("Could not convert octave object to Double");
221 long OctObject_AsLong(octave_value obj){
222 if(OctNumber_Check(obj)){
223 if(OctFloat_Check(obj)){
224 return (long) OctFloat_AsDouble(obj);
226 else if(OctInt_Check(obj) || OctLong_Check(obj)){
227 return OctLong_AsLong(obj);
230 error("Could not convert octave object to Long");
234 // standard python container routines, adapted to octave
236 // * should matrix conversions happen here or at typemap layer? or both
238 bool OctNumber_Check(const octave_value& ov) {
239 return ov.is_scalar_type();
242 octave_value OctBool_FromLong (long v) {
246 bool OctInt_Check(const octave_value& ov) {
247 return ov.is_integer_type();
250 long OctInt_AsLong (const octave_value& ov) {
251 return ov.long_value();
254 octave_value OctInt_FromLong (long v) {
258 bool OctLong_Check(const octave_value& ov) {
259 return ov.is_scalar_type();
262 long OctLong_AsLong(const octave_value& ov) {
263 return ov.long_value();
266 octave_value OctLong_FromLong(long v) {
270 octave_value OctLong_FromUnsignedLong(unsigned long v) {
274 bool OctFloat_Check(const octave_value& ov) {
275 return ov.is_scalar_type();
278 octave_value OctFloat_FromDouble(double v) {
282 double OctFloat_AsDouble (const octave_value& ov) {
283 return ov.scalar_value();
286 octave_value OctSequence_New(int n) {
287 return n ? Cell(1,n) : Cell(dim_vector(0,0));
290 bool OctSequence_Check(const octave_value& ov) {
294 int OctSequence_Size(const octave_value& ov) {
295 Cell c(ov.cell_value());
296 return ov.cell_value().numel();
299 octave_value OctSequence_GetItem(const octave_value& ov,int i) {
300 Cell c(ov.cell_value());
301 if (i<0||i>=c.numel()) {
302 error("index out of bounds");
303 return octave_value();
308 void OctSequence_SetItem(octave_value& ov,int i,const octave_value& v) {
309 Cell c(ov.cell_value());
310 if (i<0||i>=c.numel())
311 error("index out of bounds");
318 octave_value OctTuple_New(int n) {
319 return OctSequence_New(n);
322 bool OctTuple_Check(const octave_value& ov) {
323 return OctSequence_Check(ov);
326 int OctTuple_Size(const octave_value& ov) {
327 return OctSequence_Size(ov);
330 void OctTuple_SetItem(octave_value& ov,int i,const octave_value& v) {
331 OctSequence_SetItem(ov,i,v);
334 octave_value OctTuple_GetItem(const octave_value& ov,int i) {
335 return OctSequence_GetItem(ov,i);
338 octave_value OctList_New(int n) {
339 return OctSequence_New(n);
342 bool OctList_Check(const octave_value& ov) {
343 return OctSequence_Check(ov);
346 int OctList_Size(const octave_value& ov) {
347 return OctSequence_Size(ov);
350 void OctList_SetItem(octave_value& ov,int i,const octave_value& v) {
351 OctSequence_SetItem(ov,i,v);
354 octave_value OctList_GetItem(const octave_value& ov,int i) {
355 return OctSequence_GetItem(ov,i);
358 bool OctSlice_Check(const octave_value& ov) {
359 return false; // todo have these map to range and magic-colon types
362 int OctObject_Length(const octave_value& ov) {
366 bool OctSlice_GetIndicesEx(const octave_value& ov, Oct_ssize_t len, Oct_ssize_t* start, Oct_ssize_t* stop, Oct_ssize_t* step, Oct_ssize_t* slicelength ) {