Add quantize Function

This commit is contained in:
Unknown 2018-02-14 16:45:43 +01:00
parent 2f0de10848
commit 4a341451ba

View file

@ -14,7 +14,33 @@ class Pixed {
px.b = average; px.b = average;
} }
} }
return pxArr;
}
static PixelArray quantize(PixelArray image, [int steps=1]) {
PixelArray pxArr = new PixelArray.fromPixelArray(image);
for (var y = 0; y < pxArr.getHeight(); y++) {
for (var x = 0; x < pxArr.getWidth(); x++) {
var px = pxArr.getPixel(x, y);
px.r = (steps * px.r / 255).round() * (255 ~/ steps);
px.g = (steps * px.g / 255).round() * (255 ~/ steps);
px.b = (steps * px.b / 255).round() * (255 ~/ steps);
}
}
return pxArr;
}
static PixelArray calcQuantizeError(PixelArray original, PixelArray quantized) {
PixelArray pxArr = new PixelArray.fromPixelArray(original);
for (var y = 0; y < pxArr.getHeight(); y++) {
for (var x = 0; x < pxArr.getWidth(); x++) {
Pixel px = pxArr.getPixel(x, y);
Pixel quant = quantized.getPixel(x, y);
px.r = px.r - quant.r;
px.g = px.g - quant.g;
px.b = px.b - quant.b;
}
}
return pxArr; return pxArr;
} }
} }