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

📄 1394display.cpp

📁 基于IEEE 1394总线的图像采集及处理系统软件技术研究
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    // Draw the bitmap on this surface
    if( FAILED( hr = (*ppSurface)->DrawBitmap( hBMP, 0, 0, 0, 0 ) ) )
    {
        DeleteObject( hBMP );
        return hr;
    }

    DeleteObject( hBMP );

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: C1394Display::CreateSurfaceFromText()
// Desc: Creates a DirectDrawSurface from a text string using hFont or the default 
//       GDI font if hFont is NULL.
//-----------------------------------------------------------------------------
HRESULT C1394Display::CreateSurfaceFromText( C1394Surface** ppSurface,
                                         HFONT hFont, TCHAR* strText, 
                                         COLORREF crBackground, COLORREF crForeground )
{
    HDC                  hDC  = NULL;
    LPDIRECTDRAWSURFACE7 pDDS = NULL;
    HRESULT              hr;
    DDSURFACEDESC2       ddsd;
    SIZE                 sizeText;

    if( m_pDD == NULL || strText == NULL || ppSurface == NULL )
        return E_INVALIDARG;

    *ppSurface = NULL;

    hDC = GetDC( NULL );

    if( hFont )
        SelectObject( hDC, hFont );

    GetTextExtentPoint32( hDC, strText, _tcslen(strText), &sizeText );
    ReleaseDC( NULL, hDC );

    // Create a DirectDrawSurface for this bitmap
    ZeroMemory( &ddsd, sizeof(ddsd) );
    ddsd.dwSize         = sizeof(ddsd);
    ddsd.dwFlags        = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
    ddsd.dwWidth        = sizeText.cx;
    ddsd.dwHeight       = sizeText.cy;

    (*ppSurface) = new C1394Surface();
    if( FAILED( hr = (*ppSurface)->Create( m_pDD, &ddsd ) ) )
    {
        delete (*ppSurface);
        return hr;
    }

    if( FAILED( hr = (*ppSurface)->DrawText( hFont, strText, 0, 0, 
                                             crBackground, crForeground ) ) )
        return hr;

    return S_OK;
}



//窗口模式下,在Blt primary buffer 和 back buffer之间进行切换
//独占模式下,进行换页
//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::Present()
{
    HRESULT hr;

    if( NULL == m_pddsFrontBuffer && NULL == m_pddsBackBuffer )
        return E_POINTER;

    while( 1 )
    {
        if( m_bWindowed )
            hr = m_pddsFrontBuffer->Blt( &m_rcWindow, m_pddsBackBuffer,
                                         NULL, DDBLT_WAIT, NULL );
	/*		hr = m_pddsFrontBuffer->BltFast( m_rcWindow.left,m_rcWindow.bottom, m_pddsBackBuffer,
                                        CRect(0,0,512,512) , DDBLT_WAIT );*/
//			hr = m_pddsFrontBuffer->BltFast( 0, 0, m_pddsBackBuffer, CRect(0,0,512,512) , DDBLT_WAIT );
        else
            hr = m_pddsFrontBuffer->Flip( NULL, 0 );//换页

        if( hr == DDERR_SURFACELOST )
        {
            m_pddsFrontBuffer->Restore();
            m_pddsBackBuffer->Restore();
        }

        if( hr != DDERR_WASSTILLDRAWING )
            return hr;
    }
}




//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::ShowBitmap( HBITMAP hbm, LPDIRECTDRAWPALETTE pPalette )
{
    if( NULL == m_pddsFrontBuffer ||  NULL == m_pddsBackBuffer )
        return E_POINTER;

    // Set the palette before loading the bitmap
    if( pPalette )
        m_pddsFrontBuffer->SetPalette( pPalette );

    C1394Surface backBuffer;
    backBuffer.Create( m_pddsBackBuffer );

    if( FAILED( backBuffer.DrawBitmap( hbm, 0, 0, 0, 0 ) ) )
        return E_FAIL;

    return Present();
}




