Get the output of CImg C++ code in python

To speed up my python code, I'm trying to insert CImg C++ code within it.

I'm able to read the python data (input variable, is a pointer to a numpy.array phi) in the CImg C++ code and even modified it. But I don't know how to get back the new modified variable (phi).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 -*- coding: utf-8 -*-
import os
import numpy
import hashlib
#from pycvf.core.errors import pycvf_warning
import scipy
from scipy.weave import converters
from scipy.weave import inline
import scipy.weave.inline_tools as inline_tools



def cimg_code(code,realcode="",prod=False):
    #if not prod:
      #pycvf_warning("once you finished developping, please add option 'prod=True' to avoid computing a md5 checksum at each call")
    base_install="/usr/"
    base_include=os.path.join(base_install,"include")
    cimg_info={'include_dirs': [base_include,
                              ]
    , 'library_dirs':[base_install+"/lib"]}
    def fct(**context):
      args=[      code+("" if prod else "/* hash : %s */"%(hashlib.md5(realcode).hexdigest(),)),
                  context.keys()
           ]

      print 'args***', args,'****', context.keys()
      kwargs=dict(
                  support_code="# 1 \"inlinecode\"\n"+realcode,
                  headers=[],
                  libraries=['X11'],
                  include_dirs=cimg_info['include_dirs'],
                  library_dirs=cimg_info['library_dirs'],
                  compiler='gcc',
                  #type_converters = converters.blitz
                 )

      assert(not context.has_key("__inlineargs__"))
      context["__inlineargs__"]=args
      #print 'context["__inlineargs__"]', context["__inlineargs__"]  
      context["__inlinekwargs__"]=kwargs
      #r= eval("inline(*__inlineargs__,**__inlinekwargs__)",globals(),context)
      r= eval("inline_tools.inline(*__inlineargs__,**__inlinekwargs__)",globals(),context)
      context["__inlineargs__"]=None
      print 'r****', r
      return r

    return fct


if __name__=="__main__":
  import time
  st=time.clock()

  phi = numpy.zeros((8, 10))
  phi2 = numpy.zeros((15, 5))
  phi3 = phi

  for i in range (8):
    for j in range (10):
      phi[i,j] = i -j; 

  for i in range (15):
    for j in range (5):
      phi2[i,j] = i -j;


  phi3 = cimg_code("do_test( a_array );",
"""
#include</workdir/er2/dekou/dekou.copie/roux/code.LSE/CImg-1.5.7/CImg.h> 
using namespace cimg_library;
using namespace std;



PyArrayObject*  do_test(PyArrayObject* npimg ) {

CImg<double> image(npimg->data,npimg->dimensions[0],npimg->dimensions[1],1,1);

//CImg<double> image2(npimg2->data,npimg2->dimensions[0],npimg2->dimensions[1],1,1);

cout<<image.width()<<"**"<<image.height()<<endl;
//cout<<image2.width()<<"**"<<image2.height()<<endl;
cout<<npimg<<endl;

for(int i= 0; i<image.width(); i++){
   for(int j= 0; j<image.height(); j++){

      image(i,j) = 0;
   }
}


npimg = (PyArrayObject*)image.data();
cout<<npimg<<endl;

return npimg;
}
""",False)(a=phi)


  print 'phi3', phi3
  print "done in ",time.clock()-st, "seconds";


Here is the output that I get:


python -u test2.py 
args*** ['do_test( a_array );/* hash : 497cccfb05cd4f76ecd16e3c2ec4dfd4 */', ['a']] **** ['a']
8**10
0x169ab60
0x19ae480
r**** None
phi3 None
done in  0.15 seconds


I am also trying to pass more arguments to the code without success, like in this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def checkin(phi):
   code = """

   using namespace std;  
   #include</workdir/er2/dekou/dekou.copie/roux/code.LSE/CImg-1.5.7/CImg.h>   

   cout<<"a***"<<a[2]<<"**"<<endl;
   /*
   for (int i=0; i <number; i++){
    out = out+ a[i];
    cout<<out<<endl;  
   }*/ 

   for (int i=0; i <number; i++){
     for (int j=0; j <number; j++){
       phi[j+i*number] = i-j;        
       //cout<<phi[j+i*number]<<endl; 
     }
   } 
   return_val =  phi; //value that is return
   //out = a.sum();
   """
   #return inline_tools.inline(code,['a', 'out', 'number'])
   return inline_tools.inline(code,['a', 'phi', 'number'])


Where ['a', 'out', 'number'] are the arguments.

Any idea?
Last edited on
Topic archived. No new replies allowed.