通用映像類演算法擴充庫

來源:互聯網
上載者:User

通用映像類演算法擴充庫

原創:Crazybit

首頁:www.crazy-bit.com

本文樣本程式及庫檔案:點擊下載

  這是一個比較另類的映像庫,它本身並不提供對映像對象的封裝,只以類似外掛程式的形式為您提供影像處理演算法的封裝和進度顯示,而映像對象的封裝則需由使用者來提供。本映像庫平台無關,理論上它可以應用於任何已有的C++映像類上。(具體原理請參看本人拙作Crazybit開發手記(一):設計之資料結構和演算法的分離)。

使用方法:

  為了使用本映像庫,您必須對手頭的映像類做一些簡單的修改(考慮到效率的因素,我並沒有提供一個虛介面供大家繼承,而是通過讓使用者自己修改代碼來實現之。):

  1.從現有映像類(如CxImage、CDib...)派生出FCObjImage(我想您的類也叫這個名字的機率應該比火星有水的機率要低^-^)。

  2.您必須在FCObjImage類中實現以下函數(大多是很簡單的取屬性操作):

原型 功能說明
FCObjImage () 預設建構函式
FCObjImage (const FCObjImage & img) 拷貝建構函式
FCObjImage & operator= (const FCObjImage & imgSrc) 賦值操作
BOOL IsValidImage () const 本映像對象是否有效
UINT16 ColorBits () const 映像的顏色位元(1,4,8,16,24,32)
DWORD GetPitch () const 映像每行位元組數
BYTE * GetBits (int iLine = 0) const 取得第 iLine 行指標, 左上方為(0,0), 自上而下
BYTE * GetBits (int x, int y) const 取得 (x,y) 點的指標, 左上方為(0,0), 自上而下,自左而右
bool IsInside (int x, int y) const 座標(x,y)是否在映像內部
int Width () const
int Height () const
BOOL Create (int iWidth, int iHeight, WORD wColorBit) 建立一副空映像
static void fooCopyPalette (FCObjImage & DibDest, const FCObjImage & DibSrc) <=8bit映像拷貝調色盤

  3.現在,進入最重要的一環,在FCObjImage類中添加方法:
  void SinglePixelProcessProc (FCSinglePixelProcessBase & PixelProcessor, FCObjProgress * progress = NULL)
  並把下面的實現代碼拷到FCObjImage類中:

//================================================================void  FCObjImage::SinglePixelProcessProc (FCSinglePixelProcessBase & PixelProcessor, FCObjProgress * progress)
{
if (!PixelProcessor.ValidateColorBits (this))
return ;

// 計算處理地區
RECTrcImg = {0,0,Width(),Height()}, rcBlock, rcDest ;
if (PixelProcessor.GetProcessRect() == NULL)
::CopyRect (&rcBlock, &rcImg) ;
else
::CopyRect (&rcBlock, PixelProcessor.GetProcessRect()) ;
if (::IntersectRect (&rcDest, &rcImg, &rcBlock) == 0)
return ; // 處理地區為空白

// 處理前準備工作
int nSpan = ColorBits() / 8 ; // 每象素位元組數1, 2, 3, 4
PixelProcessor.OnEnterProcess (this, rcDest) ;

// 遍曆處理地區像素
for (int y=rcDest.top ; y < rcDest.bottom ; y++)
{
BYTE* pPixel = GetBits (rcDest.left, y) ;
for (int x=rcDest.left ; x < rcDest.right ; x++, pPixel += nSpan)
{
PixelProcessor.ProcessPixel (this, x, y, pPixel) ;
}
if (progress != NULL)
progress->SetProgress (y * 100 / Height()) ;
}

// 收尾工作
PixelProcessor.OnLeaveProcess (this) ;
}//================================================================

  4.修改庫裡的PixelProcessor.cpp檔案,把FCObjImage所在的.h檔案包含進去。

  現在,您就可以通過如下方法調用本庫
  imgTest.SinglePixelProcessProc (FCPixelGrayscale()) ;
  imgTest.SinglePixelProcessProc (FCPixelInvert()) ;
  ......

 

      2004/3/27  Crazybit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.