BSEImg.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "stdafx.h"
  2. #include "BSEImg.h"
  3. namespace OTSDATA {
  4. IMPLEMENT_SERIAL(CBSEImg, CObject, 1)
  5. // constructor
  6. CBSEImg::CBSEImg()
  7. : m_pnImageData(NULL)
  8. {
  9. // set chat data to 0
  10. Init();
  11. }
  12. // constructor
  13. CBSEImg::CBSEImg(CRect a_rectImage)
  14. : m_pnImageData(NULL)
  15. {
  16. // set chat data to 0
  17. Init();
  18. // set image rectangle and create memory for image data
  19. SetImageRect(a_rectImage);
  20. }
  21. // copy constructor
  22. CBSEImg::CBSEImg(const CBSEImg& a_oSource)
  23. : m_pnImageData(NULL)
  24. {
  25. // can't copy itself
  26. if (&a_oSource == this)
  27. {
  28. return;
  29. }
  30. // copy data over
  31. Duplicate(a_oSource);
  32. }
  33. // copy constructor
  34. CBSEImg::CBSEImg(CBSEImg* a_poSource)
  35. : m_pnImageData(NULL)
  36. {
  37. // input check
  38. ASSERT(a_poSource);
  39. if (!a_poSource)
  40. {
  41. return;
  42. }
  43. // can't copy itself
  44. if (a_poSource == this)
  45. {
  46. return;
  47. }
  48. // copy data over
  49. Duplicate(*a_poSource);
  50. }
  51. // =operator
  52. CBSEImg& CBSEImg::operator=(const CBSEImg& a_oSource)
  53. {
  54. // cleanup
  55. Cleanup();
  56. // copy the class data over
  57. Duplicate(a_oSource);
  58. // return class
  59. return *this;
  60. }
  61. // destructor
  62. CBSEImg::~CBSEImg()
  63. {
  64. // cleanup
  65. Cleanup();
  66. }
  67. // CBSEImage functions
  68. void CBSEImg::Serialize(CArchive& ar)
  69. {
  70. if (ar.IsStoring())
  71. {
  72. ar << m_rectImage;
  73. ar.Write(m_pnImageData, sizeof(BYTE) * m_rectImage.Width() * m_rectImage.Height());
  74. }
  75. else
  76. {
  77. ar >> m_rectImage;
  78. InitImageData(m_rectImage.Width(), m_rectImage.Height());
  79. ar.Read(m_pnImageData, sizeof(BYTE) * m_rectImage.Width() * m_rectImage.Height());
  80. }
  81. CObject::Serialize(ar);
  82. }
  83. /*void CBSEImg::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  84. {
  85. xmls::xRect xrectImage;
  86. this->Register("rectImage", &xrectImage);
  87. if (isStoring)
  88. {
  89. xrectImage = m_rectImage;
  90. Slo::Serialize(true, classDoc, rootNode);
  91. }
  92. else
  93. {
  94. xmls::Slo::Serialize(false, classDoc, rootNode);
  95. m_rectImage = xrectImage.value();
  96. }
  97. }*/
  98. // public
  99. // set image rectangle and create memory for image data
  100. void CBSEImg::SetImageRect(const CRect& a_rectImage)
  101. {
  102. // set chat data to 0
  103. InitChartData();
  104. // set image rectangle
  105. m_rectImage = a_rectImage;
  106. // create memory for image data
  107. InitImageData(a_rectImage.Width(), a_rectImage.Height());
  108. }
  109. // Initial image data
  110. void CBSEImg::InitImageData(int imgwidth, int imgheight)
  111. {
  112. // cleanup image data
  113. if (m_pnImageData)
  114. {
  115. delete[]m_pnImageData;
  116. m_pnImageData = NULL;
  117. }
  118. m_rectImage = new CRect(0, 0, imgwidth, imgheight);
  119. // create memory for image data
  120. m_pnImageData = new BYTE[imgwidth * imgheight];
  121. memset(m_pnImageData, 0, sizeof(BYTE) * imgwidth* imgheight);
  122. }
  123. // set image data
  124. void CBSEImg::SetImageData(BYTE* a_pnImageData,int imgwidth,int imgheight)
  125. {
  126. if (!a_pnImageData)
  127. {
  128. return;
  129. }
  130. // initialize image data
  131. InitImageData(imgwidth,imgheight);
  132. // copy image data over
  133. memcpy(m_pnImageData, a_pnImageData, sizeof(BYTE) * imgwidth * imgheight);
  134. }
  135. void CBSEImg::SetImageDataPointer(BYTE* a_pnImageData, int imgwidth, int imgheight)
  136. {
  137. if (!a_pnImageData)
  138. {
  139. return;
  140. }
  141. // initialize image data
  142. if (m_pnImageData)
  143. {
  144. delete[]m_pnImageData;
  145. m_pnImageData = NULL;
  146. }
  147. m_rectImage = new CRect(0, 0, imgwidth, imgheight);
  148. // copy image data over
  149. m_pnImageData = a_pnImageData;
  150. }
  151. // initial chart data
  152. void CBSEImg::InitChartData()
  153. {
  154. // set chart data to 0
  155. memset(m_nBSEChart, 0, sizeof(WORD) * MAXBYTE);
  156. }
  157. // set chart data
  158. void CBSEImg::SetChartData()
  159. {
  160. // set chart data to 0
  161. InitChartData();
  162. // make sure that image data is OK
  163. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  164. {
  165. // No image, return
  166. ASSERT(FALSE);
  167. return;
  168. }
  169. // set chart data
  170. long nDataSize = m_rectImage.Width() * m_rectImage.Height();
  171. for (long i = 0; i < nDataSize; ++i)
  172. {
  173. BYTE nValue = m_pnImageData[i];
  174. ++m_nBSEChart[nValue];
  175. }
  176. }
  177. // calculate chart data BSE high value
  178. // return chart data BSE high value, -1 if there is no image
  179. //
  180. long CBSEImg::CalBSEChartHigh()
  181. {
  182. // set return to -1
  183. long nRet = -1;
  184. // check if there is image
  185. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  186. {
  187. // NO image, return -1
  188. return nRet;
  189. }
  190. // calculate chart data BSE high value
  191. for (long i = 0; i < MAXBYTE; ++i)
  192. {
  193. nRet = max(nRet, m_nBSEChart[i]);
  194. }
  195. // return chart data BSE high value
  196. return nRet;
  197. }
  198. // calculate chart data BSE low value
  199. // return chart data BSE low value, -1 if there is no image
  200. //
  201. long CBSEImg::CalBSEChartLow()
  202. {
  203. // set return to 255 the highest value
  204. long nRet = MAXBYTE;
  205. // check if there is image
  206. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  207. {
  208. // NO image, return -1
  209. return -1;
  210. }
  211. // calculate chart data BSE low value
  212. for (long i = 0; i < MAXBYTE; ++i)
  213. {
  214. nRet = min(nRet, m_nBSEChart[i]);
  215. }
  216. // return chart data BSE low value
  217. return nRet;
  218. }
  219. // get point BSE value
  220. // return point BSE value, -1 if there is no image or point is not in the image
  221. int CBSEImg::GetBSEValue(const CPoint& a_position)
  222. {
  223. // set return to -1
  224. long nRet = -1;
  225. // check if there is image
  226. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  227. {
  228. // NO image, return -1
  229. return nRet;
  230. }
  231. // check if the point is in the image
  232. if (!m_rectImage.PtInRect(a_position))
  233. {
  234. // not in the image, return -1
  235. return nRet;
  236. }
  237. // note: BSE region should start from 0,0, otherwise need to calculate the position.
  238. nRet = m_pnImageData[a_position.x + a_position.y * m_rectImage.Width()];
  239. // return get point BSE value
  240. return nRet;
  241. }
  242. // get BSE value by x, y position values
  243. // return point BSE value, -1 if there is no image or position is not in the image
  244. int CBSEImg::GetBSEValue(const int a_nX, const int a_nY)
  245. {
  246. // convert position value to point
  247. CPoint pos(a_nX, a_nY);
  248. // calculate and return get position BSE value
  249. return GetBSEValue(pos);
  250. }
  251. void CBSEImg::SetBSEValue(const int a_nX, const int a_nY,int value)
  252. {
  253. // convert position value to point
  254. CPoint pos(a_nX, a_nY);
  255. // set return to -1
  256. long nRet = -1;
  257. // check if there is image
  258. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  259. {
  260. // NO image, return -1
  261. return ;
  262. }
  263. // check if the point is in the image
  264. if (!m_rectImage.PtInRect(pos))
  265. {
  266. // not in the image, return -1
  267. return ;
  268. }
  269. // note: BSE region should start from 0,0, otherwise need to calculate the position.
  270. m_pnImageData[pos.x + pos.y * m_rectImage.Width()]=value;
  271. // calculate and return get position BSE value
  272. //GetBSEValue(pos);
  273. }
  274. int CBSEImg::GetValueDirect(const CPoint& a_position) const
  275. {
  276. return (0 <= a_position.x) && (0 <= a_position.y) && (a_position.x < m_rectImage.Width()) && (a_position.y < m_rectImage.Height())
  277. ? m_pnImageData[a_position.x + a_position.y * m_rectImage.Width()]
  278. : 0;
  279. }
  280. // check if the image contains any given gray level pixel
  281. bool CBSEImg::DoesContainPixelValue(int inValue, int a_nPixel /*= 1*/) const
  282. {
  283. int pixelCount = m_rectImage.Width() * m_rectImage.Height();
  284. int nFindPixel = 0;
  285. for (int i = 0; i < pixelCount; ++i)
  286. {
  287. if (m_pnImageData[i] == inValue)
  288. {
  289. // find a same gray level pixel
  290. ++nFindPixel;
  291. if (nFindPixel >= a_nPixel)
  292. {
  293. return true;
  294. }
  295. }
  296. }
  297. return false;
  298. }
  299. // calculate image area
  300. // return image area, return 0 if there no image
  301. DWORD CBSEImg::CalArea()
  302. {
  303. // set return to 0
  304. DWORD nRet = 0;
  305. // check image data
  306. if (!m_pnImageData)
  307. {
  308. // return 0 if there no image
  309. return nRet;
  310. }
  311. // calculate image area
  312. BYTE* pData = m_pnImageData;
  313. for (long i = 0; i < m_rectImage.Width(); ++i)
  314. {
  315. for (long j = 0; j < m_rectImage.Height(); ++j)
  316. {
  317. if (*pData != 0)
  318. {
  319. ++nRet;
  320. }
  321. ++pData;
  322. }
  323. }
  324. // return calculated area value
  325. return nRet;
  326. }
  327. // protected
  328. // cleanup
  329. void CBSEImg::Cleanup()
  330. {
  331. // clean image data
  332. if (m_pnImageData)
  333. {
  334. delete m_pnImageData;
  335. m_pnImageData = NULL;
  336. }
  337. }
  338. // initialization
  339. void CBSEImg::Init()
  340. {
  341. m_rectImage.SetRectEmpty();
  342. InitChartData();
  343. }
  344. // duplication
  345. void CBSEImg::Duplicate(const CBSEImg& a_oSource)
  346. {
  347. // initialization
  348. Init();
  349. // copy data
  350. SetImageRect(a_oSource.GetImageRect());
  351. auto r = a_oSource.GetImageRect();
  352. SetImageData(a_oSource.GetImageDataPointer(),r.Width(),r.Height());
  353. }
  354. }