Пример #1
0
 def register_decl(self, pyName, pds, cChildName_pds=None, pyEquivName=None):
     upds = _c.unique_pds(pds)
     if upds in self.decls_reg:
         print "Declaration %s already registered." % pds
         return upds
     if '::' in pds: # assume it is a class
         print "Registration: %s ==> %s..." % (upds, pyName)
         try:
             self.find_class(upds).rename(pyName)
         except RuntimeError:
             print "Class %s does not exist." % pds
             # pass
     self.decls_reg[upds] = (pyName, _c.unique_pds(cChildName_pds), pyEquivName)
     return upds
Пример #2
0
 def get_decl_equivname(self, pds):
     z = self.decls_reg[_c.unique_pds(pds)]
     if z[2] is not None:
         return z[2]
     if z[1] is not None:
         return "list of "+get_decl_equivname(z[1])
     return z[0]
Пример #3
0
 def register_vec(self, cName_pds, cChildName_pds, pyName=None, pds=None, pyEquivName=None, excluded=False):
     cupds = _c.unique_pds(cChildName_pds)
     if pyName is None:
         pyName = cName_pds[cName_pds.rfind(':')+1:] + '_' + self.decls_reg[cupds][0]
     if pds is None:
         pds = cName_pds + '< ' + cChildName_pds + ' >'
     if excluded:
         try:
             z = self.find_class(pds)
             z.set_already_exposed(True)
             z.exclude()
         except RuntimeError:
             pass
     return self.register_decl(pyName, pds, cupds, pyEquivName)
Пример #4
0
def beautify_memvars(klass):
    try:
        zz = klass.vars()
    except RuntimeError:
        zz = []
    
    for z in [z for z in zz if not z.ignore and z.access_type=='public']:
        pds = common.unique_pds(z.type.partial_decl_string)
        if pds=='CvMemStorage *':
            expose_member_as_MemStorage(klass, z.name)
        elif pds in ['CvMat *', 'CvArr *']:
            expose_member_as_Mat(klass, z.name, True, 1)
        elif pds in ['CvMat const *', 'CvArr const *']:
            expose_member_as_Mat(klass, z.name, True, 0)
        elif pds=='IplImage *':
            expose_member_as_Mat(klass, z.name, False, 1)
        elif pds=='IplImage const *':
            expose_member_as_Mat(klass, z.name, False, 0)
        elif pds in common.c2cpp:
            expose_member_as_FixType(common.c2cpp[pds], klass, z.name)
        elif pds=='CvSeq *' or pds=='CvSet *':
            expose_member_as_pointee(klass, z.name)
