vendredi 31 mars 2017

Python 2D array i C using ctypes

i'm trying to use a self written c lib to proces 2d array from python but with little success.

Here is my c code:

CamLibC.c

int TableCam(const int x, const int y, int **Array) {
    int i = 0;
    int j = 0;

    for (i; i < x; i++) {
        for (j; j < y; j++) {
            Array[i][j] = 1;
        };

    };
}

cc -nostartfiles -shared -fPIC -o CamLibOS.os CamLibC.c

Now here is my python wrapper:

CamLibPy.py

import os,sys
import ctypes

dirname     = os.path.dirname(os.path.realpath(sys.argv[0]))
CamLibFile  = dirname + '/CamLibOS.os'

_CamLib = ctypes.CDLL(CamLibFile)
_CamLib.TableCam.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int)]

def TableCam (A) :
    global _CamLib

    x = len(A)
    y = len(A[0])

    print('x: ', x, ' y: ', y);

    arrayType = ((ctypes.c_int * x) * y)
    array = arrayType()

    _CamLib.TableCam(ctypes.c_int(x), ctypes.c_int(y), array)

    print(array)

And my python code where i use the function:

Test.py

import CamLibPy
from numpy import zeros

Anum = zeros((3,3))
print('Start: ', Anum)

CamLibPy.TableCam(Anum)

print('Ended: ', Anum)

In this test program i try to change all the zeros in the array to ones. but as soon as i try to run this is get the following output:

Start: [[ 0. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]]

x: 3 y: 3

Traceback (most recent call last): File "/media/pi/USB DISK/Test/Test.py", line 7, in CamLibPy.TableCam(Anum) File "/media/pi/USB DISK/Test/CamLibPy.py", line 21, in TableCam _CamLib.TableCam(ctypes.c_int(x), ctypes.c_int(y), array) ctypes.ArgumentError: argument 3: : expected LP_c_long instance instead of c_long_Array_3_Array_3

it's saying it expected a c_long but i clearly used c_int to make the arrayType

Can somebody tell me what i did wrong?

Aucun commentaire:

Enregistrer un commentaire