From fd839eb946700b4a91fd470a54a0dd49dfa6b8ac Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 10 Apr 2004 23:09:41 +0000 Subject: [PATCH] optimize SM_SDL_ErrorDiffusionDither: takes about half as long now (but it still takes 3x as long as SM_SDL_OrderedDither, and I still see banding in both vertical and horizontal gradients ...) --- stepmania/src/SDL_dither.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/stepmania/src/SDL_dither.cpp b/stepmania/src/SDL_dither.cpp index aef760654b..94ed29aae2 100644 --- a/stepmania/src/SDL_dither.cpp +++ b/stepmania/src/SDL_dither.cpp @@ -1,5 +1,6 @@ #include "global.h" +#include "RageUtil.h" #include "SDL.h" #include "SDL_dither.h" #include "SDL_utils.h" @@ -139,6 +140,26 @@ void SM_SDL_OrderedDither(const SDL_Surface *src, SDL_Surface *dst) #define CLAMP(x, l, h) {if (x > h) x = h; else if (x < l) x = l;} +/* Return "random" numbers in [0,2]; this is for SM_SDL_ErrorDiffusionDither (rand() is too slow). */ +static inline int GetFastRand() +{ + static int RandomNumbers[] = + { + 2, 1, 0, 2, 0, 2, 2, 1, 0, 1, 1, 2, 2, 0, 1, 1, 2, 2, 1, 0, 1, 0, 1, 2, 1, 0, 2, 1, 0, 1, 1, 0, + 2, 1, 1, 2, 1, 0, 0, 1, 1, 2, 0, 2, 0, 1, 0, 1, 2, 0, 0, 1, 1, 2, 0, 2, 1, 2, 2, 2, 2, 1, 1, 1, + 1, 0, 2, 1, 2, 1, 1, 1, 2, 1, 0, 0, 1, 0, 0, 2, 0, 1, 0, 1, 2, 2, 0, 0, 0, 2, 2, 1, 2, 2, 2, 2, + 1, 2, 1, 0, 2, 0, 1, 0, 2, 1, 2, 1, 1, 0, 1, 1, 2, 0, 0, 2, 2, 2, 1, 0, 0, 1, 2, 1, 2, 0, 0, 2, + 2, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 2, 2, 2, 0, 0, 0, 1, 2, 0, 1, 1, 0, 0, 0, 2, 1, 2, 0, 0, 1, + 1, 2, 0, 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 0, 0, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 2, + 0, 1, 2, 0, 2, 2, 0, 2, 1, 0, 0, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 0, 1, 0, 1, 2, 1, 2, 1, 0, 0, + 1, 0, 0, 0, 2, 2, 2, 2, 1, 2, 1, 0, 0, 2, 1, 0, 1, 2, 0, 2, 2, 0, 0, 0, 2, 2, 0, 0, 2, 0, 1, 1 + }; + static int iNextNumber = 0; + if( iNextNumber == ARRAYSIZE(RandomNumbers) ) + iNextNumber = 0; + return RandomNumbers[ iNextNumber++ ]; +} + void SM_SDL_ErrorDiffusionDither(const SDL_Surface *src, SDL_Surface *dst) { /* We can't dither to paletted surfaces. */ @@ -194,7 +215,9 @@ void SM_SDL_ErrorDiffusionDither(const SDL_Surface *src, SDL_Surface *dst) CLAMP( accumError[c], -128, +128 ); // Keep only a fraction of the error to make the effect more subtle. - accumError[c] /= (rand()%4)+1; + // This used to divide by [1,4]; shift right by [0,2] to get a similar + // (but much faster) effect. + accumError[c] >>= (GetFastRand())+1; } srcp += src->format->BytesPerPixel;