PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

additive / subtractive blend speed

Hi,

I have the following code and im struggling to find any other ways to speed it up :( .
Ive slightly read about RLE being very fast but I have no idea how to do it.

Additive
Code:

for ( int y (temp.GetHeight()) ; y != 0 ; --y )
{
for ( int x ( temp.GetWidth()) ; x != 0 ; --x )
{
// get front and back pixels (image and screen)
USHORT frontpixel(imagePointer[0]);
USHORT backpixel(topLeftOfImage[0]);

int red = (backpixel & 0xF800) + (frontpixel & 0xF800);
int green = (backpixel & 0x07E0) + (frontpixel & 0x07E0);
int blue = (backpixel & 0x001F) + (frontpixel & 0x001F);

if(red > 0xF800)
red = 0xF800;

if(green > 0x07E0)
green = 0x07E0;

if(blue > 0x001F)
blue = 0x001F;

*(topLeftOfImage) = (USHORT)( red | green | blue );

// Move to the next pixel
++imagePointer;

// Move to the next pixel in screen
++topLeftOfImage;
}

// Move to next line of screen
topLeftOfImage += endOfLineIncrement;

// Move to next line of image
imagePointer += imageIncrement;
}


Subtractive
Code:

for ( int y (temp.GetHeight()) ; y != 0 ; --y )
{
for ( int x ( temp.GetWidth()) ; x != 0 ; --x )
{
// get front and back pixels (image and screen)
USHORT frontpixel (imagePointer[0]);
USHORT backpixel (topLeftOfImage[0]);

int red = (backpixel & 0xF800) - (frontpixel & 0xF800);
int green = (backpixel & 0x07E0) - (frontpixel & 0x07E0);
int blue = (backpixel & 0x001F) - (frontpixel & 0x001F);

if(red < 0)
red = 0;

if(green < 0)
green = 0;

if(blue < 0)
blue = 0;

*(topLeftOfImage) = (USHORT)( red | green | blue);

// Move to the next pixel
++imagePointer;

// Move to the next pixel in screen
++topLeftOfImage;
}

// Move to next line of screen
topLeftOfImage += endOfLineIncrement;

// Move to next line of image
imagePointer += imageIncrement;
}


Thx
Mike
This post has an additive that uses just one addition and some 'tricks'. It loses a bit on accuracy to do so, though.

http://www.pocketmatrix.com/forums/view ... 868#250868

The 63454 magic number is zeroing out the LSB of each component (hence the accuracy comment above). But this allows you to check for the overflow condition.
thx
thats clever, didnt think of doing it that way :). I can see the inacurracy when things should be white they are not fully white but thats not really noticable.

cheers for the help
Mike
You might want to investigate reading a DWORD instead of WORD each time through the loop. You pay the same cost on ARM for doing 16 bit ops as you do for 32 bits.

I'd also look at the generated assembly and make sure that the compiler has inlined GetWidth and GetHeight or isn't doing something else dumb like reloading red/green/blue since they are typed as const. Or you could just read the width once into x then while(x--) instead of the for loop.

When I develop routines like this that don't do much with the per-pixel data, I try to quickly determine how fast the routine would run with a "best case" implementation. I do this by commenting out sections of the code with the reasoning that if I did a really, really great job of optimizing, it would never get any faster than not having to execute the code at all. If, after commenting out the code, it didn't make an appreciable difference in the overall performance of the *app*, then I'd spend my time elsewhere. You have to be careful about doing this as the compiler will sometimes turn your entire function into a NOP so you need to look at the generated assembly language to determine the routine is still doing the portion of work that you expect.
With the code that you've posted below, you'll probably find that the time it takes to read and write the pixel data is more than it takes to execute those few bit shifts and addition operations.
x and y aren't actually used by the algorithm - they are just counters for looping. Instead just use one of the pointers you increment each iteration, and break out of the loop when it reaches an end value.

That will at least save you a few cycles per iteration, and if you are running out of registers (which I doubt in this case) could make a huge difference.

Dan East
You can save a bitwise AND in the additive line 11:
Code:

int blue = (backpixel & 0x001F) + (frontpixel & 0x001F);
//Change to:
int blue = (backpixel + frontpixel) & 0x001F;


And with the subtractive line 9:
Code:

int red = (backpixel & 0xF800) - (frontpixel & 0xF800);
//Change to:
int red = (backpixel - frontpixel) & 0xF800;


Dan East