//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::ColorKeyBlt( DWORD x, DWORD y, LPDIRECTDRAWSURFACE7 pdds,
                               RECT* prc )
{
    if( NULL == m_pddsBackBuffer )
        return E_POINTER;

    return m_pddsBackBuffer->BltFast( x, y, pdds, prc, DDBLTFAST_SRCCOLORKEY );
}




//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::Blt( DWORD x, DWORD y, LPDIRECTDRAWSURFACE7 pdds, RECT* prc,
                       DWORD dwFlags )
{
    if( NULL == m_pddsBackBuffer )
        return E_POINTER;

    return m_pddsBackBuffer->BltFast( x, y, pdds, prc, dwFlags );
}



//// Blt all the image onto the back buffer
//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::Blt( DWORD x, DWORD y, C1394Surface* pSurface, RECT* prc )
{
    if( NULL == pSurface )
        return E_INVALIDARG;

    if( pSurface->IsColorKeyed() )
        return Blt( x, y, pSurface->GetDDrawSurface(), prc, DDBLTFAST_SRCCOLORKEY );
    else
        return Blt( x, y, pSurface->GetDDrawSurface(), prc, 0L );
}




//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::Clear( DWORD dwColor )
{
    if( NULL == m_pddsBackBuffer )
        return E_POINTER;

    // Erase the background
    DDBLTFX ddbltfx;
    ZeroMemory( &ddbltfx, sizeof(ddbltfx) );
    ddbltfx.dwSize      = sizeof(ddbltfx);
    ddbltfx.dwFillColor = dwColor;

    return m_pddsBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );
}




//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::SetPalette( LPDIRECTDRAWPALETTE pPalette )
{
    if( NULL == m_pddsFrontBuffer )
        return E_POINTER;

    return m_pddsFrontBuffer->SetPalette( pPalette );
}


//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::UpdateBounds()
{
    if( m_bWindowed )
    {
        GetClientRect( m_hWnd, &m_rcWindow );
        ClientToScreen( m_hWnd, (POINT*)&m_rcWindow );
        ClientToScreen( m_hWnd, (POINT*)&m_rcWindow+1 );
    }
    else
    {
        SetRect( &m_rcWindow, 0, 0, GetSystemMetrics(SM_CXSCREEN),
                 GetSystemMetrics(SM_CYSCREEN) );
    }

    return S_OK;
}





//-----------------------------------------------------------------------------
// Name: C1394Display::InitClipper
// Desc: 
//-----------------------------------------------------------------------------
HRESULT C1394Display::InitClipper()
{
    LPDIRECTDRAWCLIPPER pClipper;
    HRESULT hr;

    // Create a clipper when using GDI to draw on the primary surface 
    if( FAILED( hr = m_pDD->CreateClipper( 0, &pClipper, NULL ) ) )
        return hr;

    pClipper->SetHWnd( 0, m_hWnd );

    if( FAILED( hr = m_pddsFrontBuffer->SetClipper( pClipper ) ) )
        return hr;

    // We can release the clipper now since g_pDDSPrimary 
    // now maintains a ref count on the clipper
    SAFE_RELEASE( pClipper );

    return S_OK;
}

