Add quantize Function
This commit is contained in:
parent
2f0de10848
commit
4a341451ba
1 changed files with 26 additions and 0 deletions
|
@ -14,7 +14,33 @@ class Pixed {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue