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.
Page Index Toggle Pages: 1
img.mask() (Read 2087 times)
img.mask()
Jun 2nd, 2005, 2:39pm
 
In this program I want image 2 to gradually disappear showing image 1 underneath. But it does not work. Is not img.mask() supposed to do this job?

PImage img_1, img_2;
int[] intMask;
int m;

void setup()
{
 size(300,222);
 img_1 = loadImage("Gustav_1.jpg");
 img_2 = loadImage("Gustav_2.jpg");
 intMask = new int[width*height];
 for (int i=0; i < width*height; i++) {
   intMask[i] = 255;
 }
 m = 0;
}

void draw()
{
 intMask[m] = 0;

 image(img_1, 0, 0);
 img_2.mask(intMask);
 image(img_2, 0, 0);

 m++;
 if (m >= width*height) {
   noLoop();
 }
}
Re: img.mask()
Reply #1 - Jun 2nd, 2005, 6:44pm
 
well.. i got it working.. but it's a really bad way..
if you reload the images inside draw.. as in:
img_2 = loadImage("Gustav_2.jpg"); // in top of draw

it should work..

-seltar
Re: img.mask()
Reply #2 - Jun 2nd, 2005, 7:02pm
 
No, not for me! Even if it did work, the picture will be loaded more than 10.000 times. Bad programming, isn't it?
Re: img.mask()
Reply #3 - Jun 2nd, 2005, 7:07pm
 
well, yeah .. :p but that was the only way i got it working.. the image just doesn't want to update the alphavalues without reloading first.. for some wierd reason..

Code:

PImage img_1, img_2;
int[] intMask;
int m = 0;

void setup()
{
img_1 = loadImage("Hema.jpg");
img_2 = loadImage("lust.jpg");
size(img_2.width,img_2.height);
intMask = new int[width*height];
for (int i=0; i < width*height; i++) {
intMask[i] = color(255,255,255);
}
}

void draw()
{
img_2 = loadImage("lust.jpg");
intMask[m++] = color(0,0,0);
image(img_1, 0, 0);
img_2.mask(intMask);
image(img_2, 0, 0);
if (m >= width*height) {
noLoop();
}
}
Re: img.mask()
Reply #4 - Jun 2nd, 2005, 11:40pm
 
try calling updatePixels() after mask() and see if that gets things working again.

also, you should just be using tint() to set the alpha of the image, not mask(), which is intented for blocking out portions of the image and that sort of thing.
Re: img.mask()
Reply #5 - Jun 3rd, 2005, 11:49am
 
My program above is just a simplified version to show the problem. My real intention is to hide picture 2 by means of mouse movements. I have presently solved that by using loadPixels, pixels[] and updatePixels, but the mask() method could have been a much simpler way. It seems you cannot change the mask within the draw function.
Re: img.mask()
Reply #6 - Jun 3rd, 2005, 12:08pm
 
What, like this?:
Code:

PImage one,two;
void setup(){
size(640,480,P3D);
one = loadImage("trippybobo.jpg");
two = loadImage("glowbobo.jpg");
}
void draw(){
background(one);
beginShape(QUADS);
fill(255,255,255,(255.0/width)*mouseX);
texture(two);
vertex(0,0,0,0);
vertex(width,0,width,0);
vertex(width,height,width,height);
vertex(0,height,0,height);
endShape();
}

Probably not the proper solution you were thinking of but texture mapping works a treat and is easy to get your head around.

Update:
Code:

PImage one,two;
void setup(){
size(640,480,P3D);
one = loadImage("trippybobo.jpg");
two = loadImage("glowbobo.jpg");
}
void draw(){
background(one);
tint(255,255,255,(255.0/width)*mouseX);
image(two,0,0);
}

whoops :[
Re: img.mask()
Reply #7 - Jun 3rd, 2005, 2:40pm
 
Thanks for suggestions. The last example from st33d is a very good example of compact coding. My own code is much longer, and I feel it is unnessecarely long, even if it has some additional features. You can view the result here: http://www.karl-georg.net/nostalgi/Gustav.htm.

Code:

// Nostalgi
// Två bilder, en nostalgibild (a) och en modern (b)
// Vektorn cM fungerar som mask för bild b.
// Om cM = 255 visas bild b, om cM = 0 visas bild a.
// aP och bP är pixelvektorer.

PImage a, b;
int[] aP, bP, cM;

void setup()
{
size(300,222); // Size måste rättas efter de två, lika stora bilderna.
a = loadImage("Gustav_1.jpg");
b = loadImage("Gustav_2.jpg");

aP = new int[width*height];
bP = new int[width*height];
cM = new int[width*height];

for (int i=0; i < width*height; i++) {
aP[i] = a.pixels[i];
bP[i] = b.pixels[i];
cM[i] = 255;
}
framerate(30);
image(b, 0, 0); // Visa den moderna bilden.
}

void draw()
{
int ii = mouseX;
int jj = mouseY;
// Är musen inom bildarean (exkl rad 0 och kol 0)?
boolean inom = (ii > 0) && (ii < width) && (jj > 0) && (jj < height);

loadPixels();

for (int i=0; i < width; i++) {
for (int j=0; j < height; j++) {
int k = width*j + i;
int d = (i-ii)*(i-ii) + (j-jj)*(j-jj);
if ((d <= 1600) & inom) {
cM[k] = 0; // Visa nostalgibild
} else if (cM[k] < 253) {
cM[k] += 2; // Tona ned nostalgibild
} else { cM[k] = 255;} // Visa modern bild

color ca = aP[k]; // Isolera färgkomponenter
color cb = bP[k];
int ra=ca>>16&0xff;
int ga=ca>>8&0xff;
int ba=ca&0xff;
int rb=cb>>16&0xff;
int gb=cb>>8&0xff;
int bb=cb&0xff;
// Väg samman med cM
int rc = ra*(255-cM[k])/255 + rb*cM[k]/255;
int gc = ga*(255-cM[k])/255 + gb*cM[k]/255;
int bc = ba*(255-cM[k])/255 + bb*cM[k]/255;
// Uppdatera skärmbildens pixels (inte de enskilda bilderna)
pixels[k] = color(rc, gc, bc);

}
}
updatePixels();
}
Re: img.mask()
Reply #8 - Jun 3rd, 2005, 3:44pm
 
Karl-Georg: That was a really cool idea, good job!

-seltar
Re: img.mask()
Reply #9 - Jun 3rd, 2005, 7:41pm
 
[hangover]
Cool idea. Doesn't look that long to me. What I've done to v3ga's blob detection library is pretty hideous but people were still telling me I'm clever when the degree show was unveiled last night. I always try to make the following program more compact, I tend to give up on the oldies.
[/hangover]
Re: img.mask()
Reply #10 - Jun 7th, 2005, 4:37am
 
That was a cool sketch :] I like it!
Page Index Toggle Pages: 1