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

📄 atldibfilters.h

📁 这是一本学习 window编程的很好的参考教材
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef __ATLDIBFILTERS_H__
#define __ATLDIBFILTERS_H__

/////////////////////////////////////////////////////////////////////////////
// 24 bit DIB image filters
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2001 Bjarke Viksoe.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. 
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//

#pragma once

// Include my DIB class
#include "atldib.h"


/////////////////////////////////////////////////////////////////////////////
// 24 bit image filters

class CFillImageFilter
{
public:
   static BOOL Process(CDib24* pDst, COLORREF clr, LPRECT prc = NULL)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      RECT rcTemp;
      if( prc==NULL ) {
         ::SetRect(&rcTemp, 0,0, pDst->GetWidth()-1, pDst->GetHeight()-1);
         prc = &rcTemp;
      }
      if( (DWORD)prc->right > pDst->GetWidth() ) return FALSE;
      if( (DWORD)prc->top > pDst->GetHeight() ) return FALSE;
      LPBYTE pStream = pDst->GetBits() + (pDst->GetLineWidth() * prc->top) + (pDst->GetPixelWidth() * prc->left);
      DWORD cx = min( (DWORD)(prc->right-prc->left), pDst->GetWidth() );
      DWORD cy = min( (DWORD)(prc->bottom-prc->top), pDst->GetHeight() );
      BYTE r = GetRValue(clr);
      BYTE g = GetGValue(clr);
      BYTE b = GetBValue(clr);
      for( DWORD y=0; y<cy; y++ ) {
         LPBYTE p = pStream;
         for( DWORD x=0; x<cx; x++ ) {
            *p++ = b;
            *p++ = g;
            *p++ = r;
         }
         pStream += pDst->GetLineWidth();
      }
      return TRUE;
   }
};

class CStrippleImageFilter
{
public:
   static BOOL Process(CDib24* pDst, COLORREF clr, DWORD divisor, LPRECT prc = NULL)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      RECT rcTemp;
      if( prc==NULL ) {
         ::SetRect(&rcTemp, 0,0, pDst->GetWidth()-1, pDst->GetHeight()-1);
         prc = &rcTemp;
      }
      LPBYTE pStream = pDst->GetBits() + (pDst->GetLineWidth() * prc->top) + (pDst->GetPixelWidth() * prc->left);
      DWORD nPixelWidth = pDst->GetPixelWidth();
      DWORD nLineWidth = pDst->GetLineWidth();
      DWORD cx = min( (DWORD)(prc->right-prc->left), pDst->GetWidth() );
      DWORD cy = min( (DWORD)(prc->bottom-prc->top), pDst->GetHeight() );
      BYTE r = GetRValue(clr);
      BYTE g = GetGValue(clr);
      BYTE b = GetBValue(clr);
      for( DWORD y=0; y<cy; y++ ) {
         LPBYTE p = pStream;
         for( DWORD x=0; x<cx; x++ ) {
            if( (x % divisor)==0 && (y % divisor)==0 ) {
               *p = b;
               *p = g;
               *p = r;
            }
            p += nPixelWidth;
         }
         pStream += nLineWidth;
      }
      return TRUE;
   }
};

class CGreyScaleImageFilter
{
public:
   static BOOL Process(CDib24* pDst)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pStream = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      DWORD nLine = pDst->GetLineWidth();
      for( DWORD y=0; y<nHeight; y++ ) {
         LPBYTE p = pStream;
         for( DWORD x=0; x<nWidth; x++ ) {
            BYTE c = (BYTE) (*(p+0)/3 + *(p+1)/3 + *(p+2)/3);
            *p++ = c;
            *p++ = c;
            *p++ = c;
         }
         pStream += nLine;
      }
      return TRUE;
   }
};

class CBrightenImageFilter
{
public:
   static BOOL Process(CDib24* pDst, short percent)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pStream = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      DWORD nLine = pDst->GetLineWidth();
      percent += 100;
      for( DWORD y=0; y<nHeight; y++ ) {
         LPBYTE p = pStream;
         for( DWORD x=0; x<nWidth; x++ ) {
            for( int z=0; z<3; z++ ) {
               WORD c = *p;
               c = (WORD) (c * percent / 100);
               *p++ = (BYTE) max(0, min(c, 255));
            }
         }
         pStream += nLine;
      }
      return TRUE;
   }
};

class CFadeImageFilter
{
public:
   static BOOL Process(CDib24* pDst, CDib24* pSrc, short percent)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pSrcBits = pSrc->GetBits();
      LPBYTE pDstBits = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      DWORD nLine = pDst->GetLineWidth();
      _ASSERTE(pSrc->GetWidth()==pDst->GetWidth());
      _ASSERTE(pSrc->GetHeight()==pDst->GetHeight());
      for( DWORD y=0; y<nHeight; y++ ) {
         LPBYTE pS = pSrcBits;
         LPBYTE pD = pDstBits;
         for( DWORD x=0; x<nWidth; x++ ) {
            for( int z=0; z<3; z++ ) {
               long c = *pS++ - *pD;
               c = (c * percent / 100);
               *pD++ = (BYTE)(*pD + c);
            }
         }
         pSrcBits += nLine;
         pDstBits += nLine;
      }
      return TRUE;
   }
};

