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 › Limiting pixel area in a larger PImage
Pages: 1 2 
Limiting pixel area in a larger PImage (Read 2462 times)
Re: Limiting pixel area in a larger PImage
Reply #15 - Dec 9th, 2009, 2:06pm
 
A few frustrating hours later and I've got it:

Quote:
dart.loadPixels();
 for(int x = 0; x < dart.width; x++) {
   for(int y = 0; y < dart.height; y++) {
     fuzzyDart.pixels[((fuzzyDart.width/2) - (dart.width/2)) + (x*dart.width*2) + ((fuzzyDart.width*fuzzyDart.height)/2) - (fuzzyDart.width*(dart.height/2)) + y] = dart.pixels[(x*dart.width) + y];
}
}


I'm keeping fuzzyDart.width/2 and dart.width seperate because dart.width is going to become dynamic later.
The above needs a bit of tweeking and I know I'll find a better way to write it but it's brought me onto the next step finally. Thanks for all the help.
Re: Limiting pixel area in a larger PImage
Reply #16 - Dec 10th, 2009, 5:57am
 
can i just suggest precalculating all of the constants that you use inside your loop - there's no need for it to work out, say, ((fuzzyDart.width / 2) - (dart.width / 2)) for every pixel as it's always the same.

and another tip: post the images you're using - makes it easier for people to debug.

(i'm pretty sure mine does exactly the same as yours btw)
Re: Limiting pixel area in a larger PImage
Reply #17 - Dec 10th, 2009, 8:24am
 
Koogy - I'll post the images next time. Thanks.

The dart.width and height dimensions are dynamic and change on each loop of the draw function so I couldn't precalculate them. (Well, not strictly true. It's when I copy the dart PImage pixels into fuzzyDart that I dynamically alter the size. It skips adding an extra step).

I appreciate all the help. You'd be a lot more knowledgeable at this than me but when I pasted your code into mine it didn't constrain the dimensions correctly. I imagine it was something I did rather than you. If you're still curious, use any png image whatsoever. The code isn't reliant any exact dimsensions so it can be tested with any image.

I've gotten to the next step of my project and have the pixels scattering from audio levels. it's worked a treat.
Re: Limiting pixel area in a larger PImage
Reply #18 - Dec 10th, 2009, 8:57am
 
But you can still save on calculations by pre-calculating before the for loop:

Code:
dart.loadPixels();

float halfFuzzyWidth = fuzzyDart.width/2;
float halfFuzzyHeight = fuzzyDart.height/2;
float halfDartWidth = dart.width/2;
float doubleDartWidth = dart.width * 2;

for(int x = 0; x < dart.width; x++) {
  for(int y = 0; y < dart.height; y++) {

    fuzzyDart.pixels[(halfFuzzyWidth  - halfDartWidth) + (x*doubleDartWidth) + ((fuzzyDart.width*halfFuzzyHeight ) - (fuzzyDart.width*halfDartHeight ) + y] = dart.pixels[(x*dart.width) + y];
}
}


Otherwise on each iteration in the for loop (i.e. width * height repetitions) you're accessing the object data and running the calculations and that happens every frame of draw...

I tried the original code with an image and got an array out of bounds error - does the image have to be an even number of pixels wide/high?
Re: Limiting pixel area in a larger PImage
Reply #19 - Dec 10th, 2009, 9:01am
 
blindfish wrote on Dec 10th, 2009, 8:57am:
I tried the original code with an image and got an array out of bounds error - does the image have to be an even number of pixels wide/high


Ah - I see: it has to be square...
Re: Limiting pixel area in a larger PImage
Reply #20 - Dec 10th, 2009, 9:14am
 
Cheers blindfish! You're right, there's definitely some parts of the code I can precalculate afterall. I didn't look further than disagreeing with '((fuzzyDart.width / 2) - (dart.width / 2)) for every pixel as it's always the same.'

I don't believe the png has to be square. The one I'm using is close to square but isn't 1:1. I was having odd out of bounds errors too. It's why some of the integers in loops start as =1 as opposed to 0. It seemed to fix my errors. Again, I have to go back and clean that up as opposed to building on something that's messy.
Re: Limiting pixel area in a larger PImage
Reply #21 - Dec 10th, 2009, 12:34pm
 
No worries - and thanks: here's a rough implementation of what I suggested earlier about using particles - which your question inspired Smiley

http://www.openprocessing.org/visuals/?visualID=6482
Re: Limiting pixel area in a larger PImage
Reply #22 - Dec 10th, 2009, 1:44pm
 
oh, you're right btw, mine had a bit of a bug.

fuzzyDart.pixels[xd * fuzzyDart.height + yd] = dart.pixels[x * dart.width + y];

should be

fuzzyDart.pixels[xd * fuzzyDart.width + yd] = dart.pixels[x * dart.width + y];

ie should be width, not height in that first bit. it worked when i tried it as i used a square image...

there's a problem with rounding if the original sizes are odd as well.
Re: Limiting pixel area in a larger PImage
Reply #23 - Dec 14th, 2009, 6:20am
 
blindfish - that looks interesting. I'm gonna have a more thorough look through it soon.

koogy - I've yet to go back and clean up what I've written (notice a theme here?) but I think what you've written will help immensely. Thanks.
Pages: 1 2