Skip to content

Commit a8354eb

Browse files
committed
sync #67 #69 in main branch
1 parent 64bd174 commit a8354eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2045
-1118
lines changed

lib/src/calib3d/calib3d.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class Fisheye {
141141
bool centerPrincipalPoint = false,
142142
}) {
143143
return using<(Mat, Rect)>((arena) {
144-
final validPixROI = arena<cvg.Rect>();
145-
final matPtr = arena<cvg.Mat>();
144+
final validPixROI = calloc<cvg.Rect>();
145+
final matPtr = calloc<cvg.Mat>();
146146
cvRun(
147147
() => cvg.GetOptimalNewCameraMatrixWithParams(
148148
cameraMatrix.ref,
@@ -155,7 +155,7 @@ class Fisheye {
155155
matPtr,
156156
),
157157
);
158-
return (Mat.fromCMat(matPtr.ref), Rect.fromNative(validPixROI.ref));
158+
return (Mat.fromPointer(matPtr), Rect.fromPointer(validPixROI));
159159
});
160160
}
161161

@@ -189,7 +189,7 @@ class Fisheye {
189189
rvecs!.ref,
190190
tvecs!.ref,
191191
flags,
192-
criteria.toTermCriteria(arena).ref,
192+
criteria.toNativePtr(arena).ref,
193193
rmsErr,
194194
),
195195
);
@@ -242,7 +242,7 @@ Mat undistortPoints(
242242
distCoeffs.ref,
243243
R!.ref,
244244
P!.ref,
245-
criteria.toTermCriteria(arena).ref,
245+
criteria.toNativePtr(arena).ref,
246246
),
247247
),
248248
);
@@ -362,7 +362,7 @@ Mat drawChessboardCorners(
362362
}) {
363363
return cvRunArena<(Mat, Mat)>((arena) {
364364
inliers ??= Mat.empty();
365-
final p = arena<cvg.Mat>();
365+
final p = calloc<cvg.Mat>();
366366
cvRun(
367367
() => cvg.EstimateAffinePartial2DWithParams(
368368
from.ref,
@@ -376,7 +376,7 @@ Mat drawChessboardCorners(
376376
p,
377377
),
378378
);
379-
return (Mat.fromCMat(p.ref), inliers!);
379+
return (Mat.fromPointer(p), inliers!);
380380
});
381381
}
382382

@@ -396,7 +396,7 @@ Mat drawChessboardCorners(
396396
}) {
397397
return cvRunArena<(Mat, Mat)>((arena) {
398398
inliers ??= Mat.empty();
399-
final p = arena<cvg.Mat>();
399+
final p = calloc<cvg.Mat>();
400400
cvRun(
401401
() => cvg.EstimateAffine2DWithParams(
402402
from.ref,
@@ -410,6 +410,6 @@ Mat drawChessboardCorners(
410410
p,
411411
),
412412
);
413-
return (Mat.fromCMat(p.ref), inliers!);
413+
return (Mat.fromPointer(p), inliers!);
414414
});
415415
}

lib/src/contrib/aruco.dart

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import '../core/vec.dart';
1313
import '../opencv.g.dart' as cvg;
1414

1515
class ArucoDetector extends CvStruct<cvg.ArucoDetector> {
16-
ArucoDetector._(cvg.ArucoDetectorPtr ptr) : super.fromPointer(ptr) {
17-
finalizer.attach(this, ptr.cast());
16+
ArucoDetector._(cvg.ArucoDetectorPtr ptr, [bool attach = true]) : super.fromPointer(ptr) {
17+
if (attach) {
18+
finalizer.attach(this, ptr.cast(), detach: this);
19+
}
1820
}
1921

2022
factory ArucoDetector.empty() {
@@ -31,7 +33,13 @@ class ArucoDetector extends CvStruct<cvg.ArucoDetector> {
3133

3234
@override
3335
cvg.ArucoDetector get ref => ptr.ref;
34-
static final finalizer = OcvFinalizer<cvg.ArucoDetectorPtr>(ffi.Native.addressOf(cvg.ArucoDetector_Close));
36+
static final finalizer =
37+
OcvFinalizer<cvg.ArucoDetectorPtr>(ffi.Native.addressOf(cvg.ArucoDetector_Close));
38+
39+
void dispose() {
40+
finalizer.detach(this);
41+
cvg.ArucoDetector_Close(ptr);
42+
}
3543

3644
/// DetectMarkers does basic marker detection.
3745
///
@@ -74,8 +82,11 @@ void arucoGenerateImageMarker(
7482
}
7583

7684
class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
77-
ArucoDetectorParameters._(cvg.ArucoDetectorParametersPtr ptr) : super.fromPointer(ptr) {
78-
finalizer.attach(this, ptr.cast());
85+
ArucoDetectorParameters._(cvg.ArucoDetectorParametersPtr ptr, [bool attach = true])
86+
: super.fromPointer(ptr) {
87+
if (attach) {
88+
finalizer.attach(this, ptr.cast(), detach: this);
89+
}
7990
}
8091

8192
factory ArucoDetectorParameters.empty() {
@@ -86,8 +97,13 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
8697

8798
@override
8899
cvg.ArucoDetectorParameters get ref => ptr.ref;
89-
static final finalizer =
90-
OcvFinalizer<cvg.ArucoDetectorParametersPtr>(ffi.Native.addressOf(cvg.ArucoDetectorParameters_Close));
100+
static final finalizer = OcvFinalizer<cvg.ArucoDetectorParametersPtr>(
101+
ffi.Native.addressOf(cvg.ArucoDetectorParameters_Close));
102+
103+
void dispose() {
104+
finalizer.detach(this);
105+
cvg.ArucoDetectorParameters_Close(ptr);
106+
}
91107

92108
int get adaptiveThreshWinSizeMin {
93109
return cvRunArena<int>((arena) {

lib/src/contrib/aruco_dict.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ enum PredefinedDictionaryType {
8181
}
8282

8383
class ArucoDictionary extends CvStruct<cvg.ArucoDictionary> {
84-
ArucoDictionary._(cvg.ArucoDictionaryPtr ptr) : super.fromPointer(ptr) {
85-
finalizer.attach(this, ptr.cast());
84+
ArucoDictionary._(cvg.ArucoDictionaryPtr ptr, [bool attach = true]) : super.fromPointer(ptr) {
85+
if (attach) {
86+
finalizer.attach(this, ptr.cast(), detach: this);
87+
}
8688
}
8789

8890
factory ArucoDictionary.predefined(PredefinedDictionaryType type) {
@@ -96,6 +98,11 @@ class ArucoDictionary extends CvStruct<cvg.ArucoDictionary> {
9698
static final finalizer =
9799
OcvFinalizer<cvg.ArucoDictionaryPtr>(ffi.Native.addressOf(cvg.ArucoDictionary_Close));
98100

101+
void dispose() {
102+
finalizer.detach(this);
103+
cvg.ArucoDictionary_Close(ptr);
104+
}
105+
99106
@override
100107
List<int> get props => [ptr.address];
101108
}

lib/src/contrib/img_hash.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,20 @@ const int BLOCK_MEAN_HASH_MODE_1 = 1;
7373

7474
/// BlockMeanHash is implementation of the BlockMeanHash algorithm.
7575
class BlockMeanHash extends CvStruct<cvg.BlockMeanHash> implements ImgHashBase {
76-
BlockMeanHash._(cvg.BlockMeanHashPtr ptr, [this._mode = BLOCK_MEAN_HASH_MODE_0])
76+
BlockMeanHash._(cvg.BlockMeanHashPtr ptr,
77+
[this._mode = BLOCK_MEAN_HASH_MODE_0, bool attach = true])
7778
: super.fromPointer(ptr) {
78-
finalizer.attach(this, ptr.cast());
79+
if (attach) {
80+
finalizer.attach(this, ptr.cast(), detach: this);
81+
}
7982
}
8083
static final finalizer =
81-
OcvFinalizer<ffi.Pointer<cvg.BlockMeanHash>>(ffi.Native.addressOf(cvg.BlockMeanHash_Close));
84+
OcvFinalizer<cvg.BlockMeanHashPtr>(ffi.Native.addressOf(cvg.BlockMeanHash_Close));
85+
86+
void dispose() {
87+
finalizer.detach(this);
88+
cvg.BlockMeanHash_Close(ptr);
89+
}
8290

8391
factory BlockMeanHash({int mode = BLOCK_MEAN_HASH_MODE_0}) {
8492
final p = calloc<cvg.BlockMeanHash>();

lib/src/contrib/wechat_qrcode.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import '../core/vec.dart';
1010
import '../opencv.g.dart' as cvg;
1111

1212
class WeChatQRCode extends CvStruct<cvg.WeChatQRCode> {
13-
WeChatQRCode._(super.ptr) : super.fromPointer() {
14-
finalizer.attach(this, ptr.cast());
13+
WeChatQRCode._(super.ptr, [bool attach = true]) : super.fromPointer() {
14+
if (attach) {
15+
finalizer.attach(this, ptr.cast(), detach: this);
16+
}
1517
}
1618

1719
factory WeChatQRCode.empty() {
@@ -80,6 +82,11 @@ class WeChatQRCode extends CvStruct<cvg.WeChatQRCode> {
8082
static final finalizer =
8183
OcvFinalizer<cvg.WeChatQRCodePtr>(ffi.Native.addressOf(cvg.WeChatQRCode_Close));
8284

85+
void dispose() {
86+
finalizer.detach(this);
87+
cvg.WeChatQRCode_Close(ptr);
88+
}
89+
8390
@override
8491
List<int> get props => [ptr.address];
8592

lib/src/core/array.dart

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class U8Array extends NativeArray<ffi.Uint8, int> {
3535
ptr[idx] = value;
3636
}
3737
}
38-
finalizer.attach(this, ptr.cast());
38+
finalizer.attach(this, ptr.cast(), detach: this);
3939
}
4040

4141
factory U8Array.fromList(List<int> data) {
@@ -48,11 +48,16 @@ class U8Array extends NativeArray<ffi.Uint8, int> {
4848

4949
U8Array.fromPointer(ffi.Pointer<ffi.Uint8> ptr, int length) : super(length) {
5050
this.ptr = ptr;
51-
finalizer.attach(this, ptr.cast());
51+
finalizer.attach(this, ptr.cast(), detach: this);
5252
}
5353

5454
static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);
5555

56+
void dispose() {
57+
finalizer.detach(this);
58+
calloc.free(ptr);
59+
}
60+
5661
@override
5762
void operator []=(int idx, int value) {
5863
// TODO: support negative index
@@ -79,7 +84,7 @@ class I8Array extends NativeArray<ffi.Int8, int> {
7984
ptr[idx] = value;
8085
}
8186
}
82-
finalizer.attach(this, ptr.cast());
87+
finalizer.attach(this, ptr.cast(), detach: this);
8388
}
8489

8590
factory I8Array.fromList(List<int> data) {
@@ -92,11 +97,16 @@ class I8Array extends NativeArray<ffi.Int8, int> {
9297

9398
I8Array.fromPointer(ffi.Pointer<ffi.Int8> ptr, int length) : super(length) {
9499
this.ptr = ptr;
95-
finalizer.attach(this, ptr.cast());
100+
finalizer.attach(this, ptr.cast(), detach: this);
96101
}
97102

98103
static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);
99104

105+
void dispose() {
106+
finalizer.detach(this);
107+
calloc.free(ptr);
108+
}
109+
100110
@override
101111
void operator []=(int idx, int value) {
102112
// TODO: support negative index
@@ -123,7 +133,7 @@ class U16Array extends NativeArray<ffi.Uint16, int> {
123133
ptr[idx] = value;
124134
}
125135
}
126-
finalizer.attach(this, ptr.cast());
136+
finalizer.attach(this, ptr.cast(), detach: this);
127137
}
128138

129139
factory U16Array.fromList(List<int> data) {
@@ -136,11 +146,16 @@ class U16Array extends NativeArray<ffi.Uint16, int> {
136146

137147
U16Array.fromPointer(ffi.Pointer<ffi.Uint16> ptr, int length) : super(length) {
138148
this.ptr = ptr;
139-
finalizer.attach(this, ptr.cast());
149+
finalizer.attach(this, ptr.cast(), detach: this);
140150
}
141151

142152
static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);
143153

154+
void dispose() {
155+
finalizer.detach(this);
156+
calloc.free(ptr);
157+
}
158+
144159
@override
145160
void operator []=(int idx, int value) {
146161
// TODO: support negative index
@@ -167,7 +182,7 @@ class I16Array extends NativeArray<ffi.Int16, int> {
167182
ptr[idx] = value;
168183
}
169184
}
170-
finalizer.attach(this, ptr.cast());
185+
finalizer.attach(this, ptr.cast(), detach: this);
171186
}
172187

173188
factory I16Array.fromList(List<int> data) {
@@ -180,11 +195,16 @@ class I16Array extends NativeArray<ffi.Int16, int> {
180195

181196
I16Array.fromPointer(ffi.Pointer<ffi.Int16> ptr, int length) : super(length) {
182197
this.ptr = ptr;
183-
finalizer.attach(this, ptr.cast());
198+
finalizer.attach(this, ptr.cast(), detach: this);
184199
}
185200

186201
static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);
187202

203+
void dispose() {
204+
finalizer.detach(this);
205+
calloc.free(ptr);
206+
}
207+
188208
@override
189209
void operator []=(int idx, int value) {
190210
// TODO: support negative index
@@ -211,7 +231,7 @@ class I32Array extends NativeArray<ffi.Int, int> {
211231
ptr[idx] = value;
212232
}
213233
}
214-
finalizer.attach(this, ptr.cast());
234+
finalizer.attach(this, ptr.cast(), detach: this);
215235
}
216236

217237
factory I32Array.fromList(List<int> data) {
@@ -224,11 +244,16 @@ class I32Array extends NativeArray<ffi.Int, int> {
224244

225245
I32Array.fromPointer(ffi.Pointer<ffi.Int> ptr, int length) : super(length) {
226246
this.ptr = ptr;
227-
finalizer.attach(this, ptr.cast());
247+
finalizer.attach(this, ptr.cast(), detach: this);
228248
}
229249

230250
static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);
231251

252+
void dispose() {
253+
finalizer.detach(this);
254+
calloc.free(ptr);
255+
}
256+
232257
@override
233258
void operator []=(int idx, int value) {
234259
// TODO: support negative index
@@ -255,7 +280,7 @@ class F32Array extends NativeArray<ffi.Float, double> {
255280
ptr[idx] = value;
256281
}
257282
}
258-
finalizer.attach(this, ptr.cast());
283+
finalizer.attach(this, ptr.cast(), detach: this);
259284
}
260285

261286
factory F32Array.fromList(List<double> data) {
@@ -268,11 +293,16 @@ class F32Array extends NativeArray<ffi.Float, double> {
268293

269294
F32Array.fromPointer(ffi.Pointer<ffi.Float> ptr, int length) : super(length) {
270295
this.ptr = ptr;
271-
finalizer.attach(this, ptr.cast());
296+
finalizer.attach(this, ptr.cast(), detach: this);
272297
}
273298

274299
static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);
275300

301+
void dispose() {
302+
finalizer.detach(this);
303+
calloc.free(ptr);
304+
}
305+
276306
@override
277307
void operator []=(int idx, double value) {
278308
// TODO: support negative index
@@ -299,7 +329,7 @@ class F64Array extends NativeArray<ffi.Double, double> {
299329
ptr[idx] = value;
300330
}
301331
}
302-
finalizer.attach(this, ptr.cast());
332+
finalizer.attach(this, ptr.cast(), detach: this);
303333
}
304334

305335
factory F64Array.fromList(List<double> data) {
@@ -312,11 +342,16 @@ class F64Array extends NativeArray<ffi.Double, double> {
312342

313343
F64Array.fromPointer(ffi.Pointer<ffi.Double> ptr, int length) : super(length) {
314344
this.ptr = ptr;
315-
finalizer.attach(this, ptr.cast());
345+
finalizer.attach(this, ptr.cast(), detach: this);
316346
}
317347

318348
static final finalizer = ffi.NativeFinalizer(calloc.nativeFree);
319349

350+
void dispose() {
351+
finalizer.detach(this);
352+
calloc.free(ptr);
353+
}
354+
320355
@override
321356
void operator []=(int idx, double value) {
322357
// TODO: support negative index

0 commit comments

Comments
 (0)