Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array allocation of SWIG C++ type from Python

I'm writing a python script for a program that has exposed its C++ API using SWIG. A SWIG exposed function has an interface like this:

void writePixelsRect(JoxColor* colors, int left, int top, int width, int height);

JoxColor is a POD struct looking like this:

struct JoxColor {
    float r, g, b, a;
};

I can easily create a single JoxColor in Python and invoke a call to writePixelsRect like this:

c = JoxApi.JoxColor()
c.r = r
c.g = g
c.b = b
c.a = a
JoxApi.writePixelsRect(c, x, y, 1, 1)

Repeatedly calling writePixelsRect with a 1x1 pixel rectangle is very slow so I want to create an array of JoxColor from python so I can write bigger rectangles at the time. Is this possible with SWIG types?

Note that I don't have access to the source code for the C++ library exposing JoxColor and writePixelsRect so I can't add a help function for this. I also don't want to introduce new C++ code in the system since it would force the users of my python script to compile the C++ code on whatever platform they are running. I do have access to ctypes in the python environment so if I could somehow typecast a float array created in ctypes to the type of JoxColor* for SWIG it would work for me.

like image 815
Laserallan Avatar asked Nov 05 '22 22:11

Laserallan


1 Answers

This is kinda tricky, but could you, at least for this part of the code, use a pure-ctypes solution? Basically manually look at the symbols exported by the shared libary file to find the name that the writePixelsRect function was exported as. C++ does name mangling, so while it might just be writePixelsRect if the library authors chose to make it extern "C", it might be something much messier, like _Z15writePixelsRectP8JoxColoriiii (that's how it was exported in a dummy C++ library I just created on my system).

On Linux, this command should tell you the symbol name:

nm libjox.so | grep writePixel | cut -d " " -f 3

Then, save that string and insert it into Python code kinda like this:

from ctypes import *

LIBRARY_NAME = 'libjox.so'
c = cdll.LoadLibrary(LIBRARY_NAME)

WIDTH = 20
HEIGHT = 20

class JoxColor(Structure):
    _fields_ = [("r", c_float), ("g", c_float), ("b", c_float), ("a", c_float)]

ColorBlock = JoxColor * (WIDTH * HEIGHT)

data_array = ColorBlock()

color = JoxColor(0, 0, 1, 0)
for i in range(WIDTH * HEIGHT):
    data_array[i] = color

c._Z15writePixelsRectP8JoxColoriiii(data_array, 0, 0, WIDTH, HEIGHT)

Assuming that _Z15writePixelsRectP8JoxColoriiii is the symbol that the function is accessible as in the shared library. Running this code just worked on my system with a dummy library like this:

#include <stdio.h>

struct JoxColor {
    float r, g, b, a;
};

void writePixelsRect(JoxColor *colors, int left, int top, int width, int height) {
    int p = 0;
    printf("size: %i, %i\n", width, height);
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            JoxColor color = colors[p];
            printf("pixel: %f, %f, %f, %f\n", color.r, color.g, color.b, color.a);
        }
    }
}

So I'm hopeful that it's not too far from working code in your environment.

like image 157
gsteff Avatar answered Nov 11 '22 04:11

gsteff