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