GainMatrix.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // GainMatrix.cpp: implementation of the CGainMatrix class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "GainMatrix.h"
  6. #include "String.Format.tlh"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. CGainMatrix* CGainMatrix::m_instance = NULL;
  13. #define FULL_IMG_WIDTH 3072L
  14. #define FULL_IMG_HEIGHT 2560L
  15. const int nGAINMIN = 65535;
  16. //////////////////////////////////////////////////////////////////////
  17. // Construction/Destruction
  18. //////////////////////////////////////////////////////////////////////
  19. CGainMatrix::CGainMatrix(int width, int height, int woffset, int hoffset, int refrencenum, WORD maxpv)
  20. :m_nHeight(height), m_nWidth(width), m_nHOffset(hoffset), m_nWOffset(woffset), m_nRefrenceNum(refrencenum), m_nMaxPV(maxpv)
  21. {
  22. int i = 0;
  23. m_wGainBuffer = new WORD * [m_nRefrenceNum];
  24. memset(m_wGainBuffer, 0, m_nRefrenceNum * sizeof(WORD));
  25. for (i = 0; i < m_nRefrenceNum; i++)
  26. {
  27. m_wGainBuffer[i] = NULL;
  28. }
  29. m_nAverage = new int[m_nRefrenceNum];
  30. for (i = 0; i < m_nRefrenceNum; i++)
  31. {
  32. m_nAverage[i] = 0;
  33. }
  34. m_nRefrenceCount = 0;
  35. w_gOffset = 50;
  36. m_nAverageCts = 0;
  37. m_nCurRefIndex = -1;
  38. }
  39. CGainMatrix::~CGainMatrix()
  40. {
  41. if (m_wGainBuffer != NULL)
  42. {
  43. for (int i = 0; i < m_nRefrenceNum; i++)
  44. {
  45. if (m_wGainBuffer[i] != NULL)
  46. {
  47. delete[] m_wGainBuffer[i];
  48. }
  49. }
  50. delete[] m_wGainBuffer;
  51. }
  52. m_wGainBuffer = NULL;
  53. if (m_nAverage)
  54. {
  55. delete[] m_nAverage;
  56. m_nAverage = NULL;
  57. }
  58. }
  59. CGainMatrix* CGainMatrix::Instance()
  60. {
  61. if (m_instance == NULL) {
  62. m_instance = new CGainMatrix(FULL_IMG_WIDTH, FULL_IMG_HEIGHT, 0, 0, 1, 16383);
  63. }
  64. return m_instance;
  65. }
  66. void CGainMatrix::ApplyGainMap(WORD* wImage)
  67. {
  68. // 改成3幅图: 分别是500,1000,1500;
  69. DWORD temp;
  70. int i, j, k, offset;
  71. for (i = 0; i < m_nRefrenceNum; i++)
  72. {
  73. if (m_wGainBuffer[i] == NULL)
  74. {
  75. return;
  76. }
  77. }
  78. for (offset = 0; offset < m_nWidth * m_nHOffset; offset++)
  79. {
  80. wImage[offset] = m_nMaxPV;
  81. }
  82. for (i = m_nHOffset; i < m_nHeight - m_nHOffset; i++)
  83. {
  84. offset = i * m_nWidth;
  85. for (j = 0; j < m_nWOffset; j++)
  86. {
  87. wImage[offset] = m_nMaxPV;
  88. offset++;
  89. }
  90. for (; j < m_nWidth - m_nWOffset; j++)
  91. {
  92. if (m_nRefrenceNum == 1)
  93. {
  94. if (*(m_wGainBuffer[0] + i * m_nWidth + j) == 0)
  95. {
  96. offset++;
  97. continue;
  98. }
  99. temp = *(wImage + i * m_nWidth + j) * m_nAverage[0] / (*(m_wGainBuffer[0] + i * m_nWidth + j)) + w_gOffset;
  100. }
  101. else
  102. {
  103. if (*(wImage + i * m_nWidth + j) < *(m_wGainBuffer[0] + i * m_nWidth + j))
  104. {
  105. if (*(m_wGainBuffer[0] + i * m_nWidth + j) == 0)
  106. {
  107. offset++;
  108. continue;
  109. }
  110. temp = *(wImage + i * m_nWidth + j) * m_nAverage[0] / (*(m_wGainBuffer[0] + i * m_nWidth + j)) + w_gOffset;
  111. }
  112. else
  113. {
  114. for (k = 0; k < m_nRefrenceNum - 1; k++)
  115. {
  116. if (*(wImage + i * m_nWidth + j) >= *(m_wGainBuffer[k] + i * m_nWidth + j) && *(wImage + i * m_nWidth + j) < *(m_wGainBuffer[k + 1] + i * m_nWidth + j))
  117. break;
  118. }
  119. if (k == m_nRefrenceNum - 1) k = max(0, (k - 1));
  120. if ((k + 1) <= (m_nRefrenceNum - 1))
  121. {
  122. if ((*(m_wGainBuffer[k + 1] + i * m_nWidth + j) - *(m_wGainBuffer[k] + i * m_nWidth + j)) == 0)
  123. {
  124. offset++;
  125. continue;
  126. }
  127. }
  128. temp = m_nAverage[k] + (*(wImage + i * m_nWidth + j) - *(m_wGainBuffer[k] + i * m_nWidth + j)) * (m_nAverage[k + 1] - m_nAverage[k]) / (*(m_wGainBuffer[k + 1] + i * m_nWidth + j) - *(m_wGainBuffer[k] + i * m_nWidth + j)) + w_gOffset;
  129. }
  130. }
  131. if (temp > m_nMaxPV)
  132. *(wImage + i * m_nWidth + j) = m_nMaxPV;
  133. else
  134. *(wImage + i * m_nWidth + j) = WORD(temp);
  135. offset++;
  136. }
  137. for (; j < m_nWidth; j++)
  138. {
  139. wImage[offset] = m_nMaxPV;
  140. offset++;
  141. }
  142. }
  143. for (offset = i * m_nWidth; offset < m_nWidth * m_nHeight; offset++)
  144. {
  145. wImage[offset] = m_nMaxPV;
  146. }
  147. }
  148. bool CGainMatrix::LoadGainMap(void)
  149. {
  150. if (m_nRefrenceCount >= m_nRefrenceNum)
  151. {
  152. return false;
  153. }
  154. if (m_wGainBuffer[m_nRefrenceCount] != NULL)
  155. {
  156. delete[] m_wGainBuffer[m_nRefrenceCount];
  157. }
  158. m_wGainBuffer[m_nRefrenceCount] = new WORD[m_nHeight * m_nWidth];
  159. if (m_wGainBuffer[m_nRefrenceCount] == NULL)
  160. {
  161. return false;
  162. }
  163. m_nRefrenceCount++;
  164. return true;
  165. }
  166. bool CGainMatrix::LoadGainMap(const char* filename)
  167. {
  168. if (m_nRefrenceCount >= m_nRefrenceNum)
  169. {
  170. return false;
  171. }
  172. if (m_wGainBuffer[m_nRefrenceCount] != NULL) {
  173. delete[] m_wGainBuffer[m_nRefrenceCount];
  174. }
  175. m_wGainBuffer[m_nRefrenceCount] = new WORD[m_nHeight * m_nWidth];
  176. if (m_wGainBuffer[m_nRefrenceCount] == NULL) return false;
  177. // Check if the file exist or not
  178. FILE* fp = fopen(filename, "rb");
  179. if (!fp)
  180. {
  181. return false;
  182. }
  183. if (fread(m_wGainBuffer[m_nRefrenceCount], 1, (UINT)(m_nHeight * m_nWidth * sizeof(WORD)), fp) != (UINT)(m_nHeight * m_nWidth * sizeof(WORD)))
  184. {
  185. fclose(fp);
  186. return false;
  187. }
  188. fclose(fp);
  189. //calculate avg pv
  190. ULONGLONG dwSum = 0;
  191. DWORD dwIndex = 0;
  192. int i, j;
  193. DWORD dwCts = 0;
  194. for (i = m_nHOffset; i < m_nHeight - m_nHOffset; i += 8)
  195. {
  196. dwIndex = i * m_nWidth;
  197. for (j = m_nWOffset; j < m_nWidth - m_nWOffset; j += 8)
  198. {
  199. dwSum += *(m_wGainBuffer[m_nRefrenceCount] + dwIndex + j);
  200. dwCts++;
  201. }
  202. }
  203. m_nAverage[m_nRefrenceCount] = dwSum / dwCts;
  204. char szOut[128];
  205. sprintf(szOut, "LoadGainMap m_nAverage[%i]=%i\n", m_nRefrenceCount, m_nAverage[m_nRefrenceCount]);
  206. OutputDebugStringA(szOut);
  207. m_nRefrenceCount++;
  208. if (m_nRefrenceCount == m_nRefrenceNum)
  209. {
  210. PlaceRefrenceInOrder();
  211. }
  212. return true;
  213. }
  214. bool CGainMatrix::LoadGainMap(WORD* wImage)
  215. {
  216. if (m_nRefrenceCount >= m_nRefrenceNum)
  217. {
  218. return false;
  219. }
  220. if (wImage == NULL)
  221. {
  222. return false;
  223. }
  224. if (m_wGainBuffer[m_nRefrenceCount] == NULL) {
  225. m_wGainBuffer[m_nRefrenceCount] = new WORD[m_nHeight * m_nWidth];
  226. }
  227. if (m_wGainBuffer[m_nRefrenceCount] == NULL) return false;
  228. // Check if the file exist or not
  229. memcpy(m_wGainBuffer[m_nRefrenceCount], wImage, (UINT)(m_nHeight * m_nWidth * sizeof(WORD)));
  230. //calculate avg pv
  231. LONGLONG dwSum = 0;//add by song 2014-9-27 64位类型,防止越界
  232. DWORD dwIndex = 0;
  233. int i, j;
  234. DWORD dwCts = 0;
  235. for (i = m_nHOffset; i < m_nHeight - m_nHOffset; i += 8) {
  236. dwIndex = i * m_nWidth;
  237. for (j = m_nWOffset; j < m_nWidth - m_nWOffset; j += 8) {
  238. dwSum += *(m_wGainBuffer[m_nRefrenceCount] + dwIndex + j);
  239. dwCts++;
  240. }
  241. }
  242. m_nAverage[m_nRefrenceCount] = dwSum / dwCts;
  243. m_nRefrenceCount++;
  244. if (m_nRefrenceCount == m_nRefrenceNum)
  245. {
  246. PlaceRefrenceInOrder();
  247. }
  248. return true;
  249. }
  250. void CGainMatrix::PlaceRefrenceInOrder()
  251. {
  252. WORD nMin = nGAINMIN;
  253. WORD nMin_Index = 0;
  254. WORD nTemp = 0;
  255. WORD* nTempPtr = NULL;
  256. int i, j;
  257. for (i = 0; i < m_nRefrenceNum; i++)
  258. {
  259. if (m_wGainBuffer[i] == NULL)
  260. {
  261. return;
  262. }
  263. }
  264. for (i = 0; i < m_nRefrenceNum; i++)
  265. {
  266. nMin = nGAINMIN;
  267. nMin_Index = 0;
  268. for (j = i; j < m_nRefrenceNum; j++)
  269. {
  270. if (m_nAverage[j] <= nMin)
  271. {
  272. nMin = m_nAverage[j];
  273. nMin_Index = j;
  274. }
  275. }
  276. nTemp = m_nAverage[i];
  277. m_nAverage[i] = m_nAverage[nMin_Index];
  278. m_nAverage[nMin_Index] = nTemp;
  279. nTempPtr = m_wGainBuffer[i];
  280. m_wGainBuffer[i] = m_wGainBuffer[nMin_Index];
  281. m_wGainBuffer[nMin_Index] = nTempPtr;
  282. }
  283. }
  284. bool CGainMatrix::AverageGainMap(WORD* wImage, int nRefIndex)
  285. {
  286. int dwCts = m_nWidth * m_nHeight;
  287. if (NULL == m_wGainBuffer[nRefIndex])
  288. {
  289. return false;
  290. }
  291. if (m_nCurRefIndex != nRefIndex)
  292. {
  293. m_nCurRefIndex = nRefIndex;
  294. m_nAverageCts = 0;
  295. memcpy(m_wGainBuffer[nRefIndex], wImage, dwCts * sizeof(WORD));
  296. m_nAverageCts++;
  297. }
  298. else
  299. {
  300. for (int i = 0; i < dwCts; i++)
  301. {
  302. m_wGainBuffer[nRefIndex][i] = WORD(float(m_wGainBuffer[nRefIndex][i]) * m_nAverageCts / (m_nAverageCts + 1) + float(wImage[i]) / (m_nAverageCts + 1));
  303. }
  304. m_nAverageCts++;
  305. }
  306. return true;
  307. }
  308. bool CGainMatrix::AverageGainMap(WORD* wImage, int refenceindex, bool bStart)
  309. {
  310. int dwCts = m_nWidth * m_nHeight;
  311. if (m_wGainBuffer[refenceindex] == NULL)
  312. return false;
  313. if (bStart)
  314. {
  315. m_nAverageCts = 0;
  316. memcpy(m_wGainBuffer[refenceindex], wImage, dwCts * sizeof(WORD));
  317. m_nAverageCts++;
  318. return true;
  319. }
  320. else
  321. {
  322. //average offset
  323. for (int i = 0; i < dwCts; i++)
  324. {
  325. m_wGainBuffer[refenceindex][i] = WORD(float(m_wGainBuffer[refenceindex][i]) * m_nAverageCts / (m_nAverageCts + 1) + float(wImage[i]) / (m_nAverageCts + 1));
  326. }
  327. m_nAverageCts++;
  328. return true;
  329. }
  330. return true;
  331. }
  332. bool CGainMatrix::StoreGainMap(const char* filename, int refenceindex)
  333. {
  334. if (refenceindex > m_nRefrenceNum) return false;
  335. if (m_wGainBuffer[refenceindex] == NULL)
  336. {
  337. return false;
  338. }
  339. //do it at here
  340. //calculate avg pv
  341. ULONGLONG dwSum = 0;
  342. DWORD dwIndex = 0;
  343. int i, j;
  344. DWORD dwCts = 0;
  345. for (i = m_nHOffset; i < m_nHeight - m_nHOffset; i += 8)
  346. {
  347. dwIndex = i * m_nWidth;
  348. for (j = m_nWOffset; j < m_nWidth - m_nWOffset; j += 8)
  349. {
  350. dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
  351. dwCts++;
  352. }
  353. }
  354. m_nAverage[refenceindex] = dwSum / dwCts;
  355. dwSum = 0;
  356. dwIndex = 0;
  357. i = 0;
  358. j = 0;
  359. dwCts = 0;
  360. for (i = 0; i < m_nHeight; i += 8)
  361. {
  362. dwIndex = i * m_nWidth;
  363. for (j = 0; j < m_nWidth; j += 8)
  364. {
  365. dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
  366. dwCts++;
  367. }
  368. }
  369. int nAverage = dwSum / dwCts;
  370. try
  371. {
  372. FILE* fp;
  373. fp = fopen(filename, "wb");
  374. if (fwrite(m_wGainBuffer[refenceindex], 1, m_nHeight * m_nWidth * sizeof(WORD), fp) != m_nHeight * m_nWidth * sizeof(WORD))
  375. {
  376. ASSERT("123456");
  377. }
  378. fclose(fp);
  379. }
  380. catch (...)
  381. {
  382. return false;
  383. }
  384. return true;
  385. }