CropUtil.cpp
1.79 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "CropUtil.h"
#include "logger.hpp"
#include "sy_errorinfo.h"
CropUtil::CropUtil(/* args */)
{
}
CropUtil::~CropUtil()
{
release();
}
int CropUtil::init(int devId){
ACL_CALL(aclrtCreateContext(&m_ctx, devId), ACL_SUCCESS, SY_FAILED);
ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
m_dvpp = new DvppProcessx();
int ret = m_dvpp->InitResource(stream);
if (ret != SY_SUCCESS) {
delete m_dvpp;
m_dvpp = nullptr;
LOG_ERROR("dvpp init failed!");
return SY_FAILED;
}
return SY_SUCCESS;
}
int CropUtil::release(){
ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);
if (m_dvpp) {
delete m_dvpp;
m_dvpp = nullptr;
}
if (stream != nullptr) {
int ret = aclrtDestroyStream(stream);
if (ret != ACL_SUCCESS) {
LOG_ERROR("destroy stream failed");
}
stream = nullptr;
}
aclrtDestroyContext(m_ctx);
}
static void adj_position(ImageData& src, uint32_t& xmin, uint32_t& ymin, uint32_t& xmax, uint32_t& ymax) {
if (xmin < 0)
{
xmin = 0;
}
if (ymin < 0)
{
ymin = 0;
}
if (xmax > src.width)
{
xmax = src.width;
}
if (ymax > src.height)
{
ymax = src.height;
}
}
ImageData* CropUtil::crop(ImageData& src, uint32_t xmin, uint32_t ymin, uint32_t xmax, uint32_t ymax) {
int ret = aclrtSetCurrentContext(m_ctx);
if (ACL_SUCCESS != ret)
{
return nullptr;
}
adj_position(src, xmin, ymin, xmax, ymax);
int width = xmax - xmin;
int height = ymax - ymin;
uint32_t alignWidth = (width + 127) / 128 * 128;
uint32_t alignHeight = (height + 15) / 16 * 16;
return m_dvpp->Crop_naked(src, xmin, ymin, xmax, ymax, alignWidth, alignHeight);
}