r627: no message
[ctsim.git] / src / views.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          view.cpp
5 **   Purpose:       View & Canvas routines for CTSim program
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: views.cpp,v 1.131 2001/03/10 23:56:58 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "wx/wxprec.h"
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #if !wxUSE_DOC_VIEW_ARCHITECTURE
34 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
35 #endif
36
37 #include "wx/image.h"
38 #include "wx/progdlg.h"
39
40 #include "ct.h"
41 #include "ctsim.h"
42 #include "docs.h"
43 #include "views.h"
44 #include "dialogs.h"
45 #include "dlgprojections.h"
46 #include "dlgreconstruct.h"
47 #include "backprojectors.h"
48 #include "reconstruct.h"
49 #include "timer.h"
50 #include "threadproj.h"
51 #include "threadrecon.h"
52 #include "threadraster.h"
53
54 #if defined(MSVC) || HAVE_SSTREAM
55 #include <sstream>
56 #else
57 #include <sstream_subst>
58 #endif
59
60
61 // ImageFileCanvas
62
63 BEGIN_EVENT_TABLE(ImageFileCanvas, wxScrolledWindow)
64 EVT_MOUSE_EVENTS(ImageFileCanvas::OnMouseEvent)
65 EVT_CHAR(ImageFileCanvas::OnChar)
66 END_EVENT_TABLE()
67
68
69 ImageFileCanvas::ImageFileCanvas (ImageFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
70 : wxScrolledWindow(frame, -1, pos, size, style), m_pView(v), m_xCursor(-1), m_yCursor(-1)
71 {
72 }
73
74 ImageFileCanvas::~ImageFileCanvas()
75 {
76   m_pView = NULL;
77 }
78
79 void 
80 ImageFileCanvas::OnDraw(wxDC& dc)
81 {
82   if (m_pView)
83     m_pView->OnDraw(& dc);
84 }
85
86 void 
87 ImageFileCanvas::DrawRubberBandCursor (wxDC& dc, int x, int y)
88 {
89   const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
90   int nx = rIF.nx();
91   int ny = rIF.ny();
92   
93   int yPt = ny - y - 1;
94   dc.SetLogicalFunction (wxINVERT);
95   dc.SetPen (*wxGREEN_PEN);
96   dc.DrawLine (0, yPt, nx, yPt);
97   dc.DrawLine (x, 0, x, ny);
98   dc.SetLogicalFunction (wxCOPY);
99 }
100
101 bool
102 ImageFileCanvas::GetCurrentCursor (int& x, int& y)
103 {
104   x = m_xCursor;
105   y = m_yCursor;
106   
107   if (m_xCursor >= 0 && m_yCursor >= 0)
108     return true;
109   else
110     return false;
111 }
112
113 void 
114 ImageFileCanvas::OnMouseEvent(wxMouseEvent& event)
115 {
116   if (! m_pView)
117     return;
118   
119   
120   wxClientDC dc(this);
121   PrepareDC(dc);
122   
123   wxPoint pt(event.GetLogicalPosition(dc));
124   
125   const ImageFileDocument* pIFDoc = m_pView->GetDocument();
126   if (! pIFDoc)
127     return;
128   const ImageFile& rIF = pIFDoc->getImageFile();
129   ImageFileArrayConst v = rIF.getArray();
130   int nx = rIF.nx();
131   int ny = rIF.ny();
132   const int yPt = ny - 1 - pt.y;
133   if (event.RightIsDown()) {
134     if (pt.x >= 0 && pt.x < nx && pt.y >= 0 && pt.y < ny) {
135       std::ostringstream os;
136       os << "Image value (" << pt.x << "," << yPt << ") = " << v[pt.x][yPt];
137       if (rIF.isComplex()) {
138         double dImag = rIF.getImaginaryArray()[pt.x][yPt];
139         if (dImag < 0)
140           os << " - " << -dImag;
141         else
142           os << " + " << dImag;
143         os << "i\n";
144       } else
145         os << "\n";
146       *theApp->getLog() << os.str().c_str();
147     } else
148       *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << yPt << ")\n";
149   }
150   else if (event.LeftIsDown() || event.LeftUp() || event.RightUp()) {
151     if (pt.x >= 0 && pt.x < nx && pt.y >= 0 && pt.y < ny) {
152       if (m_xCursor >= 0 && m_yCursor >= 0) {
153         DrawRubberBandCursor (dc, m_xCursor, m_yCursor);
154       }
155       DrawRubberBandCursor (dc, pt.x, yPt);
156       m_xCursor = pt.x;
157       m_yCursor = yPt;
158       wxMenu* pMenu = m_pView->getMenuAnalyze();
159       if (pMenu && ! pMenu->IsEnabled(IFMENU_PLOT_ROW)) {
160         pMenu->Enable (IFMENU_PLOT_ROW, true);
161         pMenu->Enable (IFMENU_PLOT_COL, true);
162         pMenu->Enable (IFMENU_COMPARE_ROW, true);
163         pMenu->Enable (IFMENU_COMPARE_COL, true);
164         pMenu->Enable (IFMENU_PLOT_FFT_ROW, true);
165         pMenu->Enable (IFMENU_PLOT_FFT_COL, true);
166       }
167     } else
168       *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << yPt << ")\n";
169   }
170   if (event.LeftUp()) {
171     std::ostringstream os;
172     os << "Selected column " << pt.x << " , row " << yPt << "\n";
173     *theApp->getLog() << os.str().c_str();
174   }
175 }
176
177 void
178 ImageFileCanvas::OnChar (wxKeyEvent& event)
179 {
180   if (event.GetKeyCode() == WXK_ESCAPE) {
181     m_xCursor = -1;
182     m_yCursor = -1;
183     if (m_pView)
184       m_pView->OnUpdate (NULL);
185   } else
186     wxScrolledWindow::OnChar (event);
187 }
188
189 wxSize
190 ImageFileCanvas::GetBestSize() const
191 {
192   if (! m_pView)
193     return wxSize(0,0);
194   
195   const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
196   return wxSize (rIF.nx(), rIF.ny());
197 }
198
199
200 // ImageFileView
201
202 IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
203
204 BEGIN_EVENT_TABLE(ImageFileView, wxView)
205 EVT_MENU(IFMENU_FILE_EXPORT, ImageFileView::OnExport)
206 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
207 EVT_MENU(IFMENU_VIEW_SCALE_MINMAX, ImageFileView::OnScaleMinMax)
208 EVT_MENU(IFMENU_VIEW_SCALE_AUTO, ImageFileView::OnScaleAuto)
209 EVT_MENU(IFMENU_VIEW_SCALE_FULL, ImageFileView::OnScaleFull)
210 EVT_MENU(IFMENU_COMPARE_IMAGES, ImageFileView::OnCompare)
211 EVT_MENU(IFMENU_COMPARE_ROW, ImageFileView::OnCompareRow)
212 EVT_MENU(IFMENU_COMPARE_COL, ImageFileView::OnCompareCol)
213 EVT_MENU(IFMENU_FILTER_INVERTVALUES, ImageFileView::OnInvertValues)
214 EVT_MENU(IFMENU_FILTER_SQUARE, ImageFileView::OnSquare)
215 EVT_MENU(IFMENU_FILTER_SQRT, ImageFileView::OnSquareRoot)
216 EVT_MENU(IFMENU_FILTER_LOG, ImageFileView::OnLog)
217 EVT_MENU(IFMENU_FILTER_EXP, ImageFileView::OnExp)
218 EVT_MENU(IFMENU_FILTER_FOURIER, ImageFileView::OnFourier)
219 EVT_MENU(IFMENU_FILTER_INVERSE_FOURIER, ImageFileView::OnInverseFourier)
220 EVT_MENU(IFMENU_FILTER_SHUFFLEFOURIERTONATURALORDER, ImageFileView::OnShuffleFourierToNaturalOrder)
221 EVT_MENU(IFMENU_FILTER_SHUFFLENATURALTOFOURIERORDER, ImageFileView::OnShuffleNaturalToFourierOrder)
222 EVT_MENU(IFMENU_IMAGE_ADD, ImageFileView::OnAdd)
223 EVT_MENU(IFMENU_IMAGE_SUBTRACT, ImageFileView::OnSubtract)
224 EVT_MENU(IFMENU_IMAGE_MULTIPLY, ImageFileView::OnMultiply)
225 EVT_MENU(IFMENU_IMAGE_DIVIDE, ImageFileView::OnDivide)
226 EVT_MENU(IFMENU_IMAGE_SCALESIZE, ImageFileView::OnScaleSize)
227 #if wxUSE_GLCANVAS
228 EVT_MENU(IFMENU_IMAGE_CONVERT3D, ImageFileView::OnConvert3d)
229 #endif
230 #ifdef HAVE_FFT
231 EVT_MENU(IFMENU_FILTER_FFT, ImageFileView::OnFFT)
232 EVT_MENU(IFMENU_FILTER_IFFT, ImageFileView::OnIFFT)
233 EVT_MENU(IFMENU_FILTER_FFT_ROWS, ImageFileView::OnFFTRows)
234 EVT_MENU(IFMENU_FILTER_IFFT_ROWS, ImageFileView::OnIFFTRows)
235 EVT_MENU(IFMENU_FILTER_FFT_COLS, ImageFileView::OnFFTCols)
236 EVT_MENU(IFMENU_FILTER_IFFT_COLS, ImageFileView::OnIFFTCols)
237 #endif
238 EVT_MENU(IFMENU_FILTER_MAGNITUDE, ImageFileView::OnMagnitude)
239 EVT_MENU(IFMENU_FILTER_PHASE, ImageFileView::OnPhase)
240 EVT_MENU(IFMENU_PLOT_ROW, ImageFileView::OnPlotRow)
241 EVT_MENU(IFMENU_PLOT_COL, ImageFileView::OnPlotCol)
242 #ifdef HAVE_FFT
243 EVT_MENU(IFMENU_PLOT_FFT_ROW, ImageFileView::OnPlotFFTRow)
244 EVT_MENU(IFMENU_PLOT_FFT_COL, ImageFileView::OnPlotFFTCol)
245 #endif
246 EVT_MENU(IFMENU_PLOT_HISTOGRAM, ImageFileView::OnPlotHistogram)
247 END_EVENT_TABLE()
248
249 ImageFileView::ImageFileView() 
250 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
251 {
252   m_iDefaultExportFormatID = ImageFile::EXPORT_FORMAT_PNG;
253 }
254
255 ImageFileView::~ImageFileView()
256 {
257   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
258   GetDocumentManager()->ActivateView(this, FALSE, TRUE);
259 }
260
261
262 void
263 ImageFileView::OnProperties (wxCommandEvent& event)
264 {
265   const ImageFile& rIF = GetDocument()->getImageFile();
266   if (rIF.nx() == 0 || rIF.ny() == 0)
267     *theApp->getLog() << "Properties: empty imagefile\n";
268   else {
269     const std::string rFilename = m_pFrame->GetTitle().c_str();
270     std::ostringstream os;
271     double min, max, mean, mode, median, stddev;
272     rIF.statistics (rIF.getArray(), min, max, mean, mode, median, stddev);
273     os << "Filename: " << rFilename << "\n";
274     os << "Size: (" << rIF.nx() << "," << rIF.ny() << ")\n";
275     os << "Data type: ";
276     if (rIF.isComplex())
277       os << "Complex\n";
278     else
279       os << "Real\n";
280     os << "Minimum: "<<min<<"\nMaximum: "<<max<<"\nMean: "<<mean<<"\nMedian: "<<median<<"\nMode: "<<mode<<"\nStandard Deviation: "<<stddev << "\n";
281     if (rIF.isComplex()) {
282       rIF.statistics (rIF.getImaginaryArray(), min, max, mean, mode, median, stddev);
283       os << "Imaginary: min: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmedian: "<<median<<"\nmode: "<<mode<<"\nstddev: "<<stddev << "\n";
284     }
285     if (rIF.nLabels() > 0) {
286       rIF.printLabelsBrief (os);
287     }
288     *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
289     wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
290     dialogMsg.ShowModal();
291   }
292 }
293
294 void 
295 ImageFileView::OnScaleAuto (wxCommandEvent& event)
296 {
297   const ImageFile& rIF = GetDocument()->getImageFile();
298   double min, max, mean, mode, median, stddev;
299   rIF.statistics(min, max, mean, mode, median, stddev);
300   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
301   int iRetVal = dialogAutoScale.ShowModal();
302   if (iRetVal == wxID_OK) {
303     m_bMinSpecified = true;
304     m_bMaxSpecified = true;
305     double dMin, dMax;
306     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
307       m_dMinPixel = dMin;
308       m_dMaxPixel = dMax;
309       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
310       OnUpdate (this, NULL);
311     }
312   }
313 }
314
315 void 
316 ImageFileView::OnScaleMinMax (wxCommandEvent& event)
317 {
318   const ImageFile& rIF = GetDocument()->getImageFile();
319   double min, max;
320   if (! m_bMinSpecified && ! m_bMaxSpecified)
321     rIF.getMinMax (min, max);
322   
323   if (m_bMinSpecified)
324     min = m_dMinPixel;
325   if (m_bMaxSpecified)
326     max = m_dMaxPixel;
327   
328   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Image Minimum & Maximum", min, max);
329   int retVal = dialogMinMax.ShowModal();
330   if (retVal == wxID_OK) {
331     m_bMinSpecified = true;
332     m_bMaxSpecified = true;
333     m_dMinPixel = dialogMinMax.getMinimum();
334     m_dMaxPixel = dialogMinMax.getMaximum();
335     OnUpdate (this, NULL);
336   }
337 }
338
339 void 
340 ImageFileView::OnScaleFull (wxCommandEvent& event)
341 {
342   if (m_bMinSpecified || m_bMaxSpecified) {
343     m_bMinSpecified = false;
344     m_bMaxSpecified = false;
345     OnUpdate (this, NULL);
346   }
347 }
348
349 void
350 ImageFileView::OnCompare (wxCommandEvent& event)
351 {
352   std::vector<ImageFileDocument*> vecIF;
353   theApp->getCompatibleImages (GetDocument(), vecIF);
354   
355   if (vecIF.size() == 0) {
356     wxMessageBox("There are no compatible image files open for comparision", "No comparison images");
357   } else {
358     DialogGetComparisonImage dialogGetCompare(getFrameForChild(), "Get Comparison Image", vecIF, true);
359     
360     if (dialogGetCompare.ShowModal() == wxID_OK) {
361       const ImageFile& rIF = GetDocument()->getImageFile();
362       ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
363       const ImageFile& rCompareIF = pCompareDoc->getImageFile();
364       std::ostringstream os;
365       double min, max, mean, mode, median, stddev;
366       rIF.statistics (min, max, mean, mode, median, stddev);
367       os << GetFrame()->GetTitle().c_str() << ": minimum=" << min << ", maximum=" << max << ", mean=" << mean << ", mode=" << mode << ", median=" << median << ", stddev=" << stddev << "\n";
368       rCompareIF.statistics (min, max, mean, mode, median, stddev);
369       os << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str() << ": minimum=" << min << ", maximum=" << max << ", mean=" << mean << ", mode=" << mode << ", median=" << median << ", stddev=" << stddev << "\n";
370       double d, r, e;
371       rIF.comparativeStatistics (rCompareIF, d, r, e);
372       os << "Comparative Statistics: d=" << d << ", r=" << r << ", e=" << e << "\n";
373       *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
374       if (dialogGetCompare.getMakeDifferenceImage()) {
375         ImageFile* pDifferenceImage = new ImageFile;
376         
377         pDifferenceImage->setArraySize (rIF.nx(), rIF.ny());
378         if (! rIF.subtractImages (rCompareIF, *pDifferenceImage)) {
379           *theApp->getLog() << "Unable to subtract images\n";
380           delete pDifferenceImage;
381           return;
382         }
383         ImageFileDocument* pDifferenceDoc = theApp->newImageDoc();
384         if (! pDifferenceDoc) {
385           sys_error (ERR_SEVERE, "Unable to create image file");
386           return;
387         }
388         pDifferenceDoc->setImageFile (pDifferenceImage);
389         
390         wxString s = GetFrame()->GetTitle() + ": ";
391         pDifferenceImage->labelsCopy (rIF, s.c_str());
392         s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
393         pDifferenceImage->labelsCopy (rCompareIF, s.c_str());
394         std::ostringstream osLabel;
395         osLabel << "Compare image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() 
396           << " and " << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str() << ": "
397           << os.str().c_str();
398         pDifferenceImage->labelAdd (os.str().c_str());
399         if (theApp->getAskDeleteNewDocs())
400           pDifferenceDoc->Modify (true);
401         pDifferenceDoc->UpdateAllViews (this);
402         pDifferenceDoc->getView()->OnUpdate (this, NULL);
403         pDifferenceDoc->getView()->getFrame()->Show(true);
404       }
405       wxMessageBox(os.str().c_str(), "Image Comparison");
406     }
407   }
408 }
409
410 void
411 ImageFileView::OnInvertValues (wxCommandEvent& event)
412 {
413   ImageFile& rIF = GetDocument()->getImageFile();
414   rIF.invertPixelValues (rIF);
415   rIF.labelAdd ("Invert Pixel Values");
416   if (theApp->getAskDeleteNewDocs())
417     GetDocument()->Modify (true);
418   GetDocument()->UpdateAllViews (this);
419 }
420
421 void
422 ImageFileView::OnSquare (wxCommandEvent& event)
423 {
424   ImageFile& rIF = GetDocument()->getImageFile();
425   rIF.square (rIF);
426   rIF.labelAdd ("Square Pixel Values");
427   if (theApp->getAskDeleteNewDocs())
428     GetDocument()->Modify (true);
429   GetDocument()->UpdateAllViews (this);
430 }
431
432 void
433 ImageFileView::OnSquareRoot (wxCommandEvent& event)
434 {
435   ImageFile& rIF = GetDocument()->getImageFile();
436   rIF.sqrt (rIF);
437   rIF.labelAdd ("Square-root Pixel Values");
438   if (theApp->getAskDeleteNewDocs())
439     GetDocument()->Modify (true);
440   GetDocument()->UpdateAllViews (this);
441 }
442
443 void
444 ImageFileView::OnLog (wxCommandEvent& event)
445 {
446   ImageFile& rIF = GetDocument()->getImageFile();
447   rIF.log (rIF);
448   rIF.labelAdd ("Logrithm base-e Pixel Values");
449   if (theApp->getAskDeleteNewDocs())
450     GetDocument()->Modify (true);
451   GetDocument()->UpdateAllViews (this);
452 }
453
454 void
455 ImageFileView::OnExp (wxCommandEvent& event)
456 {
457   ImageFile& rIF = GetDocument()->getImageFile();
458   rIF.exp (rIF);
459   rIF.labelAdd ("Exponent base-e Pixel Values");
460   if (theApp->getAskDeleteNewDocs())
461     GetDocument()->Modify (true);
462   GetDocument()->UpdateAllViews (this);
463 }
464
465 void
466 ImageFileView::OnAdd (wxCommandEvent& event)
467 {
468   std::vector<ImageFileDocument*> vecIF;
469   theApp->getCompatibleImages (GetDocument(), vecIF);
470   
471   if (vecIF.size() == 0) {
472     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
473   } else {
474     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Add", vecIF, false);
475     
476     if (dialogGetCompare.ShowModal() == wxID_OK) {
477       ImageFile& rIF = GetDocument()->getImageFile();
478       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
479       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
480       ImageFileDocument* pNewDoc = theApp->newImageDoc();
481       if (! pNewDoc) {
482         sys_error (ERR_SEVERE, "Unable to create image file");
483         return;
484       }
485       ImageFile& newImage = pNewDoc->getImageFile();  
486       newImage.setArraySize (rIF.nx(), rIF.ny());
487       rIF.addImages (rRHSIF, newImage);
488       std::ostringstream os;
489       os << "Add image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and " 
490         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
491       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
492       newImage.labelsCopy (rIF, s.c_str());
493       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
494       newImage.labelsCopy (rRHSIF, s.c_str());
495       newImage.labelAdd (os.str().c_str());
496       *theApp->getLog() << os.str().c_str() << "\n";
497       if (theApp->getAskDeleteNewDocs())
498         pNewDoc->Modify (true);
499       pNewDoc->UpdateAllViews (this);
500       pNewDoc->getView()->OnUpdate (this, NULL);
501       pNewDoc->getView()->getFrame()->Show(true);
502     }
503   }
504 }
505
506 void
507 ImageFileView::OnSubtract (wxCommandEvent& event)
508 {
509   std::vector<ImageFileDocument*> vecIF;
510   theApp->getCompatibleImages (GetDocument(), vecIF);
511   
512   if (vecIF.size() == 0) {
513     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
514   } else {
515     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Subtract", vecIF, false);
516     
517     if (dialogGetCompare.ShowModal() == wxID_OK) {
518       ImageFile& rIF = GetDocument()->getImageFile();
519       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
520       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
521       ImageFileDocument* pNewDoc = theApp->newImageDoc();
522       if (! pNewDoc) {
523         sys_error (ERR_SEVERE, "Unable to create image file");
524         return;
525       }
526       ImageFile& newImage = pNewDoc->getImageFile();  
527       newImage.setArraySize (rIF.nx(), rIF.ny());
528       rIF.subtractImages (rRHSIF, newImage);
529       std::ostringstream os;
530       os << "Subtract image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and " 
531         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
532       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
533       newImage.labelsCopy (rIF, s.c_str());
534       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
535       newImage.labelsCopy (rRHSIF, s.c_str());
536       newImage.labelAdd (os.str().c_str());
537       *theApp->getLog() << os.str().c_str() << "\n";
538       if (theApp->getAskDeleteNewDocs())
539         pNewDoc->Modify (true);
540       pNewDoc->UpdateAllViews (this);
541       pNewDoc->getView()->OnUpdate (this, NULL);
542       pNewDoc->getView()->getFrame()->Show(true);
543     }
544   }
545 }
546
547 void
548 ImageFileView::OnMultiply (wxCommandEvent& event)
549 {
550   std::vector<ImageFileDocument*> vecIF;
551   theApp->getCompatibleImages (GetDocument(), vecIF);
552   
553   if (vecIF.size() == 0) {
554     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
555   } else {
556     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Multiply", vecIF, false);
557     
558     if (dialogGetCompare.ShowModal() == wxID_OK) {
559       ImageFile& rIF = GetDocument()->getImageFile();
560       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
561       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
562       ImageFileDocument* pNewDoc = theApp->newImageDoc();
563       if (! pNewDoc) {
564         sys_error (ERR_SEVERE, "Unable to create image file");
565         return;
566       }
567       ImageFile& newImage = pNewDoc->getImageFile();  
568       newImage.setArraySize (rIF.nx(), rIF.ny());
569       rIF.multiplyImages (rRHSIF, newImage);
570       std::ostringstream os;
571       os << "Multiply image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and " 
572         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
573       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
574       newImage.labelsCopy (rIF, s.c_str());
575       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
576       newImage.labelsCopy (rRHSIF, s.c_str());
577       newImage.labelAdd (os.str().c_str());
578       *theApp->getLog() << os.str().c_str() << "\n";
579       if (theApp->getAskDeleteNewDocs())
580         pNewDoc->Modify (true);
581       pNewDoc->UpdateAllViews (this);
582       pNewDoc->getView()->OnUpdate (this, NULL);
583       pNewDoc->getView()->getFrame()->Show(true);
584     }
585   }
586 }
587
588 void
589 ImageFileView::OnDivide (wxCommandEvent& event)
590 {
591   std::vector<ImageFileDocument*> vecIF;
592   theApp->getCompatibleImages (GetDocument(), vecIF);
593   
594   if (vecIF.size() == 0) {
595     wxMessageBox ("There are no compatible image files open for comparision", "No comparison images");
596   } else {
597     DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Image to Divide", vecIF, false);
598     
599     if (dialogGetCompare.ShowModal() == wxID_OK) {
600       ImageFile& rIF = GetDocument()->getImageFile();
601       ImageFileDocument* pRHSDoc = dialogGetCompare.getImageFileDocument();
602       const ImageFile& rRHSIF = pRHSDoc->getImageFile();
603       ImageFileDocument* pNewDoc = theApp->newImageDoc();
604       if (! pNewDoc) {
605         sys_error (ERR_SEVERE, "Unable to create image file");
606         return;
607       }
608       ImageFile& newImage = pNewDoc->getImageFile();  
609       newImage.setArraySize (rIF.nx(), rIF.ny());
610       rIF.divideImages (rRHSIF, newImage);
611       std::ostringstream os;
612       os << "Divide image " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " by " 
613         << pRHSDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
614       wxString s = GetDocument()->GetFirstView()->GetFrame()->GetTitle() + ": ";
615       newImage.labelsCopy (rIF, s.c_str());
616       s = pRHSDoc->GetFirstView()->GetFrame()->GetTitle() + ": ";
617       newImage.labelsCopy (rRHSIF, s.c_str());
618       newImage.labelAdd (os.str().c_str());
619       *theApp->getLog() << os.str().c_str() << "\n";
620       if (theApp->getAskDeleteNewDocs())
621         pNewDoc->Modify (true);
622       pNewDoc->UpdateAllViews (this);
623       pNewDoc->getView()->OnUpdate (this, NULL);
624       pNewDoc->getView()->getFrame()->Show(true);
625     }
626   }
627 }
628
629
630 #ifdef HAVE_FFT
631 void
632 ImageFileView::OnFFT (wxCommandEvent& event)
633 {
634   ImageFile& rIF = GetDocument()->getImageFile();
635   rIF.fft (rIF);
636   rIF.labelAdd ("FFT Image");
637   m_bMinSpecified = false;
638   m_bMaxSpecified = false;
639   if (theApp->getAskDeleteNewDocs())
640     GetDocument()->Modify (true);
641   GetDocument()->UpdateAllViews (this);
642 }
643
644 void
645 ImageFileView::OnIFFT (wxCommandEvent& event)
646 {
647   ImageFile& rIF = GetDocument()->getImageFile();
648   rIF.ifft (rIF);
649   rIF.labelAdd ("IFFT Image");
650   m_bMinSpecified = false;
651   m_bMaxSpecified = false;
652   if (theApp->getAskDeleteNewDocs())
653     GetDocument()->Modify (true);
654   GetDocument()->UpdateAllViews (this);
655 }
656
657 void
658 ImageFileView::OnFFTRows (wxCommandEvent& event)
659 {
660   ImageFile& rIF = GetDocument()->getImageFile();
661   rIF.fftRows (rIF);
662   rIF.labelAdd ("FFT Rows");
663   m_bMinSpecified = false;
664   m_bMaxSpecified = false;
665   if (theApp->getAskDeleteNewDocs())
666     GetDocument()->Modify (true);
667   GetDocument()->UpdateAllViews (this);
668 }
669
670 void
671 ImageFileView::OnIFFTRows (wxCommandEvent& event)
672 {
673   ImageFile& rIF = GetDocument()->getImageFile();
674   rIF.ifftRows (rIF);
675   rIF.labelAdd ("IFFT Rows");
676   m_bMinSpecified = false;
677   m_bMaxSpecified = false;
678   if (theApp->getAskDeleteNewDocs())
679     GetDocument()->Modify (true);
680   GetDocument()->UpdateAllViews (this);
681 }
682
683 void
684 ImageFileView::OnFFTCols (wxCommandEvent& event)
685 {
686   ImageFile& rIF = GetDocument()->getImageFile();
687   rIF.fftCols (rIF);
688   rIF.labelAdd ("FFT Columns");
689   m_bMinSpecified = false;
690   m_bMaxSpecified = false;
691   if (theApp->getAskDeleteNewDocs())
692     GetDocument()->Modify (true);
693   GetDocument()->UpdateAllViews (this);
694 }
695
696 void
697 ImageFileView::OnIFFTCols (wxCommandEvent& event)
698 {
699   ImageFile& rIF = GetDocument()->getImageFile();
700   rIF.ifftCols (rIF);
701   rIF.labelAdd ("IFFT Columns");
702   m_bMinSpecified = false;
703   m_bMaxSpecified = false;
704   if (theApp->getAskDeleteNewDocs())
705     GetDocument()->Modify (true);
706   GetDocument()->UpdateAllViews (this);
707 }
708 #endif
709
710 void
711 ImageFileView::OnFourier (wxCommandEvent& event)
712 {
713   ImageFile& rIF = GetDocument()->getImageFile();
714   wxProgressDialog dlgProgress (wxString("Fourier"), wxString("Fourier Progress"), 1, getFrameForChild(), wxPD_APP_MODAL);
715   rIF.fourier (rIF);
716   rIF.labelAdd ("Fourier Image");
717   m_bMinSpecified = false;
718   m_bMaxSpecified = false;
719   if (theApp->getAskDeleteNewDocs())
720     GetDocument()->Modify (true);
721   GetDocument()->UpdateAllViews (this);
722 }
723
724 void
725 ImageFileView::OnInverseFourier (wxCommandEvent& event)
726 {
727   ImageFile& rIF = GetDocument()->getImageFile();
728   wxProgressDialog dlgProgress (wxString("Inverse Fourier"), wxString("Inverse Fourier Progress"), 1, getFrameForChild(), wxPD_APP_MODAL);
729   rIF.inverseFourier (rIF);
730   rIF.labelAdd ("Inverse Fourier Image");
731   m_bMinSpecified = false;
732   m_bMaxSpecified = false;
733   if (theApp->getAskDeleteNewDocs())
734     GetDocument()->Modify (true);
735   GetDocument()->UpdateAllViews (this);
736 }
737
738 void
739 ImageFileView::OnShuffleNaturalToFourierOrder (wxCommandEvent& event)
740 {
741   ImageFile& rIF = GetDocument()->getImageFile();
742   Fourier::shuffleNaturalToFourierOrder (rIF);
743   rIF.labelAdd ("Shuffle Natural To Fourier Order");
744   m_bMinSpecified = false;
745   m_bMaxSpecified = false;
746   if (theApp->getAskDeleteNewDocs())
747     GetDocument()->Modify (true);
748   GetDocument()->UpdateAllViews (this);
749 }
750
751 void
752 ImageFileView::OnShuffleFourierToNaturalOrder (wxCommandEvent& event)
753 {
754   ImageFile& rIF = GetDocument()->getImageFile();
755   Fourier::shuffleFourierToNaturalOrder (rIF);
756   rIF.labelAdd ("Shuffle Fourier To Natural Order");
757   m_bMinSpecified = false;
758   m_bMaxSpecified = false;
759   if (theApp->getAskDeleteNewDocs())
760     GetDocument()->Modify (true);
761   GetDocument()->UpdateAllViews (this);
762 }
763
764 void
765 ImageFileView::OnMagnitude (wxCommandEvent& event)
766 {
767   ImageFile& rIF = GetDocument()->getImageFile();
768   if (rIF.isComplex()) {
769     rIF.magnitude (rIF);
770     rIF.labelAdd ("Magnitude of complex-image");
771     m_bMinSpecified = false;
772     m_bMaxSpecified = false;
773     if (theApp->getAskDeleteNewDocs())
774       GetDocument()->Modify (true);
775     GetDocument()->UpdateAllViews (this);
776   }
777 }
778
779 void
780 ImageFileView::OnPhase (wxCommandEvent& event)
781 {
782   ImageFile& rIF = GetDocument()->getImageFile();
783   if (rIF.isComplex()) {
784     rIF.phase (rIF);
785     rIF.labelAdd ("Phase of complex-image");
786     m_bMinSpecified = false;
787     m_bMaxSpecified = false;
788     if (theApp->getAskDeleteNewDocs())
789       GetDocument()->Modify (true);
790     GetDocument()->UpdateAllViews (this);
791   }
792 }
793
794
795 ImageFileCanvas* 
796 ImageFileView::CreateCanvas (wxFrame* parent)
797 {
798   ImageFileCanvas* pCanvas;
799   int width, height;
800   parent->GetClientSize(&width, &height);
801   
802   pCanvas = new ImageFileCanvas (this, parent, wxPoint(0, 0), wxSize(width, height), 0);
803   
804   pCanvas->SetScrollbars(20, 20, 50, 50);
805   pCanvas->SetBackgroundColour(*wxWHITE);
806   pCanvas->Clear();
807   
808   return pCanvas;
809 }
810
811 #if CTSIM_MDI
812 wxDocMDIChildFrame*
813 #else
814 wxDocChildFrame*
815 #endif
816 ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
817 {
818 #if CTSIM_MDI
819   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "ImageFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
820 #else
821   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "ImageFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
822 #endif
823   theApp->setIconForFrame (subframe);
824   
825   m_pFileMenu = new wxMenu;
826   
827   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
828   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
829   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
830   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
831   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
832   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
833   m_pFileMenu->Append(wxID_REVERT, "Re&vert");
834   
835   m_pFileMenu->AppendSeparator();
836   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
837   m_pFileMenu->Append(IFMENU_FILE_EXPORT, "&Export...");
838   
839   m_pFileMenu->AppendSeparator();
840   m_pFileMenu->Append(wxID_PRINT, "&Print...");
841   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
842   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
843   m_pFileMenu->AppendSeparator();
844   m_pFileMenu->Append(MAINMENU_IMPORT, "&Import...\tCtrl-M");
845 #ifdef CTSIM_MDI
846   m_pFileMenu->AppendSeparator();
847   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
848   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
849 #endif
850   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
851   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
852   
853   wxMenu *view_menu = new wxMenu;
854   view_menu->Append(IFMENU_VIEW_SCALE_MINMAX, "Display Scale S&et...\tCtrl-E");
855   view_menu->Append(IFMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
856   view_menu->Append(IFMENU_VIEW_SCALE_FULL, "Display F&ull Scale\tCtrl-U");
857   
858   wxMenu* filter_menu = new wxMenu;
859   filter_menu->Append (IFMENU_FILTER_INVERTVALUES, "&Invert Values");
860   filter_menu->Append (IFMENU_FILTER_SQUARE, "&Square");
861   filter_menu->Append (IFMENU_FILTER_SQRT, "Square &Root");
862   filter_menu->Append (IFMENU_FILTER_LOG, "&Log");
863   filter_menu->Append (IFMENU_FILTER_EXP, "&Exp");
864   filter_menu->AppendSeparator();
865 #ifdef HAVE_FFT
866   filter_menu->Append (IFMENU_FILTER_FFT, "2-D &FFT");
867   filter_menu->Append (IFMENU_FILTER_IFFT, "2-D &IFFT");
868   filter_menu->Append (IFMENU_FILTER_FFT_ROWS, "FFT Rows");
869   filter_menu->Append (IFMENU_FILTER_IFFT_ROWS, "IFFT Rows");
870   filter_menu->Append (IFMENU_FILTER_FFT_COLS, "FFT Columns");
871   filter_menu->Append (IFMENU_FILTER_IFFT_COLS, "IFFT Columns");
872   filter_menu->Append (IFMENU_FILTER_FOURIER, "2-D F&ourier");
873   filter_menu->Append (IFMENU_FILTER_INVERSE_FOURIER, "2-D Inverse Fo&urier");
874 #else
875   filter_menu->Append (IFMENU_FILTER_FOURIER, "&Fourier");
876   filter_menu->Append (IFMENU_FILTER_INVERSE_FOURIER, "&Inverse Fourier");
877 #endif
878   filter_menu->Append (IFMENU_FILTER_SHUFFLEFOURIERTONATURALORDER, "S&huffle Fourier to Natural Order");
879   filter_menu->Append (IFMENU_FILTER_SHUFFLENATURALTOFOURIERORDER, "Shu&ffle Natural to Fourier Order");
880   filter_menu->Append (IFMENU_FILTER_MAGNITUDE, "&Magnitude");
881   filter_menu->Append (IFMENU_FILTER_PHASE, "&Phase");
882   
883   wxMenu* image_menu = new wxMenu;
884   image_menu->Append (IFMENU_IMAGE_ADD, "&Add...");
885   image_menu->Append (IFMENU_IMAGE_SUBTRACT, "&Subtract...");
886   image_menu->Append (IFMENU_IMAGE_MULTIPLY, "&Multiply...");
887   image_menu->Append (IFMENU_IMAGE_DIVIDE, "&Divide...");
888   image_menu->AppendSeparator();
889   image_menu->Append (IFMENU_IMAGE_SCALESIZE, "S&cale Size...");
890 #if wxUSE_GLCANVAS
891   image_menu->Append (IFMENU_IMAGE_CONVERT3D, "Convert &3-D\tCtrl-3");
892 #endif
893   
894   m_pMenuAnalyze = new wxMenu;
895   m_pMenuAnalyze->Append (IFMENU_PLOT_ROW, "Plot &Row");
896   m_pMenuAnalyze->Append (IFMENU_PLOT_COL, "Plot &Column");
897   m_pMenuAnalyze->Append (IFMENU_PLOT_HISTOGRAM, "Plot &Histogram");
898   m_pMenuAnalyze->AppendSeparator();
899   m_pMenuAnalyze->Append (IFMENU_PLOT_FFT_ROW, "P&lot FFT Row");
900   m_pMenuAnalyze->Append (IFMENU_PLOT_FFT_COL, "Plo&t FFT Column");
901   m_pMenuAnalyze->AppendSeparator();
902   m_pMenuAnalyze->Append (IFMENU_COMPARE_IMAGES, "Compare &Images...");
903   m_pMenuAnalyze->Append (IFMENU_COMPARE_ROW, "Compare Ro&w");
904   m_pMenuAnalyze->Append (IFMENU_COMPARE_COL, "Compare Colu&mn");
905   m_pMenuAnalyze->Enable (IFMENU_PLOT_ROW, false);
906   m_pMenuAnalyze->Enable (IFMENU_PLOT_COL, false);
907   m_pMenuAnalyze->Enable (IFMENU_COMPARE_ROW, false);
908   m_pMenuAnalyze->Enable (IFMENU_COMPARE_COL, false);
909   m_pMenuAnalyze->Enable (IFMENU_PLOT_FFT_ROW, false);
910   m_pMenuAnalyze->Enable (IFMENU_PLOT_FFT_COL, false);
911   
912   wxMenu *help_menu = new wxMenu;
913   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
914   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
915   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
916   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
917   
918   wxMenuBar *menu_bar = new wxMenuBar;
919   
920   menu_bar->Append(m_pFileMenu, "&File");
921   menu_bar->Append(view_menu, "&View");
922   menu_bar->Append(image_menu, "&Image");
923   menu_bar->Append(filter_menu, "Fi&lter");
924   menu_bar->Append(m_pMenuAnalyze, "&Analyze");
925   menu_bar->Append(help_menu, "&Help");
926   
927   subframe->SetMenuBar(menu_bar);
928   
929   subframe->Centre(wxBOTH);
930   
931   wxAcceleratorEntry accelEntries[5];
932   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('A'), IFMENU_VIEW_SCALE_AUTO);
933   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('U'), IFMENU_VIEW_SCALE_FULL);
934   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('E'), IFMENU_VIEW_SCALE_MINMAX);
935   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('I'), IFMENU_FILE_PROPERTIES);
936 #if wxUSE_GLCANVAS
937   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('3'), IFMENU_IMAGE_CONVERT3D);
938   wxAcceleratorTable accelTable (5, accelEntries);
939 #else
940   wxAcceleratorTable accelTable (4, accelEntries);
941 #endif
942   
943   subframe->SetAcceleratorTable (accelTable);
944   
945   return subframe;
946 }
947
948
949 bool 
950 ImageFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
951 {
952   m_pFrame = CreateChildFrame(doc, this);
953   
954   m_bMinSpecified = false;
955   m_bMaxSpecified = false;
956   m_dAutoScaleFactor = 1.;
957   
958   int width, height;
959   m_pFrame->GetClientSize (&width, &height);
960   m_pFrame->SetTitle("ImageFileView");
961   m_pCanvas = CreateCanvas (m_pFrame);
962   
963   int x, y;  // X requires a forced resize
964   m_pFrame->GetSize(&x, &y);
965   m_pFrame->SetSize(-1, -1, x, y);
966   m_pFrame->SetFocus();
967   m_pFrame->Show(true);
968   Activate(true);
969   
970   return true;
971 }
972
973 void 
974 ImageFileView::OnDraw (wxDC* dc)
975 {
976   wxSize sizeWindow = m_pFrame->GetClientSize();
977   wxSize sizeBest = m_pCanvas->GetBestSize();
978   if (sizeWindow.x > sizeBest.x || sizeWindow.y > sizeBest.y)
979     m_pFrame->SetClientSize (sizeBest);
980   
981   if (m_bitmap.Ok())
982     dc->DrawBitmap(m_bitmap, 0, 0, false);
983   
984   int xCursor, yCursor;
985   if (m_pCanvas->GetCurrentCursor (xCursor, yCursor))
986     m_pCanvas->DrawRubberBandCursor (*dc, xCursor, yCursor);
987 }
988
989
990 void 
991 ImageFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
992 {
993   const ImageFile& rIF = GetDocument()->getImageFile();
994   ImageFileArrayConst v = rIF.getArray();
995   int nx = rIF.nx();
996   int ny = rIF.ny();
997   if (v != NULL && nx != 0 && ny != 0) {
998     if (! m_bMinSpecified || ! m_bMaxSpecified) {
999       double min, max;
1000       rIF.getMinMax (min, max);
1001       if (! m_bMinSpecified)
1002         m_dMinPixel = min;
1003       if (! m_bMaxSpecified)
1004         m_dMaxPixel = max;
1005     }
1006     double scaleWidth = m_dMaxPixel - m_dMinPixel;
1007     
1008     unsigned char* imageData = new unsigned char [nx * ny * 3];
1009     if (! imageData) {
1010       sys_error (ERR_SEVERE, "Unable to allocate memory for Image display");
1011       return;
1012     }
1013     for (int ix = 0; ix < nx; ix++) {
1014       for (int iy = 0; iy < ny; iy++) {
1015         double scaleValue = ((v[ix][iy] - m_dMinPixel) / scaleWidth) * 255;
1016         int intensity = static_cast<int>(scaleValue + 0.5);
1017         intensity = clamp (intensity, 0, 255);
1018         int baseAddr = ((ny - 1 - iy) * nx + ix) * 3;
1019         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
1020       }
1021     }
1022     wxImage image (nx, ny, imageData, true);
1023     m_bitmap = image.ConvertToBitmap();
1024     delete imageData;
1025     int xSize = nx;
1026     int ySize = ny;
1027     ySize = clamp (ySize, 0, 800);
1028     m_pFrame->SetClientSize (xSize, ySize);
1029     m_pCanvas->SetScrollbars(20, 20, nx/20, ny/20);
1030     m_pCanvas->SetBackgroundColour(*wxWHITE);
1031   } 
1032   
1033   if (m_pCanvas)
1034     m_pCanvas->Refresh();
1035 }
1036
1037 bool 
1038 ImageFileView::OnClose (bool deleteWindow)
1039 {
1040   //GetDocumentManager()->ActivateView (this, false, true);
1041   if (! GetDocument() || ! GetDocument()->Close())
1042     return false;
1043   
1044   Activate (false);
1045   if (m_pCanvas) {
1046     m_pCanvas->setView(NULL);
1047     m_pCanvas = NULL;
1048   }
1049   wxString s(theApp->GetAppName());
1050   if (m_pFrame)
1051     m_pFrame->SetTitle(s);
1052   
1053   SetFrame(NULL);
1054   
1055   if (deleteWindow) {
1056     delete m_pFrame;
1057     m_pFrame = NULL;
1058     if (GetDocument() && GetDocument()->getBadFileOpen())
1059       ::wxYield();  // wxWindows bug workaround
1060   }
1061   
1062   return true;
1063 }
1064
1065 void
1066 ImageFileView::OnExport (wxCommandEvent& event)
1067 {
1068   ImageFile& rIF = GetDocument()->getImageFile();
1069   ImageFileArrayConst v = rIF.getArray();
1070   int nx = rIF.nx();
1071   int ny = rIF.ny();
1072   if (v != NULL && nx != 0 && ny != 0) {
1073     if (! m_bMinSpecified || ! m_bMaxSpecified) {
1074       double min, max;
1075       rIF.getMinMax (min, max);
1076       if (! m_bMinSpecified)
1077         m_dMinPixel = min;
1078       if (! m_bMaxSpecified)
1079         m_dMaxPixel = max;
1080     }
1081     
1082     DialogExportParameters dialogExport (getFrameForChild(), m_iDefaultExportFormatID);
1083     if (dialogExport.ShowModal() == wxID_OK) {
1084       wxString strFormatName (dialogExport.getFormatName ());
1085       m_iDefaultExportFormatID = ImageFile::convertExportFormatNameToID (strFormatName.c_str());
1086       
1087       wxString strExt;
1088       wxString strWildcard;
1089       if (m_iDefaultExportFormatID == ImageFile::EXPORT_FORMAT_PGM || m_iDefaultExportFormatID == ImageFile::EXPORT_FORMAT_PGMASCII) {
1090         strExt = ".pgm";
1091         strWildcard = "PGM Files (*.pgm)|*.pgm";
1092       }
1093 #ifdef HAVE_PNG
1094       else if (m_iDefaultExportFormatID == ImageFile::EXPORT_FORMAT_PNG || m_iDefaultExportFormatID == ImageFile::EXPORT_FORMAT_PNG16) {
1095         strExt = ".png";
1096         strWildcard = "PNG Files (*.png)|*.png";
1097       }
1098 #endif
1099 #ifdef HAVE_CTN_DICOM
1100       else if (m_iDefaultExportFormatID == ImageFile::EXPORT_FORMAT_DICOM) {
1101         strExt = "";
1102         strWildcard = "DICOM Files (*.*)|*.*";
1103       }
1104 #endif
1105       else {
1106         strExt = "";
1107         strWildcard = "Miscellaneous (*.*)|*.*";
1108       }
1109       
1110       const wxString& strFilename = wxFileSelector (wxString("Export Filename"), wxString(""), 
1111         wxString(""), strExt, strWildcard, wxOVERWRITE_PROMPT | wxHIDE_READONLY | wxSAVE);
1112       if (strFilename) {
1113         rIF.exportImage (strFormatName.c_str(), strFilename.c_str(), 1, 1, m_dMinPixel, m_dMaxPixel);
1114         *theApp->getLog() << "Exported file " << strFilename << "\n";
1115       }
1116     }
1117   }
1118 }
1119
1120 void
1121 ImageFileView::OnScaleSize (wxCommandEvent& event)
1122 {
1123   ImageFile& rIF = GetDocument()->getImageFile();
1124   unsigned int iOldNX = rIF.nx();
1125   unsigned int iOldNY = rIF.ny();
1126   
1127   DialogGetXYSize dialogGetXYSize (getFrameForChild(), "Set New X & Y Dimensions", iOldNX, iOldNY);
1128   if (dialogGetXYSize.ShowModal() == wxID_OK) {
1129     unsigned int iNewNX = dialogGetXYSize.getXSize();
1130     unsigned int iNewNY = dialogGetXYSize.getYSize();
1131     std::ostringstream os;
1132     os << "Scale Size from (" << iOldNX << "," << iOldNY << ") to (" << iNewNX << "," << iNewNY << ")";
1133     ImageFileDocument* pScaledDoc = theApp->newImageDoc();
1134     if (! pScaledDoc) {
1135       sys_error (ERR_SEVERE, "Unable to create image file");
1136       return;
1137     }
1138     ImageFile& rScaledIF = pScaledDoc->getImageFile();
1139     rScaledIF.setArraySize (iNewNX, iNewNY);
1140     rScaledIF.labelsCopy (rIF);
1141     rScaledIF.labelAdd (os.str().c_str());
1142     rIF.scaleImage (rScaledIF);
1143     *theApp->getLog() << os.str().c_str() << "\n";
1144     if (theApp->getAskDeleteNewDocs())
1145       pScaledDoc->Modify (true);
1146     pScaledDoc->UpdateAllViews (this);
1147     pScaledDoc->getView()->OnUpdate (this, NULL);
1148     pScaledDoc->getView()->getFrame()->Show(true);
1149   }
1150 }
1151
1152 #if wxUSE_GLCANVAS
1153 void
1154 ImageFileView::OnConvert3d (wxCommandEvent& event)
1155 {
1156   ImageFile& rIF = GetDocument()->getImageFile();
1157   Graph3dFileDocument* pGraph3d = theApp->newGraph3dDoc();
1158   pGraph3d->setBadFileOpen();
1159   pGraph3d->createFromImageFile (rIF);
1160   pGraph3d->getView()->OnUpdate (this, NULL);
1161   pGraph3d->UpdateAllViews();
1162   pGraph3d->getView()->getFrame()->SetClientSize (400, 400);
1163   pGraph3d->getView()->getFrame()->Show (true);
1164   GetDocumentManager()->ActivateView (pGraph3d->getView(), true, false);
1165   ::wxYield();
1166   pGraph3d->getView()->getCanvas()->SetFocus();
1167 }
1168 #endif
1169
1170 void
1171 ImageFileView::OnPlotRow (wxCommandEvent& event)
1172 {
1173   int xCursor, yCursor;
1174   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1175     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1176     return;
1177   }
1178   
1179   const ImageFile& rIF = GetDocument()->getImageFile();
1180   ImageFileArrayConst v = rIF.getArray();
1181   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1182   int nx = rIF.nx();
1183   int ny = rIF.ny();
1184   
1185   if (v != NULL && yCursor < ny) {
1186     double* pX = new double [nx];
1187     double* pYReal = new double [nx];
1188     double *pYImag = NULL;
1189     double *pYMag = NULL;
1190     if (rIF.isComplex()) {
1191       pYImag = new double [nx];
1192       pYMag = new double [nx];
1193     }
1194     for (int i = 0; i < nx; i++) {
1195       pX[i] = i;
1196       pYReal[i] = v[i][yCursor];
1197       if (rIF.isComplex()) {
1198         pYImag[i] = vImag[i][yCursor];
1199         pYMag[i] = ::sqrt (v[i][yCursor] * v[i][yCursor] + vImag[i][yCursor] * vImag[i][yCursor]);
1200       }
1201     }
1202     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1203     if (! pPlotDoc) {
1204       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1205     } else {
1206       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1207       std::ostringstream os;
1208       os << "Row " << yCursor;
1209       std::string title("title ");
1210       title += os.str();
1211       rPlotFile.addEzsetCommand (title.c_str());
1212       rPlotFile.addEzsetCommand ("xlabel Column");
1213       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1214       rPlotFile.addEzsetCommand ("lxfrac 0");
1215       rPlotFile.addEzsetCommand ("box");
1216       rPlotFile.addEzsetCommand ("grid");
1217       rPlotFile.addEzsetCommand ("curve 1");
1218       rPlotFile.addEzsetCommand ("color 1");
1219       if (rIF.isComplex()) {
1220         rPlotFile.addEzsetCommand ("dash 1");
1221         rPlotFile.addEzsetCommand ("curve 2");
1222         rPlotFile.addEzsetCommand ("color 4");
1223         rPlotFile.addEzsetCommand ("dash 3");
1224         rPlotFile.addEzsetCommand ("curve 3");
1225         rPlotFile.addEzsetCommand ("color 0");
1226         rPlotFile.addEzsetCommand ("solid");
1227         rPlotFile.setCurveSize (4, nx);
1228       } else
1229         rPlotFile.setCurveSize (2, nx);
1230       rPlotFile.addColumn (0, pX);
1231       rPlotFile.addColumn (1, pYReal); 
1232       if (rIF.isComplex()) {
1233         rPlotFile.addColumn (2, pYImag);
1234         rPlotFile.addColumn (3, pYMag);
1235       }
1236       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1237         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1238       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1239       *theApp->getLog() << os.str().c_str() << "\n";
1240       rPlotFile.addDescription (os.str().c_str());
1241     }
1242     delete pX;
1243     delete pYReal;
1244     if (rIF.isComplex()) {
1245       delete pYImag;
1246       delete pYMag;
1247     }
1248     if (theApp->getAskDeleteNewDocs())
1249       pPlotDoc->Modify (true);
1250     pPlotDoc->UpdateAllViews ();
1251     pPlotDoc->getView()->OnUpdate (this, NULL);
1252     pPlotDoc->getView()->getFrame()->Show(true);
1253   }
1254 }
1255
1256 void
1257 ImageFileView::OnPlotCol (wxCommandEvent& event)
1258 {
1259   int xCursor, yCursor;
1260   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1261     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1262     return;
1263   }
1264   
1265   const ImageFile& rIF = GetDocument()->getImageFile();
1266   ImageFileArrayConst v = rIF.getArray();
1267   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1268   int nx = rIF.nx();
1269   int ny = rIF.ny();
1270   
1271   if (v != NULL && xCursor < nx) {
1272     double* pX = new double [ny];
1273     double* pYReal = new double [ny];
1274     double* pYImag = NULL;
1275     double* pYMag = NULL;
1276     if (rIF.isComplex()) {
1277       pYImag = new double [ny];
1278       pYMag = new double [ny];
1279     }
1280     for (int i = 0; i < ny; i++) {
1281       pX[i] = i;
1282       pYReal[i] = v[xCursor][i];
1283       if (rIF.isComplex()) {
1284         pYImag[i] = vImag[xCursor][i];
1285         pYMag[i] = ::sqrt (v[xCursor][i] * v[xCursor][i] + vImag[xCursor][i] * vImag[xCursor][i]);
1286       }
1287     }
1288     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1289     if (! pPlotDoc) {
1290       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1291     } else {
1292       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1293       std::ostringstream os;
1294       os << "Column " << xCursor;
1295       std::string title("title ");
1296       title += os.str();
1297       rPlotFile.addEzsetCommand (title.c_str());
1298       rPlotFile.addEzsetCommand ("xlabel Row");
1299       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1300       rPlotFile.addEzsetCommand ("lxfrac 0");
1301       rPlotFile.addEzsetCommand ("box");
1302       rPlotFile.addEzsetCommand ("grid");
1303       rPlotFile.addEzsetCommand ("curve 1");
1304       rPlotFile.addEzsetCommand ("color 1");
1305       if (rIF.isComplex()) {
1306         rPlotFile.addEzsetCommand ("dash 1");
1307         rPlotFile.addEzsetCommand ("curve 2");
1308         rPlotFile.addEzsetCommand ("color 4");
1309         rPlotFile.addEzsetCommand ("dash 3");
1310         rPlotFile.addEzsetCommand ("curve 3");
1311         rPlotFile.addEzsetCommand ("color 0");
1312         rPlotFile.addEzsetCommand ("solid");
1313         rPlotFile.setCurveSize (4, ny);
1314       } else
1315         rPlotFile.setCurveSize (2, ny);
1316       rPlotFile.addColumn (0, pX);
1317       rPlotFile.addColumn (1, pYReal); 
1318       if (rIF.isComplex()) {
1319         rPlotFile.addColumn (2, pYImag);
1320         rPlotFile.addColumn (3, pYMag);
1321       }
1322       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1323         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1324       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1325       *theApp->getLog() << os.str().c_str() << "\n";
1326       rPlotFile.addDescription (os.str().c_str());
1327     }
1328     delete pX;
1329     delete pYReal;
1330     if (rIF.isComplex()) {
1331       delete pYImag;
1332       delete pYMag;
1333     }
1334     if (theApp->getAskDeleteNewDocs())
1335       pPlotDoc->Modify (true);
1336     pPlotDoc->UpdateAllViews ();
1337     pPlotDoc->getView()->OnUpdate (this, NULL);
1338     pPlotDoc->getView()->getFrame()->Show(true);
1339   }
1340 }
1341
1342 #ifdef HAVE_FFT
1343 void
1344 ImageFileView::OnPlotFFTRow (wxCommandEvent& event)
1345 {
1346   int xCursor, yCursor;
1347   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1348     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1349     return;
1350   }
1351   
1352   const ImageFile& rIF = GetDocument()->getImageFile();
1353   ImageFileArrayConst v = rIF.getArray();
1354   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1355   int nx = rIF.nx();
1356   int ny = rIF.ny();
1357   
1358   if (v != NULL && yCursor < ny) {
1359     fftw_complex* pcIn = new fftw_complex [nx];
1360     
1361     int i;
1362     for (i = 0; i < nx; i++) {
1363       pcIn[i].re = v[i][yCursor];
1364       if (rIF.isComplex())
1365         pcIn[i].im = vImag[i][yCursor];
1366       else
1367         pcIn[i].im = 0;
1368     }
1369     
1370     fftw_plan plan = fftw_create_plan (nx, FFTW_FORWARD, FFTW_IN_PLACE);
1371     fftw_one (plan, pcIn, NULL);
1372     fftw_destroy_plan (plan);
1373     
1374     double* pX = new double [nx];
1375     double* pYReal = new double [nx];
1376     double* pYImag = new double [nx];
1377     double* pYMag = new double [nx];
1378     for (i = 0; i < nx; i++) {
1379       pX[i] = i;
1380       pYReal[i] = pcIn[i].re;
1381       pYImag[i] = pcIn[i].im;
1382       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1383     }
1384     Fourier::shuffleFourierToNaturalOrder (pYReal, nx);
1385     Fourier::shuffleFourierToNaturalOrder (pYImag, nx);
1386     Fourier::shuffleFourierToNaturalOrder (pYMag, nx);
1387     
1388     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1389     if (! pPlotDoc) {
1390       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1391     } else {
1392       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1393       std::ostringstream os;
1394       os << "Row " << yCursor;
1395       std::string title("title ");
1396       title += os.str();
1397       rPlotFile.addEzsetCommand (title.c_str());
1398       rPlotFile.addEzsetCommand ("xlabel Column");
1399       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1400       rPlotFile.addEzsetCommand ("lxfrac 0");
1401       rPlotFile.addEzsetCommand ("curve 1");
1402       rPlotFile.addEzsetCommand ("color 1");
1403       rPlotFile.addEzsetCommand ("dash 1");
1404       rPlotFile.addEzsetCommand ("curve 2");
1405       rPlotFile.addEzsetCommand ("color 4");
1406       rPlotFile.addEzsetCommand ("dash 3");
1407       rPlotFile.addEzsetCommand ("curve 3");
1408       rPlotFile.addEzsetCommand ("color 0");
1409       rPlotFile.addEzsetCommand ("solid");
1410       rPlotFile.addEzsetCommand ("box");
1411       rPlotFile.addEzsetCommand ("grid");
1412       rPlotFile.setCurveSize (4, nx);
1413       rPlotFile.addColumn (0, pX);
1414       rPlotFile.addColumn (1, pYReal);
1415       rPlotFile.addColumn (2, pYImag);
1416       rPlotFile.addColumn (3, pYMag);
1417       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1418         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1419       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1420       *theApp->getLog() << os.str().c_str() << "\n";
1421       rPlotFile.addDescription (os.str().c_str());
1422     }
1423     delete pX;
1424     delete pYReal;
1425     delete pYImag;
1426     delete pYMag;
1427     delete [] pcIn;
1428     
1429     if (theApp->getAskDeleteNewDocs())
1430       pPlotDoc->Modify (true);
1431     pPlotDoc->UpdateAllViews ();
1432     pPlotDoc->getView()->OnUpdate (this, NULL);
1433     pPlotDoc->getView()->getFrame()->Show(true);
1434   }
1435 }
1436
1437 void
1438 ImageFileView::OnPlotFFTCol (wxCommandEvent& event)
1439 {
1440   int xCursor, yCursor;
1441   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1442     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1443     return;
1444   }
1445   
1446   const ImageFile& rIF = GetDocument()->getImageFile();
1447   ImageFileArrayConst v = rIF.getArray();
1448   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1449   int nx = rIF.nx();
1450   int ny = rIF.ny();
1451   
1452   if (v != NULL && xCursor < nx) {
1453     fftw_complex* pcIn = new fftw_complex [ny];
1454     double *pdTemp = new double [ny];
1455     
1456     int i;
1457     for (i = 0; i < ny; i++)
1458       pdTemp[i] = v[xCursor][i];
1459     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1460     for (i = 0; i < ny; i++) 
1461       pcIn[i].re = pdTemp[i];
1462     
1463     for (i = 0; i < ny; i++) {
1464       if (rIF.isComplex())
1465         pdTemp[i] = vImag[xCursor][i];
1466       else
1467         pdTemp[i] = 0;
1468     }
1469     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1470     for (i = 0; i < ny; i++)
1471       pcIn[i].im = pdTemp[i];
1472     
1473     fftw_plan plan = fftw_create_plan (ny, FFTW_BACKWARD, FFTW_IN_PLACE);
1474     fftw_one (plan, pcIn, NULL);
1475     fftw_destroy_plan (plan);
1476     
1477     double* pX = new double [ny];
1478     double* pYReal = new double [ny];
1479     double* pYImag = new double [ny];
1480     double* pYMag = new double [ny];
1481     for (i = 0; i < ny; i++) {
1482       pX[i] = i;
1483       pYReal[i] = pcIn[i].re;
1484       pYImag[i] = pcIn[i].im;
1485       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1486     }
1487     
1488     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1489     if (! pPlotDoc) {
1490       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1491     } else {
1492       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1493       std::ostringstream os;
1494       os << "Column " << xCursor;
1495       std::string title("title ");
1496       title += os.str();
1497       rPlotFile.addEzsetCommand (title.c_str());
1498       rPlotFile.addEzsetCommand ("xlabel Column");
1499       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1500       rPlotFile.addEzsetCommand ("lxfrac 0");
1501       rPlotFile.addEzsetCommand ("curve 1");
1502       rPlotFile.addEzsetCommand ("color 1");
1503       rPlotFile.addEzsetCommand ("dash 1");
1504       rPlotFile.addEzsetCommand ("curve 2");
1505       rPlotFile.addEzsetCommand ("color 4");
1506       rPlotFile.addEzsetCommand ("dash 3");
1507       rPlotFile.addEzsetCommand ("curve 3");
1508       rPlotFile.addEzsetCommand ("color 0");
1509       rPlotFile.addEzsetCommand ("solid");
1510       rPlotFile.addEzsetCommand ("box");
1511       rPlotFile.addEzsetCommand ("grid");
1512       rPlotFile.setCurveSize (4, ny);
1513       rPlotFile.addColumn (0, pX);
1514       rPlotFile.addColumn (1, pYReal);
1515       rPlotFile.addColumn (2, pYImag);
1516       rPlotFile.addColumn (3, pYMag);
1517       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1518         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1519       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1520       *theApp->getLog() << os.str().c_str() << "\n";
1521       rPlotFile.addDescription (os.str().c_str());
1522     }
1523     delete pX;
1524     delete pYReal;
1525     delete pYImag;
1526     delete pYMag;
1527     delete pdTemp;
1528     delete [] pcIn;
1529     
1530     if (theApp->getAskDeleteNewDocs())
1531       pPlotDoc->Modify (true);
1532     pPlotDoc->UpdateAllViews ();
1533     pPlotDoc->getView()->OnUpdate (this, NULL);
1534     pPlotDoc->getView()->getFrame()->Show(true);
1535   }
1536 }
1537 #endif
1538
1539 void
1540 ImageFileView::OnCompareCol (wxCommandEvent& event)
1541 {
1542   int xCursor, yCursor;
1543   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1544     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1545     return;
1546   }
1547   
1548   std::vector<ImageFileDocument*> vecIFDoc;
1549   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1550   if (vecIFDoc.size() == 0) {
1551     wxMessageBox ("No compatible images for Column Comparison", "Error");
1552     return;
1553   }
1554   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1555   
1556   if (dialogGetCompare.ShowModal() == wxID_OK) {
1557     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1558     const ImageFile& rIF = GetDocument()->getImageFile();
1559     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1560     
1561     ImageFileArrayConst v1 = rIF.getArray();
1562     ImageFileArrayConst v2 = rCompareIF.getArray();
1563     int nx = rIF.nx();
1564     int ny = rIF.ny();
1565     
1566     if (v1 != NULL && xCursor < nx) {
1567       double* pX = new double [ny];
1568       double* pY1 = new double [ny];
1569       double* pY2 = new double [ny];
1570       for (int i = 0; i < ny; i++) {
1571         pX[i] = i;
1572         pY1[i] = v1[xCursor][i];
1573         pY2[i] = v2[xCursor][i];
1574       }
1575       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1576       if (! pPlotDoc) {
1577         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1578       } else {
1579         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1580         std::ostringstream os;
1581         os << "Column " << xCursor << " Comparison";
1582         std::string title("title ");
1583         title += os.str();
1584         rPlotFile.addEzsetCommand (title.c_str());
1585         rPlotFile.addEzsetCommand ("xlabel Row");
1586         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1587         rPlotFile.addEzsetCommand ("lxfrac 0");
1588         rPlotFile.addEzsetCommand ("curve 1");
1589         rPlotFile.addEzsetCommand ("color 2");
1590         rPlotFile.addEzsetCommand ("curve 2");
1591         rPlotFile.addEzsetCommand ("color 4");
1592         rPlotFile.addEzsetCommand ("dash 5");
1593         rPlotFile.addEzsetCommand ("box");
1594         rPlotFile.addEzsetCommand ("grid");
1595         rPlotFile.setCurveSize (3, ny);
1596         rPlotFile.addColumn (0, pX);
1597         rPlotFile.addColumn (1, pY1);
1598         rPlotFile.addColumn (2, pY2);
1599         
1600         unsigned int iL;
1601         for (iL = 0; iL < rIF.nLabels(); iL++) {
1602           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1603           s += ": ";
1604           s += rIF.labelGet(iL).getLabelString();
1605           rPlotFile.addDescription (s.c_str());
1606         }
1607         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1608           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1609           s += ": ";
1610           s += rCompareIF.labelGet(iL).getLabelString();
1611           rPlotFile.addDescription (s.c_str());
1612         }
1613         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1614           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1615         *theApp->getLog() << os.str().c_str() << "\n";
1616         rPlotFile.addDescription (os.str().c_str());
1617       }
1618       delete pX;
1619       delete pY1;
1620       delete pY2;
1621       if (theApp->getAskDeleteNewDocs())
1622         pPlotDoc->Modify (true);
1623       pPlotDoc->UpdateAllViews ();
1624       pPlotDoc->getView()->OnUpdate (this, NULL);
1625       pPlotDoc->getView()->getFrame()->Show(true);
1626     }
1627   }
1628 }
1629
1630 void
1631 ImageFileView::OnCompareRow (wxCommandEvent& event)
1632 {
1633   int xCursor, yCursor;
1634   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1635     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1636     return;
1637   }
1638   
1639   std::vector<ImageFileDocument*> vecIFDoc;
1640   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1641   
1642   if (vecIFDoc.size() == 0) {
1643     wxMessageBox ("No compatible images for Row Comparison", "Error");
1644     return;
1645   }
1646   
1647   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1648   
1649   if (dialogGetCompare.ShowModal() == wxID_OK) {
1650     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1651     const ImageFile& rIF = GetDocument()->getImageFile();
1652     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1653     
1654     ImageFileArrayConst v1 = rIF.getArray();
1655     ImageFileArrayConst v2 = rCompareIF.getArray();
1656     int nx = rIF.nx();
1657     int ny = rIF.ny();
1658     
1659     if (v1 != NULL && yCursor < ny) {
1660       double* pX = new double [nx];
1661       double* pY1 = new double [nx];
1662       double* pY2 = new double [nx];
1663       for (int i = 0; i < nx; i++) {
1664         pX[i] = i;
1665         pY1[i] = v1[i][yCursor];
1666         pY2[i] = v2[i][yCursor];
1667       }
1668       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1669       if (! pPlotDoc) {
1670         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1671       } else {
1672         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1673         std::ostringstream os;
1674         os << "Row " << yCursor << " Comparison";
1675         std::string title("title ");
1676         title += os.str();
1677         rPlotFile.addEzsetCommand (title.c_str());
1678         rPlotFile.addEzsetCommand ("xlabel Column");
1679         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1680         rPlotFile.addEzsetCommand ("lxfrac 0");
1681         rPlotFile.addEzsetCommand ("curve 1");
1682         rPlotFile.addEzsetCommand ("color 2");
1683         rPlotFile.addEzsetCommand ("curve 2");
1684         rPlotFile.addEzsetCommand ("color 4");
1685         rPlotFile.addEzsetCommand ("dash 5");
1686         rPlotFile.addEzsetCommand ("box");
1687         rPlotFile.addEzsetCommand ("grid");
1688         rPlotFile.setCurveSize (3, nx);
1689         rPlotFile.addColumn (0, pX);
1690         rPlotFile.addColumn (1, pY1);
1691         rPlotFile.addColumn (2, pY2);
1692         unsigned int iL;
1693         for (iL = 0; iL < rIF.nLabels(); iL++) {
1694           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1695           s += ": ";
1696           s += rIF.labelGet(iL).getLabelString();
1697           rPlotFile.addDescription (s.c_str());
1698         }
1699         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1700           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1701           s += ": ";
1702           s += rCompareIF.labelGet(iL).getLabelString();
1703           rPlotFile.addDescription (s.c_str());
1704         }
1705         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1706           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1707         *theApp->getLog() << os.str().c_str() << "\n";
1708         rPlotFile.addDescription (os.str().c_str());
1709       }
1710       delete pX;
1711       delete pY1;
1712       delete pY2;
1713       if (theApp->getAskDeleteNewDocs())
1714         pPlotDoc->Modify (true);
1715       pPlotDoc->UpdateAllViews ();
1716       pPlotDoc->getView()->OnUpdate (this, NULL);
1717       pPlotDoc->getView()->getFrame()->Show(true);
1718     }
1719   }
1720 }
1721
1722 static int NUMBER_HISTOGRAM_BINS = 256;
1723
1724 void
1725 ImageFileView::OnPlotHistogram (wxCommandEvent& event)
1726
1727   const ImageFile& rIF = GetDocument()->getImageFile();
1728   ImageFileArrayConst v = rIF.getArray();
1729   int nx = rIF.nx();
1730   int ny = rIF.ny();
1731   
1732   if (v != NULL && nx > 0 && ny > 0) {
1733     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1734     if (! pPlotDoc) {
1735       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1736       return;
1737     }
1738     
1739     double* pX = new double [NUMBER_HISTOGRAM_BINS];
1740     double* pY = new double [NUMBER_HISTOGRAM_BINS];
1741     double dMin, dMax;
1742     rIF.getMinMax (dMin, dMax);
1743     double dBinWidth = (dMax - dMin) / NUMBER_HISTOGRAM_BINS;
1744     
1745     for (int i = 0; i < NUMBER_HISTOGRAM_BINS; i++) {
1746       pX[i] = dMin + (i + 0.5) * dBinWidth;
1747       pY[i] = 0;
1748     }
1749     for (int ix = 0; ix < nx; ix++)
1750       for (int iy = 0; iy < ny; iy++) {
1751         int iBin = nearest<int> ((v[ix][iy] - dMin) / dBinWidth);
1752         if (iBin >= 0 && iBin < NUMBER_HISTOGRAM_BINS)
1753           pY[iBin] += 1;
1754       }
1755       
1756       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1757       std::ostringstream os;
1758       os << "Histogram";
1759       std::string title("title ");
1760       title += os.str();
1761       rPlotFile.addEzsetCommand (title.c_str());
1762       rPlotFile.addEzsetCommand ("xlabel Pixel Value");
1763       rPlotFile.addEzsetCommand ("ylabel Count");
1764       rPlotFile.addEzsetCommand ("box");
1765       rPlotFile.addEzsetCommand ("grid");
1766       rPlotFile.setCurveSize (2, NUMBER_HISTOGRAM_BINS);
1767       rPlotFile.addColumn (0, pX);
1768       rPlotFile.addColumn (1, pY);
1769       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++) {
1770         std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1771         s += ": ";
1772         s += rIF.labelGet(iL).getLabelString();
1773         rPlotFile.addDescription (s.c_str());
1774       }
1775       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1776       *theApp->getLog() << os.str().c_str() << "\n";
1777       rPlotFile.addDescription (os.str().c_str());
1778       delete pX;
1779       delete pY;
1780       if (theApp->getAskDeleteNewDocs())
1781         pPlotDoc->Modify (true);
1782       pPlotDoc->UpdateAllViews ();
1783       pPlotDoc->getView()->OnUpdate (this, NULL);
1784       pPlotDoc->getView()->getFrame()->Show(true);
1785   }
1786 }
1787
1788
1789 // PhantomCanvas
1790
1791 PhantomCanvas::PhantomCanvas (PhantomFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
1792 : wxScrolledWindow(frame, -1, pos, size, style)
1793 {
1794   m_pView = v;
1795 }
1796
1797 PhantomCanvas::~PhantomCanvas ()
1798 {
1799   m_pView = NULL;
1800 }
1801
1802 void 
1803 PhantomCanvas::OnDraw (wxDC& dc)
1804 {
1805   if (m_pView)
1806     m_pView->OnDraw(& dc);
1807 }
1808
1809 wxSize
1810 PhantomCanvas::GetBestSize() const
1811 {
1812   if (! m_pView)
1813     return wxSize(0,0);
1814   
1815   int xSize, ySize;
1816   theApp->getMainFrame()->GetClientSize (&xSize, &ySize);
1817   xSize = maxValue<int> (xSize, ySize);
1818   ySize = xSize = (xSize / 4);
1819   return wxSize (xSize, ySize);
1820 }
1821
1822
1823
1824 // PhantomFileView
1825
1826 IMPLEMENT_DYNAMIC_CLASS(PhantomFileView, wxView)
1827
1828 BEGIN_EVENT_TABLE(PhantomFileView, wxView)
1829 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomFileView::OnProperties)
1830 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomFileView::OnRasterize)
1831 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomFileView::OnProjections)
1832 END_EVENT_TABLE()
1833
1834 PhantomFileView::PhantomFileView() 
1835 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pFileMenu(0)
1836 {
1837 #if defined(DEBUG) || defined(_DEBUG)
1838   m_iDefaultNDet = 165;
1839   m_iDefaultNView = 180;
1840   m_iDefaultNSample = 1;
1841 #else
1842   m_iDefaultNDet = 367;
1843   m_iDefaultNView = 320;
1844   m_iDefaultNSample = 2;
1845 #endif
1846   m_dDefaultRotation = 1;
1847   m_dDefaultFocalLength = 2;
1848   m_dDefaultCenterDetectorLength = 2;
1849   m_dDefaultViewRatio = 1;
1850   m_dDefaultScanRatio = 1;
1851   m_iDefaultGeometry = Scanner::GEOMETRY_PARALLEL;
1852   m_iDefaultTrace = Trace::TRACE_NONE;
1853   
1854 #ifdef DEBUG 
1855   m_iDefaultRasterNX = 115;
1856   m_iDefaultRasterNY = 115;
1857   m_iDefaultRasterNSamples = 1;
1858 #else
1859   m_iDefaultRasterNX = 256;
1860   m_iDefaultRasterNY = 256;
1861   m_iDefaultRasterNSamples = 2;
1862 #endif
1863   m_dDefaultRasterViewRatio = 1;
1864 }
1865
1866 PhantomFileView::~PhantomFileView()
1867 {
1868   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
1869   GetDocumentManager()->ActivateView(this, FALSE, TRUE);
1870 }
1871
1872 void
1873 PhantomFileView::OnProperties (wxCommandEvent& event)
1874 {
1875   const int idPhantom = GetDocument()->getPhantomID();
1876   const wxString& namePhantom = GetDocument()->getPhantomName();
1877   std::ostringstream os;
1878   os << "Phantom " << namePhantom.c_str() << " (" << idPhantom << ")" << "\n";
1879   const Phantom& rPhantom = GetDocument()->getPhantom();
1880   rPhantom.printDefinitions (os);
1881 #if DEBUG
1882   rPhantom.print (os);
1883 #endif
1884   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
1885   wxMessageBox (os.str().c_str(), "Phantom Properties");
1886 }
1887
1888
1889 void
1890 PhantomFileView::OnProjections (wxCommandEvent& event)
1891 {
1892   DialogGetProjectionParameters dialogProjection (getFrameForChild(), 
1893     m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, 
1894     m_dDefaultFocalLength, m_dDefaultCenterDetectorLength, m_dDefaultViewRatio, m_dDefaultScanRatio, 
1895     m_iDefaultGeometry, m_iDefaultTrace);
1896   int retVal = dialogProjection.ShowModal();
1897   if (retVal != wxID_OK) 
1898     return;
1899   
1900   m_iDefaultNDet = dialogProjection.getNDet();
1901   m_iDefaultNView = dialogProjection.getNView();
1902   m_iDefaultNSample = dialogProjection.getNSamples();
1903   m_iDefaultTrace = dialogProjection.getTrace();
1904   m_dDefaultRotation = dialogProjection.getRotAngle();
1905   m_dDefaultFocalLength = dialogProjection.getFocalLengthRatio();
1906   m_dDefaultCenterDetectorLength = dialogProjection.getCenterDetectorLengthRatio();
1907   m_dDefaultViewRatio = dialogProjection.getViewRatio();
1908   m_dDefaultScanRatio = dialogProjection.getScanRatio();
1909   wxString sGeometry = dialogProjection.getGeometry();
1910   m_iDefaultGeometry = Scanner::convertGeometryNameToID (sGeometry.c_str());
1911   double dRotationRadians = m_dDefaultRotation;
1912   m_dDefaultRotation /= TWOPI;  // convert back to fraction of a circle
1913   
1914   if (m_iDefaultNDet <= 0 || m_iDefaultNView <= 0 || sGeometry == "")
1915     return;
1916   
1917   const Phantom& rPhantom = GetDocument()->getPhantom();
1918   Scanner theScanner (rPhantom, sGeometry.c_str(), m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, 
1919     dRotationRadians, m_dDefaultFocalLength, m_dDefaultCenterDetectorLength, m_dDefaultViewRatio, m_dDefaultScanRatio);
1920   if (theScanner.fail()) {
1921     wxString msg = "Failed making scanner\n";
1922     msg += theScanner.failMessage().c_str();
1923     *theApp->getLog() << msg << "\n";
1924     wxMessageBox (msg, "Error");
1925     return;
1926   }
1927   
1928   std::ostringstream os;
1929   os << "Projections for " << rPhantom.name() << ": nDet=" << m_iDefaultNDet 
1930     << ", nView=" << m_iDefaultNView << ", nSamples=" << m_iDefaultNSample 
1931     << ", RotAngle=" << m_dDefaultRotation << ", FocalLengthRatio=" << m_dDefaultFocalLength 
1932     << ", CenterDetectorLengthRatio=" << m_dDefaultCenterDetectorLength
1933     << ", ViewRatio=" << m_dDefaultViewRatio << ", ScanRatio=" << m_dDefaultScanRatio 
1934     << ", Geometry=" << sGeometry.c_str() << ", FanBeamAngle=" << 
1935     convertRadiansToDegrees (theScanner.fanBeamAngle());
1936   
1937   Timer timer;
1938   Projections* pProj = NULL;
1939   if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
1940     pProj = new Projections;
1941     pProj->initFromScanner (theScanner);
1942     
1943     ProjectionsDialog dialogProjections (theScanner, *pProj, rPhantom, m_iDefaultTrace, dynamic_cast<wxWindow*>(getFrameForChild()));
1944     for (int iView = 0; iView < pProj->nView(); iView++) {
1945       ::wxYield();
1946       if (dialogProjections.isCancelled() || ! dialogProjections.projectView (iView)) {
1947         delete pProj;
1948         return;
1949       }
1950       ::wxYield();
1951       while (dialogProjections.isPaused()) {
1952         ::wxYield();
1953         ::wxUsleep(50);
1954       }
1955     }
1956   } else {
1957 #if HAVE_WXTHREADS
1958     if (theApp->getUseBackgroundTasks()) {
1959       ProjectorSupervisorThread* pProjector = new ProjectorSupervisorThread (this, m_iDefaultNDet,
1960         m_iDefaultNView, sGeometry.c_str(), m_iDefaultNSample, dRotationRadians,
1961         m_dDefaultFocalLength, m_dDefaultCenterDetectorLength, m_dDefaultViewRatio, m_dDefaultScanRatio, os.str().c_str());
1962       if (pProjector->Create() != wxTHREAD_NO_ERROR) {
1963         sys_error (ERR_SEVERE, "Error creating projector thread");
1964         delete pProjector;
1965         return;
1966       }
1967       pProjector->SetPriority(60);
1968       pProjector->Run();
1969       return;
1970     } else      
1971 #endif // HAVE_WXTHREADS
1972         {
1973       pProj = new Projections;
1974       pProj->initFromScanner (theScanner);
1975       wxProgressDialog dlgProgress (wxString("Projection"), wxString("Projection Progress"), pProj->nView() + 1, getFrameForChild(), wxPD_CAN_ABORT );
1976       for (int i = 0; i < pProj->nView(); i++) {
1977         theScanner.collectProjections (*pProj, rPhantom, i, 1, true, m_iDefaultTrace);
1978         if (! dlgProgress.Update (i+1)) {
1979           delete pProj;
1980           return;
1981         }
1982       }
1983     }
1984   }
1985   
1986   *theApp->getLog() << os.str().c_str() << "\n";
1987   pProj->setRemark (os.str());
1988   pProj->setCalcTime (timer.timerEnd());
1989   
1990   ProjectionFileDocument* pProjectionDoc = theApp->newProjectionDoc();
1991   if (! pProjectionDoc) {
1992     sys_error (ERR_SEVERE, "Unable to create projection document");
1993     return;
1994   }
1995   pProjectionDoc->setProjections (pProj);
1996   ProjectionFileView* projView = pProjectionDoc->getView();
1997   if (projView) {
1998     projView->OnUpdate (projView, NULL);
1999     if (projView->getCanvas())
2000       projView->getCanvas()->SetClientSize (m_iDefaultNDet, m_iDefaultNView);
2001     if (wxFrame* pFrame = projView->getFrame()) {
2002       pFrame->Show(true);
2003       pFrame->SetFocus();
2004       pFrame->Raise();
2005     }
2006     GetDocumentManager()->ActivateView (projView, true, false);
2007   }
2008   if (theApp->getAskDeleteNewDocs())
2009     pProjectionDoc-> Modify(true);
2010   pProjectionDoc->UpdateAllViews (this);
2011 }
2012
2013
2014 void
2015 PhantomFileView::OnRasterize (wxCommandEvent& event)
2016 {
2017   DialogGetRasterParameters dialogRaster (getFrameForChild(), m_iDefaultRasterNX, m_iDefaultRasterNY, 
2018     m_iDefaultRasterNSamples, m_dDefaultRasterViewRatio);
2019   int retVal = dialogRaster.ShowModal();
2020   if (retVal != wxID_OK)
2021     return;
2022   
2023   m_iDefaultRasterNX = dialogRaster.getXSize();
2024   m_iDefaultRasterNY  = dialogRaster.getYSize();
2025   m_iDefaultRasterNSamples = dialogRaster.getNSamples();
2026   m_dDefaultRasterViewRatio = dialogRaster.getViewRatio();
2027   if (m_iDefaultRasterNSamples < 1)
2028     m_iDefaultRasterNSamples = 1;
2029   if (m_dDefaultRasterViewRatio < 0)
2030     m_dDefaultRasterViewRatio = 0;
2031   if (m_iDefaultRasterNX <= 0 || m_iDefaultRasterNY <= 0) 
2032     return;
2033   
2034   const Phantom& rPhantom = GetDocument()->getPhantom();
2035   std::ostringstream os;
2036   os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << m_iDefaultRasterNX << ", YSize=" 
2037     << m_iDefaultRasterNY << ", ViewRatio=" << m_dDefaultRasterViewRatio << ", nSamples=" 
2038     << m_iDefaultRasterNSamples;;
2039
2040 #if HAVE_WXTHREADS
2041   if (theApp->getUseBackgroundTasks()) {
2042     RasterizerSupervisorThread* pThread = new RasterizerSupervisorThread (this, m_iDefaultRasterNX, m_iDefaultRasterNY,
2043       m_iDefaultRasterNSamples, m_dDefaultRasterViewRatio, os.str().c_str());
2044     if (pThread->Create() != wxTHREAD_NO_ERROR) {
2045       *theApp->getLog() << "Error creating rasterizer thread\n";
2046       return;
2047     }
2048     pThread->SetPriority (60);
2049     pThread->Run();
2050   } else 
2051 #endif
2052   {
2053     ImageFile* pImageFile = new ImageFile (m_iDefaultRasterNX, m_iDefaultRasterNY);
2054     wxProgressDialog dlgProgress (wxString("Rasterize"), wxString("Rasterization Progress"), 
2055       pImageFile->nx() + 1, getFrameForChild(), wxPD_CAN_ABORT );
2056     Timer timer;
2057     for (unsigned int i = 0; i < pImageFile->nx(); i++) {
2058       rPhantom.convertToImagefile (*pImageFile, m_dDefaultRasterViewRatio, m_iDefaultRasterNSamples, Trace::TRACE_NONE, i, 1, true);
2059       if (! dlgProgress.Update (i+1)) {
2060         delete pImageFile;
2061         return;
2062       }
2063     }
2064   
2065     ImageFileDocument* pRasterDoc = theApp->newImageDoc();
2066     if (! pRasterDoc) {
2067       sys_error (ERR_SEVERE, "Unable to create image file");
2068       return;
2069     }
2070     pRasterDoc->setImageFile (pImageFile);
2071     if (theApp->getAskDeleteNewDocs())
2072       pRasterDoc->Modify (true);
2073     pRasterDoc->UpdateAllViews (this);
2074     pRasterDoc->getView()->getFrame()->Show(true);
2075     *theApp->getLog() << os.str().c_str() << "\n";
2076     pImageFile->labelAdd (os.str().c_str(), timer.timerEnd());
2077     ImageFileView* rasterView = pRasterDoc->getView();
2078     if (rasterView) {
2079       rasterView->getFrame()->SetFocus();
2080       rasterView->OnUpdate (rasterView, NULL);
2081     }
2082   }
2083 }
2084
2085
2086 PhantomCanvas* 
2087 PhantomFileView::CreateCanvas (wxFrame *parent)
2088 {
2089   PhantomCanvas* pCanvas;
2090   
2091   pCanvas = new PhantomCanvas (this, parent, wxPoint(0, 0), wxSize(0,0), 0);
2092   pCanvas->SetBackgroundColour(*wxWHITE);
2093   pCanvas->Clear();
2094   
2095   return pCanvas;
2096 }
2097
2098 #if CTSIM_MDI
2099 wxDocMDIChildFrame*
2100 #else
2101 wxDocChildFrame*
2102 #endif
2103 PhantomFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2104 {
2105 #if CTSIM_MDI
2106   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2107 #else
2108   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2109 #endif
2110   theApp->setIconForFrame (subframe);
2111   
2112   m_pFileMenu = new wxMenu;
2113   
2114   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2115   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2116   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2117   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2118   m_pFileMenu->Append(wxID_CLOSE, "&Close");
2119   
2120   m_pFileMenu->AppendSeparator();
2121   m_pFileMenu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
2122   
2123   m_pFileMenu->AppendSeparator();
2124   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2125   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2126   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2127   m_pFileMenu->AppendSeparator();
2128   m_pFileMenu->Append(MAINMENU_IMPORT, "&Import...\tCtrl-M");
2129 #ifdef CTSIM_MDI
2130   m_pFileMenu->AppendSeparator();
2131   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2132   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2133 #endif
2134   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2135   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2136   
2137   wxMenu *process_menu = new wxMenu;
2138   process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...\tCtrl-R");
2139   process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...\tCtrl-J");
2140   
2141   wxMenu *help_menu = new wxMenu;
2142   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2143   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
2144   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
2145   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2146   
2147   wxMenuBar *menu_bar = new wxMenuBar;
2148   
2149   menu_bar->Append(m_pFileMenu, "&File");
2150   menu_bar->Append(process_menu, "&Process");
2151   menu_bar->Append(help_menu, "&Help");
2152   
2153   subframe->SetMenuBar(menu_bar);
2154   subframe->Centre(wxBOTH);
2155   
2156   wxAcceleratorEntry accelEntries[3];
2157   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('J'), PHMMENU_PROCESS_PROJECTIONS);
2158   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('R'), PHMMENU_PROCESS_RASTERIZE);
2159   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('I'), PHMMENU_FILE_PROPERTIES);
2160   wxAcceleratorTable accelTable (3, accelEntries);
2161   subframe->SetAcceleratorTable (accelTable);
2162   
2163   return subframe;
2164 }
2165
2166
2167 bool 
2168 PhantomFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2169 {
2170   m_pFrame = CreateChildFrame(doc, this);
2171   SetFrame(m_pFrame);
2172   m_pCanvas = CreateCanvas (m_pFrame);
2173   m_pFrame->SetClientSize (m_pCanvas->GetBestSize());
2174   m_pCanvas->SetClientSize (m_pCanvas->GetBestSize());
2175   
2176   m_pFrame->SetTitle ("PhantomFileView");
2177   
2178 #ifdef __X__
2179   int x, y;  // X requires a forced resize
2180   m_pFrame->GetSize(&x, &y);
2181   m_pFrame->SetSize(-1, -1, x, y);
2182 #endif
2183   
2184   m_pFrame->Show(true);
2185   Activate(true);
2186   
2187   return true;
2188 }
2189
2190 void 
2191 PhantomFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2192 {
2193   if (m_pCanvas)
2194     m_pCanvas->Refresh();
2195 }
2196
2197 bool 
2198 PhantomFileView::OnClose (bool deleteWindow)
2199 {
2200   //GetDocumentManager()->ActivateView (this, false, true);
2201   if (! GetDocument() || ! GetDocument()->Close())
2202     return false;
2203   
2204   Activate(false);
2205   if (m_pCanvas) {
2206     m_pCanvas->setView(NULL);
2207     m_pCanvas = NULL;
2208   }
2209   wxString s(wxTheApp->GetAppName());
2210   if (m_pFrame)
2211     m_pFrame->SetTitle(s);
2212   
2213   SetFrame(NULL);
2214   
2215   if (deleteWindow) {
2216     delete m_pFrame;
2217     m_pFrame = NULL;
2218     if (GetDocument() && GetDocument()->getBadFileOpen())
2219       ::wxYield();  // wxWindows bug workaround
2220   }
2221   
2222   return true;
2223 }
2224
2225 void
2226 PhantomFileView::OnDraw (wxDC* dc)
2227 {
2228   int xsize, ysize;
2229   m_pCanvas->GetClientSize (&xsize, &ysize);
2230   SGPDriver driver (dc, xsize, ysize);
2231   SGP sgp (driver);
2232   const Phantom& rPhantom = GetDocument()->getPhantom();
2233   sgp.setColor (C_RED);
2234   rPhantom.show (sgp);
2235 }
2236
2237 // ProjectionCanvas
2238
2239 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2240 : wxScrolledWindow(frame, -1, pos, size, style)
2241 {
2242   m_pView = v;
2243 }
2244
2245 ProjectionFileCanvas::~ProjectionFileCanvas ()
2246 {
2247   m_pView = NULL;
2248 }
2249
2250 void 
2251 ProjectionFileCanvas::OnDraw(wxDC& dc)
2252 {
2253   if (m_pView)
2254     m_pView->OnDraw(& dc);
2255 }
2256
2257 wxSize
2258 ProjectionFileCanvas::GetBestSize () const
2259 {
2260   wxSize best (0, 0);
2261   if (m_pView) {
2262     Projections& rProj = m_pView->GetDocument()->getProjections();
2263     best.Set (rProj.nDet(), rProj.nView());
2264   }
2265   
2266   return best;
2267 }
2268
2269
2270 // ProjectionFileView
2271
2272 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
2273
2274 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
2275 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
2276 EVT_MENU(PJMENU_RECONSTRUCT_FBP, ProjectionFileView::OnReconstructFBP)
2277 EVT_MENU(PJMENU_RECONSTRUCT_FOURIER, ProjectionFileView::OnReconstructFourier)
2278 EVT_MENU(PJMENU_CONVERT_POLAR, ProjectionFileView::OnConvertPolar)
2279 EVT_MENU(PJMENU_CONVERT_FFT_POLAR, ProjectionFileView::OnConvertFFTPolar)
2280 EVT_MENU(PJMENU_PLOT_TTHETA_SAMPLING, ProjectionFileView::OnPlotTThetaSampling)
2281 END_EVENT_TABLE()
2282
2283 ProjectionFileView::ProjectionFileView() 
2284 : wxView(), m_pFrame(0), m_pCanvas(0), m_pFileMenu(0)
2285 {
2286 #ifdef DEBUG
2287   m_iDefaultNX = 115;
2288   m_iDefaultNY = 115;
2289 #else
2290   m_iDefaultNX = 256;
2291   m_iDefaultNY = 256;
2292 #endif
2293   
2294   m_iDefaultFilter = SignalFilter::FILTER_ABS_BANDLIMIT;
2295   m_dDefaultFilterParam = 1.;
2296 #if HAVE_FFTW
2297   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_RFFTW;
2298   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_INVERSE_FOURIER;
2299 #else
2300   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_CONVOLUTION;
2301   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
2302 #endif
2303   m_iDefaultZeropad = 1;
2304   m_iDefaultBackprojector = Backprojector::BPROJ_IDIFF;
2305   m_iDefaultInterpolation = Backprojector::INTERP_LINEAR;
2306   m_iDefaultInterpParam = 1;
2307   m_iDefaultTrace = Trace::TRACE_NONE;
2308   
2309   m_iDefaultPolarNX = 256;
2310   m_iDefaultPolarNY = 256;
2311   m_iDefaultPolarInterpolation = Projections::POLAR_INTERP_BILINEAR;
2312   m_iDefaultPolarZeropad = 1;
2313 }
2314
2315 ProjectionFileView::~ProjectionFileView()
2316 {
2317   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
2318   GetDocumentManager()->ActivateView(this, FALSE, TRUE);;
2319 }
2320
2321 void
2322 ProjectionFileView::OnProperties (wxCommandEvent& event)
2323 {
2324   const Projections& rProj = GetDocument()->getProjections();
2325   std::ostringstream os;
2326   rProj.printScanInfo(os);
2327   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
2328   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Projection File Properties", wxOK | wxICON_INFORMATION);
2329   dialogMsg.ShowModal();
2330 }
2331
2332
2333 void
2334 ProjectionFileView::OnConvertPolar (wxCommandEvent& event)
2335 {
2336   Projections& rProj = GetDocument()->getProjections();
2337   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2338     m_iDefaultPolarInterpolation, -1);
2339   if (dialogPolar.ShowModal() == wxID_OK) {
2340     wxString strInterpolation (dialogPolar.getInterpolationName());
2341     m_iDefaultPolarNX = dialogPolar.getXSize();
2342     m_iDefaultPolarNY = dialogPolar.getYSize();
2343     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2344     ImageFile* pIF = new ImageFile (m_iDefaultPolarNX, m_iDefaultPolarNY);
2345     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2346     
2347   if (! rProj.convertPolar (*pIF, m_iDefaultPolarInterpolation)) {
2348       delete pIF;
2349       *theApp->getLog() << "Error converting to Polar\n";
2350       return;
2351     }
2352
2353
2354     pPolarDoc = theApp->newImageDoc ();
2355     if (! pPolarDoc) {
2356       sys_error (ERR_SEVERE, "Unable to create image file");
2357       return;
2358     }
2359     pPolarDoc->setImageFile (pIF);
2360     pIF->labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2361     std::ostringstream os;
2362     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to polar image: xSize=" 
2363       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2364       << strInterpolation.c_str();
2365     *theApp->getLog() << os.str().c_str() << "\n";
2366     pIF->labelAdd (os.str().c_str());
2367     if (theApp->getAskDeleteNewDocs())
2368       pPolarDoc->Modify (true);
2369     pPolarDoc->UpdateAllViews ();
2370     pPolarDoc->getView()->OnUpdate (this, NULL);
2371     pPolarDoc->getView()->getFrame()->Show(true);
2372   }
2373 }
2374
2375 void
2376 ProjectionFileView::OnConvertFFTPolar (wxCommandEvent& event)
2377 {
2378   Projections& rProj = GetDocument()->getProjections();
2379   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert to FFT Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2380     m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2381   if (dialogPolar.ShowModal() == wxID_OK) {
2382     wxString strInterpolation (dialogPolar.getInterpolationName());
2383     m_iDefaultPolarNX = dialogPolar.getXSize();
2384     m_iDefaultPolarNY = dialogPolar.getYSize();
2385     m_iDefaultPolarZeropad = dialogPolar.getZeropad();
2386     ImageFile* pIF = new ImageFile (m_iDefaultPolarNX, m_iDefaultPolarNY);
2387     
2388     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2389     if (! rProj.convertFFTPolar (*pIF, m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad)) {
2390       delete pIF;
2391       *theApp->getLog() << "Error converting to polar\n";
2392       return;
2393     }
2394     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2395     if (! pPolarDoc) {
2396       sys_error (ERR_SEVERE, "Unable to create image file");
2397       return;
2398     }
2399     pPolarDoc->setImageFile (pIF);
2400     pIF->labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2401     std::ostringstream os;
2402     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to FFT polar image: xSize=" 
2403       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2404       << strInterpolation.c_str() << ", zeropad=" << m_iDefaultPolarZeropad;
2405     *theApp->getLog() << os.str().c_str() << "\n";
2406     pIF->labelAdd (os.str().c_str());
2407     if (theApp->getAskDeleteNewDocs())
2408       pPolarDoc->Modify (true);
2409     pPolarDoc->UpdateAllViews ();
2410     pPolarDoc->getView()->OnUpdate (this, NULL);
2411     pPolarDoc->getView()->getFrame()->Show(true);
2412   }
2413 }
2414
2415 void
2416 ProjectionFileView::OnPlotTThetaSampling (wxCommandEvent& event)
2417 {
2418   Projections& rProj = GetDocument()->getProjections();
2419     ParallelRaysums parallel (&rProj);
2420     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
2421     PlotFile& rPlot = pPlotDoc->getPlotFile();
2422     ParallelRaysums::CoordinateContainer& coordContainer = parallel.getCoordinates();
2423     double* pdT = new double [parallel.getNumCoordinates()];
2424     double* pdTheta = new double [parallel.getNumCoordinates()];
2425
2426     for (int i = 0; i < parallel.getNumCoordinates(); i++) {
2427       pdT[i] = coordContainer[i]->m_dT;
2428       pdTheta[i] = coordContainer[i]->m_dTheta;
2429     }
2430     rPlot.setCurveSize (2, parallel.getNumCoordinates(), true);
2431     rPlot.addEzsetCommand ("title T-Theta Sampling");
2432     rPlot.addEzsetCommand ("xlabel T");
2433     rPlot.addEzsetCommand ("ylabel Theta");
2434     rPlot.addEzsetCommand ("curve 1");
2435     if (rProj.nDet() < 50 && rProj.nView() < 50)
2436       rPlot.addEzsetCommand ("symbol 1"); // x symbol
2437     else
2438       rPlot.addEzsetCommand ("symbol 6"); // point symbol
2439     rPlot.addEzsetCommand ("noline");
2440     rPlot.addColumn (0, pdT);
2441     rPlot.addColumn (1, pdTheta);
2442     delete pdT;
2443     delete pdTheta;
2444     if (theApp->getAskDeleteNewDocs())
2445       pPlotDoc->Modify (true);
2446     pPlotDoc->UpdateAllViews ();
2447     pPlotDoc->getView()->OnUpdate (this, NULL);
2448     pPlotDoc->getView()->getFrame()->Show(true);
2449     return;
2450 }
2451
2452 void
2453 ProjectionFileView::OnReconstructFourier (wxCommandEvent& event)
2454 {
2455   wxMessageBox ("Fourier Reconstruction is not yet supported", "Unimplemented function");
2456 }
2457
2458 void
2459 ProjectionFileView::OnReconstructFBP (wxCommandEvent& event)
2460 {
2461   DialogGetReconstructionParameters dialogReconstruction (getFrameForChild(), m_iDefaultNX, m_iDefaultNY, 
2462     m_iDefaultFilter, m_dDefaultFilterParam, m_iDefaultFilterMethod, m_iDefaultFilterGeneration, 
2463     m_iDefaultZeropad, m_iDefaultInterpolation, m_iDefaultInterpParam, m_iDefaultBackprojector, 
2464     m_iDefaultTrace);
2465   
2466   int retVal = dialogReconstruction.ShowModal();
2467   if (retVal != wxID_OK)
2468     return;
2469   
2470   m_iDefaultNX = dialogReconstruction.getXSize();
2471   m_iDefaultNY = dialogReconstruction.getYSize();
2472   wxString optFilterName = dialogReconstruction.getFilterName();
2473   m_iDefaultFilter = SignalFilter::convertFilterNameToID (optFilterName.c_str());
2474   m_dDefaultFilterParam = dialogReconstruction.getFilterParam();
2475   wxString optFilterMethodName = dialogReconstruction.getFilterMethodName();
2476   m_iDefaultFilterMethod = ProcessSignal::convertFilterMethodNameToID(optFilterMethodName.c_str());
2477   m_iDefaultZeropad = dialogReconstruction.getZeropad();
2478   wxString optFilterGenerationName = dialogReconstruction.getFilterGenerationName();
2479   m_iDefaultFilterGeneration = ProcessSignal::convertFilterGenerationNameToID (optFilterGenerationName.c_str());
2480   wxString optInterpName = dialogReconstruction.getInterpName();
2481   m_iDefaultInterpolation = Backprojector::convertInterpNameToID (optInterpName.c_str());
2482   m_iDefaultInterpParam = dialogReconstruction.getInterpParam();
2483   wxString optBackprojectName = dialogReconstruction.getBackprojectName();
2484   m_iDefaultBackprojector = Backprojector::convertBackprojectNameToID (optBackprojectName.c_str());
2485   m_iDefaultTrace = dialogReconstruction.getTrace();
2486   
2487   if (m_iDefaultNX <= 0 && m_iDefaultNY <= 0) 
2488     return;
2489   
2490   const Projections& rProj = GetDocument()->getProjections();
2491   std::ostringstream os;
2492   os << "Reconstruct " << rProj.getFilename() << ": xSize=" << m_iDefaultNX << ", ySize=" << m_iDefaultNY << ", Filter=" << optFilterName.c_str() << ", FilterParam=" << m_dDefaultFilterParam << ", FilterMethod=" << optFilterMethodName.c_str() << ", FilterGeneration=" << optFilterGenerationName.c_str() << ", Zeropad=" << m_iDefaultZeropad << ", Interpolation=" << optInterpName.c_str() << ", InterpolationParam=" << m_iDefaultInterpParam << ", Backprojection=" << optBackprojectName.c_str();
2493   
2494   Timer timerRecon;
2495   ImageFile* pImageFile = NULL;
2496   if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
2497     pImageFile = new ImageFile (m_iDefaultNX, m_iDefaultNY);
2498     Reconstructor* pReconstructor = new Reconstructor (rProj, *pImageFile, optFilterName.c_str(), 
2499       m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), 
2500       optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), m_iDefaultTrace);
2501     
2502     ReconstructDialog* pDlgReconstruct = new ReconstructDialog (*pReconstructor, rProj, *pImageFile, m_iDefaultTrace, getFrameForChild());
2503     for (int iView = 0; iView < rProj.nView(); iView++) {
2504       ::wxYield();
2505       if (pDlgReconstruct->isCancelled() || ! pDlgReconstruct->reconstructView (iView, true)) {
2506         delete pDlgReconstruct;
2507         delete pReconstructor;
2508         return;
2509       }
2510       ::wxYield();
2511       ::wxYield();
2512       while (pDlgReconstruct->isPaused()) {
2513         ::wxYield();
2514         ::wxUsleep(50);
2515       }
2516     }
2517     pReconstructor->postProcessing();
2518     delete pDlgReconstruct;
2519     delete pReconstructor;
2520   } else {
2521 #if HAVE_WXTHREADS
2522     if (theApp->getUseBackgroundTasks()) {
2523       ReconstructorSupervisorThread* pReconstructor = new ReconstructorSupervisorThread (this, 
2524         m_iDefaultNX, m_iDefaultNY, optFilterName.c_str(), 
2525         m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), 
2526         optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), os.str().c_str());
2527       if (pReconstructor->Create() != wxTHREAD_NO_ERROR) {
2528         sys_error (ERR_SEVERE, "Error creating reconstructor thread");
2529         delete pReconstructor;
2530         return;
2531       }
2532       pReconstructor->SetPriority (60);
2533       pReconstructor->Run();
2534       return;
2535     } else 
2536 #endif
2537         {
2538       pImageFile = new ImageFile (m_iDefaultNX, m_iDefaultNY);
2539       Reconstructor* pReconstructor = new Reconstructor (rProj, *pImageFile, optFilterName.c_str(), 
2540         m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), 
2541         optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), m_iDefaultTrace);
2542       
2543       wxProgressDialog dlgProgress (wxString("Reconstruction"), wxString("Reconstruction Progress"), rProj.nView() + 1, getFrameForChild(), wxPD_CAN_ABORT );
2544       for (int iView = 0; iView < rProj.nView(); iView++) {
2545         pReconstructor->reconstructView (iView, 1);
2546         if (! dlgProgress.Update (iView + 1)) {
2547           delete pReconstructor;
2548           return; // don't make new window, thread will do this
2549         }
2550       }
2551       pReconstructor->postProcessing();
2552       delete pReconstructor;
2553     }
2554   }
2555   ImageFileDocument* pReconDoc = theApp->newImageDoc();
2556   if (! pReconDoc) {
2557     sys_error (ERR_SEVERE, "Unable to create image file");
2558     return;
2559   }
2560   pReconDoc->setImageFile (pImageFile);
2561   if (theApp->getAskDeleteNewDocs())
2562     pReconDoc->Modify (true);
2563   pReconDoc->UpdateAllViews (this);
2564   if (ImageFileView* rasterView = pReconDoc->getView()) {
2565     rasterView->OnUpdate (rasterView, NULL);
2566     rasterView->getFrame()->SetFocus();
2567     rasterView->getFrame()->Show(true);
2568   }
2569   *theApp->getLog() << os.str().c_str() << "\n";
2570   pImageFile->labelAdd (rProj.getLabel());
2571   pImageFile->labelAdd (os.str().c_str(), timerRecon.timerEnd());    
2572 }
2573
2574
2575 ProjectionFileCanvas* 
2576 ProjectionFileView::CreateCanvas (wxFrame *parent)
2577 {
2578   ProjectionFileCanvas* pCanvas;
2579   int width, height;
2580   parent->GetClientSize(&width, &height);
2581   
2582   pCanvas = new ProjectionFileCanvas (this, parent, wxPoint(0, 0), wxSize(width, height), 0);
2583   
2584   pCanvas->SetScrollbars(20, 20, 50, 50);
2585   pCanvas->SetBackgroundColour(*wxWHITE);
2586   pCanvas->Clear();
2587   
2588   return pCanvas;
2589 }
2590
2591 #if CTSIM_MDI
2592 wxDocMDIChildFrame*
2593 #else
2594 wxDocChildFrame*
2595 #endif
2596 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2597 {
2598 #ifdef CTSIM_MDI
2599   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2600 #else
2601   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2602 #endif
2603   theApp->setIconForFrame (subframe);
2604   
2605   m_pFileMenu = new wxMenu;
2606   
2607   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2608   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2609   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2610   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2611   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2612   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2613   
2614   m_pFileMenu->AppendSeparator();
2615   m_pFileMenu->Append(PJMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
2616   
2617   m_pFileMenu->AppendSeparator();
2618   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2619   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2620   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2621   m_pFileMenu->AppendSeparator();
2622   m_pFileMenu->Append(MAINMENU_IMPORT, "&Import...\tCtrl-M");
2623 #ifdef CTSIM_MDI
2624   m_pFileMenu->AppendSeparator();
2625   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2626   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2627 #endif
2628   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2629   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2630   
2631   wxMenu *convert_menu = new wxMenu;
2632   convert_menu->Append (PJMENU_CONVERT_POLAR, "&Polar Image...\tCtrl-L");
2633   convert_menu->Append (PJMENU_CONVERT_FFT_POLAR, "&FFT->Polar Image...\tCtrl-M");
2634
2635   wxMenu* analyze_menu = new wxMenu;
2636   analyze_menu->Append (PJMENU_PLOT_TTHETA_SAMPLING, "&Plot T-Theta Sampling\tCtrl-T");
2637
2638   wxMenu *reconstruct_menu = new wxMenu;
2639   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FBP, "&Filtered Backprojection...\tCtrl-R", "Reconstruct image using filtered backprojection");
2640   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FOURIER, "&Fourier...\tCtrl-E", "Reconstruct image using inverse Fourier");
2641   reconstruct_menu->Enable (PJMENU_RECONSTRUCT_FOURIER, false);
2642   
2643   wxMenu *help_menu = new wxMenu;
2644   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2645   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
2646   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
2647   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2648   
2649   wxMenuBar *menu_bar = new wxMenuBar;
2650   
2651   menu_bar->Append (m_pFileMenu, "&File");
2652   menu_bar->Append (convert_menu, "&Convert");
2653   menu_bar->Append (analyze_menu, "&Analyze");
2654   menu_bar->Append (reconstruct_menu, "&Reconstruct");
2655   menu_bar->Append (help_menu, "&Help");
2656   
2657   subframe->SetMenuBar(menu_bar);  
2658   subframe->Centre(wxBOTH);
2659   
2660   wxAcceleratorEntry accelEntries[6];
2661   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('L'), PJMENU_CONVERT_POLAR);
2662   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('M'), PJMENU_CONVERT_FFT_POLAR);
2663   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('R'), PJMENU_RECONSTRUCT_FBP);
2664   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('E'), PJMENU_RECONSTRUCT_FOURIER);
2665   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('I'), PJMENU_FILE_PROPERTIES);
2666   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('T'), PJMENU_PLOT_TTHETA_SAMPLING);
2667   wxAcceleratorTable accelTable (6, accelEntries);
2668   subframe->SetAcceleratorTable (accelTable);
2669   
2670   return subframe;
2671 }
2672
2673
2674 bool 
2675 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2676 {
2677   m_pFrame = CreateChildFrame(doc, this);
2678   SetFrame(m_pFrame);
2679   
2680   int width, height;
2681   m_pFrame->GetClientSize (&width, &height);
2682   m_pFrame->SetTitle ("ProjectionFileView");
2683   m_pCanvas = CreateCanvas (m_pFrame);
2684   
2685 #ifdef __X__
2686   int x, y;  // X requires a forced resize
2687   m_pFrame->GetSize(&x, &y);
2688   m_pFrame->SetSize(-1, -1, x, y);
2689 #endif
2690   
2691   m_pFrame->Show(true);
2692   Activate(true);
2693   
2694   return true;
2695 }
2696
2697 void 
2698 ProjectionFileView::OnDraw (wxDC* dc)
2699 {
2700   wxSize clientSize = m_pFrame->GetClientSize();
2701   wxSize bestSize = m_pCanvas->GetBestSize();
2702   
2703   if (clientSize.x > bestSize.x || clientSize.y > bestSize.y)
2704     m_pFrame->SetClientSize (bestSize);
2705   
2706   if (m_bitmap.Ok())
2707     dc->DrawBitmap (m_bitmap, 0, 0, false);
2708 }
2709
2710
2711 void 
2712 ProjectionFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2713 {
2714   const Projections& rProj = GetDocument()->getProjections();
2715   const int nDet = rProj.nDet();
2716   const int nView = rProj.nView();
2717   if (nDet != 0 && nView != 0) {
2718     const DetectorArray& detarray = rProj.getDetectorArray(0);
2719     const DetectorValue* detval = detarray.detValues();
2720     double min = detval[0];
2721     double max = detval[0];
2722     for (int iy = 0; iy < nView; iy++) {
2723       const DetectorArray& detarray = rProj.getDetectorArray(iy);
2724       const DetectorValue* detval = detarray.detValues();
2725       for (int ix = 0; ix < nDet; ix++) {
2726         if (min > detval[ix])
2727           min = detval[ix];
2728         else if (max < detval[ix])
2729           max = detval[ix];
2730       }
2731     }
2732     
2733     unsigned char* imageData = new unsigned char [nDet * nView * 3];
2734     if (! imageData) {
2735       sys_error (ERR_SEVERE, "Unable to allocate memory for image display");
2736       return;
2737     }
2738     double scale = (max - min) / 255;
2739     for (int iy2 = 0; iy2 < nView; iy2++) {
2740       const DetectorArray& detarray = rProj.getDetectorArray (iy2);
2741       const DetectorValue* detval = detarray.detValues();
2742       for (int ix = 0; ix < nDet; ix++) {
2743         int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
2744         intensity = clamp(intensity, 0, 255);
2745         int baseAddr = (iy2 * nDet + ix) * 3;
2746         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
2747       }
2748     }
2749     wxImage image (nDet, nView, imageData, true);
2750     m_bitmap = image.ConvertToBitmap();
2751     delete imageData;
2752     int xSize = nDet;
2753     int ySize = nView;
2754     xSize = clamp (xSize, 0, 800);
2755     ySize = clamp (ySize, 0, 800);
2756     m_pFrame->SetClientSize (xSize, ySize);
2757     m_pCanvas->SetScrollbars (20, 20, nDet/20, nView/20);
2758   }
2759   
2760   if (m_pCanvas)
2761     m_pCanvas->Refresh();
2762 }
2763
2764 bool 
2765 ProjectionFileView::OnClose (bool deleteWindow)
2766 {
2767   //GetDocumentManager()->ActivateView (this, false, true);
2768   if (! GetDocument() || ! GetDocument()->Close())
2769     return false;
2770   
2771   Activate(false);
2772   if (m_pCanvas) {
2773         m_pCanvas->setView(NULL);
2774     m_pCanvas = NULL;
2775   }
2776   wxString s(wxTheApp->GetAppName());
2777   if (m_pFrame)
2778     m_pFrame->SetTitle(s);
2779   
2780   SetFrame(NULL);
2781   
2782   if (deleteWindow) {
2783     delete m_pFrame;
2784     m_pFrame = NULL;
2785     if (GetDocument() && GetDocument()->getBadFileOpen())
2786       ::wxYield();  // wxWindows bug workaround
2787   }
2788   
2789   return true;
2790 }
2791
2792
2793
2794 // PlotFileCanvas
2795 PlotFileCanvas::PlotFileCanvas (PlotFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2796 : wxScrolledWindow(frame, -1, pos, size, style)
2797 {
2798   m_pView = v;
2799 }
2800
2801 PlotFileCanvas::~PlotFileCanvas ()
2802 {
2803   m_pView = NULL;
2804 }
2805
2806 void 
2807 PlotFileCanvas::OnDraw(wxDC& dc)
2808 {
2809   if (m_pView)
2810     m_pView->OnDraw(& dc);
2811 }
2812
2813
2814 // PlotFileView
2815
2816 IMPLEMENT_DYNAMIC_CLASS(PlotFileView, wxView)
2817
2818 BEGIN_EVENT_TABLE(PlotFileView, wxView)
2819 EVT_MENU(PLOTMENU_FILE_PROPERTIES, PlotFileView::OnProperties)
2820 EVT_MENU(PLOTMENU_VIEW_SCALE_MINMAX, PlotFileView::OnScaleMinMax)
2821 EVT_MENU(PLOTMENU_VIEW_SCALE_AUTO, PlotFileView::OnScaleAuto)
2822 EVT_MENU(PLOTMENU_VIEW_SCALE_FULL, PlotFileView::OnScaleFull)
2823 END_EVENT_TABLE()
2824
2825 PlotFileView::PlotFileView() 
2826 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pEZPlot(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
2827 {
2828 }
2829
2830 PlotFileView::~PlotFileView()
2831 {
2832   if (m_pEZPlot)
2833     delete m_pEZPlot;
2834   
2835   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);  
2836 }
2837
2838 void
2839 PlotFileView::OnProperties (wxCommandEvent& event)
2840 {
2841   const PlotFile& rPlot = GetDocument()->getPlotFile();
2842   std::ostringstream os;
2843   os << "Columns: " << rPlot.getNumColumns() << ", Records: " << rPlot.getNumRecords() << "\n";
2844   rPlot.printHeadersBrief (os);
2845   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<<\n";
2846   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Plot File Properties", wxOK | wxICON_INFORMATION);
2847   dialogMsg.ShowModal();
2848 }
2849
2850
2851 void 
2852 PlotFileView::OnScaleAuto (wxCommandEvent& event)
2853 {
2854   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2855   double min, max, mean, mode, median, stddev;
2856   rPlotFile.statistics (1, min, max, mean, mode, median, stddev);
2857   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
2858   int iRetVal = dialogAutoScale.ShowModal();
2859   if (iRetVal == wxID_OK) {
2860     m_bMinSpecified = true;
2861     m_bMaxSpecified = true;
2862     double dMin, dMax;
2863     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
2864       m_dMinPixel = dMin;
2865       m_dMaxPixel = dMax;
2866       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
2867       OnUpdate (this, NULL);
2868     }
2869   }
2870 }
2871
2872 void 
2873 PlotFileView::OnScaleMinMax (wxCommandEvent& event)
2874 {
2875   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2876   double min;
2877   double max;
2878   
2879   if (! m_bMinSpecified || ! m_bMaxSpecified) {
2880     if (! rPlotFile.getMinMax (1, min, max)) {
2881       *theApp->getLog() << "Error: unable to find Min/Max\n";
2882       return;
2883     }
2884   }
2885   
2886   if (m_bMinSpecified)
2887     min = m_dMinPixel;
2888   if (m_bMaxSpecified)
2889     max = m_dMaxPixel;
2890   
2891   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Y-axis Minimum & Maximum", min, max);
2892   int retVal = dialogMinMax.ShowModal();
2893   if (retVal == wxID_OK) {
2894     m_bMinSpecified = true;
2895     m_bMaxSpecified = true;
2896     m_dMinPixel = dialogMinMax.getMinimum();
2897     m_dMaxPixel = dialogMinMax.getMaximum();
2898     OnUpdate (this, NULL);
2899   }
2900 }
2901
2902 void 
2903 PlotFileView::OnScaleFull (wxCommandEvent& event)
2904 {
2905   if (m_bMinSpecified || m_bMaxSpecified) {
2906     m_bMinSpecified = false;
2907     m_bMaxSpecified = false;
2908     OnUpdate (this, NULL);
2909   }
2910 }
2911
2912
2913 PlotFileCanvas* 
2914 PlotFileView::CreateCanvas (wxFrame* parent)
2915 {
2916   PlotFileCanvas* pCanvas;
2917   int width, height;
2918   parent->GetClientSize(&width, &height);
2919   
2920   pCanvas = new PlotFileCanvas (this, parent, wxPoint(0, 0), wxSize(width, height), 0);
2921   
2922   pCanvas->SetBackgroundColour(*wxWHITE);
2923   pCanvas->Clear();
2924   
2925   return pCanvas;
2926 }
2927
2928 #if CTSIM_MDI
2929 wxDocMDIChildFrame*
2930 #else
2931 wxDocChildFrame*
2932 #endif
2933 PlotFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2934 {
2935 #ifdef CTSIM_MDI
2936   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2937 #else
2938   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2939 #endif
2940   theApp->setIconForFrame (subframe);
2941   
2942   m_pFileMenu = new wxMenu;
2943   
2944   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2945   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2946   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2947   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2948   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2949   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2950   
2951   m_pFileMenu->AppendSeparator();
2952   m_pFileMenu->Append(PLOTMENU_FILE_PROPERTIES, "P&roperties\tCtrl-I");
2953   
2954   m_pFileMenu->AppendSeparator();
2955   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2956   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2957   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2958   m_pFileMenu->AppendSeparator();
2959   m_pFileMenu->Append(MAINMENU_IMPORT, "&Import...\tCtrl-M");
2960 #ifdef CTSIM_MDI
2961   m_pFileMenu->AppendSeparator();
2962   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
2963   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2964 #endif
2965   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2966   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
2967   
2968   wxMenu *view_menu = new wxMenu;
2969   view_menu->Append(PLOTMENU_VIEW_SCALE_MINMAX, "Display Scale &Set...\tCtrl-E");
2970   view_menu->Append(PLOTMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
2971   view_menu->Append(PLOTMENU_VIEW_SCALE_FULL, "Display &Full Scale\tCtrl-U");
2972   
2973   wxMenu *help_menu = new wxMenu;
2974   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2975   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
2976   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
2977   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2978   
2979   wxMenuBar *menu_bar = new wxMenuBar;
2980   
2981   menu_bar->Append(m_pFileMenu, "&File");
2982   menu_bar->Append(view_menu, "&View");
2983   menu_bar->Append(help_menu, "&Help");
2984   
2985   subframe->SetMenuBar(menu_bar);
2986   subframe->Centre(wxBOTH);
2987   
2988   wxAcceleratorEntry accelEntries[4];
2989   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('E'), PLOTMENU_VIEW_SCALE_MINMAX);
2990   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('A'), PLOTMENU_VIEW_SCALE_AUTO);
2991   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('U'), PLOTMENU_VIEW_SCALE_FULL);
2992   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('I'), PLOTMENU_FILE_PROPERTIES);
2993   wxAcceleratorTable accelTable (4, accelEntries);
2994   subframe->SetAcceleratorTable (accelTable);
2995   
2996   return subframe;
2997 }
2998
2999
3000 bool 
3001 PlotFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
3002 {
3003   m_pFrame = CreateChildFrame(doc, this);
3004   SetFrame(m_pFrame);
3005   
3006   m_bMinSpecified = false;
3007   m_bMaxSpecified = false;
3008   m_dAutoScaleFactor = 1.;
3009   
3010   int width, height;
3011   m_pFrame->GetClientSize(&width, &height);
3012   m_pFrame->SetTitle ("Plot File");
3013   m_pCanvas = CreateCanvas (m_pFrame);
3014   
3015 #ifdef __X__
3016   int x, y;  // X requires a forced resize
3017   m_pFrame->GetSize(&x, &y);
3018   m_pFrame->SetSize(-1, -1, x, y);
3019 #endif
3020   
3021   m_pFrame->Show(true);
3022   Activate(true);
3023   
3024   return true;
3025 }
3026
3027 void 
3028 PlotFileView::OnDraw (wxDC* dc)
3029 {
3030   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
3031   const int iNColumns = rPlotFile.getNumColumns();
3032   const int iNRecords = rPlotFile.getNumRecords();
3033   
3034   if (iNColumns > 0 && iNRecords > 0) {
3035     int xsize, ysize;
3036     m_pCanvas->GetClientSize (&xsize, &ysize);
3037     SGPDriver driver (dc, xsize, ysize);
3038     SGP sgp (driver);
3039     if (m_pEZPlot)
3040       m_pEZPlot->plot (&sgp);
3041   }
3042 }
3043
3044
3045 void 
3046 PlotFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
3047 {
3048   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
3049   const int iNColumns = rPlotFile.getNumColumns();
3050   const int iNRecords = rPlotFile.getNumRecords();
3051   const bool bScatterPlot = rPlotFile.getIsScatterPlot();
3052
3053   if (iNColumns > 0 && iNRecords > 0) {
3054     if (m_pEZPlot)
3055       delete m_pEZPlot;
3056     m_pEZPlot = new EZPlot;
3057     
3058     for (unsigned int iEzset = 0; iEzset < rPlotFile.getNumEzsetCommands(); iEzset++)
3059       m_pEZPlot->ezset (rPlotFile.getEzsetCommand (iEzset));
3060     
3061     if (m_bMinSpecified) {
3062       std::ostringstream os;
3063       os << "ymin " << m_dMinPixel;
3064       m_pEZPlot->ezset (os.str());
3065     }
3066     
3067     if (m_bMaxSpecified) {
3068       std::ostringstream os;
3069       os << "ymax " << m_dMaxPixel;
3070       m_pEZPlot->ezset (os.str());
3071     }
3072     
3073     m_pEZPlot->ezset("box");
3074     m_pEZPlot->ezset("grid");
3075   
3076     double* pdX = new double [iNRecords];
3077     double* pdY = new double [iNRecords];
3078     if (! bScatterPlot) {
3079       rPlotFile.getColumn (0, pdX);
3080     
3081       for (int iCol = 1; iCol < iNColumns; iCol++) {
3082         rPlotFile.getColumn (iCol, pdY);
3083         m_pEZPlot->addCurve (pdX, pdY, iNRecords);
3084       }
3085     } else {
3086       rPlotFile.getColumn (0, pdX);
3087       rPlotFile.getColumn (1, pdY);
3088       m_pEZPlot->addCurve (pdX, pdY, iNRecords);
3089     }
3090     delete pdX;
3091     delete pdY;
3092   }
3093   
3094   if (m_pCanvas)
3095     m_pCanvas->Refresh();
3096 }
3097
3098 bool 
3099 PlotFileView::OnClose (bool deleteWindow)
3100 {
3101   //GetDocumentManager()->ActivateView (this, false, true);
3102   if (! GetDocument() || ! GetDocument()->Close())
3103     return false;
3104   
3105   Activate(false);
3106   if (m_pCanvas) {
3107     m_pCanvas->setView (NULL);
3108     m_pCanvas = NULL;
3109   }
3110   wxString s(wxTheApp->GetAppName());
3111   if (m_pFrame)
3112     m_pFrame->SetTitle(s);
3113   
3114   SetFrame(NULL);
3115   if (deleteWindow) {
3116     delete m_pFrame;
3117     m_pFrame = NULL;
3118     if (GetDocument() && GetDocument()->getBadFileOpen())
3119       ::wxYield();  // wxWindows bug workaround
3120   }
3121   
3122   return true;
3123 }
3124
3125
3126 ////////////////////////////////////////////////////////////////
3127
3128
3129 IMPLEMENT_DYNAMIC_CLASS(TextFileView, wxView)
3130
3131 TextFileView::~TextFileView() 
3132 {
3133   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
3134   GetDocumentManager()->ActivateView(this, FALSE, TRUE);;
3135 }
3136
3137 bool TextFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
3138 {
3139   m_pFrame = CreateChildFrame(doc, this);
3140   SetFrame (m_pFrame);
3141   
3142   int width, height;
3143   m_pFrame->GetClientSize(&width, &height);
3144   m_pFrame->SetTitle("TextFile");
3145   m_pCanvas = new TextFileCanvas (this, m_pFrame, wxPoint(0, 0), wxSize(width, height), wxTE_MULTILINE | wxTE_READONLY);
3146   m_pFrame->SetTitle("Log");
3147   
3148 #ifdef __X__
3149   // X seems to require a forced resize
3150   int x, y;
3151   frame->GetSize(&x, &y);
3152   frame->SetSize(-1, -1, x, y);
3153 #endif
3154   
3155   m_pFrame->Show (true);
3156   Activate (true);
3157   
3158   return true;
3159 }
3160
3161 // Handled by wxTextWindow
3162 void TextFileView::OnDraw(wxDC *WXUNUSED(dc) )
3163 {
3164 }
3165
3166 void TextFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
3167 {
3168 }
3169
3170 bool 
3171 TextFileView::OnClose (bool deleteWindow)
3172 {
3173   if (! theApp->getMainFrame()->getShuttingDown())
3174     return false;
3175   
3176   Activate(false);
3177   //GetDocumentManager()->ActivateView (this, false, true);
3178   if (! GetDocument() || ! GetDocument()->Close())
3179     return false;
3180   
3181   SetFrame(NULL);
3182   if (deleteWindow) {
3183     delete m_pFrame;
3184     m_pFrame = NULL;
3185     if (GetDocument() && GetDocument()->getBadFileOpen())
3186       ::wxYield();  // wxWindows bug workaround
3187   }
3188   
3189   return TRUE;
3190 }
3191
3192 #if CTSIM_MDI
3193 wxDocMDIChildFrame*
3194 #else
3195 wxDocChildFrame*
3196 #endif
3197 TextFileView::CreateChildFrame (wxDocument *doc, wxView *view)
3198 {
3199 #if CTSIM_MDI
3200   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(0,0), wxDEFAULT_FRAME_STYLE, "Log");
3201 #else
3202   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(300, 150), wxDEFAULT_FRAME_STYLE, "Log");
3203 #endif
3204   theApp->setIconForFrame (subframe);
3205   
3206   m_pFileMenu = new wxMenu;
3207   
3208   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
3209   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
3210   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
3211   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
3212   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
3213   //  m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
3214   
3215   m_pFileMenu->AppendSeparator();
3216   m_pFileMenu->Append(wxID_PRINT, "&Print...");
3217   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
3218   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
3219   m_pFileMenu->AppendSeparator();
3220   m_pFileMenu->Append(MAINMENU_IMPORT, "&Import...\tCtrl-M");
3221 #ifdef CTSIM_MDI
3222   m_pFileMenu->AppendSeparator();
3223   m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, "Prefere&nces...");
3224   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
3225 #endif
3226   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
3227   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
3228   
3229   wxMenu *help_menu = new wxMenu;
3230   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
3231   help_menu->Append (MAINMENU_HELP_TIPS, "&Tips");
3232   help_menu->Append (IDH_QUICKSTART, "&Quick Start");
3233   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
3234   
3235   wxMenuBar *menu_bar = new wxMenuBar;
3236   
3237   menu_bar->Append(m_pFileMenu, "&File");
3238   menu_bar->Append(help_menu, "&Help");
3239   
3240   subframe->SetMenuBar(menu_bar);
3241   subframe->Centre(wxBOTH);
3242   
3243   return subframe;
3244 }
3245
3246
3247 // Define a constructor for my text subwindow
3248 TextFileCanvas::TextFileCanvas (TextFileView* v, wxFrame* frame, const wxPoint& pos, const wxSize& size, long style)
3249 : wxTextCtrl (frame, -1, "", pos, size, style), m_pView(v)
3250 {
3251 }
3252
3253 TextFileCanvas::~TextFileCanvas ()
3254 {
3255   m_pView = NULL;
3256 }