Пример #5
0
def beautify_func_list(func_list):
    func_list = [f for f in func_list if not f.ignore]

    # fix default values
    # don't remove std::vector default values, old compilers _need_ std::allocator removed
    for f in func_list:
        for arg in f.arguments:
            if isinstance(arg.default_value, str):
                repl_list = {
                    'std::basic_string<char, std::char_traits<char>, std::allocator<char> >': 'std::string',
                    'std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >': 'std::vector<cv::Point>',
                    'std::vector<cv::Scalar_<double>, std::allocator<cv::Scalar_<double> > >': 'std::vector<cv::Scalar>',
                    'std::vector<int, std::allocator<int> >': 'std::vector<int>',
                    'std::vector<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >': 'std::vector<cv::Vec4i>',
                }
                for z in repl_list:
                    arg.default_value = arg.default_value.replace(z, repl_list[z])

    # one-to-one function argument
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            pds = _c.unique_pds(arg.type.partial_decl_string)
            if pds in _c.c2cpp:
                f._transformer_creators.append(_FT.input_as_FixType(pds, _c.c2cpp[pds], arg.name))
            elif pds in ['CvRNG *', 'CvRNG &', 'CvRNG cosnt *', 'CvRNG const &']:
                f._transformer_creators.append(_FT.input_asRNG(arg.name))
            elif pds in ['CvFileStorage *', 'CvFileStorage const *']:
                f._transformer_creators.append(_FT.input_as_FileStorage(arg.name))
            elif pds in ['CvFileNode *', 'CvFileNode const *']:
                f._transformer_creators.append(_FT.input_as_FileNode(arg.name))
            elif pds in ['CvMemStorage *', 'CvMemStorage const *']:
                f._transformer_creators.append(_FT.input_as_MemStorage(arg.name))
            elif pds in ['CvSparseMat *', 'CvSparseMat &', 'CvSparseMat const *', 'CvSparseMat const &']:
                f._transformer_creators.append(_FT.input_asSparseMat(arg.name))
            elif pds in ["IplImage *", "IplImage const *", "CvArr *", "CvArr const *",
                "CvMat *", "CvMat const *", "cv::Range const *"]:
                f._transformer_creators.append(_FT.input_as_Mat(arg.name))

    # function argument int *sizes and int dims
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'sizes' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == 'dims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('sizes', 'dims'))
                        break
            if arg.name == '_sizes' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == '_ndims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('_sizes', '_ndims'))
                        break
                    if arg2.name == 'dims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('_sizes', 'dims'))
                        break
            if arg.name == '_newsz' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == '_newndims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('_newsz', '_newndims'))
                        break

    # function argument const CvPoint2D32f * src and const CvPoint2D32f * dst
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'src' and _D.is_pointer(arg.type) and 'CvPoint2D32f' in arg.type.decl_string:
                for arg2 in f.arguments:
                    if arg2.name == 'dst' and _D.is_pointer(arg2.type) and 'CvPoint2D32f' in arg2.type.decl_string:
                        f._transformer_creators.append(_FT.input_array1d('src'))
                        f._transformer_creators.append(_FT.input_array1d('dst'))
                        break

    #  argument 'void *data'
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'data' and _D.is_void_pointer(arg.type):
                f._transformer_creators.append(_FT.input_string(arg.name))
                self.add_doc(f.name, "'data' is represented by a string")

    # final step: apply all the function transformations
    for f in func_list:
        if len(f._transformer_creators) > 0:
            sort_transformers(f)

            f.add_transformation(*f._transformer_creators, **f._transformer_kwds)
            if 'unique_function_name' in f._transformer_kwds:
                f.transformations[0].unique_name = f._transformer_kwds['unique_function_name']
            else:
                s = f.transformations[0].unique_name
                repl_dict = {
                    'operator()': '__call__',
                }
                for t in repl_dict:
                    if t in s:
                        s = s.replace(t, repl_dict[t])
                        f.transformations[0].unique_name = s
                        f.transformations[0].alias = repl_dict[t]
                        break

        _c.add_decl_desc(f)
