Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

many copy calls lead to OutOfMemoryError

$
0
0
Hi everyone,

I'm working on a program, which slices up images and draws these slices on the screen. But no matter if I try PGraphics or PImages after a while my program simply crashes because of the OutOfMemoryError.

I have simplified my program so that is easy go trough it. But it seems like if the copy call leaves something in the memory, which is not cleaned up.

  1.   PImage  s1, s2;
  2.   int      left  = 1;
  3.   int      mode  = 0;
  4.   int      sliceW  = 50;
  5.   PImage    pg;
  6.   
  7.   @Override
  8.   public void setup() {
  9.     super.setup();
  10.     s1 = loadImage("1.jpg");
  11.     s2 = loadImage("2.jpg");
  12.     size(s1.width * 2,s1.height);
  13.     pg = createImage((int)( s1.width  * .5f),  s1.height , RGB);
  14.   }
  15.   
  16.   @Override
  17.   public void draw() {
  18.     Object[] leftG = getSlice(getPart(false, left == 1 ? s1 : s2), sliceW);
  19.     Object[] rightG = getSlice(getPart(true, left == 1 ? s2 : s1), sliceW);
  20.     switch (mode) {
  21.       case 0: // simple copy
  22.         image((PImage) leftG[0], (Integer) leftG[1], 0);
  23.         image((PImage) rightG[0], width - (Integer) rightG[1], 0);
  24.         break;
  25.     }
  26.   }
  27.   
  28.   private Object[] getSlice(PImage pg, int size) {
  29.     PImage slice = createImage(size, pg.height, RGB);
  30.     int x = (int) random(pg.width - size);
  31.     slice.copy(pg, x, 0, size, pg.height, 0, 0, size, pg.height);
  32.     return new Object[] { slice, x };
  33.   }
  34.   
  35.   private PImage getPart(boolean left, PImage img) {
  36.     int pgX = (int) (img.width * .5f);
  37.     int pgY =  img.height;
  38.     if (left)
  39.       pg.copy(img, 0, 0, pgX, pgY, 0, 0, pgX, pgY);
  40.     else
  41.       pg.copy(img, (int) img.width - pgX, 0, pgX, pgY, 0, 0, pgX, pgY);
  42.     return pg;
  43.   }

Viewing all articles
Browse latest Browse all 1768

Trending Articles