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