⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aiedlg.cpp

📁 又VC++实现的基于TWAIN的扫描仪图像输入处理软件
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// AIEDlg.cpp : implementation file

#include  "stdafx.h"
#include  "source10.h"
#include  "AIEDlg.h"
#include  "BCEDlg.h"
#include  "CEDlg.h"
#include  "Scanner.h"
#include  "SetScanner.h"
#include  "ViewWnd.h"
#include  "functions.h"
#include  "math.h"
#include  "wnaspi32.h"
#include  "scsidefs.h"
#include  "const.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CScanner* pScanner;
extern CViewWnd* pViewWnd;

extern char InitDir2[100];

HDIB hbmp3;
int factorBR,factorBG,factorBB;
double factorCR,factorCG,factorCB;
int factorHR,factorHG,factorHB;
int factorSR,factorSG,factorSB;

/////////////////////////////////////////////////////////////////////////////
// CAIEDlg dialog
////////////////////////////////////////////////////////////////////
CAIEDlg::CAIEDlg(CWnd* pParent,int nChildID)
	: CDialog(CAIEDlg::IDD, pParent)
{
	m_nChildID = nChildID;

	m_pBCEDlg = NULL;
	m_pSHEDlg = NULL;
	m_pCEDlg  = NULL;
	m_bShowBCE = FALSE;
	m_bShowSHE = FALSE;
	m_bShowCE  = FALSE;

	be=FALSE;
    method=FALSE;
	heibai=FALSE;
    single=FALSE; 

    int bM,bR,bG,bB;
	int cM,cR,cG,cB;
	int hM,hR,hG,hB;
	int sM,sR,sG,sB;
    int lM,lR,lG,lB;
    int blackPosM,blackPosR,blackPosB,blackPosG;
    int whitePosM,whitePosR,whitePosB,whitePosG;
    char str[36];

    bM=GetPrivateProfileInt("pbrightnessX","brightM",128,InitDir2);; 
    sprintf(str, "%d", bM);
    WritePrivateProfileString("pbrightness2", "brightM", str, InitDir2);

   	bR=GetPrivateProfileInt("pbrightnessX","brightR",128,InitDir2); 
    sprintf(str, "%d", bR);
    WritePrivateProfileString("pbrightness2", "brightR", str, InitDir2);

	bG=GetPrivateProfileInt("pbrightnessX","brightG",128,InitDir2);
    sprintf(str, "%d", bG);
    WritePrivateProfileString("pbrightness2", "brightG", str, InitDir2);

    bB=GetPrivateProfileInt("pbrightnessX","brightB",128,InitDir2);;
    sprintf(str, "%d", bB);
    WritePrivateProfileString("pbrightness2", "brightB", str, InitDir2); 

    cM=GetPrivateProfileInt("pcontrastX","contrastM",128,InitDir2); ;
    sprintf(str, "%d", cM);
    WritePrivateProfileString("pcontrast2", "contrastM", str, InitDir2);  
	
	cR=GetPrivateProfileInt("pcontrastX","contrastR",128,InitDir2); ;
    sprintf(str, "%d", cR);
    WritePrivateProfileString("pcontrast2", "contrastR", str, InitDir2);  

    cG=GetPrivateProfileInt("pcontrastX","contrastG",128,InitDir2); ;
    sprintf(str, "%d", cG);
    WritePrivateProfileString("pcontrast2", "contrastG", str, InitDir2);

    cB=GetPrivateProfileInt("pcontrastX","contrastB",128,InitDir2); ;
    sprintf(str, "%d", cB);
    WritePrivateProfileString("pcontrast2", "contrastB", str, InitDir2);

    lM=GetPrivateProfileInt("plimitX","limitM",128,InitDir2); 
    sprintf(str, "%d", lM);
    WritePrivateProfileString("plimit2", "limitM", str, InitDir2);
	
	lR=GetPrivateProfileInt("plimitX","limitR",128,InitDir2); 
    sprintf(str, "%d", lR);
    WritePrivateProfileString("plimit2", "limitR", str, InitDir2);

    lG=GetPrivateProfileInt("plimitX","limitG",128,InitDir2);
    sprintf(str, "%d", lG);
    WritePrivateProfileString("plimit2", "limitG", str, InitDir2);

    lB=GetPrivateProfileInt("plimitX","limitB",128,InitDir2); 
    sprintf(str, "%d", lB);
    WritePrivateProfileString("plimit2", "limitB", str, InitDir2);
             
    sM=GetPrivateProfileInt("pSheX","shadeM",0,InitDir2);
    sprintf(str, "%d", sM);
    WritePrivateProfileString("pShe", "shadeM", str, InitDir2);
	
	sR=GetPrivateProfileInt("pSheX","shadeR",0,InitDir2);
    sprintf(str, "%d", sR);
    WritePrivateProfileString("pShe", "shadeR", str, InitDir2);

    sG=GetPrivateProfileInt("pSheX","shadeG",0,InitDir2);
    sprintf(str, "%d", sG);
    WritePrivateProfileString("pShe", "shadeG", str, InitDir2);

    sB=GetPrivateProfileInt("pSheX","shadeB",0,InitDir2);
    sprintf(str, "%d", sB);
    WritePrivateProfileString("pShe", "shadeB", str, InitDir2);

	hM=GetPrivateProfileInt("pSheX","highM",0,InitDir2);
	sprintf(str, "%d", hM);
    WritePrivateProfileString("pShe", "highM", str, InitDir2); 
	
	hR=GetPrivateProfileInt("pSheX","highR",0,InitDir2);
	sprintf(str, "%d", hR);
    WritePrivateProfileString("pShe", "highR", str, InitDir2); 

    hG=GetPrivateProfileInt("pSheX","highG",0,InitDir2); 
	sprintf(str, "%d", hG);
    WritePrivateProfileString("pShe", "highG", str, InitDir2);

    hB=GetPrivateProfileInt("pSheX","highB",0,InitDir2);  
    sprintf(str, "%d", hB);
    WritePrivateProfileString("pShe", "highB", str, InitDir2);

    blackPosM=GetPrivateProfileInt("pSheX","blackPosM",0,InitDir2); 
    sprintf(str, "%d", blackPosM);
    WritePrivateProfileString("pShe", "blackPosM", str, InitDir2); 
		
	blackPosR=GetPrivateProfileInt("pSheX","blackPosR",0,InitDir2); 
    sprintf(str, "%d", blackPosR);
    WritePrivateProfileString("pShe", "blackPosR", str, InitDir2); 
		
    blackPosG=GetPrivateProfileInt("pSheX","blackPosG",0,InitDir2); 
    sprintf(str, "%d", blackPosG);
    WritePrivateProfileString("pShe", "blackPosG", str, InitDir2);   
		
    blackPosB=GetPrivateProfileInt("pSheX","blackPosB",0,InitDir2); 
    sprintf(str, "%d", blackPosB);
    WritePrivateProfileString("pShe", "blackPosB", str, InitDir2); 
		
    whitePosM=GetPrivateProfileInt("pSheX","whitePosM",0,InitDir2);  
    sprintf(str, "%d", whitePosM);
    WritePrivateProfileString("pShe", "whitePosM", str, InitDir2); 
	
	whitePosR=GetPrivateProfileInt("pSheX","whitePosR",0,InitDir2);  
    sprintf(str, "%d", whitePosR);
    WritePrivateProfileString("pShe", "whitePosR", str, InitDir2); 
		
    whitePosG=GetPrivateProfileInt("pSheX","whitePosG",0,InitDir2);  
    sprintf(str, "%d", whitePosG);
    WritePrivateProfileString("pShe", "whitePosG", str, InitDir2);   
		
    whitePosB=GetPrivateProfileInt("pSheX","whitePosB",0,InitDir2);  
    sprintf(str, "%d", whitePosB);
    WritePrivateProfileString("pShe", "whitePosB", str, InitDir2); 
		

	int i;
    for(i=0;i<256;i++)
	{
	  IndexCanR00[i]=i;
	  IndexCanG00[i]=i;
	  IndexCanB00[i]=i;
	}
    for(i=0;i<256;i++)
	{
	  IndexCanR[i]=i;
	  IndexCanG[i]=i;
	  IndexCanB[i]=i;
	}
 
    for(i=0;i<256;i++)
	{
	  IndexCanR1[i]=i;
	  IndexCanG1[i]=i;
	  IndexCanB1[i]=i;
	}

	pScanner->m_nh1=FALSE;
 
	int j,j1,nLength,nWidth,j5,j6;
	
	CFileException e;

 //用于所有CCD头整体校正
	char IniFile0[120];
  
    GetWindowsDirectory(IniFile0,sizeof(IniFile0));
	char* pFileName0=lstrcat(IniFile0,"\\twain_32\\MTIScanner\\preview0.bmp");


	BITMAPFILEHEADER	Bmpfh0;
	BITMAPINFOHEADER Bifh0;
//    PALETTEENTRY BMPpalete[256];	 
    
//生成校正色彩的预览图即PREVIEW0.BMP
	   HANDLE hDIB0 = pScanner->GetHandleofPreviewBMP();
	 
	    LPSTR    lpDIBHdr;           
	    LPSTR    lpDIB0Bits;           
	  
	    CRect rect;
	    int f=0;

		LPSTR  lpBits, lpBitsStart,lpStart;
        LPSTR  lpBits0Start;
		int nResolution; 

		DWORD  dwBytes, dwBytesInLine, dwBytesBMP,dwBytesInLine0;

		HGLOBAL hMem;
		HDIB hbmp1;
       
        nResolution=100; 
        pScanner->m_nDPI=100;
	    lpDIBHdr  = (LPSTR) ::GlobalLock((HGLOBAL) hDIB0);
	    lpDIB0Bits = ::FindDIBBits(lpDIBHdr);
        

     	int cxDIB = (int) ::DIBWidth(lpDIBHdr);         
	    int cyDIB = (int) ::DIBHeight(lpDIBHdr);        
	    int colornum=(int)::DIBNumColors(lpDIBHdr);
		int W,L;
		int m4,m5;//表明原来预览图像的原点
	    int m6,m7;
		nColor=colornum;
      
        m4=GetPrivateProfileInt("yuandian","y0",0,InitDir2);
	    m5=GetPrivateProfileInt("yuandian","x0",0,InitDir2);
		m6=GetPrivateProfileInt("yuandian","high",0,InitDir2);
	    m7=GetPrivateProfileInt("yuandian","width",0,InitDir2);
		float rate1,rate2;
		rate1 = (float)cxDIB/(m7-m5);
		rate2 = (float)cyDIB/(m6-m4);
        
        W=(pScanner->m_rtScanZoom).right-(pScanner->m_rtScanZoom).left;
        L=(pScanner->m_rtScanZoom).bottom-(pScanner->m_rtScanZoom).top;
	                 
		nWidth=(int)(W/FuMian*nResolution);
        nLength=(int)(L/FuMian*nResolution);
        //nLengthC=(int)(L/FuMian*nResolution);
   
//用来确定BMP图像每行的字节数目
	   switch(colornum)
		{
          case 0:
       		j5=4-(int)(cxDIB*3) % 4 ;    
		    j6 = (int)(cxDIB*24) % 32 ;    
		    if(j6==0)
			{
               dwBytesInLine0 =cxDIB*3 ;
			}
		    else
			{
               dwBytesInLine0 =cxDIB*3+j5 ;
			}
//整体校正时选择的图像区域
            j1=4-((int)(nWidth*3)) % 4 ;    
		    j = ((int)(nWidth*24)) % 32 ;    
		    if(j==0)
			{
              dwBytesInLine = (nWidth*24)/8 ;
			}
		    else
			{
               dwBytesInLine =(nWidth*24)/8+j1 ;
			 }
			dwBytesBMP = dwBytes = dwBytesInLine * nLength; 

//指向源文件指针
            lpDIB0Bits=lpDIB0Bits+cyDIB*dwBytesInLine0-dwBytesInLine0; 
	        lpStart=lpDIB0Bits-(int)(((pScanner->m_rtScanZoom).top/10-m4)*rate2)*dwBytesInLine0+(int)(((pScanner->m_rtScanZoom).left/10-m5)*rate1)*3;
			break;
           }

		   lpBits0Start=lpStart;

//给头文件赋值
           	Bifh0.biSize = sizeof(BITMAPINFOHEADER);
			Bifh0.biWidth = (LONG) nWidth;
			Bifh0.biPlanes = 1;
			switch(colornum)
			{
			case 0:
                Bifh0.biHeight = (LONG)(nLength);
				Bifh0.biBitCount = 24;
				Bifh0.biSizeImage = dwBytesBMP;
				Bmpfh0.bfType		= 0x4d42;   
			    Bmpfh0.bfSize		= 0x36+dwBytes;   
			    Bmpfh0.bfReserved1	= 0; 
			    Bmpfh0.bfReserved2	= 0;
			    Bmpfh0.bfOffBits		= 0x36; 
				break;
			}
			Bifh0.biCompression = 0;
			Bifh0.biXPelsPerMeter =(long)((100*100)/2.54);
			Bifh0.biYPelsPerMeter =(long)((100*100)/2.54);
			Bifh0.biClrUsed = 0;
			Bifh0.biClrImportant = 0;

//分配内存空间,用于写文件,在这儿先写的是整体校正文件	
            hMem = GlobalAlloc(GHND, dwBytes);
            lpBits = (LPSTR)GlobalLock(hMem);
            lpBitsStart =lpBits; 
			lpBits=lpBits+nLength*dwBytesInLine-dwBytesInLine;

			if( !PreviewBitmap.Open( pFileName0, CFile::modeCreate | CFile::modeWrite, &e ) )
			{
				#ifdef _DEBUG    
					afxDump << "File could not be opened " << e.m_cause << "\n";
				#endif
			}
			   
			PreviewBitmap.Write(&Bmpfh0,sizeof(BITMAPFILEHEADER));
			PreviewBitmap.Write(&Bifh0,sizeof(BITMAPINFOHEADER));
		    
            for (i=0; i<nLength && (dwBytes)!=0 ; i++)
			{					
				   memcpy (lpBits, lpStart, nWidth*3);
				   lpBits -= dwBytesInLine;
				   lpStart-=(int)dwBytesInLine0;
				   dwBytes -= dwBytesInLine;
			}
			
           PreviewBitmap.Write(lpBitsStart, dwBytesBMP);
		   PreviewBitmap.Close();

    	   hbmp1=::ReadDIBFile( pFileName0 );
           pScanner->SetHandleofPreviewBMP(hbmp1);
           hbmp1=::ReadDIBFile( pFileName0 );
	       pScanner->SetHandleofAdjustedPreviewBMP(hbmp1);

           GlobalUnlock(hMem);
		   GlobalFree(hMem);
           ::GlobalUnlock((HGLOBAL) hDIB0);
 
	//{{AFX_DATA_INIT(CAIEDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

void CAIEDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAIEDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAIEDlg, CDialog)
	//{{AFX_MSG_MAP(CAIEDlg)
	ON_BN_CLICKED(IDC_SHOW_BCE, OnShowBCE)
	ON_BN_CLICKED(IDC_SHOW_SH, OnShowSH)
	ON_BN_CLICKED(IDC_SHOW_CE, OnShowCE)
	ON_WM_PAINT()
	ON_BN_CLICKED(ID_RESET, OnReset)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton1)
	ON_BN_CLICKED(ID_GUANBI, OnGuanbi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
BOOL CAIEDlg::OnInitDialog() 
{
	switch(m_nChildID)
	{
	  case 0 :
		OnShowBCE();
	    break;
	  case 1 :
		OnShowSH();
	    break;
	  case 2 :
		OnShowCE();
		break;
	}

    CDialog::OnInitDialog();
    return TRUE;  
}

void CAIEDlg::OnPaint() 
{
	BOOL bDone=TRUE;
	HANDLE hDIB;
	CPalette* palDIB;
	CPalette* oldPalette;
	UINT nColorsChanged;
	LPSTR lpDIB;
	int cxDIB;
	int cyDIB;
	CRect rcDIB;
	CRect rcDest;
	CPaintDC dc(this); 

//显示调整前的图像
	hDIB = pScanner->GetHandleofPreviewBMP( );
	if (hDIB != NULL)
	{
	  palDIB = new CPalette;
	  if (palDIB == NULL)
	  {
		::GlobalFree((HGLOBAL) hDIB);
		hDIB = NULL;
		return;
	  }
	  if (::CreateDIBPalette((struct HDIB__ *)hDIB, palDIB) == NULL)
	  {
		delete palDIB;
		palDIB = NULL;
	  }

	  oldPalette=dc.SelectPalette( palDIB, TRUE);
	  nColorsChanged=dc.RealizePalette();

	  lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
	  cxDIB = (int) ::DIBWidth(lpDIB);         
	  cyDIB = (int) ::DIBHeight(lpDIB);     
	  ::GlobalUnlock((HGLOBAL) hDIB);
		
	   rcDIB.top = rcDIB.left = 0;
	   rcDIB.right = cxDIB;
	   rcDIB.bottom = cyDIB;

	   GetDlgItem(IDC_STATIC_ORIGINAL)->GetClientRect(rcDest);
	   GetDlgItem(IDC_STATIC_ORIGINAL)->ClientToScreen(rcDest);
	   ScreenToClient(rcDest);
	
	   bDone=::PaintDIB(dc.m_hDC, &rcDest,(struct HDIB__ *)hDIB,
			&rcDIB, palDIB);
	   dc.SelectPalette(oldPalette,TRUE);
	}

//显示调整后的图像
	hDIB = pScanner->GetHandleofAdjustedPreviewBMP( );
    if (hDIB != NULL)
	{
	  palDIB = new CPalette;
	  if (palDIB == NULL)
	  {
		::GlobalFree((HGLOBAL) hDIB);
		hDIB = NULL;
		return;
	  }
	  if (::CreateDIBPalette((struct HDIB__ *)hDIB, palDIB) == NULL)
	  {
		delete palDIB;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -