jit.mgraphics + alpha (drawing on transparent background)

Did you know, how to fill all pixels of the [jit.mgraphics] object with an arbitrary value (something like "setall" message for the [jit.matrix])? I want to draw on transparent background, and I can’t use the "paint" method of the [jit.mgraphics] – "paint" is working with alpha on typical way, but I need a method to "replace" alpha component into new value.

I have no idea, how to do that. Maybe my post is just a"feature request"?

this will set the clear color to white, clear, and then set the drawing color back to black.
adjust the color values for your needs.
this is taken from the jit.mgraphics help file, inside the "reset" sub-patcher.

I have simiar problem in the past – it appears that jit.mgraphics have internal matrix, which alpha channel cannot be cleared. (clear) message clears output matrix, which is immediately replaced by that "internal" one on bang. BTW, the "internal" matrix of newly created jit.mgraphics object has all cells in alpha plane set to 0, but later there is no way to return to this state (except recreating the object).

maybe a solution could be to enable jit.mgraphics to receive jit.matrices to paint on them? (just a shot in the dark)

> sorry, just re-read your post saying you can’t use the paint method.

I can’t or I just don’t know, how to ;-).

In other words my question is: how to fill the [jit.mgraphics] with rgba value 0. 0. 0. 0.? In my opinion, the "paint" is not good for that, because "set_source_rgba 0. 0. 0. 0., paint" doesn’t clear alpha channel (of course, I can post the patch later, too).

I think, the "setall" message (for [jit.matrix]) is working just like I want, but unfortunately there’s no equivalent of this message for [jit.mgraphics].

Ok, I prepared a patch explaining the problem I’m trying to solve (my original project is to complicated and containing to many external dependences to post it).

In posted patch I located two [jit.gl.videoplane] objects. One filled with an image taken from [jit.grab], second one containing stuff from [jit.mgraphics]. I’m using @layer parameter to put the [jit.gl.videoplane] containing data from [jit.mgraphics] "over" the [jit.gl.videoplane] with [jit.grab] data. Moreover I’m using alphablending for both [jit.gl.videoplane]’s.

Now I need a method to change "paint" command used for [jit.mgraphics] to something allowing me to create clear (filled with 0.) alpha channel for the image produced by [jit.mgraphics] – and see both: the drawings from [jit.mgraphics] and background from [jit.grab].

Unfortunately your proposition is not a solution: this works fine if I want too draw white forms over relatively dark background (actually every color different to white will be changed ), but, if the video stream from the camera will be containing white areas and I’ll be trying to draw some black lines… every black line will be just invisible – "add" is not a good method for that. What I need is alpha blending, and for alpha blending I need the access to the alpha channel of the image created by [jit.mgraphics].

I’m thinking how to workaround the problem. Of course I can use [jit.gl.sketch] for drawing shapes, but but (1) I can’t draw texts with [jit.gl.sketch], (2) I need something "pixel oriented"and (3) [jit.mgraphics] is really handy (except this one issue ;-) ).

Attachments:

ahh, you are correct.
that’s unfortunate.
to make up for my mistake, i’ve made you a gl.pix patch that’s based on the alphaglue shader.
this will create an alpha channel based on luminance, and gives you some parameters for threshold and fading, which can be very useful.

— Pasted Max Patch, click to expand. —

Copy all of the following text. Then, in Max, select New From Clipboard.

Hey yaniki, I have been trying to solve this same problem and am curious about the solution you posted. When I load the last patch you posted, I can see the camera input, but it does not seem to draw any mgraphics commands. What version of max are you using?

> do you feel the patch you posted last is the best solution to this problem?

I don’t know yet… Workaround proposed by Rob is very efficient, and – what is also important in my opinion – is based on standard programming workflow in Max. My solution is a little bit tricky. More tests are required, that’s for sure. I’m, especially, interested in memory management when [jit.mgraphics] @dim parameter is constantly changing…

I can imagine, the best solution will be to add to the [jit.mgraphics] API methods for direct access to the alpha channel or direct access to internal matrix of the object – but it’s rather question we can ask Cycling’74 team as a feature request. In my opinion [jit.mgraphics] object is potentially very strong tool for various applications (custom GUI, for example) – with direct access to the alpha channel will be even stronger.

Moreover in Max we can do things in many ways: it’s simple to use [jit.gl.sketch] and [jit.gl.text2d] (I think, a solution for missing 64bit version of [jit.gl.text2d] will be prepared) to produce very similar results. In my case the problem is missing 64bit version of [jit.gl.text2d] and (but it’s not a "problem", rather… hmmm… rather something opposite to "problem") style in which [jit.mgraphics] is working with graphics – it’s very clear and handy, so I want to use [jit.mgraphics] instead of other solutions.

I would like to keep pushing this issue. I am working on a porting library that allows me to easily load Processing sketches in Max. Up to this point I had been getting along pretty well with mgraphics until I realized that it can not draw transparently. It would be nice to know what the best design is before I start changing code.

@Anthony: loading P5 sketches into Max? Sounds very interesting. Many of my projects are – from technical point of view – combined Max and Processing (typically joined via OSC). I’ll be keeping crossed fingers.

Hi all,
I’m struggling with the same issue here. I’ve been using the Yaniki’s dim-reset method and does seem to work well. But I’m also seeing an odd byproduct and not sure if it is a result of trying to set all the pixels in jit.mgraphics with an alpha of 0. or a byproduct of how I’m rendering it via jit.gl.videoplane: all the lines/shapes have a very thin outline in a light grey, which is really noticeable drawing a fat white line where the when other layers in the background are also white (trying to use white as an eraser, if that makes any sense).

I’m working on whittling down a patch to share, but wanted to ask to see if anyone had noticed this.

Thanks,
David

EDIT: to clarify a bit more…I only see the outline when the line or shape borders a pixel that still has its alpha set to 0. Not sure if that’s helpful info or not.

(OT : @yaniki : i’m sorry, i clicked by error on spam button on one of your posts, it disappeared for me… then i tried again to verify if i could do something and i couldn’t… hope it doesn’t disappear for everyone, they are the #243089 and #243090… rather stupid, and i find no way to get them back.)

Here’s a whittled down patch. You *should* see what I’m seeing wrt the outline once you set the pen color to white (unless it’s GPU dependent). Notes in patch. Max 6.1.2 (32-bit), MacOS 10.6.8 & 10.8.3.

Thanks in advance for any pointers!

Cheers,
David

EDIT: f-key toggles fullscreen.

Attachments:

Yes, I can reproduce the problem (unfortunately…). Hmmm… I don’t have any idea now, how to fix it, but your problem is, in fact, interfering with my projects too, so I have to dig into this more deeply during this week…