and to confuse you more:
- the color is a integer?
so the result of a bitshift should goto integer, not real
- the result of red is float ( need to, as you might use like colorMode(RGB,1.0); )
i changed the test little bit and make many of them,
and see the problem is actually with
c=pixels[j]
esp. if c is global color.
find my data in the respective line
and note my win7 PC is about double as slow as your hardware.
not to mention the RPI
final int pixSize = 1000 * 1000;
final int manyruns = 10000, repeat = 10;
// global vars
long start, end, dtime, avg = 0;
int r1 = 0;
float r2 = 0;
color c = 0;
void setup() {
size(1000, 1000);
background(20);
/*
// local vars
long start, end, dtime, avg = 0;
int r1 = 0;
float r2 = 0;
color c = 0;
*/
for (int k = 0; k < repeat; k++) {
loadPixels();
start = millis();
for (int i = 0; i < manyruns; i++) {
for (int j = 0; j < pixSize; j++) { // only loop avg 4 ||global vars avg 5
//c = pixels[j]; // only c avg 3 ||global vars avg 6848
//r1 = c >> 16 & 0xFF; // int shift avg 629 || NO c=pixels[j] 3 ||global vars avg 7176 ||globalvars NO c=pixels[j] 656
//r2 = c >> 16 & 0xFF; // float shift avg 632 || NO c=pixels[j] 3 ||global vars acg 7775 ||globalvars NO c=pixels[j] 640
//r2 = red(c); // red avg 626 || NO c=pixels[j] 6 ||global vars avg 8039 ||globalvars NO c=pixels[j] 643
}
}
end = millis();
dtime = end - start;
avg += dtime;
println("k "+k+" dtime "+dtime);
}
//rect(r, r, r, r);
println("avg "+avg/repeat);
exit();
}
update
now check again with all that features inside of draw ( and show FPS )
// https://discourse.processing.org/t/get-just-one-channel-of-rgb-photos/10732
// show red channel only
// compare red(c) with int r2 = c >> 16 & 0xFF
// check @jb4x https://discourse.processing.org/t/get-just-one-channel-of-rgb-photos/10732/11
// add test data from run on Raspberry Pi ( RPI )
String infile = "data/sunflower.jpg"; // format 1000 * 1000 with paint
PImage img;
//color c;
void setup() {
size(1000, 1000);
img = loadImage(infile);
println("picture: "+infile+" w * h "+img.width+" * "+img.height);
frameRate(120);
}
void make_red_pixels() {
img.loadPixels(); // FPS: 67
for (int i=0; i< img.pixels.length; i++) { // FPS: 67
img.pixels[i] = color(red(img.pixels[i]), 0, 0); // FPS: 21 // RPI FPS 4
// img.pixels[i] = color(img.pixels[i] >> 16 & 0xFF, 0, 0); // FPS: 21
// c = img.pixels[i]; // FPS: 63 // recheck on the global variable problem? NOT here
// img.pixels[i] = color(red(c), 0, 0); // FPS: 21
// img.pixels[i] = color(c >> 16 & 0xFF, 0, 0); // FPS: 21
;
}
img.updatePixels();
}
void make_red_setget() {
for (int i=0; i< img.width; i++) for (int j=0; j< img.height; j++) img.set(i,j,color(red(img.get(i,j)),0,0)); // FPS: 15 //RPI FPS 2
}
void draw() { // move to draw and measure FPS
surface.setTitle("my colors, FPS "+(int)frameRate+" for "+img.pixels.length+" pix");
make_red_pixels();
// make_red_setget();
image(img, 0, 0); // FPS: 80
}
/*
img.pixels[i] = color(0,green(c),0);
img.pixels[i] = color(0,0,blue(c));
img.pixels[i] = color(green(c),0,red(c)); // re-mix
int r2, g2, b2;
r2 = c >> 16 & 0xFF;
g2 = c >> 8 & 0xFF;
b2 = c & 0xFF;
*/
test with a 1000 * 1000 picture ( old win 7 PC and RPI )
- a- the global variable color c problem i not see there again
- b- the bit shift speedup v.s. red() is a myth ( or in the whole context can be neglected )
- c- the pixels[] v.s. set ( get ) is faster like 21/80 to 15/80