BSEImg.cpp 7.9 KB

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