PocketMatrix forums

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

Blending with Clamping

i want to blend pixels using adding. so:

destpixel = destpixel + srcpixel

the problem is of course, that there can be a overflow. in such a case one wants to use the maximum allowed value. so currently i'm doing this:

Code:

unsigned short blendSC = *dstC;
unsigned int newR = (blendSC&RED;_MASK) + (newCol&RED;_MASK),
newG = (blendSC&GREEN;_MASK) + (newCol&GREEN;_MASK),
newB = (blendSC&BLUE;_MASK) + (newCol&BLUE;_MASK);

if(newR>RED_MASK)
newR = RED_MASK;
if(newG>GREEN_MASK)
newG = GREEN_MASK;
if(newB>BLUE_MASK)
newB = BLUE_MASK;

newCol = newR | newG | newB;


i assume, that using a lookup table to remove the 3 if blocks would speed things up, but i'd be happiest to get away without additional memory accesses....

any ideas on this?

DANIEL
There's quite a bit of info in this thread:

http://www.pocketmatrix.com/forums/viewtopic.php?t=895

Dan East
Here's a different approach:

Code:

PIXEL c = (PIXEL)(((c1 >> 1) & SHFTMASK) + ((c2 >> 1) & SHFTMASK));
if (c & 0x8410)
{
if (c & 0x8000) c = (c & 0x7FF) + 0x7800;
if (c & 0x400) c = (c & 0xF81F) + 0x3E0;
if (c & 0x10) c = (c & 0xFFE0) + 0xF;
}
return (PIXEL)(c << 1);


Here, a single 'if' is used to detect an overflow of one of the components. If this occurs, the individual components are clipped. This reduces the 'best case' to a single if. If you have lots of overflows, this is of course one 'if' more than your original approach.

Besides, I loose some accuracy by shifting the colors before adding them.

- Jacco.
Forgot to mention that this snippet comes straigt from the just released Nutcracker source code. :)

<edit: And now I see that Dan was faster>
a disadvantage of this code is, that it gives a way the least significant bit of each component (for beeing able to do all in one addition).
besides that it still uses at least one if.
btw: why not use lookup tables inside the if block to speed up the clamp case?

in any case: thanks for the replies. i think i'll stick with this code (or a similiar version of it).

DANIEL
Code:

if(newR>RED_MASK)
newR = RED_MASK;
if(newG>GREEN_MASK)
newG = GREEN_MASK;
if(newB>BLUE_MASK)
newB = BLUE_MASK;


The arm-architecture can execute almost every operation as a conditional, this code will get down to to one compare and one conditional move per component. 6 alu-instructions in total. I think this is hard to beat. (last time I checked, The evc compiler was even smart enough to use them.. it wasn't color clamping though).
Why worry about splitting it into components? Just deal with the over/underflows.

Ref.
since we already accept to loose each component's LSB i suggest one can further optimize the three if statements to

Code:

if (c & 0x8000) c |= 0x7800;
if (c & 0x400) c |= 0x3E0;
if (c & 0x10) c |= 0xF;

by allowing the MSBs to overflow into the next components' LSB...

DANIEL
drgoldie wrote:
since we already accept to loose each component's LSB i suggest one can further optimize the three if statements...

...or leave them away and use a lut instead. although a similar idea has already been discussed in another forum, there's a different approach i once used under ms-dos. first of all, this one is just useful, if big parts of the screen will get blended all the time, since this solution requires all colors to be in RGB454 format and therefor needs an additional shift per pixel per frame. however, i have not yet tested this method on a pocketpc but here is how it works: preprocess all textures (sprites, etc.) to fit into a RGB454 sheme but still use all 16bits:
Code:
01111 011111 01111
red green blue
next, you'll have to create a lookup table for the whole "palette" (=16*32*16=65536) and fill it with the proper bit-values; notice, you're now clamping "all possible" colors before runtime:
Code:
for (int col = 0; col < 65536; col++)
{
int red = col & (31 << 11),
green = col & (63 << 5),
blue = col & 31;

lut[col] = (blue > 15 ? 15 : blue ) |
((green > 31 ? 31 : green) << 5) |
((red > 15 ? 15 : red ) << 11);
}
now, when it comes to actual blending, you can simply do:
Code:
*dest = lut[*dest + *source];
remember, since all of your textures and the lookup table itself have already been clamped, you won't need to care about overflows anymore in inner loops. however, after you've finished all of your rendering, you should shift each pixel of your screen buffer one bit to the left to fit the RGB565 sheme again, otherwise we would loose the most significant bit.
IMHO, you can do the saturated blending efficiently enough to always be faster than a LUT (which fills the cache with crap and bogs everything down).

however, after you've finished all of your rendering, you should shift each pixel of your screen buffer one bit to the left to fit the RGB565 sheme again,


Eeek! 8O In that case the LUT will lose versus a straight clamping.

Sorry to grammar-nazi, but it's driving me nuts seeing the same error over and over:

loose
1 a : not rigidly fastened or securely attached


lose
6 : to cause the loss of <one careless statement lost him the election>


(i.e. you lose accuracy).
Sorry to grammar-nazi, but it's driving me nuts seeing the same error over and over


i'm not a native english speaker. it's no problem for me, besides that we are spamming the thread with it... :)

anyway, thanks,
DANIEL
Shore. Angle leash whirreds oral enter change a bull.
I'd prefer instead to point out the difference between accuracy and precision.

3.1 is an accurate value of pi, but not very precise.

5.1415926585 is a precise value of pi, but not very accurate.

3.1415926585 is both precise and accurate.

Above, instead of losing accuracy, what's really lost is precision. Red isn't suddenly changing into green, it's just lost some gradations of value.

We now return to our regularly scheduled discussion.
>> 5.1415926585 is a precise value of pi, but not very accurate.

That is the biggest load of nonsense that I've read in these forums since Moose claimed that he could divide a number by zero.

To top it off, you've posted the wrong value of PI. It's 3.1415926535, not 3.1415926585.