Skip to main content
8 of 9
Highlighted the code that is slow after profiling
user avatar
user avatar

Line drawing algorithm for anti-aliased lines optimization

I need to optimize the following function (No SIMD, Multithreading). I already applied a lot of optimizations and got it ~35% faster, but it is possible to get it 200% faster.

The function describes the anti-aliased Wu line drawing algorithm. It draws colored lines. After profiling this part was the slowest piece of my code. I really want to get rid of the branch inside of the function

void DrawWuLine( Surface *screen, int X0, int Y0, int X1, int Y1, Color& clrLine )
{
    BYTE rr = ( rb > rl ? ( ( BYTE )( ( ( double )( grayl<grayb?Weighting:(Weighting ^ 255)) ) / 255.0 * ( rb - rl ) + rl ) ) : ( ( BYTE )( ( ( double )( grayl<grayb?Weighting:(Weighting ^ 255)) ) / 255.0 * ( rl - rb ) + rb ) ) );
    BYTE gr = ( gb > gl ? ( ( BYTE )( ( ( double )( grayl<grayb?Weighting:(Weighting ^ 255)) ) / 255.0 * ( gb - gl ) + gl ) ) : ( ( BYTE )( ( ( double )( grayl<grayb?Weighting:(Weighting ^ 255)) ) / 255.0 * ( gl - gb ) + gb ) ) );
    BYTE br = ( bb > bl ? ( ( BYTE )( ( ( double )( grayl<grayb?Weighting:(Weighting ^ 255)) ) / 255.0 * ( bb - bl ) + bl ) ) : ( ( BYTE )( ( ( double )( grayl<grayb?Weighting:(Weighting ^ 255)) ) / 255.0 * ( bl - bb ) + bb ) ) );
}

Any tips or insights on how to get rid of branches inside of the while loops? Other optimizations are welcome.

user282937