26b7085794448bbe061322bbd216e52da55c740a
[opencv] / utils / cvarr_operators.py
1 #!/usr/bin/python
2 BINARY_OPERATORS={
3     '+':'cvAdd',
4     '-':'cvSub', 
5     '/':'cvDiv', 
6     '*':'cvMul',
7     '^':'cvXor',
8     '&':'cvAnd',
9     '|':'cvOr',
10 }
11 SCALAR_OPERATORS={
12     '+':'cvAddS',
13     '-':'cvSubS', 
14     '&':'cvAndS',
15     '|':'cvOrS',
16     '^':'cvXorS',
17 }
18 SCALE_OPERATORS={
19     '*':'val',
20     '/':'1.0/val'
21 }
22 CMP_OPERATORS={
23     '==':'CV_CMP_EQ',
24     '!=':'CV_CMP_NE',
25     '>=':'CV_CMP_GE',
26     '>':'CV_CMP_GT',
27     '<=':'CV_CMP_LE',
28     '<':'CV_CMP_LT',
29 }
30 ARR={
31     'CvMat':'cvCreateMat(self->rows, self->cols, self->type)',
32     'IplImage':'cvCreateImage(cvGetSize(self), self->depth, self->nChannels)'
33 }
34 CMP_ARR={
35     'CvMat':'cvCreateMat(self->rows, self->cols, CV_8U)',
36     'IplImage':'cvCreateImage(cvGetSize(self), IPL_DEPTH_8U, 1)' 
37 }
38
39 def scalar_scale_operator(arr, op, arg):
40     print '\t%s * operator %s (double val){' % (arr, op)
41     print '\t\t%s * res = %s;' % (arr, ARR[arr] )
42     print '\t\tcvScale(self, res, %s);' % arg
43     print '\t\treturn res;'
44     print '\t}'
45     print '\t%s * operator %s (double val){' % (arr, op)
46     print '\t\t%s * res = %s;' % (arr, ARR[arr] )
47     print '\t\tcvScale(self, res, %s);' % arg
48     print '\t\treturn res;'
49     print '\t}'
50
51 print "/** This file was automatically generated using util/cvarr_operators.py script */"
52
53 for arr in ARR:
54     print '%%extend %s {' % arr
55     for op in BINARY_OPERATORS:
56         print '\t%%newobject operator %s;' % (op)
57         print '\t%s * operator %s (CvArr * src){' % ( arr, op )
58         print '\t\t%s * res = %s;' % ( arr, ARR[arr] )
59         print '\t\t%s(self, src, res);' % ( BINARY_OPERATORS[op] )
60         print '\t\treturn res;'
61         print '\t}'
62         print '\t%s * operator %s= (CvArr * src){' % ( arr, op )
63         print '\t\t%s(self, src, self);' % ( BINARY_OPERATORS[op] )
64         print '\t\treturn self;'
65         print '\t}'
66     for op in SCALAR_OPERATORS:
67         print '\t%%newobject operator %s;' % (op)
68         print '\t%s * operator %s (CvScalar val){' % ( arr, op )
69         print '\t\t%s * res = %s;' % ( arr, ARR[arr] )
70         print '\t\t%s(self, val, res);' % ( SCALAR_OPERATORS[op] )
71         print '\t\treturn res;'
72         print '\t}'
73         print '\t%s * operator %s= (CvScalar val){' % ( arr, op )
74         print '\t\t%s(self, val, self);' % ( SCALAR_OPERATORS[op] )
75         print '\t\treturn self;'
76         print '\t}'
77     for op in CMP_OPERATORS:
78         print '\t%%newobject operator %s;' % (op)
79         print '\t%s * operator %s (CvArr * src){' % ( arr, op )
80         print '\t\t%s * res = %s;' % ( arr, CMP_ARR[arr] )
81         print '\t\tcvCmp(self, src, res, %s);' % ( CMP_OPERATORS[op] )
82         print '\t\treturn res;'
83         print '\t}'
84         print '\t%s * operator %s (double val){' % ( arr, op )
85         print '\t\t%s * res = %s;' % ( arr, CMP_ARR[arr] )
86         print '\t\tcvCmpS(self, val, res, %s);' % ( CMP_OPERATORS[op] )
87         print '\t\treturn res;'
88         print '\t}'
89
90     for op in SCALE_OPERATORS:
91         print '\t%%newobject operator %s;' % (op)
92         print '\t%s * operator %s (double val){' % (arr, op)
93         print '\t\t%s * res = %s;' % (arr, ARR[arr] )
94         print '\t\tcvScale(self, res, %s);' % SCALE_OPERATORS[op]
95         print '\t\treturn res;'
96         print '\t}'
97         print '\t%s * operator %s= (double val){' % (arr, op)
98         print '\t\tcvScale(self, self, %s);' % SCALE_OPERATORS[op]
99         print '\t\treturn self;'
100         print '\t}'
101
102
103     print '} /* extend %s */\n' % arr