We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
pgraphics2 -> PImage (Read 3172 times)
pgraphics2 -> PImage
Jul 18th, 2005, 9:13am
 
hi,

I was attempting to do some offscreen buffer work and I needed to render the same graphics context to multiple PImages, but I found that all of the calls to "get()" were constructing a new object, and thus very slow and not practical for what I needed.  I threw this is my Graphics2, which I downloaded off sourceforge and it seems to be working fine - happy to hear your thoughts.  I'm messing around a bit with graphics2 to get it to what I want, so I will keep posting any changes I think you may find useful.

x,y,w,h are same as other "get"
img is the image to render into
imgx, imgy is the pixel position to start rendering to

I don't check for wierd values (as in the other gets), but I do check the the image w/h will fit into the given subsection of img and I clip if necessary using the min operator.


Code:

public void get(int x, int y, int w, int h, PImage img, int imgx, int imgy) {



if (imageMode == CORNERS) { // if CORNER, do nothing

// w/h are x2/y2 in this case, bring em down to size

w = (w - x);

h = (h - x);

}



int image_sub_width

= img.width - imgx;

int image_sub_height

= img.height - imgy;

w = Math.min(w,image_sub_width);

h = Math.min(h,image_sub_height);

int offset = imgy * img.width + imgx;

((BufferedImage) image).getRGB(x, y, w, h, img.pixels, offset, img.width);
}




- zach

Re: pgraphics2 -> PImage
Reply #1 - Jul 23rd, 2005, 2:42pm
 
in continuing to tinker with graphics2d, I needed to have a transparent background, but I was have trouble with getting that to work.  I rewrote the graphics2d clear fuction, perhaps there is a more efficient way to do this?

but it seems to work, I can now say

mom.background(0x44009900);

and it draws a transparent background, so when I grab the PImage, it's partially transparent (alpha = 0x44 or 68 )...


again, don't know if this is helpful, just wanted to post some notes online if anyone else is trying to do the same kind of thing (rendering to a buffer, transparency, etc);

- zach



Code:



public void clear() {


// first clear what was there

g2.setComposite(AlphaComposite.getInstance(



AlphaComposite.CLEAR, 0.0f));

g2.fillRect(0, 0, width, height);


// then fill with the bg color, use the transparency bits

g2.setComposite(AlphaComposite.SrcOver);

g2.setColor(new Color(backgroundColor, true)); //http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html, for alpha
g2.fillRect(0, 0, width, height);


}




(note - this is slowww, I just cannot find a way to clear the buffer and get the right kind of transparency - I don't want cumulative transparecy, I want a constant transparency).  various other blend modes didn't seem to help, although I'm sure there's a way.  to speed things up, I reverted back to the original call, (with a modified new color call) :

Code:

g2.setColor(new Color(backgroundColor, true));
g2.fillRect(0, 0, width, height);


but I'm still searching for something that isn't cumulative and will allow me to clear all the bits and set transparency.)
Re: pgraphics2 -> PImage
Reply #2 - Jul 24th, 2005, 6:21pm
 

hi,

here's another change I made to speed up my graphics2 - I'm gonna post it, since it may prove helpful as you rewrite elements for future versions.

I need to call image(PImage,x,y) alot for a changing background image in my work.  The problem I'm having is drawImage call in imageImpl:

Code:

g2.drawImage(((ImageCache) who.cache).image,
(int) x1, (int) y1, (int) x2, (int) y2,
u1, v1, u2, v2, null);


is slowing me down, since I don't need to scale and the this drawImage call assumes a scale...  When I replace it with this call (in either a seperate imageImpl call or via logic to determing that no scaling is happening) :

Code:

g2.drawImage(((ImageCache) who.cache).image,



(int) x1, (int) y1, (int) x2, (int) y2, null);


I get a pretty significant speed boost.... About 8-10fps on a 600x400 image every frame.

hope this helps -
zach




Re: pgraphics2 -> PImage
Reply #3 - Aug 4th, 2005, 2:17am
 
thanx for the efforts, zach.
I'm trying to learn Processing and I come from Lingo.
I was used to make everything with offscreen buffer.
drawing on transparente image objects etc.
then compositing everything on need.

this will be a good tutorial to feel less frustrated

thanx a lot

ubee
Page Index Toggle Pages: 1