with slight change of this dithering you can achieve the dithering scalability. Just scale the color accumulator (leftovers) r0,g0,b0
by your scale <0,1>
Example (GIF) animation:
Here just the C++ dithering part (rest is in the link above)
// dithering
r0=0; g0=0; b0=0; // no leftovers
for (y=0;y<pic0.ys;y++)
for (x=0;x<pic0.xs;x++)
{
// get source pixel color
c=pic0.p[y][x];
// add to leftovers
r0+=WORD(c.db[picture::_r]);
g0+=WORD(c.db[picture::_g]);
b0+=WORD(c.db[picture::_b]);
// find closest color from pal[]
for (i=0,j=-1;i<pal.num;i++)
{
c=pal[i];
r=WORD(c.db[picture::_r]);
g=WORD(c.db[picture::_g]);
b=WORD(c.db[picture::_b]);
e=(r-r0); e*=e; d =e;
e=(g-g0); e*=e; d+=e;
e=(b-b0); e*=e; d+=e;
if ((j<0)||(d0>d)) { d0=d; j=i; }
}
// get selected palette color
c=pal[j];
// sub from leftovers
r0-=WORD(c.db[picture::_r]);
g0-=WORD(c.db[picture::_g]);
b0-=WORD(c.db[picture::_b]);
// scale dithering
r0=(r0*coef)/100;
g0=(g0*coef)/100;
b0=(b0*coef)/100;
// copy to destination image
pic2.p[y][x]=c;
}
Where coef = <0,100>
is your scale. The only change from the code in linked answer is added 3 lines for scale dithering. Here examples with VGA 256 color default palette:
coef = 100
coef = ~75
coef = ~50
coef = ~25
coef = 0
[Notes]
My coef
is set by a scroll bar so only the 0% and 100% are precise all other coefficients could be near selected value.
If you change the coefficient range to power of 2 for example <0,128>
then you can use bitshifts instead of division while scaling.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…