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 › setting / copying pixels
Page Index Toggle Pages: 1
setting / copying pixels (Read 891 times)
setting / copying pixels
Nov 8th, 2005, 6:38pm
 
hi

How do you set pixels (or an area) of one image to pixels (or an area) of another image?  I understand the loadPixels() function below which works directly to the screen but how would I apply it to an image object?


v_width = 318;
v_height = 256;

loadPixels();
for (int i = 0; i < v_width; i++)
{
 for (int j = 0; j < v_height; j++)
 {
   pixels[j*width+i] = color(230);
 }
}
updatePixels();
Re: setting / copying pixels
Reply #1 - Nov 8th, 2005, 11:47pm
 
There are two methods. pixels[] is a property of PImage. You could also use copy() or get() / set().

If you find one of these functions does not do what it's supposed to you may want to use loadPixels() or updatePixels() accordingly. Both are methods of PImage, eg: pimage.updatePixels().
Code:

PImage one,two;
void setup(){
size(400,200);
one = new PImage(200,200);
two = new PImage(200,200);
background(0);
fill(255,0,0);
rect(100,100,100,100);
loadPixels();
//copy screen graphics into "one"
one.copy(g,0,0,200,200,0,0,one.width,one.height);
for(int i = 0; i < two.pixels.length; i++)
two.pixels[i] = one.pixels[i];
}
void draw(){
image(one,0,0);
image(two,200,0);
}
Re: setting / copying pixels
Reply #2 - Nov 9th, 2005, 2:16pm
 
st33d wrote on Nov 8th, 2005, 11:47pm:
Code:

PImage one,two;
void setup(){
 size(400,200);
 one = new PImage(200,200);
 two = new PImage(200,200);
 background(0);
 fill(255,0,0);
 rect(100,100,100,100);
 loadPixels();
 //copy screen graphics into "one"
 one.copy(g,0,0,200,200,0,0,one.width,one.height);
 for(int i = 0; i < two.pixels.length; i++)
   two.pixels[i] = one.pixels[i];
}
void draw(){
 image(one,0,0);
 image(two,200,0);
}



ok, didn't see the copy function to be honest and that was my main problem but a few questions whats the "g" in the copy function? and the loadPixels(); you use here, does it load the whole screen or just the "two" PImage?  bit confused as to when functions are applied to a PImage just created or directly to screen.

thanks
a+
gar
Re: setting / copying pixels
Reply #3 - Nov 9th, 2005, 5:22pm
 
"g" is a lesser known global variable that holds the screen graphics inside it.

loadPixels() was used to take values from "g" and put them in the pixels[] array which was blank until this point.

I made the assumption that as I had used the dot operator on the PImages one and two you would deduce what functions are methods of the applet and what functions are methods of the PImages one and two. Sorry about that.
Re: setting / copying pixels
Reply #4 - Nov 9th, 2005, 5:51pm
 
st33d wrote on Nov 9th, 2005, 5:22pm:
"g" is a lesser known global variable that holds the screen graphics inside it.

loadPixels() was used to take values from "g" and put them in the pixels[] array which was blank until this point.


ok that makes sense

st33d wrote on Nov 9th, 2005, 5:22pm:
I made the assumption that as I had used the dot operator on the PImages one and two you would deduce what functions are methods of the applet and what functions are methods of the PImages one and two. Sorry about that.


yes I could see the methods of the PImages but was unsure as to whether other methods were of the applet or images about to be created.  to me it looked similar to using GD with php where you declare a colour and then it automatically becomes the background of the image unless you explictly state a background colour - yep I'm confused but I'm learning Smiley

as a related question, I've managed to map your example onto what I was working on.  if i copy an image one into an image two which has been rotated I presume image one will have rotated to the same angle as image two  is there a way to counteract this and yet still place image one according to its top left corner - I'm guessing no  thanks for your help.

a+
gar
Re: setting / copying pixels
Reply #5 - Nov 9th, 2005, 10:36pm
 
If you want to counteract the rotation, why not just draw it to the screen rotated. The image in memory will be fine.

Failing that, most of what you draw to the screen will only be put down at the end of draw() in the order it was mentioned. If you need to grab something off of the screen you could always rotate it back, grab it, then rotate it again for presentation. When the applet draws the image, no one will know what happened. The forward and back rotation will happen before anyone sees it.

If you're thinking of doing something more complex than that, you may want to look up PGraphics3. It's not well documented because it doesn't work well, so you'll have to do some searching I'm afraid.
Re: setting / copying pixels
Reply #6 - Nov 11th, 2005, 11:09pm
 
st33d wrote on Nov 9th, 2005, 10:36pm:
If you want to counteract the rotation, why not just draw it to the screen rotated. The image in memory will be fine.

Failing that, most of what you draw to the screen will only be put down at the end of draw() in the order it was mentioned. If you need to grab something off of the screen you could always rotate it back, grab it, then rotate it again for presentation. When the applet draws the image, no one will know what happened. The forward and back rotation will happen before anyone sees it.

If you're thinking of doing something more complex than that, you may want to look up PGraphics3. It's not well documented because it doesn't work well, so you'll have to do some searching I'm afraid.


its not just a simple rotation and back, i'm trying to place an image continually on the second image which is rotating an increment on each draw() loop - so for example if it was a line i was placing from the centre to the right hand side of the image and the image rotating clockwise relative to its centre the lines would gradually build a spoked circle each new line being horizontal to the screen but not to the rotated image.  what i get is the inverse where each new line moves around the circle.

a+
gar
Re: setting / copying pixels
Reply #7 - Nov 12th, 2005, 1:51pm
 
Like this?
Code:

PImage buffer;
void setup(){
size(200,200);
buffer = new PImage(200,200);
framerate(2);
}
void draw(){
translate(100,100);
rotate(0.1);
image(buffer,-100,-100);
rect(random(100),-5,10,10);
loadPixels();
buffer.copy(g,0,0,200,200,0,0,buffer.width,buffer.height);
buffer.updatePixels();
}

I've slowed it down a lot to point out the fact that the rotation seems to degrade the image a lot.

It might be better to create an illusion of rotation somehow. Not sure.

The motion capture using EyesWeb isn't the gesture recognition thing. I've no code posted on gesture recognition as it's a new field of research for me. The motion capture isn't working too well either. Seems it only works well with lights in the dark. A lot of the code for it I've borrowed from other people. I've got to the point where I could spend hours researching how to make a new engine or just use someone else's readymades plug them all together and credit them (thankyou Ariel, v3ga, JohnG).
Page Index Toggle Pages: 1