Skip to content

Commit 73e6ace

Browse files
authored
Merge pull request #69 from rainyl/issue68
better exception for cv.imencode
2 parents 822c0f6 + 89bf7a4 commit 73e6ace

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

lib/src/imgcodecs/imgcodecs.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:ffi/ffi.dart';
88
import '../core/base.dart';
99
import '../core/mat.dart';
1010
import '../core/vec.dart';
11+
import '../core/exception.dart';
12+
import '../core/error_code.dart';
1113
import '../constants.g.dart';
1214
import '../opencv.g.dart' as cvg;
1315

@@ -61,17 +63,18 @@ Uint8List imencode(
6163
InputArray img, {
6264
VecInt? params,
6365
}) {
64-
return using<Uint8List>((arena) {
65-
final buffer = calloc<cvg.VecUChar>();
66-
final cExt = ext.toNativeUtf8(allocator: arena);
66+
final buffer = calloc<cvg.VecUChar>();
67+
final success = calloc<ffi.Bool>();
68+
final cExt = ext.toNativeUtf8().cast<ffi.Char>();
6769

68-
if (params == null) {
69-
CFFI.Image_IMEncode(cExt.cast(), img.ref, buffer);
70-
} else {
71-
CFFI.Image_IMEncode_WithParams(cExt.cast(), img.ref, params.ref, buffer);
72-
}
73-
return VecUChar.fromPointer(buffer).toU8List();
74-
});
70+
params == null
71+
? cvRun(() => CFFI.Image_IMEncode(cExt, img.ref, success, buffer))
72+
: cvRun(() => CFFI.Image_IMEncode_WithParams(cExt, img.ref, params.ref, success, buffer));
73+
if (!success.value) {
74+
throw CvException(ErrorCode.StsError.code, msg: "imencode failed, check your params");
75+
}
76+
calloc.free(cExt);
77+
return VecUChar.fromPointer(buffer).toU8List();
7578
}
7679

7780
/// imdecode reads an image from a buffer in memory.

