From 0cc15e9b4f67d0641ee9f29dfae36f9e97f4302a Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 14 Feb 2018 18:00:13 +0100 Subject: [PATCH] Add per Pixel Quantize Methods --- lib/Dither.dart | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/Dither.dart b/lib/Dither.dart index b51e2ea..1201655 100644 --- a/lib/Dither.dart +++ b/lib/Dither.dart @@ -21,16 +21,23 @@ class Pixed { 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); + pxArr.setPixel( _quantizePixel(pxArr.getPixel(x, y)) ); } } 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); for (var y = 0; y < pxArr.getHeight(); y++) { for (var x = 0; x < pxArr.getWidth(); x++) { @@ -43,6 +50,14 @@ class Pixed { } 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 { @@ -92,7 +107,6 @@ class PixelArray { int y = 0; int x = 0; pixels[y] = new List(imageWidth); - print(pixels[0].length.toString() + ","+ pixels.length.toString()); for (var pos = 0; pos < array.length; pos = pos + 4) { if (x >= imageWidth) { x = 0; @@ -107,7 +121,7 @@ class PixelArray { PixelArray.fromImageData(ImageData imagedata, int imageWidth): this.fromByteArray(imagedata.data, imageWidth); PixelArray.fromPixelArray(PixelArray pixelArray) { - pixels = new List>.from(pixelArray.pixels); + pixels = pixelArray.clone().pixels; } Uint8ClampedList toByteArray() {