Update to 2.0.0 tree from current Fremantle build
[opencv] / interfaces / swig / octave / cvarr.i
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 // 2006-02-17  Roman Stanchak <rstancha@cse.wustl.edu>
43 // 2006-07-19  Moved most operators to general/cvarr_operators.i for use with other languages
44
45 /*M//////////////////////////////////////////////////////////////////////////////////////////
46 // Macros for extending CvMat and IplImage -- primarily for operator overloading 
47 //////////////////////////////////////////////////////////////////////////////////////////M*/
48
49 // Macro to define octave function of form B = A.f(c)
50 // where A is a CvArr type, c and B are arbitrary types
51 %define %wrap_cvGeneric_CvArr(cname, rettype, octfunc, argtype, cvfunc, newobjcall)
52      %newobject cname::octfunc(argtype arg);
53      %extend cname { 
54   rettype octfunc(argtype arg){
55     rettype retarg = newobjcall;
56     cvfunc;
57     return retarg;
58   }
59      }
60 %enddef
61
62 // Macro to define octave function of the form B = A.f(c) 
63 // where A and B are both CvArr of same size and type
64 %define %wrap_cvArr_binaryop(octfunc, argtype, cvfunc)
65      %wrap_cvGeneric_CvArr(CvMat, CvMat *, octfunc, argtype, cvfunc, 
66                            cvCreateMat(self->rows, self->cols, self->type));
67      %wrap_cvGeneric_CvArr(IplImage, IplImage *, octfunc, argtype, cvfunc,
68                            cvCreateImage(cvGetSize(self), self->depth, self->nChannels));
69      %enddef
70
71      // Macro to define octave function of the form A = A.f(c) 
72      // where f modifies A inplace
73      // use for +=, etc
74      %define %wrap_cvGeneric_InPlace(cname, rettype, octfunc, argtype, cvfunc)
75      %wrap_cvGeneric_CvArr(cname, rettype, octfunc, argtype, cvfunc, self);
76      %enddef
77
78      /*M//////////////////////////////////////////////////////////////////////////////////////////
79      // Macros to map operators to specific OpenCV functions
80      //////////////////////////////////////////////////////////////////////////////////////////M*/
81
82      // map any OpenCV function of form cvFunc(src1, src2, dst)
83      %define %wrap_cvArith(octfunc, cvfunc)
84      %wrap_cvArr_binaryop(octfunc, CvArr *, cvfunc(self, arg, retarg));
85      %enddef
86
87      // map any OpenCV function of form cvFunc(src1, value, dst)
88      %define %wrap_cvArithS(octfunc, cvfuncS)
89      %wrap_cvArr_binaryop(octfunc, CvScalar, cvfuncS(self, arg, retarg));
90      %wrap_cvArr_binaryop(octfunc, double, cvfuncS(self, cvScalar(arg), retarg));
91      %enddef
92
93      // same as wrap_cvArith
94      %define %wrap_cvLogic(octfunc, cvfunc)
95      %wrap_cvArr_binaryop(octfunc, CvArr *, cvfunc(self, arg, retarg))
96      %enddef
97
98      // same as wrap_cvArithS
99      %define %wrap_cvLogicS(octfunc, cvfuncS)
100      %wrap_cvArr_binaryop(octfunc, CvScalar, cvfuncS(self, arg, retarg));
101      %wrap_cvArr_binaryop(octfunc, double, cvfuncS(self, cvScalar(arg), retarg));
102      %enddef
103
104      // Macro to map logical operations to cvCmp
105      %define %wrap_cvCmp(octfunc, cmp_op)
106      %wrap_cvGeneric_CvArr(CvMat, CvArr *, octfunc, CvMat *, 
107                            cvCmp(self, arg, retarg, cmp_op), 
108                            cvCreateMat(self->rows, self->cols, CV_8U));
109      %wrap_cvGeneric_CvArr(IplImage, CvArr *, octfunc, IplImage *, 
110                            cvCmp(self, arg, retarg, cmp_op), 
111                            cvCreateImage(cvGetSize(self), 8, 1));
112      %enddef
113
114      %define %wrap_cvCmpS(octfunc, cmp_op)
115      %wrap_cvGeneric_CvArr(CvMat, CvArr *, octfunc, double, 
116                            cvCmpS(self, arg, retarg, cmp_op), 
117                            cvCreateMat(self->rows, self->cols, CV_8U));
118      %wrap_cvGeneric_CvArr(IplImage, CvArr *, octfunc, double, 
119                            cvCmpS(self, arg, retarg, cmp_op), 
120                            cvCreateImage(cvGetSize(self), 8, 1));
121      %enddef
122
123      // special case for cvScale, /, * 
124      %define %wrap_cvScale(octfunc, scale)
125      %wrap_cvGeneric_CvArr(CvMat, CvArr *, octfunc, double,
126                            cvScale(self, retarg, scale),
127                            cvCreateMat(self->rows, self->cols, self->type));
128      %wrap_cvGeneric_CvArr(IplImage, CvArr *, octfunc, double,
129                            cvScale(self, retarg, scale),
130                            cvCreateImage(cvGetSize(self), self->depth, self->nChannels));
131      %enddef
132
133      /*M//////////////////////////////////////////////////////////////////////////////////////////
134      // Actual Operator Declarations
135      //////////////////////////////////////////////////////////////////////////////////////////M*/
136
137      // Arithmetic operators 
138      %wrap_cvArith(__radd__, cvAdd);
139
140 // special case for reverse operations
141 %wrap_cvArr_binaryop(__rsub__, CvArr *, cvSub(arg, self, retarg));
142 %wrap_cvArr_binaryop(__rdiv__, CvArr *, cvDiv(arg, self, retarg));
143 %wrap_cvArr_binaryop(__rmul__, CvArr *, cvMatMul(arg, self, retarg));
144
145 %wrap_cvArithS(__radd__, cvAddS);
146 %wrap_cvArithS(__rsub__, cvSubRS);
147
148
149 %wrap_cvScale(__rmul__, arg);
150 %wrap_cvScale(__rdiv__, 1.0/arg);
151
152 %wrap_cvLogicS(__ror__, cvOrS)
153      %wrap_cvLogicS(__rand__, cvAndS)
154      %wrap_cvLogicS(__rxor__, cvXorS)
155
156      %wrap_cvCmpS(__req__, CV_CMP_EQ);
157      %wrap_cvCmpS(__rgt__, CV_CMP_GT);
158      %wrap_cvCmpS(__rge__, CV_CMP_GE);
159      %wrap_cvCmpS(__rlt__, CV_CMP_LT);
160      %wrap_cvCmpS(__rle__, CV_CMP_LE);
161      %wrap_cvCmpS(__rne__, CV_CMP_NE);
162
163
164      // misc operators for octave
165      %wrap_cvArr_binaryop(__pow__, double, cvPow(self, retarg, arg))
166
167      // TODO -- other Octave operators listed in SWIG/Octave docs (www.swig.org)
168
169      // __abs__ -- cvAbs
170      // __nonzero__
171      // __hash__ ??
172      // __repr__  -- full string representation
173      // __str__  -- compact representation
174      // __call__ -- ??
175      // __len__ -- number of rows? or elements?
176      // __iter__ -- ??
177      // __contains__ -- cvCmpS, cvMax ?
178      // __floordiv__ ??
179      // __mul__ -- cvGEMM
180      // __lshift__ -- ??
181      // __rshift__ -- ??
182      // __pow__ -- cvPow
183
184      // Called to implement the unary arithmetic operations (-, +, abs() and ~). 
185      //__neg__(  self)
186      //__pos__(  self)
187      //__abs__(  self)
188      //__invert__(  self)
189
190      // Called to implement the built-in functions complex(), int(), long(), and float(). Should return a value of the appropriate type.  Can I abuse this to return an array of the correct type??? scipy only allows return of length 1 arrays.
191      // __complex__( self )
192      // __int__( self )
193      // __long__( self )
194      // __float__( self )
195
196      /*M//////////////////////////////////////////////////////////////////////////////////////////
197      // Slice access and assignment for CvArr types
198      //////////////////////////////////////////////////////////////////////////////////////////M*/
199
200      // TODO: CvMatND
201
202      %newobject CvMat::__paren(octave_value object);
203      %newobject _IplImage::__paren(octave_value object);
204
205      // Macro to check bounds of slice and throw error if outside
206      %define CHECK_SLICE_BOUNDS(rect,w,h,retval)
207      //printf("__setitem__ slice(%d:%d, %d:%d) array(%d,%d)", rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
208      if(rect.width<=0 || rect.height<=0 ||
209         rect.width>w || rect.height>h ||
210         rect.x<0 || rect.y<0 ||
211         rect.x>= w || rect.y >=h){
212        char errstr[256];
213        // previous function already set error string
214        if(rect.width==0 && rect.height==0 && rect.x==0 && rect.y==0) return retval;
215        sprintf(errstr, "Requested slice [ %d:%d %d:%d ] oversteps array sized [ %d %d ]", 
216                rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
217        error(errstr);
218        return retval;
219      }
220 else{}
221 %enddef
222
223 // slice access and assignment for CvMat
224 %extend CvMat
225 {
226   char * __str(){
227     static char str[8];
228     cvArrPrint( self );
229     str[0]=0;
230     return str;
231     return "";
232   }
233
234   // scalar assignment
235   void __paren_asgn(octave_value object, double val){
236     CvMat tmp;
237     CvRect subrect = OctSlice_to_CvRect( self, object );
238     CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
239     cvGetSubRect(self, &tmp, subrect);
240     cvSet(&tmp, cvScalarAll(val));
241   }
242   void __paren_asgn(octave_value object, CvPoint val){
243     CvMat tmp;
244     CvRect subrect = OctSlice_to_CvRect( self, object );
245     CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
246     cvGetSubRect(self, &tmp, subrect);
247     cvSet(&tmp, cvScalar(val.x, val.y));
248   }
249   void __paren_asgn(octave_value object, CvPoint2D32f val){
250     CvMat tmp;
251     CvRect subrect = OctSlice_to_CvRect( self, object );
252     cvGetSubRect(self, &tmp, subrect);
253     CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
254     cvSet(&tmp, cvScalar(val.x, val.y));
255   }
256   void __paren_asgn(octave_value object, CvScalar val){
257     CvMat tmp;
258     CvRect subrect = OctSlice_to_CvRect( self, object );
259     cvGetSubRect(self, &tmp, subrect);
260     CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
261     cvSet(&tmp, val);
262   }
263
264   // array slice assignment
265   void __paren_asgn(octave_value object, CvArr * arr){
266     CvMat tmp, src_stub, *src;
267     CvRect subrect = OctSlice_to_CvRect( self, object );
268     CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
269     cvGetSubRect(self, &tmp, subrect);
270
271     // Reshape source array to fit destination
272     // This will be used a lot for small arrays b/c
273     // OctObject_to_CvArr tries to compress a 2-D octave
274     // array with 1-4 columns into a multichannel vector
275     src=cvReshape(arr, &src_stub, CV_MAT_CN(tmp.type), tmp.rows);
276
277     cvConvert(src, &tmp);
278   }
279
280   // slice access
281   octave_value __paren(octave_value object){
282     CvMat * mat;
283     CvRect subrect = OctSlice_to_CvRect( self, object );
284     CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, octave_value() );
285     if(subrect.width==1 && subrect.height==1){
286       CvScalar * s; 
287       int type = cvGetElemType( self );
288       if(CV_MAT_CN(type) > 1){
289         s = new CvScalar; 
290         *s = cvGet2D( self, subrect.y, subrect.x );
291         return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
292       }
293       switch(CV_MAT_DEPTH(type)){
294       case CV_8U:
295         return OctLong_FromUnsignedLong( CV_MAT_ELEM(*self, uchar, subrect.y, subrect.x ) );
296       case CV_8S:
297         return OctLong_FromLong( CV_MAT_ELEM(*self, char, subrect.y, subrect.x ) );
298       case CV_16U:
299         return OctLong_FromUnsignedLong( CV_MAT_ELEM(*self, ushort, subrect.y, subrect.x ) );
300       case CV_16S:
301         return OctLong_FromLong( CV_MAT_ELEM(*self, short, subrect.y, subrect.x ) );
302       case CV_32S:
303         return OctLong_FromLong( CV_MAT_ELEM(*self, int, subrect.y, subrect.x ) );
304       case CV_32F:
305         return OctFloat_FromDouble( CV_MAT_ELEM(*self, float, subrect.y, subrect.x) );
306       case CV_64F:
307         return OctFloat_FromDouble( CV_MAT_ELEM(*self, double, subrect.y, subrect.x) );
308       }
309     }
310     mat = (CvMat *) cvAlloc(sizeof(CvMat));
311     cvGetSubRect(self, mat, subrect);
312                 
313     // cvGetSubRect doesn't do this since it assumes mat lives on the stack
314     mat->hdr_refcount = self->hdr_refcount;
315     mat->refcount = self->refcount;
316     cvIncRefData(mat);
317
318     return SWIG_NewPointerObj( mat, $descriptor(CvMat *), 1 );
319   }
320
321   /*
322     %octavecode %{
323     def __iter__(self):
324     """
325     generator function iterating through rows in matrix or elements in vector
326     """
327     if self.rows==1:
328     return self.colrange()
329     return self.rowrange()
330
331     def rowrange(self):
332     """
333     generator function iterating along rows in matrix
334     """
335     for i in range(self.rows):
336     yield self[i]
337
338     def colrange(self):
339     """
340     generator function iterating along columns in matrix
341     """
342     for i in range(self.cols):
343     yield self[:,i]
344
345     # if arg is None, octave still calls our operator overloads
346     # but we want
347     # if mat != None
348     # if mat == None
349     # to do the right thing -- so redefine __ne__ and __eq__
350
351     def __eq__(self, arg):
352     """
353     __eq__(self, None)
354     __eq__(self, CvArr src)
355     __eq__(self, double val)
356     """
357
358     if not arg:
359     return False 
360     return _cv.CvMat___eq__(self, arg)
361     def __ne__(self, arg):
362     """
363     __ne__(self, None)
364     __ne__(self, CvArr src)
365     __ne__(self, double val)
366     """
367
368     if not arg:
369     return True
370     return _cv.CvMat___ne__(self, arg)
371     %}
372   */
373
374 }
375
376 // slice access and assignment for IplImage 
377 %extend _IplImage
378 {
379   char * __str(){
380     static char str[8];
381     cvArrPrint( self );
382     str[0]=0;
383     return str;
384     return "";
385   }
386
387   // scalar assignment
388   void __paren_asgn(octave_value object, double val){
389     CvMat tmp;
390     CvRect subrect = OctSlice_to_CvRect( self, object );
391     cvGetSubRect(self, &tmp, subrect);
392     cvSet(&tmp, cvScalarAll(val));
393   }
394   void __paren_asgn(octave_value object, CvPoint val){
395     CvMat tmp;
396     CvRect subrect = OctSlice_to_CvRect( self, object );
397     cvGetSubRect(self, &tmp, subrect);
398     cvSet(&tmp, cvScalar(val.x, val.y));
399   }
400   void __paren_asgn(octave_value object, CvPoint2D32f val){
401     CvMat tmp;
402     CvRect subrect = OctSlice_to_CvRect( self, object );
403     cvGetSubRect(self, &tmp, subrect);
404     cvSet(&tmp, cvScalar(val.x, val.y));
405   }
406   void __paren_asgn(octave_value object, CvScalar val){
407     CvMat tmp;
408     CvRect subrect = OctSlice_to_CvRect( self, object );
409     cvGetSubRect(self, &tmp, subrect);
410     cvSet(&tmp, val);
411   }
412
413   // array slice assignment
414   void __paren_asgn(octave_value object, CvArr * arr){
415     CvMat tmp;
416     CvRect subrect = OctSlice_to_CvRect( self, object );
417     cvGetSubRect(self, &tmp, subrect);
418     cvConvert(arr, &tmp);
419   }
420
421   // slice access
422   octave_value __paren(octave_value object){
423     CvMat mat;
424     IplImage * im;
425     CvRect subrect = OctSlice_to_CvRect( self, object );
426
427     // return scalar if single element
428     if(subrect.width==1 && subrect.height==1){
429       CvScalar * s;
430       int type = cvGetElemType( self );
431       if(CV_MAT_CN(type) > 1){
432         s = new CvScalar;
433         *s = cvGet2D( self, subrect.y, subrect.x );
434         return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
435       }
436       switch(CV_MAT_DEPTH(type)){
437       case CV_8U:
438         return OctLong_FromUnsignedLong( CV_IMAGE_ELEM(self, uchar, subrect.y, subrect.x ) );
439       case CV_8S:
440         return OctLong_FromLong( CV_IMAGE_ELEM(self, char, subrect.y, subrect.x ) );
441       case CV_16U:
442         return OctLong_FromUnsignedLong( CV_IMAGE_ELEM(self, ushort, subrect.y, subrect.x ) );
443       case CV_16S:
444         return OctLong_FromLong( CV_IMAGE_ELEM(self, short, subrect.y, subrect.x ) );
445       case CV_32S:
446         return OctLong_FromLong( CV_IMAGE_ELEM(self, int, subrect.y, subrect.x ) );
447       case CV_32F:
448         return OctFloat_FromDouble( CV_IMAGE_ELEM(self, float, subrect.y, subrect.x) );
449       case CV_64F:
450         return OctFloat_FromDouble( CV_IMAGE_ELEM(self, double, subrect.y, subrect.x) );
451       }
452     }
453
454     // otherwise return array
455     im = (IplImage *) cvAlloc(sizeof(IplImage));
456     cvGetSubRect(self, &mat, subrect);
457     im = cvGetImage(&mat, im);
458     return SWIG_NewPointerObj( im, $descriptor(_IplImage *), 1 );
459   }
460 }
461