forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmat_safe.cpp
More file actions
68 lines (54 loc) · 1.56 KB
/
Copy pathmat_safe.cpp
File metadata and controls
68 lines (54 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "mat_safe.h"
#include <iostream>
#include "opencv2/core/core_c.h"
#include "opencv2/core/mat.hpp"
namespace {
using namespace cv;
class GoMatAllocator : public MatAllocator {
public:
GoMatAllocator() = default;
UMatData *allocate(int dims, const int *sizes, int type, void *data0,
size_t *step, AccessFlag /*flags*/,
UMatUsageFlags /*usageFlags*/) const CV_OVERRIDE {
size_t total = CV_ELEM_SIZE(type);
for (int i = dims - 1; i >= 0; i--) {
if (step) {
if (data0 && step[i] != CV_AUTOSTEP) {
CV_Assert(total <= step[i]);
total = step[i];
} else {
step[i] = total;
}
}
total *= sizes[i];
}
uchar *data = data0 ? (uchar *)data0 : (uchar *)goMatAllocate(total);
UMatData *u = new UMatData(this);
u->data = u->origdata = data;
u->size = total;
if (data0) {
u->flags |= UMatData::USER_ALLOCATED;
}
return u;
}
bool allocate(UMatData *u, AccessFlag /*accessFlags*/,
UMatUsageFlags /*usageFlags*/) const CV_OVERRIDE {
return u != nullptr;
}
void deallocate(UMatData *u) const CV_OVERRIDE {
if (u) {
CV_Assert(u->urefcount == 0);
CV_Assert(u->refcount == 0);
if (!(u->flags & UMatData::USER_ALLOCATED)) {
goMatDeallocate((unsigned long long)u->origdata);
u->origdata = nullptr;
}
delete u;
}
}
};
GoMatAllocator g_go_mat_allocator;
} // namespace
void GoMatAllocateInit() {
cv::Mat::setDefaultAllocator(&g_go_mat_allocator);
}