class CColorizeImageFilter
{
public:
   static BOOL Process(CDib24* pDst, COLORREF clr)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pStream = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      DWORD nLine = pDst->GetLineWidth();
      //
      float r = GetRValue(clr);
      float g = GetGValue(clr);
      float b = GetBValue(clr);
      //
      for( DWORD y=0; y<nHeight; y++ ) {
         LPBYTE p = pStream;
         for( DWORD x=0; x<nWidth; x++ ) {
            float mid = (*(p+0)*20.0f + *(p+1)*50.0f + *(p+2)*30.0f) / 100.f;
            *p++ = (BYTE) (b * mid);
            *p++ = (BYTE) (g * mid);
            *p++ = (BYTE) (r * mid);
         }
         pStream += nLine;
      }
      return TRUE;
   }
};

class CNegativeImageFilter
{
public:
   static BOOL Process(CDib24* pDst)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pStream = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      DWORD nLine = pDst->GetLineWidth();
      for( DWORD y=0; y<nHeight; y++ ) {
         LPBYTE p = pStream;
         for( DWORD x=0; x<nWidth; x++ ) {
            *p++ = (BYTE) (255-*p);
            *p++ = (BYTE) (255-*p);
            *p++ = (BYTE) (255-*p);
         }
         pStream += nLine;
      }
      return TRUE;
   }
};

class CCopyImageFilter
{
public:
   static BOOL Process(CDib24* pDst, CDib24* pSrc, POINT& ptPos)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pSrcBits = pSrc->GetBits();
      LPBYTE pDstBits = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      //
      pDstBits += (pDst->GetLineWidth()*ptPos.y) + (ptPos.x*pDst->GetPixelWidth());
      DWORD cx = min( nWidth - ptPos.x, pSrc->GetWidth() );
      DWORD cy = min( nHeight - ptPos.y, pSrc->GetHeight() );
      //
      for( DWORD y=0; y<cy; y++ ) {
         ::CopyMemory(pDstBits, pSrcBits, cx*3);
         pSrcBits += pSrc->GetLineWidth();
         pDstBits += pDst->GetLineWidth();
      }
      return TRUE;
   }
};

class CCopyMaskedImageFilter
{
public:
   static BOOL Process(CDib24* pDst, CDib24* pSrc, POINT& ptPos, COLORREF clrBack)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pSrcBits = pSrc->GetBits();
      LPBYTE pDstBits = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      //
      pDstBits += (pDst->GetLineWidth()*ptPos.y) + (ptPos.x*pDst->GetPixelWidth());
      DWORD cx = min( nWidth - ptPos.x, pSrc->GetWidth() );
      DWORD cy = min( nHeight - ptPos.y, pSrc->GetHeight() );
      //
      BYTE r = GetRValue(clrBack);
      BYTE g = GetGValue(clrBack);
      BYTE b = GetBValue(clrBack);
      //
      for( DWORD y=0; y<cy; y++ ) {
         LPBYTE pS = pSrcBits;
         LPBYTE pD = pDstBits;
         for( DWORD x=0; x<cx; x++ ) {
            if( *(pS+0)==r &&
                *(pS+1)==g &&
                *(pS+2)==b ) 
            {
               pS += 3;
               pD += 3;
            }
            else {
               *pD++ = *pS++;
               *pD++ = *pS++;
               *pD++ = *pS++;
            }
         }
         pSrcBits += pSrc->GetLineWidth();
         pDstBits += pDst->GetLineWidth();
      }
      return TRUE;
   }
};

class CReplaceColourFilter
{
public:
   static BOOL Process(CDib24* pDst, COLORREF clrSearch, COLORREF clrReplacement)
   {
      _ASSERTE(pDst->GetBitCount()==24);
      LPBYTE pDstBits = pDst->GetBits();
      DWORD nWidth = pDst->GetWidth();
      DWORD nHeight = pDst->GetHeight();
      //
      BYTE rSrc = GetRValue(clrSearch);
      BYTE gSrc = GetGValue(clrSearch);
      BYTE bSrc = GetBValue(clrSearch);
      BYTE rRepl = GetRValue(clrReplacement);
      BYTE gRepl = GetGValue(clrReplacement);
      BYTE bRepl = GetBValue(clrReplacement);
      //
      for( DWORD y=0; y<nHeight; y++ ) {
         LPBYTE pD = pDstBits;
         for( DWORD x=0; x<nWidth; x++ ) {
            if( *(pD+0)==bSrc &&
                *(pD+1)==gSrc &&
                *(pD+2)==rSrc ) 

⌨️ 快捷键说明

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