P2D vs JAVA2D Source-over alpha problems

please format code with </> button * homework policy * asking questions

I know this question has been asked in one form of another, but I wanted to pose it here in terms of getting the default blend drawing mode to work similarly between JAVA2D and P2D. Underneath the hood, I believe the problem is with the implementation of the source-over-alpha blending mode, which is working correctly in JAVA2D, but not in P2D (or P3D for that matter)

In the code below, we only get correct behavior when using JAVA2D. When using P2D, the resulting drawing has premultiplication issues which cause a blackish kind of ghosting to build over time.

Is there a way to get the P2D mode to properly composite this sketch? Note the results can be seen both onscreen, and in the test.png that the sketch saves when releasing the mouse.

Any pointers would be much appreciated.

String RENDERER = P2D; // JAVA2D; // or P2D
int BLND = BLEND; // one of BLEND ADD SUBTRACT DARKEST LIGHTEST DIFFERENCE EXCLUSION MULTIPLY SCREEN REPLACE
color rgb_gry = color(60); //grey 
color rgb_circle = color(220,220,100,5); // yellowish
String file_out = "test.png";

PGraphics layer;

public void settings() {
  
  size(640, 480, RENDERER);
  
} // end of settings

void setup(){

  layer = createGraphics(width, height, RENDERER);
  layer.beginDraw();
  layer.blendMode(BLND);
  layer.ellipseMode(CENTER);
  layer.noStroke();
  layer.strokeJoin(ROUND);
  layer.endDraw();
  
}

void draw() {
  background(rgb_gry);  // set bg color to grey
  image(layer, 0, 0);
  
} // end of draw()

void mouseDragged() {
  
  layer.beginDraw();
  layer.fill(rgb_circle);
  layer.ellipse(mouseX, mouseY, 100, 100);
  layer.endDraw();
} // end of void mouseDragged()

void mouseReleased() {
  
  layer.save(file_out);
  println ("...Wrote out file " + file_out);
  
} // end of void mouseReleased()
1 Like

Yes, fun one! Add support for pre-multiplied alpha · Issue #3391 · processing/processing · GitHub

Your only choice really is probably to do what PraxisLIVE does and subclass the P2D renderer to support this (or use PraxisLIVE obviously)

2 Likes

Thank you @neilcsmith . That project looks amazing! Can it run a plain vanilla processing sketch, such as the one I posted? Just curious what results I would get out of the box. Obviously I’ll need to investigate myself, but thought I’d ask anyway. :). Thank you

1 Like

Thanks! It’s not exactly the same, although all of Processing is there - may need some translation. Firstly, it’s straight Java, so no color type, need public on some methods, etc.

The second difference is why this bug affects us a lot. When you call ellipse(..) inside the code for one of our nodes, it’s not rendering to the window directly, but actually doing what you’re doing - layer.ellipse(..). Each “sketch” node renders to a PGraphics, and you layer them up by drawing lines between the nodes, even doing feedback. Without us switching to premultiplied alpha everything would render incorrectly.

1 Like

@neilcsmith. Hi Neil, Following up on the general topic, do you have an opinion about why this problem has not been solved in over 3 years? Is there anyone we can reach out to in order to get this resolved once and for all? It seems like this is affecting many developers and I don’t understand why the lack of oversight. I appreciate any insights you might have in advance.

1 Like