Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / interfaces / swig / general / extensions.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
43 // 2004-03-23, Mark Asbach       <asbach@ient.rwth-aachen.de>
44 //             Institute of Communications Engineering, RWTH Aachen University
45 // 2008-05-15, Xavier Delacour   <xavier.delacour@gmail.com>
46
47
48 %extend IplImage       { ~IplImage       () { IplImage       * dummy = self; cvReleaseImage              (& dummy); } }
49 %extend CvMat          { ~CvMat          () { CvMat          * dummy = self; cvReleaseMat                (& dummy); } }
50 %extend CvMatND        { ~CvMatND        () { CvMatND        * dummy = self; cvReleaseMatND              (& dummy); } }
51 %extend CvSparseMat    { ~CvSparseMat    () { CvSparseMat    * dummy = self; cvReleaseSparseMat          (& dummy); } }
52 %extend CvMemStorage   { ~CvMemStorage   () { CvMemStorage   * dummy = self; cvReleaseMemStorage         (& dummy); } }
53 %extend CvGraphScanner { ~CvGraphScanner () { CvGraphScanner * dummy = self; cvReleaseGraphScanner       (& dummy); } }
54 %extend CvFileStorage  { ~CvFileStorage  () { CvFileStorage  * dummy = self; cvReleaseFileStorage        (& dummy); } }
55 %extend IplConvKernel  { ~IplConvKernel  () { IplConvKernel  * dummy = self; cvReleaseStructuringElement (& dummy); } }
56 %extend CvKalman       { ~CvKalman       () { CvKalman       * dummy = self; cvReleaseKalman             (& dummy); } }
57 %extend CvConDensation { ~CvConDensation () { CvConDensation * dummy = self; cvReleaseConDensation       (& dummy); } }
58 %extend CvHistogram    { ~CvHistogram    () { CvHistogram    * dummy = self; cvReleaseHist               (& dummy); } }
59 %extend CvHaarClassifierCascade { ~CvHaarClassifierCascade () { CvHaarClassifierCascade * dummy = self; cvReleaseHaarClassifierCascade  (& dummy); } }
60 %extend CvPOSITObject  { ~CvPOSITObject  () { CvPOSITObject  * dummy = self; cvReleasePOSITObject        (& dummy); } }
61 %extend CvFeatureTree  { ~CvFeatureTree  () { CvFeatureTree  * dummy = self; cvReleaseFeatureTree        (& dummy); } }
62
63 // string operators for some OpenCV types
64
65 %extend CvScalar
66 {
67         const char * __str__(){
68                 static char str[256];
69                 snprintf(str, 256, "[%f, %f, %f, %f]", self->val[0], self->val[1], self->val[2], self->val[3]);
70                 return str;
71         }
72         const char * __repr__(){
73                 static char str[256];
74                 snprintf(str, 256, "cvScalar(%f, %f, %f, %f)", self->val[0], self->val[1], self->val[2], self->val[3]);
75                 return str;
76         }
77     const double __getitem__ (int index) {
78         if (index >= 4) {
79 #ifdef defined(SWIGPYTHON)
80             PyErr_SetString (PyExc_IndexError, "indice must be lower than 4");
81 #elif defined(SWIGOCTAVE)
82             error("indice must be lower than 4");
83 #endif
84             return 0;
85         }
86         if (index < -4) {
87 #ifdef defined(SWIGPYTHON)
88             PyErr_SetString (PyExc_IndexError, "indice must be bigger or egal to -4");
89 #elif defined(SWIGOCTAVE)
90             error("indice must be bigger or egal to -4");
91 #endif
92             return 0;
93         }
94         if (index < 0) {
95             /* negative index means from the end in python */
96             index = 4 - index;
97         }
98         return self->val [index];
99     }
100     void __setitem__ (int index, double value) {
101         if (index >= 4) {
102 #ifdef defined(SWIGPYTHON)
103             PyErr_SetString (PyExc_IndexError, "indice must be lower than 4");
104 #elif defined(SWIGOCTAVE)
105             error("indice must be lower than 4");
106 #endif
107             return;
108         }
109         if (index < -4) {
110 #ifdef defined(SWIGPYTHON)
111             PyErr_SetString (PyExc_IndexError, "indice must be bigger or egal to -4");
112 #elif defined(SWIGOCTAVE)
113             error("indice must be bigger or egal to -4");
114 #endif
115             return;
116         }
117         if (index < 0) {
118             /* negative index means from the end in python */
119             index = 4 - index;
120         }
121         self->val [index] = value;
122     }
123 };
124
125 %extend CvPoint2D32f
126 {
127         const char * __str__(){
128                 static char str[64];
129                 snprintf(str, 64, "[%f %f]", self->x, self->y);
130                 return str;
131         }
132         const char * __repr__(){
133                 static char str[64];
134                 snprintf(str, 64, "cvPoint2D32f(%f,%f)", self->x, self->y);
135                 return str;
136         }
137 };
138
139 %extend CvPoint
140 {
141         const char * __str__(){
142                 static char str[64];
143                 snprintf(str, 64, "[%d %d]", self->x, self->y);
144                 return str;
145         }
146         const char * __repr__(){
147                 static char str[64];
148                 snprintf(str, 64, "cvPoint(%d,%d)", self->x, self->y);
149                 return str;
150         }
151 };
152
153 // Set up CvMat to emulate IplImage fields
154 %{
155 int CvMat_cols_get(CvMat * m){
156         return m->cols;
157 }
158 int CvMat_rows_get(CvMat *m){
159         return m->rows;
160 }
161 int CvMat_width_get(CvMat * m){
162         return m->cols;
163 }
164 int CvMat_height_get(CvMat *m){
165         return m->rows;
166 }
167 int CvMat_depth_get(CvMat * m){
168         return cvCvToIplDepth(m->type);
169 }
170 int CvMat_nChannels_get(CvMat * m){
171         return CV_MAT_CN(m->type);
172 }
173 int CvMat_origin_get(CvMat * m){
174         return 0;
175 }
176 int CvMat_dataOrder_get(CvMat * m){
177         return 0;
178 }
179 int CvMat_imageSize_get(CvMat * m){
180         int step = m->step ? m->step : 
181           step = CV_ELEM_SIZE(m->type) * m->cols;
182         return step*m->rows;
183 }
184 int CvMat_widthStep_get(CvMat * m){
185         return m->step;
186 }
187 %}
188 %extend CvMat
189 {
190         const int depth;
191         const int nChannels;
192         const int dataOrder;
193         const int origin;
194         const int width;
195         const int height;
196         const int imageSize;
197         const int widthStep;
198         // swig doesn't like the embedded union in CvMat, so re-add these
199         const int rows;
200         const int cols;
201 };