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

📄 ch16p1_imagemanipulator.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
字号:
#include "Ch16p1_ImageManipulator.h"

bool CImageManipulator::ProcessImage(LPDIRECT3DTEXTURE8 pSrcTexture, 
                                     LPDIRECT3DTEXTURE8 pDestTexture, int iWidth, int iHeight, 
                                     CImageManipulatorKernel &kernel,
                                     bool bRedEnable, bool bGreenEnable, bool bBlueEnable, bool bAlphaEnable)
{
  HRESULT hr;
  try {
    m_pSrcTexture = pSrcTexture;
    m_pDestTexture = pDestTexture;
    m_iWidth = iWidth;
    m_iHeight = iHeight;
    

    int index=0;
    // lock source and destination textures
    D3DLOCKED_RECT SrcLockedRect, DestLockedRect;

    ::ZeroMemory(&SrcLockedRect, sizeof(D3DLOCKED_RECT));
    ::ZeroMemory(&DestLockedRect, sizeof(D3DLOCKED_RECT));
  
    if (FAILED(hr = pDestTexture->LockRect(0, &DestLockedRect, NULL, 0))) throw(hr);
    if (FAILED(hr = pSrcTexture->LockRect(0, &SrcLockedRect, NULL, 0))) throw(hr);
  
    m_iPitch = SrcLockedRect.Pitch;

    m_iSrcIndex = 0;
    m_iDestIndex = 0;

    m_pSrcBits = static_cast<unsigned char *>(SrcLockedRect.pBits);
    m_pDestBits = static_cast<unsigned char *>(DestLockedRect.pBits);

    for (m_iCurPosY = 0; m_iCurPosY < iHeight; m_iCurPosY++) {
      for (m_iCurPosX = 0; m_iCurPosX < iWidth; m_iCurPosX++) {
        D3DXCOLOR finalcolor;
        kernel.ProcessPixel(finalcolor, this);
        WritePixelRel(finalcolor, bRedEnable, bGreenEnable, bBlueEnable, bAlphaEnable);
      }
      m_iDestIndex += DestLockedRect.Pitch - (iWidth*4);
    }

    // unlock texture surfaces
    if (FAILED(hr = m_pSrcTexture->UnlockRect(0))) return(false);
    if (FAILED(hr = m_pDestTexture->UnlockRect(0))) return(false);
  }
  catch(...) { 
    m_pSrcTexture->UnlockRect(0);
    m_pDestTexture->UnlockRect(0);
    return(false); 
  }

  return(true);
}
  
D3DXCOLOR CImageManipulator::ReadPixelRel(int iRelPosX, int iRelPosY)
{
  int x = m_iCurPosX + iRelPosX;
  int y = m_iCurPosY + iRelPosY;

  if (x < 0 || x > m_iWidth || y < 0 || y > m_iHeight) return(D3DXCOLOR(0.0f,0.0f,0.0f,0.0f));
  int index = (m_iPitch * y) + (x*4);

  unsigned char b = m_pDestBits[index++];
  unsigned char g = m_pDestBits[index++];
  unsigned char r = m_pDestBits[index++];
  unsigned char a = m_pDestBits[index++];

  return(RGBAToD3DXColor(r,g,b,a));
}

void CImageManipulator::WritePixelRel(D3DXCOLOR color, 
                                           bool bRedEnable, 
                                           bool bGreenEnable, 
                                           bool bBlueEnable, 
                                           bool bAlphaEnable)
{
  unsigned char r,g,b,a;

  D3DXColorTo255RGBA(color, r, g, b, a);
   
  if (bBlueEnable)  m_pDestBits[m_iDestIndex++] = b; else m_iDestIndex++;
  if (bGreenEnable) m_pDestBits[m_iDestIndex++] = g; else m_iDestIndex++;
  if (bRedEnable)   m_pDestBits[m_iDestIndex++] = r; else m_iDestIndex++;
  if (bAlphaEnable) m_pDestBits[m_iDestIndex++] = a; else m_iDestIndex++;
}

⌨️ 快捷键说明

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