//自己定义的
HRESULT C1394Display::CreateSurfaceFromFile( C1394Surface** ppSurface,
                                           TCHAR* strBMP,                                            
                                           DWORD dwDesiredWidth, 
                                           DWORD dwDesiredHeight,
										   CString strImageExt)
{
    HRESULT        hr;
    
    if( m_pDD == NULL || strBMP == NULL || ppSurface == NULL ) 
        return E_INVALIDARG;
	HBITMAP hBMP;
	if( 0 == strImageExt.CompareNoCase("bmp") )   
	{
		//  Try to load the bitmap as a file		
		hBMP = (HBITMAP) LoadImage( NULL, strBMP, IMAGE_BITMAP, dwDesiredWidth, dwDesiredHeight, LR_LOADFROMFILE );
		if( hBMP == NULL )
			return E_FAIL;
	}
	if( 0 == strImageExt.CompareNoCase("raw") )
	{
		//将裸图封装成BMP 方法:c语言+win32 API
		//将裸图中的数据读入
		FILE* pFile;
		if( ( pFile = fopen(strBMP,"r") ) == NULL )
		{
			return E_FAIL;
		}	
/*-------------------------------------------------------------------------------
//图像大小为400*400时调用该段程序
		char cRaw[400][1600];//图像为400*400,每个象素用4个字节,故为1600
		char cImage[400];		
		for( int n=0;n<400;n++ )
		{
			WORD  dw1 = fread(cImage,sizeof(char),400,pFile);//sizeof(char),5,pFile);
			for(int m = 0;m<400; m++)
			{
				//设置R、G、B和Alpha通道
				cRaw[n][m*4] = cImage[m];	//R
				cRaw[n][m*4+1] = cImage[m]; //G 
				cRaw[n][m*4+2] = cImage[m]; //B
				cRaw[n][m*4+3] = 0;         //Alpha 
			}
		}
		//生成BMP
		hBMP = ::CreateBitmap(400,400,1,32,(void*)cRaw);
//--------------------------------------------------------------------------------
*/
//--------------------------------------------------------------------------------
		static char cRaw[480][640*4];//图像为640*480,每个象素用4个字节.创建较大的数组要定义为静态类型,否则堆栈会溢出
		char cImage[640];		
		//把文件中的线性一维数据结构转换成二维的图像矩阵
		for( int m = 0;m<480; m++)
		{
			WORD  dw1 = fread(cImage,sizeof(char),640,pFile);//读入一行(640字节)数据
			for(int n=0; n<640; n++)
			{
				cRaw[m][n*4] = cImage[n];
				cRaw[m][n*4+1] = cImage[n];
				cRaw[m][n*4+2] = cImage[n];
				cRaw[m][n*4+3] = 0;
				
			}
		}
		//生成BMP
		hBMP = ::CreateBitmap(640,480,1,32,(void*)cRaw);
//----------------------------------------------------------------------------
		if( NULL == hBMP )
			DWORD dw = GetLastError();
		fclose(pFile);
	}
	// Get size of the bitmap
	BITMAP bmp;
	::GetObject( hBMP, sizeof(bmp), &bmp );

	// Create a DirectDrawSurface for this bitmap    
	*ppSurface = NULL;
	if( FAILED( hr = CreateSurface( ppSurface, bmp.bmWidth, bmp.bmHeight ) ) )
		return hr;

	// Try to load the bitmap as a file,and draw the bitmap on this surface
	if( FAILED( hr = (*ppSurface)->DrawBmp( hBMP, dwDesiredWidth, dwDesiredHeight ) ) )
	{        
		return hr;
	}
	
    return S_OK;
}

HRESULT C1394Display::CreateSurfaceFromImage( C1394Surface** ppSurface,
                                           TCHAR* strBMP,
                                           DWORD dwDesiredWidth, 
                                           DWORD dwDesiredHeight)
{
	HRESULT        hr;
    
    if( m_pDD == NULL || strBMP == NULL || ppSurface == NULL ) 
        return E_INVALIDARG;
	HBITMAP hBMP;
	//将裸图封装成BMP 方法:c语言+win32 API		
	char cRaw[405504];//图像为352*288,每个象素用4个字节	
	for( int m=0;m<101376;m++ )
	{
		//设置R、G、B和Alpha通道
		cRaw[m*4] = strBMP[m];
		cRaw[m*4+1] = strBMP[m];
		cRaw[m*4+2] = strBMP[m];
		cRaw[m*4+3] = 0;		
	}
	hBMP = ::CreateBitmap(352,288,1,32,(void*)cRaw);

	if( NULL == hBMP )
		DWORD dw = GetLastError();	
	
	// Get size of the bitmap
	BITMAP bmp;
	::GetObject( hBMP, sizeof(bmp), &bmp );

	// Create a DirectDrawSurface for this bitmap    
	*ppSurface = NULL;
	if( FAILED( hr = CreateSurface( ppSurface, bmp.bmWidth, bmp.bmHeight ) ) )
		return hr;

	// Try to load the bitmap as a file,and draw the bitmap on this surface
	if( FAILED( hr = (*ppSurface)->DrawBmp( hBMP, dwDesiredWidth, dwDesiredHeight ) ) )
	{        
		return hr;
	}
	
	return S_OK;
}

⌨️ 快捷键说明

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