I’ve created a test sketch to demonstrate and try to pinpoint some of the unusual behavior I’ve noticed when switching between P2D and JAVA2D renderers (specifically, what seem to be bugs with the P2D renderer)
Differences in PGraphics get() method:
- in P2D, if you create a PGraphics pg, draw to it, then call imagecopy = pg.get(), the pg itself is no longer renderable
- in JAVA2D, if you create a PGraphics pg, draw to it, then call imagecopy = pg.get(), the pg itself is still renderable
Masking differences:
- in P2D, only PImage to PImage masking works properly. If a PGraphics is involved (with an image drawn to it), then it results in strange black and white / mirrored copy of one of the inputs
- in JAVA2D, PImage to PImage masking works, as well as PGraphics cases
In both of these cases, it seems JAVA2D wins - unless I am doing something wrong. However, large images, framerate, and potential use of custom shaders will be a concern in the bigger project I’m gearing up towards - so I’m hoping that there is still a way for me to get PGraphics working well with P2D.
If anyone is interested in the code I was testing with:
input_image = None
mask_input_image = None
masked = None
# renderer = JAVA2D
renderer = P2D
# INSTRUCTIONS FOR TESTING
## I have different masking cases commented out in the setup(), try one at a time
## Then switch the renderer type above and try the cases again - gets very different results
# FINDINGS
## maybe this link is relevant - https://forum.processing.org/two/discussion/3658/issues-with-mask-on-pgraphics-objects
## in P2D, only PImage to PImage masking works properly. If a PGraphics is involved (with an image drawn to it), then it results in strange black and white / mirrored results
## in JAVA2D, PImage to PImage masking works, as well as PGraphics cases
## in P2D, if you create a PGraphics pg, draw to it, then call imagecopy = pg.get(), the pg itself is no longer renderable
## in JAVA2D, if you create a PGraphics pg, draw to it, then call imagecopy = pg.get(), the pg itself is still renderable
## so JAVA2D seems the more reliable way to go, but we want to use shaders and openGL to boost performance - which requires P2D
def setup():
global input_image
global mask_input_image
global masked
size(400,400,renderer)
# INITIALIZATION
input_image = loadImage("some_image.jpg")
input_image.resize(200,200)
mask_input_image = loadImage("some_other_image.jpg")
mask_input_image.resize(200, 200)
input_copy = input_image.get()
mask_input_copy = mask_input_image.get()
# END INITIALIZATION
##### CASE 1 #####
##### PImage masking PImage #####
##### Works fine both with P2D and default renderer
input_copy.mask(mask_input_copy)
masked = input_copy.get()
# ##### CASE 2 #####
# ##### PGraphics masking PImage #####
# ##### With P2D, doesnt work. With default, works
# pg = createGraphics(200,200,renderer)
# pg.beginDraw()
# pg.background(255,255,255)
# pg.image(mask_input_copy,0,0)
# pg.endDraw()
# input_copy.mask(pg)
# masked = input_copy.get()
# ##### CASE 3 #####
# ##### PImage masking PGraphics #####
# ##### Weird: with P2D, produces an upside down black and white image of the mask image. Works with defualt
# pg = createGraphics(200,200,renderer)
# pg.beginDraw()
# pg.background(255,255,255)
# pg.image(input_copy,0,0)
# pg.endDraw()
# pg.mask(mask_input_copy)
# masked = pg.get()
# ##### CASE 4 #####
# ##### PGraphics masking PGraphics #####
# ##### Weird: with P2D, produces a rightside up black and white image of the mask input. Works with default
# pg = createGraphics(200,200,renderer)
# pg.beginDraw()
# pg.background(255,255,255)
# pg.image(input_copy,0,0)
# pg.endDraw()
# pg2 = createGraphics(200,200,renderer)
# pg2.beginDraw()
# pg2.background(255,255,255)
# pg2.image(mask_input_copy,0,0)
# pg2.endDraw()
# pg.mask(pg2)
# masked = pg.get()
# ##### CASE 5 #####
# ##### A Test with pg.get() #####
# ##### Weird: with P2D, once pg.get() is called, pg becomes null. This doesn't happen with default renderer
# pg = createGraphics(200,200,renderer)
# pg.beginDraw()
# pg.background(255,255,255)
# pg.image(input_copy,0,0)
# pg.endDraw()
# #####################
# # # Option A - works to keep original pg
# masked = pg
# #####################
# # # Option B - pg disappears once pg.get() is called
# # some_var = pg.get()
# # masked = pg
# #####################
def draw():
background(255)
pushMatrix()
image(input_image,0,0,200,200)
text("input image",10,30)
translate(200,0)
image(mask_input_image,0,0,200,200)
text("input mask",10,30)
translate(-100,200)
image(masked,0,0,200,200)
popMatrix()