GuessWidth.cpp
上传用户:gzboli
上传日期:2013-04-10
资源大小:471k
文件大小:9k
- // WidthCor.cpp: implementation of the CGuessWidth class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "GuessWidth.h"
- #include <math.h>
- //#include <LIMITS.H>
- #include <FLOAT.H>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CGuessWidth::CGuessWidth()
- {
- // m_iMinWidth=100;
- // m_iMaxWidth=6000;
- // m_iWidth=m_iHeight=-1;
- }
- CGuessWidth::~CGuessWidth()
- {
- }
- double CGuessWidth::WidthDetect(DWORD &iWidth,DWORD &iHeight,LPCTSTR lpszFile, short iBytes, DWORD wHeaderSize)
- {
- ASSERT(NULL != lpszFile);
- char *pExt=strrchr(lpszFile,'.');
- char cExt[10];
- strcpy(cExt,++pExt);
- _strlwr(cExt);
- if(strcmp(cExt,"cpx")==0)
- {
- // return FindWidth(iWidth,iHeight,lpszFile,(short)sizeof(short));
- return CPXDetect(iWidth, iHeight, lpszFile);
- }
- else if(strcmp(cExt,"raw")==0)
- {
- return FindWidth(iWidth, iHeight, lpszFile, (BYTE)sizeof(BYTE));
- }
- else if(strcmp(cExt,"int")==0)
- {
- return FindWidth(iWidth, iHeight, lpszFile, (float)sizeof(float), wHeaderSize);
- }
- else
- {
- return -1.0;
- }
- }
- double CGuessWidth::CPXDetect(DWORD &iWidth,DWORD &iHeight,LPCTSTR lpszFile)
- {
- CFile f;
- if(!f.Open(lpszFile,CFile::modeRead | CFile::shareExclusive))
- {
- return -1.0;
- }
- DWORD filesize=f.GetLength();
- iWidth = (DWORD)sqrt(filesize);
-
- DWORD iMaxLength, iMinLength;
- iMaxLength = (filesize/100) > (iWidth*100) ? iWidth*100 : filesize/100;
- iMinLength = iMaxLength / 100;
-
- HANDLE hHeap=NULL;
- short *pByte1=NULL;
- short *pByte2=NULL;
- hHeap=HeapCreate(HEAP_NO_SERIALIZE,iMaxLength*sizeof(short)*4,0);
- if(hHeap==NULL)
- {
- return -1.0;
- }
- HeapLock(hHeap);
- pByte1=(short*)HeapAlloc(hHeap , HEAP_NO_SERIALIZE , iMaxLength*sizeof(short)*2);
- if(pByte1==NULL)
- {
- HeapUnlock(hHeap);
- HeapDestroy(hHeap);
- return -1.0;
- }
- pByte2=(short*)HeapAlloc(hHeap , HEAP_NO_SERIALIZE , iMaxLength*sizeof(short)*2);
- if(pByte2==NULL)
- {
- HeapFree(hHeap,HEAP_NO_SERIALIZE,pByte1);
- HeapUnlock(hHeap);
- HeapDestroy(hHeap);
- return -1.0;
- }
- DWORD wMax=iWidth;
- double dMax=-1.0;
- double dTemp;
- int k;
- short iReal,iVirtual;
- double dGray1,dGray2;
- DWORD i, j;
- double iG1G2,iG12,iG22;
- DWORD wRowSize, iSkip;;
- for(i=iWidth; i<iMaxLength; i++)
- {
- wRowSize = i * sizeof(short) * 2;
- if((filesize % wRowSize) == 0)
- {
- iSkip = filesize/wRowSize/6;
- if(iSkip == 0) iSkip = 1;
- iG1G2=iG12=iG22=0.0;
- for(k=1;k<6;k++)
- {
- f.Seek(k * iSkip * wRowSize, CFile::begin);
- f.Read(pByte1, wRowSize);
- f.Read(pByte2, wRowSize);
- for(j=0;j<i;j++)
- {
- iReal = pByte1[2*j];
- iVirtual = pByte1[2*j+1];
- dGray1 = (sqrt((double)iReal*iReal + (double)iVirtual*iVirtual)/i);
- iReal = pByte2[2*j];
- iVirtual = pByte2[2*j+1];
- dGray2 = (sqrt((double)iReal*iReal + (double)iVirtual*iVirtual)/i);
- iG1G2 += dGray1*dGray2;
- iG12 += dGray1*dGray1;
- iG22 += dGray2*dGray2;
- }
-
- if((dTemp = iG1G2/iG12*iG1G2/iG22) > dMax)
- {
- dMax=dTemp;
- wMax=i;
- }
- }
- }
- }
- for(i=iWidth-1; i>iMinLength; i--)
- {
- wRowSize = i * sizeof(short) * 2;
- if((filesize % wRowSize) == 0)
- {
- iSkip = filesize/wRowSize/6;
- if(iSkip == 0) iSkip = 1;
- iG1G2=iG12=iG22=0.0;
- for(k=1;k<6;k++)
- {
- f.Seek(k * iSkip * wRowSize, CFile::begin);
- f.Read(pByte1, wRowSize);
- f.Read(pByte2, wRowSize);
- for(j=0;j<i;j++)
- {
- iReal = pByte1[2*j];
- iVirtual = pByte1[2*j+1];
- dGray1 = (sqrt((double)iReal*iReal + (double)iVirtual*iVirtual)/i);
- iReal = pByte2[2*j];
- iVirtual = pByte2[2*j+1];
- dGray2 = (sqrt((double)iReal*iReal + (double)iVirtual*iVirtual)/i);
- iG1G2 += dGray1*dGray2;
- iG12 += dGray1*dGray1;
- iG22 += dGray2*dGray2;
- }
-
- if((dTemp = iG1G2/iG12*iG1G2/iG22) > dMax)
- {
- dMax=dTemp;
- wMax=i;
- }
- }
- }
- }
- f.Close();
- try
- {
- HeapFree(hHeap,HEAP_GENERATE_EXCEPTIONS,pByte1);
- HeapFree(hHeap,HEAP_GENERATE_EXCEPTIONS,pByte2);
- HeapUnlock(hHeap);
- HeapDestroy(hHeap);
- }
- catch(CMemoryException *e)
- {
- char msg[256];
- e->GetErrorMessage(msg,255);
- e->ReportError();
- e->Delete();
- return -0.1;
- }
- iWidth=wMax;
- iHeight=filesize/iWidth/sizeof(DWORD);
- return dMax;
- }
- template <class T> double CGuessWidth::FindWidth
- (DWORD &iWidth,DWORD &iHeight,LPCTSTR lpszFile,T iDepth, DWORD wHeaderSize)
- //double CGuessWidth::FindWidth
- // (DWORD &iWidth,DWORD &iHeight,LPCTSTR lpszFile,BYTE iDepth)
- {
- CFile f;
- if(!f.Open(lpszFile,CFile::modeRead | CFile::shareExclusive))
- {
- return -1.0;
- }
- const DWORD filesize = f.GetLength() - wHeaderSize;
- iWidth = (DWORD)sqrt(filesize);
-
- DWORD iMaxLength, iMinLength;
- iMaxLength = (filesize/100) > (iWidth*100) ? iWidth*100 : filesize/100;
- iMinLength = iMaxLength / 100;
-
- HANDLE hHeap=NULL;
- T *pByte1=NULL;
- T *pByte2=NULL;
- HGLOBAL hByte1 = NULL;
- HGLOBAL hByte2 = NULL;
- hByte1 = GlobalAlloc(GHND, iMaxLength * sizeof(T));
- if(hByte1==NULL)
- {
- return -1.0;
- }
- hByte2 = GlobalAlloc(GHND, iMaxLength * sizeof(T));
- if(hByte2==NULL)
- {
- GlobalFree(hByte1);
- return -1.0;
- }
- pByte1 = (T*)GlobalLock(hByte1);
- if(pByte1 == NULL)
- {
- GlobalFree(hByte1);
- GlobalFree(hByte2);
- return -1.0;
- }
- pByte2 = (T*)GlobalLock(hByte2);
- if(pByte2 == NULL)
- {
- GlobalUnlock(hByte1);
- GlobalFree(hByte1);
- GlobalFree(hByte2);
- return -1.0;
- }
-
- DWORD wMax = iWidth;
- double dMax = -FLT_MAX;
- double dTemp;
- int k;
- DWORD i, j;
- double iG1G2, iG12, iG22;
- int iSkip;
- for(i = iWidth; i < iMaxLength; i++)
- {
- if((filesize % i) == 0)
- {
- iSkip = filesize/i/6;
- if(iSkip == 0) iSkip = 1;
- iG1G2=iG12=iG22=0.0;
- for(k=1;k<6;k++)
- {
- f.Seek(k * iSkip * i * sizeof(T), CFile::begin);
- f.Read(pByte1, i * sizeof(T));
- f.Read(pByte2, i * sizeof(T));
- for(j=0;j<i;j++)
- {
- iG1G2 += ((double)pByte1[j]*pByte2[j] / i);
- iG12 += ((double)pByte1[j]*pByte1[j] / i);
- iG22 += ((double)pByte2[j]*pByte2[j] / i);
- }
-
- }
- if((dTemp = iG1G2/iG12*iG1G2/iG22) > dMax)
- {
- dMax=dTemp;
- wMax=i;
- }
- }
- }
- for(i=iWidth-1; i>iMinLength; i--)
- {
- if((filesize % i) == 0)
- {
- iSkip = filesize/i/6;
- if(iSkip == 0) iSkip = 1;
- iG1G2=iG12=iG22=0.0;
- for(k=1;k<6;k++)
- {
- f.Seek(k * iSkip * i * sizeof(T), CFile::begin);
- f.Read(pByte1, i * sizeof(T));
- f.Read(pByte2, i * sizeof(T));
-
- for(j=0;j<i;j++)
- {
- iG1G2 += ((double)pByte1[j]*pByte2[j] / i);
- iG12 += ((double)pByte1[j]*pByte1[j] / i);
- iG22 += ((double)pByte2[j]*pByte2[j] / i);
- }
-
- }
- if((dTemp = iG1G2/iG12*iG1G2/iG22) > dMax)
- {
- dMax=dTemp;
- wMax=i;
- }
- }
- }
- f.Close();
-
- GlobalUnlock(hByte1);
- GlobalFree(hByte1);
- GlobalUnlock(hByte2);
- GlobalFree(hByte2);
-
- iWidth=wMax;
- iHeight=filesize / iWidth / int(iDepth + 0.5);
- return dMax;
- }
- /*
- double CGuessWidth::FindWidth(DWORD &iWidth,DWORD &iHeight,LPCTSTR lpszFile,BYTE iDepth)
- {
- CFile f;
- if(!f.Open(lpszFile,CFile::modeRead | CFile::shareExclusive))
- {
- return -1.0;
- }
- DWORD filesize=f.GetLength();
- if(m_iMaxWidth > filesize)
- {
- m_iMaxWidth = filesize;
- }
- int iCount=filesize/m_iMaxWidth/10/iDepth;
- if(iCount==0)
- {
- iCount=1;
- }
- HANDLE hHeap=NULL;
- BYTE *pByte1=NULL;
- BYTE *pByte2=NULL;
- hHeap=HeapCreate(HEAP_NO_SERIALIZE,m_iMaxWidth*sizeof(BYTE)*2,0);
- if(hHeap==NULL)
- {
- return -1.0;
- }
- HeapLock(hHeap);
- pByte1=(BYTE*)HeapAlloc(hHeap , HEAP_NO_SERIALIZE , m_iMaxWidth*sizeof(BYTE));
- if(pByte1==NULL)
- {
- HeapUnlock(hHeap);
- HeapDestroy(hHeap);
- return -1.0;
- }
- pByte2=(BYTE*)HeapAlloc(hHeap , HEAP_NO_SERIALIZE , m_iMaxWidth*sizeof(BYTE));
- if(pByte2==NULL)
- {
- HeapFree(hHeap,HEAP_NO_SERIALIZE,pByte1);
- HeapUnlock(hHeap);
- HeapDestroy(hHeap);
- return -1.0;
- }
- DWORD wMax=m_iMinWidth;
- double dMax=-FLT_MAX;
- double dTemp;
- int k;
- DWORD j;
- double iG1G2,iG12,iG22;
- for(k=1;k<6;k++)
- {
- for(DWORD i=m_iMinWidth;i<m_iMaxWidth;i++)
- {
- if((filesize % i) != 0)continue;
- f.Seek(k*iCount*i*sizeof(BYTE),CFile::begin);
- f.Read(pByte1,i*sizeof(BYTE));
- f.Read(pByte2,i*sizeof(BYTE));
- iG1G2=iG12=iG22=0.0;
- for(j=0;j<i;j++)
- {
- iG1G2+=(double)pByte1[i]*pByte2[i];
- iG12+=(double)pByte1[i]*pByte1[i];
- iG22+=(double)pByte2[i]*pByte2[i];
- }
-
- if((dTemp = iG1G2/iG12*iG1G2/iG22) > dMax)
- {
- dMax=dTemp;
- wMax=i;
- }
- }
- }
- f.Close();
- try
- {
- HeapFree(hHeap,HEAP_GENERATE_EXCEPTIONS,pByte1);
- HeapFree(hHeap,HEAP_GENERATE_EXCEPTIONS,pByte2);
- HeapUnlock(hHeap);
- HeapDestroy(hHeap);
- }
- catch(CMemoryException *e)
- {
- char msg[256];
- e->GetErrorMessage(msg,255);
- e->ReportError();
- e->Delete();
- return -0.1;
- }
- iWidth=wMax;
- iHeight=filesize/iWidth/iDepth;
- return dMax;
- }*/