Dynamic Image Pixel Modulation

Hello folks!

Inspired by:
Using Pixels[] Array over Points • trying to move Pixels(XY) off screen like Points

// Dynamic Image Pixel Modulation
// v1.0.1
// GLV 2025-8-19
//
// Inspired by topic:
// https://discourse.processing.org/t/using-pixels-array-over-points-trying-to-moving-pixels-xy-off-screen-like-points/24309

PImage img, img2, img3;
float mod;
int modIndex;
int choice = 1;

int sz;        // This is for width and height

void settings()
  {
  sz = (int) (255/cos(radians(45))); 
  println(sz);
  size(sz, sz, P2D);
  }

void setup() 
  {
  //size(400, 400, P2D); // Moved to settings
  frameRate(60);
  img = loadImage("http://learningprocessing.com/code/assets/sunflower.jpg");
  img.loadPixels(); 
  println(img.width, img.height);
  background(0);
  
  img2 = createImage(sz, sz, ARGB);
  img2.loadPixels();
  for (int i = 0; i < img2.width*img2.height ; i++)
    {
    img2.pixels[i] = color(0, 255);
    }
  img2.updatePixels();
  }

void draw() 
  {  
  //Sinusoidal modulation from 0 to 2
  mod = 1+1*sin(modIndex*TAU/500-TAU/4);
  
  switch(choice) 
    {
    case 1: 
      //println("Plot 1");
      plot1();
      break;
    case 2: 
      //println("Plot 2");
      plot2();
      break;
    case 3: 
      //println("Plot 3");
      plot3();
      break;
    case 4: 
      //println("Plot 4");
      plot4();
      break;      
    default:
      println("Woops!"); 
      break;
    }
  modIndex++;
  surface.setTitle("Plot: " + choice + "  FRate: " + nf(frameRate,0,1));
  }
  
//**********************************************************************************

void plot1()
  {
  fill(0, 5);
  noStroke();
  rect(0, 0, width, height);  
  
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.get(x, y);
      float sz = (brightness(c)/255)*mod;
      set(int(x*sz), int(y*sz), c);      
      }
    }  
  }

//**********************************************************************************

void plot2()
  {  
  fill(0, 5);
  noStroke();
  rect(0, 0, width, height);  
  
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.pixels[x+img.width*y];
      float sz = (brightness(c)/255)*mod;
      set(int(x*sz), int(y*sz), c);
      }
    }
  }

//**********************************************************************************  
  
void plot3()
  {
  fill(0, 5);
  noStroke();
  rect(0, 0, width, height);    
    
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.pixels[x+img.width*y];
      float sz = (brightness(c)/255)*mod;
      //sz = 1; // Try this to see original image.
      img2.pixels[int(x*sz)+int(y*sz)*img2.width] = c;    
      }
    }
  img2.updatePixels();  
  image(img2, 0, 0);     
  }
  
//**********************************************************************************  
  
void plot4()
  {
  // Sets pixel array to a fixed color with transparency  
  for (int i = 0; i < img2.width*img2.height ; i++)
    {
    img2.pixels[i] = color(0, 5);
    }
  
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.pixels[x+img.width*y];
      float sz = brightness(c)/255*mod;
      //sz = 1; // Try this to see original image.
      //c = color(255, 255, 0); //Test with fixed color
      img2.pixels[int(x*sz)+int(y*sz)*img2.width] = c;  // x + y*width     
      }
    }
  img2.updatePixels();  
  image(img2, 0, 0);    
  }  
  
void keyPressed()
  {
  modIndex = 0;
  
  //if (choice>4) choice = 1;
  //choice++;
  
  choice = choice%4;
  choice++;
  }

:)

2 Likes