|
@@ -0,0 +1,159 @@
|
|
|
+import 'dart:typed_data';
|
|
|
+
|
|
|
+class Test {
|
|
|
+ void run() {
|
|
|
+ final processor = BrightnessProcessor();
|
|
|
+ processor.propertyChanged.add(onPropertyChanged);
|
|
|
+ processor.brightness++;
|
|
|
+ processor.propertyChanged.remove(onPropertyChanged);
|
|
|
+ processor.brightness++;
|
|
|
+ }
|
|
|
+
|
|
|
+ onPropertyChanged(Object sender, void e) {
|
|
|
+ //
|
|
|
+ print('onPropertyChanged');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+abstract class IImageProcessor {
|
|
|
+ // final propertyChanged = FEventHandler<int>();
|
|
|
+ late final FEventHandler<void> propertyChanged;
|
|
|
+
|
|
|
+ bool get changed;
|
|
|
+
|
|
|
+ Uint8List process(Uint8List pixelDatas);
|
|
|
+
|
|
|
+ void reset();
|
|
|
+}
|
|
|
+
|
|
|
+class BrightnessProcessor implements IImageProcessor {
|
|
|
+ int _brightness = 0;
|
|
|
+
|
|
|
+ int get brightness => _brightness;
|
|
|
+
|
|
|
+ set brightness(int value) {
|
|
|
+ if (_brightness != value) {
|
|
|
+ _brightness = value;
|
|
|
+
|
|
|
+ // OnBrightnessChanged();
|
|
|
+ onPropertyChanged();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ bool get changed => _brightness != 0;
|
|
|
+
|
|
|
+ @override
|
|
|
+ Uint8List process(Uint8List pixelDatas) {
|
|
|
+ if (brightness == 0) {
|
|
|
+ return pixelDatas;
|
|
|
+ }
|
|
|
+ int bVal = brightness;
|
|
|
+ if (bVal < -255) bVal = -255;
|
|
|
+ if (bVal > 255) bVal = 255;
|
|
|
+ for (int i = 0; i < pixelDatas.length - 1; i += 4) {
|
|
|
+ int b = pixelDatas[i];
|
|
|
+ int g = pixelDatas[i + 1];
|
|
|
+ int r = pixelDatas[i + 2];
|
|
|
+ b += bVal;
|
|
|
+ g += bVal;
|
|
|
+ r += bVal;
|
|
|
+
|
|
|
+ if (b < 0) b = 0;
|
|
|
+ if (b > 255) b = 255;
|
|
|
+ if (g < 0) g = 0;
|
|
|
+ if (g > 255) g = 255;
|
|
|
+ if (r < 0) r = 0;
|
|
|
+ if (r > 255) r = 255;
|
|
|
+ pixelDatas[i] = b;
|
|
|
+ pixelDatas[i + 1] = g;
|
|
|
+ pixelDatas[i + 2] = r;
|
|
|
+ }
|
|
|
+ // for (int i = 0; i < pixelDatas.length - 1; i++) {
|
|
|
+ // int pixel = pixelDatas[i];
|
|
|
+
|
|
|
+ // List<int> pixelBytes = intToBytes(pixel);
|
|
|
+ // int b = pixelBytes[0];
|
|
|
+ // int g = pixelBytes[1];
|
|
|
+ // int r = pixelBytes[2];
|
|
|
+ // b += bVal;
|
|
|
+ // g += bVal;
|
|
|
+ // r += bVal;
|
|
|
+
|
|
|
+ // if (b < 0) b = 0;
|
|
|
+ // if (b > 255) b = 255;
|
|
|
+ // if (g < 0) g = 0;
|
|
|
+ // if (g > 255) g = 255;
|
|
|
+ // if (r < 0) r = 0;
|
|
|
+ // if (r > 255) r = 255;
|
|
|
+ // pixelBytes[0] = b;
|
|
|
+ // pixelBytes[1] = g;
|
|
|
+ // pixelBytes[2] = r;
|
|
|
+ // ByteData data = ByteData.view(Uint8List.fromList(pixelBytes).buffer);
|
|
|
+ // final rst = data.getInt32(0, Endian.host);
|
|
|
+ // // final rst = bytesToInt(pixelBytes);
|
|
|
+ // pixelDatas[i] = rst;
|
|
|
+ // }
|
|
|
+ return pixelDatas;
|
|
|
+ }
|
|
|
+
|
|
|
+ static List<int> intToBytes(int i) {
|
|
|
+ final bytes = List<int>.filled(4, 0, growable: false);
|
|
|
+ bytes[0] = i & 0xff;
|
|
|
+ bytes[1] = (i >> 8) & 0xff;
|
|
|
+ bytes[2] = (i >> 16) & 0xff;
|
|
|
+ bytes[3] = (i >> 24) & 0xff;
|
|
|
+ return bytes;
|
|
|
+ }
|
|
|
+
|
|
|
+ static int bytesToInt(List<int> bytes, [int offset = 0]) {
|
|
|
+ int value;
|
|
|
+ value = (bytes[offset] & 0xff) |
|
|
|
+ ((bytes[offset + 1] & 0xff) << 8) |
|
|
|
+ ((bytes[offset + 2] & 0xff) << 16) |
|
|
|
+ ((bytes[offset + 3] & 0xff) << 24);
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ void reset() {
|
|
|
+ print('reset');
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ FEventHandler<void> propertyChanged = FEventHandler<void>();
|
|
|
+
|
|
|
+ void onPropertyChanged() {
|
|
|
+ propertyChanged.emit(this, null);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+typedef FEventCallback<T> = void Function(
|
|
|
+ Object sender,
|
|
|
+ T e,
|
|
|
+);
|
|
|
+
|
|
|
+class FEventHandler<T> {
|
|
|
+ List<FEventCallback<T>>? _callbacks;
|
|
|
+
|
|
|
+ void emit(Object sender, T e) {
|
|
|
+ if (_callbacks != null && _callbacks!.isNotEmpty) {
|
|
|
+ for (final callback in _callbacks!) {
|
|
|
+ callback.call(sender, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void add(FEventCallback<T> callback) {
|
|
|
+ _callbacks ??= [];
|
|
|
+ _callbacks!.add(callback);
|
|
|
+ }
|
|
|
+
|
|
|
+ void remove(FEventCallback<T> callback) {
|
|
|
+ if (_callbacks == null) return;
|
|
|
+
|
|
|
+ if (_callbacks!.contains(callback)) {
|
|
|
+ _callbacks!.remove(callback);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|