r445: 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.77 2001/01/27 02:01:41 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->DeleteAllViews();
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_pCanvas(NULL), m_pFrame(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           ::wxYield();
1813           if (dialogProjections.isCancelled() || ! dialogProjections.projectView (iView)) {
1814             pProjectionDoc->DeleteAllViews();
1815             return;
1816           }
1817           ::wxYield();
1818           ::wxYield();
1819           while (dialogProjections.isPaused()) {
1820             ::wxYield();
1821             ::wxUsleep(50);
1822           }
1823         }
1824       } else {
1825         wxProgressDialog dlgProgress (wxString("Projection"), wxString("Projection Progress"), rProj.nView() + 1, getFrameForChild(), wxPD_CAN_ABORT);
1826         for (int i = 0; i < rProj.nView(); i++) {
1827           theScanner.collectProjections (rProj, rPhantom, i, 1, true, m_iDefaultTrace);
1828           if (! dlgProgress.Update (i+1)) {
1829             pProjectionDoc->DeleteAllViews();
1830             return;
1831           }
1832         }
1833       }
1834       
1835       std::ostringstream os;
1836       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();
1837       rProj.setCalcTime (timer.timerEnd());
1838       rProj.setRemark (os.str());
1839       *theApp->getLog() << os.str().c_str() << "\n";
1840       
1841       m_pFrame->Lower();
1842       ::wxYield();
1843       ProjectionFileView* projView = dynamic_cast<ProjectionFileView*>(pProjectionDoc->GetFirstView());
1844       if (projView) {
1845         projView->getFrame()->SetFocus();
1846         projView->OnUpdate (projView, NULL);
1847       }
1848       if (wxView* pView = pProjectionDoc->GetFirstView()) {
1849         if (wxFrame* pFrame = pView->GetFrame()) {
1850           pFrame->SetFocus();
1851           pFrame->Raise();
1852         }
1853         theApp->getDocManager()->ActivateView (pView, true, false);
1854       }
1855       ::wxYield();
1856       if (theApp->getSetModifyNewDocs())
1857         pProjectionDoc->Modify(true);
1858       pProjectionDoc->getView()->getFrame()->Show(true);
1859       pProjectionDoc->UpdateAllViews(this);
1860     }
1861   }
1862 }
1863
1864
1865 void
1866 PhantomFileView::OnRasterize (wxCommandEvent& event)
1867 {
1868   DialogGetRasterParameters dialogRaster (getFrameForChild(), m_iDefaultRasterNX, m_iDefaultRasterNY, m_iDefaultRasterNSamples);
1869   int retVal = dialogRaster.ShowModal();
1870   if (retVal == wxID_OK) {
1871     m_iDefaultRasterNX = dialogRaster.getXSize();
1872     m_iDefaultRasterNY  = dialogRaster.getYSize();
1873     m_iDefaultRasterNSamples = dialogRaster.getNSamples();
1874     if (m_iDefaultRasterNSamples < 1)
1875       m_iDefaultRasterNSamples = 1;
1876     if (m_iDefaultRasterNX > 0 && m_iDefaultRasterNY > 0) {
1877       const Phantom& rPhantom = GetDocument()->getPhantom();
1878       ImageFileDocument* pRasterDoc = theApp->newImageDoc();
1879       if (! pRasterDoc) {
1880         sys_error (ERR_SEVERE, "Unable to create image file");
1881         return;
1882       }
1883       ImageFile& imageFile = pRasterDoc->getImageFile();
1884       
1885       imageFile.setArraySize (m_iDefaultRasterNX, m_iDefaultRasterNX);
1886       wxProgressDialog dlgProgress (wxString("Rasterize"), wxString("Rasterization Progress"), imageFile.nx() + 1, getFrameForChild(), wxPD_CAN_ABORT);
1887       Timer timer;
1888       for (unsigned int i = 0; i < imageFile.nx(); i++) {
1889         rPhantom.convertToImagefile (imageFile, m_iDefaultRasterNSamples, Trace::TRACE_NONE, i, 1, true);
1890         if (! dlgProgress.Update(i+1)) {
1891           pRasterDoc->DeleteAllViews();
1892           return;
1893         }
1894       }
1895       if (theApp->getSetModifyNewDocs())
1896         pRasterDoc->Modify(true);
1897       pRasterDoc->UpdateAllViews(this);
1898       pRasterDoc->getView()->getFrame()->Show(true);
1899       std::ostringstream os;
1900       os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << m_iDefaultRasterNX << ", YSize=" 
1901         << m_iDefaultRasterNY << ", nSamples=" << m_iDefaultRasterNSamples;
1902       *theApp->getLog() << os.str().c_str() << "\n";
1903       imageFile.labelAdd (os.str().c_str(), timer.timerEnd());
1904       ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pRasterDoc->GetFirstView());
1905       if (rasterView) {
1906         rasterView->getFrame()->SetFocus();
1907         rasterView->OnUpdate (rasterView, NULL);
1908       }
1909       
1910     }
1911   }
1912 }
1913
1914
1915 PhantomCanvas* 
1916 PhantomFileView::CreateCanvas (wxView *view, wxFrame *parent)
1917 {
1918   PhantomCanvas* pCanvas;
1919   int width, height;
1920   parent->GetClientSize(&width, &height);
1921   
1922   pCanvas = new PhantomCanvas (dynamic_cast<PhantomFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
1923   
1924   pCanvas->SetBackgroundColour(*wxWHITE);
1925   pCanvas->Clear();
1926   
1927   return pCanvas;
1928 }
1929
1930 #if CTSIM_MDI
1931 wxDocMDIChildFrame*
1932 #else
1933 wxDocChildFrame*
1934 #endif
1935 PhantomFileView::CreateChildFrame(wxDocument *doc, wxView *view)
1936 {
1937 #if CTSIM_MDI
1938   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
1939 #else
1940   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
1941 #endif
1942   theApp->setIconForFrame (subframe);
1943   
1944   wxMenu *m_pFileMenu = new wxMenu;
1945   
1946   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
1947   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
1948   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
1949   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
1950   m_pFileMenu->Append(wxID_CLOSE, "&Close");
1951   
1952   m_pFileMenu->AppendSeparator();
1953   m_pFileMenu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties");
1954   
1955   m_pFileMenu->AppendSeparator();
1956   m_pFileMenu->Append(wxID_PRINT, "&Print...");
1957   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
1958   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
1959 #ifdef CTSIM_MDI
1960   m_pFileMenu->AppendSeparator();
1961   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
1962 #endif
1963   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
1964   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
1965   
1966   wxMenu *process_menu = new wxMenu;
1967   process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...\tCtrl-R");
1968   process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...\tCtrl-J");
1969   
1970   wxMenu *help_menu = new wxMenu;
1971   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
1972   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
1973   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
1974   
1975   wxMenuBar *menu_bar = new wxMenuBar;
1976   
1977   menu_bar->Append(m_pFileMenu, "&File");
1978   menu_bar->Append(process_menu, "&Process");
1979   menu_bar->Append(help_menu, "&Help");
1980   
1981   subframe->SetMenuBar(menu_bar);
1982   subframe->Centre(wxBOTH);
1983   
1984   wxAcceleratorEntry accelEntries[8];
1985   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
1986   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
1987   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
1988   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
1989   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
1990   accelEntries[5].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
1991   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('J'), PHMMENU_PROCESS_PROJECTIONS);
1992   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('R'), PHMMENU_PROCESS_RASTERIZE);
1993   wxAcceleratorTable accelTable (8, accelEntries);
1994   subframe->SetAcceleratorTable (accelTable);
1995   
1996   return subframe;
1997 }
1998
1999
2000 bool 
2001 PhantomFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2002 {
2003   m_pFrame = CreateChildFrame(doc, this);
2004   SetFrame(m_pFrame);
2005   
2006   int width, height;
2007   m_pFrame->GetClientSize(&width, &height);
2008   m_pFrame->SetTitle("PhantomFileView");
2009   m_pCanvas = CreateCanvas (this, m_pFrame);
2010   
2011 #ifdef __X__
2012   int x, y;  // X requires a forced resize
2013   m_pFrame->GetSize(&x, &y);
2014   m_pFrame->SetSize(-1, -1, x, y);
2015 #endif
2016   
2017   m_pFrame->Show(true);
2018   Activate(true);
2019   
2020   return true;
2021 }
2022
2023 void 
2024 PhantomFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2025 {
2026   if (m_pCanvas)
2027     m_pCanvas->Refresh();
2028 }
2029
2030 bool 
2031 PhantomFileView::OnClose (bool deleteWindow)
2032 {
2033   if (! GetDocument() || ! GetDocument()->Close())
2034     return false;
2035   
2036   if (m_pCanvas) {
2037     m_pCanvas->Clear();
2038     m_pCanvas->setView(NULL);
2039         m_pCanvas->Show(false);
2040     m_pCanvas = NULL;
2041   }
2042   wxString s(wxTheApp->GetAppName());
2043   if (m_pFrame)
2044     m_pFrame->SetTitle(s);
2045   SetFrame(NULL);
2046   
2047   Activate(false);
2048   
2049   if (deleteWindow) {
2050           m_pFrame->Show(false);
2051           m_pFrame->Destroy();
2052 //    delete m_pFrame;
2053     m_pFrame = NULL;
2054   }
2055   
2056   return true;
2057 }
2058
2059 void
2060 PhantomFileView::OnDraw (wxDC* dc)
2061 {
2062   int xsize, ysize;
2063   m_pCanvas->GetClientSize (&xsize, &ysize);
2064   SGPDriver driver (dc, xsize, ysize);
2065   SGP sgp (driver);
2066   const Phantom& rPhantom = GetDocument()->getPhantom();
2067   sgp.setColor (C_RED);
2068   rPhantom.show (sgp);
2069 }
2070
2071 // ProjectionCanvas
2072
2073 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2074 : wxScrolledWindow(frame, -1, pos, size, style)
2075 {
2076   m_pView = v;
2077 }
2078
2079 ProjectionFileCanvas::~ProjectionFileCanvas ()
2080 {
2081         m_pView = NULL;
2082 }
2083
2084 void 
2085 ProjectionFileCanvas::OnDraw(wxDC& dc)
2086 {
2087   if (m_pView)
2088     m_pView->OnDraw(& dc);
2089 }
2090
2091 wxSize
2092 ProjectionFileCanvas::GetBestSize () const
2093 {
2094   wxSize best (0, 0);
2095   if (m_pView) {
2096     Projections& rProj = m_pView->GetDocument()->getProjections();
2097     best.Set (rProj.nDet(), rProj.nView());
2098   }
2099   
2100   return best;
2101 }
2102
2103
2104 // ProjectionFileView
2105
2106 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
2107
2108 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
2109 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
2110 EVT_MENU(PJMENU_RECONSTRUCT_FBP, ProjectionFileView::OnReconstructFBP)
2111 EVT_MENU(PJMENU_CONVERT_POLAR, ProjectionFileView::OnConvertPolar)
2112 EVT_MENU(PJMENU_CONVERT_FFT_POLAR, ProjectionFileView::OnConvertFFTPolar)
2113 END_EVENT_TABLE()
2114
2115 ProjectionFileView::ProjectionFileView() 
2116 : wxView(), m_pCanvas(NULL), m_pFrame(NULL), m_pFileMenu(0)
2117 {
2118   m_iDefaultNX = 256;
2119   m_iDefaultNY = 256;
2120   m_iDefaultFilter = SignalFilter::FILTER_ABS_BANDLIMIT;
2121   m_dDefaultFilterParam = 1.;
2122 #if HAVE_FFTW
2123   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_RFFTW;
2124   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_INVERSE_FOURIER;
2125 #else
2126   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_CONVOLUTION;
2127   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
2128 #endif
2129   m_iDefaultZeropad = 1;
2130   m_iDefaultBackprojector = Backprojector::BPROJ_IDIFF3;
2131   m_iDefaultInterpolation = Backprojector::INTERP_LINEAR;
2132   m_iDefaultInterpParam = 1;
2133   m_iDefaultTrace = Trace::TRACE_NONE;
2134   
2135   m_iDefaultPolarNX = 256;
2136   m_iDefaultPolarNY = 256;
2137   m_iDefaultPolarInterpolation = Projections::POLAR_INTERP_BILINEAR;
2138   m_iDefaultPolarZeropad = 1;
2139 }
2140
2141 ProjectionFileView::~ProjectionFileView()
2142 {
2143         theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);
2144 }
2145
2146 void
2147 ProjectionFileView::OnProperties (wxCommandEvent& event)
2148 {
2149   const Projections& rProj = GetDocument()->getProjections();
2150   std::ostringstream os;
2151   rProj.printScanInfo(os);
2152   *theApp->getLog() << os.str().c_str();
2153   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Projection File Properties", wxOK | wxICON_INFORMATION);
2154   dialogMsg.ShowModal();
2155 }
2156
2157
2158 void
2159 ProjectionFileView::OnConvertPolar (wxCommandEvent& event)
2160 {
2161   Projections& rProj = GetDocument()->getProjections();
2162   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2163     m_iDefaultPolarInterpolation, -1);
2164   if (dialogPolar.ShowModal() == wxID_OK) {
2165     wxString strInterpolation (dialogPolar.getInterpolationName());
2166     m_iDefaultPolarNX = dialogPolar.getXSize();
2167     m_iDefaultPolarNY = dialogPolar.getYSize();
2168     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2169     ImageFile& rIF = pPolarDoc->getImageFile();
2170     if (! pPolarDoc) {
2171       sys_error (ERR_SEVERE, "Unable to create image file");
2172       return;
2173     }
2174     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2175     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2176     rProj.convertPolar (rIF, m_iDefaultPolarInterpolation);
2177     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2178     std::ostringstream os;
2179     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to polar image: xSize=" 
2180       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2181       << strInterpolation.c_str();
2182     *theApp->getLog() << os.str().c_str() << "\n";
2183     rIF.labelAdd (os.str().c_str());
2184     if (theApp->getSetModifyNewDocs())
2185       pPolarDoc->Modify(true);
2186     pPolarDoc->UpdateAllViews();
2187     pPolarDoc->getView()->OnUpdate (this, NULL);
2188     pPolarDoc->getView()->getFrame()->Show(true);
2189   }
2190 }
2191
2192 void
2193 ProjectionFileView::OnConvertFFTPolar (wxCommandEvent& event)
2194 {
2195   Projections& rProj = GetDocument()->getProjections();
2196   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert to FFT Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2197     m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2198   if (dialogPolar.ShowModal() == wxID_OK) {
2199     wxString strInterpolation (dialogPolar.getInterpolationName());
2200     m_iDefaultPolarNX = dialogPolar.getXSize();
2201     m_iDefaultPolarNY = dialogPolar.getYSize();
2202     m_iDefaultPolarZeropad = dialogPolar.getZeropad();
2203     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2204     ImageFile& rIF = pPolarDoc->getImageFile();
2205     if (! pPolarDoc) {
2206       sys_error (ERR_SEVERE, "Unable to create image file");
2207       return;
2208     }
2209     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2210     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2211     rProj.convertFFTPolar (rIF, m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2212     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2213     std::ostringstream os;
2214     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to FFT polar image: xSize=" 
2215       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2216       << strInterpolation.c_str() << ", zeropad=" << m_iDefaultPolarZeropad;
2217     *theApp->getLog() << os.str().c_str() << "\n";
2218     rIF.labelAdd (os.str().c_str());
2219     if (theApp->getSetModifyNewDocs())
2220       pPolarDoc->Modify(true);
2221     pPolarDoc->UpdateAllViews();
2222     pPolarDoc->getView()->OnUpdate (this, NULL);
2223     pPolarDoc->getView()->getFrame()->Show(true);
2224 }
2225 }
2226
2227 void
2228 ProjectionFileView::OnReconstructFourier (wxCommandEvent& event)
2229 {
2230   wxMessageBox ("Fourier Reconstruction is not yet supported", "Unimplemented function");
2231 }
2232
2233 void
2234 ProjectionFileView::OnReconstructFBP (wxCommandEvent& event)
2235 {
2236   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);
2237   
2238   int retVal = dialogReconstruction.ShowModal();
2239   if (retVal == wxID_OK) {
2240     m_iDefaultNX = dialogReconstruction.getXSize();
2241     m_iDefaultNY = dialogReconstruction.getYSize();
2242     wxString optFilterName = dialogReconstruction.getFilterName();
2243     m_iDefaultFilter = SignalFilter::convertFilterNameToID (optFilterName.c_str());
2244     m_dDefaultFilterParam = dialogReconstruction.getFilterParam();
2245     wxString optFilterMethodName = dialogReconstruction.getFilterMethodName();
2246     m_iDefaultFilterMethod = ProcessSignal::convertFilterMethodNameToID(optFilterMethodName.c_str());
2247     m_iDefaultZeropad = dialogReconstruction.getZeropad();
2248     wxString optFilterGenerationName = dialogReconstruction.getFilterGenerationName();
2249     m_iDefaultFilterGeneration = ProcessSignal::convertFilterGenerationNameToID (optFilterGenerationName.c_str());
2250     wxString optInterpName = dialogReconstruction.getInterpName();
2251     m_iDefaultInterpolation = Backprojector::convertInterpNameToID (optInterpName.c_str());
2252     m_iDefaultInterpParam = dialogReconstruction.getInterpParam();
2253     wxString optBackprojectName = dialogReconstruction.getBackprojectName();
2254     m_iDefaultBackprojector = Backprojector::convertBackprojectNameToID (optBackprojectName.c_str());
2255     m_iDefaultTrace = dialogReconstruction.getTrace();
2256     if (m_iDefaultNX > 0 && m_iDefaultNY > 0) {
2257       ImageFileDocument* pReconDoc = theApp->newImageDoc();
2258       if (! pReconDoc) {
2259         sys_error (ERR_SEVERE, "Unable to create image file");
2260         return;
2261       }
2262       ImageFile& imageFile = pReconDoc->getImageFile();
2263       const Projections& rProj = GetDocument()->getProjections();
2264       imageFile.setArraySize (m_iDefaultNX, m_iDefaultNY);
2265       
2266       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);
2267       
2268       Timer timerRecon;
2269       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
2270         ReconstructDialog* pDlgReconstruct = new ReconstructDialog (*pReconstruct, rProj, imageFile, m_iDefaultTrace, getFrameForChild());
2271         for (int iView = 0; iView < rProj.nView(); iView++) {
2272           ::wxYield();
2273           ::wxYield();
2274           if (pDlgReconstruct->isCancelled() || ! pDlgReconstruct->reconstructView (iView)) {
2275             delete pDlgReconstruct;
2276             delete pReconstruct;
2277             pReconDoc->DeleteAllViews();
2278             return;
2279           }
2280           ::wxYield();
2281           ::wxYield();
2282           while (pDlgReconstruct->isPaused()) {
2283             ::wxYield();
2284             ::wxUsleep(50);
2285           }
2286         }
2287         delete pDlgReconstruct;
2288       } else {
2289         wxProgressDialog dlgProgress (wxString("Reconstruction"), wxString("Reconstruction Progress"), rProj.nView() + 1, getFrameForChild(), wxPD_CAN_ABORT);
2290         for (int i = 0; i < rProj.nView(); i++) {
2291           pReconstruct->reconstructView (i, 1);
2292           if (! dlgProgress.Update(i + 1)) {
2293             delete pReconstruct;
2294             pReconDoc->DeleteAllViews();
2295             return;
2296           }
2297         }
2298       }
2299       delete pReconstruct;
2300       if (theApp->getSetModifyNewDocs())
2301         pReconDoc->Modify(true);
2302       pReconDoc->UpdateAllViews(this);
2303       pReconDoc->getView()->getFrame()->Show(true);
2304       ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pReconDoc->GetFirstView());
2305       if (rasterView) {
2306         rasterView->getFrame()->SetFocus();
2307         rasterView->OnUpdate (rasterView, NULL);
2308       }
2309       std::ostringstream os;
2310       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();
2311       *theApp->getLog() << os.str().c_str() << "\n";
2312       imageFile.labelAdd (rProj.getLabel());
2313       imageFile.labelAdd (os.str().c_str(), timerRecon.timerEnd());
2314     }
2315   }
2316 }
2317
2318
2319 ProjectionFileCanvas* 
2320 ProjectionFileView::CreateCanvas (wxView *view, wxFrame *parent)
2321 {
2322   ProjectionFileCanvas* pCanvas;
2323   int width, height;
2324   parent->GetClientSize(&width, &height);
2325   
2326   pCanvas = new ProjectionFileCanvas (dynamic_cast<ProjectionFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
2327   
2328   pCanvas->SetScrollbars(20, 20, 50, 50);
2329   pCanvas->SetBackgroundColour(*wxWHITE);
2330   pCanvas->Clear();
2331   
2332   return pCanvas;
2333 }
2334
2335 #if CTSIM_MDI
2336 wxDocMDIChildFrame*
2337 #else
2338 wxDocChildFrame*
2339 #endif
2340 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2341 {
2342 #ifdef CTSIM_MDI
2343   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2344 #else
2345   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2346 #endif
2347   theApp->setIconForFrame (subframe);
2348   
2349   wxMenu *m_pFileMenu = new wxMenu;
2350   
2351   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2352   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2353   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2354   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2355   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2356   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2357   
2358   m_pFileMenu->AppendSeparator();
2359   m_pFileMenu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
2360   
2361   m_pFileMenu->AppendSeparator();
2362   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2363   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2364   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2365 #ifdef CTSIM_MDI
2366   m_pFileMenu->AppendSeparator();
2367   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2368 #endif
2369   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2370   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
2371   
2372   wxMenu *convert_menu = new wxMenu;
2373   convert_menu->Append (PJMENU_CONVERT_POLAR, "&Polar Image...\tCtrl-L");
2374   convert_menu->Append (PJMENU_CONVERT_FFT_POLAR, "&FFT->Polar Image...\tCtrl-I");
2375   
2376   wxMenu *reconstruct_menu = new wxMenu;
2377   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FBP, "&Filtered Backprojection...\tCtrl-R");
2378   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FOURIER, "&Fourier...\tCtrl-E");
2379   
2380   wxMenu *help_menu = new wxMenu;
2381   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2382   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2383   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2384   
2385   wxMenuBar *menu_bar = new wxMenuBar;
2386   
2387   menu_bar->Append (m_pFileMenu, "&File");
2388   menu_bar->Append (convert_menu, "&Convert");
2389   menu_bar->Append (reconstruct_menu, "&Reconstruct");
2390   menu_bar->Append (help_menu, "&Help");
2391   
2392   subframe->SetMenuBar(menu_bar);  
2393   subframe->Centre(wxBOTH);
2394   
2395   wxAcceleratorEntry accelEntries[11];
2396   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2397   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2398   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2399   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2400   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
2401   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
2402   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2403   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('L'), PJMENU_CONVERT_POLAR);
2404   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('I'), PJMENU_CONVERT_FFT_POLAR);
2405   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('R'), PJMENU_RECONSTRUCT_FBP);
2406   accelEntries[10].Set (wxACCEL_CTRL, static_cast<int>('E'), PJMENU_RECONSTRUCT_FOURIER);
2407   wxAcceleratorTable accelTable (11, accelEntries);
2408   subframe->SetAcceleratorTable (accelTable);
2409   
2410   return subframe;
2411 }
2412
2413
2414 bool 
2415 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2416 {
2417   m_pFrame = CreateChildFrame(doc, this);
2418   SetFrame(m_pFrame);
2419   
2420   int width, height;
2421   m_pFrame->GetClientSize(&width, &height);
2422   m_pFrame->SetTitle("ProjectionFileView");
2423   m_pCanvas = CreateCanvas(this, m_pFrame);
2424   
2425 #ifdef __X__
2426   int x, y;  // X requires a forced resize
2427   m_pFrame->GetSize(&x, &y);
2428   m_pFrame->SetSize(-1, -1, x, y);
2429 #endif
2430   
2431   m_pFrame->Show(true);
2432   Activate(true);
2433   
2434   return true;
2435 }
2436
2437 void 
2438 ProjectionFileView::OnDraw (wxDC* dc)
2439 {
2440   wxSize clientSize = m_pFrame->GetClientSize();
2441   wxSize bestSize = m_pCanvas->GetBestSize();
2442   
2443   if (clientSize.x > bestSize.x || clientSize.y > bestSize.y)
2444     m_pFrame->SetClientSize (bestSize);
2445   
2446   if (m_bitmap.Ok())
2447     dc->DrawBitmap (m_bitmap, 0, 0, false);
2448 }
2449
2450
2451 void 
2452 ProjectionFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2453 {
2454   const Projections& rProj = GetDocument()->getProjections();
2455   const int nDet = rProj.nDet();
2456   const int nView = rProj.nView();
2457   if (nDet != 0 && nView != 0) {
2458     const DetectorArray& detarray = rProj.getDetectorArray(0);
2459     const DetectorValue* detval = detarray.detValues();
2460     double min = detval[0];
2461     double max = detval[0];
2462     for (int iy = 0; iy < nView; iy++) {
2463       const DetectorArray& detarray = rProj.getDetectorArray(iy);
2464       const DetectorValue* detval = detarray.detValues();
2465       for (int ix = 0; ix < nDet; ix++) {
2466         if (min > detval[ix])
2467           min = detval[ix];
2468         else if (max < detval[ix])
2469           max = detval[ix];
2470       }
2471     }
2472     
2473     unsigned char* imageData = new unsigned char [nDet * nView * 3];
2474     double scale = (max - min) / 255;
2475     for (int iy2 = 0; iy2 < nView; iy2++) {
2476       const DetectorArray& detarray = rProj.getDetectorArray (iy2);
2477       const DetectorValue* detval = detarray.detValues();
2478       for (int ix = 0; ix < nDet; ix++) {
2479         int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
2480         intensity = clamp(intensity, 0, 255);
2481         int baseAddr = (iy2 * nDet + ix) * 3;
2482         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
2483       }
2484     }
2485     wxImage image (nDet, nView, imageData, true);
2486     m_bitmap = image.ConvertToBitmap();
2487     delete imageData;
2488     int xSize = nDet;
2489     int ySize = nView;
2490     xSize = clamp (xSize, 0, 800);
2491     ySize = clamp (ySize, 0, 800);
2492     m_pFrame->SetClientSize (xSize, ySize);
2493     m_pCanvas->SetScrollbars (20, 20, nDet/20, nView/20);
2494   }
2495   
2496   if (m_pCanvas)
2497     m_pCanvas->Refresh();
2498 }
2499
2500 bool 
2501 ProjectionFileView::OnClose (bool deleteWindow)
2502 {
2503   if (! GetDocument() || ! GetDocument()->Close())
2504     return false;
2505   
2506   if (m_pCanvas) {
2507     m_pCanvas->Clear();
2508     m_pCanvas->setView(NULL);
2509         m_pCanvas->Show(false);
2510     m_pCanvas = NULL;
2511   }
2512   wxString s(wxTheApp->GetAppName());
2513   if (m_pFrame)
2514     m_pFrame->SetTitle(s);
2515 //  SetFrame(NULL);
2516   
2517   Activate(false);
2518   
2519   if (deleteWindow) {
2520           m_pFrame->Show(false);
2521           m_pFrame->Destroy();
2522 //        delete m_pFrame;
2523     m_pFrame = NULL;
2524   }
2525   return true;
2526 }
2527
2528
2529
2530 // PlotFileCanvas
2531 PlotFileCanvas::PlotFileCanvas (PlotFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2532 : wxScrolledWindow(frame, -1, pos, size, style)
2533 {
2534   m_pView = v;
2535 }
2536
2537 PlotFileCanvas::~PlotFileCanvas ()
2538 {
2539     m_pView = NULL;
2540 }
2541
2542 void 
2543 PlotFileCanvas::OnDraw(wxDC& dc)
2544 {
2545   if (m_pView)
2546     m_pView->OnDraw(& dc);
2547 }
2548
2549
2550 // PlotFileView
2551
2552 IMPLEMENT_DYNAMIC_CLASS(PlotFileView, wxView)
2553
2554 BEGIN_EVENT_TABLE(PlotFileView, wxView)
2555 EVT_MENU(PJMENU_FILE_PROPERTIES, PlotFileView::OnProperties)
2556 EVT_MENU(PLOTMENU_VIEW_SCALE_MINMAX, PlotFileView::OnScaleMinMax)
2557 EVT_MENU(PLOTMENU_VIEW_SCALE_AUTO, PlotFileView::OnScaleAuto)
2558 EVT_MENU(PLOTMENU_VIEW_SCALE_FULL, PlotFileView::OnScaleFull)
2559 END_EVENT_TABLE()
2560
2561 PlotFileView::PlotFileView() 
2562 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pEZPlot(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
2563 {
2564 }
2565
2566 PlotFileView::~PlotFileView()
2567 {
2568   if (m_pEZPlot)
2569     delete m_pEZPlot;
2570
2571   theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);  
2572 }
2573
2574 void
2575 PlotFileView::OnProperties (wxCommandEvent& event)
2576 {
2577   const PlotFile& rPlot = GetDocument()->getPlotFile();
2578   std::ostringstream os;
2579   os << "Columns: " << rPlot.getNumColumns() << ", Records: " << rPlot.getNumRecords() << "\n";
2580   rPlot.printHeadersBrief (os);
2581   *theApp->getLog() << os.str().c_str();
2582   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Plot File Properties", wxOK | wxICON_INFORMATION);
2583   dialogMsg.ShowModal();
2584 }
2585
2586
2587 void 
2588 PlotFileView::OnScaleAuto (wxCommandEvent& event)
2589 {
2590   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2591   double min, max, mean, mode, median, stddev;
2592   rPlotFile.statistics (1, min, max, mean, mode, median, stddev);
2593   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
2594   int iRetVal = dialogAutoScale.ShowModal();
2595   if (iRetVal == wxID_OK) {
2596     m_bMinSpecified = true;
2597     m_bMaxSpecified = true;
2598     double dMin, dMax;
2599     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
2600       m_dMinPixel = dMin;
2601       m_dMaxPixel = dMax;
2602       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
2603       OnUpdate (this, NULL);
2604     }
2605   }
2606 }
2607
2608 void 
2609 PlotFileView::OnScaleMinMax (wxCommandEvent& event)
2610 {
2611   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2612   double min;
2613   double max;
2614   
2615   if (! m_bMinSpecified || ! m_bMaxSpecified) {
2616     if (! rPlotFile.getMinMax (1, min, max)) {
2617       *theApp->getLog() << "Error: unable to find Min/Max\n";
2618       return;
2619     }
2620   }
2621   
2622   if (m_bMinSpecified)
2623     min = m_dMinPixel;
2624   if (m_bMaxSpecified)
2625     max = m_dMaxPixel;
2626   
2627   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Y-axis Minimum & Maximum", min, max);
2628   int retVal = dialogMinMax.ShowModal();
2629   if (retVal == wxID_OK) {
2630     m_bMinSpecified = true;
2631     m_bMaxSpecified = true;
2632     m_dMinPixel = dialogMinMax.getMinimum();
2633     m_dMaxPixel = dialogMinMax.getMaximum();
2634     OnUpdate (this, NULL);
2635   }
2636 }
2637
2638 void 
2639 PlotFileView::OnScaleFull (wxCommandEvent& event)
2640 {
2641   if (m_bMinSpecified || m_bMaxSpecified) {
2642     m_bMinSpecified = false;
2643     m_bMaxSpecified = false;
2644     OnUpdate (this, NULL);
2645   }
2646 }
2647
2648
2649 PlotFileCanvas* 
2650 PlotFileView::CreateCanvas (wxView *view, wxFrame *parent)
2651 {
2652   PlotFileCanvas* pCanvas;
2653   int width, height;
2654   parent->GetClientSize(&width, &height);
2655   
2656   pCanvas = new PlotFileCanvas (dynamic_cast<PlotFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
2657   
2658   pCanvas->SetBackgroundColour(*wxWHITE);
2659   pCanvas->Clear();
2660   
2661   return pCanvas;
2662 }
2663
2664 #if CTSIM_MDI
2665 wxDocMDIChildFrame*
2666 #else
2667 wxDocChildFrame*
2668 #endif
2669 PlotFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2670 {
2671 #ifdef CTSIM_MDI
2672   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2673 #else
2674   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2675 #endif
2676   theApp->setIconForFrame (subframe);
2677   
2678   wxMenu *m_pFileMenu = new wxMenu;
2679   
2680   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2681   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2682   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2683   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2684   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2685   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2686   
2687   m_pFileMenu->AppendSeparator();
2688   m_pFileMenu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
2689   
2690   m_pFileMenu->AppendSeparator();
2691   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2692   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2693   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2694 #ifdef CTSIM_MDI
2695   m_pFileMenu->AppendSeparator();
2696   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2697 #endif
2698   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2699   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
2700   
2701   wxMenu *view_menu = new wxMenu;
2702   view_menu->Append(PLOTMENU_VIEW_SCALE_MINMAX, "Display Scale &Set...\tCtrl-E");
2703   view_menu->Append(PLOTMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
2704   view_menu->Append(PLOTMENU_VIEW_SCALE_FULL, "Display &Full Scale\tCtrl-U");
2705   
2706   wxMenu *help_menu = new wxMenu;
2707   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2708   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2709   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2710   
2711   wxMenuBar *menu_bar = new wxMenuBar;
2712   
2713   menu_bar->Append(m_pFileMenu, "&File");
2714   menu_bar->Append(view_menu, "&View");
2715   menu_bar->Append(help_menu, "&Help");
2716   
2717   subframe->SetMenuBar(menu_bar);
2718   subframe->Centre(wxBOTH);
2719   
2720   wxAcceleratorEntry accelEntries[10];
2721   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2722   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2723   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2724   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2725   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
2726   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
2727   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2728   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('E'), PLOTMENU_VIEW_SCALE_MINMAX);
2729   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('A'), PLOTMENU_VIEW_SCALE_AUTO);
2730   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('U'), PLOTMENU_VIEW_SCALE_FULL);
2731   wxAcceleratorTable accelTable (10, accelEntries);
2732   subframe->SetAcceleratorTable (accelTable);
2733   
2734   return subframe;
2735 }
2736
2737
2738 bool 
2739 PlotFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
2740 {
2741   m_pFrame = CreateChildFrame(doc, this);
2742   SetFrame(m_pFrame);
2743   
2744   m_bMinSpecified = false;
2745   m_bMaxSpecified = false;
2746   m_dAutoScaleFactor = 1.;
2747   
2748   int width, height;
2749   m_pFrame->GetClientSize(&width, &height);
2750   m_pFrame->SetTitle ("Plot File");
2751   m_pCanvas = CreateCanvas (this, m_pFrame);
2752   
2753 #ifdef __X__
2754   int x, y;  // X requires a forced resize
2755   m_pFrame->GetSize(&x, &y);
2756   m_pFrame->SetSize(-1, -1, x, y);
2757 #endif
2758   
2759   m_pFrame->Show(true);
2760   Activate(true);
2761   
2762   return true;
2763 }
2764
2765 void 
2766 PlotFileView::OnDraw (wxDC* dc)
2767 {
2768   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2769   const int iNColumns = rPlotFile.getNumColumns();
2770   const int iNRecords = rPlotFile.getNumRecords();
2771   
2772   if (iNColumns > 0 && iNRecords > 0) {
2773     int xsize, ysize;
2774     m_pCanvas->GetClientSize (&xsize, &ysize);
2775     SGPDriver driver (dc, xsize, ysize);
2776     SGP sgp (driver);
2777     if (m_pEZPlot)
2778       m_pEZPlot->plot (&sgp);
2779   }
2780 }
2781
2782
2783 void 
2784 PlotFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2785 {
2786   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2787   const int iNColumns = rPlotFile.getNumColumns();
2788   const int iNRecords = rPlotFile.getNumRecords();
2789   
2790   if (iNColumns > 0 && iNRecords > 0) {
2791     if (m_pEZPlot)
2792       delete m_pEZPlot;
2793     m_pEZPlot = new EZPlot;
2794     
2795     for (unsigned int iEzset = 0; iEzset < rPlotFile.getNumEzsetCommands(); iEzset++)
2796       m_pEZPlot->ezset (rPlotFile.getEzsetCommand (iEzset));
2797     
2798     if (m_bMinSpecified) {
2799       std::ostringstream os;
2800       os << "ymin " << m_dMinPixel;
2801       m_pEZPlot->ezset (os.str());
2802     }
2803     
2804     if (m_bMaxSpecified) {
2805       std::ostringstream os;
2806       os << "ymax " << m_dMaxPixel;
2807       m_pEZPlot->ezset (os.str());
2808     }
2809     
2810     m_pEZPlot->ezset("box");
2811     m_pEZPlot->ezset("grid");
2812     
2813     double* pdXaxis = new double [iNRecords];
2814     rPlotFile.getColumn (0, pdXaxis);
2815     
2816     double* pdY = new double [iNRecords];
2817     for (int iCol = 1; iCol < iNColumns; iCol++) {
2818       rPlotFile.getColumn (iCol, pdY);
2819       m_pEZPlot->addCurve (pdXaxis, pdY, iNRecords);
2820     }
2821     
2822     delete pdXaxis;
2823     delete pdY;
2824   }
2825   
2826   if (m_pCanvas)
2827     m_pCanvas->Refresh();
2828 }
2829
2830 bool 
2831 PlotFileView::OnClose (bool deleteWindow)
2832 {
2833   if (! GetDocument() || ! GetDocument()->Close())
2834     return false;
2835   
2836   if (m_pCanvas) {
2837     m_pCanvas->Clear();
2838     m_pCanvas->setView (NULL);
2839         m_pCanvas->Show(false);
2840     m_pCanvas = NULL;
2841   }
2842   wxString s(wxTheApp->GetAppName());
2843   if (m_pFrame)
2844     m_pFrame->SetTitle(s);
2845   SetFrame(NULL);
2846   
2847   Activate(false);
2848   
2849   if (deleteWindow) {
2850           m_pFrame->Show(false);
2851           m_pFrame->Destroy();
2852 //        delete m_pFrame;
2853     m_pFrame = NULL;
2854   }
2855   return true;
2856 }
2857
2858
2859 ////////////////////////////////////////////////////////////////
2860
2861
2862 IMPLEMENT_DYNAMIC_CLASS(TextFileView, wxView)
2863
2864 TextFileView::~TextFileView() 
2865 {
2866     theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);
2867 }
2868
2869 bool TextFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2870 {
2871   m_pFrame = CreateChildFrame(doc, this);
2872   SetFrame (m_pFrame);
2873   
2874   int width, height;
2875   m_pFrame->GetClientSize(&width, &height);
2876   m_pFrame->SetTitle("TextFile");
2877   m_pCanvas = new TextFileCanvas (this, m_pFrame, wxPoint(0, 0), wxSize(width, height), wxTE_MULTILINE | wxTE_READONLY);
2878   m_pFrame->SetTitle("Log");
2879   
2880 #ifdef __X__
2881   // X seems to require a forced resize
2882   int x, y;
2883   frame->GetSize(&x, &y);
2884   frame->SetSize(-1, -1, x, y);
2885 #endif
2886   
2887   m_pFrame->Show (true);
2888   Activate (true);
2889   
2890   return true;
2891 }
2892
2893 // Handled by wxTextWindow
2894 void TextFileView::OnDraw(wxDC *WXUNUSED(dc) )
2895 {
2896 }
2897
2898 void TextFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2899 {
2900 }
2901
2902 bool 
2903 TextFileView::OnClose (bool deleteWindow)
2904 {
2905         if (m_pFrame && m_pFrame->GetTitle() == "Log")
2906        return false;
2907   
2908   if (! GetDocument() || ! GetDocument()->Close())
2909     return false;
2910
2911   m_pCanvas->Show(false);
2912   Activate(false);
2913   
2914   if (deleteWindow) {
2915           m_pFrame->Show(false);
2916           m_pFrame->Destroy();
2917 //        delete m_pFrame;
2918         m_pFrame = NULL;
2919    
2920   }
2921   return TRUE;
2922 }
2923
2924 #if CTSIM_MDI
2925 wxDocMDIChildFrame*
2926 #else
2927 wxDocChildFrame*
2928 #endif
2929 TextFileView::CreateChildFrame (wxDocument *doc, wxView *view)
2930 {
2931 #if CTSIM_MDI
2932   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(300, 150), wxDEFAULT_FRAME_STYLE);
2933 #else
2934   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(300, 150), wxDEFAULT_FRAME_STYLE);
2935 #endif
2936   theApp->setIconForFrame (subframe);
2937   
2938   wxMenu *m_pFileMenu = new wxMenu;
2939   
2940   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2941   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2942   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2943   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2944   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2945   //  m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2946   
2947   m_pFileMenu->AppendSeparator();
2948   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2949   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2950   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2951 #ifdef CTSIM_MDI
2952   m_pFileMenu->AppendSeparator();
2953   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2954 #endif
2955   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2956   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
2957   
2958   wxMenu *help_menu = new wxMenu;
2959   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2960   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2961   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2962   
2963   wxMenuBar *menu_bar = new wxMenuBar;
2964   
2965   menu_bar->Append(m_pFileMenu, "&File");
2966   menu_bar->Append(help_menu, "&Help");
2967   
2968   subframe->SetMenuBar(menu_bar);
2969   subframe->Centre(wxBOTH);
2970   
2971   wxAcceleratorEntry accelEntries[5];
2972   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2973   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2974   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2975   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2976   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2977   wxAcceleratorTable accelTable (5, accelEntries);
2978   subframe->SetAcceleratorTable (accelTable);
2979   
2980   return subframe;
2981 }
2982
2983
2984 // Define a constructor for my text subwindow
2985 TextFileCanvas::TextFileCanvas (TextFileView* v, wxFrame* frame, const wxPoint& pos, const wxSize& size, long style)
2986   : wxTextCtrl (frame, -1, "", pos, size, style), m_pView(v)
2987 {
2988 }
2989
2990 TextFileCanvas::~TextFileCanvas ()
2991 {
2992         m_pView = NULL;
2993 }