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