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 › dynamic mask, pixels array
Page Index Toggle Pages: 1
dynamic mask, pixels array (Read 1278 times)
dynamic mask, pixels array
Feb 28th, 2007, 10:59pm
 
Hi

I'm having trouble using the pixels array for a mask. I can set the mask initially using loadPixels and updatePixels... but if I want to move the mask I run into problems.

There aren't any good examples of dynamic masking using pixels on the site soo

Here's my code
http://a.parsons.edu/~ivy/EWT/week6/maskDyn/applet/
Re: dynamic mask, pixels array
Reply #1 - Mar 1st, 2007, 9:30am
 
The first problem in your code is to load the image every time the mouse moved which is slowing down your sketch. It's better to use 2 PImages. One original and one which is cloned from the first every time the mouse moved. Then set the mask to the second image.

Another thing, you always create a new mask with a ellipse from the same size. A clever way would be to create the mask at the start and then set the mask only to an part of the image. So you not need to clone the whole original image but a part from the same size as your mask image. And set the mask to this piece and then add this image to the sketch. I've made a think like this here:

http://www.eskimoblood.de/applets/applet24/applet24.pde

Its written in an old processing version but the method should also work in 124. The interesting part are this 3 lines:

Code:

BImage pic=back.get(x,y,theSize,theSize);//get the image part
pic.mask(gBuffer);//set the mask
image(pic,x,y);// add the masked image back to the screen

Note that there will be thrown an error if the mask image is bigger then the image you want to masked. This will happens at the left and bottom border.
Re: dynamic mask, pixels array
Reply #2 - Mar 1st, 2007, 9:48am
 
Smiley andreas was faster ...

Code:

PImage img1;
PImage img2;
PImage masked;

float xPos ;
float yPos ;

void setup() {

img1 = loadImage("IMG_0205a.jpg");
img2 = loadImage("mask.png");
// a black image with a filled white ellipse
// size width/4, height/4
masked = new PImage(img2.width, img2.height);

size(500,375);

xPos = width/2;
yPos = height/2;

ellipseMode(CENTER);
ellipse(xPos, yPos, width/4,height/4);
}

void draw() {
background(0);
applyMask();
image(img1, 0,0);
image(masked,xPos-img2.width/2,yPos-img2.height/2);
noFill();
ellipse(xPos, yPos, width/4,height/4);
}

void applyMask() {
xPos = mouseX;
yPos = mouseY;
// this is what andreas was doing translated to Processing 124
masked.copy(img1, mouseX-img2.width/2,mouseY-img2.height/2,img2.width,img2.height, 0,0,img2.width, img2.height);
masked.mask(img2);
}


F
Re: dynamic mask, pixels array
Reply #3 - Mar 2nd, 2007, 7:06pm
 
Thanks for the responses.
I will try these code strategies tonight.
Re: dynamic mask, pixels array
Reply #4 - Mar 4th, 2007, 10:40pm
 
So I guess there is no way to do this with the pixels array?
Re: dynamic mask, pixels array
Reply #5 - Mar 4th, 2007, 11:20pm
 
sure there is ... using an image is just easier.

you can set a pixels ( ARGB ) alpha value by:

0xBB000000 + (pixels[i] & 0x00FFFFFF)
// assume your pixel was 0xFF990099
// now it's 0xBB990099

this masks out the alpha byte from the pixel and applies a new one.

in your case you might want to do something like:

(alphas[i] & 0xFF000000) + (pixels[i] & 0x00FFFFFF)
// or do
color( red(pixels[i]), green(pixels[i]), blue(pixels[i]), alpha(alphas[i]) );

which will use an alpha value from an array (of ARGB pixels).

F
Re: dynamic mask, pixels array
Reply #6 - Mar 5th, 2007, 7:22am
 
well this is what I ended up with.
runs alright...
see anywhere to optimize?

PImage img;
PImage masked;
color cp;
int xPos, yPos;
boolean doCopy = false;
int maskW, maskH;
int[] maskArray;

void setup() {
 
 img = loadImage("IMG_0205a.jpg");
 size(img.width, img.height);
 
 maskW = 190;
 maskH = 100;
 int arraySize = maskW * maskH;
 maskArray = new int[arraySize];

 createMaskArray();
 xPos = width/2;
 yPos = height/2;
 
 masked = new PImage(maskW, maskH);
 copyApplyMask();
}

void draw() {
 
 //image(img, 0, 0);
 background(0);
 image(masked, xPos - maskW/2, yPos - maskH/2);

}

void mouseMoved() {
 if (!(mouseX < maskW/2 || mouseX > width - maskW/2))
 {
   xPos = mouseX;
   doCopy = true;
 }
 if (!(mouseY < maskH/2 || mouseY > height - maskH/2))
 {
   yPos = mouseY;
   doCopy = true;
 }
 if (doCopy) copyApplyMask();
 doCopy = false;
}

void createMaskArray() {
 background(0);
 fill(255);
 ellipseMode(CORNER);
 ellipse(0, 0, maskW, maskH);
 
 loadPixels();
 for (int x = 0; x < maskW; x++)
 {
     for (int y = 0; y < maskH; y++)
     {
       maskArray[x + (y * maskW)] = pixels[x + (y * width)];
     }
 }
 updatePixels();
}

void copyApplyMask() {
 // this is what andreas was doing translated to Processing 124
 masked = img.get(xPos - maskW/2, yPos - maskH/2, maskW, maskH);
 masked.mask(maskArray);
}
Page Index Toggle Pages: 1