问题
I've been using aforge's floyed-steinberg dithering method, but I noticed that you can't specify the amount of dithering. I'd like to be able to specify an amount between 0 and 100. So that if I ask for 50, it will dither half as much as 100. Or, if I specify 0, then the resulting image will be made of solid colors with hard edges between each color... or in other words, no dithering. I'm looking for c# code for a routine such as floyed-steinberg, or Jarvis, Judice, Ninke dithering, that accepts an amount. anyone know of any?
回答1:
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.
来源:https://stackoverflow.com/questions/32939300/c-sharp-image-dithering-routine-that-accepts-an-amount-of-dithering