From 2f0de10848e8498a11a3abfc8a7728a12b12e8a4 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 14 Feb 2018 16:44:56 +0100 Subject: [PATCH] Add PixelArray clone function --- lib/Dither.dart | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/Dither.dart b/lib/Dither.dart index 940c068..8579376 100644 --- a/lib/Dither.dart +++ b/lib/Dither.dart @@ -48,10 +48,17 @@ class Pixel { } class PixelArray { + //TODO Implement iterable List> pixels; PixelArray(int width, int height) { - pixels = new List.filled(height, new List(width)); + pixels = new List(height); + for (int y = 0; y < height; y++) { + pixels[y] = new List(width); + for (int x = 0; x < height; x++) { + pixels[y][x] = new Pixel(x, y, 0, 0, 0, 0); + } + } } PixelArray.fromByteArray(Uint8ClampedList array, int imageWidth) { @@ -107,12 +114,12 @@ class PixelArray { } void setPixel(Pixel pixel, [int x, int y]) { - if (x != null && y != null && pixels[y] != null && pixels[y].length >= x) { + if (x != null && y != null && getHeight() < y && getWidth() < x) { pixel.x = x; pixel.y = y; pixels[y][x] = pixel; return; - } else if (pixels.length < pixel.y || pixels[y].length < pixel.x) { + } else if (getHeight() <= pixel.y || getWidth() <= pixel.x) { return; } else { pixels[pixel.y][pixel.x] = pixel; @@ -121,10 +128,23 @@ class PixelArray { } int getWidth() { + //TODO find longest/shortest width not at 0 (support for non square img) return pixels[0].length; } int getHeight() { return pixels.length; } + + PixelArray clone() { + PixelArray pxArr = new PixelArray(getWidth(), getHeight()); + for (var y = 0; y < getHeight(); y++) { + for (var x = 0; x < getWidth(); x++) { + Pixel px = getPixel(x, y); + Pixel newPix = new Pixel(x, y, px.r, px.g, px.b, px.a); + pxArr.setPixel( newPix, x, y ); + } + } + return pxArr; + } } \ No newline at end of file