P2D vs JAVA2D - strange differences in PGraphics methods and masking

Yes, and breaks semantics for clone() - just pointing out that you don’t really want what you say you want! :wink:

Yes, definitely broken in 3.5.3. In fact, looks like some other uses of the pixel array are broken too. Going to do a diff on the sources.

However, creating a copy of a PGraphics via the pixel array is a daft idea anyway, particularly if the graphics is on the GPU. Just do this, which works correctly across renderers, and will perform vastly better -

static final String RENDER = P3D, URL =
  "https://" + "Forum.Processing.org/" + "processing-org.jpg";

PImage img;
PGraphics clonedPg;

void settings() {
  img = loadImage(URL);
  size(img.width, img.height, RENDER);

  smooth(8);
  noLoop();
}

void setup() {
  // Buggy for all renderers but JAVA2D under PDE 3.5.3:
  final PGraphics pg = createGraphics(width, height, RENDER);

  pg.beginDraw();
  pg.blendMode(REPLACE);
  pg.image(img, 0, 0);
  pg.endDraw();

  clonedPg = clonePGraphics(pg);
}

void draw() {
  image(clonedPg, 0, 0);
}

PGraphics clonePGraphics(final PGraphics pg) {
  final int w = pg.width, h = pg.height;
  final PGraphics cloned = createGraphics(w, h, RENDER);

  cloned.beginDraw();
  cloned.blendMode(REPLACE);
  cloned.image(pg, 0, 0);
  cloned.endDraw();

  return cloned;
}

You should set the blendMode() back to its default BLEND at the end: :warning:

cloned.beginDraw();
cloned.blendMode(REPLACE);
cloned.image(pg, 0, 0);
cloned.blendMode(BLEND);
cloned.endDraw();

Also, cloned.image(pg, 0, 0); unnecessarily stores the original PGraphics object pg in cloned’s internal WeakHashMap. :hash:

Therefore the cloned PGraphics object isn’t fully detached from the original 1. :roll_eyes:

Yes, good point!

a) who cares?!
b) no, it doesn’t! (well, with this renderer anyway) :grinning:

1 Like