floyd-steinberg-dithering/web/main.dart

35 lines
919 B
Dart
Raw Permalink Normal View History

2018-02-12 14:25:58 +00:00
import 'dart:html';
2018-02-12 14:55:45 +00:00
import 'package:dart_floyd_steinberg_dithering/Dither.dart';
2018-02-12 14:25:58 +00:00
CanvasElement input;
CanvasElement output;
ImageElement inputImg;
2018-02-12 14:55:45 +00:00
ImageElement outputImg;
2018-02-12 14:25:58 +00:00
void main() {
2018-02-14 17:00:57 +00:00
inputImg = new ImageElement(src: 'kitten.jpg');
2018-02-12 14:25:58 +00:00
input = document.querySelector('#input');
output = document.querySelector('#output');
2018-02-14 17:00:57 +00:00
input.width = inputImg.width;
input.height = inputImg.height;
output.width = inputImg.width;
output.height = inputImg.height;
2018-02-12 14:55:45 +00:00
inputImg.onLoad.listen(imgLoaded);
2018-02-12 14:25:58 +00:00
}
void imgLoaded(Event e) {
print("image loaded");
input.context2D.drawImage(inputImg, 0, 0);
2018-02-14 17:00:57 +00:00
PixelArray px = new PixelArray.fromImageData(getImageData(input), input.width);
2018-02-14 14:56:55 +00:00
px = Pixed.greyscale(px);
2018-02-14 17:00:57 +00:00
px = Pixed.dither(px);
2018-02-14 14:56:55 +00:00
output.context2D.putImageData(px.toImageData(), 0, 0);
2018-02-12 14:25:58 +00:00
}
2018-02-12 14:55:45 +00:00
ImageData getImageData(CanvasElement canvas) {
return canvas.context2D.getImageData(0, 0, canvas.width, canvas.height);
2018-02-12 14:25:58 +00:00
}