Пример #6
0
    def __init__(self, module_name, include_paths=[], number_of_files=1):
        self.module_name = module_name
        self.number_of_files = number_of_files
        _c.current_sb = self

        self.mb = None
        self.cc = None
        self.funs = None

        self.dummy_struct = None
        self.decls_reg = {}

        # package directory
        self.pkg_dir = _op.join(_op.split(_op.abspath(__file__))[0], '..', 'src', 'package')

        # create an instance of class that will help you to expose your declarations
        self.mb = _pp.module_builder.module_builder_t([module_name+"_wrapper.hpp"],
            gccxml_path=r"M:/utils/gccxml/bin/gccxml.exe",
            include_paths=include_paths+[
                _op.join(self.pkg_dir, "extras", "core"),
                _op.join(self.pkg_dir, "extras", "sdopencv"),
                _op.join(self.pkg_dir, module_name+"_ext"),
                r"M:\programming\builders\MinGW\gcc\gcc-4.4.0-mingw\lib\gcc\mingw32\4.4.0\include\c++",
                r"M:\programming\builders\MinGW\gcc\gcc-4.4.0-mingw\lib\gcc\mingw32\4.4.0\include\c++\mingw32",
                r"M:\programming\builders\MinGW\gcc\gcc-4.4.0-mingw\lib\gcc\mingw32\4.4.0\include",
            ])

        # create a Python file
        self.cc = open(_op.join(self.pkg_dir, 'pyopencv', self.module_name+'.py'), 'w')
        self.cc.write('''#!/usr/bin/env python
# PyOpenCV - A Python wrapper for OpenCV 2.x using Boost.Python and NumPy

# Copyright (c) 2009, Minh-Tri Pham
# All rights reserved.

# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

#    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#    * Neither the name of pyopencv's copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# For further inquiries, please contact Minh-Tri Pham at [email protected]
# ----------------------------------------------------------------------------

import common as _c
import MODULE_NAME_ext as _ext
from MODULE_NAME_ext import *
        '''.replace("MODULE_NAME", module_name))

        # Well, don't you want to see what is going on?
        # self.mb.print_declarations() -- too many declarations

        # Disable every declarations first
        self.mb.decls().exclude()

        # disable some warnings
        # self.mb.decls().disable_warnings(messages.W1027, messages.W1025)

        # expose 'this'
        try:
            self.mb.classes().expose_this = True
        except RuntimeError:
            pass

        # expose all enumerations
        # try:
            # self.mb.enums().include()
        # except RuntimeError:
            # pass

        # except some weird enums
        # for z in ('_', 'VARENUM', 'GUARANTEE', 'NLS_FUNCTION', 'POWER_ACTION',
            # 'PROPSETFLAG', 'PROXY_PHASE', 'PROXY_PHASE', 'SYS', 'XLAT_SIDE',
            # 'STUB_PHASE',
            # ):
            # try:
                # self.mb.enums(lambda x: x.name.startswith(z)).exclude()
            # except RuntimeError:
                # pass
        # for z in ('::std', '::tag'):
            # try:
                # self.mb.enums(lambda x: x.decl_string.startswith(z)).exclude()
            # except RuntimeError:
                # pass

        # add 'pds' attribute to every class
        for z in self.mb.classes():
            z.pds = _c.unique_pds(z.partial_decl_string)
            if z.name.startswith('vector'):
                z.set_already_exposed(True)

        # dummy struct
        z = self.mb.class_(module_name+"_dummy_struct")
        self.dummy_struct = z
        z.include()
        z.decls().exclude()
        z.class_('dummy_struct2').include()
        z.rename("__"+z.name)
        z._reg_code = ""

        # turn on 'most' of the constants
        for z in ('IPL_', 'CV_'):
            try:
                self.mb.decls(lambda decl: decl.name.startswith(z)).include()
            except RuntimeError:
                pass

        # initialise the list of free functions
        try:
            self.funs = self.mb.free_funs()
        except RuntimeError:
            self.funs = []
        _c.init_transformers(self.funs)
        
        # make sure size_t is still size_t -- for 64-bit support
        z = self.mb.decl('size_t')
        z.type = _FT.size_t_t()
Пример #7
0
 def register_ti(self, cName_pds, cElemNames_pds=[], pyName=None, pds=None):
     if pyName is None:
         pyName = cName_pds[cName_pds.rfind(':')+1:]
         for elem in cElemNames_pds:
             pyName += '_' + (str(elem) if isinstance(elem, int) else self.decls_reg[_c.unique_pds(elem)][0])
     if pds is None:
         pds = cName_pds
         if len(cElemNames_pds)>0:
             pds += '< '
             for elem in cElemNames_pds:
                 pds += (str(elem) if isinstance(elem, int) else elem) + ', '
             pds = pds[:-2] + ' >'
     return self.register_decl(pyName, pds)
Пример #8
0
 def find_class(self, pds):
     pds = _c.unique_pds(pds)
     return self.mb.class_(lambda x: x.pds==pds)
Пример #9
0
 def get_registered_decl_name(self, pds):
     upds = _c.unique_pds(pds)
     try:
         return self.decls_reg[upds][0]
     except KeyError:
         return "(C++)"+upds
Пример #10
0
 def get_registered_decl(self, pds):
     upds = _c.unique_pds(pds)
     try:
         return self.decls_reg[upds]
     except KeyError:
         raise ValueError("Class of pds '%s' has not been registered." % pds)