help with typemap
Robert Arkiletian <[email protected]> Fri, 27 Nov 2020 19:12:13 -0800
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CAN_+rp+WtNYKh7ssrJARepk7P469sM9VdzqXu6KZ_nMd8R_+Rw@mail.gmail.com> |
Hi, I'm trying to add support to read a JPEG from memory from a C++
library (FLTK) to a python wrapper library (pyFltk) using swig 4.0.1
in Ubuntu 20.04
Here is my typemap in Fl_JPEG_Image.i
----------------------------------------------------------------
%{
#include "FL/Fl_JPEG_Image.H"
%}
%typemap(in) const unsigned char *buffer {
const void *buffer;
Py_buffer view;
int failure = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO);
if (!failure) {
buffer = view.buf;
PyBuffer_Release(&view);
$1 = (uchar *)buffer;
}
else {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError,"does not support
single-segment readable buffer interface");
return NULL;
}
}
%include "FL/Fl_JPEG_Image.H"
----------------------------------------------------------------
Here is the header for the C++ library:
Parameters for ctor of JPEG image from memory
name: A unique name or NULL
data: A pointer to the memory location of the JPEG image
class FL_EXPORT Fl_JPEG_Image : public Fl_RGB_Image {
public:
Fl_JPEG_Image(const char *filename);
Fl_JPEG_Image(const char *name, const unsigned char *data);
};
----------------------------------------------------------------
I keep getting this error
Traceback (most recent call last):
File "image_pil_jpg.py", line 17, in <module>
pic = jpg_resize('cat.jpg', 300)
File "image_pil_jpg.py", line 15, in jpg_resize
return Fl_JPEG_Image(None, temp.getvalue())
File "/usr/local/lib/python3.8/dist-packages/pyFltk-1.3.5-py3.8-linux-x86_64.egg/fltk/fltk.py",
line 8732, in __init__
_fltk.Fl_JPEG_Image_swiginit(self, _fltk.new_Fl_JPEG_Image(_self, *args))
TypeError: in method 'new_Fl_JPEG_Image', argument 3 of type 'unsigned
char const *'
Additional information:
Wrong number or type of arguments for overloaded function 'new_Fl_JPEG_Image'.
Possible C/C++ prototypes are:
Fl_JPEG_Image::Fl_JPEG_Image(char const *)
Fl_JPEG_Image::Fl_JPEG_Image(PyObject *,char const *,unsigned char const *)
----------------------------------------------------------------
Here is the python test code
import fltk
from PIL import Image
import io
img = Image.open('cat.jpg')
temp = io.BytesIO() #byte stream memory object
img.save(temp,format="JPEG")
pic= fltk.Fl_JPEG_Image(None, temp.getbuffer())