We are about to switch to a new forum software. Until then we have removed the registration on this forum.
If I have lets say 100 PGraphics and I want to get only 1 pixel at (x,y) every frame, what is the fastest and optional way by knowing that
get(x,y);
is slow
pixels[I]
too
and using a java robot .getPixel() is perfect for the speed but only gets the final image after draw is executed
here is a exemple
PGraphics[] pgs = new PGraphics[100];
Robot robot;
void setup() {
size(500,500);
//
for(int i=0; i<100; i++) {
pgs = createGraphics(width, height);
}
try { robot = new Robot(); } catch(Exception e) {}
}
void draw() {
background(0);
for(int i=0; i<100; i++) {
image(pgs,0,0);
int pix = robot.getPixelColor(width/2,height/2).getRGB;
println(pix);
}
background(255);
}
what is the fastest solution to get a pixel at every frame ?
Answers
get(x,y), loadPixels(), etc... are all rather slow, but it also depends on the used renderer (OpenGL or Java).
one trick is to render all the pixels you want to to another pgraphics (pg_read in this example), and then read the pixels of that buffer at once.
the above example can be improved by using the clip() function:
The original code doesn't use the array, just keeps overwriting one image. Line 10.
It also copies that image to the screen 100 times when it can access the pixels without doing that.
In short, no wonder it is slow.
As koogs said:
That one line:
is probably very time consuming, and accomplishes nothing in the loop (or ever after you blank it out).
thank you for introducing me to the clip() function and for you examples, now that I have this technic that works perfectly.
PROBLEM + SOLUTION
How could I replace all the pixels color in all of these 100 graphics by a X color if the currently checking pixel color is not equal to a color Y in a fast way ? Because in my main sketch I want to know if the pixel at (x,y) is == to a color, so if the color is alpha = 0, then it would be considered as not a color, and because we merge every pgraphics in 1 graphics, we would need to replace all these alpha != 0 color by a "Color representing this Graphics" so I would need a system to store the current (i) variable from the for loop in 3 channels and 255 * 255 * 255 = 16581375, its limited but it is too high to go higher than that number in my case.
WHAT I NEED
Why I need to do all this is basically to know if pixel at (x,y) is on a graphics and if yes which one.
sorry for my errors I speak French
okay ! never mind for the theory I had, thank you again T_D, the clip() function all arranged and I have a fully functional 60 fps sketch :D