Add Basic Data Structures

This commit is contained in:
Unknown 2018-02-14 15:19:21 +01:00
parent 60f5ce3cc1
commit d6503bb613
2 changed files with 91 additions and 12 deletions

View file

@ -1,18 +1,98 @@
import 'dart:html';
import 'dart:typed_data';
class Dither {
static ImageData editImage(ImageData image) {
class Pixel {
int x;
int y;
int r;
int g;
int b;
int a;
Pixel(this.x, this.y, this.r, this.g, this.b, this.a);
// image = contrast( image, 200);
for (var y = 0; y < image.height; y++) {
for (var x = 0; x < image.width; x++) {
// print(image.getPixel(x, y));
}
}
return image;
int getBytePos(int imageWidth) {
return (x+y*imageWidth)*4;
}
void setRGB(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
@override
String toString() {
return 'Pixel at $x, $y: {r: $r, g: $g, b: $b, a: $a}';
}
}
class PixelArray {
List<List<Pixel>> pixels;
PixelArray.fromByteArray(Uint8ClampedList array, int imageWidth) {
pixels = new List(array.length~/imageWidth~/4);
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;
y++;
pixels[y] = new List(imageWidth);
}
pixels[y][x] = new Pixel(x, y, array[pos], array[pos+1], array[pos+2], array[pos+3]);
x++;
}
}
Uint8ClampedList toByteArray() {
//TODO: look for longest array, or each line separately. Only gets width from line 1 currently.
Uint8ClampedList result = new Uint8ClampedList(getHeight()*getWidth()*4);
int x = 0;
int y = 0;
for (var i = 0; i < result.length; i = i + 4) {
if (x >= getWidth()) {
y++;
x = 0;
}
result[i] = pixels[y][x].r;
result[i+1] = pixels[y][x].g;
result[i+2] = pixels[y][x].b;
result[i+3] = pixels[y][x].a;
x++;
}
return result;
}
Pixel getPixel(int x, int y) {
return pixels[y][x];
}
void setPixel(Pixel pixel, [int x, int y]) {
if (x != null && y != null && pixels[y] != null && pixels[y].length >= x) {
pixel.x = x;
pixel.y = y;
pixels[y][x] = pixel;
return;
} else if (pixels.length < pixel.y || pixels[y].length < pixel.x) {
return;
} else {
pixels[pixel.y][pixel.x] = pixel;
return;
}
}
int getWidth() {
return pixels[0].length;
}
int getHeight() {
return pixels.length;
}
}

View file

@ -20,7 +20,6 @@ void imgLoaded(Event e) {
print("image loaded");
input.context2D.drawImage(inputImg, 0, 0);
output.context2D.putImageData( Dither.editImage( getImageData(input)), 0, 0);
}
ImageData getImageData(CanvasElement canvas) {