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
return a PImage? (Read 3000 times)
return a PImage?
Dec 22nd, 2009, 4:59pm
 
I was just wondering...can a method take a PImage as a parameter and return a PImage? This would be convenient for what I am trying to do at the moment.
Re: return a PImage?
Reply #1 - Dec 22nd, 2009, 9:03pm
 
Giles,
I think you can. Say you want to pass a PImage img1 to a function which returns another PImage and you store its reference in img2.
PImage img1, img2;
setup()
{
   img1=loadImage("test.jpg");
}

draw()
{
   img2=do_stuff(img1);
   image(img2,0,0);
}

PImage do_stuff(PImage i1)
{
   PImage i2=createImage(320,240,RGB); //this is necessary to allocate memory for the image otherwise i2 points to nothing.
   //say you take infomation from i1 and construct a new image i2
   return i2;
}

I think this way you send img1 to the function and receive a new image and assign it to i2. Let me know if it works. It should. The only concern I have is that the new image created inside do_stuff gets collected by the garbage collector (someone advise me).
Re: return a PImage?
Reply #2 - Dec 22nd, 2009, 10:02pm
 
I did this...pretty similar to what you were suggesting, and it works fine:

Code:
PImage img;

void setup()
{
size(640, 480);
img= loadImage("gray.jpg"); //just an image with all grey pixels
PImage img2= alterImage(img);
image(img2,0,0);
}

void draw()
{
}

PImage alterImage(PImage img1)
{
for(int i=0; i<50000; i++)
{
img1.pixels[i] = color(255,0,0);
}
return img1;
}


Now how do I draw stuff like rects into the image in the alterimage method?
Re: return a PImage?
Reply #3 - Dec 22nd, 2009, 11:15pm
 
Quote:
how do I draw stuff like rects into the image

image1.rect() and so on.
Re: return a PImage?
Reply #4 - Dec 23rd, 2009, 6:33am
 
Giles wrote on Dec 22nd, 2009, 10:02pm:
Code:
img1.pixels[i] = color(255,0,0); 



http://processing.org/reference/loadPixels_.html documentation indicates that you should call loadPixels() before using the pixels[] array, and updatePixels() afterwards.

Code:
img1.loadPixels();
// do stuff... img1.pixels[blah]
img1.updatePixels();
Re: return a PImage?
Reply #5 - Dec 23rd, 2009, 6:54am
 
mh,
img.rect() doesn really work, does it?
i get a does not exist message, and i though you have to do it the
PGraphics pg way
Re: return a PImage?
Reply #6 - Dec 23rd, 2009, 7:53am
 
You only have to do loadPixels[] when dealing with the stuff on the screen, i.e. when using the pixels[] array, not the img.pixels[] array. That was my understanding... as the img pixels are already in memory, as you have declared and initialised this object? (I could be wrong).
Re: return a PImage?
Reply #7 - Dec 23rd, 2009, 8:06am
 
Ramin... you are right, img.rect() doesn't seem to work. The PImage class doesn't have the rect method, it seems.... What is the "PGraphics pg way"?

Another idea...can you extend the PImage class to add all the Processing drawing goodies (rects, ellipses, triangles, quads, bezier curves, etc.)? I'm not quite sure how to do this myself.

class PImageplus extends PImage
{
   //copy the methods in here somehow??    
}
Re: return a PImage?
Reply #8 - Dec 23rd, 2009, 9:04am
 
Looks like you need to be using PGraphics and not PImage Wink

Code:
PGraphics img;

void setup() {
 size(201,201);
 background(255);
 img = createGraphics(101, 101, P2D);

 img.beginDraw();
   img.noFill();
   img.stroke(0);
   img.rect(0,0,100,100);
   img.noStroke();
   img.fill(0);
   img.rect(0,0,50,50);
   img.rect(50,50,100,100);
 img.endDraw();
 
 image(img, 0, 0);
 image(img, 100, 100);
}
Re: return a PImage?
Reply #9 - Dec 23rd, 2009, 9:57am
 
That's it, sweet! I knew I'd seen this sort of thing somewhere, but my fuzzy memory couldn't recall it. You're the main man!
Re: return a PImage?
Reply #10 - Dec 23rd, 2009, 10:27am
 
Giles wrote on Dec 22nd, 2009, 10:02pm:
 PImage img2= alterImage(img);

I think you're getting the reference of img1 although you have declared a new PImage 1mg2. Hope that's what you intended, same for PGraphics. If you want a real separate image, do a createImage or something in your function and return the created image.
Re: return a PImage?
Reply #11 - Dec 23rd, 2009, 11:51am
 
anyway it might be usefull to copy the whole PGraphics to a PImage, cause PGraphics look strange when you save them:

Code:


PImage img;
PGraphics pg;
size(500,500);
pg = createGraphics(200,200,P2D);
pg.beginDraw();
pg.fill(255);
pg.rect(50,50,100,100);
pg.stroke(0);
pg.endDraw();
image(pg,0,0);
pg.save("pg.jpg");
img = createImage(200,200,RGB);
img.copy(pg,0,0,200,200,0,0,200,200);
img.save("img.jpg");

Re: return a PImage?
Reply #12 - Dec 23rd, 2009, 1:33pm
 
You were right, Liudr... when I drew the original image, it had also been changed. So it was just the reference being passed. So I incorporated your suggestion and Ramin's suggestion, and here is a method which passes a PImage and returns a PImage, allowing you to draw whatever you want on it in the method (in this case just drawing a rect on it). This is just what I need as I want to create an array of images and draw on them all, but without actually drawing them to the screen initially. So I'll pass them all down to the method which will alter them, and then I can store them in the array.

Thanks for all your help, guys. Very useful indeed.

Code:
PImage img;

void setup()
{
size(640, 480);
img= loadImage("gray.jpg"); //just an image with all grey pixels
PImage img2= alterImage(img);
image(img2,0,0);
}

void draw()
{
}

PImage alterImage(PImage i1)
{
PGraphics pg=createGraphics(640,480,P2D);
pg.beginDraw();
pg.copy(i1, 0, 0, 640, 480, 0, 0, 640, 480);
pg.fill(0,255,0);
pg.rect(width/2, height/2, 50, 50);
pg.endDraw();
PImage i2=createImage(640,480, RGB);
i2.copy(pg,0,0,640,480,0,0,640,480);
return i2;
}
Page Index Toggle Pages: 1