| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef NCNN_SIMPLEOCV_H |
| 16 | #define NCNN_SIMPLEOCV_H |
| 17 | |
| 18 | #include "platform.h" |
| 19 | |
| 20 | #if NCNN_SIMPLEOCV |
| 21 | |
| 22 | #include <limits.h> |
| 23 | #include <string.h> |
| 24 | #include "allocator.h" |
| 25 | #include "mat.h" |
| 26 | |
| 27 | #if defined(_MSC_VER) || defined(__GNUC__) |
| 28 | #pragma push_macro("min") |
| 29 | #pragma push_macro("max") |
| 30 | #undef min |
| 31 | #undef max |
| 32 | #endif |
| 33 | |
| 34 | #ifndef NCNN_XADD |
| 35 | using ncnn::NCNN_XADD; |
| 36 | #endif |
| 37 | |
| 38 | typedef unsigned char uchar; |
| 39 | typedef unsigned short ushort; |
| 40 | typedef unsigned int uint; |
| 41 | |
| 42 | enum |
| 43 | { |
| 44 | CV_LOAD_IMAGE_UNCHANGED = -1, |
| 45 | CV_LOAD_IMAGE_GRAYSCALE = 0, |
| 46 | CV_LOAD_IMAGE_COLOR = 1, |
| 47 | }; |
| 48 | |
| 49 | enum |
| 50 | { |
| 51 | CV_IMWRITE_JPEG_QUALITY = 1 |
| 52 | }; |
| 53 | |
| 54 | // minimal opencv style data structure implementation |
| 55 | namespace cv { |
| 56 | |
| 57 | template<typename _Tp> |
| 58 | static inline _Tp saturate_cast(int v) |
| 59 | { |
| 60 | return _Tp(v); |
| 61 | } |
| 62 | template<> |
| 63 | inline uchar saturate_cast<uchar>(int v) |
| 64 | { |
| 65 | return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); |
| 66 | } |
| 67 | |
| 68 | template<typename _Tp> |
| 69 | struct Scalar_ |
| 70 | { |
| 71 | Scalar_() |
| 72 | { |
| 73 | v[0] = 0; |
| 74 | v[1] = 0; |
| 75 | v[2] = 0; |
| 76 | v[3] = 0; |
| 77 | } |
| 78 | Scalar_(_Tp _v0) |
| 79 | { |
| 80 | v[0] = _v0; |
| 81 | v[1] = 0; |
| 82 | v[2] = 0; |
| 83 | v[3] = 0; |
| 84 | } |
| 85 | Scalar_(_Tp _v0, _Tp _v1, _Tp _v2) |
| 86 | { |
| 87 | v[0] = _v0; |
| 88 | v[1] = _v1; |
| 89 | v[2] = _v2; |
| 90 | v[3] = 0; |
| 91 | } |
| 92 | Scalar_(_Tp _v0, _Tp _v1, _Tp _v2, _Tp _v3) |
| 93 | { |
| 94 | v[0] = _v0; |
| 95 | v[1] = _v1; |
| 96 | v[2] = _v2; |
| 97 | v[3] = _v3; |
| 98 | } |
| 99 | |
| 100 | const _Tp operator[](const int i) const |
| 101 | { |
| 102 | return v[i]; |
| 103 | } |
| 104 | |
| 105 | _Tp operator[](const int i) |
| 106 | { |
| 107 | return v[i]; |
| 108 | } |
| 109 | |
| 110 | _Tp v[4]; |
| 111 | }; |
| 112 | |
| 113 | typedef Scalar_<uchar> Scalar; |
| 114 | |
| 115 | template<typename _Tp> |
| 116 | struct Point_ |
| 117 | { |
| 118 | Point_() |
| 119 | : x(0), y(0) |
| 120 | { |
| 121 | } |
| 122 | Point_(_Tp _x, _Tp _y) |
| 123 | : x(_x), y(_y) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | template<typename _Tp2> |
| 128 | operator Point_<_Tp2>() const |
| 129 | { |
| 130 | return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y)); |
| 131 | } |
| 132 | |
| 133 | _Tp x; |
| 134 | _Tp y; |
| 135 | }; |
| 136 | |
| 137 | typedef Point_<int> Point; |
| 138 | typedef Point_<float> Point2f; |
| 139 | |
| 140 | template<typename _Tp> |
| 141 | struct Size_ |
| 142 | { |
| 143 | Size_() |
| 144 | : width(0), height(0) |
| 145 | { |
| 146 | } |
| 147 | Size_(_Tp _w, _Tp _h) |
| 148 | : width(_w), height(_h) |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | template<typename _Tp2> |
| 153 | operator Size_<_Tp2>() const |
| 154 | { |
| 155 | return Size_<_Tp2>(saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); |
| 156 | } |
| 157 | |
| 158 | _Tp width; |
| 159 | _Tp height; |
| 160 | }; |
| 161 | |
| 162 | typedef Size_<int> Size; |
| 163 | typedef Size_<float> Size2f; |
| 164 | |
| 165 | template<typename _Tp> |
| 166 | struct Rect_ |
| 167 | { |
| 168 | Rect_() |
| 169 | : x(0), y(0), width(0), height(0) |
| 170 | { |
| 171 | } |
| 172 | Rect_(_Tp _x, _Tp _y, _Tp _w, _Tp _h) |
| 173 | : x(_x), y(_y), width(_w), height(_h) |
| 174 | { |
| 175 | } |
| 176 | Rect_(Point_<_Tp> _p, Size_<_Tp> _size) |
| 177 | : x(_p.x), y(_p.y), width(_size.width), height(_size.height) |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | template<typename _Tp2> |
| 182 | operator Rect_<_Tp2>() const |
| 183 | { |
| 184 | return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); |
| 185 | } |
| 186 | |
| 187 | _Tp x; |
| 188 | _Tp y; |
| 189 | _Tp width; |
| 190 | _Tp height; |
| 191 | |
| 192 | // area |
| 193 | _Tp area() const |
| 194 | { |
| 195 | return width * height; |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | template<typename _Tp> |
| 200 | static inline Rect_<_Tp>& operator&=(Rect_<_Tp>& a, const Rect_<_Tp>& b) |
| 201 | { |
| 202 | _Tp x1 = std::max(a.x, b.x), y1 = std::max(a.y, b.y); |
| 203 | a.width = std::min(a.x + a.width, b.x + b.width) - x1; |
| 204 | a.height = std::min(a.y + a.height, b.y + b.height) - y1; |
| 205 | a.x = x1; |
| 206 | a.y = y1; |
| 207 | if (a.width <= 0 || a.height <= 0) |
| 208 | a = Rect_<_Tp>(); |
| 209 | return a; |
| 210 | } |
| 211 | |
| 212 | template<typename _Tp> |
| 213 | static inline Rect_<_Tp>& operator|=(Rect_<_Tp>& a, const Rect_<_Tp>& b) |
| 214 | { |
| 215 | _Tp x1 = std::min(a.x, b.x), y1 = std::min(a.y, b.y); |
| 216 | a.width = std::max(a.x + a.width, b.x + b.width) - x1; |
| 217 | a.height = std::max(a.y + a.height, b.y + b.height) - y1; |
| 218 | a.x = x1; |
| 219 | a.y = y1; |
| 220 | return a; |
| 221 | } |
| 222 | |
| 223 | template<typename _Tp> |
| 224 | static inline Rect_<_Tp> operator&(const Rect_<_Tp>& a, const Rect_<_Tp>& b) |
| 225 | { |
| 226 | Rect_<_Tp> c = a; |
| 227 | return c &= b; |
| 228 | } |
| 229 | |
| 230 | template<typename _Tp> |
| 231 | static inline Rect_<_Tp> operator|(const Rect_<_Tp>& a, const Rect_<_Tp>& b) |
| 232 | { |
| 233 | Rect_<_Tp> c = a; |
| 234 | return c |= b; |
| 235 | } |
| 236 | |
| 237 | typedef Rect_<int> Rect; |
| 238 | typedef Rect_<float> Rect2f; |
| 239 | |
| 240 | #define CV_8UC1 1 |
| 241 | #define CV_8UC3 3 |
| 242 | #define CV_8UC4 4 |
| 243 | #define CV_32FC1 4 |
| 244 | |
| 245 | struct NCNN_EXPORT Mat |
| 246 | { |
| 247 | Mat() |
| 248 | : data(0), refcount(0), rows(0), cols(0), c(0) |
| 249 | { |
| 250 | } |
| 251 | |
| 252 | Mat(int _rows, int _cols, int flags) |
| 253 | : data(0), refcount(0) |
| 254 | { |
| 255 | create(_rows, _cols, flags); |
| 256 | } |
| 257 | |
| 258 | // copy |
| 259 | Mat(const Mat& m) |
| 260 | : data(m.data), refcount(m.refcount) |
| 261 | { |
| 262 | if (refcount) |
| 263 | NCNN_XADD(refcount, 1); |
| 264 | |
| 265 | rows = m.rows; |
| 266 | cols = m.cols; |
| 267 | c = m.c; |
| 268 | } |
| 269 | |
| 270 | Mat(int _rows, int _cols, int flags, void* _data) |
| 271 | : data((unsigned char*)_data), refcount(0) |
| 272 | { |
| 273 | rows = _rows; |
| 274 | cols = _cols; |
| 275 | c = flags; |
| 276 | } |
| 277 | |
| 278 | ~Mat() |
| 279 | { |
| 280 | release(); |
| 281 | } |
| 282 | |
| 283 | // assign |
| 284 | Mat& operator=(const Mat& m) |
| 285 | { |
| 286 | if (this == &m) |
| 287 | return *this; |
| 288 | |
| 289 | if (m.refcount) |
| 290 | NCNN_XADD(m.refcount, 1); |
| 291 | |
| 292 | release(); |
| 293 | |
| 294 | data = m.data; |
| 295 | refcount = m.refcount; |
| 296 | |
| 297 | rows = m.rows; |
| 298 | cols = m.cols; |
| 299 | c = m.c; |
| 300 | |
| 301 | return *this; |
| 302 | } |
| 303 | |
| 304 | Mat& operator=(const Scalar& s) |
| 305 | { |
| 306 | if (total() > 0) |
| 307 | { |
| 308 | uchar* p = data; |
| 309 | for (int i = 0; i < cols * rows; i++) |
| 310 | { |
| 311 | for (int j = 0; j < c; j++) |
| 312 | { |
| 313 | *p++ = s[j]; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return *this; |
| 319 | } |
| 320 | |
| 321 | void create(int _rows, int _cols, int flags) |
| 322 | { |
| 323 | release(); |
| 324 | |
| 325 | rows = _rows; |
| 326 | cols = _cols; |
| 327 | c = flags; |
| 328 | |
| 329 | if (total() > 0) |
| 330 | { |
| 331 | // refcount address must be aligned, so we expand totalsize here |
| 332 | size_t totalsize = (total() + 3) >> 2 << 2; |
| 333 | data = (uchar*)ncnn::fastMalloc(totalsize + (int)sizeof(*refcount)); |
| 334 | refcount = (int*)(((uchar*)data) + totalsize); |
| 335 | *refcount = 1; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void release() |
| 340 | { |
| 341 | if (refcount && NCNN_XADD(refcount, -1) == 1) |
| 342 | ncnn::fastFree(data); |
| 343 | |
| 344 | data = 0; |
| 345 | |
| 346 | rows = 0; |
| 347 | cols = 0; |
| 348 | c = 0; |
| 349 | |
| 350 | refcount = 0; |
| 351 | } |
| 352 | |
| 353 | Mat clone() const |
| 354 | { |
| 355 | if (empty()) |
| 356 | return Mat(); |
| 357 | |
| 358 | Mat m(rows, cols, c); |
| 359 | |
| 360 | if (total() > 0) |
| 361 | { |
| 362 | memcpy(m.data, data, total()); |
| 363 | } |
| 364 | |
| 365 | return m; |
| 366 | } |
| 367 | |
| 368 | bool empty() const |
| 369 | { |
| 370 | return data == 0 || total() == 0; |
| 371 | } |
| 372 | |
| 373 | int channels() const |
| 374 | { |
| 375 | return c; |
| 376 | } |
| 377 | |
| 378 | int type() const |
| 379 | { |
| 380 | return c; |
| 381 | } |
| 382 | |
| 383 | size_t total() const |
| 384 | { |
| 385 | return cols * rows * c; |
| 386 | } |
| 387 | |
| 388 | const uchar* ptr(int y) const |
| 389 | { |
| 390 | return data + y * cols * c; |
| 391 | } |
| 392 | |
| 393 | uchar* ptr(int y) |
| 394 | { |
| 395 | return data + y * cols * c; |
| 396 | } |
| 397 | |
| 398 | template<typename _Tp> |
| 399 | const _Tp* ptr(int y) const |
| 400 | { |
| 401 | return (const _Tp*)(data + y * cols * c); |
| 402 | } |
| 403 | |
| 404 | template<typename _Tp> |
| 405 | _Tp* ptr(int y) |
| 406 | { |
| 407 | return (_Tp*)(data + y * cols * c); |
| 408 | } |
| 409 | |
| 410 | // roi |
| 411 | Mat operator()(const Rect& roi) const |
| 412 | { |
| 413 | if (empty()) |
| 414 | return Mat(); |
| 415 | |
| 416 | Mat m(roi.height, roi.width, c); |
| 417 | |
| 418 | int sy = roi.y; |
| 419 | for (int y = 0; y < roi.height; y++) |
| 420 | { |
| 421 | const uchar* sptr = ptr(sy) + roi.x * c; |
| 422 | uchar* dptr = m.ptr(y); |
| 423 | memcpy(dptr, sptr, roi.width * c); |
| 424 | sy++; |
| 425 | } |
| 426 | |
| 427 | return m; |
| 428 | } |
| 429 | |
| 430 | uchar* data; |
| 431 | |
| 432 | // pointer to the reference counter; |
| 433 | // when points to user-allocated data, the pointer is NULL |
| 434 | int* refcount; |
| 435 | |
| 436 | int rows; |
| 437 | int cols; |
| 438 | |
| 439 | int c; |
| 440 | }; |
| 441 | |
| 442 | enum ImreadModes |
| 443 | { |
| 444 | IMREAD_UNCHANGED = -1, |
| 445 | IMREAD_GRAYSCALE = 0, |
| 446 | IMREAD_COLOR = 1 |
| 447 | }; |
| 448 | |
| 449 | NCNN_EXPORT Mat imread(const std::string& path, int flags = IMREAD_COLOR); |
| 450 | |
| 451 | NCNN_EXPORT Mat imdecode(const std::vector<uchar>& buf, int flags = IMREAD_COLOR); |
| 452 | |
| 453 | enum ImwriteFlags |
| 454 | { |
| 455 | IMWRITE_JPEG_QUALITY = 1 |
| 456 | }; |
| 457 | |
| 458 | NCNN_EXPORT bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params = std::vector<int>()); |
| 459 | |
| 460 | NCNN_EXPORT void imshow(const std::string& name, const Mat& m); |
| 461 | |
| 462 | NCNN_EXPORT int waitKey(int delay = 0); |
| 463 | |
| 464 | #if NCNN_PIXEL |
| 465 | NCNN_EXPORT void resize(const Mat& src, Mat& dst, const Size& size, float sw = 0.f, float sh = 0.f, int flags = 0); |
| 466 | #endif // NCNN_PIXEL |
| 467 | |
| 468 | #if NCNN_PIXEL_DRAWING |
| 469 | |
| 470 | enum |
| 471 | { |
| 472 | FILLED = -1 |
| 473 | }; |
| 474 | |
| 475 | NCNN_EXPORT void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness = 1); |
| 476 | |
| 477 | NCNN_EXPORT void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness = 1); |
| 478 | |
| 479 | NCNN_EXPORT void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness = 1); |
| 480 | |
| 481 | NCNN_EXPORT void line(Mat& img, Point p0, Point p1, const Scalar& color, int thickness = 1); |
| 482 | |
| 483 | enum |
| 484 | { |
| 485 | FONT_HERSHEY_SIMPLEX = 0 |
| 486 | }; |
| 487 | |
| 488 | NCNN_EXPORT void putText(Mat& img, const std::string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1); |
| 489 | |
| 490 | NCNN_EXPORT Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine); |
| 491 | |
| 492 | #endif // NCNN_PIXEL_DRAWING |
| 493 | |
| 494 | } // namespace cv |
| 495 | |
| 496 | #if defined(_MSC_VER) || defined(__GNUC__) |
| 497 | #pragma pop_macro("min") |
| 498 | #pragma pop_macro("max") |
| 499 | #endif |
| 500 | |
| 501 | #endif // NCNN_SIMPLEOCV |
| 502 | |
| 503 | #endif // NCNN_SIMPLEOCV_H |