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.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › convert a PImage/PGraphics into a BufferedImage
Pages: 1 2 
convert a PImage/PGraphics into a BufferedImage? (Read 4344 times)
convert a PImage/PGraphics into a BufferedImage?
May 25th, 2008, 4:17pm
 
Hi I'm trying to convert an image made in processing into a bufferImage for use in openGL as a texture, however, when I try to copy the pixels from the PGraphics object to the bufferedImage, I just get a blank result.

Has anyone managed to do this properly?

Quote:
BufferedImage prepareText(String txt, PFont f){
 //the idea with this is to draw Text to an offscreen buffer, then load that as an image for use as a texture
 
 textFont(f);
 int w = int(textWidth(txt));
 PGraphics pg;
 if(w<256){
   w=256;
 }
 
 pg = createGraphics(w,w,JAVA2D);
 
 pg.beginDraw();
 pg.background(0);
 pg.fill(255);
 pg.textFont(f);
 pg.textAlign(CENTER);
 pg.text(txt,w*0.5,w*0.5-(textDescent()*0.5));
 pg.endDraw();
 pg.loadPixels();
 BufferedImage bm=new BufferedImage(w,w,BufferedImage.TYPE_INT_ARGB);
 bm.setRGB(0, 0, w, w, pg.pixels, 0, w);
 return bm;
}

Re: convert a PImage/PGraphics into a BufferedImag
Reply #1 - May 25th, 2008, 6:32pm
 
you should take a look at this thread:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1199399046
Re: convert a PImage/PGraphics into a BufferedImag
Reply #2 - May 25th, 2008, 6:48pm
 
I had a look at that thread prior to posting, however it only deals with loading an image file. I want to be able to use an image that I've just made.

It would be possible to just save the image out and load it that way, but it seems counter intuitive to me.

I've got no problem with the syntax involved with loading the texture, its converting the PImage to an BufferedImage that's vexing me.

Thanks for the response though.
Re: convert a PImage/PGraphics into a BufferedImag
Reply #3 - May 25th, 2008, 7:27pm
 
have you tried casting the PGraphics.image directly into a BufferedImage?

Code:
java.awt.image.BufferedImage bm = (java.awt.image.BufferedImage)pg.image; 

Re: convert a PImage/PGraphics into a BufferedImag
Reply #4 - May 25th, 2008, 7:44pm
 
Just tried it, and no it didn't Sad

I also had a crack at adapting julapy's code on the thread moka linked to, but while it does actually produce something, the result is weird and skewed.

Also, making BufferedImages into Textures seems to be really slow somehow.

What's weird is if I change the call to TextureIO.newTexture(...) to load the texture with a file, its pretty much instant, but if I want it to load the Buffered one, it takes ages.

You'd have thought it would be the other way round.

I'm trying to make this all run independent of processing's OGL, as per http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1209764312
I'm trying to make a VJ app and I'm trying to avoid the output stopping when an image or whatever is loaded.

Maybe I'm going about this the wrong way.
Re: convert a PImage/PGraphics into a BufferedImag
Reply #5 - May 25th, 2008, 10:20pm
 
And something like this?

Code:
PGraphics pg = createGraphics(width, height, P2D);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(pg.image, 0, 0, width, height, null);
g2d.dispose();
Re: convert a PImage/PGraphics into a BufferedImag
Reply #6 - May 26th, 2008, 12:25pm
 
Nope, still doesn't want to work.

I found this forum post on it; http://forum.java.sun.com/thread.jspa?threadID=736202&messageID=4229597

but the solution uses Swing and i still don't understand it.

It looks like this is just beyond my understanding. I guess I'll just have to stick to processing's openGL and run all other computations in different Java threads.
Re: convert a PImage/PGraphics into a BufferedImag
Reply #7 - May 26th, 2008, 1:16pm
 
I don't know how you use the BufferedImage, but your code actually works.
I pasted it as is in Processing, added the following lines:
Code:
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

PFont fnt;

void setup()
{
size(1024, 512);
fnt = createFont("Verdana", 20);
BufferedImage bi = prepareText("Foo", fnt);
println(bi);
File file = new File("E:/Dev/PhiLhoSoft/Processing/Test/Foo.png");
try
{
ImageIO.write(bi, "PNG", file);
}
catch (Exception e) { println(e); }
}

void draw()
{
}
and I got an image file with a black square and the white Foo text centered.
Re: convert a PImage/PGraphics into a BufferedImag
Reply #8 - May 26th, 2008, 2:51pm
 
Interesting, although the example I linked to above describes the same thing happening, but not being able to read it from memory.

I want to do this on the fly, so saving then loading from disk will be problematic. All I'm doing after the prepareText function is (attempting to) load the buffered image to a texture.

