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 › Please Help... semi newbe stumped
Page Index Toggle Pages: 1
Please Help... semi newbe stumped (Read 2908 times)
Please Help... semi newbe stumped
Mar 7th, 2006, 6:23pm
 
is there a way to load just a few images at a time... perhaps preloading one while another is displaying? I need to set up a full screen slideshow that will eventually get synced up with midi. i need to be able to draw from literally hundreds of 1024x768 size images. right now i am getting the OutOfMemoryError with only 22 images loaded (see code below). also is there a way to dump the image from memory when done displaying it?

essentially i need it to work like this:

dump last image > display current image > preload next image

import promidi.*;

int numImage = 22;
int Image = 0;
float xpos = -64;
float ypos = -48;
float xspeed = random(-2, 2);
float yspeed = random(-2, 2);
PImage[] pics = new PImage[numImage];

void setup()

{

 size(screen.width, screen.height); // screen resolution

 pics[0] = loadImage("buginbook_1600x1200.jpg");
 pics[1] = loadImage("IMG_0077.jpg");
 pics[2] = loadImage("IMG_0323.jpg");
 pics[3] = loadImage("IMG_0327.jpg");
 pics[4] = loadImage("IMG_0332.jpg");
 pics[5] = loadImage("IMG_0484.jpg");
 pics[6] = loadImage("IMG_0613.jpg");
 pics[7] = loadImage("IMG_0850.jpg");
 pics[8] = loadImage("IMG_0907.jpg");
 pics[9] = loadImage("IMG_1002.jpg");
 pics[10] = loadImage("IMG_1029.jpg");
 pics[11] = loadImage("IMG_1037.jpg");
 pics[12] = loadImage("IMG_1189.jpg");
 pics[13] = loadImage("IMG_1230.jpg");
 pics[14] = loadImage("IMG_1261.jpg");
 pics[15] = loadImage("IMG_1365.jpg");
 pics[16] = loadImage("IMG_1371.jpg");
 pics[17] = loadImage("IMG_1379.jpg");
 pics[18] = loadImage("IMG_1402.jpg");
 pics[19] = loadImage("IMG_0937.jpg");
 pics[20] = loadImage("IMG_0964.jpg");
 pics[21] = loadImage("IMG_1035.jpg");


}

void draw()

{

 image(pics[Image], xpos, ypos, 1152, 864); // loaded image position and offset
 xpos = xpos + xspeed;            // left/right direction & speed
 ypos = ypos + yspeed;            // up/down direction & speed
 noCursor();

}

void keyPressed()

{
 if(keyPressed){                 // generic pulse to change image... to be replaced by midi
   if(key == ' '){               // if pulse received...
     Image = Image + 1;          // go to next image (to be replaced with "select a random image"
     xpos = -64;                 // reset and recenter new image x axis
     ypos = -48;                 // reset and recenter new image y axis
     xspeed = random(-2, 2);     // new x direction and speed of image
     yspeed = random(-2, 2);     // new y direction and speed of image
     if(Image == numImage){      // if you hit the last image...
       Image = 0;                // start over
     }
   }
 }
}
Re: Please Help... semi newbe stumped
Reply #1 - Mar 9th, 2006, 2:54am
 
Basically you can't preload all of those images due to memory restrictions.

You can, however, load on the fly. Since it is a slide show, performance won't be such an issue.

Here's what I suggest:

Don't draw the image on every frame. You don't have to. You only ever want to draw to your frame when you change an image.

After the timer runs out, load a new image then display the image into your frame, and then reset the timer.

This way you only ever need one PImage to store your image. You'll still need an array to track all your image filenames, but this way you won't be trying to load all of your images into memory. The idea is to load one image at a time into memory when you actually need it.
Re: Please Help... semi newbe stumped
Reply #2 - Mar 10th, 2006, 4:48am
 
i see what you mean... the only problem, is that in order to keep the visuals from getting boring, i must move the image slightly and zoom, so i need to redraw the image every frame.... i have worked this out, but if i use loadImage in my within draw i loose all my performance. any suggestions?
Re: Please Help... semi newbe stumped
Reply #3 - Mar 10th, 2006, 8:02am
 
