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 & HelpPrograms › Unfaithful copying: Pimage to PGraphics
Page Index Toggle Pages: 1
Unfaithful copying: Pimage to PGraphics (Read 871 times)
Unfaithful copying: Pimage to PGraphics
Jan 10th, 2010, 11:54am
 
I'm writing a program which has a method to transform images in certain ways - this method takes a PImage as a parameter, and returns a PImage (this is something I was discussing on here earlier).

This works, but the image is gradually zoomed and blurred over time with repeated copying- see the code below. I'm guessing in one of the copying steps the size does not match up correctly. But as all the copying starts at 0,0, and has size 640, 480, I can't see where the unfaithful copying is taking place. Can anyone spot anything obvious? Thanks....

(I have just taken out the relevant part of the program to isolate the problem here):

Code:
PImage currentimg;

void setup()
{
 size(640, 480);
 frameRate(3);
 noStroke();
 initialise();
}

void draw()
{
  currentimg=transform(currentimg);
  image(currentimg,0,0);
}

void initialise()
{
  fill(random(100), random(100), random(100));
  rect(width/2,height/2, width, height);
  fill(random(100), random(100), random(100));
  rect(random(width), random(height), random(200), random(200));
  fill(random(100), random(100), random(100));
  rect(random(width), random(height), random(200), random(200));
  fill(random(100), random(100), random(100));
  ellipse(random(width), random(height), random(200), random(200));
  fill(random(100), random(100), random(100));
  ellipse(random(width), random(height), random(200), random(200));
  currentimg=get(0, 0, 640, 480);
}

PImage transform(PImage img)
{
 PGraphics pg=createGraphics(640,480,P2D);
 pg.beginDraw();
 pg.copy(img, 0, 0, 640, 480, 0, 0, 640, 480);
 pg.endDraw();
 PImage i2=createImage(640,480, RGB);
 i2.copy(pg,0,0,640,480,0,0,640,480);
 return i2;
}
Re: Unfaithful copying: Pimage to PGraphics
Reply #1 - Jan 13th, 2010, 9:31am
 
No, you're not missing anything obvious. I fiddled with it a bit, and it turns out that copy() is indeed the problem. Thankfully, you can do the same thing without resorting to using copy().

Quote:
PImage transform(PImage img)
{
  PGraphics pg=createGraphics(640,480,P2D);
  pg.beginDraw();
  pg.image(img,0,0);
  pg.fill(random(100), random(100), random(100));
  pg.ellipse(random(pg.width), random(pg.height), random(200), random(200));
  pg.endDraw();
  PImage i2 = pg.get();
  return i2;
}  



I stuck adding a new random ellipse into the transform function to prove that it's being called (and to keep it from being boring  Wink).
Re: Unfaithful copying: Pimage to PGraphics
Reply #2 - Jan 13th, 2010, 10:30am
 
Brilliant, thanks. This helps me a lot.
Re: Unfaithful copying: Pimage to PGraphics
Reply #3 - Jan 13th, 2010, 11:03am
 
Interestingly, when I had the PGraphics initialised to JAVA2D rather then P2D, the program would use progressively more and more ram until it crashed. I changed it back to P2D and it is fine....
Page Index Toggle Pages: 1