Skip to content

Commit e19bc72

Browse files
authored
Merge pull request #71 from rainyl/optimize-vector
Optimize vector returns
2 parents e7c6682 + c6a035e commit e19bc72

File tree

19 files changed

+250
-207
lines changed

19 files changed

+250
-207
lines changed

lib/src/core/core.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ Mat insertChannel(InputArray src, InputOutputArray dst, int coi) {
699699
final rval = cvRunArena<double>((arena) {
700700
final p = arena<ffi.Double>();
701701
cvRun(
702-
() => CFFI.KMeansPoints(pts.ref, K, bestLabels.ref, criteria.toNativePtr(arena).ref,
703-
attempts, flags, centers!.ref, p),
702+
() => CFFI.KMeansPoints(pts.ref, K, bestLabels.ref, criteria.toNativePtr(arena).ref, attempts,
703+
flags, centers!.ref, p),
704704
);
705705
return p.value;
706706
});
@@ -829,12 +829,7 @@ Mat min(InputArray src1, InputArray src2, {OutputArray? dst}) {
829829
final minLocP = calloc<cvg.Point>();
830830
final maxLocP = calloc<cvg.Point>();
831831
cvRun(() => CFFI.Mat_MinMaxLoc(src.ref, minValP, maxValP, minLocP, maxLocP));
832-
return (
833-
minValP.value,
834-
maxValP.value,
835-
Point.fromPointer(minLocP),
836-
Point.fromPointer(maxLocP)
837-
);
832+
return (minValP.value, maxValP.value, Point.fromPointer(minLocP), Point.fromPointer(maxLocP));
838833
});
839834
}
840835

