1 /*****************************************************************************
5 ** Purpose: View & Canvas routines for CTSim program
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: July 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: views.cpp,v 1.3 2000/07/18 03:14:35 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
29 // #pragma implementation
32 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
43 #if !wxUSE_DOC_VIEW_ARCHITECTURE
44 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
58 BEGIN_EVENT_TABLE(ImageFileCanvas, wxScrolledWindow)
59 EVT_MOUSE_EVENTS(ImageFileCanvas::OnMouseEvent)
63 ImageFileCanvas::ImageFileCanvas (ImageFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
64 : wxScrolledWindow(frame, -1, pos, size, style)
70 ImageFileCanvas::OnDraw(wxDC& dc)
73 m_pView->OnDraw(& dc);
77 ImageFileCanvas::OnMouseEvent(wxMouseEvent& event)
85 wxPoint pt(event.GetLogicalPosition(dc));
88 const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
89 ImageFileArrayConst v = rIF.getArray();
93 if (pt.x >= 0 && pt.x < nx && pt.y >= 0 & pt.y < ny) {
95 os << "Image value (" << pt.x << "," << pt.y << ") = " << v[pt.x][pt.y] << "\n";
96 *theApp->getLog() << os.str().c_str();
98 *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << pt.y << ")\n";
106 IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
108 BEGIN_EVENT_TABLE(ImageFileView, wxView)
109 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
110 EVT_MENU(IFMENU_VIEW_WINDOW_MINMAX, ImageFileView::OnWindowMinMax)
111 EVT_MENU(IFMENU_VIEW_WINDOW_AUTO, ImageFileView::OnWindowAuto)
114 ImageFileView::ImageFileView(void)
115 : wxView(), m_canvas(NULL), m_frame(NULL), m_bMinSpecified(false), m_bMaxSpecified(false)
119 ImageFileView::~ImageFileView(void)
124 ImageFileView::OnProperties (wxCommandEvent& event)
126 double min, max, mean, mode, median, stddev;
127 const ImageFile& rIF = GetDocument()->getImageFile();
128 if (rIF.nx() == 0 || rIF.ny() == 0)
129 *theApp->getLog() << "Properties: empty imagefile\n";
131 const string& rFilename = rIF.getFilename();
132 rIF.statistics (min, max, mean, mode, median, stddev);
134 os << "file: " << rFilename << "\nmin: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmode: "<<mode<<"\nstddev: "<<stddev << "\n";
135 *theApp->getLog() << os.str().c_str();
140 ImageFileView::OnWindowAuto (wxCommandEvent& event)
142 *theApp->getLog() << "ImageFile: Window Auto\n";
146 ImageFileView::OnWindowMinMax (wxCommandEvent& event)
148 const ImageFile& rIF = GetDocument()->getImageFile();
150 if (! m_bMinSpecified && ! m_bMaxSpecified)
151 rIF.getMinMax (min, max);
158 DialogGetImageMinMax dialogMinMax (m_frame, rIF, min, max);
159 int retVal = dialogMinMax.ShowModal();
160 if (retVal == wxID_OK) {
161 m_bMinSpecified = true;
162 m_bMaxSpecified = true;
163 m_dMinPixel = dialogMinMax.getMinimum();
164 m_dMaxPixel = dialogMinMax.getMaximum();
165 OnUpdate (this, NULL);
171 ImageFileView::CreateCanvas (wxView *view, wxFrame *parent)
173 ImageFileCanvas* pCanvas;
175 parent->GetClientSize(&width, &height);
177 pCanvas = new ImageFileCanvas (dynamic_cast<ImageFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
179 pCanvas->SetScrollbars(20, 20, 50, 50);
180 pCanvas->SetBackgroundColour(*wxWHITE);
187 ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
189 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "ImageFile Frame", wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
191 wxMenu *file_menu = new wxMenu;
193 file_menu->Append(wxID_OPEN, "&Open...");
194 file_menu->Append(wxID_CLOSE, "&Close");
195 file_menu->Append(wxID_SAVE, "&Save");
196 file_menu->Append(wxID_SAVEAS, "Save &As...");
198 file_menu->AppendSeparator();
199 file_menu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
201 file_menu->AppendSeparator();
202 file_menu->Append(wxID_PRINT, "&Print...");
203 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
204 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
206 wxMenu *view_menu = new wxMenu;
207 view_menu->Append(IFMENU_VIEW_WINDOW_AUTO, "&Window auto");
208 view_menu->Append(IFMENU_VIEW_WINDOW_MINMAX, "&Set Window Min/Max");
210 wxMenu *help_menu = new wxMenu;
211 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
213 wxMenuBar *menu_bar = new wxMenuBar;
215 menu_bar->Append(file_menu, "&File");
216 menu_bar->Append(view_menu, "&View");
217 menu_bar->Append(help_menu, "&Help");
219 subframe->SetMenuBar(menu_bar);
221 subframe->Centre(wxBOTH);
228 ImageFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
230 m_frame = CreateChildFrame(doc, this);
232 m_bMinSpecified = false;
233 m_bMaxSpecified = false;
236 m_frame->GetClientSize(&width, &height);
237 m_frame->SetTitle("ImageFileView");
238 m_canvas = CreateCanvas(this, m_frame);
241 int x, y; // X requires a forced resize
242 m_frame->GetSize(&x, &y);
243 m_frame->SetSize(-1, -1, x, y);
253 ImageFileView::OnDraw (wxDC* dc)
256 dc->DrawBitmap(m_bitmap, 0, 0, false);
261 ImageFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
263 const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
264 ImageFileArrayConst v = rIF.getArray();
267 if (v != NULL && nx != 0 && ny != 0) {
268 if (! m_bMinSpecified || ! m_bMaxSpecified) {
270 rIF.getMinMax (min, max);
271 if (! m_bMinSpecified)
273 if (! m_bMaxSpecified)
276 double scaleWidth = m_dMaxPixel - m_dMinPixel;
278 unsigned char imageData [nx * ny * 3];
279 for (int ix = 0; ix < nx; ix++) {
280 for (int iy = 0; iy < ny; iy++) {
281 double scaleValue = ((v[ix][iy] - m_dMinPixel) / scaleWidth) * 255;
282 int intensity = static_cast<int>(scaleValue + 0.5);
283 intensity = clamp (intensity, 0, 255);
284 int baseAddr = ((ny - 1 - iy) * nx + ix) * 3;
285 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
288 wxImage image (ny, nx, imageData, true);
289 m_bitmap = image.ConvertToBitmap();
297 ImageFileView::OnClose (bool deleteWindow)
299 if (!GetDocument()->Close())
303 m_canvas->m_pView = NULL;
305 wxString s(theApp->GetAppName());
307 m_frame->SetTitle(s);
323 PhantomCanvas::PhantomCanvas (PhantomView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
324 : wxScrolledWindow(frame, -1, pos, size, style)
330 PhantomCanvas::OnDraw(wxDC& dc)
333 m_pView->OnDraw(& dc);
338 IMPLEMENT_DYNAMIC_CLASS(PhantomView, wxView)
340 BEGIN_EVENT_TABLE(PhantomView, wxView)
341 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomView::OnProperties)
342 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomView::OnRasterize)
343 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomView::OnProjections)
346 PhantomView::PhantomView(void)
347 : wxView(), m_canvas(NULL), m_frame(NULL)
351 PhantomView::~PhantomView(void)
356 PhantomView::OnProperties (wxCommandEvent& event)
358 const Phantom::PhantomID idPhantom = GetDocument()->getPhantomID();
359 const string& namePhantom = GetDocument()->getPhantomName();
361 os << "Phantom " << namePhantom << " (" << static_cast<int>(idPhantom) << ")\n";
362 *theApp->getLog() << os.str().c_str();
364 const Phantom& rPhantom = GetDocument()->getPhantom();
371 PhantomView::OnProjections (wxCommandEvent& event)
373 const Phantom& rPhantom = GetDocument()->getPhantom();
374 ProjectionFileDocument* pProjectionDoc = dynamic_cast<ProjectionFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.pj", wxDOC_SILENT));
375 Projections& rProj = pProjectionDoc->getProjections();
376 Scanner theScanner (rPhantom, "parallel", 367, 320, 2, PI);
377 rProj.initFromScanner (theScanner);
378 theScanner.collectProjections (rProj, rPhantom, 0, TRACE_NONE);
379 pProjectionDoc->Modify(true);
380 pProjectionDoc->UpdateAllViews(this);
385 PhantomView::OnRasterize (wxCommandEvent& event)
387 const Phantom& rPhantom = GetDocument()->getPhantom();
388 ImageFileDocument* pRasterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
389 ImageFile& imageFile = pRasterDoc->getImageFile();
390 imageFile.setArraySize (256, 256);
391 rPhantom.convertToImagefile (imageFile, 1, TRACE_NONE);
392 pRasterDoc->Modify(true);
393 pRasterDoc->UpdateAllViews(this);
398 PhantomView::CreateCanvas (wxView *view, wxFrame *parent)
400 PhantomCanvas* pCanvas;
402 parent->GetClientSize(&width, &height);
404 pCanvas = new PhantomCanvas (dynamic_cast<PhantomView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
406 pCanvas->SetScrollbars(20, 20, 50, 50);
407 pCanvas->SetBackgroundColour(*wxWHITE);
414 PhantomView::CreateChildFrame(wxDocument *doc, wxView *view)
416 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
418 wxMenu *file_menu = new wxMenu;
420 file_menu->Append(wxID_OPEN, "&Open...");
421 file_menu->Append(wxID_CLOSE, "&Close");
423 file_menu->AppendSeparator();
424 file_menu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties");
426 file_menu->AppendSeparator();
427 file_menu->Append(wxID_PRINT, "&Print...");
428 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
429 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
431 wxMenu *process_menu = new wxMenu;
432 process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...");
433 process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...");
435 wxMenu *help_menu = new wxMenu;
436 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
437 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
439 wxMenuBar *menu_bar = new wxMenuBar;
441 menu_bar->Append(file_menu, "&File");
442 menu_bar->Append(process_menu, "&Process");
443 menu_bar->Append(help_menu, "&Help");
445 subframe->SetMenuBar(menu_bar);
447 subframe->Centre(wxBOTH);
454 PhantomView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
456 m_frame = CreateChildFrame(doc, this);
459 m_frame->GetClientSize(&width, &height);
460 m_frame->SetTitle("PhantomView");
461 m_canvas = CreateCanvas(this, m_frame);
464 int x, y; // X requires a forced resize
465 m_frame->GetSize(&x, &y);
466 m_frame->SetSize(-1, -1, x, y);
476 PhantomView::OnDraw (wxDC* dc)
478 // if (m_bitmap.Ok())
479 // dc->DrawBitmap (m_bitmap, 0, 0, false);
484 PhantomView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
491 PhantomView::OnClose (bool deleteWindow)
493 if (!GetDocument()->Close())
497 m_canvas->m_pView = NULL;
499 wxString s(wxTheApp->GetAppName());
501 m_frame->SetTitle(s);
516 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
517 : wxScrolledWindow(frame, -1, pos, size, style)
523 ProjectionFileCanvas::OnDraw(wxDC& dc)
526 m_pView->OnDraw(& dc);
529 // ProjectionFileView
531 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
533 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
534 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
535 EVT_MENU(PJMENU_PROCESS_RECONSTRUCT, ProjectionFileView::OnReconstruct)
538 ProjectionFileView::ProjectionFileView(void)
539 : wxView(), m_canvas(NULL), m_frame(NULL)
543 ProjectionFileView::~ProjectionFileView(void)
548 ProjectionFileView::OnProperties (wxCommandEvent& event)
550 const Projections& rProj = GetDocument()->getProjections();
552 os << "ProjectionFile " << rProj.getFilename() << ": Number of Detectors = " << rProj.nDet() << ", Number of Views = " << rProj.nView() << "\n";
553 *theApp->getLog() << os.str().c_str();
558 ProjectionFileView::OnReconstruct (wxCommandEvent& event)
560 ImageFileDocument* pReconDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
561 ImageFile& imageFile = pReconDoc->getImageFile();
562 const Projections& rProj = GetDocument()->getProjections();
563 string optFilterName = "abs_bandlimit";
564 double optFilterParam = 1.;
565 string optFilterMethodName = "convolution";
567 string optInterpName = "linear";
568 int optInterpParam = 1;
569 string optBackprojectName = "idiff3";
570 imageFile.setArraySize (256, 256);
571 rProj.reconstruct (imageFile, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optZeropad, optInterpName.c_str(), optInterpParam, optBackprojectName.c_str(), TRACE_NONE);
572 pReconDoc->Modify(true);
573 pReconDoc->UpdateAllViews(this);
577 ProjectionFileCanvas*
578 ProjectionFileView::CreateCanvas (wxView *view, wxFrame *parent)
580 ProjectionFileCanvas* pCanvas;
582 parent->GetClientSize(&width, &height);
584 pCanvas = new ProjectionFileCanvas (dynamic_cast<ProjectionFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
586 pCanvas->SetScrollbars(20, 20, 50, 50);
587 pCanvas->SetBackgroundColour(*wxWHITE);
594 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
596 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
598 wxMenu *file_menu = new wxMenu;
600 file_menu->Append(wxID_OPEN, "&Open...");
601 file_menu->Append(wxID_CLOSE, "&Close");
603 file_menu->AppendSeparator();
604 file_menu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
606 file_menu->AppendSeparator();
607 file_menu->Append(wxID_PRINT, "&Print...");
608 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
609 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
611 wxMenu *process_menu = new wxMenu;
612 process_menu->Append(PJMENU_PROCESS_RECONSTRUCT, "R&econstruct...");
614 wxMenu *help_menu = new wxMenu;
615 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
616 help_menu->AppendSeparator();
617 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
619 wxMenuBar *menu_bar = new wxMenuBar;
621 menu_bar->Append(file_menu, "&File");
622 menu_bar->Append(process_menu, "&Process");
623 menu_bar->Append(help_menu, "&Help");
625 subframe->SetMenuBar(menu_bar);
627 subframe->Centre(wxBOTH);
634 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
636 m_frame = CreateChildFrame(doc, this);
639 m_frame->GetClientSize(&width, &height);
640 m_frame->SetTitle("ProjectionFileView");
641 m_canvas = CreateCanvas(this, m_frame);
644 int x, y; // X requires a forced resize
645 m_frame->GetSize(&x, &y);
646 m_frame->SetSize(-1, -1, x, y);
656 ProjectionFileView::OnDraw (wxDC* dc)
659 dc->DrawBitmap (m_bitmap, 0, 0, false);
664 ProjectionFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
666 const Projections& rProj = GetDocument()->getProjections();
667 const int nDet = rProj.nDet();
668 const int nView = rProj.nView();
669 if (nDet != 0 && nView != 0) {
670 const DetectorArray& detarray = rProj.getDetectorArray(0);
671 const DetectorValue* detval = detarray.detValues();
672 double min = detval[0];
673 double max = detval[0];
674 for (int iy = 0; iy < nView; iy++) {
675 const DetectorArray& detarray = rProj.getDetectorArray(iy);
676 const DetectorValue* detval = detarray.detValues();
677 for (int ix = 0; ix < nDet; ix++) {
678 if (min > detval[ix])
680 else if (max < detval[ix])
685 unsigned char imageData [nDet * nView * 3];
686 double scale = (max - min) / 255;
687 for (int iy = 0; iy < nView; iy++) {
688 const DetectorArray& detarray = rProj.getDetectorArray(iy);
689 const DetectorValue* detval = detarray.detValues();
690 for (int ix = 0; ix < nDet; ix++) {
691 int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
692 intensity = clamp(intensity, 0, 255);
693 int baseAddr = (iy * nDet + ix) * 3;
694 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
697 wxImage image (nDet, nView, imageData, true);
698 m_bitmap = image.ConvertToBitmap();
706 ProjectionFileView::OnClose (bool deleteWindow)
708 if (!GetDocument()->Close())
712 m_canvas->m_pView = NULL;
714 wxString s(wxTheApp->GetAppName());
716 m_frame->SetTitle(s);