?? mydipview.cpp
字號:
// MyDIPView.cpp : implementation of the CMyDIPView class
//
#include "stdafx.h"
#include "MyDIP.h"
#include "MyDIPDoc.h"
#include "MyDIPView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*************************************************************************
*
* 函數(shù)名稱:
* WALSH()
*
* 參數(shù):
* double * f - 指向時域值的指針
* double * F - 指向頻域值的指針
* r -2的冪數(shù)
*
* 返回值:
* 無。
*
* 說明:
* 該函數(shù)用來實現(xiàn)快速沃爾什-哈達瑪變換。
*
************************************************************************/
VOID WINAPI WALSH(double *f, double *F, int r)
{
// 沃爾什-哈達瑪變換點數(shù)
LONG count;
// 循環(huán)變量
int i,j,k;
// 中間變量
int bfsize,p;
double *X1,*X2,*X;
// 計算快速沃爾什變換點數(shù)
count = 1 << r;
// 分配運算所需的數(shù)組
X1 = new double[count];
X2 = new double[count];
// 將時域點寫入數(shù)組X1
memcpy(X1, f, sizeof(double) * count);
// 蝶形運算
for(k = 0; k < r; k++)
{
for(j = 0; j < 1<<k; j++)
{
bfsize = 1 << (r-k);
for(i = 0; i < bfsize / 2; i++)
{
p = j * bfsize;
X2[i + p] = X1[i + p] + X1[i + p + bfsize / 2];
X2[i + p + bfsize / 2] = X1[i + p] - X1[i + p + bfsize / 2];
}
}
// 互換X1和X2
X = X1;
X1 = X2;
X2 = X;
}
// 調(diào)整系數(shù)
for(j = 0; j < count; j++)
{
p = 0;
for(i = 0; i < r; i++)
{
if (j & (1<<i))
{
p += 1 << (r-i-1);
}
}
F[j] = X1[p] / count;
}
// 釋放內(nèi)存
delete X1;
delete X2;
}
/*************************************************************************
*
* 函數(shù)名稱:
* DIBWalsh1()
*
* 參數(shù):
* LPSTR lpDIBBits - 指向源DIB圖像指針
* LONG lWidth - 源圖像寬度(象素數(shù))
* LONG lHeight - 源圖像高度(象素數(shù))
*
* 返回值:
* BOOL - 成功返回TRUE,否則返回FALSE。
*
* 說明:
* 該函數(shù)用來對圖像進行沃爾什-哈達瑪變換。于上面不同的是,此處是將二維
* 矩陣轉(zhuǎn)換成一個列向量,然后對該列向量進行一次一維沃爾什-哈達瑪變換。
*
************************************************************************/
BOOL WINAPI DIBWalsh1(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
// 指向源圖像的指針
unsigned char* lpSrc;
// 循環(huán)變量
LONG i;
LONG j;
// 進行付立葉變換的寬度和高度(2的整數(shù)次方)
LONG w;
LONG h;
// 中間變量
double dTemp;
int wp;
int hp;
// 圖像每行的字節(jié)數(shù)
LONG lLineBytes;
// 計算圖像每行的字節(jié)數(shù)
lLineBytes = WIDTHBYTES(lWidth * 8);
// 賦初值
w = 1;
h = 1;
wp = 0;
hp = 0;
// 計算進行離散余弦變換的寬度和高度(2的整數(shù)次方)
while(w * 2 <= lWidth)
{
w *= 2;
wp++;
}
while(h * 2 <= lHeight)
{
h *= 2;
hp++;
}
// 分配內(nèi)存
double *f = new double[w * h];
double *F = new double[w * h];
// 列
for(i = 0; i < w; i++)
{
// 行
for(j = 0; j < h; j++)
{
// 指向DIB第j行,第i個象素的指針
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - j) + i;
// 給時域賦值
f[j + i * w] = *(lpSrc);
}
}
// 調(diào)用快速沃爾什-哈達瑪變換
WALSH(f, F, wp + hp);
// 列
for(i = 0; i < w; i++)
{
// 行
for(j = 0; j < h; j++)
{
// 計算頻譜
dTemp = fabs(F[i * w + j] * 1000);
// 判斷是否超過255
if (dTemp > 255)
{
// 對于超過的,直接設(shè)置為255
dTemp = 255;
}
// 指向DIB第j行,第i個象素的指針
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - j) + i;
// 更新源圖像
* (lpSrc) = (BYTE)(dTemp);
}
}
//釋放內(nèi)存
delete f;
delete F;
// 返回
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMyDIPView
IMPLEMENT_DYNCREATE(CMyDIPView, CScrollView)
BEGIN_MESSAGE_MAP(CMyDIPView, CScrollView)
//{{AFX_MSG_MAP(CMyDIPView)
ON_COMMAND(ID_MENUITEM32778, OnMenuitem32778)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDIPView construction/destruction
CMyDIPView::CMyDIPView()
{
// TODO: add construction code here
}
CMyDIPView::~CMyDIPView()
{
}
BOOL CMyDIPView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyDIPView drawing
void CMyDIPView::OnDraw(CDC* pDC)
{
CMyDIPDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(pDoc->m_hDIB == NULL)
return ;
// TODO: add draw code for native data here
int i,j;
unsigned char *lpSrc;
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->m_hDIB);
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
LPSTR lpDIBBits=::FindDIBBits (lpDIB);
// 計算圖像每行的字節(jié)數(shù)
long lLineBytes = WIDTHBYTES(cxDIB * 8);
// 每行
for(i = 0; i < cyDIB; i++)
{
// 每列
for(j = 0; j < cxDIB; j++)
{
// 指向DIB第i行,第j個象素的指針
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
// 計算新的灰度值
//*(lpSrc) = BYTE(255-*lpSrc);
}
}
::GlobalUnlock((HGLOBAL) pDoc->m_hDIB);
CRect rect(0,0,cxDIB,cyDIB), rcDIB(0,0,cxDIB,cyDIB);
::PaintDIB(pDC->m_hDC, &rect, pDoc->m_hDIB, &rcDIB, pDoc->m_palDIB);
}
/////////////////////////////////////////////////////////////////////////////
// CMyDIPView printing
BOOL CMyDIPView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyDIPView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyDIPView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyDIPView diagnostics
#ifdef _DEBUG
void CMyDIPView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMyDIPView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMyDIPDoc* CMyDIPView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDIPDoc)));
return (CMyDIPDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyDIPView message handlers
void CMyDIPView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
}
void CMyDIPView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
SetScrollSizes(MM_TEXT, GetDocument()->m_sizeDoc);
}
//對比度拉伸
//DEL void CMyDIPView::OnMenuitem32777()
//DEL {
//DEL
//DEL // 獲取文檔
//DEL CMyDIPDoc* pDoc = GetDocument();
//DEL int i,j;
//DEL int r1=60,r2=200;
//DEL double k=1.5;
//DEL unsigned char *lpSrc;
//DEL ASSERT_VALID(pDoc);
//DEL if(pDoc->m_hDIB == NULL)
//DEL return ;
//DEL LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->m_hDIB);
//DEL LPSTR lpDIBBits=::FindDIBBits (lpDIB);
//DEL int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
//DEL int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
//DEL long lLineBytes = WIDTHBYTES(cxDIB * 8); // 計算圖像每行的字節(jié)數(shù)
//DEL // 每行
//DEL for(i = 0; i < cyDIB; i++)
//DEL {
//DEL // 每列
//DEL for(j = 0; j < cxDIB; j++)
//DEL {
//DEL // 指向DIB第i行,第j個象素的指針
//DEL lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
//DEL // 計算新的灰度值
//DEL if(*lpSrc<r1) *lpSrc=BYTE(*lpSrc/k);
//DEL else if(*lpSrc<r2) *lpSrc= BYTE((*lpSrc-r1)*k+r1/k);
//DEL else *lpSrc=BYTE((*lpSrc-r2)/k+255-(255-r2)/k);
//DEL }
//DEL }
//DEL ::GlobalUnlock((HGLOBAL) pDoc->m_hDIB);
//DEL Invalidate(TRUE);
//DEL }
void CMyDIPView::OnMenuitem32778()
{
// 圖像沃爾什-哈達瑪變換
// 獲取文檔
CMyDIPDoc* pDoc = GetDocument();
// 指向DIB的指針
LPSTR lpDIB;
// 指向DIB象素指針
LPSTR lpDIBBits;
// 鎖定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
// 找到DIB圖像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
// 判斷是否是8-bpp位圖(這里為了方便,只處理8-bpp位圖的沃爾什-哈達瑪變換,其它的可以類推)
if (::DIBNumColors(lpDIB) != 256)
{
// 提示用戶
MessageBox("目前只支持256色位圖的沃爾什-哈達瑪變換!", "系統(tǒng)提示" ,
MB_ICONINFORMATION | MB_OK);
// 解除鎖定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
// 返回
return;
}
// 更改光標形狀
BeginWaitCursor();
// 調(diào)用DIBWalsh()或者DIBWalsh1()函數(shù)進行變換
if (::DIBWalsh1(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB)))
{
// 設(shè)置臟標記
pDoc->SetModifiedFlag(TRUE);
// 更新視圖
pDoc->UpdateAllViews(NULL);
}
else
{
// 提示用戶
MessageBox("分配內(nèi)存失敗!", "系統(tǒng)提示" , MB_ICONINFORMATION | MB_OK);
}
// 解除鎖定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
// 恢復(fù)光標
EndWaitCursor();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -