📄 wvltdoc.cpp
字号:
// WvltDoc.cpp : implementation of the CWvltDoc class
//
#include "stdafx.h"
#include "Wavelets.h"
#include "WvltDoc.h"
#include "TextureDlg.h"
//#include "WvltApp.h"
#include "ImageView.h"
#include "WvltView.h"
#include <math.h>
#include <string.h>
#include "WvltTrans.h"
#include "WFilter.h"
#include "Diproc.h"
#include "ZhiLiang.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWvltDoc
IMPLEMENT_DYNCREATE(CWvltDoc, CDocument)
BEGIN_MESSAGE_MAP(CWvltDoc, CDocument)
//{{AFX_MSG_MAP(CWvltDoc)
ON_COMMAND(IDM_WVLT_HORTRANS, OnWvltHortrans)
ON_COMMAND(IDM_WVLT_VERTRANS, OnWvltVertrans)
ON_COMMAND(IDM_WVLT_ONCE, OnWvltTransOnce)
ON_COMMAND(IDM_WVLT_TWICE, OnWvltTransTwice)
ON_COMMAND(IDM_WVLT_TRBL, OnWvltTransTrbl)
ON_COMMAND(IDM_DIPROC_ENHACE, OnDiprocEnhace)
ON_COMMAND(IDM_DIPROC_FUSION, OnDiprocFusion)
ON_COMMAND(IDM_DIPROC_REVER, OnDiprocRever)
ON_COMMAND(IDM_FILTER_BLUR, OnFilterBlur)
ON_COMMAND(IDM_FILTER_SHARPNESS, OnFilterSharpness)
ON_COMMAND(IDM_FILTER_BLUR2, OnFilterBlur2)
ON_COMMAND(IDM_FILTER_SHARPNESS2, OnFilterSharpness2)
ON_COMMAND(IDM_JIAQUAN_FUSION, OnJiaquanFusion)
ON_COMMAND(IDM_QUDA_FUSION, OnQudaFusion)
ON_COMMAND(IDM_QUXIAO_FUSION, OnQuxiaoFusion)
ON_COMMAND(IDM_FANGCHA_FUSION, OnFangchaFusion)
ON_COMMAND(IDM_PIJU_QD, OnPijuQd)
ON_COMMAND(IDM_PIJU_QF, OnPijuQf)
ON_COMMAND(IDM_PIJU_QN, OnPijuQn)
ON_COMMAND(IDM_QUDA_QD, OnQudaQd)
ON_COMMAND(IDM_QUDA_QF, OnQudaQf)
ON_COMMAND(IDM_QUDA_QN, OnQudaQn)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_DUIHUA, OnDuihua)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWvltDoc construction/destruction
CWvltDoc::CWvltDoc()
{
m_fDirty = FALSE;
m_pBitmap = NULL ;
m_pTransfered = NULL;
m_pTransfered1 = NULL;
m_bTribl = FALSE; m_bOnce = FALSE; m_bTwice = FALSE; m_bFilter = FALSE;
}
CWvltDoc::~CWvltDoc()
{
if( m_pBitmap != NULL)
free( m_pBitmap);
if( m_pTransfered != NULL)
free( m_pTransfered );
}
/////////////////////////////////////////////////////////////////////////////
// CWvltDoc serialization
void CWvltDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CWvltDoc diagnostics
#ifdef _DEBUG
void CWvltDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CWvltDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CWvltDoc commands
BOOL CWvltDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
BOOL CWvltDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
m_strFileName = lpszPathName;
m_strFileFusion = m_strFileName;
if (ReadBitmap(lpszPathName, m_pBitmap))
UpdateAllViews(NULL);
return TRUE;
}
BOOL CWvltDoc::ReadBitmap(LPCTSTR lpszPathName, unsigned char *& buffer)
{
FILE *file;
file = fopen( lpszPathName, "rb");
if(!file)
return FALSE;
fpos_t posend, posbegin;
fseek(file, 0, SEEK_END);
fgetpos(file, &posend);
fseek(file, 0, SEEK_SET);
fgetpos(file,&posbegin);
size_t filesize = (size_t)(posend - posbegin);
if( buffer == NULL )
buffer = (unsigned char*)malloc( filesize);
if( buffer == NULL )
{
AfxMessageBox(IDM_MEMORY_INSUFFICIENT, MB_OK, 0);
return FALSE;
}
memset(buffer, 0, filesize);
size_t sizeread = fread(buffer, sizeof(unsigned char), filesize, file);
if(sizeread != filesize)
{
AfxMessageBox(IDM_READFILE_ERROR, MB_OK, 0);
free(buffer);
buffer = NULL;
fclose(file);
return FALSE;
}
if( buffer[0] != 'B'||buffer[1] != 'M')
{
AfxMessageBox(IDM_FILEFORMAT_ERROR, MB_OK, 0);
free(buffer);
buffer = NULL;
fclose(file);
return FALSE;
}
fclose(file);
return TRUE;
}
void CWvltDoc::OnWvltHortrans()
{
// TODO: Add your command handler code here
//读取数字图像的文件头,获取图像的属性参数
LPBITMAPINFOHEADER lpBitmapInfoHeader = (LPBITMAPINFOHEADER)(m_pBitmap+14);
LPBITMAPFILEHEADER lpBitmapFileHeader = (LPBITMAPFILEHEADER)m_pBitmap;
unsigned char *lpData = m_pBitmap + lpBitmapFileHeader->bfOffBits;
unsigned long biHeight = lpBitmapInfoHeader->biHeight;
unsigned long biWidth = lpBitmapInfoHeader->biWidth;
unsigned long biAlign = (biWidth)/4 *4;
unsigned long bmSize = biHeight * biAlign;
if(m_pTransfered==NULL)
m_pTransfered=(unsigned char*) malloc (bmSize);
if(m_pTransfered==NULL)
return;
//图像矩阵坐标与像素数值
int x,y,cur;
// unsigned char tempR, tempG, tempB;
short **spOriginData, **spTransData0;
float fTempBufforDisp;
short MaxPixVal,MinPixVal,Diff;
//分配数据空间
spOriginData = new short* [biHeight];
spTransData0 = new short* [biHeight];
//
for(int i = 0; i < biHeight; i ++)
{
spOriginData[i] = new short [biWidth];
spTransData0[i] = new short [biWidth];
}
//创建图像小波变换类
CWvltTrans *pTrans;
//从设备缓存中获得原始图像数据
for(y=0; y<(int)biHeight; y++)
{
for( x=0; x<(int)biWidth; x++)
{
cur = y*biAlign+x;
//tempB=lpData[cur];
//tempG=lpData[cur+1];
//tempR=lpData[cur+2];
//spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB);
spOriginData[biHeight-1-y][x]=lpData[cur];
}
}
//完成图像水平方向的一次小波变换
pTrans->Hori_Transform(spOriginData,spTransData0,biHeight,biWidth/2,1);
//屏蔽图像复原操作标志
m_bFilter = TRUE;
m_bFilter = m_bFilter & ~m_bOnce & ~m_bTwice & ~m_bTribl;
MaxPixVal=spTransData0[0][0];
MinPixVal=spTransData0[0][0];
//得到图像小波系数的极大值与极小值
for( y=0; y<(int)biHeight; y++)
{
for( x=0; x<(int)biWidth; x++)
{
if(MaxPixVal<spTransData0[y][x])
MaxPixVal=spTransData0[y][x];
if(MinPixVal>spTransData0[y][x])
MinPixVal=spTransData0[y][x];
}
}
//计算获取图像小波系数的极值差
Diff=MaxPixVal-MinPixVal;
//小波经过处理后,放入显示缓存中
for(y=0; y<(int)biHeight; y++)
{
for(x=0; x<(int)biWidth; x++)
{
//因为小波变换后的小波系数有可能超过255甚至更多,那么就将
//小波系数的范围映射到0~255区间内,以后出现类似的处理,目的都是一样的
fTempBufforDisp=spTransData0[biHeight-1-y][x];
fTempBufforDisp-=MinPixVal;
fTempBufforDisp*=255;
fTempBufforDisp/=Diff;
cur= y*biAlign+x; //current pixel
m_pTransfered[cur] = (unsigned char)fTempBufforDisp;
//m_pTransfered[cur+1]= (unsigned char)fTempBufforDisp;
//m_pTransfered[cur+2]= (unsigned char)fTempBufforDisp;
}
}
//小波图像水平方向小波变换
UpdateAllViews(NULL);
//删除临时的数据空间
delete spOriginData;
delete spTransData0;
}
void CWvltDoc::OnWvltVertrans()
{
// TODO: Add your command handler code here
//读取数字图像的文件头,获取图像的属性参数
LPBITMAPINFOHEADER lpBitmapInfoHeader = (LPBITMAPINFOHEADER)(m_pBitmap+14);
LPBITMAPFILEHEADER lpBitmapFileHeader = (LPBITMAPFILEHEADER)m_pBitmap;
unsigned char *lpData = m_pBitmap + lpBitmapFileHeader->bfOffBits;
unsigned long biHeight = lpBitmapInfoHeader->biHeight;
unsigned long biWidth = lpBitmapInfoHeader->biWidth;
unsigned long biAlign = (biWidth)/4 *4;
unsigned long bmSize = biHeight * biAlign;
if(m_pTransfered==NULL)
m_pTransfered=(unsigned char*) malloc (bmSize);
if(m_pTransfered==NULL)
return;
//图像矩阵坐标与像素数据
int x,y,cur;
//unsigned char tempR, tempG, tempB;
float fTempBufforDisp;
short MaxPixVal,MinPixVal,Diff;
short **spOriginData, **spTransData1;
//分配图像小波变换所需的数据空间
spOriginData = new short* [biHeight];
spTransData1 = new short* [biHeight];
for(int i = 0; i < biHeight; i ++)
{
spOriginData[i] = new short [biWidth];
spTransData1[i] = new short [biWidth];
}
//创建图像小波变换类
CWvltTrans *pTrans;
//从设备缓存中获取原始图像数据
for(y=0; y<(int)biHeight; y++)
{
for( x=0; x<(int)biWidth; x++)
{
cur = y*biAlign+x;
//tempB=lpData[cur];
// tempG=lpData[cur+1];
//tempR=lpData[cur+2];
//spOriginData[biHeight-1-y][x]=(short)(0.3*tempR+0.59*tempG+0.11*tempB);
spOriginData[biHeight-1-y][x]=lpData[cur];
}
}
//完成一次竖直方向的图像小波变换
pTrans->Vert_Transform(spOriginData,spTransData1,biHeight/2,biWidth,1);
//屏蔽图像复原操作标志
m_bFilter = TRUE;
m_bFilter = m_bFilter & ~m_bOnce & ~m_bTwice & ~m_bTribl;
MaxPixVal=spTransData1[0][0];
MinPixVal=spTransData1[0][0];
//计算得到图像小波系数的极大值和极小值
for( y=0; y<(int)biHeight; y++)
{
for( x=0; x<(int)biWidth; x++)
{
if(MaxPixVal<spTransData1[y][x])
MaxPixVal=spTransData1[y][x];
if(MinPixVal>spTransData1[y][x])
MinPixVal=spTransData1[y][x];
}
}
//计算小波系数的极值差
Diff=MaxPixVal-MinPixVal;
//将小波系数处理后,放入显示缓存中
for(y=0; y<(int)biHeight; y++)
{
for(x=0; x<(int)biWidth; x++)
{
//因为小波变换后的小波系数有可能超过255甚至更多,那么就将
//小波系数的范围映射到0~255区间内,以后出现类似的处理,目的都是一样的
fTempBufforDisp=spTransData1[biHeight-1-y][x];
fTempBufforDisp-=MinPixVal;
fTempBufforDisp*=255;
fTempBufforDisp/=Diff;
cur= y*biAlign+x; //current pixel
m_pTransfered[cur] = (unsigned char)fTempBufforDisp;
// m_pTransfered[cur+1]= (unsigned char)fTempBufforDisp;
// m_pTransfered[cur+2]= (unsigned char)fTempBufforDisp;
}
}
//显示图像的小波系数
UpdateAllViews(NULL);
//删除临时的数据空间
delete spOriginData;
delete spTransData1;
}
void CWvltDoc::OnWvltTransOnce()
{
// TODO: Add your command handler code here
//读取数字图像的文件头,获取图像的属性参数
LPBITMAPINFOHEADER lpBitmapInfoHeader = (LPBITMAPINFOHEADER)(m_pBitmap+14);
LPBITMAPFILEHEADER lpBitmapFileHeader = (LPBITMAPFILEHEADER)m_pBitmap;
unsigned char *lpData = m_pBitmap + lpBitmapFileHeader->bfOffBits;
unsigned long biHeight = lpBitmapInfoHeader->biHeight;
unsigned long biWidth = lpBitmapInfoHeader->biWidth;
unsigned long biAlign = (biWidth)/4*4;
unsigned long bmSize = biHeight * biAlign;
if(m_pTransfered==NULL)
m_pTransfered=(unsigned char*) malloc (bmSize);
if(m_pTransfered==NULL)
return;
//图像矩阵坐标与像素数据
int x,y,cur;
//unsigned char tempR, tempG, tempB;
float fTempBufforDisp;
short MaxPixVal,MinPixVal,Diff;
short **spOriginData, **spTransData0, **spTransData1;
//分配图像小波变换所用的数据空间
spOriginData = new short* [biHeight];
spTransData0 = new short* [biHeight];
spTransData1 = new short* [biHeight];
m_WvltCoeff = new short * [biHeight];
for(int i = 0; i < biHeight; i ++)
{
spOriginData[i] = new short [biWidth];
spTransData0[i] = new short [biWidth];
spTransData1[i] = new short [biWidth];
m_WvltCoeff[i] = new short [biWidth];
}
//创建图像小波变换类
CWvltTrans *pTrans;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -