Loading...
Logo
Processing Forum
Hi all...

I feel like I'm missing something very simple here. I'm creating a simple image of random colored pixels and want the red value of each pixel to increase step by step. For the life of me I can't figure out why I'm not seeing the image change tint. What am I missing? Here's the code...


PImage img;

void setup()
{
  frameRate(5);
  size(200, 200); 
  img = createImage(100, 100, RGB);
  for(int i=0; i < img.pixels.length; i++) {
    img.pixels[i] = color(random(0,255),random(0,255), random(0,255));
  }
}

void draw()
{
  background(204);
  for (int i = 0; i < img.pixels.length; i++) {

    float r = red(img.pixels[i]);
    float g = green(img.pixels[i]);
    float b = blue(img.pixels[i]);


    if (r < 254.0) {
      r = r + 1.0;
    }
    color c;
    c = color(r,g,b);
    img.pixels[i] = c;
  }
  image(img, 50, 50);
  println(frameCount);
}

Replies(2)

You're missing img.loadPixels() & img.updatePixels() before and after your for().

http://www.processing.org/reference/loadPixels_.html
Certain renderers may or may not seem to require loadPixels() or updatePixels(). However, the rule is that any time you want to manipulate the pixels[] array, you must first call loadPixels(), and after changes have been made, call updatePixels(). Even if the renderer may not seem to use this function in the current Processing release, this will always be subject to change.
Copy code
  1. PImage img;
  2. void setup()
  3. {
  4.   frameRate(5);
  5.   size(200, 200);
  6.   img = createImage(100, 100, RGB);
  7.   for(int i=0; i < img.pixels.length; i++) {
  8.     img.pixels[i] = color(random(0,255),random(0,255), random(0,255));
  9.   }
  10. }
  11. void draw()
  12. {
  13.   background(204);
  14.   img.loadPixels();
  15.   for (int i = 0; i < img.pixels.length; i++) {
  16.     float r = red(img.pixels[i]);
  17.     float g = green(img.pixels[i]);
  18.     float b = blue(img.pixels[i]);
  19.     if (r < 254.0) {
  20.       r = r + 5.0;
  21.     }
  22.     color c;
  23.     c = color(r,g,b);
  24.     img.pixels[i] = c;
  25.   
  26.   }
  27.   img.updatePixels();
  28.   image(img, 50, 50);
  29.   println(frameCount);
  30. }
Woot! That's working, thanks!

Yeah, I didn't think I'd need those functions in this example. Thanks again!