You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

imreadwrite.h 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2017 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #ifndef IMREADWRITE_H
  4. #define IMREADWRITE_H
  5. #include <limits.h>
  6. #include <string.h>
  7. #include "allocator.h"
  8. #include "mat.h"
  9. #ifndef NCNN_XADD
  10. using ncnn::NCNN_XADD;
  11. #endif
  12. typedef unsigned char uchar;
  13. enum
  14. {
  15. CV_LOAD_IMAGE_UNCHANGED = -1,
  16. CV_LOAD_IMAGE_GRAYSCALE = 0,
  17. CV_LOAD_IMAGE_COLOR = 1,
  18. };
  19. enum
  20. {
  21. CV_IMWRITE_JPEG_QUALITY = 1
  22. };
  23. // minimal opencv style data structure implementation
  24. namespace cv {
  25. #define CV_8UC1 1
  26. #define CV_8UC3 3
  27. #define CV_8UC4 4
  28. #define CV_32FC1 4
  29. struct Mat
  30. {
  31. Mat()
  32. : data(0), refcount(0), rows(0), cols(0), c(0)
  33. {
  34. }
  35. Mat(int _rows, int _cols, int flags)
  36. : data(0), refcount(0)
  37. {
  38. create(_rows, _cols, flags);
  39. }
  40. // copy
  41. Mat(const Mat& m)
  42. : data(m.data), refcount(m.refcount)
  43. {
  44. if (refcount)
  45. NCNN_XADD(refcount, 1);
  46. rows = m.rows;
  47. cols = m.cols;
  48. c = m.c;
  49. }
  50. Mat(int _rows, int _cols, int flags, void* _data)
  51. : data((unsigned char*)_data), refcount(0)
  52. {
  53. rows = _rows;
  54. cols = _cols;
  55. c = flags;
  56. }
  57. ~Mat()
  58. {
  59. release();
  60. }
  61. // assign
  62. Mat& operator=(const Mat& m)
  63. {
  64. if (this == &m)
  65. return *this;
  66. if (m.refcount)
  67. NCNN_XADD(m.refcount, 1);
  68. release();
  69. data = m.data;
  70. refcount = m.refcount;
  71. rows = m.rows;
  72. cols = m.cols;
  73. c = m.c;
  74. return *this;
  75. }
  76. void create(int _rows, int _cols, int flags)
  77. {
  78. release();
  79. rows = _rows;
  80. cols = _cols;
  81. c = flags;
  82. if (total() > 0)
  83. {
  84. // refcount address must be aligned, so we expand totalsize here
  85. size_t totalsize = (total() + 3) >> 2 << 2;
  86. data = (uchar*)ncnn::fastMalloc(totalsize + (int)sizeof(*refcount));
  87. refcount = (int*)(((uchar*)data) + totalsize);
  88. *refcount = 1;
  89. }
  90. }
  91. void release()
  92. {
  93. if (refcount && NCNN_XADD(refcount, -1) == 1)
  94. ncnn::fastFree(data);
  95. data = 0;
  96. rows = 0;
  97. cols = 0;
  98. c = 0;
  99. refcount = 0;
  100. }
  101. Mat clone() const
  102. {
  103. if (empty())
  104. return Mat();
  105. Mat m(rows, cols, c);
  106. if (total() > 0)
  107. {
  108. memcpy(m.data, data, total());
  109. }
  110. return m;
  111. }
  112. bool empty() const
  113. {
  114. return data == 0 || total() == 0;
  115. }
  116. int type() const
  117. {
  118. return c;
  119. }
  120. size_t total() const
  121. {
  122. return (size_t)cols * rows * c;
  123. }
  124. uchar* data;
  125. // pointer to the reference counter;
  126. // when points to user-allocated data, the pointer is NULL
  127. int* refcount;
  128. int rows;
  129. int cols;
  130. int c;
  131. };
  132. enum ImreadModes
  133. {
  134. IMREAD_UNCHANGED = -1,
  135. IMREAD_GRAYSCALE = 0,
  136. IMREAD_COLOR = 1
  137. };
  138. Mat imread(const std::string& path, int flags = IMREAD_COLOR);
  139. enum ImwriteFlags
  140. {
  141. IMWRITE_JPEG_QUALITY = 1
  142. };
  143. bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params = std::vector<int>());
  144. } // namespace cv
  145. #endif // IMREADWRITE_H