Writing an Algorithm to update values?

edited March 2016 in Questions about Code

Hello everyone, I am writing a code that has turned the pixels of an image into noise. While I am excited that I managed to get the code to scramble an image, I was wondering if anyone had an idea of how to slow the effect down so that when the code is run, the effect is more apparent; rather than the sketch opening, the picture appearing for a moment, and then is scrambled. An algorithm that would slowly update the RGB values- because I am really stumped. Any other suggestions aside from frame rate? Something that could happen in the draw loop? Code follows! Thanks!


PImage img;       // The source image
color c;
int r;
int g;
int b;
int loc;

void setup() {
  size(564, 424);
  img = loadImage("ring.jpg"); // Load the image
image(img,0,0);
  loadPixels();
}

void draw() {

  updatePixels();


  // Begin loop for columns
  for (int i = 0; i < img.width; i++ ) {
    // Begin loop for rows
    for (int j = 0; j < img.height; j++ ) {


    //focus on an algorithm to slowly update the RGB values
    if(r<255){r=r+10;}else {r=0;}
     if(b<255){b=b+10;}else {b=0;}
     if(g<255){g=g+10;}else {g=0;}

    // print("R:");println(r);
     //print("G:");println(r);
     //print("B:");println(r);

      loc = (int)random(236139);     

     //loc=i+j*img.width;      

    /*  if(loc<239135)
      {
      loc++;       }
      else{loc=0;}*/

      c = color(r,g,b);

      pixels[loc]=c;       
      //println(loc);

      }//j
  }//i


}
Tagged:

Answers

  • Answer ✓

    Here is an example that you might to try. The picture can be downloaded from below.

    PImage london;
    int np = 1000; // number of pixels to update per frame
    
    void setup() {
      size(500, 324); // the ame size as the image
      london = loadImage("london.jpg");
    }
    
    void draw() {
      // Update some picels
      london.loadPixels();
      for (int i = 0; i < np; i++) {
        // Select random pixel
        int idx = int(random(london.pixels.length));
        int argb = london.pixels[idx];
        // Fast way to get argb values in range 0-255
        // for a pixel. The alpha is shown but not altered
        // in this example
        int alpha = argb >>> 24;
        int red = (argb >> 16) & 0xFF;
        int green = (argb >> 8) & 0xFF;
        int blue = argb & 0xFF;
        red = (red + 10) % 255;
        green = (green + 10) % 255;
        blue = (blue + 10) % 255;
        // calculate updated argb value
        argb = (alpha << 24) | (red << 16) | (green << 8) | blue;
        // update the pixel
        london.pixels[idx] = argb;
      }
      // Stote the changes made to the image before displaying it
      london.updatePixels();
      // Now display updated image
      image(london, 0, 0);
    }
    

    london

  • Oh my goodness! This is amazing! this is what I was trying to do! Thank you!!!

Sign In or Register to comment.