123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- // GainMatrix.cpp: implementation of the CGainMatrix class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "GainMatrix.h"
- //#include "afx.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- CGainMatrix* CGainMatrix::m_instance = NULL;
- #define FULL_IMG_WIDTH 3072L
- #define FULL_IMG_HEIGHT 2560L
- const int nGAINMIN = 65535;
- //extern unsigned w_gOffset; // = 50;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CGainMatrix::CGainMatrix(int width, int height,int woffset, int hoffset,int refrencenum,WORD maxpv)
- :m_nHeight(height), m_nWidth(width), m_nHOffset(hoffset), m_nWOffset(woffset),m_nRefrenceNum(refrencenum),m_nMaxPV(maxpv)
- {
- int i = 0;
- m_wGainBuffer = new WORD*[m_nRefrenceNum];
- memset(m_wGainBuffer, 0, m_nRefrenceNum * sizeof(WORD));
- for(i = 0; i< m_nRefrenceNum;i++)
- {
- m_wGainBuffer[i]=NULL;
- }
- m_nAverage = new int[m_nRefrenceNum];
- for(i = 0; i< m_nRefrenceNum;i++)
- {
- m_nAverage[i] = 0;
- }
-
- m_nRefrenceCount = 0;
- w_gOffset = 50;
- m_nAverageCts = 0;
- m_nCurRefIndex = -1;
- }
- CGainMatrix::~CGainMatrix()
- {
- if(m_wGainBuffer != NULL)
- {
- for(int i = 0; i< m_nRefrenceNum;i++)
- {
- if(m_wGainBuffer[i] != NULL)
- {
- delete[] m_wGainBuffer[i];
- }
- }
- delete[] m_wGainBuffer;
- }
- m_wGainBuffer = NULL;
- //if(m_wTempGainBuffer != NULL)
- //{
- // for(int i = 0; i< m_nRefrenceNum;i++)
- // {
- // if(m_wTempGainBuffer[i] != NULL)
- // {
- // delete[] m_wTempGainBuffer[i];
- // }
- // }
- // delete[] m_wTempGainBuffer;
- //}
- //m_wTempGainBuffer = NULL;
- if(m_nAverage)
- {
- delete[] m_nAverage;
- m_nAverage = NULL;
- }
- }
- CGainMatrix* CGainMatrix::Instance()
- {
- if(m_instance == NULL) {
- m_instance = new CGainMatrix(FULL_IMG_WIDTH, FULL_IMG_HEIGHT, 0,0,1,16383);
- }
- return m_instance;
- }
- void CGainMatrix::ApplyGainMap(WORD *wImage)
- {
- // 改成3幅图: 分别是500,1000,1500;
- DWORD temp;
- int i,j,k,offset;
- for( i = 0;i <m_nRefrenceNum;i++)
- {
- if(m_wGainBuffer[i] == NULL)
- {
- return;
- }
- }
-
- for(offset = 0; offset < m_nWidth*m_nHOffset; offset++) {
- wImage[offset] = m_nMaxPV;
- }
- for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i++) {
- offset = i*m_nWidth;
- for(j = 0; j<m_nWOffset; j++)
- {
- wImage[offset] = m_nMaxPV;
- offset++;
- }
- for(;j<m_nWidth-m_nWOffset; j++)
- {
-
- if(m_nRefrenceNum == 1)
- {
- if(*(m_wGainBuffer[0]+i*m_nWidth+j)==0)
- {
- offset++;
- continue;
- }
- temp = *(wImage+i*m_nWidth+j)*m_nAverage[0]/(*(m_wGainBuffer[0]+i*m_nWidth+j))+w_gOffset;
- }
- else
- {
- if(*(wImage+i*m_nWidth+j)<*(m_wGainBuffer[0]+i*m_nWidth+j))
- {
- if(*(m_wGainBuffer[0]+i*m_nWidth+j)==0)
- {
- offset++;
- continue;
- }
- temp = *(wImage+i*m_nWidth+j)*m_nAverage[0]/(*(m_wGainBuffer[0]+i*m_nWidth+j))+w_gOffset;
- }
- else
- {
- for(k=0;k<m_nRefrenceNum-1;k++)
- {
- 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))
- break;
- }
- if (k==m_nRefrenceNum-1) k = max(0,(k-1));
- if((k+1) <=(m_nRefrenceNum-1))
- {
- if((*(m_wGainBuffer[k+1]+i*m_nWidth+j)-*(m_wGainBuffer[k]+i*m_nWidth+j))==0)
- {
- offset++;
- continue;
- }
- }
- 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;
- }
- }
- if (temp>m_nMaxPV)
- *(wImage+i*m_nWidth+j) = m_nMaxPV;
- else
- *(wImage+i*m_nWidth+j) = WORD(temp);
- offset++;
- }
- for(;j<m_nWidth; j++)
- {
- wImage[offset] = m_nMaxPV;
- offset++;
- }
- }
- for(offset = i*m_nWidth; offset < m_nWidth*m_nHeight; offset++) {
- wImage[offset] = m_nMaxPV;
- }
- }
- bool CGainMatrix::LoadGainMap(void)
- {
- if(m_nRefrenceCount >= m_nRefrenceNum)
- {
- return false;
- }
- if(m_wGainBuffer[m_nRefrenceCount] != NULL)
- {
- delete [] m_wGainBuffer[m_nRefrenceCount];
- }
- m_wGainBuffer[m_nRefrenceCount] = new WORD [m_nHeight*m_nWidth];
- if(m_wGainBuffer[m_nRefrenceCount] == NULL)
- {
- return false;
- }
- m_nRefrenceCount++;
- return true;
- }
- bool CGainMatrix::LoadGainMap(const char *filename)
- {
- if(m_nRefrenceCount >= m_nRefrenceNum)
- {
- return false;
- }
- if(m_wGainBuffer[m_nRefrenceCount] != NULL) {
- delete [] m_wGainBuffer[m_nRefrenceCount];
- }
- m_wGainBuffer[m_nRefrenceCount] = new WORD [m_nHeight*m_nWidth];
- if(m_wGainBuffer[m_nRefrenceCount] == NULL) return false;
- //
- // Check if the file exist or not
- //
- FILE * fp = fopen (filename, "rb");
- if (! fp)
- {
- return false;
- }
- if(fread( m_wGainBuffer[m_nRefrenceCount], 1,(UINT)(m_nHeight*m_nWidth*sizeof(WORD)),fp) != (UINT)(m_nHeight*m_nWidth*sizeof(WORD)))
- {
- fclose(fp);
- return false;
- }
- fclose(fp);
-
- //calculate avg pv
- ULONGLONG dwSum = 0;
- DWORD dwIndex = 0;
- int i, j;
- DWORD dwCts = 0;
- for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8) {
- dwIndex = i*m_nWidth;
- for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8) {
- dwSum += *(m_wGainBuffer[m_nRefrenceCount] + dwIndex + j);
- dwCts++;
- }
- }
- m_nAverage[m_nRefrenceCount] = dwSum/dwCts;
- char szOut[128];
- sprintf(szOut, "LoadGainMap m_nAverage[%i]=%i\n", m_nRefrenceCount, m_nAverage[m_nRefrenceCount]);
- OutputDebugStringA(szOut);
- m_nRefrenceCount++;
- if(m_nRefrenceCount == m_nRefrenceNum)
- {
- PlaceRefrenceInOrder();
- }
- /*CString strLog;
- strLog.Format("The average pixel value of current node is %d", m_nAverage[m_nRefrenceCount]);
- m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);*/
- return true;
- }
- bool CGainMatrix::LoadGainMap(WORD* wImage)
- {
- if(m_nRefrenceCount >= m_nRefrenceNum)
- {
- return false;
- }
- if(wImage == NULL)
- {
- return false;
- }
- if(m_wGainBuffer[m_nRefrenceCount] == NULL) {
- m_wGainBuffer[m_nRefrenceCount] = new WORD [m_nHeight*m_nWidth];
- }
-
- if(m_wGainBuffer[m_nRefrenceCount] == NULL) return false;
- //
- // Check if the file exist or not
- //
- memcpy(m_wGainBuffer[m_nRefrenceCount],wImage,(UINT)(m_nHeight*m_nWidth*sizeof(WORD)));
-
- //calculate avg pv
- //DWORD dwSum = 0;//delelte by song 2014-9-27 如果像素较多且值很大会越界
- LONGLONG dwSum = 0;//add by song 2014-9-27 64位类型,防止越界
- DWORD dwIndex = 0;
- int i, j;
- DWORD dwCts = 0;
- for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8) {
- dwIndex = i*m_nWidth;
- for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8) {
- dwSum += *(m_wGainBuffer[m_nRefrenceCount] + dwIndex + j);
- dwCts++;
- }
- }
- m_nAverage[m_nRefrenceCount] = dwSum/dwCts;
- m_nRefrenceCount++;
- if(m_nRefrenceCount == m_nRefrenceNum)
- {
- PlaceRefrenceInOrder();
- }
- return true;
- }
- void CGainMatrix::PlaceRefrenceInOrder()
- {
- //code delete 20101214
- //WORD nMin = 16383;
- //code begin 20101214
- WORD nMin = nGAINMIN;
- //code end 20101214
- WORD nMin_Index = 0;
- WORD nTemp = 0;
- WORD* nTempPtr = NULL;
- int i,j;
- for( i = 0;i <m_nRefrenceNum;i++)
- {
- if(m_wGainBuffer[i] == NULL)
- {
- return;
- }
- }
- for(i = 0;i<m_nRefrenceNum;i++)
- {
- //code delete 20101214
- //nMin = 16383;
- //code begin 20101214
- nMin = nGAINMIN;
- //code end 20101214
- nMin_Index = 0;
- for(j = i; j< m_nRefrenceNum; j++)
- {
- if(m_nAverage[j] <= nMin)
- {
- nMin = m_nAverage[j];
- nMin_Index = j;
- }
- }
- nTemp = m_nAverage[i];
- m_nAverage[i] = m_nAverage[nMin_Index];
- m_nAverage[nMin_Index] = nTemp;
- nTempPtr = m_wGainBuffer[i];
- m_wGainBuffer[i] = m_wGainBuffer[nMin_Index];
- m_wGainBuffer[nMin_Index] = nTempPtr;
- }
- }
- bool CGainMatrix::AverageGainMap(WORD* wImage, int nRefIndex)
- {
- int dwCts = m_nWidth*m_nHeight;
- if(NULL == m_wGainBuffer[nRefIndex])
- {
- return false;
- }
- if (m_nCurRefIndex != nRefIndex)
- {
- m_nCurRefIndex = nRefIndex;
- m_nAverageCts = 0;
- memcpy(m_wGainBuffer[nRefIndex], wImage, dwCts*sizeof(WORD));
- m_nAverageCts ++;
- }
- else
- {
- for(int i = 0; i<dwCts; i++)
- {
- m_wGainBuffer[nRefIndex][i] = WORD(float(m_wGainBuffer[nRefIndex][i])*m_nAverageCts/(m_nAverageCts+1) + float(wImage[i])/(m_nAverageCts+1));
- }
- m_nAverageCts++;
- }
- return true;
- }
- bool CGainMatrix::AverageGainMap(WORD* wImage, int refenceindex, bool bStart)
- {
- int dwCts = m_nWidth*m_nHeight;
- if(m_wGainBuffer[refenceindex] == NULL)
- return false;
- if(bStart)
- {
- m_nAverageCts = 0;
-
- memcpy(m_wGainBuffer[refenceindex], wImage, dwCts*sizeof(WORD));
- m_nAverageCts ++;
- return true;
- }
- else
- {
- //average offset
- for(int i = 0; i < dwCts; i++)
- {
- m_wGainBuffer[refenceindex][i] = WORD(float(m_wGainBuffer[refenceindex][i])*m_nAverageCts/(m_nAverageCts+1) + float(wImage[i])/(m_nAverageCts+1));
- }
- m_nAverageCts++;
- return true;
- }
- /*if (refenceindex >= m_nRefrenceNum || refenceindex < 0)
- {
- m_pLogFile->WriteLog("The reference index is out of range", LOG_INFORMATION, LOG_DEBUG, true);
- return false;
- }
- int dwCts = m_nWidth*m_nHeight;
- if(m_wTempGainBuffer[refenceindex] == NULL)
- return false;
- memset(m_wTempGainBuffer[refenceindex], 0, m_nHeight * m_nWidth * sizeof(WORD));
- if(bStart)
- {
- m_nAverageCts = 0;
-
- memcpy(m_wTempGainBuffer[refenceindex], wImage, dwCts*sizeof(WORD));
- m_nAverageCts ++;
- return true;
- }
- else
- {
- //average offset
- for(int i = 0; i < dwCts; i++)
- {
- m_wTempGainBuffer[refenceindex][i] = WORD(float((m_wTempGainBuffer[refenceindex][i])*m_nAverageCts/(m_nAverageCts+1)) + float(wImage[i])/(m_nAverageCts+1));
- }
- m_nAverageCts++;
- //do it at here //test
- //calculate avg pv
- DWORD dwSum = 0;
- DWORD dwIndex = 0;
- int i, j;
- DWORD dwCts = 0;
- for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8)
- {
- dwIndex = i*m_nWidth;
- for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8)
- {
- dwSum += *(m_wTempGainBuffer[refenceindex] + dwIndex + j);
- dwCts++;
- }
- }
- int nAverage = dwSum/dwCts;
- CString strLog;
- strLog.Format("The average pixel value of current node is %d", nAverage);
- m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);
- //test end
- return true;
- }*/
- return true;
- }
- //replace the old gain map, do it after calibration
- /*bool CGainMatrix::ApplyGainCalibration()
- {
- //replace the old gain map
- for (int refenceindex = 0; refenceindex < m_nRefrenceNum; refenceindex++)
- {
- if(m_wTempGainBuffer[refenceindex] == NULL || m_wGainBuffer[refenceindex] == NULL)
- {
- AfxMessageBox("No gain buffer exists");
- return false;
- }
- memcpy(m_wGainBuffer[refenceindex], m_wTempGainBuffer[refenceindex], m_nHeight*m_nWidth*sizeof(WORD));
-
- delete [] m_wTempGainBuffer[refenceindex];
- //do it at here
- //calculate avg pv
- DWORD dwSum = 0;
- DWORD dwIndex = 0;
- int i, j;
- DWORD dwCts = 0;
- for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8)
- {
- dwIndex = i*m_nWidth;
- for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8)
- {
- dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
- dwCts++;
- }
- }
- m_nAverage[refenceindex] = dwSum/dwCts;
- CString strLog;
- strLog.Format("The average pixel value of current node is %d", m_nAverage[refenceindex]);
- m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);
- }
- return true;
- }*/
- bool CGainMatrix::StoreGainMap(const char* filename, int refenceindex)
- {
- if (refenceindex>m_nRefrenceNum) return false;
-
- if(m_wGainBuffer[refenceindex] == NULL) {
-
- //AfxMessageBox("No gain buffer exists");
- return false;
- }
- //do it at here
- //calculate avg pv
- ULONGLONG dwSum = 0;
- DWORD dwIndex = 0;
- int i, j;
- DWORD dwCts = 0;
- for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8)
- {
- dwIndex = i*m_nWidth;
- for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8)
- {
- dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
- dwCts++;
- }
- }
- m_nAverage[refenceindex] = dwSum/dwCts;
- //CString strLog;
- //strLog.Format("The average pixel value of current node 3008 x 3072 is %d", m_nAverage[refenceindex]);
- //m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);
- {
- ULONGLONG dwSum = 0;
- DWORD dwIndex = 0;
- int i, j;
- DWORD dwCts = 0;
- for(i = 0; i < m_nHeight; i += 8)
- {
- dwIndex = i*m_nWidth;
- for(j = 0; j < m_nWidth; j+= 8)
- {
- dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
- dwCts++;
- }
- }
- int nAverage = dwSum/dwCts;
- /*CString strLog;
- strLog.Format("The average pixel value of current node 3072 x 3072is %d", nAverage);
- m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);*/
- }
- try {
- FILE *fp;
- fp = fopen(filename, "wb+");
- if (fwrite(m_wGainBuffer[refenceindex],1,m_nHeight*m_nWidth*sizeof(WORD),fp)!=m_nHeight*m_nWidth*sizeof(WORD))
- {
- ASSERT("123456");
- }
- fclose(fp);
- //CFile file( filename, CFile::modeWrite|CFile::modeCreate);
- //file.Write(m_wGainBuffer[refenceindex], (UINT)(m_nHeight*m_nWidth*sizeof(WORD)));
- //file.Close();
- //return true;
- }
- catch (.../*CFileException &e*/)
- {
- #ifdef _DEBUG
- // afxDump << "File could not be opened " << e->m_cause << "\n";
- #endif
- // (void)e;
- return false;
- }
- return true;
- }
|