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 › dynamically masking function
Page Index Toggle Pages: 1
dynamically masking function (Read 226 times)
dynamically masking function
Mar 12th, 2009, 2:52am
 



hello, ive made a little program that analyses an imagen and then mask some pixels based on the luminosity . It works in this way:the program iterates over all the pixles on the image and if the luminosity of each pixel is less than  some threshold it applies a mask to the pixel to make it transparent. The problem im having now is that i need to change dynamically the images with keys so when you press the key "a" it reads another pic and do the same procedure but the thing is that im getting this message when i try to change the pic:
The PImage used with mask() must be the same size as the applet.
I think this is because i have "img.mask(maskimg);" in the draw function and when I press "a" it calls to the function that analyses a new pic but it looks like it calls the "img.mask(maskimg);" from draw before it finishes to analyse the pic , do anybody have any idea of how to fix this?

thanks

here is the code:


int x;

float a = 0.0;
float inc = TWO_PI/25.0;

PImage img;

color luminosidad;
float luminosidad_total;
color [] imagenn ;
int[] maskimg ;

float sube = 1;
float tamano = 11;
int moco;

float cambia;
float switcher;
float x_random = 44;

void setup () {

 background(162, 166, 196) ;
 size (480, 480) ;
 img = loadImage("glitch.jpg");
  maskimg = new int[img.pixels.length];
  loadPixels();
       
  for (int y = 0; y<img.height * img.width ;y++){

 luminosidad =  img.pixels[y];

 maskimg[y]=  int(alpha(img.pixels[y]))/34;

   luminosidad_total =( ((red(luminosidad)) + (green(luminosidad)) + (blue(luminosidad)))/3);
       if(luminosidad_total<214){
       // porque no funca lo de abajo?
       maskimg[y] = 0;  
             
       }
       else {
           maskimg[y] = 255;  
       }    
           }
updatePixels();


}
void draw()
{

 moco = moco + 1;
x = x+ 1;
a = a + inc;

img.mask(maskimg);  
image(img, x  , 8+sin(a/5)*27 , 33 * 4, 33 * 4);

image(img, x + x_random  *328, 120  , tamano * 3, tamano * 3);


if( moco % 280 == 0 ) {
 tamano = random(110);
 x_random = 0;
 switcher = int(random(2));

}
if (switcher == 0 )
 {
   cambia = 0;
   
 }
 else
 {
   cambia =  180+sin(a/4)*7;
   
 }

if (x > 400 )

{
x = -200;
}
}
 
void keyPressed(){
     if (key =='a' ){
     updateimagen();
    }  
   }
 
void updateimagen(){
 
img = loadImage("glitchdos.jpg");
 


loadPixels();
       
  for (int y = 0; y<img.height * img.width ;y++){
   
 luminosidad =  img.pixels[y];
 maskimg[y]=  int(alpha(img.pixels[y]))/34;

 luminosidad_total =( ((red(luminosidad)) + (green(luminosidad)) + (blue(luminosidad)))/3);
       if(luminosidad_total<114){
 
       maskimg[y] = 0;  
             
       }
       else {
           maskimg[y] = 255;  
       }
 
           }
         
updatePixels();

// img.mask(maskimg);

}
Page Index Toggle Pages: 1