Add PixelArray clone function

This commit is contained in:
Unknown 2018-02-14 16:44:56 +01:00
parent 33e07962bd
commit 2f0de10848

View file

@ -48,10 +48,17 @@ class Pixel {
} }
class PixelArray { class PixelArray {
//TODO Implement iterable
List<List<Pixel>> pixels; List<List<Pixel>> pixels;
PixelArray(int width, int height) { 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) { PixelArray.fromByteArray(Uint8ClampedList array, int imageWidth) {
@ -107,12 +114,12 @@ class PixelArray {
} }
void setPixel(Pixel pixel, [int x, int y]) { 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.x = x;
pixel.y = y; pixel.y = y;
pixels[y][x] = pixel; pixels[y][x] = pixel;
return; return;
} else if (pixels.length < pixel.y || pixels[y].length < pixel.x) { } else if (getHeight() <= pixel.y || getWidth() <= pixel.x) {
return; return;
} else { } else {
pixels[pixel.y][pixel.x] = pixel; pixels[pixel.y][pixel.x] = pixel;
@ -121,10 +128,23 @@ class PixelArray {
} }
int getWidth() { int getWidth() {
//TODO find longest/shortest width not at 0 (support for non square img)
return pixels[0].length; return pixels[0].length;
} }
int getHeight() { int getHeight() {
return pixels.length; 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;
}
} }