Try to make a first implementation of your first point ( crop PImage), but it actually make the frameRate drop dramatically (less than 10 fps).
Perhaps the idea is good but not my implementationā¦
class User {
PImage bodyImg;
ArrayList<NewImg> imgList;
PGraphics pgTotal;
User() {
imgList = new ArrayList<NewImg>();
pgTotal=createGraphics(512, 424);
}
void delayEffect() {
createWhiteAndTransparentImage();
colorAllPg();
}
void createWhiteAndTransparentImage() {
bodyImg.filter(THRESHOLD);
PGraphics pgWhite=createGraphics(512, 424);
pgWhite.beginDraw();
pgWhite.background(255);
pgWhite.mask(bodyImg);
pgWhite.endDraw();
NewImg newImg=new NewImg();
newImg.trimImage(pgWhite);
imgList.add(0, newImg);
if (imgList.size()>100)imgList.remove(imgList.size()-1);
}
void colorAllPg() {
pgTotal.beginDraw();
pgTotal.clear();
for (int i = imgList.size()-1; i > 0; i--) {
PImage colorImg=imgList.get(i).img;
pgTotal.tint(random(255), random(255), random(255));
float x=imgList.get(i).cornerPos.x;
float y=imgList.get(i).cornerPos.y;
pgTotal.image(colorImg, x, y);
}
pgTotal.endDraw();
image(pgTotal, 0, 0, width, height);
}
}
class NewImg {
PVector cornerPos;
PImage img;
NewImg () {
}
void trimImage(PImage image) {
image.loadPixels();
int width = image.width;
int height = image.height;
int left = 0;
int top = 0;
int right = width - 1;
int bottom = height - 1;
int minRight = width - 1;
int minBottom = height - 1;
top:
for (; top < bottom; top++) {
for (int x = 0; x < width; x++) {
if (alpha(image.get(x, top)) != 0) {
minRight = x;
minBottom = top;
break top;
}
}
}
left:
for (; left < minRight; left++) {
for (int y = height - 1; y > top; y--) {
if (alpha(image.get(left, y)) != 0) {
minBottom = y;
break left;
}
}
}
bottom:
for (; bottom > minBottom; bottom--) {
for (int x = width - 1; x >= left; x--) {
if (alpha(image.get(x, bottom)) != 0) {
minRight = x;
break bottom;
}
}
}
right:
for (; right > minRight; right--) {
for (int y = bottom; y >= top; y--) {
if (alpha(image.get(right, y)) != 0) {
break right;
}
}
}
cornerPos=new PVector(left, top);
img= image.get(left, top, right - left + 1, bottom - top + 1);
}
}