BSEImg.cpp 7.8 KB

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