求助!编译的时候没有错误,运行时出现“0x102199f31”指令引用的“0x00000000”内存,该内存不能为“read
// StudyView.cpp : implementation of the CStudyView class//
#include "stdafx.h"
#include "Study.h"
#include "StudyDoc.h"
#include "StudyView.h"
#include "MainFrm.h"
#include "ImgCenterDib.h"
#include "Gray.h"
#include "Pretreatment.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStudyView
IMPLEMENT_DYNCREATE(CStudyView, CScrollView)
BEGIN_MESSAGE_MAP(CStudyView, CScrollView)
//{{AFX_MSG_MAP(CStudyView)
ON_COMMAND(ID_BINARY, OnBinary)
ON_COMMAND(ID_FILE_WRITE, OnFileWrite)
ON_COMMAND(ID_PRETREAT_CUT, OnPretreatCut)
ON_COMMAND(ID_PRETREAT_ENHANCE, OnPretreatEnhance)
ON_COMMAND(ID_FILTER_GAUSS, OnFilterGauss)
ON_COMMAND(ID_FILTER_MEDIAN, OnFilterMedian)
ON_COMMAND(ID_GRAD, OnGrad)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CStudyView construction/destruction
CStudyView::CStudyView()
{
// TODO: add construction code here
}
CStudyView::~CStudyView()
{
}
BOOL CStudyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CStudyView drawing
void CStudyView::OnDraw(CDC* pDC)
{
CStudyDoc* pDoc = GetDocument();
//返回m_dib指针
ImgCenterDib *pDib =pDoc->GetPDib();
//获取DIB尺寸
CSize sfDib=pDib->GetDimensions();
//显示DIB
pDib->Draw(pDC, CPoint(0,0),sfDib);
}
//根据DIB的尺寸设置窗口视图的大小
void CStudyView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
//获取文档类指针
CStudyDoc* pDoc = GetDocument();
//获取DIB的指针
ImgCenterDib *pDib=pDoc->GetPDib();
//根据DIB尺寸设置视窗大小
if(pDib!=NULL)
SetScrollSizes(MM_TEXT, pDib->GetDimensions());
else{
//如果m_dib为空,则设置一个固定的尺寸
CSize sizeTotal;
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
}
/////////////////////////////////////////////////////////////////////////////
// CStudyView printing
/////////////////////////////////////////////////////////////////////////////
// CStudyView diagnostics
#ifdef _DEBUG
void CStudyView::AssertValid() const
{
CScrollView::AssertValid();
}
void CStudyView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CStudyDoc* CStudyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStudyDoc)));
return (CStudyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStudyView message handlers
void CStudyView::OnBinary()
{
// TODO: Add your command handler code here
//获取文档类m_dib指针,访问当前DIB数据
CStudyDoc *pDoc=GetDocument();
ImgCenterDib* pdib=pDoc->GetPDib();
//只处理灰度图像
if(pdib->m_nBitCount!=8)
{
::MessageBox(0,"只处理灰度图像",MB_OK,0);
return;
}
GrayTrans gray(pdib->GetDimensions(),pdib->m_nBitCount,pdib->m_lpColorTable,pdib->m_pImgData);
gray.Binary(128);
//打开新视图,显示结果
CMainFrame* pFrame = (CMainFrame *)(AfxGetApp()->m_pMainWnd);
//发送一个新建文件的消息,创建一个新的文档-视图
pFrame->SendMessage(WM_COMMAND, ID_FILE_NEW);
// ID_FILE_NEW表示文件里面的新建
//获取新建视图指针
CStudyView* pView=(CStudyView*)pFrame->MDIGetActive()->GetActiveView();
//获取相关联的新的文档类指针
CStudyDoc* pDocNew=pView->GetDocument();
//获取新文档中的ImgCenterDib类对象指针
ImgCenterDib *dibNew=pDocNew->GetPDib();
//调用ReplaceDib,用改变以后的位图数据替换原位图,调用ImgCenterDib类中的
//子函数ReplaceDib()
dibNew->ReplaceDib(gray.GetDimensions(),gray.m_nBitCountOut,gray.m_lpColorTableOut,gray.m_pImgDataOut);
//设置滚动窗口
pView->OnInitialUpdate();
//文档数据置脏,提示存盘信息
pDocNew->SetModifiedFlag(TRUE);
//各视图刷新显示
pDocNew->UpdateAllViews(pView);
}
void CStudyView::OnFileWrite()
{
}
void CStudyView::OnPretreatCut()
{
// TODO: Add your command handler code here
//图像裁剪
}
void CStudyView::OnPretreatEnhance()
{
// TODO: Add your command handler code here
//对比度增强
}
void CStudyView::OnFilterGauss()
{
// TODO: Add your command handler code here、
//高斯滤波
}
void CStudyView::OnFilterMedian()
{
// TODO: Add your command handler code here
//中值滤波
}
void CStudyView::OnGrad()
{
// TODO: Add your command handler code here
//获取文档类m_dib指针,访问当前DIB数据
CStudyDoc *pDoc=GetDocument();
ImgCenterDib* pdib=pDoc->GetPDib();
//只处理灰度图像
if(pdib->m_nBitCount!=8)
{
::MessageBox(0,"只处理灰度图像",MB_OK,0);
return;
}
ImgPretreat pregrad(pdib->GetDimensions(),pdib->m_nBitCount,pdib->m_lpColorTable,pdib->m_pImgData);
pregrad.Grad();
//打开新视图,显示结果
CMainFrame* pFrame = (CMainFrame *)(AfxGetApp()->m_pMainWnd);
//发送一个新建文件的消息,创建一个新的文档-视图
pFrame->SendMessage(WM_COMMAND, ID_FILE_NEW);
// ID_FILE_NEW表示文件里面的新建
//获取新建视图指针
CStudyView* pView=(CStudyView*)pFrame->MDIGetActive()->GetActiveView();
//获取相关联的新的文档类指针
CStudyDoc* pDocNew=pView->GetDocument();
//获取新文档中的ImgCenterDib类对象指针
ImgCenterDib *dibNew=pDocNew->GetPDib();
//调用ReplaceDib,用改变以后的位图数据替换原位图,调用ImgCenterDib类中的
//子函数ReplaceDib()
dibNew->ReplaceDib(pregrad.GetDimensions(),pregrad.m_nBitCountOut,pregrad.m_lpColorTableOut,pregrad.m_pImgDataOut);
//设置滚动窗口
pView->OnInitialUpdate();
//文档数据置脏,提示存盘信息
pDocNew->SetModifiedFlag(TRUE);
//各视图刷新显示
pDocNew->UpdateAllViews(pView);
Invalidate();
}
/////*************************
ImgPretreat是从ImgCenterDib派生的
**************************////
//#ifdef _INSIDE_VISUAL_CPP_PRETREATMENTIMG
//#define _INSIDE_VISUAL_CPP_PRETREATMENTIMG
#include "ImgCenterDib.h"
class ImgPretreat: public ImgCenterDib
{
public:
//输出图像每像素位数
int m_nBitCountOut;
//输出图像位图数据指针
unsigned char* m_pImgDataOut;
//输出图像颜色表
LPRGBQUAD m_lpColorTableOut;
//int m_nColorTableLengthOut;
private:
//输出图像的宽,像素为单位
int m_imgWidthOut;
//输出图像的高,像素为单位
int m_imgHeightOut;
//输出图像颜色表长度
int m_nColorTableLengthOut;
public:
ImgPretreat();
ImgPretreat(CSize size,int nBitCount, LPRGBQUAD lpColorTable, unsigned char* pImgData);
~ImgPretreat();
//以像素为单位返回输出图像的宽和高
CSize GetDimensions();
void Grad(); //计算梯度
/*void ImgCut(); //裁剪
void ImgAdjust(); //对比度增强
void GaussFilter(); //高斯滤波
void MedianFilter(); // 中值滤波
BYTE CircleTemplate(); //圆形模版
BYTE SquareTemplate(); //方形模版*/
};
//#endif //_INSIDE_VISUAL_CPP_PRETREATMENTIMG
///实现文件
#include "stdafx.h"
#include "Pretreatment.h"
//构造函数
ImgPretreat::ImgPretreat()
{
m_nBitCountOut=0; //输出图像每像素位数
m_pImgDataOut=NULL; //输出图像位图数据指针
m_lpColorTableOut=NULL; //输出图像颜色表
m_imgWidthOut=0; //输出图像的宽,像素为单位
m_imgHeightOut=0;//输出图像的高,像素为单位
m_nColorTableLengthOut=0;//输出图像颜色表长度
}
ImgPretreat::ImgPretreat(CSize size,int nBitCount, LPRGBQUAD lpColorTable, unsigned char* pImgData)
:ImgCenterDib(size,nBitCount,lpColorTable,pImgData)
{
m_nBitCountOut=0; //输出图像每像素位数
m_pImgDataOut=NULL; //输出图像位图数据指针
m_lpColorTableOut=NULL; //输出图像颜色表
m_imgWidthOut=0; //输出图像的宽,像素为单位
m_imgHeightOut=0;//输出图像的高,像素为单位
m_nColorTableLengthOut=0;//输出图像颜色表长度
}
//析构函数
ImgPretreat::~ImgPretreat()
{
if (m_pImgDataOut!=NULL)
{
delete []m_pImgDataOut;
m_pImgDataOut=NULL ;
}
if (m_lpColorTableOut!=NULL)
{
delete []m_lpColorTableOut;
m_lpColorTableOut=NULL;
}
}
/***********************************************************************
* 函数名称:
* GetDimensions()
*
*函数参数:
* 无
*
*返回值:
* 图像的尺寸,用CSize类型表达
*
*说明:返回输出图像的宽和高
***********************************************************************/
CSize ImgPretreat::GetDimensions()
{
if(m_pImgDataOut == NULL) return CSize(0, 0);
return CSize(m_imgWidthOut, m_imgHeightOut);
}
void ImgPretreat::Grad()
{
unsigned char* pSrc; // 指向源图像的指针
unsigned char* pDst;
unsigned char* pSrc1;
unsigned char* pSrc2;
LONG i,j; // 循环变量
int Temp;
if(m_pImgDataOut != NULL)
{
delete []m_pImgDataOut;
m_pImgDataOut = NULL;
}
int lineByte = (m_imgWidth * m_nBitCount / 8 + 3) / 4 * 4;
if(m_nBitCount != 8)
{
AfxMessageBox("只能处理8位灰度图像!");
return ;
}
//创建要复制的图像区域
m_nBitCountOut = m_nBitCount;
int lineByteOut = (m_imgWidth * m_nBitCountOut / 8 + 3) / 4 * 4;
if (!m_pImgDataOut)
{
m_pImgDataOut = new unsigned char[lineByteOut * m_imgHeight];
}
//memcpy(m_pImgDataOut,m_pImgData,lineByteOut * m_imgHeight);
//保存输出图像
int Byteperpixel = m_nBitCountOut / 8;
for(i = 0; i < m_imgHeight; i++)
{
for(j = 0; j < m_imgWidth * Byteperpixel; j++)
*(m_pImgDataOut + i * lineByteOut + j) = *(m_pImgData + i * lineByteOut + j);
}
//计算梯度
for(i = 0; i < m_imgHeight; i++) // 每行
{
for(j = 0; j < m_imgWidth; j++) // 每列
{
//指向新DIB第i行第j列的像素的指针
pDst = m_pImgDataOut + lineByte * (m_imgHeight -1 - i) + j;
// 进行梯度运算
// 指向DIB第i行,第j个象素的指针
pSrc = (unsigned char*)m_pImgData + lineByte * (m_imgHeight - 1 - i) + j;
// 指向DIB第i+1行,第j个象素的指针
pSrc1 = (unsigned char*)m_pImgData + lineByte * (m_imgHeight - 2 - i) + j;
// 指向DIB第i行,第j+1个象素的指针
pSrc2 = (unsigned char*)m_pImgData + lineByte * (m_imgHeight - 1 - i) + j + 1;
Temp = abs((*pSrc)-(*pSrc1)) + abs((*pSrc)-(*pSrc2));
*pSrc = (Temp+0);
*pDst = *pSrc;
}
}
}
编译的时候没有错误,运行的时候出现:
编译通过,能够打开保存图像,但是点击梯度钮时就出现“0x102199f31”指令引用的“0x00000000”内存,该内存不能为“read”,调试后出现下面的提示:
unhandled exception in study.exe(MSVCRTD.DLL):0XC0000005:Access Violation
Loaded symbols for 'E:\VC编写的代码\毕业设计代码\Study\Debug\Study.exe'
Loaded 'C:\WINDOWS\system32\ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded symbols for 'C:\WINDOWS\system32\MFC42D.DLL'
Loaded symbols for 'C:\WINDOWS\system32\MSVCRTD.DLL'
Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
Loaded symbols for 'C:\WINDOWS\system32\MFCO42D.DLL'
Loaded 'C:\WINDOWS\system32\imm32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\secur32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\lpk.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\usp10.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\mfc42loc.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\uxtheme.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\Program Files\Maxthon2\Modules\MxKWS\kwsui.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\psapi.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\shell32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\shlwapi.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ole32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\oleaut32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\wininet.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\crypt32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msasn1.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\winmm.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.\comctl32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\comctl32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\MSCTF.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\MSCTFIME.IME', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\SogouPy.ime', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msimg32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ntmarta.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\samlib.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\wldap32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\comdlg32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\userenv.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\apphelp.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\clbcatq.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\comres.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\version.dll', no matching symbolic information found.
Loaded 'D:\Program Files\Microsoft Office\Office12\GrooveShellExtensions.dll', no matching symbolic information found.
Loaded 'D:\Program Files\Microsoft Office\Office12\GrooveUtil.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_0de06acd\msvcr80.dll', no matching symbolic information found.
Loaded 'D:\Program Files\Microsoft Office\Office12\GrooveNew.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.ATL_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_6e805841\ATL80.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rsaenh.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\setupapi.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ntshrui.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\atl.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\netapi32.dll', no matching symbolic information found.
The thread 0x788 has exited with code 0 (0x0).