Is there any moment in which your slide show pauses from motion? You could take that as an opportunity to load images.
Re: Please Help... semi newbe stumped
Reply #4 - Mar 10th, 2006, 10:53am
 
It is possible to load images in the background, whilst not pausing the display, but requires the use or Threads, which is sometimes a bit of a dark art. I have written a threaded image loader, but I'm not confident enough about it being stable to release yet unfortunately. Hopefully I'll get a few hours soon to put in the work needed to get it ready.
Re: Please Help... semi newbe stumped
Reply #5 - Mar 10th, 2006, 12:49pm
 
Wow John. That sounds mighty sexy.

Dark arts indeed. Black magic, threading.. et al.
Re: Please Help... semi newbe stumped
Reply #6 - Mar 10th, 2006, 9:44pm
 
-- Awooga! - Awooga! --
Beta code alert, use at your own risk...

I claim to be no sort of expert on threading in general, let alone Java Threads.

This may make it into a Processing library at some point if people would find it useful.

Code:
class threadedImageLoader extends Thread
{
String filename;
PImage img;
boolean ready;
boolean keepRunning;

threadedImageLoader()
{
keepRunning=true;
ready=false;
filename=null;
img=null;
}

void loadFile()
{
img=loadImage(filename);
ready=true;
}

void load(String _filename)
{
filename=_filename;
ready=false;
}

PImage getImage()
{
if(ready==false)
{
// You asked for it too early. Or asked for a new one
// before getting this one...
return null;
}
ready=false;
filename=null;
return img;
//We can only return it once, should make user code easier.
}

void close()
{
keepRunning=false;
}

void run()
{
while(keepRunning)
{
// println("threadedImageLoader.loop");
if(ready==false && filename!=null)
loadFile();
try
{
this.sleep(200);
}
catch (Exception e)
{
//we can be woken up early.. oh woe is us!
}
}
println("threadedImageLoader stopping");
}
}


To use:

Code:
threadedImageLoader loader;
...
void setup()
{
...
loader=new threadedImageLoader();
loader.start();
...
}

void draw()
{
// want a PImage? do this:
if(some condition)
loader.load("whicheverImageIsNext");
// PImage will get loaded in the background, probably
// fairly quickly, but there is a likely delay, as the
// loader only checks every 0.2 seconds to see if you've
// asked for an image to be laoded, to stop it chewing
// CPU..
if(loader.ready) // Woo, the image is ready!
// and we've not already copied it.
{
myPImage=loader.getImage();
}
if(myPImage!=null)
{
//draw it.. or something.
}
}
...
/* This is important.. without it, even though your sketch
has finished, the threadedImageLoader could keep running..
and that would be silly. */
public void stop()
{
loader.close();
super.stop();
}
Re: Please Help... semi newbe stumped
Reply #7 - Mar 11th, 2006, 3:05am
 
JohnG,

trying to try out your code, but perhaps i don't know enough to use it yet. i have pasted the class code into a new tab and started working the to use code, but keep getting "Type "threadedImageLoader" was not found."... hmmm totaly lost here. if you could point me in the right direction i would appreciate it.
Re: Please Help... semi newbe stumped
Reply #8 - Mar 11th, 2006, 1:51pm
 
I've just created a blank sketc, and done similar, creating a new taba dn putting the class in, and I seem to be able to use it fine in the main code...

If you archive your sketch, and put the zip file online, I can take a look and see what's going wrong.
Re: Please Help... semi newbe stumped
Reply #9 - Mar 13th, 2006, 6:00am
 
John,

not sure what i did before, but like you, i started a blank sketch and it seems to be working now... i'll play with this for a few hours and post again when i see what i can do with it. thanks in advance...
Re: Please Help... semi newbe stumped
Reply #10 - Mar 13th, 2006, 6:31am
 
just sent you a PM Smiley
Page Index Toggle Pages: 1