PocketMatrix forums

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

Dithering

I haven't seen much on dithering algorithms here, so I'm starting a new thread =)

I've implemented the Floyd-Steinberg error diffusion to support palettized devices such as the Jornada 525. I dither the backbuffer (565) using a "332" palette. In the hope of getting good performances, I used a simple diffusion pattern: 1/2 of the error goes to the next pixel on the right and 1/2 of the error goes to the one below. The resulting image has really good visual quality... But the frame rate is way too low.

Right now, I had report of about 4-6 fps using this dithering algorithm and about 21 fps using a "closest" color approach. Compared to the 145 fps I get on the Casio E-125 (native 565), this is quite lame.

Even using a really fast (and optimized) dithering algorithm (using a matrix for example), it seems I won't get much more then 21 fps... And this is without any game logic running!

I see two solutions: 1) Implement native 8-bits surfaces (something I really don't want to do because of needed time / suppport / ...) or 2) Drop support for 8-bit devices.

Any ideas? What about monochrome devices? They surely could use some dithering...
I did this a while back. It isn't Floyd-Steinberg though. <subjectivism> It's much faster. </subjectivism> (i.e. I never profiled it but in theory it should be faster)

http://forums.pocketmatrix.com/viewtopic.php?t=946&highlight;=dithering
Pretty much figured out you used ordered dithering... It seems the only way to get some fps out of the device. Did you go with the simple 2x2 Bayer matrix?

Do you still have the code for this? I'm really curious to see how you did it with only two "and"s and a lookup + clamping.
I used a 4x4 dither matrix. I unable to locate the code right now but I should be able to find it later this week.

Per-pixel, the ordered dither requires two ANDs, one table lookup, 3 adds, and 3 clamps, then a pixel pack.

Have you tried just creating a generic halftone palette and generating a 64K lookup table?

Don't perform the color conversion per-frame. Only do it when the app locks/unlocks a surface.

Personally, unless I had to choose between supporting palettized displays and a having a root canal, I'd do nothing to support these dead devices. They are well over a year old now and there are no future devices being made with 8-bpp displays.
I haven't tried a 64K lookup table and am a little reluctant to do that... There isn't much device supporting 8 bits, but I still receive mails from peoples using Jornada 525 who wants support...

A 4x4 matrix seems a little big when dealing with sprites and video game graphics, although I'm sure it gives good results with photos and gradients.

I think I will stick with my "closest color" approximation for now... Results are not impressives, but at least it works and relatively fast:

Code:

// Return the nearest color in the palette
static inline BYTE ConvertToPalette( Pixel c )
{
c = ((c >> 8) & 0xE0) | ((c >> 6) & 0x1C) | ((c >> 3) & 0x03);

return (BYTE) c;
}
It's the same cost perf-wise in using a 4x4 dither matrix as using a 64x64 (except for cache misses).

Just curious, why are you reluctant to using a 64K look up table, memory usage?
Well yes, the memory it takes (64K * 2 bytes), and the effect this can have on the cache. On the other hand, 128K is not that much... I'm thinking about using Floyd-Steinberg with a lookup table that would return the resulting color and the error... Something like:

Code:

struct
{
BYTE m_color;
BYTE m_errorRed, m_errorGreen, m_errorBlue

} table[65536];


I want this dithering done in real-time (ie: dither the backbuffer each frame)... There is no need for me to optimize this while loading images.

When I say that I feel a 4x4 matrix is a little big, I am not talking about memory usage but the fact that using a big matrix won't give really good results for sprite images that are small. If my sprite is 16x16 with 20% of the pixels masked and the matrix is 4x4, then I don't have the feeling the results will be really good. I might be wrong though.
I've managed to optimize my Floyd-Steinberg dithering using some LUTs and I get a performance gain of about 50% on a Jornada 525.

The only part I don't like in my algorithm is the clamping of the color intensities (clamping to 0 or 0xFF):

Code:

static inline BYTE ConvertToPalette( Pixel c, int& errorR, int& errorG, int& errorB )
{
// Find out the target color
int targetR = l_lut32[ c >> 11 ];
int targetG = l_lut64[ (c & GREEN_MASK) >> 5 ];
int targetB = l_lut32[ c & BLUE_MASK ];

// Add the error component
targetR -= errorR;
targetG -= errorG;
targetB -= errorB;

// Check saturation
if (targetR & ~0xFF) targetR = ~(targetR >> 24);
if (targetG & ~0xFF) targetG = ~(targetG >> 24);
if (targetB & ~0xFF) targetB = ~(targetB >> 24);

// Find error
errorR = l_error3[ targetR ];
errorG = l_error3[ targetG ];
errorB = l_error2[ targetB ];

BYTE result = (targetR & 0xE0) | ((targetG >> 3) & 0x1C) | (targetB >> 6);

return result;
}


Any idea how to optimize this even further? I've tried a lookup table for saturation with 768 entries. Something like:

Code:

targetR = l_saturation[ targetR ]
...


This was actually slower...
If you are exposing a 16-bpp buffer to an app running on an 8-bpp device, then the look up table is 64K bytes. Each byte in the table contains the 8 bit palette index.

If you're going to dither the entire back buffer then the dithering will help the final image, regardless of what size your sprites are. Or are you talking about dithering the sprites individually before copying to the back buffer?

My comment about doing the color conversion at load time was to think about this problem in the same manner as rotating a bitmap at load time to solve the native landscape oriented displays. If you do the conversion up front, you won't have to do it each frame, which is going to be a big perf win.
I hope I am not bothering you with this Digby... I am indeed exposing a 16 bpp buffer to an app running on a 8 bpp device (with a 332 RGB palette). I want to dither the entire backbuffer when "flipping".

I am timing about 2.84 ms / frame on my desktop PC for the Floyd-Steinberg algo. What I am trying to do is use something faster (ordered dithering). In the following code (adapted from Graphic Gems II), I am using a 16x16 threeshold matrix to do dithering. The results are nice (but less so then Floyd-Steinberg), but what is bugging me is that it is actually slower by about 25% (3.65 ms / frame).

You told me you did it with far less operations, so I am curious...

The DitherRGB() function does the dirty work... It takes the 565 color pixel (16-bits) and returns a 332 pixel (8-bits).

Thanks for your help!



Code:

int l_divN8[256];
int l_modN8[256];
int l_divN4[256];
int l_modN4[256];
int l_magic8[16][16];
int l_magic4[16][16];


static int magic4x4[4][4] =
{
0, 14, 3, 13,
11, 5, 8, 6,
12, 2, 15, 1,
7, 9, 4, 10
};



void InitializeLUTs( double N, int divN[256], int modN[256], int magic[16][16] )
{
register int i, j, k, l;
double magicfact;

for ( i = 0; i < 256; i++ )
{
divN[i] = (int)(i / N);
modN[i] = i - (int)(N * divN[i]);
}
modN[255] = 0;


// Expand 4x4 dither pattern to 16x16. 4x4 leaves obvious patterning,
// and doesn't give us full intensity range (only 17 sublevels).
//
// magicfact is (N - 1)/16 so that we get numbers in the matrix from 0 to
// N - 1: mod N gives numbers in 0 to N - 1, don't ever want all
// pixels incremented to the next level (this is reserved for the
// pixel value with mod N == 0 at the next level).

magicfact = (N - 1) / 16.;
for ( i = 0; i < 4; i++ )
for ( j = 0; j < 4; j++ )
for ( k = 0; k < 4; k++ )
for ( l = 0; l < 4; l++ )
magic[4*k+i][4*l+j] =
(int)(0.5 + magic4x4[i][j] * magicfact +
(magic4x4[k][l] / 16.) * magicfact);
}



static inline BYTE DitherRGB( Pixel c, int column, int row )
{
int x = column & 15;
int y = row & 15;

int r = c >> 8;
int g = (c >> 3) & 0xFF;
int b = (c << 3) & 0xFF;

r = l_modN8[r] > l_magic8[x][y] ? l_divN8[r] + 1 : l_divN8[r];
g = l_modN8[g] > l_magic8[x][y] ? l_divN8[g] + 1 : l_divN8[g];
b = l_modN4[b] > l_magic4[x][y] ? l_divN4[b] + 1 : l_divN4[b];

BYTE result = (r << 5) | (g << 2) | (b);

return result;
}
Plan9,

Check your messages.