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 › The equivalent to get(x, y, width, height)
Page Index Toggle Pages: 1
The equivalent to get(x, y, width, height) (Read 321 times)
The equivalent to get(x, y, width, height)
Mar 12th, 2009, 3:04pm
 
Hi, whic is
the equivalent statement to "get(x, y, widht, height)" using pixels[] ?
I can't understand...
Thank to everybody

micron
Re: The equivalent to get(x, y, width, height)
Reply #1 - Mar 12th, 2009, 4:39pm
 
There little advantages to do that by using pixels[]. It is interesting for one pixel, much less for an area, unless you want to bypass color conversions, perhaps.

Anyway, the solution is to use two nested loops, one on the width, the other on the height. The formula should look like: pixels[x + horizontalIterator + (y + verticalIterator) * width]
although I haven't tested it, so it can be wrong.
Re: The equivalent to get(x, y, width, height)
Reply #2 - Mar 12th, 2009, 5:12pm
 
Thanks for your help,
so you don't suggest me to use this method?
Is it better get() or your formula?

I have to read the red value of each pixel in an area...
Re: The equivalent to get(x, y, width, height)
Reply #3 - Mar 12th, 2009, 5:26pm
 
Honestly, you really are better off using get() to obtain the rectangle you're interested in as another, separate, PImage object.  Work with the pixels[] array of that object.

For one thing, doing it this way means you won't have to constantly account for the offsets from the source image's origin to the start of the rectangle you want.  You're thus much less likely to introduce bugs by getting the formula wrong somewhere.

For another thing, using get() insulates your code against any future changes in Processing's implementation of PImage; if Fry decides, for instance, that PImage.pixels should really have been a two-dimensional array of pixels, rather than a linear array, he can do that and update the implementation of get() so that it still does the right thing.  This minimizes the amount of code in your sketch that you'd have to change if anything like that happened.

Not that I'm saying it will, just that if somebody provides you an object type with a well defined interface, it is considered better programming practice to use that interface rather than essentially re-implementing parts of that interface yourself by means of accessing the object's underlying data directly.  I mean, why re-invent the wheel?
Re: The equivalent to get(x, y, width, height)
Reply #4 - Mar 12th, 2009, 5:32pm
 
Yes you are right...
i will use get()..
Thanks for your time

bye

Micron
Page Index Toggle Pages: 1