lib/src/opencv.g.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4892,44 +4892,53 @@ class CvNative {
48924892
CvStatus Image_IMEncode(
48934893
ffi.Pointer<ffi.Char> fileExt,
48944894
Mat img,
4895+
ffi.Pointer<ffi.Bool> success,
48954896
ffi.Pointer<VecUChar> rval,
48964897
) {
48974898
return _Image_IMEncode(
48984899
fileExt,
48994900
img,
4901+
success,
49004902
rval,
49014903
);
49024904
}
49034905

49044906
late final _Image_IMEncodePtr = _lookup<
49054907
ffi.NativeFunction<
4906-
CvStatus Function(ffi.Pointer<ffi.Char>, Mat,
4908+
CvStatus Function(ffi.Pointer<ffi.Char>, Mat, ffi.Pointer<ffi.Bool>,
49074909
ffi.Pointer<VecUChar>)>>('Image_IMEncode');
49084910
late final _Image_IMEncode = _Image_IMEncodePtr.asFunction<
4909-
CvStatus Function(ffi.Pointer<ffi.Char>, Mat, ffi.Pointer<VecUChar>)>();
4911+
CvStatus Function(ffi.Pointer<ffi.Char>, Mat, ffi.Pointer<ffi.Bool>,
4912+
ffi.Pointer<VecUChar>)>();
49104913

49114914
CvStatus Image_IMEncode_WithParams(
49124915
ffi.Pointer<ffi.Char> fileExt,
49134916
Mat img,
49144917
VecInt params,
4918+
ffi.Pointer<ffi.Bool> success,
49154919
ffi.Pointer<VecUChar> rval,
49164920
) {
49174921
return _Image_IMEncode_WithParams(
49184922
fileExt,
49194923
img,
49204924
params,
4925+
success,
49214926
rval,
49224927
);
49234928
}
49244929

49254930
late final _Image_IMEncode_WithParamsPtr = _lookup<
49264931
ffi.NativeFunction<
4927-
CvStatus Function(ffi.Pointer<ffi.Char>, Mat, VecInt,
4932+
CvStatus Function(
4933+
ffi.Pointer<ffi.Char>,
4934+
Mat,
4935+
VecInt,
4936+
ffi.Pointer<ffi.Bool>,
49284937
ffi.Pointer<VecUChar>)>>('Image_IMEncode_WithParams');
49294938
late final _Image_IMEncode_WithParams =
49304939
_Image_IMEncode_WithParamsPtr.asFunction<
4931-
CvStatus Function(
4932-
ffi.Pointer<ffi.Char>, Mat, VecInt, ffi.Pointer<VecUChar>)>();
4940+
CvStatus Function(ffi.Pointer<ffi.Char>, Mat, VecInt,
4941+
ffi.Pointer<ffi.Bool>, ffi.Pointer<VecUChar>)>();
49334942

49344943
CvStatus Image_IMRead(
49354944
ffi.Pointer<ffi.Char> filename,

lib/src/video/video.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,9 @@ class KalmanFilter extends CvStruct<cvg.KalmanFilter> {
337337

338338
Mat predict({Mat? control}) {
339339
final p = calloc<cvg.Mat>();
340-
if (control == null) {
341-
cvRun(() => CFFI.KalmanFilter_Predict(ref, p));
342-
} else {
343-
CFFI.KalmanFilter_PredictWithParams(ref, control.ref, p);
344-
}
340+
control == null
341+
? cvRun(() => CFFI.KalmanFilter_Predict(ref, p))
342+
: cvRun(() => CFFI.KalmanFilter_PredictWithParams(ref, control.ref, p));
345343
return Mat.fromPointer(p);
346344
}
347345

src/imgcodecs/imgcodecs.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ CvStatus Image_IMWrite_WithParams(const char *filename, Mat img, VecInt params,
2727
*rval = cv::imwrite(filename, *img.ptr, *params.ptr);
2828
END_WRAP
2929
}
30-
CvStatus Image_IMEncode(const char *fileExt, Mat img, VecUChar *rval)
30+
CvStatus Image_IMEncode(const char *fileExt, Mat img, bool *success, VecUChar *rval)
3131
{
3232
BEGIN_WRAP
3333
auto buf = new std::vector<uchar>();
34-
cv::imencode(fileExt, *img.ptr, *buf);
34+
*success = cv::imencode(fileExt, *img.ptr, *buf);
3535
*rval = {buf};
3636
END_WRAP
3737
}
38-
CvStatus Image_IMEncode_WithParams(const char *fileExt, Mat img, VecInt params, VecUChar *rval)
38+
CvStatus Image_IMEncode_WithParams(const char *fileExt, Mat img, VecInt params, bool *success, VecUChar *rval)
3939
{
4040
BEGIN_WRAP
4141
auto buf = new std::vector<uchar>();
42-
cv::imencode(fileExt, *img.ptr, *buf, *params.ptr);
42+
*success = cv::imencode(fileExt, *img.ptr, *buf, *params.ptr);
4343
*rval = {buf};
4444
END_WRAP
4545
}

src/imgcodecs/imgcodecs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ extern "C" {
2121
CvStatus Image_IMRead(const char *filename, int flags, CVD_OUT Mat *rval);
2222
CvStatus Image_IMWrite(const char *filename, Mat img, CVD_OUT bool *rval);
2323
CvStatus Image_IMWrite_WithParams(const char *filename, Mat img, VecInt params, CVD_OUT bool *rval);
24-
CvStatus Image_IMEncode(const char *fileExt, Mat img, CVD_OUT VecUChar *rval);
25-
CvStatus Image_IMEncode_WithParams(const char *fileExt, Mat img, VecInt params, CVD_OUT VecUChar *rval);
24+
CvStatus Image_IMEncode(const char *fileExt, Mat img, CVD_OUT bool *success, CVD_OUT VecUChar *rval);
25+
CvStatus Image_IMEncode_WithParams(const char *fileExt, Mat img, VecInt params, CVD_OUT bool *success,
26+
CVD_OUT VecUChar *rval);
2627
CvStatus Image_IMDecode(VecUChar buf, int flags, CVD_OUT Mat *rval);
2728

2829
#ifdef __cplusplus

0 commit comments

Comments
 (0)