Image.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #pragma once
  2. #ifndef __IMAGE_H_
  3. #define __IMAGE_H_
  4. #include <vector>
  5. #include <atlimage.h>
  6. struct Image
  7. {
  8. using iterator = unsigned __int32*;
  9. Image() :width(0), height(0), pdata(nullptr) {
  10. }
  11. Image(int w, int h) :pdata(nullptr) {
  12. create(w, h);
  13. }
  14. //copy ctr
  15. Image(const Image& rhs) :pdata(nullptr) {
  16. if (rhs.empty()) {
  17. this->clear();
  18. }
  19. else {
  20. this->create(rhs.width, rhs.height);
  21. memcpy(this->pdata, rhs.pdata, width * height * 4);
  22. }
  23. }
  24. ~Image() {
  25. release();
  26. }
  27. void create(int w, int h) {
  28. width = w, height = h;
  29. if (!pdata) {
  30. pdata = (unsigned char*)malloc(w * h * 4);
  31. }
  32. else {
  33. pdata = (unsigned char*)realloc(pdata, w * h * 4);
  34. }
  35. if (pdata == nullptr)throw("memory not enough");
  36. }
  37. void release() {
  38. width = height = 0;
  39. if (pdata)free(pdata);
  40. pdata = nullptr;
  41. }
  42. int size() {
  43. return width * height;
  44. }
  45. void clear() {
  46. width = height = 0;
  47. }
  48. bool empty()const {
  49. return width == 0;
  50. }
  51. Image& operator=(const Image& rhs) {
  52. if (rhs.empty()) {
  53. this->clear();
  54. }
  55. else if (this != &rhs) {
  56. this->create(rhs.width, rhs.height);
  57. memcpy(this->pdata, rhs.pdata, width * height * 4);
  58. }
  59. return *this;
  60. }
  61. bool read(LPCTSTR file) {
  62. clear();
  63. ATL::CImage img;
  64. HRESULT hr = img.Load(file);
  65. if (hr == S_OK) {
  66. create(img.GetWidth(), img.GetHeight());
  67. translate((unsigned char*)img.GetBits(), img.GetBPP() / 8, img.GetPitch());
  68. return true;
  69. }
  70. else {
  71. return false;
  72. }
  73. }
  74. bool read(void* pMemData, long len) {
  75. clear();
  76. ATL::CImage img;
  77. auto hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len);
  78. if (hGlobal)
  79. {
  80. void* pData = GlobalLock(hGlobal);
  81. memcpy_s(pData, len, pMemData, len);
  82. GlobalUnlock(hGlobal);
  83. }
  84. else {
  85. return false;
  86. }
  87. IStream* pStream = NULL;
  88. if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK) {
  89. GlobalFree(hGlobal);
  90. return false;
  91. }
  92. HRESULT hr = img.Load(pStream);
  93. if (hr == S_OK) {
  94. pStream->Release();
  95. GlobalFree(hGlobal);
  96. create(img.GetWidth(), img.GetHeight());
  97. translate((unsigned char*)img.GetBits(), img.GetBPP() / 8, img.GetPitch());
  98. return true;
  99. }
  100. else {
  101. GlobalFree(hGlobal);
  102. return false;
  103. }
  104. }
  105. bool read(ATL::CImage* img) {
  106. translate((unsigned char*)img->GetBits(), img->GetBPP() / 8, img->GetPitch());
  107. return true;
  108. }
  109. bool write(LPCTSTR file) {
  110. if (empty())
  111. return false;
  112. ATL::CImage img;
  113. img.Create(width, height, 32);
  114. auto pdst = (unsigned char*)img.GetBits();
  115. auto psrc = pdata;
  116. int pitch = img.GetPitch();
  117. for (int i = 0; i < height; ++i) {
  118. for (int j = 0; j < width; ++j) {
  119. ((int*)pdst)[j] = ((int*)psrc)[j];
  120. }
  121. pdst += pitch;
  122. psrc += width * 4;
  123. }
  124. return img.Save(file) == S_OK;
  125. }
  126. void translate(unsigned char* psrc, int pixSize, int pitch) {
  127. auto pdst = pdata;
  128. //gray
  129. if (pixSize == 1) {
  130. for (int i = 0; i < height; ++i) {
  131. for (int j = 0; j < width; ++j) {
  132. *pdst++ = psrc[j];
  133. *pdst++ = psrc[j];
  134. *pdst++ = psrc[j];
  135. *pdst++ = 0xff;
  136. }
  137. psrc += pitch;
  138. }
  139. }//bgr
  140. else if (pixSize == 3) {
  141. for (int i = 0; i < height; ++i) {
  142. for (int j = 0; j < width; ++j) {
  143. *pdst++ = psrc[j * 3 + 0];
  144. *pdst++ = psrc[j * 3 + 1];
  145. *pdst++ = psrc[j * 3 + 2];
  146. *pdst++ = 0xff;
  147. }
  148. psrc += pitch;
  149. }
  150. }
  151. else if (pixSize == 4) {
  152. for (int i = 0; i < height; ++i) {
  153. for (int j = 0; j < width; ++j) {
  154. *pdst++ = psrc[j * 4 + 0];
  155. *pdst++ = psrc[j * 4 + 1];
  156. *pdst++ = psrc[j * 4 + 2];
  157. *pdst++ = psrc[j * 4 + 3];
  158. }
  159. psrc += pitch;
  160. }
  161. }
  162. }
  163. template<typename Tp>
  164. Tp at(int y, int x)const {
  165. return ((Tp*)pdata)[y * width + x];
  166. }
  167. template<typename Tp>
  168. Tp& at(int y, int x) {
  169. return ((Tp*)pdata)[y * width + x];
  170. }
  171. template<typename Tp>
  172. Tp* ptr(int y) {
  173. return (Tp*)(pdata + y * width * 4);
  174. }
  175. template<typename Tp>
  176. const Tp* ptr(int y)const {
  177. return (Tp*)(pdata + y * width * 4);
  178. }
  179. iterator begin() {
  180. return (iterator)pdata;
  181. }
  182. iterator end() {
  183. return (iterator)pdata + width * height;
  184. }
  185. iterator begin()const {
  186. return (iterator)pdata;
  187. }
  188. iterator end()const {
  189. return (iterator)pdata + width * height;
  190. }
  191. void fill(unsigned int val) {
  192. std::fill(begin(), end(), val);
  193. }
  194. void fill(int row, int col, int h, int w, unsigned int val) {
  195. for (int i = 0; i < h; ++i) {
  196. auto p = ptr<unsigned int>(row + i) + col;
  197. std::fill(p, p + w, val);
  198. }
  199. }
  200. int width, height;
  201. unsigned char* pdata;
  202. };
  203. //µ¥Í¨µÀͼÏñ
  204. struct ImageBin {
  205. using iterator = unsigned char*;
  206. ImageBin() :width(0), height(0) {}
  207. ImageBin(const ImageBin& rhs) {
  208. this->width = rhs.width;
  209. this->height = rhs.height;
  210. this->pixels = rhs.pixels;
  211. }
  212. void create(int w, int h) {
  213. width = w, height = h;
  214. pixels.resize(w * h);
  215. }
  216. void clear() {
  217. width = height = 0;
  218. }
  219. int size()const {
  220. return width * height;
  221. }
  222. bool empty()const {
  223. return width == 0;
  224. }
  225. unsigned char* data() {
  226. return pixels.data();
  227. }
  228. ImageBin& operator=(const ImageBin& rhs) {
  229. this->width = rhs.width;
  230. this->height = rhs.height;
  231. this->pixels = rhs.pixels;
  232. return *this;
  233. }
  234. unsigned char at(int y, int x)const {
  235. return pixels[y * width + x];
  236. }
  237. unsigned char& at(int y, int x) {
  238. return pixels[y * width + x];
  239. }
  240. unsigned char* ptr(int y) {
  241. return pixels.data() + y * width;
  242. }
  243. unsigned char const* ptr(int y) const {
  244. return pixels.data() + y * width;
  245. }
  246. void fromImage4(const Image& img4) {
  247. create(img4.width, img4.height);
  248. auto psrc = img4.pdata;
  249. for (size_t i = 0; i < pixels.size(); ++i) {
  250. //pixels[i] = (psrc[0] + psrc[1] + psrc[2]) / 3;
  251. // Gray = (R*299 + G*587 + B*114 + 500) / 1000
  252. pixels[i] = (psrc[2] * 299 + psrc[1] * 587 + psrc[0] * 114 + 500) / 1000;
  253. psrc += 4;
  254. }
  255. }
  256. bool write(LPCTSTR file) {
  257. if (empty())
  258. return false;
  259. ATL::CImage img;
  260. img.Create(width, height, 32);
  261. auto pdst = (unsigned char*)img.GetBits();
  262. auto psrc = pixels.data();
  263. int pitch = img.GetPitch();
  264. for (int i = 0; i < height; ++i) {
  265. for (int j = 0; j < width; ++j) {
  266. //((int*)pdst)[j] = ((int*)psrc)[j];
  267. uchar v = psrc[j] == 1 ? 0xff : 0;
  268. pdst[j * 4] = pdst[j * 4 + 1] = pdst[j * 4 + 2] = v;
  269. pdst[j * 4 + 3] = 0xff;
  270. }
  271. pdst += pitch;
  272. psrc += width;
  273. }
  274. return img.Save(file) == S_OK;
  275. }
  276. iterator begin() {
  277. return pixels.data();
  278. }
  279. iterator end() {
  280. return pixels.data() + pixels.size();
  281. }
  282. int width, height;
  283. std::vector<unsigned char> pixels;
  284. };
  285. using inputimg = const Image&;
  286. using outputimg = Image&;
  287. using inputbin = const ImageBin&;
  288. using outputbin = ImageBin&;
  289. #endif