BSEImg.cpp 7.2 KB

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