@@ -1081,10 +1076,10 @@ Mat sortIdx(InputArray src, int flags, {OutputArray? dst}) {
10811076
/// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga0547c7fed86152d7e9d0096029c8518a
10821077
VecMat split(InputArray m) {
10831078
final p = calloc<cvg.VecMat>();
1084-
final vec = VecMat.fromList([]);
1085-
cvRun(() => CFFI.Mat_Split(m.ref, vec.ptr));
1079+
final vec = calloc<cvg.VecMat>();
1080+
cvRun(() => CFFI.Mat_Split(m.ref, vec));
10861081
calloc.free(p);
1087-
return vec;
1082+
return VecMat.fromPointer(vec);
10881083
}
10891084

10901085
/// Subtract calculates the per-element subtraction of two arrays or an array and a scalar.

lib/src/core/exception.dart

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
// ignore_for_file: avoid_print
2-
3-
import 'dart:ffi' as ffi;
4-
import 'dart:io';
5-
6-
import 'package:ffi/ffi.dart';
7-
8-
import 'base.dart';
92
import 'error_code.dart';
10-
import '../opencv.g.dart' as cvg;
113

124
class CvException implements Exception {
135
CvException(
@@ -29,35 +21,35 @@ class CvException implements Exception {
2921
}
3022
}
3123

32-
void defaultCvErrorCallback(
33-
int code,
34-
ffi.Pointer<ffi.Char> funcName,
35-
ffi.Pointer<ffi.Char> errMsg,
36-
ffi.Pointer<ffi.Char> fileName,
37-
int line,
38-
ffi.Pointer<ffi.Void> userdata,
39-
) {
40-
final err = errMsg.cast<Utf8>();
41-
final func = funcName.cast<Utf8>();
42-
final file = fileName.cast<Utf8>();
43-
final msg = "OpenCV Native Error Occurred ("
44-
"code: $code, "
45-
"err: ${err.toDartString()}) "
46-
"func: ${func.toDartString()} "
47-
"file: ${file.toDartString()}:$line";
48-
print(msg);
49-
calloc.free(err);
50-
calloc.free(func);
51-
calloc.free(file);
52-
exit(code);
53-
}
54-
55-
void registerErrorCallback({cvg.DartErrorCallbackFunction? callback}) {
56-
callback ??= defaultCvErrorCallback;
57-
// final fp = ffi.NativeCallable<cvg.ErrorCallbackFunction>.listener(callback);
58-
final fp = ffi.NativeCallable<cvg.ErrorCallbackFunction>.isolateLocal(callback);
59-
CFFI.registerErrorCallback(fp.nativeFunction);
60-
}
24+
// void defaultCvErrorCallback(
25+
// int code,
26+
// ffi.Pointer<ffi.Char> funcName,
27+
// ffi.Pointer<ffi.Char> errMsg,
28+
// ffi.Pointer<ffi.Char> fileName,
29+
// int line,
30+
// ffi.Pointer<ffi.Void> userdata,
31+
// ) {
32+
// final err = errMsg.cast<Utf8>();
33+
// final func = funcName.cast<Utf8>();
34+
// final file = fileName.cast<Utf8>();
35+
// final msg = "OpenCV Native Error Occurred ("
36+
// "code: $code, "
37+
// "err: ${err.toDartString()}) "
38+
// "func: ${func.toDartString()} "
39+
// "file: ${file.toDartString()}:$line";
40+
// print(msg);
41+
// calloc.free(err);
42+
// calloc.free(func);
43+
// calloc.free(file);
44+
// exit(code);
45+
// }
46+
47+
// void registerErrorCallback({cvg.DartErrorCallbackFunction? callback}) {
48+
// callback ??= defaultCvErrorCallback;
49+
// // final fp = ffi.NativeCallable<cvg.ErrorCallbackFunction>.listener(callback);
50+
// final fp = ffi.NativeCallable<cvg.ErrorCallbackFunction>.isolateLocal(callback);
51+
// CFFI.registerErrorCallback(fp.nativeFunction);
52+
// }
6153

6254
class CvdException implements Exception {
6355
final String message;

lib/src/dnn/dnn.dart

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,6 @@ class Net extends CvStruct<cvg.Net> {
130130
return using<Net>((arena) {
131131
bufferConfig ??= Uint8List(0);
132132
final cFramework = framework.toNativeUtf8(allocator: arena).cast<ffi.Char>();
133-
// final bufM = arena<cvg.VecUChar>();
134-
// cvRun(() => CFFI.VecUChar_New(bufM));
135-
// for (var e in bufferModel) {
136-
// cvRun(() => CFFI.VecUChar_Append(bufM.value, e));
137-
// }
138-
// final bufC = arena<cvg.VecUChar>();
139-
// cvRun(() => CFFI.VecUChar_New(bufC));
140-
// for (var e in bufferConfig!) {
141-
// cvRun(() => CFFI.VecUChar_Append(bufC.value, e));
142-
// }
143133
final bufM = VecUChar.fromList(bufferModel);
144134
final bufC = VecUChar.fromList(bufferConfig!);
145135
final p = calloc<cvg.Net>();
@@ -283,9 +273,11 @@ class Net extends CvStruct<cvg.Net> {
283273
}
284274

285275
String dump() {
286-
final p = VecChar();
287-
cvRun(() => CFFI.Net_Dump(ref, p.ref));
288-
return p.asString();
276+
final p = calloc<ffi.Pointer<ffi.Char>>();
277+
cvRun(() => CFFI.Net_Dump(ref, p));
278+
final ret = p.value.cast<Utf8>().toDartString();
279+
calloc.free(p);
280+
return ret;
289281
}
290282

291283
/// SetInput sets the new value for the layer output blob.
@@ -400,15 +392,12 @@ class Net extends CvStruct<cvg.Net> {
400392

401393
/// Returns input scale and zeropoint for a quantized Net.
402394
/// https://docs.opencv.org/4.x/db/d30/classcv_1_1dnn_1_1Net.html#af82a1c7e7de19712370a34667056102d
403-
(List<double>, List<int>) getInputDetails() {
404-
return using<(List<double>, List<int>)>((arena) {
395+
(VecFloat, VecInt) getInputDetails() {
396+
return using<(VecFloat, VecInt)>((arena) {
405397
final sc = calloc<cvg.VecFloat>();
406398
final zp = calloc<cvg.VecInt>();
407399
cvRun(() => CFFI.Net_GetInputDetails(ref, sc, zp));
408-
return (
409-
VecFloat.fromPointer(sc).toList(),
410-
VecInt.fromPointer(zp).toList(),
411-
);
400+
return (VecFloat.fromPointer(sc), VecInt.fromPointer(zp));
412401
});
413402
}
414403

lib/src/imgproc/imgproc.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -703,16 +703,16 @@ VecPoint2f goodFeaturesToTrack(
703703
bool useHarrisDetector = false,
704704
double k = 0.04,
705705
}) {
706-
corners ??= VecPoint2f();
706+
final c = corners?.ptr ?? calloc<cvg.VecPoint2f>();
707707
mask ??= Mat.empty();
708708
if (gradientSize == null) {
709-
cvRun(() => CFFI.GoodFeaturesToTrack(image.ref, corners!.ref, maxCorners, qualityLevel,
710-
minDistance, mask!.ref, blockSize, useHarrisDetector, k));
709+
cvRun(() => CFFI.GoodFeaturesToTrack(image.ref, c, maxCorners, qualityLevel, minDistance,
710+
mask!.ref, blockSize, useHarrisDetector, k));
711711
} else {
712-
cvRun(() => CFFI.GoodFeaturesToTrackWithGradient(image.ref, corners!.ref, maxCorners,
713-
qualityLevel, minDistance, mask!.ref, blockSize, gradientSize, useHarrisDetector, k));
712+
cvRun(() => CFFI.GoodFeaturesToTrackWithGradient(image.ref, c, maxCorners, qualityLevel,
713+
minDistance, mask!.ref, blockSize, gradientSize, useHarrisDetector, k));
714714
}
715-
return corners;
715+
return corners ?? VecPoint2f.fromPointer(c);
716716
}
717717

718718
/// Grabcut runs the GrabCut algorithm.

lib/src/imgproc/subdiv2d.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ class Subdiv2D extends CvStruct<cvg.Subdiv2D> {
120120
/// https://docs.opencv.org/4.x/df/dbf/classcv_1_1Subdiv2D.html#a2d02a1d66ef7f8f267beb549cb2823f1
121121
VecInt getLeadingEdgeList() {
122122
return using<VecInt>((arena) {
123-
final pv = VecInt();
124-
cvRun(() => CFFI.Subdiv2D_GetLeadingEdgeList(ref, pv.ptr));
125-
return pv;
123+
final pv = calloc<cvg.VecInt>();
124+
cvRun(() => CFFI.Subdiv2D_GetLeadingEdgeList(ref, pv));
125+
return VecInt.fromPointer(pv);
126126
});
127127
}
128128

@@ -161,10 +161,10 @@ class Subdiv2D extends CvStruct<cvg.Subdiv2D> {
161161
/// https://docs.opencv.org/4.x/df/dbf/classcv_1_1Subdiv2D.html#a3a9e080423475be056a79da4c04741ea
162162
(VecVecPoint2f facetList, VecPoint2f facetCenters) getVoronoiFacetList(VecInt idx) {
163163
return using<(VecVecPoint2f, VecPoint2f)>((arena) {
164-
final pf = VecVecPoint2f.fromList([]);
165-
final pfc = VecPoint2f();
166-
cvRun(() => CFFI.Subdiv2D_GetVoronoiFacetList(ref, idx.ref, pf.ptr, pfc.ptr));
167-
return (pf, pfc);
164+
final pf = calloc<cvg.VecVecPoint2f>();
165+
final pfc = calloc<cvg.VecPoint2f>();
166+
cvRun(() => CFFI.Subdiv2D_GetVoronoiFacetList(ref, idx.ref, pf, pfc));
167+
return (VecVecPoint2f.fromPointer(pf), VecPoint2f.fromPointer(pfc));
168168
});
169169
}
170170

lib/src/objdetect/objdetect.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,10 @@ class QRCodeDetector extends CvStruct<cvg.QRCodeDetector> {
497497
}) {
498498
final code = straightCode?.ptr ?? calloc<cvg.Mat>();
499499
final points = calloc<cvg.VecPoint>();
500-
final v = calloc<cvg.VecChar>();
500+
final v = calloc<ffi.Pointer<ffi.Char>>();
501501
cvRun(() => CFFI.QRCodeDetector_DetectAndDecode(ref, img.ref, points, code, v));
502-
final s = v == ffi.nullptr ? "" : VecChar.fromPointer(v).toString();
502+
final s = v == ffi.nullptr ? "" : v.value.cast<Utf8>().toDartString();
503+
calloc.free(v);
503504
return (s, VecPoint.fromPointer(points), Mat.fromPointer(code));
504505
}
505506

@@ -525,14 +526,13 @@ class QRCodeDetector extends CvStruct<cvg.QRCodeDetector> {
525526
VecPoint? points,
526527
Mat? straightCode,
527528
}) {
528-
return cvRunArena<(String, VecPoint?, Mat?)>((arena) {
529-
points ??= VecPoint();
530-
final ret = VecChar();
531-
straightCode ??= Mat.empty();
532-
cvRun(
533-
() => CFFI.QRCodeDetector_Decode(ref, img.ref, points!.ref, straightCode!.ref, ret.ptr));
534-
return (ret.toString(), points, straightCode!);
535-
});
529+
final p = points?.ptr ?? calloc<cvg.VecPoint>();
530+
final ret = calloc<ffi.Pointer<ffi.Char>>();
531+
straightCode ??= Mat.empty();
532+
cvRun(() => CFFI.QRCodeDetector_Decode(ref, img.ref, p, straightCode!.ref, ret));
533+
final info = ret.value.cast<Utf8>().toDartString();
534+
calloc.free(ret);
535+
return (info, VecPoint.fromPointer(p), straightCode);
536536
}
537537

538538
/// Detects QR codes in image and finds of the quadrangles containing the codes.
@@ -542,12 +542,12 @@ class QRCodeDetector extends CvStruct<cvg.QRCodeDetector> {
542542
/// For usage please see TestQRCodeDetector
543543
/// For further details, please see:
544544
/// https://docs.opencv.org/master/de/dc3/classcv_1_1QRCodeDetector.html#aaf2b6b2115b8e8fbc9acf3a8f68872b6
545-
(bool, VecPoint? points) detectMulti(InputArray img, {VecPoint? points}) {
546-
return cvRunArena<(bool, VecPoint?)>((arena) {
547-
points ??= VecPoint.fromList([]);
545+
(bool, VecPoint points) detectMulti(InputArray img, {VecPoint? points}) {
546+
return cvRunArena<(bool, VecPoint)>((arena) {
547+
final p = points?.ptr ?? calloc<cvg.VecPoint>();
548548
final ret = arena<ffi.Bool>();
549-
cvRun(() => CFFI.QRCodeDetector_DetectMulti(ref, img.ref, points!.ref, ret));
550-
return ret.value ? (ret.value, VecPoint.fromVec(points!.ref)) : (ret.value, null);
549+
cvRun(() => CFFI.QRCodeDetector_DetectMulti(ref, img.ref, p, ret));
550+
return (ret.value, VecPoint.fromPointer(p));
551551
});
552552
}
553553

0 commit comments

Comments
 (0)