Re: help with typemap

Robert Arkiletian <[email protected]> Fri, 27 Nov 2020 19:44:35 -0800
Newsgroups gmane.comp.programming.swig
Message-ID <CAN_+rpKcnJhEGD+t2gC8XFg64dDDRA-Sk8AKR0S+k50NkBjjhw@mail.gmail.com>
Sorry, the python test code error lines

pic= fltk.Fl_JPEG_Image(None, temp.getbuffer())
return Fl_JPEG_Image(None, temp.getvalue())

 don't match because I simplified the python code for the purposes of
this mailing list. Also, getvalue() and getbuffer() result in the same
error.

On Fri, Nov 27, 2020 at 7:12 PM Robert Arkiletian <[email protected]> wrote:
>
> 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())