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
   Programs
(Moderators: fry, REAS)
   optimization help?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: optimization help?  (Read 451 times)
rgovostes

rgovostes
optimization help?
« on: Mar 17th, 2004, 2:05am »

I went back and took a look at my 'colors' applet. I've been looking for ways to make it run a little faster. A large chunk of processing time is used on the following lines of code, which calculate the average RGB value of nearby pixels which have already been calculated - it will never average in the value of the pixels below or to the right.
 
I was wondering if anyone had a suggestion for quickly adding the red/green/blue values, or at least shorten the code a little so it is not such an eyesore. One idea that I had was to make 3 arrays, one for each color, and store the corresponding pixels' values in each, so it doesn't need to use red(), green(), and blue() many times. However, I thought I'd ask if anyone had a better suggestion.
 
Thanks,
Ryan Govostes
 
Code:
   if(i < width) {         // First row
 avgColor = out.pixels[i - 1];    // .
    } else {
    if(i % width == 0) {    // First column
 r  = red(   out.pixels[i - width] );  // ^
 r += red(   out.pixels[i - (width - 1)] ); //  ^
 g  = green( out.pixels[i - width] );  // ^
 g += green( out.pixels[i - (width - 1)] ); //  ^
 b  = blue(  out.pixels[i - width] );  // ^  
 b += blue(  out.pixels[i - (width - 1)] ); //  ^
 avgColor = color((r * 0.50), (g * 0.50), (b * 0.50));
    } else {
    if(i % width == (width - 1)) {    // Last column
 r  = red(   out.pixels[i - width] );  //  ^
 r += red(   out.pixels[i - (width + 1)] ); // ^  
 r += red(   out.pixels[i - 1] );      // .
 g  = green( out.pixels[i - width] );  //  ^
 g += green( out.pixels[i - (width + 1)] ); // ^
 g += green( out.pixels[i - 1] );      // .
 b  = blue(  out.pixels[i - width] );  //  ^
 b += blue(  out.pixels[i - (width + 1)] ); // ^
 b += blue(  out.pixels[i - 1] );      // .
 avgColor = color((r * 0.33), (g * 0.33), (b * 0.33));
    } else {       // Anywhere else
 r  = red(   out.pixels[i - (width + 1)] ); // ^  
 r += red(   out.pixels[i - width] );  //  ^  
 r += red(   out.pixels[i - (width - 1)] ); //   ^
 r += red(   out.pixels[i - 1] );      // .  
 g  = green( out.pixels[i - (width + 1)] ); // ^  
 g += green( out.pixels[i - width] );  //  ^  
 g += green( out.pixels[i - (width - 1)] ); //   ^
 g += green( out.pixels[i - 1] );      // .  
 b  = blue(  out.pixels[i - (width + 1)] ); // ^  
 b += blue(  out.pixels[i - width] );  //  ^  
 b += blue(  out.pixels[i - (width - 1)] ); //   ^
 b += blue(  out.pixels[i - 1] );      // .  
 avgColor = color((r * 0.25), (g * 0.25), (b * 0.25));
    }}}
 
arielm

WWW
Re: optimization help?
« Reply #1 on: Mar 17th, 2004, 2:53am »

it's too late here in tlv for starting a chapter on bit-packing operations , but obviously: you should'nt use functions like red(), green() or blue() and color() if you need speed.
 
something else that should make the existing code already much faster is to "cache" the following ones into variables, at the beginning of your algorithm:
 
out.pixels[i - width]
out.pixels[i - (width - 1)]
etc...
 
for 2 reasons:
1) accessing an object's property is slower than accessing a local variable
2) accessing an array's entry is something slower too
 

Ariel Malka | www.chronotext.org
rgovostes

rgovostes
Re: optimization help?
« Reply #2 on: Mar 17th, 2004, 4:08am »

I hadn't thought of bitshifting... Is there an easier way than shifting it twice?
 
Code:
colorMode(RGB, 255, 255, 255, 255);
color a = color(100, 222, 156);
int r = (a <<  8) >> 24; // red
int g = (a << 16) >> 24; // green
int b = (a << 24) >> 24; // blue

 
I don't have a copy of Processing handy but I'll try it out tomorrow morning.
 
Mythmon


Re: optimization help?
« Reply #3 on: Mar 17th, 2004, 4:27am »

A good way to get RGB values out of an enitre hex code is this:
for red  
Code:
  color >> 16  

for blue  
Code:
 (color >> 8) & 255  

for green  
Code:
 color & 255  

 
im not sure if its faster than other methods, but since its all bit shifting and & operation it should be faster than red() blue() and green()
 
TomC

WWW
Re: optimization help?
« Reply #4 on: Mar 17th, 2004, 10:24am »

I think you got green and blue the wrong way around there.
 
Also, if pixels are packed in hex as 0xAARRGGBB (hex values for alpha, red, green, blue), then red is more safely picked out using  
Code:
(col >> 16) & 0xff;
 
(as you did for green) since that will give you 0xRR, but col>>16 will give you 0xAARR and could cause problems if 0xAA isn't zero.
 
rgovostes

rgovostes
Re: optimization help?
« Reply #5 on: Mar 17th, 2004, 10:06pm »

Thanks for the help, it seems to be running smoother now. I think to reduce the time it takes to average, I will store the last <width * 2> colors in an array, the first <width> being the row above, and the second reserved for injecting newly calculated ones. This should be faster than accessing the pixel array, right?
 
Pages: 1 

« Previous topic | Next topic »