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.9 kB

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