// MITKTestDoc.cpp : implementation of the CMITKTestDoc class
//
#include "stdafx.h"
#include "MITKTest.h"
#include "MITKTestDoc.h"
#include "SpacingSetDlg.h"
#include "RawSetDlg.h"
#include "mitkVolume.h"
#include "mitkIM0Reader.h"
#include "mitkJPEGReader.h"
#include "mitkDICOMReader.h"
#include "mitkTIFFReader.h"
#include "mitkBMPReader.h"
#include "mitkRawReader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMITKTestDoc
IMPLEMENT_DYNCREATE(CMITKTestDoc, CDocument)
BEGIN_MESSAGE_MAP(CMITKTestDoc, CDocument)
//{{AFX_MSG_MAP(CMITKTestDoc)
ON_COMMAND(ID_FILE_OPEN_BMP, OnFileOpenBmp)
ON_COMMAND(ID_FILE_OPEN_DICOM, OnFileOpenDicom)
ON_COMMAND(ID_FILE_OPEN_IM0, OnFileOpenIm0)
ON_COMMAND(ID_FILE_OPEN_JPEG, OnFileOpenJpeg)
ON_COMMAND(ID_FILE_OPEN_RAW, OnFileOpenRaw)
ON_COMMAND(ID_FILE_OPEN_TIFF, OnFileOpenTiff)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMITKTestDoc construction/destruction
CMITKTestDoc::CMITKTestDoc()
{
// TODO: add one-time construction code here
m_Volume = NULL; //初始化指针变量
}
CMITKTestDoc::~CMITKTestDoc()
{
this->clearVolume(); //清理Volume数据
}
BOOL CMITKTestDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMITKTestDoc serialization
void CMITKTestDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CMITKTestDoc diagnostics
#ifdef _DEBUG
void CMITKTestDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMITKTestDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMITKTestDoc commands
void CMITKTestDoc::clearVolume()
{
if (m_Volume)
{
// 解除引用
m_Volume->RemoveReference();
// 这时候的m_Volume是无意义的指针,应将其置为NULL
m_Volume = NULL;
}
}
void CMITKTestDoc::OnFileOpenBmp()
{
// TODO: Add your command handler code here
// 生成打开文件对话框(可以选择多个文件)
CFileDialog dlg(TRUE,
".bmp",
NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
"BMP文件(*.bmp)|*.bmp||",
NULL);
char *fileNamesBuf = new char[512 * _MAX_PATH];
fileNamesBuf[0] = '\0';
DWORD maxBytes = 512 * _MAX_PATH;
dlg.m_ofn.lpstrFile = fileNamesBuf;
dlg.m_ofn.nMaxFile = maxBytes;
if (dlg.DoModal() == IDOK)
{
// 弹出设置像素间距的对话框
CSpacingSetDlg setDlg;
if (setDlg.DoModal() == IDCANCEL) return;
// 生成一个mitkBMPReader
mitkBMPReader *reader = new mitkBMPReader;
// 根据对话框中的数据设置像素间距
reader->SetSpacingX(setDlg.m_SpacingX);
reader->SetSpacingY(setDlg.m_SpacingY);
reader->SetSpacingZ(setDlg.m_SpacingZ);
// 得到所有选中文件的文件名,加入到reader中。
// 各个切片在Volume中是有排列次序的,对于BMP文件来说,次序信息只能从文件名得到,
// 但为简便起见,这儿省去了对文件名进行排序的过程
POSITION pos = dlg.GetStartPosition();
while (pos) reader->AddFileName(dlg.GetNextPathName(pos));
// 运行reader
if (reader->Run())
{
// 若无错误发生,则清除旧的数据,加载新的数据
this->clearVolume();
m_Volume = reader->GetOutput();
m_Volume->AddReference(); //重要!
// 更新View中的显示
this->UpdateAllViews(NULL);
}
// 删除reader
reader->Delete();
}
delete []fileNamesBuf;
}
void CMITKTestDoc::OnFileOpenDicom()
{
// TODO: Add your command handler code here
// 生成打开文件对话框(可以选择多个文件)
CFileDialog dlg(TRUE,
NULL,
NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
"DICOM文件(*.*)|*.*||",
NULL);
char *fileNamesBuf = new char[512 * _MAX_PATH];
fileNamesBuf[0] = '\0';
DWORD maxBytes = 512 * _MAX_PATH;
dlg.m_ofn.lpstrFile = fileNamesBuf;
dlg.m_ofn.nMaxFile = maxBytes;
if (dlg.DoModal() == IDOK)
{
// 生成一个mitkDICOMReader
mitkDICOMReader *reader = new mitkDICOMReader;
// 得到所有选中文件的文件名,加入到reader中。
// DICOM文件本身包含了切片的位置信息,mitkDICOMReader会根据相关信息
// 对切片进行重排,所以这里只需要把文件名直接加入进去就可以了。
POSITION pos = dlg.GetStartPosition();
while (pos) reader->AddFileName(dlg.GetNextPathName(pos));
// 运行reader
if (reader->Run())
{
// 若无错误发生,则清除旧的数据,加载新的数据
this->clearVolume();
m_Volume = reader->GetOutput();
m_Volume->AddReference(); //重要!
// 更新View中的显示
this->UpdateAllViews(NULL);
}
// 删除reader
reader->Delete();
}
delete []fileNamesBuf;
}
void CMITKTestDoc::OnFileOpenIm0()
{
// TODO: Add your command handler code here
// 生成打开文件对话框
CFileDialog dlg(TRUE,
".im0",
NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"IM0文件(*.im0)|*.im0||",
NULL);
if (dlg.DoModal() == IDOK)
{
// 生成一个mitkIM0Reader
mitkIM0Reader *reader = new mitkIM0Reader;
// 一个IM0文件就包含一个Volume,只需加一个文件名
reader->AddFileName(dlg.GetPathName());
// 运行reader
if (reader->Run())
{
// 若无错误发生,则清除旧的数据,加载新的数据
this->clearVolume();
m_Volume = reader->GetOutput();
m_Volume->AddReference(); //重要!
// 更新View中的显示
this->UpdateAllViews(NULL);
}
// 删除reader
reader->Delete();
}
}
void CMITKTestDoc::OnFileOpenJpeg()
{
// TODO: Add your command handler code here
// 生成打开文件对话框(可以选择多个文件)
CFileDialog dlg(TRUE,
".jpg",
NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
"JPEG文件(*.jpeg;*.jpg)|*.jpeg;*.jpg||",
NULL);
if (dlg.DoModal() == IDOK)
{
// 弹出设置像素间距的对话框
CSpacingSetDlg setDlg;
if (setDlg.DoModal() == IDCANCEL) return;
// 生成一个mitkJPEGReader
mitkJPEGReader *reader = new mitkJPEGReader;
// 根据对话框中的数据设置像素间距
reader->SetSpacingX(setDlg.m_SpacingX);
reader->SetSpacingY(setDlg.m_SpacingY);
reader->SetSpacingZ(setDlg.m_SpacingZ);
// 得到所有选中文件的文件名,加入到reader中。
// 各个切片在Volume中是有排列次序的,对于JPEG文件来说,次序信息只能从文件名得到,
// 但为简便起见,这儿省去了对文件名进行排序的过程
POSITION pos = dlg.GetStartPosition();
while (pos) reader->AddFileName(dlg.GetNextPathName(pos));
// 运行reader
if (reader->Run())
{
// 若无错误发生,则清除旧的数据,加载新的数据
this->clearVolume();
m_Volume = reader->GetOutput();
m_Volume->AddReference(); //重要!
// 更新View中的显示
this->UpdateAllViews(NULL);
}
// 删除reader
reader->Delete();
}
}
void CMITKTestDoc::OnFileOpenRaw()
{
// TODO: Add your command handler code here
// 生成打开文件对话框
CFileDialog dlg(TRUE,
".raw",
NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"Raw文件(*.raw;*.*)|*.raw;*.*||",
NULL);
if (dlg.DoModal() == IDOK)
{
// 弹出参数设置对话框
CRawSetDlg setDlg;
if (setDlg.DoModal() == IDCANCEL) return;
// 生成一个mitkRawReader
mitkRawReader *reader = new mitkRawReader;
// 根据对话框中的数据设置读取图像的参数
// 图像的尺寸以及切片数
reader->SetWidth(setDlg.m_Width);
reader->SetHeight(setDlg.m_Height);
reader->S