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