c#图像抖动例程接受一定量的抖动? [英] c# image dithering routine that accepts an amount of dithering?

查看:346
本文介绍了c#图像抖动例程接受一定量的抖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用aforge的floyed-steinberg抖动方法,但我注意到你不能指定抖动量。我想要能够指定一个0到100之间的数量。所以,如果我要求50,它将抖动一半高达100.或者,如果我指定0,那么生成的图像将是纯色在每个颜色之间有硬边缘...或换句话说,没有抖动。我正在寻找一个例程的c#代码,如floyed-steinberg,或Jarvis,Judice,Ninke抖动,接受金额。任何人知道任何?



这里只是C ++抖动部分(其余部分在上面的链接中)

  // dithering 
r0 = 0; g0 = 0; b0 = 0; // no leftovers
for(y = 0; y for(x = 0; 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]);
//从pal []中找到最接近的颜色
for(i = 0,j = -1; 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; }
}
//获取选定的调色板颜色
c = pal [j];
//从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;
}

其中 coef =< 0,100& code>是你的规模。与链接答案中的代码的唯一变化是添加3行用于缩放抖动。这里有 VGA 256色调默认调色板的示例:



coef = 100





coef =〜75





coef =〜50





coef =〜25





coef = 0





[注意]



我的 coef 是由滚动条设置的,所以只有0%和100%



如果您将系数范围改为2的幂,例如< 0,128> 那么你可以使用bitshifts而不是除法缩放。


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?

解决方案

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.

这篇关于c#图像抖动例程接受一定量的抖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