程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c++-C++ 處理圖片像素時無法讀取內存

c++-C++ 處理圖片像素時無法讀取內存

編輯:編程綜合問答
C++ 處理圖片像素時無法讀取內存

//bmps 中裝的是CImage
for(int i = 0; i < bmps[nIndex].GetHeight(); i++){
for(int j = 0; j < bmps[nIndex].GetWidth();j++){
BYTE * tp1=(BYTE*)bmps[nIndex].GetPixelAddress(i,j);
BYTE * tp2=(BYTE*)bmps[nIndex+1].GetPixelAddress(i,j);
BYTE * tpOut=(BYTE*)bitmap.GetPixelAddress(i,j);
_asm{
emms
mov eax,fadetA

movupd xmm3,[eax]

mov ebx,fadetB
movupd xmm4,[ebx]

            mov ecx,tp1             
            movupd xmm5,[ecx]//這裡tp1無法讀取內存
            mov edx,tp2
            movupd xmm6,[edx]

            pxor xmm7,xmm7
            punpcklbw   xmm5,xmm7       
            punpcklbw   xmm6,xmm7
            punpcklbw   xmm3,xmm7       
            punpcklbw   xmm4,xmm7

            pmullw      xmm5,xmm3
            pmullw      xmm6,xmm4
            psrlw       xmm5,8  
            psrlw       xmm6,8  
            paddw       xmm5,xmm6   

            packuswb    xmm5,xmm7               

            mov edi,tpOut
            movsd [edi],xmm5                

            EMMS;           
            }
        }
    }
    delete []fadetA;
    delete []fadetB;
    //tmp = bitmap;
    //CDC* pDC;
    //pDC = GetWindowDC();  
    //bitmap.Draw(pDC->m_hDC,0,0);  
    Ondraw();
}

最佳回答:


給你一段代碼參考一下:
CImage* CCatchScreenDlg::LoadCImage( UINT nID, LPCTSTR lpszType, HINSTANCE hInstance )
{
CImage* pImage = NULL;

hInstance = ( NULL == hInstance ) ? ::AfxGetResourceHandle() : hInstance;

if( RT_BITMAP == lpszType )
{
    pImage = new CImage();
    pImage->LoadFromResource( hInstance, nID );
    if ( !pImage->IsNull() )
    {
        return pImage;
    }
    else
    {
        WriteScreenCatchLog( _T("[CCatchScreenDlg::LoadCImage]CImage::LoadFromResource失敗") );

        delete pImage;
        pImage = NULL;
        return NULL;
    }
}   

CString strLog;
HRSRC hRsrc = ::FindResource( hInstance, MAKEINTRESOURCE(nID), lpszType ); 
ASSERT( hRsrc != NULL );
if ( NULL == hRsrc ) 
{
    WriteScreenCatchLog( _T("[CCatchScreenDlg::LoadCImage]::FindResource失敗") );
    return NULL;
}

DWORD dwSize = ::SizeofResource( hInstance, hRsrc);
LPBYTE lpRsrc = (LPBYTE)::LoadResource( hInstance, hRsrc);
ASSERT( lpRsrc != NULL );
if ( NULL == hRsrc ) 
{
    WriteScreenCatchLog( _T("[CCatchScreenDlg::LoadCImage]::LoadResource失敗") );
    return NULL;
}

HGLOBAL hMem = ::GlobalAlloc( GMEM_FIXED, dwSize );
if ( NULL == hMem ) 
{
    WriteScreenCatchLog( _T("[CCatchScreenDlg::LoadCImage]::GlobalAlloc失敗") );

    ::FreeResource( lpRsrc );
    return NULL;
}

LPBYTE pMem = (LPBYTE)::GlobalLock( hMem );
if ( NULL == pMem ) 
{
    WriteScreenCatchLog( _T("[CCatchScreenDlg::LoadCImage]::GlobalLock失敗") );

    ::GlobalUnlock( hMem );
    ::GlobalFree( hMem );
    ::FreeResource( lpRsrc );
    return NULL;
}

memcpy( pMem, lpRsrc, dwSize );
IStream * pStream = NULL;
HRESULT hr = ::CreateStreamOnHGlobal( hMem, FALSE, &pStream );
if ( pStream != NULL && hr == S_OK ) 
{
    pImage = new CImage();
    HRESULT hrs = pImage->Load( pStream );

    pStream->Release();

    //釋放
    ::GlobalUnlock( hMem );
    ::GlobalFree( hMem );
    ::FreeResource( lpRsrc );

    if ( hrs == S_OK )
    {
        if ( pImage->GetBPP() == 32 )
        {
            for(int i = 0; i < pImage->GetWidth(); i++)   
            {   
                for(int j = 0; j < pImage->GetHeight(); j++)   
                {   
                    unsigned char* pucColor = reinterpret_cast<unsigned char *>(pImage->GetPixelAddress(i , j));   
                    pucColor[0] = pucColor[0] * pucColor[3] / 255;   
                    pucColor[1] = pucColor[1] * pucColor[3] / 255;   
                    pucColor[2] = pucColor[2] * pucColor[3] / 255;   
                }   
            }
        }

        return pImage;
    }
    else
    {
        delete pImage;
        pImage = NULL;
        return NULL;
    }
}
else
{
    WriteScreenCatchLog( _T("[CCatchScreenDlg::LoadCImage]::CreateStreamOnHGlobal失敗") );

    ::GlobalUnlock( hMem );
    ::GlobalFree( hMem );
    ::FreeResource( lpRsrc );
    return NULL;
}

}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved