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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
#include "image.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image/stb_image_write.h"
#include <cstring>
#include <iostream>
Image::Image()
{
ptr_ = nullptr;
owner_ = false;
unit_type_ = 0;
origin_rect_ = Rect();
my_rect_ = Rect();
rows = 0;
cols = 0;
}
Image::Image(const Size &size, uattr type)
{
ptr_ = (uchar*)malloc(size.width * size.height * UNITATTR_SIZE(type));
assert(ptr_ != nullptr);
my_rect_ = origin_rect_ = Rect(0, 0, size.width, size.height);
unit_type_ = type;
rows = size.height;
cols = size.width;
}
Image::Image(const Size &size, uattr type, uchar* data)
{
assert(data != nullptr);
ptr_ = data;
my_rect_ = origin_rect_ = Rect(0, 0, size.width, size.height);
unit_type_ = type;
rows = size.height;
cols = size.width;
}
Image::Image(const Image &other)
{
ptr_ = other.ptr_;
owner_ = true;
auto t = const_cast<Image&>(other);
t.owner_ = false;
unit_type_ = other.unit_type_;
origin_rect_ = other.origin_rect_;
my_rect_ = other.my_rect_;
rows = other.rows;
cols = other.cols;
}
Image::Image(const Image &&other)
{
ptr_ = other.ptr_;
owner_ = true;
auto t = const_cast<Image&>(other);
t.owner_ = false;
unit_type_ = other.unit_type_;
origin_rect_ = other.origin_rect_;
my_rect_ = other.my_rect_;
rows = other.rows;
cols = other.cols;
}
Image &Image::operator=(const Image &other)
{
if (this != &other)
{
Release();
ptr_ = other.ptr_;
origin_rect_ = other.origin_rect_;
my_rect_ = other.my_rect_;
unit_type_ = other.unit_type_;
owner_ = true;
auto t = const_cast<Image&>(other);
t.owner_ = false;
rows = other.rows;
cols = other.cols;
}
return *this;
}
Image Image::operator()(const Rect &roi)
{
Image ret = *this;
ret.owner_ = false;
this->owner_ = true;
ret.my_rect_ = roi;
return ret;
}
void Image::Create(const Size &size, uattr type)
{
Release();
ptr_ = (uchar*)malloc(size.width * size.height * UNITATTR_SIZE(type));
assert(ptr_ != NULL);
my_rect_ = origin_rect_ = Rect(0, 0, size.width, size.height);
unit_type_ = type;
rows = size.height;
cols = size.width;
}
void Image::Release()
{
if (owner_ && ptr_)
free(ptr_);
ptr_ = nullptr;
owner_ = true;
unit_type_ = 0;
origin_rect_ = Rect();
my_rect_ = Rect();
rows = 0;
cols = 0;
}
std::uint8_t Image::Channels() const
{
return UNITATTR_SIZE(unit_type_);
}
bool Image::IsEmpty() const
{
return ptr_ == nullptr || rows <= 0 || cols <= 0;
}
Size Image::GetSize() const
{
return Size(my_rect_.width, my_rect_.height);
}
void Image::CopyTo(Image &other) const
{
other.Create(my_rect_.size(), unit_type_);
if (my_rect_.x == 0 && my_rect_.y == 0)
{
memcpy(other.ptr_, ptr_, my_rect_.height * my_rect_.width * UNITATTR_SIZE(unit_type_));
return;
}
for (int row = 0; row < other.my_rect_.height; ++row)
memcpy(other.ptr_ + (UNITATTR_SIZE(unit_type_) * other.my_rect_.width * row), ptr_ + (((origin_rect_.width * (row + my_rect_.y)) + my_rect_.x) * UNITATTR_SIZE(unit_type_)), (UNITATTR_SIZE(unit_type_) * other.my_rect_.width));
}
Image::~Image()
{
Release();
}
template<typename E>
E &Image::At(int x, int y)
{
return *((E*)(&(ptr_[UNITATTR_SIZE(unit_type_)*((origin_rect_.width * (y + my_rect_.y)) + (x + my_rect_.x))])));
}
template<typename E>
E &Image::At(const Point &point)
{
return At<E>(point.x, point.y);
}
template<typename E>
E *Image::Ptr(int x, int y)
{
return &At<E>(x, y);
}
template<typename E>
E *Image::Ptr(const Point &point)
{
return Ptr<E>(point.x, point.y);
}
static IMAGE_TYPE ChannelToType(const int channels)
{
if(channels == 1)
{
return IMAGE_TYPE::IMAGE_8UC1;
}else if(channels == 2)
{
return IMAGE_TYPE::IMAGE_8UC2;
}else if(channels == 3)
{
return IMAGE_TYPE::IMAGE_8UC3;
}else if(channels == 4)
{
return IMAGE_TYPE::IMAGE_32FC4;
}else{
return IMAGE_TYPE::IMAGE_32FC4;
}
}
Image LoadImage(const char* img_path)
{
Image matrix;
assert(img_path != nullptr);
if(img_path != nullptr)
{
int w, h, n;
w = h = n = 0;
std::uint8_t *data = stbi_load(img_path, &w, &h, &n, 0);
if(nullptr != data)
{
auto tmp = Image(Size(w, h), ChannelToType(n), data);
matrix = tmp;
}
}
return matrix;
}
int WriteImage(Image img, const char* img_path)
{
if(img.IsEmpty() || img_path == nullptr)
{
return -1;
}
std::uint8_t* data = img.Ptr<std::uint8_t>(0, 0);
auto src_size = img.GetSize();
int h = src_size.height;
int w = src_size.width;
auto n = img.Channels();
std::string file_name = std::string(img_path);
size_t dot_position = file_name.find_last_of(".");
if (dot_position != std::string::npos)
{
std::string file_extension = file_name.substr(dot_position + 1);
if(file_extension == "jeg" || file_extension == "jpeg" )
{
return stbi_write_jpg(img_path, w, h, n, data, 100);
}else if(file_extension == "png")
{
return stbi_write_png(img_path,w, h, n, data, w * n );
}else if(file_extension == "bmp")
{
return stbi_write_bmp(img_path, w, h, n, data);
}else
{
std::cerr<< "The image format unsupported!!!" << std::endl;
}
}
return -1;
}
|