| 1 |
/* File : Fl_Image.i */ |
| 2 |
//%module Fl_Image |
| 3 |
|
| 4 |
%feature("docstring") ::Fl_Image |
| 5 |
""" |
| 6 |
Fl_Image is the base class used for caching and drawing all kinds of images |
| 7 |
in FLTK. This class keeps track of common image data such as the pixels, |
| 8 |
colormap, width, height, and depth. Virtual methods are used to provide |
| 9 |
type-specific image handling. |
| 10 |
|
| 11 |
Since the Fl_Image class does not support image drawing by itself, calling |
| 12 |
the draw() method results in a box with an X in it being drawn instead. |
| 13 |
""" ; |
| 14 |
|
| 15 |
%{ |
| 16 |
#include "FL/Fl_Image.H" |
| 17 |
%} |
| 18 |
|
| 19 |
//%include "macros.i" |
| 20 |
//CHANGE_OWNERSHIP(Fl_Image) |
| 21 |
//CHANGE_OWNERSHIP(Fl_RGB_Image) |
| 22 |
|
| 23 |
|
| 24 |
%typemap(in) const uchar *bits { |
| 25 |
/* Check if the input support the buffer protocol */ |
| 26 |
Py_buffer view; |
| 27 |
const void * buffer; |
| 28 |
int failure = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO); |
| 29 |
if (!failure) { |
| 30 |
// work with array object |
| 31 |
buffer = view.buf; |
| 32 |
PyBuffer_Release(&view); |
| 33 |
$1 = (uchar *) buffer; |
| 34 |
} else { |
| 35 |
// work with list object |
| 36 |
// clear the error from PyObject_GetBuffer |
| 37 |
PyErr_Clear(); |
| 38 |
buffer=0; |
| 39 |
/* Check if is a list */ |
| 40 |
if (PyList_Check($input)) { |
| 41 |
Py_ssize_t size = PyList_Size($input); |
| 42 |
int i = 0; |
| 43 |
$1 = (uchar *) malloc((size+1)*sizeof(char)); |
| 44 |
for (i = 0; i < size; i++) { |
| 45 |
PyObject *o = PyList_GetItem($input,i); |
| 46 |
if (PyInt_Check(o)) |
| 47 |
$1[i] = (uchar)PyInt_AsLong(o); |
| 48 |
else { |
| 49 |
PyErr_SetString(PyExc_TypeError,"list must contain ints"); |
| 50 |
free($1); |
| 51 |
return NULL; |
| 52 |
} |
| 53 |
} |
| 54 |
$1[i] = 0; |
| 55 |
} else { |
| 56 |
PyErr_SetString(PyExc_TypeError,"not a list or does not support single-segment readable buffer interface"); |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// Typemap for converting const char *const * (image data) to Python bytes |
| 63 |
%typemap(out) const char *const * { |
| 64 |
int height = arg1->data_h(); |
| 65 |
int width = arg1->data_w(); // Get the image width |
| 66 |
int depth = arg1->d(); // Get the image depth (e.g., 3 for RGB) |
| 67 |
int size = width * height * depth; |
| 68 |
|
| 69 |
const char *const *img_data = $1; // Access the image data |
| 70 |
|
| 71 |
// Create a bytes object |
| 72 |
PyObject *pyBytes = PyBytes_FromStringAndSize(NULL, size); |
| 73 |
|
| 74 |
if (img_data != NULL) { |
| 75 |
// Copy the data into the bytes object |
| 76 |
memcpy(PyBytes_AsString(pyBytes), img_data[0], size); |
| 77 |
} |
| 78 |
|
| 79 |
$result = pyBytes; // Set the output result to the new bytes object |
| 80 |
} |
| 81 |
|
| 82 |
%ignore Fl_RGB_Image::id; |
| 83 |
%ignore Fl_RGB_Image::mask; |
| 84 |
|
| 85 |
%newobject Fl_Image::copy; |
| 86 |
|
| 87 |
%include "FL/Fl_Image.H" |
| 88 |
|
| 89 |
|
| 90 |
|