Offscreen patcher capture

I wonder if there’s a way with the new API to export a patcher or a portion of it as an image file (a la jit.desktop but without having the patcher as the frontmost window).

Not sure it’s very clear, my goal is to export easily & quickly the waveform drawn by the waveform~ object, but more generally, this could be useful (without having to use jit.desktop or render a buffer~ in a matrix).

The best way would be to have an exportimage method in the waveform object.

Has anyone any idea where to start ?
I see jsurface has an "jgraphics_image_surface_writepng" method but how can I obtain the t_jsurface in question ?
Can I for example attach my object to a waveform~ and obtain a t_jsurface ?

Here is how you can do it. Be warned though that this is undocumented and is not guaranteed to keep working in future versions of max. Below is some loosely sketched code that could work.

The patcherview object has an A_CANT method called "getimage" with the following prototype:

t_jsurface* patcherview_getimage(t_patcherview *pv, t_rect *rect);

So, you could use the jpatcher_get_firstview() function to get a patcherview object from the patcher. Then, do:

t_rect rect;
// fill in the rect with the coords you want
// You could call jbox_get_rect_for_view to
// get the rect for a given box in a given view.
// This would take into account the difference between
// patching and presentation modes.
surface = object_method(patcherview, gensym("getimage"), &rect);

Then you could use jgraphics_image_surface_writepng() to write the surface to a file. Or you could use the surface to draw to a jgraphics context later in a UI object’s paint method. I’m sure you could have lots of fun with that.

When you are done make sure to call jgraphics_surface_destroy() to free the surface.

Just in case anyone is (or will be) interested in this thread, I’ve made a simple example of painting a patcherview inside another ui object.

See example attached.

I’ve also made an external which combines both methods (rendering a patcherview or a portion of it in a file or in an ui object). Rendering a waveform to an image has never been easier ! I’ll release it soon.

Questions : in the attached example, I’m building a patcher window and placing a nobox ui object in it (like the scripto example of the sdk).
Is it possible to prevent this patcher from being edited or switched to presentation mode (like the file browser for example) ? I assume there are just a few attributes to add to the following code, but which of them ? Is this part of a private API ?

As you found, jgraphics_image_surface_writepng is not exported. Perhaps we can export that in max/msp 5.0.7.

Meanwhile I’m glad jgraphics_write_image_surface_to_filedata is mostly working for you. We’ll have to look into the JPEG writing crash when we get a chance. It is quite possible that that has never been tested.