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 › usa a rect as a mask for an image...
Page Index Toggle Pages: 1
usa a rect as a mask for an image... (Read 849 times)
usa a rect as a mask for an image...
Apr 20th, 2006, 3:50pm
 
...or paste an image into a rect,
...or display only part of an image
eg:
imageMode(CROP)
image(img, x, y, a, b, width, height)

a int or float: start x-coordinate of the image-part
b int or float: start y-coordinate of the image-part
width         : width of part to show
height        : height of part to show



hello, i am totally new to processing!

great stuff!
is it somehow possible to solve my problem?
or is there a work-around?
Re: usa a rect as a mask for an image...
Reply #1 - Apr 20th, 2006, 3:54pm
 
Have a look at http://processing.org/reference/copy_.html

I think the second format is the one you're looking for:

copy(srcImg, sx, sy, swidth, sheight, dx, dy, dwidth, dheight)
Re: usa a rect as a mask for an image...
Reply #2 - Apr 20th, 2006, 4:15pm
 
thanks for trying to help!

unfortunately this is not what i am searching...
i really need the copied/cropped image data inside a real rect/container, so that i can scale this rect and the part of the image beeing inside would scale with it...

i hope this is a bit more clear!
Re: usa a rect as a mask for an image...
Reply #3 - Apr 20th, 2006, 4:26pm
 
You could use the image as a texture for a QUAD, which you can move/resize/turn etc, and onyl use parts of the original image

Code:

beginShape(QUADS);
texture(MyImage);
vertex(100,100,20,20); //top left corner is at 100,100 on screen,
//and starts at 20,20 in the image for it's texture
vertex(200,100,80,20);
vertex(200,200,80,80);
vertex(100,200,20,80);
endShape();
Re: usa a rect as a mask for an image...
Reply #4 - Apr 20th, 2006, 4:30pm
 
ill try this!
it might work, thanks!
Re: usa a rect as a mask for an image...
Reply #5 - Apr 20th, 2006, 4:50pm
 
it works... but tooo heavy!
here is the code of what i am trying to do...
now imagine one image showing through the rects,
and if a rect gets scaled then this part of the image
gets scaled as well...

Code:

int matrix;
int squares = 15;
int steps;
int col_on = 1;

float col_factor;
float col_X;
float col_Y;

float max_distance;
float max_size = 25; // best = 25
float max_factor = 1.25; // best = 1.25

void setup() {

matrix = (squares+1)*round(max_size/max_factor);
steps = matrix/(squares+1);

size(matrix, matrix);
max_distance = dist(0, 0, width, height);
rectMode(CENTER);
noStroke();

col_factor = (matrix*100)/255; // kalkuliert den faktor der mouse/matrix relation
}

void draw()
{
background(51);
col_X = round((mouseX/col_factor)*100)*col_on; // kalkuliert die mouseXpos auf 0-255
col_Y = round((mouseY/col_factor)*100)*col_on; // kalkuliert die mouseXpos auf 0-255

for(int i=steps; i<width; i+=steps) {
for(int j=steps; j<width; j+=steps) {
float size = dist(mouseX, mouseY, i, j);
size = max_size/((size/100)+max_factor);
fill((size*12.75)-0, (size*12.75)-col_X, (size*12.75)-col_Y); // size*12.75 um auf 0-255 zu kommen
rect(i, j, size, size);
}
}
}
Re: usa a rect as a mask for an image...
Reply #6 - Apr 27th, 2006, 10:09am
 
so, its been a week now...

do i understand that there is no way of doing this in processing? a yes or no would be fine!

thanks!
Re: usa a rect as a mask for an image...
Reply #7 - Apr 27th, 2006, 10:49am
 
have you tried this (bit hasty, but could be modified):

where source is a PImage containing a 320*320 (size of your sketch) image.


Use another renderer to do this, i.e. size(matrix, matrix, P3D) immediate improvement!

instead of your rect(i,j,size,size), use:

Code:


copy(src,(int)i,(int)j,20,20,i,j,(int)size,(int)size);



It does what you want, if I understand you correctly, and is what I think JohnG was getting at.
Re: usa a rect as a mask for an image...
Reply #8 - Apr 27th, 2006, 2:21pm
 
you are right it is faster, but still... could be faster...
(any idea how?)
i will post the code after making some changes.

thanks for now!!!
Re: usa a rect as a mask for an image...
Reply #9 - Apr 27th, 2006, 2:26pm
 
I guess having a look at how optimized that copy/scale method is might prove useful.

Anything you can get outside of the loop will of course be useful.

Did you go back and try the QUAD version but with the faster renderers?

I suspect you're moving close to the optimum.
Re: usa a rect as a mask for an image...
Reply #10 - Apr 27th, 2006, 2:47pm
 
Hmmm! I'd look at producing your own copy method, because even if you copy the whole image into one of your rectangles and scale it, it makes no impact upon performance.

So, there might be possibilities in writing your own. I'd take a look at the processing copy() source and strip it out into your own myCopy() method and look for ways to improve on it (though it's probably pretty slick already).

Can't think of much else, apart from shifting the declarations of variables outside the loop and not casting in the copy itself but only once outside, but then these aren't real killers given the limited number of iterations made:

Code:


float size;
int intsize;

for(int i=steps; i<width; i+=steps) {
for(int j=steps; j<width; j+=steps) {
size = dist(mouseX, mouseY, i, j);
intsize = (int)(max_size/((size/100)+max_factor));

copy(src,i,j,320,320,i,j,intsize,intsize);
}
}

Re: usa a rect as a mask for an image...
Reply #11 - Apr 27th, 2006, 2:58pm
 
And one last idea..

If you make the source image quarter screen size (80,80), then you can scale up. I don't know if that saves time?


Code:


copy(src,i/4,j/4,5,5,i,j,intsize,intsize);

Re: usa a rect as a mask for an image...
Reply #12 - Apr 27th, 2006, 2:59pm
 
copy() should be very fast on its own.

try image(imageName, x, y, width, height, u1, v1, u2, v2);

where the u and v coordinates are the x1/y1/x2/y2 of the subset of the image that you want to draw. it does the same thing as the texture() stuff, but maybe it's not "heavy" like you had complained.
Page Index Toggle Pages: 1