Skip to content

Commit

Permalink
change string convension to using arena
Browse files Browse the repository at this point in the history
  • Loading branch information
rainyl committed Mar 1, 2024
1 parent bfa076b commit 144bec6
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 176 deletions.
9 changes: 3 additions & 6 deletions lib/src/core/scalar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ class Scalar extends CvObject<cvg.Scalar> with EquatableMixin {
..ref.val4 = val4;
return Scalar._(_ptr);
}
factory Scalar.fromNative(cvg.Scalar s) =>
Scalar(s.val1, s.val2, s.val3, s.val4);
factory Scalar.fromNative(cvg.Scalar s) => Scalar(s.val1, s.val2, s.val3, s.val4);
factory Scalar.all(double val) => Scalar(val, val, val, val);
factory Scalar.default_() => Scalar(
double.maxFinite, double.maxFinite, double.maxFinite, double.maxFinite);
factory Scalar.default_() => Scalar(double.maxFinite, double.maxFinite, double.maxFinite, double.maxFinite);
factory Scalar.fromRgb(int r, int g, int b) {
return Scalar(b.toDouble(), g.toDouble(), r.toDouble(), 0);
}

static final _finalizer =
Finalizer<ffi.Pointer<cvg.Scalar>>((p0) => calloc.free(p0));
static final _finalizer = Finalizer<ffi.Pointer<cvg.Scalar>>((p0) => calloc.free(p0));
double get val1 => _ptr.ref.val1;
double get val2 => _ptr.ref.val2;
double get val3 => _ptr.ref.val3;
Expand Down
138 changes: 73 additions & 65 deletions lib/src/highgui/highgui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,48 @@ class Window {
/// For further details, please see:
/// http://docs.opencv.org/master/d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b
Window(this.name) : isOpen = true {
final cname = name.toNativeUtf8();
_bindings.Window_New(cname.cast(), 0);
calloc.free(cname);
using((arena) {
_bindings.Window_New(name.toNativeUtf8(allocator: arena).cast(), 0);
});
}

void close() {
final cname = name.toNativeUtf8();
_bindings.Window_Close(cname.cast());
calloc.free(cname);
isOpen = false;
using((arena) {
_bindings.Window_Close(name.toNativeUtf8(allocator: arena).cast());
isOpen = false;
});
}

/// [getWindowProperty] returns properties of a window.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#gaaf9504b8f9cf19024d9d44a14e461656
double getWindowProperty(WindowPropertyFlags flag) {
final cname = name.toNativeUtf8();
final result = _bindings.Window_GetProperty(cname.cast(), flag.value);
calloc.free(cname);
return result;
return using<double>((arena) {
final result = _bindings.Window_GetProperty(name.toNativeUtf8(allocator: arena).cast(), flag.value);
return result;
});
}

/// [setWindowProperty] changes parameters of a window dynamically.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga66e4a6db4d4e06148bcdfe0d70a5df27
void setWindowProperty(WindowPropertyFlags flag, double value) {
final cname = name.toNativeUtf8();
_bindings.Window_SetProperty(cname.cast(), flag.value, value);
calloc.free(cname);
using((arena) {
_bindings.Window_SetProperty(name.toNativeUtf8(allocator: arena).cast(), flag.value, value);
});
}

/// SetWindowTitle updates window title.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga56f8849295fd10d0c319724ddb773d96
void setWindowTitle(String title) {
final cname = name.toNativeUtf8();
final ctitle = title.toNativeUtf8();
_bindings.Window_SetTitle(cname.cast(), ctitle.cast());
calloc.free(cname);
calloc.free(ctitle);
using((arena) {
_bindings.Window_SetTitle(
name.toNativeUtf8(allocator: arena).cast(), title.toNativeUtf8(allocator: arena).cast());
});
}

/// IMShow displays an image Mat in the specified window.
Expand All @@ -75,9 +74,9 @@ class Window {
/// For further details, please see:
/// http://docs.opencv.org/master/d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563
void imshow(Mat img) {
final cname = name.toNativeUtf8();
_bindings.Window_IMShow(cname.cast(), img.ptr);
calloc.free(cname);
using((arena) {
_bindings.Window_IMShow(name.toNativeUtf8(allocator: arena).cast(), img.ptr);
});
}

/// WaitKey waits for a pressed key.
Expand All @@ -94,19 +93,19 @@ class Window {
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga8d86b207f7211250dbe6e28f76307ffb
void moveWindow(int x, int y) {
final cname = name.toNativeUtf8();
_bindings.Window_Move(cname.cast(), x, y);
calloc.free(cname);
using((arena) {
_bindings.Window_Move(name.toNativeUtf8(allocator: arena).cast(), x, y);
});
}

/// ResizeWindow resizes window to the specified size.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga9e80e080f7ef33f897e415358aee7f7e
void resizeWindow(int width, int height) {
final cname = name.toNativeUtf8();
_bindings.Window_Resize(cname.cast(), width, height);
calloc.free(cname);
using((arena) {
_bindings.Window_Resize(name.toNativeUtf8(allocator: arena).cast(), width, height);
});
}

/// SelectROI selects a Region Of Interest (ROI) on the given image.
Expand All @@ -119,10 +118,10 @@ class Window {
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga8daf4730d3adf7035b6de9be4c469af5
Rect selectROI(Mat img) {
final cname = name.toNativeUtf8();
final result = _bindings.Window_SelectROI(cname.cast(), img.ptr);
calloc.free(cname);
return Rect.fromNative(result);
return using<Rect>((arena) {
final result = _bindings.Window_SelectROI(name.toNativeUtf8(allocator: arena).cast(), img.ptr);
return Rect.fromNative(result);
});
}

/// SelectROIs selects multiple Regions Of Interest (ROI) on the given image.
Expand All @@ -135,10 +134,10 @@ class Window {
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga0f11fad74a6432b8055fb21621a0f893
List<Rect> selectROIs(Mat img) {
final cname = name.toNativeUtf8();
final result = _bindings.Window_SelectROIs(cname.cast(), img.ptr);
calloc.free(cname);
return Rects.toList(result);
return using<List<Rect>>((arena) {
final result = _bindings.Window_SelectROIs(name.toNativeUtf8(allocator: arena).cast(), img.ptr);
return Rects.toList(result);
});
}

String name;
Expand All @@ -151,63 +150,72 @@ int waitKey(int delay) => _bindings.Window_WaitKey(delay);

class Trackbar {
Trackbar(this.name, this.parent, this.max, {int? value}) {
final tname = name.toNativeUtf8();
final pname = parent.name.toNativeUtf8();
_bindings.Trackbar_Create(pname.cast(), tname.cast(), max);
if (value != null) {
pos = value;
}
calloc.free(tname);
calloc.free(pname);
using((arena) {
_bindings.Trackbar_Create(
parent.name.toNativeUtf8(allocator: arena).cast(),
name.toNativeUtf8(allocator: arena).cast(),
max,
);
if (value != null) {
pos = value;
}
});
}

/// pos returns the trackbar position.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga122632e9e91b9ec06943472c55d9cda8
int get pos {
final pname = parent.name.toNativeUtf8();
final tname = name.toNativeUtf8();
final result = _bindings.Trackbar_GetPos(pname.cast(), tname.cast());
calloc.free(pname);
calloc.free(tname);
return result;
return using<int>((arena) {
final result = _bindings.Trackbar_GetPos(
parent.name.toNativeUtf8(allocator: arena).cast(),
name.toNativeUtf8(allocator: arena).cast(),
);
return result;
});
}

/// pos sets the trackbar position.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d
void set pos(int pos) {
final pname = parent.name.toNativeUtf8();
final tname = name.toNativeUtf8();
_bindings.Trackbar_SetPos(pname.cast(), tname.cast(), pos);
calloc.free(pname);
calloc.free(tname);
using((arena) {
_bindings.Trackbar_SetPos(
parent.name.toNativeUtf8(allocator: arena).cast(),
name.toNativeUtf8(allocator: arena).cast(),
pos,
);
});
}

/// SetMin sets the trackbar minimum position.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#gabe26ffe8d2b60cc678895595a581b7aa
set minPos(int pos) {
final pname = parent.name.toNativeUtf8();
final tname = name.toNativeUtf8();
_bindings.Trackbar_SetMin(pname.cast(), tname.cast(), pos);
calloc.free(pname);
calloc.free(tname);
using((arena) {
_bindings.Trackbar_SetMin(
parent.name.toNativeUtf8(allocator: arena).cast(),
name.toNativeUtf8(allocator: arena).cast(),
pos,
);
});
}

/// SetMax sets the trackbar maximum position.
///
/// For further details, please see:
/// https://docs.opencv.org/master/d7/dfc/group__highgui.html#ga7e5437ccba37f1154b65210902fc4480
set maxPos(int pos) {
final pname = parent.name.toNativeUtf8();
final tname = name.toNativeUtf8();
_bindings.Trackbar_SetMax(pname.cast(), tname.cast(), pos);
calloc.free(pname);
calloc.free(tname);
using((arena) {
_bindings.Trackbar_SetMax(
parent.name.toNativeUtf8(allocator: arena).cast(),
name.toNativeUtf8(allocator: arena).cast(),
pos,
);
});
}

String name;
Expand Down
58 changes: 28 additions & 30 deletions lib/src/imgcodecs/imgcodecs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,31 @@ final _bindings = cvg.CvNative(loadNativeLibrary());
/// For further details, please see:
/// http://docs.opencv.org/master/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56
Mat imread(String filename, {int flags = IMREAD_COLOR}) {
final name_ptr = filename.toNativeUtf8();
final mat = _bindings.Image_IMRead(name_ptr.cast(), flags);
calloc.free(name_ptr);
return Mat.fromCMat(mat);
return using<Mat>((arena) {
final mat = _bindings.Image_IMRead(filename.toNativeUtf8(allocator: arena).cast(), flags);
return Mat.fromCMat(mat);
});
}

/// IMWrite writes a Mat to an image file.
///
/// For further details, please see:
/// http://docs.opencv.org/master/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce
bool imwrite(String filename, InputArray img, {List<int>? params}) {
bool isSuccess = false;
if (params == null) {
isSuccess = _bindings.Image_IMWrite(filename.toNativeUtf8().cast(), img.ptr);
} else {
using((arena) {
final fname = filename.toNativeUtf8();
return using<bool>((arena) {
bool isSuccess = false;
if (params == null) {
isSuccess = _bindings.Image_IMWrite(filename.toNativeUtf8(allocator: arena).cast(), img.ptr);
} else {
final fname = filename.toNativeUtf8(allocator: arena);
isSuccess = _bindings.Image_IMWrite_WithParams(
fname.cast(),
img.ptr,
params.toNativeVector(arena).ref,
);
calloc.free(fname);
});
}
return isSuccess;
}
return isSuccess;
});
}

/// IMEncode encodes an image Mat into a memory buffer.
Expand All @@ -60,32 +59,31 @@ Uint8List imencode(
InputArray img, {
List<int>? params,
}) {
final buffer = _bindings.UCharVector_New();
return using<Uint8List>((arena) {
final buffer = _bindings.UCharVector_New();

if (params == null) {
_bindings.Image_IMEncode(ext.toNativeUtf8().cast(), img.ptr, buffer);
} else {
using((arena) {
final ptr = ext.toNativeUtf8();
if (params == null) {
_bindings.Image_IMEncode(ext.toNativeUtf8(allocator: arena).cast(), img.ptr, buffer);
} else {
final ptr = ext.toNativeUtf8(allocator: arena);
_bindings.Image_IMEncode_WithParams(
ptr.cast(),
img.ptr,
params.toNativeVector(arena).ref,
buffer,
);
calloc.free(ptr);
});
}
}

final length = _bindings.UCharVector_Size(buffer);
final length = _bindings.UCharVector_Size(buffer);

final buf = Uint8List(length);
for (var i = 0; i < length; i++) {
buf[i] = _bindings.UCharVector_At(buffer, i);
}
_bindings.UCharVector_Free(buffer);
final buf = Uint8List(length);
for (var i = 0; i < length; i++) {
buf[i] = _bindings.UCharVector_At(buffer, i);
}
_bindings.UCharVector_Free(buffer);

return buf;
return buf;
});
}

/// imdecode reads an image from a buffer in memory.
Expand Down
Loading

0 comments on commit 144bec6

Please sign in to comment.