diff --git a/lib/src/core/array.dart b/lib/src/core/array.dart index 6e2393d6..51bb1f34 100644 --- a/lib/src/core/array.dart +++ b/lib/src/core/array.dart @@ -3,6 +3,8 @@ import 'dart:ffi' as ffi; import 'package:equatable/equatable.dart'; import 'package:ffi/ffi.dart'; +import 'base.dart'; + // Dart does not support multiple upper bounds for T now, if they implement it, this can be simplified. // https://github.com/dart-lang/language/issues/2709 abstract class NativeArray @@ -25,8 +27,14 @@ abstract class INativeArray { } class U8Array extends NativeArray { - U8Array([int length = 0]) : super(length) { + U8Array([int length = 0, int value = 0]) : super(length) { + assert(length >= 0 && value <= CV_U8_MAX && value >= CV_U8_MIN); ptr = calloc(length); + if (value != 0) { + for (var idx = 0; idx < length; idx++) { + ptr[idx] = value; + } + } finalizer.attach(this, ptr.cast()); } @@ -63,8 +71,14 @@ class U8Array extends NativeArray { } class I8Array extends NativeArray { - I8Array([int length = 0]) : super(length) { + I8Array([int length = 0, int value = 0]) : super(length) { + assert(length >= 0 && value <= CV_I8_MAX && value >= CV_I8_MIN); ptr = calloc(length); + if (value != 0) { + for (var idx = 0; idx < length; idx++) { + ptr[idx] = value; + } + } finalizer.attach(this, ptr.cast()); } @@ -76,6 +90,11 @@ class I8Array extends NativeArray { return array; } + I8Array.fromPointer(ffi.Pointer ptr, int length) : super(length) { + this.ptr = ptr; + finalizer.attach(this, ptr.cast()); + } + static final finalizer = ffi.NativeFinalizer(calloc.nativeFree); @override @@ -96,8 +115,14 @@ class I8Array extends NativeArray { } class U16Array extends NativeArray { - U16Array([int length = 0]) : super(length) { + U16Array([int length = 0, int value = 0]) : super(length) { + assert(length >= 0 && value <= CV_U16_MAX && value >= CV_U16_MIN); ptr = calloc(length); + if (value != 0) { + for (var idx = 0; idx < length; idx++) { + ptr[idx] = value; + } + } finalizer.attach(this, ptr.cast()); } @@ -109,6 +134,11 @@ class U16Array extends NativeArray { return array; } + U16Array.fromPointer(ffi.Pointer ptr, int length) : super(length) { + this.ptr = ptr; + finalizer.attach(this, ptr.cast()); + } + static final finalizer = ffi.NativeFinalizer(calloc.nativeFree); @override @@ -129,8 +159,14 @@ class U16Array extends NativeArray { } class I16Array extends NativeArray { - I16Array([int length = 0]) : super(length) { + I16Array([int length = 0, int value = 0]) : super(length) { + assert(length >= 0 && value <= CV_I16_MAX && value >= CV_I16_MIN); ptr = calloc(length); + if (value != 0) { + for (var idx = 0; idx < length; idx++) { + ptr[idx] = value; + } + } finalizer.attach(this, ptr.cast()); } @@ -142,6 +178,11 @@ class I16Array extends NativeArray { return array; } + I16Array.fromPointer(ffi.Pointer ptr, int length) : super(length) { + this.ptr = ptr; + finalizer.attach(this, ptr.cast()); + } + static final finalizer = ffi.NativeFinalizer(calloc.nativeFree); @override @@ -162,8 +203,14 @@ class I16Array extends NativeArray { } class I32Array extends NativeArray { - I32Array([int length = 0]) : super(length) { + I32Array([int length = 0, int value = 0]) : super(length) { + assert(length >= 0 && value <= CV_I32_MAX && value >= CV_I32_MIN); ptr = calloc(length); + if (value != 0) { + for (var idx = 0; idx < length; idx++) { + ptr[idx] = value; + } + } finalizer.attach(this, ptr.cast()); } @@ -175,6 +222,11 @@ class I32Array extends NativeArray { return array; } + I32Array.fromPointer(ffi.Pointer ptr, int length) : super(length) { + this.ptr = ptr; + finalizer.attach(this, ptr.cast()); + } + static final finalizer = ffi.NativeFinalizer(calloc.nativeFree); @override @@ -195,8 +247,14 @@ class I32Array extends NativeArray { } class F32Array extends NativeArray { - F32Array([int length = 0]) : super(length) { + F32Array([int length = 0, double value = 0]) : super(length) { + assert(length >= 0 && value <= CV_F32_MAX); ptr = calloc(length); + if (value != 0) { + for (var idx = 0; idx < length; idx++) { + ptr[idx] = value; + } + } finalizer.attach(this, ptr.cast()); } @@ -208,6 +266,11 @@ class F32Array extends NativeArray { return array; } + F32Array.fromPointer(ffi.Pointer ptr, int length) : super(length) { + this.ptr = ptr; + finalizer.attach(this, ptr.cast()); + } + static final finalizer = ffi.NativeFinalizer(calloc.nativeFree); @override @@ -228,8 +291,14 @@ class F32Array extends NativeArray { } class F64Array extends NativeArray { - F64Array([int length = 0]) : super(length) { + F64Array([int length = 0, double value = 0]) : super(length) { + assert(length >= 0 && value <= CV_F64_MAX); ptr = calloc(length); + if (value != 0) { + for (var idx = 0; idx < length; idx++) { + ptr[idx] = value; + } + } finalizer.attach(this, ptr.cast()); } @@ -241,6 +310,11 @@ class F64Array extends NativeArray { return array; } + F64Array.fromPointer(ffi.Pointer ptr, int length) : super(length) { + this.ptr = ptr; + finalizer.attach(this, ptr.cast()); + } + static final finalizer = ffi.NativeFinalizer(calloc.nativeFree); @override diff --git a/lib/src/core/error_code.dart b/lib/src/core/error_code.dart index 6fbf0335..641f11db 100644 --- a/lib/src/core/error_code.dart +++ b/lib/src/core/error_code.dart @@ -1,3 +1,4 @@ +// coverage:ignore-file // ignore_for_file: non_constant_identifier_names /* Error status codes @@ -5,7 +6,7 @@ Original Author: @shimat LICENSE: Apache-2.0 https://github.com/shimat/opencvsharp/blob/main/src/OpenCvSharp/Modules/core/Enum/ErrorCode.cs - + Ported to Dart By: @Rainyl LICENSE: Apache-2.0 */ diff --git a/lib/src/opencv.dart b/lib/src/opencv.dart index 6bdd5546..f34d2ef5 100644 --- a/lib/src/opencv.dart +++ b/lib/src/opencv.dart @@ -6,6 +6,7 @@ export 'contrib/aruco.dart'; export 'contrib/aruco_dict.dart'; export 'contrib/img_hash.dart'; +export 'core/array.dart'; export 'core/asyncarray.dart'; export 'core/base.dart'; export 'core/core.dart'; diff --git a/test/core/array_test.dart b/test/core/array_test.dart new file mode 100644 index 00000000..81139a37 --- /dev/null +++ b/test/core/array_test.dart @@ -0,0 +1,95 @@ +import 'package:test/test.dart'; +import 'package:opencv_dart/opencv_dart.dart' as cv; + +void main() { + test('cv.U8Array', () { + final array = cv.U8Array(10, 1); + expect(array[0], 1); + array[1] = 2; + expect(array[1], 2); + array[1] = 1; + expect(array.toList(), List.generate(10, (i) => 1)); + + final array2 = cv.U8Array.fromList(List.generate(array.length, (i) => 1)); + expect(array2.props, isNotEmpty); + expect(array2.toList(), array2.toList()); + }); + + test('cv.I8Array', () { + final array = cv.I8Array(10, 1); + expect(array[0], 1); + array[1] = 2; + expect(array[1], 2); + array[1] = 1; + expect(array.toList(), List.generate(10, (i) => 1)); + + final array2 = cv.I8Array.fromList(List.generate(array.length, (i) => 1)); + expect(array2.props, isNotEmpty); + expect(array2.toList(), array2.toList()); + }); + + test('cv.U16Array', () { + final array = cv.U16Array(10, 1); + expect(array[0], 1); + array[1] = 2; + expect(array[1], 2); + array[1] = 1; + expect(array.toList(), List.generate(10, (i) => 1)); + + final array2 = cv.U16Array.fromList(List.generate(array.length, (i) => 1)); + expect(array2.props, isNotEmpty); + expect(array2.toList(), array2.toList()); + }); + + test('cv.I16Array', () { + final array = cv.I16Array(10, 1); + expect(array[0], 1); + array[1] = 2; + expect(array[1], 2); + array[1] = 1; + expect(array.toList(), List.generate(10, (i) => 1)); + + final array2 = cv.I16Array.fromList(List.generate(array.length, (i) => 1)); + expect(array2.props, isNotEmpty); + expect(array2.toList(), array2.toList()); + }); + + test('cv.I32Array', () { + final array = cv.I32Array(10, 1); + expect(array[0], 1); + array[1] = 2; + expect(array[1], 2); + array[1] = 1; + expect(array.toList(), List.generate(10, (i) => 1)); + + final array2 = cv.I32Array.fromList(List.generate(array.length, (i) => 1)); + expect(array2.props, isNotEmpty); + expect(array2.toList(), array2.toList()); + }); + + test('cv.F32Array', () { + final array = cv.F32Array(10, 1); + expect(array[0], 1); + array[1] = 2; + expect(array[1], 2); + array[1] = 1; + expect(array.toList(), List.generate(10, (i) => 1)); + + final array2 = cv.F32Array.fromList(List.generate(array.length, (i) => 1)); + expect(array2.props, isNotEmpty); + expect(array2.toList(), array2.toList()); + }); + + test('cv.F64Array', () { + final array = cv.F64Array(10, 1); + expect(array[0], 1); + array[1] = 2; + expect(array[1], 2); + array[1] = 1; + expect(array.toList(), List.generate(10, (i) => 1)); + + final array2 = cv.F64Array.fromList(List.generate(array.length, (i) => 1)); + expect(array2.props, isNotEmpty); + expect(array2.toList(), array2.toList()); + }); +} diff --git a/test/core/others_test.dart b/test/core/others_test.dart index 3b833f4c..0fb47d08 100644 --- a/test/core/others_test.dart +++ b/test/core/others_test.dart @@ -22,4 +22,36 @@ void main() { }); expect(s5, cv.Scalar.blue); }); + + test('cv.Vec6i', () { + final vec = cv.Vec6i(1, 2, 3, 4, 5, 6); + expect(vec.val, [1, 2, 3, 4, 5, 6]); + final vec1 = cv.Vec6i.fromNative(vec.ref); + expect(vec1.val, vec.val); + expect(vec.toString(), "Vec6i(1, 2, 3, 4, 5, 6)"); + }); + + test('cv.Vec6f', () { + final vec = cv.Vec6f(1, 2, 3, 4, 5, 6); + expect(vec.val, [1, 2, 3, 4, 5, 6]); + final vec1 = cv.Vec6f.fromNative(vec.ref); + expect(vec1.val, vec.val); + expect(vec.toString(), "Vec6f(1.000, 2.000, 3.000, 4.000, 5.000, 6.000)"); + }); + + test('cv.Vec6d', () { + final vec = cv.Vec6d(1, 2, 3, 4, 5, 6); + expect(vec.val, [1, 2, 3, 4, 5, 6]); + final vec1 = cv.Vec6d.fromNative(vec.ref); + expect(vec1.val, vec.val); + expect(vec.toString(), "Vec6d(1.000, 2.000, 3.000, 4.000, 5.000, 6.000)"); + }); + + test('cv.Vec8i', () { + final vec = cv.Vec8i(1, 2, 3, 4, 5, 6, 7, 8); + expect(vec.val, [1, 2, 3, 4, 5, 6, 7, 8]); + final vec1 = cv.Vec8i.fromNative(vec.ref); + expect(vec1.val, vec.val); + expect(vec.toString(), "Vec8i(1, 2, 3, 4, 5, 6, 7, 8)"); + }); }