r446: no message
[ctsim.git] / src / views.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          view.cpp
5 **   Purpose:       View & Canvas routines for CTSim program
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: views.cpp,v 1.78 2001/01/27 03:08:16 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 // For compilers that support precompilation, includes "wx/wx.h".
29 #include "wx/wxprec.h"
30
31 #ifdef __BORLANDC__
32 #pragma hdrstop
33 #endif
34
35 #ifndef WX_PRECOMP
36 #include "wx/wx.h"
37 #endif
38
39 #if !wxUSE_DOC_VIEW_ARCHITECTURE
40 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
41 #endif
42
43 #include "wx/image.h"
44 #include "wx/progdlg.h"
45
46 #include "ct.h"
47 #include "ctsim.h"
48 #include "docs.h"
49 #include "views.h"
50 #include "dialogs.h"
51 #include "dlgprojections.h"
52 #include "dlgreconstruct.h"
53 #include "backprojectors.h"
54 #include "reconstruct.h"
55 #include "timer.h"
56
57 #if defined(MSVC) || HAVE_SSTREAM
58 #include <sstream>
59 #else
60 #include <sstream_subst>
61 #endif
62
63
64 // ImageFileCanvas
65
66 BEGIN_EVENT_TABLE(ImageFileCanvas, wxScrolledWindow)
67 EVT_MOUSE_EVENTS(ImageFileCanvas::OnMouseEvent)
68 END_EVENT_TABLE()
69
70
71 ImageFileCanvas::ImageFileCanvas (ImageFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
72 : wxScrolledWindow(frame, -1, pos, size, style), m_pView(v), m_xCursor(-1), m_yCursor(-1)
73 {
74 }
75
76 ImageFileCanvas::~ImageFileCanvas()
77 {
78         m_pView = NULL;
79 }
80
81 void 
82 ImageFileCanvas::OnDraw(wxDC& dc)
83 {
84   if (m_pView)
85     m_pView->OnDraw(& dc);
86 }
87
88 void 
89 ImageFileCanvas::DrawRubberBandCursor (wxDC& dc, int x, int y)
90 {
91   const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
92   int nx = rIF.nx();
93   int ny = rIF.ny();
94   
95   int yPt = ny - y - 1;
96   dc.SetLogicalFunction (wxINVERT);
97   dc.SetPen (*wxGREEN_PEN);
98   dc.DrawLine (0, yPt, nx, yPt);
99   dc.DrawLine (x, 0, x, ny);
100   dc.SetLogicalFunction (wxCOPY);
101 }
102
103 bool
104 ImageFileCanvas::GetCurrentCursor (int& x, int& y)
105 {
106   x = m_xCursor;
107   y = m_yCursor;
108   
109   if (m_xCursor >= 0 && m_yCursor >= 0)
110     return true;
111   else
112     return false;
113 }
114
115 void 
116 ImageFileCanvas::OnMouseEvent(wxMouseEvent& event)
117 {
118   if (! m_pView)
119     return;
120   
121   wxClientDC dc(this);
122   PrepareDC(dc);
123   
124   wxPoint pt(event.GetLogicalPosition(dc));
125   
126   const ImageFileDocument* pIFDoc = m_pView->GetDocument();
127   if (! pIFDoc)
128     return;
129   const ImageFile& rIF = pIFDoc->getImageFile();
130   ImageFileArrayConst v = rIF.getArray();
131   int nx = rIF.nx();
132   int ny = rIF.ny();
133   const int yPt = ny - 1 - pt.y;
134   if (event.RightIsDown()) {
135     if (pt.x >= 0 && pt.x < nx && pt.y >= 0 && pt.y < ny) {
136       std::ostringstream os;
137       os << "Image value (" << pt.x << "," << yPt << ") = " << v[pt.x][yPt];
138       if (rIF.isComplex()) {
139         double dImag = rIF.getImaginaryArray()[pt.x][yPt];
140         if (dImag < 0)
141           os << " - " << -dImag;
142         else
143           os << " + " << dImag;
144         os << "i\n";
145       } else
146         os << "\n";
147       *theApp->getLog() << os.str().c_str();
148     } else
149       *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << yPt << ")\n";
150   }
151   else if (event.LeftIsDown() || event.LeftUp() || event.RightUp()) {
152     if (pt.x >= 0 && pt.x < nx && pt.y >= 0 && pt.y < ny) {
153       if (m_xCursor >= 0 && m_yCursor >= 0) {
154         DrawRubberBandCursor (dc, m_xCursor, m_yCursor);
155       }
156       DrawRubberBandCursor (dc, pt.x, yPt);
157       m_xCursor = pt.x;
158       m_yCursor = yPt;
159     } else
160       *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << yPt << ")\n";
161   }
162   if (event.LeftUp()) {
163     std::ostringstream os;
164     os << "Selected column " << pt.x << " , row " << yPt << "\n";
165     *theApp->getLog() << os.str().c_str();
166   }
167 }
168
169 wxSize
170 ImageFileCanvas::GetBestSize() const
171 {
172   if (! m_pView)
173     return wxSize(0,0);
174   
175   const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
176   return wxSize (rIF.nx(), rIF.ny());
177 }
178
179
180 // ImageFileView
181
182 IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
183
184 BEGIN_EVENT_TABLE(ImageFileView, wxView)
185 EVT_MENU(IFMENU_FILE_EXPORT, ImageFileView::OnExport)
186 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
187 EVT_MENU(IFMENU_VIEW_SCALE_MINMAX, ImageFileView::OnScaleMinMax)
188 EVT_MENU(IFMENU_VIEW_SCALE_AUTO, ImageFileView::OnScaleAuto)
189 EVT_MENU(IFMENU_VIEW_SCALE_FULL, ImageFileView::OnScaleFull)
190 EVT_MENU(IFMENU_COMPARE_IMAGES, ImageFileView::OnCompare)
191 EVT_MENU(IFMENU_COMPARE_ROW, ImageFileView::OnCompareRow)
192 EVT_MENU(IFMENU_COMPARE_COL, ImageFileView::OnCompareCol)
193 EVT_MENU(IFMENU_FILTER_INVERTVALUES, ImageFileView::OnInvertValues)
194 EVT_MENU(IFMENU_FILTER_SQUARE, ImageFileView::OnSquare)
195 EVT_MENU(IFMENU_FILTER_SQRT, ImageFileView::OnSquareRoot)
196 EVT_MENU(IFMENU_FILTER_LOG, ImageFileView::OnLog)
197 EVT_MENU(IFMENU_FILTER_EXP, ImageFileView::OnExp)
198 EVT_MENU(IFMENU_FILTER_FOURIER, ImageFileView::OnFourier)
199 EVT_MENU(IFMENU_FILTER_INVERSE_FOURIER, ImageFileView::OnInverseFourier)
200 EVT_MENU(IFMENU_FILTER_SHUFFLEFOURIERTONATURALORDER, ImageFileView::OnShuffleFourierToNaturalOrder)
201 EVT_MENU(IFMENU_FILTER_SHUFFLENATURALTOFOURIERORDER, ImageFileView::OnShuffleNaturalToFourierOrder)
202 EVT_MENU(IFMENU_IMAGE_ADD, ImageFileView::OnAdd)
203 EVT_MENU(IFMENU_IMAGE_SUBTRACT, ImageFileView::OnSubtract)
204 EVT_MENU(IFMENU_IMAGE_MULTIPLY, ImageFileView::OnMultiply)
205 EVT_MENU(IFMENU_IMAGE_DIVIDE, ImageFileView::OnDivide)
206 EVT_MENU(IFMENU_IMAGE_SCALESIZE, ImageFileView::OnScaleSize)
207 #ifdef HAVE_FFT
208 EVT_MENU(IFMENU_FILTER_FFT, ImageFileView::OnFFT)
209 EVT_MENU(IFMENU_FILTER_IFFT, ImageFileView::OnIFFT)
210 EVT_MENU(IFMENU_FILTER_FFT_ROWS, ImageFileView::OnFFTRows)
211 EVT_MENU(IFMENU_FILTER_IFFT_ROWS, ImageFileView::OnIFFTRows)
212 EVT_MENU(IFMENU_FILTER_FFT_COLS, ImageFileView::OnFFTCols)
213 EVT_MENU(IFMENU_FILTER_IFFT_COLS, ImageFileView::OnIFFTCols)
214 #endif
215 EVT_MENU(IFMENU_FILTER_MAGNITUDE, ImageFileView::OnMagnitude)
216 EVT_MENU(IFMENU_FILTER_PHASE, ImageFileView::OnPhase)
217 EVT_MENU(IFMENU_PLOT_ROW, ImageFileView::OnPlotRow)
218 EVT_MENU(IFMENU_PLOT_COL, ImageFileView::OnPlotCol)
219 #ifdef HAVE_FFT
220 EVT_MENU(IFMENU_PLOT_FFT_ROW, ImageFileView::OnPlotFFTRow)
221 EVT_MENU(IFMENU_PLOT_FFT_COL, ImageFileView::OnPlotFFTCol)
222 #endif
223 EVT_MENU(IFMENU_PLOT_HISTOGRAM, ImageFileView::OnPlotHistogram)
224 END_EVENT_TABLE()
225
226 ImageFileView::ImageFileView() 
227 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
228 {
229   m_iDefaultExportFormatID = ImageFile::FORMAT_PNG;
230 }
231
232 ImageFileView::~ImageFileView()
233 {
234   theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);
235 }
236
237 void
238 ImageFileView::OnProperties (wxCommandEvent& event)
239 {
240   const ImageFile& rIF = GetDocument()->getImageFile();
241   if (rIF.nx() == 0 || rIF.ny() == 0)
242     *theApp->getLog() << "Properties: empty imagefile\n";
243   else {
244     const std::string& rFilename = rIF.getFilename();
245     std::ostringstream os;
246     double min, max, mean, mode, median, stddev;
247     rIF.statistics (rIF.getArray(), min, max, mean, mode, median, stddev);
248     os << "Filename: " << rFilename << "\n";
249     os << "Size: (" << rIF.nx() << "," << rIF.ny() << ")\n";
250     os << "Data type: ";
251     if (rIF.isComplex())
252       os << "Complex\n";
253     else
254       os << "Real\n";
255     os << "\nMinimum: "<<min<<"\nMaximum: "<<max<<"\nMean: "<<mean<<"\nMedian: "<<median<<"\nMode: "<<mode<<"\nStandard Deviation: "<<stddev << "\n";
256     if (rIF.isComplex()) {
257       rIF.statistics (rIF.getImaginaryArray(), min, max, mean, mode, median, stddev);
258       os << "\nImaginary: min: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmedian: "<<median<<"\nmode: "<<mode<<"\nstddev: "<<stddev << "\n";
259     }
260     if (rIF.nLabels() > 0) {
261       os << "\n";
262       rIF.printLabelsBrief (os);
263     }
264     *theApp->getLog() << os.str().c_str();
265     wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
266     dialogMsg.ShowModal();
267   }
268 }
269
270 void 
271 ImageFileView::OnScaleAuto (wxCommandEvent& event)
272 {
273   const ImageFile& rIF = GetDocument()->getImageFile();
274   double min, max, mean, mode, median, stddev;
275   rIF.statistics(min, max, mean, mode, median, stddev);
276   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
277   int iRetVal = dialogAutoScale.ShowModal();
278   if (iRetVal == wxID_OK) {
279     m_bMinSpecified = true;
280     m_bMaxSpecified = true;
281     double dMin, dMax;
282     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
283       m_dMinPixel = dMin;
284       m_dMaxPixel = dMax;
285       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
286       OnUpdate (this, NULL);
287     }
288   }
289 }
290
291 void 
292 ImageFileView::OnScaleMinMax (wxCommandEvent& event)
293 {
294   const ImageFile& rIF = GetDocument()->getImageFile();
295   double min, max;
296   if (! m_bMinSpecified && ! m_bMaxSpecified)
297     rIF.getMinMax (min, max);
298   
299   if (m_bMinSpecified)
300     min = m_dMinPixel;
301   if (m_bMaxSpecified)
302     max = m_dMaxPixel;
303   
304   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Image Minimum & Maximum", min, max);
305   int retVal = dialogMinMax.ShowModal();
306   if (retVal == wxID_OK) {
307     m_bMinSpecified = true;
308     m_bMaxSpecified = true;
309     m_dMinPixel = dialogMinMax.getMinimum();
310     m_dMaxPixel = dialogMinMax.getMaximum();
311     OnUpdate (this, NULL);
312   }
313 }
314
315 void 
316 ImageFileView::OnScaleFull (wxCommandEvent& event)
317 {
318   if (m_bMinSpecified || m_bMaxSpecified) {
319     m_bMinSpecified = false;
320     m_bMaxSpecified = false;
321     OnUpdate (this, NULL);
322   }
323 }
324
325 void
326 ImageFileView::OnCompare (wxCommandEvent& event)
327 {
328   std::vector<ImageFileDocument*> vecIF;
329   theApp->getCompatibleImages (GetDocument(), vecIF);
330   
331   if (vecIF.size() == 0) {
332     wxMessageBox("There are no compatible image files open for comparision", "No comparison images");
333   } else {
334     DialogGetComparisonImage dialogGetCompare(getFrameForChild(), "Get Comparison Image", vecIF, true);
335     
336     if (dialogGetCompare.ShowModal() == wxID_OK) {
337       const ImageFile& rIF = GetDocument()->getImageFile();
338       ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
339       const ImageFile& rCompareIF = pCompareDoc->getImageFile();
340       std::ostringstream os;
341       double min, max, mean, mode, median, stddev;
342       rIF.statistics (min, max, mean, mode, median, stddev);
343       os << GetFrame()->GetTitle().c_str() << ": minimum=" << min << ", maximum=" << max << ", mean=" << mean << ", mode=" << mode << ", median=" << median << ", stddev=" << stddev << "\n";
344       rCompareIF.statistics (min, max, mean, mode, median, stddev);
345       os << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str() << ": minimum=" << min << ", maximum=" << max << ", mean=" << mean << ", mode=" << mode << ", median=" << median << ", stddev=" << stddev << "\n";
346       os << "\n";
347       double d, r, e;
348       rIF.comparativeStatistics (rCompareIF, d, r, e);
349       os << "Comparative Statistics: d=" << d << ", r=" << r << ", e=" << e << "\n";
350       *theApp->getLog() << os.str().c_str();
351       if (dialogGetCompare.getMakeDifferenceImage()) {
352         ImageFileDocument* pDifferenceDoc = theApp->newImageDoc();
353         if (! pDifferenceDoc) {
354           sys_error (ERR_SEVERE, "Unable to create image file");
355           return;
356         }
357         ImageFile& differenceImage = pDifferenceDoc->getImageFile();
358         
359         differenceImage.setArraySize (rIF.nx(), rIF.ny());
360         if (! rIF.subtractImages (rCompareIF, differenceImage)) {
361           pDifferenceDoc->Close();
362           //pDifferenceDoc->DeleteAllViews();
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 (wxView *view, wxFrame *parent)
773 {
774   ImageFileCanvas* pCanvas;
775   int width, height;
776   parent->GetClientSize(&width, &height);
777   
778   pCanvas = new ImageFileCanvas (dynamic_cast<ImageFileView*>(view), 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   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
823   theApp->getDocManager()->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   SetFrame (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 (this, 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 = dynamic_cast<ImageFileDocument*>(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->Clear();
1004         m_pCanvas->Show(false);
1005     m_pCanvas->setView(NULL);
1006     m_pCanvas = NULL;
1007   }
1008   wxString s(theApp->GetAppName());
1009   if (m_pFrame)
1010     m_pFrame->SetTitle(s);
1011   SetFrame(NULL);
1012   
1013   Activate(false);
1014   
1015   if (deleteWindow) {
1016           m_pFrame->Show(false);
1017           m_pFrame->Destroy();
1018 //        delete m_pFrame;
1019     m_pFrame = NULL;
1020   }
1021   return true;
1022 }
1023
1024 void
1025 ImageFileView::OnExport (wxCommandEvent& event)
1026 {
1027   ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1028   ImageFileArrayConst v = rIF.getArray();
1029   int nx = rIF.nx();
1030   int ny = rIF.ny();
1031   if (v != NULL && nx != 0 && ny != 0) {
1032     if (! m_bMinSpecified || ! m_bMaxSpecified) {
1033       double min, max;
1034       rIF.getMinMax (min, max);
1035       if (! m_bMinSpecified)
1036         m_dMinPixel = min;
1037       if (! m_bMaxSpecified)
1038         m_dMaxPixel = max;
1039     }
1040     
1041     DialogExportParameters dialogExport (getFrameForChild(), m_iDefaultExportFormatID);
1042     if (dialogExport.ShowModal() == wxID_OK) {
1043       wxString strFormatName (dialogExport.getFormatName ());
1044       m_iDefaultExportFormatID = ImageFile::convertFormatNameToID (strFormatName.c_str());
1045       
1046       wxString strExt;
1047       wxString strWildcard;
1048       if (m_iDefaultExportFormatID == ImageFile::FORMAT_PGM || m_iDefaultExportFormatID == ImageFile::FORMAT_PGMASCII) {
1049         strExt = ".pgm";
1050         strWildcard = "PGM Files (*.pgm)|*.pgm";
1051       }
1052 #ifdef HAVE_PNG
1053       else if (m_iDefaultExportFormatID == ImageFile::FORMAT_PNG || m_iDefaultExportFormatID == ImageFile::FORMAT_PNG16) {
1054         strExt = ".png";
1055         strWildcard = "PNG Files (*.png)|*.png";
1056       }
1057 #endif
1058       
1059       const wxString& strFilename = wxFileSelector (wxString("Export Filename"), wxString(""), 
1060         wxString(""), strExt, strWildcard, wxOVERWRITE_PROMPT | wxHIDE_READONLY | wxSAVE);
1061       if (strFilename) {
1062         rIF.exportImage (strFormatName.c_str(), strFilename.c_str(), 1, 1, m_dMinPixel, m_dMaxPixel);
1063         *theApp->getLog() << "Exported file " << strFilename << "\n";
1064       }
1065     }
1066   }
1067 }
1068
1069 void
1070 ImageFileView::OnScaleSize (wxCommandEvent& event)
1071 {
1072   ImageFile& rIF = GetDocument()->getImageFile();
1073   unsigned int iOldNX = rIF.nx();
1074   unsigned int iOldNY = rIF.ny();
1075   
1076   DialogGetXYSize dialogGetXYSize (getFrameForChild(), "Set New X & Y Dimensions", iOldNX, iOldNY);
1077   if (dialogGetXYSize.ShowModal() == wxID_OK) {
1078     unsigned int iNewNX = dialogGetXYSize.getXSize();
1079     unsigned int iNewNY = dialogGetXYSize.getYSize();
1080     std::ostringstream os;
1081     os << "Scale Size from (" << iOldNX << "," << iOldNY << ") to (" << iNewNX << "," << iNewNY << ")";
1082     ImageFileDocument* pScaledDoc = theApp->newImageDoc();
1083     if (! pScaledDoc) {
1084       sys_error (ERR_SEVERE, "Unable to create image file");
1085       return;
1086     }
1087     ImageFile& rScaledIF = pScaledDoc->getImageFile();
1088     rScaledIF.setArraySize (iNewNX, iNewNY);
1089     rScaledIF.labelsCopy (rIF);
1090     rScaledIF.labelAdd (os.str().c_str());
1091     rIF.scaleImage (rScaledIF);
1092     *theApp->getLog() << os.str().c_str() << "\n";
1093     if (theApp->getSetModifyNewDocs())
1094       pScaledDoc->Modify(TRUE);
1095     pScaledDoc->UpdateAllViews (this);
1096     pScaledDoc->getView()->OnUpdate (this, NULL);
1097     pScaledDoc->getView()->getFrame()->Show(true);
1098   }
1099 }
1100
1101 void
1102 ImageFileView::OnPlotRow (wxCommandEvent& event)
1103 {
1104   int xCursor, yCursor;
1105   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1106     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1107     return;
1108   }
1109   
1110   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1111   ImageFileArrayConst v = rIF.getArray();
1112   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1113   int nx = rIF.nx();
1114   int ny = rIF.ny();
1115   
1116   if (v != NULL && yCursor < ny) {
1117     double* pX = new double [nx];
1118     double* pYReal = new double [nx];
1119     double *pYImag = NULL;
1120     double *pYMag = NULL;
1121     if (rIF.isComplex()) {
1122       pYImag = new double [nx];
1123       pYMag = new double [nx];
1124     }
1125     for (int i = 0; i < nx; i++) {
1126       pX[i] = i;
1127       pYReal[i] = v[i][yCursor];
1128       if (rIF.isComplex()) {
1129         pYImag[i] = vImag[i][yCursor];
1130         pYMag[i] = ::sqrt (v[i][yCursor] * v[i][yCursor] + vImag[i][yCursor] * vImag[i][yCursor]);
1131       }
1132     }
1133     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1134     if (! pPlotDoc) {
1135       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1136     } else {
1137       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1138       std::ostringstream os;
1139       os << "Row " << yCursor;
1140       std::string title("title ");
1141       title += os.str();
1142       rPlotFile.addEzsetCommand (title.c_str());
1143       rPlotFile.addEzsetCommand ("xlabel Column");
1144       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1145       rPlotFile.addEzsetCommand ("lxfrac 0");
1146       rPlotFile.addEzsetCommand ("box");
1147       rPlotFile.addEzsetCommand ("grid");
1148       rPlotFile.addEzsetCommand ("curve 1");
1149       rPlotFile.addEzsetCommand ("color 1");
1150       if (rIF.isComplex()) {
1151         rPlotFile.addEzsetCommand ("dash 1");
1152         rPlotFile.addEzsetCommand ("curve 2");
1153         rPlotFile.addEzsetCommand ("color 4");
1154         rPlotFile.addEzsetCommand ("dash 3");
1155         rPlotFile.addEzsetCommand ("curve 3");
1156         rPlotFile.addEzsetCommand ("color 0");
1157         rPlotFile.addEzsetCommand ("solid");
1158         rPlotFile.setCurveSize (4, nx);
1159       } else
1160         rPlotFile.setCurveSize (2, nx);
1161       rPlotFile.addColumn (0, pX);
1162       rPlotFile.addColumn (1, pYReal); 
1163       if (rIF.isComplex()) {
1164         rPlotFile.addColumn (2, pYImag);
1165         rPlotFile.addColumn (3, pYMag);
1166       }
1167       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1168         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1169       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1170       *theApp->getLog() << os.str().c_str() << "\n";
1171       rPlotFile.addDescription (os.str().c_str());
1172     }
1173     delete pX;
1174     delete pYReal;
1175     if (rIF.isComplex()) {
1176       delete pYImag;
1177       delete pYMag;
1178     }
1179     if (theApp->getSetModifyNewDocs())
1180       pPlotDoc->Modify(true);
1181     pPlotDoc->UpdateAllViews();
1182   }
1183 }
1184
1185 void
1186 ImageFileView::OnPlotCol (wxCommandEvent& event)
1187 {
1188   int xCursor, yCursor;
1189   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1190     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1191     return;
1192   }
1193   
1194   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1195   ImageFileArrayConst v = rIF.getArray();
1196   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1197   int nx = rIF.nx();
1198   int ny = rIF.ny();
1199   
1200   if (v != NULL && xCursor < nx) {
1201     double* pX = new double [ny];
1202     double* pYReal = new double [ny];
1203     double* pYImag = NULL;
1204     double* pYMag = NULL;
1205     if (rIF.isComplex()) {
1206       pYImag = new double [ny];
1207       pYMag = new double [ny];
1208     }
1209     for (int i = 0; i < ny; i++) {
1210       pX[i] = i;
1211       pYReal[i] = v[xCursor][i];
1212       if (rIF.isComplex()) {
1213         pYImag[i] = vImag[xCursor][i];
1214         pYMag[i] = ::sqrt (v[xCursor][i] * v[xCursor][i] + vImag[xCursor][i] * vImag[xCursor][i]);
1215       }
1216     }
1217     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1218     if (! pPlotDoc) {
1219       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1220     } else {
1221       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1222       std::ostringstream os;
1223       os << "Column " << xCursor;
1224       std::string title("title ");
1225       title += os.str();
1226       rPlotFile.addEzsetCommand (title.c_str());
1227       rPlotFile.addEzsetCommand ("xlabel Row");
1228       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1229       rPlotFile.addEzsetCommand ("lxfrac 0");
1230       rPlotFile.addEzsetCommand ("box");
1231       rPlotFile.addEzsetCommand ("grid");
1232       rPlotFile.addEzsetCommand ("curve 1");
1233       rPlotFile.addEzsetCommand ("color 1");
1234       if (rIF.isComplex()) {
1235         rPlotFile.addEzsetCommand ("dash 1");
1236         rPlotFile.addEzsetCommand ("curve 2");
1237         rPlotFile.addEzsetCommand ("color 4");
1238         rPlotFile.addEzsetCommand ("dash 3");
1239         rPlotFile.addEzsetCommand ("curve 3");
1240         rPlotFile.addEzsetCommand ("color 0");
1241         rPlotFile.addEzsetCommand ("solid");
1242         rPlotFile.setCurveSize (4, ny);
1243       } else
1244         rPlotFile.setCurveSize (2, ny);
1245       rPlotFile.addColumn (0, pX);
1246       rPlotFile.addColumn (1, pYReal); 
1247       if (rIF.isComplex()) {
1248         rPlotFile.addColumn (2, pYImag);
1249         rPlotFile.addColumn (3, pYMag);
1250       }
1251       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++)
1252         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1253       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1254       *theApp->getLog() << os.str().c_str() << "\n";
1255       rPlotFile.addDescription (os.str().c_str());
1256     }
1257     delete pX;
1258     delete pYReal;
1259     if (rIF.isComplex()) {
1260       delete pYImag;
1261       delete pYMag;
1262     }
1263     if (theApp->getSetModifyNewDocs())
1264       pPlotDoc->Modify(true);
1265     pPlotDoc->UpdateAllViews();
1266   }
1267 }
1268
1269 #ifdef HAVE_FFT
1270 void
1271 ImageFileView::OnPlotFFTRow (wxCommandEvent& event)
1272 {
1273   int xCursor, yCursor;
1274   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1275     wxMessageBox ("No row selected. Please use left mouse button on image to select column","Error");
1276     return;
1277   }
1278   
1279   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1280   ImageFileArrayConst v = rIF.getArray();
1281   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1282   int nx = rIF.nx();
1283   int ny = rIF.ny();
1284   
1285   if (v != NULL && yCursor < ny) {
1286     fftw_complex* pcIn = new fftw_complex [nx];
1287     
1288     int i;
1289     for (i = 0; i < nx; i++) {
1290       pcIn[i].re = v[i][yCursor];
1291       if (rIF.isComplex())
1292         pcIn[i].im = vImag[i][yCursor];
1293       else
1294         pcIn[i].im = 0;
1295     }
1296     
1297     fftw_plan plan = fftw_create_plan (nx, FFTW_FORWARD, FFTW_IN_PLACE);
1298     fftw_one (plan, pcIn, NULL);
1299     fftw_destroy_plan (plan);
1300     
1301     double* pX = new double [nx];
1302     double* pYReal = new double [nx];
1303     double* pYImag = new double [nx];
1304     double* pYMag = new double [nx];
1305     for (i = 0; i < nx; i++) {
1306       pX[i] = i;
1307       pYReal[i] = pcIn[i].re;
1308       pYImag[i] = pcIn[i].im;
1309       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1310     }
1311     Fourier::shuffleFourierToNaturalOrder (pYReal, nx);
1312     Fourier::shuffleFourierToNaturalOrder (pYImag, nx);
1313     Fourier::shuffleFourierToNaturalOrder (pYMag, nx);
1314     
1315     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1316     if (! pPlotDoc) {
1317       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1318     } else {
1319       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1320       std::ostringstream os;
1321       os << "Row " << yCursor;
1322       std::string title("title ");
1323       title += os.str();
1324       rPlotFile.addEzsetCommand (title.c_str());
1325       rPlotFile.addEzsetCommand ("xlabel Column");
1326       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1327       rPlotFile.addEzsetCommand ("lxfrac 0");
1328       rPlotFile.addEzsetCommand ("curve 1");
1329       rPlotFile.addEzsetCommand ("color 1");
1330       rPlotFile.addEzsetCommand ("dash 1");
1331       rPlotFile.addEzsetCommand ("curve 2");
1332       rPlotFile.addEzsetCommand ("color 4");
1333       rPlotFile.addEzsetCommand ("dash 3");
1334       rPlotFile.addEzsetCommand ("curve 3");
1335       rPlotFile.addEzsetCommand ("color 0");
1336       rPlotFile.addEzsetCommand ("solid");
1337       rPlotFile.addEzsetCommand ("box");
1338       rPlotFile.addEzsetCommand ("grid");
1339       rPlotFile.setCurveSize (4, nx);
1340       rPlotFile.addColumn (0, pX);
1341       rPlotFile.addColumn (1, pYReal);
1342       rPlotFile.addColumn (2, pYImag);
1343       rPlotFile.addColumn (3, pYMag);
1344       for (int iL = 0; iL < rIF.nLabels(); iL++)
1345         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1346       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1347       *theApp->getLog() << os.str().c_str() << "\n";
1348       rPlotFile.addDescription (os.str().c_str());
1349     }
1350     delete pX;
1351     delete pYReal;
1352     delete pYImag;
1353     delete pYMag;
1354     delete [] pcIn;
1355     
1356     if (theApp->getSetModifyNewDocs())
1357       pPlotDoc->Modify(true);
1358     pPlotDoc->UpdateAllViews();
1359   }
1360 }
1361
1362 void
1363 ImageFileView::OnPlotFFTCol (wxCommandEvent& event)
1364 {
1365   int xCursor, yCursor;
1366   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1367     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1368     return;
1369   }
1370   
1371   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1372   ImageFileArrayConst v = rIF.getArray();
1373   ImageFileArrayConst vImag = rIF.getImaginaryArray();
1374   int nx = rIF.nx();
1375   int ny = rIF.ny();
1376   
1377   if (v != NULL && xCursor < nx) {
1378     fftw_complex* pcIn = new fftw_complex [ny];
1379     double *pdTemp = new double [ny];
1380     
1381     int i;
1382     for (i = 0; i < ny; i++)
1383       pdTemp[i] = v[xCursor][i];
1384     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1385     for (i = 0; i < ny; i++) 
1386       pcIn[i].re = pdTemp[i];
1387     
1388     for (i = 0; i < ny; i++) {
1389       if (rIF.isComplex())
1390         pdTemp[i] = vImag[xCursor][i];
1391       else
1392         pdTemp[i] = 0;
1393     }
1394     Fourier::shuffleNaturalToFourierOrder (pdTemp, ny);
1395     for (i = 0; i < ny; i++)
1396       pcIn[i].im = pdTemp[i];
1397     
1398     fftw_plan plan = fftw_create_plan (ny, FFTW_BACKWARD, FFTW_IN_PLACE);
1399     fftw_one (plan, pcIn, NULL);
1400     fftw_destroy_plan (plan);
1401     
1402     double* pX = new double [ny];
1403     double* pYReal = new double [ny];
1404     double* pYImag = new double [ny];
1405     double* pYMag = new double [ny];
1406     for (i = 0; i < ny; i++) {
1407       pX[i] = i;
1408       pYReal[i] = pcIn[i].re;
1409       pYImag[i] = pcIn[i].im;
1410       pYMag[i] = ::sqrt (pcIn[i].re * pcIn[i].re + pcIn[i].im * pcIn[i].im);
1411     }
1412     
1413     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1414     if (! pPlotDoc) {
1415       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1416     } else {
1417       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1418       std::ostringstream os;
1419       os << "Column " << xCursor;
1420       std::string title("title ");
1421       title += os.str();
1422       rPlotFile.addEzsetCommand (title.c_str());
1423       rPlotFile.addEzsetCommand ("xlabel Column");
1424       rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1425       rPlotFile.addEzsetCommand ("lxfrac 0");
1426       rPlotFile.addEzsetCommand ("curve 1");
1427       rPlotFile.addEzsetCommand ("color 1");
1428       rPlotFile.addEzsetCommand ("dash 1");
1429       rPlotFile.addEzsetCommand ("curve 2");
1430       rPlotFile.addEzsetCommand ("color 4");
1431       rPlotFile.addEzsetCommand ("dash 3");
1432       rPlotFile.addEzsetCommand ("curve 3");
1433       rPlotFile.addEzsetCommand ("color 0");
1434       rPlotFile.addEzsetCommand ("solid");
1435       rPlotFile.addEzsetCommand ("box");
1436       rPlotFile.addEzsetCommand ("grid");
1437       rPlotFile.setCurveSize (4, ny);
1438       rPlotFile.addColumn (0, pX);
1439       rPlotFile.addColumn (1, pYReal);
1440       rPlotFile.addColumn (2, pYImag);
1441       rPlotFile.addColumn (3, pYMag);
1442       for (int iL = 0; iL < rIF.nLabels(); iL++)
1443         rPlotFile.addDescription (rIF.labelGet(iL).getLabelString().c_str());
1444       os << " FFT Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1445       *theApp->getLog() << os.str().c_str() << "\n";
1446       rPlotFile.addDescription (os.str().c_str());
1447     }
1448     delete pX;
1449     delete pYReal;
1450     delete pYImag;
1451     delete pYMag;
1452     delete pdTemp;
1453     delete [] pcIn;
1454     
1455     if (theApp->getSetModifyNewDocs())
1456       pPlotDoc->Modify(true);
1457     pPlotDoc->UpdateAllViews();
1458   }
1459 }
1460 #endif
1461
1462 void
1463 ImageFileView::OnCompareCol (wxCommandEvent& event)
1464 {
1465   int xCursor, yCursor;
1466   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1467     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1468     return;
1469   }
1470   
1471   std::vector<ImageFileDocument*> vecIFDoc;
1472   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1473   if (vecIFDoc.size() == 0) {
1474     wxMessageBox ("No compatible images for Column Comparison", "Error");
1475     return;
1476   }
1477   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1478   
1479   if (dialogGetCompare.ShowModal() == wxID_OK) {
1480     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1481     const ImageFile& rIF = GetDocument()->getImageFile();
1482     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1483     
1484     ImageFileArrayConst v1 = rIF.getArray();
1485     ImageFileArrayConst v2 = rCompareIF.getArray();
1486     int nx = rIF.nx();
1487     int ny = rIF.ny();
1488     
1489     if (v1 != NULL && xCursor < nx) {
1490       double* pX = new double [ny];
1491       double* pY1 = new double [ny];
1492       double* pY2 = new double [ny];
1493       for (int i = 0; i < ny; i++) {
1494         pX[i] = i;
1495         pY1[i] = v1[xCursor][i];
1496         pY2[i] = v2[xCursor][i];
1497       }
1498       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1499       if (! pPlotDoc) {
1500         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1501       } else {
1502         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1503         std::ostringstream os;
1504         os << "Column " << xCursor << " Comparison";
1505         std::string title("title ");
1506         title += os.str();
1507         rPlotFile.addEzsetCommand (title.c_str());
1508         rPlotFile.addEzsetCommand ("xlabel Row");
1509         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1510         rPlotFile.addEzsetCommand ("lxfrac 0");
1511         rPlotFile.addEzsetCommand ("curve 1");
1512         rPlotFile.addEzsetCommand ("color 2");
1513         rPlotFile.addEzsetCommand ("curve 2");
1514         rPlotFile.addEzsetCommand ("color 4");
1515         rPlotFile.addEzsetCommand ("dash 5");
1516         rPlotFile.addEzsetCommand ("box");
1517         rPlotFile.addEzsetCommand ("grid");
1518         rPlotFile.setCurveSize (3, ny);
1519         rPlotFile.addColumn (0, pX);
1520         rPlotFile.addColumn (1, pY1);
1521         rPlotFile.addColumn (2, pY2);
1522         
1523         unsigned int iL;
1524         for (iL = 0; iL < rIF.nLabels(); iL++) {
1525           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1526           s += ": ";
1527           s += rIF.labelGet(iL).getLabelString();
1528           rPlotFile.addDescription (s.c_str());
1529         }
1530         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1531           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1532           s += ": ";
1533           s += rCompareIF.labelGet(iL).getLabelString();
1534           rPlotFile.addDescription (s.c_str());
1535         }
1536         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1537           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1538         *theApp->getLog() << os.str().c_str() << "\n";
1539         rPlotFile.addDescription (os.str().c_str());
1540       }
1541       delete pX;
1542       delete pY1;
1543       delete pY2;
1544       if (theApp->getSetModifyNewDocs())
1545         pPlotDoc->Modify(true);
1546       pPlotDoc->UpdateAllViews();
1547     }
1548   }
1549 }
1550
1551 void
1552 ImageFileView::OnCompareRow (wxCommandEvent& event)
1553 {
1554   int xCursor, yCursor;
1555   if (! m_pCanvas->GetCurrentCursor (xCursor, yCursor)) {
1556     wxMessageBox ("No column selected. Please use left mouse button on image to select column","Error");
1557     return;
1558   }
1559   
1560   std::vector<ImageFileDocument*> vecIFDoc;
1561   theApp->getCompatibleImages (GetDocument(), vecIFDoc);
1562   
1563   if (vecIFDoc.size() == 0) {
1564     wxMessageBox ("No compatible images for Row Comparison", "Error");
1565     return;
1566   }
1567   
1568   DialogGetComparisonImage dialogGetCompare (getFrameForChild(), "Get Comparison Image", vecIFDoc, false);
1569   
1570   if (dialogGetCompare.ShowModal() == wxID_OK) {
1571     ImageFileDocument* pCompareDoc = dialogGetCompare.getImageFileDocument();
1572     const ImageFile& rIF = GetDocument()->getImageFile();
1573     const ImageFile& rCompareIF = pCompareDoc->getImageFile();
1574     
1575     ImageFileArrayConst v1 = rIF.getArray();
1576     ImageFileArrayConst v2 = rCompareIF.getArray();
1577     int nx = rIF.nx();
1578     int ny = rIF.ny();
1579     
1580     if (v1 != NULL && yCursor < ny) {
1581       double* pX = new double [nx];
1582       double* pY1 = new double [nx];
1583       double* pY2 = new double [nx];
1584       for (int i = 0; i < nx; i++) {
1585         pX[i] = i;
1586         pY1[i] = v1[i][yCursor];
1587         pY2[i] = v2[i][yCursor];
1588       }
1589       PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1590       if (! pPlotDoc) {
1591         sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1592       } else {
1593         PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1594         std::ostringstream os;
1595         os << "Row " << yCursor << " Comparison";
1596         std::string title("title ");
1597         title += os.str();
1598         rPlotFile.addEzsetCommand (title.c_str());
1599         rPlotFile.addEzsetCommand ("xlabel Column");
1600         rPlotFile.addEzsetCommand ("ylabel Pixel Value");
1601         rPlotFile.addEzsetCommand ("lxfrac 0");
1602         rPlotFile.addEzsetCommand ("curve 1");
1603         rPlotFile.addEzsetCommand ("color 2");
1604         rPlotFile.addEzsetCommand ("curve 2");
1605         rPlotFile.addEzsetCommand ("color 4");
1606         rPlotFile.addEzsetCommand ("dash 5");
1607         rPlotFile.addEzsetCommand ("box");
1608         rPlotFile.addEzsetCommand ("grid");
1609         rPlotFile.setCurveSize (3, nx);
1610         rPlotFile.addColumn (0, pX);
1611         rPlotFile.addColumn (1, pY1);
1612         rPlotFile.addColumn (2, pY2);
1613         unsigned int iL;
1614         for (iL = 0; iL < rIF.nLabels(); iL++) {
1615           std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1616           s += ": ";
1617           s += rIF.labelGet(iL).getLabelString();
1618           rPlotFile.addDescription (s.c_str());
1619         }
1620         for (iL = 0; iL < rCompareIF.nLabels(); iL++) {
1621           std::string s = pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1622           s += ": ";
1623           s += rCompareIF.labelGet(iL).getLabelString();
1624           rPlotFile.addDescription (s.c_str());
1625         }
1626         os << " Between " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str() << " and "
1627           << pCompareDoc->GetFirstView()->GetFrame()->GetTitle().c_str();
1628         *theApp->getLog() << os.str().c_str() << "\n";
1629         rPlotFile.addDescription (os.str().c_str());
1630       }
1631       delete pX;
1632       delete pY1;
1633       delete pY2;
1634       if (theApp->getSetModifyNewDocs())
1635         pPlotDoc->Modify(true);
1636       pPlotDoc->UpdateAllViews();
1637     }
1638   }
1639 }
1640
1641 static int NUMBER_HISTOGRAM_BINS = 256;
1642
1643 void
1644 ImageFileView::OnPlotHistogram (wxCommandEvent& event)
1645
1646   const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
1647   ImageFileArrayConst v = rIF.getArray();
1648   int nx = rIF.nx();
1649   int ny = rIF.ny();
1650   
1651   if (v != NULL && nx > 0 && ny > 0) {
1652     PlotFileDocument* pPlotDoc = theApp->newPlotDoc();
1653     if (! pPlotDoc) {
1654       sys_error (ERR_SEVERE, "Internal error: unable to create Plot file");
1655       return;
1656     }
1657     
1658     double* pX = new double [NUMBER_HISTOGRAM_BINS];
1659     double* pY = new double [NUMBER_HISTOGRAM_BINS];
1660     double dMin, dMax;
1661     rIF.getMinMax (dMin, dMax);
1662     double dBinWidth = (dMax - dMin) / NUMBER_HISTOGRAM_BINS;
1663     
1664     for (int i = 0; i < NUMBER_HISTOGRAM_BINS; i++) {
1665       pX[i] = dMin + (i + 0.5) * dBinWidth;
1666       pY[i] = 0;
1667     }
1668     for (int ix = 0; ix < nx; ix++)
1669       for (int iy = 0; iy < ny; iy++) {
1670         int iBin = nearest<int> ((v[ix][iy] - dMin) / dBinWidth);
1671         if (iBin >= 0 && iBin < NUMBER_HISTOGRAM_BINS)
1672           pY[iBin] += 1;
1673       }
1674       
1675       PlotFile& rPlotFile = pPlotDoc->getPlotFile();
1676       std::ostringstream os;
1677       os << "Histogram";
1678       std::string title("title ");
1679       title += os.str();
1680       rPlotFile.addEzsetCommand (title.c_str());
1681       rPlotFile.addEzsetCommand ("xlabel Pixel Value");
1682       rPlotFile.addEzsetCommand ("ylabel Count");
1683       rPlotFile.addEzsetCommand ("box");
1684       rPlotFile.addEzsetCommand ("grid");
1685       rPlotFile.setCurveSize (2, NUMBER_HISTOGRAM_BINS);
1686       rPlotFile.addColumn (0, pX);
1687       rPlotFile.addColumn (1, pY);
1688       for (unsigned int iL = 0; iL < rIF.nLabels(); iL++) {
1689         std::string s = GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1690         s += ": ";
1691         s += rIF.labelGet(iL).getLabelString();
1692         rPlotFile.addDescription (s.c_str());
1693       }
1694       os << " Plot of " << GetDocument()->GetFirstView()->GetFrame()->GetTitle().c_str();
1695       *theApp->getLog() << os.str().c_str() << "\n";
1696       rPlotFile.addDescription (os.str().c_str());
1697       delete pX;
1698       delete pY;
1699       if (theApp->getSetModifyNewDocs())
1700         pPlotDoc->Modify(true);
1701       pPlotDoc->UpdateAllViews();
1702   }
1703 }
1704
1705
1706 // PhantomCanvas
1707
1708 PhantomCanvas::PhantomCanvas (PhantomFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
1709 : wxScrolledWindow(frame, -1, pos, size, style)
1710 {
1711   m_pView = v;
1712 }
1713
1714 PhantomCanvas::~PhantomCanvas ()
1715 {
1716     m_pView = NULL;
1717 }
1718
1719 void 
1720 PhantomCanvas::OnDraw (wxDC& dc)
1721 {
1722   if (m_pView)
1723     m_pView->OnDraw(& dc);
1724 }
1725
1726
1727 // PhantomFileView
1728
1729 IMPLEMENT_DYNAMIC_CLASS(PhantomFileView, wxView)
1730
1731 BEGIN_EVENT_TABLE(PhantomFileView, wxView)
1732 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomFileView::OnProperties)
1733 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomFileView::OnRasterize)
1734 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomFileView::OnProjections)
1735 END_EVENT_TABLE()
1736
1737 PhantomFileView::PhantomFileView() 
1738 : wxView(), m_pCanvas(NULL), m_pFrame(NULL), m_pFileMenu(0)
1739 {
1740   m_iDefaultNDet = 367;
1741   m_iDefaultNView = 320;
1742   m_iDefaultNSample = 1;
1743   m_dDefaultRotation = 1;
1744   m_dDefaultFocalLength = 2;
1745   m_dDefaultFieldOfView = 1;
1746   m_iDefaultGeometry = Scanner::GEOMETRY_PARALLEL;
1747   m_iDefaultTrace = Trace::TRACE_NONE;
1748   
1749   m_iDefaultRasterNX = 256;
1750   m_iDefaultRasterNY = 256;
1751   m_iDefaultRasterNSamples = 2;
1752 }
1753
1754 PhantomFileView::~PhantomFileView()
1755 {
1756     theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);
1757 }
1758
1759 void
1760 PhantomFileView::OnProperties (wxCommandEvent& event)
1761 {
1762   const int idPhantom = GetDocument()->getPhantomID();
1763   const wxString& namePhantom = GetDocument()->getPhantomName();
1764   std::ostringstream os;
1765   os << "Phantom " << namePhantom.c_str() << " (" << idPhantom << ")" << "\n";
1766   const Phantom& rPhantom = GetDocument()->getPhantom();
1767   rPhantom.printDefinitions (os);
1768 #if DEBUG
1769   rPhantom.print (os);
1770 #endif
1771   *theApp->getLog() << os.str().c_str() << "\n";
1772   wxMessageBox (os.str().c_str(), "Phantom Properties");
1773 }
1774
1775
1776 void
1777 PhantomFileView::OnProjections (wxCommandEvent& event)
1778 {
1779   DialogGetProjectionParameters dialogProjection (getFrameForChild(), m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultFieldOfView, m_iDefaultGeometry, m_iDefaultTrace);
1780   int retVal = dialogProjection.ShowModal();
1781   if (retVal == wxID_OK) {
1782     m_iDefaultNDet = dialogProjection.getNDet();
1783     m_iDefaultNView = dialogProjection.getNView();
1784     m_iDefaultNSample = dialogProjection.getNSamples();
1785     m_iDefaultTrace = dialogProjection.getTrace();
1786     m_dDefaultRotation = dialogProjection.getRotAngle();
1787     m_dDefaultFocalLength = dialogProjection.getFocalLengthRatio();
1788     m_dDefaultFieldOfView = dialogProjection.getFieldOfViewRatio();
1789     wxString sGeometry = dialogProjection.getGeometry();
1790     m_iDefaultGeometry = Scanner::convertGeometryNameToID (sGeometry.c_str());
1791     
1792     if (m_iDefaultNDet > 0 && m_iDefaultNView > 0 && sGeometry != "") {
1793       const Phantom& rPhantom = GetDocument()->getPhantom();
1794       ProjectionFileDocument* pProjectionDoc = theApp->newProjectionDoc();
1795       if (! pProjectionDoc) {
1796         sys_error (ERR_SEVERE, "Unable to create projection document");
1797         return;
1798       }
1799       Projections& rProj = pProjectionDoc->getProjections();
1800       Scanner theScanner (rPhantom, sGeometry.c_str(), m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultFieldOfView);
1801       if (theScanner.fail()) {
1802         *theApp->getLog() << "Failed making scanner: " << theScanner.failMessage().c_str() << "\n";
1803         return;
1804       }
1805       rProj.initFromScanner (theScanner);
1806       m_dDefaultRotation /= PI;  // convert back to PI units
1807       
1808       Timer timer;
1809       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
1810         ProjectionsDialog dialogProjections (theScanner, rProj, rPhantom, m_iDefaultTrace, dynamic_cast<wxWindow*>(getFrameForChild()));
1811         for (int iView = 0; iView < rProj.nView(); iView++) {
1812           ::wxYield();
1813           if (dialogProjections.isCancelled() || ! dialogProjections.projectView (iView)) {
1814             pProjectionDoc->Close();
1815           //pProjectionDoc->DeleteAllViews();
1816             return;
1817           }
1818           ::wxYield();
1819           while (dialogProjections.isPaused()) {
1820             ::wxYield();
1821             ::wxUsleep(50);
1822           }
1823         }
1824       } else {
1825         wxProgressDialog dlgProgress (wxString("Projection"), wxString("Projection Progress"), rProj.nView() + 1, getFrameForChild(), wxPD_CAN_ABORT);
1826         for (int i = 0; i < rProj.nView(); i++) {
1827           theScanner.collectProjections (rProj, rPhantom, i, 1, true, m_iDefaultTrace);
1828           if (! dlgProgress.Update (i+1)) {
1829             pProjectionDoc->Close();
1830             //pProjectionDoc->DeleteAllViews();
1831             return;
1832           }
1833         }
1834       }
1835       
1836       std::ostringstream os;
1837       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();
1838       rProj.setCalcTime (timer.timerEnd());
1839       rProj.setRemark (os.str());
1840       *theApp->getLog() << os.str().c_str() << "\n";
1841       
1842       m_pFrame->Lower();
1843       ::wxYield();
1844       ProjectionFileView* projView = dynamic_cast<ProjectionFileView*>(pProjectionDoc->GetFirstView());
1845       if (projView) {
1846         projView->getFrame()->SetFocus();
1847         projView->OnUpdate (projView, NULL);
1848       }
1849       if (wxView* pView = pProjectionDoc->GetFirstView()) {
1850         if (wxFrame* pFrame = pView->GetFrame()) {
1851           pFrame->SetFocus();
1852           pFrame->Raise();
1853         }
1854         theApp->getDocManager()->ActivateView (pView, true, false);
1855       }
1856       ::wxYield();
1857       if (theApp->getSetModifyNewDocs())
1858         pProjectionDoc->Modify(true);
1859       pProjectionDoc->getView()->getFrame()->Show(true);
1860       pProjectionDoc->UpdateAllViews(this);
1861     }
1862   }
1863 }
1864
1865
1866 void
1867 PhantomFileView::OnRasterize (wxCommandEvent& event)
1868 {
1869   DialogGetRasterParameters dialogRaster (getFrameForChild(), m_iDefaultRasterNX, m_iDefaultRasterNY, m_iDefaultRasterNSamples);
1870   int retVal = dialogRaster.ShowModal();
1871   if (retVal == wxID_OK) {
1872     m_iDefaultRasterNX = dialogRaster.getXSize();
1873     m_iDefaultRasterNY  = dialogRaster.getYSize();
1874     m_iDefaultRasterNSamples = dialogRaster.getNSamples();
1875     if (m_iDefaultRasterNSamples < 1)
1876       m_iDefaultRasterNSamples = 1;
1877     if (m_iDefaultRasterNX > 0 && m_iDefaultRasterNY > 0) {
1878       const Phantom& rPhantom = GetDocument()->getPhantom();
1879       ImageFileDocument* pRasterDoc = theApp->newImageDoc();
1880       if (! pRasterDoc) {
1881         sys_error (ERR_SEVERE, "Unable to create image file");
1882         return;
1883       }
1884       ImageFile& imageFile = pRasterDoc->getImageFile();
1885       
1886       imageFile.setArraySize (m_iDefaultRasterNX, m_iDefaultRasterNX);
1887       wxProgressDialog dlgProgress (wxString("Rasterize"), wxString("Rasterization Progress"), imageFile.nx() + 1, getFrameForChild(), wxPD_CAN_ABORT);
1888       Timer timer;
1889       for (unsigned int i = 0; i < imageFile.nx(); i++) {
1890         rPhantom.convertToImagefile (imageFile, m_iDefaultRasterNSamples, Trace::TRACE_NONE, i, 1, true);
1891         if (! dlgProgress.Update(i+1)) {
1892           pRasterDoc->Close();
1893           //pRasterDoc->DeleteAllViews();
1894           return;
1895         }
1896       }
1897       if (theApp->getSetModifyNewDocs())
1898         pRasterDoc->Modify(true);
1899       pRasterDoc->UpdateAllViews(this);
1900       pRasterDoc->getView()->getFrame()->Show(true);
1901       std::ostringstream os;
1902       os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << m_iDefaultRasterNX << ", YSize=" 
1903         << m_iDefaultRasterNY << ", nSamples=" << m_iDefaultRasterNSamples;
1904       *theApp->getLog() << os.str().c_str() << "\n";
1905       imageFile.labelAdd (os.str().c_str(), timer.timerEnd());
1906       ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pRasterDoc->GetFirstView());
1907       if (rasterView) {
1908         rasterView->getFrame()->SetFocus();
1909         rasterView->OnUpdate (rasterView, NULL);
1910       }
1911       
1912     }
1913   }
1914 }
1915
1916
1917 PhantomCanvas* 
1918 PhantomFileView::CreateCanvas (wxView *view, wxFrame *parent)
1919 {
1920   PhantomCanvas* pCanvas;
1921   int width, height;
1922   parent->GetClientSize(&width, &height);
1923   
1924   pCanvas = new PhantomCanvas (dynamic_cast<PhantomFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
1925   
1926   pCanvas->SetBackgroundColour(*wxWHITE);
1927   pCanvas->Clear();
1928   
1929   return pCanvas;
1930 }
1931
1932 #if CTSIM_MDI
1933 wxDocMDIChildFrame*
1934 #else
1935 wxDocChildFrame*
1936 #endif
1937 PhantomFileView::CreateChildFrame(wxDocument *doc, wxView *view)
1938 {
1939 #if CTSIM_MDI
1940   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
1941 #else
1942   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
1943 #endif
1944   theApp->setIconForFrame (subframe);
1945   
1946   wxMenu *m_pFileMenu = new wxMenu;
1947   
1948   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
1949   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
1950   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
1951   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
1952   m_pFileMenu->Append(wxID_CLOSE, "&Close");
1953   
1954   m_pFileMenu->AppendSeparator();
1955   m_pFileMenu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties");
1956   
1957   m_pFileMenu->AppendSeparator();
1958   m_pFileMenu->Append(wxID_PRINT, "&Print...");
1959   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
1960   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
1961 #ifdef CTSIM_MDI
1962   m_pFileMenu->AppendSeparator();
1963   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
1964 #endif
1965   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
1966   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
1967   
1968   wxMenu *process_menu = new wxMenu;
1969   process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...\tCtrl-R");
1970   process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...\tCtrl-J");
1971   
1972   wxMenu *help_menu = new wxMenu;
1973   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
1974   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
1975   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
1976   
1977   wxMenuBar *menu_bar = new wxMenuBar;
1978   
1979   menu_bar->Append(m_pFileMenu, "&File");
1980   menu_bar->Append(process_menu, "&Process");
1981   menu_bar->Append(help_menu, "&Help");
1982   
1983   subframe->SetMenuBar(menu_bar);
1984   subframe->Centre(wxBOTH);
1985   
1986   wxAcceleratorEntry accelEntries[8];
1987   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
1988   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
1989   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
1990   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
1991   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
1992   accelEntries[5].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
1993   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('J'), PHMMENU_PROCESS_PROJECTIONS);
1994   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('R'), PHMMENU_PROCESS_RASTERIZE);
1995   wxAcceleratorTable accelTable (8, accelEntries);
1996   subframe->SetAcceleratorTable (accelTable);
1997   
1998   return subframe;
1999 }
2000
2001
2002 bool 
2003 PhantomFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2004 {
2005   m_pFrame = CreateChildFrame(doc, this);
2006   SetFrame(m_pFrame);
2007   
2008   int width, height;
2009   m_pFrame->GetClientSize(&width, &height);
2010   m_pFrame->SetTitle("PhantomFileView");
2011   m_pCanvas = CreateCanvas (this, m_pFrame);
2012   
2013 #ifdef __X__
2014   int x, y;  // X requires a forced resize
2015   m_pFrame->GetSize(&x, &y);
2016   m_pFrame->SetSize(-1, -1, x, y);
2017 #endif
2018   
2019   m_pFrame->Show(true);
2020   Activate(true);
2021   
2022   return true;
2023 }
2024
2025 void 
2026 PhantomFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2027 {
2028   if (m_pCanvas)
2029     m_pCanvas->Refresh();
2030 }
2031
2032 bool 
2033 PhantomFileView::OnClose (bool deleteWindow)
2034 {
2035   if (! GetDocument() || ! GetDocument()->Close())
2036     return false;
2037   
2038   if (m_pCanvas) {
2039     m_pCanvas->Clear();
2040     m_pCanvas->setView(NULL);
2041         m_pCanvas->Show(false);
2042     m_pCanvas = NULL;
2043   }
2044   wxString s(wxTheApp->GetAppName());
2045   if (m_pFrame)
2046     m_pFrame->SetTitle(s);
2047   SetFrame(NULL);
2048   
2049   Activate(false);
2050   
2051   if (deleteWindow) {
2052           m_pFrame->Show(false);
2053           m_pFrame->Destroy();
2054 //    delete m_pFrame;
2055     m_pFrame = NULL;
2056   }
2057   
2058   return true;
2059 }
2060
2061 void
2062 PhantomFileView::OnDraw (wxDC* dc)
2063 {
2064   int xsize, ysize;
2065   m_pCanvas->GetClientSize (&xsize, &ysize);
2066   SGPDriver driver (dc, xsize, ysize);
2067   SGP sgp (driver);
2068   const Phantom& rPhantom = GetDocument()->getPhantom();
2069   sgp.setColor (C_RED);
2070   rPhantom.show (sgp);
2071 }
2072
2073 // ProjectionCanvas
2074
2075 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2076 : wxScrolledWindow(frame, -1, pos, size, style)
2077 {
2078   m_pView = v;
2079 }
2080
2081 ProjectionFileCanvas::~ProjectionFileCanvas ()
2082 {
2083         m_pView = NULL;
2084 }
2085
2086 void 
2087 ProjectionFileCanvas::OnDraw(wxDC& dc)
2088 {
2089   if (m_pView)
2090     m_pView->OnDraw(& dc);
2091 }
2092
2093 wxSize
2094 ProjectionFileCanvas::GetBestSize () const
2095 {
2096   wxSize best (0, 0);
2097   if (m_pView) {
2098     Projections& rProj = m_pView->GetDocument()->getProjections();
2099     best.Set (rProj.nDet(), rProj.nView());
2100   }
2101   
2102   return best;
2103 }
2104
2105
2106 // ProjectionFileView
2107
2108 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
2109
2110 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
2111 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
2112 EVT_MENU(PJMENU_RECONSTRUCT_FBP, ProjectionFileView::OnReconstructFBP)
2113 EVT_MENU(PJMENU_CONVERT_POLAR, ProjectionFileView::OnConvertPolar)
2114 EVT_MENU(PJMENU_CONVERT_FFT_POLAR, ProjectionFileView::OnConvertFFTPolar)
2115 END_EVENT_TABLE()
2116
2117 ProjectionFileView::ProjectionFileView() 
2118 : wxView(), m_pCanvas(NULL), m_pFrame(NULL), m_pFileMenu(0)
2119 {
2120   m_iDefaultNX = 256;
2121   m_iDefaultNY = 256;
2122   m_iDefaultFilter = SignalFilter::FILTER_ABS_BANDLIMIT;
2123   m_dDefaultFilterParam = 1.;
2124 #if HAVE_FFTW
2125   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_RFFTW;
2126   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_INVERSE_FOURIER;
2127 #else
2128   m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_CONVOLUTION;
2129   m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
2130 #endif
2131   m_iDefaultZeropad = 1;
2132   m_iDefaultBackprojector = Backprojector::BPROJ_IDIFF3;
2133   m_iDefaultInterpolation = Backprojector::INTERP_LINEAR;
2134   m_iDefaultInterpParam = 1;
2135   m_iDefaultTrace = Trace::TRACE_NONE;
2136   
2137   m_iDefaultPolarNX = 256;
2138   m_iDefaultPolarNY = 256;
2139   m_iDefaultPolarInterpolation = Projections::POLAR_INTERP_BILINEAR;
2140   m_iDefaultPolarZeropad = 1;
2141 }
2142
2143 ProjectionFileView::~ProjectionFileView()
2144 {
2145         theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);
2146 }
2147
2148 void
2149 ProjectionFileView::OnProperties (wxCommandEvent& event)
2150 {
2151   const Projections& rProj = GetDocument()->getProjections();
2152   std::ostringstream os;
2153   rProj.printScanInfo(os);
2154   *theApp->getLog() << os.str().c_str();
2155   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Projection File Properties", wxOK | wxICON_INFORMATION);
2156   dialogMsg.ShowModal();
2157 }
2158
2159
2160 void
2161 ProjectionFileView::OnConvertPolar (wxCommandEvent& event)
2162 {
2163   Projections& rProj = GetDocument()->getProjections();
2164   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2165     m_iDefaultPolarInterpolation, -1);
2166   if (dialogPolar.ShowModal() == wxID_OK) {
2167     wxString strInterpolation (dialogPolar.getInterpolationName());
2168     m_iDefaultPolarNX = dialogPolar.getXSize();
2169     m_iDefaultPolarNY = dialogPolar.getYSize();
2170     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2171     ImageFile& rIF = pPolarDoc->getImageFile();
2172     if (! pPolarDoc) {
2173       sys_error (ERR_SEVERE, "Unable to create image file");
2174       return;
2175     }
2176     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2177     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2178     rProj.convertPolar (rIF, m_iDefaultPolarInterpolation);
2179     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2180     std::ostringstream os;
2181     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to polar image: xSize=" 
2182       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2183       << strInterpolation.c_str();
2184     *theApp->getLog() << os.str().c_str() << "\n";
2185     rIF.labelAdd (os.str().c_str());
2186     if (theApp->getSetModifyNewDocs())
2187       pPolarDoc->Modify(true);
2188     pPolarDoc->UpdateAllViews();
2189     pPolarDoc->getView()->OnUpdate (this, NULL);
2190     pPolarDoc->getView()->getFrame()->Show(true);
2191   }
2192 }
2193
2194 void
2195 ProjectionFileView::OnConvertFFTPolar (wxCommandEvent& event)
2196 {
2197   Projections& rProj = GetDocument()->getProjections();
2198   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert to FFT Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
2199     m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2200   if (dialogPolar.ShowModal() == wxID_OK) {
2201     wxString strInterpolation (dialogPolar.getInterpolationName());
2202     m_iDefaultPolarNX = dialogPolar.getXSize();
2203     m_iDefaultPolarNY = dialogPolar.getYSize();
2204     m_iDefaultPolarZeropad = dialogPolar.getZeropad();
2205     ImageFileDocument* pPolarDoc = theApp->newImageDoc();
2206     ImageFile& rIF = pPolarDoc->getImageFile();
2207     if (! pPolarDoc) {
2208       sys_error (ERR_SEVERE, "Unable to create image file");
2209       return;
2210     }
2211     rIF.setArraySize (m_iDefaultPolarNX, m_iDefaultPolarNY);
2212     m_iDefaultPolarInterpolation = Projections::convertInterpNameToID (strInterpolation.c_str());
2213     rProj.convertFFTPolar (rIF, m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
2214     rIF.labelAdd (rProj.getLabel().getLabelString().c_str(), rProj.calcTime());
2215     std::ostringstream os;
2216     os << "Convert projection file " << GetFrame()->GetTitle().c_str() << " to FFT polar image: xSize=" 
2217       << m_iDefaultPolarNX << ", ySize=" << m_iDefaultPolarNY << ", interpolation=" 
2218       << strInterpolation.c_str() << ", zeropad=" << m_iDefaultPolarZeropad;
2219     *theApp->getLog() << os.str().c_str() << "\n";
2220     rIF.labelAdd (os.str().c_str());
2221     if (theApp->getSetModifyNewDocs())
2222       pPolarDoc->Modify(true);
2223     pPolarDoc->UpdateAllViews();
2224     pPolarDoc->getView()->OnUpdate (this, NULL);
2225     pPolarDoc->getView()->getFrame()->Show(true);
2226 }
2227 }
2228
2229 void
2230 ProjectionFileView::OnReconstructFourier (wxCommandEvent& event)
2231 {
2232   wxMessageBox ("Fourier Reconstruction is not yet supported", "Unimplemented function");
2233 }
2234
2235 void
2236 ProjectionFileView::OnReconstructFBP (wxCommandEvent& event)
2237 {
2238   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);
2239   
2240   int retVal = dialogReconstruction.ShowModal();
2241   if (retVal == wxID_OK) {
2242     m_iDefaultNX = dialogReconstruction.getXSize();
2243     m_iDefaultNY = dialogReconstruction.getYSize();
2244     wxString optFilterName = dialogReconstruction.getFilterName();
2245     m_iDefaultFilter = SignalFilter::convertFilterNameToID (optFilterName.c_str());
2246     m_dDefaultFilterParam = dialogReconstruction.getFilterParam();
2247     wxString optFilterMethodName = dialogReconstruction.getFilterMethodName();
2248     m_iDefaultFilterMethod = ProcessSignal::convertFilterMethodNameToID(optFilterMethodName.c_str());
2249     m_iDefaultZeropad = dialogReconstruction.getZeropad();
2250     wxString optFilterGenerationName = dialogReconstruction.getFilterGenerationName();
2251     m_iDefaultFilterGeneration = ProcessSignal::convertFilterGenerationNameToID (optFilterGenerationName.c_str());
2252     wxString optInterpName = dialogReconstruction.getInterpName();
2253     m_iDefaultInterpolation = Backprojector::convertInterpNameToID (optInterpName.c_str());
2254     m_iDefaultInterpParam = dialogReconstruction.getInterpParam();
2255     wxString optBackprojectName = dialogReconstruction.getBackprojectName();
2256     m_iDefaultBackprojector = Backprojector::convertBackprojectNameToID (optBackprojectName.c_str());
2257     m_iDefaultTrace = dialogReconstruction.getTrace();
2258     if (m_iDefaultNX > 0 && m_iDefaultNY > 0) {
2259       ImageFileDocument* pReconDoc = theApp->newImageDoc();
2260       if (! pReconDoc) {
2261         sys_error (ERR_SEVERE, "Unable to create image file");
2262         return;
2263       }
2264       ImageFile& imageFile = pReconDoc->getImageFile();
2265       const Projections& rProj = GetDocument()->getProjections();
2266       imageFile.setArraySize (m_iDefaultNX, m_iDefaultNY);
2267       
2268       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);
2269       
2270       Timer timerRecon;
2271       if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
2272         ReconstructDialog* pDlgReconstruct = new ReconstructDialog (*pReconstruct, rProj, imageFile, m_iDefaultTrace, getFrameForChild());
2273         for (int iView = 0; iView < rProj.nView(); iView++) {
2274           ::wxYield();
2275           ::wxYield();
2276           if (pDlgReconstruct->isCancelled() || ! pDlgReconstruct->reconstructView (iView)) {
2277             delete pDlgReconstruct;
2278             delete pReconstruct;
2279             pReconDoc->Close();
2280             //pReconDoc->DeleteAllViews();
2281             return;
2282           }
2283           ::wxYield();
2284           ::wxYield();
2285           while (pDlgReconstruct->isPaused()) {
2286             ::wxYield();
2287             ::wxUsleep(50);
2288           }
2289         }
2290         delete pDlgReconstruct;
2291       } else {
2292         wxProgressDialog dlgProgress (wxString("Reconstruction"), wxString("Reconstruction Progress"), rProj.nView() + 1, getFrameForChild(), wxPD_CAN_ABORT);
2293         for (int i = 0; i < rProj.nView(); i++) {
2294           pReconstruct->reconstructView (i, 1);
2295           if (! dlgProgress.Update(i + 1)) {
2296             delete pReconstruct;
2297             pReconDoc->Close();
2298             //pReconDoc->DeleteAllViews();
2299             return;
2300           }
2301         }
2302       }
2303       delete pReconstruct;
2304       if (theApp->getSetModifyNewDocs())
2305         pReconDoc->Modify(true);
2306       pReconDoc->UpdateAllViews(this);
2307       pReconDoc->getView()->getFrame()->Show(true);
2308       ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pReconDoc->GetFirstView());
2309       if (rasterView) {
2310         rasterView->getFrame()->SetFocus();
2311         rasterView->OnUpdate (rasterView, NULL);
2312       }
2313       std::ostringstream os;
2314       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();
2315       *theApp->getLog() << os.str().c_str() << "\n";
2316       imageFile.labelAdd (rProj.getLabel());
2317       imageFile.labelAdd (os.str().c_str(), timerRecon.timerEnd());
2318     }
2319   }
2320 }
2321
2322
2323 ProjectionFileCanvas* 
2324 ProjectionFileView::CreateCanvas (wxView *view, wxFrame *parent)
2325 {
2326   ProjectionFileCanvas* pCanvas;
2327   int width, height;
2328   parent->GetClientSize(&width, &height);
2329   
2330   pCanvas = new ProjectionFileCanvas (dynamic_cast<ProjectionFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
2331   
2332   pCanvas->SetScrollbars(20, 20, 50, 50);
2333   pCanvas->SetBackgroundColour(*wxWHITE);
2334   pCanvas->Clear();
2335   
2336   return pCanvas;
2337 }
2338
2339 #if CTSIM_MDI
2340 wxDocMDIChildFrame*
2341 #else
2342 wxDocChildFrame*
2343 #endif
2344 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2345 {
2346 #ifdef CTSIM_MDI
2347   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2348 #else
2349   wxDocChildFrame *subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
2350 #endif
2351   theApp->setIconForFrame (subframe);
2352   
2353   wxMenu *m_pFileMenu = new wxMenu;
2354   
2355   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2356   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2357   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2358   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2359   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2360   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2361   
2362   m_pFileMenu->AppendSeparator();
2363   m_pFileMenu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
2364   
2365   m_pFileMenu->AppendSeparator();
2366   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2367   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2368   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2369 #ifdef CTSIM_MDI
2370   m_pFileMenu->AppendSeparator();
2371   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2372 #endif
2373   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2374   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
2375   
2376   wxMenu *convert_menu = new wxMenu;
2377   convert_menu->Append (PJMENU_CONVERT_POLAR, "&Polar Image...\tCtrl-L");
2378   convert_menu->Append (PJMENU_CONVERT_FFT_POLAR, "&FFT->Polar Image...\tCtrl-I");
2379   
2380   wxMenu *reconstruct_menu = new wxMenu;
2381   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FBP, "&Filtered Backprojection...\tCtrl-R");
2382   reconstruct_menu->Append (PJMENU_RECONSTRUCT_FOURIER, "&Fourier...\tCtrl-E");
2383   
2384   wxMenu *help_menu = new wxMenu;
2385   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2386   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2387   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2388   
2389   wxMenuBar *menu_bar = new wxMenuBar;
2390   
2391   menu_bar->Append (m_pFileMenu, "&File");
2392   menu_bar->Append (convert_menu, "&Convert");
2393   menu_bar->Append (reconstruct_menu, "&Reconstruct");
2394   menu_bar->Append (help_menu, "&Help");
2395   
2396   subframe->SetMenuBar(menu_bar);  
2397   subframe->Centre(wxBOTH);
2398   
2399   wxAcceleratorEntry accelEntries[11];
2400   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2401   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2402   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2403   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2404   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
2405   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
2406   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2407   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('L'), PJMENU_CONVERT_POLAR);
2408   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('I'), PJMENU_CONVERT_FFT_POLAR);
2409   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('R'), PJMENU_RECONSTRUCT_FBP);
2410   accelEntries[10].Set (wxACCEL_CTRL, static_cast<int>('E'), PJMENU_RECONSTRUCT_FOURIER);
2411   wxAcceleratorTable accelTable (11, accelEntries);
2412   subframe->SetAcceleratorTable (accelTable);
2413   
2414   return subframe;
2415 }
2416
2417
2418 bool 
2419 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2420 {
2421   m_pFrame = CreateChildFrame(doc, this);
2422   SetFrame(m_pFrame);
2423   
2424   int width, height;
2425   m_pFrame->GetClientSize(&width, &height);
2426   m_pFrame->SetTitle("ProjectionFileView");
2427   m_pCanvas = CreateCanvas(this, m_pFrame);
2428   
2429 #ifdef __X__
2430   int x, y;  // X requires a forced resize
2431   m_pFrame->GetSize(&x, &y);
2432   m_pFrame->SetSize(-1, -1, x, y);
2433 #endif
2434   
2435   m_pFrame->Show(true);
2436   Activate(true);
2437   
2438   return true;
2439 }
2440
2441 void 
2442 ProjectionFileView::OnDraw (wxDC* dc)
2443 {
2444   wxSize clientSize = m_pFrame->GetClientSize();
2445   wxSize bestSize = m_pCanvas->GetBestSize();
2446   
2447   if (clientSize.x > bestSize.x || clientSize.y > bestSize.y)
2448     m_pFrame->SetClientSize (bestSize);
2449   
2450   if (m_bitmap.Ok())
2451     dc->DrawBitmap (m_bitmap, 0, 0, false);
2452 }
2453
2454
2455 void 
2456 ProjectionFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2457 {
2458   const Projections& rProj = GetDocument()->getProjections();
2459   const int nDet = rProj.nDet();
2460   const int nView = rProj.nView();
2461   if (nDet != 0 && nView != 0) {
2462     const DetectorArray& detarray = rProj.getDetectorArray(0);
2463     const DetectorValue* detval = detarray.detValues();
2464     double min = detval[0];
2465     double max = detval[0];
2466     for (int iy = 0; iy < nView; iy++) {
2467       const DetectorArray& detarray = rProj.getDetectorArray(iy);
2468       const DetectorValue* detval = detarray.detValues();
2469       for (int ix = 0; ix < nDet; ix++) {
2470         if (min > detval[ix])
2471           min = detval[ix];
2472         else if (max < detval[ix])
2473           max = detval[ix];
2474       }
2475     }
2476     
2477     unsigned char* imageData = new unsigned char [nDet * nView * 3];
2478     double scale = (max - min) / 255;
2479     for (int iy2 = 0; iy2 < nView; iy2++) {
2480       const DetectorArray& detarray = rProj.getDetectorArray (iy2);
2481       const DetectorValue* detval = detarray.detValues();
2482       for (int ix = 0; ix < nDet; ix++) {
2483         int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
2484         intensity = clamp(intensity, 0, 255);
2485         int baseAddr = (iy2 * nDet + ix) * 3;
2486         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
2487       }
2488     }
2489     wxImage image (nDet, nView, imageData, true);
2490     m_bitmap = image.ConvertToBitmap();
2491     delete imageData;
2492     int xSize = nDet;
2493     int ySize = nView;
2494     xSize = clamp (xSize, 0, 800);
2495     ySize = clamp (ySize, 0, 800);
2496     m_pFrame->SetClientSize (xSize, ySize);
2497     m_pCanvas->SetScrollbars (20, 20, nDet/20, nView/20);
2498   }
2499   
2500   if (m_pCanvas)
2501     m_pCanvas->Refresh();
2502 }
2503
2504 bool 
2505 ProjectionFileView::OnClose (bool deleteWindow)
2506 {
2507   if (! GetDocument() || ! GetDocument()->Close())
2508     return false;
2509   
2510   if (m_pCanvas) {
2511     m_pCanvas->Clear();
2512     m_pCanvas->setView(NULL);
2513         m_pCanvas->Show(false);
2514     m_pCanvas = NULL;
2515   }
2516   wxString s(wxTheApp->GetAppName());
2517   if (m_pFrame)
2518     m_pFrame->SetTitle(s);
2519 //  SetFrame(NULL);
2520   
2521   Activate(false);
2522   
2523   if (deleteWindow) {
2524           m_pFrame->Show(false);
2525           m_pFrame->Destroy();
2526 //        delete m_pFrame;
2527     m_pFrame = NULL;
2528   }
2529   return true;
2530 }
2531
2532
2533
2534 // PlotFileCanvas
2535 PlotFileCanvas::PlotFileCanvas (PlotFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
2536 : wxScrolledWindow(frame, -1, pos, size, style)
2537 {
2538   m_pView = v;
2539 }
2540
2541 PlotFileCanvas::~PlotFileCanvas ()
2542 {
2543     m_pView = NULL;
2544 }
2545
2546 void 
2547 PlotFileCanvas::OnDraw(wxDC& dc)
2548 {
2549   if (m_pView)
2550     m_pView->OnDraw(& dc);
2551 }
2552
2553
2554 // PlotFileView
2555
2556 IMPLEMENT_DYNAMIC_CLASS(PlotFileView, wxView)
2557
2558 BEGIN_EVENT_TABLE(PlotFileView, wxView)
2559 EVT_MENU(PJMENU_FILE_PROPERTIES, PlotFileView::OnProperties)
2560 EVT_MENU(PLOTMENU_VIEW_SCALE_MINMAX, PlotFileView::OnScaleMinMax)
2561 EVT_MENU(PLOTMENU_VIEW_SCALE_AUTO, PlotFileView::OnScaleAuto)
2562 EVT_MENU(PLOTMENU_VIEW_SCALE_FULL, PlotFileView::OnScaleFull)
2563 END_EVENT_TABLE()
2564
2565 PlotFileView::PlotFileView() 
2566 : wxView(), m_pFrame(NULL), m_pCanvas(NULL), m_pEZPlot(NULL), m_pFileMenu(0), m_bMinSpecified(false), m_bMaxSpecified(false)
2567 {
2568 }
2569
2570 PlotFileView::~PlotFileView()
2571 {
2572   if (m_pEZPlot)
2573     delete m_pEZPlot;
2574
2575   theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);  
2576 }
2577
2578 void
2579 PlotFileView::OnProperties (wxCommandEvent& event)
2580 {
2581   const PlotFile& rPlot = GetDocument()->getPlotFile();
2582   std::ostringstream os;
2583   os << "Columns: " << rPlot.getNumColumns() << ", Records: " << rPlot.getNumRecords() << "\n";
2584   rPlot.printHeadersBrief (os);
2585   *theApp->getLog() << os.str().c_str();
2586   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Plot File Properties", wxOK | wxICON_INFORMATION);
2587   dialogMsg.ShowModal();
2588 }
2589
2590
2591 void 
2592 PlotFileView::OnScaleAuto (wxCommandEvent& event)
2593 {
2594   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2595   double min, max, mean, mode, median, stddev;
2596   rPlotFile.statistics (1, min, max, mean, mode, median, stddev);
2597   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
2598   int iRetVal = dialogAutoScale.ShowModal();
2599   if (iRetVal == wxID_OK) {
2600     m_bMinSpecified = true;
2601     m_bMaxSpecified = true;
2602     double dMin, dMax;
2603     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
2604       m_dMinPixel = dMin;
2605       m_dMaxPixel = dMax;
2606       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
2607       OnUpdate (this, NULL);
2608     }
2609   }
2610 }
2611
2612 void 
2613 PlotFileView::OnScaleMinMax (wxCommandEvent& event)
2614 {
2615   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2616   double min;
2617   double max;
2618   
2619   if (! m_bMinSpecified || ! m_bMaxSpecified) {
2620     if (! rPlotFile.getMinMax (1, min, max)) {
2621       *theApp->getLog() << "Error: unable to find Min/Max\n";
2622       return;
2623     }
2624   }
2625   
2626   if (m_bMinSpecified)
2627     min = m_dMinPixel;
2628   if (m_bMaxSpecified)
2629     max = m_dMaxPixel;
2630   
2631   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Y-axis Minimum & Maximum", min, max);
2632   int retVal = dialogMinMax.ShowModal();
2633   if (retVal == wxID_OK) {
2634     m_bMinSpecified = true;
2635     m_bMaxSpecified = true;
2636     m_dMinPixel = dialogMinMax.getMinimum();
2637     m_dMaxPixel = dialogMinMax.getMaximum();
2638     OnUpdate (this, NULL);
2639   }
2640 }
2641
2642 void 
2643 PlotFileView::OnScaleFull (wxCommandEvent& event)
2644 {
2645   if (m_bMinSpecified || m_bMaxSpecified) {
2646     m_bMinSpecified = false;
2647     m_bMaxSpecified = false;
2648     OnUpdate (this, NULL);
2649   }
2650 }
2651
2652
2653 PlotFileCanvas* 
2654 PlotFileView::CreateCanvas (wxView *view, wxFrame *parent)
2655 {
2656   PlotFileCanvas* pCanvas;
2657   int width, height;
2658   parent->GetClientSize(&width, &height);
2659   
2660   pCanvas = new PlotFileCanvas (dynamic_cast<PlotFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
2661   
2662   pCanvas->SetBackgroundColour(*wxWHITE);
2663   pCanvas->Clear();
2664   
2665   return pCanvas;
2666 }
2667
2668 #if CTSIM_MDI
2669 wxDocMDIChildFrame*
2670 #else
2671 wxDocChildFrame*
2672 #endif
2673 PlotFileView::CreateChildFrame(wxDocument *doc, wxView *view)
2674 {
2675 #ifdef CTSIM_MDI
2676   wxDocMDIChildFrame *subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2677 #else
2678   wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Plot Frame", wxPoint(10, 10), wxSize(500, 300), wxDEFAULT_FRAME_STYLE);
2679 #endif
2680   theApp->setIconForFrame (subframe);
2681   
2682   wxMenu *m_pFileMenu = new wxMenu;
2683   
2684   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2685   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2686   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2687   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2688   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2689   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2690   
2691   m_pFileMenu->AppendSeparator();
2692   m_pFileMenu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
2693   
2694   m_pFileMenu->AppendSeparator();
2695   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2696   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2697   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2698 #ifdef CTSIM_MDI
2699   m_pFileMenu->AppendSeparator();
2700   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2701 #endif
2702   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2703   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
2704   
2705   wxMenu *view_menu = new wxMenu;
2706   view_menu->Append(PLOTMENU_VIEW_SCALE_MINMAX, "Display Scale &Set...\tCtrl-E");
2707   view_menu->Append(PLOTMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
2708   view_menu->Append(PLOTMENU_VIEW_SCALE_FULL, "Display &Full Scale\tCtrl-U");
2709   
2710   wxMenu *help_menu = new wxMenu;
2711   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2712   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2713   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2714   
2715   wxMenuBar *menu_bar = new wxMenuBar;
2716   
2717   menu_bar->Append(m_pFileMenu, "&File");
2718   menu_bar->Append(view_menu, "&View");
2719   menu_bar->Append(help_menu, "&Help");
2720   
2721   subframe->SetMenuBar(menu_bar);
2722   subframe->Centre(wxBOTH);
2723   
2724   wxAcceleratorEntry accelEntries[10];
2725   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2726   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2727   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2728   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2729   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
2730   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
2731   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2732   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('E'), PLOTMENU_VIEW_SCALE_MINMAX);
2733   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('A'), PLOTMENU_VIEW_SCALE_AUTO);
2734   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('U'), PLOTMENU_VIEW_SCALE_FULL);
2735   wxAcceleratorTable accelTable (10, accelEntries);
2736   subframe->SetAcceleratorTable (accelTable);
2737   
2738   return subframe;
2739 }
2740
2741
2742 bool 
2743 PlotFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
2744 {
2745   m_pFrame = CreateChildFrame(doc, this);
2746   SetFrame(m_pFrame);
2747   
2748   m_bMinSpecified = false;
2749   m_bMaxSpecified = false;
2750   m_dAutoScaleFactor = 1.;
2751   
2752   int width, height;
2753   m_pFrame->GetClientSize(&width, &height);
2754   m_pFrame->SetTitle ("Plot File");
2755   m_pCanvas = CreateCanvas (this, m_pFrame);
2756   
2757 #ifdef __X__
2758   int x, y;  // X requires a forced resize
2759   m_pFrame->GetSize(&x, &y);
2760   m_pFrame->SetSize(-1, -1, x, y);
2761 #endif
2762   
2763   m_pFrame->Show(true);
2764   Activate(true);
2765   
2766   return true;
2767 }
2768
2769 void 
2770 PlotFileView::OnDraw (wxDC* dc)
2771 {
2772   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2773   const int iNColumns = rPlotFile.getNumColumns();
2774   const int iNRecords = rPlotFile.getNumRecords();
2775   
2776   if (iNColumns > 0 && iNRecords > 0) {
2777     int xsize, ysize;
2778     m_pCanvas->GetClientSize (&xsize, &ysize);
2779     SGPDriver driver (dc, xsize, ysize);
2780     SGP sgp (driver);
2781     if (m_pEZPlot)
2782       m_pEZPlot->plot (&sgp);
2783   }
2784 }
2785
2786
2787 void 
2788 PlotFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2789 {
2790   const PlotFile& rPlotFile = GetDocument()->getPlotFile();
2791   const int iNColumns = rPlotFile.getNumColumns();
2792   const int iNRecords = rPlotFile.getNumRecords();
2793   
2794   if (iNColumns > 0 && iNRecords > 0) {
2795     if (m_pEZPlot)
2796       delete m_pEZPlot;
2797     m_pEZPlot = new EZPlot;
2798     
2799     for (unsigned int iEzset = 0; iEzset < rPlotFile.getNumEzsetCommands(); iEzset++)
2800       m_pEZPlot->ezset (rPlotFile.getEzsetCommand (iEzset));
2801     
2802     if (m_bMinSpecified) {
2803       std::ostringstream os;
2804       os << "ymin " << m_dMinPixel;
2805       m_pEZPlot->ezset (os.str());
2806     }
2807     
2808     if (m_bMaxSpecified) {
2809       std::ostringstream os;
2810       os << "ymax " << m_dMaxPixel;
2811       m_pEZPlot->ezset (os.str());
2812     }
2813     
2814     m_pEZPlot->ezset("box");
2815     m_pEZPlot->ezset("grid");
2816     
2817     double* pdXaxis = new double [iNRecords];
2818     rPlotFile.getColumn (0, pdXaxis);
2819     
2820     double* pdY = new double [iNRecords];
2821     for (int iCol = 1; iCol < iNColumns; iCol++) {
2822       rPlotFile.getColumn (iCol, pdY);
2823       m_pEZPlot->addCurve (pdXaxis, pdY, iNRecords);
2824     }
2825     
2826     delete pdXaxis;
2827     delete pdY;
2828   }
2829   
2830   if (m_pCanvas)
2831     m_pCanvas->Refresh();
2832 }
2833
2834 bool 
2835 PlotFileView::OnClose (bool deleteWindow)
2836 {
2837   if (! GetDocument() || ! GetDocument()->Close())
2838     return false;
2839   
2840   if (m_pCanvas) {
2841     m_pCanvas->Clear();
2842     m_pCanvas->setView (NULL);
2843         m_pCanvas->Show(false);
2844     m_pCanvas = NULL;
2845   }
2846   wxString s(wxTheApp->GetAppName());
2847   if (m_pFrame)
2848     m_pFrame->SetTitle(s);
2849   SetFrame(NULL);
2850   
2851   Activate(false);
2852   
2853   if (deleteWindow) {
2854           m_pFrame->Show(false);
2855           m_pFrame->Destroy();
2856 //        delete m_pFrame;
2857     m_pFrame = NULL;
2858   }
2859   return true;
2860 }
2861
2862
2863 ////////////////////////////////////////////////////////////////
2864
2865
2866 IMPLEMENT_DYNAMIC_CLASS(TextFileView, wxView)
2867
2868 TextFileView::~TextFileView() 
2869 {
2870     theApp->getDocManager()->FileHistoryRemoveMenu (m_pFileMenu);
2871 }
2872
2873 bool TextFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
2874 {
2875   m_pFrame = CreateChildFrame(doc, this);
2876   SetFrame (m_pFrame);
2877   
2878   int width, height;
2879   m_pFrame->GetClientSize(&width, &height);
2880   m_pFrame->SetTitle("TextFile");
2881   m_pCanvas = new TextFileCanvas (this, m_pFrame, wxPoint(0, 0), wxSize(width, height), wxTE_MULTILINE | wxTE_READONLY);
2882   m_pFrame->SetTitle("Log");
2883   
2884 #ifdef __X__
2885   // X seems to require a forced resize
2886   int x, y;
2887   frame->GetSize(&x, &y);
2888   frame->SetSize(-1, -1, x, y);
2889 #endif
2890   
2891   m_pFrame->Show (true);
2892   Activate (true);
2893   
2894   return true;
2895 }
2896
2897 // Handled by wxTextWindow
2898 void TextFileView::OnDraw(wxDC *WXUNUSED(dc) )
2899 {
2900 }
2901
2902 void TextFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
2903 {
2904 }
2905
2906 bool 
2907 TextFileView::OnClose (bool deleteWindow)
2908 {
2909         if (m_pFrame && m_pFrame->GetTitle() == "Log")
2910        return false;
2911   
2912   if (! GetDocument() || ! GetDocument()->Close())
2913     return false;
2914
2915   m_pCanvas->Show(false);
2916   Activate(false);
2917   
2918   if (deleteWindow) {
2919           m_pFrame->Show(false);
2920           m_pFrame->Destroy();
2921 //        delete m_pFrame;
2922         m_pFrame = NULL;
2923    
2924   }
2925   return TRUE;
2926 }
2927
2928 #if CTSIM_MDI
2929 wxDocMDIChildFrame*
2930 #else
2931 wxDocChildFrame*
2932 #endif
2933 TextFileView::CreateChildFrame (wxDocument *doc, wxView *view)
2934 {
2935 #if CTSIM_MDI
2936   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(300, 150), wxDEFAULT_FRAME_STYLE);
2937 #else
2938   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "TextFile Frame", wxPoint(-1, -1), wxSize(300, 150), wxDEFAULT_FRAME_STYLE);
2939 #endif
2940   theApp->setIconForFrame (subframe);
2941   
2942   wxMenu *m_pFileMenu = new wxMenu;
2943   
2944   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
2945   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
2946   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
2947   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
2948   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
2949   //  m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
2950   
2951   m_pFileMenu->AppendSeparator();
2952   m_pFileMenu->Append(wxID_PRINT, "&Print...");
2953   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
2954   m_pFileMenu->Append(wxID_PREVIEW, "Print Pre&view");
2955 #ifdef CTSIM_MDI
2956   m_pFileMenu->AppendSeparator();
2957   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
2958 #endif
2959   theApp->getDocManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
2960   theApp->getDocManager()->FileHistoryUseMenu(m_pFileMenu);
2961   
2962   wxMenu *help_menu = new wxMenu;
2963   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
2964   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
2965   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
2966   
2967   wxMenuBar *menu_bar = new wxMenuBar;
2968   
2969   menu_bar->Append(m_pFileMenu, "&File");
2970   menu_bar->Append(help_menu, "&Help");
2971   
2972   subframe->SetMenuBar(menu_bar);
2973   subframe->Centre(wxBOTH);
2974   
2975   wxAcceleratorEntry accelEntries[5];
2976   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
2977   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
2978   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
2979   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
2980   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
2981   wxAcceleratorTable accelTable (5, accelEntries);
2982   subframe->SetAcceleratorTable (accelTable);
2983   
2984   return subframe;
2985 }
2986
2987
2988 // Define a constructor for my text subwindow
2989 TextFileCanvas::TextFileCanvas (TextFileView* v, wxFrame* frame, const wxPoint& pos, const wxSize& size, long style)
2990   : wxTextCtrl (frame, -1, "", pos, size, style), m_pView(v)
2991 {
2992 }
2993
2994 TextFileCanvas::~TextFileCanvas ()
2995 {
2996         m_pView = NULL;
2997 }