Update to 2.0.0 tree from current Fremantle build
[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 struct CvLSH {};
49
50 %extend IplImage       { ~IplImage       () { IplImage       * dummy = self; cvReleaseImage              (& dummy); } }
51 %extend CvMat          { ~CvMat          () { CvMat          * dummy = self; cvReleaseMat                (& dummy); } }
52 %extend CvMatND        { ~CvMatND        () { CvMatND        * dummy = self; cvReleaseMatND              (& dummy); } }
53 %extend CvSparseMat    { ~CvSparseMat    () { CvSparseMat    * dummy = self; cvReleaseSparseMat          (& dummy); } }
54 %extend CvMemStorage   { ~CvMemStorage   () { CvMemStorage   * dummy = self; cvReleaseMemStorage         (& dummy); } }
55 %extend CvGraphScanner { ~CvGraphScanner () { CvGraphScanner * dummy = self; cvReleaseGraphScanner       (& dummy); } }
56 %extend CvFileStorage  { ~CvFileStorage  () { CvFileStorage  * dummy = self; cvReleaseFileStorage        (& dummy); } }
57 %extend IplConvKernel  { ~IplConvKernel  () { IplConvKernel  * dummy = self; cvReleaseStructuringElement (& dummy); } }
58 %extend CvKalman       { ~CvKalman       () { CvKalman       * dummy = self; cvReleaseKalman             (& dummy); } }
59 %extend CvConDensation { ~CvConDensation () { CvConDensation * dummy = self; cvReleaseConDensation       (& dummy); } }
60 %extend CvHistogram    { ~CvHistogram    () { CvHistogram    * dummy = self; cvReleaseHist               (& dummy); } }
61 %extend CvHaarClassifierCascade { ~CvHaarClassifierCascade () { CvHaarClassifierCascade * dummy = self; cvReleaseHaarClassifierCascade  (& dummy); } }
62 %extend CvPOSITObject  { ~CvPOSITObject  () { CvPOSITObject  * dummy = self; cvReleasePOSITObject        (& dummy); } }
63 %extend CvFeatureTree  { ~CvFeatureTree  () { CvFeatureTree  * dummy = self; cvReleaseFeatureTree        (& dummy); } }
64 %extend CvLSH          { ~CvLSH          () { CvLSH          * dummy = self; cvReleaseLSH                (& dummy); } }
65
66 // string operators for some OpenCV types
67
68 %extend CvScalar
69 {
70         const char * __str__(){
71                 static char str[256];
72                 snprintf(str, 256, "[%f, %f, %f, %f]", self->val[0], self->val[1], self->val[2], self->val[3]);
73                 return str;
74         }
75         const char * __repr__(){
76                 static char str[256];
77                 snprintf(str, 256, "cvScalar(%f, %f, %f, %f)", self->val[0], self->val[1], self->val[2], self->val[3]);
78                 return str;
79         }
80     const double __getitem__ (int index) {
81         if (index >= 4) {
82 #ifdef defined(SWIGPYTHON)
83             PyErr_SetString (PyExc_IndexError, "indice must be lower than 4");
84 #elif defined(SWIGOCTAVE)
85             error("indice must be lower than 4");
86 #endif
87             return 0;
88         }
89         if (index < -4) {
90 #ifdef defined(SWIGPYTHON)
91             PyErr_SetString (PyExc_IndexError, "indice must be bigger or egal to -4");
92 #elif defined(SWIGOCTAVE)
93             error("indice must be bigger or egal to -4");
94 #endif
95             return 0;
96         }
97         if (index < 0) {
98             /* negative index means from the end in python */
99             index = 4 - index;
100         }
101         return self->val [index];
102     }
103     void __setitem__ (int index, double value) {
104         if (index >= 4) {
105 #ifdef defined(SWIGPYTHON)
106             PyErr_SetString (PyExc_IndexError, "indice must be lower than 4");
107 #elif defined(SWIGOCTAVE)
108             error("indice must be lower than 4");
109 #endif
110             return;
111         }
112         if (index < -4) {
113 #ifdef defined(SWIGPYTHON)
114             PyErr_SetString (PyExc_IndexError, "indice must be bigger or egal to -4");
115 #elif defined(SWIGOCTAVE)
116             error("indice must be bigger or egal to -4");
117 #endif
118             return;
119         }
120         if (index < 0) {
121             /* negative index means from the end in python */
122             index = 4 - index;
123         }
124         self->val [index] = value;
125     }
126 };
127
128 %extend CvPoint2D32f
129 {
130         const char * __str__(){
131                 static char str[64];
132                 snprintf(str, 64, "[%f %f]", self->x, self->y);
133                 return str;
134         }
135         const char * __repr__(){
136                 static char str[64];
137                 snprintf(str, 64, "cvPoint2D32f(%f,%f)", self->x, self->y);
138                 return str;
139         }
140 };
141
142 %extend CvPoint
143 {
144         const char * __str__(){
145                 static char str[64];
146                 snprintf(str, 64, "[%d %d]", self->x, self->y);
147                 return str;
148         }
149         const char * __repr__(){
150                 static char str[64];
151                 snprintf(str, 64, "cvPoint(%d,%d)", self->x, self->y);
152                 return str;
153         }
154 };
155
156 // Set up CvMat to emulate IplImage fields
157 %{
158 int CvMat_cols_get(CvMat * m){
159         return m->cols;
160 }
161 void CvMat_cols_set(CvMat * m, int cols){
162     m->cols = cols;
163 }
164 int CvMat_rows_get(CvMat *m){
165         return m->rows;
166 }
167 void CvMat_rows_set(CvMat *m, int rows){
168     m->rows = rows;
169 }
170 int CvMat_width_get(CvMat * m){
171         return m->cols;
172 }
173 void CvMat_width_set(CvMat * m, int width){
174     m->cols = width;
175 }
176 int CvMat_height_get(CvMat *m){
177         return m->rows;
178 }
179 void CvMat_height_set(CvMat * m, int height){
180     m->rows = height;
181 }
182 int CvMat_depth_get(CvMat * m){
183         return cvIplDepth(m->type);
184 }
185 void CvMat_depth_set(CvMat *m, int depth){
186     cvError(CV_StsNotImplemented, "CvMat_depth_set", "Not Implemented", __FILE__, __LINE__);
187 }
188 int CvMat_nChannels_get(CvMat * m){
189         return CV_MAT_CN(m->type);
190 }
191 void CvMat_nChannels_set(CvMat *m, int nChannels){
192     int depth = CV_MAT_DEPTH(m->type);
193     m->type = CV_MAKETYPE(depth, nChannels);
194 }
195 int CvMat_origin_get(CvMat * m){
196     /* Always 0 - top-left origin */
197     return 0;
198 }
199 void CvMat_origin_set(CvMat * m, int origin){
200     cvError(CV_StsNotImplemented, "CvMat_origin_get", "IplImage is replaced by CvMat in Python, so its fields are read-only", __FILE__, __LINE__);
201 }
202 int CvMat_dataOrder_get(CvMat * m){
203     cvError(CV_StsNotImplemented, "CvMat_dataOrder_get", "Not Implemented", __FILE__, __LINE__);
204     return 0;
205 }
206 void CvMat_dataOrder_set(CvMat * m, int dataOrder){
207     cvError(CV_StsNotImplemented, "CvMat_dataOrder_get", "IplImage is replaced by CvMat in Python, so its fields are read-only", __FILE__, __LINE__);
208 }
209 int CvMat_imageSize_get(CvMat * m){
210         int step = m->step ? m->step : CV_ELEM_SIZE(m->type) * m->cols;
211         return step*m->rows;
212 }
213 void CvMat_imageSize_set(CvMat * m, int imageSize){
214     cvError(CV_StsNotImplemented, "CvMat_imageSize_set", "IplImage is not implemented in Python, so origin is read-only", __FILE__, __LINE__);
215 }
216 int CvMat_widthStep_get(CvMat * m){
217         return m->step;
218 }
219 void CvMat_widthStep_set(CvMat *m, int widthStep){
220     m->step = widthStep;
221 }
222 %}
223
224 %extend CvMat
225 {
226         int depth;
227         int nChannels;
228         int dataOrder;
229         int origin;
230         int width;
231         int height;
232         int imageSize;
233         int widthStep;
234         // swig doesn't like the embedded union in CvMat, so re-add these
235         int rows;
236         int cols;
237 };