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 i
c_long
is the same as c_int
, but that is not the problem.There are a number of issues:
numpy.zeros
is default float.TableCam
, A
is never modified.TableCam
, the 3rd parameter should be int* Array
and the elements modified by computing i*y+j
. That also agrees with argtypes
, which was correct.ctypes
type.Corrected code (on Windows, for my testing):
cam.c
#include <stdio.h>
__declspec(dllexport) void TableCam(const int x, const int y, int *Array)
{
int i,j;
for (i = 0; i < x; i++)
for (j = 0; j < y; j++)
Array[i*y+j] = 1;
}
camlib.py
import ctypes
_CamLib = ctypes.CDLL('cam')
_CamLib.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
_CamLib.restype = None
def TableCam(A):
x,y = A.shape
array = A.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
_CamLib.TableCam(x,y,array)
test.py
import camlib
import numpy as np
Anum = np.zeros((3,3),dtype=np.int)
print('Start:')
print(Anum)
camlib.TableCam(Anum)
print('Ended:')
print(Anum)
Start: [[0 0 0] [0 0 0] [0 0 0]] Ended: [[1 1 1] [1 1 1] [1 1 1]]