Add PixelArray clone function

This commit is contained in:
Unknown 2018-02-14 16:44:56 +01:00
parent 33e07962bd
commit 2f0de10848
1 changed files with 23 additions and 3 deletions

View File

@ -48,10 +48,17 @@ class Pixel {
}
class PixelArray {
//TODO Implement iterable
List<List<Pixel>> 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;
}
}