For some reason the brightness example is egregiously slow in Python mode. Golan and I went through the code to see if there was a particular line or function that was causing the slowdown, but we couldn't find a single culprit.
In Python mode the outer for loop takes about 700ms to run vs. 6ms in Java mode.
"""
Brightness
by Daniel Shiffman.
This program adjusts the brightness of a part of the image by
calculating the distance of each pixel to the mouse.
"""
img = None
runningavg = 0;
total = 0;
def setup():
size(640, 360)
frameRate(30)
img = loadImage("moon-wide.jpg")
img.loadPixels()
# Only need to load the pixels[] array once, because we're only
# manipulating pixels[] inside draw(), not drawing shapes.
loadPixels()
def draw():
now = millis()
for x in range(img.width):
for y in range(img.height):
# Calculate the 1D location from a 2D grid
loc = x + y * img.width
# Get the R,G,B values from image
r = 0
g = 0
b = 0
r = red(img.pixels[loc])
#g = green (img.pixels[loc])
#b = blue (img.pixels[loc])
# Calculate an amount to change brightness based on proximity to
# the mouse
maxdist = 50 # dist(0,0,width,height)
d = dist(x, y, mouseX, mouseY)
adjustbrightness = 255 * (maxdist - d) / maxdist
r += adjustbrightness
#g += adjustbrightness
#b += adjustbrightness
# Constrain RGB to make sure they are within 0-255 color range
r = constrain(r, 0, 255)
#g = constrain(g, 0, 255)
#b = constrain(b, 0, 255)
# Make a color and set pixel in the window
# color c = color(r, g, b)
c = color(r)
pixels[y * width + x] = c
then = millis()
total += then - now
runningavg += 1
println(total/runningavg)
updatePixels()
/**
* Brightness
* by Daniel Shiffman.
*
* This program adjusts the brightness of a part of the image by
* calculating the distance of each pixel to the mouse.
*/
// The next line is needed if running in JavaScript Mode with Processing.js
/* @pjs preload="moon-wide.jpg"; */
PImage img;
int runningavg = 0;
int total = 0;
void setup() {
size(640, 360);
frameRate(30);
img = loadImage("moon-wide.jpg");
img.loadPixels();
// Only need to load the pixels[] array once, because we're only
// manipulating pixels[] inside draw(), not drawing shapes.
loadPixels();
}
void draw() {
float now = millis();
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++ ) {
// Calculate the 1D location from a 2D grid
int loc = x + y*img.width;
// Get the R,G,B values from image
float r,g,b;
r = red (img.pixels[loc]);
//g = green (img.pixels[loc]);
//b = blue (img.pixels[loc]);
// Calculate an amount to change brightness based on proximity to the mouse
float maxdist = 50;//dist(0,0,width,height);
float d = dist(x, y, mouseX, mouseY);
float adjustbrightness = 255*(maxdist-d)/maxdist;
r += adjustbrightness;
//g += adjustbrightness;
//b += adjustbrightness;
// Constrain RGB to make sure they are within 0-255 color range
r = constrain(r, 0, 255);
//g = constrain(g, 0, 255);
//b = constrain(b, 0, 255);
// Make a new color and set pixel in the window
//color c = color(r, g, b);
color c = color(r);
pixels[y*width + x] = c;
}
}
float then = millis();
total += then - now;
runningavg += 1;
println(total/runningavg);
updatePixels();
}