BSEImg.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. // input check
  127. //AMICS_ASSERT(a_pnImageData);
  128. if (!a_pnImageData)
  129. {
  130. return;
  131. }
  132. // initialize image data
  133. InitImageData(imgwidth,imgheight);
  134. // copy image data over
  135. memcpy(m_pnImageData, a_pnImageData, sizeof(BYTE) * imgwidth * imgheight);
  136. }
  137. // initial chart data
  138. void CBSEImg::InitChartData()
  139. {
  140. // set chart data to 0
  141. memset(m_nBSEChart, 0, sizeof(WORD) * MAXBYTE);
  142. }
  143. // set chart data
  144. void CBSEImg::SetChartData()
  145. {
  146. // set chart data to 0
  147. InitChartData();
  148. // make sure that image data is OK
  149. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  150. {
  151. // No image, return
  152. ASSERT(FALSE);
  153. return;
  154. }
  155. // set chart data
  156. long nDataSize = m_rectImage.Width() * m_rectImage.Height();
  157. for (long i = 0; i < nDataSize; ++i)
  158. {
  159. BYTE nValue = m_pnImageData[i];
  160. ++m_nBSEChart[nValue];
  161. }
  162. }
  163. // calculate chart data BSE high value
  164. // return chart data BSE high value, -1 if there is no image
  165. //
  166. long CBSEImg::CalBSEChartHigh()
  167. {
  168. // set return to -1
  169. long nRet = -1;
  170. // check if there is image
  171. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  172. {
  173. // NO image, return -1
  174. return nRet;
  175. }
  176. // calculate chart data BSE high value
  177. for (long i = 0; i < MAXBYTE; ++i)
  178. {
  179. nRet = max(nRet, m_nBSEChart[i]);
  180. }
  181. // return chart data BSE high value
  182. return nRet;
  183. }
  184. // calculate chart data BSE low value
  185. // return chart data BSE low value, -1 if there is no image
  186. //
  187. long CBSEImg::CalBSEChartLow()
  188. {
  189. // set return to 255 the highest value
  190. long nRet = MAXBYTE;
  191. // check if there is image
  192. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  193. {
  194. // NO image, return -1
  195. return -1;
  196. }
  197. // calculate chart data BSE low value
  198. for (long i = 0; i < MAXBYTE; ++i)
  199. {
  200. nRet = min(nRet, m_nBSEChart[i]);
  201. }
  202. // return chart data BSE low value
  203. return nRet;
  204. }
  205. // get point BSE value
  206. // return point BSE value, -1 if there is no image or point is not in the image
  207. int CBSEImg::GetBSEValue(const CPoint& a_position)
  208. {
  209. // set return to -1
  210. long nRet = -1;
  211. // check if there is image
  212. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  213. {
  214. // NO image, return -1
  215. return nRet;
  216. }
  217. // check if the point is in the image
  218. if (!m_rectImage.PtInRect(a_position))
  219. {
  220. // not in the image, return -1
  221. return nRet;
  222. }
  223. // note: BSE region should start from 0,0, otherwise need to calculate the position.
  224. nRet = m_pnImageData[a_position.x + a_position.y * m_rectImage.Width()];
  225. // return get point BSE value
  226. return nRet;
  227. }
  228. // get BSE value by x, y position values
  229. // return point BSE value, -1 if there is no image or position is not in the image
  230. int CBSEImg::GetBSEValue(const int a_nX, const int a_nY)
  231. {
  232. // convert position value to point
  233. CPoint pos(a_nX, a_nY);
  234. // calculate and return get position BSE value
  235. return GetBSEValue(pos);
  236. }
  237. void CBSEImg::SetBSEValue(const int a_nX, const int a_nY,int value)
  238. {
  239. // convert position value to point
  240. CPoint pos(a_nX, a_nY);
  241. // set return to -1
  242. long nRet = -1;
  243. // check if there is image
  244. if (m_rectImage.IsRectEmpty() || !m_pnImageData)
  245. {
  246. // NO image, return -1
  247. return ;
  248. }
  249. // check if the point is in the image
  250. if (!m_rectImage.PtInRect(pos))
  251. {
  252. // not in the image, return -1
  253. return ;
  254. }
  255. // note: BSE region should start from 0,0, otherwise need to calculate the position.
  256. m_pnImageData[pos.x + pos.y * m_rectImage.Width()]=value;
  257. // calculate and return get position BSE value
  258. //GetBSEValue(pos);
  259. }
  260. int CBSEImg::GetValueDirect(const CPoint& a_position) const
  261. {
  262. return (0 <= a_position.x) && (0 <= a_position.y) && (a_position.x < m_rectImage.Width()) && (a_position.y < m_rectImage.Height())
  263. ? m_pnImageData[a_position.x + a_position.y * m_rectImage.Width()]
  264. : 0;
  265. }
  266. // check if the image contains any given gray level pixel
  267. bool CBSEImg::DoesContainPixelValue(int inValue, int a_nPixel /*= 1*/) const
  268. {
  269. int pixelCount = m_rectImage.Width() * m_rectImage.Height();
  270. int nFindPixel = 0;
  271. for (int i = 0; i < pixelCount; ++i)
  272. {
  273. if (m_pnImageData[i] == inValue)
  274. {
  275. // find a same gray level pixel
  276. ++nFindPixel;
  277. if (nFindPixel >= a_nPixel)
  278. {
  279. return true;
  280. }
  281. }
  282. }
  283. return false;
  284. }
  285. // calculate image area
  286. // return image area, return 0 if there no image
  287. DWORD CBSEImg::CalArea()
  288. {
  289. // set return to 0
  290. DWORD nRet = 0;
  291. // check image data
  292. if (!m_pnImageData)
  293. {
  294. // return 0 if there no image
  295. return nRet;
  296. }
  297. // calculate image area
  298. BYTE* pData = m_pnImageData;
  299. for (long i = 0; i < m_rectImage.Width(); ++i)
  300. {
  301. for (long j = 0; j < m_rectImage.Height(); ++j)
  302. {
  303. if (*pData != 0)
  304. {
  305. ++nRet;
  306. }
  307. ++pData;
  308. }
  309. }
  310. // return calculated area value
  311. return nRet;
  312. }
  313. // protected
  314. // cleanup
  315. void CBSEImg::Cleanup()
  316. {
  317. // clean image data
  318. if (m_pnImageData)
  319. {
  320. delete m_pnImageData;
  321. m_pnImageData = NULL;
  322. }
  323. }
  324. // initialization
  325. void CBSEImg::Init()
  326. {
  327. m_rectImage.SetRectEmpty();
  328. InitChartData();
  329. }
  330. // duplication
  331. void CBSEImg::Duplicate(const CBSEImg& a_oSource)
  332. {
  333. // initialization
  334. Init();
  335. // copy data
  336. SetImageRect(a_oSource.GetImageRect());
  337. auto r = a_oSource.GetImageRect();
  338. SetImageData(a_oSource.GetImageDataPointer(),r.Width(),r.Height());
  339. }
  340. }