FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   fast grey
« No topic | Next topic »

Pages: 1 
   Author  Topic: fast grey  (Read 1804 times)
st33d

WWW Email
fast grey
« on: Apr 16th, 2005, 12:13am »

I currently get grey values of pixels like this:
Code:

float grey(color p){
  int  r=p>>16&0xff;
  int  g=p>>8&0xff;
  int  b=p&0xff;
  return (r+g+b)/3;
}

Is there a faster way of doing this with an integral return value? I've tried the function as an int but it's inaccurate and I'm calling this function a lot.
 

I could murder a pint.
fry


WWW
Re: fast grey
« Reply #1 on: Apr 16th, 2005, 1:47am »

the quickest gray i know of (where gray is defined as "brightness")  
 
max((value >> 16) & 0xff, (value >> 8 ) & 0xff, value & 0xff);  
 
and to avoid putting it in a function, either just write that inline to your code, or use:
 
static final int gray(color p) {
  return max((value >> 16) & 0xff, (value >> 8 ) & 0xff, value & 0xff);  
}
 
"final" should make it a little faster (maybe inline it by the compiler) and static may help too, ymmv.
« Last Edit: Apr 16th, 2005, 1:47am by fry »  
st33d

WWW Email
Re: fast grey
« Reply #2 on: Apr 18th, 2005, 11:35pm »

Thank you. Seems to work fine. Static final doesn't seem to work within the API (I'm using Processing as a glorified photoshop filter these days - need to save). My experience is limited to using the API only. In what conditions does "static final" pass as opperable? Is there a link to a tutorial which could explain these Java conditions? (public, static and final I have seen but it's never really sunk in as the Processing API is much more straight-forward).
 

I could murder a pint.
Pages: 1 

« No topic | Next topic »