Skip to content
Permalink
Browse files

Floyd-Steinberg Dithering

  • Loading branch information...
Jeremy Heminger Jeremy Heminger
Jeremy Heminger authored and Jeremy Heminger committed Apr 2, 2019
1 parent 9804a22 commit 588deb56deaf65a7e0d9c39b0ef2609b0f42cc0f
Showing with 52 additions and 1 deletion.
  1. +8 −1 index.html
  2. +44 −0 script.js
@@ -30,9 +30,16 @@
.then(function(e){
test.draw(i,e[0].img)
test.loopPixels(test.getData(i),[{
f:test.nearestPixel,
params:{
i:j,
l:N,
ctx:test._ctx
}
},{
f:test.reduceColor,
params:{
f:4
f:10
}
}]).then(function(p) {
test.drawBuffer(p,j)
@@ -190,6 +190,50 @@ export class ReduceColors extends ImageData {
p.buffer[j+1] = max.c[1]
p.buffer[j+2] = max.c[2]
}
/**
* Floyd-Steinberg Dithering
* @param {Object}
* @param {Number}
* @param {Number}
* @param {Number}
* @param {Object} parameters
* @param {Object} reference the calling class
* */
FSDither(p,w,h,j,params,$t) {
// each pixel is 3 colors and opacity ... handle it here
w = w * 4
let oldp = p.buffer[j] + p.buffer[j+1] + p.buffer[j+2]
let newp = (Math.floor(Math.round(params.f * p.buffer[j] / 255) * (255/params.f))) * 3;
let qerror = oldp - newp

// r
if(undefined !== p.buffer[j+4])
p.buffer[j+4] += qerror * 7 / 16
if(undefined !== p.buffer[p.buffer[j-4+w]])
p.buffer[j-4+w] += qerror * 3 / 16
if(undefined !== p.buffer[j+w])
p.buffer[j+w] += qerror * 5 / 16
if(undefined !== p.buffer[j+4+w])
p.buffer[j+4+w] += qerror * 1 / 16
// g
if(undefined !== p.buffer[j+1+4])
p.buffer[j+1+4] += qerror * 7 / 16
if(undefined !== p.buffer[p.buffer[j-4+1+w]])
p.buffer[j-4+1+w] += qerror * 3 / 16
if(undefined !== p.buffer[j+w])
p.buffer[j+1+w] += qerror * 5 / 16
if(undefined !== p.buffer[j+4+1+w])
p.buffer[j+4+1+w] += qerror * 1 / 16
// b
if(undefined !== p.buffer[j+2+4])
p.buffer[j+2+4] += qerror * 7 / 16
if(undefined !== p.buffer[p.buffer[j-4+2+w]])
p.buffer[j-4+2+w] += qerror * 3 / 16
if(undefined !== p.buffer[j+2+w])
p.buffer[j+2+w] += qerror * 5 / 16
if(undefined !== p.buffer[j+4+2+w])
p.buffer[j+4+2+w] += qerror * 1 / 16
}
/**
* @param {Object}
* @param {Number}

0 comments on commit 588deb5

Please sign in to comment.
You can’t perform that action at this time.