Emacs supports creating image objects called “canvas” which have a pixel buffer associated with them. These image objects can be accessed and updated natively via dynamic modules. See Canvas Images, for what they are.
The following functions allow you to operate on canvas image objects with dynamic modules:
uint32_t *canvas_data (emacs_env *env, emacs_value canvas) ¶The function gives access to the pixel buffer of canvas. The
pixel buffer is in row-major order with a size widthxheight.
The pixel format is ARGB32 on all platforms. Return NULL in case
of error.
Note that the pixel buffer is only valid as long as the canvas object is alive and its dimensions have not been changed. If it has changed, Emacs will create a new pixel buffer and automatically resize the canvas.
Once we update the canvas pixel buffer via dynamic modules, we also have
to call canvas-refresh for Emacs to redisplay the canvas. This
can be done from the module via an Elisp funcall.
/* Access the pixel buffer */
uint32_t *pixel = env->canvas_data (env, canvas);
/* Write to the pixel buffer */
...
/* Redraw the canvas */
env->funcall(env, env->intern(env, "canvas-refresh"), 2,
(emacs_value[]){canvas, Qnil});
Note that canvas-refresh has an optional boolean argument to
reload the :data vector or string from the canvas image
specification. When called from dynamic modules, the argument should
usually be Qnil.
Thus, one can simply define a module function that takes as an argument a canvas image object, following the specification as laid down in Canvas Images, and then use the above code to manipulate the pixel buffer of the canvas and redraw it.