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 › Off-screen buffer and its scaled representation
Page Index Toggle Pages: 1
Off-screen buffer and its scaled representation (Read 2032 times)
Off-screen buffer and its scaled representation
Apr 5th, 2010, 1:12pm
 
First, I would like to tell what I already have and then ask you kindly for suggestions.

  • The user is asked to input the width and height of the image he wants to design.

  • From that a aspect ratios are derived.

  • A rectangle maintaining the aspect ratios is created; it is never bigger than the window



My program will be aiding design of images that will most likely be greater in size than the program's window.


What would be the cleverest way to go about doing the following:
  • I want to have a scaled representation of that image (presumably buffer) inside the window.


  • Later I want to output the buffer to an image file;

What do you suggest?
Thanks
Re: Off-screen buffer and its scaled representation
Reply #1 - Apr 5th, 2010, 6:21pm
 
The simplest (maybe not the "best") way to do it would be to have the "buffer" as a PImage (whatever size you like), and then draw it to screen using image(bufferimage, x, y, w, h). Processing already knows how to resize an image for drawing using this method....

See how that works (I don't know how good it will look).
Re: Off-screen buffer and its scaled representation
Reply #2 - Apr 5th, 2010, 6:25pm
 
Tried it (with a 2048x1536 image scaled to 400x300). Seems to look OK.

Code:
PImage buffer;

void setup()
{
 size(500, 500);
 background(255);
 buffer=loadImage("test.jpg");
 image(buffer, 50, 50, 400, 300);
}

void draw()
{
 
}
Re: Off-screen buffer and its scaled representation
Reply #3 - Apr 5th, 2010, 6:30pm
 
Now I think about it, if you want to be drawing into the buffer, using the scaled representation, it needs to be a PGraphics rather than a PImage...
Re: Off-screen buffer and its scaled representation
Reply #4 - Apr 5th, 2010, 6:42pm
 
OK...here is a simple prototype....click and drag to draw in the photo (you will need your own big photo called test.jpg). The black drawing line is being drawn in the big photo (in the buffer), but is being shown in the smaller representation.... adding more drawing tools will just apply the same principles.

Code:
PImage loadedimage;
PGraphics buffer;


void setup()
{
 size(500, 500);
 background(255);
 loadedimage=loadImage("test.jpg");
 buffer = createGraphics(loadedimage.width, loadedimage.height, JAVA2D);
 buffer.beginDraw();
buffer.image(loadedimage, 0, 0);
 buffer.endDraw();
 image(buffer, 50, 50, 400, 300);
}

void draw()
{
 image(buffer, 50, 50, 400, 300);
}


void mouseDragged()
{
 float bufferx, buffery;
 bufferx = map(mouseX, 50, 450, 0, loadedimage.width);
 buffery = map(mouseY, 50, 350, 0, loadedimage.height);
 buffer.beginDraw();
   buffer.fill(0);
   buffer.noStroke();
   buffer.ellipse(bufferx, buffery, 20, 20);
 buffer.endDraw();
}



How do you save the contents of the PGraphics buffer to a file without first drawing it to screen? I wouldn't mind knowing the answer to that myself...anyone?

EDIT -  this runs much faster using P2D rather than JAVA2D in buffer = createGraphics(loadedimage.width, loadedimage.height, JAVA2D);
The reason I used JAVA2D was that I found some stuff didn't work with P2D in the past.... (I'm not an expert on this, so not sure about the difference).
Re: Off-screen buffer and its scaled representation
Reply #5 - Apr 6th, 2010, 11:40am
 
Giles wrote on Apr 5th, 2010, 6:42pm:
How do you save the contents of the PGraphics buffer to a file without first drawing it to screen I wouldn't mind knowing the answer to that myself...anyone


Thank you so much Giles, you confirmed my suspicion about the use of PGraphics.
So from what I have read as of now, the only way to save the big version is to change the window size, flush everything but the image I want to write and begin writing. Am I on the right track
Re: Off-screen buffer and its scaled representation
Reply #6 - Apr 6th, 2010, 2:17pm
 
No idea...I'm hoping someone else will jump in here and enlighten us  Smiley
Re: Off-screen buffer and its scaled representation
Reply #7 - Apr 6th, 2010, 5:09pm
 
isn't it just myGraphicsObject.saveFrame("blahblah-###.png"); ?
Re: Off-screen buffer and its scaled representation
Reply #8 - Apr 6th, 2010, 7:31pm
 
saveFrame didn't work (with or without parameters, it said the method didn't exist)....but I added this to the program:

Code:
void keyPressed()
{
 buffer.save("saved.jpg");
}


It saved the file....but the colours all look really weird, so something's not right....

EDIT - tried in both P2D and JAVA2D with same result.
Re: Off-screen buffer and its scaled representation
Reply #9 - Apr 13th, 2010, 1:03pm
 
Giles wrote on Apr 6th, 2010, 7:31pm:
It saved the file....but the colours all look really weird, so something's not right....

EDIT - tried in both P2D and JAVA2D with same result.


You are saying that you got the colors, but they are weird
I tried a few times and always end up with a black image but with the correct dimensions - dimensions of my PGraphic Smiley
Re: Off-screen buffer and its scaled representation
Reply #10 - Apr 13th, 2010, 1:12pm
 
re: colors, make sure you're using the right colorMode(RGB/HSB, #) in the PGraphics object...
Re: Off-screen buffer and its scaled representation
Reply #11 - Apr 13th, 2010, 2:28pm
 
Acting on your suggestion, even though I though by default the PGraphics and the main applet shoul dbe in the same color mode, I explicitly set them up to be so.... but it didn't help.

Code:
PImage loadedimage;
PGraphics buffer;


void setup()
{
 size(500, 500);
 colorMode(RGB, 255, 255, 255);
 background(255);
 loadedimage=loadImage("test.jpg");
 buffer = createGraphics(loadedimage.width, loadedimage.height, P2D);
 buffer.beginDraw();
buffer.image(loadedimage, 0, 0);
 buffer.endDraw();
 image(buffer, 50, 50, 400, 300);
}

void draw()
{
 image(buffer, 50, 50, 400, 300);
}


void mouseDragged()
{
 float bufferx, buffery;
 bufferx = map(mouseX, 50, 450, 0, loadedimage.width);
 buffery = map(mouseY, 50, 350, 0, loadedimage.height);
 buffer.beginDraw();
   buffer.colorMode(RGB, 255, 255, 255);
   buffer.fill(0);
   buffer.noStroke();
   buffer.ellipse(bufferx, buffery, 20, 20);
 buffer.endDraw();
}

void keyPressed()
{
 buffer.save("saved.jpg");
}



The colours in the output image are all sort of washed out pinks and purples (it is a city scene with sky so they started out as mostly blues and greys). It's weird that you got a different result, remerer...what version of Processing, OS and Java are you using?

I have Processing 1.09, Windows XP, Java 1.6.0_18.
Re: Off-screen buffer and its scaled representation
Reply #12 - Apr 13th, 2010, 2:51pm
 
aha.  works for me if I replace your .jpg with .png.  not sure why!  might be a bug...
Re: Off-screen buffer and its scaled representation
Reply #13 - Apr 13th, 2010, 8:13pm
 
Yes, works if both loaded image and saved image are png. But the saved image still isn't quite right (has a black bar along the bottom). If you try to load a jpg and save as a png in this program you get half solid black, photo (divided horizontally). Curiouser and curiouser!
Page Index Toggle Pages: 1