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 › Images taking up too much memory
Page Index Toggle Pages: 1
Images taking up too much memory (Read 872 times)
Images taking up too much memory
Feb 16th, 2010, 1:33pm
 
Hello.
I'm having a program that in the end will be a map viewer. (Like Google maps but with he ability to interact with the map).

I'm loading the map images into an array and somehow, although they are very small, they take up a lot of memory.
The images are around 1 KB each. Still I had to release some 200 MB memory for Processing.
The images are in .png format.
And it takes almost a quarter of a second to load the 16 images.

Can someone see what I'm doing wrong?
Code:
//textBoxOne variables
PFont fontA;
int textBoxOneX = 600;

//Window settings
int windowWidth = 1920;
int windowHeight = 1200;

//Image settings
PImage a; //Temporary holder
PImage[][] mapHolder; //Array holder
int mapImageWidth = 256;
int mapImageHeight = 256;
String mapImagePath;

//Map settings
String mapType = "Map"; //Currently two modes (Map/Sat)
//Starting position
int xPos = 71195;
int yPos = 39185;
//This is the allowed space to move in
int xPosMin = 70195;
int xPosMax = 72213;
int yPosMin = 38185;
int yPosMax = 40211;

/*This is the space which contains images
int xPosMin = 71195;
int xPosMax = 71213;
int yPosMin = 39185;
int yPosMax = 39211;
*/

//The xSpan and ySpan is the number of images which will be loaded simultaneously
//((number of images to cover up the window) times three so that one can look around with mouse without the need to load more images)
int xSpan = (windowWidth/mapImageWidth) + 1;
int ySpan = (windowHeight/mapImageHeight) + 1;

void setup(){
 size(windowWidth,windowHeight);
 background(200);
 noLoop();
 
 //Text
 fontA = loadFont("CourierNew36.vlw");
 textFont(fontA, 20);  
 textMode(MODEL);
 println(xSpan*ySpan);
}

void keyPressed(){
 if (key != CODED) {
   switch (key) {
     case 'x': mapType = "Map"; redraw(); break;
     case 'c': mapType = "Sat"; redraw(); break;
   }
 } else {
   switch (keyCode) {
     case UP: moveMap(1,2); break;
     case DOWN: moveMap(1,3); break;
     case LEFT: moveMap(1,1); break;
     case RIGHT: moveMap(1,4); break;
   }
 }
 
}

void draw(){
 mapHolder = new PImage[xSpan + 1][ySpan + 1];
 getMaps();
 showTextBox();
}

PImage getMapImage(int xPos, int yPos, int zoom, String type){
 mapImagePath = "map/" + type + "_x=" + xPos + "y=" + yPos + "zoom=" + zoom + ".png";
 return loadImage(mapImagePath);
}

void getMaps(){  
 for (int x = 0; x <= xSpan; x++){
   for (int y = 0; y <= ySpan; y++){
     mapHolder[x][y] = getMapImage (xPos + x, yPos + y, 17, mapType);
     try {
       image(mapHolder[x][y], x*256, y*256);
     } catch (Exception e) {
       image(loadImage("map/unavailable.png"), x*256, y*256);
     }
   }
 }
}

void showTextBox(){
 fill(0);
 text("xPos: " + xPos, textBoxOneX, 60);
 text("yPos: " + yPos, textBoxOneX, 90);
}

void moveMap(int step, int direction){
 if (direction == 1 && xPos - step >= xPosMin){
   xPos -= step;
 }
 if (direction == 2 && yPos - step >= yPosMin){
   yPos -= step;
 }
 if (direction == 3 && yPos + step + ySpan <= yPosMax){
   yPos += step;
 }
 if (direction == 4 && xPos + step + xSpan <= xPosMax){
   xPos += step;
 }
 redraw();
}

Re: Images taking up too much memory
Reply #1 - Feb 16th, 2010, 2:38pm
 
classic error: loadImage in draw(). put them in setup() as you are (or should be) only doing it once.

and although your images are small on disk, when they are loaded into memory they get decompressed into 24 (or 32) bit per pixel format, hence using up memory.

(i bet PhiLho got here first)
Re: Images taking up too much memory
Reply #2 - Feb 16th, 2010, 2:51pm
 
koogy wrote on Feb 16th, 2010, 2:38pm:
classic error: loadImage in draw(). put them in setup() as you are (or should be) only doing it once.

and although your images are small on disk, when they are loaded into memory they get decompressed into 24 (or 32) bit per pixel format, hence using up memory.

(i bet PhiLho got here first)


Grin
I was going to post with the same response, but he's using noLoop() and redraw() and a slightly unusual application of try/catch in getMaps() so I thought I'd leave it to PhiLho...  but it looks like you got there first Wink

I also wondered whether the reason for this construction was that once this gets scaled up to a large map there might be a lot more images to deal with - at which point you might want to do image loading dynamically; though certainly not every time draw is called...
Re: Images taking up too much memory
Reply #3 - Feb 17th, 2010, 9:00am
 
Well you are correct about that there will be a lot more images in the future. But I understand now that you can't load the images every time.

I guess I can use something like
Code:
imageHolder[x][y] = imageHolder[x+1][y] 


or will that to use too much power?

I haven't understood the way to use try/catch but at least it works as I want. Why I'm trying image() instead of getMapImage() is that getMapImage() does not return an error if there is a missing file.


<edit>
I did try to use the already loaded images and store them in new cells in the holder array. It works pretty smooth now.

But it will always load the "border" images. And they are approximately 10 on every border. These will not show until I drag the map with the mouse, so is there anyway to make these images load in the background or something?
</edit>
Page Index Toggle Pages: 1