diff options
author | Benoit Bolsee <benoit.bolsee@online.be> | 2010-02-22 01:20:00 +0300 |
---|---|---|
committer | Benoit Bolsee <benoit.bolsee@online.be> | 2010-02-22 01:20:00 +0300 |
commit | 37b9c9fe4dc6a4adea419ce6b20b28dd60e24794 (patch) | |
tree | ec184b994766f3e5b3c7c757d37898a5cda9432b /source/gameengine/VideoTexture/ImageBase.h | |
parent | 115fc34dd382c76cf74c062db5741bcbb52fe988 (diff) |
VideoTexture: improvements to image data access API.
- Use BGL buffer instead of string for image data.
- Add buffer interface to image source.
- Allow customization of pixel format.
- Add valid property to check if the image data is available.
The image property of all Image source objects will now
return a BGL 'buffer' object. Previously it was returning
a string, which was not working at all with Python 3.1.
The BGL buffer type allows sequence access to bytes and
is directly usable in BGL OpenGL wrapper functions.
The buffer is formated as a 1 dimensional array of bytes
with 4 bytes per pixel in RGBA order.
BGL buffers will also be accepted in the ImageBuff load()
and plot() functions.
It is possible to customize the pixel format by using
the VideoTexture.imageToArray(image, mode) function:
the first argument is a Image source object, the second
optional argument is a format string using the R, G, B,
A, 0 and 1 characters. For example "BGR" means that each
pixel will be 3 bytes, corresponding to the Blue, Green
and Red channel in that order. Use 0 for a fixed hex 00
value, 1 for hex FF. The default mode is "RGBA".
All Image source objects now support the buffer interface
which allows to create memoryview objects for direct access
to the image internal buffer without memory copy. The buffer
format is one dimensional array of bytes with 4 bytes per
pixel in RGBA order. The buffer is writable, which allows
custom modifications of the image data.
v = memoryview(source)
A bug in the Python 3.1 buffer API will cause a crash if
the memoryview object cannot be created. Therefore, you
must always check first that an image data is available
before creating a memoryview object. Use the new valid
attribute for that:
if source.valid:
v = memoryview(source)
...
Note: the BGL buffer object itself does not yet support
the buffer interface.
Note: the valid attribute makes sense only if you use
image source in conjunction with texture object like this:
# refresh texture but keep image data in memory
texture.refresh(False)
if texture.source.valid:
v = memoryview(texture.source)
# process image
...
# invalidate image for next texture refresh
texture.source.refresh()
Limitation: While memoryview objects exist, the image cannot be
resized. Resizing occurs with ImageViewport objects when the
viewport size is changed or with ImageFFmpeg when a new image
is reloaded for example. Any attempt to resize will cause a
runtime error. Delete the memoryview objects is you want to
resize an image source object.
Diffstat (limited to 'source/gameengine/VideoTexture/ImageBase.h')
-rw-r--r-- | source/gameengine/VideoTexture/ImageBase.h | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/source/gameengine/VideoTexture/ImageBase.h b/source/gameengine/VideoTexture/ImageBase.h index 70b1929b91d..43a56290bee 100644 --- a/source/gameengine/VideoTexture/ImageBase.h +++ b/source/gameengine/VideoTexture/ImageBase.h @@ -53,6 +53,9 @@ public: /// release contained objects, if returns true, object should be deleted virtual bool release (void); + /// is an image available + bool isImageAvailable(void) + { return m_avail; } /// get image unsigned int * getImage (unsigned int texId = 0, double timestamp=-1.0); /// get image size @@ -85,6 +88,9 @@ public: /// calculate size (nearest power of 2) static short calcSize (short size); + /// number of buffer pointing to m_image, public because not handled by this class + int m_exports; + protected: /// image buffer unsigned int * m_image; @@ -295,8 +301,6 @@ private: ImageSource (void) {} }; - - // list of python image types extern PyTypeList pyImageTypes; @@ -320,7 +324,7 @@ PyObject * Image_allocNew (PyTypeObject * type, PyObject * args, PyObject * kwds void Image_dealloc (PyImage * self); // get image data -PyObject * Image_getImage (PyImage * self, void * closure); +PyObject * Image_getImage (PyImage * self, char * mode); // get image size PyObject * Image_getSize (PyImage * self, void * closure); // refresh image - invalidate current content @@ -344,6 +348,9 @@ PyObject * Image_setSource (PyImage * self, PyObject * args); PyObject * Image_getFilter (PyImage * self, void * closure); // set pixel filter object int Image_setFilter (PyImage * self, PyObject * value, void * closure); - +// check if a buffer can be extracted +PyObject * Image_valid(PyImage * self, void * closure); +// for buffer access to PyImage objects +extern PyBufferProcs imageBufferProcs; #endif |