Add per Pixel Quantize Methods

This commit is contained in:
Unknown 2018-02-14 18:00:13 +01:00
parent 56994771f0
commit 0cc15e9b4f

View file

@ -21,16 +21,23 @@ class Pixed {
PixelArray pxArr = new PixelArray.fromPixelArray(image); PixelArray pxArr = new PixelArray.fromPixelArray(image);
for (var y = 0; y < pxArr.getHeight(); y++) { for (var y = 0; y < pxArr.getHeight(); y++) {
for (var x = 0; x < pxArr.getWidth(); x++) { for (var x = 0; x < pxArr.getWidth(); x++) {
var px = pxArr.getPixel(x, y); pxArr.setPixel( _quantizePixel(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; return pxArr;
} }
static PixelArray calcQuantizeError(PixelArray original, PixelArray quantized) { static Pixel _quantizePixel(Pixel pixel, [int steps=1]) {
return new Pixel(
pixel.x,
pixel.y,
(steps * pixel.r / 255).round() * (255 ~/ steps),
(steps * pixel.g / 255).round() * (255 ~/ steps),
(steps * pixel.b / 255).round() * (255 ~/ steps),
pixel.a);
}
static PixelArray quantizeError(PixelArray original, PixelArray quantized) {
PixelArray pxArr = new PixelArray.fromPixelArray(original); PixelArray pxArr = new PixelArray.fromPixelArray(original);
for (var y = 0; y < pxArr.getHeight(); y++) { for (var y = 0; y < pxArr.getHeight(); y++) {
for (var x = 0; x < pxArr.getWidth(); x++) { for (var x = 0; x < pxArr.getWidth(); x++) {
@ -43,6 +50,14 @@ class Pixed {
} }
return pxArr; return pxArr;
} }
static Pixel _quantizeErrorPixel(Pixel original, Pixel quantized) {
return new Pixel(original.x, original.y,
original.r - quantized.r,
original.g - quantized.g,
original.b - quantized.b,
original.a);
}
} }
class Pixel { class Pixel {
@ -92,7 +107,6 @@ class PixelArray {
int y = 0; int y = 0;
int x = 0; int x = 0;
pixels[y] = new List(imageWidth); pixels[y] = new List(imageWidth);
print(pixels[0].length.toString() + ","+ pixels.length.toString());
for (var pos = 0; pos < array.length; pos = pos + 4) { for (var pos = 0; pos < array.length; pos = pos + 4) {
if (x >= imageWidth) { if (x >= imageWidth) {
x = 0; x = 0;
@ -107,7 +121,7 @@ class PixelArray {
PixelArray.fromImageData(ImageData imagedata, int imageWidth): this.fromByteArray(imagedata.data, imageWidth); PixelArray.fromImageData(ImageData imagedata, int imageWidth): this.fromByteArray(imagedata.data, imageWidth);
PixelArray.fromPixelArray(PixelArray pixelArray) { PixelArray.fromPixelArray(PixelArray pixelArray) {
pixels = new List<List<Pixel>>.from(pixelArray.pixels); pixels = pixelArray.clone().pixels;
} }
Uint8ClampedList toByteArray() { Uint8ClampedList toByteArray() {