?? imageview.cpp
字號(hào):
// ************************************************************************
// 文件名:ImageView.cpp
//
// 圖像復(fù)原API函數(shù)庫(kù):
//
// LimbPatternBayer() - 用BAYER表抖動(dòng)顯示圖象
// DitherFloydSteinberg() - 用Floyd-Steinberg算法抖動(dòng)生成圖象
//
// *************************************************************************
#include "stdafx.h"
#include "GlobalApi.h"
#include "Cdib.h"
#include <math.h>
#include <direct.h>
#include <complex>
using namespace std;
/*************************************************************************
*
* 函數(shù)名稱:
* LimbPatternBayer()
*
* 參數(shù):
* CDib *pDib - 指向CDib類的指針
*
* 返回值:
* BOOL - 成功返回TRUE,否則返回FALSE。
*
* 說(shuō)明:
* 該函數(shù)利用BAYER表抖動(dòng)顯示圖象。
*
************************************************************************/
BOOL LimbPatternBayer(CDib *pDib)
{
// Bayer表的定義
BYTE BayerPattern[8][8]={ 0, 32, 8, 40, 2, 34, 10, 42,
48, 16, 56, 24, 50, 18, 58, 26,
12, 44, 4, 36, 14, 46, 6, 38,
60, 28, 52, 20, 62, 30, 54, 22,
3, 35, 11, 43, 1, 33, 9, 41,
51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37,
63, 31, 55, 23, 61, 29, 53, 21};
// 指向源圖像的指針
BYTE * lpSrc;
//圖象的寬度和高度
LONG lWidth;
LONG lHeight;
//得到實(shí)際的Dib圖象存儲(chǔ)大小
CSize SizeRealDim;
SizeRealDim = pDib->GetDibSaveDim();
// 圖像每行的字節(jié)數(shù)
LONG lLineBytes;
//得到圖象的寬度和高度
CSize SizeDim;
SizeDim = pDib->GetDimensions();
lWidth = SizeDim.cx;
lHeight = SizeDim.cy;;
// 計(jì)算圖像每行的字節(jié)數(shù)
lLineBytes = SizeRealDim.cx;
//圖像數(shù)據(jù)的指針
LPBYTE lpDIBBits = pDib->m_lpImage;
// 循環(huán)變量
int i, j;
// 象素的值
int nPixelValue;
// 將圖象二值化,利用BAYER表抖動(dòng)顯示圖象
for (j = 0; j < lHeight ; j++)
{
for(i = 0; i < lLineBytes ; i++)
{
// 指向源圖像倒數(shù)第j行,第i個(gè)象素的指針
lpSrc = (unsigned char *)lpDIBBits + lLineBytes * j + i;
nPixelValue = (*lpSrc);
nPixelValue =nPixelValue;
// 右移兩位后做比較
if ( (nPixelValue>>2) > BayerPattern[j&7][i&7])
//打白點(diǎn)
*(lpSrc)=(unsigned char)255;
else
//打黑點(diǎn)
*(lpSrc)=(unsigned char)0;
}
}
return true;
}
/*************************************************************************
*
* 函數(shù)名稱:
* DitherFloydSteinberg()
*
* 參數(shù):
* CDib *pDib - 指向CDib類的指針
*
* 返回值:
* BOOL - 成功返回TRUE,否則返回FALSE。
*
* 說(shuō)明:
* 該函數(shù)用來(lái)用Floyd-Steinberg算法抖動(dòng)生成圖象。
*
************************************************************************/
BOOL DitherFloydSteinberg(CDib *pDib)
{
// 指向源圖像的指針
BYTE * lpSrc;
//圖象的寬度和高度
LONG lWidth;
LONG lHeight;
// 圖像每行的字節(jié)數(shù)
LONG lLineBytes;
//得到圖象的寬度和高度
CSize SizeDim;
SizeDim = pDib->GetDimensions();
lWidth = SizeDim.cx;
lHeight = SizeDim.cy;
//得到實(shí)際的Dib圖象存儲(chǔ)大小
CSize SizeRealDim;
SizeRealDim = pDib->GetDibSaveDim();
// 計(jì)算圖像每行的字節(jié)數(shù)
lLineBytes = SizeRealDim.cx;
//圖像數(shù)據(jù)的指針
LPBYTE lpDIBBits = pDib->m_lpImage;
// 循環(huán)變量
int i, j;
// 誤差傳播系數(shù)
double temp, error;
// 象素值
int nPixelValue;
// 將圖象二值化,并用Floyd-Steinberg算法抖動(dòng)生成圖象
for (j = 0; j < lHeight; j++)
{
for(i = 0; i < lLineBytes; i++)
{
// 指向源圖像倒數(shù)第j行,第i個(gè)象素的指針
lpSrc = (unsigned char *)lpDIBBits + lLineBytes * j + i;
nPixelValue = *lpSrc;
//128是中值
if ( nPixelValue > 128 )
{
//打白點(diǎn)
*lpSrc=255;
//計(jì)算誤差
error = (double)(nPixelValue-255.0);
}
else
{
//打黑點(diǎn)
*lpSrc=0;
//計(jì)算誤差
error = (double)nPixelValue;
}
// 如果不是邊界
if(i < lLineBytes-1)
{
//向右傳播
temp = (float)*(lpSrc+1);
temp = temp + error * (1.5/8.0);
if(temp > 255.0)
temp = 255.0;
*(lpSrc+1)=(int)temp;
}
// 如果不是邊界
if(j < lHeight - 1)
{
// 向下傳播
temp = (float)*(lpSrc + lLineBytes);
temp = temp + error * (1.5/8.0);
*(lpSrc+lLineBytes) = (int)temp;
if(i < lLineBytes-1)
{
// 向右下傳播
temp = (float)*(lpSrc + lLineBytes + 1);
temp = temp + error * (2.0/16.0);
*(lpSrc + lLineBytes + 1) = (int)temp;
}
}
}
}
return true;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -