Hello there,
A beginner here trying desperately to understand the pixels array but feeling a bit lost / confused still. I have done a sketch using many point()
, and was able to do what seems like a melting image successfully. However the frameRate
I noticed dropped drastically.
I found this this tutorial online which makes a good comparison / case for using pixels
over point()
: processing-using-pixels-array-over-point
Using this tutorial I have tried to adapt my initial sketch using points, to plotting using pixels
instead, but I am not able to move the pixels around or off the screen like I am able to in my initial sketch. Feel very unsure and confused about this code, even as is and the way I adapted it. Thank you for taking the time to read this.
Both sketches need a PImage
, 200 x 200, I used the sunflower.jpg from Learning Processing, Dan Shiffman book. I would like to apply this to multiple images, but I am not there yet in terms of understanding, so just using one (1), for the moment… I feel I have to paste both codes (find below) to be able to address my question.
INITIAL SKETCH USING POINTS
// Flower_Photosynthesis_v3B
// v3B
// Gus Fermin 2020-09-22
PImage img;
PGraphics imgPoints;
int imageScale = 2;
void setup() {
size(500, 500, P3D);
frameRate(30);
img = loadImage("http://learningprocessing.com/code/assets/sunflower.jpg");
imgPoints = createGraphics(width, height, P3D);
imgPoints.smooth(8);
}
void draw() {
lights();
imgPoints.beginDraw();
imgPoints.fill(0, 25);
imgPoints.noStroke();
imgPoints.rect(0, 0, width, height);
img.loadPixels();
for ( int i = 0; i < img.width; i++ ) {
for ( int j = 0; j < img.height; j++ ) {
// Pixel location and color
int x = i * imageScale;
int y = j * imageScale;
int loc = (img.width - i - 1) + j*img.width;
color c = img.pixels[loc];
float sz = (brightness(c)/255)*(millis()/1000.0)/10.4;// 10.4 9.4 14.4 0.25 1.10 imageScale;
imgPoints.stroke(255, 100);
imgPoints.point((float)x*sz, (float)y*sz); // melting imgage coming from corner, intensity determined by 'sz' value
imgPoints.stroke(255);
imgPoints.point((int)x*sz+y, (int)y*sz+x, (int)x+millis()/1000.0); // melting image coming from top / center.
}
}
imgPoints.updatePixels();
imgPoints.endDraw(); /*For some reason I do not understand, the points look VERY different If this endDraw(), is INSIDE the `for` loop, vs where it is now OUTSIDE*/
translate(width/2, height/2);
imageMode(CENTER);
image(imgPoints, 0, 0);
println(frameRate);
}
SKETCH USING PIXELS ARRAY INSTEAD OF POINTS
final int THOUSAND = 1000;
final int C_POINT_COUNT = 200 *THOUSAND;
final color C_PIXEL_COLOR = #FFFFFF;
PImage img;
RandomPoints2[] pointSets = new RandomPoints2[2];
int curPointsetIndex = 0;
void setup(){
size(displayWidth, displayHeight);
img = loadImage("http://learningprocessing.com/code/assets/sunflower.jpg");
pointSets[0] = new RandomPoints2(img, C_POINT_COUNT);
pointSets[1] = new RandomPoints2(img, C_POINT_COUNT);
}
void draw(){
background(0);
// we choose different point sets
RandomPoints2 pointsToDraw = pointSets[curPointsetIndex];
curPointsetIndex++;
curPointsetIndex %= pointSets.length;
echoFrameRate("Drawing points with pixel array. ");
drawAllPointsViaPixelArray(pointsToDraw); // this will be fast
}
void echoFrameRate(String s){
fill(#FF0000);
text(s + "Framerate: " + frameRate , 10, 10);
}
void drawAllPointsViaPixelArray(RandomPoints2 rp){
loadPixels();
for(int i = 0; i < rp.pointsX.length ; i++){
nirclePoint(rp.pointsX[i], rp.pointsY[i], width, pixels, C_PIXEL_COLOR); // we need those extra parameters
}
updatePixels();
}
/*** Plots ***/
void nirclePoint(float xx, float yy, int mWidth, color[] pixelz , color pixelColor){
int ixx = (int) xx;
int iyy = (int) yy+50;
int pixelIndex = iyy * mWidth + ixx;
if ( pixelIndex < 0 || pixelIndex >= pixelz.length ){
// we just don't plot pixels outside of range
//println("Pixel index is out of bounds: " + pixelIndex + " pixel array size: " + mPixelz.length);
}
else{
pixelz[pixelIndex] = pixelColor;
}
}
//This is just array of point X & Y coordinates wrapped into a class.
class RandomPoints2 {
float[] pointsX;
float[] pointsY;
PImage img;
int imageScale = 2;
RandomPoints2(PImage img, int qty) {
this.img = img;
initPoints(qty);
}
void initPoints(int pointCount) {
pointsX = new float[pointCount];
pointsY = new float[pointCount];
this.img.loadPixels();
for (int i = 0; i < img.width; i++) {
for (int j = 0; j < img.height; j++) {
int x = i * imageScale;
int y = j * imageScale;
int loc = (img.width -i -1) + j * img.width;
color c = img.pixels[loc];
float sz = (brightness(c)/255)*(millis()/1000.0)/10.4;
pointsX[loc] = (float)x*sz+y;
pointsY[loc] = (float)y*sz+x;
}
}
}
}