Move the sources to trunk
[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
46
47 %extend IplImage       { ~IplImage       () { IplImage       * dummy = self; cvReleaseImage              (& dummy); } }
48 %extend CvMat          { ~CvMat          () { CvMat          * dummy = self; cvReleaseMat                (& dummy); } }
49 %extend CvMatND        { ~CvMatND        () { CvMatND        * dummy = self; cvReleaseMatND              (& dummy); } }
50 %extend CvSparseMat    { ~CvSparseMat    () { CvSparseMat    * dummy = self; cvReleaseSparseMat          (& dummy); } }
51 %extend CvMemStorage   { ~CvMemStorage   () { CvMemStorage   * dummy = self; cvReleaseMemStorage         (& dummy); } }
52 %extend CvGraphScanner { ~CvGraphScanner () { CvGraphScanner * dummy = self; cvReleaseGraphScanner       (& dummy); } }
53 %extend CvFileStorage  { ~CvFileStorage  () { CvFileStorage  * dummy = self; cvReleaseFileStorage        (& dummy); } }
54 %extend IplConvKernel  { ~IplConvKernel  () { IplConvKernel  * dummy = self; cvReleaseStructuringElement (& dummy); } }
55 %extend CvKalman       { ~CvKalman       () { CvKalman       * dummy = self; cvReleaseKalman             (& dummy); } }
56 %extend CvConDensation { ~CvConDensation () { CvConDensation * dummy = self; cvReleaseConDensation       (& dummy); } }
57 %extend CvHistogram    { ~CvHistogram    () { CvHistogram    * dummy = self; cvReleaseHist               (& dummy); } }
58 %extend CvHaarClassifierCascade { ~CvHaarClassifierCascade () { CvHaarClassifierCascade * dummy = self; cvReleaseHaarClassifierCascade  (& dummy); } }
59 %extend CvPOSITObject  { ~CvPOSITObject  () { CvPOSITObject  * dummy = self; cvReleasePOSITObject        (& dummy); } }
60
61 // string operators for some OpenCV types
62
63 %extend CvScalar
64 {
65         const char * __str__(){
66                 static char str[256];
67                 snprintf(str, 256, "[%f, %f, %f, %f]", self->val[0], self->val[1], self->val[2], self->val[3]);
68                 return str;
69         }
70         const char * __repr__(){
71                 static char str[256];
72                 snprintf(str, 256, "cvScalar(%f, %f, %f, %f)", self->val[0], self->val[1], self->val[2], self->val[3]);
73                 return str;
74         }
75     const double __getitem__ (int index) {
76         if (index >= 4) {
77             PyErr_SetString (PyExc_IndexError, "indice must be lower than 4");
78             return 0;
79         }
80         if (index < -4) {
81             PyErr_SetString (PyExc_IndexError, "indice must be bigger or egal to -4");
82             return 0;
83         }
84         if (index < 0) {
85             /* negative index means from the end in python */
86             index = 4 - index;
87         }
88         return self->val [index];
89     }
90     void __setitem__ (int index, double value) {
91         if (index >= 4) {
92             PyErr_SetString (PyExc_IndexError, "indice must be lower than 4");
93             return;
94         }
95         if (index < -4) {
96             PyErr_SetString (PyExc_IndexError, "indice must be bigger or egal to -4");
97             return;
98         }
99         if (index < 0) {
100             /* negative index means from the end in python */
101             index = 4 - index;
102         }
103         self->val [index] = value;
104     }
105 };
106
107 %extend CvPoint2D32f
108 {
109         const char * __str__(){
110                 static char str[64];
111                 snprintf(str, 64, "[%f %f]", self->x, self->y);
112                 return str;
113         }
114         const char * __repr__(){
115                 static char str[64];
116                 snprintf(str, 64, "cvPoint2D32f(%f,%f)", self->x, self->y);
117                 return str;
118         }
119 };
120
121 %extend CvPoint
122 {
123         const char * __str__(){
124                 static char str[64];
125                 snprintf(str, 64, "[%d %d]", self->x, self->y);
126                 return str;
127         }
128         const char * __repr__(){
129                 static char str[64];
130                 snprintf(str, 64, "cvPoint(%d,%d)", self->x, self->y);
131                 return str;
132         }
133 };
134
135 // Set up CvMat to emulate IplImage fields
136 %{
137 int CvMat_cols_get(CvMat * m){
138         return m->cols;
139 }
140 int CvMat_rows_get(CvMat *m){
141         return m->rows;
142 }
143 int CvMat_width_get(CvMat * m){
144         return m->cols;
145 }
146 int CvMat_height_get(CvMat *m){
147         return m->rows;
148 }
149 int CvMat_depth_get(CvMat * m){
150         return cvCvToIplDepth(m->type);
151 }
152 int CvMat_nChannels_get(CvMat * m){
153         return CV_MAT_CN(m->type);
154 }
155 int CvMat_origin_get(CvMat * m){
156         return 0;
157 }
158 int CvMat_dataOrder_get(CvMat * m){
159         return 0;
160 }
161 int CvMat_imageSize_get(CvMat * m){
162         return m->step*m->rows;
163 }
164 int CvMat_widthStep_get(CvMat * m){
165         return m->step;
166 }
167 %}
168 %extend CvMat
169 {
170         const int depth;
171         const int nChannels;
172         const int dataOrder;
173         const int origin;
174         const int width;
175         const int height;
176         const int imageSize;
177         const int widthStep;
178         // swig doesn't like the embedded union in CvMat, so re-add these
179         const int rows;
180         const int cols;
181 };