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 & HelpPrograms › Acces to PImage.pixels within draw()
Page Index Toggle Pages: 1
Acces to PImage.pixels within draw() (Read 1228 times)
Acces to PImage.pixels within draw()
Jun 29th, 2005, 3:51pm
 
Hi everybody.
I'm trying to acces the pixels Array from a PImage within a draw loop with no luck.

Check this dummy code:
Code:

int w = 320;
int h = 240;

PImage p = new PImage(w, h);

void setup(){
size(w, h);

background(255, 255, 255);

for (int i = 0; i < (w*h); i++){
if (i%2 == 0) p.pixels[i] = color(255, 0, 0);
}
image(p, 0, 0);
//Now I've got a bunch of white and red lines.
//Everything is fine.
}

void draw(){
for (int i = 0; i < (w*h); i++){
if (i%2 != 0) p.pixels[i] = color(0, 255, 0);
}
image(p, 0, 0);
//I expected the white lines to turn green, but...
}


I've managed to workaround this with this code
Code:

void draw(){
image(p, 0, 0);
loadPixels();
for (int i = 0; i < (w*h); i++){
if (i%2 != 0) pixels[i] = color(0, 255, 0);
}
updatePixels();
}


but this seems to me a bit on the ugly side.

Am I missing something?
TIA
Re: Acces to PImage.pixels within draw()
Reply #1 - Jun 29th, 2005, 5:45pm
 
As of recent releases you have to use loadPixels() and udpatePixels() if you want to use the img.pixels[] array. This was to speed things up for users who didn't need to access the img.pixels[] array as far as I understand.
Re: Acces to PImage.pixels within draw()
Reply #2 - Jun 29th, 2005, 7:58pm
 
So, let's say I want to draw an image, grab another image and update the first one with information of the second.

For example, I want to turn an image into another pixel to pixel on every loop.

Am I suposed to do something along these lines?

Code:

int w = 320;
int h = 240;

int total = w*h;
int counter = 0;

PImage a;
PImage b = new PImage(w, h);

color myColors[] = new color[w*h];

void setup(){
size(w, h);
a = loadImage("myImage.jpg");
}

void draw(){
if (counter < total){
image(a, 0, 0);
loadPixels();
//Copy pixels to an array
arraycopy(pixels, myColors);

//Now load pixels from image b
image(b, 0, 0);
loadPixels();
//And do the replace
for (int i = 0; i < counter; i++) pixels[i] = myColors[i];

updatePixels();
}
}



It sounds like a bit of overhead, doesn't it?
Re: Acces to PImage.pixels within draw()
Reply #3 - Jun 29th, 2005, 8:09pm
 
nope, that would be really slow. call loadPixels() on the image itself, and then use image.pixels[] to access its pixels.

then, the quickest way to composite would be to make a PImage "c" the same way you made "b", and use that as the target. then call c.updatePixels() and image(c, 0, 0) to draw it to the screen. or if you aren't using tint() or sizing the image differently than its original, use set(c, 0, 0) for more speed.

also fyi.. loadPixels() and updatePixels() aren't required for PImage right now, but will be in the future, so you should get in the habit of using them.

using size(320, 240, P3D) makes things faster if you're just doing pixel stuff.

and it's recommended that you don't use size(w, h) because it won't be able to read the size when you export it to an applet. do something like this instead:

Code:
int total;
int counter = 0;

PImage a, b, c;
color myColors[];

void setup() {
size(320, 240);

a = loadImage("myImage.jpg");
b = new PImage(width, height);
c = new PImage(width, height);


total = width * height;
myColors = new color[total];
}
Re: Acces to PImage.pixels within draw()
Reply #4 - Jun 29th, 2005, 8:13pm
 
...actually, if you use P3D, you can even skip using a separate image 'c', and use loadPixels() and updatePixels() on the main image surface and it'll be speedier than using set() or image() to draw your new image.
Re: Acces to PImage.pixels within draw()
Reply #5 - Jun 30th, 2005, 11:15am
 
OK, I'll give it a try.

Thanks!
Page Index Toggle Pages: 1