tex=TextureIO.newTexture(bi,false); //second argument is whether to generate mipmaps

If I use this command in its load file form;
tex=TextureIO.newTexture(new File("C:/foo.png"),false);

it works fine, so either the newTexture command is broken in JOGL or the image is somehow lost by the memory.

It doesn't make much sense.

Cheers
Martin
Re: convert a PImage/PGraphics into a BufferedImag
Reply #9 - May 26th, 2008, 3:11pm
 
I wrote to disk only to show there is the wanted data in the BufferedImage. I could have put it to clipboard or something else.
I have no experience with OpenGL, so I can't go much farther, I fear.
But when I searched BufferedImage in this forum, I saw somebody having better results with BufferedImage.TYPE_INT_RGB instead of TYPE_INT_ARGB. Maybe you can try that?
Or show a little snippet illustrating how you use the BI, so I can play with it.
Re: convert a PImage/PGraphics into a BufferedImag
Reply #10 - May 26th, 2008, 4:51pm
 
the thread I posted does not only deal with loading a texture. look at the end. There is an example of how to create a texture from a PImage object.
Re: convert a PImage/PGraphics into a BufferedImag
Reply #11 - May 26th, 2008, 11:27pm
 
Phil: the code I'm using is really basic;

Code:

BufferedImage openStatus = prepareText("Some Text", disp);
try {
oS=TextureIO.newTexture(openStatus,false);
}
catch(Exception e) {
println(e);
}


Moka; I had a look at that code already, but it does weird things to the image, either corrupts and distorts it, or skews it weirdly.
Re: convert a PImage/PGraphics into a BufferedImag
Reply #12 - May 27th, 2008, 9:47pm
 
O M G

Sorry, I changed the boolean value for generate mipmaps to true instead, and it works.

I'm sorry for taking up so much time!

I now open the floor to insults and abuse.

Cheers guys
Re: convert a PImage/PGraphics into a BufferedImag
Reply #13 - May 27th, 2008, 10:21pm
 
No problem for me, it was an opportunity to see some interesting code and to experiment... Cheesy
Re: convert a PImage/PGraphics into a BufferedImag
Reply #14 - May 27th, 2008, 11:25pm
 
Well, for those that are interested, here's the final code;

I've tested this method, and my original method of copying the pixel array, and this one works out a good 30ms faster on my computer.

Code:

BufferedImage prepareText(String txt, PFont f){
//the idea with this is to draw Text to an offscreen buffer, then load that as an image for use as a texture
textFont(f);
int w = int(textWidth(txt)+50);
PGraphics pg;
if(w<256){
w=256;
}
pg = createGraphics(w,w,JAVA2D);
pg.beginDraw();
pg.background(0,0);
pg.fill(255);
pg.textFont(f);
pg.textAlign(CENTER);
pg.text(txt,w*0.5,w*0.5-(textDescent()*0.5));
pg.endDraw();
BufferedImage img = new BufferedImage(w, w, BufferedImage.TYPE_INT_ARGB_PRE );
Graphics2D g2d = img.createGraphics();
g2d.drawImage((java.awt.Image)pg.image, 0, 0, w, w, this);
g2d.finalize();
g2d.dispose();

return img;
}


You could extend this function to output a GL.Texture quite easily;

Code:

Texture prepareText(String txt, PFont f){
//the idea with this is to draw Text to an offscreen buffer, then load that as an image for use as a texture
textFont(f);
int w = int(textWidth(txt)+50);
PGraphics pg;
if(w<256){
w=256;
}
pg = createGraphics(w,w,JAVA2D);
pg.beginDraw();
pg.background(0,0);
pg.fill(255);
pg.textFont(f);
pg.textAlign(CENTER);
pg.text(txt,w*0.5,w*0.5-(textDescent()*0.5));
pg.endDraw();
BufferedImage img = new BufferedImage(w, w, BufferedImage.TYPE_INT_ARGB_PRE );
Graphics2D g2d = img.createGraphics();
g2d.drawImage((java.awt.Image)pg.image, 0, 0, w, w, this);
g2d.finalize();
g2d.dispose();

Texture tx = TextureIO.newTexture(img ,true);

return tx;
}


Although I think its considered good practice to wrap any calls to TextureIO.newTexture in try catch braces, that might just be for loading from a file.

From what I've been reading as I've tried to solve this problem;

Once you have a GL texture made, you should edit it using the Texture.updateSubImage method, as its much faster and avoids unnecessary cycles.

BufferedImages have lots of different modes, I've picked the one that seems to work fastest.


The question is now, is it worth it? When I've got some time, I'll see if just doing this purely in processing's API is faster/slower.


Thanks for all the help

Martin
Pages: 1 2