JavaScript中的单色抖动(Bayer、Atkinson、Floyd-Steinberg)

13

我正在使用HTML5进行网络摄像头滤镜的开发。已经成功实现了 Atkinson dither ,可以让画面呈现出老式Mac的感觉。

meemoo cam to dither
现场演示 | 源代码

现在我想要制作一个Bayer有序抖动选项,以呈现1989年Gameboy的感觉。

我已经 阅读了该算法,但是我在将伪代码转换为JavaScript时遇到了一些困难:

for each y
  for each x
    oldpixel := pixel[x][y] + threshold_map_4x4[x mod 4][y mod 4]
    newpixel := find_closest_palette_color(oldpixel)
    pixel[x][y] := newpixel

在AS3、PHP或JS中有没有相关的示例呢?你能解释一下 threshold_map_4x4[x mod 4][y mod 4] 发生了什么吗?


类似Gameboy的gif动画(使用Meemoo Gameboy GIFerizer制作)

我想到了。在维基百科上,它说:“例如,在单色渲染中,如果像素的值(缩放到0-9范围内)小于矩阵中相应单元格中的数字,则将该像素绘制为黑色,否则将其绘制为白色。” 在JS中,通过对当前像素(0-255)和映射值(15-240)进行平均并将其与阈值(通常为129)进行比较,可以获得良好的结果:

var map = (imageData.data[currentPixel] + bayerThresholdMap[x%4][y%4]) / 2;
imageData.data[currentPixel] = (map < threshold) ? 0 : 255;

以下是我整个单色函数并包含不同算法:

var bayerThresholdMap = [
  [  15, 135,  45, 165 ],
  [ 195,  75, 225, 105 ],
  [  60, 180,  30, 150 ],
  [ 240, 120, 210,  90 ]
];

var lumR = [];
var lumG = [];
var lumB = [];
for (var i=0; i<256; i++) {
  lumR[i] = i*0.299;
  lumG[i] = i*0.587;
  lumB[i] = i*0.114;
}

function monochrome(imageData, threshold, type){

  var imageDataLength = imageData.data.length;

  // Greyscale luminance (sets r pixels to luminance of rgb)
  for (var i = 0; i <= imageDataLength; i += 4) {
    imageData.data[i] = Math.floor(lumR[imageData.data[i]] + lumG[imageData.data[i+1]] + lumB[imageData.data[i+2]]);
  }

  var w = imageData.width;
  var newPixel, err;

  for (var currentPixel = 0; currentPixel <= imageDataLength; currentPixel+=4) {

    if (type === "none") {
      // No dithering
      imageData.data[currentPixel] = imageData.data[currentPixel] < threshold ? 0 : 255;
    } else if (type === "bayer") {
      // 4x4 Bayer ordered dithering algorithm
      var x = currentPixel/4 % w;
      var y = Math.floor(currentPixel/4 / w);
      var map = Math.floor( (imageData.data[currentPixel] + bayerThresholdMap[x%4][y%4]) / 2 );
      imageData.data[currentPixel] = (map < threshold) ? 0 : 255;
    } else if (type === "floydsteinberg") {
      // Floyd–Steinberg dithering algorithm
      newPixel = imageData.data[currentPixel] < 129 ? 0 : 255;
      err = Math.floor((imageData.data[currentPixel] - newPixel) / 16);
      imageData.data[currentPixel] = newPixel;

      imageData.data[currentPixel       + 4 ] += err*7;
      imageData.data[currentPixel + 4*w - 4 ] += err*3;
      imageData.data[currentPixel + 4*w     ] += err*5;
      imageData.data[currentPixel + 4*w + 4 ] += err*1;
    } else {
      // Bill Atkinson's dithering algorithm
      newPixel = imageData.data[currentPixel] < threshold ? 0 : 255;
      err = Math.floor((imageData.data[currentPixel] - newPixel) / 8);
      imageData.data[currentPixel] = newPixel;

      imageData.data[currentPixel       + 4 ] += err;
      imageData.data[currentPixel       + 8 ] += err;
      imageData.data[currentPixel + 4*w - 4 ] += err;
      imageData.data[currentPixel + 4*w     ] += err;
      imageData.data[currentPixel + 4*w + 4 ] += err;
      imageData.data[currentPixel + 8*w     ] += err;
    }

    // Set g and b pixels equal to r
    imageData.data[currentPixel + 1] = imageData.data[currentPixel + 2] = imageData.data[currentPixel];
  }

  return imageData;
}

我希望得到优化提示。

2个回答

11

@500-服务器内部错误 :) 已修复。 - forresto

4

我将这段代码作为调试代码:

var canvas = document.createElement('canvas');
var ctx    = canvas.getContext('2d');

var img    = new Image();
img.src    = "http://i.stack.imgur.com/tHDY8.png";
img.onload = function() {
    canvas.width  = this.width;
    canvas.height = this.height;
    ctx.drawImage( this, 0, 0, this.width, this.height );

    var imageData  = ctx.getImageData( 0, 0, this.width, this.height);
    var depth      = 32;


    // Matrix
    var threshold_map_4x4 = [
        [  1,  9,  3, 11 ],
        [ 13,  5, 15,  7 ],
        [  4, 12,  2, 10 ],
        [ 16,  8, 14,  6 ]
    ];

    // imageData
    var width  = imageData.width;
    var height = imageData.height;
    var pixel  = imageData.data;
    var x, y, a, b;

    // filter
    for ( x=0; x<width; x++ )
    {
        for ( y=0; y<height; y++ )
        {
            a    = ( x * height + y ) * 4;
            b    = threshold_map_4x4[ x%4 ][ y%4 ];
            pixel[ a + 0 ] = ( (pixel[ a + 0 ]+ b) / depth | 0 ) * depth;
            pixel[ a + 1 ] = ( (pixel[ a + 1 ]+ b) / depth | 0 ) * depth;
            pixel[ a + 2 ] = ( (pixel[ a + 2 ]+ b) / depth | 0 ) * depth;
            //pixel[ a + 3 ] = ( (pixel[ a + 3 ]+ b) / depth | 3 ) * depth;
        }
    }

    ctx.putImageData( imageData, 0, 0);

};

document.body.appendChild(canvas);

看起来它运行良好,您可以更改深度变量以更改色调分离。


由于这是单色的,您可以将其更改为:pixel [a] = pixel [a + 1] = pixel [a + 2] = ((pixel [a] + b) / depth | 0) * depth; - forresto
请检查控制台,我在跨域图像方面遇到了问题(好吧,我正在使用旧浏览器...)。所以我去到图像网址,打开控制台,复制粘贴代码,它可以正常工作(http://tof.canardpc.com/view/f47567dc-d06b-4fea-8b9f-ac7c23e9ceb8.jpg),或者您想要其他结果? - Vincent Thibault
是的,我想要单色输出。我把我的算法放在原问题中,并制作了这个实时演示:http://meemoo.org/iframework/#gist/3721129 - forresto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接