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

Image masking help needed. Framerate drops while drawing PImage

$
0
0
Hello everyone,
I am new to the forum. I am using processing through another framework (MT4J), which uses Processing 1.5

What I am trying to achieve is simple. I have a background image-bgImage , a foreground image-fgImage .(both of same size drawn on screen with fgImage on top). Now when a person touches at x,y . The background image is revealed near the finger with some animation (say one star rotating/circle with border on fire etc). 

So I draw the bgImage , then set alpha of respective pixels of fgImage to 0. I have loaded the animated png sequence to calculate the animated pixels. The draw code is as follows.

P.S - I am using processing 1.5 through mt4j , therefore I have to call the functions using graphics.image() instead of image() and app.alpha() instead of alpha().
  1. void draw()
  2. {
  3. graphics.image(bgImage,0,0);  //draw backgroundimage
  4. PImage current = animatedSprite.getCurrent();  //Returns a PImage
  5. int spritew = current.width;
  6. int spriteh = current.height;

  7. fgImage.loadPixels();  //LoadPixels
  8. //fgImage.pixels = origPixels;
  9. for(Position p:idToPosition.values())   //Iterate through all the finger positions
  10. {
  11. for (int x=0; x<current.width; x++) {
  12. for(int y=0; y<current.height; y++) {
  13. int a = y*spritew+x;
  14. float alpha =255- app.alpha(current.pixels[a]);
  15. float xpos = p.x()-spritew/2 + x;
  16. float ypos = p.y() -spriteh/2 + y;
  17. if (xpos < 0 || ypos < 0 || xpos > fgWidth || ypos> fgHeight){continue;}
  18. int pos =(int)xpos+(int)ypos*fgImage.width;
  19. fgImage.pixels[pos] = graphics.color(graphics.color(fgImage.pixels[pos]),alpha); ;
  20. }
  21. }
  22. }
  23. fgImage.updatePixels(); //Update the pixels, seems like this reduces the fps
  24.  graphics.image(fgImage,0,0);
  25. }
Till line 8 - Everything runs normally with 60fps. (i.e with other parts commented out)
Till line 26 - Runs fine with 60fps , but with one finger on , runs with 40 fps , average
When line 27 is uncommented , the fps drops directly to 4-5 fps.

I don't see any obvious error. Can anyone please point me where I am doing wrong ? Or is there any better approach to handle this problem ?

Thanks in advance.

Edit: Seems like if I comment out line #21 & #25 , it runs @ 60fps. i.e. if I dont update the pixels in fgImage, with #25 commented out , runs @ 40-45 fps when there's any finger.

Viewing all articles
Browse latest Browse all 1768

Trending Articles