We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › img content update
Page Index Toggle Pages: 1
img content update (Read 643 times)
img content update
Aug 8th, 2005, 8:08pm
 
I'm trying to change the contents of an image in the draw loop, but I guess my understanding of the PImage format is lacking.

For example, I want to do a variation of the noise3D example. Rather than alter the display pixels directly as in the example, I want to store the noise values in an image var and then display that image. Here's my version of that example:

-------------

PImage img;

float increment = 0.01;
float zoff = 0;
float zinc = 0.2;
   
void setup()
{
 img = loadImage("blah.jpg");
 int xsize = img.width;
 int ysize = img.height;
 size(xsize, ysize);
 background(0);

 // black out img
 loadPixels();
 for (int i=0; i<(width*height); i++) {
   img.pixels[i] = color(0,0,0);
 }
 updatePixels();
}

void draw()
{
 // load up img with new values
 loadPixels();
 float xoff = 0.0f;
 for (int x = 0; x < width; x++) {
   xoff += increment;
   float yoff = 0.0f;
   for (int y = 0; y < height; y++) {
     yoff += increment;
     float bright = noise(xoff,yoff,zoff)*255;
     img.pixels[x+y*width] = color(bright,bright,bright);
   }
 }
 updatePixels();
 // display img
 image(img,0,0);
 
 zoff += zinc;
}


--------------

My question is, why doesn't this update? I'm under the impression that I should be able to use the "img.pixels[index] = whatever" command to change the contents of img in the draw loop.

Just to clarify, what I'd like to do is:

a) initialize an image variable
b) change the contents of that image (such as with noise)
c) display that image
d) loop back to b.

Any help is appreciated!
Mark
Re: img content update
Reply #1 - Aug 8th, 2005, 8:39pm
 
erm.. as you're updating the pixel buffer of an image object, you'll have to call updatePixels on the image. so instead of:

Code:
updatePixels(); 


use this instead:
Code:
img.updatePixels(); 


Re: img content update
Reply #2 - Aug 8th, 2005, 8:43pm
 
Bingo!

I didn't understand that loadPixels() and updatePixels() could be used that way.

thanks,
Mark
Page Index Toggle Pages: 1