Add Dither Function

This commit is contained in:
Unknown 2018-02-14 18:00:25 +01:00
parent 0cc15e9b4f
commit c5a68ea94b

View file

@ -58,6 +58,39 @@ class Pixed {
original.b - quantized.b,
original.a);
}
static PixelArray dither(PixelArray image) {
PixelArray pxArr = new PixelArray.fromPixelArray(image);
for (var y = 0; y < pxArr.getHeight()-1; y++) {
for (var x = 1; x < pxArr.getWidth()-1; x++) {
Pixel origPx = pxArr.getPixel(x, y);
Pixel quantPx = _quantizePixel(origPx);
Pixel errPx = _quantizeErrorPixel(origPx, quantPx);
pxArr.setPixel(quantPx);
Pixel px = pxArr.getPixel(x+1, y);
px.r = (px.r + errPx.r * (7/16)).toInt();
px.g = (px.g + errPx.g * (7/16)).toInt();
px.b = (px.b + errPx.b * (7/16)).toInt();
px = pxArr.getPixel(x-1, y+1);
px.r = (px.r + errPx.r * (3/16)).toInt();
px.g = (px.g + errPx.g * (3/16)).toInt();
px.b = (px.b + errPx.b * (3/16)).toInt();
px = pxArr.getPixel(x, y+1);
px.r = (px.r + errPx.r * (5/16)).toInt();
px.g = (px.g + errPx.g * (5/16)).toInt();
px.b = (px.b + errPx.b * (5/16)).toInt();
px = pxArr.getPixel(x+1, y+1);
px.r = (px.r + errPx.r * (1/16)).toInt();
px.g = (px.g + errPx.g * (1/16)).toInt();
px.b = (px.b + errPx.b * (1/16)).toInt();
}
}
return pxArr;
}
}
class Pixel {