开发者社区> 问答> 正文

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();
}

展开
收起
a123456678 2016-03-05 11:35:42 2971 0
1 条回答
写回答
取消 提交回答
  • 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;
    }
    }
    2019-07-17 18:53:07
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载