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.1 2000/07/13 07:01:59 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!
52 //#include "imagefile.h"
53 //#include "phantom.h"
54 //#include "scanner.h"
55 //#include "projections.h"
59 BEGIN_EVENT_TABLE(ImageFileCanvas, wxScrolledWindow)
60 EVT_MOUSE_EVENTS(ImageFileCanvas::OnMouseEvent)
64 ImageFileCanvas::ImageFileCanvas (ImageFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
65 : wxScrolledWindow(frame, -1, pos, size, style)
71 ImageFileCanvas::OnDraw(wxDC& dc)
74 m_pView->OnDraw(& dc);
78 ImageFileCanvas::OnMouseEvent(wxMouseEvent& event)
86 dc.SetPen(*wxBLACK_PEN);
88 wxPoint pt(event.GetLogicalPosition(dc));
91 cout << pt.x << "x" << pt.y << endl;
97 IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
99 BEGIN_EVENT_TABLE(ImageFileView, wxView)
100 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
103 bool ImageFileView::m_bPColoursInitialized = false;
104 wxColour* ImageFileView::m_pColours[256];
106 ImageFileView::ImageFileView(void)
107 : wxView(), m_canvas(NULL), m_frame(NULL), m_bMinSpecified(false), m_bMaxSpecified(false)
109 if (! m_bPColoursInitialized) {
110 for (int i = 0; i < 256; i++)
111 m_pColours[i] = new wxColour (i, i, i);
113 m_bPColoursInitialized = true;
117 ImageFileView::~ImageFileView(void)
122 ImageFileView::OnProperties (wxCommandEvent& event)
124 double min, max, mean, mode, median, stddev;
125 const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
126 const string& rFilename = rIF.getFilename();
127 rIF.statistics (min, max, mean, mode, median, stddev);
129 os << "file: " << rFilename << "\nmin: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmode: "<<mode<<"\nstddev: "<<stddev;
130 wxMessageBox(os.str().c_str(), "Image Properties", wxOK | wxICON_INFORMATION, m_frame);
135 ImageFileView::CreateCanvas (wxView *view, wxFrame *parent)
137 ImageFileCanvas* pCanvas;
139 parent->GetClientSize(&width, &height);
141 pCanvas = new ImageFileCanvas (dynamic_cast<ImageFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
143 pCanvas->SetScrollbars(20, 20, 50, 50);
144 pCanvas->SetBackgroundColour(*wxWHITE);
151 ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
153 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, dynamic_cast<CTSimApp*>(wxTheApp)->getMainFrame(), -1, "ImageFile Frame", wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
155 wxMenu *file_menu = new wxMenu;
157 file_menu->Append(wxID_NEW, "&New...");
158 file_menu->Append(wxID_OPEN, "&Open...");
159 file_menu->Append(wxID_CLOSE, "&Close");
160 file_menu->Append(wxID_SAVE, "&Save");
161 file_menu->Append(wxID_SAVEAS, "Save &As...");
163 file_menu->AppendSeparator();
164 file_menu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
166 file_menu->AppendSeparator();
167 file_menu->Append(wxID_PRINT, "&Print...");
168 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
169 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
171 wxMenu *help_menu = new wxMenu;
172 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
174 wxMenuBar *menu_bar = new wxMenuBar;
176 menu_bar->Append(file_menu, "&File");
177 menu_bar->Append(help_menu, "&Help");
179 subframe->SetMenuBar(menu_bar);
181 subframe->Centre(wxBOTH);
188 ImageFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
190 m_frame = CreateChildFrame(doc, this);
192 m_bMinSpecified = false;
193 m_bMaxSpecified = false;
196 m_frame->GetClientSize(&width, &height);
197 m_frame->SetTitle("ImageFileView");
198 m_canvas = CreateCanvas(this, m_frame);
201 int x, y; // X requires a forced resize
202 m_frame->GetSize(&x, &y);
203 m_frame->SetSize(-1, -1, x, y);
213 ImageFileView::OnDraw (wxDC* dc)
215 const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
216 dc->Blit(static_cast<wxCoord>(0), static_cast<wxCoord>(0), static_cast<wxCoord>(rIF.nx()), static_cast<wxCoord>(rIF.ny()), &m_memoryDC, static_cast<wxCoord>(0), static_cast<wxCoord>(0));
221 ImageFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
223 const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
224 ImageFileArrayConst v = rIF.getArray();
227 if (v != NULL && nx != 0 && ny != 0) {
228 if (! m_bMinSpecified || ! m_bMaxSpecified) {
230 rIF.getMinMax (min, max);
231 if (! m_bMinSpecified)
233 if (! m_bMaxSpecified)
236 double scaleWidth = m_maxPixel - m_minPixel;
238 m_pBitmap = new wxBitmap (rIF.nx(), rIF.ny());
239 m_memoryDC.SelectObject (*m_pBitmap);
240 m_memoryDC.BeginDrawing();
242 wxPen pen (*m_pColours[128], 1, wxDOT);
243 m_memoryDC.SetPen (pen);
244 for (int ix = 0; ix < nx; ix++) {
245 for (int iy = 0; iy < ny; iy++) {
246 double scaleValue = ((v[ix][iy] - m_minPixel) / scaleWidth) * 255;
247 int intensity = static_cast<int>(scaleValue + 0.5);
248 intensity = clamp (intensity, 0, 255);
249 pen.SetColour (*m_pColours[intensity]);
250 m_memoryDC.SetPen(pen);
251 m_memoryDC.DrawPoint(ix, ny - iy);
255 m_memoryDC.EndDrawing();
258 // m_canvas->SetScrollbars (50, 50, nx / 50, ny / 50, 0, 0, false);
267 wxClientDC dc(m_canvas);
275 ImageFileView::OnClose (bool deleteWindow)
277 if (!GetDocument()->Close())
281 m_canvas->m_pView = NULL;
283 wxString s(wxTheApp->GetAppName());
285 m_frame->SetTitle(s);
299 // ProjectionFileCanvas
301 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
302 : wxScrolledWindow(frame, -1, pos, size, style)
308 ProjectionFileCanvas::OnDraw(wxDC& dc)
311 m_pView->OnDraw(& dc);
314 // ProjectionFileView
316 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
318 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
319 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
320 EVT_MENU(PJMENU_FILE_RECONSTRUCT, ProjectionFileView::OnReconstruct)
323 bool ProjectionFileView::m_bPColoursInitialized = false;
324 wxColour* ProjectionFileView::m_pColours[256];
326 ProjectionFileView::ProjectionFileView(void)
327 : wxView(), m_canvas(NULL), m_frame(NULL)
329 if (! m_bPColoursInitialized) {
330 for (int i = 0; i < 256; i++)
331 m_pColours[i] = new wxColour (i, i, i);
333 m_bPColoursInitialized = true;
337 ProjectionFileView::~ProjectionFileView(void)
342 ProjectionFileView::OnProperties (wxCommandEvent& event)
344 const Projections& rProj = dynamic_cast<ProjectionFileDocument*>(GetDocument())->getProjections();
345 const string& rFilename = rProj.getFilename();
347 os << "file: " << rFilename << "\nDetectors: " << rProj.nDet() << "\nViews: " << rProj.nView();
348 wxMessageBox(os.str().c_str(), "Projection Properties", wxOK | wxICON_INFORMATION, m_frame);
353 ProjectionFileView::OnReconstruct (wxCommandEvent& event)
355 const Projections& rProj = dynamic_cast<ProjectionFileDocument*>(GetDocument())->getProjections();
356 const string& rFilename = rProj.getFilename();
358 os << "Reconstruct file " << rFilename;
359 wxMessageBox(os.str().c_str(), "Reconstruction Dialog", wxOK | wxICON_INFORMATION, m_frame);
363 ProjectionFileCanvas*
364 ProjectionFileView::CreateCanvas (wxView *view, wxFrame *parent)
366 ProjectionFileCanvas* pCanvas;
368 parent->GetClientSize(&width, &height);
370 pCanvas = new ProjectionFileCanvas (dynamic_cast<ProjectionFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
372 pCanvas->SetScrollbars(20, 20, 50, 50);
373 pCanvas->SetBackgroundColour(*wxWHITE);
380 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
382 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, dynamic_cast<CTSimApp*>(wxTheApp)->getMainFrame(), -1, "ProjectionFile Frame", wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
384 wxMenu *file_menu = new wxMenu;
386 file_menu->Append(wxID_NEW, "&New...");
387 file_menu->Append(wxID_OPEN, "&Open...");
388 file_menu->Append(wxID_CLOSE, "&Close");
389 file_menu->Append(wxID_SAVE, "&Save");
390 file_menu->Append(wxID_SAVEAS, "Save &As...");
392 file_menu->AppendSeparator();
393 file_menu->Append(PJMENU_FILE_RECONSTRUCT, "R&econstuct");
394 file_menu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
396 file_menu->AppendSeparator();
397 file_menu->Append(wxID_PRINT, "&Print...");
398 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
399 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
401 wxMenu *help_menu = new wxMenu;
402 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
404 wxMenuBar *menu_bar = new wxMenuBar;
406 menu_bar->Append(file_menu, "&File");
407 menu_bar->Append(help_menu, "&Help");
409 subframe->SetMenuBar(menu_bar);
411 subframe->Centre(wxBOTH);
418 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
420 m_frame = CreateChildFrame(doc, this);
423 m_frame->GetClientSize(&width, &height);
424 m_frame->SetTitle("ProjectionFileView");
425 m_canvas = CreateCanvas(this, m_frame);
428 int x, y; // X requires a forced resize
429 m_frame->GetSize(&x, &y);
430 m_frame->SetSize(-1, -1, x, y);
440 ProjectionFileView::OnDraw (wxDC* dc)
442 const Projections& rProj = dynamic_cast<ProjectionFileDocument*>(GetDocument())->getProjections();
443 dc->Blit(static_cast<wxCoord>(0), static_cast<wxCoord>(0), static_cast<wxCoord>(rProj.nDet()), static_cast<wxCoord>(rProj.nView()), &m_memoryDC, static_cast<wxCoord>(0), static_cast<wxCoord>(0));
448 ProjectionFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
450 const Projections& rProj = dynamic_cast<ProjectionFileDocument*>(GetDocument())->getProjections();
451 const DetectorArray& detarray = rProj.getDetectorArray (0);
452 const DetectorValue* detval = detarray.detValues();
453 double min = detval[0];
454 double max = detval[0];
455 for (int iy = 0; iy < rProj.nView(); iy++) {
456 const DetectorArray& detarray = rProj.getDetectorArray (iy);
457 detval = detarray.detValues();
458 for (int ix = 0; ix < rProj.nDet(); ix++) {
459 if (min > detval[ix])
461 else if (max < detval[ix])
466 double scaleWidth = max - min;
468 m_pBitmap = new wxBitmap (rProj.nDet(), rProj.nView());
469 m_memoryDC.SelectObject (*m_pBitmap);
470 m_memoryDC.BeginDrawing();
472 wxPen pen (*m_pColours[128], 1, wxDOT);
473 m_memoryDC.SetPen (pen);
474 for (int iy = 0; iy < rProj.nView(); iy++) {
475 const DetectorArray& detarray = rProj.getDetectorArray (iy);
476 detval = detarray.detValues();
477 for (int ix = 0; ix < rProj.nDet(); ix++) {
478 double scaleValue = ((detval[ix] - min) / scaleWidth) * 255;
479 int intensity = static_cast<int>(scaleValue + 0.5);
480 intensity = clamp (intensity, 0, 255);
481 pen.SetColour (*m_pColours[intensity]);
482 m_memoryDC.SetPen(pen);
483 m_memoryDC.DrawPoint(ix, iy);
486 m_memoryDC.EndDrawing();
489 // m_canvas->SetScrollbars (50, 50, nx / 50, ny / 50, 0, 0, false);
498 wxClientDC dc(m_canvas);
506 ProjectionFileView::OnClose (bool deleteWindow)
508 if (!GetDocument()->Close())
512 m_canvas->m_pView = NULL;
514 wxString s(wxTheApp->GetAppName());
516 m_frame->SetTitle(s);