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.cpp 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2021 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "imreadwrite.h"
  4. #include <stdio.h>
  5. #define STB_IMAGE_IMPLEMENTATION
  6. #define STBI_NO_THREAD_LOCALS
  7. #define STBI_ONLY_JPEG
  8. #define STBI_ONLY_PNG
  9. #define STBI_ONLY_BMP
  10. #define STBI_ONLY_PNM
  11. #include "../../src/stb_image.h"
  12. #define STB_IMAGE_WRITE_IMPLEMENTATION
  13. #include "../../src/stb_image_write.h"
  14. namespace cv {
  15. Mat imread(const std::string& path, int flags)
  16. {
  17. int desired_channels = 0;
  18. if (flags == IMREAD_UNCHANGED)
  19. {
  20. desired_channels = 0;
  21. }
  22. else if (flags == IMREAD_GRAYSCALE)
  23. {
  24. desired_channels = 1;
  25. }
  26. else if (flags == IMREAD_COLOR)
  27. {
  28. desired_channels = 3;
  29. }
  30. else
  31. {
  32. // unknown flags
  33. return Mat();
  34. }
  35. int w;
  36. int h;
  37. int c;
  38. unsigned char* pixeldata = stbi_load(path.c_str(), &w, &h, &c, desired_channels);
  39. if (!pixeldata)
  40. {
  41. // load failed
  42. return Mat();
  43. }
  44. if (desired_channels)
  45. {
  46. c = desired_channels;
  47. }
  48. // copy pixeldata to Mat
  49. Mat img;
  50. if (c == 1)
  51. {
  52. img.create(h, w, CV_8UC1);
  53. }
  54. else if (c == 3)
  55. {
  56. img.create(h, w, CV_8UC3);
  57. }
  58. else if (c == 4)
  59. {
  60. img.create(h, w, CV_8UC4);
  61. }
  62. else
  63. {
  64. // unexpected channels
  65. stbi_image_free(pixeldata);
  66. return Mat();
  67. }
  68. memcpy(img.data, pixeldata, static_cast<size_t>(w) * static_cast<size_t>(h) * static_cast<size_t>(c));
  69. stbi_image_free(pixeldata);
  70. // // resolve exif orientation
  71. // {
  72. // std::ifstream ifs;
  73. // ifs.open(filename.c_str(), std::ifstream::in);
  74. //
  75. // if (ifs.good())
  76. // {
  77. // ExifReader exif_reader(ifs);
  78. // if (exif_reader.parse())
  79. // {
  80. // ExifEntry_t e = exif_reader.getTag(ORIENTATION);
  81. // int orientation = e.field_u16;
  82. // if (orientation >= 1 && orientation <= 8)
  83. // rotate_by_orientation(img, img, orientation);
  84. // }
  85. // }
  86. //
  87. // ifs.close();
  88. // }
  89. // rgb to bgr
  90. if (c == 3)
  91. {
  92. uchar* p = img.data;
  93. for (int i = 0; i < w * h; i++)
  94. {
  95. std::swap(p[0], p[2]);
  96. p += 3;
  97. }
  98. }
  99. if (c == 4)
  100. {
  101. uchar* p = img.data;
  102. for (int i = 0; i < w * h; i++)
  103. {
  104. std::swap(p[0], p[2]);
  105. p += 4;
  106. }
  107. }
  108. return img;
  109. }
  110. bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params)
  111. {
  112. const char* _ext = strrchr(path.c_str(), '.');
  113. if (!_ext)
  114. {
  115. // missing extension
  116. return false;
  117. }
  118. std::string ext = _ext;
  119. Mat img = m.clone();
  120. // bgr to rgb
  121. int c = 0;
  122. if (img.type() == CV_8UC1)
  123. {
  124. c = 1;
  125. }
  126. else if (img.type() == CV_8UC3)
  127. {
  128. c = 3;
  129. uchar* p = img.data;
  130. for (int i = 0; i < img.cols * img.rows; i++)
  131. {
  132. std::swap(p[0], p[2]);
  133. p += 3;
  134. }
  135. }
  136. else if (img.type() == CV_8UC4)
  137. {
  138. c = 4;
  139. uchar* p = img.data;
  140. for (int i = 0; i < img.cols * img.rows; i++)
  141. {
  142. std::swap(p[0], p[2]);
  143. p += 4;
  144. }
  145. }
  146. else
  147. {
  148. // unexpected image channels
  149. return false;
  150. }
  151. bool success = false;
  152. if (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG")
  153. {
  154. int quality = 95;
  155. for (size_t i = 0; i < params.size(); i += 2)
  156. {
  157. if (params[i] == IMWRITE_JPEG_QUALITY)
  158. {
  159. quality = params[i + 1];
  160. break;
  161. }
  162. }
  163. success = stbi_write_jpg(path.c_str(), img.cols, img.rows, c, img.data, quality);
  164. }
  165. else if (ext == ".png" || ext == ".PNG")
  166. {
  167. success = stbi_write_png(path.c_str(), img.cols, img.rows, c, img.data, 0);
  168. }
  169. else if (ext == ".bmp" || ext == ".BMP")
  170. {
  171. success = stbi_write_bmp(path.c_str(), img.cols, img.rows, c, img.data);
  172. }
  173. else
  174. {
  175. // unknown extension type
  176. return false;
  177. }
  178. return success;
  179. }
  180. } // namespace cv